-
Notifications
You must be signed in to change notification settings - Fork 211
/
kv.ts
185 lines (162 loc) · 4.39 KB
/
kv.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the Apache 2.0 License.
/**
* This module provides access to the Key-Value Store of CCF.
*
* Example of using raw access:
* ```
* import * as ccfapp from '@microsoft/ccf-app';
*
* const foo = ccfapp.rawKv['foo'];
* foo.set(
* ccfapp.string.encode("key-1"),
* ccfapp.json.encode({"prop1": 42})
* );
* ```
*
* Example of using typed access:
* ```
* import * as ccfapp from '@microsoft/ccf-app';
*
* const foo = ccfapp.typedKv('foo', ccfapp.string, ccfapp.json);
* foo.set("key-1", {"prop1": 42});
* ```
*
* Example of using typed access with historical state:
* ```
* import * as ccfapp from '@microsoft/ccf-app';
*
* const states = ccfapp.getStateRange(handle, begin, end, expiry);
* // ... error handling ...
* const firstKv = states[0].kv;
* const foo = ccfapp.typedKv(firstKv['foo'], ccfapp.string, ccfapp.json);
* const val = foo.get("key-1");
* ```
*
* @module
*/
import { KvMap, KvSet, ccf } from "./global.js";
import { DataConverter } from "./converters.js";
export class TypedKvMap<K, V> {
constructor(
private kv: KvMap,
private kt: DataConverter<K>,
private vt: DataConverter<V>,
) {}
has(key: K): boolean {
return this.kv.has(this.kt.encode(key));
}
get(key: K): V | undefined {
const v = this.kv.get(this.kt.encode(key));
return v === undefined ? undefined : this.vt.decode(v);
}
getVersionOfPreviousWrite(key: K): number | undefined {
return this.kv.getVersionOfPreviousWrite(this.kt.encode(key));
}
set(key: K, value: V): TypedKvMap<K, V> {
this.kv.set(this.kt.encode(key), this.vt.encode(value));
return this;
}
delete(key: K): void {
this.kv.delete(this.kt.encode(key));
}
clear(): void {
this.kv.clear();
}
forEach(callback: (value: V, key: K, table: TypedKvMap<K, V>) => void): void {
let kt = this.kt;
let vt = this.vt;
let typedMap = this;
this.kv.forEach(function (
raw_v: ArrayBuffer,
raw_k: ArrayBuffer,
table: KvMap,
) {
callback(vt.decode(raw_v), kt.decode(raw_k), typedMap);
});
}
get size(): number {
return this.kv.size;
}
}
export class TypedKvSet<K> {
constructor(
private kv: KvMap,
private kt: DataConverter<K>,
) {}
has(key: K): boolean {
return this.kv.has(this.kt.encode(key));
}
getVersionOfPreviousWrite(key: K): number | undefined {
return this.kv.getVersionOfPreviousWrite(this.kt.encode(key));
}
add(key: K): TypedKvSet<K> {
this.kv.set(this.kt.encode(key), new ArrayBuffer(8));
return this;
}
delete(key: K): void {
this.kv.delete(this.kt.encode(key));
}
clear(): void {
this.kv.clear();
}
forEach(callback: (key: K, table: TypedKvSet<K>) => void): void {
let kt = this.kt;
let typedSet = this;
this.kv.forEach(function (
raw_v: ArrayBuffer,
raw_k: ArrayBuffer,
table: KvMap,
) {
callback(kt.decode(raw_k), typedSet);
});
}
get size(): number {
return this.kv.size;
}
}
/**
* Returns a typed view of a map in the Key-Value Store,
* where keys and values are automatically converted
* to and from ``ArrayBuffer`` based on the given key
* and value converters.
*
* See the {@linkcode converters} module for available converters.
*
* @param nameOrMap Either the map name in the Key-Value Store,
* or a ``KvMap`` object.
* @param kt The converter to use for map keys.
* @param vt The converter to use for map values.
*/
export function typedKv<K, V>(
nameOrMap: string | KvMap,
kt: DataConverter<K>,
vt: DataConverter<V>,
) {
const kvMap = typeof nameOrMap === "string" ? ccf.kv[nameOrMap] : nameOrMap;
return new TypedKvMap(kvMap, kt, vt);
}
/**
* Returns a typed view of a set in the Key-Value Store,
* where keys are automatically converted
* to and from ``ArrayBuffer`` based on the given key
* converter.
*
* See the {@linkcode converters} module for available converters.
*
* @param nameOrMap Either the map name in the Key-Value Store,
* or a ``KvMap`` object.
* @param kt The converter to use for map keys.
*/
export function typedKvSet<K, V>(
nameOrMap: string | KvMap,
kt: DataConverter<K>,
) {
const kvMap = typeof nameOrMap === "string" ? ccf.kv[nameOrMap] : nameOrMap;
return new TypedKvSet(kvMap, kt);
}
/**
* @inheritDoc global!CCF.kv
*/
export const rawKv = ccf.kv;
export { KvMap, KvSet, KvMaps } from "./global";