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

Node: implement MSETNX #2046

Merged
merged 10 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#### Changes
* Node: Added MSETNX command ([#2046](https://github.com/valkey-io/valkey-glide/pull/2046))
* Node: Exported client configuration types ([#2023](https://github.com/valkey-io/valkey-glide/pull/2023))
* Java, Python: Update docs for GEOSEARCH command ([#2017](https://github.com/valkey-io/valkey-glide/pull/2017))
* Node: Added FUNCTION LIST command ([#2019](https://github.com/valkey-io/valkey-glide/pull/2019))
Expand Down
24 changes: 24 additions & 0 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ import {
createLTrim,
createMGet,
createMSet,
createMSetNX,
createObjectEncoding,
createObjectFreq,
createObjectIdletime,
Expand Down Expand Up @@ -908,6 +909,29 @@ export class BaseClient {
return this.createWritePromise(createMSet(keyValueMap));
}

/**
* Sets multiple keys to values if the key does not exist. The operation is atomic, and if one or
* more keys already exist, the entire operation fails.
*
* See https://valkey.io/commands/msetnx/ for more details.
*
* @remarks When in cluster mode, all keys in `keyValueMap` must map to the same hash slot.
* @param keyValueMap - A key-value map consisting of keys and their respective values to set.
* @returns `True` if all keys were set. `False` if no key was set.
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
*
* @example
* ```typescript
* const result1 = await client.msetnx({"key1": "value1", "key2": "value2"});
* console.log(result1); // Output: `True`
*
* const result2 = await client.msetnx({"key2": "value4", "key3": "value5"});
* console.log(result2); // Output: `False`
* ```
*/
public async msetnx(keyValueMap: Record<string, string>): Promise<number> {
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
return this.createWritePromise(createMSetNX(keyValueMap));
}

/** Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/incr/ for details.
*
Expand Down
12 changes: 12 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,18 @@ export function createMSet(
return createCommand(RequestType.MSet, Object.entries(keyValueMap).flat());
}

/**
* @internal
*/
export function createMSetNX(
keyValueMap: Record<string, string>,
): command_request.Command {
return createCommand(
RequestType.MSetNX,
Object.entries(keyValueMap).flat(),
);
}

/**
* @internal
*/
Expand Down
15 changes: 15 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ import {
createLolwut,
createMGet,
createMSet,
createMSetNX,
createObjectEncoding,
createObjectFreq,
createObjectIdletime,
Expand Down Expand Up @@ -347,6 +348,20 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createMSet(keyValueMap));
}

/**
* Sets multiple keys to values if the key does not exist. The operation is atomic, and if one or
* more keys already exist, the entire operation fails.
*
* See https://valkey.io/commands/msetnx/ for more details.
*
* @remarks When in cluster mode, all keys in `keyValueMap` must map to the same hash slot.
cyip10 marked this conversation as resolved.
Show resolved Hide resolved
* @param keyValueMap - A key-value map consisting of keys and their respective values to set.
* @returns `True` if all keys were set. `False` if no key was set.
*/
Yury-Fridlyand marked this conversation as resolved.
Show resolved Hide resolved
public msetnx(keyValueMap: Record<string, string>): T {
return this.addAndReturn(createMSetNX(keyValueMap));
}

/** Increments the number stored at `key` by one. If `key` does not exist, it is set to 0 before performing the operation.
* See https://valkey.io/commands/incr/ for details.
*
Expand Down
33 changes: 33 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,39 @@ export function runBaseTests<Context>(config: {
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`msetnx test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
const key1 = "{key}-1" + uuidv4();
const key2 = "{key}-2" + uuidv4();
const key3 = "{key}-3" + uuidv4();
const nonExistingKey = uuidv4();
const value = uuidv4();
const keyValueMap1 = {
[key1]: value,
GumpacG marked this conversation as resolved.
Show resolved Hide resolved
[key2]: value,
};
const keyValueMap2 = {
[key2]: value,
[key3]: value,
};

expect(await client.msetnx(keyValueMap1)).toEqual(true);

checkSimple(
await client.mget([key1, key2, nonExistingKey]),
).toEqual([value, value, null]);

expect(await client.msetnx(keyValueMap2)).toEqual(false);

expect(await client.get(key3)).toEqual(null);
checkSimple(await client.get(key2)).toEqual(value);
cyip10 marked this conversation as resolved.
Show resolved Hide resolved
}, protocol);
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`incr, incrBy and incrByFloat with existing key_%p`,
async (protocol) => {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ export async function transactionTest(
responseData.push(['customCommand(["MGET", key1, key2])', ["bar", "baz"]]);
baseTransaction.mset({ [key3]: value });
responseData.push(["mset({ [key3]: value })", "OK"]);
baseTransaction.msetnx({ [key3]: value });
responseData.push(["msetnx({ [key3]: value })", false]);
baseTransaction.mget([key1, key2]);
responseData.push(["mget([key1, key2])", ["bar", "baz"]]);
baseTransaction.strlen(key1);
Expand Down
Loading