forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btree.go
181 lines (164 loc) · 3.4 KB
/
btree.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
175
176
177
178
179
180
181
// Copyright 2017 The Cayley Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package btree
import (
"bytes"
"context"
"io"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/graph/kv"
)
func init() {
kv.Register(Type, kv.Registration{
NewFunc: Create,
InitFunc: Create,
IsPersistent: false,
})
}
const (
Type = "btree"
)
func Create(path string, _ graph.Options) (kv.BucketKV, error) {
return New(), nil
}
func New() *DB {
return &DB{m: make(map[string]*Tree)}
}
type DB struct {
m map[string]*Tree
}
func (db *DB) Type() string {
return Type
}
func (db *DB) Close() error {
return nil
}
func (db *DB) Tx(update bool) (kv.BucketTx, error) {
return &Tx{db: db, ro: !update}, nil
}
type Tx struct {
db *DB
ro bool
err error
}
func clone(p []byte) []byte {
if p == nil {
return nil
}
b := make([]byte, len(p))
copy(b, p)
return b
}
func (tx *Tx) Get(ctx context.Context, keys []kv.BucketKey) ([][]byte, error) {
vals := make([][]byte, len(keys))
for i, k := range keys {
if t := tx.db.m[string(k.Bucket)]; t != nil {
if v, ok := t.Get(k.Key); ok {
vals[i] = clone(v)
}
}
}
return vals, nil
}
func (tx *Tx) Commit(ctx context.Context) error {
return nil
}
func (tx *Tx) Rollback() error {
return nil
}
func (tx *Tx) Bucket(name []byte) kv.Bucket {
t := tx.db.m[string(name)]
if t == nil && tx.ro {
return &Bucket{err: kv.ErrNoBucket}
}
if t == nil {
t = TreeNew(bytes.Compare)
tx.db.m[string(name)] = t
}
return &Bucket{tree: t}
}
type Bucket struct {
tree *Tree
err error
}
func (b *Bucket) Get(ctx context.Context, keys [][]byte) ([][]byte, error) {
if b.err != nil {
return nil, b.err
} else if b.tree == nil {
return nil, kv.ErrNotFound
}
vals := make([][]byte, len(keys))
for i, k := range keys {
if v, ok := b.tree.Get(k); ok {
vals[i] = clone(v)
}
}
return vals, nil
}
func (b *Bucket) Put(k, v []byte) error {
if b.err != nil {
return b.err
}
b.tree.Set(clone(k), clone(v))
return nil
}
func (b *Bucket) Del(k []byte) error {
if b.err != nil {
return b.err
}
b.tree.Delete(k)
return nil
}
func (b *Bucket) Scan(pref []byte) kv.KVIterator {
return &Iterator{b: b, pref: pref}
}
type Iterator struct {
b *Bucket
pref []byte
e *Enumerator
k, v []byte
}
func (it *Iterator) Next(ctx context.Context) bool {
if it.b == nil {
return false
} else if it.b.err != nil {
return false
}
if it.e == nil {
it.e, _ = it.b.tree.Seek(it.pref)
}
k, v, err := it.e.Next()
if err == io.EOF {
return false
} else if !bytes.HasPrefix(k, it.pref) {
return false
}
it.k, it.v = k, v
return true
}
func (it *Iterator) Key() []byte { return it.k }
func (it *Iterator) Val() []byte { return it.v }
func (it *Iterator) Err() error {
if it.b == nil {
return nil
}
return it.b.err
}
func (it *Iterator) Close() error {
if it.e != nil {
it.e.Close()
it.e = nil
}
return it.Err()
}