Search in sources :

Example 26 with Value

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

the class TestAsyncOperate method asyncOperateList.

@Test
public void asyncOperateList() {
    final Key key = new Key(args.namespace, args.set, "aoplkey1");
    client.delete(eventLoop, new DeleteListener() {

        public void onSuccess(Key key, boolean existed) {
            List<Value> itemList = new ArrayList<Value>();
            itemList.add(Value.get(55));
            itemList.add(Value.get(77));
            client.operate(eventLoop, new ReadHandler(), null, key, ListOperation.appendItems(binName, itemList), ListOperation.pop(binName, -1), ListOperation.size(binName));
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, key);
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Value(com.aerospike.client.Value) DeleteListener(com.aerospike.client.listener.DeleteListener) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 27 with Value

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

the class TestAsyncOperate method asyncOperateMap.

@Test
public void asyncOperateMap() {
    final Key key = new Key(args.namespace, args.set, "aopmkey1");
    client.delete(eventLoop, new DeleteListener() {

        public void onSuccess(Key key, boolean existed) {
            Map<Value, Value> map = new HashMap<Value, Value>();
            map.put(Value.get("a"), Value.get(1));
            map.put(Value.get("b"), Value.get(2));
            map.put(Value.get("c"), Value.get(3));
            client.operate(eventLoop, new MapHandler(), null, key, MapOperation.putItems(MapPolicy.Default, binName, map), MapOperation.getByRankRange(binName, -1, 1, MapReturnType.KEY_VALUE));
        }

        public void onFailure(AerospikeException e) {
            setError(e);
            notifyComplete();
        }
    }, null, key);
    waitTillComplete();
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Value(com.aerospike.client.Value) DeleteListener(com.aerospike.client.listener.DeleteListener) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 28 with Value

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

the class TestSerialize method serializeComplex.

@Test
public void serializeComplex() {
    Key key = new Key(args.namespace, args.set, "serialcomplexkey");
    // Delete record if it already exists.
    client.delete(null, key);
    ArrayList<Object> inner = new ArrayList<Object>();
    inner.add("string2");
    inner.add(8);
    HashMap<Object, Object> innerMap = new HashMap<Object, Object>();
    innerMap.put("a", 1);
    innerMap.put(2, "b");
    innerMap.put("list", inner);
    ArrayList<Object> list = new ArrayList<Object>();
    list.add("string1");
    list.add(4);
    list.add(inner);
    list.add(innerMap);
    Bin bin = new Bin(args.getBinName("complexbin"), new Value.BlobValue(list));
    client.put(null, key, bin);
    Record record = client.get(null, key, bin.name);
    assertRecordFound(key, record);
    Object received = null;
    try {
        received = (List<?>) record.getValue(bin.name);
    } catch (Exception e) {
        fail("Failed to parse returned value: namespace=" + key.namespace + " set=" + key.setName + " key=" + key.userKey + " bin=" + bin.name);
    }
    if (received == null || !received.equals(list)) {
        fail("Data mismatch" + Environment.Newline + "Expected " + list + Environment.Newline + "Received " + received);
    }
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 29 with Value

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

the class TestLargeList method distinctBinsLargeList.

@Test
@SuppressWarnings("unchecked")
public void distinctBinsLargeList() {
    if (!args.validateLDT()) {
        return;
    }
    Key key = new Key(args.namespace, args.set, "accountId");
    // Delete record if it already exists.
    client.delete(null, key);
    // Initialize large list operator.
    LargeList list = client.getLargeList(null, key, "trades");
    // Write trades
    Map<String, Value> map = new HashMap<String, Value>();
    Calendar timestamp1 = new GregorianCalendar(2014, 6, 25, 12, 18, 43);
    map.put("key", Value.get(timestamp1.getTimeInMillis()));
    map.put("ticker", Value.get("IBM"));
    map.put("qty", Value.get(100));
    map.put("price", Value.get(Double.doubleToLongBits(181.82)));
    list.add(Value.get(map));
    Calendar timestamp2 = new GregorianCalendar(2014, 6, 26, 9, 33, 17);
    map.put("key", Value.get(timestamp2.getTimeInMillis()));
    map.put("ticker", Value.get("GE"));
    map.put("qty", Value.get(500));
    map.put("price", Value.get(Double.doubleToLongBits(26.36)));
    list.add(Value.get(map));
    Calendar timestamp3 = new GregorianCalendar(2014, 6, 27, 14, 40, 19);
    map.put("key", Value.get(timestamp3.getTimeInMillis()));
    map.put("ticker", Value.get("AAPL"));
    map.put("qty", Value.get(75));
    map.put("price", Value.get(Double.doubleToLongBits(91.85)));
    list.add(Value.get(map));
    // Verify list size
    int size = list.size();
    assertEquals(3, size);
    // Filter on range of timestamps
    Calendar begin = new GregorianCalendar(2014, 6, 26);
    Calendar end = new GregorianCalendar(2014, 6, 28);
    List<Map<String, Object>> results = (List<Map<String, Object>>) list.range(Value.get(begin.getTimeInMillis()), Value.get(end.getTimeInMillis()));
    assertNotNull(results);
    assertEquals(2, results.size());
    // Verify data.
    validateWithDistinctBins(results, 0, timestamp2, "GE", 500, 26.36);
    validateWithDistinctBins(results, 1, timestamp3, "AAPL", 75, 91.85);
    List<Map<String, Object>> rows = (List<Map<String, Object>>) list.scan();
    for (Map<String, Object> row : rows) {
        for (@SuppressWarnings("unused") Map.Entry<String, Object> entry : row.entrySet()) {
        //console.Info(entry.Key.ToString());
        //console.Info(entry.Value.ToString());
        }
    }
}
Also used : HashMap(java.util.HashMap) LargeList(com.aerospike.client.large.LargeList) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Value(com.aerospike.client.Value) ArrayList(java.util.ArrayList) LargeList(com.aerospike.client.large.LargeList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 30 with Value

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

the class TestLargeList method simpleLargeList.

@Test
public void simpleLargeList() {
    if (!args.validateLDT()) {
        return;
    }
    Key key = new Key(args.namespace, args.set, "setkey");
    // Delete record if it already exists.
    client.delete(null, key);
    // Initialize large set operator.
    LargeList llist = client.getLargeList(null, key, binName);
    String orig1 = "llistValue1";
    String orig2 = "llistValue2";
    String orig3 = "llistValue3";
    // Write values.
    llist.add(Value.get(orig1));
    llist.add(Value.get(orig2));
    llist.add(Value.get(orig3));
    // Perform exists.
    boolean b = llist.exists(Value.get(orig2));
    assertTrue(b);
    b = llist.exists(Value.get("notfound"));
    assertFalse(b);
    // Test record not found.
    LargeList nflist = client.getLargeList(null, new Key(args.namespace, args.set, "sfdfdqw"), binName);
    try {
        b = nflist.exists(Value.get(orig2));
        assertFalse(b);
    } catch (AerospikeException ae) {
        assertEquals(ResultCode.KEY_NOT_FOUND_ERROR, ae.getResultCode());
    }
    List<Value> klist = new ArrayList<Value>();
    klist.add(Value.get(orig2));
    klist.add(Value.get(orig1));
    klist.add(Value.get("notfound"));
    List<Boolean> blist = llist.exists(klist);
    assertTrue(blist.get(0));
    assertTrue(blist.get(1));
    assertFalse(blist.get(2));
    // Test record not found.
    try {
        List<Boolean> blist2 = nflist.exists(klist);
        assertFalse(blist2.get(0));
        assertFalse(blist2.get(1));
        assertFalse(blist2.get(2));
    } catch (AerospikeException ae) {
        assertEquals(ResultCode.KEY_NOT_FOUND_ERROR, ae.getResultCode());
    }
    // Perform a Range Query -- look for "llistValue2" to "llistValue3"
    List<?> rangeList = llist.range(Value.get(orig2), Value.get(orig3));
    assertNotNull(rangeList);
    assertEquals(2, rangeList.size());
    String v2 = (String) rangeList.get(0);
    String v3 = (String) rangeList.get(1);
    assertEquals(orig2, v2);
    assertEquals(orig3, v3);
    // Remove last value.
    llist.remove(Value.get(orig3));
    int size = llist.size();
    assertEquals(2, size);
    List<?> listReceived = llist.find(Value.get(orig2));
    String expected = orig2;
    assertNotNull(listReceived);
    String stringReceived = (String) listReceived.get(0);
    assertNotNull(stringReceived);
    assertEquals(expected, stringReceived);
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) LargeList(com.aerospike.client.large.LargeList) ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Key(com.aerospike.client.Key) Test(org.junit.Test)

Aggregations

Value (com.aerospike.client.Value)44 Key (com.aerospike.client.Key)37 ArrayList (java.util.ArrayList)26 Test (org.junit.Test)26 Record (com.aerospike.client.Record)25 HashMap (java.util.HashMap)25 List (java.util.List)18 Bin (com.aerospike.client.Bin)9 Map (java.util.Map)7 Collections.singletonList (java.util.Collections.singletonList)6 AerospikeException (com.aerospike.client.AerospikeException)5 LargeList (com.aerospike.client.large.LargeList)5 Calendar (java.util.Calendar)4 GregorianCalendar (java.util.GregorianCalendar)4 MapPolicy (com.aerospike.client.cdt.MapPolicy)3 Entry (java.util.Map.Entry)3 LargeStack (com.aerospike.client.large.LargeStack)2 DeleteListener (com.aerospike.client.listener.DeleteListener)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2