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>
  • Loading branch information
jeroiraz committed Nov 26, 2022
1 parent d6d7c89 commit 16951ab
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 65 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
37 changes: 23 additions & 14 deletions src/main/java/io/codenotary/immudb4j/ImmuClient.java
Expand Up @@ -972,31 +972,38 @@ 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 (seekKey != null) {
reqBuilder.setSeekKey(Utils.toByteString(seekKey));
}

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

if (minScore != null) {
reqBuilder.setMinScore(Score.newBuilder().setScore(minScore).build());
}
Expand All @@ -1010,10 +1017,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 @@ -1375,32 +1378,38 @@ 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 (seekKey != null) {
reqBuilder.setSeekKey(Utils.toByteString(seekKey));
}

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

if (minScore != null) {
reqBuilder.setMinScore(Score.newBuilder().setScore(minScore).build());
}
Expand Down
51 changes: 1 addition & 50 deletions src/test/java/io/codenotary/immudb4j/ScanTest.java
Expand Up @@ -25,7 +25,7 @@

public class ScanTest extends ImmuClientIntegrationTest {

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

Expand Down Expand Up @@ -66,53 +66,4 @@ public void t1() {
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);
}

List<ZEntry> zScan1 = immuClient.zScanAll("set1", false, 5);
Assert.assertEquals(zScan1.size(), 2);

Assert.assertEquals(zScan1.get(0).getSet(), "set1".getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(zScan1.get(0).getKey(), "zadd1".getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(zScan1.get(0).getScore(), 1.0);
Assert.assertEquals(zScan1.get(0).getAtTx(), 0);
Assert.assertEquals(zScan1.get(0).getEntry().getValue(), value1);

List<ZEntry> zScan2 = immuClient.zScanAll("set2");
Assert.assertEquals(zScan2.size(), 2);

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

while (zScan3.hasNext()) {
Assert.assertEquals(zScan3.next().getKey(), zScan2.get(i).getKey());
i++;
}

Assert.assertEquals(i, 2);

immuClient.closeSession();
}

}
77 changes: 77 additions & 0 deletions src/test/java/io/codenotary/immudb4j/ZScanTest.java
@@ -0,0 +1,77 @@
/*
Copyright 2022 CodeNotary, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.codenotary.immudb4j;

import io.codenotary.immudb4j.exceptions.CorruptedDataException;
import org.testng.Assert;
import org.testng.annotations.Test;

import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.List;

public class ZScanTest extends ImmuClientIntegrationTest {

@Test(testName = "set, zAdd, zScan")
public void t1() {
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);
}

List<ZEntry> zScan1 = immuClient.zScanAll("set1", false, 5);
Assert.assertEquals(zScan1.size(), 2);

Assert.assertEquals(zScan1.get(0).getSet(), "set1".getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(zScan1.get(0).getKey(), "zadd1".getBytes(StandardCharsets.UTF_8));
Assert.assertEquals(zScan1.get(0).getScore(), 1.0);
Assert.assertEquals(zScan1.get(0).getAtTx(), 0);
Assert.assertEquals(zScan1.get(0).getEntry().getValue(), value1);

List<ZEntry> zScan2 = immuClient.zScanAll("set2");
Assert.assertEquals(zScan2.size(), 2);

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

while (zScan3.hasNext()) {
Assert.assertEquals(zScan3.next().getKey(), zScan2.get(i).getKey());
i++;
}

Assert.assertEquals(i, 2);

immuClient.closeSession();
}

}
1 change: 1 addition & 0 deletions tests.sh
Expand Up @@ -32,6 +32,7 @@ TESTS="${TESTS} TxTest"
TESTS="${TESTS} UserMgmtTest"
TESTS="${TESTS} VerifiedSetAndGetTest"
TESTS="${TESTS} ZAddTest"
TESTS="${TESTS} ZScanTest"

# -----------------------------------------------------------------------------

Expand Down

0 comments on commit 16951ab

Please sign in to comment.