Skip to content

Commit 4f15d0f

Browse files
authored
feat(json.set): add fpha option to json.set (#3235)
* feat(json.set): add fpha option to json.set fpha - floating point homogenous array hint for how Redis should store arrays of fp values * chore: add `available since` in the jsdocs
1 parent 2fdf89e commit 4f15d0f

2 files changed

Lines changed: 72 additions & 1 deletion

File tree

packages/json/lib/commands/SET.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ describe('JSON.SET', () => {
1212
);
1313
});
1414

15+
it('condition NX', () => {
16+
assert.deepEqual(
17+
parseArgs(SET, 'key', '$', 'json', { condition: 'NX' }),
18+
['JSON.SET', 'key', '$', '"json"', 'NX']
19+
);
20+
});
21+
22+
it('condition XX', () => {
23+
assert.deepEqual(
24+
parseArgs(SET, 'key', '$', 'json', { condition: 'XX' }),
25+
['JSON.SET', 'key', '$', '"json"', 'XX']
26+
);
27+
});
28+
1529
it('NX', () => {
1630
assert.deepEqual(
1731
parseArgs(SET, 'key', '$', 'json', { NX: true }),
@@ -25,6 +39,22 @@ describe('JSON.SET', () => {
2539
['JSON.SET', 'key', '$', '"json"', 'XX']
2640
);
2741
});
42+
43+
for (const fpha of ['BF16', 'FP16', 'FP32', 'FP64'] as const) {
44+
it(`FPHA ${fpha}`, () => {
45+
assert.deepEqual(
46+
parseArgs(SET, 'key', '$', 'json', { fpha }),
47+
['JSON.SET', 'key', '$', '"json"', 'FPHA', fpha]
48+
);
49+
});
50+
}
51+
52+
it('condition with FPHA', () => {
53+
assert.deepEqual(
54+
parseArgs(SET, 'key', '$', 'json', { condition: 'NX', fpha: 'FP32' }),
55+
['JSON.SET', 'key', '$', '"json"', 'NX', 'FPHA', 'FP32']
56+
);
57+
});
2858
});
2959

3060
testUtils.testWithClient('client.json.set', async client => {
@@ -33,4 +63,37 @@ describe('JSON.SET', () => {
3363
'OK'
3464
);
3565
}, GLOBAL.SERVERS.OPEN);
66+
67+
testUtils.testWithClientIfVersionWithinRange([[8, 8], 'LATEST'], 'client.json.set with FPHA', async client => {
68+
const fpArray = [1.1, 2.2, 3.3, 4.4];
69+
70+
for (const fpha of ['FP32', 'FP64', 'FP16', 'BF16'] as const) {
71+
const key = `fpha:${fpha}`;
72+
assert.equal(
73+
await client.json.set(key, '$', fpArray, { fpha }),
74+
'OK'
75+
);
76+
77+
const result = await client.json.get(key);
78+
assert.equal(Array.isArray(result), true);
79+
assert.equal((result as Array<unknown>).length, fpArray.length);
80+
}
81+
}, GLOBAL.SERVERS.OPEN);
82+
83+
testUtils.testWithClientIfVersionWithinRange([[8, 8], 'LATEST'], 'client.json.set with FPHA and conditions', async client => {
84+
const fpArray = [1.5, 2.5, 3.5];
85+
86+
assert.equal(
87+
await client.json.set('fpha:nx', '$', fpArray, { condition: 'NX', fpha: 'FP32' }),
88+
'OK'
89+
);
90+
assert.equal(
91+
await client.json.set('fpha:nx', '$', [4.5, 5.5], { condition: 'NX', fpha: 'FP32' }),
92+
null
93+
);
94+
assert.equal(
95+
await client.json.set('fpha:nx', '$', [4.5, 5.5], { condition: 'XX', fpha: 'FP32' }),
96+
'OK'
97+
);
98+
}, GLOBAL.SERVERS.OPEN);
3699
});

packages/json/lib/commands/SET.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ export interface JsonSetOptions {
1212
* @deprecated Use `{ condition: 'XX' }` instead.
1313
*/
1414
XX?: boolean;
15+
/**
16+
* If set, forces Redis to use the specified floating-point type for storing all FP homogeneous arrays.
17+
* available since 8.8
18+
*/
19+
fpha?: 'BF16' | 'FP16' | 'FP32' | 'FP64';
1520
}
1621

1722
export default {
1823
IS_READ_ONLY: false,
1924
/**
2025
* Sets a JSON value at a specific path in a JSON document.
2126
* Returns OK on success, or null if condition (NX/XX) is not met.
22-
*
27+
*
2328
* @param parser - The Redis command parser
2429
* @param key - The key containing the JSON document
2530
* @param path - Path in the document to set
@@ -47,6 +52,9 @@ export default {
4752
} else if (options?.XX) {
4853
parser.push('XX');
4954
}
55+
if (options?.fpha !== undefined) {
56+
parser.push('FPHA', options.fpha);
57+
}
5058
},
5159
transformReply: undefined as unknown as () => SimpleStringReply<'OK'> | NullReply
5260
} as const satisfies Command;

0 commit comments

Comments
 (0)