This repository was archived by the owner on Sep 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathreqstore.go
116 lines (94 loc) · 2.82 KB
/
reqstore.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
// TODO: This is the original old code with very few modifications.
// Go through all of it, comment what is to be kept and delete what is not needed.
// Package reqstore is an implementation of the RequestStore utilized by the samples.
// Depending on your application, it may or may not be appropriate. In particular, if your
// application wants to retain the requests rather than simply apply and discard them, you may
// wish to write your own or adapt this one.
package reqstore
import (
"fmt"
badger "github.com/dgraph-io/badger/v2"
"github.com/hyperledger-labs/mirbft/pkg/pb/requestpb"
t "github.com/hyperledger-labs/mirbft/pkg/types"
"github.com/pkg/errors"
)
func reqKey(ack *requestpb.RequestRef) []byte {
return []byte(fmt.Sprintf("req-%d.%d.%x", ack.ClientId, ack.ReqNo, ack.Digest))
}
func allocKey(clientID t.ClientID, reqNo t.ReqNo) []byte {
return []byte(fmt.Sprintf("alloc-%d.%d", clientID, reqNo))
}
type Store struct {
db *badger.DB
}
func Open(dirPath string) (*Store, error) {
var badgerOpts badger.Options
if dirPath == "" {
badgerOpts = badger.DefaultOptions("").WithInMemory(true)
} else {
badgerOpts = badger.DefaultOptions(dirPath).WithSyncWrites(false).WithTruncate(true)
// TODO, maybe WithDetectConflicts as false?
}
db, err := badger.Open(badgerOpts)
if err != nil {
return nil, errors.WithMessage(err, "could not open backing db")
}
return &Store{
db: db,
}, nil
}
func (s *Store) PutAllocation(clientID t.ClientID, reqNo t.ReqNo, digest []byte) error {
return s.db.Update(func(txn *badger.Txn) error {
return txn.Set(allocKey(clientID, reqNo), digest)
})
}
func (s *Store) GetAllocation(clientID t.ClientID, reqNo t.ReqNo) ([]byte, error) {
var valCopy []byte
err := s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get(allocKey(clientID, reqNo))
if err != nil {
return err
}
valCopy, err = item.ValueCopy(nil)
return err
})
if err == badger.ErrKeyNotFound {
return nil, nil
}
return valCopy, err
}
func (s *Store) PutRequest(requestRef *requestpb.RequestRef, data []byte) error {
return s.db.Update(func(txn *badger.Txn) error {
return txn.Set(reqKey(requestRef), data)
})
}
func (s *Store) GetRequest(requestRef *requestpb.RequestRef) ([]byte, error) {
var valCopy []byte
err := s.db.View(func(txn *badger.Txn) error {
item, err := txn.Get(reqKey(requestRef))
if err != nil {
return err
}
valCopy, err = item.ValueCopy(nil)
return err
})
if err == badger.ErrKeyNotFound {
return nil, nil
}
return valCopy, err
}
func (s *Store) Commit(ack *requestpb.RequestRef) error {
return s.db.Update(func(txn *badger.Txn) error {
return txn.Delete(reqKey(ack))
})
}
func (s *Store) Sync() error {
return s.db.Sync()
}
func (s *Store) Close() {
s.db.Close()
}