-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* `XADD` Signed-off-by: Yury-Fridlyand <yury.fridlyand@improving.com>
- Loading branch information
1 parent
994455a
commit 2d6ae0b
Showing
6 changed files
with
277 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,120 @@ | ||
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 | ||
|
||
package options | ||
|
||
import ( | ||
"github.com/valkey-io/valkey-glide/go/glide/utils" | ||
) | ||
|
||
type triStateBool int | ||
|
||
// Tri-state bool for use option builders. We cannot rely on the default value of an non-initialized variable. | ||
const ( | ||
triStateBoolUndefined triStateBool = iota | ||
triStateBoolTrue | ||
triStateBoolFalse | ||
) | ||
|
||
// Optional arguments to `XAdd` in [StreamCommands] | ||
type XAddOptions struct { | ||
id string | ||
makeStream triStateBool | ||
trimOptions *XTrimOptions | ||
} | ||
|
||
// Create new empty `XAddOptions` | ||
func NewXAddOptions() *XAddOptions { | ||
return &XAddOptions{} | ||
} | ||
|
||
// New entry will be added with this `id“. | ||
func (xao *XAddOptions) SetId(id string) *XAddOptions { | ||
xao.id = id | ||
return xao | ||
} | ||
|
||
// If set, a new stream won't be created if no stream matches the given key. | ||
func (xao *XAddOptions) SetDontMakeNewStream() *XAddOptions { | ||
xao.makeStream = triStateBoolFalse | ||
return xao | ||
} | ||
|
||
// If set, add operation will also trim the older entries in the stream. | ||
func (xao *XAddOptions) SetTrimOptions(options *XTrimOptions) *XAddOptions { | ||
xao.trimOptions = options | ||
return xao | ||
} | ||
|
||
func (xao *XAddOptions) ToArgs() ([]string, error) { | ||
args := []string{} | ||
var err error | ||
if xao.makeStream == triStateBoolFalse { | ||
args = append(args, "NOMKSTREAM") | ||
} | ||
if xao.trimOptions != nil { | ||
moreArgs, err := xao.trimOptions.ToArgs() | ||
if err != nil { | ||
return args, err | ||
} | ||
args = append(args, moreArgs...) | ||
} | ||
if xao.id != "" { | ||
args = append(args, xao.id) | ||
} else { | ||
args = append(args, "*") | ||
} | ||
return args, err | ||
} | ||
|
||
// Optional arguments for `XTrim` and `XAdd` in [StreamCommands] | ||
type XTrimOptions struct { | ||
exact triStateBool | ||
limit int64 | ||
method string | ||
threshold string | ||
} | ||
|
||
// Option to trim the stream according to minimum ID. | ||
func NewXTrimOptionsWithMinId(threshold string) *XTrimOptions { | ||
return &XTrimOptions{threshold: threshold, method: "MINID"} | ||
} | ||
|
||
// Option to trim the stream according to maximum stream length. | ||
func NewXTrimOptionsWithMaxLen(threshold int64) *XTrimOptions { | ||
return &XTrimOptions{threshold: utils.IntToString(threshold), method: "MAXLEN"} | ||
} | ||
|
||
// Match exactly on the threshold. | ||
func (xto *XTrimOptions) SetExactTrimming() *XTrimOptions { | ||
xto.exact = triStateBoolTrue | ||
return xto | ||
} | ||
|
||
// Trim in a near-exact manner, which is more efficient. | ||
func (xto *XTrimOptions) SetNearlyExactTrimming() *XTrimOptions { | ||
xto.exact = triStateBoolFalse | ||
return xto | ||
} | ||
|
||
// Max number of stream entries to be trimmed for non-exact match. | ||
func (xto *XTrimOptions) SetNearlyExactTrimmingAndLimit(limit int64) *XTrimOptions { | ||
xto.exact = triStateBoolFalse | ||
xto.limit = limit | ||
return xto | ||
} | ||
|
||
func (xto *XTrimOptions) ToArgs() ([]string, error) { | ||
args := []string{} | ||
args = append(args, xto.method) | ||
if xto.exact == triStateBoolTrue { | ||
args = append(args, "=") | ||
} else if xto.exact == triStateBoolFalse { | ||
args = append(args, "~") | ||
} | ||
args = append(args, xto.threshold) | ||
if xto.limit > 0 { | ||
args = append(args, "LIMIT", utils.IntToString(xto.limit)) | ||
} | ||
var err error | ||
return args, err | ||
} |
This file contains 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,52 @@ | ||
// Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0 | ||
|
||
package api | ||
|
||
import "github.com/valkey-io/valkey-glide/go/glide/api/options" | ||
|
||
// Supports commands and transactions for the "Stream" group of commands for standalone and cluster clients. | ||
// | ||
// See [valkey.io] for details. | ||
// | ||
// [valkey.io]: https://valkey.io/commands/#stream | ||
type StreamCommands interface { | ||
// Adds an entry to the specified stream stored at `key`. If the `key` doesn't exist, the stream is created. | ||
// | ||
// See [valkey.io] for details. | ||
// | ||
// Parameters: | ||
// key - The key of the stream. | ||
// values - Field-value pairs to be added to the entry. | ||
// | ||
// Return value: | ||
// The id of the added entry. | ||
// | ||
// For example: | ||
// result, err := client.XAdd("myStream", [][]string{{"field1", "value1"}, {"field2", "value2"}}) | ||
// result.IsNil(): false | ||
// result.Value(): "1526919030474-55" | ||
// | ||
// [valkey.io]: https://valkey.io/commands/xadd/ | ||
XAdd(key string, values [][]string) (Result[string], error) | ||
|
||
// Adds an entry to the specified stream stored at `key`. If the `key` doesn't exist, the stream is created. | ||
// | ||
// See [valkey.io] for details. | ||
// | ||
// Parameters: | ||
// key - The key of the stream. | ||
// values - Field-value pairs to be added to the entry. | ||
// options - Stream add options. | ||
// | ||
// Return value: | ||
// The id of the added entry. | ||
// | ||
// For example: | ||
// options := options.NewXAddOptions().SetId("100-500").SetDontMakeNewStream() | ||
// result, err := client.XAddWithOptions("myStream", [][]string{{"field1", "value1"}, {"field2", "value2"}}, options) | ||
// result.IsNil(): false | ||
// result.Value(): "100-500" | ||
// | ||
// [valkey.io]: https://valkey.io/commands/xadd/ | ||
XAddWithOptions(key string, values [][]string, options *options.XAddOptions) (Result[string], error) | ||
} |
This file contains 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
This file contains 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