Skip to content

Commit

Permalink
Fix double prefix bug & small edits
Browse files Browse the repository at this point in the history
  • Loading branch information
karlfloersch authored and Akhila Raju committed Aug 9, 2019
1 parent 85ce404 commit f9124c3
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/app/db/iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class BaseRangeIterator extends BaseIterator {
* @returns the RangeEntry at the next key.
*/
public async nextRange(): Promise<RangeEntry> {
const res = await this.next()
const res: KV = await this.next()
if (typeof res.key === 'undefined') {
return
}
Expand Down
16 changes: 9 additions & 7 deletions packages/core/src/app/db/range-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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)
)
Expand Down
12 changes: 6 additions & 6 deletions packages/core/test/app/db/range-bucket.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
],
}

Expand Down

0 comments on commit f9124c3

Please sign in to comment.