Skip to content

Commit 574e5a1

Browse files
committed
move batch functionality to main
1 parent 7a64c42 commit 574e5a1

File tree

13 files changed

+174
-251
lines changed

13 files changed

+174
-251
lines changed

src/base/sdk.ts

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import type {
55
ContractMode,
66
ContractState,
77
GetInput,
8+
GetManyInput,
89
GetKVMapInput,
910
GetKeysInput,
1011
PutInput,
12+
PutManyInput,
1113
RemoveInput,
1214
UpdateInput,
1315
} from '../contracts/types';
@@ -54,18 +56,6 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
5456
return await this.base.readState().then(s => s.cachedValue.state);
5557
}
5658

57-
/**
58-
* Gets the values at the given keys as an array.
59-
*
60-
* If a value does not exist, it is returned as `null`.
61-
*
62-
* @param keys an array of keys
63-
* @returns an array of corresponding values
64-
*/
65-
async getMany(keys: string[]): Promise<(V | null)[]> {
66-
return await Promise.all(keys.map(key => this.get(key)));
67-
}
68-
6959
/**
7060
* Alternative method of getting key values. Uses the underlying `getStorageValues`
7161
* function, returns a Map instead of an array.
@@ -133,8 +123,30 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
133123
});
134124
}
135125

126+
/**
127+
* Gets the values at the given keys as an array.
128+
*
129+
* If a value does not exist, it is returned as `null`.
130+
*
131+
* Note that the transaction limit may become a problem for too many keys.
132+
*
133+
* @param keys an array of keys
134+
* @returns an array of corresponding values
135+
*/
136+
async getMany(keys: string[]): Promise<(V | null)[]> {
137+
return await this.base.safeReadInteraction<GetManyInput, (V | null)[]>({
138+
function: 'getMany',
139+
value: {
140+
keys,
141+
},
142+
});
143+
}
144+
136145
/**
137146
* Inserts the given value into database.
147+
*
148+
* There must not be a value at the given key.
149+
*
138150
* @param key the key of the value to be inserted
139151
* @param value the value to be inserted
140152
*/
@@ -148,6 +160,24 @@ export class SDK<V = unknown, M extends ContractMode = ContractMode> {
148160
});
149161
}
150162

163+
/**
164+
* Inserts an array of value into database.
165+
*
166+
* There must not be a value at the given key.
167+
*
168+
* @param keys the keys of the values to be inserted
169+
* @param values the values to be inserted
170+
*/
171+
async putMany(keys: string[], values: V[]): Promise<void> {
172+
await this.base.dryWriteInteraction<PutManyInput<V>>({
173+
function: 'putMany',
174+
value: {
175+
keys,
176+
values,
177+
},
178+
});
179+
}
180+
151181
/**
152182
* Updates the value of given key.
153183
* @param key key of the value to be updated

src/contracts/build/hollowdb-htx.contract.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var NullValueError = new ContractError("Value cant be null, use remove instead.");
1212
var NotOwnerError = new ContractError("Not contract owner.");
1313
var InvalidFunctionError = new ContractError("Invalid function.");
14+
var ArrayLengthMismatchError = new ContractError("Key and value counts mismatch.");
1415

1516
// src/contracts/utils/index.ts
1617
var verifyProof = async (proof, psignals, verificationKey) => {
@@ -36,6 +37,12 @@
3637
}
3738
return input;
3839
};
40+
var onlyNonNullValues = (_, input) => {
41+
if (input.values.some((val) => val === null)) {
42+
throw NullValueError;
43+
}
44+
return input;
45+
};
3946
var onlyWhitelisted = (list) => {
4047
return (caller, input, state) => {
4148
if (!state.isWhitelistRequired[list]) {
@@ -70,6 +77,11 @@
7077
const { options } = await apply(caller, input.value, state);
7178
return { result: await SmartWeave.kv.kvMap(options) };
7279
}
80+
case "getMany": {
81+
const { keys } = await apply(caller, input.value, state);
82+
const values = await Promise.all(keys.map((key) => SmartWeave.kv.get(key)));
83+
return { result: values };
84+
}
7385
case "put": {
7486
const { key, value } = await apply(caller, input.value, state, onlyNonNullValue, onlyWhitelisted("put"));
7587
if (await SmartWeave.kv.get(key) !== null) {
@@ -78,6 +90,17 @@
7890
await SmartWeave.kv.put(key, value);
7991
return { state };
8092
}
93+
case "putMany": {
94+
const { keys, values } = await apply(caller, input.value, state, onlyWhitelisted("put"), onlyNonNullValues);
95+
if (keys.length !== values.length) {
96+
throw new ContractError("Key and value counts mismatch");
97+
}
98+
if (await Promise.all(keys.map((key) => SmartWeave.kv.get(key))).then((values2) => values2.some((val) => val !== null))) {
99+
throw KeyExistsError;
100+
}
101+
await Promise.all(keys.map((key, i) => SmartWeave.kv.put(key, values[i])));
102+
return { state };
103+
}
81104
case "update": {
82105
const { key, value } = await apply(
83106
caller,

src/contracts/build/hollowdb.contract.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
var NullValueError = new ContractError("Value cant be null, use remove instead.");
1212
var NotOwnerError = new ContractError("Not contract owner.");
1313
var InvalidFunctionError = new ContractError("Invalid function.");
14+
var ArrayLengthMismatchError = new ContractError("Key and value counts mismatch.");
1415

1516
// src/contracts/utils/index.ts
1617
var verifyProof = async (proof, psignals, verificationKey) => {
@@ -43,6 +44,12 @@
4344
}
4445
return input;
4546
};
47+
var onlyNonNullValues = (_, input) => {
48+
if (input.values.some((val) => val === null)) {
49+
throw NullValueError;
50+
}
51+
return input;
52+
};
4653
var onlyWhitelisted = (list) => {
4754
return (caller, input, state) => {
4855
if (!state.isWhitelistRequired[list]) {
@@ -88,6 +95,11 @@
8895
const { key } = await apply(caller, input.value, state);
8996
return { result: await SmartWeave.kv.get(key) };
9097
}
98+
case "getMany": {
99+
const { keys } = await apply(caller, input.value, state);
100+
const values = await Promise.all(keys.map((key) => SmartWeave.kv.get(key)));
101+
return { result: values };
102+
}
91103
case "getKeys": {
92104
const { options } = await apply(caller, input.value, state);
93105
return { result: await SmartWeave.kv.keys(options) };
@@ -97,13 +109,24 @@
97109
return { result: await SmartWeave.kv.kvMap(options) };
98110
}
99111
case "put": {
100-
const { key, value } = await apply(caller, input.value, state, onlyNonNullValue, onlyWhitelisted("put"));
112+
const { key, value } = await apply(caller, input.value, state, onlyWhitelisted("put"), onlyNonNullValue);
101113
if (await SmartWeave.kv.get(key) !== null) {
102114
throw KeyExistsError;
103115
}
104116
await SmartWeave.kv.put(key, value);
105117
return { state };
106118
}
119+
case "putMany": {
120+
const { keys, values } = await apply(caller, input.value, state, onlyWhitelisted("put"), onlyNonNullValues);
121+
if (keys.length !== values.length) {
122+
throw new ContractError("Key and value counts mismatch");
123+
}
124+
if (await Promise.all(keys.map((key) => SmartWeave.kv.get(key))).then((values2) => values2.some((val) => val !== null))) {
125+
throw KeyExistsError;
126+
}
127+
await Promise.all(keys.map((key, i) => SmartWeave.kv.put(key, values[i])));
128+
return { state };
129+
}
107130
case "update": {
108131
const { key, value } = await apply(
109132
caller,

src/contracts/errors/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ export const ExpectedProofError = new ContractError('Expected a proof.');
99
export const NullValueError = new ContractError('Value cant be null, use remove instead.');
1010
export const NotOwnerError = new ContractError('Not contract owner.');
1111
export const InvalidFunctionError = new ContractError('Invalid function.');
12+
export const ArrayLengthMismatchError = new ContractError('Key and value counts mismatch.');

src/contracts/hollowdb-batch.contract.ts

Lines changed: 0 additions & 160 deletions
This file was deleted.

0 commit comments

Comments
 (0)