Search in sources :

Example 6 with Value

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

the class Serialize method testComplex.

/**
	 * Write complex object using standard java serializer.
	 */
public void testComplex(AerospikeClient client, Parameters params) throws Exception {
    Key key = new Key(params.namespace, params.set, "serialcomplexkey");
    // Delete record if it already exists.
    client.delete(params.writePolicy, key);
    console.info("Initialize complex object");
    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(params.getBinName("complexbin"), new Value.BlobValue(list));
    console.info("Write complex object using serializer.");
    client.put(params.writePolicy, key, bin);
    console.info("Read complex object using serializer.");
    Record record = client.get(params.policy, key, bin.name);
    if (record == null) {
        throw new Exception(String.format("Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey));
    }
    Object received;
    try {
        received = (List<?>) record.getValue(bin.name);
    } catch (Exception e) {
        throw new Exception(String.format("Failed to parse returned value: namespace=%s set=%s key=%s bin=%s", key.namespace, key.setName, key.userKey, bin.name));
    }
    if (received != null && received.equals(list)) {
        console.info("Data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received);
    } else {
        console.error("Data mismatch");
        console.error("Expected " + list);
        console.error("Received " + received);
    }
    console.info("Read complex object successful.");
}
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)

Example 7 with Value

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

the class TestLargeList method runWithSerializedBin.

@Test
@SuppressWarnings("unchecked")
public void runWithSerializedBin() throws IOException {
    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>();
    ByteArrayOutputStream byteStream = new ByteArrayOutputStream(500);
    DataOutputStream writer = new DataOutputStream(byteStream);
    Calendar timestamp1 = new GregorianCalendar(2014, 6, 25, 12, 18, 43);
    map.put("key", Value.get(timestamp1.getTimeInMillis()));
    // ticker
    writer.writeUTF("IBM");
    // qty
    writer.writeInt(100);
    // price
    writer.writeDouble(181.82);
    map.put("value", Value.get(byteStream.toByteArray()));
    list.add(Value.get(map));
    Calendar timestamp2 = new GregorianCalendar(2014, 6, 26, 9, 33, 17);
    map.put("key", Value.get(timestamp2.getTimeInMillis()));
    byteStream.reset();
    // ticker
    writer.writeUTF("GE");
    // qty
    writer.writeInt(500);
    // price
    writer.writeDouble(26.36);
    map.put("value", Value.get(byteStream.toByteArray()));
    list.add(Value.get(map));
    Calendar timestamp3 = new GregorianCalendar(2014, 6, 27, 14, 40, 19);
    map.put("key", Value.get(timestamp3.getTimeInMillis()));
    byteStream.reset();
    // ticker
    writer.writeUTF("AAPL");
    // qty
    writer.writeInt(75);
    // price
    writer.writeDouble(91.85);
    map.put("value", Value.get(byteStream.toByteArray()));
    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.
    validateWithSerializedBin(results, 0, timestamp2, "GE", 500, 26.36);
    validateWithSerializedBin(results, 1, timestamp3, "AAPL", 75, 91.85);
}
Also used : HashMap(java.util.HashMap) LargeList(com.aerospike.client.large.LargeList) DataOutputStream(java.io.DataOutputStream) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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 8 with Value

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

the class TestOperateList method operateList5.

@Test
public void operateList5() {
    // Test trim.
    Key key = new Key(args.namespace, args.set, "oplkey5");
    client.delete(null, key);
    List<Value> itemList = new ArrayList<Value>();
    itemList.add(Value.get("s11"));
    itemList.add(Value.get("s22222"));
    itemList.add(Value.get("s3333333"));
    itemList.add(Value.get("s4444444444"));
    itemList.add(Value.get("s5555555555555555"));
    Record record = client.operate(null, key, ListOperation.insertItems(binName, 0, itemList), ListOperation.trim(binName, -5, 5), ListOperation.trim(binName, 1, -5), ListOperation.trim(binName, 1, 2));
    assertRecordFound(key, record);
    //System.out.println("Record: " + record);
    List<?> list = record.getList(binName);
    long size = (Long) list.get(0);
    assertEquals(5, size);
    size = (Long) list.get(1);
    assertEquals(0, size);
    size = (Long) list.get(2);
    assertEquals(1, size);
    size = (Long) list.get(3);
    assertEquals(2, size);
}
Also used : ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 9 with Value

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

the class TestOperateList method operateList4.

@Test
public void operateList4() {
    // Test all value types.
    Key key = new Key(args.namespace, args.set, "oplkey4");
    client.delete(null, key);
    List<Value> inputList = new ArrayList<Value>();
    inputList.add(Value.get(12));
    inputList.add(Value.get(-8734.81));
    inputList.add(Value.get("my string"));
    Map<Integer, String> inputMap = new HashMap<Integer, String>();
    inputMap.put(9, "data 9");
    inputMap.put(-2, "data -2");
    byte[] bytes = "string bytes".getBytes();
    List<Value> itemList = new ArrayList<Value>();
    itemList.add(Value.get(true));
    itemList.add(Value.get(55));
    itemList.add(Value.get("string value"));
    itemList.add(Value.get(inputList));
    itemList.add(Value.get(bytes));
    itemList.add(Value.get(99.99));
    itemList.add(Value.get(inputMap));
    Record record = client.operate(null, key, ListOperation.appendItems(binName, itemList), ListOperation.getRange(binName, 0, 100), ListOperation.set(binName, 1, Value.get("88")), ListOperation.get(binName, 1), ListOperation.popRange(binName, -2, 1), ListOperation.popRange(binName, -1), ListOperation.remove(binName, 3), ListOperation.removeRange(binName, 0, 1), ListOperation.removeRange(binName, 2), ListOperation.size(binName));
    assertRecordFound(key, record);
    //System.out.println("Record: " + record);
    List<?> list = record.getList(binName);
    long size = (Long) list.get(0);
    assertEquals(7, size);
    List<?> rangeList = (List<?>) list.get(1);
    assertTrue((boolean) (Boolean) rangeList.get(0));
    assertEquals(55, (long) (Long) rangeList.get(1));
    assertEquals("string value", (String) rangeList.get(2));
    List<?> subList = (List<?>) rangeList.get(3);
    assertEquals(3, subList.size());
    assertEquals(12, (long) (Long) subList.get(0));
    assertEquals(-8734.81, (double) (Double) subList.get(1), 0.00001);
    assertEquals("my string", (String) subList.get(2));
    byte[] bt = (byte[]) rangeList.get(4);
    assertArrayEquals("bytes not equal", bytes, bt);
    assertEquals(99.99, (double) (Double) rangeList.get(5), 0.00001);
    Map<?, ?> subMap = (Map<?, ?>) rangeList.get(6);
    assertEquals(2, subMap.size());
    assertEquals("data 9", (String) subMap.get(9L));
    assertEquals("data -2", (String) subMap.get(-2L));
    // Set does not return a result.
    assertEquals("88", (String) list.get(2));
    subList = (List<?>) list.get(3);
    assertEquals(1, subList.size());
    assertEquals(99.99, (double) (Double) subList.get(0), 0.00001);
    subList = (List<?>) list.get(4);
    assertEquals(1, subList.size());
    assertTrue(subList.get(0) instanceof Map);
    assertEquals(1, (long) (Long) list.get(5));
    assertEquals(1, (long) (Long) list.get(6));
    assertEquals(1, (long) (Long) list.get(7));
    size = (Long) list.get(8);
    assertEquals(2, size);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 10 with Value

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

the class TestOperateList method operateList2.

@Test
public void operateList2() {
    Key key = new Key(args.namespace, args.set, "oplkey2");
    client.delete(null, key);
    List<Value> itemList = new ArrayList<Value>();
    itemList.add(Value.get(12));
    itemList.add(Value.get(-8734));
    itemList.add(Value.get("my string"));
    Record record = client.operate(null, key, ListOperation.appendItems(binName, itemList), Operation.put(new Bin("otherbin", "hello")));
    assertRecordFound(key, record);
    record = client.operate(null, key, ListOperation.insert(binName, -1, Value.get(8)), Operation.append(new Bin("otherbin", Value.get("goodbye"))), Operation.get("otherbin"), ListOperation.getRange(binName, 0, 4), ListOperation.getRange(binName, 3));
    assertRecordFound(key, record);
    //System.out.println("Record: " + record);
    String val = record.getString("otherbin");
    assertEquals("hellogoodbye", val);
    List<?> list = record.getList(binName);
    long size = (Long) list.get(0);
    assertEquals(4, size);
    List<?> rangeList = (List<?>) list.get(1);
    long lval = (Long) rangeList.get(0);
    assertEquals(12, lval);
    lval = (Long) rangeList.get(1);
    assertEquals(-8734, lval);
    lval = (Long) rangeList.get(2);
    assertEquals(8, lval);
    val = (String) rangeList.get(3);
    assertEquals("my string", val);
    rangeList = (List<?>) list.get(2);
    val = (String) rangeList.get(0);
    assertEquals("my string", val);
}
Also used : Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.aerospike.client.Key) Test(org.junit.Test)

Aggregations

Value (com.aerospike.client.Value)37 Key (com.aerospike.client.Key)30 HashMap (java.util.HashMap)23 ArrayList (java.util.ArrayList)19 Test (org.junit.Test)19 Record (com.aerospike.client.Record)18 List (java.util.List)13 Bin (com.aerospike.client.Bin)9 Map (java.util.Map)7 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 LargeStack (com.aerospike.client.large.LargeStack)2 DeleteListener (com.aerospike.client.listener.DeleteListener)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 DataOutputStream (java.io.DataOutputStream)2 LuaInputStream (com.aerospike.client.lua.LuaInputStream)1 LuaOutputStream (com.aerospike.client.lua.LuaOutputStream)1