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

enable non index updates of sorted set elements #7

Merged
merged 4 commits into from
Sep 27, 2023
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
289 changes: 209 additions & 80 deletions pkg/conformance/client_single_sorted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,116 @@ func Test_Client_Single_Sorted_Delete_Empty(t *testing.T) {
}
}

func Test_Client_Single_Sorted_Delete_Index(t *testing.T) {
var err error

var cli redigo.Interface
{
c := client.Config{
Kind: client.KindSingle,
}

cli, err = client.New(c)
if err != nil {
t.Fatal(err)
}

err = cli.Purge()
if err != nil {
t.Fatal(err)
}
}

{
exi, err := cli.Sorted().Exists().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
if exi {
t.Fatal("element must not exist")
}
}

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

{
exi, err := cli.Sorted().Exists().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
if !exi {
t.Fatal("element must exist")
}
}

// We just created an element that defined the indices a and b. Now we delete
// this very element including its indices. With this test we ensure that
// elements as well as their associated indices get automatically purged when
// deleting indexed elements.
{
err := cli.Sorted().Delete().Index("ssk", "foo")
if err != nil {
t.Fatal(err)
}
}

// It should be possible to create the exact same element again including the
// same indizes after it has been deleted. This verifies that deleting
// elements including its indizes works as expected.
{
err := cli.Sorted().Create().Index("ssk", "foo", 0.8, "a", "b")
if err != nil {
t.Fatal(err)
}
}

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

{
exi, err := cli.Sorted().Exists().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
if exi {
t.Fatal("element must not exist")
}
}

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

{
exi, err := cli.Sorted().Exists().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
if !exi {
t.Fatal("element must exist")
}
}

{
err := cli.Sorted().Delete().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
}
}

func Test_Client_Single_Sorted_Delete_Limit(t *testing.T) {
var err error

Expand Down Expand Up @@ -523,85 +633,6 @@ func Test_Client_Single_Sorted_Delete_Score(t *testing.T) {
t.Fatal("element must not exist")
}
}

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

{
exi, err := cli.Sorted().Exists().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
if !exi {
t.Fatal("element must exist")
}
}

// We just created an element that defined the indices a and b. Now we
// delete this very element only using its score. With this test we ensure
// that elements as well as their associated indices get automatically
// purged when deleting elements only using their score.
{
err := cli.Sorted().Delete().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
}

// It should be possible to create the exact same element again including
// its indizes after it has been deleted before. This verifies that deleting
// elements including its indizes works as expected.
{
err := cli.Sorted().Create().Index("ssk", "foo", 0.8, "a", "b")
if err != nil {
t.Fatal(err)
}
}

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

{
exi, err := cli.Sorted().Exists().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
if exi {
t.Fatal("element must not exist")
}
}

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

{
exi, err := cli.Sorted().Exists().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
if !exi {
t.Fatal("element must exist")
}
}

{
err := cli.Sorted().Delete().Score("ssk", 0.8)
if err != nil {
t.Fatal(err)
}
}
}

func Test_Client_Single_Sorted_Delete_Value(t *testing.T) {
Expand Down Expand Up @@ -1663,7 +1694,7 @@ func Test_Client_Single_Sorted_Search_Value(t *testing.T) {
}
}

func Test_Client_Single_Sorted_Update(t *testing.T) {
func Test_Client_Single_Sorted_Update_Index(t *testing.T) {
var err error

var cli redigo.Interface
Expand Down Expand Up @@ -1751,6 +1782,104 @@ func Test_Client_Single_Sorted_Update(t *testing.T) {
}
}

func Test_Client_Single_Sorted_Update_Score(t *testing.T) {
var err error

var cli redigo.Interface
{
c := client.Config{
Kind: client.KindSingle,
}

cli, err = client.New(c)
if err != nil {
t.Fatal(err)
}

err = cli.Purge()
if err != nil {
t.Fatal(err)
}
}

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

{
res, err := cli.Sorted().Search().Score("ssk", 0.8, 0.8)
if err != nil {
t.Fatal(err)
}
if len(res) != 1 {
t.Fatal("expected", 1, "got", len(res))
}
if res[0] != "foo" {
t.Fatal("expected", "foo", "got", res[0])
}
}

{
err := cli.Sorted().Create().Value("ssk", "foo", 0.7)
if !sorted.IsAlreadyExistsError(err) {
t.Fatal("expected", "alreadyExistsError", "got", err)
}
}

{
_, err := cli.Sorted().Update().Score("ssk", "bar", 0.7)
if !sorted.IsNotFound(err) {
t.Fatal("expected", "notFoundError", "got", err)
}
}

{
upd, err := cli.Sorted().Update().Score("ssk", "bar", 0.8)
if err != nil {
t.Fatal(err)
}
if !upd {
t.Fatal("element must be updated")
}
}

{
upd, err := cli.Sorted().Update().Score("ssk", "bar", 0.8)
if err != nil {
t.Fatal(err)
}
if upd {
t.Fatal("element must not be updated")
}
}

{
res, err := cli.Sorted().Search().Score("ssk", 0.8, 0.8)
if err != nil {
t.Fatal(err)
}
if len(res) != 1 {
t.Fatal("expected", 1, "got", len(res))
}
if res[0] != "bar" {
t.Fatal("expected", "bar", "got", res[0])
}
}

{
res, err := cli.Sorted().Search().Order("ssk", 0, -1)
if err != nil {
t.Fatal(err)
}
if len(res) != 1 {
t.Fatal("expected", 1, "got", len(res))
}
}
}

func contains(lis []string, itm string) bool {
for _, l := range lis {
if l == itm {
Expand Down
9 changes: 9 additions & 0 deletions pkg/fake/sorted_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fake

type SortedUpdate struct {
FakeIndex func() (bool, error)
FakeScore func() (bool, error)
}

func (u *SortedUpdate) Index(key string, new string, sco float64, ind ...string) (bool, error) {
Expand All @@ -11,3 +12,11 @@ func (u *SortedUpdate) Index(key string, new string, sco float64, ind ...string)

return false, nil
}

func (u *SortedUpdate) Score(key string, new string, sco float64) (bool, error) {
if u.FakeScore != nil {
return u.FakeScore()
}

return false, nil
}
2 changes: 1 addition & 1 deletion pkg/sorted/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (c *create) Value(key string, val string, sco float64) error {
con := c.pool.Get()
defer con.Close()

res, err := redis.Int(con.Do("ZADD", prefix.WithKeys(c.prefix, key), sco, val))
res, err := redis.Int(con.Do("ZADD", prefix.WithKeys(c.prefix, key), "NX", sco, val))
if err != nil {
return tracer.Mask(err)
}
Expand Down
21 changes: 3 additions & 18 deletions pkg/sorted/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@ const deleteIndexScript = `
return 0
`

const deleteScoreScript = `
redis.call("ZREMRANGEBYSCORE", KEYS[2], ARGV[1], ARGV[1])
redis.call("ZREMRANGEBYSCORE", KEYS[1], ARGV[1], ARGV[1])

return 0
`

type delete struct {
pool *redis.Pool

deleteCleanScript *redis.Script
deleteIndexScript *redis.Script
deleteScoreScript *redis.Script

prefix string
}
Expand All @@ -56,7 +48,7 @@ func (d *delete) Clean(key string) error {
arg = append(arg, prefix.WithKeys(d.prefix, index.New(key))) // KEYS[2]
}

_, err := redis.Int(d.deleteCleanScript.Do(con, arg...))
_, err := redis.Int64(d.deleteCleanScript.Do(con, arg...))
if err != nil {
return tracer.Mask(err)
}
Expand All @@ -78,7 +70,7 @@ func (d *delete) Index(key string, val ...string) error {
}
}

_, err := redis.Int(d.deleteIndexScript.Do(con, arg...))
_, err := redis.Int64(d.deleteIndexScript.Do(con, arg...))
if err != nil {
return tracer.Mask(err)
}
Expand Down Expand Up @@ -116,14 +108,7 @@ func (d *delete) Score(key string, sco float64) 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, sco) // ARGV[1]
}

_, err := redis.Int(d.deleteScoreScript.Do(con, arg...))
_, err := redis.Int64(con.Do("ZREMRANGEBYSCORE", prefix.WithKeys(d.prefix, key), sco, sco))
if err != nil {
return tracer.Mask(err)
}
Expand Down
Loading