@Override public void cleanup() throws DBException { client.close(); }
@Override public Status delete(String table, String key) { try { if (!client.delete(deletePolicy, new Key(namespace, table, key))) { System.err.println("Record key " + key + " not found (delete)"); return Status.ERROR; } return Status.OK; } catch (AerospikeException e) { System.err.println("Error while deleting key " + key + ": " + e); return Status.ERROR; } } }
private Status write(String table, String key, WritePolicy writePolicy, Map<String, ByteIterator> values) { Bin[] bins = new Bin[values.size()]; int index = 0; for (Map.Entry<String, ByteIterator> entry: values.entrySet()) { bins[index] = new Bin(entry.getKey(), entry.getValue().toArray()); ++index; } Key keyObj = new Key(namespace, table, key); try { client.put(writePolicy, keyObj, bins); return Status.OK; } catch (AerospikeException e) { System.err.println("Error while writing key " + key + ": " + e); return Status.ERROR; } }
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."); }
@Override public Status read(String table, String key, Set<String> fields, Map<String, ByteIterator> result) { try { Record record; if (fields != null) { record = client.get(readPolicy, new Key(namespace, table, key), fields.toArray(new String[fields.size()])); } else { record = client.get(readPolicy, new Key(namespace, table, key)); } if (record == null) { System.err.println("Record key " + key + " not found (read)"); return Status.ERROR; } for (Map.Entry<String, Object> entry: record.bins.entrySet()) { result.put(entry.getKey(), new ByteArrayByteIterator((byte[])entry.getValue())); } return Status.OK; } catch (AerospikeException e) { System.err.println("Error while reading key " + key + ": " + e); return Status.ERROR; } }
/** * Create bin with a GeoJSON value. * * @param name bin name, current limit is 14 characters * @param value bin value */ public static Bin asGeoJSON(String name, String value) { return new Bin(name, Value.getAsGeoJSON(value)); }
@Override public void init() throws DBException { insertPolicy.recordExistsAction = RecordExistsAction.CREATE_ONLY; updatePolicy.recordExistsAction = RecordExistsAction.REPLACE_ONLY; Properties props = getProperties(); namespace = props.getProperty("as.namespace", DEFAULT_NAMESPACE); String host = props.getProperty("as.host", DEFAULT_HOST); String user = props.getProperty("as.user"); String password = props.getProperty("as.password"); int port = Integer.parseInt(props.getProperty("as.port", DEFAULT_PORT)); int timeout = Integer.parseInt(props.getProperty("as.timeout", DEFAULT_TIMEOUT)); readPolicy.timeout = timeout; insertPolicy.timeout = timeout; updatePolicy.timeout = timeout; deletePolicy.timeout = timeout; ClientPolicy clientPolicy = new ClientPolicy(); if (user != null && password != null) { clientPolicy.user = user; clientPolicy.password = password; } try { client = new com.aerospike.client.AerospikeClient(clientPolicy, host, port); } catch (AerospikeException e) { throw new DBException(String.format("Error while creating Aerospike " + "client for %s:%d.", host, port), e); } }
/** * Create bin with a blob value. The value will be java serialized. * This method is faster than the bin Object constructor because the blob is converted * directly instead of using multiple "instanceof" type checks with a blob default. * <p> * For servers configured as "single-bin", enter a null or empty name. * * @param name bin name, current limit is 14 characters * @param value bin value */ public static Bin asBlob(String name, Object value) { return new Bin(name, Value.getAsBlob(value)); }
/** * Constructor, specifying bin name and byte array value. * For servers configured as "single-bin", enter a null or empty name. * * @param name bin name, current limit is 14 characters * @param value bin value */ public Bin(String name, byte[] value) { this.name = name; this.value = Value.get(value); }
/** * Filter and forward message to callback. * * @param level message severity level * @param message message string not terminated with a newline */ public static void log(Level level, String message) { if (gCallback != null && level.ordinal() <= gLevel.ordinal() ) { gCallback.log(level, message); } } }
/** * Parse response in name/value pair format: * <p> * <command>\t<name1>=<value1>;<name2>=<value2>;...\n * * @return parser for name/value pairs */ public NameValueParser getNameValueParser() { skipToValue(); return new NameValueParser(); }
/** * Read only constructor (does not load the data) for use with Aerospike. * @param name String. The name of the symbol. * @param client AerospikeClient. The aerospike to use. */ public Membership(String name, AerospikeClient client) { this.name = name; this.client = client; key = new Key("test", "database", "rtb4free"); bin1 = new Bin(name, tree); client.put(null, key, bin1); tree = null; }
/** * Create bin with a GeoJSON value. * * @param name bin name, current limit is 14 characters * @param value bin value */ public static Bin asGeoJSON(String name, String value) { return new Bin(name, Value.getAsGeoJSON(value)); }
/** * Create bin with a blob value. The value will be java serialized. * This method is faster than the bin Object constructor because the blob is converted * directly instead of using multiple "instanceof" type checks with a blob default. * <p> * For servers configured as "single-bin", enter a null or empty name. * * @param name bin name, current limit is 14 characters * @param value bin value */ public static Bin asBlob(String name, Object value) { return new Bin(name, Value.getAsBlob(value)); }
/** * Filter and forward message to callback. * * @param level message severity level * @param message message string not terminated with a newline */ public static void log(Level level, String message) { if (gCallback != null && level.ordinal() <= gLevel.ordinal() ) { gCallback.log(level, message); } } }
/** * Parse response in name/value pair format: * <p> * <command>\t<name1>=<value1>;<name2>=<value2>;...\n * * @return parser for name/value pairs */ public NameValueParser getNameValueParser() { skipToValue(); return new NameValueParser(); }