2019-06-07 10:05:15 +02:00
|
|
|
const BaseService = require('lib/services/BaseService.js');
|
|
|
|
const Mutex = require('async-mutex').Mutex;
|
|
|
|
|
|
|
|
class KvStore extends BaseService {
|
|
|
|
static instance() {
|
|
|
|
if (this.instance_) return this.instance_;
|
|
|
|
this.instance_ = new KvStore();
|
|
|
|
return this.instance_;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.incMutex_ = new Mutex();
|
|
|
|
}
|
|
|
|
|
|
|
|
setDb(v) {
|
|
|
|
this.db_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
db() {
|
|
|
|
if (!this.db_) throw new Error('Accessing DB before it has been set!');
|
|
|
|
return this.db_;
|
|
|
|
}
|
|
|
|
|
|
|
|
typeFromValue_(value) {
|
|
|
|
if (typeof value === 'string') return KvStore.TYPE_TEXT;
|
|
|
|
if (typeof value === 'number') return KvStore.TYPE_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
|
|
|
}
|
|
|
|
|
2019-06-08 00:11:08 +02:00
|
|
|
formatValues_(kvs) {
|
|
|
|
const output = [];
|
|
|
|
for (const kv of kvs) {
|
|
|
|
kv.value = this.formatValue_(kv.value, kv.type);
|
|
|
|
output.push(kv);
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:05:15 +02:00
|
|
|
formatValue_(value, type) {
|
|
|
|
if (type === KvStore.TYPE_INT) return Number(value);
|
2019-09-19 23:51:18 +02:00
|
|
|
if (type === KvStore.TYPE_TEXT) return `${value}`;
|
|
|
|
throw new Error(`Unknown type: ${type}`);
|
2019-06-07 10:05:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async value(key) {
|
|
|
|
const r = await this.db().selectOne('SELECT `value`, `type` FROM key_values WHERE `key` = ?', [key]);
|
|
|
|
if (!r) return null;
|
|
|
|
return this.formatValue_(r.value, r.type);
|
|
|
|
}
|
|
|
|
|
|
|
|
async setValue(key, value) {
|
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteValue(key) {
|
|
|
|
await this.db().exec('DELETE FROM key_values WHERE `key` = ?', [key]);
|
|
|
|
}
|
|
|
|
|
2019-06-08 00:11:08 +02:00
|
|
|
async clear() {
|
|
|
|
await this.db().exec('DELETE FROM key_values');
|
|
|
|
}
|
|
|
|
|
|
|
|
async all() {
|
|
|
|
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.
|
|
|
|
async incValue(key, inc = 1) {
|
|
|
|
const release = await this.incMutex_.acquire();
|
|
|
|
|
|
|
|
try {
|
|
|
|
const result = await this.db().selectOne('SELECT `value`, `type` FROM key_values WHERE `key` = ?', [key]);
|
|
|
|
const newValue = result ? this.formatValue_(result.value, result.type) + inc : inc;
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-08 00:11:08 +02:00
|
|
|
async searchByPrefix(prefix) {
|
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);
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:05:15 +02:00
|
|
|
async countKeys() {
|
|
|
|
const r = await this.db().selectOne('SELECT count(*) as total FROM key_values');
|
|
|
|
return r.total ? r.total : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KvStore.TYPE_INT = 1;
|
|
|
|
KvStore.TYPE_TEXT = 2;
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = KvStore;
|