-
Notifications
You must be signed in to change notification settings - Fork 3
/
example_test.go
59 lines (50 loc) · 1.1 KB
/
example_test.go
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
package sharded_test
import (
"context"
"errors"
"fmt"
"os"
"github.com/embano1/memlog"
"github.com/embano1/memlog/sharded"
)
func Example() {
ctx := context.Background()
// the default number of shards (1000) is sufficient to assign a shard per key
// for this example (i.e. no key overlap within a shard)
l, err := sharded.New(ctx)
if err != nil {
fmt.Printf("create log: %v", err)
os.Exit(1)
}
data := map[string][]string{
"users": {"tom", "sarah", "ajit"},
"groups": {"friends", "family", "colleagues"},
}
for key, vals := range data {
for _, val := range vals {
_, err := l.Write(ctx, []byte(key), []byte(val))
if err != nil {
fmt.Printf("write: %v", err)
os.Exit(1)
}
}
}
fmt.Println("reading all users...")
offset := memlog.Offset(0)
for {
record, err := l.Read(ctx, []byte("users"), offset)
if err != nil {
if errors.Is(err, memlog.ErrFutureOffset) {
break // end of log
}
fmt.Printf("read: %v", err)
os.Exit(1)
}
fmt.Printf("- %s\n", string(record.Data))
offset++
}
// Output: reading all users...
// - tom
// - sarah
// - ajit
}