From f9124c318724fa09bbd2e8b739877c8870ac6115 Mon Sep 17 00:00:00 2001 From: Karl Floersch Date: Sat, 20 Jul 2019 11:11:42 -0400 Subject: [PATCH] Fix double prefix bug & small edits --- packages/core/src/app/db/iterator.ts | 2 +- packages/core/src/app/db/range-bucket.ts | 16 +++++++++------- packages/core/test/app/db/range-bucket.spec.ts | 12 ++++++------ 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/packages/core/src/app/db/iterator.ts b/packages/core/src/app/db/iterator.ts index 05a705b8..f58fd2a8 100644 --- a/packages/core/src/app/db/iterator.ts +++ b/packages/core/src/app/db/iterator.ts @@ -249,7 +249,7 @@ export class BaseRangeIterator extends BaseIterator { * @returns the RangeEntry at the next key. */ public async nextRange(): Promise { - const res = await this.next() + const res: KV = await this.next() if (typeof res.key === 'undefined') { return } diff --git a/packages/core/src/app/db/range-bucket.ts b/packages/core/src/app/db/range-bucket.ts index 005e73a6..ee9aa7a0 100644 --- a/packages/core/src/app/db/range-bucket.ts +++ b/packages/core/src/app/db/range-bucket.ts @@ -47,12 +47,14 @@ export class BaseRangeBucket implements RangeBucket { ) {} /** - * Adds this RangeBucket's prefix to the target Buffer - * @param target A Buffer which will have the prefix prepended to it. - * @returns resulting Buffer `prefix+target`. + * Concatenates some value to this bucket's prefix. + * @param value Value to concatenate. + * @returns the value concatenated to the prefix. */ - private addPrefix(target: Buffer): Buffer { - return Buffer.concat([this.prefix, target]) + private addPrefix(value: Buffer): Buffer { + return value !== undefined + ? Buffer.concat([this.prefix, value]) + : this.prefix } /** @@ -289,12 +291,12 @@ export class BaseRangeBucket implements RangeBucket { * @param options Parameters for the iterator. * @returns the iterator instance. */ - public iterator(options?: IteratorOptions): RangeIterator { + public iterator(options: IteratorOptions = {}): RangeIterator { const rangeIterator = new BaseRangeIterator( this.db, { ...options, - prefix: this.addPrefix(this.prefix), + prefix: this.addPrefix(options.prefix), }, (res: KV) => this.resultToRange(res) ) diff --git a/packages/core/test/app/db/range-bucket.spec.ts b/packages/core/test/app/db/range-bucket.spec.ts index afb5f01c..969e5e2e 100644 --- a/packages/core/test/app/db/range-bucket.spec.ts +++ b/packages/core/test/app/db/range-bucket.spec.ts @@ -274,14 +274,14 @@ describe('RangeDB', () => { it('allows nextRange() to be called by the iterator returning a RangeEntry instead of a KV', async () => { const testRanges = { inputs: [ - { start: '0', end: '100', value: 'x4' }, - { start: '100', end: '200', value: 'y4' }, - { start: '50', end: '150', value: 'z4' }, + { start: '0', end: '100', value: 'x' }, + { start: '100', end: '200', value: 'y' }, + { start: '200', end: '225', value: 'z' }, ], expectedResults: [ - { start: '0', end: '50', value: 'x4' }, - { start: '50', end: '150', value: 'z4' }, - { start: '150', end: '200', value: 'y4' }, + { start: '0', end: '100', value: 'x' }, + { start: '100', end: '200', value: 'y' }, + { start: '200', end: '225', value: 'z' }, ], }