1
0
mirror of https://github.com/sqlite/sqlite.git synced 2025-07-30 19:03:16 +03:00

FTS-related JNI refactoring. Move FTS-specific tests into their own class and dynamically load it, if possible, from the main test app.

FossilOrigin-Name: b7a8428fcd969e7a29a23c2dae61883f69501094f2de0f79bbee3c02c672cbf5
This commit is contained in:
stephan
2023-08-04 13:03:31 +00:00
parent b15223bce6
commit 5e208f712f
6 changed files with 132 additions and 16 deletions

View File

@ -206,6 +206,7 @@ static const struct {
#ifdef SQLITE_ENABLE_FTS5
const char * const Fts5Context;
const char * const Fts5ExtensionApi;
const char * const fts5_api;
#endif
} S3ClassNames = {
"org/sqlite/jni/sqlite3",
@ -216,7 +217,7 @@ static const struct {
"org/sqlite/jni/OutputPointer$Int64",
#ifdef SQLITE_ENABLE_FTS5
"org/sqlite/jni/Fts5Context",
"org/sqlite/jni/Fts5ExtensionApi"
"org/sqlite/jni/Fts5ExtensionApi",
"org/sqlite/jni/fts5_api"
#endif
};
@ -959,6 +960,7 @@ static void setOutputInt32(JNIEnv * env, jobject jOut, int v){
EXCEPTION_IS_FATAL("Cannot set OutputPointer.Int32.value");
}
#ifdef SQLITE_ENABLE_FTS5
/* Sets a native int64 value in OutputPointer.Int64 object jOut. */
static void setOutputInt64(JNIEnv * env, jobject jOut, jlong v){
jfieldID setter = 0;
@ -977,6 +979,7 @@ static void setOutputInt64(JNIEnv * env, jobject jOut, jlong v){
(*env)->SetIntField(env, jOut, setter, (jint)v);
EXCEPTION_IS_FATAL("Cannot set OutputPointer.Int64.value");
}
#endif /* SQLITE_ENABLE_FTS5 */
static int encodingTypeIsValid(int eTextRep){
switch(eTextRep){
@ -2618,7 +2621,7 @@ static jobject s3jni_getFts5ExensionApi(JNIEnv * const env){
JNIEnvCacheLine * const row = S3Global_env_cache(env);
if( !row->jFtsExt ){
row->jFtsExt = new_NativePointerHolder_object(env, S3ClassNames.Fts5ExtensionApi,
s3jni_ftsext());
s3jni_ftsext());
if(row->jFtsExt) row->jFtsExt = REF_G(row->jFtsExt);
}
return row->jFtsExt;

View File

@ -909,10 +909,19 @@ public class Tester1 {
sqlite3_close_v2(db);
}
private static void testFts1(){
Fts5ExtensionApi fea = Fts5ExtensionApi.getInstance();
affirm( null != fea );
affirm( fea.getNativePointer() != 0 );
@SuppressWarnings("unchecked")
private static void testFts5(){
try {
Class t = Class.forName("org.sqlite.jni.TesterFts5");
java.lang.reflect.Constructor ctor = t.getConstructor();
ctor.newInstance();
}catch(ClassNotFoundException e){
outln("FTS5 classes not loaded. Skipping FTS tests.");
}catch(NoSuchMethodException e){
outln("FTS5 tester ctor not found. Skipping FTS tests.");
}catch(Exception e){
outln("FTS5 tester cannot be instantiated. Skipping FTS tests.");
}
}
private static void testSleep(){
@ -947,6 +956,7 @@ public class Tester1 {
testCommitHook();
testRollbackHook();
testUpdateHook();
testFts5();
//testSleep();
if(liArgs.indexOf("-v")>0){
sqlite3_do_something_for_developer();

View File

@ -0,0 +1,101 @@
/*
** 2023-08-04
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** This file contains a set of tests for the sqlite3 JNI bindings.
*/
package org.sqlite.jni;
import static org.sqlite.jni.SQLite3Jni.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class TesterFts5 {
private static <T> void out(T val){
System.out.print(val);
}
private static <T> void outln(T val){
System.out.println(val);
}
private static int affirmCount = 0;
private static void affirm(Boolean v){
++affirmCount;
if( !v ) throw new RuntimeException("Assertion failed.");
}
private static void execSql(sqlite3 db, String[] sql){
execSql(db, String.join("", sql));
}
private static int execSql(sqlite3 db, boolean throwOnError, String sql){
OutputPointer.Int32 oTail = new OutputPointer.Int32();
final byte[] sqlUtf8 = sql.getBytes(StandardCharsets.UTF_8);
int pos = 0, n = 1;
byte[] sqlChunk = sqlUtf8;
sqlite3_stmt stmt = new sqlite3_stmt();
int rc = 0;
while(pos < sqlChunk.length){
if(pos > 0){
sqlChunk = Arrays.copyOfRange(sqlChunk, pos,
sqlChunk.length);
}
if( 0==sqlChunk.length ) break;
rc = sqlite3_prepare_v2(db, sqlChunk, stmt, oTail);
affirm(0 == rc);
pos = oTail.getValue();
affirm(0 != stmt.getNativePointer());
rc = sqlite3_step(stmt);
sqlite3_finalize(stmt);
affirm(0 == stmt.getNativePointer());
if(0!=rc && SQLITE_ROW!=rc && SQLITE_DONE!=rc){
if(throwOnError){
throw new RuntimeException("db op failed with rc="+rc);
}else{
break;
}
}
}
if(SQLITE_ROW==rc || SQLITE_DONE==rc) rc = 0;
return rc;
}
private static void execSql(sqlite3 db, String sql){
execSql(db, true, sql);
}
private static sqlite3 createNewDb(){
sqlite3 db = new sqlite3();
affirm(0 == db.getNativePointer());
int rc = sqlite3_open(":memory:", db);
affirm(0 == rc);
affirm(0 != db.getNativePointer());
rc = sqlite3_busy_timeout(db, 2000);
affirm( 0 == rc );
return db;
}
private static void test1(){
Fts5ExtensionApi fea = Fts5ExtensionApi.getInstance();
affirm( null != fea );
affirm( fea.getNativePointer() != 0 );
affirm( fea == Fts5ExtensionApi.getInstance() )/*singleton*/;
}
public TesterFts5(){
final long timeStart = System.nanoTime();
test1();
final long timeEnd = System.nanoTime();
outln("FTS5 Tests done. Metrics:");
outln("\tAssertions checked: "+affirmCount);
outln("\tTotal time = "
+((timeEnd - timeStart)/1000000.0)+"ms");
}
}