【发布时间】:2018-10-20 11:36:41
【问题描述】:
我有脚本,它工作正常,但我必须更新它。脚本现在添加项目而不检查是否存在。
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
我尝试在 list.iterator(l) 中使用 for value 迭代列表,并在 value~=val 时附加项目,但它没有用。 函数中的 ID 是 solr document_id,val 是 users_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
【问题讨论】:
-
您的列表看起来如何?您能否向我们展示您的列表结构示例以及如何在列表中添加新项目?
-
用python脚本添加:aero_client_insert.apply(key, "channels_utils", "put_page", [page_id, Audience_id])
-
lua 列表是您的示例中带有键、值对
Table = {key1 = val1, key2 = val2 ... etc }的表。目前还不清楚您的表格是什么样的,以及您是如何读取值的。 -
我有 python 脚本将 [page_id, audince_id] 放入 lua 函数和 lua 中的脚本,它的代码有问题。就这样。 rec['h'] 列表,我希望其中没有重复项。