Search in sources :

Example 26 with Bin

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

the class TestListMap method mapStrings.

@Test
public void mapStrings() {
    Key key = new Key(args.namespace, args.set, "mapkey1");
    client.delete(null, key);
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("key1", "string1");
    map.put("key2", "loooooooooooooooooooooooooongerstring2");
    map.put("key3", "string3");
    Bin bin = new Bin(args.getBinName("mapbin1"), map);
    client.put(null, key, bin);
    Record record = client.get(null, key, bin.name);
    Map<?, ?> receivedMap = (Map<?, ?>) record.getValue(bin.name);
    assertEquals(3, receivedMap.size());
    assertEquals("string1", receivedMap.get("key1"));
    assertEquals("loooooooooooooooooooooooooongerstring2", receivedMap.get("key2"));
    assertEquals("string3", receivedMap.get("key3"));
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 27 with Bin

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

the class TestListMap method listStrings.

@Test
public void listStrings() {
    Key key = new Key(args.namespace, args.set, "listkey1");
    client.delete(null, key);
    ArrayList<String> list = new ArrayList<String>();
    list.add("string1");
    list.add("string2");
    list.add("string3");
    Bin bin = new Bin(args.getBinName("listbin1"), list);
    client.put(null, key, bin);
    Record record = client.get(null, key, bin.name);
    List<?> receivedList = (List<?>) record.getValue(bin.name);
    assertEquals(3, receivedList.size());
    assertEquals("string1", receivedList.get(0));
    assertEquals("string2", receivedList.get(1));
    assertEquals("string3", receivedList.get(2));
}
Also used : Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 28 with Bin

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

the class TestListMap method listMapCombined.

@Test
public void listMapCombined() {
    Key key = new Key(args.namespace, args.set, "listmapkey");
    client.delete(null, key);
    byte[] blob = new byte[] { 3, 52, 125 };
    ArrayList<Object> inner = new ArrayList<Object>();
    inner.add("string2");
    inner.add(5);
    HashMap<Object, Object> innerMap = new HashMap<Object, Object>();
    innerMap.put("a", 1);
    innerMap.put(2, "b");
    innerMap.put(3, blob);
    innerMap.put("list", inner);
    ArrayList<Object> list = new ArrayList<Object>();
    list.add("string1");
    list.add(8);
    list.add(inner);
    list.add(innerMap);
    Bin bin = new Bin(args.getBinName("listmapbin"), list);
    client.put(null, key, bin);
    Record record = client.get(null, key, bin.name);
    List<?> received = (List<?>) record.getValue(bin.name);
    assertEquals(4, received.size());
    assertEquals("string1", received.get(0));
    // Server convert numbers to long, so must expect long.
    assertEquals(8L, received.get(1));
    List<?> receivedInner = (List<?>) received.get(2);
    assertEquals(2, receivedInner.size());
    assertEquals("string2", receivedInner.get(0));
    assertEquals(5L, receivedInner.get(1));
    Map<?, ?> receivedMap = (Map<?, ?>) received.get(3);
    assertEquals(4, receivedMap.size());
    assertEquals(1L, receivedMap.get("a"));
    assertEquals("b", receivedMap.get(2L));
    assertArrayEquals(blob, (byte[]) receivedMap.get(3L));
    List<?> receivedInner2 = (List<?>) receivedMap.get("list");
    assertEquals(2, receivedInner2.size());
    assertEquals("string2", receivedInner2.get(0));
    assertEquals(5L, receivedInner2.get(1));
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 29 with Bin

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

the class TestListMap method sortedMapReplace.

@Test
public void sortedMapReplace() {
    Key key = new Key(args.namespace, args.set, "mapkey3");
    client.delete(null, key);
    List<Entry<Integer, String>> list = new ArrayList<Entry<Integer, String>>();
    list.add(new AbstractMap.SimpleEntry<Integer, String>(1, "s1"));
    list.add(new AbstractMap.SimpleEntry<Integer, String>(2, "s2"));
    list.add(new AbstractMap.SimpleEntry<Integer, String>(3, "s3"));
    WritePolicy policy = new WritePolicy();
    policy.recordExistsAction = RecordExistsAction.REPLACE;
    Bin bin = new Bin("mapbin", list, MapOrder.KEY_VALUE_ORDERED);
    client.put(policy, key, bin);
    Record record = client.get(null, key, bin.name);
    // System.out.println(record);
    // It's unfortunate that the client returns a tree map here because
    // KEY_VALUE_ORDERED is sorted by key and then by value.  TreeMap
    // does not support this sorting behavior...
    // 
    // TODO: Return List<Entry<?,?>> for KEY_VALUE_ORDERED maps.  This
    // would be a breaking change.
    TreeMap<?, ?> receivedMap = (TreeMap<?, ?>) record.getValue(bin.name);
    assertEquals(3, receivedMap.size());
    assertEquals("s1", receivedMap.get((long) 1));
    assertEquals("s2", receivedMap.get((long) 2));
    assertEquals("s3", receivedMap.get((long) 3));
}
Also used : Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) AbstractMap(java.util.AbstractMap) Entry(java.util.Map.Entry) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) Test(org.junit.Test)

Example 30 with Bin

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

the class TestListMap method mapComplex.

@Test
public void mapComplex() {
    Key key = new Key(args.namespace, args.set, "mapkey2");
    client.delete(null, key);
    byte[] blob = new byte[] { 3, 52, 125 };
    List<Integer> list = new ArrayList<Integer>();
    list.add(100034);
    list.add(12384955);
    list.add(3);
    list.add(512);
    HashMap<Object, Object> map = new HashMap<Object, Object>();
    map.put("key1", "string1");
    map.put("key2", 2);
    map.put("key3", blob);
    // map.put("key4", Value.getAsList(list)) works too
    map.put("key4", list);
    map.put("key5", true);
    map.put("key6", false);
    Bin bin = new Bin(args.getBinName("mapbin2"), map);
    client.put(null, key, bin);
    Record record = client.get(null, key, bin.name);
    Map<?, ?> receivedMap = (Map<?, ?>) record.getValue(bin.name);
    assertEquals(6, receivedMap.size());
    assertEquals("string1", receivedMap.get("key1"));
    // Server convert numbers to long, so must expect long.
    assertEquals(2L, receivedMap.get("key2"));
    assertArrayEquals(blob, (byte[]) receivedMap.get("key3"));
    List<?> receivedInner = (List<?>) receivedMap.get("key4");
    assertEquals(4, receivedInner.size());
    assertEquals(100034L, receivedInner.get(0));
    assertEquals(12384955L, receivedInner.get(1));
    assertEquals(3L, receivedInner.get(2));
    assertEquals(512L, receivedInner.get(3));
    assertEquals(true, receivedMap.get("key5"));
    assertEquals(false, receivedMap.get("key6"));
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Record(com.aerospike.client.Record) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) TreeMap(java.util.TreeMap) Map(java.util.Map) Key(com.aerospike.client.Key) Test(org.junit.Test)

Aggregations

Bin (com.aerospike.client.Bin)151 Key (com.aerospike.client.Key)129 Record (com.aerospike.client.Record)70 Test (org.junit.Test)54 AerospikeException (com.aerospike.client.AerospikeException)34 ArrayList (java.util.ArrayList)31 WritePolicy (com.aerospike.client.policy.WritePolicy)27 HashMap (java.util.HashMap)22 Value (com.aerospike.client.Value)20 List (java.util.List)15 BeforeClass (org.junit.BeforeClass)14 Map (java.util.Map)13 Policy (com.aerospike.client.policy.Policy)12 IndexTask (com.aerospike.client.task.IndexTask)11 WriteListener (com.aerospike.client.listener.WriteListener)5 RegisterTask (com.aerospike.client.task.RegisterTask)5 AbstractMap (java.util.AbstractMap)4 TreeMap (java.util.TreeMap)4 CTX (com.aerospike.client.cdt.CTX)3 BitPolicy (com.aerospike.client.operation.BitPolicy)3