mirror of
https://github.com/KingDuckZ/dindexer.git
synced 2024-11-25 00:53:43 +00:00
Improve lua scripts.
Now a "tags" field gets added to the file item. It contains a comma separated sorted list of the tags to which the item belongs.
This commit is contained in:
parent
9423c4da73
commit
4bd7c3515a
2 changed files with 85 additions and 12 deletions
|
@ -18,13 +18,51 @@ local tag_key = KEYS[1]
|
|||
local file_key = KEYS[2]
|
||||
local group_key = ARGV[1]
|
||||
|
||||
if group_key == "" then
|
||||
return redis.call("SREM", tag_key, file_key)
|
||||
else
|
||||
local function split_string(inputstr, sep)
|
||||
if sep == nil then
|
||||
sep = "%s"
|
||||
end
|
||||
local t = {}
|
||||
local z = 1
|
||||
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
|
||||
t[z] = str
|
||||
z = z + 1
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local function dele_tag_from_list(tag_list, dele_tag)
|
||||
tag_list = split_string(tag_list or "", ",")
|
||||
|
||||
local z = 1
|
||||
for _,str in ipairs(tag_list) do
|
||||
if str == dele_tag then
|
||||
table.remove(tag_list, z)
|
||||
break
|
||||
end
|
||||
z = z + 1
|
||||
end
|
||||
return table.concat(tag_list, ",")
|
||||
end
|
||||
|
||||
if group_key ~= "" then
|
||||
local found_group_key = redis.call("HGET", file_key, "group_id")
|
||||
if found_group_key == group_key then
|
||||
return redis.call("SREM", tag_key, file_key)
|
||||
else
|
||||
if found_group_key ~= group_key then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
local dele_tag = split_string(tag_key, ":")
|
||||
local new_tag_list = dele_tag_from_list(redis.call("HGET", file_key, "tags"), dele_tag[#dele_tag])
|
||||
|
||||
if new_tag_list == "" then
|
||||
redis.call("HDEL", file_key, "tags")
|
||||
else
|
||||
redis.call("HSET", file_key, "tags", new_tag_list)
|
||||
end
|
||||
|
||||
local retval = redis.call("SREM", tag_key, file_key)
|
||||
if redis.call("SCARD", tag_key) == 0 then
|
||||
redis.call("SREM", tag_key)
|
||||
end
|
||||
return retval
|
||||
|
|
|
@ -18,13 +18,48 @@ local tag_key = KEYS[1]
|
|||
local file_key = KEYS[2]
|
||||
local group_key = ARGV[1]
|
||||
|
||||
if group_key == "" then
|
||||
return redis.call("SADD", tag_key, file_key)
|
||||
else
|
||||
local function split_string(inputstr, sep)
|
||||
if sep == nil then
|
||||
sep = "%s"
|
||||
end
|
||||
local t = {}
|
||||
local z = 1
|
||||
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
|
||||
t[z] = str
|
||||
z = z + 1
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
local function add_tag_to_list(tag_list, new_tag)
|
||||
if tag_list == false or tag_list == "" then
|
||||
return new_tag
|
||||
end
|
||||
tag_list = split_string(tag_list, ",")
|
||||
|
||||
local new_tag_found = false
|
||||
for _,str in ipairs(tag_list) do
|
||||
if str == new_tag then
|
||||
new_tag_found = true
|
||||
break
|
||||
end
|
||||
end
|
||||
if not new_tag_found then
|
||||
tag_list[#tag_list+1] = new_tag
|
||||
end
|
||||
table.sort(tag_list)
|
||||
return table.concat(tag_list, ",")
|
||||
end
|
||||
|
||||
if group_key ~= "" then
|
||||
local found_group_key = redis.call("HGET", file_key, "group_id")
|
||||
if found_group_key == group_key then
|
||||
return redis.call("SADD", tag_key, file_key)
|
||||
else
|
||||
if found_group_key ~= group_key then
|
||||
return nil
|
||||
end
|
||||
end
|
||||
|
||||
local new_tag = split_string(tag_key, ":")
|
||||
local new_tag_list = add_tag_to_list(redis.call("HGET", file_key, "tags"), new_tag[#new_tag])
|
||||
|
||||
redis.call("HSET", file_key, "tags", new_tag_list)
|
||||
return redis.call("SADD", tag_key, file_key)
|
||||
|
|
Loading…
Reference in a new issue