Skip to content

Commit

Permalink
Added the ttl property to batchWritePolicy
Browse files Browse the repository at this point in the history
CLIENT-2774
CLIENT-2793
Added batchParentWrite policy to client config
  • Loading branch information
DomPeliniAerospike committed Feb 14, 2024
1 parent 417856f commit 6041d5a
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 1 deletion.
8 changes: 8 additions & 0 deletions lib/policies/batch_write_policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class BatchWritePolicy {
*
*/
this.filterExpression = props.filterExpression

/**
* The time-to-live (expiration) of the record in seconds.
*
* @type number
*/
this.ttl = props.ttl

/**
* Specifies the behavior for the key.
*
Expand Down
1 change: 1 addition & 0 deletions lib/policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ function policyClass (type) {
case 'batchRead': return BatchReadPolicy
case 'batchRemove': return BatchRemovePolicy
case 'batchWrite': return BatchWritePolicy
case 'batchParentWrite': return BatchPolicy
case 'batchApply': return BatchApplyPolicy
case 'commandQueue': return CommandQueuePolicy
case 'hll': return HLLPolicy
Expand Down
1 change: 1 addition & 0 deletions src/include/enums.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define TTL_NAMESPACE_DEFAULT 0
#define TTL_NEVER_EXPIRE -1
#define TTL_DONT_UPDATE -2
#define TTL_CLIENT_DEFAULT -3

v8::Local<v8::Object> auth_mode_enum_values();
v8::Local<v8::Object> bitwise_enum_values();
Expand Down
20 changes: 20 additions & 0 deletions src/main/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,26 @@ int config_from_jsobject(as_config *config, Local<Object> configObj,
}
}

policy_val = Nan::Get(policies_obj, Nan::New("batchWrite").ToLocalChecked())
.ToLocalChecked();
if (policy_val->IsObject()) {
if ((rc = batchwrite_policy_from_jsobject(&policies->batch_write,
policy_val.As<Object>(),
log)) != AS_NODE_PARAM_OK) {
goto Cleanup;
}
}

policy_val = Nan::Get(policies_obj, Nan::New("batchParentWrite").ToLocalChecked())
.ToLocalChecked();
if (policy_val->IsObject()) {
if ((rc = batchpolicy_from_jsobject(&policies->batch_parent_write,
policy_val.As<Object>(),
log)) != AS_NODE_PARAM_OK) {
goto Cleanup;
}
}

policy_val = Nan::Get(policies_obj, Nan::New("info").ToLocalChecked())
.ToLocalChecked();
if (policy_val->IsObject()) {
Expand Down
1 change: 1 addition & 0 deletions src/main/enums/ttl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ Local<Object> ttl_enum_values()
set(obj, "NAMESPACE_DEFAULT", TTL_NAMESPACE_DEFAULT);
set(obj, "NEVER_EXPIRE", TTL_NEVER_EXPIRE);
set(obj, "DONT_UPDATE", TTL_DONT_UPDATE);
set(obj, "CLIENT_DEFAULT", TTL_CLIENT_DEFAULT);
return scope.Escape(obj);
}
5 changes: 5 additions & 0 deletions src/main/policy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ int batchwrite_policy_from_jsobject(as_policy_batch_write *policy,
AS_NODE_PARAM_OK) {
return rc;
}
if ((rc = get_optional_uint32_property((uint32_t *)&policy->ttl,
NULL, obj, "ttl", log)) !=
AS_NODE_PARAM_OK) {
return rc;
}
if ((rc = get_optional_uint32_property((uint32_t *)&policy->gen, NULL, obj,
"gen", log)) != AS_NODE_PARAM_OK) {
return rc;
Expand Down
1 change: 1 addition & 0 deletions src/main/util/conversions_batch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ int batch_write_record_from_jsobject(as_batch_records *batch_records,
Local<Array> ops = maybeOps.As<Array>();
as_v8_debug(log, "Adding operations to batch write record");
record->ops = as_operations_new(ops->Length());
record->ops->ttl = AS_RECORD_CLIENT_DEFAULT_TTL;
if (operations_from_jsarray(record->ops, ops, (LogInfo *)log) !=
AS_NODE_PARAM_OK) {
as_v8_error(
Expand Down
58 changes: 57 additions & 1 deletion test/batch_write.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ const metagen = helper.metagen
const recgen = helper.recgen
const putgen = helper.putgen
const valgen = helper.valgen
const options = require('./util/options')

const Key = Aerospike.Key

describe('client.batchWrite()', function () {
const client = helper.client

before(function () {
const nrecords = 17
const nrecords = 20
const generators = {
keygen: keygen.string(helper.namespace, helper.set, { prefix: 'test/batch_write/', random: false }),
recgen: recgen.record({
Expand Down Expand Up @@ -509,4 +510,59 @@ describe('client.batchWrite()', function () {
})
})
})

context('with BatchParentPolicy', function () {
helper.skipUnlessVersion('>= 6.0.0', this)

it('returns list and map bins as byte buffers', async function () {
const batch = [{
type: batchType.BATCH_READ,
key: new Key(helper.namespace, helper.set, 'test/batch_write/18'),
readAllBins: true
}]

const config = {
hosts: options.host + ':' + options.port,
policies: {
batchParentWrite: new Aerospike.BatchPolicy({ socketTimeout: 0, totalTimeout: 0, deserialize: false })
}
}

const dummyClient = await Aerospike.connect(config)
const results = await dummyClient.batchWrite(batch)
const bins = results[0].record.bins
expect(bins.i).to.be.a('number')
expect(bins.s).to.be.a('string')
expect(bins.l).to.be.instanceof(Buffer)
expect(bins.m).to.be.instanceof(Buffer)
await dummyClient.close()
})
})

context('with BatchWritePolicy ttl', function () {
helper.skipUnlessVersion('>= 6.0.0', this)

it('writes value with correct ttl', async function () {
const batch = [{
type: batchType.BATCH_WRITE,
key: new Key(helper.namespace, helper.set, 'test/batch_write/19'),
ops: [
op.write('example', 35),
op.write('blob', [4, 14, 28])
],
policy: new Aerospike.BatchWritePolicy({
exists: Aerospike.policy.exists.REPLACE,
ttl: 1367
})
}]
await client.batchWrite(batch)
return client.get(new Key(helper.namespace, helper.set, 'test/batch_write/19'))
.then(results => {
const bins = results.bins
expect(bins.example).to.be.a('number')
expect(bins.blob).to.be.a('array')
expect(results.ttl).to.equal(1367)
})
})
})
})

0 comments on commit 6041d5a

Please sign in to comment.