Skip to content

Commit

Permalink
Initial work on iterating over a range in RecordStore (implemented fo…
Browse files Browse the repository at this point in the history
…r nextunique cursor)
  • Loading branch information
dumbmatter committed Apr 19, 2017
1 parent dd175e9 commit 2e224bd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
41 changes: 38 additions & 3 deletions src/FDBCursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ const getEffectiveObjectStore = (cursor) => {
return cursor.source;
};

const makeKeyRange = (lower, upper) => {
if (lower !== undefined && upper !== undefined) {
return FDBKeyRange.bound(lower, upper);
}
if (lower !== undefined) {
return FDBKeyRange.lowerBound(lower);
}
if (upper !== undefined) {
return FDBKeyRange.upperBound(upper);
}
}

// http://www.w3.org/TR/2015/REC-IndexedDB-20150108/#cursor
class FDBCursor {
constructor(source, range, direction = 'next', request) {
Expand Down Expand Up @@ -78,7 +90,30 @@ class FDBCursor {
break;
}
} else if (this.direction === "nextunique") {
for (const record of records.values()) {
let lower;
let upper;
if (key !== undefined) {
lower = key;
}
if (this._position !== undefined) {
if (lower === undefined || cmp(lower, this._position) === 1) {
lower = this._position;
}
}
if (this._range !== undefined) {
if (this._range.lower !== undefined && (lower === undefined || cmp(lower, this._range.lower) === 1)) {
lower = this._range.lower;
}
if (this._range.upper !== undefined) {
upper = this._range.upper;
}
}
const range = makeKeyRange(lower, upper);

// This could be done without iterating, if the range was defined slightly better (to handle gt/gte cases).
// But the performance difference should be small, and that wouldn't work anyway for directions where the
// value needs to be used (like next and prev).
for (const record of records.values(range)) {
if (key !== undefined) {
if (cmp(record.key, key) === -1) {
continue;
Expand All @@ -98,7 +133,7 @@ class FDBCursor {
break;
}
} else if (this.direction === "prev") {
for (const record of records.values('prev')) {
for (const record of records.values(undefined, 'prev')) {
if (key !== undefined) {
if (cmp(record.key, key) === 1) {
continue;
Expand Down Expand Up @@ -128,7 +163,7 @@ class FDBCursor {
}
} else if (this.direction === "prevunique") {
let tempRecord;
for (const record of records.values('prev')) {
for (const record of records.values(undefined, 'prev')) {
if (key !== undefined) {
if (cmp(record.key, key) === 1) {
continue;
Expand Down
28 changes: 24 additions & 4 deletions src/lib/RecordStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,44 @@ class RecordStore {
return deletedRecords;
}

values(direction = 'next') {
values(range, direction = 'next') {
return {
[Symbol.iterator]: () => {
let i = direction === 'next' ? -1 : this._records.length;
let i;
if (direction === 'next') {
i = 0;
if (range !== undefined && range.lower !== undefined) {
while (this._records[i] !== undefined && cmp(this._records[i].key, range.lower) === -1) {
i += 1;
}
}
} else {
i = this._records.length;
}

return {
next: () => {
let done;
let value;
if (direction === 'next') {
i += 1;
value = this._records[i];
done = i >= this._records.length;
i += 1;

if (!done && range !== undefined && range.upper !== undefined) {
done = cmp(value.key, range.upper) === 1;
if (done) {
value = undefined;
}
}
} else {
i -= 1;
value = this._records[i];
done = i < 0;
}

return {
value: this._records[i],
value,
done,
}
},
Expand Down

0 comments on commit 2e224bd

Please sign in to comment.