Beispiel #1
0
Datei: txn.go Projekt: hyc/gomdb
func (txn *Txn) Del(dbi DBI, key, val []byte) error {
	ckey := Wrap(key)
	if val == nil {
		ret := C.mdb_del(txn._txn, C.MDB_dbi(dbi), (*C.MDB_val)(&ckey), nil)
		return errno(ret)
	}
	cval := Wrap(val)
	ret := C.mdb_del(txn._txn, C.MDB_dbi(dbi), (*C.MDB_val)(&ckey), (*C.MDB_val)(&cval))
	return errno(ret)
}
Beispiel #2
0
// SetMaxDBs sets the maximum number of named databases for the environment.
//
// See mdb_env_set_maxdbs.
func (env *Env) SetMaxDBs(size int) error {
	if size < 0 {
		return errNegSize
	}
	ret := C.mdb_env_set_maxdbs(env._env, C.MDB_dbi(size))
	return operrno("mdb_env_set_maxdbs", ret)
}
Beispiel #3
0
func (env *Env) SetMaxDBs(size DBI) error {
	ret := C.mdb_env_set_maxdbs(env._env, C.MDB_dbi(size))
	if ret != SUCCESS {
		return Errno(ret)
	}
	return nil
}
Beispiel #4
0
// Do NOT call Free() on the result *Val
func (txn *Txn) GetVal(dbi DBI, key []byte) (*Val, error) {
	ckey := Wrap(key)
	defer ckey.Free()
	var cval Val
	ret := C.mdb_get(txn.txn, C.MDB_dbi(dbi), (*C.MDB_val)(ckey), (*C.MDB_val)(&cval))
	return &cval, errno(ret)
}
Beispiel #5
0
func (txn *Txn) Drop(dbi DBI, del int) error {
	ret := C.mdb_drop(txn._txn, C.MDB_dbi(dbi), C.int(del))
	if ret != SUCCESS {
		return Errno(ret)
	}
	return nil
}
Beispiel #6
0
func (txn *Txn) CursorOpen(dbi DBI) (*Cursor, error) {
	var _cursor *C.MDB_cursor
	ret := C.mdb_cursor_open(txn._txn, C.MDB_dbi(dbi), &_cursor)
	if ret != SUCCESS {
		return nil, Errno(ret)
	}
	return &Cursor{_cursor}, nil
}
Beispiel #7
0
func (txn *Txn) Put(dbi DBI, key []byte, val []byte, flags uint) error {
	ckey := Wrap(key)
	cval := Wrap(val)
	defer ckey.Free()
	defer cval.Free()
	ret := C.mdb_put(txn.txn, C.MDB_dbi(dbi), (*C.MDB_val)(ckey), (*C.MDB_val)(cval), C.uint(flags))
	return errno(ret)
}
Beispiel #8
0
func openCursor(txn *Txn, db DBI) (*Cursor, error) {
	c := &Cursor{txn: txn}
	ret := C.mdb_cursor_open(txn._txn, C.MDB_dbi(db), &c._c)
	if ret != success {
		return nil, operrno("mdb_cursor_open", ret)
	}
	return c, nil
}
Beispiel #9
0
// Del deletes an item from database dbi.  Del ignores val unless dbi has the
// DupSort flag.
//
// See mdb_del.
func (txn *Txn) Del(dbi DBI, key, val []byte) error {
	kdata, kn := valBytes(key)
	vdata, vn := valBytes(val)
	ret := C.lmdbgo_mdb_del(
		txn._txn, C.MDB_dbi(dbi),
		(*C.char)(unsafe.Pointer(&kdata[0])), C.size_t(kn),
		(*C.char)(unsafe.Pointer(&vdata[0])), C.size_t(vn),
	)
	return operrno("mdb_del", ret)
}
Beispiel #10
0
func (txn *Txn) Del(dbi DBI, key, val []byte) error {
	ckey := Wrap(key)
	defer ckey.Free()
	var cval *Val
	if val != nil {
		cval = Wrap(val)
		defer cval.Free()
	}
	ret := C.mdb_del(txn.txn, C.MDB_dbi(dbi), (*C.MDB_val)(ckey), (*C.MDB_val)(cval))
	return errno(ret)
}
Beispiel #11
0
func (txn *Txn) Get(dbi DBI, key []byte) ([]byte, error) {
	ckey := &C.MDB_val{mv_size: C.size_t(len(key)),
		mv_data: unsafe.Pointer(&key[0])}
	var cval *C.MDB_val
	ret := C.mdb_get(txn._txn, C.MDB_dbi(dbi), ckey, cval)
	if ret != SUCCESS {
		return nil, Errno(ret)
	}
	val := C.GoBytes(cval.mv_data, C.int(cval.mv_size))
	return val, nil
}
Beispiel #12
0
func (txn *Txn) Put(dbi DBI, key []byte, val []byte, flags uint) error {
	ckey := &C.MDB_val{mv_size: C.size_t(len(key)),
		mv_data: unsafe.Pointer(&key[0])}
	cval := &C.MDB_val{mv_size: C.size_t(len(val)),
		mv_data: unsafe.Pointer(&val[0])}
	ret := C.mdb_put(txn._txn, C.MDB_dbi(dbi), ckey, cval, C.uint(flags))
	if ret != SUCCESS {
		return Errno(ret)
	}
	return nil
}
Beispiel #13
0
func (txn *Txn) Stat(dbi DBI) (*Stat, error) {
	var _stat C.MDB_stat
	ret := C.mdb_stat(txn._txn, C.MDB_dbi(dbi), &_stat)
	if ret != SUCCESS {
		return nil, Errno(ret)
	}
	stat := Stat{PSize: uint(_stat.ms_psize),
		Depth:         uint(_stat.ms_depth),
		BranchPages:   uint64(_stat.ms_branch_pages),
		LeafPages:     uint64(_stat.ms_leaf_pages),
		OwerflowPages: uint64(_stat.ms_overflow_pages),
		Entries:       uint64(_stat.ms_entries)}
	return &stat, nil
}
Beispiel #14
0
// Get retrieves items from database dbi.  If txn.RawRead is true the slice
// returned by Get references a readonly section of memory that must not be
// accessed after txn has terminated.
//
// See mdb_get.
func (txn *Txn) Get(dbi DBI, key []byte) ([]byte, error) {
	kdata, kn := valBytes(key)
	ret := C.lmdbgo_mdb_get(
		txn._txn, C.MDB_dbi(dbi),
		(*C.char)(unsafe.Pointer(&kdata[0])), C.size_t(kn),
		txn.val,
	)
	err := operrno("mdb_get", ret)
	if err != nil {
		*txn.val = C.MDB_val{}
		return nil, err
	}
	b := txn.bytes(txn.val)
	*txn.val = C.MDB_val{}
	return b, nil
}
Beispiel #15
0
func (txn *Txn) Del(dbi DBI, key, val []byte) error {
	ckey := &C.MDB_val{mv_size: C.size_t(len(key)),
		mv_data: unsafe.Pointer(&key[0])}
	var cval *C.MDB_val
	if val == nil {
		cval = nil
	} else {
		cval = &C.MDB_val{mv_size: C.size_t(len(val)),
			mv_data: unsafe.Pointer(&val[0])}
	}
	ret := C.mdb_del(txn._txn, C.MDB_dbi(dbi), ckey, cval)
	if ret != SUCCESS {
		return Errno(ret)
	}
	return nil
}
Beispiel #16
0
// Put stores an item in database dbi.
//
// See mdb_put.
func (txn *Txn) Put(dbi DBI, key []byte, val []byte, flags uint) error {
	kn := len(key)
	if kn == 0 {
		return txn.putNilKey(dbi, flags)
	}
	vn := len(val)
	if vn == 0 {
		val = []byte{0}
	}

	ret := C.lmdbgo_mdb_put2(
		txn._txn, C.MDB_dbi(dbi),
		(*C.char)(unsafe.Pointer(&key[0])), C.size_t(kn),
		(*C.char)(unsafe.Pointer(&val[0])), C.size_t(vn),
		C.uint(flags),
	)
	return operrno("mdb_put", ret)
}
Beispiel #17
0
// PutReserve returns a []byte of length n that can be written to, potentially
// avoiding a memcopy.  The returned byte slice is only valid in txn's thread,
// before it has terminated.
func (txn *Txn) PutReserve(dbi DBI, key []byte, n int, flags uint) ([]byte, error) {
	if len(key) == 0 {
		return nil, txn.putNilKey(dbi, flags)
	}
	txn.val.mv_size = C.size_t(n)
	ret := C.lmdbgo_mdb_put1(
		txn._txn, C.MDB_dbi(dbi),
		(*C.char)(unsafe.Pointer(&key[0])), C.size_t(len(key)),
		txn.val,
		C.uint(flags|C.MDB_RESERVE),
	)
	err := operrno("mdb_put", ret)
	if err != nil {
		*txn.val = C.MDB_val{}
		return nil, err
	}
	b := getBytes(txn.val)
	*txn.val = C.MDB_val{}
	return b, nil
}
Beispiel #18
0
Datei: txn.go Projekt: hyc/gomdb
func (txn *Txn) Drop(dbi DBI, del int) error {
	ret := C.mdb_drop(txn._txn, C.MDB_dbi(dbi), C.int(del))
	return errno(ret)
}
Beispiel #19
0
func (txn *Txn) putNilKey(dbi DBI, flags uint) error {
	// mdb_put with an empty key will always fail
	ret := C.lmdbgo_mdb_put2(txn._txn, C.MDB_dbi(dbi), nil, 0, nil, 0, C.uint(flags))
	return operrno("mdb_put", ret)
}
Beispiel #20
0
// Drop empties the database if del is false.  Drop deletes and closes the
// database if del is true.
//
// See mdb_drop.
func (txn *Txn) Drop(dbi DBI, del bool) error {
	ret := C.mdb_drop(txn._txn, C.MDB_dbi(dbi), cbool(del))
	return operrno("mdb_drop", ret)
}
Beispiel #21
0
// CloseDBI closes the database handle, db.  Normally calling CloseDBI
// explicitly is not necessary.
//
// It is the caller's responsibility to serialize calls to CloseDBI.
//
// See mdb_dbi_close.
func (env *Env) CloseDBI(db DBI) {
	C.mdb_dbi_close(env._env, C.MDB_dbi(db))
}
Beispiel #22
0
Datei: env.go Projekt: ngaut/bmdb
func (env *Env) SetMaxDBs(size DBI) error {
	ret := C.mdb_env_set_maxdbs(env._env, C.MDB_dbi(size))
	return errno(ret)
}
Beispiel #23
0
Datei: txn.go Projekt: hyc/gomdb
func (txn *Txn) GetVal(dbi DBI, key []byte) (Val, error) {
	ckey := Wrap(key)
	var cval Val
	ret := C.mdb_get(txn._txn, C.MDB_dbi(dbi), (*C.MDB_val)(&ckey), (*C.MDB_val)(&cval))
	return cval, errno(ret)
}
Beispiel #24
0
func (env *Env) DBIClose(dbi DBI) {
	C.mdb_dbi_close(env._env, C.MDB_dbi(dbi))
}
Beispiel #25
0
// Flags returns the database flags for handle dbi.
func (txn *Txn) Flags(dbi DBI) (uint, error) {
	var cflags C.uint
	ret := C.mdb_dbi_flags(txn._txn, C.MDB_dbi(dbi), (*C.uint)(&cflags))
	return uint(cflags), operrno("mdb_dbi_flags", ret)
}