Skip to content

Commit cd3ce51

Browse files
author
Ellyse
committed
sync and send batch of updates to entities
1 parent 8b09db9 commit cd3ce51

File tree

1 file changed

+54
-30
lines changed

1 file changed

+54
-30
lines changed

typescript/packages/common-cli/memory_test.ts

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
11
import { CharmManager, createStorage } from "@commontools/charm";
22
import { RemoteStorageProvider } from "../common-charm/src/storage/remote.ts"
33
import { StorageValue } from "../common-charm/src/storage/base.ts";
4+
import { EntityId } from "../common-runner/src/cell-map.ts";
45

56
// some config stuff, hardcoded, ofcourse
67
const replica = "ellyse5";
8+
79
// i'm running common memory locally, so connect to it directly
810
const BASE_URL = "http://localhost:8000"
9-
const entityId = { "/": "foobar" }
11+
12+
// how many entities will we try to update
13+
const batch_size = 200;
14+
15+
type SomePerson = {
16+
name: string;
17+
position: number;
18+
random: number;
19+
};
20+
21+
function create_person(i: number): SomePerson {
22+
return {
23+
name: "foobar",
24+
position: i,
25+
random: Math.random()
26+
}
27+
}
28+
29+
function create_people(len: number): SomePerson[] {
30+
return Array.from({ length: len }, (_, i) => create_person(i) );
31+
}
32+
33+
function entity_id(i: number): EntityId {
34+
return { "/": "foo"+i};
35+
}
1036

1137
async function main() {
1238
const storageProvider = new RemoteStorageProvider({
@@ -16,37 +42,35 @@ async function main() {
1642

1743
console.log("created RemoteStorageProvider: " + JSON.stringify(storageProvider, null, 2));
1844

19-
// first lets try to get the value
20-
await storageProvider.sync(entityId);
21-
const fetchedValue = storageProvider.get<SomePerson>(entityId);
22-
if (fetchedValue)
23-
console.log("retrieved entity: " + JSON.stringify(fetchedValue.value))
24-
else
25-
console.log("retrieved entity but it was undefined");
26-
27-
// now lets try to store a new value into the entity
28-
type SomePerson = {
29-
name: string;
30-
version: number;
31-
};
45+
const people = create_people(batch_size);
46+
const people_batch = people.map((p: SomePerson, i) => {
47+
return {
48+
entityId: entity_id(i),
49+
value: {
50+
value: p
51+
}
52+
}
53+
});
54+
console.log("create list of people, length=" + people_batch.length);
3255

33-
const myValue: SomePerson = {
34-
name: "something_special",
35-
version: Math.floor(Math.random() * 1000)
36-
};
37-
38-
const myStorageValue: StorageValue<SomePerson> = {
39-
value: myValue
40-
}
41-
42-
console.log("sending value: " + JSON.stringify(myValue, null, 2));
56+
// first lets try to get all the values
57+
console.log("attempting to fetch entities");
58+
const promises = Array.from({ length: batch_size }, (_, i) =>
59+
storageProvider.sync(entity_id(i))
60+
);
61+
await Promise.all(promises);
62+
Array.from({ length: batch_size }, (_, i) => {
63+
const entityId = entity_id(i);
64+
const fetchedValue = storageProvider.get<SomePerson>(entityId);
65+
if (fetchedValue)
66+
console.log("retrieved entity: " + JSON.stringify(entityId));
67+
else
68+
console.log("retrieved entity but it was undefined: entityId=" + JSON.stringify(entityId));
69+
});
4370

44-
const result = await storageProvider.send([
45-
{
46-
entityId: entityId,
47-
value: myStorageValue
48-
}
49-
]);
71+
// now lets try to store a batch of values
72+
console.log("storing all entities")
73+
const result = await storageProvider.send(people_batch);
5074

5175
console.log("sent entity, result: " + JSON.stringify(result, null, 2))
5276
}

0 commit comments

Comments
 (0)