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

Fix: SQLite causes crash when trying to merge undefined #334

Merged
Merged
Show file tree
Hide file tree
Changes from 7 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
21 changes: 18 additions & 3 deletions lib/Onyx.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import fastMerge from './fastMerge';
import * as PerformanceUtils from './metrics/PerformanceUtils';
import Storage from './storage';
import DevTools from './DevTools';
import Utils from './utils';

// Method constants
const METHOD = {
Expand Down Expand Up @@ -962,7 +963,7 @@ function removeNullObjectValues(value) {
* @returns {Promise}
*/
function set(key, value) {
if (_.isNull(value)) {
if (_.isUndefined(value) || _.isNull(value)) {
return remove(key);
}

Expand Down Expand Up @@ -1044,11 +1045,21 @@ function applyMerge(existingValue, changes) {
return lastChange;
}

if (_.isObject(existingValue) || _.every(changes, _.isObject)) {
if (_.isObject(existingValue) || _.isObject(lastChange)) {
// We want to merge multiple object delates together
chrispader marked this conversation as resolved.
Show resolved Hide resolved
// If all of the changes are objects, we can simply merge all of them together
// If the batched changes contain nullish values or arrays, we should only merge the very last objects in the batch
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please expand this comment to explain why only the last objects in the batch should be merged? I'm not sure I understand the scenario or what nullish and array values have to do with it.

Copy link
Contributor

Choose a reason for hiding this comment

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

+1
May be we want to merge the very last value in the batch - irrespective of if it is null or not null

let numberOfObjectsAtEndOfArray;
if (_.every(changes, _.isObject)) {
numberOfObjectsAtEndOfArray = changes.length;
} else {
numberOfObjectsAtEndOfArray = Utils.countObjectsAtEndOfArray(changes);
}

// Object values are merged one after the other
// lodash adds a small overhead so we don't use it here
// eslint-disable-next-line prefer-object-spread, rulesdir/prefer-underscore-method
return _.reduce(changes, (modifiedData, change) => fastMerge(modifiedData, change),
return _.reduce(changes.slice(-numberOfObjectsAtEndOfArray), (modifiedData, change) => fastMerge(modifiedData, change),
existingValue || {});
}

Expand Down Expand Up @@ -1092,6 +1103,10 @@ function merge(key, changes) {
// We first only merge the changes, so we can provide these to the native implementation (SQLite uses only delta changes in "JSON_PATCH" to merge)
let batchedChanges = applyMerge(undefined, mergeQueue[key]);

if (_.isUndefined(batchedChanges) || _.isNull(batchedChanges)) {
return remove(key);
}

// Clean up the write queue so we
// don't apply these changes again
delete mergeQueue[key];
Expand Down
6 changes: 4 additions & 2 deletions lib/storage/providers/SQLiteStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,12 @@ const provider = {
ON CONFLICT DO UPDATE
SET valueJSON = JSON_PATCH(valueJSON, JSON(:value));
`;

const queryArguments = _.map(pairs, (pair) => {
const value = JSON.stringify(pair[1]);
return [pair[0], value];
const value = pair[1];
chrispader marked this conversation as resolved.
Show resolved Hide resolved
return [pair[0], JSON.stringify(value)];
});

return db.executeBatchAsync([[query, queryArguments]]);
},

Expand Down
13 changes: 13 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function countObjectsAtEndOfArray(array) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not quite getting this... what about objects at the beginning or the middle of the array?

let count = 0;
for (let i = array.length - 1; i >= 0; i--) {
if (typeof array[i] === 'object' && array[i] !== null) {
count++;
} else {
break;
}
}
return count;
}

export default {countObjectsAtEndOfArray};
Loading