Skip to content

Commit

Permalink
refactor(swingset-liveslots): Introduce collectionManager mustGet h…
Browse files Browse the repository at this point in the history
…elper
  • Loading branch information
gibson042 committed Sep 20, 2023
1 parent 7c48d4c commit 5a06ded
Showing 1 changed file with 13 additions and 16 deletions.
29 changes: 13 additions & 16 deletions packages/swingset-liveslots/src/collectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function throwNotDurable(value, slotIndex, serializedValue) {
Fail`value is not durable: ${value} at slot ${q(slotIndex)} of ${serializedValue.body}`;
}

function failNotFound(label, key) {
function failNotFound(key, label) {
Fail`key ${key} not found in collection ${q(label)}`;
}

Expand Down Expand Up @@ -354,14 +354,19 @@ export function makeCollectionManager(
}
}

function mustGet(key, label) {
if (passStyleOf(key) === 'remotable' && getOrdinal(key) === undefined) {
failNotFound(key, label);
}
const dbKey = keyToDBKey(key);
const result = syscall.vatstoreGet(dbKey) || failNotFound(key, label);
return { dbKey, result };
}

function get(key) {
const { keyShape, label } = getSchema();
mustMatch(key, keyShape, makeInvalidKeyTypeMsg(label));
if (passStyleOf(key) === 'remotable' && getOrdinal(key) === undefined) {
failNotFound(label, key);
}
const result =
syscall.vatstoreGet(keyToDBKey(key)) || failNotFound(label, key);
const { result } = mustGet(key, label);
return unserializeValue(JSON.parse(result));
}

Expand Down Expand Up @@ -432,11 +437,7 @@ export function makeCollectionManager(
}
}
}
if (passStyleOf(key) === 'remotable' && getOrdinal(key) === undefined) {
failNotFound(label, key);
}
const dbKey = keyToDBKey(key);
const rawBefore = syscall.vatstoreGet(dbKey) || failNotFound(label, key);
const { dbKey, result: rawBefore } = mustGet(key, label);
const before = JSON.parse(rawBefore);
vrm.updateReferenceCounts(before.slots, after.slots);
syscall.vatstoreSet(dbKey, JSON.stringify(after));
Expand All @@ -445,11 +446,7 @@ export function makeCollectionManager(
function deleteInternal(key) {
const { keyShape, label } = getSchema();
mustMatch(key, keyShape, makeInvalidKeyTypeMsg(label));
if (passStyleOf(key) === 'remotable' && getOrdinal(key) === undefined) {
failNotFound(label, key);
}
const dbKey = keyToDBKey(key);
const rawValue = syscall.vatstoreGet(dbKey) || failNotFound(label, key);
const { dbKey, result: rawValue } = mustGet(key, label);
const value = JSON.parse(rawValue);
const doMoreGC1 = value.slots.map(vrm.removeReachableVref).some(b => b);
syscall.vatstoreDelete(dbKey);
Expand Down

0 comments on commit 5a06ded

Please sign in to comment.