-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.go
50 lines (39 loc) · 1.13 KB
/
store.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
package goserver
import (
"context"
"fmt"
"google.golang.org/protobuf/types/known/anypb"
dspb "github.com/brotherlogic/dstore/proto"
)
func (s *GoServer) LoadData(ctx context.Context, key string, consensus float32) ([]byte, error) {
conn, err := s.FDialServer(ctx, "dstore")
if err != nil {
return nil, err
}
defer conn.Close()
client := dspb.NewDStoreServiceClient(conn)
res, err := client.Read(ctx, &dspb.ReadRequest{Key: key})
if err != nil {
return nil, err
}
if res.GetConsensus() < consensus {
return nil, fmt.Errorf("could not get read consensus (%v)", res.GetConsensus())
}
return res.GetValue().GetValue(), nil
}
func (s *GoServer) SaveData(ctx context.Context, data []byte, key string, consensus float32) error {
conn, err := s.FDialServer(ctx, "dstore")
if err != nil {
return err
}
defer conn.Close()
client := dspb.NewDStoreServiceClient(conn)
res, err := client.Write(ctx, &dspb.WriteRequest{Key: key, Value: &anypb.Any{Value: data}})
if err != nil {
return err
}
if res.GetConsensus() < consensus {
return fmt.Errorf("could not get write consensus (%v)", res.GetConsensus())
}
return nil
}