-
Notifications
You must be signed in to change notification settings - Fork 4
/
view.go
86 lines (71 loc) · 1.99 KB
/
view.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
package bubt
import "github.com/bnclabs/gostore/api"
// View read only transaction instance.
type View struct {
id uint64
snap *Snapshot
cursors []*Cursor
}
//---- Exported Control methods
// ID return view-transaction id.
func (view *View) ID() uint64 {
return view.id
}
// OpenCursor open an active cursor, point at key, inside the index.
func (view *View) OpenCursor(key []byte) (api.Cursor, error) {
snap := view.snap
msize, zsize, vsize := snap.mblocksize, snap.zblocksize, snap.vblocksize
buf := snap.rdpool.getreadbuffer(msize, zsize, vsize)
cur, err := view.getcursor().opencursor(view.snap, key, buf)
return cur, err
}
// Set not allowed.
func (view *View) Set(key, value, oldvalue []byte) []byte {
panic("Set not allowed on view")
}
// Delete not allowed.
func (view *View) Delete(key, oldvalue []byte, lsm bool) []byte {
panic("Delete not allowed on view")
}
// Commit not allowed.
func (view *View) Commit() error {
panic("Commit not allowed on view")
}
// Abort view, must be called once done with the view.
func (view *View) Abort() {
view.snap.abortview(view)
}
//---- Exported Read methods
// Get value for key, if value argument is not nil it will be used to copy the
// entry's value. Also return whether entry is marked as deleted by LSM.
// If ok is false, then key is not found.
func (view *View) Get(
key, value []byte) (v []byte, cas uint64, deleted, ok bool) {
v, cas, deleted, ok = view.snap.Get(key, value)
return v, cas, deleted, ok
}
//---- local methods
func (view *View) getcursor() (cur *Cursor) {
select {
case cur = <-view.snap.curcache:
default:
cur = &Cursor{
snap: view.snap,
fposs: make([]int64, len(view.snap.readzs)),
}
}
cur.ynext = false
cur.index, cur.finished = 0, false
for i := range cur.fposs {
cur.fposs[i] = -1
}
view.cursors = append(view.cursors, cur)
return
}
func (view *View) putcursor(cur *Cursor) {
view.snap.rdpool.putreadbuffer(cur.buf)
select {
case view.snap.curcache <- cur:
default: // leave it for GC
}
}