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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
package bolt
import (
"sort"
"unsafe"
)
// RWTransaction represents a transaction that can read and write data.
// Only one read/write transaction can be active for a DB at a time.
type RWTransaction struct {
Transaction
nodes map[pgid]*node
}
// init initializes the transaction.
func (t *RWTransaction) init(db *DB) {
t.Transaction.init(db)
t.pages = make(map[pgid]*page)
// Copy the meta and increase the transaction id.
t.meta = &meta{}
db.meta().copy(t.meta)
t.meta.txnid += txnid(1)
}
// CreateBucket creates a new bucket.
func (t *RWTransaction) CreateBucket(name string) error {
// Check if bucket already exists.
if b := t.Bucket(name); b != nil {
return &Error{"bucket already exists", nil}
} else if len(name) == 0 {
return &Error{"bucket name cannot be blank", nil}
} else if len(name) > MaxBucketNameSize {
return &Error{"bucket name too long", nil}
}
// Create a blank root leaf page.
p := t.allocate(1)
p.flags = p_leaf
// Add bucket to system page.
t.sys.put(name, &bucket{root: p.id})
return nil
}
// DropBucket deletes a bucket.
func (t *RWTransaction) DeleteBucket(name string) error {
// Remove from system page.
t.sys.del(name)
// TODO: Delete entry from system bucket.
// TODO: Free all pages.
// TODO: Remove cursor.
return nil
}
func (t *RWTransaction) Put(name string, key []byte, value []byte) error {
b := t.Bucket(name)
if b == nil {
return &Error{"bucket not found", nil}
}
// Validate the key and data size.
if len(key) == 0 {
return &Error{"key required", nil}
} else if len(key) > MaxKeySize {
return &Error{"key too large", nil}
} else if len(value) > MaxDataSize {
return &Error{"data too large", nil}
}
// Insert a new node.
c := b.cursor()
c.Get(key)
t.node(c.stack).put(key, key, value, 0)
return nil
}
func (t *RWTransaction) Delete(name string, key []byte) error {
// TODO: Traverse to the correct node.
// TODO: If missing, exit.
// TODO: Remove node from page.
// TODO: If page is empty then add it to the freelist.
return nil
}
// Commit writes all changes to disk.
func (t *RWTransaction) Commit() error {
// TODO(benbjohnson): Use vectorized I/O to write out dirty pages.
// TODO: Rebalance.
// Spill data onto dirty pages.
t.spill()
// Spill system page.
p := t.allocate((t.sys.size() / t.db.pageSize) + 1)
t.sys.write(p)
// Write dirty pages to disk.
if err := t.write(); err != nil {
return err
}
// Update the meta.
t.meta.sys = p.id
// Write meta to disk.
if err := t.writeMeta(); err != nil {
return err
}
return nil
}
func (t *RWTransaction) Rollback() {
t.close()
}
func (t *RWTransaction) close() {
// Clear nodes.
t.nodes = nil
// TODO: Release writer lock.
}
// allocate returns a contiguous block of memory starting at a given page.
func (t *RWTransaction) allocate(count int) *page {
// TODO(benbjohnson): Use pages from the freelist.
// Allocate a set of contiguous pages from the end of the file.
buf := make([]byte, count*t.db.pageSize)
p := (*page)(unsafe.Pointer(&buf[0]))
p.id = t.meta.pgid
p.overflow = uint32(count - 1)
// Increment the last page id.
t.meta.pgid += pgid(count)
// Save it in our page cache.
t.pages[p.id] = p
return p
}
// spill writes all the nodes to dirty pages.
func (t *RWTransaction) spill() {
// Keep track of the current root nodes.
// We will update this at the end once all nodes are created.
type root struct {
node *node
pgid pgid
}
var roots []root
// Sort nodes by highest depth first.
nodes := make(nodesByDepth, 0, len(t.nodes))
for _, n := range t.nodes {
nodes = append(nodes, n)
}
sort.Sort(nodes)
// Spill nodes by deepest first.
for i := 0; i < len(nodes); i++ {
n := nodes[i]
// Save existing root buckets for later.
if n.parent == nil && n.pgid != 0 {
roots = append(roots, root{n, n.pgid})
}
// Split nodes and write them.
newNodes := n.split(t.db.pageSize)
// If this is a root node that split then create a parent node.
if n.parent == nil && len(newNodes) > 1 {
n.parent = &node{
isLeaf: false,
key: newNodes[0].inodes[0].key,
depth: n.depth - 1,
inodes: make(inodes, 0),
}
nodes = append(nodes, n.parent)
sort.Sort(nodes)
}
// Write nodes to dirty pages.
for i, newNode := range newNodes {
// Allocate contiguous space for the node.
p := t.allocate((newNode.size() / t.db.pageSize) + 1)
// Write the node to the page.
newNode.write(p)
newNode.pgid = p.id
newNode.parent = n.parent
// The first node should use the existing entry, other nodes are inserts.
var oldKey []byte
if i == 0 {
oldKey = n.key
} else {
oldKey = newNode.inodes[0].key
}
// Update the parent entry.
if newNode.parent != nil {
newNode.parent.put(oldKey, newNode.inodes[0].key, nil, newNode.pgid)
}
}
}
// Update roots with new roots.
for _, root := range roots {
for _, b := range t.sys.buckets {
if b.root == root.pgid {
b.root = root.node.root().pgid
break
}
}
}
}
// write writes any dirty pages to disk.
func (t *RWTransaction) write() error {
// TODO(benbjohnson): If our last page id is greater than the mmap size then lock the DB and resize.
// Sort pages by id.
pages := make(pages, 0, len(t.pages))
for _, p := range t.pages {
pages = append(pages, p)
}
sort.Sort(pages)
// Write pages to disk in order.
for _, p := range pages {
size := (int(p.overflow) + 1) * t.db.pageSize
buf := (*[maxAllocSize]byte)(unsafe.Pointer(p))[:size]
t.db.file.WriteAt(buf, int64(p.id)*int64(t.db.pageSize))
}
return nil
}
// writeMeta writes the meta to the disk.
func (t *RWTransaction) writeMeta() error {
// Create a temporary buffer for the meta page.
buf := make([]byte, t.db.pageSize)
p := t.db.pageInBuffer(buf, 0)
t.meta.write(p)
// Write the meta page to file.
t.db.metafile.WriteAt(buf, int64(p.id)*int64(t.db.pageSize))
return nil
}
// node retrieves a node based a cursor stack.
func (t *RWTransaction) node(stack []elem) *node {
if len(stack) == 0 {
return nil
}
// Retrieve branch if it has already been fetched.
e := &stack[len(stack)-1]
id := e.page.id
if n := t.nodes[id]; n != nil {
return n
}
// Otherwise create a branch and cache it.
n := &node{}
n.read(t.page(id))
n.depth = len(stack) - 1
n.parent = t.node(stack[:len(stack)-1])
t.nodes[id] = n
return n
}
|