Search in sources :

Example 96 with Bin

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

the class TestOperateList method operateNestedList.

@Test
public void operateNestedList() {
    Key key = new Key(args.namespace, args.set, "oplkey18");
    client.delete(null, key);
    List<Value> l1 = new ArrayList<Value>();
    l1.add(Value.get(7));
    l1.add(Value.get(9));
    l1.add(Value.get(5));
    List<Value> l2 = new ArrayList<Value>();
    l2.add(Value.get(1));
    l2.add(Value.get(2));
    l2.add(Value.get(3));
    List<Value> l3 = new ArrayList<Value>();
    l3.add(Value.get(6));
    l3.add(Value.get(5));
    l3.add(Value.get(4));
    l3.add(Value.get(1));
    List<Value> inputList = new ArrayList<Value>();
    inputList.add(Value.get(l1));
    inputList.add(Value.get(l2));
    inputList.add(Value.get(l3));
    // Create list.
    client.put(null, key, new Bin(binName, inputList));
    // Append value to last list and retrieve all lists.
    Record record = client.operate(null, key, ListOperation.append(binName, Value.get(11), CTX.listIndex(-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(5, count);
    List<?> list = (List<?>) results.get(i++);
    assertEquals(3, list.size());
    // Test last nested list.
    list = (List<?>) list.get(2);
    assertEquals(5, list.size());
    assertEquals(6, (long) (Long) list.get(0));
    assertEquals(5, (long) (Long) list.get(1));
    assertEquals(4, (long) (Long) list.get(2));
    assertEquals(1, (long) (Long) list.get(3));
    assertEquals(11, (long) (Long) list.get(4));
}
Also used : Bin(com.aerospike.client.Bin) Value(com.aerospike.client.Value) 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 97 with Bin

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

the class TestExpire method noExpire.

@Test
public void noExpire() {
    Key key = new Key(args.namespace, args.set, "expirekey");
    Bin bin = new Bin(binName, "noexpirevalue");
    // Specify that record NEVER expires.
    // The "Never Expire" value is -1, or 0xFFFFFFFF.
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.expiration = -1;
    client.put(writePolicy, key, bin);
    // Read the record, showing it is there.
    Record record = client.get(null, key, bin.name);
    assertBinEqual(key, record, bin);
    // Read this Record after the Default Expiration, showing it is still there.
    // We should have set the Namespace TTL at 5 sec.
    Util.sleep(10 * 1000);
    record = client.get(null, key, bin.name);
    assertNotNull(record);
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) Test(org.junit.Test)

Example 98 with Bin

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

the class TestExpire method expire.

@Test
public void expire() {
    Key key = new Key(args.namespace, args.set, "expirekey ");
    Bin bin = new Bin(binName, "expirevalue");
    // Specify that record expires 2 seconds after it's written.
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.expiration = 2;
    client.put(writePolicy, key, bin);
    // Read the record before it expires, showing it is there.
    Record record = client.get(null, key, bin.name);
    assertBinEqual(key, record, bin);
    // Read the record after it expires, showing it's gone.
    Util.sleep(3 * 1000);
    record = client.get(null, key, bin.name);
    assertNull(record);
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) Test(org.junit.Test)

Example 99 with Bin

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

the class TestGeneration method generation.

@Test
public void generation() {
    Key key = new Key(args.namespace, args.set, "genkey");
    String binName = args.getBinName("genbin");
    // Delete record if it already exists.
    client.delete(null, key);
    // Set some values for the same record.
    Bin bin = new Bin(binName, "genvalue1");
    client.put(null, key, bin);
    bin = new Bin(binName, "genvalue2");
    client.put(null, key, bin);
    // Retrieve record and its generation count.
    Record record = client.get(null, key, bin.name);
    assertBinEqual(key, record, bin);
    // Set record and fail if it's not the expected generation.
    bin = new Bin(binName, "genvalue3");
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.generationPolicy = GenerationPolicy.EXPECT_GEN_EQUAL;
    writePolicy.generation = record.generation;
    client.put(writePolicy, key, bin);
    // Set record with invalid generation and check results .
    bin = new Bin(binName, "genvalue4");
    writePolicy.generation = 9999;
    try {
        client.put(writePolicy, key, bin);
        fail("Should have received generation error instead of success.");
    } catch (AerospikeException ae) {
        if (ae.getResultCode() != ResultCode.GENERATION_ERROR) {
            fail("Unexpected return code: namespace=" + key.namespace + " set=" + key.setName + " key=" + key.userKey + " bin=" + bin.name + " value=" + bin.value + " code=" + ae.getResultCode());
        }
    }
    // Verify results.
    record = client.get(null, key, bin.name);
    assertBinEqual(key, record, bin.name, "genvalue3");
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) Test(org.junit.Test)

Example 100 with Bin

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

the class TestOperate method operateDelete.

@Test
public void operateDelete() {
    // Write initial record.
    Key key = new Key(args.namespace, args.set, "opkey");
    Bin bin1 = new Bin("optintbin1", 1);
    client.put(null, key, bin1);
    // Read bin1 and then delete all.
    Record record = client.operate(null, key, Operation.get(bin1.name), Operation.delete());
    assertBinEqual(key, record, bin1.name, 1);
    // Verify record is gone.
    assertFalse(client.exists(null, key));
    // Rewrite record.
    Bin bin2 = new Bin("optintbin2", 2);
    client.put(null, key, bin1, bin2);
    // Read bin 1 and then delete all followed by a write of bin2.
    record = client.operate(null, key, Operation.get(bin1.name), Operation.delete(), Operation.put(bin2), Operation.get(bin2.name));
    assertBinEqual(key, record, bin1.name, 1);
    // Read record.
    record = client.get(null, key);
    assertBinEqual(key, record, bin2.name, 2);
    assertTrue(record.bins.size() == 1);
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) 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