resplite 1.4.6 → 1.4.8

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resplite",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "A RESP2 server with practical Redis compatibility, backed by SQLite",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Parse score bound used by sorted-set score range commands.
3
+ * Supports Redis-style infinities: -inf, +inf, inf.
4
+ *
5
+ * @param {Buffer|string|number} raw
6
+ * @returns {number|null}
7
+ */
8
+ export function parseScoreBound(raw) {
9
+ const s = Buffer.isBuffer(raw) ? raw.toString('utf8') : String(raw);
10
+ const lower = s.toLowerCase();
11
+ if (lower === '-inf') return Number.NEGATIVE_INFINITY;
12
+ if (lower === '+inf' || lower === 'inf') return Number.POSITIVE_INFINITY;
13
+ const n = parseFloat(s);
14
+ return Number.isNaN(n) ? null : n;
15
+ }
@@ -2,14 +2,16 @@
2
2
  * ZCOUNT key min max - returns count of members in sorted set with score in [min, max].
3
3
  */
4
4
 
5
+ import { parseScoreBound } from './score-bounds.js';
6
+
5
7
  export function handleZcount(engine, args) {
6
8
  if (!args || args.length < 3) {
7
9
  return { error: 'ERR wrong number of arguments for \'ZCOUNT\' command' };
8
10
  }
9
11
  try {
10
- const min = parseFloat(String(args[1]));
11
- const max = parseFloat(String(args[2]));
12
- if (Number.isNaN(min) || Number.isNaN(max)) {
12
+ const min = parseScoreBound(args[1]);
13
+ const max = parseScoreBound(args[2]);
14
+ if (min == null || max == null) {
13
15
  return { error: 'ERR value is not a valid float' };
14
16
  }
15
17
  const n = engine.zcount(args[0], min, max);
@@ -2,6 +2,8 @@
2
2
  * ZRANGEBYSCORE key min max [WITHSCORES] [LIMIT offset count]
3
3
  */
4
4
 
5
+ import { parseScoreBound } from './score-bounds.js';
6
+
5
7
  export function handleZrangebyscore(engine, args) {
6
8
  if (!args || args.length < 3) {
7
9
  return { error: 'ERR wrong number of arguments for \'ZRANGEBYSCORE\' command' };
@@ -25,9 +27,9 @@ export function handleZrangebyscore(engine, args) {
25
27
  }
26
28
  }
27
29
  try {
28
- const min = parseFloat(String(args[1]));
29
- const max = parseFloat(String(args[2]));
30
- if (Number.isNaN(min) || Number.isNaN(max)) {
30
+ const min = parseScoreBound(args[1]);
31
+ const max = parseScoreBound(args[2]);
32
+ if (min == null || max == null) {
31
33
  return { error: 'ERR value is not a valid float' };
32
34
  }
33
35
  const options = { withScores };
@@ -2,14 +2,16 @@
2
2
  * ZREMRANGEBYSCORE key min max - removes members with score in [min, max]. Returns count removed.
3
3
  */
4
4
 
5
+ import { parseScoreBound } from './score-bounds.js';
6
+
5
7
  export function handleZremrangebyscore(engine, args) {
6
8
  if (!args || args.length < 3) {
7
9
  return { error: 'ERR wrong number of arguments for \'ZREMRANGEBYSCORE\' command' };
8
10
  }
9
11
  try {
10
- const min = parseFloat(String(args[1]));
11
- const max = parseFloat(String(args[2]));
12
- if (Number.isNaN(min) || Number.isNaN(max)) {
12
+ const min = parseScoreBound(args[1]);
13
+ const max = parseScoreBound(args[2]);
14
+ if (min == null || max == null) {
13
15
  return { error: 'ERR value is not a valid float' };
14
16
  }
15
17
  const n = engine.zremrangebyscore(args[0], min, max);
@@ -4,6 +4,8 @@
4
4
  * Note: Redis uses max min (first score is upper bound, second is lower bound).
5
5
  */
6
6
 
7
+ import { parseScoreBound } from './score-bounds.js';
8
+
7
9
  export function handleZrevrangebyscore(engine, args) {
8
10
  if (!args || args.length < 3) {
9
11
  return { error: 'ERR wrong number of arguments for \'ZREVRANGEBYSCORE\' command' };
@@ -27,9 +29,9 @@ export function handleZrevrangebyscore(engine, args) {
27
29
  }
28
30
  }
29
31
  try {
30
- const max = parseFloat(String(args[1]));
31
- const min = parseFloat(String(args[2]));
32
- if (Number.isNaN(max) || Number.isNaN(min)) {
32
+ const max = parseScoreBound(args[1]);
33
+ const min = parseScoreBound(args[2]);
34
+ if (max == null || min == null) {
33
35
  return { error: 'ERR value is not a valid float' };
34
36
  }
35
37
  const options = { withScores };
@@ -569,6 +569,7 @@ export function createEngine(opts = {}) {
569
569
  if (keys.get(nk)) keys.delete(nk);
570
570
  keys.set(nk, meta.type, {
571
571
  expiresAt: meta.expiresAt,
572
+ setCount: meta.type === KEY_TYPES.SET ? meta.setCount : undefined,
572
573
  hashCount: meta.type === KEY_TYPES.HASH ? meta.hashCount : undefined,
573
574
  zsetCount: meta.type === KEY_TYPES.ZSET ? meta.zsetCount : undefined,
574
575
  });
@@ -9,16 +9,22 @@ import { KEY_TYPES } from './schema.js';
9
9
  */
10
10
  export function createKeysStorage(db) {
11
11
  const getByKey = db.prepare(
12
- 'SELECT key, type, expires_at AS expiresAt, hash_count AS hashCount, zset_count AS zsetCount, version, updated_at AS updatedAt FROM redis_keys WHERE key = ?'
12
+ 'SELECT key, type, expires_at AS expiresAt, set_count AS setCount, hash_count AS hashCount, zset_count AS zsetCount, version, updated_at AS updatedAt FROM redis_keys WHERE key = ?'
13
13
  );
14
14
  const insert = db.prepare(
15
- `INSERT INTO redis_keys (key, type, expires_at, hash_count, zset_count, version, updated_at) VALUES (?, ?, ?, ?, ?, 1, ?)`
15
+ `INSERT INTO redis_keys (key, type, expires_at, set_count, hash_count, zset_count, version, updated_at) VALUES (?, ?, ?, ?, ?, ?, 1, ?)`
16
16
  );
17
17
  const updateMeta = db.prepare(
18
- 'UPDATE redis_keys SET type = ?, expires_at = ?, hash_count = ?, zset_count = ?, version = version + 1, updated_at = ? WHERE key = ?'
18
+ 'UPDATE redis_keys SET type = ?, expires_at = ?, set_count = ?, hash_count = ?, zset_count = ?, version = version + 1, updated_at = ? WHERE key = ?'
19
19
  );
20
20
  const updateExpires = db.prepare('UPDATE redis_keys SET expires_at = ?, updated_at = ? WHERE key = ?');
21
21
  const updateVersion = db.prepare('UPDATE redis_keys SET version = version + 1, updated_at = ? WHERE key = ?');
22
+ const updateSetCount = db.prepare('UPDATE redis_keys SET set_count = ?, updated_at = ? WHERE key = ?');
23
+ const updateSetCountOnly = db.prepare('UPDATE redis_keys SET set_count = ? WHERE key = ?');
24
+ const incrSetCount = db.prepare(
25
+ 'UPDATE redis_keys SET set_count = COALESCE(set_count, 0) + ?, updated_at = ? WHERE key = ?'
26
+ );
27
+ const incrSetCountOnly = db.prepare('UPDATE redis_keys SET set_count = COALESCE(set_count, 0) + ? WHERE key = ?');
22
28
  const updateHashCount = db.prepare('UPDATE redis_keys SET hash_count = ?, updated_at = ? WHERE key = ?');
23
29
  const updateHashCountOnly = db.prepare('UPDATE redis_keys SET hash_count = ? WHERE key = ?');
24
30
  const incrHashCount = db.prepare(
@@ -50,6 +56,9 @@ export function createKeysStorage(db) {
50
56
  const now = options.updatedAt ?? Date.now();
51
57
  const expiresAt = options.expiresAt ?? null;
52
58
  const existing = getByKey.get(key);
59
+ const setCount = type === KEY_TYPES.SET
60
+ ? (options.setCount ?? existing?.setCount ?? 0)
61
+ : null;
53
62
  const hashCount = type === KEY_TYPES.HASH
54
63
  ? (options.hashCount ?? existing?.hashCount ?? 0)
55
64
  : null;
@@ -57,9 +66,9 @@ export function createKeysStorage(db) {
57
66
  ? (options.zsetCount ?? existing?.zsetCount ?? 0)
58
67
  : null;
59
68
  if (existing) {
60
- updateMeta.run(type, expiresAt, hashCount, zsetCount, now, key);
69
+ updateMeta.run(type, expiresAt, setCount, hashCount, zsetCount, now, key);
61
70
  } else {
62
- insert.run(key, type, expiresAt, hashCount, zsetCount, now);
71
+ insert.run(key, type, expiresAt, setCount, hashCount, zsetCount, now);
63
72
  }
64
73
  },
65
74
 
@@ -71,6 +80,24 @@ export function createKeysStorage(db) {
71
80
  updateVersion.run(Date.now(), key);
72
81
  },
73
82
 
83
+ setSetCount(key, setCount, options = {}) {
84
+ const touchUpdatedAt = options.touchUpdatedAt !== false;
85
+ if (touchUpdatedAt) {
86
+ updateSetCount.run(setCount, options.updatedAt ?? Date.now(), key);
87
+ } else {
88
+ updateSetCountOnly.run(setCount, key);
89
+ }
90
+ },
91
+
92
+ incrSetCount(key, delta, options = {}) {
93
+ const touchUpdatedAt = options.touchUpdatedAt !== false;
94
+ if (touchUpdatedAt) {
95
+ incrSetCount.run(delta, options.updatedAt ?? Date.now(), key);
96
+ } else {
97
+ incrSetCountOnly.run(delta, key);
98
+ }
99
+ },
100
+
74
101
  setHashCount(key, hashCount, options = {}) {
75
102
  const touchUpdatedAt = options.touchUpdatedAt !== false;
76
103
  if (touchUpdatedAt) {
@@ -7,6 +7,7 @@ CREATE TABLE IF NOT EXISTS redis_keys (
7
7
  key BLOB PRIMARY KEY,
8
8
  type INTEGER NOT NULL,
9
9
  expires_at INTEGER,
10
+ set_count INTEGER,
10
11
  hash_count INTEGER,
11
12
  zset_count INTEGER,
12
13
  version INTEGER NOT NULL DEFAULT 1,
@@ -90,8 +91,12 @@ export function applySchema(db) {
90
91
  db.exec(SCHEMA);
91
92
  // Backward-compatible migration for databases created before count columns existed.
92
93
  const cols = db.prepare('PRAGMA table_info(redis_keys)').all();
94
+ const hasSetCount = cols.some((c) => c.name === 'set_count');
93
95
  const hasHashCount = cols.some((c) => c.name === 'hash_count');
94
96
  const hasZsetCount = cols.some((c) => c.name === 'zset_count');
97
+ if (!hasSetCount) {
98
+ db.exec('ALTER TABLE redis_keys ADD COLUMN set_count INTEGER;');
99
+ }
95
100
  if (!hasHashCount) {
96
101
  db.exec('ALTER TABLE redis_keys ADD COLUMN hash_count INTEGER;');
97
102
  }
@@ -22,34 +22,52 @@ export function createSetsStorage(db, keys) {
22
22
  return runInTransaction(db, () => {
23
23
  const now = options.updatedAt ?? Date.now();
24
24
  const meta = keys.get(key);
25
+ let knownCount = 0;
25
26
  if (meta) {
26
27
  if (meta.type !== KEY_TYPES.SET) {
27
28
  throw new Error('WRONGTYPE Operation against a key holding the wrong kind of value');
28
29
  }
29
30
  keys.bumpVersion(key);
31
+ if (meta.setCount == null) {
32
+ const row = countStmt.get(key);
33
+ knownCount = (row && row.n) || 0;
34
+ keys.setSetCount(key, knownCount, { touchUpdatedAt: false });
35
+ } else {
36
+ knownCount = meta.setCount;
37
+ }
30
38
  } else {
31
- keys.set(key, KEY_TYPES.SET, { updatedAt: now });
39
+ keys.set(key, KEY_TYPES.SET, { updatedAt: now, setCount: 0 });
32
40
  }
33
41
  let added = 0;
34
42
  for (const m of members) {
35
43
  const r = insertStmt.run(key, m);
36
44
  if (r.changes > 0) added++;
37
45
  }
46
+ if (added > 0) {
47
+ if (meta) keys.incrSetCount(key, added, { touchUpdatedAt: false });
48
+ else keys.setSetCount(key, added, { touchUpdatedAt: false });
49
+ } else if (meta && meta.setCount == null) {
50
+ // Legacy rows may have null counters; persist hydrated value.
51
+ keys.setSetCount(key, knownCount, { touchUpdatedAt: false });
52
+ }
38
53
  return added;
39
54
  });
40
55
  },
41
56
 
42
57
  remove(key, members) {
43
58
  return runInTransaction(db, () => {
59
+ const meta = keys.get(key);
60
+ const before = meta && meta.setCount != null ? meta.setCount : null;
44
61
  let n = 0;
45
62
  for (const m of members) {
46
63
  n += deleteStmt.run(key, m).changes;
47
64
  }
48
- const row = countStmt.get(key);
49
- const remaining = (row && row.n) || 0;
65
+ const remaining = before != null ? Math.max(0, before - n) : ((countStmt.get(key) || {}).n || 0);
50
66
  if (remaining === 0) {
51
67
  deleteAllStmt.run(key);
52
68
  keys.delete(key);
69
+ } else if (n > 0) {
70
+ keys.setSetCount(key, remaining, { touchUpdatedAt: false });
53
71
  }
54
72
  return n;
55
73
  });
@@ -65,8 +83,17 @@ export function createSetsStorage(db, keys) {
65
83
  },
66
84
 
67
85
  count(key) {
86
+ const meta = keys.get(key);
87
+ if (meta && meta.type === KEY_TYPES.SET && meta.setCount != null) {
88
+ return meta.setCount;
89
+ }
68
90
  const row = countStmt.get(key);
69
- return row ? row.n : 0;
91
+ const n = row ? row.n : 0;
92
+ if (meta && meta.type === KEY_TYPES.SET && meta.setCount == null) {
93
+ // One-time hydration for databases created before set_count existed.
94
+ keys.setSetCount(key, n, { touchUpdatedAt: false });
95
+ }
96
+ return n;
70
97
  },
71
98
 
72
99
  /** Copy all members from oldKey to newKey. Caller ensures newKey exists in redis_keys. */
@@ -75,6 +102,9 @@ export function createSetsStorage(db, keys) {
75
102
  for (const r of rows) {
76
103
  insertStmt.run(newKey, r.member);
77
104
  }
105
+ const sourceMeta = keys.get(oldKey);
106
+ const nextCount = sourceMeta && sourceMeta.setCount != null ? sourceMeta.setCount : rows.length;
107
+ keys.setSetCount(newKey, nextCount, { touchUpdatedAt: false });
78
108
  },
79
109
 
80
110
  /** Get random members without removing. count null/1 = single; count > 0 = up to count distinct; count < 0 = |count| with replacement. */
@@ -117,13 +147,13 @@ export function createSetsStorage(db, keys) {
117
147
  for (let i = 0; i < -c; i++) chosen.push(arr[Math.floor(Math.random() * arr.length)]);
118
148
  }
119
149
  for (const m of chosen) deleteStmt.run(key, m);
120
- const row = countStmt.get(key);
121
- const remaining = (row && row.n) || 0;
150
+ const remaining = arr.length - chosen.length;
122
151
  if (remaining === 0) {
123
152
  deleteAllStmt.run(key);
124
153
  keys.delete(key);
125
154
  } else {
126
155
  keys.bumpVersion(key);
156
+ keys.setSetCount(key, remaining, { touchUpdatedAt: false });
127
157
  }
128
158
  return c === 1 ? chosen[0] : chosen;
129
159
  });
@@ -315,6 +315,9 @@ export function createZsetsStorage(db, keys) {
315
315
  },
316
316
 
317
317
  countByScore(key, min, max) {
318
+ if (min === Number.NEGATIVE_INFINITY && max === Number.POSITIVE_INFINITY) {
319
+ return this.count(key);
320
+ }
318
321
  const row = countByScoreStmt.get(key, min, max);
319
322
  return row ? row.n : 0;
320
323
  },
@@ -1,7 +1,9 @@
1
1
  import { describe, it, before, after } from 'node:test';
2
2
  import assert from 'node:assert/strict';
3
+ import Database from 'better-sqlite3';
3
4
  import { createTestServer } from '../helpers/server.js';
4
5
  import { sendCommand, argv } from '../helpers/client.js';
6
+ import { tryParseValue } from '../../src/resp/parser.js';
5
7
 
6
8
  describe('Sets integration', () => {
7
9
  let s;
@@ -24,4 +26,51 @@ describe('Sets integration', () => {
24
26
  assert.ok(s.includes('$1\r\nb\r\n'));
25
27
  assert.ok(s.includes('$1\r\nc\r\n'));
26
28
  });
29
+
30
+ it('SCARD returns member count', async () => {
31
+ await sendCommand(port, argv('SADD', 'scard:1', 'a', 'b', 'c'));
32
+ const reply = await sendCommand(port, argv('SCARD', 'scard:1'));
33
+ assert.equal(tryParseValue(reply, 0).value, 3);
34
+ });
35
+
36
+ it('SCARD on non-existent key returns 0', async () => {
37
+ const reply = await sendCommand(port, argv('SCARD', 'scard:none'));
38
+ assert.equal(tryParseValue(reply, 0).value, 0);
39
+ });
40
+
41
+ it('SCARD decreases after SREM', async () => {
42
+ await sendCommand(port, argv('SADD', 'scard:2', 'x', 'y'));
43
+ await sendCommand(port, argv('SREM', 'scard:2', 'x'));
44
+ const reply = await sendCommand(port, argv('SCARD', 'scard:2'));
45
+ assert.equal(tryParseValue(reply, 0).value, 1);
46
+ });
47
+
48
+ it('RENAME keeps set cardinality metadata', async () => {
49
+ await sendCommand(port, argv('SADD', 'srename:src', 'm1', 'm2', 'm3'));
50
+ await sendCommand(port, argv('RENAME', 'srename:src', 'srename:dst'));
51
+ const reply = await sendCommand(port, argv('SCARD', 'srename:dst'));
52
+ assert.equal(tryParseValue(reply, 0).value, 3);
53
+ });
54
+
55
+ it('legacy set rows with null set_count hydrate on first SCARD', async () => {
56
+ const s1 = await createTestServer();
57
+ await sendCommand(s1.port, argv('SADD', 'legacy:s', 'a', 'b'));
58
+ const dbPath = s1.dbPath;
59
+ await s1.closeAsync();
60
+ s1.db.close();
61
+
62
+ const legacyDb = new Database(dbPath);
63
+ legacyDb.prepare('UPDATE redis_keys SET set_count = NULL WHERE key = ?').run(Buffer.from('legacy:s', 'utf8'));
64
+ legacyDb.close();
65
+
66
+ const s2 = await createTestServer({ dbPath });
67
+ const first = await sendCommand(s2.port, argv('SCARD', 'legacy:s'));
68
+ assert.equal(tryParseValue(first, 0).value, 2);
69
+ const second = await sendCommand(s2.port, argv('SCARD', 'legacy:s'));
70
+ assert.equal(tryParseValue(second, 0).value, 2);
71
+
72
+ const row = s2.db.prepare('SELECT set_count AS n FROM redis_keys WHERE key = ?').get(Buffer.from('legacy:s', 'utf8'));
73
+ assert.equal(row.n, 2);
74
+ await s2.closeAsync();
75
+ });
27
76
  });
@@ -159,6 +159,37 @@ describe('ZSET integration', () => {
159
159
  assert.equal(limited[1].toString('utf8'), 'c');
160
160
  });
161
161
 
162
+ it('ZCOUNT supports numeric ranges and full-range infinities', async () => {
163
+ await sendCommand(port, argv('ZADD', 'zc1', '1', 'a', '2', 'b', '3', 'c', '4', 'd'));
164
+
165
+ const mid = await sendCommand(port, argv('ZCOUNT', 'zc1', '2', '3'));
166
+ assert.equal(tryParseValue(mid, 0).value, 2);
167
+
168
+ const all = await sendCommand(port, argv('ZCOUNT', 'zc1', '-inf', '+inf'));
169
+ assert.equal(tryParseValue(all, 0).value, 4);
170
+ });
171
+
172
+ it('score-range commands accept -inf/+inf bounds', async () => {
173
+ await sendCommand(port, argv('ZADD', 'zinf', '1', 'a', '2', 'b', '3', 'c', '4', 'd'));
174
+
175
+ const rangeAll = await sendCommand(port, argv('ZRANGEBYSCORE', 'zinf', '-inf', '+inf'));
176
+ const rAll = tryParseValue(rangeAll, 0).value;
177
+ assert.equal(rAll.length, 4);
178
+ assert.equal(rAll[0].toString('utf8'), 'a');
179
+ assert.equal(rAll[3].toString('utf8'), 'd');
180
+
181
+ const revAll = await sendCommand(port, argv('ZREVRANGEBYSCORE', 'zinf', '+inf', '-inf'));
182
+ const rvAll = tryParseValue(revAll, 0).value;
183
+ assert.equal(rvAll.length, 4);
184
+ assert.equal(rvAll[0].toString('utf8'), 'd');
185
+ assert.equal(rvAll[3].toString('utf8'), 'a');
186
+
187
+ const removed = await sendCommand(port, argv('ZREMRANGEBYSCORE', 'zinf', '-inf', '+inf'));
188
+ assert.equal(tryParseValue(removed, 0).value, 4);
189
+ const card = await sendCommand(port, argv('ZCARD', 'zinf'));
190
+ assert.equal(tryParseValue(card, 0).value, 0);
191
+ });
192
+
162
193
  it('ZCARD non-existent returns 0, ZSCORE non-existent returns nil', async () => {
163
194
  const cardReply = await sendCommand(port, argv('ZCARD', 'nonexistent_z'));
164
195
  assert.equal(tryParseValue(cardReply, 0).value, 0);