Skip to content

Commit

Permalink
Merge pull request #127 from aerospike/CLIENT-2621
Browse files Browse the repository at this point in the history
Client 2621 Support persistant maps
  • Loading branch information
vmsachin committed Dec 15, 2023
2 parents c57c8d6 + 8bde993 commit 4a6c4e8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
34 changes: 24 additions & 10 deletions lib/aerospike/cdt/map_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,23 @@ def initialize(op_type, map_op, bin_name, *arguments, ctx: nil, return_type: nil
self
end

##
# Creates a map create operation.
# Server creates map at given context level.
def self.create(bin_name, order, ctx: nil)
if !ctx || ctx.length == 0
# Create map create operation.
# Server creates a map at the given context level.
#
# @param [String] bin_name The bin name.
# @param [Integer] order The map order.
# @param [Boolean] persist_index If true, persist map index. A map index improves lookup performance,
# but requires more storage. A map index can be created for a top-level
# ordered map only. Nested and unordered map indexes are not supported.
# @param [String] ctx Optional path to a nested map. If not defined, the top-level map is used.
def self.create(bin_name, order, persistent_index, ctx: nil)
if !ctx || ctx.empty?
# If context not defined, the set order for top-level bin map.
self.set_policy(MapPolicy.new(order: order, flags: 0), bin_name)
attr = order[:attr]
attr += 0x10 if persistent_index
MapOperation.new(Operation::CDT_MODIFY, SET_TYPE, bin_name, attr, ctx: ctx, flag: order[:flag])
else
# Create nested map. persistIndex does not apply here, so ignore it
MapOperation.new(Operation::CDT_MODIFY, SET_TYPE, bin_name, order[:attr], ctx: ctx, flag: order[:flag])
end
end
Expand All @@ -128,7 +137,12 @@ def self.create(bin_name, order, ctx: nil)
#
# The required map policy attributes can be changed after the map is created.
def self.set_policy(bin_name, policy, ctx: nil)
MapOperation.new(Operation::CDT_MODIFY, SET_TYPE, bin_name, policy.order[:attr], ctx: ctx)
attr = policy.attributes
# Remove persistIndex flag for nested maps.
if !ctx.nil? && !ctx.empty? && (attr & 0x10) != 0
attr &= ~0x10
end
MapOperation.new(Operation::CDT_MODIFY, SET_TYPE, bin_name, attr, ctx: ctx)
end

##
Expand Down Expand Up @@ -635,7 +649,7 @@ def pack_bin_value
args.unshift(return_type) if return_type

Packer.use do |packer|
if @ctx != nil && @ctx.length > 0
if @ctx != nil && !@ctx.empty?
packer.write_array_header(3)
Value.of(0xff).pack(packer)

Expand All @@ -645,12 +659,12 @@ def pack_bin_value
Value.of(@map_op).pack(packer)
else
packer.write_raw_short(@map_op)
if args.length > 0
if !args.empty?
packer.write_array_header(args.length)
end
end

if args.length > 0
if !args.empty?
args.each do |value|
Value.of(value).pack(packer)
end
Expand Down
9 changes: 6 additions & 3 deletions lib/aerospike/cdt/map_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
module Aerospike
module CDT
class MapPolicy
attr_accessor :order, :write_mode, :flags
attr_accessor :item_command, :items_command, :attributes
attr_accessor :order, :write_mode, :flags, :item_command, :items_command, :attributes, :persist_index

def initialize(order: nil, write_mode: nil, flags: nil)
def initialize(order: nil, write_mode: nil, persist_index: false, flags: nil)
if write_mode && flags
raise ArgumentError, "Use write mode for server versions < 4.3; use write flags for server versions >= 4.3."
end
Expand All @@ -30,6 +29,10 @@ def initialize(order: nil, write_mode: nil, flags: nil)
@flags = flags || MapWriteFlags::DEFAULT
@attributes = order ? order[:attr] : 0

if @persist_index
@attributes |= 0x10
end

case @write_mode
when CDT::MapWriteMode::DEFAULT
@item_command = CDT::MapOperation::PUT
Expand Down
2 changes: 1 addition & 1 deletion spec/aerospike/cdt/cdt_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def map_post_op
let(:map_value) { { "c" => 1, "b" => 2, "a" => 3 } }

it "sets the map order" do
new_policy = MapPolicy.new(order: MapOrder::KEY_ORDERED)
new_policy = MapPolicy.new(order: MapOrder::KEY_ORDERED, persist_index: true)
operation = MapOperation.set_policy(map_bin, new_policy)

expect { client.operate(key, [operation]) }.not_to raise_error
Expand Down

0 comments on commit 4a6c4e8

Please sign in to comment.