Skip to content

Commit b54588b

Browse files
committed
feat: add Redis 8.2 deletion policies to XTRIM
command
1 parent 3aec14b commit b54588b

File tree

2 files changed

+100
-3
lines changed

2 files changed

+100
-3
lines changed

packages/client/lib/commands/XTRIM.spec.ts

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL } from '../test-utils';
33
import XTRIM from './XTRIM';
44
import { parseArgs } from './generic-transformers';
5+
import { STREAM_DELETION_POLICY } from './common-stream.types';
56

67
describe('XTRIM', () => {
78
describe('transformArguments', () => {
@@ -12,6 +13,13 @@ describe('XTRIM', () => {
1213
);
1314
});
1415

16+
it('simple - MINID', () => {
17+
assert.deepEqual(
18+
parseArgs(XTRIM, 'key', 'MINID', 123),
19+
['XTRIM', 'key', 'MINID', '123']
20+
);
21+
});
22+
1523
it('with strategyModifier', () => {
1624
assert.deepEqual(
1725
parseArgs(XTRIM, 'key', 'MAXLEN', 1, {
@@ -39,15 +47,96 @@ describe('XTRIM', () => {
3947
['XTRIM', 'key', 'MAXLEN', '=', '1', 'LIMIT', '1']
4048
);
4149
});
50+
51+
it('with policy', () => {
52+
assert.deepEqual(
53+
parseArgs(XTRIM, 'key', 'MAXLEN', 1, {
54+
policy: STREAM_DELETION_POLICY.DELREF
55+
}),
56+
['XTRIM', 'key', 'MAXLEN', '1', 'DELREF']
57+
);
58+
});
59+
60+
it('with all options', () => {
61+
assert.deepEqual(
62+
parseArgs(XTRIM, 'key', 'MAXLEN', 1, {
63+
strategyModifier: '~',
64+
LIMIT: 100,
65+
policy: STREAM_DELETION_POLICY.ACKED
66+
}),
67+
['XTRIM', 'key', 'MAXLEN', '~', '1', 'LIMIT', '100', 'ACKED']
68+
);
69+
});
70+
});
71+
72+
testUtils.testAll('xTrim with MAXLEN', async client => {
73+
assert.equal(
74+
typeof await client.xTrim('key', 'MAXLEN', 1),
75+
'number'
76+
);
77+
}, {
78+
client: GLOBAL.SERVERS.OPEN,
79+
cluster: GLOBAL.CLUSTERS.OPEN,
4280
});
4381

44-
testUtils.testAll('xTrim', async client => {
82+
testUtils.testAll('xTrim with MINID', async client => {
4583
assert.equal(
46-
await client.xTrim('key', 'MAXLEN', 1),
47-
0
84+
typeof await client.xTrim('key', 'MINID', 1),
85+
'number'
4886
);
4987
}, {
5088
client: GLOBAL.SERVERS.OPEN,
5189
cluster: GLOBAL.CLUSTERS.OPEN,
5290
});
91+
92+
testUtils.testAll(
93+
'xTrim with LIMIT',
94+
async (client) => {
95+
assert.equal(
96+
typeof await client.xTrim('{tag}key', 'MAXLEN', 1000, {
97+
strategyModifier: '~',
98+
LIMIT: 10
99+
}),
100+
'number'
101+
);
102+
},
103+
{
104+
client: GLOBAL.SERVERS.OPEN,
105+
cluster: GLOBAL.CLUSTERS.OPEN,
106+
}
107+
);
108+
109+
testUtils.testAll(
110+
'xTrim with policy',
111+
async (client) => {
112+
assert.equal(
113+
typeof await client.xTrim('{tag}key', 'MAXLEN', 0, {
114+
policy: STREAM_DELETION_POLICY.DELREF
115+
}),
116+
'number'
117+
);
118+
},
119+
{
120+
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 2] },
121+
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 2] },
122+
}
123+
);
124+
125+
testUtils.testAll(
126+
'xTrim with all options',
127+
async (client) => {
128+
assert.equal(
129+
typeof await client.xTrim('{tag}key', 'MINID', 0, {
130+
strategyModifier: '~',
131+
LIMIT: 10,
132+
policy: STREAM_DELETION_POLICY.KEEPREF
133+
}),
134+
'number'
135+
);
136+
},
137+
{
138+
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 2] },
139+
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 2] },
140+
}
141+
);
53142
});

packages/client/lib/commands/XTRIM.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { CommandParser } from '../client/parser';
22
import { NumberReply, Command, RedisArgument } from '../RESP/types';
3+
import { StreamDeletionPolicy } from './common-stream.types';
34

45
/**
56
* Options for the XTRIM command
67
*
78
* @property strategyModifier - Exact ('=') or approximate ('~') trimming
89
* @property LIMIT - Maximum number of entries to trim in one call (Redis 6.2+)
10+
* @property policy - Policy to apply when deleting entries (optional, defaults to KEEPREF)
911
*/
1012
export interface XTrimOptions {
1113
strategyModifier?: '=' | '~';
1214
/** added in 6.2 */
1315
LIMIT?: number;
16+
/** added in 8.2 */
17+
policy?: StreamDeletionPolicy;
1418
}
1519

1620
/**
@@ -49,6 +53,10 @@ export default {
4953
if (options?.LIMIT) {
5054
parser.push('LIMIT', options.LIMIT.toString());
5155
}
56+
57+
if (options?.policy) {
58+
parser.push(options.policy);
59+
}
5260
},
5361
transformReply: undefined as unknown as () => NumberReply
5462
} as const satisfies Command;

0 commit comments

Comments
 (0)