Skip to content

Commit

Permalink
FIX wrong-index-generation-on-some-number-fields (pubkey#4605)
Browse files Browse the repository at this point in the history
* TRY fix

* FIX

* FIX logs
  • Loading branch information
pubkey authored Mar 31, 2023
1 parent 98178e2 commit 6c5337c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# RxDB Changelog

<!-- CHANGELOG NEWEST -->

- FIX wrong index generation on some number fields that do not have decimals.
<!-- ADD new changes here! -->

<!-- /CHANGELOG NEWEST -->
Expand Down
13 changes: 10 additions & 3 deletions docs-src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@
</div>
</a>

<div class="right" style="display: flex;">
<div
class="right"
style="display: flex;"
>
<a
href="/premium.html"
target="_blank"
Expand Down Expand Up @@ -141,8 +144,9 @@ <h2>
<li>Realtime Queries</li>
<li>Realtime Replication</li>
<li>Works Offline</li>
<li>Great performance</li>
<li>Supports all major JavaScript runtimes</li>
<li>Fast as F<b>*</b>ck</li>
<li>Easy to learn</li>
</ul>
<!--
<div class="text">
Expand Down Expand Up @@ -973,7 +977,10 @@ <h2>
<div class="clear"></div>

<footer>
<a href="./imprint.html" target="_blank">Imprint</a>
<a
href="./imprint.html"
target="_blank"
>Imprint</a>
</footer>
</body>

Expand Down
32 changes: 28 additions & 4 deletions src/custom-index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ export function getIndexableStringMonad<RxDocType>(
} else {
// is number
const parsedLengths = props.parsedLengths as ParsedLengths;
if (!fieldValue) {
fieldValue = 0;
}
str += getNumberIndexString(
parsedLengths,
fieldValue
Expand All @@ -118,7 +115,11 @@ export function getIndexableStringMonad<RxDocType>(
return ret;
}



declare type ParsedLengths = {
minimum: number;
maximum: number;
nonDecimals: number;
decimals: number;
roundedMinimum: number;
Expand All @@ -139,6 +140,8 @@ export function getStringLengthOfIndexNumber(
decimals = multipleOfParts[1].length;
}
return {
minimum,
maximum,
nonDecimals,
decimals,
roundedMinimum: minimum
Expand Down Expand Up @@ -183,14 +186,35 @@ export function getNumberIndexString(
parsedLengths: ParsedLengths,
fieldValue: number
): string {
/**
* Ensure that the given value is in the boundaries
* of the schema, otherwise it would create a broken index string.
* This can happen for example if you have a minimum of 0
* and run a query like
* selector {
* numField: { $gt: -1000 }
* }
*/
if (typeof fieldValue === 'undefined') {
fieldValue = 0;
}
if (fieldValue < parsedLengths.minimum) {
fieldValue = parsedLengths.minimum;
}
if (fieldValue > parsedLengths.maximum) {
fieldValue = parsedLengths.maximum;
}

let str: string = '';
const nonDecimalsValueAsString = (Math.floor(fieldValue) - parsedLengths.roundedMinimum).toString();
str += nonDecimalsValueAsString.padStart(parsedLengths.nonDecimals, '0');

const splitByDecimalPoint = fieldValue.toString().split('.');
const decimalValueAsString = splitByDecimalPoint.length > 1 ? splitByDecimalPoint[1] : '0';

str += decimalValueAsString.padEnd(parsedLengths.decimals, '0');
if (parsedLengths.decimals > 0) {
str += decimalValueAsString.padEnd(parsedLengths.decimals, '0');
}
return str;
}

Expand Down
4 changes: 4 additions & 0 deletions src/plugins/utils/utils-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,7 @@ export function sumNumberArray(array: number[]): number {
}
return count;
}

export function maxOfNumbers(arr: number[]): number {
return Math.max(...arr);
}
61 changes: 61 additions & 0 deletions test/unit/custom-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,67 @@ config.parallel('custom-index.test.ts', () => {
assert.strictEqual(indexString.length, length);
});
});
it('issue did not work with this numberValue', () => {
const testIndex = ['numberValue', 'key'];
const testSchema: RxJsonSchema<RxDocumentData<any>> = fillWithDefaultSettings({
version: 0,
type: 'object',
primaryKey: 'key',
properties: {
key: {
type: 'string',
maxLength: 50
},
numberValue: {
type: 'number',
minimum: -10,
maximum: 1000,
multipleOf: 1
}
},
required: [
'key',
'numberValue'
],
indexes: [
testIndex
],
additionalProperties: false
});
const length = getIndexStringLength(
testSchema,
testIndex
);
const testDocs: RxDocumentData<any>[] = [
{
id: randomString(10),
numberValue: 17,
_deleted: false,
_attachments: {},
_meta: {
lwt: new Date().getTime()
},
_rev: EXAMPLE_REVISION_1
},
{
id: randomString(10),
numberValue: -5,
_deleted: false,
_attachments: {},
_meta: {
lwt: new Date().getTime()
},
_rev: EXAMPLE_REVISION_1
}
];
testDocs.forEach(docData => {
const indexString = getIndexableStringMonad(
testSchema,
testIndex
)(docData);
assert.strictEqual(indexString.length, length);
});
});
});
describe('.getPrimaryKeyFromIndexableString()', () => {
it('get the correct id', () => {
Expand Down

0 comments on commit 6c5337c

Please sign in to comment.