Skip to content

Commit

Permalink
fix: zscanAll optional seekKey
Browse files Browse the repository at this point in the history
Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

chore: default non-inclusive seek

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

chore: zscanall optional seekKey

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

chore: scanall optional seekKey

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

fix: optional seekKey

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

chore: revert gradle version

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

test: split scan and zscan test cases

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

test: simplified zscan

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

test: simplified zscan

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>

minor code changes

Signed-off-by: Jeronimo Irazabal <jeronimo.irazabal@gmail.com>
  • Loading branch information
jeroiraz committed Nov 26, 2022
1 parent d6d7c89 commit 373a575
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
40 changes: 23 additions & 17 deletions src/main/java/io/codenotary/immudb4j/ImmuClient.java
Expand Up @@ -581,8 +581,8 @@ public synchronized List<Entry> historyAll(byte[] key, long offset, boolean desc
try {
ImmudbProto.Entries entries = blockingStub.history(ImmudbProto.HistoryRequest.newBuilder()
.setKey(Utils.toByteString(key))
.setOffset(offset)
.setDesc(desc)
.setOffset(offset)
.setLimit(limit)
.build());

Expand Down Expand Up @@ -633,8 +633,8 @@ public synchronized List<Entry> scanAll(byte[] prefix, byte[] seekKey, byte[] en
.setEndKey(Utils.toByteString(endKey))
.setInclusiveSeek(inclusiveSeek)
.setInclusiveEnd(inclusiveEnd)
.setLimit(limit)
.setDesc(desc)
.setLimit(limit)
.build();

final ImmudbProto.Entries entries = blockingStub.scan(req);
Expand Down Expand Up @@ -972,31 +972,35 @@ public List<ZEntry> zScanAll(String set) {
}

public List<ZEntry> zScanAll(String set, boolean reverse, long limit) {
return pzScanAll(Utils.toByteArray(set), null, null, 0, null, 0, true, reverse, limit);
return pzScanAll(Utils.toByteArray(set), null, null, null, null, 0, false, reverse, limit);
}

public List<ZEntry> zScanAll(byte[] set, double minScore, double maxScore, boolean reverse, long limit) {
return zScanAll(set, minScore, maxScore, 0, null, 0, true, false, 0);
return pzScanAll(set, minScore, maxScore, null, null, 0, false, false, 0);
}

public List<ZEntry> zScanAll(byte[] set, double minScore, double maxScore, double seekScore, byte[] seekKey,
long seekAtTx, boolean inclusiveSeek, boolean reverse, long limit) {
return pzScanAll(set, minScore, maxScore, seekScore, seekKey, seekAtTx, inclusiveSeek, reverse, limit);
}

private List<ZEntry> pzScanAll(byte[] set, Double minScore, Double maxScore, double seekScore, byte[] seekKey,
private synchronized List<ZEntry> pzScanAll(byte[] set, Double minScore, Double maxScore, Double seekScore,
byte[] seekKey,
long seekAtTx, boolean inclusiveSeek, boolean reverse, long limit) {

final ImmudbProto.ZScanRequest.Builder reqBuilder = ImmudbProto.ZScanRequest.newBuilder();

reqBuilder.setSet(Utils.toByteString(set))
.setSeekScore(seekScore)
.setSeekKey(Utils.toByteString(seekKey))
.setSeekAtTx(seekAtTx)
.setInclusiveSeek(inclusiveSeek)
.setDesc(reverse)
.setLimit(limit);

if (seekScore != null) {
reqBuilder.setSeekScore(seekScore);
}

if (minScore != null) {
reqBuilder.setMinScore(Score.newBuilder().setScore(minScore).build());
}
Expand All @@ -1010,10 +1014,6 @@ private List<ZEntry> pzScanAll(byte[] set, Double minScore, Double maxScore, dou
return buildList(zEntries);
}

//
// ========== TX ==========
//

public synchronized Tx txById(long txId) throws TxNotFoundException, NoSuchAlgorithmException {
try {
final ImmudbProto.Tx tx = blockingStub.txById(ImmudbProto.TxRequest.newBuilder().setTx(txId).build());
Expand Down Expand Up @@ -1099,16 +1099,18 @@ public synchronized Tx verifiedTxById(long txId) throws TxNotFoundException, Ver
public synchronized List<Tx> txScanAll(long initialTxId) {
final ImmudbProto.TxScanRequest req = ImmudbProto.TxScanRequest.newBuilder().setInitialTx(initialTxId).build();
final ImmudbProto.TxList txList = blockingStub.txScan(req);

return buildList(txList);
}

public synchronized List<Tx> txScanAll(long initialTxId, int limit, boolean desc) {
final ImmudbProto.TxScanRequest req = ImmudbProto.TxScanRequest
.newBuilder()
.setInitialTx(initialTxId)
.setLimit(limit)
.setDesc(desc)
.setLimit(limit)
.build();

final ImmudbProto.TxList txList = blockingStub.txScan(req);
return buildList(txList);
}
Expand Down Expand Up @@ -1351,6 +1353,7 @@ public Iterator<Entry> scan(byte[] prefix, byte[] seekKey, byte[] endKey, boolea
public synchronized Iterator<Entry> scan(byte[] prefix, byte[] seekKey, byte[] endKey, boolean inclusiveSeek,
boolean inclusiveEnd,
boolean desc, long limit) {

final ImmudbProto.ScanRequest req = ScanRequest.newBuilder()
.setPrefix(Utils.toByteString(prefix))
.setSeekKey(Utils.toByteString(seekKey))
Expand All @@ -1375,32 +1378,35 @@ public Iterator<ZEntry> zScan(String set) {
}

public Iterator<ZEntry> zScan(String set, boolean reverse, long limit) {
return pzScan(Utils.toByteArray(set), null, null, 0, null, 0, true, reverse, limit);
return pzScan(Utils.toByteArray(set), null, null, null, null, 0, false, reverse, limit);
}

public Iterator<ZEntry> zScan(byte[] set, double minScore, double maxScore, boolean reverse, long limit) {
return zScan(set, minScore, maxScore, 0, null, 0, true, false, 0);
return pzScan(set, minScore, maxScore, null, null, 0, false, false, 0);
}

public Iterator<ZEntry> zScan(byte[] set, double minScore, double maxScore, double seekScore, byte[] seekKey,
long seekAtTx, boolean inclusiveSeek, boolean reverse, long limit) {
return pzScan(set, minScore, maxScore, seekScore, seekKey, seekAtTx, inclusiveSeek, reverse, limit);
}

private synchronized Iterator<ZEntry> pzScan(byte[] set, Double minScore, Double maxScore, double seekScore,
private synchronized Iterator<ZEntry> pzScan(byte[] set, Double minScore, Double maxScore, Double seekScore,
byte[] seekKey,
long seekAtTx, boolean inclusiveSeek, boolean reverse, long limit) {

final ImmudbProto.ZScanRequest.Builder reqBuilder = ImmudbProto.ZScanRequest.newBuilder();

reqBuilder.setSet(Utils.toByteString(set))
.setSeekScore(seekScore)
.setSeekKey(Utils.toByteString(seekKey))
.setSeekAtTx(seekAtTx)
.setInclusiveSeek(inclusiveSeek)
.setDesc(reverse)
.setLimit(limit);

if (seekScore != null) {
reqBuilder.setSeekScore(seekScore);
}

if (minScore != null) {
reqBuilder.setMinScore(Score.newBuilder().setScore(minScore).build());
}
Expand All @@ -1427,9 +1433,9 @@ public synchronized Iterator<Entry> history(byte[] key, long offset, boolean des
try {
ImmudbProto.HistoryRequest req = ImmudbProto.HistoryRequest.newBuilder()
.setKey(Utils.toByteString(key))
.setLimit(limit)
.setOffset(offset)
.setDesc(desc)
.setOffset(offset)
.setLimit(limit)
.build();

final Iterator<Chunk> chunks = blockingStub.streamHistory(req);
Expand Down
54 changes: 24 additions & 30 deletions src/test/java/io/codenotary/immudb4j/ScanTest.java
Expand Up @@ -25,12 +25,12 @@

public class ScanTest extends ImmuClientIntegrationTest {

@Test(testName = "scan", priority = 2)
@Test(testName = "scan zscan")
public void t1() {
immuClient.openSession("defaultdb", "immudb", "immudb");

byte[] value1 = {0, 1, 2, 3};
byte[] value2 = {4, 5, 6, 7};
byte[] value1 = { 0, 1, 2, 3 };
byte[] value2 = { 4, 5, 6, 7 };

try {
immuClient.set("scan1", value1);
Expand All @@ -39,6 +39,23 @@ public void t1() {
Assert.fail("Failed at set.", e);
}

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

try {
immuClient.zAdd("set1", "zadd1", 1);
immuClient.zAdd("set1", "zadd2", 2);

immuClient.zAdd("set2", "zadd1", 2);
immuClient.zAdd("set2", "zadd2", 1);
} catch (CorruptedDataException e) {
Assert.fail("Failed to zAdd", e);
}

List<Entry> scanResult = immuClient.scanAll("scan");
System.out.println(scanResult.size());

Expand All @@ -63,32 +80,7 @@ public void t1() {

Assert.assertEquals(i, 2);

immuClient.closeSession();
}

@Test(testName = "set, zAdd, zScan", priority = 3)
public void t2() {
immuClient.openSession("defaultdb", "immudb", "immudb");

byte[] value1 = {0, 1, 2, 3};
byte[] value2 = {4, 5, 6, 7};

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

try {
immuClient.zAdd("set1", "zadd1", 1);
immuClient.zAdd("set1", "zadd2", 2);

immuClient.zAdd("set2", "zadd1", 2);
immuClient.zAdd("set2", "zadd2", 1);
} catch (CorruptedDataException e) {
Assert.fail("Failed to zAdd", e);
}
Assert.assertFalse(immuClient.scan("nonexistent-prefix").hasNext());

List<ZEntry> zScan1 = immuClient.zScanAll("set1", false, 5);
Assert.assertEquals(zScan1.size(), 2);
Expand All @@ -103,7 +95,7 @@ public void t2() {
Assert.assertEquals(zScan2.size(), 2);

Iterator<ZEntry> zScan3 = immuClient.zScan("set2");
int i = 0;
i = 0;

while (zScan3.hasNext()) {
Assert.assertEquals(zScan3.next().getKey(), zScan2.get(i).getKey());
Expand All @@ -112,6 +104,8 @@ public void t2() {

Assert.assertEquals(i, 2);

Assert.assertFalse(immuClient.zScan("nonexistent-set").hasNext());

immuClient.closeSession();
}

Expand Down

0 comments on commit 373a575

Please sign in to comment.