Skip to content

Commit

Permalink
Refinements to update for version 0.12.5
Browse files Browse the repository at this point in the history
  • Loading branch information
castortech committed Apr 24, 2023
1 parent ae63033 commit 03a35de
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 26 deletions.
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/build.sh=UTF-8
18 changes: 9 additions & 9 deletions mdbxjni/src/main/java/com/castortech/mdbxjni/Cursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void close() {
* transaction handle
*/
public void renew(Transaction tx) {
checkErrorCode(env, mdbx_cursor_renew(tx.pointer(), pointer()));
checkErrorCode(env, tx, mdbx_cursor_renew(tx.pointer(), pointer()));
}

/**
Expand All @@ -96,7 +96,7 @@ public Entry get(CursorOp op) {
if (rc == MDBX_NOTFOUND) {
return null;
}
checkErrorCode(env, rc);
checkErrorCode(env, tx, rc);
return new Entry(key.toByteArray(), value.toByteArray());
}

Expand All @@ -110,7 +110,7 @@ public Entry get(CursorOp op, byte[] key) {
if (rc == MDBX_NOTFOUND) {
return null;
}
checkErrorCode(env, rc);
checkErrorCode(env, tx, rc);
return new Entry(keyValue.toByteArray(), value.toByteArray());
}
finally {
Expand All @@ -131,7 +131,7 @@ public Entry get(CursorOp op, byte[] key, byte[] value) {
if (rc == MDBX_NOTFOUND) {
return null;
}
checkErrorCode(env, rc);
checkErrorCode(env, tx, rc);
return new Entry(keyValue.toByteArray(), valValue.toByteArray());
}
finally {
Expand All @@ -154,7 +154,7 @@ public OperationStatus get(CursorOp op, DatabaseEntry key, DatabaseEntry value)
if (rc == MDBX_NOTFOUND) {
return OperationStatus.NOTFOUND;
}
checkErrorCode(env, rc);
checkErrorCode(env, tx, rc);
key.setData(keyValue.toByteArray());
value.setData(valValue.toByteArray());
return OperationStatus.SUCCESS;
Expand Down Expand Up @@ -262,7 +262,7 @@ private byte[] put(Value keySlice, Value valueSlice, int flags) {
}
else {
// If the put failed, throw an exception..
checkErrorCode(env, rc);
checkErrorCode(env, tx, rc);

if (!valueSlices.isEmpty()) {
db.deleteSecondaries(tx, keySlice, valueSlices);
Expand All @@ -289,7 +289,7 @@ public void delete() {
}

int rc = mdbx_cursor_del(pointer(), 0);
checkErrorCode(env, rc);
checkErrorCode(env, tx, rc);

if (hasSec) {
for (SecondaryDatabase secDb : db.getSecondaries()) {
Expand All @@ -311,7 +311,7 @@ public void delete() {
* May only be called if the database was opened with {@link com.castortech.mdbxjni.Constants#DUPSORT}.
*/
public void deleteIncludingDups() {
checkErrorCode(env, mdbx_cursor_del(pointer(), MDBX_NODUPDATA));
checkErrorCode(env, tx, mdbx_cursor_del(pointer(), MDBX_NODUPDATA));
}

/**
Expand All @@ -326,7 +326,7 @@ public void deleteIncludingDups() {
*/
public long count() {
long[] rc = new long[1];
checkErrorCode(env, mdbx_cursor_count(pointer(), rc));
checkErrorCode(env, tx, mdbx_cursor_count(pointer(), rc));
return rc[0];
}

Expand Down
33 changes: 31 additions & 2 deletions mdbxjni/src/main/java/com/castortech/mdbxjni/EnvInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public class EnvInfo {

pgopStat = new PgopStat(rc.mi_pgop_stat_newly, rc.mi_pgop_stat_cow, rc.mi_pgop_stat_clone,
rc.mi_pgop_stat_split, rc.mi_pgop_stat_merge, rc.mi_pgop_stat_spill, rc.mi_pgop_stat_unspill,
rc.mi_pgop_stat_wops);
rc.mi_pgop_stat_wops, rc.mi_pgop_stat_prefault, rc.mi_pgop_stat_mincore, rc.mi_pgop_stat_msync,
rc.mi_pgop_stat_fsync);
}

public Geo getGeo() {
Expand Down Expand Up @@ -338,9 +339,13 @@ public class PgopStat {
private final long spill;
private final long unspill;
private final long wops;
private final long prefault;
private final long mincore;
private final long msync;
private final long fsync;

public PgopStat(long newly, long cow, long clone, long split, long merge, long spill, long unspill,
long wops) {
long wops, long prefault, long mincore, long msync, long fsync) {
super();
this.newly = newly;
this.cow = cow;
Expand All @@ -350,6 +355,10 @@ public PgopStat(long newly, long cow, long clone, long split, long merge, long s
this.spill = spill;
this.unspill = unspill;
this.wops = wops;
this.prefault = prefault;
this.mincore = mincore;
this.msync = msync;
this.fsync = fsync;
}

public long getNewly() {
Expand Down Expand Up @@ -384,6 +393,22 @@ public long getWops() {
return wops;
}

public long getPrefault() {
return prefault;
}

public long getMincore() {
return mincore;
}

public long getMsync() {
return msync;
}

public long getFsync() {
return fsync;
}

@SuppressWarnings("nls")
@Override
public String toString() {
Expand All @@ -396,6 +421,10 @@ public String toString() {
", spill=" + spill +
", unspill=" + unspill +
", wops=" + wops +
", prefault=" + prefault +
", mincore=" + mincore +
", msync=" + msync +
", fsync=" + fsync +
"}";
}
}
Expand Down
18 changes: 9 additions & 9 deletions mdbxjni/src/main/java/com/castortech/mdbxjni/MDBXException.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@ public class MDBXException extends RuntimeException {
@SuppressWarnings("nls")
public enum Status {
OK(0, "OK"),
ENODATA(JNI.MDBX_ENODATA, "Handle EOF"),
EINVAL(JNI.MDBX_EINVAL, "Invalid Parameter"),
EACCES(JNI.MDBX_EACCESS, "Access Denied"),
ENODATA(JNI.MDBX_ENODATA, "Handle EOF"),
ENOMEM(JNI.MDBX_ENOMEM, "Out of Memory"),
EROFS(JNI.MDBX_EROFS, "File Read Only"),
ENOSYS(JNI.MDBX_ENOSYS, "Not Supported"),
EIO(JNI.MDBX_EIO, "Write Fault"),
EPERM(JNI.MDBX_EPERM, "Invalid Function"),
EINTR(JNI.MDBX_EINTR, "Cancelled"),
ENOFILE(JNI.MDBX_ENOFILE, "File not found"),
EREMOTE(JNI.MDBX_EREMOTE, "Remote storage media error"),
RESULT_FALSE(JNI.MDBX_RESULT_FALSE, "Alias for Successful result"),
RESULT_TRUE(JNI.MDBX_RESULT_TRUE, "Successful result with special meaning or a flag"),
KEYEXIST(JNI.MDBX_KEYEXIST, "key/data pair already exists"),
FIRST_LMDB_ERRCODE(JNI.MDBX_FIRST_LMDB_ERRCODE, "The first LMDB-compatible defined error code"),
NOTFOUND(JNI.MDBX_NOTFOUND, "key/data pair not found (EOF)"),
PAGE_NOTFOUND(JNI.MDBX_PAGE_NOTFOUND, "Requested page not found - this usually indicates corruption"),
CORRUPTED(JNI.MDBX_CORRUPTED, "Located page was wrong type"),
Expand All @@ -51,29 +56,24 @@ public enum Status {
TXN_FULL(JNI.MDBX_TXN_FULL, "Transaction has too many dirty pages"),
CURSOR_FULL(JNI.MDBX_CURSOR_FULL, "Cursor stack too deep - internal error"),
PAGE_FULL(JNI.MDBX_PAGE_FULL, "Page has not enough space - internal error"),
UNABLE_EXTEND_MAPSIZE(JNI.MDBX_UNABLE_EXTEND_MAPSIZE, "Database engine was unable to extend mapping, e.g. since address space is unavailable or busy"),
INCOMPATIBLE(JNI.MDBX_INCOMPATIBLE, "Operation and DB incompatible, or DB type changed."),
BAD_RSLOT(JNI.MDBX_BAD_RSLOT, "Invalid reuse of reader locktable slot"),
BAD_TXN(JNI.MDBX_BAD_TXN, "Transaction must abort, has a child, or is invalid"),
BAD_VALSIZE(JNI.MDBX_BAD_VALSIZE, "Unsupported size of key/DB name/data, or wrong DUPFIXED size"),
BAD_DBI(JNI.MDBX_BAD_DBI, "The specified DBI was changed unexpectedly"),
PROBLEM(JNI.MDBX_PROBLEM, "Unexpected problem - Transaction should abort"),
LAST_LMDB_ERRCODE(JNI.MDBX_LAST_LMDB_ERRCODE, "The last LMDB-compatible defined error code"),
BUSY(JNI.MDBX_BUSY, "Another write transaction is running"),
FIRST_ADDED_ERRCODE(JNI.MDBX_FIRST_ADDED_ERRCODE, "The first of MDBX-added error codes"),
EMULTIVAL(JNI.MDBX_EMULTIVAL, "The mdbx_put() or mdbx_replace() was called for key, that has more that one associated value."),
BAD_SIGNATGURE(JNI.MDBX_EBADSIGN, "Bad signature of a runtime object(s), this can mean: - memory corruption or double-free; - ABI version mismatch (rare case)"),
WANNA_RECOVERY(JNI.MDBX_WANNA_RECOVERY, "Database should be recovered, but this could NOT be done automatically right now (e.g. in readonly mode and so forth)."),
KE_YMISMATCH(JNI.MDBX_EKEYMISMATCH, "The given key value is mismatched to the current cursor position, when mdbx_cursor_put() called with MDBX_CURRENT option."),
TOO_LARGE(JNI.MDBX_TOO_LARGE, "Database is too large for current system, e.g. could NOT be mapped into RAM."),
THREAD_MISMATCH(JNI.MDBX_THREAD_MISMATCH, "A thread has attempted to use a not owned object, e.g. a transaction that started by another thread."),
RESULT_FALSE(JNI.MDBX_RESULT_FALSE, "Alias for Successful result"),
RESULT_TRUE(JNI.MDBX_RESULT_TRUE, "Successful result with special meaning or a flag"),
FIRST_LMDB_ERRCODE(JNI.MDBX_FIRST_LMDB_ERRCODE, "The first LMDB-compatible defined error code"),
UNABLE_EXTEND_MAPSIZE(JNI.MDBX_UNABLE_EXTEND_MAPSIZE, "Database engine was unable to extend mapping, e.g. since address space is unavailable or busy"),
LAST_LMDB_ERRCODE(JNI.MDBX_LAST_LMDB_ERRCODE, "The last LMDB-compatible defined error code"),
FIRST_ADDED_ERRCODE(JNI.MDBX_FIRST_ADDED_ERRCODE, "The first of MDBX-added error codes"),
TXN_OVERLAPPING(JNI.MDBX_TXN_OVERLAPPING, "Overlapping read and write transactions for the current thread"),
LAST_ADDED_ERRCODE(JNI.MDBX_LAST_ADDED_ERRCODE, "The last added error code"),
ENOFILE(JNI.MDBX_ENOFILE, "?? no description"),
EREMOTE(JNI.MDBX_EREMOTE, "?? no description"),
;

private final int code;
Expand Down
10 changes: 5 additions & 5 deletions mdbxjni/src/main/java/com/castortech/mdbxjni/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public long getId() {
* must be called before a reset transaction may be used again.
*/
public void renew() {
checkErrorCode(env, mdbx_txn_renew(pointer()));
checkErrorCode(env, this, mdbx_txn_renew(pointer()));
}

/**
Expand All @@ -78,15 +78,15 @@ public void renew() {
*/
public void commit() {
if (self != 0) {
checkErrorCode(env, mdbx_txn_commit(self));
checkErrorCode(env, this, mdbx_txn_commit(self));
self = 0;
}
}

public CommitLatency commitWithLatency() {
if (self != 0) {
MDBX_commit_latency rc = new MDBX_commit_latency();
checkErrorCode(env, mdbx_txn_commit_ex(self, rc));
checkErrorCode(env, this, mdbx_txn_commit_ex(self, rc));
self = 0;
return new CommitLatency(rc);
}
Expand Down Expand Up @@ -132,12 +132,12 @@ public void abort() {
}

public void broken() {
checkErrorCode(env, mdbx_txn_break(pointer()));
checkErrorCode(env, this, mdbx_txn_break(pointer()));
}

public TxnInfo info(boolean scanRlt) {
MDBX_txn_info rc = new MDBX_txn_info();
checkErrorCode(env, mdbx_txn_info(pointer(), rc, scanRlt ? 1 : 0));
checkErrorCode(env, this, mdbx_txn_info(pointer(), rc, scanRlt ? 1 : 0));
return new TxnInfo(rc);
}

Expand Down
11 changes: 11 additions & 0 deletions mdbxjni/src/main/java/com/castortech/mdbxjni/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
*
* @author <a href="http://hiramchirino.com">Hiram Chirino</a>
*/
@SuppressWarnings("nls")
class Util {
public static final boolean isAndroid = isAndroid();

Expand Down Expand Up @@ -57,6 +58,16 @@ public static void checkErrorCode(Env env, int rc) {
}
}

public static void checkErrorCode(Env env, Transaction txn, int rc) {
if (rc != JNI.MDBX_SUCCESS && rc != JNI.MDBX_RESULT_TRUE) {
String msg = string(mdbx_strerror(rc));
if (env != null) {
System.err.println("MDBX Exception. Msg:" + msg + ", Env:" + env.info(txn).toString());
}
throw new MDBXException(msg, rc);
}
}

public static void checkSize(final Env env, final Value val) {
long size = val.getOffendingSize(env.getMaxKeySize());

Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Description

MDBX JNI gives you a Java interface to the
[libmdbx](https://github.com/leo-yuriev/libmdbx) library
[libmdbx](https://gitflic.ru/project/leo-yuriev/libmdbx) library
which is a fast key-value storage library written for ReOpenLDAP project
that provides an ordered mapping from string keys to string values.

Expand Down

0 comments on commit 03a35de

Please sign in to comment.