mirror of https://github.com/etcd-io/bbolt.git
Intermediate commit.
parent
153372abd4
commit
bce3e667df
34
README.md
34
README.md
|
@ -1,4 +1,38 @@
|
||||||
bolt
|
bolt
|
||||||
====
|
====
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
A low-level key/value database for Go.
|
A low-level key/value database for Go.
|
||||||
|
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### DB
|
||||||
|
|
||||||
|
### Creating a database
|
||||||
|
|
||||||
|
```
|
||||||
|
db := DB()
|
||||||
|
err := db.Open("/path/to/db", 0666)
|
||||||
|
...
|
||||||
|
err := db.Close()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Creating a bucket
|
||||||
|
|
||||||
|
|
||||||
|
* Cursor
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
DB
|
||||||
|
Bucket
|
||||||
|
Transaction / RWTransaction
|
||||||
|
Cursor / RWCursor
|
||||||
|
|
||||||
|
page
|
||||||
|
meta
|
||||||
|
branchNode
|
||||||
|
leafNode
|
||||||
|
```
|
||||||
|
|
|
@ -10,86 +10,7 @@ const (
|
||||||
dupNode = 0x04
|
dupNode = 0x04
|
||||||
)
|
)
|
||||||
|
|
||||||
// branchNode represents a node on a branch page.
|
|
||||||
type branchNode struct {
|
|
||||||
pgno uint32
|
|
||||||
flags uint16
|
|
||||||
keySize uint16
|
|
||||||
data uintptr // Pointer to the beginning of the data.
|
|
||||||
}
|
|
||||||
|
|
||||||
// key returns a byte slice that of the key data.
|
// key returns a byte slice that of the key data.
|
||||||
func (n *branchNode) key() []byte {
|
func (n *branchNode) key() []byte {
|
||||||
return (*[MaxKeySize]byte)(unsafe.Pointer(&n.data))[:n.keySize]
|
return (*[MaxKeySize]byte)(unsafe.Pointer(&n.data))[:n.keySize]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *branchNode) size() int {
|
|
||||||
return 0 // TODO: offsetof(MDB_node, mn_data)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: #define INDXSIZE(k) (NODESIZE + ((k) == NULL ? 0 : (k)->mv_size))
|
|
||||||
// TODO: #define LEAFSIZE(k, d) (NODESIZE + (k)->mv_size + (d)->mv_size)
|
|
||||||
// TODO: #define NODEPTR(p, i) ((MDB_node *)((char *)(p) + (p)->mp_ptrs[i]))
|
|
||||||
// TODO: #define NODEKEY(node) (void *)((node)->mn_data)
|
|
||||||
// TODO: #define NODEDATA(node) (void *)((char *)(node)->mn_data + (node)->mn_ksize)
|
|
||||||
// TODO: #define NODEPGNO(node) ((node)->mn_lo | ((pgno_t) (node)->mn_hi << 16) | (PGNO_TOPWORD ? ((pgno_t) (node)->mn_flags << PGNO_TOPWORD) : 0))
|
|
||||||
// TODO: #define SETPGNO(node,pgno) do { (node)->mn_lo = (pgno) & 0xffff; (node)->mn_hi = (pgno) >> 16; if (PGNO_TOPWORD) (node)->mn_flags = (pgno) >> PGNO_TOPWORD; } while(0)
|
|
||||||
// TODO: #define NODEDSZ(node) ((node)->mn_lo | ((unsigned)(node)->mn_hi << 16))
|
|
||||||
// TODO: #define SETDSZ(node,size) do { (node)->mn_lo = (size) & 0xffff; (node)->mn_hi = (size) >> 16;} while(0)
|
|
||||||
// TODO: #define NODEKSZ(node) ((node)->mn_ksize)
|
|
||||||
|
|
||||||
// TODO: #define LEAF2KEY(p, i, ks) ((char *)(p) + PAGEHDRSZ + ((i)*(ks)))
|
|
||||||
|
|
||||||
// TODO: #define MDB_GET_KEY(node, keyptr) { if ((keyptr) != NULL) { (keyptr)->mv_size = NODEKSZ(node); (keyptr)->mv_data = NODEKEY(node); } }
|
|
||||||
// TODO: #define MDB_GET_KEY2(node, key) { key.mv_size = NODEKSZ(node); key.mv_data = NODEKEY(node); }
|
|
||||||
|
|
||||||
// Compact the main page after deleting a node on a subpage.
|
|
||||||
// @param[in] mp The main page to operate on.
|
|
||||||
// @param[in] indx The index of the subpage on the main page.
|
|
||||||
func (n *node) shrink(index int) {
|
|
||||||
/*
|
|
||||||
MDB_node *node;
|
|
||||||
MDB_page *sp, *xp;
|
|
||||||
char *base;
|
|
||||||
int nsize, delta;
|
|
||||||
indx_t i, numkeys, ptr;
|
|
||||||
|
|
||||||
node = NODEPTR(mp, indx);
|
|
||||||
sp = (MDB_page *)NODEDATA(node);
|
|
||||||
delta = SIZELEFT(sp);
|
|
||||||
xp = (MDB_page *)((char *)sp + delta);
|
|
||||||
|
|
||||||
// shift subpage upward
|
|
||||||
if (IS_LEAF2(sp)) {
|
|
||||||
nsize = NUMKEYS(sp) * sp->mp_pad;
|
|
||||||
if (nsize & 1)
|
|
||||||
return; // do not make the node uneven-sized
|
|
||||||
memmove(METADATA(xp), METADATA(sp), nsize);
|
|
||||||
} else {
|
|
||||||
int i;
|
|
||||||
numkeys = NUMKEYS(sp);
|
|
||||||
for (i=numkeys-1; i>=0; i--)
|
|
||||||
xp->mp_ptrs[i] = sp->mp_ptrs[i] - delta;
|
|
||||||
}
|
|
||||||
xp->mp_upper = sp->mp_lower;
|
|
||||||
xp->mp_lower = sp->mp_lower;
|
|
||||||
xp->mp_flags = sp->mp_flags;
|
|
||||||
xp->mp_pad = sp->mp_pad;
|
|
||||||
COPY_PGNO(xp->mp_pgno, mp->mp_pgno);
|
|
||||||
|
|
||||||
nsize = NODEDSZ(node) - delta;
|
|
||||||
SETDSZ(node, nsize);
|
|
||||||
|
|
||||||
// shift lower nodes upward
|
|
||||||
ptr = mp->mp_ptrs[indx];
|
|
||||||
numkeys = NUMKEYS(mp);
|
|
||||||
for (i = 0; i < numkeys; i++) {
|
|
||||||
if (mp->mp_ptrs[i] <= ptr)
|
|
||||||
mp->mp_ptrs[i] += delta;
|
|
||||||
}
|
|
||||||
|
|
||||||
base = (char *)mp + mp->mp_upper;
|
|
||||||
memmove(base + delta, base, ptr - mp->mp_upper + NODESIZE + NODEKSZ(node));
|
|
||||||
mp->mp_upper += delta;
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
|
@ -29,4 +29,3 @@ type bucket struct {
|
||||||
entries uint64
|
entries uint64
|
||||||
root pgno
|
root pgno
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
40
cursor.go
40
cursor.go
|
@ -5,31 +5,19 @@ package bolt
|
||||||
const (
|
const (
|
||||||
c_initialized = 0x01 /**< cursor has been initialized and is valid */
|
c_initialized = 0x01 /**< cursor has been initialized and is valid */
|
||||||
c_eof = 0x02 /**< No more data */
|
c_eof = 0x02 /**< No more data */
|
||||||
c_sub = 0x04 /**< Cursor is a sub-cursor */
|
|
||||||
c_del = 0x08 /**< last op was a cursor_del */
|
c_del = 0x08 /**< last op was a cursor_del */
|
||||||
c_splitting = 0x20 /**< Cursor is in page_split */
|
c_splitting = 0x20 /**< Cursor is in page_split */
|
||||||
c_untrack = 0x40 /**< Un-track cursor when closing */
|
c_untrack = 0x40 /**< Un-track cursor when closing */
|
||||||
)
|
)
|
||||||
|
|
||||||
// TODO: #define MDB_NOSPILL 0x8000 /** Do not spill pages to disk if txn is getting full, may fail instead */
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
type Cursor interface {
|
type Cursor interface {
|
||||||
First() error
|
First() error
|
||||||
FirstDup() error
|
Last() error
|
||||||
Get() ([]byte, []byte, error)
|
|
||||||
GetRange() ([]byte, []byte, error)
|
|
||||||
Current() ([]byte, []byte, error)
|
|
||||||
Last()
|
|
||||||
LastDup()
|
|
||||||
Next() ([]byte, []byte, error)
|
Next() ([]byte, []byte, error)
|
||||||
NextDup() ([]byte, []byte, error)
|
|
||||||
NextNoDup() ([]byte, []byte, error)
|
|
||||||
Prev() ([]byte, []byte, error)
|
Prev() ([]byte, []byte, error)
|
||||||
PrevDup() ([]byte, []byte, error)
|
Current() ([]byte, []byte, error)
|
||||||
PrevNoDup() ([]byte, []byte, error)
|
Get([]byte) ([]byte, error)
|
||||||
Set() ([]byte, []byte, error)
|
|
||||||
SetRange() ([]byte, []byte, error)
|
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@ -37,10 +25,8 @@ type Cursor struct {
|
||||||
flags int
|
flags int
|
||||||
next *Cursor
|
next *Cursor
|
||||||
backup *Cursor
|
backup *Cursor
|
||||||
subcursor *Cursor
|
|
||||||
transaction *Transaction
|
transaction *Transaction
|
||||||
bucket *Bucket
|
bucket *Bucket
|
||||||
subbucket *Bucket
|
|
||||||
top int
|
top int
|
||||||
pages []*page
|
pages []*page
|
||||||
indices []int /* the index of the node for the page at the same level */
|
indices []int /* the index of the node for the page at the same level */
|
||||||
|
@ -53,12 +39,6 @@ func (c *Cursor) Get(key []byte) ([]byte, error) {
|
||||||
int exact = 0;
|
int exact = 0;
|
||||||
int (*mfunc)(MDB_cursor *mc, MDB_val *key, MDB_val *data);
|
int (*mfunc)(MDB_cursor *mc, MDB_val *key, MDB_val *data);
|
||||||
|
|
||||||
if (mc == NULL)
|
|
||||||
return EINVAL;
|
|
||||||
|
|
||||||
if (mc->mc_txn->mt_flags & MDB_TXN_ERROR)
|
|
||||||
return MDB_BAD_TXN;
|
|
||||||
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case MDB_GET_CURRENT:
|
case MDB_GET_CURRENT:
|
||||||
if (!(mc->mc_flags & C_INITIALIZED)) {
|
if (!(mc->mc_flags & C_INITIALIZED)) {
|
||||||
|
@ -222,14 +202,14 @@ func (c *Cursor) page(key []byte, flags int) (*page, error) {
|
||||||
if (flags & ps_first) != 0 {
|
if (flags & ps_first) != 0 {
|
||||||
index = 0
|
index = 0
|
||||||
} else if (flags & ps_last) != 0 {
|
} else if (flags & ps_last) != 0 {
|
||||||
index = indx(p.numkeys()) - 1;
|
index = indx(p.numkeys()) - 1
|
||||||
} else {
|
} else {
|
||||||
node, i, exact := p.find(key, c.transaction.db.pageSize);
|
node, i, exact := p.find(key, c.transaction.db.pageSize)
|
||||||
if exact {
|
if exact {
|
||||||
c.indices[c.top] = i
|
c.indices[c.top] = i
|
||||||
}
|
}
|
||||||
if node == nil {
|
if node == nil {
|
||||||
index = indx(p.numkeys()) - 1;
|
index = indx(p.numkeys()) - 1
|
||||||
} else {
|
} else {
|
||||||
index = indx(c.indices[c.top])
|
index = indx(c.indices[c.top])
|
||||||
if !exact {
|
if !exact {
|
||||||
|
@ -279,8 +259,8 @@ func (c *Cursor) push(p *page) {
|
||||||
c.top = len(c.pages) - 1
|
c.top = len(c.pages) - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// page retrieves the last page on the page stack.
|
// currentPage retrieves the last page on the page stack.
|
||||||
func (c *Cursor) page() *page {
|
func (c *Cursor) currentPage() *page {
|
||||||
top := len(c.pages)
|
top := len(c.pages)
|
||||||
if top > 0 {
|
if top > 0 {
|
||||||
return c.pages[top]
|
return c.pages[top]
|
||||||
|
@ -306,7 +286,6 @@ func (c *Cursor) currentLeafNode() *node {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// //
|
// //
|
||||||
// //
|
// //
|
||||||
// //
|
// //
|
||||||
|
@ -642,9 +621,6 @@ func (c *Cursor) page_touch() int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Search for the lowest key under the current branch page.
|
// Search for the lowest key under the current branch page.
|
||||||
// This just bypasses a NUMKEYS check in the current page
|
// This just bypasses a NUMKEYS check in the current page
|
||||||
// before calling mdb_page_search_root(), because the callers
|
// before calling mdb_page_search_root(), because the callers
|
||||||
|
|
|
@ -4,14 +4,26 @@ import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
// leafNode represents a node on a leaf page.
|
// node represents a node on a page.
|
||||||
type leafNode struct {
|
type node struct {
|
||||||
flags uint16
|
flags uint16
|
||||||
keySize uint16
|
keySize uint16
|
||||||
|
}
|
||||||
|
|
||||||
|
// leafNode represents a node on a leaf page.
|
||||||
|
type leafNode struct {
|
||||||
|
node
|
||||||
dataSize uint32
|
dataSize uint32
|
||||||
data uintptr // Pointer to the beginning of the data.
|
data uintptr // Pointer to the beginning of the data.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// branchNode represents a node on a branch page.
|
||||||
|
type branchNode struct {
|
||||||
|
node
|
||||||
|
pgno uint32
|
||||||
|
data uintptr // Pointer to the beginning of the data.
|
||||||
|
}
|
||||||
|
|
||||||
// key returns a byte slice that of the key data.
|
// key returns a byte slice that of the key data.
|
||||||
func (n *leafNode) key() []byte {
|
func (n *leafNode) key() []byte {
|
||||||
return (*[MaxKeySize]byte)(unsafe.Pointer(&n.data))[:n.keySize]
|
return (*[MaxKeySize]byte)(unsafe.Pointer(&n.data))[:n.keySize]
|
5
page.go
5
page.go
|
@ -21,7 +21,6 @@ const (
|
||||||
p_overflow = 0x04
|
p_overflow = 0x04
|
||||||
p_meta = 0x08
|
p_meta = 0x08
|
||||||
p_dirty = 0x10 /**< dirty page, also set for #P_SUBP pages */
|
p_dirty = 0x10 /**< dirty page, also set for #P_SUBP pages */
|
||||||
p_sub = 0x40
|
|
||||||
p_keep = 0x8000 /**< leave this page alone during spill */
|
p_keep = 0x8000 /**< leave this page alone during spill */
|
||||||
|
|
||||||
p_invalid = ^pgno(0)
|
p_invalid = ^pgno(0)
|
||||||
|
@ -124,13 +123,13 @@ func (p *page) find(key []byte, pageSize int) (*node, int, bool) {
|
||||||
|
|
||||||
// Perform a binary search to find the correct node.
|
// Perform a binary search to find the correct node.
|
||||||
var i, rc int
|
var i, rc int
|
||||||
for ; low <= high; {
|
for low <= high {
|
||||||
i = (low + high) / 2
|
i = (low + high) / 2
|
||||||
|
|
||||||
node = p.node(indx(i))
|
node = p.node(indx(i))
|
||||||
rc = bytes.Compare(key, node.key())
|
rc = bytes.Compare(key, node.key())
|
||||||
if rc == 0 {
|
if rc == 0 {
|
||||||
break;
|
break
|
||||||
} else if rc > 0 {
|
} else if rc > 0 {
|
||||||
low = i + 1
|
low = i + 1
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -1,5 +1,12 @@
|
||||||
package bolt
|
package bolt
|
||||||
|
|
||||||
|
/*
|
||||||
|
type RWCursor interface {
|
||||||
|
Put([]byte, []byte) (error)
|
||||||
|
Delete([]byte) (error)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// RWCursor represents a cursor that can read and write data for a bucket.
|
// RWCursor represents a cursor that can read and write data for a bucket.
|
||||||
type RWCursor struct {
|
type RWCursor struct {
|
||||||
Cursor
|
Cursor
|
||||||
|
|
|
@ -4,6 +4,4 @@ package bolt
|
||||||
// Only one read/write transaction can be active for a DB at a time.
|
// Only one read/write transaction can be active for a DB at a time.
|
||||||
type RWTransaction struct {
|
type RWTransaction struct {
|
||||||
Transaction
|
Transaction
|
||||||
pagestate pagestate
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue