Skip to content

Commit 63577cd

Browse files
committed
Fix zScan, add special case for inclusion proof, add zScan and sql showcases
1 parent b6762e2 commit 63577cd

File tree

12 files changed

+601
-42
lines changed

12 files changed

+601
-42
lines changed
File renamed without changes.

immudb-node-showcase/src/scanning.ts renamed to immudb-node-showcase/src/overview-showcase.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,11 @@ import {
1010

1111

1212

13-
scanningShowcase()
13+
overviewSchowcase()
1414
.catch(console.error)
1515

1616

17-
18-
19-
20-
21-
async function scanningShowcase() {
17+
async function overviewSchowcase() {
2218

2319

2420
const client = new Client({
@@ -236,7 +232,7 @@ async function scanningShowcase() {
236232
refTxId: stateAt9.txId,
237233
})
238234
console.log('getTx2AndVerification')
239-
console.dir(getTx2AndVerification, {depth: 10})
235+
console.log(getTx2AndVerification, {depth: 10})
240236
console.log('verifyVerification(getTx2AndVerification) result:')
241237
console.log(verifyVerification(getTx2AndVerification.verification))
242238

@@ -250,7 +246,7 @@ async function scanningShowcase() {
250246
refTxId: stateAt9.txId,
251247
})
252248
console.log('getTx6AndVerification')
253-
console.dir(getTx6AndVerification, {depth: 10})
249+
console.log(getTx6AndVerification, {depth: 10})
254250
console.log('verifyVerification(getTx6AndVerification) result:')
255251
console.log(verifyVerification(getTx6AndVerification.verification))
256252

@@ -263,7 +259,7 @@ async function scanningShowcase() {
263259
refTxId: stateAt9.txId,
264260
})
265261
console.log('getTx7AndVerification')
266-
console.dir(getTx7AndVerification, {depth: 10})
262+
console.log(getTx7AndVerification, {depth: 10})
267263
console.log('verifyVerification(getTx7AndVerification) result:')
268264
console.log(verifyVerification(getTx7AndVerification.verification))
269265

@@ -275,7 +271,7 @@ async function scanningShowcase() {
275271
refTxId: stateAt9.txId,
276272
})
277273
console.log('getTx8AndVerification')
278-
console.dir(getTx8AndVerification, {depth: 10})
274+
console.log(getTx8AndVerification, {depth: 10})
279275
console.log('verifyVerification(getTx8AndVerification) result:')
280276
console.log(verifyVerification(getTx8AndVerification.verification))
281277

@@ -284,10 +280,6 @@ async function scanningShowcase() {
284280

285281

286282

287-
288-
289-
290-
291283
await client.close()
292284
}
293285

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* Run with:
3+
*
4+
* ```sh
5+
* npx ts-node --esm .\immudb-node-showcase\src\sql-showcase.ts
6+
* ```
7+
*/
8+
import Long from 'long'
9+
import {
10+
Client,
11+
verifyVerification,
12+
types,
13+
stream,
14+
} from 'immudb-node'
15+
16+
17+
18+
19+
20+
sqlSchowcase()
21+
.catch(console.error)
22+
23+
async function sqlSchowcase() {
24+
25+
const client = new Client({
26+
host: '127.0.0.1',
27+
port: 3322,
28+
user: 'immudb',
29+
password: 'immudb',
30+
database: 'defaultdb',
31+
})
32+
33+
// since tx used for verification reference
34+
// cannot be first db transaction lets insert some dummy value:
35+
const {valEntries: [dummyValEntry]} = await client.setValEntries({kvms: [
36+
{key: Buffer.of(0), val: Buffer.of(0)}
37+
]})
38+
39+
// state will be dummyValEntry if database was empty
40+
const stateId2 = await client.getDbCurrentState()
41+
console.log('stateId2:', stateId2)
42+
43+
44+
const {subTxes: [{tx: createTestTableTx}]} = await client.sqlExec({sql: `
45+
create table if not exists testtable (
46+
id1 integer not null,
47+
id2 varchar[3] null,
48+
created timestamp null,
49+
data varchar[512] not null,
50+
isActive boolean not null,
51+
primary key (id1, id2)
52+
);
53+
`})
54+
console.log('createTestTableTx:', createTestTableTx)
55+
56+
57+
const {subTxes: [{
58+
tx: insertTestTableTx,
59+
lastPK: insertTestTableLastPK,
60+
firstPK: insertTestTableFirstPK,
61+
updatedRowsCount: insertTestTableUpdatedRowsCount
62+
}]} = await client.sqlExec({sql: `
63+
upsert into testtable
64+
(id1, id2, created, data, isactive)
65+
values
66+
(-2, 'kkk', NOW(), 'upsert existing', true),
67+
(10, 'yoy', NOW(), 'upsert operation 2', false),
68+
(11, 'qoy', NOW(), 'upsert operation 3', true);
69+
`})
70+
console.log('insertTestTableTx:', insertTestTableTx)
71+
console.log('insertTestTableLastPK:', insertTestTableLastPK)
72+
console.log('insertTestTableFirstPK:', insertTestTableFirstPK)
73+
console.log('insertTestTableUpdatedRowsCount:', insertTestTableUpdatedRowsCount)
74+
75+
// state at last sql insert (assuming empty db)
76+
const stateId4 = await client.getDbCurrentState()
77+
console.log('stateId4:', stateId4)
78+
79+
80+
const {valEntries: [dummyValEntry1]} = await client.setValEntries({kvms: [
81+
{key: Buffer.of(0), val: Buffer.of(1)}
82+
]})
83+
84+
// state 1 transactions after last sql insert (assuming empty db)
85+
const stateId5 = await client.getDbCurrentState()
86+
console.log('stateId5:', stateId5)
87+
88+
89+
const {valEntries: [dummyValEntry2]} = await client.setValEntries({kvms: [
90+
{key: Buffer.of(0), val: Buffer.of(2)}
91+
]})
92+
93+
// state 2 transactions after last sql insert (assuming empty db)
94+
const stateId6 = await client.getDbCurrentState()
95+
console.log('stateId6:', stateId6)
96+
97+
98+
99+
100+
if(createTestTableTx) {
101+
const createTestTableTxVer = await client.getTxAndVerification({
102+
txId: createTestTableTx.id,
103+
refHash: stateId5.txHash,
104+
refTxId: stateId5.txId,
105+
})
106+
verifyVerification(createTestTableTxVer.verification)
107+
console.log('createTestTableTxVer has been verified.')
108+
}
109+
110+
111+
112+
if(insertTestTableTx) {
113+
const insertTestTableTxVer = await client.getTxAndVerification({
114+
txId: insertTestTableTx.id,
115+
refHash: stateId5.txHash,
116+
refTxId: stateId5.txId,
117+
})
118+
verifyVerification(insertTestTableTxVer.verification)
119+
console.log('insertTestTableTxVer has been verified.')
120+
// console.log('insertTestTableTxVer')
121+
// console.log(insertTestTableTxVer, {depth: 10})
122+
123+
124+
125+
// await client.getSqlRowEntryAndVerification({
126+
// pk: is.encodeAsPK([
127+
// {type: 'INTEGER', isNotNullable: true, val: -2},
128+
// {type: 'VARCHAR', isNotNullable: false, val: 'kkk'},
129+
// ])
130+
// })
131+
}
132+
133+
134+
135+
console.log('dbScan:', await client.scanDbEntries())
136+
137+
138+
await client.close()
139+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import Long from 'long'
2+
import {
3+
Client,
4+
verifyVerification,
5+
types,
6+
stream,
7+
} from 'immudb-node'
8+
9+
10+
11+
12+
13+
zSetShowcase()
14+
.catch(console.error)
15+
16+
async function zSetShowcase() {
17+
18+
const client = new Client({
19+
host: '127.0.0.1',
20+
port: 3322,
21+
user: 'immudb',
22+
password: 'immudb',
23+
database: 'defaultdb',
24+
})
25+
26+
27+
const {valEntries: [kv1, kv2]} = await client.setValEntries({
28+
kvms: [
29+
{key: Buffer.of(0), val: Buffer.of(0)},
30+
{key: Buffer.of(1), val: Buffer.of(1)},
31+
]
32+
})
33+
// console.log('kv1', kv1)
34+
// console.log('kv2', kv2)
35+
36+
37+
const {ref: ref1} = await client.setRefEntry({
38+
key: Buffer.of(3),
39+
referToKey: kv1.key,
40+
keyTxId: kv1.id,
41+
boundRef: true,
42+
})
43+
const {ref: ref2} = await client.setRefEntry({
44+
key: Buffer.of(4),
45+
referToKey: kv2.key,
46+
keyTxId: kv2.id,
47+
boundRef: true,
48+
})
49+
// console.log('ref1', ref1)
50+
// console.log('ref2', ref2)
51+
52+
53+
const {zSetTxEntry: zSet1} = await client.setZSetEntry({
54+
zSet: Buffer.of(4),
55+
referredKey: ref1.referredKey,
56+
referredKeyScore: 3.14,
57+
referredKeyAtTxId: ref1.referredAtTxId,
58+
boundReferredKeyAtTxId: true,
59+
})
60+
const {zSetTxEntry: zSet2} = await client.setZSetEntry({
61+
zSet: Buffer.of(4),
62+
referredKey: ref2.referredKey,
63+
referredKeyScore: 5.15,
64+
})
65+
// console.log('zSet1', zSet1)
66+
// console.log('zSet2', zSet2)
67+
68+
69+
const zSetEntries = await client.scanZEntries({
70+
set: zSet2.zSet,
71+
})
72+
console.log('zSetEntries', zSetEntries)
73+
74+
75+
76+
const dbEntries = await client.scanDbEntries({
77+
scanStartAtTxId: Long.fromInt(1, true)
78+
})
79+
// console.log('dbEntries', dbEntries)
80+
81+
await client.close()
82+
}

immudb-node/src/immu-api/scan-txes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ export type ScanDBProps = {
165165
* If set operation will start scanning from transaction with specified id,
166166
* if not set operation will start from first transaction.
167167
*
168-
* **Mandatory**.
168+
*
169169
*/
170-
scanStartAtTxId: Long,
170+
scanStartAtTxId?: Long,
171171
}
172172

173173

@@ -186,7 +186,7 @@ export function createScanDb(client: igrpc.ImmuServiceClient) {
186186

187187
const allEntries = await scanTxes({
188188
credentials: props.credentials,
189-
scanStartAtTxId: props.scanStartAtTxId,
189+
scanStartAtTxId: props.scanStartAtTxId ?? Long.fromInt(1, true),
190190
dontWaitForLatestKeys: props.dontWaitForLatestKeys,
191191
seenSinceTxId: props.seenSinceTxId,
192192
sortDescending: props.sortDescending,

immudb-node/src/immu-api/scan-zSet-entries.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ export function createScanZEntries(client: igrpc.ImmuServiceClient) {
7878
request: {
7979

8080
set: props.set,
81-
minScore: { score: props.minScore },
82-
maxScore: { score: props.maxScore },
81+
minScore: props.minScore ? { score: props.minScore } : undefined,
82+
maxScore: props.maxScore ? { score: props.maxScore } : undefined,
8383
seekScore: props.equalsScore,
8484

8585

@@ -140,8 +140,8 @@ export function createScanZEntriesStreaming(client: igrpc.ImmuServiceClient) {
140140
request: {
141141

142142
set: props.set,
143-
minScore: { score: props.minScore },
144-
maxScore: { score: props.maxScore },
143+
minScore: props.minScore ? { score: props.minScore } : undefined,
144+
maxScore: props.maxScore ? { score: props.maxScore } : undefined,
145145
seekScore: props.equalsScore,
146146

147147

immudb-node/src/immu-api/sql-exec.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,12 @@ export function createSqlExec(client: igrpc.ImmuServiceClient) {
8686
const firstPK = igs.grpcSqlObjectNamedValueToNamedValues(
8787
grpcCommitedSqlTx.firstInsertedPKs
8888
)
89+
console.log('grpcCommitedSqlTx.firstInsertedPKs', grpcCommitedSqlTx.firstInsertedPKs)
8990
const lastPK = igs.grpcSqlObjectNamedValueToNamedValues(
9091
grpcCommitedSqlTx.lastInsertedPKs
9192
)
93+
console.log('grpcCommitedSqlTx.lastInsertedPKs', grpcCommitedSqlTx.lastInsertedPKs)
94+
9295
return {
9396
tx,
9497
firstPK,

immudb-node/src/immu-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ export class Client {
483483
/**
484484
* Scans all database entries in one transaction.
485485
*/
486-
async scanDbEntries(props: api.ScanDBProps) {
486+
async scanDbEntries(props?: api.ScanDBProps) {
487487
return this.immuGrpcApi.scanDbEntries({
488488
...props,
489489
credentials: await this.getCallCredentials(),

0 commit comments

Comments
 (0)