-
Notifications
You must be signed in to change notification settings - Fork 19
/
buckets.go
110 lines (85 loc) · 2.49 KB
/
buckets.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
package db
import (
"strings"
"github.com/ShoshinNikita/bolt"
)
// GetRoot returns records from root of db
func (db *BoltAPI) GetRoot() (data Data, err error) {
err = db.db.View(func(tx *bolt.Tx) error {
c := tx.Cursor()
data.Records = db.getRecords(c)
return nil
})
data.PrevBucket = false
data.PrevRecords = false
data.NextRecords = (db.recordsAmount > maxOffset)
data.Path = "/"
data.RecordsAmount = db.recordsAmount
return data, err
}
// GetCurrent returns records from current bucket
func (db *BoltAPI) GetCurrent() (data Data, err error) {
if len(db.currentBucket) == 0 {
return db.GetRoot()
}
err = db.db.View(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
c := b.Cursor()
data.Records = db.getRecords(c)
return nil
})
data.PrevBucket = true
data.PrevRecords = (db.pages.top() > 1)
data.NextRecords = (db.recordsAmount > maxOffset*db.pages.top())
data.Path = "/" + strings.Join(db.currentBucket, "/")
data.RecordsAmount = db.recordsAmount
return data, err
}
// Back return records from previous bucket
func (db *BoltAPI) Back() (data Data, err error) {
db.currentBucket = db.currentBucket[:len(db.currentBucket)-1]
db.pages.del()
if len(db.currentBucket) == 0 {
return db.GetRoot()
}
err = db.db.View(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
c := b.Cursor()
data.Records = db.getRecords(c)
return nil
})
data.PrevBucket = true // if there is no previous bucket will be called GetRoot()
data.PrevRecords = (db.pages.top() > 1)
data.NextRecords = (db.recordsAmount > maxOffset*db.pages.top())
data.Path = "/" + strings.Join(db.currentBucket, "/")
data.RecordsAmount = db.recordsAmount
return data, err
}
// Next return records from next bucket with according name
func (db *BoltAPI) Next(name string) (data Data, err error) {
db.currentBucket = append(db.currentBucket, name)
db.pages.add()
err = db.db.View(func(tx *bolt.Tx) error {
b := db.getCurrentBucket(tx)
c := b.Cursor()
data.Records = db.getRecords(c)
return nil
})
data.PrevBucket = true
data.PrevRecords = false
data.NextRecords = (db.recordsAmount > maxOffset)
data.Path = "/" + strings.Join(db.currentBucket, "/")
data.RecordsAmount = db.recordsAmount
return data, err
}
func (db *BoltAPI) getCurrentBucket(tx *bolt.Tx) (b *bolt.Bucket) {
if len(db.currentBucket) == 0 {
b = tx.Root()
} else {
b = tx.Bucket([]byte(db.currentBucket[0]))
for i := 1; i < len(db.currentBucket); i++ {
b = b.Bucket([]byte(db.currentBucket[i]))
}
}
return b
}