-
Notifications
You must be signed in to change notification settings - Fork 50
/
inspector.go
136 lines (106 loc) · 3.15 KB
/
inspector.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package vault
import (
"github.com/hyperledger-labs/fabric-smart-client/platform/fabric/driver"
"github.com/pkg/errors"
)
type Inspector struct {
Rws readWriteSet
}
func NewInspector() *Inspector {
return &Inspector{
Rws: readWriteSet{
readSet: readSet{
orderedReads: map[string][]string{},
reads: reads{},
},
writeSet: writeSet{
orderedWrites: map[string][]string{},
writes: writes{},
},
metaWriteSet: metaWriteSet{
metawrites: namespaceKeyedMetaWrites{},
},
},
}
}
func (i *Inspector) IsValid() error {
return nil
}
func (i *Inspector) SetState(namespace string, key string, value []byte) error {
panic("programming error: the rwset inspector is read-only")
}
func (i *Inspector) GetState(namespace string, key string, opts ...driver.GetStateOpt) ([]byte, error) {
return i.Rws.writeSet.get(namespace, key), nil
}
func (i *Inspector) DeleteState(namespace string, key string) error {
panic("programming error: the rwset inspector is read-only")
}
func (i *Inspector) GetStateMetadata(namespace, key string, opts ...driver.GetStateOpt) (map[string][]byte, error) {
return i.Rws.metaWriteSet.get(namespace, key), nil
}
func (i *Inspector) SetStateMetadata(namespace, key string, metadata map[string][]byte) error {
panic("programming error: the rwset inspector is read-only")
}
func (i *Inspector) GetReadKeyAt(ns string, pos int) (string, error) {
key, in := i.Rws.readSet.getAt(ns, pos)
if !in {
return "", errors.Errorf("no read at position %d for namespace %s", pos, ns)
}
return key, nil
}
func (i *Inspector) GetReadAt(ns string, pos int) (string, []byte, error) {
key, in := i.Rws.readSet.getAt(ns, pos)
if !in {
return "", nil, errors.Errorf("no read at position %d for namespace %s", pos, ns)
}
val, err := i.GetState(ns, key)
if err != nil {
return "", nil, err
}
return key, val, nil
}
func (i *Inspector) GetWriteAt(ns string, pos int) (string, []byte, error) {
key, in := i.Rws.writeSet.getAt(ns, pos)
if !in {
return "", nil, errors.Errorf("no write at position %d for namespace %s", pos, ns)
}
return key, i.Rws.writeSet.get(ns, key), nil
}
func (i *Inspector) NumReads(ns string) int {
return len(i.Rws.reads[ns])
}
func (i *Inspector) NumWrites(ns string) int {
return len(i.Rws.writes[ns])
}
func (i *Inspector) Namespaces() []string {
mergedMaps := map[string]struct{}{}
for ns := range i.Rws.reads {
mergedMaps[ns] = struct{}{}
}
for ns := range i.Rws.writes {
mergedMaps[ns] = struct{}{}
}
namespaces := make([]string, 0, len(mergedMaps))
for ns := range mergedMaps {
namespaces = append(namespaces, ns)
}
return namespaces
}
func (i *Inspector) AppendRWSet(raw []byte, nss ...string) error {
panic("programming error: the rwset inspector is read-only")
}
func (i *Inspector) Bytes() ([]byte, error) {
panic("programming error: unexpected call")
}
func (i *Inspector) Equals(other interface{}, nss ...string) error {
panic("programming error: unexpected call")
}
func (i *Inspector) Done() {
}
func (i *Inspector) Clear(ns string) error {
panic("programming error: unexpected call")
}