Skip to content

Commit e2e31c5

Browse files
committed
fixes and extra test
1 parent a5ac9f9 commit e2e31c5

4 files changed

Lines changed: 60 additions & 10 deletions

File tree

yarn-project/kv-store/src/indexeddb/map.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
4141

4242
async sizeAsync(): Promise<number> {
4343
const index = this.db.index('key');
44-
// Use a range from [container] to [container + sentinel] to match all keys for this container
45-
// This works because [container] < [container, anyKey] < [container + '\uffff']
46-
const rangeQuery = IDBKeyRange.bound(
47-
[this.container],
48-
[this.container + '\uffff'],
49-
true, // Exclude lower bound (don't match exactly [container])
50-
true, // Exclude upper bound
51-
);
44+
const rangeQuery = IDBKeyRange.bound([this.container], [this.container + '\uffff'], true, true);
5245
return await index.count(rangeQuery);
5346
}
5447

yarn-project/kv-store/src/indexeddb/multi_map.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,6 @@ export class IndexedDBAztecMultiMap<K extends Key, V extends Value>
9090
}
9191

9292
override async delete(key: K): Promise<void> {
93-
// For MultiMap, we need to delete ALL entries for this key, not just one
94-
// Delete entries as we iterate - single pass, no array allocation
9593
const index = this.db.index('keyCount');
9694
const rangeQuery = IDBKeyRange.bound(
9795
[this.container, this.normalizeKey(key), 0],

yarn-project/kv-store/src/interfaces/map_test_suite.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,33 @@ export function describeAztecMap(
107107
expect(await size()).to.equal(1);
108108
});
109109

110+
it('returns 0 for empty map size', async () => {
111+
expect(await size()).to.equal(0);
112+
});
113+
114+
it('calculates size correctly across multiple operations', async () => {
115+
expect(await size()).to.equal(0);
116+
117+
// Add items
118+
await map.set('a', 'value1');
119+
await map.set('b', 'value2');
120+
await map.set('c', 'value3');
121+
expect(await size()).to.equal(3);
122+
123+
// Update existing (size should not change)
124+
await map.set('b', 'updated');
125+
expect(await size()).to.equal(3);
126+
127+
// Delete some
128+
await map.delete('a');
129+
expect(await size()).to.equal(2);
130+
131+
// Delete all
132+
await map.delete('b');
133+
await map.delete('c');
134+
expect(await size()).to.equal(0);
135+
});
136+
110137
it('should be able to iterate over entries when there are no keys', async () => {
111138
expect(await entries()).to.deep.equal([]);
112139
});

yarn-project/kv-store/src/interfaces/multi_map_test_suite.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,38 @@ export function describeAztecMultiMap(
104104
expect(await size()).to.equal(1);
105105
});
106106

107+
it('returns 0 for empty multimap size', async () => {
108+
expect(await size()).to.equal(0);
109+
});
110+
111+
it('calculates size correctly with multiple values per key', async () => {
112+
expect(await size()).to.equal(0);
113+
114+
// Add multiple values for same key
115+
await multiMap.set('key1', 'value1');
116+
expect(await size()).to.equal(1);
117+
await multiMap.set('key1', 'value2');
118+
expect(await size()).to.equal(2);
119+
await multiMap.set('key1', 'value3');
120+
expect(await size()).to.equal(3);
121+
122+
// Add values for different key
123+
await multiMap.set('key2', 'value4');
124+
expect(await size()).to.equal(4);
125+
126+
// Delete one value from key1
127+
await multiMap.deleteValue('key1', 'value2');
128+
expect(await size()).to.equal(3);
129+
130+
// Delete entire key
131+
await multiMap.delete('key1');
132+
expect(await size()).to.equal(1);
133+
134+
// Delete last key
135+
await multiMap.delete('key2');
136+
expect(await size()).to.equal(0);
137+
});
138+
107139
it('should be able to iterate over entries when there are no keys', async () => {
108140
expect(await entries()).to.deep.equal([]);
109141
});

0 commit comments

Comments
 (0)