Initial db.open.

master
Ben Johnson 2014-01-11 22:51:01 -07:00
parent df8333328f
commit ee24437bfc
12 changed files with 4872 additions and 4960 deletions

20
Makefile Normal file
View File

@ -0,0 +1,20 @@
PKG=./...
TEST=.
BENCH=.
COVERPROFILE=/tmp/c.out
bench: benchpreq
go test -v -test.bench=$(BENCH) ./.bench
cover: fmt
go test -coverprofile=$(COVERPROFILE) .
go tool cover -html=$(COVERPROFILE)
rm $(COVERPROFILE)
fmt:
@go fmt ./...
test: fmt
@go test -v -cover -test.run=$(TEST) $(PKG)
.PHONY: bench cover fmt test

153
cursor.go
View File

@ -11,7 +11,6 @@ package bolt
// TODO: #define MDB_NOSPILL 0x8000 /** Do not spill pages to disk if txn is getting full, may fail instead */
type Cursor interface {
First() error
FirstDup() error
@ -32,13 +31,13 @@ type Cursor interface {
type cursor struct {
flags int
next *cursor
_next *cursor
backup *cursor
xcursor *xcursor
transaction *transaction
bucketId int
bucket *bucket
bucketx *bucketx
bucket *Bucket
// bucketx *bucketx
bucketFlag int
snum int
top int
@ -48,8 +47,8 @@ type cursor struct {
type xcursor struct {
cursor cursor
bucket *bucket
bucketx *bucketx
bucket *Bucket
// bucketx *bucketx
bucketFlag int
}
@ -59,7 +58,7 @@ type xcursor struct {
// P_DIRTY to set P_KEEP, P_DIRTY|P_KEEP to clear it.
// @param[in] all No shortcuts. Needed except after a full #mdb_page_flush().
// @return 0 on success, non-zero on failure.
func (c *cursor) xkeep(unsigned pflags, int all) int {
func (c *cursor) xkeep(pflags int, all int) error {
/*
enum { Mask = P_SUBP|P_DIRTY|P_KEEP };
MDB_txn *txn = mc->mc_txn;
@ -116,7 +115,7 @@ func (c *cursor) xkeep(unsigned pflags, int all) int {
return rc;
*/
return 0
return nil
}
// Spill pages from the dirty list back to disk.
@ -150,7 +149,7 @@ func (c *cursor) xkeep(unsigned pflags, int all) int {
// @param[in] key For a put operation, the key being stored.
// @param[in] data For a put operation, the data being stored.
// @return 0 on success, non-zero on failure.
func (c *cursor) spill(MDB_val *key, MDB_val *data) int {
func (c *cursor) spill(key []byte, data []byte) error {
/*
MDB_txn *txn = m0->mc_txn;
MDB_page *dp;
@ -240,28 +239,28 @@ func (c *cursor) spill(MDB_val *key, MDB_val *data) int {
// Reset any dirty pages we kept that page_flush didn't see
rc = mdb_pages_xkeep(m0, P_DIRTY|P_KEEP, i);
done:
done:
txn->mt_flags |= rc ? MDB_TXN_ERROR : MDB_TXN_SPILLS;
return rc;
/*
return 0
}
}
// Allocate page numbers and memory for writing. Maintain me_pglast,
// me_pghead and mt_next_pgno.
//
// If there are free pages available from older transactions, they
// are re-used first. Otherwise allocate a new page at mt_next_pgno.
// Do not modify the freedB, just merge freeDB records into me_pghead[]
// and move me_pglast to say which records were consumed. Only this
// function can create me_pghead and move me_pglast/mt_next_pgno.
// @param[in] mc cursor A cursor handle identifying the transaction and
// database for which we are allocating.
// @param[in] num the number of pages to allocate.
// @param[out] mp Address of the allocated page(s). Requests for multiple pages
// will always be satisfied by a single contiguous chunk of memory.
// @return 0 on success, non-zero on failure.
func (c *cursor) allocPage(int num, MDB_page **mp) {
// Allocate page numbers and memory for writing. Maintain me_pglast,
// me_pghead and mt_next_pgno.
//
// If there are free pages available from older transactions, they
// are re-used first. Otherwise allocate a new page at mt_next_pgno.
// Do not modify the freedB, just merge freeDB records into me_pghead[]
// and move me_pglast to say which records were consumed. Only this
// function can create me_pghead and move me_pglast/mt_next_pgno.
// @param[in] mc cursor A cursor handle identifying the transaction and
// database for which we are allocating.
// @param[in] num the number of pages to allocate.
// @param[out] mp Address of the allocated page(s). Requests for multiple pages
// will always be satisfied by a single contiguous chunk of memory.
// @return 0 on success, non-zero on failure.
func (c *cursor) allocPage(int num, MDB_page **mp) {
int rc, retry = INT_MAX;
MDB_txn *txn = mc->mc_txn;
MDB_env *env = txn->mt_env;
@ -341,12 +340,12 @@ func (c *cursor) allocPage(int num, MDB_page **mp) {
mop = env->me_pghead;
}
env->me_pglast = last;
#if (MDB_DEBUG) > 1
#if (MDB_DEBUG) > 1
DPRINTF(("IDL read txn %"Z"u root %"Z"u num %u",
last, txn->mt_dbs[FREE_DBI].md_root, i));
for (k = i; k; k--)
DPRINTF(("IDL %"Z"u", idl[k]));
#endif
#endif
// Merge in descending sorted order
j = mop_len;
k = mop_len += i;
@ -370,7 +369,7 @@ func (c *cursor) allocPage(int num, MDB_page **mp) {
goto fail;
}
search_done:
search_done:
if (env->me_flags & MDB_WRITEMAP) {
np = (MDB_page *)(env->me_map + env->me_psize * pgno);
} else {
@ -393,11 +392,11 @@ search_done:
return MDB_SUCCESS;
fail:
fail:
txn->mt_flags |= MDB_TXN_ERROR;
return rc;
*/
return 0
return nil
}
// Copy the used portions of a non-overflow page.
@ -491,7 +490,7 @@ func (c *cursor) page_touch() int {
np->mp_pgno = pgno;
np->mp_flags |= P_DIRTY;
done:
done:
// Adjust cursors pointing to mp
mc->mc_pg[mc->mc_top] = np;
m2 = txn->mt_cursors[mc->mc_dbi];
@ -519,7 +518,7 @@ done:
}
return 0;
fail:
fail:
txn->mt_flags |= MDB_TXN_ERROR;
return rc;
*/
@ -588,14 +587,14 @@ func (c *cursor) search(key []byte) (*node, bool) {
nodekey.mv_data = NODEKEY(node);
rc = cmp(key, &nodekey);
#if MDB_DEBUG
#if MDB_DEBUG
if (IS_LEAF(mp))
DPRINTF(("found leaf index %u [%s], rc = %i",
i, DKEY(&nodekey), rc));
else
DPRINTF(("found branch index %u [%s -> %"Z"u], rc = %i",
i, DKEY(&nodekey), NODEPGNO(node), rc));
#endif
#endif
if (rc == 0)
break;
if (rc > 0)
@ -627,9 +626,9 @@ func (c *cursor) search(key []byte) (*node, bool) {
func (c *cursor) pop() {
/*
if (mc->mc_snum) {
#if MDB_DEBUG
#if MDB_DEBUG
MDB_page *top = mc->mc_pg[mc->mc_top];
#endif
#endif
mc->mc_snum--;
if (mc->mc_snum)
mc->mc_top--;
@ -748,6 +747,7 @@ func (c *cursor) searchLowest() error {
return rc;
return mdb_page_search_root(mc, NULL, MDB_PS_FIRST);
*/
return nil
}
// Search for the page a given key should be in.
@ -886,7 +886,7 @@ func (c *cursor) freeOverflowPage(p *page) error {
}
if (!(env->me_flags & MDB_WRITEMAP))
mdb_dpage_free(env, mp);
release:
release:
// Insert in me_pghead
mop = env->me_pghead;
j = mop[0] + ovpages;
@ -906,7 +906,6 @@ release:
return nil
}
// Find a sibling for a page.
// Replaces the page at the top of the cursor's stack with the
// specified sibling, if one exists.
@ -1013,7 +1012,7 @@ func (c *cursor) next(key []byte, data []byte, op int) error {
} else
mc->mc_ki[mc->mc_top]++;
skip:
skip:
DPRINTF(("==> cursor points to page %"Z"u with %u keys, key index %u",
mdb_dbg_pgno(mp), NUMKEYS(mp), mc->mc_ki[mc->mc_top]));
@ -1120,6 +1119,7 @@ func (c *cursor) prev(key []byte, data []byte, op int) error {
MDB_GET_KEY(leaf, key);
return MDB_SUCCESS;
*/
return nil
}
// Set the cursor on a specific data item.
@ -1233,7 +1233,7 @@ func (c *cursor) set(key []byte, data []byte, op int) (error, bool) {
mp = mc->mc_pg[mc->mc_top];
mdb_cassert(mc, IS_LEAF(mp));
set2:
set2:
leaf = mdb_node_search(mc, key, exactp);
if (exactp != NULL && !*exactp) {
// MDB_SET specified and not an exact match.
@ -1249,7 +1249,7 @@ set2:
leaf = NODEPTR(mp, 0);
}
set1:
set1:
mc->mc_flags |= C_INITIALIZED;
mc->mc_flags &= ~C_EOF;
@ -1306,7 +1306,7 @@ set1:
return rc;
*/
return nil
return nil, false
}
// Move the cursor to the first item in the database.
@ -1356,6 +1356,7 @@ func (c *cursor) first(key []byte, data []byte) error {
// Move the cursor to the last item in the database.
func (c *cursor) last() ([]byte, []byte) {
/*
int rc;
MDB_node *leaf;
@ -1396,6 +1397,8 @@ func (c *cursor) last() ([]byte, []byte) {
MDB_GET_KEY(leaf, key);
return MDB_SUCCESS;
*/
return nil, nil
}
func (c *cursor) Get(key []byte, data []byte, op int) ([]byte, []byte, error) {
@ -1492,7 +1495,7 @@ func (c *cursor) Get(key []byte, data []byte, op int) ([]byte, []byte, error) {
if (rc == MDB_SUCCESS) {
if (mc->mc_xcursor->mx_cursor.mc_flags & C_INITIALIZED) {
MDB_cursor *mx;
fetchm:
fetchm:
mx = &mc->mc_xcursor->mx_cursor;
data->mv_size = NUMKEYS(mx->mc_pg[mx->mc_top]) *
mx->mc_db->md_pad;
@ -1587,12 +1590,12 @@ func (c *cursor) touch() error {
mc->mc_top = mc->mc_snum-1;
}
return rc;
}
}
int
mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
int
mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
unsigned int flags)
{
{
enum { MDB_NO_ROOT = MDB_LAST_ERRCODE+10 }; // internal code
MDB_env *env;
MDB_node *leaf = NULL;
@ -1630,13 +1633,13 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
if (flags != MDB_CURRENT && key->mv_size-1 >= ENV_MAXKEY(env))
return MDB_BAD_VALSIZE;
#if SIZE_MAX > MAXDATASIZE
#if SIZE_MAX > MAXDATASIZE
if (data->mv_size > ((mc->mc_db->md_flags & MDB_DUPSORT) ? ENV_MAXKEY(env) : MAXDATASIZE))
return MDB_BAD_VALSIZE;
#else
#else
if ((mc->mc_db->md_flags & MDB_DUPSORT) && data->mv_size > ENV_MAXKEY(env))
return MDB_BAD_VALSIZE;
#endif
#endif
DPRINTF(("==> put db %d key [%s], size %"Z"u, data size %"Z"u",
DDBI(mc), DKEY(key), key ? key->mv_size : 0, data->mv_size));
@ -1745,7 +1748,7 @@ mdb_cursor_put(MDB_cursor *mc, MDB_val *key, MDB_val *data,
return MDB_SUCCESS;
}
more:
more:
leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
olddata.mv_size = NODEDSZ(leaf);
olddata.mv_data = NODEDATA(leaf);
@ -1766,14 +1769,14 @@ more:
if (flags == MDB_CURRENT)
goto current;
#if UINT_MAX < SIZE_MAX
#if UINT_MAX < SIZE_MAX
if (mc->mc_dbx->md_dcmp == mdb_cmp_int && olddata.mv_size == sizeof(size_t))
#ifdef MISALIGNED_OK
#ifdef MISALIGNED_OK
mc->mc_dbx->md_dcmp = mdb_cmp_long;
#else
#else
mc->mc_dbx->md_dcmp = mdb_cmp_cint;
#endif
#endif
#endif
#endif
// if data matches, skip it
if (!mc->mc_dbx->md_dcmp(data, &olddata)) {
if (flags & MDB_NODUPDATA)
@ -1837,7 +1840,7 @@ more:
if (NODESIZE + NODEKSZ(leaf) + xdata.mv_size > env->me_nodemax) {
// Too big for a sub-page, convert to sub-DB
fp_flags &= ~P_SUBP;
prep_subDB:
prep_subDB:
if (mc->mc_db->md_flags & MDB_DUPFIXED) {
fp_flags |= P_LEAF2;
dummy.md_pad = fp->mp_pad;
@ -1883,7 +1886,7 @@ prep_subDB:
mdb_node_del(mc, 0);
goto new_sub;
}
current:
current:
// overflow page overwrites need special handling
if (F_ISSET(leaf->mn_flags, F_BIGDATA)) {
MDB_page *omp;
@ -1960,7 +1963,7 @@ current:
rdata = data;
new_sub:
new_sub:
nflags = flags & NODE_ADD_FLAGS;
nsize = IS_LEAF2(mc->mc_pg[mc->mc_top]) ? key->mv_size : mdb_leaf_size(env, key, rdata);
if (SIZELEFT(mc->mc_pg[mc->mc_top]) < nsize) {
@ -2001,7 +2004,7 @@ new_sub:
// DB are all zero size.
if (do_sub) {
int xflags;
put_sub:
put_sub:
xdata.mv_size = 0;
xdata.mv_data = "";
leaf = NODEPTR(mc->mc_pg[mc->mc_top], mc->mc_ki[mc->mc_top]);
@ -2048,7 +2051,7 @@ put_sub:
mc->mc_db->md_entries++;
if (flags & MDB_MULTIPLE) {
if (!rc) {
next_mult:
next_mult:
mcount++;
// let caller know how many succeeded, if any
data[1].mv_size = mcount;
@ -2059,7 +2062,7 @@ next_mult:
}
}
}
done:
done:
// If we succeeded and the key didn't exist before, make sure
// the cursor is marked valid.
if (!rc && insert)
@ -2256,7 +2259,7 @@ func (c *cursor) addNode(index int, key []byte, data []byte, pgno int, flags int
if ((ssize_t)node_size > room)
goto full;
update:
update:
// Move higher pointers up one slot.
for (i = NUMKEYS(mp); i > indx; i--)
mp->mp_ptrs[i] = mp->mp_ptrs[i - 1];
@ -2303,7 +2306,7 @@ update:
return MDB_SUCCESS;
full:
full:
DPRINTF(("not enough room in page %"Z"u, got %u ptrs",
mdb_dbg_pgno(mp), NUMKEYS(mp)));
DPRINTF(("upper-lower = %u - %u = %"Z"d", mp->mp_upper,mp->mp_lower,room));
@ -2369,7 +2372,7 @@ func (c *cursor) deleteNode(ksize int) {
mp->mp_lower -= sizeof(indx_t);
mp->mp_upper += sz;
*/
*/
}
// Initial setup of a sorted-dups cursor.
@ -2441,19 +2444,19 @@ func (c *cursor) xcursor_init1(n *node) {
DPRINTF(("Sub-db -%u root page %"Z"u", mx->mx_cursor.mc_dbi,
mx->mx_db.md_root));
mx->mx_dbflag = DB_VALID|DB_DIRTY; // DB_DIRTY guides mdb_cursor_touch
#if UINT_MAX < SIZE_MAX
#if UINT_MAX < SIZE_MAX
if (mx->mx_dbx.md_cmp == mdb_cmp_int && mx->mx_db.md_pad == sizeof(size_t))
#ifdef MISALIGNED_OK
#ifdef MISALIGNED_OK
mx->mx_dbx.md_cmp = mdb_cmp_long;
#else
#else
mx->mx_dbx.md_cmp = mdb_cmp_cint;
#endif
#endif
#endif
#endif
*/
}
// Initialize a cursor for a given transaction and database.
func (c *cursor) init(t *transaction, bucket *bucket, mx *xcursor) {
func (c *cursor) init(t *transaction, bucket *Bucket, mx *xcursor) {
/*
mc->mc_next = NULL;
mc->mc_backup = NULL;
@ -2527,7 +2530,7 @@ func (c *cursor) Transaction() Transaction {
return nil
}
func (c *cursor) Bucket() Bucket {
func (c *cursor) Bucket() *Bucket {
return c.bucket
}
@ -2549,7 +2552,7 @@ func (c *cursor) updateKey(key []byte) error {
mp = mc->mc_pg[mc->mc_top];
node = NODEPTR(mp, indx);
ptr = mp->mp_ptrs[indx];
#if MDB_DEBUG
#if MDB_DEBUG
{
MDB_val k2;
char kbuf2[DKBUF_MAXKEYSIZE*2+1];
@ -2561,7 +2564,7 @@ func (c *cursor) updateKey(key []byte) error {
DKEY(key),
mp->mp_pgno));
}
#endif
#endif
// Sizes must be 2-byte aligned.
ksize = EVEN(key->mv_size);

444
db.go
View File

@ -1,5 +1,12 @@
package bolt
import (
"os"
"sync"
"syscall"
"unsafe"
)
const (
NoSync = iota
NoMetaSync
@ -8,59 +15,54 @@ const (
IntegerDupKey
)
var DatabaseAlreadyOpenedError = &Error{"Database already open"}
var DatabaseAlreadyOpenedError = &Error{"Database already open", nil}
// TODO: #define MDB_FATAL_ERROR 0x80000000U /** Failed to update the meta page. Probably an I/O error. */
// TODO: #define MDB_ENV_ACTIVE 0x20000000U /** Some fields are initialized. */
// TODO: #define MDB_ENV_TXKEY 0x10000000U /** me_txkey is set */
// TODO: #define MDB_LIVE_READER 0x08000000U /** Have liveness lock in reader table */
type DB interface {
syncEnabled bool
metaSyncEnabled bool
}
type db struct {
type DB struct {
sync.Mutex
opened bool
file os.File
metafile os.File
file *os.File
metafile *os.File
data []byte
buf []byte
meta0 *meta
meta1 *meta
pageSize int
readers []*reader
buckets []*bucket
xbuckets []*bucketx /**< array of static DB info */
buckets []*Bucket
// xbuckets []*bucketx /**< array of static DB info */
bucketFlags []int /**< array of flags from MDB_db.md_flags */
path string
mmap []byte
mmapSize int /**< size of the data memory map */
size int /**< current file size */
meta1 []byte
meta2 []byte
pbuf []byte
transaction *transaction /**< current write transaction */
maxPageNumber int /**< me_mapsize / me_psize */
pageState pageStage /**< state of old pages from freeDB */
pageState pageState /**< state of old pages from freeDB */
dpages []*page /**< list of malloc'd blocks for re-use */
freePages []int /** IDL of pages that became unused in a write txn */
dirtyPages []int /** ID2L of pages written during a write txn. Length MDB_IDL_UM_SIZE. */
maxFreeOnePage int /** Max number of freelist items that can fit in a single overflow page */
maxPageDataSize int
maxNodeSize int /** Max size of a node on a page */
maxKeySize int /**< max size of a key */
}
func NewDB() DB {
return &db{}
func NewDB() *DB {
return &DB{}
}
func (db *db) Path() string {
func (db *DB) Path() string {
return db.path
}
func (db *db) Open(path string, mode os.FileMode) error {
func (db *DB) Open(path string, mode os.FileMode) error {
var err error
db.Lock()
defer db.Unlock()
@ -72,24 +74,24 @@ func (db *db) Open(path string, mode os.FileMode) error {
// Open data file and separate sync handler for metadata writes.
db.path = path
if db.file, err = os.OpenFile(db.path, O_RDWR | O_CREAT, mode); err != nil {
if db.file, err = os.OpenFile(db.path, os.O_RDWR|os.O_CREATE, mode); err != nil {
db.close()
return err
}
if db.metafile, err = os.OpenFile(db.path, O_RDWR | O_SYNC, mode); err != nil {
if db.metafile, err = os.OpenFile(db.path, os.O_RDWR|os.O_SYNC, mode); err != nil {
db.close()
return err
}
// Read enough data to get both meta pages.
var m, m0, m1 *meta
var buf [headerSize + unsafe.Sizeof(meta)]byte
if _, err := db.file.ReadAt(buf, 0); err == nil {
var buf [pageHeaderSize + int(unsafe.Sizeof(meta{}))]byte
if _, err := db.file.ReadAt(buf[:], 0); err == nil {
if m0, _ = db.page(buf[:], 0).meta(); m0 != nil {
db.pageSize = m0.free.pad
db.pageSize = int(m0.free.pad)
}
}
if _, err := db.file.ReadAt(buf, db.pageSize); err == nil {
if _, err := db.file.ReadAt(buf[:], int64(db.pageSize)); err == nil {
m1, _ = db.page(buf[:], 0).meta()
}
if m0 != nil && m1 != nil {
@ -102,27 +104,16 @@ func (db *db) Open(path string, mode os.FileMode) error {
// Initialize the page size for new environments.
if m == nil {
db.pageSize = os.Getpagesize()
if db.pageSize > maxPageSize {
db.pageSize = maxPageSize
if err := db.init(); err != nil {
return err
}
}
// TODO: Check mapsize.
/*
// Was a mapsize configured?
if (!env->me_mapsize) {
// If this is a new environment, take the default,
// else use the size recorded in the existing env.
env->me_mapsize = newenv ? DEFAULT_MAPSIZE : meta.mm_mapsize;
} else if (env->me_mapsize < meta.mm_mapsize) {
// If the configured size is smaller, make sure it's
// still big enough. Silently round up to minimum if not.
size_t minsize = (meta.mm_last_pg + 1) * meta.mm_psize;
if (env->me_mapsize < minsize)
env->me_mapsize = minsize;
}
*/
// Initialize db fields.
db.buf = make([]byte, db.pageSize)
db.maxPageDataSize = ((db.pageSize - pageHeaderSize) / int(unsafe.Sizeof(pgno(0)))) - 1
db.maxNodeSize = (((db.pageSize - pageHeaderSize) / minKeyCount) & -2) - int(unsafe.Sizeof(indx(0)))
// TODO?: env->me_maxpg = env->me_mapsize / env->me_psize;
// Memory map the data file.
if err := db.mmap(); err != nil {
@ -130,94 +121,87 @@ func (db *db) Open(path string, mode os.FileMode) error {
return err
}
// Initialize the buffer.
db.buf = make([]byte, db.pageSize)
// TODO: Initialize meta.
// if (newenv) {
// i = mdb_env_init_meta(env, &meta);
// if (i != MDB_SUCCESS) {
// return i;
// }
// }
// Mark the database as opened and return.
db.opened = true
return nil
}
// Read the meta pages and return the latest.
func (db *db) readMeta() *meta {
m := &meta{}
m.read()
// int mdb_env_map(MDB_env *env, void *addr, int newsize)
func (db *DB) mmap() error {
var err error
/*
if ((i = mdb_env_read_header(env, &meta)) != 0) {
if (i != ENOENT)
return i;
DPUTS("new mdbenv");
newenv = 1;
env->me_psize = env->me_os_psize;
if (env->me_psize > MAX_PAGESIZE)
env->me_psize = MAX_PAGESIZE;
// Determine the map size based on the file size.
var size int
if info, err := os.Stat(db.file.Name()); err != nil {
return err
} else if info.Size() < int64(db.pageSize*2) {
return &Error{"file size too small", nil}
} else {
env->me_psize = meta.mm_psize;
size = int(info.Size())
}
rc = mdb_env_map(env, meta.mm_address, newenv);
if (rc)
return rc;
if (newenv) {
if (flags & MDB_FIXEDMAP)
meta.mm_address = env->me_map;
i = mdb_env_init_meta(env, &meta);
if (i != MDB_SUCCESS) {
return i;
}
// Memory-map the data file as a byte slice.
if db.data, err = syscall.Mmap(int(db.file.Fd()), 0, size, syscall.PROT_READ, syscall.MAP_SHARED); err != nil {
return err
}
env->me_maxfree_1pg = (env->me_psize - PAGEHDRSZ) / sizeof(pgno_t) - 1;
env->me_nodemax = (((env->me_psize - PAGEHDRSZ) / MDB_MINKEYS) & -2)
- sizeof(indx_t);
#if !(MDB_MAXKEYSIZE)
env->me_maxkey = env->me_nodemax - (NODESIZE + sizeof(MDB_db));
#endif
env->me_maxpg = env->me_mapsize / env->me_psize;
// TODO?: If nordahead, then: madvise(env->me_map, env->me_mapsize, MADV_RANDOM);
#if MDB_DEBUG
{
int toggle = mdb_env_pick_meta(env);
MDB_db *db = &env->me_metas[toggle]->mm_dbs[MAIN_DBI];
DPRINTF(("opened database version %u, pagesize %u",
env->me_metas[0]->mm_version, env->me_psize));
DPRINTF(("using meta page %d", toggle));
DPRINTF(("depth: %u", db->md_depth));
DPRINTF(("entries: %"Z"u", db->md_entries));
DPRINTF(("branch pages: %"Z"u", db->md_branch_pages));
DPRINTF(("leaf pages: %"Z"u", db->md_leaf_pages));
DPRINTF(("overflow pages: %"Z"u", db->md_overflow_pages));
DPRINTF(("root: %"Z"u", db->md_root));
// Save references to the meta pages.
if db.meta0, err = db.page(db.data, 0).meta(); err != nil {
return &Error{"meta0 error", err}
}
if db.meta1, err = db.page(db.data, 1).meta(); err != nil {
return &Error{"meta1 error", err}
}
#endif
return MDB_SUCCESS;
*/
return nil
}
// page retrieves a page reference from a given byte array based on the current page size.
func (db *db) page(b []byte, id int) *page {
return (*page)(unsafe.Pointer(b[id * db.pageSize]))
// init creates a new database file and initializes its meta pages.
func (db *DB) init() error {
// Set the page size to the OS page size unless that is larger than max page size.
db.pageSize = os.Getpagesize()
if db.pageSize > maxPageSize {
db.pageSize = maxPageSize
}
// Create two meta pages on a buffer.
buf := make([]byte, db.pageSize*2)
for i := 0; i < 2; i++ {
p := db.page(buf[:], i)
p.id = pgno(i)
p.initMeta(db.pageSize)
}
// Write the buffer to our data file.
if _, err := db.metafile.WriteAt(buf, 0); err != nil {
return err
}
return nil
}
func (db *DB) close() {
// TODO
}
// page retrieves a page reference from a given byte array based on the current page size.
func (db *DB) page(b []byte, id int) *page {
return (*page)(unsafe.Pointer(&b[id*db.pageSize]))
}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ CONVERTED ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
func (db *db) freePage(p *page) {
func (db *DB) freePage(p *page) {
/*
mp->mp_next = env->me_dpages;
VGMEMP_FREE(env, mp);
@ -225,7 +209,7 @@ func (db *db) freePage(p *page) {
*/
}
func (db *db) freeDirtyPage(p *page) {
func (db *DB) freeDirtyPage(p *page) {
/*
if (!IS_OVERFLOW(dp) || dp->mp_pages == 1) {
mdb_page_free(env, dp);
@ -237,7 +221,7 @@ func (db *db) freeDirtyPage(p *page) {
*/
}
func (db *db) freeAllDirtyPages(p *page) {
func (db *DB) freeAllDirtyPages(p *page) {
/*
MDB_env *env = txn->mt_env;
MDB_ID2L dl = txn->mt_u.dirty_list;
@ -250,7 +234,7 @@ func (db *db) freeAllDirtyPages(p *page) {
*/
}
func (db *db) sync(force bool) error {
func (db *DB) sync(force bool) error {
/*
int rc = 0;
if (force || !F_ISSET(env->me_flags, MDB_NOSYNC)) {
@ -259,10 +243,10 @@ func (db *db) sync(force bool) error {
? MS_ASYNC : MS_SYNC;
if (MDB_MSYNC(env->me_map, env->me_mapsize, flags))
rc = ErrCode();
#ifdef _WIN32
#ifdef _WIN32
else if (flags == MS_SYNC && MDB_FDATASYNC(env->me_fd))
rc = ErrCode();
#endif
#endif
} else {
if (MDB_FDATASYNC(env->me_fd))
rc = ErrCode();
@ -273,7 +257,7 @@ func (db *db) sync(force bool) error {
return nil
}
func (db *db) Transaction(parent *transaction, flags int) (*transaction, error) {
func (db *DB) Transaction(parent *transaction, flags int) (*transaction, error) {
/*
MDB_txn *txn;
MDB_ntxn *ntxn;
@ -367,80 +351,20 @@ func (db *db) Transaction(parent *transaction, flags int) (*transaction, error)
return rc;
*/
return nil
}
// Write the environment parameters of a freshly created DB environment.
// @param[in] env the environment handle
// @param[out] meta address of where to store the meta information
// @return 0 on success, non-zero on failure.
func (db *db) initMeta(meta *meta) error {
/*
MDB_page *p, *q;
int rc;
unsigned int psize;
#ifdef _WIN32
DWORD len;
OVERLAPPED ov;
memset(&ov, 0, sizeof(ov));
#define DO_PWRITE(rc, fd, ptr, size, len, pos) do { \
ov.Offset = pos; \
rc = WriteFile(fd, ptr, size, &len, &ov); } while(0)
#else
int len;
#define DO_PWRITE(rc, fd, ptr, size, len, pos) do { \
len = pwrite(fd, ptr, size, pos); \
rc = (len >= 0); } while(0)
#endif
DPUTS("writing new meta page");
psize = env->me_psize;
meta->mm_magic = MDB_MAGIC;
meta->mm_version = MDB_DATA_VERSION;
meta->mm_mapsize = env->me_mapsize;
meta->mm_psize = psize;
meta->mm_last_pg = 1;
meta->mm_flags = env->me_flags & 0xffff;
meta->mm_flags |= MDB_INTEGERKEY;
meta->mm_dbs[0].md_root = P_INVALID;
meta->mm_dbs[1].md_root = P_INVALID;
p = calloc(2, psize);
p->mp_pgno = 0;
p->mp_flags = P_META;
*(MDB_meta *)METADATA(p) = *meta;
q = (MDB_page *)((char *)p + psize);
q->mp_pgno = 1;
q->mp_flags = P_META;
*(MDB_meta *)METADATA(q) = *meta;
DO_PWRITE(rc, env->me_fd, p, psize * 2, len, 0);
if (!rc)
rc = ErrCode();
else if ((unsigned) len == psize * 2)
rc = MDB_SUCCESS;
else
rc = ENOSPC;
free(p);
return rc;
*/
return nil
return nil, nil
}
// Check both meta pages to see which one is newer.
// @param[in] env the environment handle
// @return meta toggle (0 or 1).
func (db *db) pickMeta() int {
func (db *DB) pickMeta() int {
/*
return (env->me_metas[0]->mm_txnid < env->me_metas[1]->mm_txnid);
*/
return 0
}
func (db *db) Create() error {
func (db *DB) Create() error {
/*
MDB_env *e;
@ -453,10 +377,10 @@ func (db *db) Create() error {
e->me_fd = INVALID_HANDLE_VALUE;
e->me_lfd = INVALID_HANDLE_VALUE;
e->me_mfd = INVALID_HANDLE_VALUE;
#ifdef MDB_USE_POSIX_SEM
#ifdef MDB_USE_POSIX_SEM
e->me_rmutex = SEM_FAILED;
e->me_wmutex = SEM_FAILED;
#endif
#endif
e->me_pid = getpid();
GET_PAGESIZE(e->me_os_psize);
VGMEMP_CREATE(e,0,0);
@ -466,81 +390,7 @@ func (db *db) Create() error {
return nil
}
// int mdb_env_map(MDB_env *env, void *addr, int newsize)
func (db *db) mmap(newsize int) error {
/*
MDB_page *p;
unsigned int flags = env->me_flags;
#ifdef _WIN32
int rc;
HANDLE mh;
LONG sizelo, sizehi;
sizelo = env->me_mapsize & 0xffffffff;
sizehi = env->me_mapsize >> 16 >> 16; // only needed on Win64
// Windows won't create mappings for zero length files.
// Just allocate the maxsize right now.
if (newsize) {
if (SetFilePointer(env->me_fd, sizelo, &sizehi, 0) != (DWORD)sizelo
|| !SetEndOfFile(env->me_fd)
|| SetFilePointer(env->me_fd, 0, NULL, 0) != 0)
return ErrCode();
}
mh = CreateFileMapping(env->me_fd, NULL, flags & MDB_WRITEMAP ?
PAGE_READWRITE : PAGE_READONLY,
sizehi, sizelo, NULL);
if (!mh)
return ErrCode();
env->me_map = MapViewOfFileEx(mh, flags & MDB_WRITEMAP ?
FILE_MAP_WRITE : FILE_MAP_READ,
0, 0, env->me_mapsize, addr);
rc = env->me_map ? 0 : ErrCode();
CloseHandle(mh);
if (rc)
return rc;
#else
int prot = PROT_READ;
if (flags & MDB_WRITEMAP) {
prot |= PROT_WRITE;
if (ftruncate(env->me_fd, env->me_mapsize) < 0)
return ErrCode();
}
env->me_map = mmap(addr, env->me_mapsize, prot, MAP_SHARED,
env->me_fd, 0);
if (env->me_map == MAP_FAILED) {
env->me_map = NULL;
return ErrCode();
}
if (flags & MDB_NORDAHEAD) {
// Turn off readahead. It's harmful when the DB is larger than RAM.
#ifdef MADV_RANDOM
madvise(env->me_map, env->me_mapsize, MADV_RANDOM);
#else
#ifdef POSIX_MADV_RANDOM
posix_madvise(env->me_map, env->me_mapsize, POSIX_MADV_RANDOM);
#endif // POSIX_MADV_RANDOM
#endif // MADV_RANDOM
}
#endif // _WIN32
// Can happen because the address argument to mmap() is just a
// hint. mmap() can pick another, e.g. if the range is in use.
// The MAP_FIXED flag would prevent that, but then mmap could
// instead unmap existing pages to make room for the new map.
if (addr && env->me_map != addr)
return EBUSY; // TODO: Make a new MDB_* error code?
p = (MDB_page *)env->me_map;
env->me_metas[0] = METADATA(p);
env->me_metas[1] = (MDB_meta *)((char *)env->me_metas[0] + env->me_psize);
return MDB_SUCCESS;
*/
return nil
}
func (db *db) setMapSize(size int) error {
func (db *DB) setMapSize(size int) error {
/*
// If env is already open, caller is responsible for making
// sure there are no active txns.
@ -573,7 +423,7 @@ func (db *db) setMapSize(size int) error {
return nil
}
func (db *db) setMaxBucketCount(count int) error {
func (db *DB) setMaxBucketCount(count int) error {
/*
if (env->me_map)
return EINVAL;
@ -583,16 +433,17 @@ func (db *db) setMaxBucketCount(count int) error {
return nil
}
func (db *db) setMaxReaderCount(count int) error {
func (db *DB) setMaxReaderCount(count int) error {
/*
if (env->me_map || readers < 1)
return EINVAL;
env->me_maxreaders = readers;
return MDB_SUCCESS;
*/
return nil
}
func (db *db) getMaxReaderCount(count int) (int, error) {
func (db *DB) getMaxReaderCount(count int) (int, error) {
/*
if (!env || !readers)
return EINVAL;
@ -602,9 +453,8 @@ func (db *db) getMaxReaderCount(count int) (int, error) {
return 0, nil
}
// Destroy resources from mdb_env_open(), clear our readers & DBIs
func (db *db) close0(excl) {
func (db *DB) close0(excl int) {
/*
int i;
@ -624,7 +474,7 @@ func (db *db) close0(excl) {
if (env->me_flags & MDB_ENV_TXKEY) {
pthread_key_delete(env->me_txkey);
#ifdef _WIN32
#ifdef _WIN32
// Delete our key from the global list
for (i=0; i<mdb_tls_nkeys; i++)
if (mdb_tls_keys[i] == env->me_txkey) {
@ -632,7 +482,7 @@ func (db *db) close0(excl) {
mdb_tls_nkeys--;
break;
}
#endif
#endif
}
if (env->me_map) {
@ -649,14 +499,14 @@ func (db *db) close0(excl) {
for (i = env->me_numreaders; --i >= 0; )
if (env->me_txns->mti_readers[i].mr_pid == pid)
env->me_txns->mti_readers[i].mr_pid = 0;
#ifdef _WIN32
#ifdef _WIN32
if (env->me_rmutex) {
CloseHandle(env->me_rmutex);
if (env->me_wmutex) CloseHandle(env->me_wmutex);
}
// Windows automatically destroys the mutexes when
// the last handle closes.
#elif defined(MDB_USE_POSIX_SEM)
#elif defined(MDB_USE_POSIX_SEM)
if (env->me_rmutex != SEM_FAILED) {
sem_close(env->me_rmutex);
if (env->me_wmutex != SEM_FAILED)
@ -670,17 +520,17 @@ func (db *db) close0(excl) {
sem_unlink(env->me_txns->mti_wmname);
}
}
#endif
#endif
munmap((void *)env->me_txns, (env->me_maxreaders-1)*sizeof(MDB_reader)+sizeof(MDB_txninfo));
}
if (env->me_lfd != INVALID_HANDLE_VALUE) {
#ifdef _WIN32
#ifdef _WIN32
if (excl >= 0) {
// Unlock the lockfile. Windows would have unlocked it
// after closing anyway, but not necessarily at once.
UnlockFile(env->me_lfd, 0, 0, 1, 0);
}
#endif
#endif
(void) close(env->me_lfd);
}
@ -688,20 +538,20 @@ func (db *db) close0(excl) {
*/
}
func (db *db) copyfd(handle int) error {
func (db *DB) copyfd(handle int) error {
/*
MDB_txn *txn = NULL;
int rc;
size_t wsize;
char *ptr;
#ifdef _WIN32
#ifdef _WIN32
DWORD len, w2;
#define DO_WRITE(rc, fd, ptr, w2, len) rc = WriteFile(fd, ptr, w2, &len, NULL)
#else
#define DO_WRITE(rc, fd, ptr, w2, len) rc = WriteFile(fd, ptr, w2, &len, NULL)
#else
ssize_t len;
size_t w2;
#define DO_WRITE(rc, fd, ptr, w2, len) len = write(fd, ptr, w2); rc = (len >= 0)
#endif
#define DO_WRITE(rc, fd, ptr, w2, len) len = write(fd, ptr, w2); rc = (len >= 0)
#endif
// Do the lock/unlock of the reader mutex before starting the
// write txn. Otherwise other read txns could block writers.
@ -769,14 +619,14 @@ func (db *db) copyfd(handle int) error {
}
}
leave:
leave:
mdb_txn_abort(txn);
return rc;
}
}
int
mdb_env_copy(MDB_env *env, const char *path)
{
int
mdb_env_copy(MDB_env *env, const char *path)
{
int rc, len;
char *lpath;
HANDLE newfd = INVALID_HANDLE_VALUE;
@ -795,33 +645,33 @@ mdb_env_copy(MDB_env *env, const char *path)
// The destination path must exist, but the destination file must not.
// We don't want the OS to cache the writes, since the source data is
// already in the OS cache.
#ifdef _WIN32
#ifdef _WIN32
newfd = CreateFile(lpath, GENERIC_WRITE, 0, NULL, CREATE_NEW,
FILE_FLAG_NO_BUFFERING|FILE_FLAG_WRITE_THROUGH, NULL);
#else
#else
newfd = open(lpath, O_WRONLY|O_CREAT|O_EXCL, 0666);
#endif
#endif
if (newfd == INVALID_HANDLE_VALUE) {
rc = ErrCode();
goto leave;
}
#ifdef O_DIRECT
#ifdef O_DIRECT
// Set O_DIRECT if the file system supports it
if ((rc = fcntl(newfd, F_GETFL)) != -1)
(void) fcntl(newfd, F_SETFL, rc | O_DIRECT);
#endif
#ifdef F_NOCACHE // __APPLE__
#endif
#ifdef F_NOCACHE // __APPLE__
rc = fcntl(newfd, F_NOCACHE, 1);
if (rc) {
rc = ErrCode();
goto leave;
}
#endif
#endif
rc = mdb_env_copyfd(env, newfd);
leave:
leave:
if (!(env->me_flags & MDB_NOSUBDIR))
free(lpath);
if (newfd != INVALID_HANDLE_VALUE)
@ -833,7 +683,7 @@ leave:
return nil
}
func (db *db) Close() {
func (db *DB) Close() {
/*
MDB_page *dp;
@ -862,7 +712,7 @@ func (db *db) Close() {
// @param[in] key The key for the node.
// @param[in] data The data for the node.
// @return The number of bytes needed to store the node.
func (db *db) LeafSize(key []byte, data []byte) int {
func (db *DB) LeafSize(key []byte, data []byte) int {
/*
size_t sz;
@ -886,7 +736,7 @@ func (db *db) LeafSize(key []byte, data []byte) int {
// @param[in] env The environment handle.
// @param[in] key The key for the node.
// @return The number of bytes needed to store the node.
func (db *db) BranchSize(key []byte) int {
func (db *DB) BranchSize(key []byte) int {
/*
size_t sz;
@ -902,7 +752,7 @@ func (db *db) BranchSize(key []byte) int {
return 0
}
func (db *db) SetFlags(flag int, onoff bool) error {
func (db *DB) SetFlags(flag int, onoff bool) error {
/*
if ((flag & CHANGEABLE) != flag)
return EINVAL;
@ -915,12 +765,7 @@ func (db *db) SetFlags(flag int, onoff bool) error {
return nil
}
func (db *db) Flags() int {
return db.flags
}
func (db *db) Stat() *Stat
func (db *DB) Stat() *Stat {
/*
int toggle;
@ -939,9 +784,10 @@ func (db *db) Stat() *Stat
//return mdb_stat0(env, &env->me_metas[toggle]->mm_dbs[MAIN_DBI], stat);
return stat
*/
return nil
}
func (db *db) Info() *Info {
func (db *DB) Info() *Info {
/*
int toggle;
@ -965,7 +811,7 @@ func (db *db) Info() *Info {
}
// TODO: Move to bucket.go
func (db *db) CloseBucket(b Bucket) {
func (db *DB) CloseBucket(b Bucket) {
/*
char *ptr;
if (dbi <= MAIN_DBI || dbi >= env->me_maxdbs)
@ -979,7 +825,7 @@ func (db *db) CloseBucket(b Bucket) {
}
//int mdb_reader_list(MDB_env *env, MDB_msg_func *func, void *ctx)
func (db *db) getReaderList() error {
func (db *DB) getReaderList() error {
/*
unsigned int i, rdrs;
MDB_reader *mr;
@ -1019,7 +865,7 @@ func (db *db) getReaderList() error {
}
// (bool return is whether reader is dead)
func (db *db) checkReaders() (bool, error) {
func (db *DB) checkReaders() (bool, error) {
/*
unsigned int i, j, rdrs;
MDB_reader *mr;

27
db_test.go Normal file
View File

@ -0,0 +1,27 @@
package bolt
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDBOpen(t *testing.T) {
withDB(func(db *DB, path string) {
err := db.Open(path, 0666)
assert.NoError(t, err)
})
}
func withDB(fn func(*DB, string)) {
f, _ := ioutil.TempFile("", "bolt-")
path := f.Name()
f.Close()
os.Remove(path)
defer os.RemoveAll(path)
db := NewDB()
fn(db, path)
}

View File

@ -1,30 +1,34 @@
package bolt
var (
KeyExistError = &Error{"Key/data pair already exists"}
NotFoundError = &Error{"No matching key/data pair found"}
PageNotFoundError = &Error{"Requested page not found"}
CorruptedError = &Error{"Located page was wrong type"}
PanicError = &Error{"Update of meta page failed"}
VersionMismatchError = &Error{"Database environment version mismatch"}
InvalidError = &Error{"File is not an MDB file"}
MapFullError = &Error{"Environment mapsize limit reached"}
BucketFullError = &Error{"Environment maxdbs limit reached"}
ReadersFullError = &Error{"Environment maxreaders limit reached"}
TransactionFullError = &Error{"Transaction has too many dirty pages - transaction too big"}
CursorFullError = &Error{"Internal error - cursor stack limit reached"}
PageFullError = &Error{"Internal error - page has no more space"}
MapResizedError = &Error{"Database contents grew beyond environment mapsize"}
IncompatibleError = &Error{"Operation and DB incompatible, or DB flags changed"}
BadReaderSlotError = &Error{"Invalid reuse of reader locktable slot"}
BadTransactionError = &Error{"Transaction cannot recover - it must be aborted"}
BadValueSizeError = &Error{"Too big key/data, key is empty, or wrong DUPFIXED size"}
KeyExistError = &Error{"key/data pair already exists", nil}
NotFoundError = &Error{"no matching key/data pair found", nil}
PageNotFoundError = &Error{"requested page not found", nil}
CorruptedError = &Error{"located page was wrong type", nil}
PanicError = &Error{"update of meta page failed", nil}
VersionMismatchError = &Error{"database environment version mismatch", nil}
InvalidError = &Error{"file is not a bolt file", nil}
MapFullError = &Error{"environment mapsize limit reached", nil}
BucketFullError = &Error{"environment maxdbs limit reached", nil}
ReadersFullError = &Error{"environment maxreaders limit reached", nil}
TransactionFullError = &Error{"transaction has too many dirty pages - transaction too big", nil}
CursorFullError = &Error{"internal error - cursor stack limit reached", nil}
PageFullError = &Error{"internal error - page has no more space", nil}
MapResizedError = &Error{"database contents grew beyond environment mapsize", nil}
IncompatibleError = &Error{"operation and db incompatible, or db flags changed", nil}
BadReaderSlotError = &Error{"invalid reuse of reader locktable slot", nil}
BadTransactionError = &Error{"transaction cannot recover - it must be aborted", nil}
BadValueSizeError = &Error{"too big key/data or key is empty", nil}
)
type Error struct {
message string
cause error
}
func (e *Error) Error() {
func (e *Error) Error() string {
if e.cause != nil {
return e.message + ": " + e.cause.Error()
}
return e.message
}

17
meta.go
View File

@ -1,7 +1,7 @@
package bolt
var (
InvalidMetaPageError = &Error{"Invalid meta page"}
InvalidMetaPageError = &Error{"Invalid meta page", nil}
)
// TODO: #define mm_psize mm_dbs[0].md_pad
@ -25,16 +25,14 @@ var (
// void *md_relctx; /**< user-provided context for md_rel */
// } MDB_dbx;
const magic int32 = 0xBEEFC0DE
const magic uint32 = 0xC0DEC0DE
const version uint32 = 1
type meta struct {
magic int32
version int32
mapsize int
free bucket
main bucket
magic uint32
version uint32
free Bucket
main Bucket
pgno int
txnid int
}
@ -49,7 +47,6 @@ func (m *meta) validate() error {
return nil
}
// Read the environment parameters of a DB environment before
// mapping it into memory.
// @param[in] env the environment handle

5
os.go Normal file
View File

@ -0,0 +1,5 @@
package bolt
import (
_ "os"
)

44
page.go
View File

@ -5,9 +5,11 @@ import (
)
const maxPageSize = 0x8000
const minKeyCount = 2
var _page page
const headerSize = unsafe.Offsetof(_page.ptr)
const pageHeaderSize = int(unsafe.Offsetof(_page.ptr))
const minPageKeys = 2
const fillThreshold = 250 // 25%
@ -20,13 +22,15 @@ const (
p_dirty = 0x10 /**< dirty page, also set for #P_SUBP pages */
p_sub = 0x40
p_keep = 0x8000 /**< leave this page alone during spill */
p_invalid = ^pgno(0)
)
// maxCommitPages is the maximum number of pages to commit in one writev() call.
const maxCommitPages 64
const maxCommitPages = 64
/* max bytes to write in one call */
const maxWriteByteCount 0x80000000U // TODO: #define MAX_WRITE 0x80000000U >> (sizeof(ssize_t) == 4))
const maxWriteByteCount uint = 0x80000000 // TODO: #define MAX_WRITE 0x80000000U >> (sizeof(ssize_t) == 4))
// TODO:
// #if defined(IOV_MAX) && IOV_MAX < MDB_COMMIT_PAGES
@ -42,12 +46,14 @@ const maxWriteByteCount 0x80000000U // TODO: #define MAX_WRITE 0x80000000U >>
// TODO: #define MDB_SPLIT_REPLACE MDB_APPENDDUP /**< newkey is not new */
type pgno uint64
type txnid uint64
type indx uint16
type page struct {
id pgno
flags int
lower int
upper int
lower indx
upper indx
overflow int
ptr int
}
@ -60,8 +66,8 @@ type pageState struct {
// meta returns a pointer to the metadata section of the page.
func (p *page) meta() (*meta, error) {
// Exit if page is not a meta page.
if (p.flags & p_meta) != 0 {
return InvalidMetaPageError
if (p.flags & p_meta) == 0 {
return nil, InvalidMetaPageError
}
// Cast the meta section and validate before returning.
@ -72,12 +78,17 @@ func (p *page) meta() (*meta, error) {
return m, nil
}
// initMeta initializes a page as a new meta page.
func (p *page) initMeta(pageSize int) {
p.flags = p_meta
m := (*meta)(unsafe.Pointer(&p.ptr))
m.magic = magic
m.version = version
m.free.pad = uint32(pageSize)
m.pgno = 1
m.free.root = p_invalid
m.main.root = p_invalid
}
// nodeCount returns the number of nodes on the page.
func (p *page) nodeCount() int {
@ -86,10 +97,5 @@ func (p *page) nodeCount() int {
// remainingSize returns the number of bytes left in the page.
func (p *page) remainingSize() int {
return p.header.upper - p.header.lower
}
// remainingSize returns the number of bytes left in the page.
func (p *page) remainingSize() int {
return p.header.upper - p.header.lower
return int(p.upper - p.lower)
}

View File

@ -1,5 +1,5 @@
package bolt
type reader struct {
int transactionID
txnid int
}

View File

@ -16,7 +16,7 @@ type Transaction interface {
type transaction struct {
id int
flags int
db *db
db *DB
parent *transaction
child *transaction
nextPageNumber int
@ -25,7 +25,7 @@ type transaction struct {
dirtyList []int
reader *reader
// TODO: bucketxs []*bucketx
buckets []*bucket
buckets []*Bucket
bucketFlags []int
cursors []*cursor
// Implicit from slices? TODO: MDB_dbi mt_numdbs;
@ -38,7 +38,6 @@ type ntxn struct {
pageState pageState /**< parent transaction's saved freestate */
}
func (t *transaction) allocPage(num int) *page {
/*
MDB_env *env = txn->mt_env;
@ -327,10 +326,10 @@ func (t *transaction) renew() error {
txn->mt_txnid = meta->mm_txnid;
}
txn->mt_txnid++;
#if MDB_DEBUG
#if MDB_DEBUG
if (txn->mt_txnid == mdb_debug_start)
mdb_debug = 1;
#endif
#endif
txn->mt_dirty_room = MDB_IDL_UM_MAX;
txn->mt_u.dirty_list = env->me_dirty_list;
txn->mt_u.dirty_list[0].mid = 0;
@ -390,7 +389,7 @@ func (t *transaction) Renew() error {
return nil
}
func (t *transaction) DB() DB {
func (t *transaction) DB() *DB {
return t.db
}
@ -573,7 +572,7 @@ func (t *transaction) saveFreelist() error {
} while (freecnt < free_pgs[0]);
mdb_midl_sort(free_pgs);
memcpy(data.mv_data, free_pgs, data.mv_size);
#if (MDB_DEBUG) > 1
#if (MDB_DEBUG) > 1
{
unsigned int i = free_pgs[0];
DPRINTF(("IDL write txn %"Z"u root %"Z"u num %u",
@ -581,7 +580,7 @@ func (t *transaction) saveFreelist() error {
for (; i; i--)
DPRINTF(("IDL %"Z"u", free_pgs[i]));
}
#endif
#endif
continue;
}
@ -663,7 +662,7 @@ func (t *transaction) saveFreelist() error {
// @param[in] txn the transaction that's being committed
// @param[in] keep number of initial pages in dirty_list to keep dirty.
// @return 0 on success, non-zero on failure.
func (t *transaction) flush(keep bool) {
func (t *transaction) flush(keep bool) error {
/*
MDB_env *env = txn->mt_env;
MDB_ID2L dl = txn->mt_u.dirty_list;
@ -672,14 +671,14 @@ func (t *transaction) flush(keep bool) {
size_t size = 0, pos = 0;
pgno_t pgno = 0;
MDB_page *dp = NULL;
#ifdef _WIN32
#ifdef _WIN32
OVERLAPPED ov;
#else
#else
struct iovec iov[MDB_COMMIT_PAGES];
ssize_t wpos = 0, wsize = 0, wres;
size_t next_pos = 1; // impossible pos, so pos != next_pos
int n = 0;
#endif
#endif
j = i = keep;
@ -715,7 +714,7 @@ func (t *transaction) flush(keep bool) {
size = psize;
if (IS_OVERFLOW(dp)) size *= dp->mp_pages;
}
#ifdef _WIN32
#ifdef _WIN32
else break;
// Windows actually supports scatter/gather I/O, but only on
@ -733,14 +732,14 @@ func (t *transaction) flush(keep bool) {
DPRINTF(("WriteFile: %d", rc));
return rc;
}
#else
#else
// Write up to MDB_COMMIT_PAGES dirty pages at a time.
if (pos!=next_pos || n==MDB_COMMIT_PAGES || wsize+size>MAX_WRITE) {
if (n) {
// Write previous page(s)
#ifdef MDB_USE_PWRITEV
#ifdef MDB_USE_PWRITEV
wres = pwritev(env->me_fd, iov, n, wpos);
#else
#else
if (n == 1) {
wres = pwrite(env->me_fd, iov[0].iov_base, wsize, wpos);
} else {
@ -751,7 +750,7 @@ func (t *transaction) flush(keep bool) {
}
wres = writev(env->me_fd, iov, n);
}
#endif
#endif
if (wres != wsize) {
if (wres < 0) {
rc = ErrCode();
@ -775,7 +774,7 @@ func (t *transaction) flush(keep bool) {
iov[n].iov_base = (char *)dp;
wsize += size;
n++;
#endif // _WIN32
#endif // _WIN32
}
for (i = keep; ++i <= pagecount; ) {
@ -789,16 +788,16 @@ func (t *transaction) flush(keep bool) {
mdb_dpage_free(env, dp);
}
done:
done:
i--;
txn->mt_dirty_room += i - j;
dl[0].mid = j;
return MDB_SUCCESS;
}
}
int
mdb_txn_commit(MDB_txn *txn)
{
int
mdb_txn_commit(MDB_txn *txn)
{
int rc;
unsigned int i;
MDB_env *env;
@ -976,16 +975,16 @@ mdb_txn_commit(MDB_txn *txn)
if (mdb_midl_shrink(&txn->mt_free_pgs))
env->me_free_pgs = txn->mt_free_pgs;
#if (MDB_DEBUG) > 2
#if (MDB_DEBUG) > 2
mdb_audit(txn);
#endif
#endif
if ((rc = mdb_page_flush(txn, 0)) ||
(rc = mdb_env_sync(env, 0)) ||
(rc = mdb_env_write_meta(txn)))
goto fail;
done:
done:
env->me_pglast = 0;
env->me_txn = NULL;
mdb_dbis_update(txn, 1);
@ -996,7 +995,7 @@ done:
return MDB_SUCCESS;
fail:
fail:
mdb_txn_abort(txn);
return rc;
*/
@ -1014,11 +1013,11 @@ func (t *transaction) writeMeta() error {
int rc, len, toggle;
char *ptr;
HANDLE mfd;
#ifdef _WIN32
#ifdef _WIN32
OVERLAPPED ov;
#else
#else
int r2;
#endif
#endif
toggle = txn->mt_txnid & 1;
DPRINTF(("writing meta page %d for root page %"Z"u",
@ -1040,11 +1039,11 @@ func (t *transaction) writeMeta() error {
rc = (env->me_flags & MDB_MAPASYNC) ? MS_ASYNC : MS_SYNC;
ptr = env->me_map;
if (toggle) {
#ifndef _WIN32 // POSIX msync() requires ptr = start of OS page
#ifndef _WIN32 // POSIX msync() requires ptr = start of OS page
if (meta_size < env->me_os_psize)
meta_size += meta_size;
else
#endif
#endif
ptr += meta_size;
}
if (MDB_MSYNC(ptr, meta_size, rc)) {
@ -1080,16 +1079,16 @@ func (t *transaction) writeMeta() error {
// Write to the SYNC fd
mfd = env->me_flags & (MDB_NOSYNC|MDB_NOMETASYNC) ?
env->me_fd : env->me_mfd;
#ifdef _WIN32
#ifdef _WIN32
{
memset(&ov, 0, sizeof(ov));
ov.Offset = off;
if (!WriteFile(mfd, ptr, len, (DWORD *)&rc, &ov))
rc = -1;
}
#else
#else
rc = pwrite(mfd, ptr, len, off);
#endif
#endif
if (rc != len) {
rc = rc < 0 ? ErrCode() : EIO;
DPUTS("write failed, disk error?");
@ -1098,19 +1097,19 @@ func (t *transaction) writeMeta() error {
// Use the non-SYNC fd; we know it will fail anyway.
meta.mm_last_pg = metab.mm_last_pg;
meta.mm_txnid = metab.mm_txnid;
#ifdef _WIN32
#ifdef _WIN32
memset(&ov, 0, sizeof(ov));
ov.Offset = off;
WriteFile(env->me_fd, ptr, len, NULL, &ov);
#else
#else
r2 = pwrite(env->me_fd, ptr, len, off);
(void)r2; // Silence warnings. We don't care about pwrite's return value
#endif
fail:
#endif
fail:
env->me_flags |= MDB_FATAL_ERROR;
return rc;
}
done:
done:
// Memory ordering issues are irrelevant; since the entire writer
// is wrapped by wmutex, all of these changes will become visible
// after the wmutex is unlocked. Since the DB is multi-version,
@ -1174,7 +1173,7 @@ func (t *transaction) getPage(id int) (*page, int, error) {
return MDB_PAGE_NOTFOUND;
}
done:
done:
*ret = p;
if (lvl)
*lvl = level;
@ -1272,10 +1271,10 @@ func (t *transaction) Cursor(b Bucket) (Cursor, error) {
return MDB_SUCCESS;
*/
return nil
return nil, nil
}
func (t *transaction) Renew(c Cursor) error {
func (t *transaction) Renew1(c Cursor) error {
/*
if (txn == NULL || mc == NULL || mc->mc_dbi >= txn->mt_numdbs)
return EINVAL;
@ -1286,9 +1285,10 @@ func (t *transaction) Renew(c Cursor) error {
mdb_cursor_init(mc, txn, mc->mc_dbi, mc->mc_xcursor);
return MDB_SUCCESS;
*/
return nil
}
func (t *transaction) Delete(b *bucket, key []byte, data []byte) error {
func (t *transaction) Delete(b *Bucket, key []byte, data []byte) error {
/*
MDB_cursor mc;
MDB_xcursor mx;
@ -1360,9 +1360,10 @@ func (t *transaction) Put(b Bucket, key []byte, data []byte, flags int) error {
mdb_cursor_init(&mc, txn, dbi, &mx);
return mdb_cursor_put(&mc, key, data, flags);
*/
return nil
}
func (t *transaction) Bucket(name string, flags int) (Bucket, error) {
func (t *transaction) Bucket(name string, flags int) (*Bucket, error) {
/*
MDB_val key, data;
MDB_dbi i;
@ -1467,16 +1468,19 @@ func (t *transaction) Bucket(name string, flags int) (Bucket, error) {
}
func (t *transaction) Stat(b Bucket) *Stat {
/*
if (txn == NULL || arg == NULL || dbi >= txn->mt_numdbs)
return EINVAL;
if (txn->mt_dbflags[dbi] & DB_STALE) {
MDB_cursor mc;
MDB_xcursor mx;
/* Stale, must read the DB's root. cursor_init does it for us. */
// Stale, must read the DB's root. cursor_init does it for us.
mdb_cursor_init(&mc, txn, dbi, &mx);
}
return mdb_stat0(txn->mt_env, &txn->mt_dbs[dbi], arg);
*/
return nil
}
func (t *transaction) BucketFlags(b Bucket) (int, error) {
@ -1490,7 +1494,7 @@ func (t *transaction) BucketFlags(b Bucket) (int, error) {
return 0, nil
}
func (t *transaction) Drop(b Bucket int del) error {
func (t *transaction) Drop(b *Bucket, del int) error {
/*
MDB_cursor *mc, *m2;
int rc;
@ -1531,7 +1535,7 @@ func (t *transaction) Drop(b Bucket int del) error {
txn->mt_flags |= MDB_TXN_DIRTY;
}
leave:
leave:
mdb_cursor_close(mc);
return rc;
*/