@Override protected void get(Key key, String udfPackageName, String udfFunctionName, Value[] udfValues) { Object udfReturnObj; if (counters.read.latency != null) { long begin = System.nanoTime(); udfReturnObj = client.execute(args.writePolicy, key, udfPackageName, udfFunctionName, udfValues); long elapsed = System.nanoTime() - begin; counters.read.latency.add(elapsed); } else { udfReturnObj = client.execute(args.writePolicy, key, udfPackageName, udfFunctionName, udfValues); } processRead(key, udfReturnObj); }
private void writeBlobUsingUdf(AerospikeClient client, Parameters params) throws Exception { Key key = new Key(params.namespace, params.set, "udfkey6"); String binName = params.getBinName("udfbin6"); // Create packed blob using standard java tools. ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(9845); dos.writeUTF("Hello world."); byte[] blob = baos.toByteArray(); client.execute(params.writePolicy, key, "record_example", "writeBin", Value.get(binName), Value.get(blob)); byte[] received = (byte[])client.execute(params.writePolicy, key, "record_example", "readBin", Value.get(binName)); if (Arrays.equals(blob, received)) { console.info("Blob data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, binName, Arrays.toString(received)); } else { throw new Exception(String.format( "Mismatch: expected=%s received=%s", Arrays.toString(blob), Arrays.toString(received))); } } }
public void cleanExpiredDocumentsBefore(long expireTimeMillis) { long duration = expireTimeMillis - System.currentTimeMillis(); int expiration = (int) TimeUnit.MILLISECONDS.toSeconds(duration); Value value = Value.get(expiration); Statement statement = new Statement(); statement.setNamespace(namespace); ExecuteTask executeTask = client.execute(null, statement, PACKAGE_NAME, FUNC_NAME, value); executeTask.waitTillComplete(SLEEP_INTERVAL, TIMEOUT); } }
public void onSuccess(final Key key, final Object obj) { try { // Write succeeded. Now call read using udf. console.info("Get: namespace=%s set=%s key=%s", key.namespace, key.setName, key.userKey); client.execute(eventLoop, new ExecuteListener() { public void onSuccess(final Key key, final Object received) { Object expected = bin.value.getObject(); if (received != null && received.equals(expected)) { console.info("Data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received); } else { console.error("Data mismatch: Expected %s. Received %s.", expected, received); } } public void onFailure(AerospikeException e) { console.error("Failed to get: namespace=%s set=%s key=%s exception=%s", key.namespace, key.setName, key.userKey, e.getMessage()); } }, writePolicy, key, "record_example", "readBin", Value.get(bin.name)); } catch (Exception e) { console.error("Failed to read: namespace=%s set=%s key=%s exception=%s", key.namespace, key.setName, key.userKey, e.getMessage()); } }
private void writeIfGenerationNotChanged(AerospikeClient client, Parameters params) throws Exception { Key key = new Key(params.namespace, params.set, "udfkey2"); Bin bin = new Bin(params.getBinName("udfbin2"), "string value"); // Seed record. client.put(params.writePolicy, key, bin); // Get record generation. long gen = (Long)client.execute(params.writePolicy, key, "record_example", "getGeneration"); // Write record if generation has not changed. client.execute(params.writePolicy, key, "record_example", "writeIfGenerationNotChanged", Value.get(bin.name), bin.value, Value.get(gen)); console.info("Record written."); }
/** * Deletes the records specified by the Statement and Qualifiers * * @param stmt A Statement object containing Namespace and Set * @param qualifiers Zero or more Qualifiers for the update query * @return returns a Map containing a number of successful updates. The Map will contain 2 keys "read" and "write", the values will be the count of successful operations */ public Map<String, Long> delete(Statement stmt, Qualifier... qualifiers) { if (qualifiers == null || qualifiers.length == 0) { /* * There are no qualifiers, so delete every record in the set * using Scan UDF delete */ ExecuteTask task = client.execute(null, stmt, QUERY_MODULE, "delete_record"); task.waitTillComplete(); return null; } if (qualifiers.length == 1 && qualifiers[0] instanceof KeyQualifier) { KeyQualifier keyQualifier = (KeyQualifier) qualifiers[0]; Key key = keyQualifier.makeKey(stmt.getNamespace(), stmt.getSetName()); this.client.delete(null, key); Map<String, Long> map = new HashMap<String, Long>(); map.put("read", 1L); map.put("write", 1L); return map; } KeyRecordIterator results = select(stmt, true, null, qualifiers); return delete(results); }
private void writeUsingUdf(AerospikeClient client, Parameters params) throws Exception { Key key = new Key(params.namespace, params.set, "udfkey1"); Bin bin = new Bin(params.getBinName("udfbin1"), "string value"); client.execute(params.writePolicy, key, "record_example", "writeBin", Value.get(bin.name), bin.value); Record record = client.get(params.policy, key, bin.name); String expected = bin.value.toString(); String received = record.getString(bin.name); if (received != null && received.equals(expected)) { console.info("Data matched: namespace=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, received); } else { console.error("Data mismatch: Expected %s. Received %s.", expected, received); } }
private void writeWithValidation(AerospikeClient client, Parameters params) throws Exception { Key key = new Key(params.namespace, params.set, "udfkey4"); String binName = "udfbin4"; // Lua function writeWithValidation accepts number between 1 and 10. // Write record with valid value. console.info("Write with valid value."); client.execute(params.writePolicy, key, "record_example", "writeWithValidation", Value.get(binName), Value.get(4)); // Write record with invalid value. console.info("Write with invalid value."); try { client.execute(params.writePolicy, key, "record_example", "writeWithValidation", Value.get(binName), Value.get(11)); console.error("UDF should not have succeeded!"); } catch (Exception e) { console.info("Success. UDF resulted in exception as expected."); } }
String value = "appended value"; client.execute(params.writePolicy, key, "record_example", "appendListBin", Value.get(binName), Value.get(value));
client.execute(eventLoop, new ExecuteListener() {
private void runQueryExecute( AerospikeClient client, Parameters params, String indexName, String binName1, String binName2 ) throws Exception { int begin = 3; int end = 9; console.info("For ns=%s set=%s index=%s bin=%s >= %s <= %s", params.namespace, params.set, indexName, binName1, begin, end); console.info("Even integers: add 100 to existing " + binName1); console.info("Multiple of 5: delete " + binName2 + " bin"); console.info("Multiple of 9: delete record"); Statement stmt = new Statement(); stmt.setNamespace(params.namespace); stmt.setSetName(params.set); stmt.setFilter(Filter.range(binName1, begin, end)); ExecuteTask task = client.execute(params.writePolicy, stmt, "record_example", "processRecord", Value.get(binName1), Value.get(binName2), Value.get(100)); task.waitTillComplete(); }