-
Notifications
You must be signed in to change notification settings - Fork 50
/
session.go
82 lines (69 loc) · 2.23 KB
/
session.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package orion
import (
"github.com/hyperledger-labs/fabric-smart-client/platform/orion/driver"
"github.com/pkg/errors"
)
type QueryIterator struct {
it driver.QueryIterator
}
func (i *QueryIterator) Next() (string, []byte, uint64, uint64, bool, error) {
return i.it.Next()
}
type SessionQueryExecutor struct {
db string
dataTx driver.DataTx
query driver.Query
}
func (d *SessionQueryExecutor) Get(key string) ([]byte, error) {
return d.dataTx.Get(d.db, key)
}
// GetDataByRange executes a range query. The startKey is
// inclusive but endKey is not. When the startKey is an empty string, it denotes
// `fetch keys from the beginning` while an empty endKey denotes `fetch keys till the
// the end`. The limit denotes the number of records to be fetched in total. However,
// when the limit is set to 0, it denotes no limit. The iterator returned by
// GetDataByRange is used to retrieve the records.
func (d *SessionQueryExecutor) GetDataByRange(startKey, endKey string, limit uint64) (*QueryIterator, error) {
it, err := d.query.GetDataByRange(d.db, startKey, endKey, limit)
if err != nil {
return nil, errors.Wrapf(err, "failed getting data by range")
}
return &QueryIterator{it}, nil
}
type Session struct {
s driver.Session
}
func (s *Session) QueryExecutor(db string) (*SessionQueryExecutor, error) {
dataTx, err := s.s.DataTx("")
if err != nil {
return nil, errors.Wrapf(err, "failed creating data tx")
}
query, err := s.s.Query()
if err != nil {
return nil, errors.Wrapf(err, "failed creating query")
}
return &SessionQueryExecutor{dataTx: dataTx, db: db, query: query}, nil
}
func (s *Session) Ledger() (*Ledger, error) {
ledger, err := s.s.Ledger()
if err != nil {
return nil, errors.Wrapf(err, "failed to get the ledger")
}
return &Ledger{l: ledger}, nil
}
// SessionManager is a session manager that allows the developer to access orion directly
type SessionManager struct {
sm driver.SessionManager
}
// NewSession creates a new session to orion using the passed identity
func (sm *SessionManager) NewSession(id string) (*Session, error) {
s, err := sm.sm.NewSession(id)
if err != nil {
return nil, err
}
return &Session{s: s}, nil
}