Skip to content

Commit e919ec9

Browse files
committed
feat: additional operations, SDK alignment, ver. 0.9.0.5
1 parent 0765f2e commit e919ec9

13 files changed

+238
-106
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ immudb4j implements a [gRPC] immudb client, based on [immudb's official protobuf
3434
It exposes a minimal and simple to use API for applications, while the cryptographic verifications and state update protocol implementation
3535
are fully implemented internally by this client.
3636

37-
The latest validated immudb state may be kept in the local file system using default `FileRootHolder`.<br/>
37+
The latest validated immudb state may be kept in the local file system using default `FileImmuStateHolder`.<br/>
3838
Please read [immudb Research Paper] for details of how immutability is ensured by [immudb].
3939

4040
[gRPC]: https://grpc.io/
@@ -55,12 +55,12 @@ Just include immudb4j as a dependency in your project:
5555
<dependency>
5656
<groupId>io.codenotary</groupId>
5757
<artifactId>immudb4j</artifactId>
58-
<version>0.9.0.4</version>
58+
<version>0.9.0.5</version>
5959
</dependency>
6060
```
6161
- if using Gradle:
6262
```groovy
63-
compile 'io.codenotary:immudb4j:0.9.0.4'
63+
compile 'io.codenotary:immudb4j:0.9.0.5'
6464
```
6565

6666
`immudb4j` is currently hosted on both [Maven Central] and [Github Packages].
@@ -89,7 +89,7 @@ immudb4j supports the [latest immudb server] release, that is 0.9.1 at the time
8989

9090
Follow its [README](https://github.com/codenotary/immudb-client-examples/blob/master/java/README.md) to build and run it.
9191

92-
## Step-by-step guide
92+
## Step-by-step Guide
9393

9494
### Creating a Client
9595

@@ -150,7 +150,7 @@ Specify the active database with:
150150

151151
### Standard Read and Write
152152

153-
immudb provides standard read and write operations that behave as in a traditional
153+
immudb provides standard read and write operations that behave as in a standard
154154
key-value store i.e. no cryptographic verification is involved. Such operations
155155
may be used when validations can be postponed.
156156

@@ -160,10 +160,10 @@ may be used when validations can be postponed.
160160
byte[] v = client.get("k123");
161161
```
162162

163-
### Verified or Safe read and write
163+
### Verified or Safe Read and Write
164164

165165
immudb provides built-in cryptographic verification for any entry. The client
166-
implements the mathematical validations while the application uses as a traditional
166+
implements the mathematical validations while the application uses as a standard
167167
read or write operation:
168168

169169
```java

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ apply plugin: 'signing'
3838

3939
group = 'io.codenotary'
4040
archivesBaseName = 'immudb4j'
41-
version = '0.9.0.4'
41+
version = '0.9.0.5'
4242

4343
sourceCompatibility = 1.8
4444
targetCompatibility = 1.8

src/main/java/io/codenotary/immudb4j/ImmuClient.java

Lines changed: 62 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class ImmuClient {
5353

5454
private static final String AUTH_HEADER = "authorization";
5555
private final ImmuServiceGrpc.ImmuServiceBlockingStub stub;
56-
private final boolean withAuthToken;
56+
private final boolean withAuth;
5757
private final ImmuStateHolder stateHolder;
5858
private ManagedChannel channel;
5959
private String authToken;
@@ -62,7 +62,7 @@ public class ImmuClient {
6262

6363
public ImmuClient(Builder builder) {
6464
this.stub = createStubFrom(builder);
65-
this.withAuthToken = builder.isWithAuthToken();
65+
this.withAuth = builder.isWithAuth();
6666
this.stateHolder = builder.getStateHolder();
6767
}
6868

@@ -111,7 +111,7 @@ public synchronized boolean isShutdown() {
111111
}
112112

113113
private ImmuServiceGrpc.ImmuServiceBlockingStub getStub() {
114-
if (!withAuthToken || authToken == null) {
114+
if (!withAuth || authToken == null) {
115115
return stub;
116116
}
117117
Metadata metadata = new Metadata();
@@ -377,18 +377,28 @@ public Entry verifiedGetSince(byte[] key, long txId) throws VerificationExceptio
377377
// ========== HISTORY ==========
378378
//
379379

380-
public List<KV> history(String key, int limit, long offset, boolean reverse) {
381-
return history(key.getBytes(StandardCharsets.UTF_8), limit, offset, reverse);
380+
381+
public List<KV> history(String key, int limit, long offset, boolean desc) {
382+
return history(key.getBytes(StandardCharsets.UTF_8), limit, offset, desc);
383+
}
384+
385+
public List<KV> history(byte[] key, int limit, long offset, boolean desc) {
386+
return history(key, limit, offset, desc, 1);
382387
}
383388

384-
public List<KV> history(byte[] key, int limit, long offset, boolean reverse) {
389+
public List<KV> history(String key, int limit, long offset, boolean desc, long sinceTxId) {
390+
return history(key.getBytes(StandardCharsets.UTF_8), limit, offset, desc, sinceTxId);
391+
}
392+
393+
public List<KV> history(byte[] key, int limit, long offset, boolean desc, long sinceTxId) {
385394
ImmudbProto.Entries entries;
386395
try {
387396
entries = getStub().history(ImmudbProto.HistoryRequest.newBuilder()
388397
.setKey(ByteString.copyFrom(key))
389398
.setLimit(limit)
390399
.setOffset(offset)
391-
.setDesc(reverse)
400+
.setDesc(desc)
401+
.setSinceTx(sinceTxId)
392402
.build()
393403
);
394404
} catch (StatusRuntimeException e) {
@@ -401,26 +411,45 @@ public List<KV> history(byte[] key, int limit, long offset, boolean reverse) {
401411
// ========== SCAN ==========
402412
//
403413

404-
public List<KV> scan(String key) {
405-
return scan(ByteString.copyFrom(key, StandardCharsets.UTF_8).toByteArray());
414+
public List<KV> scan(String prefix) {
415+
return scan(ByteString.copyFrom(prefix, StandardCharsets.UTF_8).toByteArray());
416+
}
417+
418+
public List<KV> scan(String prefix, long sinceTxId, long limit, boolean desc) {
419+
return scan(ByteString.copyFrom(prefix, StandardCharsets.UTF_8).toByteArray(), sinceTxId, limit, desc);
406420
}
407421

408-
public List<KV> scan(String key, long sinceTxId, long limit, boolean reverse) {
409-
return scan(ByteString.copyFrom(key, StandardCharsets.UTF_8).toByteArray(), sinceTxId, limit, reverse);
422+
public List<KV> scan(String prefix, String seekKey, long sinceTxId, long limit, boolean desc) {
423+
return scan(
424+
ByteString.copyFrom(prefix, StandardCharsets.UTF_8).toByteArray(),
425+
ByteString.copyFrom(seekKey, StandardCharsets.UTF_8).toByteArray(),
426+
sinceTxId, limit, desc);
410427
}
411428

412-
public List<KV> scan(byte[] key) {
413-
ScanRequest req = ScanRequest.newBuilder().setPrefix(ByteString.copyFrom(key)).build();
429+
public List<KV> scan(byte[] prefix) {
430+
ScanRequest req = ScanRequest.newBuilder().setPrefix(ByteString.copyFrom(prefix)).build();
414431
ImmudbProto.Entries entries = getStub().scan(req);
415432
return buildList(entries);
416433
}
417434

418-
public List<KV> scan(byte[] key, long sinceTxId, long limit, boolean reverse) {
435+
public List<KV> scan(byte[] prefix, long sinceTxId, long limit, boolean desc) {
419436
ScanRequest req = ScanRequest.newBuilder()
420-
.setPrefix(ByteString.copyFrom(key))
437+
.setPrefix(ByteString.copyFrom(prefix))
421438
.setLimit(limit)
422439
.setSinceTx(sinceTxId)
423-
.setDesc(reverse)
440+
.setDesc(desc)
441+
.build();
442+
ImmudbProto.Entries entries = getStub().scan(req);
443+
return buildList(entries);
444+
}
445+
446+
public List<KV> scan(byte[] prefix, byte[] seekKey, long sinceTxId, long limit, boolean desc) {
447+
ScanRequest req = ScanRequest.newBuilder()
448+
.setPrefix(ByteString.copyFrom(prefix))
449+
.setLimit(limit)
450+
.setSeekKey(ByteString.copyFrom(seekKey))
451+
.setSinceTx(sinceTxId)
452+
.setDesc(desc)
424453
.build();
425454
ImmudbProto.Entries entries = getStub().scan(req);
426455
return buildList(entries);
@@ -800,12 +829,12 @@ public List<Tx> txScan(long initialTxId) {
800829
return buildList(txList);
801830
}
802831

803-
public List<Tx> txScan(long initialTxId, int limit, boolean reverse) {
832+
public List<Tx> txScan(long initialTxId, int limit, boolean desc) {
804833
ImmudbProto.TxScanRequest req = ImmudbProto.TxScanRequest
805834
.newBuilder()
806835
.setInitialTx(initialTxId)
807836
.setLimit(limit)
808-
.setDesc(reverse)
837+
.setDesc(desc)
809838
.build();
810839
ImmudbProto.TxList txList = getStub().txScan(req);
811840
return buildList(txList);
@@ -892,6 +921,15 @@ public void changePassword(String user, String oldPassword, String newPassword)
892921
getStub().changePassword(changePasswordRequest);
893922
}
894923

924+
//
925+
// ========== USER MGMT ==========
926+
//
927+
928+
public void cleanIndex() {
929+
//noinspection ResultOfMethodCallIgnored
930+
getStub().cleanIndex(Empty.getDefaultInstance());
931+
}
932+
895933
//
896934
// ========== INTERNAL UTILS ==========
897935
//
@@ -932,15 +970,15 @@ public static class Builder {
932970

933971
private int serverPort;
934972

935-
private boolean withAuthToken;
973+
private boolean withAuth;
936974

937975
private ImmuStateHolder stateHolder;
938976

939977
private Builder() {
940978
this.serverUrl = "localhost";
941979
this.serverPort = 3322;
942980
this.stateHolder = new SerializableImmuStateHolder();
943-
this.withAuthToken = true;
981+
this.withAuth = true;
944982
}
945983

946984
public ImmuClient build() {
@@ -965,12 +1003,12 @@ public Builder withServerPort(int serverPort) {
9651003
return this;
9661004
}
9671005

968-
public boolean isWithAuthToken() {
969-
return withAuthToken;
1006+
public boolean isWithAuth() {
1007+
return withAuth;
9701008
}
9711009

972-
public Builder withAuthToken(boolean withAuthToken) {
973-
this.withAuthToken = withAuthToken;
1010+
public Builder withAuth(boolean withAuth) {
1011+
this.withAuth = withAuth;
9741012
return this;
9751013
}
9761014

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
Copyright 2021 CodeNotary, Inc. All rights reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package io.codenotary.immudb4j;
17+
18+
import io.codenotary.immudb4j.exceptions.CorruptedDataException;
19+
import org.testng.Assert;
20+
import org.testng.annotations.Test;
21+
22+
import java.nio.charset.StandardCharsets;
23+
import java.util.List;
24+
25+
public class HistoryTest extends ImmuClientIntegrationTest {
26+
27+
@Test(testName = "set, history", priority = 2)
28+
public void t1() {
29+
30+
immuClient.login("immudb", "immudb");
31+
immuClient.useDatabase("defaultdb");
32+
33+
byte[] value1 = {0, 1, 2, 3};
34+
byte[] value2 = {4, 5, 6, 7};
35+
byte[] value3 = {8, 9, 10, 11};
36+
37+
try {
38+
immuClient.set("history1", value1);
39+
immuClient.set("history1", value2);
40+
immuClient.set("history2", value1);
41+
immuClient.set("history2", value2);
42+
immuClient.set("history2", value3);
43+
} catch (CorruptedDataException e) {
44+
Assert.fail("Failed at set.", e);
45+
}
46+
47+
List<KV> historyResponse1 = immuClient.history("history1", 10, 0, false);
48+
49+
Assert.assertEquals(historyResponse1.size(), 2);
50+
51+
Assert.assertEquals(historyResponse1.get(0).getKey(), "history1".getBytes(StandardCharsets.UTF_8));
52+
Assert.assertEquals(historyResponse1.get(0).getValue(), value1);
53+
54+
Assert.assertEquals(historyResponse1.get(1).getKey(), "history1".getBytes(StandardCharsets.UTF_8));
55+
Assert.assertEquals(historyResponse1.get(1).getValue(), value2);
56+
57+
List<KV> historyResponse2 = immuClient.history("history2", 10, 0, false);
58+
59+
Assert.assertEquals(historyResponse2.size(), 3);
60+
61+
Assert.assertEquals(historyResponse2.get(0).getKey(), "history2".getBytes(StandardCharsets.UTF_8));
62+
Assert.assertEquals(historyResponse2.get(0).getValue(), value1);
63+
64+
Assert.assertEquals(historyResponse2.get(1).getKey(), "history2".getBytes(StandardCharsets.UTF_8));
65+
Assert.assertEquals(historyResponse2.get(1).getValue(), value2);
66+
67+
Assert.assertEquals(historyResponse2.get(2).getKey(), "history2".getBytes(StandardCharsets.UTF_8));
68+
Assert.assertEquals(historyResponse2.get(2).getValue(), value3);
69+
70+
historyResponse2 = immuClient.history("history2", 10, 2, false, 5);
71+
Assert.assertNotNull(historyResponse2);
72+
Assert.assertEquals(historyResponse2.size(), 1);
73+
74+
List<KV> nonExisting = immuClient.history("nonExisting", 10, 0, false);
75+
Assert.assertTrue(nonExisting.isEmpty());
76+
77+
immuClient.logout();
78+
}
79+
80+
}

src/test/java/io/codenotary/immudb4j/ImmuClientIntegrationTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ public static void beforeClass() throws IOException {
3434
.withStateHolder(stateHolder)
3535
.withServerUrl("localhost")
3636
.withServerPort(3322)
37-
.withAuthToken(true)
3837
.build();
3938
}
4039

src/test/java/io/codenotary/immudb4j/LoginAndHealthCheckTest.java renamed to src/test/java/io/codenotary/immudb4j/LoginAndHealthCheckAndCleanIndexTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,17 @@
1919
import org.testng.Assert;
2020
import org.testng.annotations.Test;
2121

22-
public class LoginAndHealthCheckTest extends ImmuClientIntegrationTest {
22+
public class LoginAndHealthCheckAndCleanIndexTest extends ImmuClientIntegrationTest {
2323

2424
@Test(testName = "login (with default credentials), healthCheck, logout")
2525
public void t1() {
2626

2727
immuClient.login("immudb", "immudb");
2828

29-
Assert.assertTrue(immuClient.healthCheck());
29+
boolean isHealthy = immuClient.healthCheck();
30+
Assert.assertTrue(isHealthy);
31+
32+
immuClient.cleanIndex();
3033

3134
immuClient.logout();
3235
}

src/test/java/io/codenotary/immudb4j/ReferenceTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public void t1() {
4949
}
5050
Assert.assertNotNull(ref1TxMd);
5151

52+
53+
5254
TxMetadata ref2TxMd = null;
5355
try {
5456
ref2TxMd = immuClient.setReferenceAt(ref2Key, key, setTxMd.id);

0 commit comments

Comments
 (0)