Debian Codegraph @

lualib/redis_scripts/fuzzy_update.lua

raw
1 -- Copyright 2026 Vsevolod Stakhov
2 --
3 -- Licensed under the Apache License, Version 2.0 (the "License");
4 -- you may not use this file except in compliance with the License.
5 -- You may obtain a copy of the License at
6 --
7 -- http://www.apache.org/licenses/LICENSE-2.0
8 --
9 -- Unless required by applicable law or agreed to in writing, software
10 -- distributed under the License is distributed on an "AS IS" BASIS,
11 -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 -- See the License for the specific language governing permissions and
13 -- limitations under the License.
14
15 -- Fuzzy hash update script (per-hash, atomic)
16 -- Handles ADD, DEL, and REFRESH operations including multi-flag merge and shingles
17 --
18 -- KEYS[1] = hash_key (prefix + digest)
19 -- KEYS[2] = count_key (prefix .. "_count")
20 -- KEYS[3..] = shingle keys (0 or 32 of them)
21 -- ARGV[1] = operation: "add", "del", "refresh"
22 -- ARGV[2] = flag (string number)
23 -- ARGV[3] = value (string number)
24 -- ARGV[4] = expire (string number, seconds)
25 -- ARGV[5] = timestamp (string number, calendar seconds)
26 -- ARGV[6] = is_weak ("0" or "1")
27 -- ARGV[7] = digest (raw bytes, used as value for shingle SETEX)
28
29 local key = KEYS[1]
30 local count_key = KEYS[2]
31 local op = ARGV[1]
32 local new_flag = tonumber(ARGV[2])
33 local new_value = tonumber(ARGV[3])
34 local expire = tonumber(ARGV[4])
35 local timestamp = ARGV[5]
36 local is_weak = tonumber(ARGV[6])
37 local digest = ARGV[7]
38
39 if op == "add" then
40 -- Multi-flag merge logic: up to 8 flag slots (primary '' + extra '1'..'7')
41 local data = redis.call('HGETALL', key)
42 local fields = {}
43 for i = 1, #data, 2 do
44 fields[data[i]] = data[i+1]
45 end
46
47 local slots = {}
48 local n_slots = 0
49
50 -- Check primary slot
51 if fields['V'] and fields['F'] then
52 slots[''] = {flag=tonumber(fields['F']), value=tonumber(fields['V'])}
53 n_slots = n_slots + 1
54 end
55 -- Check extra slots 1..7
56 for i = 1, 7 do
57 local si = tostring(i)
58 if fields['V'..si] and fields['F'..si] then
59 slots[si] = {flag=tonumber(fields['F'..si]), value=tonumber(fields['V'..si])}
60 n_slots = n_slots + 1
61 end
62 end
63
64 -- Try to find existing slot with same flag
65 local found_slot = nil
66 for slot, entry in pairs(slots) do
67 if entry.flag == new_flag then
68 found_slot = slot
69 break
70 end
71 end
72
73 if found_slot then
74 -- Increment existing slot value
75 redis.call('HINCRBY', key, 'V'..found_slot, new_value)
76 elseif n_slots == 0 then
77 -- Empty hash: create primary slot
78 if is_weak == 1 then
79 redis.call('HSETNX', key, 'F', new_flag)
80 redis.call('HSETNX', key, 'V', new_value)
81 else
82 redis.call('HSET', key, 'F', new_flag, 'V', new_value)
83 end
84 slots[''] = {flag=new_flag, value=new_value}
85 n_slots = 1
86 elseif n_slots < 8 then
87 -- Find an empty slot and use it
88 local empty_slot = nil
89 if not slots[''] then
90 empty_slot = ''
91 else
92 for i = 1, 7 do
93 if not slots[tostring(i)] then
94 empty_slot = tostring(i)
95 break
96 end
97 end
98 end
99 if empty_slot then
100 redis.call('HSET', key, 'F'..empty_slot, new_flag, 'V'..empty_slot, new_value)
101 slots[empty_slot] = {flag=new_flag, value=new_value}
102 n_slots = n_slots + 1
103 end
104 else
105 -- All 8 slots full: replace the minimum-value slot if new_value is larger
106 if is_weak == 0 then
107 local min_slot = nil
108 local min_val = nil
109 for slot, entry in pairs(slots) do
110 if min_val == nil or entry.value < min_val then
111 min_val = entry.value
112 min_slot = slot
113 end
114 end
115 if min_val ~= nil and new_value > min_val then
116 redis.call('HSET', key, 'F'..min_slot, new_flag, 'V'..min_slot, new_value)
117 slots[min_slot] = {flag=new_flag, value=new_value}
118 end
119 end
120 end
121
122 -- Ensure primary slot has the highest value (swap if needed)
123 if n_slots > 1 then
124 local max_val = nil
125 local max_slot = nil
126 for slot, _ in pairs(slots) do
127 local v = tonumber(redis.call('HGET', key, 'V'..slot) or '0')
128 if max_val == nil or v > max_val then
129 max_val = v
130 max_slot = slot
131 end
132 end
133 if max_slot ~= nil and max_slot ~= '' and slots[''] then
134 local pv = redis.call('HGET', key, 'V')
135 local pf = redis.call('HGET', key, 'F')
136 local bv = redis.call('HGET', key, 'V'..max_slot)
137 local bf = redis.call('HGET', key, 'F'..max_slot)
138 redis.call('HSET', key, 'V', bv, 'F', bf)
139 redis.call('HSET', key, 'V'..max_slot, pv, 'F'..max_slot, pf)
140 end
141 end
142
143 redis.call('HSETNX', key, 'C', timestamp)
144 redis.call('EXPIRE', key, expire)
145 redis.call('INCR', count_key)
146
147 -- Handle shingles: SETEX each shingle key with expire and digest as value
148 for i = 3, #KEYS do
149 redis.call('SETEX', KEYS[i], expire, digest)
150 end
151
152 elseif op == "del" then
153 redis.call('DEL', key)
154 redis.call('DECR', count_key)
155
156 for i = 3, #KEYS do
157 redis.call('DEL', KEYS[i])
158 end
159
160 elseif op == "refresh" then
161 redis.call('EXPIRE', key, expire)
162
163 for i = 3, #KEYS do
164 redis.call('EXPIRE', KEYS[i], expire)
165 end
166 end
167
168 return 1