Skip to content

Commit

Permalink
wip version
Browse files Browse the repository at this point in the history
  • Loading branch information
Ilya Miheev committed Jun 12, 2024
1 parent 2cc4e87 commit 45c9fd2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
8 changes: 7 additions & 1 deletion mdbx/mdbxgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ int mdbxgo_del(MDBX_txn *txn, MDBX_dbi dbi, char *kdata, size_t kn, char *vdata,
return mdbx_del(txn, dbi, &key, NULL);
}

int mdbxgo_get(MDBX_txn *txn, MDBX_dbi dbi, char *kdata, size_t kn, MDBX_val *val) {
int mdbxgo_get(MDBX_txn *txn, MDBX_dbi dbi, char* kdata, size_t kn, MDBX_val *val) {
MDBX_val key;
MDBXGO_SET_VAL(&key, kn, kdata);
return mdbx_get(txn, dbi, &key, val);
}

int mdbxgo_getfast(MDBX_txn *txn, MDBX_dbi dbi, char kdata[128], MDBX_val *val) {
MDBX_val key;
MDBXGO_SET_VAL(&key, 128, kdata);
return mdbx_get(txn, dbi, &key, val);
}

int mdbxgo_put2(MDBX_txn *txn, MDBX_dbi dbi, char *kdata, size_t kn, char *vdata, size_t vn, MDBX_put_flags_t flags) {
MDBX_val key, val;
MDBXGO_SET_VAL(&key, kn, kdata);
Expand Down
1 change: 1 addition & 0 deletions mdbx/mdbxgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* */
int mdbxgo_del(MDBX_txn *txn, MDBX_dbi dbi, char *kdata, size_t kn, char *vdata, size_t vn);
int mdbxgo_get(MDBX_txn *txn, MDBX_dbi dbi, char *kdata, size_t kn, MDBX_val *val);
int mdbxgo_getfast(MDBX_txn *txn, MDBX_dbi dbi, char kdata[128], MDBX_val *val);
int mdbxgo_put1(MDBX_txn *txn, MDBX_dbi dbi, char *kdata, size_t kn, MDBX_val *val, MDBX_put_flags_t flags);
int mdbxgo_put2(MDBX_txn *txn, MDBX_dbi dbi, char *kdata, size_t kn, char *vdata, size_t vn, MDBX_put_flags_t flags);
int mdbxgo_cursor_put1(MDBX_cursor *cur, char *kdata, size_t kn, MDBX_val *val, MDBX_put_flags_t flags);
Expand Down
20 changes: 20 additions & 0 deletions mdbx/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,26 @@ func (txn *Txn) Get(dbi DBI, key []byte) ([]byte, error) {
txn.val,
)
err := operrno("mdbx_get", ret)
if err != nil {
//*txn.val = C.MDBX_val{}
return nil, err
}
b := castToBytes(txn.val)
//*txn.val = C.MDBX_val{}
return b, nil
}

func (txn *Txn) GetFast(dbi DBI, key [128]byte) ([]byte, error) {
var ckey [128]C.char
for i, v := range key {
ckey[i] = C.char(v)
}
ret := C.mdbxgo_getfast(
txn._txn, C.MDBX_dbi(dbi),
ckey,

Check failure on line 615 in mdbx/txn.go

View workflow job for this annotation

GitHub Actions / win (windows-2022)

cannot use ckey (variable of type [128]_Ctype_char) as *_Ctype_char value in variable declaration

Check failure on line 615 in mdbx/txn.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-20.04)

cannot use ckey (variable of type [128]_Ctype_char) as *_Ctype_char value in variable declaration

Check failure on line 615 in mdbx/txn.go

View workflow job for this annotation

GitHub Actions / test (macos-13)

cannot use ckey (variable of type [128]_Ctype_char) as *_Ctype_char value in variable declaration
txn.val,
)
err := operrno("mdbx_getfast", ret)
if err != nil {
*txn.val = C.MDBX_val{}
return nil, err
Expand Down
22 changes: 18 additions & 4 deletions mdbx/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1263,19 +1263,33 @@ func openDBI(env *Env, key string, flags uint) (DBI, error) {
return db, nil
}

func PutUint64(b [128]byte, v uint64) {
_ = b[7] // early bounds check to guarantee safety of writes below
b[0] = byte(v >> 56)
b[1] = byte(v >> 48)
b[2] = byte(v >> 40)
b[3] = byte(v >> 32)
b[4] = byte(v >> 24)
b[5] = byte(v >> 16)
b[6] = byte(v >> 8)
b[7] = byte(v)
}

func BenchmarkTxn_Get(b *testing.B) {
env, _ := setup(b)

var db DBI
k := make([]byte, 8)
binary.BigEndian.PutUint64(k, uint64(1))
k := [128]byte{}
kSl := make([]byte, 8)
binary.BigEndian.PutUint64(kSl, uint64(1))
PutUint64(k, uint64(1))

if err := env.Update(func(txn *Txn) (err error) {
db, err = txn.OpenRoot(0)
if err != nil {
return err
}
err = txn.Put(db, k, k, 0)
err = txn.Put(db, kSl, kSl, 0)
if err != nil {
return err
}
Expand All @@ -1288,7 +1302,7 @@ func BenchmarkTxn_Get(b *testing.B) {
if err := env.View(func(txn *Txn) (err error) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := txn.Get(db, k)
_, err := txn.GetFast(db, k)
if err != nil {
return err
}
Expand Down

0 comments on commit 45c9fd2

Please sign in to comment.