Skip to content

Commit

Permalink
Merge pull request #391 from daniel-cohere/streamCmp-in-xtrim
Browse files Browse the repository at this point in the history
use streamCmp in Xtrim
  • Loading branch information
alicebob authored Dec 18, 2024
2 parents ef93126 + e4791b5 commit 5320c5c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion cmd_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1076,16 +1076,22 @@ func TestStreamTrim(t *testing.T) {
ok(t, err)
_, err = c.Do("XADD", "planets", "5-1", "name", "Saturn")
ok(t, err)
_, err = c.Do("XADD", "planets", "5-2", "name", "Uranus")
ok(t, err)
_, err = c.Do("XADD", "planets", "5-11", "name", "Pluto")
ok(t, err)

mustDo(t, c,
"XTRIM", "planets", "MAXLEN", "=", "3", proto.Int(3))
"XTRIM", "planets", "MAXLEN", "=", "5", proto.Int(3))

mustDo(t, c,
"XRANGE", "planets", "-", "+",
proto.Array(
proto.Array(proto.String("3-0"), proto.Strings("name", "Mars")),
proto.Array(proto.String("4-1"), proto.Strings("name", "Jupiter")),
proto.Array(proto.String("5-1"), proto.Strings("name", "Saturn")),
proto.Array(proto.String("5-2"), proto.Strings("name", "Uranus")),
proto.Array(proto.String("5-11"), proto.Strings("name", "Pluto")),
))

mustDo(t, c,
Expand All @@ -1096,6 +1102,8 @@ func TestStreamTrim(t *testing.T) {
proto.Array(
proto.Array(proto.String("4-1"), proto.Strings("name", "Jupiter")),
proto.Array(proto.String("5-1"), proto.Strings("name", "Saturn")),
proto.Array(proto.String("5-2"), proto.Strings("name", "Uranus")),
proto.Array(proto.String("5-11"), proto.Strings("name", "Pluto")),
))

mustDo(t, c,
Expand All @@ -1105,6 +1113,17 @@ func TestStreamTrim(t *testing.T) {
"XRANGE", "planets", "-", "+",
proto.Array(
proto.Array(proto.String("5-1"), proto.Strings("name", "Saturn")),
proto.Array(proto.String("5-2"), proto.Strings("name", "Uranus")),
proto.Array(proto.String("5-11"), proto.Strings("name", "Pluto")),
))

mustDo(t, c,
"XTRIM", "planets", "MINID", "5-11", proto.Int(2))

mustDo(t, c,
"XRANGE", "planets", "-", "+",
proto.Array(
proto.Array(proto.String("5-11"), proto.Strings("name", "Pluto")),
))
}

Expand Down
2 changes: 1 addition & 1 deletion stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (s *streamKey) trimBefore(id string) int {
s.mu.Lock()
var delete []string
for _, entry := range s.entries {
if entry.ID < id {
if streamCmp(entry.ID, id) < 0 {
delete = append(delete, entry.ID)
} else {
break
Expand Down

0 comments on commit 5320c5c

Please sign in to comment.