forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filtered_batch.go
52 lines (45 loc) · 1.14 KB
/
filtered_batch.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gsharedmemory
import (
"github.com/haowang0402/avalanchego/api/proto/gsharedmemoryproto"
)
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, len(b.writes))
i := 0
for keyStr, value := range b.writes {
reqs[i] = &gsharedmemoryproto.BatchPut{
Key: []byte(keyStr),
Value: value,
}
i++
}
return reqs
}
func (b *filteredBatch) DeleteRequests() []*gsharedmemoryproto.BatchDelete {
reqs := make([]*gsharedmemoryproto.BatchDelete, len(b.deletes))
i := 0
for keyStr := range b.deletes {
reqs[i] = &gsharedmemoryproto.BatchDelete{
Key: []byte(keyStr),
}
i++
}
return reqs
}