Search in sources :

Example 1 with HLLValue

use of com.aerospike.client.Value.HLLValue in project aerospike-client-java by aerospike.

the class TestOperateHll method operateGetUnion.

@Test
public void operateGetUnion() {
    int nIndexBits = 14;
    long expectedUnionCount = 0;
    ArrayList<List<Value>> vals = new ArrayList<List<Value>>();
    List<HLLValue> hlls = new ArrayList<HLLValue>();
    for (int i = 0; i < keys.length; i++) {
        ArrayList<Value> subVals = new ArrayList<Value>();
        for (int j = 0; j < nEntries / 3; j++) {
            subVals.add(new StringValue("key" + i + " " + j));
        }
        Record record = assertSuccess("init other keys", keys[i], Operation.delete(), HLLOperation.add(HLLPolicy.Default, binName, subVals, nIndexBits), Operation.get(binName));
        List<?> resultList = record.getList(binName);
        hlls.add((HLLValue) resultList.get(1));
        expectedUnionCount += subVals.size();
        vals.add(subVals);
    }
    // Keep record around win binName is removed.
    assertSuccess("other bin", key, Operation.delete(), HLLOperation.init(HLLPolicy.Default, binName + "other", nIndexBits), HLLOperation.add(HLLPolicy.Default, binName, vals.get(0), nIndexBits));
    Record record = assertSuccess("union and unionCount", key, HLLOperation.getUnion(binName, hlls), HLLOperation.getUnionCount(binName, hlls));
    List<?> resultList = record.getList(binName);
    long unionCount = (Long) resultList.get(1);
    assertHLLCount("verify union count", nIndexBits, unionCount, expectedUnionCount);
    HLLValue unionHll = (HLLValue) resultList.get(0);
    record = assertSuccess("", key, Operation.put(new Bin(binName, unionHll)), HLLOperation.getCount(binName));
    resultList = record.getList(binName);
    long unionCount2 = (Long) resultList.get(1);
    assertEquals("unions equal", unionCount, unionCount2);
}
Also used : HLLValue(com.aerospike.client.Value.HLLValue) Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) HLLValue(com.aerospike.client.Value.HLLValue) StringValue(com.aerospike.client.Value.StringValue) Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) List(java.util.List) Record(com.aerospike.client.Record) StringValue(com.aerospike.client.Value.StringValue) Test(org.junit.Test)

Example 2 with HLLValue

use of com.aerospike.client.Value.HLLValue in project aerospike-client-java by aerospike.

the class TestOperateHll method operateIntersectHLL.

@Test
public void operateIntersectHLL() {
    String otherBinName = binName + "other";
    for (ArrayList<Integer> desc : legalDescriptions) {
        int indexBits = desc.get(0);
        int minhashBits = desc.get(1);
        if (minhashBits != 0) {
            break;
        }
        Record record = assertSuccess("init", key, Operation.delete(), HLLOperation.add(HLLPolicy.Default, binName, entries, indexBits, minhashBits), Operation.get(binName), HLLOperation.add(HLLPolicy.Default, otherBinName, entries, indexBits, 4), Operation.get(otherBinName));
        List<HLLValue> hlls = new ArrayList<HLLValue>();
        List<HLLValue> hmhs = new ArrayList<HLLValue>();
        List<?> resultList = record.getList(binName);
        hlls.add((HLLValue) resultList.get(1));
        hlls.add(hlls.get(0));
        resultList = record.getList(otherBinName);
        hmhs.add((HLLValue) resultList.get(1));
        hmhs.add(hmhs.get(0));
        record = assertSuccess("intersect", key, HLLOperation.getIntersectCount(binName, hlls), HLLOperation.getSimilarity(binName, hlls));
        resultList = record.getList(binName);
        long intersectCount = (Long) resultList.get(0);
        assertTrue("intersect value too high", intersectCount < 1.8 * entries.size());
        hlls.add(hlls.get(0));
        assertThrows("Expect parameter error", key, AerospikeException.class, ResultCode.PARAMETER_ERROR, HLLOperation.getIntersectCount(binName, hlls));
        assertThrows("Expect parameter error", key, AerospikeException.class, ResultCode.PARAMETER_ERROR, HLLOperation.getSimilarity(binName, hlls));
        record = assertSuccess("intersect", key, HLLOperation.getIntersectCount(binName, hmhs), HLLOperation.getSimilarity(binName, hmhs));
        resultList = record.getList(binName);
        intersectCount = (Long) resultList.get(0);
        assertTrue("intersect value too high", intersectCount < 1.8 * entries.size());
        hmhs.add(hmhs.get(0));
        assertThrows("Expect parameter error", key, AerospikeException.class, ResultCode.OP_NOT_APPLICABLE, HLLOperation.getIntersectCount(binName, hmhs));
        assertThrows("Expect parameter error", key, AerospikeException.class, ResultCode.OP_NOT_APPLICABLE, HLLOperation.getSimilarity(binName, hmhs));
    }
}
Also used : HLLValue(com.aerospike.client.Value.HLLValue) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) Test(org.junit.Test)

Example 3 with HLLValue

use of com.aerospike.client.Value.HLLValue in project aerospike-client-java by aerospike.

the class TestOperateHll method operateEmptySimilarity.

@Test
public void operateEmptySimilarity() {
    for (ArrayList<Integer> desc : legalDescriptions) {
        int nIndexBits = desc.get(0);
        int nMinhashBits = desc.get(1);
        Record record = assertSuccess("init", key, Operation.delete(), HLLOperation.init(HLLPolicy.Default, binName, nIndexBits, nMinhashBits), Operation.get(binName));
        List<?> resultList = record.getList(binName);
        List<HLLValue> hlls = new ArrayList<HLLValue>();
        hlls.add((HLLValue) resultList.get(1));
        record = assertSuccess("test", key, HLLOperation.getSimilarity(binName, hlls), HLLOperation.getIntersectCount(binName, hlls));
        resultList = record.getList(binName);
        double sim = (Double) resultList.get(0);
        long intersectCount = (Long) resultList.get(1);
        String msg = "(" + nIndexBits + ", " + nMinhashBits + ")";
        assertEquals(msg, 0, intersectCount);
        assertEquals(msg, Double.NaN, sim, 0.0);
    }
}
Also used : HLLValue(com.aerospike.client.Value.HLLValue) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) Test(org.junit.Test)

Example 4 with HLLValue

use of com.aerospike.client.Value.HLLValue in project aerospike-client-java by aerospike.

the class TestExpOperation method expReturnsHLL.

@Test
public void expReturnsHLL() {
    Expression exp = Exp.build(HLLExp.init(HLLPolicy.Default, Exp.val(4), Exp.nil()));
    Record record = client.operate(null, keyA, HLLOperation.init(HLLPolicy.Default, binH, 4), ExpOperation.write(binC, exp, ExpWriteFlags.DEFAULT), Operation.get(binH), Operation.get(binC), ExpOperation.read(expVar, exp, ExpReadFlags.DEFAULT));
    assertRecordFound(keyA, record);
    // System.out.println(record);
    List<?> results = record.getList(binH);
    HLLValue valH = (HLLValue) results.get(1);
    results = record.getList(binC);
    HLLValue valC = (HLLValue) results.get(1);
    HLLValue valExp = record.getHLLValue(expVar);
    String resultString = "bytes not equal";
    assertArrayEquals(resultString, valH.getBytes(), valC.getBytes());
    assertArrayEquals(resultString, valH.getBytes(), valExp.getBytes());
    record = client.operate(null, keyA, ExpOperation.read(expVar, exp, ExpReadFlags.DEFAULT));
    valExp = record.getHLLValue(expVar);
    assertArrayEquals(resultString, valH.getBytes(), valExp.getBytes());
}
Also used : HLLValue(com.aerospike.client.Value.HLLValue) Expression(com.aerospike.client.exp.Expression) Record(com.aerospike.client.Record) Test(org.junit.Test)

Example 5 with HLLValue

use of com.aerospike.client.Value.HLLValue in project aerospike-client-java by aerospike.

the class TestOperateHll method operateSetUnionFlags.

@Test
public void operateSetUnionFlags() {
    int nIndexBits = 6;
    int lowNBits = 4;
    int highNBits = 8;
    String otherName = binName + "o";
    // Keep record around win binName is removed.
    ArrayList<HLLValue> hlls = new ArrayList<HLLValue>();
    Record record = assertSuccess("other bin", key, Operation.delete(), HLLOperation.add(HLLPolicy.Default, otherName, entries, nIndexBits), Operation.get(otherName));
    List<?> resultList = record.getList(otherName);
    HLLValue hll = (HLLValue) resultList.get(1);
    hlls.add(hll);
    // create_only
    HLLPolicy c = new HLLPolicy(HLLWriteFlags.CREATE_ONLY);
    assertSuccess("create_only", key, HLLOperation.setUnion(c, binName, hlls));
    assertThrows("create_only - error", key, AerospikeException.class, ResultCode.BIN_EXISTS_ERROR, HLLOperation.setUnion(c, binName, hlls));
    // update_only
    HLLPolicy u = new HLLPolicy(HLLWriteFlags.UPDATE_ONLY);
    assertSuccess("update_only", key, HLLOperation.setUnion(u, binName, hlls));
    assertSuccess("remove bin", key, Operation.put(Bin.asNull(binName)));
    assertThrows("update_only - error", key, AerospikeException.class, ResultCode.BIN_NOT_FOUND, HLLOperation.setUnion(u, binName, hlls));
    // create_only no_fail
    HLLPolicy cn = new HLLPolicy(HLLWriteFlags.CREATE_ONLY | HLLWriteFlags.NO_FAIL);
    assertSuccess("create_only nofail", key, HLLOperation.setUnion(cn, binName, hlls));
    assertSuccess("create_only nofail - no error", key, HLLOperation.setUnion(cn, binName, hlls));
    // update_only no_fail
    HLLPolicy un = new HLLPolicy(HLLWriteFlags.UPDATE_ONLY | HLLWriteFlags.NO_FAIL);
    assertSuccess("update_only nofail", key, HLLOperation.setUnion(un, binName, hlls));
    assertSuccess("remove bin", key, Operation.put(Bin.asNull(binName)));
    assertSuccess("update_only nofail - no error", key, HLLOperation.setUnion(un, binName, hlls));
    // fold
    HLLPolicy f = new HLLPolicy(HLLWriteFlags.ALLOW_FOLD);
    // fold down
    assertSuccess("size up", key, HLLOperation.init(HLLPolicy.Default, binName, highNBits));
    assertSuccess("fold down to index_bits", key, HLLOperation.setUnion(f, binName, hlls));
    // fold up
    assertSuccess("size down", key, HLLOperation.init(HLLPolicy.Default, binName, lowNBits));
    assertSuccess("fold down to low_n_bits", key, HLLOperation.setUnion(f, binName, hlls));
}
Also used : HLLValue(com.aerospike.client.Value.HLLValue) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) HLLPolicy(com.aerospike.client.operation.HLLPolicy) Test(org.junit.Test)

Aggregations

Record (com.aerospike.client.Record)12 HLLValue (com.aerospike.client.Value.HLLValue)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)7 Value (com.aerospike.client.Value)3 List (java.util.List)3 Bin (com.aerospike.client.Bin)2 StringValue (com.aerospike.client.Value.StringValue)2 HLLPolicy (com.aerospike.client.operation.HLLPolicy)2 Key (com.aerospike.client.Key)1 Operation (com.aerospike.client.Operation)1 Expression (com.aerospike.client.exp.Expression)1 HLLOperation (com.aerospike.client.operation.HLLOperation)1