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

Iterator does not work correctly when the supplied range is not a key #2

Closed
chrbala opened this issue Feb 18, 2018 · 1 comment
Closed

Comments

@chrbala
Copy link
Owner

chrbala commented Feb 18, 2018

The iterator currently starts with a key when getting the initial value to iterate through. This does not work if the range is defined by values that are not keys.

lmdb-leveldown/src/index.js

Lines 144 to 168 in 5d34800

const getInitialCurrentValue = (
{ gt, gte, lt, lte, reverse }: IteratorOptionsType,
cursor: *
) => {
const invalid = (a: string, b: string) =>
`${a} can not be provided with ${b}`;
if (gt) {
if (gte) throw new Error(invalid('gt', 'gte'));
if (reverse) return cursor.goToLast();
cursor.goToKey(gt);
return cursor.goToNext();
} else if (gte) return cursor.goToKey(gte);
else if (lt) {
if (lte) throw new Error(invalid('lt', 'lte'));
if (reverse) {
cursor.goToKey(lt);
return cursor.goToPrev();
}
return cursor.goToFirst();
} else if (lte) return reverse ? cursor.goToKey(lte) : cursor.goToFirst();
return reverse ? cursor.goToLast() : cursor.goToFirst();
};

Example test output:

# test iterator with gte=49.5 (midway)
ok 200 undefined
not ok 201 correct number of entries
  ---
    operator: equal
    expected: 50
    actual:   0
  ...
not ok 202 should be equivalent
  ---
    operator: deepEqual
    expected: |-
      [ { key: '50', value: '0.9926293454942214' }, { key: '51', value: '0.36530754007971344' }, { key: '52', value: '0.07948485238433345' }, { key: '53', value: '0.4460314320292509' }, { key: '54', value: '0.6563670401821771' }, { key: '55', value: '0.7778772324231' }, { key: '56', value: '0.6401857734726797' }, { key: '57', value: '0.8552353376818715' }, { key: '58', value: '0.5319237025637298' }, { key: '59', value: '0.5568737819443985' }, { key: '60', value: '0.9624005486051628' }, { key: '61', value: '0.42510185325347893' }, { key: '62', value: '0.11216823705999612' }, { key: '63', value: '0.12620420026129908' }, { key: '64', value: '0.49373167690324027' }, { key: '65', value: '0.030064742250546006' }, { key: '66', value: '0.9132791307734551' }, { key: '67', value: '0.3480505763258064' }, { key: '68', value: '0.10233997012150153' }, { key: '69', value: '0.5664507517525017' }, { key: '70', value: '0.8025281173721805' }, { key: '71', value: '0.5920517761246549' }, { key: '72', value: '0.7022464461638869' }, { key: '73', value: '0.10957992173264319' }, { key: '74', value: '0.7761877436457496' }, { key: '75', value: '0.5781361569669583' }, { key: '76', value: '0.3111166336007185' }, { key: '77', value: '0.4878780537177789' }, { key: '78', value: '0.12655914496335186' }, { key: '79', value: '0.38618150055929523' }, { key: '80', value: '0.31609012678322257' }, { key: '81', value: '0.6589613745332377' }, { key: '82', value: '0.6048494277682703' }, { key: '83', value: '0.2066701342922257' }, { key: '84', value: '0.0018538658409072983' }, { key: '85', value: '0.8492683128917726' }, { key: '86', value: '0.0848514102379645' }, { key: '87', value: '0.9711296854693521' }, { key: '88', value: '0.49853902528205385' }, { key: '89', value: '0.9740776483919469' }, { key: '90', value: '0.6530275273713964' }, { key: '91', value: '0.25511761710292546' }, { key: '92', value: '0.28199455595791534' }, { key: '93', value: '0.57805842900936' }, { key: '94', value: '0.20249379034314852' }, { key: '95', value: '0.3205214395313847' }, { key: '96', value: '0.15968226699990007' }, { key: '97', value: '0.6626543905212701' }, { key: '98', value: '0.08386572138126924' }, { key: '99', value: '0.8048711309013743' } ]
    actual: |-
      []
    stack: |-
      Error: should be equivalent
          at Test.assert [as _assert] (/Users/chris/lmdb-leveldown/node_modules/tape/lib/test.js:212:54)
          at Test.bound [as _assert] (/Users/chris/lmdb-leveldown/node_modules/tape/lib/test.js:64:32)
          at Test.deepEqual.Test.deepEquals.Test.isEquivalent.Test.same (/Users/chris/lmdb-leveldown/node_modules/tape/lib/test.js:380:10)
          at Test.bound [as same] (/Users/chris/lmdb-leveldown/node_modules/tape/lib/test.js:64:32)
          at /Users/chris/lmdb-leveldown/node_modules/abstract-leveldown/abstract/iterator-range-test.js:29:11
          at /Users/chris/lmdb-leveldown/node_modules/abstract-leveldown/testCommon.js:56:11
          at /Users/chris/lmdb-leveldown/src/index.js:281:48
  ...
@chrbala chrbala changed the title Iterator does not work when the supplied range is not a key Iterator does not work correctly when the supplied range is not a key Feb 18, 2018
This was referenced Feb 18, 2018
chrbala added a commit that referenced this issue Feb 18, 2018
@chrbala
Copy link
Owner Author

chrbala commented Feb 18, 2018

Resolved by creating bogus key/value pairs, starting the cursor at the target, then aborting the transaction when done.

@chrbala chrbala closed this as completed Feb 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant