-
Notifications
You must be signed in to change notification settings - Fork 0
/
uniqueIndex.go
174 lines (158 loc) · 4.35 KB
/
uniqueIndex.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package bolt
import (
"bytes"
"errors"
"github.com/boltdb/bolt"
"github.com/sp0x/torrentd/storage/indexing"
)
type IndexDoesNotExistAndNotWritable struct{}
func (e *IndexDoesNotExistAndNotWritable) Error() string {
return "index does not exist and couln't be created"
}
//NewUniqueIndex creates a new unique index bucket
func NewUniqueIndex(parentBucket *bolt.Bucket, name []byte) (*UniqueIndex, error) {
var err error
if parentBucket == nil {
return nil, errors.New("parent bucket is required")
}
bucket := parentBucket.Bucket(name)
if bucket == nil {
if !parentBucket.Writable() {
return nil, &IndexDoesNotExistAndNotWritable{}
}
bucket, err = parentBucket.CreateBucket(name)
if err != nil {
return nil, err
}
}
return &UniqueIndex{
IndexBucket: bucket,
ParentBucket: parentBucket,
}, nil
}
//UniqueIndex ensures the indexed values are all unique.
type UniqueIndex struct {
ParentBucket *bolt.Bucket
IndexBucket *bolt.Bucket
}
//Add a value to the unique index. We're using the index value as a keyParts
//and we're storing the id in there.
func (ix *UniqueIndex) Add(indexValue []byte, id []byte) error {
if indexValue == nil {
return errors.New("indexValue is required")
}
if id == nil {
return errors.New("id is required")
}
existingIndex := ix.IndexBucket.Get(indexValue)
if existingIndex != nil {
if bytes.Equal(existingIndex, id) {
return nil
}
return errors.New("index indexValue already exists")
}
return ix.IndexBucket.Put(indexValue, id)
}
//Remove a index value from the unique index.
func (ix *UniqueIndex) Remove(indexValue []byte) error {
return ix.IndexBucket.Delete(indexValue)
}
//RemoveById removes the first id from the index that matches the given id.
func (ix *UniqueIndex) RemoveById(id []byte) error {
cursor := ix.IndexBucket.Cursor()
for value, otherId := cursor.First(); value != nil; value, otherId = cursor.Next() {
if bytes.Equal(otherId, id) {
return ix.Remove(value)
}
}
return nil
}
//Get the id behind an indexed value.
func (ix *UniqueIndex) Get(indexValue []byte) []byte {
return ix.IndexBucket.Get(indexValue)
}
//All returns all the IDs corresponding to the given index value.
//For unique indexes this should be a single ID.
func (ix *UniqueIndex) All(indexValue []byte, _ *indexing.CursorOptions) [][]byte {
id := ix.IndexBucket.Get(indexValue)
if id != nil {
return [][]byte{id}
}
return nil
}
//AllRecords returns all the IDs.
func (ix *UniqueIndex) AllRecords(ops *indexing.CursorOptions) [][]byte {
shouldReverse := ops != nil && ops.Reverse
c := &ReversibleCursor{
C: ix.IndexBucket.Cursor(),
Reverse: shouldReverse,
}
return scanCursor(c, ops)
}
//Range gets the IDs in the given range.
func (ix *UniqueIndex) Range(min []byte, max []byte, ops *indexing.CursorOptions) [][]byte {
shouldReverse := ops != nil && ops.Reverse
c := &RangeCursor{
C: ix.IndexBucket.Cursor(),
Reverse: shouldReverse,
Min: min,
Max: max,
Comparator: func(val, limit []byte) int {
return bytes.Compare(val, limit)
},
}
return scanCursor(c, ops)
}
//AllWithPrefix finds all the IDs that are prefixed with a given byte array
func (ix *UniqueIndex) AllWithPrefix(prefix []byte, ops *indexing.CursorOptions) [][]byte {
c := &PrefixCursor{
C: ix.IndexBucket.Cursor(),
Reverse: ops != nil && ops.Reverse,
Prefix: prefix,
}
return scanCursor(c, ops)
}
func (ix *UniqueIndex) IsEmpty() bool {
c := &ReversibleCursor{
C: ix.IndexBucket.Cursor(),
Reverse: false,
}
val, _ := c.First()
return val != nil
}
func (ix *UniqueIndex) GoOverCursor(action func(id []byte), opts *indexing.CursorOptions) {
c := &ReversibleCursor{
C: ix.IndexBucket.Cursor(),
Reverse: opts != nil && opts.Reverse,
}
for value, id := c.First(); value != nil; value, id = c.Next() {
if opts != nil && opts.Skip > 0 {
opts.Skip--
continue
}
if opts != nil && opts.Limit == 0 {
break
}
if opts != nil && opts.Limit > 0 {
opts.Limit--
}
action(id)
}
}
func scanCursor(c indexing.Cursor, ops *indexing.CursorOptions) [][]byte {
var results [][]byte
for value, id := c.First(); value != nil && c.CanContinue(value); value, id = c.Next() {
if ops != nil && ops.Skip > 0 {
ops.Skip--
continue
}
if ops != nil && ops.Limit == 0 {
break
}
if ops != nil && ops.Limit > 0 {
ops.Limit--
}
results = append(results, id)
}
return results
}