-
Notifications
You must be signed in to change notification settings - Fork 665
/
shared_memory_client.go
167 lines (147 loc) · 3.98 KB
/
shared_memory_client.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gsharedmemory
import (
"context"
"github.com/ava-labs/avalanchego/chains/atomic"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/gsharedmemory/gsharedmemoryproto"
)
var (
_ atomic.SharedMemory = &Client{}
)
// Client is an implementation of a messenger channel that talks over RPC.
type Client struct {
client gsharedmemoryproto.SharedMemoryClient
}
// NewClient returns a shared memory instance connected to a shared memory instance
func NewClient(client gsharedmemoryproto.SharedMemoryClient) *Client {
return &Client{client: client}
}
// Put ...
func (c *Client) Put(peerChainID ids.ID, elems []*atomic.Element, batches ...database.Batch) error {
req := gsharedmemoryproto.PutRequest{
PeerChainID: peerChainID[:],
Elems: make([]*gsharedmemoryproto.Element, len(elems)),
Batches: make([]*gsharedmemoryproto.Batch, len(batches)),
}
for i, elem := range elems {
req.Elems[i] = &gsharedmemoryproto.Element{
Key: elem.Key,
Value: elem.Value,
Traits: elem.Traits,
}
}
for i, batch := range batches {
batch := batch.Inner()
fb := filteredBatch{
writes: make(map[string][]byte),
deletes: make(map[string]struct{}),
}
if err := batch.Replay(&fb); err != nil {
return err
}
req.Batches[i] = &gsharedmemoryproto.Batch{
Puts: fb.PutRequests(),
Deletes: fb.DeleteRequests(),
}
}
_, err := c.client.Put(context.Background(), &req)
return err
}
// Get ...
func (c *Client) Get(peerChainID ids.ID, keys [][]byte) (values [][]byte, err error) {
resp, err := c.client.Get(context.Background(), &gsharedmemoryproto.GetRequest{
PeerChainID: peerChainID[:],
Keys: keys,
})
if err != nil {
return nil, err
}
return resp.Values, nil
}
// Indexed ...
func (c *Client) Indexed(
peerChainID ids.ID,
traits [][]byte,
startTrait,
startKey []byte,
limit int,
) (
values [][]byte,
lastTrait,
lastKey []byte,
err error,
) {
resp, err := c.client.Indexed(context.Background(), &gsharedmemoryproto.IndexedRequest{
PeerChainID: peerChainID[:],
Traits: traits,
StartTrait: startTrait,
StartKey: startKey,
Limit: int32(limit),
})
if err != nil {
return nil, nil, nil, err
}
return resp.Values, resp.LastTrait, resp.LastKey, nil
}
// Remove ...
func (c *Client) Remove(peerChainID ids.ID, keys [][]byte, batches ...database.Batch) error {
req := gsharedmemoryproto.RemoveRequest{
PeerChainID: peerChainID[:],
Keys: keys,
Batches: make([]*gsharedmemoryproto.Batch, len(batches)),
}
for i, batch := range batches {
batch := batch.Inner()
fb := filteredBatch{
writes: make(map[string][]byte),
deletes: make(map[string]struct{}),
}
if err := batch.Replay(&fb); err != nil {
return err
}
req.Batches[i] = &gsharedmemoryproto.Batch{
Puts: fb.PutRequests(),
Deletes: fb.DeleteRequests(),
}
}
_, err := c.client.Remove(context.Background(), &req)
return err
}
type filteredBatch struct {
writes map[string][]byte
deletes map[string]struct{}
}
func (b *filteredBatch) Put(key []byte, value []byte) error {
keyStr := string(key)
delete(b.deletes, keyStr)
b.writes[keyStr] = value
return nil
}
func (b *filteredBatch) Delete(key []byte) error {
keyStr := string(key)
delete(b.writes, keyStr)
b.deletes[keyStr] = struct{}{}
return nil
}
func (b *filteredBatch) PutRequests() []*gsharedmemoryproto.BatchPut {
reqs := make([]*gsharedmemoryproto.BatchPut, 0, len(b.writes))
for keyStr, value := range b.writes {
reqs = append(reqs, &gsharedmemoryproto.BatchPut{
Key: []byte(keyStr),
Value: value,
})
}
return reqs
}
func (b *filteredBatch) DeleteRequests() []*gsharedmemoryproto.BatchDelete {
reqs := make([]*gsharedmemoryproto.BatchDelete, 0, len(b.deletes))
for keyStr := range b.deletes {
reqs = append(reqs, &gsharedmemoryproto.BatchDelete{
Key: []byte(keyStr),
})
}
return reqs
}