如何处理 Aerospike 脚本中的重复项

我有一份脚本,它运行良好,但我需要更新它。脚本现在会添加项目而不进行任何检查以查看是否已存在。

    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

我尝试使用 for value in list.iterator(l) 迭代列表并在 value ~= val 时添加项目,但它不起作用。 该函数中的 ID 是 solr 文档 _id,val 是用户 _id。我从 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': []})

更新 我尝试了不同的变体,这个是可行的:

    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
点赞
用户582436
用户582436

不要为已经存在 List API 操作的内容创建 UDF。UDF 的性能和伸缩性都不如 List API 好。

你可以不使用 UDF 来完成这个操作。以下是使用 Python 客户端 实现相同操作的示例代码。

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)

我在 rbotzer/aerospike-cdt-examples 中也有类似的示例。

2018-10-20 03:36:41