2020-11-08 03:08:33 +02:00
|
|
|
import BaseService from './BaseService';
|
2019-06-07 10:05:15 +02:00
|
|
|
const Mutex = require('async-mutex').Mutex;
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
enum ValueType {
|
|
|
|
Int = 1,
|
|
|
|
Text = 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class KvStore extends BaseService {
|
|
|
|
|
|
|
|
private incMutex_:any = null
|
|
|
|
private db_:any = null;
|
|
|
|
|
|
|
|
private static instance_:KvStore = null;
|
|
|
|
|
|
|
|
public static instance() {
|
2019-06-07 10:05:15 +02:00
|
|
|
if (this.instance_) return this.instance_;
|
|
|
|
this.instance_ = new KvStore();
|
|
|
|
return this.instance_;
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public static destroyInstance() {
|
|
|
|
this.instance_ = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private constructor() {
|
2019-06-07 10:05:15 +02:00
|
|
|
super();
|
|
|
|
this.incMutex_ = new Mutex();
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public setDb(v:any) {
|
2019-06-07 10:05:15 +02:00
|
|
|
this.db_ = v;
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
private db() {
|
2019-06-07 10:05:15 +02:00
|
|
|
if (!this.db_) throw new Error('Accessing DB before it has been set!');
|
|
|
|
return this.db_;
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
private typeFromValue_(value:any) {
|
|
|
|
if (typeof value === 'string') return ValueType.Text;
|
|
|
|
if (typeof value === 'number') return ValueType.Int;
|
2019-09-19 23:51:18 +02:00
|
|
|
throw new Error(`Unsupported value type: ${typeof value}`);
|
2019-06-07 10:05:15 +02:00
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
private formatValues_(kvs:any[]) {
|
2019-06-08 00:11:08 +02:00
|
|
|
const output = [];
|
|
|
|
for (const kv of kvs) {
|
|
|
|
kv.value = this.formatValue_(kv.value, kv.type);
|
|
|
|
output.push(kv);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
private formatValue_(value:any, type:ValueType):string | number {
|
|
|
|
if (type === ValueType.Int) return Number(value);
|
|
|
|
if (type === ValueType.Text) return `${value}`;
|
2019-09-19 23:51:18 +02:00
|
|
|
throw new Error(`Unknown type: ${type}`);
|
2019-06-07 10:05:15 +02:00
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public async value<T>(key:string):Promise<T> {
|
2019-06-07 10:05:15 +02:00
|
|
|
const r = await this.db().selectOne('SELECT `value`, `type` FROM key_values WHERE `key` = ?', [key]);
|
|
|
|
if (!r) return null;
|
2020-11-08 03:08:33 +02:00
|
|
|
return this.formatValue_(r.value, r.type) as any;
|
2019-06-07 10:05:15 +02:00
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public async setValue(key:string, value:any) {
|
2019-06-07 10:05:15 +02:00
|
|
|
const t = Date.now();
|
|
|
|
await this.db().exec('INSERT OR REPLACE INTO key_values (`key`, `value`, `type`, `updated_time`) VALUES (?, ?, ?, ?)', [key, value, this.typeFromValue_(value), t]);
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public async deleteValue(key:string) {
|
2019-06-07 10:05:15 +02:00
|
|
|
await this.db().exec('DELETE FROM key_values WHERE `key` = ?', [key]);
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public async deleteByPrefix(prefix:string) {
|
2020-04-08 19:02:31 +02:00
|
|
|
await this.db().exec('DELETE FROM key_values WHERE `key` LIKE ?', [`${prefix}%`]);
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public async clear() {
|
2019-06-08 00:11:08 +02:00
|
|
|
await this.db().exec('DELETE FROM key_values');
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public async all() {
|
2019-06-08 00:11:08 +02:00
|
|
|
return this.formatValues_(await this.db().selectAll('SELECT * FROM key_values'));
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:05:15 +02:00
|
|
|
// Note: atomicity is done at application level so two difference instances
|
|
|
|
// accessing the db at the same time could mess up the increment.
|
2020-11-08 03:08:33 +02:00
|
|
|
public async incValue(key:string, inc:number = 1) {
|
2019-06-07 10:05:15 +02:00
|
|
|
const release = await this.incMutex_.acquire();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await this.db().selectOne('SELECT `value`, `type` FROM key_values WHERE `key` = ?', [key]);
|
2020-11-08 03:08:33 +02:00
|
|
|
const value = this.formatValue_(result.value, result.type) as ValueType.Int;
|
|
|
|
const newValue = result ? value + inc : inc;
|
2019-06-07 10:05:15 +02:00
|
|
|
await this.setValue(key, newValue);
|
|
|
|
release();
|
|
|
|
return newValue;
|
|
|
|
} catch (error) {
|
|
|
|
release();
|
|
|
|
throw error;
|
2019-07-29 15:43:53 +02:00
|
|
|
}
|
2019-06-07 10:05:15 +02:00
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public async searchByPrefix(prefix:string) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const results = await this.db().selectAll('SELECT `key`, `value`, `type` FROM key_values WHERE `key` LIKE ?', [`${prefix}%`]);
|
2019-06-08 00:11:08 +02:00
|
|
|
return this.formatValues_(results);
|
|
|
|
}
|
|
|
|
|
2020-11-08 03:08:33 +02:00
|
|
|
public async countKeys() {
|
2019-06-07 10:05:15 +02:00
|
|
|
const r = await this.db().selectOne('SELECT count(*) as total FROM key_values');
|
|
|
|
return r.total ? r.total : 0;
|
|
|
|
}
|
|
|
|
}
|