-
Notifications
You must be signed in to change notification settings - Fork 648
/
filtered_batch.go
54 lines (46 loc) · 1.16 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
53
54
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gsharedmemory
import (
"github.com/ava-labs/avalanchego/utils/set"
sharedmemorypb "github.com/ava-labs/avalanchego/proto/pb/sharedmemory"
)
type filteredBatch struct {
writes map[string][]byte
deletes set.Set[string]
}
func (b *filteredBatch) Put(key []byte, value []byte) error {
keyStr := string(key)
b.deletes.Remove(keyStr)
b.writes[keyStr] = value
return nil
}
func (b *filteredBatch) Delete(key []byte) error {
keyStr := string(key)
delete(b.writes, keyStr)
b.deletes.Add(keyStr)
return nil
}
func (b *filteredBatch) PutRequests() []*sharedmemorypb.BatchPut {
reqs := make([]*sharedmemorypb.BatchPut, len(b.writes))
i := 0
for keyStr, value := range b.writes {
reqs[i] = &sharedmemorypb.BatchPut{
Key: []byte(keyStr),
Value: value,
}
i++
}
return reqs
}
func (b *filteredBatch) DeleteRequests() []*sharedmemorypb.BatchDelete {
reqs := make([]*sharedmemorypb.BatchDelete, len(b.deletes))
i := 0
for keyStr := range b.deletes {
reqs[i] = &sharedmemorypb.BatchDelete{
Key: []byte(keyStr),
}
i++
}
return reqs
}