Skip to content

Commit

Permalink
Merge branch 'main' into node/integ_cyip10_expiretime_series
Browse files Browse the repository at this point in the history
  • Loading branch information
cyip10 authored Aug 2, 2024
2 parents eb0bea4 + 14ebf28 commit cd290c5
Show file tree
Hide file tree
Showing 11 changed files with 742 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint-rust/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ runs:

- run: |
cargo update
cargo install cargo-deny
cargo install --locked --version 0.15.1 cargo-deny
cargo deny check --config ${GITHUB_WORKSPACE}/deny.toml
working-directory: ${{ inputs.cargo-toml-folder }}
shell: bash
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@
* Node: Added ZINCRBY command ([#2009](https://github.com/valkey-io/valkey-glide/pull/2009))
* Node: Added BZMPOP command ([#2018](https://github.com/valkey-io/valkey-glide/pull/2018))
* Node: Added PFMERGE command ([#2053](https://github.com/valkey-io/valkey-glide/pull/2053))
* Node: Added WATCH and UNWATCH commands ([#2076](https://github.com/valkey-io/valkey-glide/pull/2076))
* Node: Added ZLEXCOUNT command ([#2022](https://github.com/valkey-io/valkey-glide/pull/2022))
* Node: Added ZREMRANGEBYLEX command ([#2025]((https://github.com/valkey-io/valkey-glide/pull/2025))
* Node: Added ZSCAN command ([#2061](https://github.com/valkey-io/valkey-glide/pull/2061))
* Node: Added SETRANGE command ([#2066](https://github.com/valkey-io/valkey-glide/pull/2066))
* Node: Added XDEL command ([#2064]((https://github.com/valkey-io/valkey-glide/pull/2064))
* Node: Added LMPOP & BLMPOP command ([#2050](https://github.com/valkey-io/valkey-glide/pull/2050))

#### Breaking Changes
* Node: (Refactor) Convert classes to types ([#2005](https://github.com/valkey-io/valkey-glide/pull/2005))
Expand Down
124 changes: 122 additions & 2 deletions node/src/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
createBitField,
createBitOp,
createBitPos,
createBLMPop,
createDecr,
createDecrBy,
createDel,
Expand Down Expand Up @@ -92,6 +93,7 @@ import {
createLInsert,
createLLen,
createLMove,
createLMPop,
createLPop,
createLPos,
createLPush,
Expand Down Expand Up @@ -143,7 +145,9 @@ import {
createTouch,
createType,
createUnlink,
createWatch,
createXAdd,
createXDel,
createXLen,
createXRead,
createXTrim,
Expand Down Expand Up @@ -171,8 +175,8 @@ import {
createZRemRangeByScore,
createZRevRank,
createZRevRankWithScore,
createZScore,
createZScan,
createZScore,
} from "./Commands";
import {
ClosingError,
Expand Down Expand Up @@ -3548,6 +3552,26 @@ export class BaseClient {
return this.createWritePromise(createXAdd(key, values, options));
}

/**
* Removes the specified entries by id from a stream, and returns the number of entries deleted.
*
* See https://valkey.io/commands/xdel for more details.
*
* @param key - The key of the stream.
* @param ids - An array of entry ids.
* @returns The number of entries removed from the stream. This number may be less than the number of entries in
* `ids`, if the specified `ids` don't exist in the stream.
*
* @example
* ```typescript
* console.log(await client.xdel("key", ["1538561698944-0", "1538561698944-1"]));
* // Output is 2 since the stream marked 2 entries as deleted.
* ```
*/
public xdel(key: string, ids: string[]): Promise<number> {
return this.createWritePromise(createXDel(key, ids));
}

/**
* Trims the stream stored at `key` by evicting older entries.
* See https://valkey.io/commands/xtrim/ for more details.
Expand Down Expand Up @@ -4520,10 +4544,42 @@ export class BaseClient {
* console.log(result); // Output: 2 - The last access time of 2 keys has been updated.
* ```
*/
public touch(keys: string[]): Promise<number> {
public async touch(keys: string[]): Promise<number> {
return this.createWritePromise(createTouch(keys));
}

/**
* Marks the given keys to be watched for conditional execution of a transaction. Transactions
* will only execute commands if the watched keys are not modified before execution of the
* transaction. Executing a transaction will automatically flush all previously watched keys.
*
* See https://valkey.io/commands/watch/ and https://valkey.io/topics/transactions/#cas for more details.
*
* @remarks When in cluster mode, the command may route to multiple nodes when `keys` map to different hash slots.
* @param keys - The keys to watch.
* @returns A simple "OK" response.
*
* @example
* ```typescript
* const response = await client.watch(["sampleKey"]);
* console.log(response); // Output: "OK"
* const transaction = new Transaction().set("SampleKey", "foobar");
* const result = await client.exec(transaction);
* console.log(result); // Output: "OK" - Executes successfully and keys are unwatched.
* ```
* ```typescript
* const response = await client.watch(["sampleKey"]);
* console.log(response); // Output: "OK"
* const transaction = new Transaction().set("SampleKey", "foobar");
* await client.set("sampleKey", "hello world");
* const result = await client.exec(transaction);
* console.log(result); // Output: null - null is returned when the watched key is modified before transaction execution.
* ```
*/
public async watch(keys: string[]): Promise<"OK"> {
return this.createWritePromise(createWatch(keys));
}

/**
* Overwrites part of the string stored at `key`, starting at the specified `offset`,
* for the entire length of `value`. If the `offset` is larger than the current length of the string at `key`,
Expand Down Expand Up @@ -4552,6 +4608,70 @@ export class BaseClient {
return this.createWritePromise(createSetRange(key, offset, value));
}

/**
* Pops one or more elements from the first non-empty list from the provided `keys`.
*
* See https://valkey.io/commands/lmpop/ for more details.
*
* @remarks When in cluster mode, all `key`s must map to the same hash slot.
* @param keys - An array of keys to lists.
* @param direction - The direction based on which elements are popped from - see {@link ListDirection}.
* @param count - (Optional) The maximum number of popped elements.
* @returns A `Record` of key-name mapped array of popped elements.
*
* since Valkey version 7.0.0.
*
* @example
* ```typescript
* await client.lpush("testKey", ["one", "two", "three"]);
* await client.lpush("testKey2", ["five", "six", "seven"]);
* const result = await client.lmpop(["testKey", "testKey2"], ListDirection.LEFT, 1L);
* console.log(result.get("testKey")); // Output: { "testKey": ["three"] }
* ```
*/
public async lmpop(
keys: string[],
direction: ListDirection,
count?: number,
): Promise<Record<string, string[]>> {
return this.createWritePromise(createLMPop(keys, direction, count));
}

/**
* Blocks the connection until it pops one or more elements from the first non-empty list from the
* provided `key`. `BLMPOP` is the blocking variant of {@link lmpop}.
*
* See https://valkey.io/commands/blmpop/ for more details.
*
* @remarks When in cluster mode, all `key`s must map to the same hash slot.
* @param keys - An array of keys to lists.
* @param direction - The direction based on which elements are popped from - see {@link ListDirection}.
* @param timeout - The number of seconds to wait for a blocking operation to complete. A value of `0` will block indefinitely.
* @param count - (Optional) The maximum number of popped elements.
* @returns - A `Record` of `key` name mapped array of popped elements.
* If no member could be popped and the timeout expired, returns `null`.
*
* since Valkey version 7.0.0.
*
* @example
* ```typescript
* await client.lpush("testKey", ["one", "two", "three"]);
* await client.lpush("testKey2", ["five", "six", "seven"]);
* const result = await client.blmpop(["testKey", "testKey2"], ListDirection.LEFT, 0.1, 1L);
* console.log(result.get("testKey")); // Output: { "testKey": ["three"] }
* ```
*/
public async blmpop(
keys: string[],
direction: ListDirection,
timeout: number,
count?: number,
): Promise<Record<string, string[]>> {
return this.createWritePromise(
createBLMPop(timeout, keys, direction, count),
);
}

/**
* @internal
*/
Expand Down
65 changes: 65 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,9 @@ function addTrimOptions(options: StreamTrimOptions, args: string[]) {
}
}

/**
* @internal
*/
export function createXAdd(
key: string,
values: [string, string][],
Expand Down Expand Up @@ -1976,6 +1979,16 @@ export function createXAdd(
return createCommand(RequestType.XAdd, args);
}

/**
* @internal
*/
export function createXDel(
key: string,
ids: string[],
): command_request.Command {
return createCommand(RequestType.XDel, [key, ...ids]);
}

/**
* @internal
*/
Expand Down Expand Up @@ -3014,6 +3027,16 @@ export function createRandomKey(): command_request.Command {
return createCommand(RequestType.RandomKey, []);
}

/** @internal */
export function createWatch(keys: string[]): command_request.Command {
return createCommand(RequestType.Watch, keys);
}

/** @internal */
export function createUnWatch(): command_request.Command {
return createCommand(RequestType.UnWatch, []);
}

/**
* This base class represents the common set of optional arguments for the SCAN family of commands.
* Concrete implementations of this class are tied to specific SCAN commands (SCAN, HSCAN, SSCAN,
Expand Down Expand Up @@ -3067,3 +3090,45 @@ export function createSetRange(
): command_request.Command {
return createCommand(RequestType.SetRange, [key, offset.toString(), value]);
}

/**
* @internal
*/
export function createLMPop(
keys: string[],
direction: ListDirection,
count?: number,
): command_request.Command {
const args: string[] = [keys.length.toString(), ...keys, direction];

if (count !== undefined) {
args.push("COUNT");
args.push(count.toString());
}

return createCommand(RequestType.LMPop, args);
}

/**
* @internal
*/
export function createBLMPop(
timeout: number,
keys: string[],
direction: ListDirection,
count?: number,
): command_request.Command {
const args: string[] = [
timeout.toString(),
keys.length.toString(),
...keys,
direction,
];

if (count !== undefined) {
args.push("COUNT");
args.push(count.toString());
}

return createCommand(RequestType.BLMPop, args);
}
23 changes: 22 additions & 1 deletion node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
createSort,
createSortReadOnly,
createTime,
createUnWatch,
} from "./Commands";
import { connection_request } from "./ProtobufMessage";
import { Transaction } from "./Transaction";
Expand Down Expand Up @@ -742,7 +743,27 @@ export class GlideClient extends BaseClient {
* console.log(result); // Output: "key12" - "key12" is a random existing key name from the currently selected database.
* ```
*/
public randomKey(): Promise<string | null> {
public async randomKey(): Promise<string | null> {
return this.createWritePromise(createRandomKey());
}

/**
* Flushes all the previously watched keys for a transaction. Executing a transaction will
* automatically flush all previously watched keys.
*
* See https://valkey.io/commands/unwatch/ and https://valkey.io/topics/transactions/#cas for more details.
*
* @returns A simple "OK" response.
*
* @example
* ```typescript
* let response = await client.watch(["sampleKey"]);
* console.log(response); // Output: "OK"
* response = await client.unwatch();
* console.log(response); // Output: "OK"
* ```
*/
public async unwatch(): Promise<"OK"> {
return this.createWritePromise(createUnWatch());
}
}
25 changes: 24 additions & 1 deletion node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
createSort,
createSortReadOnly,
createTime,
createUnWatch,
} from "./Commands";
import { RequestError } from "./Errors";
import { command_request, connection_request } from "./ProtobufMessage";
Expand Down Expand Up @@ -1117,10 +1118,32 @@ export class GlideClusterClient extends BaseClient {
* console.log(result); // Output: "key12" - "key12" is a random existing key name.
* ```
*/
public randomKey(route?: Routes): Promise<string | null> {
public async randomKey(route?: Routes): Promise<string | null> {
return this.createWritePromise(
createRandomKey(),
toProtobufRoute(route),
);
}

/**
* Flushes all the previously watched keys for a transaction. Executing a transaction will
* automatically flush all previously watched keys.
*
* See https://valkey.io/commands/unwatch/ and https://valkey.io/topics/transactions/#cas for more details.
*
* @param route - (Optional) The command will be routed to all primary nodes, unless `route` is provided,
* in which case the client will route the command to the nodes defined by `route`.
* @returns A simple "OK" response.
*
* @example
* ```typescript
* let response = await client.watch(["sampleKey"]);
* console.log(response); // Output: "OK"
* response = await client.unwatch();
* console.log(response); // Output: "OK"
* ```
*/
public async unwatch(route?: Routes): Promise<"OK"> {
return this.createWritePromise(createUnWatch(), toProtobufRoute(route));
}
}
Loading

0 comments on commit cd290c5

Please sign in to comment.