Skip to content

Commit

Permalink
mdbx_env_warmup initial commit (#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
racytech authored Jul 19, 2023
1 parent 5ab3def commit 8d98de7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
32 changes: 14 additions & 18 deletions mdbx/mdbxgo.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "mdbx.h"

#define MDBXGO_SET_VAL(val, size, data) \
*(val) = (MDBX_val){.iov_len = (size), .iov_base = (data)}
*(val) = (MDBX_val){ .iov_len = (size), .iov_base = (data) }

int mdbxgo_msg_func_proxy(const char *msg, void *ctx) {
// wrap msg and call the bridge function exported from lmdb.go.
Expand All @@ -17,13 +17,13 @@ int mdbxgo_msg_func_proxy(const char *msg, void *ctx) {
return mdbxgoMDBMsgFuncBridge(s, (size_t)ctx);
}

//int mdbxgo_reader_list(MDBX_env *env, size_t ctx) {
// // list readers using a static proxy function that does dynamic dispatch on
// // ctx.
// int mdbxgo_reader_list(MDBX_env *env, size_t ctx) {
// // list readers using a static proxy function that does dynamic dispatch on
// // ctx.
// if (ctx)
// return mdbx_reader_list(env, &mdbxgo_msg_func_proxy, (void *)ctx);
// return mdbx_reader_list(env, 0, (void *)ctx);
//}
// }

int mdbxgo_del(MDBX_txn *txn, MDBX_dbi dbi, char *kdata, size_t kn, char *vdata, size_t vn) {
MDBX_val key, val;
Expand Down Expand Up @@ -87,15 +87,15 @@ int mdbxgo_cursor_get2(MDBX_cursor *cur, char *kdata, size_t kn, char *vdata, si
}

/* Compare two items lexically */
//static int __hot cmp_lexical(const MDBX_val *a, const MDBX_val *b) {
// if (a->iov_len == b->iov_len)
// return memcmp(a->iov_base, b->iov_base, a->iov_len);
// static int __hot cmp_lexical(const MDBX_val *a, const MDBX_val *b) {
// if (a->iov_len == b->iov_len)
// return memcmp(a->iov_base, b->iov_base, a->iov_len);
//
// const int diff_len = (a->iov_len < b->iov_len) ? -1 : 1;
// const size_t shortest = (a->iov_len < b->iov_len) ? a->iov_len : b->iov_len;
// int diff_data = memcmp(a->iov_base, b->iov_base, shortest);
// return likely(diff_data) ? diff_data : diff_len;
//}
// const int diff_len = (a->iov_len < b->iov_len) ? -1 : 1;
// const size_t shortest = (a->iov_len < b->iov_len) ? a->iov_len : b->iov_len;
// int diff_data = memcmp(a->iov_base, b->iov_base, shortest);
// return likely(diff_data) ? diff_data : diff_len;
// }

int mdbxgo_cmp(MDBX_txn *txn, MDBX_dbi dbi, char *adata, size_t an, char *bdata, size_t bn) {
MDBX_val a;
Expand All @@ -111,8 +111,4 @@ int mdbxgo_dcmp(MDBX_txn *txn, MDBX_dbi dbi, char *adata, size_t an, char *bdata
MDBX_val b;
MDBXGO_SET_VAL(&b, bn, bdata);
return mdbx_dcmp(txn, dbi, &a, &b);
}




}
4 changes: 1 addition & 3 deletions mdbx/mdbxgo.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ int mdbxgo_cursor_put2(MDBX_cursor *cur, char *kdata, size_t kn, char *vdata, si
int mdbxgo_cursor_putmulti(MDBX_cursor *cur, char *kdata, size_t kn, char *vdata, size_t vn, size_t vstride, MDBX_put_flags_t flags);
int mdbxgo_cursor_get1(MDBX_cursor *cur, char *kdata, size_t kn, MDBX_val *key, MDBX_val *val, MDBX_cursor_op op);
int mdbxgo_cursor_get2(MDBX_cursor *cur, char *kdata, size_t kn, char *vdata, size_t vn, MDBX_val *key, MDBX_val *val, MDBX_cursor_op op);

/* ConstCString wraps a null-terminated (const char *) because Go's type system
* does not represent the 'cosnt' qualifier directly on a function argument and
* causes warnings to be emitted during linking.
* */
typedef struct{ const char *p; } mdbxgo_ConstCString;
typedef struct { const char *p; } mdbxgo_ConstCString;

/* mdbxgo_reader_list is a proxy for mdb_reader_list that uses a special
* mdb_msg_func proxy function to relay messages over the
* mdbxgo_reader_list_bridge external Go func.
* */
int mdbxgo_reader_list(MDBX_env *env, size_t ctx);


int mdbxgo_cmp(MDBX_txn *txn, MDBX_dbi dbi, char *adata, size_t an, char *bdata, size_t bn);
int mdbxgo_dcmp(MDBX_txn *txn, MDBX_dbi dbi, char *adata, size_t an, char *bdata, size_t bn);

Expand Down
18 changes: 18 additions & 0 deletions mdbx/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ const (
TxNoSync = C.MDBX_TXN_NOSYNC
)

const (
WarmupDefault = C.MDBX_warmup_default
WarmupForce = C.MDBX_warmup_force
WarmupOomSafe = C.MDBX_warmup_oomsafe
WarmupLock = C.MDBX_warmup_lock
WarmupTouchLimit = C.MDBX_warmup_touchlimit
WarmupRelease = C.MDBX_warmup_release
)

// Txn is a database transaction in an environment.
//
// WARNING: A writable Txn is not threadsafe and may only be used in the
Expand Down Expand Up @@ -761,3 +770,12 @@ func (txn *Txn) ListDBI() (res []string, err error) {
}
return res, nil
}

func (txn *Txn) EnvWarmup(flags, timeout uint) error {
ret := C.mdbx_env_warmup(
txn.env._env, txn._txn,
C.MDBX_warmup_flags_t(flags),
C.uint(timeout),
)
return operrno("mdbx_env_warmup", ret)
}
16 changes: 16 additions & 0 deletions mdbx/txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,3 +1262,19 @@ func openDBI(env *Env, key string, flags uint) (DBI, error) {
}
return db, nil
}

func TestTxnEnvWarmup(t *testing.T) {
env, _ := setup(t)

txn, err := env.BeginTxn(nil, EnvDefaults)
if err != nil {
t.Errorf("%s", err)
return
}
err = txn.EnvWarmup(WarmupDefault, 2)
if err != nil {
t.Errorf("%s", err)
return
}
defer txn.Abort()
}

0 comments on commit 8d98de7

Please sign in to comment.