pull/34/head
Steven Normore 2014-04-16 15:00:26 +00:00
parent b178373351
commit 32937280c3
3 changed files with 103 additions and 58 deletions

View File

@ -3,40 +3,41 @@ package c
/* /*
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <inttypes.h>
#define MAX_DEPTH 100 #define MAX_DEPTH 100
#define BRANCH_PAGE 1 #define BRANCH_PAGE 1
// These types MUST have the same layout as their corresponding Go types // These types MUST have the same layout as their corresponding Go types
typedef unsigned long long pgid; typedef int64_t pgid;
typedef unsigned short elemid;
typedef struct page { typedef struct page {
pgid id; pgid id;
unsigned short flags; uint16_t flags;
elemid count; uint16_t count;
unsigned long overflow; uint32_t overflow;
} page; } page;
typedef struct branch_elem { typedef struct branch_elem {
unsigned long pos; uint32_t pos;
unsigned long ksize; uint32_t ksize;
pgid page; pgid page;
} branch_elem; } branch_elem;
typedef struct leaf_elem { typedef struct leaf_elem {
unsigned long flags; uint32_t flags;
unsigned long pos; uint32_t pos;
unsigned long ksize; uint32_t ksize;
unsigned long vsize; uint32_t vsize;
} leaf_elem; } leaf_elem;
// private types // private types
typedef struct elem_ref { typedef struct elem_ref {
page *page; page *page;
elemid index; uint16_t index;
} elem_ref; } elem_ref;
// public types // public types
@ -60,14 +61,17 @@ typedef struct bolt_cursor {
// private functions // private functions
page *get_page(bolt_cursor *c, pgid id) { page *get_page(bolt_cursor *c, pgid id) {
printf("get_page: c->data=%d, c->pgsz=%d, pgid=%d\n\n", c->data, c->pgsz, id);
return (page *)(c->data + (c->pgsz * id)); return (page *)(c->data + (c->pgsz * id));
} }
branch_elem *branch_page_element(page *p, elemid index) { branch_elem *branch_page_element(page *p, uint16_t index) {
return (branch_elem*)(p + sizeof(page) + index * sizeof(branch_elem)); return (branch_elem*)(p + sizeof(page) + index * sizeof(branch_elem));
} }
leaf_elem *leaf_page_element(page *p, elemid index) { leaf_elem *leaf_page_element(page *p, uint16_t index) {
printf("leaf_page_element: page=%d, index=%d, sizeof(page)=%d, sizeof(leaf_elem)=%d\n\n", p, index, sizeof(page), sizeof(leaf_elem));
printf("leaf_page_element: elem=%x\n", (leaf_elem*)(p + sizeof(page) + index * sizeof(leaf_elem))[0]);
return (leaf_elem*)(p + sizeof(page) + index * sizeof(leaf_elem)); return (leaf_elem*)(p + sizeof(page) + index * sizeof(leaf_elem));
} }
@ -75,16 +79,26 @@ leaf_elem *leaf_page_element(page *p, elemid index) {
// if stack points at a branch page descend down to the first elemenet // if stack points at a branch page descend down to the first elemenet
// of the first leaf page // of the first leaf page
int leaf_element(bolt_cursor *c, bolt_val *key, bolt_val *value) { int leaf_element(bolt_cursor *c, bolt_val *key, bolt_val *value) {
printf("leaf_element:1:\n\n");
elem_ref *ref = &(c->stack[c->stackp]); elem_ref *ref = &(c->stack[c->stackp]);
printf("leaf_element:2:, ref->page->flags=%d\n\n", ref->page->flags);
branch_elem *branch; branch_elem *branch;
while (ref->page->flags | BRANCH_PAGE) { while (ref->page->flags & BRANCH_PAGE) {
printf("leaf_element:2.1, ref->page->flags=%d\n\n", ref->page->flags);
branch = branch_page_element(ref->page,ref->index); branch = branch_page_element(ref->page,ref->index);
printf("leaf_element:2.2\n\n");
c->stackp++; c->stackp++;
//printf("leaf_element:2.3, c->stack=%d, c->stackp=%d\n\n", c->stack, c->stackp);
ref = &c->stack[c->stackp]; ref = &c->stack[c->stackp];
//printf("leaf_element:2.4, ref=%d\n\n", ref);
ref->index = 0; ref->index = 0;
printf("leaf_element:2.5\n\n");
ref->page = get_page(c, branch->page); ref->page = get_page(c, branch->page);
printf("leaf_element:2.6\n\n");
}; };
printf("leaf_element:3, key=%s, value=%s\n\n", key, value);
set_key_value(leaf_page_element(ref->page,ref->index), key, value); set_key_value(leaf_page_element(ref->page,ref->index), key, value);
printf("leaf_element:3, key=%s, value=%s\n\n", key, value);
return 0; return 0;
} }
@ -93,6 +107,7 @@ set_key_value(leaf_elem *leaf, bolt_val *key, bolt_val *value) {
key->data = leaf + leaf->pos; key->data = leaf + leaf->pos;
value->size = leaf->vsize; value->size = leaf->vsize;
value->data = key->data + key->size; value->data = key->data + key->size;
printf("set_key_value: key=%s (%d), value=%s (%d)\n\n", key->data, key->size, value->data, value->size);
} }
// public functions // public functions
@ -136,6 +151,7 @@ int bolt_cursor_next(bolt_cursor *c, bolt_val *key, bolt_val *value) {
import "C" import "C"
import ( import (
// "fmt"
"unsafe" "unsafe"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
@ -144,14 +160,18 @@ import (
type bolt_cursor *C.bolt_cursor type bolt_cursor *C.bolt_cursor
func NewCursor(b *bolt.Bucket) bolt_cursor { func NewCursor(b *bolt.Bucket) bolt_cursor {
data, pgsz := b.Tx().DB().RawData() info := b.Tx().DB().Info()
root := b.Root()
cursor := new(C.bolt_cursor) cursor := new(C.bolt_cursor)
C.bolt_cursor_init(cursor, unsafe.Pointer(&data[0]), (C.size_t)(pgsz), (C.pgid)(b.Root())) C.bolt_cursor_init(cursor, unsafe.Pointer(&info.Data[0]), (C.size_t)(info.PageSize), (C.pgid)(root))
return cursor return cursor
} }
func first(c bolt_cursor) (key, value []byte) { func first(c bolt_cursor) (key, value []byte) {
var k, v C.bolt_val var k, v C.bolt_val
// fmt.Println("cursor =", c)
// fmt.Println("key =", k)
// fmt.Println("value =", v)
C.bolt_cursor_first(c, &k, &v) C.bolt_cursor_first(c, &k, &v)
return C.GoBytes(k.data, C.int(k.size)), C.GoBytes(v.data, C.int(v.size)) return C.GoBytes(k.data, C.int(k.size)), C.GoBytes(v.data, C.int(v.size))
} }

View File

@ -4,9 +4,9 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"sort" // "sort"
"testing" "testing"
"testing/quick" // "testing/quick"
"github.com/boltdb/bolt" "github.com/boltdb/bolt"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -16,44 +16,64 @@ import (
// Implement seek; binary search within the page (branch page and element page) // Implement seek; binary search within the page (branch page and element page)
// Ensure that a cursor can iterate over all elements in a bucket. // Ensure that a cursor can iterate over all elements in a bucket.
func TestIterate(t *testing.T) { // func TestIterate(t *testing.T) {
if testing.Short() { // if testing.Short() {
t.Skip("skipping test in short mode.") // t.Skip("skipping test in short mode.")
} // }
f := func(items testdata) bool { // f := func(items testdata) bool {
withOpenDB(func(db *bolt.DB, path string) { // withOpenDB(func(db *bolt.DB, path string) {
// Bulk insert all values. // // Bulk insert all values.
tx, _ := db.Begin(true) // tx, _ := db.Begin(true)
tx.CreateBucket("widgets") // tx.CreateBucket("widgets")
b := tx.Bucket("widgets") // b := tx.Bucket("widgets")
for _, item := range items { // for _, item := range items {
assert.NoError(t, b.Put(item.Key, item.Value)) // assert.NoError(t, b.Put(item.Key, item.Value))
} // }
assert.NoError(t, tx.Commit()) // assert.NoError(t, tx.Commit())
// Sort test data. // // Sort test data.
sort.Sort(items) // sort.Sort(items)
// Iterate over all items and check consistency. // // Iterate over all items and check consistency.
var index = 0 // var index = 0
tx, _ = db.Begin(false) // tx, _ = db.Begin(false)
c := NewCursor(tx.Bucket("widgets")) // c := NewCursor(tx.Bucket("widgets"))
for key, value := first(c); key != nil && index < len(items); key, value = next(c) { // for key, value := first(c); key != nil && index < len(items); key, value = next(c) {
assert.Equal(t, key, items[index].Key) // assert.Equal(t, key, items[index].Key)
assert.Equal(t, value, items[index].Value) // assert.Equal(t, value, items[index].Value)
index++ // index++
} // }
assert.Equal(t, len(items), index) // assert.Equal(t, len(items), index)
assert.Equal(t, len(items), index) // assert.Equal(t, len(items), index)
tx.Rollback() // tx.Rollback()
}) // })
return true // return true
} // }
if err := quick.Check(f, qconfig()); err != nil { // if err := quick.Check(f, qconfig()); err != nil {
t.Error(err) // t.Error(err)
} // }
fmt.Fprint(os.Stderr, "\n") // fmt.Fprint(os.Stderr, "\n")
// }
func TestCursorFirst(t *testing.T) {
withOpenDB(func(db *bolt.DB, path string) {
// Bulk insert all values.
tx, _ := db.Begin(true)
b, _ := tx.CreateBucket([]byte("widgets"))
assert.NoError(t, b.Put([]byte("foo"), []byte("bar")))
assert.NoError(t, tx.Commit())
// Get first and check consistency
tx, _ = db.Begin(false)
c := NewCursor(tx.Bucket([]byte("widgets")))
key, value := first(c)
assert.Equal(t, key, []byte("foo"))
assert.Equal(t, value, []byte("bar"))
tx.Rollback()
})
} }
// withTempPath executes a function with a database reference. // withTempPath executes a function with a database reference.

9
db.go
View File

@ -580,8 +580,8 @@ func (db *DB) checkBucket(b *Bucket, reachable map[pgid]*page, errors *ErrorList
// This is for internal access to the raw data bytes from the C cursor, use // This is for internal access to the raw data bytes from the C cursor, use
// carefully, or not at all. // carefully, or not at all.
func (db *DB) RawData() ([]byte, int) { func (db *DB) Info() *Info {
return db.data, db.pageSize return &Info{db.data, db.pageSize}
} }
// page retrieves a page reference from the mmap based on the current page size. // page retrieves a page reference from the mmap based on the current page size.
@ -647,3 +647,8 @@ func (s *Stats) Sub(other *Stats) Stats {
func (s *Stats) add(other *Stats) { func (s *Stats) add(other *Stats) {
s.TxStats.add(&other.TxStats) s.TxStats.add(&other.TxStats)
} }
type Info struct {
Data []byte
PageSize int
}