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

Bugfix/issue #5605 corrupt keys containing square bracket #5607

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# RxDB Changelog

<!-- CHANGELOG NEWEST -->
- FIX key-compression broken on keys with brackets [#5605](https://github.com/pubkey/rxdb/issues/5605)

<!-- ADD new changes here! -->

Expand Down
47 changes: 47 additions & 0 deletions orga/performance-trackings.md
Original file line number Diff line number Diff line change
Expand Up @@ -1865,3 +1865,50 @@ AFTER (batchSize 1000):
"time-to-first-insert": 1020,
"insert-documents-1200": 32.3
}'



## key-compression improvements (feb 2024)

BEFORE:

{
"notice": "times are in milliseconds",
"createCompressionTable": {
"amount": 10000,
"total": 87.29935099929571,
"perInstance": 0.008729935099929571
},
"compress": {
"amount": 10000,
"total": 87.45416999980807,
"perObject": 0.008745416999980807
},
"decompress": {
"amount": 10000,
"total": 121.86422900017351,
"perObject": 0.012186422900017351
}
}


AFTER:

{
"notice": "times are in milliseconds",
"createCompressionTable": {
"amount": 10000,
"total": 89.79377999994904,
"perInstance": 0.008979377999994903
},
"compress": {
"amount": 10000,
"total": 81.2287579998374,
"perObject": 0.00812287579998374
},
"decompress": {
"amount": 10000,
"total": 118.99220599979162,
"perObject": 0.011899220599979162
}
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@
"is-my-json-valid": "2.20.6",
"isomorphic-ws": "5.0.0",
"js-base64": "3.7.6",
"jsonschema-key-compression": "1.6.1",
"jsonschema-key-compression": "1.7.0",
"lokijs": "1.5.12",
"mingo": "6.4.10",
"mongodb": "6.3.0",
Expand Down Expand Up @@ -574,4 +574,4 @@
"webpack-cli": "5.1.4",
"webpack-dev-server": "4.15.1"
}
}
}
3 changes: 2 additions & 1 deletion src/plugins/key-compression/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ export function wrappedKeyCompressionStorage<Internals, InstanceCreationOptions>

const compressionState = getCompressionStateByRxJsonSchema(params.schema);
function modifyToStorage(docData: RxDocumentWriteData<RxDocType>) {
return compressDocumentData(compressionState, docData);
const ret = compressDocumentData(compressionState, docData);
return ret;
}
function modifyFromStorage(docData: RxDocumentData<any>): Promise<RxDocumentData<RxDocType>> {
return decompressDocumentData(compressionState, docData);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/bug-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('bug-report.test.js', () => {


// you can also wait for events
const emitted = [];
const emitted: any[] = [];
const sub = collectionInOtherTab.mycollection
.findOne().$
.subscribe(doc => {
Expand Down
76 changes: 76 additions & 0 deletions test/unit/key-compression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import {
RxStorageReplicationMeta,
FilledMangoQuery,
prepareQuery,
toTypedRxJsonSchema,
ExtractDocumentTypeFromTypedRxJsonSchema,
} from '../../plugins/core/index.mjs';
import {
wrappedKeyCompressionStorage
Expand Down Expand Up @@ -399,5 +401,79 @@ describeParallel('key-compression.test.js', () => {
assert.strictEqual(counts, 1);
col.database.destroy();
});
it('#5603 corrupt keys containing square brackets', async () => {
const mySchema = {
version: 0,
primaryKey: 'passportId',
type: 'object',
keyCompression: true,
properties: {
passportId: {
type: 'string',
maxLength: 100
},
tags: {
type: 'object',
patternProperties: {
'.*': {
properties: {
name: { type: 'string' },
},
required: ['name'],
}
},
}
}
} as const;
const schemaTyped = toTypedRxJsonSchema(mySchema);
type TestDocType = ExtractDocumentTypeFromTypedRxJsonSchema<typeof schemaTyped>;

const db = await createRxDatabase<{ mycollection: RxCollection<TestDocType>; }>({
name: randomCouchString(10),
/**
* By calling config.storage.getStorage(),
* we can ensure that all variations of RxStorage are tested in the CI.
*/
storage: wrappedKeyCompressionStorage({ storage: config.storage.getStorage() }),
eventReduce: true,
ignoreDuplicate: true
});
await db.addCollections({
mycollection: {
schema: mySchema
},
});
const collection = db.mycollection;
let myDocument = await collection.insert({
passportId: 'foobar',
tags: {
example: 'example',
}
});

assert.strictEqual((myDocument.tags as any).example, 'example');

await myDocument.incrementalModify((docData) => {
const newDocData = Object.assign({}, docData);
(newDocData.tags as any)['[example2]'] = '[example2]';
return newDocData;
});
const expectedTags = {
example: 'example',
'[example2]': '[example2]',
};

// check on plain storage
const storageResults = await collection.storageInstance.findDocumentsById([myDocument.primary], false);
const storageDoc = ensureNotFalsy(storageResults[0]);
assert.deepStrictEqual(storageDoc.tags, expectedTags);

// check on the RxDocument
myDocument = myDocument.getLatest();
const tags = myDocument.toJSON().tags;
assert.deepEqual(tags, expectedTags);

db.destroy();
});
});
});
Loading