-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
145 lines (115 loc) · 3.82 KB
/
index.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
import * as Debug from "debug";
import { BigtableFactory } from "..";
const debug = Debug("yildiz:bigtable:example");
const config = {
projectId: "my-project-1",
instanceName: "my-bigtable-cluster",
keyFilename: "keyfile.json",
ttlScanIntervalMs: 2000,
minJitterMs: 2000,
maxJitterMs: 5000,
};
const myTableConfig = {
name: "mytable",
columnFamily: "myfamily",
defaultColumn: "default",
maxVersions: 1,
};
const sleep = (ms: any) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
(async () => {
// the goal of this api is to take away the complexity of the original big table api
// by encapsulating columnFamilies and multi cell rows, via default values
// and also adding ttl and metadata with sub millisecond access per default
const bigtableFactory = new BigtableFactory(config);
await bigtableFactory.init();
const myInstance = await bigtableFactory.get(myTableConfig);
const rowKey = "myrowkey";
myInstance.on("expired", (data: any) => {
debug("expired row:", data);
});
// when column is null, the default column from the config is used
// ttl only works on cell level, if a row has no more cells, bigtable will delete the row automatically
// we create a ${config.name}_metadata table for every table we create
// we use it to keep track of cell ttls and scan them
// we use it to keep track of the current count (size) of the table
// you can append by calling set with different column values
const value = "myvalue";
await myInstance.set(rowKey, value);
await myInstance.set(rowKey, value, 5, "newColumn");
await myInstance.set(rowKey, value, 8, "newColumn");
await myInstance.bulkInsert([
{
row: "jean-paul",
column: "sartre",
data: "france",
ttl: 3,
},
{
row: "emmanuel",
column: "kant",
data: "germany",
},
{
row: "baruch",
column: "spinoza",
data: "netherland",
},
], 5);
debug(`TTL For ${rowKey}:`, await myInstance.ttl(rowKey, "newColumn"));
await myInstance.multiSet(rowKey, {testColumn: "hello", anotherColumn: "yes"});
await myInstance.multiSet(rowKey, {testColumn: "hello", anotherColumn: "yes"}, 5);
await myInstance.multiSet(rowKey, {testColumn: "hello", anotherColumn: "no"}, 7);
debug(`TTL For ${rowKey}#testColumn:`, await myInstance.ttl(rowKey, "testColumn"));
await myInstance.increase(rowKey, "numberColumn");
await myInstance.increase(rowKey, "numberColumn");
await myInstance.decrease(rowKey, "numberColumn");
await myInstance.bulkInsert([
{
row: "jean-paul",
column: "sartre",
data: "france",
ttl: 3,
},
{
row: "emmanuel",
column: "kant",
data: "germany",
ttl: 7,
},
], 6);
await myInstance.bulkInsert([
{
row: "thomas",
column: "hobbes",
data: "england",
ttl: 3,
},
{
row: "friedrich",
column: "nietzche",
data: "germany",
},
]);
await myInstance.multiAdd(rowKey, {foo: 1, bar: -5}, 5);
debug("counts :", await myInstance.count());
debug(await myInstance.get(rowKey));
debug(await myInstance.get(rowKey, "numberColumn"));
debug("waiting...");
await sleep(9000);
await myInstance.set("rowKey1", value);
debug("counts :", await myInstance.count());
await myInstance.delete(rowKey);
await myInstance.delete(rowKey, "foo");
debug(await myInstance.getRow(rowKey));
await myInstance.deleteRow(rowKey);
await myInstance.deleteRow("rowKey1");
debug("waiting...");
await sleep(3000);
await myInstance.deleteRow("friedrich");
debug("counts :", await myInstance.count());
bigtableFactory.close();
})().catch(debug);