Search in sources :

Example 66 with Record

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

the class TestOperateMap method operateMapMixed.

@Test
public void operateMapMixed() {
    // Test normal operations with map operations.
    Key key = new Key(args.namespace, args.set, "opmkey2");
    client.delete(null, key);
    Map<Value, Value> itemMap = new HashMap<Value, Value>();
    itemMap.put(Value.get(12), Value.get("myval"));
    itemMap.put(Value.get(-8734), Value.get("str2"));
    itemMap.put(Value.get(1), Value.get("my default"));
    itemMap.put(Value.get(7), Value.get(1));
    Record record = client.operate(null, key, MapOperation.putItems(new MapPolicy(MapOrder.KEY_VALUE_ORDERED, MapWriteFlags.DEFAULT), binName, itemMap), Operation.put(new Bin("otherbin", "hello")));
    assertRecordFound(key, record);
    long size = record.getLong(binName);
    assertEquals(4, size);
    record = client.operate(null, key, MapOperation.getByKey(binName, Value.get(12), MapReturnType.INDEX), Operation.append(new Bin("otherbin", Value.get("goodbye"))), Operation.get("otherbin"));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    long index = record.getLong(binName);
    assertEquals(3, index);
    List<?> results = record.getList("otherbin");
    String val = (String) results.get(1);
    assertEquals("hellogoodbye", val);
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) MapPolicy(com.aerospike.client.cdt.MapPolicy) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 67 with Record

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

the class TestOperateMap method operateMapRemove.

@Test
public void operateMapRemove() {
    // Test remove.
    Key key = new Key(args.namespace, args.set, "opmkey7");
    client.delete(null, key);
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get("Charlie"), Value.get(55));
    inputMap.put(Value.get("Jim"), Value.get(98));
    inputMap.put(Value.get("John"), Value.get(76));
    inputMap.put(Value.get("Harry"), Value.get(82));
    inputMap.put(Value.get("Sally"), Value.get(79));
    inputMap.put(Value.get("Lenny"), Value.get(84));
    inputMap.put(Value.get("Abe"), Value.get(88));
    List<Value> removeItems = new ArrayList<Value>();
    removeItems.add(Value.get("Sally"));
    removeItems.add(Value.get("UNKNOWN"));
    removeItems.add(Value.get("Lenny"));
    Record record = client.operate(null, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap), MapOperation.removeByKey(binName, Value.get("NOTFOUND"), MapReturnType.VALUE), MapOperation.removeByKey(binName, Value.get("Jim"), MapReturnType.VALUE), MapOperation.removeByKeyList(binName, removeItems, MapReturnType.COUNT), MapOperation.removeByValue(binName, Value.get(55), MapReturnType.KEY), MapOperation.size(binName));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    List<?> results = record.getList(binName);
    int i = 0;
    long val = (Long) results.get(i++);
    assertEquals(7, val);
    Object obj = results.get(i++);
    assertNull(obj);
    val = (Long) results.get(i++);
    assertEquals(98, val);
    val = (Long) results.get(i++);
    assertEquals(2, val);
    List<?> list = (List<?>) results.get(i++);
    assertEquals(1, list.size());
    assertEquals("Charlie", (String) list.get(0));
    val = (Long) results.get(i++);
    assertEquals(3, val);
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 68 with Record

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

the class TestOperateMap method operateMapRemoveRelative.

@Test
public void operateMapRemoveRelative() {
    Key key = new Key(args.namespace, args.set, "opmkey15");
    client.delete(null, key);
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get(0), Value.get(17));
    inputMap.put(Value.get(4), Value.get(2));
    inputMap.put(Value.get(5), Value.get(15));
    inputMap.put(Value.get(9), Value.get(10));
    // Write values to empty map.
    Record record = client.operate(null, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
    assertRecordFound(key, record);
    record = client.operate(null, key, MapOperation.removeByKeyRelativeIndexRange(binName, Value.get(5), 0, MapReturnType.VALUE), MapOperation.removeByKeyRelativeIndexRange(binName, Value.get(5), 1, MapReturnType.VALUE), MapOperation.removeByKeyRelativeIndexRange(binName, Value.get(5), -1, 1, MapReturnType.VALUE));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    List<?> results = record.getList(binName);
    int i = 0;
    List<?> list = (List<?>) results.get(i++);
    assertEquals(2L, list.size());
    assertEquals(15L, list.get(0));
    assertEquals(10L, list.get(1));
    list = (List<?>) results.get(i++);
    assertEquals(0L, list.size());
    list = (List<?>) results.get(i++);
    assertEquals(1L, list.size());
    assertEquals(2L, list.get(0));
    // Write values to empty map.
    client.delete(null, key);
    record = client.operate(null, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
    assertRecordFound(key, record);
    record = client.operate(null, key, MapOperation.removeByValueRelativeRankRange(binName, Value.get(11), 1, MapReturnType.VALUE), MapOperation.removeByValueRelativeRankRange(binName, Value.get(11), -1, 1, MapReturnType.VALUE));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    results = record.getList(binName);
    i = 0;
    list = (List<?>) results.get(i++);
    assertEquals(1L, list.size());
    assertEquals(17L, list.get(0));
    list = (List<?>) results.get(i++);
    assertEquals(1L, list.size());
    assertEquals(10L, list.get(0));
}
Also used : HashMap(java.util.HashMap) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 69 with Record

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

the class TestOperateMap method operateDoubleNestedMap.

@Test
public void operateDoubleNestedMap() {
    Key key = new Key(args.namespace, args.set, "opmkey20");
    client.delete(null, key);
    Map<Value, Value> m11 = new HashMap<Value, Value>();
    m11.put(Value.get("key111"), Value.get(1));
    Map<Value, Value> m12 = new HashMap<Value, Value>();
    m12.put(Value.get("key121"), Value.get(5));
    Map<Value, Value> m1 = new HashMap<Value, Value>();
    m1.put(Value.get("key11"), Value.get(m11));
    m1.put(Value.get("key12"), Value.get(m12));
    Map<Value, Value> m21 = new HashMap<Value, Value>();
    m21.put(Value.get("key211"), Value.get(7));
    Map<Value, Value> m2 = new HashMap<Value, Value>();
    m2.put(Value.get("key21"), Value.get(m21));
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get("key1"), Value.get(m1));
    inputMap.put(Value.get("key2"), Value.get(m2));
    // Create maps.
    client.put(null, key, new Bin(binName, inputMap));
    // Set map value to 11 for map key "key21" inside of map key "key2"
    // and retrieve all maps.
    Record record = client.operate(null, key, MapOperation.put(MapPolicy.Default, binName, Value.get("key121"), Value.get(11), CTX.mapKey(Value.get("key1")), CTX.mapRank(-1)), Operation.get(binName));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    List<?> results = record.getList(binName);
    int i = 0;
    long count = (Long) results.get(i++);
    assertEquals(1, count);
    Map<?, ?> map = (Map<?, ?>) results.get(i++);
    assertEquals(2, map.size());
    map = (Map<?, ?>) map.get("key1");
    assertEquals(2, map.size());
    map = (Map<?, ?>) map.get("key12");
    assertEquals(1, map.size());
    long v = (Long) map.get("key121");
    assertEquals(11, v);
}
Also used : HashMap(java.util.HashMap) Bin(com.aerospike.client.Bin) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) HashMap(java.util.HashMap) Map(java.util.Map) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 70 with Record

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

the class TestOperateMap method operateMapScore.

@Test
public void operateMapScore() {
    // Test score.
    Key key = new Key(args.namespace, args.set, "opmkey10");
    client.delete(null, key);
    MapPolicy mapPolicy = new MapPolicy(MapOrder.KEY_VALUE_ORDERED, MapWriteFlags.DEFAULT);
    Map<Value, Value> inputMap = new HashMap<Value, Value>();
    inputMap.put(Value.get("weiling"), Value.get(0));
    inputMap.put(Value.get("briann"), Value.get(0));
    inputMap.put(Value.get("brianb"), Value.get(0));
    inputMap.put(Value.get("meher"), Value.get(0));
    // Create map.
    Record record = client.operate(null, key, MapOperation.putItems(mapPolicy, binName, inputMap));
    assertRecordFound(key, record);
    // Change scores
    record = client.operate(null, key, MapOperation.increment(mapPolicy, binName, Value.get("weiling"), Value.get(10)), MapOperation.increment(mapPolicy, binName, Value.get("briann"), Value.get(20)), MapOperation.increment(mapPolicy, binName, Value.get("brianb"), Value.get(1)), MapOperation.increment(mapPolicy, binName, Value.get("meher"), Value.get(20)));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    // Query top 3 scores
    record = client.operate(null, key, MapOperation.getByRankRange(binName, -3, 3, MapReturnType.KEY));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    // Remove people with score 10 and display top 3 again
    record = client.operate(null, key, MapOperation.removeByValue(binName, Value.get(10), MapReturnType.KEY), MapOperation.getByRankRange(binName, -3, 3, MapReturnType.KEY));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    List<?> results = record.getList(binName);
    int i = 0;
    List<?> list = (List<?>) results.get(i++);
    String s = (String) list.get(0);
    assertEquals("weiling", s);
    list = (List<?>) results.get(i++);
    s = (String) list.get(0);
    assertEquals("brianb", s);
    s = (String) list.get(1);
    assertEquals("briann", s);
    s = (String) list.get(2);
    assertEquals("meher", s);
}
Also used : HashMap(java.util.HashMap) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Collections.singletonList(java.util.Collections.singletonList) ArrayList(java.util.ArrayList) List(java.util.List) MapPolicy(com.aerospike.client.cdt.MapPolicy) Key(com.aerospike.client.Key) Test(org.junit.Test)

Aggregations

Record (com.aerospike.client.Record)259 Key (com.aerospike.client.Key)140 Test (org.junit.Test)139 ArrayList (java.util.ArrayList)75 Bin (com.aerospike.client.Bin)70 AerospikeException (com.aerospike.client.AerospikeException)62 Value (com.aerospike.client.Value)54 WritePolicy (com.aerospike.client.policy.WritePolicy)47 List (java.util.List)41 HashMap (java.util.HashMap)36 ThrowingRunnable (org.junit.function.ThrowingRunnable)32 BatchPolicy (com.aerospike.client.policy.BatchPolicy)29 Policy (com.aerospike.client.policy.Policy)29 Statement (com.aerospike.client.query.Statement)23 RecordSet (com.aerospike.client.query.RecordSet)22 Expression (com.aerospike.client.exp.Expression)18 Map (java.util.Map)15 HLLValue (com.aerospike.client.Value.HLLValue)14 Collections.singletonList (java.util.Collections.singletonList)9 MapPolicy (com.aerospike.client.cdt.MapPolicy)7