@voltro/kv 0.89.2 → 0.90.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -67,12 +67,13 @@ local function fields(value, names)
67
67
  for name, _ in pairs(value) do if not allowed[name] then invalid() end end
68
68
  end
69
69
  local layouts = {
70
- revision = { 'op' }, fence = { 'op' }, inspect = { 'op', 'key' }, ['inspect-values'] = { 'op', 'keys' },
71
- read = { 'op', 'key', 'snapshot' }, tag = { 'op', 'tag' }, scan = { 'op', 'cursor' },
72
- write = { 'op', 'key', 'snapshot', 'version', 'revision', 'tags', 'value', 'ttlMs', 'fence' },
73
- remove = { 'op', 'key', 'snapshot', 'revision' },
74
- invalidate = { 'op', 'tag', 'members', 'snapshots', 'revision' },
75
- clear = { 'op', 'keys', 'revision' },
70
+ fence = { 'op' }, inspect = { 'op', 'key' }, ['inspect-values'] = { 'op', 'keys' },
71
+ read = { 'op', 'key', 'snapshot' }, scan = { 'op', 'cursor', 'count' },
72
+ write = { 'op', 'key', 'snapshot', 'version', 'tags', 'value', 'ttlMs', 'fence' },
73
+ remove = { 'op', 'key', 'snapshot' },
74
+ retire = { 'op', 'tag', 'retired' }, retired = { 'op', 'tag', 'retired', 'count' },
75
+ purge = { 'op', 'tag', 'retired', 'members', 'snapshots' },
76
+ clear = { 'op' }, sweep = { 'op', 'snapshots', 'retired' },
76
77
  }
77
78
  if not layouts[c.op] then invalid() end
78
79
  fields(c, layouts[c.op])
@@ -83,14 +84,22 @@ for name, value in pairs(policy) do
83
84
  if type(value) ~= 'number' or value < minimum or value % 1 ~= 0 or value > 9007199254740991 then invalid() end
84
85
  end
85
86
  local root = KEYS[1]
86
- if string.sub(root, -2) ~= ':r' then invalid() end
87
+ -- KEYS may carry a registry prefix; the canonical namespace token must end the root.
88
+ local token = string.match(root, '{voltro:cache:atomic:([a-f0-9]+)}:r$')
89
+ if not token then invalid() end
90
+ id(token)
87
91
  local space = string.sub(root, 1, #root - 2)
88
92
  local declared = {}
89
93
  for _, key in ipairs(KEYS) do if declared[key] then invalid() end; declared[key] = key end
90
94
  local needed = { [root] = true }
95
+ local PAGE = ${64}
96
+ local SCAN = ${200}
91
97
  local function needValue(k) needed[space .. ':v:' .. id(k)] = true end
92
98
  local function needTag(t) needed[space .. ':t:' .. id(t)] = true end
93
99
  local function needEpoch(t) needed[space .. ':e:' .. id(t)] = true end
100
+ local function needGeneration() needed[space .. ':g'] = true end
101
+ local function retiredRef(r) local v, t = member(r); return v .. ':' .. t end
102
+ local function needRetired(r) needed[space .. ':d:' .. retiredRef(r)] = true end
94
103
  local function counter(n)
95
104
  if type(n) ~= 'number' or n < 0 or n % 1 ~= 0 or n > 9007199254740991 then invalid() end
96
105
  return n
@@ -100,93 +109,88 @@ local function needSnapshot(s)
100
109
  local value = snapshot(s)
101
110
  if value then for _, tag in ipairs(value.tags) do needTag(tag) end end
102
111
  end
112
+ local function bounded(a) if #a > PAGE then invalid() end; return a end
113
+ local function needSnapshots(list)
114
+ local keys = {}
115
+ array(bounded(list), function(s)
116
+ fields(s, { 'key', 'snapshot' }); needValue(s.key); needSnapshot(s.snapshot); table.insert(keys, s.key)
117
+ end)
118
+ for i = 2, #keys do if keys[i - 1] >= keys[i] then invalid() end end
119
+ return keys
120
+ end
103
121
  if c.op == 'fence' then needed[space .. ':s'] = true
104
122
  elseif c.op == 'inspect' then needValue(c.key)
105
- elseif c.op == 'inspect-values' then ids(c.keys); for _, key in ipairs(c.keys) do needValue(key) end
106
- elseif c.op == 'read' then needValue(c.key); needSnapshot(c.snapshot)
123
+ elseif c.op == 'inspect-values' then ids(bounded(c.keys)); for _, key in ipairs(c.keys) do needValue(key) end
124
+ elseif c.op == 'read' then needValue(c.key); needSnapshot(c.snapshot); needGeneration()
107
125
  elseif c.op == 'write' then
108
- needValue(c.key); needSnapshot(c.snapshot); ids(c.tags)
126
+ needValue(c.key); needSnapshot(c.snapshot); ids(c.tags); needGeneration()
109
127
  for _, tag in ipairs(c.tags) do needTag(tag) end
110
- version(c.version); version(c.revision)
128
+ version(c.version)
111
129
  if not null(c.fence) then counter(c.fence); for _, tag in ipairs(c.tags) do needEpoch(tag) end end
112
130
  if not null(c.snapshot) and c.snapshot.version == c.version then invalid() end
113
131
  if type(c.value) ~= 'string' or not null(c.ttlMs) and (type(c.ttlMs) ~= 'number' or c.ttlMs < 0 or c.ttlMs % 1 ~= 0 or c.ttlMs > 9007199254740991) then invalid() end
114
- elseif c.op == 'remove' then needValue(c.key); needSnapshot(c.snapshot); version(c.revision)
115
- elseif c.op == 'tag' then needTag(c.tag)
116
- elseif c.op == 'invalidate' then
117
- needTag(c.tag); version(c.revision); needed[space .. ':s'] = true; needEpoch(c.tag)
132
+ elseif c.op == 'remove' then needValue(c.key); needSnapshot(c.snapshot); needGeneration()
133
+ elseif c.op == 'retire' then
134
+ needTag(c.tag); needed[space .. ':s'] = true; needEpoch(c.tag); needRetired(version(c.retired) .. ':' .. c.tag)
135
+ elseif c.op == 'retired' then
136
+ needRetired(version(c.retired) .. ':' .. id(c.tag))
137
+ if type(c.count) ~= 'number' or c.count < 1 or c.count > PAGE or c.count % 1 ~= 0 then invalid() end
138
+ elseif c.op == 'purge' then
139
+ needTag(c.tag); needRetired(version(c.retired) .. ':' .. c.tag); needGeneration()
118
140
  local memberKeys = {}
119
- array(c.members, function(m) local _, key = member(m); memberKeys[key] = true end)
141
+ array(bounded(c.members), function(m) local _, key = member(m); memberKeys[key] = true end)
120
142
  for i = 2, #c.members do if c.members[i - 1] >= c.members[i] then invalid() end end
121
- array(c.snapshots, function(s)
122
- fields(s, { 'key', 'snapshot' }); needValue(s.key); needSnapshot(s.snapshot)
123
- if not memberKeys[s.key] then invalid() end; memberKeys[s.key] = nil
124
- end)
143
+ for _, key in ipairs(needSnapshots(c.snapshots)) do
144
+ if not memberKeys[key] then invalid() end; memberKeys[key] = nil
145
+ end
125
146
  for _ in pairs(memberKeys) do invalid() end
126
- for i = 2, #c.snapshots do if c.snapshots[i - 1].key >= c.snapshots[i].key then invalid() end end
127
- elseif c.op == 'clear' then
128
- if not null(c.revision) then version(c.revision) end
129
- -- KEYS may carry a registry prefix; ARGV retains caller-logical keys.
130
- -- The canonical cache token binds those two domains exactly, without
131
- -- accepting a foreign connection prefix or another cache namespace.
132
- local logicalRoot = string.match(root, '({voltro:cache:atomic:[a-f0-9]+}:r)$')
133
- if not logicalRoot then invalid() end
134
- id(string.match(logicalRoot, '^{voltro:cache:atomic:([a-f0-9]+)}:r$'))
135
- local logicalSpace = string.sub(logicalRoot, 1, #logicalRoot - 2)
136
- local selected = {}
137
- array(c.keys, function(key)
138
- if type(key) ~= 'string' then invalid() end
139
- local kind = string.sub(key, 1, #space + 3)
140
- if key ~= root and kind ~= space .. ':v:' and kind ~= space .. ':t:' then
141
- local logicalKind = string.sub(key, 1, #logicalSpace + 3)
142
- if key == logicalRoot then key = root
143
- elseif logicalKind == logicalSpace .. ':v:' or logicalKind == logicalSpace .. ':t:' then
144
- key = space .. string.sub(key, #logicalSpace + 1)
145
- else invalid() end
146
- end
147
- if selected[key] then invalid() end
148
- selected[key] = true
149
- if key ~= root then
150
- local prefix = string.sub(key, 1, #space + 3)
151
- if prefix ~= space .. ':v:' and prefix ~= space .. ':t:' then invalid() end
152
- id(string.sub(key, #space + 4)); needed[key] = true
153
- end
154
- end)
155
- for i = 2, #c.keys do if c.keys[i - 1] >= c.keys[i] then invalid() end end
147
+ elseif c.op == 'clear' then needGeneration()
148
+ elseif c.op == 'sweep' then
149
+ needGeneration(); needSnapshots(c.snapshots)
150
+ array(bounded(c.retired), needRetired)
151
+ for i = 2, #c.retired do if c.retired[i - 1] >= c.retired[i] then invalid() end end
156
152
  elseif c.op == 'scan' then
157
153
  if type(c.cursor) ~= 'string' or not string.match(c.cursor, '^%d+$') or #c.cursor > 20
158
154
  or #c.cursor > 1 and string.sub(c.cursor, 1, 1) == '0'
159
155
  or #c.cursor == 20 and c.cursor > '18446744073709551615' then invalid() end
160
- elseif c.op ~= 'revision' then invalid() end
156
+ if type(c.count) ~= 'number' or c.count < 1 or c.count > SCAN or c.count % 1 ~= 0 then invalid() end
157
+ else invalid() end
161
158
  for key, _ in pairs(needed) do if not declared[key] then invalid() end end
162
159
  for key, _ in pairs(declared) do if not needed[key] then invalid() end end
163
160
  local function valueKey(k) local key = declared[space .. ':v:' .. k]; if not key then invalid() end; return key end
164
161
  local function tagKey(t) local key = declared[space .. ':t:' .. t]; if not key then invalid() end; return key end
165
162
  local function epochKey(t) local key = declared[space .. ':e:' .. t]; if not key then invalid() end; return key end
163
+ local function retiredKey(r) local key = declared[space .. ':d:' .. r]; if not key then invalid() end; return key end
166
164
  local function sequence()
167
165
  local key = declared[space .. ':s']; if not key then invalid() end
168
166
  local raw = redis.call('GET', key)
169
167
  if not raw then return 0, key end
170
168
  return counter(tonumber(raw)), key
171
169
  end
170
+ local function generation()
171
+ local key = declared[space .. ':g']; if not key then invalid() end
172
+ local raw = redis.call('GET', key)
173
+ if not raw then return 0, key end
174
+ return counter(tonumber(raw)), key
175
+ end
172
176
  local RETENTION = ${e}
173
177
  local function keyType(k)
174
178
  local t = redis.call('TYPE', k)
175
179
  return type(t) == 'table' and t.ok or t
176
180
  end
177
181
  for _, key in ipairs(KEYS) do
178
- if c.op ~= 'clear' then
179
- local t = keyType(key)
180
- local kind = string.sub(key, #space + 1, #space + 3)
181
- local wanted = key == root or key == space .. ':s' or kind == ':v:' or kind == ':e:'
182
- if t ~= 'none' and (wanted and t ~= 'string' or not wanted and t ~= 'zset') then invalid() end
183
- end
182
+ local t = keyType(key)
183
+ local kind = string.sub(key, #space + 1, #space + 3)
184
+ local wanted = key == root or key == space .. ':s' or key == space .. ':g' or kind == ':v:' or kind == ':e:'
185
+ if t ~= 'none' and (wanted and t ~= 'string' or not wanted and t ~= 'zset') then invalid() end
184
186
  end
185
187
  local function entry(k)
186
188
  local raw = redis.call('GET', valueKey(k))
187
189
  if not raw then return nil end
188
190
  local e = cjson.decode(raw)
189
- fields(e, { 'version', 'tags', 'value' })
191
+ if type(e) ~= 'table' then invalid() end
192
+ if e.g == nil then fields(e, { 'version', 'tags', 'value' }); e.g = 0
193
+ else fields(e, { 'version', 'tags', 'value', 'g' }); if counter(e.g) < 1 then invalid() end end
190
194
  snapshot(e)
191
195
  if type(e.value) ~= 'string' then invalid() end
192
196
  return e
@@ -208,9 +212,22 @@ local function detach(k, e)
208
212
  local m = e.version .. ':' .. k
209
213
  for _, tag in ipairs(e.tags) do redis.call('ZREM', tagKey(tag), m); touch(tag) end
210
214
  end
211
- if c.op == 'revision' then
212
- return reply('ok', redis.call('GET', root) or cjson.null)
213
- elseif c.op == 'fence' then
215
+ -- Readable = written in the current generation with every membership live.
216
+ local function readable(k, e, current)
217
+ if e.g ~= current then return false end
218
+ local m = e.version .. ':' .. k
219
+ for _, tag in ipairs(e.tags) do
220
+ local score = redis.call('ZSCORE', tagKey(tag), m)
221
+ if not score or tonumber(score) <= now then return false end
222
+ end
223
+ return true
224
+ end
225
+ local function expected(list)
226
+ local out = {}
227
+ for _, s in ipairs(list) do out[s.key] = s.snapshot end
228
+ return out
229
+ end
230
+ if c.op == 'fence' then
214
231
  local n = sequence()
215
232
  return reply('ok', n)
216
233
  elseif c.op == 'inspect' then return reply('ok', meta(entry(c.key)))
@@ -218,7 +235,8 @@ elseif c.op == 'inspect-values' then
218
235
  local out = {}
219
236
  for _, key in ipairs(c.keys) do table.insert(out, { key = key, snapshot = meta(entry(key)) }) end
220
237
  return reply('ok', out)
221
- elseif c.op == 'tag' then return reply('ok', redis.call('ZRANGE', tagKey(c.tag), 0, -1))
238
+ elseif c.op == 'retired' then
239
+ return reply('ok', redis.call('ZRANGE', retiredKey(c.retired .. ':' .. c.tag), 0, c.count - 1))
222
240
  elseif c.op == 'scan' then
223
241
  local escaped = {}
224
242
  for i = 1, #space do
@@ -226,7 +244,7 @@ elseif c.op == 'scan' then
226
244
  if char == '\\' or char == '*' or char == '?' or char == '[' or char == ']' then table.insert(escaped, '\\') end
227
245
  table.insert(escaped, char)
228
246
  end
229
- local page = redis.call('SCAN', c.cursor, 'MATCH', table.concat(escaped) .. ':*', 'COUNT', 200)
247
+ local page = redis.call('SCAN', c.cursor, 'MATCH', table.concat(escaped) .. ':*', 'COUNT', c.count)
230
248
  local relative = {}
231
249
  for _, key in ipairs(page[2]) do table.insert(relative, string.sub(key, #space + 1)) end
232
250
  return reply('ok', { cursor = page[1], keys = relative })
@@ -234,11 +252,7 @@ elseif c.op == 'read' then
234
252
  local e = entry(c.key)
235
253
  if not e then return reply('ok') end
236
254
  if not same(e, c.snapshot) then return reply('conflict') end
237
- local m = e.version .. ':' .. c.key
238
- for _, tag in ipairs(e.tags) do
239
- local score = redis.call('ZSCORE', tagKey(tag), m)
240
- if not score or tonumber(score) <= now then return reply('ok') end
241
- end
255
+ if not readable(c.key, e, generation()) then return reply('ok') end
242
256
  return reply('ok', e.value)
243
257
  elseif c.op == 'write' or c.op == 'remove' then
244
258
  -- A compute fenced before an invalidation of one of its tags is void: its
@@ -249,14 +263,20 @@ elseif c.op == 'write' or c.op == 'remove' then
249
263
  if raw and counter(tonumber(raw)) > c.fence then return reply('ok', 0) end
250
264
  end
251
265
  end
266
+ local current = generation()
252
267
  local old = entry(c.key)
268
+ -- An untagged entry of an earlier generation is unreadable and has no
269
+ -- membership to retire: a writer expecting the key absent may replace it.
270
+ if old and old.g ~= current and #old.tags == 0 and null(c.snapshot) then
271
+ redis.call('DEL', valueKey(c.key)); old = nil
272
+ end
253
273
  if not same(old, c.snapshot) then return reply('conflict') end
254
274
  if c.op == 'remove' or c.ttlMs == 0 then
255
275
  if not old then return reply('ok', 0) end
256
- redis.call('SET', root, c.revision)
257
- local removed = redis.call('DEL', valueKey(c.key))
276
+ local wasReadable = readable(c.key, old, current)
277
+ redis.call('DEL', valueKey(c.key))
258
278
  detach(c.key, old)
259
- return reply('ok', removed)
279
+ return reply('ok', wasReadable and 1 or 0)
260
280
  end
261
281
  if policy.maxTagMembers then
262
282
  local oldMember = old and old.version .. ':' .. c.key or nil
@@ -271,55 +291,80 @@ elseif c.op == 'write' or c.op == 'remove' then
271
291
  if count > policy.maxTagMembers then return reply('limit') end
272
292
  end
273
293
  end
274
- -- Revise first: even an interrupted/erroring writer invalidates clear snapshots.
275
- redis.call('SET', root, c.revision)
276
294
  local expires = null(c.ttlMs) and NOEXP or now + c.ttlMs
277
295
  local m = c.version .. ':' .. c.key
278
296
  -- Prepare independent new memberships. No old live membership is removed yet.
279
297
  for _, tag in ipairs(c.tags) do redis.call('ZADD', tagKey(tag), expires, m); touch(tag) end
280
- local raw = cjson.encode({ version = c.version, tags = c.tags, value = c.value })
298
+ local record = { version = c.version, tags = c.tags, value = c.value }
299
+ if current > 0 then record.g = current end
300
+ local raw = cjson.encode(record)
281
301
  if null(c.ttlMs) then redis.call('SET', valueKey(c.key), raw)
282
302
  else redis.call('PSETEX', valueKey(c.key), c.ttlMs, raw) end
283
303
  if old then detach(c.key, old) end
284
304
  return reply('ok', 1)
285
- elseif c.op == 'invalidate' then
305
+ elseif c.op == 'retire' then
306
+ -- One transition: the tag index moves aside, so no entry of the tag has a
307
+ -- live membership any more, and the fence advances. Entries are reclaimed
308
+ -- by later bounded purges; a new write of the tag starts a fresh index.
309
+ local destination = retiredKey(c.retired .. ':' .. c.tag)
310
+ if redis.call('EXISTS', destination) == 1 then return reply('conflict') end
311
+ local n, sequenceKey = sequence()
312
+ redis.call('SET', sequenceKey, n + 1)
313
+ redis.call('SET', epochKey(c.tag), n + 1, 'PX', RETENTION)
286
314
  local tag = tagKey(c.tag)
287
- if redis.call('ZCARD', tag) ~= #c.members then return reply('conflict') end
288
- for _, m in ipairs(c.members) do if not redis.call('ZSCORE', tag, m) then return reply('conflict') end end
289
- local expected = {}
290
- for _, s in ipairs(c.snapshots) do expected[s.key] = s.snapshot end
315
+ if redis.call('EXISTS', tag) == 0 then return reply('ok', 0) end
316
+ redis.call('RENAME', tag, destination)
317
+ return reply('ok', 1)
318
+ elseif c.op == 'purge' then
319
+ local retired = retiredKey(c.retired .. ':' .. c.tag)
320
+ local current = generation()
321
+ local snapshots = expected(c.snapshots)
291
322
  local selected = {}
292
323
  for _, m in ipairs(c.members) do
293
324
  local v, k = member(m)
294
- if not selected[k] then
325
+ if not selected[k] and redis.call('ZSCORE', retired, m) then
295
326
  local e = entry(k)
296
327
  if e and e.version == v and contains(e.tags, c.tag) then
297
- if not same(e, expected[k]) then return reply('conflict') end
328
+ if not same(e, snapshots[k]) then return reply('conflict') end
298
329
  selected[k] = e
299
330
  end
300
331
  end
301
332
  end
302
- redis.call('SET', root, c.revision)
303
- local n, sequenceKey = sequence()
304
- redis.call('SET', sequenceKey, n + 1)
305
- redis.call('SET', epochKey(c.tag), n + 1, 'PX', RETENTION)
306
333
  local removed = 0
307
334
  for k, e in pairs(selected) do
308
- removed = removed + redis.call('DEL', valueKey(k))
335
+ -- Counted when it was readable until the retirement: current generation,
336
+ -- every other membership still live.
337
+ local counted = e.g == current
338
+ local m = e.version .. ':' .. k
339
+ for _, tag in ipairs(e.tags) do
340
+ if tag ~= c.tag then
341
+ local score = redis.call('ZSCORE', tagKey(tag), m)
342
+ if not score or tonumber(score) <= now then counted = false end
343
+ end
344
+ end
345
+ redis.call('DEL', valueKey(k))
309
346
  detach(k, e)
347
+ if counted then removed = removed + 1 end
310
348
  end
311
- redis.call('DEL', tag)
349
+ for _, m in ipairs(c.members) do redis.call('ZREM', retired, m) end
312
350
  return reply('ok', removed)
313
351
  elseif c.op == 'clear' then
314
- local observed = redis.call('GET', root) or cjson.null
315
- if observed ~= c.revision then return reply('conflict') end
316
- -- DEL is bounded per native command; all keys were declared before execution.
317
- for first = 1, #KEYS, 500 do
318
- local batch = {}
319
- for i = first, math.min(first + 499, #KEYS) do table.insert(batch, KEYS[i]) end
320
- redis.call('DEL', unpack(batch))
352
+ local n, key = generation()
353
+ redis.call('SET', key, n + 1)
354
+ return reply('ok', n + 1)
355
+ elseif c.op == 'sweep' then
356
+ local current = generation()
357
+ local removed = 0
358
+ for _, s in ipairs(c.snapshots) do
359
+ local e = entry(s.key)
360
+ if e and same(e, s.snapshot) and not readable(s.key, e, current) then
361
+ redis.call('DEL', valueKey(s.key))
362
+ detach(s.key, e)
363
+ removed = removed + 1
364
+ end
321
365
  end
322
- return reply('ok')
366
+ for _, r in ipairs(c.retired) do redis.call('DEL', retiredKey(r)) end
367
+ return reply('ok', removed)
323
368
  end
324
369
  invalid()
325
370
  `;
@@ -1,140 +1,156 @@
1
- import { CACHE_FENCE_RETENTION_MS as e, CacheAtomicProtocolError as t, cacheAtomicKeys as n, cacheAtomicMember as r, cacheAtomicMembership as i, cacheAtomicSnapshot as a, decodeCacheAtomicCommand as o, decodeCacheAtomicPolicy as s } from "./cacheAtomicProtocol.js";
1
+ import { CACHE_FENCE_RETENTION_MS as e, CacheAtomicProtocolError as t, cacheAtomicKeys as n, cacheAtomicMember as r, cacheAtomicMembership as i, cacheAtomicRetired as a, cacheAtomicSnapshot as o, decodeCacheAtomicCommand as s, decodeCacheAtomicPolicy as c } from "./cacheAtomicProtocol.js";
2
2
  // src/cacheAtomicState.ts
3
- var c = 9e15, l = () => {
3
+ var l = 9e15, u = () => {
4
4
  throw new t();
5
- }, u = (t, u, d, f = {}) => {
6
- let p = o(d), m = s(f);
7
- if (!Number.isSafeInteger(t.now) || t.now < 0 || t.now >= c) return l();
8
- let h = u[0];
9
- if (h === void 0) return l();
10
- let g = n(h, p), _ = new Set(u);
11
- if (u.length !== g.length || _.size !== u.length || g.some((e) => !_.has(e))) return l();
12
- let v = h.slice(0, -2), y = (e) => `${v}:v:${e}`, b = (e) => `${v}:t:${e}`, x = `${v}:s`, S = (e) => `${v}:e:${e}`, C = (e) => e === h || e === x || e.startsWith(`${v}:v:`) || e.startsWith(`${v}:e:`);
13
- if (p.op !== "clear") for (let e of u) {
5
+ }, d = (t, d, f, p = {}) => {
6
+ let m = s(f), h = c(p);
7
+ if (!Number.isSafeInteger(t.now) || t.now < 0 || t.now >= l) return u();
8
+ let g = d[0];
9
+ if (g === void 0) return u();
10
+ let _ = n(g, m), v = new Set(d);
11
+ if (d.length !== _.length || v.size !== d.length || _.some((e) => !v.has(e))) return u();
12
+ let y = g.slice(0, -2), b = (e) => `${y}:v:${e}`, x = (e) => `${y}:t:${e}`, S = `${y}:s`, C = `${y}:g`, w = (e) => `${y}:e:${e}`, T = (e, t) => `${y}:d:${a(e, t)}`, E = (e) => e === g || e === S || e === C || e.startsWith(`${y}:v:`) || e.startsWith(`${y}:e:`);
13
+ for (let e of d) {
14
14
  let n = t.kind(e);
15
- if (n !== "none" && n !== (C(e) ? "string" : "zset")) return l();
15
+ if (n !== "none" && n !== (E(e) ? "string" : "zset")) return u();
16
16
  }
17
- let w = (e) => {
17
+ let D = (e) => {
18
18
  if (e === null) return 0;
19
19
  let t = Number(e);
20
- return Number.isSafeInteger(t) && t >= 0 ? t : l();
21
- }, T = (e) => {
22
- let n = t.get(y(e));
20
+ return Number.isSafeInteger(t) && t >= 0 ? t : u();
21
+ }, O = (e) => {
22
+ let n = t.get(b(e));
23
23
  if (n === null) return null;
24
24
  let r;
25
25
  try {
26
26
  r = JSON.parse(n);
27
27
  } catch {
28
- return l();
28
+ return u();
29
29
  }
30
- if (typeof r != "object" || !r || Array.isArray(r)) return l();
31
- let i = r;
32
- if (Object.keys(i).length !== 3 || typeof i.value != "string") return l();
33
- let o = a({
30
+ if (typeof r != "object" || !r || Array.isArray(r)) return u();
31
+ let i = r, a = Object.hasOwn(i, "g") ? i.g : 0;
32
+ if (Object.keys(i).length !== (Object.hasOwn(i, "g") ? 4 : 3) || typeof i.value != "string" || typeof a != "number" || !Number.isSafeInteger(a) || a < +!!Object.hasOwn(i, "g")) return u();
33
+ let s = o({
34
34
  version: i.version,
35
35
  tags: i.tags
36
36
  });
37
- return o === null ? l() : {
38
- ...o,
39
- value: i.value
37
+ return s === null ? u() : {
38
+ ...s,
39
+ value: i.value,
40
+ generation: a
40
41
  };
41
- }, E = (e, t) => e === null || t === null ? e === t : e.version === t.version && e.tags.length === t.tags.length && e.tags.every((e, n) => e === t.tags[n]), D = (e) => e === null ? null : {
42
+ }, k = (e, t) => e === null || t === null ? e === t : e.version === t.version && e.tags.length === t.tags.length && e.tags.every((e, n) => e === t.tags[n]), A = (e) => e === null ? null : {
42
43
  version: e.version,
43
44
  tags: e.tags
44
- }, O = (e, t = null) => {
45
+ }, j = (e, t = null) => {
45
46
  let n = JSON.stringify({
46
47
  status: e,
47
48
  value: t
48
49
  });
49
- return m.maxReplyBytes !== void 0 && new TextEncoder().encode(n).length > m.maxReplyBytes ? JSON.stringify({
50
+ return h.maxReplyBytes !== void 0 && new TextEncoder().encode(n).length > h.maxReplyBytes ? JSON.stringify({
50
51
  status: "limit",
51
52
  value: null
52
53
  }) : n;
53
- }, k = (e) => {
54
- let n = b(e);
55
- for (let e of t.members(n)) e.score <= t.now && t.removeMember(n, e.member);
56
- let r = t.members(n);
57
- if (r.length === 0) {
58
- t.remove(n);
54
+ }, M = (e, n) => t.members(e).some((e) => e.member === n && e.score > t.now), N = (e) => {
55
+ let n = t.members(e);
56
+ if (n.length === 0) {
57
+ t.remove(e);
59
58
  return;
60
59
  }
61
- let i = -Infinity;
62
- for (let e of r) i = Math.max(i, e.score);
63
- t.expire(n, i >= c ? null : Math.max(1, Math.ceil(i - t.now)));
64
- }, A = (e, n) => {
65
- for (let r of n.tags) t.removeMember(b(r), i(e, n.version)), k(r);
66
- }, j = (e) => t.set(h, e.revision);
67
- switch (p.op) {
68
- case "revision": return O("ok", t.get(h));
69
- case "fence": return O("ok", w(t.get(x)));
70
- case "inspect": return O("ok", D(T(p.key)));
71
- case "inspect-values": return O("ok", p.keys.map((e) => ({
60
+ let r = -Infinity;
61
+ for (let e of n) r = Math.max(r, e.score);
62
+ t.expire(e, r >= l ? null : Math.max(1, Math.ceil(r - t.now)));
63
+ }, P = (e) => {
64
+ let n = x(e);
65
+ for (let e of t.members(n)) e.score <= t.now && t.removeMember(n, e.member);
66
+ N(n);
67
+ }, F = (e, n) => {
68
+ for (let r of n.tags) t.removeMember(x(r), i(e, n.version)), P(r);
69
+ }, I = () => D(t.get(C)), L = (e, t, n) => t.generation === n && t.tags.every((n) => M(x(n), i(e, t.version)));
70
+ switch (m.op) {
71
+ case "fence": return j("ok", D(t.get(S)));
72
+ case "inspect": return j("ok", A(O(m.key)));
73
+ case "inspect-values": return j("ok", m.keys.map((e) => ({
72
74
  key: e,
73
- snapshot: D(T(e))
75
+ snapshot: A(O(e))
74
76
  })));
75
- case "tag": return O("ok", t.members(b(p.tag)).slice().sort((e, t) => e.score - t.score || (e.member < t.member ? -1 : +(e.member > t.member))).map((e) => e.member));
76
- case "scan": return p.cursor === "0" ? O("ok", {
77
+ case "retired": return j("ok", t.members(T(m.retired, m.tag)).slice().sort((e, t) => e.score - t.score || (e.member < t.member ? -1 : +(e.member > t.member))).slice(0, m.count).map((e) => e.member));
78
+ case "scan": return m.cursor === "0" ? j("ok", {
77
79
  cursor: "0",
78
- keys: t.keys().filter((e) => e.startsWith(`${v}:`)).map((e) => e.slice(v.length))
79
- }) : l();
80
+ keys: t.keys().filter((e) => e.startsWith(`${y}:`)).map((e) => e.slice(y.length))
81
+ }) : u();
80
82
  case "read": {
81
- let e = T(p.key);
82
- if (e === null) return O("ok");
83
- if (!E(e, p.snapshot)) return O("conflict");
84
- let n = i(p.key, e.version);
85
- return e.tags.some((e) => !t.members(b(e)).some((e) => e.member === n && e.score > t.now)) ? O("ok") : O("ok", e.value);
83
+ let e = O(m.key);
84
+ return e === null ? j("ok") : k(e, m.snapshot) ? L(m.key, e, I()) ? j("ok", e.value) : j("ok") : j("conflict");
86
85
  }
87
86
  case "write":
88
87
  case "remove": {
89
- if (p.op === "write" && p.fence !== null) {
90
- let e = p.fence;
91
- if (p.tags.some((n) => w(t.get(S(n))) > e)) return O("ok", 0);
88
+ if (m.op === "write" && m.fence !== null) {
89
+ let e = m.fence;
90
+ if (m.tags.some((n) => D(t.get(w(n))) > e)) return j("ok", 0);
92
91
  }
93
- let e = T(p.key);
94
- if (!E(e, p.snapshot)) return O("conflict");
95
- if (p.op === "remove" || p.ttlMs === 0) {
96
- if (e === null) return O("ok", 0);
97
- j(p);
98
- let n = t.remove(y(p.key));
99
- return A(p.key, e), O("ok", n);
92
+ let e = I(), n = O(m.key);
93
+ if (n !== null && n.generation !== e && n.tags.length === 0 && m.snapshot === null && (t.remove(b(m.key)), n = null), !k(n, m.snapshot)) return j("conflict");
94
+ if (m.op === "remove" || m.ttlMs === 0) {
95
+ if (n === null) return j("ok", 0);
96
+ let r = L(m.key, n, e);
97
+ return t.remove(b(m.key)), F(m.key, n), j("ok", +!!r);
100
98
  }
101
- if (p.ttlMs !== null && p.ttlMs > c - t.now - 1) return l();
102
- if (m.maxTagMembers !== void 0) for (let n of p.tags) {
103
- let r = t.members(b(n)).filter((e) => e.score > t.now), a = e === null ? null : i(p.key, e.version), o = i(p.key, p.version);
104
- if (r.length - +!!r.some((e) => e.member === a) + +!r.some((e) => e.member === o) > m.maxTagMembers) return O("limit");
99
+ if (m.ttlMs !== null && m.ttlMs > l - t.now - 1) return u();
100
+ if (h.maxTagMembers !== void 0) for (let e of m.tags) {
101
+ let r = t.members(x(e)).filter((e) => e.score > t.now), a = n === null ? null : i(m.key, n.version), o = i(m.key, m.version);
102
+ if (r.length - +!!r.some((e) => e.member === a) + +!r.some((e) => e.member === o) > h.maxTagMembers) return j("limit");
105
103
  }
106
- j(p);
107
- for (let e of p.tags) t.addMember(b(e), i(p.key, p.version), p.ttlMs === null ? c : t.now + p.ttlMs), k(e);
108
- return t.set(y(p.key), JSON.stringify({
109
- version: p.version,
110
- tags: p.tags,
111
- value: p.value
112
- }), p.ttlMs ?? void 0), e !== null && A(p.key, e), O("ok", 1);
104
+ for (let e of m.tags) t.addMember(x(e), i(m.key, m.version), m.ttlMs === null ? l : t.now + m.ttlMs), P(e);
105
+ return t.set(b(m.key), JSON.stringify({
106
+ version: m.version,
107
+ tags: m.tags,
108
+ value: m.value,
109
+ ...e > 0 ? { g: e } : {}
110
+ }), m.ttlMs ?? void 0), n !== null && F(m.key, n), j("ok", 1);
113
111
  }
114
- case "invalidate": {
115
- let n = t.members(b(p.tag));
116
- if (n.length !== p.members.length || p.members.some((e) => !n.some((t) => t.member === e))) return O("conflict");
117
- let i = /* @__PURE__ */ new Map(), a = new Map(p.snapshots.map((e) => [e.key, e.snapshot]));
118
- for (let e of p.members) {
119
- let t = r(e);
120
- if (i.has(t.key)) continue;
121
- let n = T(t.key);
122
- if (n !== null && n.version === t.version && n.tags.includes(p.tag)) {
123
- if (!E(n, a.get(t.key) ?? null)) return O("conflict");
124
- i.set(t.key, n);
112
+ case "retire": {
113
+ let n = T(m.retired, m.tag);
114
+ if (t.kind(n) !== "none") return j("conflict");
115
+ let r = D(t.get(S)) + 1;
116
+ t.set(S, String(r)), t.set(w(m.tag), String(r), e);
117
+ let i = t.members(x(m.tag));
118
+ if (i.length === 0) return j("ok", 0);
119
+ for (let e of i) t.addMember(n, e.member, e.score);
120
+ return N(n), t.remove(x(m.tag)), j("ok", 1);
121
+ }
122
+ case "purge": {
123
+ let e = T(m.retired, m.tag), n = I(), a = new Map(m.snapshots.map((e) => [e.key, e.snapshot])), o = /* @__PURE__ */ new Map();
124
+ for (let n of m.members) {
125
+ let i = r(n);
126
+ if (o.has(i.key) || !t.members(e).some((e) => e.member === n)) continue;
127
+ let s = O(i.key);
128
+ if (s !== null && s.version === i.version && s.tags.includes(m.tag)) {
129
+ if (!k(s, a.get(i.key) ?? null)) return j("conflict");
130
+ o.set(i.key, s);
125
131
  }
126
132
  }
127
- j(p);
128
- let o = w(t.get(x)) + 1;
129
- t.set(x, String(o)), t.set(S(p.tag), String(o), e);
130
133
  let s = 0;
131
- for (let [e, n] of i) s += t.remove(y(e)), A(e, n);
132
- return t.remove(b(p.tag)), O("ok", s);
134
+ for (let [e, r] of o) {
135
+ let a = i(e, r.version), o = r.generation === n && r.tags.every((e) => e === m.tag || M(x(e), a));
136
+ t.remove(b(e)), F(e, r), o && s++;
137
+ }
138
+ for (let n of m.members) t.removeMember(e, n);
139
+ return N(e), j("ok", s);
140
+ }
141
+ case "clear": {
142
+ let e = I() + 1;
143
+ return t.set(C, String(e)), j("ok", e);
144
+ }
145
+ case "sweep": {
146
+ let e = I(), n = 0;
147
+ for (let r of m.snapshots) {
148
+ let i = O(r.key);
149
+ i !== null && k(i, r.snapshot) && !L(r.key, i, e) && (t.remove(b(r.key)), F(r.key, i), n++);
150
+ }
151
+ for (let e of m.retired) t.remove(`${y}:d:${e}`);
152
+ return j("ok", n);
133
153
  }
134
- case "clear":
135
- if (t.get(h) !== p.revision) return O("conflict");
136
- for (let e of u) t.remove(e);
137
- return O("ok");
138
154
  }
139
155
  };
140
- export { u as applyCacheAtomicCommand };
156
+ export { d as applyCacheAtomicCommand };