Skip to content

Fix error in Memory LRU implementation #1299

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

Merged
merged 6 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions packages/firestore/src/local/memory_persistence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,20 @@ export class MemoryLruDelegate implements ReferenceDelegate, LruDelegate {
txn: PersistenceTransaction,
f: (sequenceNumber: ListenSequenceNumber) => void
): PersistencePromise<void> {
this.orphanedSequenceNumbers.forEach((_, sequenceNumber) =>
f(sequenceNumber)
return PersistencePromise.forEach(
this.orphanedSequenceNumbers,
({ key, value: sequenceNumber }) => {
// Pass in the exact sequence number as the upper bound so we know it won't be pinned by
// being too recent.
return this.isPinned(txn, key, sequenceNumber).next(isPinned => {
if (!isPinned) {
return f(sequenceNumber);
} else {
return PersistencePromise.resolve();
}
});
}
);
return PersistencePromise.resolve();
}

setInMemoryPins(inMemoryPins: ReferenceSet): void {
Expand Down
8 changes: 8 additions & 0 deletions packages/firestore/src/util/obj_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,12 @@ export class ObjectMap<KeyType extends Equatable<KeyType>, ValueType> {
isEmpty(): boolean {
return objUtil.isEmpty(this.inner);
}

[Symbol.iterator](): Iterator<{ key: KeyType; value: ValueType }> {
// We don't care about the keys, all of the actual keys are in the
// arrays that are the values of the inner object.
const entries: Array<{ key: KeyType; value: ValueType }> = [];
this.forEach((key, value) => entries.push({ key, value }));
return entries[Symbol.iterator]();
}
}
35 changes: 35 additions & 0 deletions packages/firestore/test/unit/util/obj_map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,39 @@ describe('ObjectMap', () => {
expect(map.get(k2)).to.equal(undefined);
expect(map.get(k3)).to.equal(undefined);
});

it('can iterate', () => {
const map = new ObjectMap<TestKey, string>(o => o.mapKey);
const k1 = new TestKey(4, 4);
const k2 = new TestKey(5, 5);
// Test a collision
const k3 = new TestKey(5, 6);
// Test an overwrite
const k4 = new TestKey(-12354, -12354);
const k5 = new TestKey(-12354, -12354);
map.set(k1, 'foo');
map.set(k2, 'bar');
map.set(k3, 'blah');
map.set(k4, 'baz');
map.set(k5, 'boo');

const expectedKeys: TestKey[] = [];
map.forEach(key => {
expectedKeys.push(key);
});

const iterated: Array<{ key: TestKey; value: string }> = [];
const it = map[Symbol.iterator]();
let result = it.next();
while (!result.done) {
iterated.push(result.value);
result = it.next();
}

for (const entry of iterated) {
expect(expectedKeys.indexOf(entry.key)).to.not.equal(-1);
expect(map.get(entry.key)).to.equal(entry.value);
}
expect(iterated.length).to.equal(expectedKeys.length);
});
});