Search in sources :

Example 71 with Record

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

the class TestOperateMap method operateMapSwitch.

@Test
public void operateMapSwitch() {
    // Switch from unordered map to a key ordered map.
    Key key = new Key(args.namespace, args.set, "opmkey4");
    client.delete(null, key);
    Record record = client.operate(null, key, MapOperation.put(MapPolicy.Default, binName, Value.get(4), Value.get(4)), MapOperation.put(MapPolicy.Default, binName, Value.get(3), Value.get(3)), MapOperation.put(MapPolicy.Default, binName, Value.get(2), Value.get(2)), MapOperation.put(MapPolicy.Default, binName, Value.get(1), Value.get(1)), MapOperation.getByIndex(binName, 2, MapReturnType.KEY_VALUE), MapOperation.getByIndexRange(binName, 0, 10, MapReturnType.KEY_VALUE));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    List<?> results = record.getList(binName);
    int i = 3;
    long size = (Long) results.get(i++);
    assertEquals(4, size);
    List<?> list = (List<?>) results.get(i++);
    assertEquals(1, list.size());
    list = (List<?>) results.get(i++);
    assertEquals(4, list.size());
    record = client.operate(null, key, MapOperation.setMapPolicy(new MapPolicy(MapOrder.KEY_ORDERED, MapWriteFlags.DEFAULT), binName), MapOperation.getByKeyRange(binName, Value.get(3), Value.get(5), MapReturnType.COUNT), MapOperation.getByKeyRange(binName, Value.get(-5), Value.get(2), MapReturnType.KEY_VALUE), MapOperation.getByIndexRange(binName, 0, 10, MapReturnType.KEY_VALUE));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    results = record.getList(binName);
    i = 0;
    Object obj = results.get(i++);
    assertNull(obj);
    long val = (Long) results.get(i++);
    assertEquals(2, val);
    list = (List<?>) results.get(i++);
    assertEquals(1, list.size());
    Entry<?, ?> entry = (Entry<?, ?>) list.get(0);
    assertEquals(1L, entry.getValue());
    list = (List<?>) results.get(i++);
    entry = (Entry<?, ?>) list.get(3);
    assertEquals(4L, entry.getKey());
}
Also used : Entry(java.util.Map.Entry) 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)

Example 72 with Record

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

the class TestOperateMap method operateMapClear.

@Test
public void operateMapClear() {
    // Test clear.
    Key key = new Key(args.namespace, args.set, "opmkey9");
    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));
    Record record = client.operate(null, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
    assertRecordFound(key, record);
    long size = record.getLong(binName);
    assertEquals(2, size);
    record = client.operate(null, key, MapOperation.clear(binName), MapOperation.size(binName));
    List<?> results = record.getList(binName);
    size = (Long) results.get(1);
    assertEquals(0, size);
}
Also used : HashMap(java.util.HashMap) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) Test(org.junit.Test)

Example 73 with Record

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

the class TestOperateMap method operateMapRank.

@Test
public void operateMapRank() {
    // Test rank.
    Key key = new Key(args.namespace, args.set, "opmkey6");
    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));
    // Write values to empty map.
    Record record = client.operate(null, key, MapOperation.putItems(MapPolicy.Default, binName, inputMap));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    // Increment some user scores.
    record = client.operate(null, key, MapOperation.increment(MapPolicy.Default, binName, Value.get("John"), Value.get(5)), MapOperation.increment(MapPolicy.Default, binName, Value.get("Jim"), Value.get(-4)));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    // Get scores.
    record = client.operate(null, key, MapOperation.getByRankRange(binName, -2, 2, MapReturnType.KEY), MapOperation.getByRankRange(binName, 0, 2, MapReturnType.KEY_VALUE), MapOperation.getByRank(binName, 0, MapReturnType.VALUE), MapOperation.getByRank(binName, 2, MapReturnType.KEY), MapOperation.getByValueRange(binName, Value.get(90), Value.get(95), MapReturnType.RANK), MapOperation.getByValueRange(binName, Value.get(90), Value.get(95), MapReturnType.COUNT), MapOperation.getByValueRange(binName, Value.get(90), Value.get(95), MapReturnType.KEY_VALUE), MapOperation.getByValueRange(binName, Value.get(81), Value.get(82), MapReturnType.KEY), MapOperation.getByValue(binName, Value.get(77), MapReturnType.KEY), MapOperation.getByValue(binName, Value.get(81), MapReturnType.RANK), MapOperation.getByKey(binName, Value.get("Charlie"), MapReturnType.RANK), MapOperation.getByKey(binName, Value.get("Charlie"), MapReturnType.REVERSE_RANK));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    List<?> results = record.getList(binName);
    int i = 0;
    List<?> list = (List<?>) results.get(i++);
    String str;
    long val;
    str = (String) list.get(0);
    assertEquals("Harry", str);
    str = (String) list.get(1);
    assertEquals("Jim", str);
    list = (List<?>) results.get(i++);
    Entry<?, ?> entry = (Entry<?, ?>) list.get(0);
    assertEquals("Charlie", entry.getKey());
    assertEquals(55L, entry.getValue());
    entry = (Entry<?, ?>) list.get(1);
    assertEquals("John", entry.getKey());
    assertEquals(81L, entry.getValue());
    val = (Long) results.get(i++);
    assertEquals(55, val);
    str = (String) results.get(i++);
    assertEquals("Harry", str);
    list = (List<?>) results.get(i++);
    val = (Long) list.get(0);
    assertEquals(3, val);
    val = (Long) results.get(i++);
    assertEquals(1, val);
    list = (List<?>) results.get(i++);
    entry = (Entry<?, ?>) list.get(0);
    assertEquals("Jim", entry.getKey());
    assertEquals(94L, entry.getValue());
    list = (List<?>) results.get(i++);
    str = (String) list.get(0);
    assertEquals("John", str);
    list = (List<?>) results.get(i++);
    assertEquals(0, list.size());
    list = (List<?>) results.get(i++);
    val = (Long) list.get(0);
    assertEquals(1, val);
    val = (Long) results.get(i++);
    assertEquals(0, val);
    val = (Long) results.get(i++);
    assertEquals(3, val);
}
Also used : Entry(java.util.Map.Entry) 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 74 with Record

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

the class TestOperateMap method operateMapPutItems.

@Test
public void operateMapPutItems() {
    Key key = new Key(args.namespace, args.set, "opmkey2");
    client.delete(null, key);
    Map<Value, Value> addMap = new HashMap<Value, Value>();
    addMap.put(Value.get(12), Value.get("myval"));
    addMap.put(Value.get(-8734), Value.get("str2"));
    addMap.put(Value.get(1), Value.get("my default"));
    Map<Value, Value> putMap = new HashMap<Value, Value>();
    putMap.put(Value.get(12), Value.get("myval12222"));
    putMap.put(Value.get(13), Value.get("str13"));
    Map<Value, Value> updateMap = new HashMap<Value, Value>();
    updateMap.put(Value.get(13), Value.get("myval2"));
    Map<Value, Value> replaceMap = new HashMap<Value, Value>();
    replaceMap.put(Value.get(12), Value.get(23));
    replaceMap.put(Value.get(-8734), Value.get("changed"));
    MapPolicy putMode = MapPolicy.Default;
    MapPolicy addMode = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteFlags.CREATE_ONLY);
    MapPolicy updateMode = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteFlags.UPDATE_ONLY);
    Record record = client.operate(null, key, MapOperation.putItems(addMode, binName, addMap), MapOperation.putItems(putMode, binName, putMap), MapOperation.putItems(updateMode, binName, updateMap), MapOperation.putItems(updateMode, binName, replaceMap), MapOperation.getByKey(binName, Value.get(1), MapReturnType.VALUE), MapOperation.getByKey(binName, Value.get(-8734), MapReturnType.VALUE), MapOperation.getByKeyRange(binName, Value.get(12), Value.get(15), MapReturnType.KEY_VALUE));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    List<?> results = record.getList(binName);
    int i = 0;
    long size = (Long) results.get(i++);
    assertEquals(3, size);
    size = (Long) results.get(i++);
    assertEquals(4, size);
    size = (Long) results.get(i++);
    assertEquals(4, size);
    size = (Long) results.get(i++);
    assertEquals(4, size);
    String str = (String) results.get(i++);
    assertEquals("my default", str);
    str = (String) results.get(i++);
    assertEquals("changed", str);
    List<?> list = (List<?>) results.get(i++);
    assertEquals(2, list.size());
}
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)

Example 75 with Record

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

the class TestOperateMap method operateMapCreateContext.

@Test
public void operateMapCreateContext() {
    Key key = new Key(args.namespace, args.set, "opmkey22");
    client.delete(null, key);
    Map<Value, Value> m1 = new HashMap<Value, Value>();
    m1.put(Value.get("key11"), Value.get(9));
    m1.put(Value.get("key12"), Value.get(4));
    Map<Value, Value> m2 = new HashMap<Value, Value>();
    m2.put(Value.get("key21"), Value.get(3));
    m2.put(Value.get("key22"), Value.get(5));
    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.create(binName, MapOrder.KEY_ORDERED, CTX.mapKey(Value.get("key3"))), MapOperation.put(MapPolicy.Default, binName, Value.get("key31"), Value.get(99), CTX.mapKey(Value.get("key3"))), // MapOperation.put(MapPolicy.Default, binName, Value.get("key31"), Value.get(99), CTX.mapKeyCreate(Value.get("key3"), MapOrder.KEY_ORDERED)),
    Operation.get(binName));
    assertRecordFound(key, record);
    // System.out.println("Record: " + record);
    List<?> results = record.getList(binName);
    int i = 1;
    long count = (Long) results.get(i++);
    assertEquals(1, count);
    Map<?, ?> map = (Map<?, ?>) results.get(i++);
    assertEquals(3, map.size());
    map = (Map<?, ?>) map.get("key3");
    long v = (Long) map.get("key31");
    assertEquals(99, 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)

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