Skip to content

Commit

Permalink
feat: KV has also txId, builders "with" standard methods, version bum…
Browse files Browse the repository at this point in the history
…p to 0.9.0.4
  • Loading branch information
dxps committed Feb 26, 2021
1 parent 9588a6a commit 0765f2e
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 21 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ Just include immudb4j as a dependency in your project:
<dependency>
<groupId>io.codenotary</groupId>
<artifactId>immudb4j</artifactId>
<version>0.9.0.3</version>
<version>0.9.0.4</version>
</dependency>
```
- if using Gradle:
```groovy
compile 'io.codenotary:immudb4j:0.9.0.3'
compile 'io.codenotary:immudb4j:0.9.0.4'
```

`immudb4j` is currently hosted on both [Maven Central] and [Github Packages].
Expand Down Expand Up @@ -103,19 +103,19 @@ Using default configuration:
Setting `immudb` url and port:
```java
ImmuClient immuClient = ImmuClient.newBuilder()
.setServerUrl("localhost")
.setServerPort(3322)
.withServerUrl("localhost")
.withServerPort(3322)
.build();
```

Customizing the `State Holder`:
```java
FileImmuStateHolder stateHolder = FileImmuStateHolder.newBuilder()
.setStatesFolder("./my_immuapp_roots")
.withStatesFolder("./my_immuapp_states")
.build();

ImmuClient immuClient = ImmuClient.newBuilder()
.setStateHolder(stateHolder)
.withStateHolder(stateHolder)
.build();
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ apply plugin: 'signing'

group = 'io.codenotary'
archivesBaseName = 'immudb4j'
version = '0.9.0.3'
version = '0.9.0.4'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public FileImmuStateHolder build() throws IOException, IllegalStateException {
return new FileImmuStateHolder(this);
}

public Builder setStatesFolder(String statesFolder) {
public Builder withStatesFolder(String statesFolder) {
this.statesFolder = statesFolder;
return this;
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/io/codenotary/immudb4j/ImmuClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -951,7 +951,7 @@ public String getServerUrl() {
return this.serverUrl;
}

public Builder setServerUrl(String serverUrl) {
public Builder withServerUrl(String serverUrl) {
this.serverUrl = serverUrl;
return this;
}
Expand All @@ -960,7 +960,7 @@ public int getServerPort() {
return serverPort;
}

public Builder setServerPort(int serverPort) {
public Builder withServerPort(int serverPort) {
this.serverPort = serverPort;
return this;
}
Expand All @@ -969,7 +969,7 @@ public boolean isWithAuthToken() {
return withAuthToken;
}

public Builder setWithAuthToken(boolean withAuthToken) {
public Builder withAuthToken(boolean withAuthToken) {
this.withAuthToken = withAuthToken;
return this;
}
Expand All @@ -978,7 +978,7 @@ public ImmuStateHolder getStateHolder() {
return stateHolder;
}

public Builder setStateHolder(ImmuStateHolder stateHolder) {
public Builder withStateHolder(ImmuStateHolder stateHolder) {
this.stateHolder = stateHolder;
return this;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/codenotary/immudb4j/KV.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public interface KV {

byte[] getKey();
byte[] getValue();
long getTxId();

byte[] digest();

Expand Down
21 changes: 19 additions & 2 deletions src/main/java/io/codenotary/immudb4j/KVPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,35 @@ public class KVPair implements KV {

private final byte[] key;
private final byte[] value;
private final long txId;

public static KV from(ImmudbProto.Entry entry) {
return new KVPair(entry.getKey().toByteArray(), entry.getValue().toByteArray());
return new KVPair(entry.getKey().toByteArray(),
entry.getValue().toByteArray(),
entry.getTx()
);
}

public static KV from(ImmudbProto.ZEntry zEntry) {
return KVPair.from(zEntry.getEntry());
}

public KVPair(byte[] key, byte[] value) {
public KVPair(byte[] key, byte[] value, long txId) {
this.key = key;
this.value = value;
this.txId = txId;
}

public KVPair(String key, byte[] value) {
this.key = key.getBytes(StandardCharsets.UTF_8);
this.value = value;
this.txId = 0;
}

public KVPair(byte[] key, byte[] value) {
this.key = key;
this.value = value;
this.txId = 0;
}

@Override
Expand All @@ -57,6 +69,11 @@ public byte[] getValue() {
return this.value;
}

@Override
public long getTxId() {
return this.txId;
}

@Override
public byte[] digest() {
byte[] b = new byte[key.length + Consts.SHA256_SIZE];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;

public class FileImmuStateHolderTest {

Expand All @@ -24,7 +23,7 @@ public void t1() {
// Write some fake "state_..." into "current_state" file.
Files.write(currStateFile.toPath(), "state_fake".getBytes(StandardCharsets.UTF_8));

FileImmuStateHolder.newBuilder().setStatesFolder(statesDir.getAbsolutePath()).build();
FileImmuStateHolder.newBuilder().withStatesFolder(statesDir.getAbsolutePath()).build();

cleanupDir(statesDir);
Assert.fail("stateHolder creation must have fail, but it didn't.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public abstract class ImmuClientIntegrationTest {
@BeforeClass
public static void beforeClass() throws IOException {
FileImmuStateHolder stateHolder = FileImmuStateHolder.newBuilder()
.setStatesFolder("immudb/states")
.withStatesFolder("immudb/states")
.build();

immuClient = ImmuClient.newBuilder()
.setStateHolder(stateHolder)
.setServerUrl("localhost")
.setServerPort(3322)
.setWithAuthToken(true)
.withStateHolder(stateHolder)
.withServerUrl("localhost")
.withServerPort(3322)
.withAuthToken(true)
.build();
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/java/io/codenotary/immudb4j/KVPairTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class KVPairTest {

@Test
public void t1() {

byte[] key = "someKey".getBytes(StandardCharsets.UTF_8);
byte[] val = "someVal".getBytes(StandardCharsets.UTF_8);

Expand All @@ -18,9 +19,16 @@ public void t1() {
Assert.assertEquals(kv1.getKey(), key);
Assert.assertEquals(kv1.getValue(), val);
Assert.assertEquals(kv1.hashCode(), kv2.hashCode());
Assert.assertEquals(kv1.getTxId(), kv2.getTxId());

Assert.assertEquals(kv1, kv2);

Assert.assertEquals(kv1.digest().length, Consts.SHA256_SIZE);

long txId = 123;
KV kv3 = new KVPair(key, val, txId);
Assert.assertEquals(kv3.getTxId(), txId);

}

}

0 comments on commit 0765f2e

Please sign in to comment.