From fbe7e63e8441014ef2256fcc300e66deff9a726a Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Fri, 13 Sep 2024 10:08:20 +0100 Subject: [PATCH 1/7] DOC-4232 added first stream example --- doctests/stream_tutorial_test.go | 96 ++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 doctests/stream_tutorial_test.go diff --git a/doctests/stream_tutorial_test.go b/doctests/stream_tutorial_test.go new file mode 100644 index 000000000..a3cd25e67 --- /dev/null +++ b/doctests/stream_tutorial_test.go @@ -0,0 +1,96 @@ +// EXAMPLE: stream_tutorial +// HIDE_START +package example_commands_test + +import ( + "context" + "fmt" + + "github.com/redis/go-redis/v9" +) + +// HIDE_END + +// REMOVE_START +func UNUSED(v ...interface{}) {} + +// REMOVE_END + +func ExampleClient_xadd() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + // STEP_START xadd + res1, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + }).Result() + + if err != nil { + panic(err) + } + + // fmt.Println(res1) // >>> 1692629576966-0 + + res2, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + }).Result() + + if err != nil { + panic(err) + } + + // fmt.PrintLn(res2) // >>> 1692629594113-0 + + res3, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + }).Result() + + if err != nil { + panic(err) + } + + // fmt.Println(res3) // >>> 1692629613374-0 + // STEP_END + + // REMOVE_START + UNUSED(res1, res2, res3) + // REMOVE_END + + xlen, err := rdb.XLen(ctx, "race:france").Result() + + if err != nil { + panic(err) + } + + fmt.Println(xlen) // >>> 3 + + // Output: + // 3 +} From 67d7bf679871c987413a304834092524a0fa899f Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 17 Sep 2024 11:34:13 +0100 Subject: [PATCH 2/7] DOC-4232 examples up to xadd_7 --- doctests/stream_tutorial_test.go | 438 ++++++++++++++++++++++++++++++- 1 file changed, 435 insertions(+), 3 deletions(-) diff --git a/doctests/stream_tutorial_test.go b/doctests/stream_tutorial_test.go index a3cd25e67..93102df52 100644 --- a/doctests/stream_tutorial_test.go +++ b/doctests/stream_tutorial_test.go @@ -44,7 +44,7 @@ func ExampleClient_xadd() { panic(err) } - // fmt.Println(res1) // >>> 1692629576966-0 + // fmt.Println(res1) // >>> 1692632086370-0 res2, err := rdb.XAdd(ctx, &redis.XAddArgs{ Stream: "race:france", @@ -60,7 +60,7 @@ func ExampleClient_xadd() { panic(err) } - // fmt.PrintLn(res2) // >>> 1692629594113-0 + // fmt.PrintLn(res2) // >>> 1692632094485-0 res3, err := rdb.XAdd(ctx, &redis.XAddArgs{ Stream: "race:france", @@ -76,7 +76,7 @@ func ExampleClient_xadd() { panic(err) } - // fmt.Println(res3) // >>> 1692629613374-0 + // fmt.Println(res3) // >>> 1692632102976-0 // STEP_END // REMOVE_START @@ -94,3 +94,435 @@ func ExampleClient_xadd() { // Output: // 3 } + +func ExampleClient_xrange() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + // STEP_START xrange + res4, err := rdb.XRangeN(ctx, "race:france", "1691765278160-0", "+", 2).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res4) + // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla... + // STEP_END + + // Output: + // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] +} + +func ExampleClient_xreadblock() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + // STEP_START xread_block + res5, err := rdb.XRead(ctx, &redis.XReadArgs{ + Streams: []string{"race:france", "0"}, + Count: 100, + Block: 300, + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res5) + // >>> // [{race:france [{1692632086370-0 map[location_id:1 position:1... + // STEP_END + + // Output: + // [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]} {1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]}]}] +} + +func ExampleClient_xadd2() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + // STEP_START xadd_2 + res6, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + }).Result() + + if err != nil { + panic(err) + } + + //fmt.Println(res6) // >>> 1692632147973-0 + // STEP_END + + // REMOVE_START + UNUSED(res6) + // REMOVE_END + + xlen, err := rdb.XLen(ctx, "race:france").Result() + + if err != nil { + panic(err) + } + + fmt.Println(xlen) // >>> 1 + // Output: + // 1 +} + +func ExampleClient_xlen() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + ID: "1692632147973-0", + }).Result() + + if err != nil { + panic(err) + } + // STEP_START xlen + res7, err := rdb.XLen(ctx, "race:france").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res7) // >>> 4 + // STEP_END + + // Output: + // 4 +} + +func ExampleClient_xaddid() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:usa") + // REMOVE_END + + // STEP_START xadd_id + res8, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:usa", + Values: map[string]interface{}{ + "racer": "Castilla", + }, + ID: "0-1", + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res8) // >>> 0-1 + + res9, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:usa", + Values: map[string]interface{}{ + "racer": "Norem", + }, + ID: "0-2", + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res9) // >>> 0-2 + // STEP_END + + // Output: + // 0-1 + // 0-2 +} + +func ExampleClient_xaddbadid() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:usa") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:usa", + Values: map[string]interface{}{ + + "racer": "Castilla", + }, + ID: "0-1", + }).Result() + + if err != nil { + panic(err) + } + + // STEP_START xadd_bad_id + res10, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Values: map[string]interface{}{ + "racer": "Prickett", + }, + ID: "0-1", + }).Result() + + if err != nil { + fmt.Println(err) + // >>> ERR The ID specified in XADD is equal or smaller than the target stream top item + } + + fmt.Println(res10) + // STEP_END + + // Output: + // ERR The ID specified in XADD is equal or smaller than the target stream top item +} + +func ExampleClient_xadd7() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:usa") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:usa", + Values: map[string]interface{}{ + "racer": "Castilla", + }, + ID: "0-1", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:usa", + Values: map[string]interface{}{ + "racer": "Norem", + }, + ID: "0-2", + }).Result() + + if err != nil { + panic(err) + } + + // STEP_START xadd_7 + res11, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:usa", + Values: map[string]interface{}{ + "racer": "Prickett", + }, + ID: "0-*", + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res11) // >>> 0-3 + // STEP_END + + // Output: + // 0-3 +} From a9181e5ac014ec8fa234651b98f4367b391a16a6 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 17 Sep 2024 13:29:03 +0100 Subject: [PATCH 3/7] DOC-4232 examples up to xread --- doctests/stream_tutorial_test.go | 621 +++++++++++++++++++++++++++++++ 1 file changed, 621 insertions(+) diff --git a/doctests/stream_tutorial_test.go b/doctests/stream_tutorial_test.go index 93102df52..6b3332691 100644 --- a/doctests/stream_tutorial_test.go +++ b/doctests/stream_tutorial_test.go @@ -526,3 +526,624 @@ func ExampleClient_xadd7() { // Output: // 0-3 } + +func ExampleClient_xrangeall() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + ID: "1692632147973-0", + }).Result() + + if err != nil { + panic(err) + } + // STEP_START xrange_all + res12, err := rdb.XRange(ctx, "race:france", "-", "+").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res12) + // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla... + // STEP_END + + // Output: + // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]} {1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] +} + +func ExampleClient_xrangetime() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + ID: "1692632147973-0", + }).Result() + + if err != nil { + panic(err) + } + // STEP_START xrange_time + res13, err := rdb.XRange(ctx, "race:france", + "1692632086369", "1692632086371", + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res13) + // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]}] + // STEP_END + + // Output: + // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]}] +} + +func ExampleClient_xrangestep1() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + ID: "1692632147973-0", + }).Result() + + if err != nil { + panic(err) + } + // STEP_START xrange_step_1 + res14, err := rdb.XRangeN(ctx, "race:france", "-", "+", 2).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res14) + // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] + // STEP_END + + // Output: + // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] +} + +func ExampleClient_xrangestep2() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + ID: "1692632147973-0", + }).Result() + + if err != nil { + panic(err) + } + // STEP_START xrange_step_2 + res15, err := rdb.XRangeN(ctx, "race:france", + "(1692632094485-0", "+", 2, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res15) + // >>> [{1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + // STEP_END + + // Output: + // [{1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] +} + +func ExampleClient_xrangeempty() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + ID: "1692632147973-0", + }).Result() + + if err != nil { + panic(err) + } + + // STEP_START xrange_empty + res16, err := rdb.XRangeN(ctx, "race:france", + "(1692632147973-0", "+", 2, + ).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res16) + // >>> [] + // STEP_END + + // Output: + // [] +} + +func ExampleClient_xrevrange() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + ID: "1692632147973-0", + }).Result() + + if err != nil { + panic(err) + } + + // STEP_START xrevrange + res17, err := rdb.XRevRangeN(ctx, "race:france", "+", "-", 1).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res17) + // >>> [{1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + // STEP_END + + // Output: + // [{1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] +} + +func ExampleClient_xread() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:france") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 30.2, + "position": 1, + "location_id": 1, + }, + ID: "1692632086370-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Norem", + "speed": 28.8, + "position": 3, + "location_id": 1, + }, + ID: "1692632094485-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Prickett", + "speed": 29.7, + "position": 2, + "location_id": 1, + }, + ID: "1692632102976-0", + }).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:france", + Values: map[string]interface{}{ + "rider": "Castilla", + "speed": 29.9, + "position": 1, + "location_id": 2, + }, + ID: "1692632147973-0", + }).Result() + + if err != nil { + panic(err) + } + + // STEP_START xread + res18, err := rdb.XRead(ctx, &redis.XReadArgs{ + Streams: []string{"race:france", "0"}, + Count: 2, + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res18) + // >>> [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]}] + // STEP_END + + // Output: + // [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]}] +} From a02310609595ac11f3947b5c3997e88a0dbd82d8 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Tue, 17 Sep 2024 16:53:23 +0100 Subject: [PATCH 4/7] DOC-4232 examples up to xclaim --- doctests/stream_tutorial_test.go | 804 ++++++++++--------------------- 1 file changed, 255 insertions(+), 549 deletions(-) diff --git a/doctests/stream_tutorial_test.go b/doctests/stream_tutorial_test.go index 6b3332691..97bc20dc5 100644 --- a/doctests/stream_tutorial_test.go +++ b/doctests/stream_tutorial_test.go @@ -95,7 +95,7 @@ func ExampleClient_xadd() { // 3 } -func ExampleClient_xrange() { +func ExampleClient_racefrance1() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ @@ -164,67 +164,6 @@ func ExampleClient_xrange() { // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla... // STEP_END - // Output: - // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] -} - -func ExampleClient_xreadblock() { - ctx := context.Background() - - rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", // no password docs - DB: 0, // use default DB - }) - - // REMOVE_START - rdb.Del(ctx, "race:france") - // REMOVE_END - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 30.2, - "position": 1, - "location_id": 1, - }, - ID: "1692632086370-0", - }).Result() - - if err != nil { - panic(err) - } - - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Norem", - "speed": 28.8, - "position": 3, - "location_id": 1, - }, - ID: "1692632094485-0", - }).Result() - - if err != nil { - panic(err) - } - - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Prickett", - "speed": 29.7, - "position": 2, - "location_id": 1, - }, - ID: "1692632102976-0", - }).Result() - - if err != nil { - panic(err) - } - // STEP_START xread_block res5, err := rdb.XRead(ctx, &redis.XReadArgs{ Streams: []string{"race:france", "0"}, @@ -240,23 +179,6 @@ func ExampleClient_xreadblock() { // >>> // [{race:france [{1692632086370-0 map[location_id:1 position:1... // STEP_END - // Output: - // [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]} {1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]}]}] -} - -func ExampleClient_xadd2() { - ctx := context.Background() - - rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", // no password docs - DB: 0, // use default DB - }) - - // REMOVE_START - rdb.Del(ctx, "race:france") - // REMOVE_END - // STEP_START xadd_2 res6, err := rdb.XAdd(ctx, &redis.XAddArgs{ Stream: "race:france", @@ -275,93 +197,6 @@ func ExampleClient_xadd2() { //fmt.Println(res6) // >>> 1692632147973-0 // STEP_END - // REMOVE_START - UNUSED(res6) - // REMOVE_END - - xlen, err := rdb.XLen(ctx, "race:france").Result() - - if err != nil { - panic(err) - } - - fmt.Println(xlen) // >>> 1 - // Output: - // 1 -} - -func ExampleClient_xlen() { - ctx := context.Background() - - rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", // no password docs - DB: 0, // use default DB - }) - - // REMOVE_START - rdb.Del(ctx, "race:france") - // REMOVE_END - - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 30.2, - "position": 1, - "location_id": 1, - }, - ID: "1692632086370-0", - }).Result() - - if err != nil { - panic(err) - } - - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Norem", - "speed": 28.8, - "position": 3, - "location_id": 1, - }, - ID: "1692632094485-0", - }).Result() - - if err != nil { - panic(err) - } - - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Prickett", - "speed": 29.7, - "position": 2, - "location_id": 1, - }, - ID: "1692632102976-0", - }).Result() - - if err != nil { - panic(err) - } - - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 29.9, - "position": 1, - "location_id": 2, - }, - ID: "1692632147973-0", - }).Result() - - if err != nil { - panic(err) - } // STEP_START xlen res7, err := rdb.XLen(ctx, "race:france").Result() @@ -372,11 +207,17 @@ func ExampleClient_xlen() { fmt.Println(res7) // >>> 4 // STEP_END + // REMOVE_START + UNUSED(res6) + // REMOVE_END + // Output: + // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] + // [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]} {1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]}]}] // 4 } -func ExampleClient_xaddid() { +func ExampleClient_raceusa() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ @@ -419,37 +260,6 @@ func ExampleClient_xaddid() { fmt.Println(res9) // >>> 0-2 // STEP_END - // Output: - // 0-1 - // 0-2 -} - -func ExampleClient_xaddbadid() { - ctx := context.Background() - - rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", // no password docs - DB: 0, // use default DB - }) - - // REMOVE_START - rdb.Del(ctx, "race:usa") - // REMOVE_END - - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:usa", - Values: map[string]interface{}{ - - "racer": "Castilla", - }, - ID: "0-1", - }).Result() - - if err != nil { - panic(err) - } - // STEP_START xadd_bad_id res10, err := rdb.XAdd(ctx, &redis.XAddArgs{ Values: map[string]interface{}{ @@ -466,47 +276,6 @@ func ExampleClient_xaddbadid() { fmt.Println(res10) // STEP_END - // Output: - // ERR The ID specified in XADD is equal or smaller than the target stream top item -} - -func ExampleClient_xadd7() { - ctx := context.Background() - - rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", // no password docs - DB: 0, // use default DB - }) - - // REMOVE_START - rdb.Del(ctx, "race:usa") - // REMOVE_END - - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:usa", - Values: map[string]interface{}{ - "racer": "Castilla", - }, - ID: "0-1", - }).Result() - - if err != nil { - panic(err) - } - - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:usa", - Values: map[string]interface{}{ - "racer": "Norem", - }, - ID: "0-2", - }).Result() - - if err != nil { - panic(err) - } - // STEP_START xadd_7 res11, err := rdb.XAdd(ctx, &redis.XAddArgs{ Stream: "race:usa", @@ -524,10 +293,14 @@ func ExampleClient_xadd7() { // STEP_END // Output: + // 0-1 + // 0-2 + // ERR The ID specified in XADD is equal or smaller than the target stream top item + // // 0-3 } -func ExampleClient_xrangeall() { +func ExampleClient_racefrance2() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ @@ -610,100 +383,92 @@ func ExampleClient_xrangeall() { // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla... // STEP_END - // Output: - // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]} {1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] -} + // STEP_START xrange_time + res13, err := rdb.XRange(ctx, "race:france", + "1692632086369", "1692632086371", + ).Result() -func ExampleClient_xrangetime() { - ctx := context.Background() + if err != nil { + panic(err) + } - rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", // no password docs - DB: 0, // use default DB - }) - - // REMOVE_START - rdb.Del(ctx, "race:france") - // REMOVE_END + fmt.Println(res13) + // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]}] + // STEP_END - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 30.2, - "position": 1, - "location_id": 1, - }, - ID: "1692632086370-0", - }).Result() + // STEP_START xrange_step_1 + res14, err := rdb.XRangeN(ctx, "race:france", "-", "+", 2).Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Norem", - "speed": 28.8, - "position": 3, - "location_id": 1, - }, - ID: "1692632094485-0", - }).Result() + fmt.Println(res14) + // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] + // STEP_END + + // STEP_START xrange_step_2 + res15, err := rdb.XRangeN(ctx, "race:france", + "(1692632094485-0", "+", 2, + ).Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Prickett", - "speed": 29.7, - "position": 2, - "location_id": 1, - }, - ID: "1692632102976-0", - }).Result() + fmt.Println(res15) + // >>> [{1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + // STEP_END + + // STEP_START xrange_empty + res16, err := rdb.XRangeN(ctx, "race:france", + "(1692632147973-0", "+", 2, + ).Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 29.9, - "position": 1, - "location_id": 2, - }, - ID: "1692632147973-0", - }).Result() + fmt.Println(res16) + // >>> [] + // STEP_END + + // STEP_START xrevrange + res17, err := rdb.XRevRangeN(ctx, "race:france", "+", "-", 1).Result() if err != nil { panic(err) } - // STEP_START xrange_time - res13, err := rdb.XRange(ctx, "race:france", - "1692632086369", "1692632086371", - ).Result() + + fmt.Println(res17) + // >>> [{1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + // STEP_END + + // STEP_START xread + res18, err := rdb.XRead(ctx, &redis.XReadArgs{ + Streams: []string{"race:france", "0"}, + Count: 2, + }).Result() if err != nil { panic(err) } - fmt.Println(res13) - // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]}] + fmt.Println(res18) + // >>> [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]}] // STEP_END // Output: + // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]} {1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]}] + // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] + // [{1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + // [] + // [{1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + // [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]}] } -func ExampleClient_xrangestep1() { +func ExampleClient_xgroupcreate() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ @@ -731,66 +496,50 @@ func ExampleClient_xrangestep1() { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Norem", - "speed": 28.8, - "position": 3, - "location_id": 1, - }, - ID: "1692632094485-0", - }).Result() + // STEP_START xgroup_create + res19, err := rdb.XGroupCreate(ctx, "race:france", "france_riders", "$").Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Prickett", - "speed": 29.7, - "position": 2, - "location_id": 1, - }, - ID: "1692632102976-0", - }).Result() + fmt.Println(res19) // >>> OK + // STEP_END - if err != nil { - panic(err) - } + // Output: + // OK +} - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 29.9, - "position": 1, - "location_id": 2, - }, - ID: "1692632147973-0", - }).Result() +func ExampleClient_xgroupcreatemkstream() { + ctx := context.Background() - if err != nil { - panic(err) - } - // STEP_START xrange_step_1 - res14, err := rdb.XRangeN(ctx, "race:france", "-", "+", 2).Result() + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:italy") + // REMOVE_END + + // STEP_START xgroup_create_mkstream + res20, err := rdb.XGroupCreateMkStream(ctx, + "race:italy", "italy_riders", "$", + ).Result() if err != nil { panic(err) } - fmt.Println(res14) - // >>> [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] + fmt.Println(res20) // >>> OK // STEP_END // Output: - // [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}] + // OK } -func ExampleClient_xrangestep2() { +func ExampleClient_xgroupread() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ @@ -800,86 +549,102 @@ func ExampleClient_xrangestep2() { }) // REMOVE_START - rdb.Del(ctx, "race:france") + rdb.Del(ctx, "race:italy") // REMOVE_END - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 30.2, - "position": 1, - "location_id": 1, - }, - ID: "1692632086370-0", + _, err := rdb.XGroupCreateMkStream(ctx, + "race:italy", "italy_riders", "$", + ).Result() + + if err != nil { + panic(err) + } + + // STEP_START xgroup_read + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Castilla"}, }).Result() + // >>> 1692632639151-0 if err != nil { panic(err) } _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Norem", - "speed": 28.8, - "position": 3, - "location_id": 1, - }, - ID: "1692632094485-0", + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Royce"}, }).Result() + // >>> 1692632647899-0 if err != nil { panic(err) } _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Prickett", - "speed": 29.7, - "position": 2, - "location_id": 1, - }, - ID: "1692632102976-0", + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Sam-Bodden"}, }).Result() + // >>> 1692632662819-0 if err != nil { panic(err) } _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 29.9, - "position": 1, - "location_id": 2, - }, - ID: "1692632147973-0", + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Prickett"}, }).Result() + // >>> 1692632670501-0 if err != nil { panic(err) } - // STEP_START xrange_step_2 - res15, err := rdb.XRangeN(ctx, "race:france", - "(1692632094485-0", "+", 2, - ).Result() + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Norem"}, + }).Result() + // >>> 1692632678249-0 if err != nil { panic(err) } - fmt.Println(res15) - // >>> [{1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + // fmt.Println(res25) + + res21, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{ + Streams: []string{"race:italy", ">"}, + Group: "italy_riders", + Consumer: "Alice", + Count: 1, + }).Result() + + if err != nil { + panic(err) + } + + // fmt.Println(res21) + // >>> [{race:italy [{1692632639151-0 map[rider:Castilla]}]}] // STEP_END + // REMOVE_START + UNUSED(res21) + // REMOVE_END + + xlen, err := rdb.XLen(ctx, "race:italy").Result() + + if err != nil { + panic(err) + } + + fmt.Println(xlen) + // Output: - // [{1692632102976-0 map[location_id:1 position:2 rider:Prickett speed:29.7]} {1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + // 5 } -func ExampleClient_xrangeempty() { +func ExampleClient_raceitaly() { ctx := context.Background() rdb := redis.NewClient(&redis.Options{ @@ -889,33 +654,22 @@ func ExampleClient_xrangeempty() { }) // REMOVE_START - rdb.Del(ctx, "race:france") + rdb.Del(ctx, "race:italy") + rdb.XGroupDestroy(ctx, "race:italy", "italy_riders") // REMOVE_END - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 30.2, - "position": 1, - "location_id": 1, - }, - ID: "1692632086370-0", - }).Result() + _, err := rdb.XGroupCreateMkStream(ctx, + "race:italy", "italy_riders", "$", + ).Result() if err != nil { panic(err) } _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Norem", - "speed": 28.8, - "position": 3, - "location_id": 1, - }, - ID: "1692632094485-0", + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Castilla"}, + ID: "1692632639151-0", }).Result() if err != nil { @@ -923,14 +677,9 @@ func ExampleClient_xrangeempty() { } _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Prickett", - "speed": 29.7, - "position": 2, - "location_id": 1, - }, - ID: "1692632102976-0", + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Royce"}, + ID: "1692632647899-0", }).Result() if err != nil { @@ -938,212 +687,169 @@ func ExampleClient_xrangeempty() { } _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 29.9, - "position": 1, - "location_id": 2, - }, - ID: "1692632147973-0", + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Sam-Bodden"}, + ID: "1692632662819-0", }).Result() if err != nil { panic(err) } - // STEP_START xrange_empty - res16, err := rdb.XRangeN(ctx, "race:france", - "(1692632147973-0", "+", 2, - ).Result() + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Prickett"}, + ID: "1692632670501-0", + }).Result() if err != nil { panic(err) } - fmt.Println(res16) - // >>> [] - // STEP_END - - // Output: - // [] -} - -func ExampleClient_xrevrange() { - ctx := context.Background() - - rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", // no password docs - DB: 0, // use default DB - }) - - // REMOVE_START - rdb.Del(ctx, "race:france") - // REMOVE_END - - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 30.2, - "position": 1, - "location_id": 1, - }, - ID: "1692632086370-0", + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + Values: map[string]interface{}{"rider": "Norem"}, + ID: "1692632678249-0", }).Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Norem", - "speed": 28.8, - "position": 3, - "location_id": 1, - }, - ID: "1692632094485-0", + _, err = rdb.XReadGroup(ctx, &redis.XReadGroupArgs{ + Streams: []string{"race:italy", ">"}, + Group: "italy_riders", + Consumer: "Alice", + Count: 1, }).Result() if err != nil { panic(err) } - - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Prickett", - "speed": 29.7, - "position": 2, - "location_id": 1, - }, - ID: "1692632102976-0", + // STEP_START xgroup_read_id + res22, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{ + Streams: []string{"race:italy", "0"}, + Group: "italy_riders", + Consumer: "Alice", }).Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 29.9, - "position": 1, - "location_id": 2, - }, - ID: "1692632147973-0", - }).Result() + fmt.Println(res22) + // >>> [{race:italy [{1692632639151-0 map[rider:Castilla]}]}] + // STEP_END + + // STEP_START xack + res23, err := rdb.XAck(ctx, + "race:italy", "italy_riders", "1692632639151-0", + ).Result() if err != nil { panic(err) } - // STEP_START xrevrange - res17, err := rdb.XRevRangeN(ctx, "race:france", "+", "-", 1).Result() + fmt.Println(res23) // >>> 1 + + res24, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{ + Streams: []string{"race:italy", "0"}, + Group: "italy_riders", + Consumer: "Alice", + }).Result() if err != nil { panic(err) } - fmt.Println(res17) - // >>> [{1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] + fmt.Println(res24) + // >>> [{race:italy []}] // STEP_END - // Output: - // [{1692632147973-0 map[location_id:2 position:1 rider:Castilla speed:29.9]}] -} - -func ExampleClient_xread() { - ctx := context.Background() - - rdb := redis.NewClient(&redis.Options{ - Addr: "localhost:6379", - Password: "", // no password docs - DB: 0, // use default DB - }) - - // REMOVE_START - rdb.Del(ctx, "race:france") - // REMOVE_END - - _, err := rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 30.2, - "position": 1, - "location_id": 1, - }, - ID: "1692632086370-0", + // STEP_START xgroup_read_bob + res25, err := rdb.XReadGroup(ctx, &redis.XReadGroupArgs{ + Streams: []string{"race:italy", ">"}, + Group: "italy_riders", + Consumer: "Bob", + Count: 2, }).Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Norem", - "speed": 28.8, - "position": 3, - "location_id": 1, - }, - ID: "1692632094485-0", - }).Result() + fmt.Println(res25) + // >>> [{race:italy [{1692632647899-0 map[rider:Royce]} {1692632662819-0 map[rider:Sam-Bodden]}]}] + + // STEP_END + + // STEP_START xpending + res26, err := rdb.XPending(ctx, "race:italy", "italy_riders").Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Prickett", - "speed": 29.7, - "position": 2, - "location_id": 1, - }, - ID: "1692632102976-0", + fmt.Println(res26) + // >>> &{2 1692632647899-0 1692632662819-0 map[Bob:2]} + // STEP_END + + // STEP_START xpending_plus_minus + res27, err := rdb.XPendingExt(ctx, &redis.XPendingExtArgs{ + Stream: "race:italy", + Group: "italy_riders", + Start: "-", + End: "+", + Count: 10, }).Result() if err != nil { panic(err) } - _, err = rdb.XAdd(ctx, &redis.XAddArgs{ - Stream: "race:france", - Values: map[string]interface{}{ - "rider": "Castilla", - "speed": 29.9, - "position": 1, - "location_id": 2, - }, - ID: "1692632147973-0", - }).Result() + // fmt.Println(res27) + // >>> [{1692632647899-0 Bob 0s 1} {1692632662819-0 Bob 0s 1}] + // STEP_END + + // STEP_START xrange_pending + res28, err := rdb.XRange(ctx, "race:italy", + "1692632647899-0", "1692632647899-0", + ).Result() if err != nil { panic(err) } - // STEP_START xread - res18, err := rdb.XRead(ctx, &redis.XReadArgs{ - Streams: []string{"race:france", "0"}, - Count: 2, + fmt.Println(res28) + // STEP_END + + // STEP_START xclaim + res29, err := rdb.XClaim(ctx, &redis.XClaimArgs{ + Stream: "race:italy", + Group: "italy_riders", + Consumer: "Alice", + MinIdle: 0, + Messages: []string{"1692632647899-0"}, }).Result() if err != nil { panic(err) } - fmt.Println(res18) - // >>> [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]}] + fmt.Println(res29) + // >>> [{1692632647899-0 map[rider:Royce]}] // STEP_END + // REMOVE_START + UNUSED(res27) + // REMOVE_END + // Output: - // [{race:france [{1692632086370-0 map[location_id:1 position:1 rider:Castilla speed:30.2]} {1692632094485-0 map[location_id:1 position:3 rider:Norem speed:28.8]}]}] + // [{race:italy [{1692632639151-0 map[rider:Castilla]}]}] + // 1 + // [{race:italy []}] + // [{race:italy [{1692632647899-0 map[rider:Royce]} {1692632662819-0 map[rider:Sam-Bodden]}]}] + // &{2 1692632647899-0 1692632662819-0 map[Bob:2]} + // [{1692632647899-0 map[rider:Royce]}] + // [{1692632647899-0 map[rider:Royce]}] } From 157a7f48a3f14e5ab848d20e6b43adf2a7c21353 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 18 Sep 2024 10:06:18 +0100 Subject: [PATCH 5/7] DOC-4232 added remaining examples --- doctests/stream_tutorial_test.go | 221 ++++++++++++++++++++++++++++++- 1 file changed, 220 insertions(+), 1 deletion(-) diff --git a/doctests/stream_tutorial_test.go b/doctests/stream_tutorial_test.go index 97bc20dc5..d47cf1638 100644 --- a/doctests/stream_tutorial_test.go +++ b/doctests/stream_tutorial_test.go @@ -840,8 +840,147 @@ func ExampleClient_raceitaly() { // >>> [{1692632647899-0 map[rider:Royce]}] // STEP_END + // STEP_START xautoclaim + res30, res30a, err := rdb.XAutoClaim(ctx, &redis.XAutoClaimArgs{ + Stream: "race:italy", + Group: "italy_riders", + Consumer: "Alice", + Start: "0-0", + Count: 1, + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res30) // >>> [{1692632647899-0 map[rider:Royce]}] + fmt.Println(res30a) // >>> 1692632662819-0 + // STEP_END + + // STEP_START xautoclaim_cursor + res31, res31a, err := rdb.XAutoClaim(ctx, &redis.XAutoClaimArgs{ + Stream: "race:italy", + Group: "italy_riders", + Consumer: "Lora", + Start: "(1692632662819-0", + Count: 1, + }).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res31) + fmt.Println(res31a) + // STEP_END + + // STEP_START xinfo + res32, err := rdb.XInfoStream(ctx, "race:italy").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res32) + // >>> &{5 1 2 1 1692632678249-0 0-0 5 {1692632639151-0 map[rider:Castilla]} {1692632678249-0 map[rider:Norem]} 1692632639151-0} + // STEP_END + + // STEP_START xinfo_groups + res33, err := rdb.XInfoGroups(ctx, "race:italy").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res33) + // >>> [{italy_riders 3 2 1692632662819-0 3 2}] + // STEP_END + + // STEP_START xinfo_consumers + res34, err := rdb.XInfoConsumers(ctx, "race:italy", "italy_riders").Result() + + if err != nil { + panic(err) + } + + // fmt.Println(res34) + // >>> [{Alice 1 1ms 1ms} {Bob 1 2ms 2ms} {Lora 0 1ms -1ms}] + // STEP_END + + // STEP_START maxlen + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + MaxLen: 2, + Values: map[string]interface{}{"rider": "Jones"}, + }, + ).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + MaxLen: 2, + Values: map[string]interface{}{"rider": "Wood"}, + }, + ).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + MaxLen: 2, + Values: map[string]interface{}{"rider": "Henshaw"}, + }, + ).Result() + + if err != nil { + panic(err) + } + + res35, err := rdb.XLen(ctx, "race:italy").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res35) // >>> 2 + + res36, err := rdb.XRange(ctx, "race:italy", "-", "+").Result() + + if err != nil { + panic(err) + } + + // fmt.Println(res36) + // >>> [{1726649529170-1 map[rider:Wood]} {1726649529171-0 map[rider:Henshaw]}] + // STEP_END + + // STEP_START xtrim + res37, err := rdb.XTrimMaxLen(ctx, "race:italy", 10).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res37) // >>> 0 + // STEP_END + + // STEP_START xtrim + res38, err := rdb.XTrimMaxLenApprox(ctx, "race:italy", 10, 20).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res38) // >>> 0 + // STEP_END + // REMOVE_START - UNUSED(res27) + UNUSED(res27, res34, res36) // REMOVE_END // Output: @@ -852,4 +991,84 @@ func ExampleClient_raceitaly() { // &{2 1692632647899-0 1692632662819-0 map[Bob:2]} // [{1692632647899-0 map[rider:Royce]}] // [{1692632647899-0 map[rider:Royce]}] + // [{1692632647899-0 map[rider:Royce]}] + // 1692632662819-0 + // [] + // 0-0 + // &{5 1 2 1 1692632678249-0 0-0 5 {1692632639151-0 map[rider:Castilla]} {1692632678249-0 map[rider:Norem]} 1692632639151-0} + // [{italy_riders 3 2 1692632662819-0 3 2}] + // 2 + // 0 + // 0 +} + +func ExampleClient_xdel() { + ctx := context.Background() + + rdb := redis.NewClient(&redis.Options{ + Addr: "localhost:6379", + Password: "", // no password docs + DB: 0, // use default DB + }) + + // REMOVE_START + rdb.Del(ctx, "race:italy") + // REMOVE_END + + _, err := rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + MaxLen: 2, + Values: map[string]interface{}{"rider": "Wood"}, + ID: "1692633198206-0", + }, + ).Result() + + if err != nil { + panic(err) + } + + _, err = rdb.XAdd(ctx, &redis.XAddArgs{ + Stream: "race:italy", + MaxLen: 2, + Values: map[string]interface{}{"rider": "Henshaw"}, + ID: "1692633208557-0", + }, + ).Result() + + if err != nil { + panic(err) + } + + // STEP_START xdel + res39, err := rdb.XRangeN(ctx, "race:italy", "-", "+", 2).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res39) + // >>> [{1692633198206-0 map[rider:Wood]} {1692633208557-0 map[rider:Henshaw]}] + + res40, err := rdb.XDel(ctx, "race:italy", "1692633208557-0").Result() + + if err != nil { + panic(err) + } + + fmt.Println(res40) // 1 + + res41, err := rdb.XRangeN(ctx, "race:italy", "-", "+", 2).Result() + + if err != nil { + panic(err) + } + + fmt.Println(res41) + // >>> [{1692633198206-0 map[rider:Wood]}] + // STEP_END + + // Output: + // [{1692633198206-0 map[rider:Wood]} {1692633208557-0 map[rider:Henshaw]}] + // 1 + // [{1692633198206-0 map[rider:Wood]}] } From f23ce445da7f07d2ba13cfec0a0c42c4e8e2870a Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 18 Sep 2024 11:14:18 +0100 Subject: [PATCH 6/7] DOC-4232 more fixes --- doctests/stream_tutorial_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/doctests/stream_tutorial_test.go b/doctests/stream_tutorial_test.go index d47cf1638..3091ba0e4 100644 --- a/doctests/stream_tutorial_test.go +++ b/doctests/stream_tutorial_test.go @@ -820,7 +820,7 @@ func ExampleClient_raceitaly() { panic(err) } - fmt.Println(res28) + fmt.Println(res28) // >>> [{1692632647899-0 map[rider:Royce]}] // STEP_END // STEP_START xclaim @@ -837,7 +837,6 @@ func ExampleClient_raceitaly() { } fmt.Println(res29) - // >>> [{1692632647899-0 map[rider:Royce]}] // STEP_END // STEP_START xautoclaim @@ -870,8 +869,8 @@ func ExampleClient_raceitaly() { panic(err) } - fmt.Println(res31) - fmt.Println(res31a) + fmt.Println(res31) // >>> [] + fmt.Println(res31a) // >>> 0-0 // STEP_END // STEP_START xinfo @@ -969,7 +968,7 @@ func ExampleClient_raceitaly() { fmt.Println(res37) // >>> 0 // STEP_END - // STEP_START xtrim + // STEP_START xtrim2 res38, err := rdb.XTrimMaxLenApprox(ctx, "race:italy", 10, 20).Result() if err != nil { From b85a97adc13357fbda7c19fa051f5c736a15fe56 Mon Sep 17 00:00:00 2001 From: Andy Stark Date: Wed, 18 Sep 2024 11:37:47 +0100 Subject: [PATCH 7/7] DOC-4232 fix for test fail on CI build --- doctests/stream_tutorial_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/doctests/stream_tutorial_test.go b/doctests/stream_tutorial_test.go index 3091ba0e4..093324705 100644 --- a/doctests/stream_tutorial_test.go +++ b/doctests/stream_tutorial_test.go @@ -269,11 +269,9 @@ func ExampleClient_raceusa() { }).Result() if err != nil { - fmt.Println(err) + // fmt.Println(err) // >>> ERR The ID specified in XADD is equal or smaller than the target stream top item } - - fmt.Println(res10) // STEP_END // STEP_START xadd_7 @@ -292,11 +290,13 @@ func ExampleClient_raceusa() { fmt.Println(res11) // >>> 0-3 // STEP_END + // REMOVE_START + UNUSED(res10) + // REMOVE_END + // Output: // 0-1 // 0-2 - // ERR The ID specified in XADD is equal or smaller than the target stream top item - // // 0-3 }