Search in sources :

Example 11 with Bin

use of com.aerospike.client.Bin in project gora by apache.

the class AerospikeStore method put.

/**
 * Method to insert the persistent objects with the given key to the aerospike database server.
 * In writing the records, the policy defined in the mapping file is used to decide on the
 * behaviour of transaction handling.
 *
 * @param key        key of the object
 * @param persistent object to be persisted
 */
@Override
public void put(K key, T persistent) throws GoraException {
    try {
        Key recordKey = getAerospikeKey(key);
        List<Field> fields = persistent.getSchema().getFields();
        for (int i = 0; i < fields.size(); i++) {
            if (!persistent.isDirty(i)) {
                continue;
            }
            Object persistentValue = persistent.get(i);
            String mappingBinName = aerospikeParameters.getAerospikeMapping().getBinMapping().get(fields.get(i).name());
            if (mappingBinName == null) {
                LOG.error("Aerospike mapping for field {}#{} not found. Wrong gora-aerospike-mapping.xml?", persistent.getClass().getName(), fields.get(i).name());
                throw new RuntimeException("Aerospike mapping for field [" + persistent.getClass().getName() + "#" + fields.get(i).name() + "] not found. Wrong gora-aerospike-mapping.xml?");
            }
            Bin bin;
            if (persistentValue != null) {
                bin = new Bin(mappingBinName, getSerializableValue(persistentValue, fields.get(i).schema()));
            } else {
                bin = Bin.asNull(mappingBinName);
            }
            aerospikeClient.put(aerospikeParameters.getAerospikeMapping().getWritePolicy(), recordKey, bin);
        }
    } catch (Exception e) {
        throw new GoraException(e);
    }
}
Also used : Field(org.apache.avro.Schema.Field) GoraException(org.apache.gora.util.GoraException) Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) AerospikeException(com.aerospike.client.AerospikeException) GoraException(org.apache.gora.util.GoraException) IOException(java.io.IOException)

Example 12 with Bin

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

the class RWTask method doWrite.

/**
 * Write the key at the given index
 */
protected void doWrite(RandomShift random, long keyIdx, boolean multiBin, WritePolicy writePolicy) {
    Key key = new Key(args.namespace, args.setName, keyStart + keyIdx);
    // Use predictable value for 0th bin same as key value
    Bin[] bins = args.getBins(random, multiBin, keyStart + keyIdx);
    try {
        put(writePolicy, key, bins);
    } catch (AerospikeException ae) {
        writeFailure(ae);
        runNextCommand();
    } catch (Exception e) {
        writeFailure(e);
        runNextCommand();
    }
}
Also used : AerospikeException(com.aerospike.client.AerospikeException) Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key) AerospikeException(com.aerospike.client.AerospikeException)

Example 13 with Bin

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

the class Arguments method getBins.

public Bin[] getBins(RandomShift random, boolean multiBin, long keySeed) {
    if (fixedBins != null) {
        return (multiBin) ? fixedBins : fixedBin;
    }
    int binCount = (multiBin) ? nBins : 1;
    Bin[] bins = new Bin[binCount];
    int specLength = objectSpec.length;
    for (int i = 0; i < binCount; i++) {
        String name = Integer.toString(i);
        // Use passed in value for 0th bin. Random for others.
        Value value = genValue(random, objectSpec[i % specLength], i == 0 ? keySeed : -1);
        bins[i] = new Bin(name, value);
    }
    return bins;
}
Also used : Bin(com.aerospike.client.Bin) Value(com.aerospike.client.Value)

Example 14 with Bin

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

the class InsertTaskSync method runCommand.

private void runCommand(long keyCurrent, RandomShift random) {
    Key key = new Key(args.namespace, args.setName, keyCurrent);
    // Use predictable value for 0th bin same as key value
    Bin[] bins = args.getBins(random, true, keyCurrent);
    put(key, bins);
}
Also used : Bin(com.aerospike.client.Bin) Key(com.aerospike.client.Key)

Example 15 with Bin

use of com.aerospike.client.Bin 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)

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