-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
120 lines (99 loc) · 2.6 KB
/
service.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Package service implements the store service interface
package service
import (
"context"
"io"
"time"
"github.com/focalsolution/micro-go-micro/client"
"github.com/focalsolution/micro-go-micro/config/options"
"github.com/focalsolution/micro-go-micro/store"
pb "github.com/focalsolution/micro-go-micro/store/service/proto"
)
type serviceStore struct {
options.Options
// Addresses of the nodes
Nodes []string
// store service client
Client pb.StoreService
}
// Sync all the known records
func (s *serviceStore) List() ([]*store.Record, error) {
stream, err := s.Client.List(context.Background(), &pb.ListRequest{}, client.WithAddress(s.Nodes...))
if err != nil {
return nil, err
}
defer stream.Close()
var records []*store.Record
for {
rsp, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
return records, err
}
for _, record := range rsp.Records {
records = append(records, &store.Record{
Key: record.Key,
Value: record.Value,
Expiry: time.Duration(record.Expiry) * time.Second,
})
}
}
return records, nil
}
// Read a record with key
func (s *serviceStore) Read(keys ...string) ([]*store.Record, error) {
rsp, err := s.Client.Read(context.Background(), &pb.ReadRequest{
Keys: keys,
}, client.WithAddress(s.Nodes...))
if err != nil {
return nil, err
}
records := make([]*store.Record, 0, len(rsp.Records))
for _, val := range rsp.Records {
records = append(records, &store.Record{
Key: val.Key,
Value: val.Value,
Expiry: time.Duration(val.Expiry) * time.Second,
})
}
return records, nil
}
// Write a record
func (s *serviceStore) Write(recs ...*store.Record) error {
records := make([]*pb.Record, 0, len(recs))
for _, record := range recs {
records = append(records, &pb.Record{
Key: record.Key,
Value: record.Value,
Expiry: int64(record.Expiry.Seconds()),
})
}
_, err := s.Client.Write(context.Background(), &pb.WriteRequest{
Records: records,
}, client.WithAddress(s.Nodes...))
return err
}
// Delete a record with key
func (s *serviceStore) Delete(keys ...string) error {
_, err := s.Client.Delete(context.Background(), &pb.DeleteRequest{
Keys: keys,
}, client.WithAddress(s.Nodes...))
return err
}
// NewStore returns a new store service implementation
func NewStore(opts ...options.Option) store.Store {
options := options.NewOptions(opts...)
var nodes []string
n, ok := options.Values().Get("store.nodes")
if ok {
nodes = n.([]string)
}
service := &serviceStore{
Options: options,
Nodes: nodes,
Client: pb.NewStoreService("go.micro.store", client.DefaultClient),
}
return service
}