-
Notifications
You must be signed in to change notification settings - Fork 3
/
RemoveFromTree.lua
56 lines (48 loc) · 1.46 KB
/
RemoveFromTree.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local function checkNode(treeName,nodeId)
local keys = {
treeName .. '/' .. nodeId .. '/children',
treeName .. '/' .. nodeId .. '/values'
};
local kill = true;
table.foreach(keys, function(i,key)
if 1 == redis.call('exists',key) then
kill = false;
end
end);
return kill;
end
local function getChild(treeName,parentId,childLabel)
local key = treeName .. '/' .. parentId .. '/children';
local childId = redis.call('hget',key,childLabel);
if childId ~= false then
childId = tonumber(childId);
end
return childId;
end
local function removeChild(treeName,parentId,childLabel)
local key = treeName .. '/' .. parentId .. '/children';
return redis.call('hdel',key,childLabel);
end
local function removeValue(treeName,nodeId,value)
local key = treeName .. '/' .. nodeId .. '/values';
return redis.call('srem',key,value);
end
local function dfsRemove(treeName,rootId,path,value)
if 0 == # path then
removeValue(treeName,rootId,value);
else
local label = table.remove(path,1);
local childId = getChild(treeName,rootId,label);
if false ~= childId then
if dfsRemove(treeName,childId,path,value) then
removeChild(treeName,rootId,label)
end
end
end
return checkNode(treeName,rootId);
end
local treeName = KEYS[1];
local rootId = ARGV[1];
local value = ARGV[2];
local path = slice(ARGV,3);
return dfsRemove(treeName,rootId,path,value);