-
Notifications
You must be signed in to change notification settings - Fork 71
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: add SRANDMEMBER command #1938
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ import * as net from "net"; | |||||
import { Buffer, BufferWriter, Reader, Writer } from "protobufjs"; | ||||||
import { | ||||||
AggregationType, | ||||||
BulkString, | ||||||
ExpireOptions, | ||||||
InsertPosition, | ||||||
KeyWeight, | ||||||
|
@@ -80,7 +81,9 @@ import { | |||||
createSMembers, | ||||||
createSMove, | ||||||
createSPop, | ||||||
createSRandMember, | ||||||
createSRem, | ||||||
createSUnion, | ||||||
createSUnionStore, | ||||||
createSet, | ||||||
createStrlen, | ||||||
|
@@ -105,7 +108,6 @@ import { | |||||
createZRemRangeByRank, | ||||||
createZRemRangeByScore, | ||||||
createZScore, | ||||||
createSUnion, | ||||||
} from "./Commands"; | ||||||
import { | ||||||
ClosingError, | ||||||
|
@@ -1491,6 +1493,60 @@ export class BaseClient { | |||||
); | ||||||
} | ||||||
|
||||||
/** Returns a random element from the set value stored at `key`. | ||||||
* See https://valkey.io/commands/srandmember for more details. | ||||||
* | ||||||
* @param key - The key from which to retrieve the set member. | ||||||
* @returns a random element from the set, or null if `key` does not exist. | ||||||
* | ||||||
* @example | ||||||
* ```typescript | ||||||
* // Example usage of srandmember method to return a random member from a set | ||||||
* const result = await client.srandmember("my_set"); | ||||||
* console.log(result); // Output: 'member1' - A random member of "my_set". | ||||||
* ``` | ||||||
* | ||||||
* @example | ||||||
* ```typescript | ||||||
* // Example usage of srandmember method with non-existing key | ||||||
* const result = await client.srandmember("non_existing_set"); | ||||||
* console.log(result); // Output: null | ||||||
* ``` | ||||||
*/ | ||||||
public srandmember(key: BulkString): Promise<BulkString | null> { | ||||||
return this.createWritePromise(createSRandMember(key)); | ||||||
} | ||||||
|
||||||
/** Returns one or more random elements from the set value stored at `key`. | ||||||
* See https://valkey.io/commands/srandmember for more details. | ||||||
* | ||||||
* @param key - The key of the sorted set. | ||||||
* @param count - The number of members to return. | ||||||
* If `count` is positive, returns unique members. | ||||||
* If `count` is negative, allows for duplicates members. | ||||||
* @returns a list of members from the set. If the set does not exist or is empty, an empty list will be returned. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Same here |
||||||
* | ||||||
* @example | ||||||
* ```typescript | ||||||
* // Example usage of srandmemberCount method to return multiple random members from a set | ||||||
* const result = await client.srandmemberCount("my_set", -3); | ||||||
* console.log(result); // Output: ['member1', 'member1', 'member2'] - Random members of "my_set". | ||||||
* ``` | ||||||
* | ||||||
* @example | ||||||
* ```typescript | ||||||
* // Example usage of srandmemberCount method with non-existing key | ||||||
* const result = await client.srandmemberCount("non_existing_set", 3); | ||||||
* console.log(result); // Output: [] - An empty list since the key does not exist. | ||||||
* ``` | ||||||
*/ | ||||||
public async srandmemberCount( | ||||||
key: BulkString, | ||||||
count: number, | ||||||
): Promise<BulkString[]> { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to merge #1953 ahead of this |
||||||
return this.createWritePromise(createSRandMember(key, count)); | ||||||
} | ||||||
|
||||||
/** Returns the number of keys in `keys` that exist in the database. | ||||||
* See https://valkey.io/commands/exists/ for details. | ||||||
* | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,6 +4,7 @@ | |||||
|
||||||
import { | ||||||
AggregationType, | ||||||
BulkString, | ||||||
ExpireOptions, | ||||||
InfoOptions, | ||||||
InsertPosition, | ||||||
|
@@ -83,7 +84,9 @@ import { | |||||
createSMembers, | ||||||
createSMove, | ||||||
createSPop, | ||||||
createSRandMember, | ||||||
createSRem, | ||||||
createSUnion, | ||||||
createSUnionStore, | ||||||
createSelect, | ||||||
createSet, | ||||||
|
@@ -110,7 +113,6 @@ import { | |||||
createZRemRangeByRank, | ||||||
createZRemRangeByScore, | ||||||
createZScore, | ||||||
createSUnion, | ||||||
} from "./Commands"; | ||||||
import { command_request } from "./ProtobufMessage"; | ||||||
|
||||||
|
@@ -831,6 +833,29 @@ export class BaseTransaction<T extends BaseTransaction<T>> { | |||||
return this.addAndReturn(createSPop(key, count), true); | ||||||
} | ||||||
|
||||||
/** Returns a random element from the set value stored at `key`. | ||||||
* See https://valkey.io/commands/srandmember for more details. | ||||||
* | ||||||
* @param key - The key from which to retrieve the set member. | ||||||
* Command Response - a random element from the set, or null if `key` does not exist. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
public srandmember(key: BulkString): T { | ||||||
return this.addAndReturn(createSRandMember(key)); | ||||||
} | ||||||
|
||||||
/** Returns one or more random elements from the set value stored at `key`. | ||||||
* See https://valkey.io/commands/srandmember for more details. | ||||||
* | ||||||
* @param key - The key of the sorted set. | ||||||
* @param count - The number of members to return. | ||||||
* If `count` is positive, returns unique members. | ||||||
* If `count` is negative, allows for duplicates members. | ||||||
* Command Response - a list of members from the set. If the set does not exist or is empty, an empty list will be returned. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
*/ | ||||||
public srandmemberCount(key: BulkString, count: number): T { | ||||||
return this.addAndReturn(createSRandMember(key, count)); | ||||||
} | ||||||
|
||||||
/** Returns the number of keys in `keys` that exist in the database. | ||||||
* See https://valkey.io/commands/exists/ for details. | ||||||
* | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we capitalize? If not, please ignore