How can I handle with duplicate items in Aerospike script

I have script, its work properly, but I have to update it. Script now add items without any checking for existing.

    function put_page(rec, id, val)
        local l = rec['h']
        if l==nil  then l = list() rec['id'] = id  end
        list.append(l, val)
        rec['h'] = l
        if aerospike:exists(rec) then aerospike:update(rec) else aerospike:create(rec) end 
    end

I try iterate over list with for value in list.iterator(l) and append item if value~=val, but it didnt work. ID in function is solr document_id, val is users_id. I get example object from aerospike: (('contextChannel', 'ContextChannel', None, bytearray(b'E\xfb\xa3\xd0\r\xd6\r\J@f\xa8\xf6>y!\xd18=\x9b')), {'ttl': 2592000, 'gen': 8}, {'id': 'ALKSD4EW', 'h': []})

UPDATE I try different variants, and this is worked:

    function put_page(rec, id, val)
        local l = rec['h']
        local count = 0
        if l==nil  then l = list() rec['id'] = id  end
        for value in list.iterator(l) do
            if (value ~= val) then count = count + 1 end
        end
        if (list.size(l) == count) then list.append(l, val) end
        rec['h'] = l
        if aerospike:exists(rec) then aerospike:update(rec) else aerospike:create(rec) end
    end

Upvotes: 0

Views: 225

Answers (1)

Ronen Botzer
Ronen Botzer

Reputation: 7117

Don't create a UDF for something that exists as a List API operation. UDFs will not perform as well, nor scale as well.

You can do this without a UDF. Here's an example of doing the same thing using the Python client.

from aerospike_helpers.operations import list_operations as lh
from aerospike_helpers.operations import operations as oh

list_policy = {
    "list_order": aerospike.LIST_UNORDERED,
    "write_flags": (aerospike.LIST_WRITE_ADD_UNIQUE |
                    aerospike.LIST_WRITE_NO_FAIL)
}
ops = [
    oh.write('id', id),
    lh.list_append('h', val, list_policy)
]
client.operate(key, ops)

I have an example of a similar thing at rbotzer/aerospike-cdt-examples.

Upvotes: 1

Related Questions