Skip to content

Commit 3b45a98

Browse files
committed
support for "<timestamp>-*"
1 parent 72a6b15 commit 3b45a98

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

cmd_stream_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ func TestStream(t *testing.T) {
5656
ok(t, err)
5757
equals(t, "978321845004-0", id)
5858

59+
id, err = s.XAdd("s1", "978321845004-*", []string{"name", "bazz"})
60+
ok(t, err)
61+
equals(t, "978321845004-1", id)
62+
5963
stream, err := s.Stream("s1")
6064
ok(t, err)
61-
equals(t, 2, len(stream))
65+
equals(t, 3, len(stream))
6266
equals(t, StreamEntry{
6367
ID: "12345-67",
6468
Values: []string{"name", "bar"},
@@ -67,6 +71,10 @@ func TestStream(t *testing.T) {
6771
ID: "978321845004-0",
6872
Values: []string{"name", "baz"},
6973
}, stream[1])
74+
equals(t, StreamEntry{
75+
ID: "978321845004-1",
76+
Values: []string{"name", "bazz"},
77+
}, stream[2])
7078
})
7179

7280
useRESP3(t, c)

stream.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,14 +230,23 @@ func (s *streamKey) createGroup(group, id string) error {
230230
}
231231

232232
// streamAdd adds an entry to a stream. Returns the new entry ID.
233-
// If id is empty or "*" the ID will be generated automatically.
233+
// If id is empty, "*", or "123-*", the ID will be generated automatically.
234234
// `values` should have an even length.
235235
func (s *streamKey) add(entryID string, values []string, now time.Time) (string, error) {
236236
s.mu.Lock()
237237
defer s.mu.Unlock()
238238

239-
if entryID == "" || entryID == "*" {
239+
switch {
240+
case entryID == "" || entryID == "*":
240241
entryID = s.generateID(now)
242+
default:
243+
// "<timestamp>-*"
244+
parts := strings.Split(entryID, "-")
245+
if len(parts) == 2 && parts[1] == "*" {
246+
if ts, err := strconv.Atoi(parts[0]); err == nil {
247+
entryID = s.generateID(time.Unix(int64(ts), 0))
248+
}
249+
}
241250
}
242251

243252
entryID, err := formatStreamID(entryID)

0 commit comments

Comments
 (0)