-
Notifications
You must be signed in to change notification settings - Fork 344
/
store_message.go
56 lines (48 loc) · 1.73 KB
/
store_message.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
package kv
import (
"context"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
// StoreMessage protobuf generic implementation for kv.Store interface applicable for all data models
type StoreMessage struct {
Store
}
// GetMsg based on 'path' the value will be loaded into 'msg' and return a predicate.
//
// In case 'msg' is nil, a predicate will be returned
func (s *StoreMessage) GetMsg(ctx context.Context, partitionKey string, key []byte, msg protoreflect.ProtoMessage) (Predicate, error) {
res, err := s.Get(ctx, []byte(partitionKey), key)
if err != nil {
return nil, err
}
// conditional msg - make it work like Get just using key
if msg == nil {
return res.Predicate, nil
}
err = proto.Unmarshal(res.Value, msg)
if err != nil {
return nil, err
}
return res.Predicate, nil
}
func (s *StoreMessage) SetMsg(ctx context.Context, partitionKey string, key []byte, msg protoreflect.ProtoMessage) error {
val, err := proto.Marshal(msg)
if err != nil {
return err
}
return s.Set(ctx, []byte(partitionKey), key, val)
}
func (s *StoreMessage) SetMsgIf(ctx context.Context, partitionKey string, key []byte, msg protoreflect.ProtoMessage, predicate Predicate) error {
val, err := proto.Marshal(msg)
if err != nil {
return err
}
return s.SetIf(ctx, []byte(partitionKey), key, val, predicate)
}
func (s *StoreMessage) DeleteMsg(ctx context.Context, partitionKey string, key []byte) error {
return s.Delete(ctx, []byte(partitionKey), key)
}
func (s *StoreMessage) Scan(ctx context.Context, msgType protoreflect.MessageType, partitionKey string, prefix, after []byte) (*PrimaryIterator, error) {
return NewPrimaryIterator(ctx, s.Store, msgType, partitionKey, prefix, IteratorOptionsAfter(after))
}