|
1 | 1 | // @ts-check
|
2 | 2 | import { initSwingStore, openSwingStore } from '@agoric/swing-store';
|
3 |
| -import { assert, details as X } from '@agoric/assert'; |
4 | 3 |
|
5 | 4 | /*
|
6 | 5 | The "Storage API" is a set of functions { has, getKeys, get, set, delete } that
|
7 | 6 | work on string keys and accept string values. A lot of kernel-side code
|
8 | 7 | expects to get an object which implements the Storage API, which is usually
|
9 | 8 | associated with a write-back buffer wrapper.
|
10 | 9 |
|
11 |
| -The "HostDB API" is a different set of functions { has, getKeys, get, |
12 |
| -applyBatch } which the host is expected to provide to the Controller in the |
13 |
| -config object. This API allows SwingSet to deliver batches of changes to the |
14 |
| -host-side storage medium. |
15 |
| -
|
16 |
| -buildHostDBInMemory creates hostDB objects for testing and casual hosts that |
17 |
| -can afford to hold all state in RAM. They must arrange to call getState() at |
18 |
| -the end of each block and save the resulting string to disk. |
19 |
| -
|
20 | 10 | A more sophisticated host will build a hostDB that writes changes to disk
|
21 | 11 | directly.
|
22 | 12 | */
|
23 | 13 |
|
24 |
| -/** |
25 |
| - * Create a new instance of a bare-bones implementation of the HostDB API. |
26 |
| - * |
27 |
| - * @param {KVStore} kvStore Storage object that the new HostDB object will be based on. |
28 |
| - * If omitted, defaults to a new in memory store. |
29 |
| - */ |
30 |
| -export function buildHostDBInMemory(kvStore) { |
31 |
| - if (!kvStore) { |
32 |
| - kvStore = initSwingStore(null).kvStore; |
33 |
| - } |
34 |
| - |
35 |
| - /** |
36 |
| - * Test if the storage contains a value for a given key. |
37 |
| - * |
38 |
| - * @param {string} key The key that is of interest. |
39 |
| - * |
40 |
| - * @returns {boolean} true if a value is stored for the key, false if not. |
41 |
| - */ |
42 |
| - function has(key) { |
43 |
| - return kvStore.has(key); |
44 |
| - } |
45 |
| - |
46 |
| - /** |
47 |
| - * Obtain an iterator over all the keys within a given range. |
48 |
| - * |
49 |
| - * @param {string} start Start of the key range of interest (inclusive) |
50 |
| - * @param {string} end End of the key range of interest (exclusive) |
51 |
| - * |
52 |
| - * @returns {Iterable<string>} an iterator for the keys from start <= key < end |
53 |
| - */ |
54 |
| - function getKeys(start, end) { |
55 |
| - return kvStore.getKeys(start, end); |
56 |
| - } |
57 |
| - |
58 |
| - /** |
59 |
| - * Obtain the value stored for a given key. |
60 |
| - * |
61 |
| - * @param {string} key The key whose value is sought. |
62 |
| - * |
63 |
| - * @returns {string=} the (string) value for the given key, or undefined if there is no |
64 |
| - * such value. |
65 |
| - */ |
66 |
| - function get(key) { |
67 |
| - return kvStore.get(key); |
68 |
| - } |
69 |
| - |
70 |
| - /** |
71 |
| - * @typedef {{ |
72 |
| - * op: 'set', |
73 |
| - * key: string, |
74 |
| - * value: string, |
75 |
| - * }} SetOperation |
76 |
| - * |
77 |
| - * @typedef {{ |
78 |
| - * op: 'delete', |
79 |
| - * key: string, |
80 |
| - * }} DeleteOperation |
81 |
| - * |
82 |
| - * @typedef {{ |
83 |
| - * op: Exclude<string, 'set' | 'delete'>, |
84 |
| - * key: string, |
85 |
| - * }} UnknownOperation |
86 |
| - * |
87 |
| - * @typedef {SetOperation | DeleteOperation} BatchOperation |
88 |
| - * x typedef {Exclude<AnyOperation, BatchOperation>} UnknownOperation |
89 |
| - */ |
90 |
| - |
91 |
| - /** |
92 |
| - * Make an ordered set of changes to the state that is stored. The changes |
93 |
| - * are described by a series of change description objects, each of which |
94 |
| - * describes a single change. There are currently two forms: |
95 |
| - * |
96 |
| - * { op: 'set', key: <KEY>, value: <VALUE> } |
97 |
| - * or |
98 |
| - * { op: 'delete', key: <KEY> } |
99 |
| - * which describe a set or delete operation respectively. |
100 |
| - * |
101 |
| - * @param {Array<BatchOperation>} changes An array of the changes to be |
102 |
| - * applied in order. |
103 |
| - * @throws {Error} if any of the changes are not well formed. |
104 |
| - */ |
105 |
| - function applyBatch(changes) { |
106 |
| - // TODO: Note that the parameter checking is done incrementally, thus a |
107 |
| - // malformed change descriptor later in the list will only be discovered |
108 |
| - // after earlier changes have actually been applied, potentially leaving |
109 |
| - // the store in an indeterminate state. Problem? I suspect so... |
110 |
| - for (const c of changes) { |
111 |
| - assert(`${c.op}` === c.op, X`non-string c.op ${c.op}`); |
112 |
| - assert(`${c.key}` === c.key, X`non-string c.key ${c.key}`); |
113 |
| - switch (c.op) { |
114 |
| - case 'set': |
115 |
| - assert(`${c.value}` === c.value, X`non-string c.value ${c.value}`); |
116 |
| - kvStore.set(c.key, c.value); |
117 |
| - break; |
118 |
| - case 'delete': |
119 |
| - kvStore.delete(c.key); |
120 |
| - break; |
121 |
| - default: |
122 |
| - assert.fail(X`unknown c.op ${/** @type {*} */ (c).op}`); |
123 |
| - } |
124 |
| - } |
125 |
| - } |
126 |
| - |
127 |
| - const hostDB = { |
128 |
| - has, |
129 |
| - getKeys, |
130 |
| - get, |
131 |
| - applyBatch, |
132 |
| - }; |
133 |
| - |
134 |
| - return harden(hostDB); |
135 |
| -} |
136 |
| - |
137 | 14 | /**
|
138 | 15 | * Helper function to initialize the appropriate storage objects for the kernel
|
139 | 16 | *
|
|
0 commit comments