-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathtransaction.go
77 lines (62 loc) · 1.29 KB
/
transaction.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
package remote
import (
"bytes"
"errors"
"github.com/NethermindEth/juno/db"
"github.com/NethermindEth/juno/grpc/gen"
"github.com/NethermindEth/juno/utils"
)
var _ db.Transaction = (*transaction)(nil)
type transaction struct {
client gen.KV_TxClient
log utils.SimpleLogger
}
func (t *transaction) NewIterator(_ []byte, _ bool) (db.Iterator, error) {
err := t.client.Send(&gen.Cursor{
Op: gen.Op_OPEN,
})
if err != nil {
return nil, err
}
pair, err := t.client.Recv()
if err != nil {
return nil, err
}
return &iterator{
client: t.client,
cursorID: pair.CursorId,
log: t.log,
}, nil
}
func (t *transaction) Discard() error {
return t.client.CloseSend()
}
func (t *transaction) Commit() error {
return errors.New("read only DB")
}
func (t *transaction) Set(key, val []byte) error {
return errors.New("read only DB")
}
func (t *transaction) Delete(key []byte) error {
return errors.New("read only DB")
}
func (t *transaction) Get(key []byte, cb func([]byte) error) error {
err := t.client.Send(&gen.Cursor{
Op: gen.Op_GET,
K: key,
})
if err != nil {
return err
}
pair, err := t.client.Recv()
if err != nil {
return err
}
if !bytes.Equal(key, pair.K) {
return db.ErrKeyNotFound
}
return cb(pair.V)
}
func (t *transaction) Impl() any {
return t.client
}