Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: use idb-keyval #293

Merged
merged 5 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/storage/WebStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* data changes and then stay up-to-date with everything happening in Onyx.
*/
import _ from 'underscore';
import Storage from './providers/LocalForage';
import Storage from './providers/IDBKeyVal';
hannojg marked this conversation as resolved.
Show resolved Hide resolved

const SYNC_ONYX = 'SYNC_ONYX';

Expand Down
111 changes: 111 additions & 0 deletions lib/storage/providers/IDBKeyVal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
set,
keys,
getMany,
setMany,
get,
clear,
del,
delMany,
createStore,
promisifyRequest,
} from 'idb-keyval';
import _ from 'underscore';
import fastMerge from '../../fastMerge';

// Same config as localforage, so we can swap the providers easily
const customStore = createStore('OnyxDB', 'keyvaluepairs');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

confirming: This will have the existing data from the current DB previously set up by localforage?

Copy link
Contributor Author

@hannojg hannojg Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes exactly 😊 It can just be replaced in place


const provider = {
/**
* Sets the value for a given key. The only requirement is that the value should be serializable to JSON string
* @param {String} key
* @param {*} value
* @return {Promise<void>}
*/
setItem: (key, value) => set(key, value, customStore),

/**
* Get multiple key-value pairs for the give array of keys in a batch.
* This is optimized to use only one database transaction.
* @param {String[]} keysParam
* @return {Promise<Array<[key, value]>>}
*/
multiGet: keysParam => getMany(keysParam, customStore)
.then(values => _.map(values, (value, index) => [keysParam[index], value])),

/**
* Multiple merging of existing and new values in a batch
* @param {Array<[key, value]>} pairs
* @return {Promise<void>}
*/
multiMerge: pairs => customStore('readwrite', (store) => {
// Note: we are using the manual store transaction here, to fit the read and update
// of the items in one transaction to achieve best performance.

const getValues = Promise.all(_.map(pairs, ([key]) => promisifyRequest(store.get(key))));

return getValues.then((values) => {
const upsertMany = _.map(pairs, ([key, value], index) => {
const prev = values[index];
const newValue = _.isObject(prev) ? fastMerge(prev, value) : value;
return promisifyRequest(store.put(newValue, key));
});
return Promise.all(upsertMany);
});
}),

/**
* Merging an existing value with a new one
* @param {String} key
* @param {any} _changes - not used, as we rely on the pre-merged data from the `modifiedData`
* @param {any} modifiedData - the pre-merged data from `Onyx.applyMerge`
* @return {Promise<void>}
*/
mergeItem(key, _changes, modifiedData) {
return provider.multiMerge([[key, modifiedData]]);
},

/**
* Stores multiple key-value pairs in a batch
* @param {Array<[key, value]>} pairs
* @return {Promise<void>}
*/
multiSet: pairs => setMany(pairs, customStore),

/**
* Clear everything from storage and also stops the SyncQueue from adding anything more to storage
* @returns {Promise<void>}
*/
clear: () => clear(customStore),

/**
* Returns all keys available in storage
* @returns {Promise<String[]>}
*/
getAllKeys: () => keys(customStore),

/**
* Get the value of a given key or return `null` if it's not available in storage
* @param {String} key
* @return {Promise<*>}
*/
getItem: key => get(key, customStore),

/**
* Remove given key and it's value from storage
* @param {String} key
* @returns {Promise<void>}
*/
removeItem: key => del(key, customStore),

/**
* Remove given keys and their values from storage
*
* @param {Array} keysParam
* @returns {Promise}
*/
removeItems: keysParam => delMany(keysParam, customStore),
};

export default provider;
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"eslint-config-expensify": "^2.0.38",
"eslint-plugin-jsx-a11y": "^6.6.1",
"eslint-plugin-react": "^7.31.10",
"idb-keyval": "^6.2.1",
"jest": "^26.5.2",
"jest-cli": "^26.5.2",
"jsdoc-to-markdown": "^7.1.0",
Expand All @@ -73,13 +74,17 @@
"webpack-merge": "^5.8.0"
},
"peerDependencies": {
"idb-keyval": "^6.2.1",
"localforage": "^1.10.0",
"localforage-removeitems": "^1.4.0",
"react": ">=18.1.0",
"react-native-performance": "^4.0.0",
"react-native-quick-sqlite": "^8.0.0-beta.2"
},
"peerDependenciesMeta": {
"idb-keyval": {
"optional": true
},
"react-native-performance": {
"optional": true
},
Expand Down