Search in sources :

Example 91 with Key

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

the class TestOperateMap method operateMapRank.

@Test
public void operateMapRank() {
    // Test rank.
    if (!args.validateMap()) {
        return;
    }
    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.decrement(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 92 with Key

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

the class TestOperateMap method operateMapPutItems.

@Test
public void operateMapPutItems() {
    if (!args.validateMap()) {
        return;
    }
    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, MapWriteMode.CREATE_ONLY);
    MapPolicy updateMode = new MapPolicy(MapOrder.KEY_ORDERED, MapWriteMode.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 93 with Key

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

the class TestOperateMap method operateMapRemove.

@Test
public void operateMapRemove() {
    // Test remove.
    if (!args.validateMap()) {
        return;
    }
    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 94 with Key

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

the class TestOperateMap method operateMapScore.

@Test
public void operateMapScore() {
    // Test score.
    if (!args.validateMap()) {
        return;
    }
    Key key = new Key(args.namespace, args.set, "opmkey10");
    client.delete(null, key);
    MapPolicy mapPolicy = new MapPolicy(MapOrder.KEY_VALUE_ORDERED, MapWriteMode.UPDATE);
    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)

Example 95 with Key

use of com.aerospike.client.Key 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.
    if (!args.validateMap()) {
        return;
    }
    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, MapWriteMode.UPDATE), 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)

Aggregations

Key (com.aerospike.client.Key)204 Record (com.aerospike.client.Record)106 Bin (com.aerospike.client.Bin)104 Test (org.junit.Test)79 AerospikeException (com.aerospike.client.AerospikeException)50 ArrayList (java.util.ArrayList)50 Value (com.aerospike.client.Value)37 HashMap (java.util.HashMap)32 List (java.util.List)31 WritePolicy (com.aerospike.client.policy.WritePolicy)21 Map (java.util.Map)15 Statement (com.aerospike.client.query.Statement)10 BeforeClass (org.junit.BeforeClass)10 Policy (com.aerospike.client.policy.Policy)9 RecordSet (com.aerospike.client.query.RecordSet)9 IndexTask (com.aerospike.client.task.IndexTask)8 IOException (java.io.IOException)6 Collections.singletonList (java.util.Collections.singletonList)6 AerospikeClient (com.aerospike.client.AerospikeClient)5 BatchRead (com.aerospike.client.BatchRead)5