Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle GT and LT options in ZADD. #1278

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/commands/zadd.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function zadd(key, ...vals) {
// consume options
const options = []
while (['NX', 'XX', 'CH', 'INCR'].includes(vals[0])) {
while (['NX', 'XX', 'GT', 'LT', 'CH', 'INCR'].includes(vals[0])) {
options.push(vals.shift())
}

Expand All @@ -12,12 +12,16 @@ export function zadd(key, ...vals) {
// set option vals
const nx = options.includes('NX')
const xx = options.includes('XX')
const gt = options.includes('GT')
const lt = options.includes('LT')
const ch = options.includes('CH')
const incr = options.includes('INCR')

// validate options
if (nx && xx)
throw new Error('XX and NX options at the same time are not compatible')
if (gt && lt)
throw new Error('GT and LT options at the same time are not compatible')
if (incr && elems > 2)
throw new Error('INCR option supports a single increment-element pair')

Expand All @@ -36,8 +40,13 @@ export function zadd(key, ...vals) {

if (map.has(value)) {
if (!nx) {
const exist = map.get(value)
if (incr) {
score += Number(map.get(value).score)
score += Number(exist.score)
}
// eslint-ignore-next-line
if (lt && score >= exist.score || gt && score <= exist.score) {
continue
stipsan marked this conversation as resolved.
Show resolved Hide resolved
stipsan marked this conversation as resolved.
Show resolved Hide resolved
}
map.set(value, { score, value })
updated++
Expand Down
41 changes: 41 additions & 0 deletions test/integration/commands/zadd.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ runTwinSuite('zadd', command => {
redis[command]('key', ['NX', 'XX'], 1, 'value')
).rejects.toThrow('not compatible')
})
it('should not allow GT and LT options in the same call', async () => {
await expect(
redis[command]('key', ['GT', 'LT'], 1, 'value')
).rejects.toThrow('not compatible')
})
it('should not update a value that exists with NX option', async () => {
await redis[command]('key', 'NX', 1, 'value')
expect(await redis[command]('key', 'NX', 2, 'value')).toBe(0)
Expand All @@ -46,6 +51,42 @@ runTwinSuite('zadd', command => {
await redis[command]('key', 1, 'value')
expect(await redis[command]('key', ['XX', 'CH'], 2, 'value')).toBe(1)
})
it('should not update a value with equal or lower score with LT option', async () => {
await redis[command]('key', 'LT', 2, 'value')
expect(await redis[command]('key', ['LT', 'CH'], 2, 'value')).toBe(0)
expect(await redis[command]('key', ['LT', 'CH'], 3, 'value')).toBe(0)
expect(await redis[command]('key', ['LT', 'CH'], 1, 'value')).toBe(1)
})
it('should not update a value with equal or higher score with GT option', async () => {
await redis[command]('key', 'GT', 2, 'value')
expect(await redis[command]('key', ['GT', 'CH'], 2, 'value')).toBe(0)
expect(await redis[command]('key', ['GT', 'CH'], 1, 'value')).toBe(0)
expect(await redis[command]('key', ['GT', 'CH'], 3, 'value')).toBe(1)
})
it('should handle INCR and LT option', async () => {
await redis[command]('key', 2, 'value')
// LT + INCR w/ non-negative should never work
expect(await redis[command]('key', ['LT', 'CH', 'INCR'], 0, 'value')).toBe(0)
expect(await redis[command]('key', ['LT', 'CH', 'INCR'], -2, 'value')).toBe(1)
expect(await redis.zrange('key', 0, -1, 'WITHSCORES')).toEqual([
'value',
'0',
])
})
it('should handle INCR and GT option', async () => {
await redis[command]('key', 2, 'value')
expect(await redis[command]('key', ['GT', 'CH', 'INCR'], 2, 'value')).toBe(1)
expect(await redis.zrange('key', 0, -1, 'WITHSCORES')).toEqual([
'value',
'4',
])
// should work even if specified score is lower
expect(await redis[command]('key', ['GT', 'CH', 'INCR'], 1, 'value')).toBe(1)
expect(await redis.zrange('key', 0, -1, 'WITHSCORES')).toEqual([
'value',
'4',
])
})
it('should handle INCR option', async () => {
await redis[command]('key', 1, 'value')
await redis[command]('key', 'INCR', 2, 'value')
Expand Down