forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
redolog.go
88 lines (74 loc) · 1.96 KB
/
redolog.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package statecouchdb
import (
"bytes"
"encoding/gob"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/statedb"
"github.com/hyperledger/fabric/core/ledger/kvledger/txmgmt/version"
)
var redologKeyPrefix = []byte{byte(0)}
type redoLoggerProvider struct {
leveldbProvider *leveldbhelper.Provider
}
type redoLogger struct {
dbName string
dbHandle *leveldbhelper.DBHandle
}
type redoRecord struct {
UpdateBatch *statedb.UpdateBatch
Version *version.Height
}
func newRedoLoggerProvider(dirPath string) (*redoLoggerProvider, error) {
provider, err := leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: dirPath})
if err != nil {
return nil, err
}
return &redoLoggerProvider{leveldbProvider: provider}, nil
}
func (p *redoLoggerProvider) newRedoLogger(dbName string) *redoLogger {
return &redoLogger{
dbHandle: p.leveldbProvider.GetDBHandle(dbName),
}
}
func (p *redoLoggerProvider) close() {
p.leveldbProvider.Close()
}
func (l *redoLogger) persist(r *redoRecord) error {
k := encodeRedologKey(l.dbName)
v, err := encodeRedologVal(r)
if err != nil {
return err
}
return l.dbHandle.Put(k, v, true)
}
func (l *redoLogger) load() (*redoRecord, error) {
k := encodeRedologKey(l.dbName)
v, err := l.dbHandle.Get(k)
if err != nil || v == nil {
return nil, err
}
return decodeRedologVal(v)
}
func encodeRedologKey(dbName string) []byte {
return append(redologKeyPrefix, []byte(dbName)...)
}
func encodeRedologVal(r *redoRecord) ([]byte, error) {
buf := bytes.NewBuffer(nil)
encoder := gob.NewEncoder(buf)
if err := encoder.Encode(r); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func decodeRedologVal(b []byte) (*redoRecord, error) {
decoder := gob.NewDecoder(bytes.NewBuffer(b))
var r *redoRecord
if err := decoder.Decode(&r); err != nil {
return nil, err
}
return r, nil
}