Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (72 loc) · 2.54 KB
/
Copy pathmain.go
File metadata and controls
75 lines (72 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// Basic demonstrates typed finite read and mutation batches addressed to one Store.
package main
import (
"context"
"flag"
"fmt"
"time"
weir "github.com/batchstream/weir-go"
"go.mongodb.org/mongo-driver/v2/bson"
)
func main() {
if err := run(); err != nil {
panic(err)
}
}
func run() error {
address := flag.String("address", "127.0.0.1:7447", "Weir application address")
storeName := flag.String("store", "mongo", "logical Store: mongo or search")
database := flag.String("database", "weir_example", "pre-created MongoDB database")
index := flag.String("index", "weir_example", "pre-created Search index")
count := flag.Int("count", 100, "finite number of read requests")
flag.Parse()
if *storeName != "mongo" && *storeName != "search" || *count < 1 {
return fmt.Errorf("invalid Store or count")
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
client, err := weir.New(*address)
if err != nil {
return err
}
defer client.Close()
target := weir.EncodeSegment(*database) + "/records/s:example"
record := bson.D{{Key: "_id", Value: "example"}, {Key: "n", Value: int32(1)}}
data, err := bson.Marshal(record)
if err != nil {
return err
}
document := &weir.Document{ContentType: "application/bson", Data: data}
if *storeName == "search" {
target = weir.EncodeSegment(*index) + "/s:example"
document = &weir.Document{ContentType: "application/json", Data: []byte(`{"n":1}`)}
}
request := &weir.MutateRequest{Resource: target, Action: weir.MutationPut, Document: document}
results, err := client.Mutate(ctx, *storeName, weir.WithRequests(request), weir.WithQueryVisible())
if err != nil {
if results[0] != nil {
return fmt.Errorf("write RPC incomplete; backend evidence=%v: %w", results[0], err)
}
return fmt.Errorf("write result unavailable; effects indeterminate: %w", err)
}
if results[0].GetOutcome() != weir.MutationApplied || results[0].GetFailure() != nil {
return fmt.Errorf("write: %v", results[0])
}
var batch []*weir.ReadRequest
for range *count {
read := &weir.ReadRequest{Resource: target}
batch = append(batch, read)
}
reads, err := client.Read(ctx, *storeName, weir.WithRequests(batch...))
if err != nil {
return fmt.Errorf("read RPC incomplete: %w", err)
}
for index, read := range reads {
if read.Failure != nil {
return fmt.Errorf("read %d: %v", index, read.Failure)
}
fmt.Printf("index=%d record=%d bytes missing=%t\n", index, len(read.Document.GetData()), read.Missing)
}
fmt.Printf("completed %d reads with indexed stream responses\n", *count)
return nil
}