/** * An atomic operation that adds a value to an existing bin value. Bin value must be an integer type. * * @param fieldName Name of the bin or, if mapped Class was provided, name of the mapped field * @param value Value to add to the bin value. */ public SingleKeyCommander<T> add(String fieldName, long value) { String binName = mapper != null ? mapper.getBinName(fieldName) : fieldName; operations.add(Operation.add(new Bin(binName, value))); return this; }
@SuppressWarnings("unchecked") public <T> T add(T objectToAddTo, Map<String, Long> values) { Assert.notNull(objectToAddTo, "Object to add to must not be null!"); Assert.notNull(values, "Values must not be null!"); try { AerospikeWriteData data = AerospikeWriteData.forWrite(); converter.write(objectToAddTo, data); Operation[] operations = new Operation[values.size() + 1]; int x = 0; for (Map.Entry<String, Long> entry : values.entrySet()) { Bin newBin = new Bin(entry.getKey(), entry.getValue()); operations[x] = Operation.add(newBin); x++; } operations[x] = Operation.get(); WritePolicy writePolicy = new WritePolicy(this.client.writePolicyDefault); writePolicy.expiration = data.getExpiration(); Record record = this.client.operate(writePolicy, data.getKey(), operations); return mapToEntity(data.getKey(), (Class<T>) objectToAddTo.getClass(), record); } catch (AerospikeException e) { DataAccessException translatedException = exceptionTranslator.translateExceptionIfPossible(e); throw translatedException == null ? e : translatedException; } }
@SuppressWarnings("unchecked") public <T> T add(T objectToAddTo, String binName, long value) { Assert.notNull(objectToAddTo, "Object to add to must not be null!"); Assert.notNull(binName, "Bin name must not be null!"); try { AerospikeWriteData data = AerospikeWriteData.forWrite(); converter.write(objectToAddTo, data); WritePolicy writePolicy = new WritePolicy(this.client.writePolicyDefault); writePolicy.expiration = data.getExpiration(); Record record = this.client.operate(writePolicy, data.getKey(), Operation.add(new Bin(binName, value)), Operation.get()); return mapToEntity(data.getKey(), (Class<T>) objectToAddTo.getClass(), record); } catch (AerospikeException e) { DataAccessException translatedException = exceptionTranslator.translateExceptionIfPossible(e); throw translatedException == null ? e : translatedException; } }
/** * Demonstrate multiple operations on a single record in one call. */ @Override public void runExample(AerospikeClient client, Parameters params) throws Exception { // Write initial record. Key key = new Key(params.namespace, params.set, "opkey"); Bin bin1 = new Bin("optintbin", 7); Bin bin2 = new Bin("optstringbin", "string value"); console.info("Put: namespace=%s set=%s key=%s bin1=%s value1=%s bin2=%s value2=%s", key.namespace, key.setName, key.userKey, bin1.name, bin1.value, bin2.name, bin2.value); client.put(params.writePolicy, key, bin1, bin2); // Add integer, write new string and read record. Bin bin3 = new Bin(bin1.name, 4); Bin bin4 = new Bin(bin2.name, "new string"); console.info("Add: " + bin3.value); console.info("Write: " + bin4.value); console.info("Read:"); Record record = client.operate(params.writePolicy, key, Operation.add(bin3), Operation.put(bin4), Operation.get()); if (record == null) { throw new Exception(String.format( "Failed to get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey)); } validateBin(key, record, bin3.name, 11, record.getInt(bin3.name)); validateBin(key, record, bin4.name, bin4.value.toString(), record.getValue(bin4.name)); }
record = client.operate(params.writePolicy, key, Operation.add(bin), Operation.get(bin.name));