Skip to content

Commit

Permalink
enable multi deletions with Sorted.Delete.Index
Browse files Browse the repository at this point in the history
  • Loading branch information
xh3b4sd committed Sep 27, 2023
1 parent ecdd329 commit 2c2833c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
28 changes: 21 additions & 7 deletions pkg/conformance/client_single_sorted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,42 @@ func Test_Client_Single_Sorted_Create_Order(t *testing.T) {
}

{
err := cli.Sorted().Create().Index("ssk", "foo", 0.8, "a", "b")
err = cli.Sorted().Create().Index("ssk", "foo", 0.8, "a", "b")
if err != nil {
t.Fatal(err)
}
}

{
err := cli.Sorted().Create().Index("ssk", "bar", 0.7, "c", "d")
err = cli.Sorted().Create().Index("ssk", "bar", 0.7, "c", "d")
if err != nil {
t.Fatal(err)
}
}

{
err := cli.Sorted().Create().Index("ssk", "baz", 0.6, "c", "d")
err = cli.Sorted().Create().Index("ssk", "baz", 0.6, "a")
if !sorted.IsAlreadyExistsError(err) {
t.Fatal("expected", "alreadyExistsError", "got", err)
}
err = cli.Sorted().Create().Index("ssk", "baz", 0.6, "b", "z")
if !sorted.IsAlreadyExistsError(err) {
t.Fatal("expected", "alreadyExistsError", "got", err)
}
err = cli.Sorted().Create().Index("ssk", "baz", 0.6, "a", "b")
if !sorted.IsAlreadyExistsError(err) {
t.Fatal("expected", "alreadyExistsError", "got", err)
}
err = cli.Sorted().Create().Index("ssk", "baz", 0.6, "c", "d")
if !sorted.IsAlreadyExistsError(err) {
t.Fatal("expected", "alreadyExistsError", "got", err)
}
err = cli.Sorted().Create().Index("ssk", "baz", 0.7, "z")
if !sorted.IsAlreadyExistsError(err) {
t.Fatal("expected", "alreadyExistsError", "got", err)
}
}

// Ensure deleting multiple values with index mappings can be deleted at once.
{
err := cli.Sorted().Delete().Index("ssk", "bar")
err := cli.Sorted().Delete().Index("ssk", "foo", "bar")
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/fake/sorted_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (d *SortedDelete) Clean(key string) error {
return nil
}

func (d *SortedDelete) Index(key string, val string) error {
func (d *SortedDelete) Index(key string, val ...string) error {
if d.FakeIndex != nil {
return d.FakeIndex()
}
Expand Down
18 changes: 10 additions & 8 deletions pkg/sorted/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const deleteCleanScript = `
return 0
`

// TODO conformance tests for multiple deletions in one call
const deleteIndexScript = `
for i = 1, #ARGV do
local sco = redis.call("ZSCORE", KEYS[1], ARGV[i])
Expand Down Expand Up @@ -53,8 +52,8 @@ func (d *delete) Clean(key string) error {

var arg []interface{}
{
arg = append(arg, prefix.WithKeys(d.prefix, key))
arg = append(arg, prefix.WithKeys(d.prefix, index.New(key)))
arg = append(arg, prefix.WithKeys(d.prefix, key)) // KEYS[1]
arg = append(arg, prefix.WithKeys(d.prefix, index.New(key))) // KEYS[2]
}

_, err := redis.Int(d.deleteCleanScript.Do(con, arg...))
Expand All @@ -65,15 +64,18 @@ func (d *delete) Clean(key string) error {
return nil
}

func (d *delete) Index(key string, val string) error {
func (d *delete) Index(key string, val ...string) error {
con := d.pool.Get()
defer con.Close()

var arg []interface{}
{
arg = append(arg, prefix.WithKeys(d.prefix, key)) // KEYS[1]
arg = append(arg, prefix.WithKeys(d.prefix, index.New(key))) // KEYS[2]
arg = append(arg, val) // ARGV[1]

for _, x := range val {
arg = append(arg, x)
}
}

_, err := redis.Int(d.deleteIndexScript.Do(con, arg...))
Expand Down Expand Up @@ -116,9 +118,9 @@ func (d *delete) Score(key string, sco float64) error {

var arg []interface{}
{
arg = append(arg, prefix.WithKeys(d.prefix, key))
arg = append(arg, prefix.WithKeys(d.prefix, index.New(key)))
arg = append(arg, sco)
arg = append(arg, prefix.WithKeys(d.prefix, key)) // KEYS[1]
arg = append(arg, prefix.WithKeys(d.prefix, index.New(key))) // KEYS[2]
arg = append(arg, sco) // ARGV[1]
}

_, err := redis.Int(d.deleteScoreScript.Do(con, arg...))
Expand Down
8 changes: 4 additions & 4 deletions pkg/sorted/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ type Delete interface {
// Clean removes the sorted set under key including the derived indeizes.
Clean(key string) error

// Index deletes the element identified by value within the specified sorted
// set. Note that indices associated with the underlying element are purged
// automatically as well.
Index(key string, val string) error
// Index deletes the elements identified by the given values within the
// specified sorted set. Note that indices associated with the underlying
// elements are purged automatically as well.
Index(key string, val ...string) error

// Limit cuts off all older elements from the sorted set under key resulting
// in a sorted set that contains the latest lim amount of elements. Consider
Expand Down

0 comments on commit 2c2833c

Please sign in to comment.