Skip to content

Commit

Permalink
chore: api simplifications
Browse files Browse the repository at this point in the history
Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>
  • Loading branch information
jeroiraz committed Dec 1, 2022
1 parent 77eeb85 commit 2510747
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 227 deletions.
110 changes: 49 additions & 61 deletions src/main/java/io/codenotary/immudb4j/ImmuClient.java
Expand Up @@ -1084,27 +1084,39 @@ public synchronized List<Entry> scanAll(byte[] prefix, byte[] seekKey, byte[] en
// ========== SET ==========
//

public TxHeader set(String key, byte[] value) throws CorruptedDataException {
/**
* Commits a change of a value for a single key.
*
* @param key the key to set
* @param value the value to set
*/
public TxHeader set(String key, byte[] value) {
return set(Utils.toByteArray(key), value);
}

public synchronized TxHeader set(byte[] key, byte[] value) throws CorruptedDataException {
/**
* Commits a change of a value for a single key.
*
* @param key the key to set
* @param value the value to set
*/
public synchronized TxHeader set(byte[] key, byte[] value) {
final ImmudbProto.KeyValue kv = ImmudbProto.KeyValue.newBuilder()
.setKey(Utils.toByteString(key))
.setValue(Utils.toByteString(value))
.build();

final ImmudbProto.SetRequest req = ImmudbProto.SetRequest.newBuilder().addKVs(kv).build();
final ImmudbProto.TxHeader txHdr = blockingStub.set(req);

if (txHdr.getNentries() != 1) {
throw new CorruptedDataException();
}

return TxHeader.valueOf(txHdr);
return TxHeader.valueOf(blockingStub.set(req));
}

public synchronized TxHeader setAll(List<KVPair> kvList) throws CorruptedDataException {
/**
* Commits multiple entries in a single transaction.
*
* @param kvList the list of key-value pairs to set
*/
public synchronized TxHeader setAll(List<KVPair> kvList) {
final ImmudbProto.SetRequest.Builder reqBuilder = ImmudbProto.SetRequest.newBuilder();

for (KVPair kv : kvList) {
Expand All @@ -1116,43 +1128,36 @@ public synchronized TxHeader setAll(List<KVPair> kvList) throws CorruptedDataExc
reqBuilder.addKVs(kvBuilder.build());
}

final ImmudbProto.TxHeader txHdr = blockingStub.set(reqBuilder.build());

if (txHdr.getNentries() != kvList.size()) {
throw new CorruptedDataException();
}

return TxHeader.valueOf(txHdr);
return TxHeader.valueOf(blockingStub.set(reqBuilder.build()));
}

public TxHeader setReference(String key, String referencedKey) throws CorruptedDataException {
/**
*
* @param key
* @param referencedKey
* @return
*/
public TxHeader setReference(String key, String referencedKey) {
return setReference(Utils.toByteArray(key), Utils.toByteArray(referencedKey));
}

public TxHeader setReference(byte[] key, byte[] referencedKey) throws CorruptedDataException {
public TxHeader setReference(byte[] key, byte[] referencedKey) {
return setReference(key, referencedKey, 0);
}

public TxHeader setReference(String key, String referencedKey, long atTx) throws CorruptedDataException {
public TxHeader setReference(String key, String referencedKey, long atTx) {
return setReference(Utils.toByteArray(key), Utils.toByteArray(referencedKey), atTx);
}

public synchronized TxHeader setReference(byte[] key, byte[] referencedKey, long atTx)
throws CorruptedDataException {
public synchronized TxHeader setReference(byte[] key, byte[] referencedKey, long atTx) {
final ImmudbProto.ReferenceRequest req = ImmudbProto.ReferenceRequest.newBuilder()
.setKey(Utils.toByteString(key))
.setReferencedKey(Utils.toByteString(referencedKey))
.setAtTx(atTx)
.setBoundRef(atTx > 0)
.build();

final ImmudbProto.TxHeader txHdr = blockingStub.setReference(req);

if (txHdr.getNentries() != 1) {
throw new CorruptedDataException();
}

return TxHeader.valueOf(txHdr);
return TxHeader.valueOf(blockingStub.setReference(req));
}

public TxHeader verifiedSet(String key, byte[] value) throws VerificationException {
Expand Down Expand Up @@ -1307,29 +1312,24 @@ private ImmuState verifyDualProof(ImmudbProto.VerifiableTx vtx, Tx tx, ImmuState
// ========== Z ==========
//

public TxHeader zAdd(String set, String key, double score) throws CorruptedDataException {
public TxHeader zAdd(String set, String key, double score) {
return zAdd(Utils.toByteArray(set), Utils.toByteArray(key), score);
}

public TxHeader zAdd(byte[] set, byte[] key, double score) throws CorruptedDataException {
public TxHeader zAdd(byte[] set, byte[] key, double score) {
return zAdd(set, key, 0, score);
}

public synchronized TxHeader zAdd(byte[] set, byte[] key, long atTx, double score) throws CorruptedDataException {
final ImmudbProto.TxHeader txHdr = blockingStub.zAdd(
ImmudbProto.ZAddRequest.newBuilder()
.setSet(Utils.toByteString(set))
.setKey(Utils.toByteString(key))
.setAtTx(atTx)
.setScore(score)
.setBoundRef(atTx > 0)
.build());

if (txHdr.getNentries() != 1) {
throw new CorruptedDataException();
}
public synchronized TxHeader zAdd(byte[] set, byte[] key, long atTx, double score) {
ImmudbProto.ZAddRequest req = ImmudbProto.ZAddRequest.newBuilder()
.setSet(Utils.toByteString(set))
.setKey(Utils.toByteString(key))
.setAtTx(atTx)
.setScore(score)
.setBoundRef(atTx > 0)
.build();

return TxHeader.valueOf(txHdr);
return TxHeader.valueOf(blockingStub.zAdd(req));
}

public TxHeader verifiedZAdd(String set, String key, double score) throws VerificationException {
Expand Down Expand Up @@ -1693,12 +1693,12 @@ public ZEntry next() {
// ========== STREAM SET ==========
//

public TxHeader streamSet(String key, byte[] value) throws InterruptedException, CorruptedDataException {
public TxHeader streamSet(String key, byte[] value) throws InterruptedException {
return streamSet(Utils.toByteArray(key), value);
}

public synchronized TxHeader streamSet(byte[] key, byte[] value)
throws InterruptedException, CorruptedDataException {
throws InterruptedException {
final LatchHolder<ImmudbProto.TxHeader> latchHolder = new LatchHolder<>();
final StreamObserver<Chunk> streamObserver = nonBlockingStub.streamSet(txHeaderStreamObserver(latchHolder));

Expand All @@ -1707,16 +1707,10 @@ public synchronized TxHeader streamSet(byte[] key, byte[] value)

streamObserver.onCompleted();

final ImmudbProto.TxHeader txHdr = latchHolder.awaitValue();

if (txHdr.getNentries() != 1) {
throw new CorruptedDataException();
}

return TxHeader.valueOf(txHdr);
return TxHeader.valueOf(latchHolder.awaitValue());
}

public synchronized TxHeader streamSetAll(List<KVPair> kvList) throws InterruptedException, CorruptedDataException {
public synchronized TxHeader streamSetAll(List<KVPair> kvList) throws InterruptedException {
final LatchHolder<ImmudbProto.TxHeader> latchHolder = new LatchHolder<>();
final StreamObserver<Chunk> streamObserver = nonBlockingStub.streamSet(txHeaderStreamObserver(latchHolder));

Expand All @@ -1727,13 +1721,7 @@ public synchronized TxHeader streamSetAll(List<KVPair> kvList) throws Interrupte

streamObserver.onCompleted();

final ImmudbProto.TxHeader txHdr = latchHolder.awaitValue();

if (txHdr.getNentries() != kvList.size()) {
throw new CorruptedDataException();
}

return TxHeader.valueOf(txHdr);
return TxHeader.valueOf(latchHolder.awaitValue());
}

//
Expand Down

This file was deleted.

11 changes: 3 additions & 8 deletions src/test/java/io/codenotary/immudb4j/BasicImmuClientTest.java
Expand Up @@ -16,7 +16,6 @@
package io.codenotary.immudb4j;

import com.google.common.base.Charsets;
import io.codenotary.immudb4j.exceptions.CorruptedDataException;
import io.codenotary.immudb4j.exceptions.KeyNotFoundException;
import io.codenotary.immudb4j.exceptions.VerificationException;
import io.grpc.StatusRuntimeException;
Expand All @@ -30,7 +29,7 @@
public class BasicImmuClientTest extends ImmuClientIntegrationTest {

@Test(testName = "set, get")
public void t1() throws VerificationException, CorruptedDataException, InterruptedException {
public void t1() throws VerificationException, InterruptedException {
immuClient.openSession("defaultdb", "immudb", "immudb");

byte[] v0 = new byte[] { 0, 1, 2, 3 };
Expand Down Expand Up @@ -130,12 +129,8 @@ public void t2() {
kvListBuilder.add(keys.get(i), values.get(i));
}

try {
immuClient.setAll(kvListBuilder.entries());
} catch (CorruptedDataException e) {
Assert.fail("Failed at setAll.", e);
}

immuClient.setAll(kvListBuilder.entries());

List<Entry> getAllResult = immuClient.getAll(keys);

Assert.assertNotNull(getAllResult);
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/io/codenotary/immudb4j/ExceptionsTest.java
@@ -1,6 +1,5 @@
package io.codenotary.immudb4j;

import io.codenotary.immudb4j.exceptions.CorruptedDataException;
import io.codenotary.immudb4j.exceptions.VerificationException;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand All @@ -9,12 +8,6 @@ public class ExceptionsTest {

@Test
public void t1() {
CorruptedDataException cdex = new CorruptedDataException();
Assert.assertNull(cdex.getMessage());
}

@Test
public void t2() {
String errorMsg = "data is corrupted";
VerificationException vex = new VerificationException(errorMsg);
Assert.assertEquals(errorMsg, vex.getMessage());
Expand Down
27 changes: 11 additions & 16 deletions src/test/java/io/codenotary/immudb4j/HistoryTest.java
Expand Up @@ -15,7 +15,6 @@
*/
package io.codenotary.immudb4j;

import io.codenotary.immudb4j.exceptions.CorruptedDataException;
import io.codenotary.immudb4j.exceptions.KeyNotFoundException;
import org.testng.Assert;
import org.testng.annotations.Test;
Expand All @@ -31,19 +30,15 @@ public class HistoryTest extends ImmuClientIntegrationTest {
public void t1() {
immuClient.openSession("defaultdb", "immudb", "immudb");

byte[] value1 = {0, 1, 2, 3};
byte[] value2 = {4, 5, 6, 7};
byte[] value3 = {8, 9, 10, 11};
byte[] value1 = { 0, 1, 2, 3 };
byte[] value2 = { 4, 5, 6, 7 };
byte[] value3 = { 8, 9, 10, 11 };

try {
immuClient.set("history1", value1);
immuClient.set("history1", value2);
immuClient.set("history2", value1);
immuClient.set("history2", value2);
immuClient.set("history2", value3);
} catch (CorruptedDataException e) {
Assert.fail("Failed at set.", e);
}
immuClient.set("history1", value1);
immuClient.set("history1", value2);
immuClient.set("history2", value1);
immuClient.set("history2", value2);
immuClient.set("history2", value3);

List<Entry> historyResponse1 = immuClient.historyAll("history1", 0, false, 2);

Expand Down Expand Up @@ -93,11 +88,11 @@ public void t1() {
} catch (KeyNotFoundException e) {
// exception is expected here
}

Iterator<Entry> entriesIt2 = immuClient.history("nonExisting", 0, false, 0);

Assert.assertFalse(entriesIt2.hasNext());

immuClient.closeSession();
}

Expand Down
17 changes: 5 additions & 12 deletions src/test/java/io/codenotary/immudb4j/MultidatabaseTest.java
Expand Up @@ -15,7 +15,6 @@
*/
package io.codenotary.immudb4j;

import io.codenotary.immudb4j.exceptions.CorruptedDataException;
import io.codenotary.immudb4j.exceptions.VerificationException;
import io.grpc.StatusRuntimeException;

Expand Down Expand Up @@ -58,22 +57,16 @@ public void t5() throws VerificationException {
immuClient.openSession("db1", "immudb", "immudb");

byte[] v0 = new byte[] { 0, 1, 2, 3 };
try {
immuClient.set("k0", v0);
} catch (CorruptedDataException e) {
Assert.fail("Failed at set.", e);
}

immuClient.set("k0", v0);

immuClient.closeSession();

immuClient.openSession("db2", "immudb", "immudb");

byte[] v1 = new byte[] { 3, 2, 1, 0 };
try {
immuClient.set("k1", v1);
} catch (CorruptedDataException e) {
Assert.fail("Failed at set.", e);
}

immuClient.set("k1", v1);

immuClient.closeSession();

Expand Down Expand Up @@ -123,7 +116,7 @@ public void t6() {
} catch (StatusRuntimeException e) {
Assert.assertTrue(e.getMessage().contains("database does not exist"));
}

immuClient.closeSession();
}
}

0 comments on commit 2510747

Please sign in to comment.