Search in sources :

Example 16 with WritePolicy

use of com.aerospike.client.policy.WritePolicy 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 17 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project aerospike-client-java by aerospike.

the class TestOperateList method operateList6.

@Test
public void operateList6() {
    // Test clear.
    Key key = new Key(args.namespace, args.set, "oplkey6");
    client.delete(null, key);
    WritePolicy policy = new WritePolicy();
    policy.respondAllOps = true;
    List<Value> itemList = new ArrayList<Value>();
    itemList.add(Value.get("s11"));
    itemList.add(Value.get("s22222"));
    itemList.add(Value.get("s3333333"));
    itemList.add(Value.get("s4444444444"));
    itemList.add(Value.get("s5555555555555555"));
    Record record = client.operate(policy, key, Operation.put(new Bin("otherbin", 11)), Operation.get("otherbin"), ListOperation.appendItems(binName, itemList), ListOperation.clear(binName), ListOperation.size(binName));
    assertRecordFound(key, record);
    //System.out.println("Record: " + record);
    List<?> list = record.getList("otherbin");
    assertEquals(2, list.size());
    assertNull(list.get(0));
    assertEquals(11, (long) (Long) list.get(1));
    list = record.getList(binName);
    long size = (Long) list.get(0);
    assertEquals(5, size);
    // clear() does not return value by default, but we set respondAllOps, so it returns null.
    assertNull(list.get(1));
    size = (Long) list.get(2);
    assertEquals(0, size);
}
Also used : Bin(com.aerospike.client.Bin) ArrayList(java.util.ArrayList) Value(com.aerospike.client.Value) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) Test(org.junit.Test)

Example 18 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project aerospike-client-java by aerospike.

the class TestQueryKey method prepare.

@BeforeClass
public static void prepare() {
    Policy policy = new Policy();
    // Do not timeout on index create.
    policy.socketTimeout = 0;
    IndexTask itask = client.createIndex(policy, args.namespace, args.set, indexName, binName, IndexType.NUMERIC);
    itask.waitTillComplete();
    WritePolicy writePolicy = new WritePolicy();
    writePolicy.sendKey = true;
    for (int i = 1; i <= size; i++) {
        Key key = new Key(args.namespace, args.set, keyPrefix + i);
        Bin bin = new Bin(binName, i);
        client.put(writePolicy, key, bin);
    }
}
Also used : Policy(com.aerospike.client.policy.Policy) WritePolicy(com.aerospike.client.policy.WritePolicy) Bin(com.aerospike.client.Bin) IndexTask(com.aerospike.client.task.IndexTask) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy) BeforeClass(org.junit.BeforeClass)

Example 19 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project XRTB by benmfaul.

the class RedissonClient method set.

/**
	 * Set a key value as string with an expiration (No expiration set on cache2k, it is already set 
	 * @param skey String. The key name.
	 * @param value String. The value.
	 * @param expire int. The number of seconds before expiring.
	 * @throws Exception on aerorpike or cache errors.
	 */
public void set(String skey, String value, int expire) throws Exception {
    if (ae == null) {
        cache.put(skey, value);
        return;
    }
    AerospikeClient client = ae.getClient();
    if (client == null)
        return;
    WritePolicy policy = new WritePolicy();
    policy.expiration = expire;
    Key key = new Key("test", "cache", skey);
    Bin bin1 = new Bin("value", value);
    ae.getClient().put(policy, key, bin1);
}
Also used : AerospikeClient(com.aerospike.client.AerospikeClient) Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Example 20 with WritePolicy

use of com.aerospike.client.policy.WritePolicy in project XRTB by benmfaul.

the class RedissonClient method expire.

/**
	 * Expire a key (no op on Cache2k, expirt is set globally for it).
	 * @param id String. The key to expire.
	 * @param expire int. The number of seconds before expiration.
	 * @throws Exception on cache2k or Aerorpike errors.
	 */
public void expire(String id, int expire) throws Exception {
    if (cache != null) {
        return;
    }
    Key key = new Key("test", "cache", id);
    Record record = null;
    record = ae.getClient().get(null, key);
    if (record == null) {
        return;
    }
    Bin bin = new Bin("value", record.bins.get("value"));
    WritePolicy policy = new WritePolicy();
    policy.expiration = expire;
    ae.getClient().put(policy, key, bin);
}
Also used : Bin(com.aerospike.client.Bin) Record(com.aerospike.client.Record) Key(com.aerospike.client.Key) WritePolicy(com.aerospike.client.policy.WritePolicy)

Aggregations

Bin (com.aerospike.client.Bin)21 Key (com.aerospike.client.Key)21 WritePolicy (com.aerospike.client.policy.WritePolicy)21 Record (com.aerospike.client.Record)12 Test (org.junit.Test)7 AerospikeException (com.aerospike.client.AerospikeException)6 BeforeClass (org.junit.BeforeClass)3 Policy (com.aerospike.client.policy.Policy)2 AerospikeClient (com.aerospike.client.AerospikeClient)1 Value (com.aerospike.client.Value)1 ClientPolicy (com.aerospike.client.policy.ClientPolicy)1 GenerationPolicy (com.aerospike.client.policy.GenerationPolicy)1 IndexTask (com.aerospike.client.task.IndexTask)1 IOException (java.io.IOException)1 Writer (java.io.Writer)1 ArrayList (java.util.ArrayList)1