Skip to content

Commit

Permalink
Merge pull request #590 from aerospike/CLIENT-2617
Browse files Browse the repository at this point in the history
Added maps.create method
  • Loading branch information
DomPeliniAerospike authored Nov 1, 2023
2 parents 96b6fc0 + c5d0c63 commit 6f43a85
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
26 changes: 26 additions & 0 deletions lib/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -1275,3 +1275,29 @@ exports.getByRankRange = function (bin, rank, count, returnType) {
op.returnType = returnType
return op
}

/**
* @summary Retrieves one or more items in the specified rank range from the
* map.
*
* @description This operation returns the data specified by
* <code>returnType</code>.
*
* @param {string} bin - The name of the bin, which must contain a Map value.
* @param {number} index - Starting rank.
* @param {number} count - Number of items to delete; if not specified, the
* range includes all items starting from <code>rank</code>.
* @param {number} [returnType] - The {@link module:aerospike/maps.returnType|return type}
* indicating what data of the selected item(s) to return.
* @returns {Object} Operation that can be passed to the {@link Client#operate} command.
*
* @see Instead of passing <code>returnType</code>, you can also use
* {@link module:aerospike/maps~MapOperation#andReturn|MapOperation#andReturn} to
* select what data to return.
*/
exports.create = function (bin, order, persistIndex=false) {
const op = new MapOperation(opcodes.MAP_CREATE, bin)
op.order = order
op.persistIndex = persistIndex
return op
}
27 changes: 25 additions & 2 deletions src/main/map_operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,28 @@ bool add_map_get_by_rank_range_op(as_operations *ops, const char *bin,
return true;
}

bool add_map_create_op(as_operations *ops, const char *bin,
as_cdt_ctx *context, Local<Object> obj,
LogInfo *log)
{
as_map_order order;
if (get_uint32_property((uint32_t*)&order, obj, "order", log) != AS_NODE_PARAM_OK) {
return false;
}

bool persist_index;
if (get_bool_property(&persist_index, obj, "persistIndex", log) != AS_NODE_PARAM_OK) {
return false;
}


as_v8_debug(log, "order=%i, persist_index=%s", order, persist_index ? "true" : "false");
as_operations_map_create_all(ops, bin, context, order, persist_index);


return true;
}

typedef bool (*MapOperation)(as_operations *ops, const char *bin,
as_cdt_ctx *context, Local<Object> op,
LogInfo *log);
Expand Down Expand Up @@ -1069,7 +1091,8 @@ const ops_table_entry ops_table[] = {
{"MAP_GET_BY_INDEX", add_map_get_by_index_op},
{"MAP_GET_BY_INDEX_RANGE", add_map_get_by_index_range_op},
{"MAP_GET_BY_RANK", add_map_get_by_rank_op},
{"MAP_GET_BY_RANK_RANGE", add_map_get_by_rank_range_op}};
{"MAP_GET_BY_RANK_RANGE", add_map_get_by_rank_range_op},
{"MAP_CREATE", add_map_create_op}};

int add_map_op(as_operations *ops, uint32_t opcode, Local<Object> op,
LogInfo *log)
Expand Down Expand Up @@ -1120,4 +1143,4 @@ Local<Object> map_opcode_values()
}

return scope.Escape(obj);
}
}
34 changes: 34 additions & 0 deletions test/maps.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
const Aerospike = require('../lib/aerospike')
const helper = require('./test_helper')

const { Buffer } = require('node:buffer');


const maps = Aerospike.maps
const op = Aerospike.operations
const Context = Aerospike.cdt.Context
const status = Aerospike.status

Expand Down Expand Up @@ -62,6 +66,36 @@ describe('client.operate() - CDT Map operations', function () {
})
})

describe('maps.create', function () {
it('Creates a new map', function () {
return initState()
.then(createRecord({ map: { c: 1, b: 2, a: 3 } }))
.then(orderByKey('map'))
.then(operate(maps.create('emptyMap', maps.order.KEY_ORDERED)))
.then(operate(op.read('dap')))
.then(assertRecordEql({emptyMap: {}, map: {a: 3, b: 2, c: 1}}))
.then(cleanup())
})

it('Creates a new map from a cdt context', function () {
return initState()
.then(createRecord({ map: { c: 1, b: 2, a: 3 } }))
.then(orderByKey('map'))
.then(operate(maps.create('map', maps.order.KEY_ORDERED).withContext(ctx => ctx.addMapKeyCreate('nested'))))
.then(assertRecordEql({map: {a: 3, b: 2, c: 1, nested: {}}}))
.then(cleanup())
})

it('Creates a new map with persistent index', function () {
return initState()
.then(createRecord({ map: { c: 1, b: 2, a: 3 } }))
.then(orderByKey('map'))
.then(operate(maps.create('emptyMap', maps.order.KEY_ORDERED, true)))
.then(assertRecordEql({ emptyMap: {}, map: {a: 3, b: 2, c: 1}}))
.then(cleanup())
})
})

describe('maps.put', function () {
it('adds the item to the map and returns the size of the map', function () {
return initState()
Expand Down

0 comments on commit 6f43a85

Please sign in to comment.