mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Added simple Key-Value store to support temporary data
This commit is contained in:
parent
b5b228af15
commit
de5fdc84f8
@ -36,6 +36,7 @@ npm test tests-build/models_Setting.js
|
||||
npm test tests-build/models_Tag.js
|
||||
npm test tests-build/pathUtils.js
|
||||
npm test tests-build/services_InteropService.js
|
||||
npm test tests-build/services_KvStore.js
|
||||
npm test tests-build/services_ResourceService.js
|
||||
npm test tests-build/services_rest_Api.js
|
||||
npm test tests-build/services_SearchEngine.js
|
||||
|
94
CliClient/tests/services_KvStore.js
Normal file
94
CliClient/tests/services_KvStore.js
Normal file
@ -0,0 +1,94 @@
|
||||
require('app-module-path').addPath(__dirname);
|
||||
|
||||
const { asyncTest, fileContentEqual, setupDatabase, revisionService, setupDatabaseAndSynchronizer, db, synchronizer, fileApi, sleep, clearDatabase, switchClient, syncTargetId, objectsEqual, checkThrowAsync } = require('test-utils.js');
|
||||
const KvStore = require('lib/services/KvStore.js');
|
||||
|
||||
process.on('unhandledRejection', (reason, p) => {
|
||||
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
|
||||
});
|
||||
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
|
||||
|
||||
function setupStore() {
|
||||
const store = KvStore.instance();
|
||||
store.setDb(db());
|
||||
return store;
|
||||
}
|
||||
|
||||
describe('services_KvStore', function() {
|
||||
|
||||
beforeEach(async (done) => {
|
||||
await setupDatabaseAndSynchronizer(1);
|
||||
await switchClient(1);
|
||||
done();
|
||||
});
|
||||
|
||||
it('should set and get values', asyncTest(async () => {
|
||||
const store = setupStore();
|
||||
await store.setValue('a', 123);
|
||||
expect(await store.value('a')).toBe(123);
|
||||
|
||||
await store.setValue('a', 123);
|
||||
expect(await store.countKeys()).toBe(1);
|
||||
expect(await store.value('a')).toBe(123);
|
||||
|
||||
await store.setValue('a', 456);
|
||||
expect(await store.countKeys()).toBe(1);
|
||||
expect(await store.value('a')).toBe(456);
|
||||
|
||||
await store.setValue('b', 789);
|
||||
expect(await store.countKeys()).toBe(2);
|
||||
expect(await store.value('a')).toBe(456);
|
||||
expect(await store.value('b')).toBe(789);
|
||||
}));
|
||||
|
||||
it('should set and get values with the right type', asyncTest(async () => {
|
||||
const store = setupStore();
|
||||
await store.setValue('string', 'something');
|
||||
await store.setValue('int', 123);
|
||||
expect(await store.value('string')).toBe('something');
|
||||
expect(await store.value('int')).toBe(123);
|
||||
}));
|
||||
|
||||
it('should increment values', asyncTest(async () => {
|
||||
const store = setupStore();
|
||||
await store.setValue('int', 1);
|
||||
const newValue = await store.incValue('int');
|
||||
|
||||
expect(newValue).toBe(2);
|
||||
expect(await store.value('int')).toBe(2);
|
||||
|
||||
expect(await store.incValue('int2')).toBe(1);
|
||||
expect(await store.countKeys()).toBe(2);
|
||||
}));
|
||||
|
||||
it('should handle non-existent values', asyncTest(async () => {
|
||||
const store = setupStore();
|
||||
expect(await store.value('nope')).toBe(null);
|
||||
}));
|
||||
|
||||
it('should delete values', asyncTest(async () => {
|
||||
const store = setupStore();
|
||||
await store.setValue('int', 1);
|
||||
expect(await store.countKeys()).toBe(1);
|
||||
await store.deleteValue('int');
|
||||
expect(await store.countKeys()).toBe(0);
|
||||
|
||||
await store.deleteValue('int'); // That should not throw
|
||||
}));
|
||||
|
||||
it('should increment in an atomic way', asyncTest(async () => {
|
||||
const store = setupStore();
|
||||
await store.setValue('int', 0);
|
||||
|
||||
const promises = [];
|
||||
for (let i = 0; i < 20; i++) {
|
||||
promises.push(store.incValue('int'));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
expect(await store.value('int')).toBe(20);
|
||||
}));
|
||||
|
||||
});
|
@ -154,6 +154,7 @@ async function clearDatabase(id = null) {
|
||||
'sync_items',
|
||||
'notes_normalized',
|
||||
'revisions',
|
||||
'key_values',
|
||||
];
|
||||
|
||||
const queries = [];
|
||||
|
@ -292,7 +292,7 @@ class JoplinDatabase extends Database {
|
||||
// must be set in the synchronizer too.
|
||||
|
||||
// Note: v16 and v17 don't do anything. They were used to debug an issue.
|
||||
const existingDatabaseVersions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22];
|
||||
const existingDatabaseVersions = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23];
|
||||
|
||||
let currentVersionIndex = existingDatabaseVersions.indexOf(fromVersion);
|
||||
|
||||
@ -608,6 +608,21 @@ class JoplinDatabase extends Database {
|
||||
queries.push('CREATE INDEX resources_to_download_updated_time ON resources_to_download (updated_time)');
|
||||
}
|
||||
|
||||
if (targetVersion == 23) {
|
||||
const newTableSql = `
|
||||
CREATE TABLE key_values (
|
||||
id INTEGER PRIMARY KEY,
|
||||
\`key\` TEXT NOT NULL,
|
||||
\`value\` TEXT NOT NULL,
|
||||
\`type\` INT NOT NULL,
|
||||
updated_time INT NOT NULL
|
||||
);
|
||||
`;
|
||||
queries.push(this.sqlStringToLines(newTableSql)[0]);
|
||||
|
||||
queries.push('CREATE UNIQUE INDEX key_values_key ON key_values (key)');
|
||||
}
|
||||
|
||||
queries.push({ sql: 'UPDATE version SET version = ?', params: [targetVersion] });
|
||||
|
||||
try {
|
||||
|
80
ReactNativeClient/lib/services/KvStore.js
Normal file
80
ReactNativeClient/lib/services/KvStore.js
Normal file
@ -0,0 +1,80 @@
|
||||
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;
|
||||
throw new Error('Unsupported value type: ' + (typeof value));
|
||||
}
|
||||
|
||||
formatValue_(value, type) {
|
||||
if (type === KvStore.TYPE_INT) return Number(value);
|
||||
if (type === KvStore.TYPE_TEXT) return value + '';
|
||||
throw new Error('Unknown type: ' + type);
|
||||
}
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
module.exports = KvStore;
|
Loading…
Reference in New Issue
Block a user