Skip to content

Commit

Permalink
fix(SwingSet): enable getKeys('','') in blockBuffer/crankBuffer
Browse files Browse the repository at this point in the history
Previously, these two wrappers didn't provide the same functionality as their
underlying HostStorage: you couldn't use empty strings for the start/end to
get more keys.

Also, add type assertions to crankBuffer to detect non-string keys/values
earlier than commitCrank.
  • Loading branch information
warner committed May 21, 2021
1 parent 26ea09e commit ff6af69
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion packages/SwingSet/src/blockBuffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export function buildBlockBuffer(hostDB) {
keys.delete(k);
}
for (const k of Array.from(keys).sort()) {
if (start <= k && k < end) {
if ((start === '' || start <= k) && (end === '' || k < end)) {
yield k;
}
}
Expand Down
8 changes: 7 additions & 1 deletion packages/SwingSet/src/kernel/state/storageWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export function buildCrankBuffer(kvStore) {
},

*getKeys(start, end) {
assert.typeof(start, 'string');
assert.typeof(end, 'string');
const keys = new Set(kvStore.getKeys(start, end));
for (const k of additions.keys()) {
keys.add(k);
Expand All @@ -129,13 +131,14 @@ export function buildCrankBuffer(kvStore) {
keys.delete(k);
}
for (const k of Array.from(keys).sort()) {
if (start <= k && k < end) {
if ((start === '' || start <= k) && (end === '' || k < end)) {
yield k;
}
}
},

get(key) {
assert.typeof(key, 'string');
if (additions.has(key)) {
return additions.get(key);
}
Expand All @@ -146,11 +149,14 @@ export function buildCrankBuffer(kvStore) {
},

set(key, value) {
assert.typeof(key, 'string');
assert.typeof(value, 'string');
additions.set(key, value);
deletions.delete(key);
},

delete(key) {
assert.typeof(key, 'string');
additions.delete(key);
deletions.add(key);
},
Expand Down
27 changes: 12 additions & 15 deletions packages/SwingSet/test/test-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ function testStorage(t, s, getState, commit) {
s.set('foo3', 'f3');
t.deepEqual(Array.from(s.getKeys('foo1', 'foo3')), ['foo1', 'foo2']);
t.deepEqual(Array.from(s.getKeys('foo1', 'foo4')), ['foo1', 'foo2', 'foo3']);
t.deepEqual(Array.from(s.getKeys('', '')), ['foo', 'foo1', 'foo2', 'foo3']);
t.deepEqual(Array.from(s.getKeys('foo1', '')), ['foo1', 'foo2', 'foo3']);
t.deepEqual(Array.from(s.getKeys('', 'foo2')), ['foo', 'foo1']);

s.delete('foo2');
t.falsy(s.has('foo2'));
Expand Down Expand Up @@ -250,21 +253,15 @@ function duplicateKeeper(getState) {
}

test('hostStorage param guards', async t => {
const { kstorage, commitCrank } = buildKeeperStorageInMemory();
kstorage.set('foo', true);
t.throws(commitCrank, { message: /must be a string/ });
kstorage.set(true, 'foo');
t.throws(commitCrank, { message: /must be a string/ });
kstorage.has(true);
t.throws(commitCrank, { message: /must be a string/ });
kstorage.getKeys('foo', true);
t.throws(commitCrank, { message: /must be a string/ });
kstorage.getKeys(true, 'foo');
t.throws(commitCrank, { message: /must be a string/ });
kstorage.get(true);
t.throws(commitCrank, { message: /must be a string/ });
kstorage.delete(true);
t.throws(commitCrank, { message: /must be a string/ });
const { kstorage } = buildKeeperStorageInMemory();
const exp = { message: /true must be a string/ };
t.throws(() => kstorage.set('foo', true), exp);
t.throws(() => kstorage.set(true, 'foo'), exp);
t.throws(() => kstorage.has(true), exp);
t.throws(() => Array.from(kstorage.getKeys('foo', true)), exp);
t.throws(() => Array.from(kstorage.getKeys(true, 'foo')), exp);
t.throws(() => kstorage.get(true), exp);
t.throws(() => kstorage.delete(true), exp);
});

test('kernel state', async t => {
Expand Down

0 comments on commit ff6af69

Please sign in to comment.