Rework the JNI wrapper1 variants of status() and db_status() to be more Java-esque.

FossilOrigin-Name: 40ad3920673561a06edf0b70a50a40be6cd20817fe22b87b63a9ac80cb2c9df8
This commit is contained in:
stephan 2023-11-04 13:37:42 +00:00
parent 5f3b13d136
commit 348e192ea8
4 changed files with 87 additions and 14 deletions

View File

@ -44,6 +44,20 @@ public final class Sqlite implements AutoCloseable {
public static final int STATUS_PAGECACHE_SIZE = CApi.SQLITE_STATUS_PAGECACHE_SIZE;
public static final int STATUS_MALLOC_COUNT = CApi.SQLITE_STATUS_MALLOC_COUNT;
public static final int DBSTATUS_LOOKASIDE_USED = CApi.SQLITE_DBSTATUS_LOOKASIDE_USED;
public static final int DBSTATUS_CACHE_USED = CApi.SQLITE_DBSTATUS_CACHE_USED;
public static final int DBSTATUS_SCHEMA_USED = CApi.SQLITE_DBSTATUS_SCHEMA_USED;
public static final int DBSTATUS_STMT_USED = CApi.SQLITE_DBSTATUS_STMT_USED;
public static final int DBSTATUS_LOOKASIDE_HIT = CApi.SQLITE_DBSTATUS_LOOKASIDE_HIT;
public static final int DBSTATUS_LOOKASIDE_MISS_SIZE = CApi.SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE;
public static final int DBSTATUS_LOOKASIDE_MISS_FULL = CApi.SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL;
public static final int DBSTATUS_CACHE_HIT = CApi.SQLITE_DBSTATUS_CACHE_HIT;
public static final int DBSTATUS_CACHE_MISS = CApi.SQLITE_DBSTATUS_CACHE_MISS;
public static final int DBSTATUS_CACHE_WRITE = CApi.SQLITE_DBSTATUS_CACHE_WRITE;
public static final int DBSTATUS_DEFERRED_FKS = CApi.SQLITE_DBSTATUS_DEFERRED_FKS;
public static final int DBSTATUS_CACHE_USED_SHARED = CApi.SQLITE_DBSTATUS_CACHE_USED_SHARED;
public static final int DBSTATUS_CACHE_SPILL = CApi.SQLITE_DBSTATUS_CACHE_SPILL;
public static final int LIMIT_LENGTH = CApi.SQLITE_LIMIT_LENGTH;
public static final int LIMIT_SQL_LENGTH = CApi.SQLITE_LIMIT_SQL_LENGTH;
public static final int LIMIT_COLUMN = CApi.SQLITE_LIMIT_COLUMN;
@ -150,19 +164,49 @@ public final class Sqlite implements AutoCloseable {
return CApi.sqlite3_sourceid();
}
/**
Output object for use with status() and libStatus().
*/
public static final class Status {
/** The current value for the requested status() or libStatus() metric. */
long current;
/** The peak value for the requested status() or libStatus() metric. */
long peak;
};
/**
As per sqlite3_status64(), but returns its current and high-water
results as a two-element array. Throws if the first argument is
results as a Status object. Throws if the first argument is
not one of the STATUS_... constants.
*/
public long[] libStatus(int op, boolean resetStats){
public static Status libStatus(int op, boolean resetStats){
org.sqlite.jni.capi.OutputPointer.Int64 pCurrent =
new org.sqlite.jni.capi.OutputPointer.Int64();
org.sqlite.jni.capi.OutputPointer.Int64 pHighwater =
new org.sqlite.jni.capi.OutputPointer.Int64();
final int rc = CApi.sqlite3_status64(op, pCurrent, pHighwater, resetStats);
checkRc(rc);
return new long[] {pCurrent.value, pHighwater.value};
checkRc2( CApi.sqlite3_status64(op, pCurrent, pHighwater, resetStats) );
final Status s = new Status();
s.current = pCurrent.value;
s.peak = pHighwater.value;
return s;
}
/**
As per sqlite3_status64(), but returns its current and high-water
results as a Status object. Throws if the first argument is
not one of the DBSTATUS_... constants or on any other misuse.
*/
public Status status(int op, boolean resetStats){
org.sqlite.jni.capi.OutputPointer.Int32 pCurrent =
new org.sqlite.jni.capi.OutputPointer.Int32();
org.sqlite.jni.capi.OutputPointer.Int32 pHighwater =
new org.sqlite.jni.capi.OutputPointer.Int32();
checkRc( CApi.sqlite3_db_status(thisDb(), op, pCurrent, pHighwater, resetStats) );
final Status s = new Status();
s.current = pCurrent.value;
s.peak = pHighwater.value;
return s;
}
@Override public void close(){
@ -272,6 +316,20 @@ public final class Sqlite implements AutoCloseable {
}
}
/**
Like checkRc() but behaves as if that function were
called with a null db object.
*/
private static void checkRc2(int rc){
if( 0!=rc ){
if( CApi.SQLITE_NOMEM==rc ){
throw new OutOfMemoryError();
}else{
throw new SqliteException(rc);
}
}
}
/**
prepFlags must be 0 or a bitmask of the PREPARE_... constants.
@ -440,7 +498,7 @@ public final class Sqlite implements AutoCloseable {
/**
Analog to sqlite3_release_memory().
*/
public static int releaseMemory(int n){
public static int libReleaseMemory(int n){
return CApi.sqlite3_release_memory(n);
}

View File

@ -501,6 +501,21 @@ public class Tester2 implements Runnable {
affirm( 7 == counter.value );
}
private void testStatus(){
final Sqlite db = openDb();
execSql(db, "create table t(a); insert into t values(1),(2),(3)");
Sqlite.Status s = Sqlite.libStatus(Sqlite.STATUS_MEMORY_USED, false);
affirm( s.current > 0 );
affirm( s.peak >= s.current );
s = db.status(Sqlite.DBSTATUS_SCHEMA_USED, false);
affirm( s.current > 0 );
affirm( s.peak == 0 /* always 0 for SCHEMA_USED */ );
db.close();
}
private void runTests(boolean fromThread) throws Exception {
List<java.lang.reflect.Method> mlist = testMethods;
affirm( null!=mlist );

View File

@ -1,5 +1,5 @@
C Bind\sthe\sbool-flag\ssqlite3_db_config()\svariants\sto\sthe\sJNI\swrapper1\sAPI.
D 2023-11-04T13:16:49.628
C Rework\sthe\sJNI\swrapper1\svariants\sof\sstatus()\sand\sdb_status()\sto\sbe\smore\sJava-esque.
D 2023-11-04T13:37:42.589
F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@ -296,9 +296,9 @@ F ext/jni/src/org/sqlite/jni/test-script-interpreter.md f9f25126127045d051e918fe
F ext/jni/src/org/sqlite/jni/wrapper1/AggregateFunction.java bbe60ac7fd8718edb215a23dc901771bcedb1df3b46d9cf6caff6f419828587f
F ext/jni/src/org/sqlite/jni/wrapper1/ScalarFunction.java 43c43adfb7866098aadaaca1620028a6ec82d5193149970019b1cce9eb59fb03
F ext/jni/src/org/sqlite/jni/wrapper1/SqlFunction.java 0b01b9058ef6737c85b505c6aa2490fb1dc1d974fb39d88a93269fed09553f9f
F ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java 0033898c318eea50489817957ef6078064f2b70b2838733462663ca3d6b09127
F ext/jni/src/org/sqlite/jni/wrapper1/Sqlite.java ab6bee53895e5f7345b57d32ef30e9cc9c9c09979a31211f63e60dcdea9a7ab1
F ext/jni/src/org/sqlite/jni/wrapper1/SqliteException.java aa85b4b05fae240b14f3d332f9524a2f80c619fb03856be72b4adda866b63b72
F ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java 00c697a57af398d6c49b2c32c10314201a5d0fa0862dc496f9d4f2139087b76b
F ext/jni/src/org/sqlite/jni/wrapper1/Tester2.java cbd087d3fcbfca384656fc2189a9b017e4afe70f0f17d8266d28c628ab97cada
F ext/jni/src/org/sqlite/jni/wrapper1/ValueHolder.java 7b89a7391f771692c5b83b0a5b86266abe8d59f1c77d7a0eccc9b79f259d79af
F ext/jni/src/org/sqlite/jni/wrapper1/WindowFunction.java 1a1afbafbd7406ff67e7d6405541c6347517c731de535a97d7a3df1d4db835b4
F ext/jni/src/tests/000-000-sanity.test c3427a0e0ac84d7cbe4c95fdc1cd4b61f9ddcf43443408f3000139478c4dc745
@ -2142,8 +2142,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
P 702910e0d1cfc897a269b4fb36b255165958edf529ac9553ebc5155e404d4cd3
R 86eeaee301f846f2f1ea6341b6a33699
P b5cdcb9279d9276f24b67083839f463beecd731f46f2e8bf68fff716df0a3921
R 9ee74b8e162e4e8a23028ccec92c6373
U stephan
Z e6b3bd265a1978cd37eafbdc1dc5ccf4
Z ce3f2fb1a954c596b6cdbc560de71d23
# Remove this line to create a well-formed Fossil manifest.

View File

@ -1 +1 @@
b5cdcb9279d9276f24b67083839f463beecd731f46f2e8bf68fff716df0a3921
40ad3920673561a06edf0b70a50a40be6cd20817fe22b87b63a9ac80cb2c9df8