-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Vector sets #2998
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
Merged
+1,608
−34
Merged
Vector sets #2998
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
baff63b
wip
nkaradzhov d318e65
improve the vadd api
nkaradzhov 8d68df6
resp3 tests
nkaradzhov 27998a9
fix some tests
nkaradzhov 9c93f89
extract json helper functions in client package
nkaradzhov a3456f8
use transformJsonReply
nkaradzhov 4403881
remove the CACHEABLE flag for all vector set commands
nkaradzhov d1569aa
properly transform vinfo result
nkaradzhov e6046fc
add resp3 test for vlinks
nkaradzhov 89a0f8c
add more tests for vrandmember
nkaradzhov 5a49190
fix vrem return types
nkaradzhov 3f72cc6
fix vsetattr return type
nkaradzhov f6c0a56
fix vsim_withscores
nkaradzhov c6a1000
implement vlinks_withscores
nkaradzhov f3aea00
set minimum docker image version to 8
nkaradzhov 1afe3d7
align return types
nkaradzhov 5d31674
add RAW variant for VEMB -> VEMB_RAW
nkaradzhov d6c7860
use the new parseCommand api
nkaradzhov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import VADD from './VADD'; | ||
import { parseArgs } from './generic-transformers'; | ||
|
||
describe('VADD', () => { | ||
describe('transformArguments', () => { | ||
it('basic usage', () => { | ||
assert.deepEqual( | ||
parseArgs(VADD, 'key', [1.0, 2.0, 3.0], 'element'), | ||
['VADD', 'key', 'VALUES', '3', '1', '2', '3', 'element'] | ||
); | ||
}); | ||
|
||
it('with REDUCE option', () => { | ||
assert.deepEqual( | ||
parseArgs(VADD, 'key', [1.0, 2], 'element', { REDUCE: 50 }), | ||
['VADD', 'key', 'REDUCE', '50', 'VALUES', '2', '1', '2', 'element'] | ||
); | ||
}); | ||
|
||
it('with quantization options', () => { | ||
assert.deepEqual( | ||
parseArgs(VADD, 'key', [1.0, 2.0], 'element', { QUANT: 'Q8' }), | ||
['VADD', 'key', 'VALUES', '2', '1', '2', 'element', 'Q8'] | ||
); | ||
|
||
assert.deepEqual( | ||
parseArgs(VADD, 'key', [1.0, 2.0], 'element', { QUANT: 'BIN' }), | ||
['VADD', 'key', 'VALUES', '2', '1', '2', 'element', 'BIN'] | ||
); | ||
|
||
assert.deepEqual( | ||
parseArgs(VADD, 'key', [1.0, 2.0], 'element', { QUANT: 'NOQUANT' }), | ||
['VADD', 'key', 'VALUES', '2', '1', '2', 'element', 'NOQUANT'] | ||
); | ||
}); | ||
|
||
it('with all options', () => { | ||
assert.deepEqual( | ||
parseArgs(VADD, 'key', [1.0, 2.0], 'element', { | ||
REDUCE: 50, | ||
CAS: true, | ||
QUANT: 'Q8', | ||
EF: 200, | ||
SETATTR: { name: 'test', value: 42 }, | ||
M: 16 | ||
}), | ||
[ | ||
'VADD', 'key', 'REDUCE', '50', 'VALUES', '2', '1', '2', 'element', | ||
'CAS', 'Q8', 'EF', '200', 'SETATTR', '{"name":"test","value":42}', 'M', '16' | ||
] | ||
); | ||
}); | ||
}); | ||
|
||
testUtils.testAll('vAdd', async client => { | ||
assert.equal( | ||
await client.vAdd('key', [1.0, 2.0, 3.0], 'element'), | ||
true | ||
); | ||
|
||
// same element should not be added again | ||
assert.equal( | ||
await client.vAdd('key', [1, 2 , 3], 'element'), | ||
false | ||
); | ||
|
||
}, { | ||
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 0] }, | ||
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 0] }, | ||
}); | ||
|
||
testUtils.testWithClient('vAdd with RESP3', async client => { | ||
// Test basic functionality with RESP3 | ||
assert.equal( | ||
await client.vAdd('resp3-key', [1.5, 2.5, 3.5], 'resp3-element'), | ||
true | ||
); | ||
|
||
// same element should not be added again | ||
assert.equal( | ||
await client.vAdd('resp3-key', [1, 2 , 3], 'resp3-element'), | ||
false | ||
); | ||
|
||
// Test with options to ensure complex parameters work with RESP3 | ||
assert.equal( | ||
await client.vAdd('resp3-key', [4.0, 5.0, 6.0], 'resp3-element2', { | ||
QUANT: 'Q8', | ||
CAS: true, | ||
SETATTR: { type: 'test', value: 123 } | ||
}), | ||
true | ||
); | ||
|
||
// Verify the vector set was created correctly | ||
assert.equal( | ||
await client.vCard('resp3-key'), | ||
2 | ||
); | ||
}, { | ||
...GLOBAL.SERVERS.OPEN, | ||
clientOptions: { | ||
RESP: 3 | ||
}, | ||
minimumDockerVersion: [8, 0] | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { CommandParser } from '../client/parser'; | ||
import { RedisArgument, Command } from '../RESP/types'; | ||
import { transformBooleanReply, transformDoubleArgument } from './generic-transformers'; | ||
|
||
export interface VAddOptions { | ||
REDUCE?: number; | ||
CAS?: boolean; | ||
QUANT?: 'NOQUANT' | 'BIN' | 'Q8', | ||
EF?: number; | ||
SETATTR?: Record<string, any>; | ||
M?: number; | ||
} | ||
|
||
export default { | ||
/** | ||
* Add a new element into the vector set specified by key | ||
* | ||
* @param parser - The command parser | ||
* @param key - The name of the key that will hold the vector set data | ||
* @param vector - The vector data as array of numbers | ||
* @param element - The name of the element being added to the vector set | ||
* @param options - Optional parameters for vector addition | ||
* @see https://redis.io/commands/vadd/ | ||
*/ | ||
parseCommand( | ||
parser: CommandParser, | ||
key: RedisArgument, | ||
vector: Array<number>, | ||
element: RedisArgument, | ||
options?: VAddOptions | ||
) { | ||
parser.push('VADD'); | ||
parser.pushKey(key); | ||
|
||
if (options?.REDUCE !== undefined) { | ||
parser.push('REDUCE', options.REDUCE.toString()); | ||
} | ||
|
||
parser.push('VALUES', vector.length.toString()); | ||
for (const value of vector) { | ||
parser.push(transformDoubleArgument(value)); | ||
} | ||
|
||
parser.push(element); | ||
|
||
if (options?.CAS) { | ||
parser.push('CAS'); | ||
} | ||
|
||
options?.QUANT && parser.push(options.QUANT); | ||
|
||
if (options?.EF !== undefined) { | ||
parser.push('EF', options.EF.toString()); | ||
} | ||
|
||
if (options?.SETATTR) { | ||
parser.push('SETATTR', JSON.stringify(options.SETATTR)); | ||
} | ||
|
||
if (options?.M !== undefined) { | ||
parser.push('M', options.M.toString()); | ||
} | ||
}, | ||
transformReply: transformBooleanReply | ||
} as const satisfies Command; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import VCARD from './VCARD'; | ||
import { parseArgs } from './generic-transformers'; | ||
|
||
describe('VCARD', () => { | ||
it('transformArguments', () => { | ||
assert.deepEqual( | ||
parseArgs(VCARD, 'key'), | ||
['VCARD', 'key'] | ||
); | ||
}); | ||
|
||
testUtils.testAll('vCard', async client => { | ||
await client.vAdd('key', [1.0, 2.0, 3.0], 'element1'); | ||
await client.vAdd('key', [4.0, 5.0, 6.0], 'element2'); | ||
|
||
assert.equal( | ||
await client.vCard('key'), | ||
2 | ||
); | ||
|
||
assert.equal(await client.vCard('unknown'), 0); | ||
}, { | ||
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 0] }, | ||
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 0] } | ||
}); | ||
|
||
testUtils.testWithClient('vCard with RESP3', async client => { | ||
// Test empty vector set | ||
assert.equal( | ||
await client.vCard('resp3-empty-key'), | ||
0 | ||
); | ||
|
||
// Add elements and test cardinality | ||
await client.vAdd('resp3-key', [1.0, 2.0], 'elem1'); | ||
assert.equal( | ||
await client.vCard('resp3-key'), | ||
1 | ||
); | ||
|
||
await client.vAdd('resp3-key', [3.0, 4.0], 'elem2'); | ||
await client.vAdd('resp3-key', [5.0, 6.0], 'elem3'); | ||
assert.equal( | ||
await client.vCard('resp3-key'), | ||
3 | ||
); | ||
|
||
assert.equal(await client.vCard('unknown'), 0); | ||
}, { | ||
...GLOBAL.SERVERS.OPEN, | ||
clientOptions: { | ||
RESP: 3 | ||
}, | ||
minimumDockerVersion: [8, 0] | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { CommandParser } from '../client/parser'; | ||
import { RedisArgument, NumberReply, Command } from '../RESP/types'; | ||
|
||
export default { | ||
IS_READ_ONLY: true, | ||
/** | ||
* Retrieve the number of elements in a vector set | ||
* | ||
* @param parser - The command parser | ||
* @param key - The key of the vector set | ||
* @see https://redis.io/commands/vcard/ | ||
*/ | ||
parseCommand(parser: CommandParser, key: RedisArgument) { | ||
parser.push('VCARD'); | ||
parser.pushKey(key); | ||
}, | ||
transformReply: undefined as unknown as () => NumberReply | ||
} as const satisfies Command; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import VDIM from './VDIM'; | ||
import { parseArgs } from './generic-transformers'; | ||
|
||
describe('VDIM', () => { | ||
it('transformArguments', () => { | ||
assert.deepEqual( | ||
parseArgs(VDIM, 'key'), | ||
['VDIM', 'key'] | ||
); | ||
}); | ||
|
||
testUtils.testAll('vDim', async client => { | ||
await client.vAdd('key', [1.0, 2.0, 3.0], 'element'); | ||
|
||
assert.equal( | ||
await client.vDim('key'), | ||
3 | ||
); | ||
}, { | ||
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 0] }, | ||
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 0] } | ||
}); | ||
|
||
testUtils.testWithClient('vDim with RESP3', async client => { | ||
await client.vAdd('resp3-5d', [1.0, 2.0, 3.0, 4.0, 5.0], 'elem5d'); | ||
|
||
assert.equal( | ||
await client.vDim('resp3-5d'), | ||
5 | ||
); | ||
|
||
}, { | ||
...GLOBAL.SERVERS.OPEN, | ||
clientOptions: { | ||
RESP: 3 | ||
}, | ||
minimumDockerVersion: [8, 0] | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { CommandParser } from '../client/parser'; | ||
import { RedisArgument, NumberReply, Command } from '../RESP/types'; | ||
|
||
export default { | ||
IS_READ_ONLY: true, | ||
/** | ||
* Retrieve the dimension of the vectors in a vector set | ||
* | ||
* @param parser - The command parser | ||
* @param key - The key of the vector set | ||
* @see https://redis.io/commands/vdim/ | ||
*/ | ||
parseCommand(parser: CommandParser, key: RedisArgument) { | ||
parser.push('VDIM'); | ||
parser.pushKey(key); | ||
}, | ||
transformReply: undefined as unknown as () => NumberReply | ||
} as const satisfies Command; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { strict as assert } from 'node:assert'; | ||
import testUtils, { GLOBAL } from '../test-utils'; | ||
import VEMB from './VEMB'; | ||
import { parseArgs } from './generic-transformers'; | ||
|
||
describe('VEMB', () => { | ||
it('transformArguments', () => { | ||
assert.deepEqual( | ||
parseArgs(VEMB, 'key', 'element'), | ||
['VEMB', 'key', 'element'] | ||
); | ||
}); | ||
|
||
testUtils.testAll('vEmb', async client => { | ||
await client.vAdd('key', [1.0, 2.0, 3.0], 'element'); | ||
|
||
const result = await client.vEmb('key', 'element'); | ||
assert.ok(Array.isArray(result)); | ||
assert.equal(result.length, 3); | ||
assert.equal(typeof result[0], 'number'); | ||
}, { | ||
client: { ...GLOBAL.SERVERS.OPEN, minimumDockerVersion: [8, 0] }, | ||
cluster: { ...GLOBAL.CLUSTERS.OPEN, minimumDockerVersion: [8, 0] } | ||
}); | ||
|
||
testUtils.testWithClient('vEmb with RESP3', async client => { | ||
await client.vAdd('resp3-key', [1.5, 2.5, 3.5, 4.5], 'resp3-element'); | ||
|
||
const result = await client.vEmb('resp3-key', 'resp3-element'); | ||
assert.ok(Array.isArray(result)); | ||
assert.equal(result.length, 4); | ||
assert.equal(typeof result[0], 'number'); | ||
}, { | ||
...GLOBAL.SERVERS.OPEN, | ||
clientOptions: { | ||
RESP: 3 | ||
}, | ||
minimumDockerVersion: [8, 0] | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { CommandParser } from '../client/parser'; | ||
import { RedisArgument, Command } from '../RESP/types'; | ||
import { transformDoubleArrayReply } from './generic-transformers'; | ||
|
||
export default { | ||
IS_READ_ONLY: true, | ||
/** | ||
* Retrieve the approximate vector associated with a vector set element | ||
* | ||
* @param parser - The command parser | ||
* @param key - The key of the vector set | ||
* @param element - The name of the element to retrieve the vector for | ||
* @see https://redis.io/commands/vemb/ | ||
*/ | ||
parseCommand(parser: CommandParser, key: RedisArgument, element: RedisArgument) { | ||
parser.push('VEMB'); | ||
parser.pushKey(key); | ||
parser.push(element); | ||
}, | ||
transformReply: transformDoubleArrayReply | ||
} as const satisfies Command; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.