use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class AsyncPutGet method runExample.
/**
* Asynchronously write and read a bin using alternate methods.
*/
@Override
public void runExample(AerospikeClient client, EventLoop eventLoop) {
Key key = new Key(params.namespace, params.set, "putgetkey");
Bin bin = new Bin(params.getBinName("putgetbin"), "value");
runPutGetInline(client, eventLoop, key, bin);
waitTillComplete();
resetComplete();
runPutGetWithRetry(client, eventLoop, key, bin);
waitTillComplete();
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Batch method writeRecords.
/**
* Write records individually.
*/
private void writeRecords(AerospikeClient client, Parameters params, String keyPrefix, String binName, String valuePrefix, int size) throws Exception {
for (int i = 1; i <= size; i++) {
Key key = new Key(params.namespace, params.set, keyPrefix + i);
Bin bin = new Bin(binName, valuePrefix + i);
console.info("Put: ns=%s set=%s key=%s bin=%s value=%s", key.namespace, key.setName, key.userKey, bin.name, bin.value);
client.put(params.writePolicy, key, bin);
}
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Batch method batchReadHeaders.
/**
* Read record header data in one batch.
*/
private void batchReadHeaders(AerospikeClient client, Parameters params, String keyPrefix, int size) throws Exception {
// Batch gets into one call.
Key[] keys = new Key[size];
for (int i = 0; i < size; i++) {
keys[i] = new Key(params.namespace, params.set, keyPrefix + (i + 1));
}
Record[] records = client.getHeader(null, keys);
for (int i = 0; i < records.length; i++) {
Key key = keys[i];
Record record = records[i];
Level level = Level.ERROR;
int generation = 0;
int expiration = 0;
if (record != null && (record.generation > 0 || record.expiration > 0)) {
level = Level.INFO;
generation = record.generation;
expiration = record.expiration;
}
console.write(level, "Record: ns=%s set=%s key=%s generation=%d expiration=%d", key.namespace, key.setName, key.userKey, generation, expiration);
}
if (records.length != size) {
console.error("Record size mismatch. Expected %d. Received %d.", size, records.length);
}
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class Command method setBatchRead.
public final void setBatchRead(BatchPolicy policy, List<BatchRead> records, BatchNode batch) {
// Estimate full row size
final int[] offsets = batch.offsets;
final int max = batch.offsetsSize;
final int fieldCount = policy.sendSetName ? 2 : 1;
BatchRead prev = null;
begin();
dataOffset += FIELD_HEADER_SIZE + 5;
for (int i = 0; i < max; i++) {
final BatchRead record = records.get(offsets[i]);
final Key key = record.key;
final String[] binNames = record.binNames;
dataOffset += key.digest.length + 4;
// results in more space used. The batch will still be correct.
if (prev != null && prev.key.namespace == key.namespace && (!policy.sendSetName || prev.key.setName == key.setName) && prev.binNames == binNames && prev.readAllBins == record.readAllBins) {
// Can set repeat previous namespace/bin names to save space.
dataOffset++;
} else {
// Estimate full header, namespace and bin names.
dataOffset += Buffer.estimateSizeUtf8(key.namespace) + FIELD_HEADER_SIZE + 6;
if (policy.sendSetName) {
dataOffset += Buffer.estimateSizeUtf8(key.setName) + FIELD_HEADER_SIZE;
}
if (binNames != null) {
for (String binName : binNames) {
estimateOperationSize(binName);
}
}
prev = record;
}
}
sizeBuffer();
int readAttr = Command.INFO1_READ;
if (policy.consistencyLevel == ConsistencyLevel.CONSISTENCY_ALL) {
readAttr |= Command.INFO1_CONSISTENCY_ALL;
}
writeHeader(policy, readAttr | Command.INFO1_BATCH, 0, 1, 0);
writeHeader(policy, Command.INFO1_READ | Command.INFO1_BATCH, 0, 1, 0);
final int fieldSizeOffset = dataOffset;
// Need to update size at end
writeFieldHeader(0, policy.sendSetName ? FieldType.BATCH_INDEX_WITH_SET : FieldType.BATCH_INDEX);
Buffer.intToBytes(max, dataBuffer, dataOffset);
dataOffset += 4;
dataBuffer[dataOffset++] = (policy.allowInline) ? (byte) 1 : (byte) 0;
prev = null;
for (int i = 0; i < max; i++) {
final int index = offsets[i];
Buffer.intToBytes(index, dataBuffer, dataOffset);
dataOffset += 4;
final BatchRead record = records.get(index);
final Key key = record.key;
final String[] binNames = record.binNames;
final byte[] digest = key.digest;
System.arraycopy(digest, 0, dataBuffer, dataOffset, digest.length);
dataOffset += digest.length;
// results in more space used. The batch will still be correct.
if (prev != null && prev.key.namespace == key.namespace && (!policy.sendSetName || prev.key.setName == key.setName) && prev.binNames == binNames && prev.readAllBins == record.readAllBins) {
// Can set repeat previous namespace/bin names to save space.
// repeat
dataBuffer[dataOffset++] = 1;
} else {
// Write full header, namespace and bin names.
// do not repeat
dataBuffer[dataOffset++] = 0;
if (binNames != null && binNames.length != 0) {
dataBuffer[dataOffset++] = (byte) readAttr;
Buffer.shortToBytes(fieldCount, dataBuffer, dataOffset);
dataOffset += 2;
Buffer.shortToBytes(binNames.length, dataBuffer, dataOffset);
dataOffset += 2;
writeField(key.namespace, FieldType.NAMESPACE);
if (policy.sendSetName) {
writeField(key.setName, FieldType.TABLE);
}
for (String binName : binNames) {
writeOperation(binName, Operation.Type.READ);
}
} else {
dataBuffer[dataOffset++] = (byte) (readAttr | (record.readAllBins ? Command.INFO1_GET_ALL : Command.INFO1_NOBINDATA));
Buffer.shortToBytes(fieldCount, dataBuffer, dataOffset);
dataOffset += 2;
Buffer.shortToBytes(0, dataBuffer, dataOffset);
dataOffset += 2;
writeField(key.namespace, FieldType.NAMESPACE);
if (policy.sendSetName) {
writeField(key.setName, FieldType.TABLE);
}
}
prev = record;
}
}
// Write real field size.
Buffer.intToBytes(dataOffset - MSG_TOTAL_HEADER_SIZE - 4, dataBuffer, fieldSizeOffset);
end();
}
use of com.aerospike.client.Key in project aerospike-client-java by aerospike.
the class MultiCommand method parseGroup.
/**
* Parse all records in the group.
*/
private final boolean parseGroup(int receiveSize) throws IOException {
//Parse each message response and add it to the result array
dataOffset = 0;
while (dataOffset < receiveSize) {
readBytes(MSG_REMAINING_HEADER_SIZE);
resultCode = dataBuffer[5] & 0xFF;
// If other return codes are received, then abort the batch.
if (resultCode != 0) {
if (resultCode == ResultCode.KEY_NOT_FOUND_ERROR) {
if (stopOnNotFound) {
return false;
}
} else {
throw new AerospikeException(resultCode);
}
}
byte info3 = dataBuffer[3];
// If this is the end marker of the response, do not proceed further
if ((info3 & Command.INFO3_LAST) == Command.INFO3_LAST) {
return false;
}
generation = Buffer.bytesToInt(dataBuffer, 6);
expiration = Buffer.bytesToInt(dataBuffer, 10);
batchIndex = Buffer.bytesToInt(dataBuffer, 14);
fieldCount = Buffer.bytesToShort(dataBuffer, 18);
opCount = Buffer.bytesToShort(dataBuffer, 20);
Key key = parseKey(fieldCount);
parseRow(key);
}
return true;
}
Aggregations