Hello, i want to do some operation on maps with a UDF.
I want to do some nested map merging with a user passed map.
However, the map that i am passing from the client to the UDF says it is of type userdata
and value null
. It is unclear to me what to do about that. I want to use the merge
method on maps with this userdata
type. And i am 100% sure it is not null
because i am hardcoding it from the client side.
UDF
function map_merge(rec, old_bin, new_bin, identifiers)
old_bin_type = type(old_bin)
new_bin_type = type(new_bin)
--ident_type = type(identifiers)
assert(old_bin_type == 'number', 'old_bin should be a number, ' .. old_bin_type .. ' given')
assert( new_bin_type == 'number', 'new_bin should be a number, ' .. new_bin_type .. ' given')
--assert(ident_type == 'map', 'identifiers should be a map, ' .. ident_type .. ' given' )
local ret = map()
if not aerospike:exists(rec) then
ret['status'] = 'RECORD DOES NOT EXIST'
return ret
end
names = record.bin_names(rec)
exists = false
for i, name in ipairs(names) do
if name == old_bin then
exists = true
break
end
end
if not exists then
ret['status'] = 'old_bin(' .. tostring(old_bin) .. ') does not exist'
end
local idents = identifiers.pairs()
local new_list = map.merge(rec[old_bin], idents)
rec[new_bin] = new_list
return ret
end
Client
let res = client
.execute_udf(
&WritePolicy::default(),
&key,
"map_merge",
"map_merge",
Some(&[
old_bin,
Value::Int(now.timestamp()),
Value::HashMap(val_map),
]),
)
.await;
Response
UdfBadResponse(
"/opt/aerospike/usr/udf/lua/map_merge.lua:31: attempt to call field 'pairs' (a nil value)",
),
In addition, i’ve printed the object metadata.
function print_type(identifiers)
mt = getmetatable(identifiers)
return tostring(mt)
end
"table: 0x41290fb0"