From f9f64991cdd7558c8fa86e98ee85a61c700a4f98 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 20 Dec 2022 14:18:36 +0100 Subject: [PATCH 01/32] Add a basic wrapper around libzim. This validate the build system has we now have a compiling wrapper around libzim. --- .gitignore | 1 + lib/build.gradle | 4 +- lib/src/main/cpp/CMakeLists.txt | 53 ++++ lib/src/main/cpp/book.cpp | 3 +- lib/src/main/cpp/common.cpp | 4 + lib/src/main/cpp/libzim/archive.cpp | 232 ++++++++++++++++++ lib/src/main/cpp/libzim/blob.cpp | 47 ++++ lib/src/main/cpp/libzim/entry.cpp | 67 +++++ lib/src/main/cpp/libzim/item.cpp | 54 ++++ lib/src/main/cpp/utils.h | 143 ++++++++++- .../main/java/org/kiwix/libzim/Archive.java | 107 ++++++++ lib/src/main/java/org/kiwix/libzim/Blob.java | 41 ++++ lib/src/main/java/org/kiwix/libzim/Entry.java | 46 ++++ lib/src/main/java/org/kiwix/libzim/Item.java | 45 ++++ .../kiwix/libzim/ZimFileFormatException.java | 27 ++ 15 files changed, 858 insertions(+), 16 deletions(-) create mode 100644 lib/src/main/cpp/CMakeLists.txt create mode 100644 lib/src/main/cpp/common.cpp create mode 100644 lib/src/main/cpp/libzim/archive.cpp create mode 100644 lib/src/main/cpp/libzim/blob.cpp create mode 100644 lib/src/main/cpp/libzim/entry.cpp create mode 100644 lib/src/main/cpp/libzim/item.cpp create mode 100644 lib/src/main/java/org/kiwix/libzim/Archive.java create mode 100644 lib/src/main/java/org/kiwix/libzim/Blob.java create mode 100644 lib/src/main/java/org/kiwix/libzim/Entry.java create mode 100644 lib/src/main/java/org/kiwix/libzim/Item.java create mode 100644 lib/src/main/java/org/kiwix/libzim/ZimFileFormatException.java diff --git a/.gitignore b/.gitignore index 15f02b0..a72e5fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .gradle local.properties build +lib/.cxx diff --git a/lib/build.gradle b/lib/build.gradle index 9a58b87..395b15c 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -59,7 +59,7 @@ android { } externalNativeBuild { cmake { - path file('src/cpp/CMakeLists.txt') + path file('src/main/cpp/CMakeLists.txt') version '3.18.1' } } @@ -285,5 +285,5 @@ task checkCurrentJavaVersion() { task generateHeaderFilesFromJavaWrapper(type: Exec) { workingDir "${projectDir}/src/main/java/org/kiwix/" - commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/kiwixlib/ kiwixlib/Book.java kiwixlib/DirectAccessInfo.java kiwixlib/Filter.java kiwixlib/JNIICU.java kiwixlib/JNIKiwixBool.java kiwixlib/JNIKiwixException.java kiwixlib/JNIKiwixInt.java kiwixlib/JNIKiwixReader.java kiwixlib/JNIKiwixSearcher.java kiwixlib/JNIKiwixServer.java kiwixlib/JNIKiwixString.java kiwixlib/Library.java kiwixlib/Manager.java" + commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ libzim/Archive.java libzim/Blob.java libzim/Entry.java libzim/Item.java libzim/ZimFileFormatException.java" } diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000..cfa907f --- /dev/null +++ b/lib/src/main/cpp/CMakeLists.txt @@ -0,0 +1,53 @@ + +set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH) +cmake_minimum_required(VERSION 3.18.1) + +set(CMAKE_ANDROID_STL_TYPE llvm-libc++_static) + +project("libzim_wrapper") + +add_library( + libzim_wrapper + + SHARED + common.cpp + libzim/archive.cpp + libzim/entry.cpp + libzim/item.cpp + libzim/blob.cpp +) + +find_library(libzim + zim + PATHS + ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libzim) +if (NOT libzim) + message(FATAL_ERROR "libzim not found!") +endif() +add_library(libzim SHARED IMPORTED) + +set_property(TARGET + libzim + PROPERTY + IMPORTED_LOCATION + ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libzim/libzim.so) + +include_directories( +${CMAKE_SOURCE_DIR} +${BUILD_DIR}/include/libkiwix +${BUILD_DIR}/include/libzim +${BUILD_DIR}/include/javah_generated +#${CMAKE_SOURCE_DIR}/include/utils +) + +find_library( + log-lib + log) + +target_link_libraries( + libzim_wrapper + libzim + + ${log-lib} + ) + diff --git a/lib/src/main/cpp/book.cpp b/lib/src/main/cpp/book.cpp index 157cc52..238f490 100644 --- a/lib/src/main/cpp/book.cpp +++ b/lib/src/main/cpp/book.cpp @@ -23,6 +23,7 @@ #include "utils.h" #include "book.h" +#include JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_Book_allocate( @@ -46,7 +47,7 @@ METHOD(void, Book, update__Lorg_kiwix_kiwixlib_Book_2, jobject otherBook) METHOD(void, Book, update__Lorg_kiwix_kiwixlib_JNIKiwixReader_2, jobject reader) { - BOOK->update(**Handle::getHandle(env, reader)); + BOOK->update(**Handle::getHandle(env, reader)); } #define GETTER(retType, name) JNIEXPORT retType JNICALL \ diff --git a/lib/src/main/cpp/common.cpp b/lib/src/main/cpp/common.cpp new file mode 100644 index 0000000..f510798 --- /dev/null +++ b/lib/src/main/cpp/common.cpp @@ -0,0 +1,4 @@ + +#include + +std::mutex globalLock; diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp new file mode 100644 index 0000000..065393d --- /dev/null +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -0,0 +1,232 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Archive.h" + +#include + +#include +#include + +#include +#include + +/* Kiwix Reader JNI functions */ +JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchive( + JNIEnv* env, jobject obj, jstring filename) +{ + std::string cPath = TO_C(filename); + + LOG("Attempting to create reader with: %s", cPath.c_str()); + Lock l; + try { + zim::Archive* reader = new zim::Archive(cPath); + return reinterpret_cast(new Handle(reader)); + } catch (std::exception& e) { + LOG("Error opening ZIM file"); + LOG("%s", e.what()); + return 0; + } +} + +namespace +{ + +int jni2fd(const jobject& fdObj, JNIEnv* env) +{ + jclass class_fdesc = env->FindClass("java/io/FileDescriptor"); + jfieldID field_fd = env->GetFieldID(class_fdesc, "fd", "I"); + if ( field_fd == NULL ) + { + env->ExceptionClear(); + // Under Android the (private) 'fd' field of java.io.FileDescriptor has been + // renamed to 'descriptor'. See, for example, + // https://android.googlesource.com/platform/libcore/+/refs/tags/android-8.1.0_r1/ojluni/src/main/java/java/io/FileDescriptor.java#55 + field_fd = env->GetFieldID(class_fdesc, "descriptor", "I"); + } + return env->GetIntField(fdObj, field_fd); +} + +} // unnamed namespace + +JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveByFD( + JNIEnv* env, jobject obj, jobject fdObj) +{ +#ifndef _WIN32 + int fd = jni2fd(fdObj, env); + + LOG("Attempting to create reader with fd: %d", fd); + Lock l; + try { + zim::Archive* reader = new zim::Archive(fd); + return reinterpret_cast(new Handle(reader)); + } catch (std::exception& e) { + LOG("Error opening ZIM file"); + LOG("%s", e.what()); + return 0; + } +#else + jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); + env->ThrowNew(exception, "org.kiwix.libzim.Archive.getNativeArchiveByFD() is not supported under Windows"); + return 0; +#endif +} + +JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveEmbedded( + JNIEnv* env, jobject obj, jobject fdObj, jlong offset, jlong size) +{ +#ifndef _WIN32 + int fd = jni2fd(fdObj, env); + + LOG("Attempting to create reader with fd: %d", fd); + Lock l; + try { + zim::Archive* reader = new zim::Archive(fd, offset, size); + return reinterpret_cast(new Handle(reader)); + } catch (std::exception& e) { + LOG("Error opening ZIM file"); + LOG("%s", e.what()); + return 0; + } +#else + jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); + env->ThrowNew(exception, "org.kiwix.libzim.Archive.getNativeArchiveEmbedded() is not supported under Windows"); + return 0; +#endif +} + +JNIEXPORT void JNICALL +Java_org_kiwix_libzim_Archive_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS (Handle::getHandle(env, thisObj)) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_Archive_##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + + +GETTER(jstring, getFilename) +GETTER(jlong, getFilesize) +GETTER(jint, getAllEntryCount) +GETTER(jint, getEntryCount) +GETTER(jint, getArticleCount) +GETTER(jint, getMediaCount) + +METHOD0(jstring, Archive, getUuid) { + return TO_JNI(std::string(THIS->getUuid())); +} + +METHOD(jstring, Archive, getMetadata, jstring name) { + return TO_JNI(THIS->getMetadata(TO_C(name))); +} + +METHOD(jobject, Archive, getMetadataItem, jstring name) { + auto item = THIS->getMetadataItem(TO_C(name)); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item); + return obj; +} + +GETTER(jobjectArray, getMetadataKeys) + +METHOD(jobject, Archive, getIllustrationItem, jint size) { + auto item = THIS->getIllustrationItem(TO_C(size)); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item); + return obj; +} + +METHOD(jboolean, Archive, hasIllustration, jint size) { + return TO_JNI(THIS->hasIllustration(TO_C(size))); +} + +GETTER(jlongArray, getIllustrationSizes) + +METHOD(jobject, Archive, getEntryByPath, jlong index) { + auto entry = THIS->getEntryByPath(TO_C(index)); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + return obj; +} + +METHOD(jobject, Archive, getEntryByPath, jstring path) { + auto entry = THIS->getEntryByPath(TO_C(path)); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + return obj; +} + +METHOD(jobject, Archive, getEntryByTitle, jlong index) { + auto entry = THIS->getEntryByTitle(TO_C(index)); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + return obj; +} + +METHOD(jobject, Archive, getEntryByTitle, jstring title) { + auto entry = THIS->getEntryByTitle(TO_C(title)); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + return obj; +} + +METHOD(jobject, Archive, getEntryByClusterOrder, jlong index) { + auto entry = THIS->getEntryByClusterOrder(TO_C(index)); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + return obj; +} + +METHOD0(jobject, Archive, getMainEntry) { + auto entry = THIS->getMainEntry(); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + return obj; +} + +METHOD0(jobject, Archive, getRandomEntry) { + auto entry = THIS->getRandomEntry(); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + return obj; +} + +METHOD(jboolean, Archive, hasEntryByPath, jstring path) { + return TO_JNI(THIS->hasEntryByPath(TO_C(path))); +} + +METHOD(jboolean, Archive, hasEntryByTitle, jstring title) { + return TO_JNI(THIS->hasEntryByPath(TO_C(title))); +} + +GETTER(jboolean, hasMainEntry) + +METHOD(jboolean, Archive, hasIllustration, jlong size) { + return TO_JNI(THIS->hasIllustration(TO_C(size))); +} + +GETTER(jboolean, hasFulltextIndex) +GETTER(jboolean, hasTitleIndex) +GETTER(jboolean, hasChecksum) +GETTER(jstring, getChecksum) +GETTER(jboolean, check) + +GETTER(jboolean, isMultiPart) +GETTER(jboolean, hasNewNamespaceScheme) + + + diff --git a/lib/src/main/cpp/libzim/blob.cpp b/lib/src/main/cpp/libzim/blob.cpp new file mode 100644 index 0000000..27f5ee6 --- /dev/null +++ b/lib/src/main/cpp/libzim/blob.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Blob.h" + +#include "utils.h" + +#include + +#include + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_Blob_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS (Handle::getHandle(env, thisObj)) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_Blob__##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + +METHOD0(jstring, Blob, getData) { + return TO_JNI(std::string(**THIS)); +} +GETTER(jlong, size) diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp new file mode 100644 index 0000000..d71f0d5 --- /dev/null +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Entry.h" + +#include + +#include + +#include +#include + +#define NATIVE_TYPE zim::Entry + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_Entry_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS (Handle::getHandle(env, thisObj)) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_Entry__##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + + +GETTER(jboolean, isRedirect) +GETTER(jstring, getTitle) +GETTER(jstring, getPath) +METHOD(jobject, Entry, getItem, jboolean follow) { + auto item = THIS->getItem(TO_C(follow)); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item); + return obj; +} + +METHOD0(jobject, Entry, getRedirect) { + auto item = THIS->getRedirect(); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item); + return obj; +} + +METHOD0(jobject, Entry, getRedirectEntry) { + auto entry = THIS->getRedirectEntry(); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + return obj; +} diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp new file mode 100644 index 0000000..1168a2d --- /dev/null +++ b/lib/src/main/cpp/libzim/item.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Item.h" + +#include + +#include + +#include + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_Item_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS (Handle::getHandle(env, thisObj)) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_Item__##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + +GETTER(jstring, getTitle) +GETTER(jstring, getPath) +GETTER(jstring, getMimetype) + +METHOD0(jobject, Item, getData) { + auto blob = THIS->getData(); + auto obj = CREATE_WRAPPER("org/kiwix/libzim/Blob", blob); + return obj; +} + +GETTER(jlong, getSize) diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index 232071d..e5475e8 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -27,6 +27,7 @@ #include #include #include +#include #include #if __ANDROID__ @@ -68,11 +69,11 @@ void dispose(JNIEnv* env, jobject thisObj) } #define METHOD0(retType, class, name) \ -JNIEXPORT retType JNICALL Java_org_kiwix_kiwixlib_##class##_##name( \ +JNIEXPORT retType JNICALL Java_org_kiwix_libzim_##class##_##name( \ JNIEnv* env, jobject thisObj) #define METHOD(retType, class, name, ...) \ -JNIEXPORT retType JNICALL Java_org_kiwix_kiwixlib_##class##_##name( \ +JNIEXPORT retType JNICALL Java_org_kiwix_libzim_##class##_##name( \ JNIEnv* env, jobject thisObj, __VA_ARGS__) inline jfieldID getHandleField(JNIEnv* env, jobject obj) @@ -88,6 +89,9 @@ inline jobjectArray createArray(JNIEnv* env, size_t length, const std::string& t return env->NewObjectArray(length, c, NULL); } + +// A mixin class which will lock the globalLock when a instance is created +// This avoid the cration of two instance inheriting from Lock in the same time. class Lock : public std::unique_lock { public: @@ -97,14 +101,19 @@ class Lock : public std::unique_lock template class LockedHandle; + +/* + * A handle to a shared_ptr + * Not usable by itself, you must get a LockedHandle to access the value. + */ template class Handle { protected: - T* h; + std::shared_ptr value; public: - Handle(T* h) : h(h){}; + Handle(T* v) : value(v){}; // No destructor. This must and will be handled by dispose method. @@ -118,34 +127,42 @@ class Handle { auto lHandle = getHandle(env, obj); auto handle = lHandle.h; - delete handle->h; delete handle; } friend class LockedHandle; }; +/* + * A locked handle. + * Only one LockedHandle can exist at the same time. + * As LockedHandle is the + */ template struct LockedHandle : public Lock { Handle* h; LockedHandle(Handle* h) : h(h) {} - T* operator->() { return h->h; } - T* operator*() { return h->h; } - operator bool() const { return (h->h != nullptr); } - operator T*() const { return h->h; } + T* operator->() { return h->value.get(); } + T* operator*() { return h->value.get(); } + operator bool() const { return (h->value); } + operator T*() const { return h->value.get(); } }; +// --------------------------------------------------------------------------- +// Convert things to JAVA +// --------------------------------------------------------------------------- + template struct JType { }; template<> struct JType{ typedef jboolean type_t; }; -template<> struct JType{ typedef jint type_t; }; -template<> struct JType{ typedef jlong type_t; }; +template<> struct JType{ typedef jint type_t; }; +template<> struct JType{ typedef jlong type_t; }; template<> struct JType { typedef jlong type_t; }; template<> struct JType { typedef jlong type_t; }; template<> struct JType{ typedef jstring type_t; }; -template<> struct JType>{ typedef jobjectArray type_t; }; -template + +template inline typename JType::type_t c2jni(const T& val, JNIEnv* env) { return static_cast::type_t>(val); } @@ -159,6 +176,70 @@ inline jstring c2jni(const std::string& val, JNIEnv* env) return env->NewStringUTF(val.c_str()); } + +template +struct JTypeArray {}; +template<> struct JTypeArray{ + typedef jbooleanArray type_t; + static jbooleanArray createArray(JNIEnv* env, size_t length) { + return env->NewBooleanArray(length); + } + static void setArray(JNIEnv* env, jbooleanArray array, const bool* data, size_t length) { + env->SetBooleanArrayRegion(array, 0, length, reinterpret_cast(data)); + } +}; +template<> struct JTypeArray{ + typedef jintArray type_t; + static jintArray createArray(JNIEnv* env, size_t length) { + return env->NewIntArray(length); + } + static void setArray(JNIEnv* env, jintArray array, const int32_t* data, size_t length) { + env->SetIntArrayRegion(array, 0, length, data); + } +}; +template<> struct JTypeArray{ + typedef jlongArray type_t; + static jlongArray createArray(JNIEnv* env, size_t length) { + return env->NewLongArray(length); + } + static void setArray(JNIEnv* env, jlongArray array, const int64_t* data, size_t length) { + env->SetLongArrayRegion(array, 0, length, data); + } +}; +template<> struct JTypeArray { + typedef jlongArray type_t; + static jlongArray createArray(JNIEnv* env, size_t length) { + return env->NewLongArray(length); + } + static void setArray(JNIEnv* env, jlongArray array, const uint64_t* data, size_t length) { + env->SetLongArrayRegion(array, 0, length, reinterpret_cast(data)); + } +}; +template<> struct JTypeArray { + typedef jlongArray type_t; + static jlongArray createArray(JNIEnv* env, size_t length) { + return env->NewLongArray(length); + } + static void setArray(JNIEnv* env, jlongArray array, const uint32_t* data, size_t length) { + std::vector temp(data, data+length); + env->SetLongArrayRegion(array, 0, length, temp.data()); + } +}; +template<> struct JTypeArray{ + typedef jobjectArray type_t; + static jobjectArray createArray(JNIEnv* env, size_t length) { + return ::createArray(env, length, "java/lang/String"); + } + static void setArray(JNIEnv* env, jobjectArray array, const std::string* data, size_t length) { + size_t index = 0; + for(size_t index=0; indexSetObjectArrayElement(array, index, jElem); + } + } +}; + +/* template<> inline jobjectArray c2jni(const std::vector& val, JNIEnv* env) { @@ -169,8 +250,31 @@ inline jobjectArray c2jni(const std::vector& val, JNIEnv* env) env->SetObjectArrayElement(array, index++, jElem); } return array; +}*/ + +template +inline typename JTypeArray::type_t c2jni(const std::vector& val, JNIEnv* env) +{ + auto array = JTypeArray::createArray(env, val.size()); + JTypeArray::setArray(env, array, val.data(), val.size()); + return array; +} + +template +inline typename JTypeArray::type_t c2jni(const std::set& val, JNIEnv* env) +{ + std::vector temp(val.begin(), val.end()); + return c2jni(temp, env); } +#define TO_JNI(VAL) c2jni(VAL, env) + + +// --------------------------------------------------------------------------- +// Convert things to C +// --------------------------------------------------------------------------- + + template struct CType { }; @@ -215,6 +319,8 @@ inline typename CType::type_t jni2c(const jobjectArray& val, JN return v; } +#define TO_C(VAL) jni2c(VAL, env) + /* Method to deal with variable passed by reference */ inline std::string getStringObjValue(const jobject obj, JNIEnv* env) { @@ -256,4 +362,15 @@ inline void setDaiObjValue(const std::string& filename, const long offset, env->SetLongField(obj, offsetFid, offset); } +template +inline jobject createWrapper(const char* className, T && nativeObj, JNIEnv* env) { + jclass objClass = env->FindClass(className); + jmethodID initMethod = env->GetMethodID(objClass, "", "(J)V"); + jlong nativeHandle = reinterpret_cast(new Handle(new T(std::forward(nativeObj)))); + jobject object = env->NewObject(objClass, initMethod, nativeHandle); + return object; +} + +#define CREATE_WRAPPER(CLASSNAME, OBJ) createWrapper(CLASSNAME, std::move(OBJ), env) + #endif // _ANDROID_JNI_UTILS_H diff --git a/lib/src/main/java/org/kiwix/libzim/Archive.java b/lib/src/main/java/org/kiwix/libzim/Archive.java new file mode 100644 index 0000000..b6d8104 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Archive.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.ZimFileFormatException; +import org.kiwix.libzim.Entry; +import org.kiwix.libzim.Item; +import java.io.FileDescriptor; + +public class Archive +{ + + public Archive(String filename) throws ZimFileFormatException + { + nativeHandle = getNativeArchive(filename); + if (nativeHandle == 0) { + throw new ZimFileFormatException("Cannot open zimfile "+filename); + } + } + + public Archive(FileDescriptor fd) throws ZimFileFormatException + { + nativeHandle = getNativeArchiveByFD(fd); + if (nativeHandle == 0) { + throw new ZimFileFormatException("Cannot open zimfile by fd "+fd.toString()); + } + } + + public Archive(FileDescriptor fd, long offset, long size) + throws ZimFileFormatException + { + nativeHandle = getNativeArchiveEmbedded(fd, offset, size); + if (nativeHandle == 0) { + throw new ZimFileFormatException(String.format("Cannot open embedded zimfile (fd=%s, offset=%d, size=%d)", fd, offset, size)); + } + } + + public native String getFilename(); + public native long getFilesize(); + public native int getAllEntryCount(); + public native int getEntryCount(); + public native int getArticleCount(); + public native int getMediaCount(); + public native String getUuid(); + public native String getMetadata(String name); + public native Item getMetadataItem(String name); + public native String[] getMetadataKeys(); + + public native Item getIllustrationItem(int size); + public native boolean hasIllustration(int size); + public native long[] getIllustrationSizes(); + + public native Entry getEntryByPath(String path); + public native Entry getEntryByPath(int index); + public native boolean hasEntryByPath(String path); + + public native Entry getEntryByTitle(String title); + public native Entry getEntryByTitle(int index); + public native boolean hasEntryByTitle(String title); + + public native Entry getEntryByClusterOrder(int index); + + public native Entry getMainEntry(); + public native boolean hasMainEntry(); + + public native Entry getRandomEntry(); + + public native boolean hasFulltextIndex(); + public native boolean hasTitleIndex(); + + public native boolean hasChecksum(); + public native String getChecksum(); + public native boolean check(); + + public native boolean isMultiPart(); + public native boolean hasNewNamespaceScheme(); + + + private native long getNativeArchive(String filename); + private native long getNativeArchiveByFD(FileDescriptor fd); + private native long getNativeArchiveEmbedded(FileDescriptor fd, long offset, long size); + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Blob.java b/lib/src/main/java/org/kiwix/libzim/Blob.java new file mode 100644 index 0000000..c6ccf6c --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Blob.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.Blob; + +public class Blob +{ + private Blob(long handle) {nativeHandle = handle;} + + public native String getData(); + public native long size(); + + protected void finalize() { + dispose(); + } + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Entry.java b/lib/src/main/java/org/kiwix/libzim/Entry.java new file mode 100644 index 0000000..713691b --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Entry.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.Item; + +public class Entry +{ + private Entry(long handle) {nativeHandle = handle;} + + public native boolean isRedirect(); + public native String getTitle(); + public native String getPath(); + + public native Item getItem(boolean follow); + public native Item getRedirect(); + public native Entry getRedirectEntry(); + + protected void finalize() { + dispose(); + } + +///--------- The wrapper thing + // To delete our native wrapper + private native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Item.java b/lib/src/main/java/org/kiwix/libzim/Item.java new file mode 100644 index 0000000..e96b2a7 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Item.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.Blob; + +public class Item +{ + private Item(long handle) {nativeHandle = handle;} + + public native String getTitle(); + public native String getPath(); + public native String getMimeType(); + + public native Blob getData(); + public native long getSize(); + + protected void finalize() { + dispose(); + } + +///--------- The wrapper thing + // To delete our native wrapper + private native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/ZimFileFormatException.java b/lib/src/main/java/org/kiwix/libzim/ZimFileFormatException.java new file mode 100644 index 0000000..72a2897 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/ZimFileFormatException.java @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +public class ZimFileFormatException extends Exception +{ + public ZimFileFormatException(String message) { + super(message); + } +} From 030b2f95f5aa1c6caf107acbcf69aeed82384176 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 21 Dec 2022 15:52:52 +0100 Subject: [PATCH 02/32] [WIP] Add search wrapping --- lib/src/main/java/org/kiwix/libzim/Query.java | 34 +++++++++++ .../main/java/org/kiwix/libzim/Search.java | 35 +++++++++++ .../java/org/kiwix/libzim/SearchIterator.java | 44 ++++++++++++++ .../org/kiwix/libzim/SearchResultSet.java | 35 +++++++++++ .../main/java/org/kiwix/libzim/Searcher.java | 60 +++++++++++++++++++ 5 files changed, 208 insertions(+) create mode 100644 lib/src/main/java/org/kiwix/libzim/Query.java create mode 100644 lib/src/main/java/org/kiwix/libzim/Search.java create mode 100644 lib/src/main/java/org/kiwix/libzim/SearchIterator.java create mode 100644 lib/src/main/java/org/kiwix/libzim/SearchResultSet.java create mode 100644 lib/src/main/java/org/kiwix/libzim/Searcher.java diff --git a/lib/src/main/java/org/kiwix/libzim/Query.java b/lib/src/main/java/org/kiwix/libzim/Query.java new file mode 100644 index 0000000..30eb781 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Query.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +public class Query +{ + public Query(string query); + public native Query setQuery(string query); + public native Query setGeorange(float latitude, float longitute, float distance); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Search.java b/lib/src/main/java/org/kiwix/libzim/Search.java new file mode 100644 index 0000000..e3c62b8 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Search.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SearchResultSet; + +public class Search +{ + public native SearchResultSet getResults(int start, int maxResults); + public native long getEstimatedMatches(); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SearchIterator.java b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java new file mode 100644 index 0000000..ac93003 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SearchIterator; + +public class SearchIterator implement Iterator +{ + public native string getPath(); + public native string getTitle(); + public native int getScore(); + public native string getSnippet(); + public native int getWordCount(); + public native int getFileIndex(); + public native int size(); + public native string getZimId(); + + public native boolean hasNext(); + public native Entry next(); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SearchResultSet.java b/lib/src/main/java/org/kiwix/libzim/SearchResultSet.java new file mode 100644 index 0000000..27c9833 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SearchResultSet.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SearchIterator; + +public class SearchResultSet +{ + public native SearchIterator begin(); + public native long size(); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/Searcher.java b/lib/src/main/java/org/kiwix/libzim/Searcher.java new file mode 100644 index 0000000..b1158a3 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/Searcher.java @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.ZimFileFormatException; +import org.kiwix.libzim.Archive; +import org.kiwix.libzim.Search; +import org.kiwix.libzim.Query; +import java.io.FileDescriptor; + +public class Searcher +{ + + public Searcĥer(Archive archive) throws ZimFileFormatException + { + nativeHandle = getNativeSearcher(archive); + if (nativeHandle == 0) { + throw new ZimFileFormatException("Cannot open zimfile "+filename); + } + } + + public Searcher(List archives) throws ZimFileFormatException + { + nativeHandle = getNativeSearcher(archives); + if (nativeHandle == 0) { + throw new ZimFileFormatException("Cannot open zimfile by fd "+fd.toString()); + } + } + + public native Searcher addArchive(Archive archive); + public native Search search(Query query); + public native void setVerbose(boolean verbose); + + private native long getNativeSearcher(Archive archive); + private native long getNativeSearcherMulti(List archives); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} From 7887202eb090a07ae328ec6b9a62874baf6b243d Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 11 Jan 2023 16:15:08 +0100 Subject: [PATCH 03/32] Better wrapping structure. Avoid the Handle and directly use a shared_ptr. This simplify a lot the wrapping code and potentially remove some bug. --- lib/src/main/cpp/libzim/archive.cpp | 71 +++++----- lib/src/main/cpp/libzim/blob.cpp | 9 +- lib/src/main/cpp/libzim/entry.cpp | 16 +-- lib/src/main/cpp/libzim/item.cpp | 10 +- lib/src/main/cpp/utils.h | 130 ++++++++---------- .../main/java/org/kiwix/libzim/Archive.java | 12 +- lib/src/main/java/org/kiwix/libzim/Blob.java | 2 - lib/src/main/java/org/kiwix/libzim/Entry.java | 2 - lib/src/main/java/org/kiwix/libzim/Item.java | 2 - 9 files changed, 115 insertions(+), 139 deletions(-) diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index 065393d..c6a4763 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -31,20 +31,19 @@ #include /* Kiwix Reader JNI functions */ -JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchive( - JNIEnv* env, jobject obj, jstring filename) +JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchive( + JNIEnv* env, jobject thisObj, jstring filename) { std::string cPath = TO_C(filename); LOG("Attempting to create reader with: %s", cPath.c_str()); Lock l; try { - zim::Archive* reader = new zim::Archive(cPath); - return reinterpret_cast(new Handle(reader)); + auto archive = std::make_shared(cPath); + SET_PTR(archive); } catch (std::exception& e) { LOG("Error opening ZIM file"); LOG("%s", e.what()); - return 0; } } @@ -68,8 +67,8 @@ int jni2fd(const jobject& fdObj, JNIEnv* env) } // unnamed namespace -JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveByFD( - JNIEnv* env, jobject obj, jobject fdObj) +JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD( + JNIEnv* env, jobject thisObj, jobject fdObj) { #ifndef _WIN32 int fd = jni2fd(fdObj, env); @@ -77,22 +76,20 @@ JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveByFD( LOG("Attempting to create reader with fd: %d", fd); Lock l; try { - zim::Archive* reader = new zim::Archive(fd); - return reinterpret_cast(new Handle(reader)); + auto archive = std::make_shared(fd); + SET_PTR(archive); } catch (std::exception& e) { LOG("Error opening ZIM file"); LOG("%s", e.what()); - return 0; } #else jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); - env->ThrowNew(exception, "org.kiwix.libzim.Archive.getNativeArchiveByFD() is not supported under Windows"); - return 0; + env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveByFD() is not supported under Windows"); #endif } -JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveEmbedded( - JNIEnv* env, jobject obj, jobject fdObj, jlong offset, jlong size) +JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded( + JNIEnv* env, jobject thisObj, jobject fdObj, jlong offset, jlong size) { #ifndef _WIN32 int fd = jni2fd(fdObj, env); @@ -100,17 +97,15 @@ JNIEXPORT jlong JNICALL Java_org_kiwix_libzim_Archive_getNativeArchiveEmbedded( LOG("Attempting to create reader with fd: %d", fd); Lock l; try { - zim::Archive* reader = new zim::Archive(fd, offset, size); - return reinterpret_cast(new Handle(reader)); + auto archive = std::make_shared(fd, offset, size); + SET_PTR(archive); } catch (std::exception& e) { LOG("Error opening ZIM file"); LOG("%s", e.what()); - return 0; } #else jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); - env->ThrowNew(exception, "org.kiwix.libzim.Archive.getNativeArchiveEmbedded() is not supported under Windows"); - return 0; + env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows"); #endif } @@ -120,7 +115,7 @@ Java_org_kiwix_libzim_Archive_dispose(JNIEnv* env, jobject thisObj) dispose(env, thisObj); } -#define THIS (Handle::getHandle(env, thisObj)) +#define THIS GET_PTR(zim::Archive) #define GETTER(retType, name) JNIEXPORT retType JNICALL \ Java_org_kiwix_libzim_Archive_##name (JNIEnv* env, jobject thisObj) \ { \ @@ -144,16 +139,16 @@ METHOD(jstring, Archive, getMetadata, jstring name) { } METHOD(jobject, Archive, getMetadataItem, jstring name) { - auto item = THIS->getMetadataItem(TO_C(name)); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item); + auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); + SET_HANDLE(zim::Item, obj, THIS->getMetadataItem(TO_C(name))); return obj; } GETTER(jobjectArray, getMetadataKeys) METHOD(jobject, Archive, getIllustrationItem, jint size) { - auto item = THIS->getIllustrationItem(TO_C(size)); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item); + auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); + SET_HANDLE(zim::Item, obj, THIS->getIllustrationItem(TO_C(size))); return obj; } @@ -164,44 +159,44 @@ METHOD(jboolean, Archive, hasIllustration, jint size) { GETTER(jlongArray, getIllustrationSizes) METHOD(jobject, Archive, getEntryByPath, jlong index) { - auto entry = THIS->getEntryByPath(TO_C(index)); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(index))); return obj; } METHOD(jobject, Archive, getEntryByPath, jstring path) { - auto entry = THIS->getEntryByPath(TO_C(path)); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(path))); return obj; } METHOD(jobject, Archive, getEntryByTitle, jlong index) { - auto entry = THIS->getEntryByTitle(TO_C(index)); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(index))); return obj; } METHOD(jobject, Archive, getEntryByTitle, jstring title) { - auto entry = THIS->getEntryByTitle(TO_C(title)); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(title))); return obj; } METHOD(jobject, Archive, getEntryByClusterOrder, jlong index) { - auto entry = THIS->getEntryByClusterOrder(TO_C(index)); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, THIS->getEntryByClusterOrder(TO_C(index))); return obj; } METHOD0(jobject, Archive, getMainEntry) { - auto entry = THIS->getMainEntry(); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, THIS->getMainEntry()); return obj; } METHOD0(jobject, Archive, getRandomEntry) { - auto entry = THIS->getRandomEntry(); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, THIS->getRandomEntry()); return obj; } diff --git a/lib/src/main/cpp/libzim/blob.cpp b/lib/src/main/cpp/libzim/blob.cpp index 27f5ee6..39bed81 100644 --- a/lib/src/main/cpp/libzim/blob.cpp +++ b/lib/src/main/cpp/libzim/blob.cpp @@ -28,13 +28,14 @@ #include +#define NATIVE_TYPE zim::Blob + JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_libzim_Blob_dispose(JNIEnv* env, jobject thisObj) { - dispose(env, thisObj); + dispose(env, thisObj); } - -#define THIS (Handle::getHandle(env, thisObj)) +#define THIS GET_PTR(NATIVE_TYPE) #define GETTER(retType, name) JNIEXPORT retType JNICALL \ Java_org_kiwix_libzim_Blob__##name (JNIEnv* env, jobject thisObj) \ { \ @@ -42,6 +43,6 @@ Java_org_kiwix_libzim_Blob__##name (JNIEnv* env, jobject thisObj) \ } METHOD0(jstring, Blob, getData) { - return TO_JNI(std::string(**THIS)); + return TO_JNI(std::string(*THIS)); } GETTER(jlong, size) diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp index d71f0d5..6e5ce73 100644 --- a/lib/src/main/cpp/libzim/entry.cpp +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -34,10 +34,10 @@ JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_libzim_Entry_dispose(JNIEnv* env, jobject thisObj) { - dispose(env, thisObj); + dispose(env, thisObj); } -#define THIS (Handle::getHandle(env, thisObj)) +#define THIS GET_PTR(NATIVE_TYPE) #define GETTER(retType, name) JNIEXPORT retType JNICALL \ Java_org_kiwix_libzim_Entry__##name (JNIEnv* env, jobject thisObj) \ { \ @@ -49,19 +49,19 @@ GETTER(jboolean, isRedirect) GETTER(jstring, getTitle) GETTER(jstring, getPath) METHOD(jobject, Entry, getItem, jboolean follow) { - auto item = THIS->getItem(TO_C(follow)); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item); + auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); + SET_HANDLE(zim::Item, obj, THIS->getItem(TO_C(follow))); return obj; } METHOD0(jobject, Entry, getRedirect) { - auto item = THIS->getRedirect(); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Item", item); + auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); + SET_HANDLE(zim::Item, obj, THIS->getRedirect()); return obj; } METHOD0(jobject, Entry, getRedirectEntry) { - auto entry = THIS->getRedirectEntry(); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Entry", entry); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, THIS->getRedirectEntry()); return obj; } diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp index 1168a2d..81d7aaf 100644 --- a/lib/src/main/cpp/libzim/item.cpp +++ b/lib/src/main/cpp/libzim/item.cpp @@ -28,13 +28,15 @@ #include +#define NATIVE_TYPE zim::Item + JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_libzim_Item_dispose(JNIEnv* env, jobject thisObj) { - dispose(env, thisObj); + dispose(env, thisObj); } -#define THIS (Handle::getHandle(env, thisObj)) +#define THIS GET_PTR(NATIVE_TYPE) #define GETTER(retType, name) JNIEXPORT retType JNICALL \ Java_org_kiwix_libzim_Item__##name (JNIEnv* env, jobject thisObj) \ { \ @@ -46,8 +48,8 @@ GETTER(jstring, getPath) GETTER(jstring, getMimetype) METHOD0(jobject, Item, getData) { - auto blob = THIS->getData(); - auto obj = CREATE_WRAPPER("org/kiwix/libzim/Blob", blob); + auto obj = NEW_OBJECT("org/kiwix/libzim/Blob"); + SET_HANDLE(zim::Blob, obj, THIS->getData()); return obj; } diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index e5475e8..7557637 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -38,34 +38,74 @@ #endif extern std::mutex globalLock; +using std::shared_ptr; +// Here is the wrapping structure. +// All java class wrapping a `Native` instance must contain a pointer (cast as a long (J)) called "nativeHandle". +// They also need a constructor taking no argument (or they can, but helper here cannot be used.) +// The "nativeHandle" pointer points to a heap allocated `Handle`. The handle itself IS a shared_ptr. +// !!! This is a shared_ptr heap allocated instance which contains a `Native` heap allocated. +// So if the `Native` instance is own by a smart pointer (shared_ptr), the shared_ptr itself is "own" by a raw pointer. + +// From the jobject and the pointer field ("nativeHandle"), we can access a shared_ptr (not a pointer to a shared_ptr). +// To avoid a memory leak, we must at java wrapper destruction (dispose), delete the heap shared_ptr. + +// Create a java object using a default constructor. No field is set. +inline jobject newObject(const char* className, JNIEnv* env) { + jclass objClass = env->FindClass(className); + jmethodID initMethod = env->GetMethodID(objClass, "", "()V"); + jobject wrapper = env->NewObject(objClass, initMethod); + return wrapper; +} +#define NEW_OBJECT(CLASSNAME) newObject(CLASSNAME, env) + + +// Set the pointer to the wrapped object. template -void setPtr(JNIEnv* env, jobject thisObj, T* ptr) +inline void setPtr(JNIEnv* env, jobject thisObj, shared_ptr&& ptr, const char* handleName = "nativeHandle") { jclass thisClass = env->GetObjectClass(thisObj); - jfieldID fieldId = env->GetFieldID(thisClass, "nativeHandle", "J"); - env->SetLongField(thisObj, fieldId, reinterpret_cast(ptr)); + jfieldID fieldId = env->GetFieldID(thisClass, handleName, "J"); + // if field id is not null, we are leaking the currently stored handle + assert(env->GetLongField(thisObj, fieldId) == 0); + // allocate a shared_ptr on the head + shared_ptr* handle = new shared_ptr(ptr); + env->SetLongField(thisObj, fieldId, reinterpret_cast(handle)); } +#define SET_PTR(SHARED_PTR) setPtr(env, thisObj, std::move(SHARED_PTR)) +// This create a object and set it in the wrapper template -void allocate(JNIEnv* env, jobject thisObj, Args && ...args) +inline void setHandle(JNIEnv* env, jobject thisObj, Args && ...args) { - T* ptr = new T(std::forward(args)...); - setPtr(env, thisObj, ptr); + auto ptr = std::make_shared(std::forward(args)...); + setPtr(env, thisObj, std::move(ptr)); } +#define SET_HANDLE(NATIVE_TYPE, OBJ, VALUE) setHandle(env, OBJ, VALUE) + +// Return a shared_ptr for the handle template -T* getPtr(JNIEnv* env, jobject thisObj) +shared_ptr getPtr(JNIEnv* env, jobject thisObj, const char* handleName = "nativeHandle") { jclass thisClass = env->GetObjectClass(thisObj); - jfieldID fidNumber = env->GetFieldID(thisClass, "nativeHandle", "J"); - return reinterpret_cast(env->GetLongField(thisObj, fidNumber)); + jfieldID fidNumber = env->GetFieldID(thisClass, handleName, "J"); + auto handle = reinterpret_cast*>(env->GetLongField(thisObj, fidNumber)); + return *handle; } +#define GET_PTR(NATIVE_TYPE) getPtr(env, thisObj) +// Delete the heap allocated handle template -void dispose(JNIEnv* env, jobject thisObj) +void dispose(JNIEnv* env, jobject thisObj, const char* handleName = "nativeHandle") { - delete getPtr(env, thisObj); + jclass thisClass = env->GetObjectClass(thisObj); + jfieldID fieldId = env->GetFieldID(thisClass, handleName, "J"); + auto handle = reinterpret_cast*>(env->GetLongField(thisObj, fieldId)); + if (handle != 0) { + delete handle; + } + env->SetLongField(thisObj, fieldId, 0); } #define METHOD0(retType, class, name) \ @@ -76,11 +116,11 @@ JNIEXPORT retType JNICALL Java_org_kiwix_libzim_##class##_##name( \ JNIEXPORT retType JNICALL Java_org_kiwix_libzim_##class##_##name( \ JNIEnv* env, jobject thisObj, __VA_ARGS__) -inline jfieldID getHandleField(JNIEnv* env, jobject obj) +inline jfieldID getHandleField(JNIEnv* env, jobject obj, const char* handleName) { jclass c = env->GetObjectClass(obj); // J is the type signature for long: - return env->GetFieldID(c, "nativeHandle", "J"); + return env->GetFieldID(c, handleName, "J"); } inline jobjectArray createArray(JNIEnv* env, size_t length, const std::string& type_sig) @@ -98,55 +138,6 @@ class Lock : public std::unique_lock Lock() : std::unique_lock(globalLock) { } }; -template -class LockedHandle; - - -/* - * A handle to a shared_ptr - * Not usable by itself, you must get a LockedHandle to access the value. - */ -template -class Handle -{ - protected: - std::shared_ptr value; - - public: - Handle(T* v) : value(v){}; - - // No destructor. This must and will be handled by dispose method. - - static LockedHandle getHandle(JNIEnv* env, jobject obj) - { - jlong handle = env->GetLongField(obj, getHandleField(env, obj)); - return LockedHandle(reinterpret_cast*>(handle)); - } - - static void dispose(JNIEnv* env, jobject obj) - { - auto lHandle = getHandle(env, obj); - auto handle = lHandle.h; - delete handle; - } - friend class LockedHandle; -}; - -/* - * A locked handle. - * Only one LockedHandle can exist at the same time. - * As LockedHandle is the - */ -template -struct LockedHandle : public Lock { - Handle* h; - LockedHandle(Handle* h) : h(h) {} - T* operator->() { return h->value.get(); } - T* operator*() { return h->value.get(); } - operator bool() const { return (h->value); } - operator T*() const { return h->value.get(); } -}; - // --------------------------------------------------------------------------- // Convert things to JAVA // --------------------------------------------------------------------------- @@ -160,6 +151,8 @@ template<> struct JType{ typedef jlong type_t; }; template<> struct JType { typedef jlong type_t; }; template<> struct JType { typedef jlong type_t; }; template<> struct JType{ typedef jstring type_t; }; +template<> struct JType { typedef jfloat type_t;}; +template<> struct JType { typedef jdouble type_t;}; template @@ -282,6 +275,8 @@ template<> struct CType{ typedef bool type_t; }; template<> struct CType{ typedef int type_t; }; template<> struct CType{ typedef long type_t; }; template<> struct CType{ typedef std::string type_t; }; +template<> struct CType { typedef float type_t; }; +template<> struct CType { typedef double type_t; }; template struct CType{ typedef std::vector::type_t> type_t; }; @@ -362,15 +357,4 @@ inline void setDaiObjValue(const std::string& filename, const long offset, env->SetLongField(obj, offsetFid, offset); } -template -inline jobject createWrapper(const char* className, T && nativeObj, JNIEnv* env) { - jclass objClass = env->FindClass(className); - jmethodID initMethod = env->GetMethodID(objClass, "", "(J)V"); - jlong nativeHandle = reinterpret_cast(new Handle(new T(std::forward(nativeObj)))); - jobject object = env->NewObject(objClass, initMethod, nativeHandle); - return object; -} - -#define CREATE_WRAPPER(CLASSNAME, OBJ) createWrapper(CLASSNAME, std::move(OBJ), env) - #endif // _ANDROID_JNI_UTILS_H diff --git a/lib/src/main/java/org/kiwix/libzim/Archive.java b/lib/src/main/java/org/kiwix/libzim/Archive.java index b6d8104..a663c65 100644 --- a/lib/src/main/java/org/kiwix/libzim/Archive.java +++ b/lib/src/main/java/org/kiwix/libzim/Archive.java @@ -29,7 +29,7 @@ public class Archive public Archive(String filename) throws ZimFileFormatException { - nativeHandle = getNativeArchive(filename); + setNativeArchive(filename); if (nativeHandle == 0) { throw new ZimFileFormatException("Cannot open zimfile "+filename); } @@ -37,7 +37,7 @@ public Archive(String filename) throws ZimFileFormatException public Archive(FileDescriptor fd) throws ZimFileFormatException { - nativeHandle = getNativeArchiveByFD(fd); + setNativeArchiveByFD(fd); if (nativeHandle == 0) { throw new ZimFileFormatException("Cannot open zimfile by fd "+fd.toString()); } @@ -46,7 +46,7 @@ public Archive(FileDescriptor fd) throws ZimFileFormatException public Archive(FileDescriptor fd, long offset, long size) throws ZimFileFormatException { - nativeHandle = getNativeArchiveEmbedded(fd, offset, size); + setNativeArchiveEmbedded(fd, offset, size); if (nativeHandle == 0) { throw new ZimFileFormatException(String.format("Cannot open embedded zimfile (fd=%s, offset=%d, size=%d)", fd, offset, size)); } @@ -93,9 +93,9 @@ public Archive(FileDescriptor fd, long offset, long size) public native boolean hasNewNamespaceScheme(); - private native long getNativeArchive(String filename); - private native long getNativeArchiveByFD(FileDescriptor fd); - private native long getNativeArchiveEmbedded(FileDescriptor fd, long offset, long size); + private native void setNativeArchive(String filename); + private native void setNativeArchiveByFD(FileDescriptor fd); + private native void setNativeArchiveEmbedded(FileDescriptor fd, long offset, long size); ///--------- The wrapper thing diff --git a/lib/src/main/java/org/kiwix/libzim/Blob.java b/lib/src/main/java/org/kiwix/libzim/Blob.java index c6ccf6c..fc8390a 100644 --- a/lib/src/main/java/org/kiwix/libzim/Blob.java +++ b/lib/src/main/java/org/kiwix/libzim/Blob.java @@ -23,8 +23,6 @@ public class Blob { - private Blob(long handle) {nativeHandle = handle;} - public native String getData(); public native long size(); diff --git a/lib/src/main/java/org/kiwix/libzim/Entry.java b/lib/src/main/java/org/kiwix/libzim/Entry.java index 713691b..67b47f6 100644 --- a/lib/src/main/java/org/kiwix/libzim/Entry.java +++ b/lib/src/main/java/org/kiwix/libzim/Entry.java @@ -23,8 +23,6 @@ public class Entry { - private Entry(long handle) {nativeHandle = handle;} - public native boolean isRedirect(); public native String getTitle(); public native String getPath(); diff --git a/lib/src/main/java/org/kiwix/libzim/Item.java b/lib/src/main/java/org/kiwix/libzim/Item.java index e96b2a7..1c198fc 100644 --- a/lib/src/main/java/org/kiwix/libzim/Item.java +++ b/lib/src/main/java/org/kiwix/libzim/Item.java @@ -23,8 +23,6 @@ public class Item { - private Item(long handle) {nativeHandle = handle;} - public native String getTitle(); public native String getPath(); public native String getMimeType(); From 9b971ce4dcb049f097e5c107cf51f2c43d35c1ba Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 11 Jan 2023 16:17:31 +0100 Subject: [PATCH 04/32] Add wrapping around Search --- lib/build.gradle | 2 +- lib/src/main/cpp/CMakeLists.txt | 4 + lib/src/main/cpp/libzim/query.cpp | 65 ++++++++++++++++ lib/src/main/cpp/libzim/search.cpp | 55 +++++++++++++ lib/src/main/cpp/libzim/search_iterator.cpp | 75 ++++++++++++++++++ lib/src/main/cpp/libzim/searcher.cpp | 78 +++++++++++++++++++ lib/src/main/java/org/kiwix/libzim/Query.java | 8 +- .../main/java/org/kiwix/libzim/Search.java | 4 +- .../java/org/kiwix/libzim/SearchIterator.java | 14 ++-- .../org/kiwix/libzim/SearchResultSet.java | 35 --------- .../main/java/org/kiwix/libzim/Searcher.java | 16 ++-- 11 files changed, 303 insertions(+), 53 deletions(-) create mode 100644 lib/src/main/cpp/libzim/query.cpp create mode 100644 lib/src/main/cpp/libzim/search.cpp create mode 100644 lib/src/main/cpp/libzim/search_iterator.cpp create mode 100644 lib/src/main/cpp/libzim/searcher.cpp delete mode 100644 lib/src/main/java/org/kiwix/libzim/SearchResultSet.java diff --git a/lib/build.gradle b/lib/build.gradle index 395b15c..a9070dd 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -285,5 +285,5 @@ task checkCurrentJavaVersion() { task generateHeaderFilesFromJavaWrapper(type: Exec) { workingDir "${projectDir}/src/main/java/org/kiwix/" - commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ libzim/Archive.java libzim/Blob.java libzim/Entry.java libzim/Item.java libzim/ZimFileFormatException.java" + commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ libzim/Archive.java libzim/Blob.java libzim/Entry.java libzim/Item.java libzim/ZimFileFormatException.java libzim/Searcher.java libzim/Search.java libzim/Query.java libzim/SearchIterator.java" } diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt index cfa907f..5476ab1 100644 --- a/lib/src/main/cpp/CMakeLists.txt +++ b/lib/src/main/cpp/CMakeLists.txt @@ -15,6 +15,10 @@ add_library( libzim/entry.cpp libzim/item.cpp libzim/blob.cpp + libzim/searcher.cpp + libzim/query.cpp + libzim/search.cpp + libzim/search_iterator.cpp ) find_library(libzim diff --git a/lib/src/main/cpp/libzim/query.cpp b/lib/src/main/cpp/libzim/query.cpp new file mode 100644 index 0000000..c39e0cd --- /dev/null +++ b/lib/src/main/cpp/libzim/query.cpp @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Searcher.h" + +#include + +#include + +#include + +#define NATIVE_TYPE zim::Query + +JNIEXPORT void JNICALL Java_org_kiwix_libzim_Query_getNativeQuery( + JNIEnv* env, jobject thisObj, jstring query) +{ + auto cQuery = TO_C(query); + Lock l; + try { + auto query = std::make_shared(cQuery); + SET_PTR(query); + } catch (std::exception& e) { + LOG("Cannot create query"); + LOG("%s", e.what()); + } +} + + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_Query_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS GET_PTR(NATIVE_TYPE) + +METHOD(jobject, Query, setQuery, jstring query) { + THIS->setQuery(TO_C(query)); + return thisObj; +} + +METHOD(jobject, Query, setGeorange, jfloat latitude, jfloat longitude, jfloat distance) { + THIS->setGeorange(TO_C(latitude), TO_C(longitude), TO_C(distance)); + return thisObj; +} + diff --git a/lib/src/main/cpp/libzim/search.cpp b/lib/src/main/cpp/libzim/search.cpp new file mode 100644 index 0000000..057aa06 --- /dev/null +++ b/lib/src/main/cpp/libzim/search.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Search.h" + +#include + +#include + +#include + +#define NATIVE_TYPE zim::Search + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_Search_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS GET_PTR(NATIVE_TYPE) + +METHOD(jobject, Search, getResults, jint start, jint maxResults) { + auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); + auto obj = NEW_OBJECT("ork/kiwix/libzim/SearchIterator"); + SET_HANDLE(zim::SearchIterator, obj, results.begin()); + + // We have to set the nativeHandleEnd but no macro ease our work here. + auto end_ptr = std::make_shared(results.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD0(jlong, Search, getEstimatedMatches) { + return TO_JNI(THIS->getEstimatedMatches()); +} + diff --git a/lib/src/main/cpp/libzim/search_iterator.cpp b/lib/src/main/cpp/libzim/search_iterator.cpp new file mode 100644 index 0000000..9b1fa18 --- /dev/null +++ b/lib/src/main/cpp/libzim/search_iterator.cpp @@ -0,0 +1,75 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SearchIterator.h" + +#include + +#include + +#include +#include + +#define NATIVE_TYPE zim::SearchIterator + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_SearchIterotar_dispose(JNIEnv* env, jobject thisObj) +{ + // Delete end iterator + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); +} + +#define THIS GET_PTR(NATIVE_TYPE) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_SearchIterator__##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + +GETTER(jstring, getPath) +GETTER(jstring, getTitle) +GETTER(jint, getScore) +GETTER(jstring, getSnippet) +GETTER(jint, getWordCount) +GETTER(jint, getFileIndex) +GETTER(jint, getSize) + +METHOD0(jstring, SearchIterator, getZimId) { + return TO_JNI(std::string(THIS->getZimId())); +} + +METHOD0(jboolean, SearchIterator, hasNext) { + zim::SearchIterator next(*THIS); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; +} + +METHOD0(jobject, SearchIterator, next) { + (*THIS)++; + zim::Entry entry = **THIS; + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, entry); + return obj; +} + diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp new file mode 100644 index 0000000..2ce9cd4 --- /dev/null +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_Searcher.h" + +#include + +#include + +#include + +#define NATIVE_TYPE zim::Searcher + +JNIEXPORT void JNICALL Java_org_kiwix_libzim_Searcher_setNativeSearcher( + JNIEnv* env, jobject thisObj, jobject archive) +{ + + Lock l; + auto cArchive = getPtr(env, archive); + try { + auto searcher = std::make_shared(*cArchive); + SET_PTR(searcher); + } catch (std::exception& e) { + LOG("Cannot create searcher"); + LOG("%s", e.what()); + } +} + + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_Searcher_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS GET_PTR(NATIVE_TYPE) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_Searcher__##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + +METHOD(jobject, Searcher, addArchive, jobject archive) { + auto cArchive = getPtr(env, archive); + THIS->addArchive(*cArchive); + return thisObj; +} + +METHOD(jobject, Searcher, search, jobject query) { + auto cQuery = getPtr(env, query); + auto obj = NEW_OBJECT("org/kiwix/libzim/Search"); + SET_HANDLE(zim::Search, obj, THIS->search(*cQuery)); + return obj; +} + +METHOD(void, Searcher, setVerbose, jboolean verbose) { + THIS->setVerbose(TO_C(verbose)); +} + diff --git a/lib/src/main/java/org/kiwix/libzim/Query.java b/lib/src/main/java/org/kiwix/libzim/Query.java index 30eb781..1a71408 100644 --- a/lib/src/main/java/org/kiwix/libzim/Query.java +++ b/lib/src/main/java/org/kiwix/libzim/Query.java @@ -21,8 +21,11 @@ public class Query { - public Query(string query); - public native Query setQuery(string query); + public Query(String query) { + setNativeQuery(query); + } + + public native Query setQuery(String query); public native Query setGeorange(float latitude, float longitute, float distance); ///--------- The wrapper thing @@ -30,5 +33,6 @@ public class Query public native void dispose(); // A pointer (as a long) to a native Handle + private native long setNativeQuery(String query); private long nativeHandle; } diff --git a/lib/src/main/java/org/kiwix/libzim/Search.java b/lib/src/main/java/org/kiwix/libzim/Search.java index e3c62b8..8901344 100644 --- a/lib/src/main/java/org/kiwix/libzim/Search.java +++ b/lib/src/main/java/org/kiwix/libzim/Search.java @@ -19,11 +19,11 @@ package org.kiwix.libzim; -import org.kiwix.libzim.SearchResultSet; +import org.kiwix.libzim.SearchIterator; public class Search { - public native SearchResultSet getResults(int start, int maxResults); + public native SearchIterator getResults(int start, int maxResults); public native long getEstimatedMatches(); ///--------- The wrapper thing diff --git a/lib/src/main/java/org/kiwix/libzim/SearchIterator.java b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java index ac93003..f623f15 100644 --- a/lib/src/main/java/org/kiwix/libzim/SearchIterator.java +++ b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java @@ -20,17 +20,18 @@ package org.kiwix.libzim; import org.kiwix.libzim.SearchIterator; +import java.util.Iterator; -public class SearchIterator implement Iterator +public class SearchIterator implements Iterator { - public native string getPath(); - public native string getTitle(); + public native String getPath(); + public native String getTitle(); public native int getScore(); - public native string getSnippet(); + public native String getSnippet(); public native int getWordCount(); public native int getFileIndex(); public native int size(); - public native string getZimId(); + public native String getZimId(); public native boolean hasNext(); public native Entry next(); @@ -41,4 +42,7 @@ public class SearchIterator implement Iterator // A pointer (as a long) to a native Handle private long nativeHandle; + + // A pointer (as a long) to the native end + private long nativeHandleEnd; } diff --git a/lib/src/main/java/org/kiwix/libzim/SearchResultSet.java b/lib/src/main/java/org/kiwix/libzim/SearchResultSet.java deleted file mode 100644 index 27c9833..0000000 --- a/lib/src/main/java/org/kiwix/libzim/SearchResultSet.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (C) 2022 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -package org.kiwix.libzim; - -import org.kiwix.libzim.SearchIterator; - -public class SearchResultSet -{ - public native SearchIterator begin(); - public native long size(); - -///--------- The wrapper thing - // To delete our native wrapper - public native void dispose(); - - // A pointer (as a long) to a native Handle - private long nativeHandle; -} diff --git a/lib/src/main/java/org/kiwix/libzim/Searcher.java b/lib/src/main/java/org/kiwix/libzim/Searcher.java index b1158a3..13dc73e 100644 --- a/lib/src/main/java/org/kiwix/libzim/Searcher.java +++ b/lib/src/main/java/org/kiwix/libzim/Searcher.java @@ -28,19 +28,19 @@ public class Searcher { - public Searcĥer(Archive archive) throws ZimFileFormatException + public Searcher(Archive archive) throws Exception { - nativeHandle = getNativeSearcher(archive); + setNativeSearcher(archive); if (nativeHandle == 0) { - throw new ZimFileFormatException("Cannot open zimfile "+filename); + throw new Exception("Cannot create searcher"); } } - public Searcher(List archives) throws ZimFileFormatException + public Searcher(Archive[] archives) throws Exception { - nativeHandle = getNativeSearcher(archives); + setNativeSearcherMulti(archives); if (nativeHandle == 0) { - throw new ZimFileFormatException("Cannot open zimfile by fd "+fd.toString()); + throw new Exception("Cannot create searcher"); } } @@ -48,8 +48,8 @@ public Searcher(List archives) throws ZimFileFormatException public native Search search(Query query); public native void setVerbose(boolean verbose); - private native long getNativeSearcher(Archive archive); - private native long getNativeSearcherMulti(List archives); + private native void setNativeSearcher(Archive archive); + private native void setNativeSearcherMulti(Archive[] archives); ///--------- The wrapper thing // To delete our native wrapper From f3ac28f56a4893fa0ddcebe15c02200f5da3ef2f Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 17 Jan 2023 15:10:16 +0100 Subject: [PATCH 05/32] Generate headers for all the `*.java` files. --- lib/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/build.gradle b/lib/build.gradle index a9070dd..ef7d759 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -285,5 +285,5 @@ task checkCurrentJavaVersion() { task generateHeaderFilesFromJavaWrapper(type: Exec) { workingDir "${projectDir}/src/main/java/org/kiwix/" - commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ libzim/Archive.java libzim/Blob.java libzim/Entry.java libzim/Item.java libzim/ZimFileFormatException.java libzim/Searcher.java libzim/Search.java libzim/Query.java libzim/SearchIterator.java" + commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ libzim/*.java" } From 3d2f3083b01b67522e6c76fd584cc8385f227485 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 17 Jan 2023 15:14:26 +0100 Subject: [PATCH 06/32] Wrap suggestion searcher. --- lib/src/main/cpp/CMakeLists.txt | 4 ++ lib/src/main/cpp/libzim/suggestion_item.cpp | 50 +++++++++++++ .../main/cpp/libzim/suggestion_iterator.cpp | 62 ++++++++++++++++ lib/src/main/cpp/libzim/suggestion_search.cpp | 55 ++++++++++++++ .../main/cpp/libzim/suggestion_searcher.cpp | 71 +++++++++++++++++++ .../java/org/kiwix/libzim/SuggestionItem.java | 39 ++++++++++ .../org/kiwix/libzim/SuggestionIterator.java | 39 ++++++++++ .../org/kiwix/libzim/SuggestionSearch.java | 35 +++++++++ .../org/kiwix/libzim/SuggestionSearcher.java | 49 +++++++++++++ 9 files changed, 404 insertions(+) create mode 100644 lib/src/main/cpp/libzim/suggestion_item.cpp create mode 100644 lib/src/main/cpp/libzim/suggestion_iterator.cpp create mode 100644 lib/src/main/cpp/libzim/suggestion_search.cpp create mode 100644 lib/src/main/cpp/libzim/suggestion_searcher.cpp create mode 100644 lib/src/main/java/org/kiwix/libzim/SuggestionItem.java create mode 100644 lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java create mode 100644 lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java create mode 100644 lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt index 5476ab1..9e87b1d 100644 --- a/lib/src/main/cpp/CMakeLists.txt +++ b/lib/src/main/cpp/CMakeLists.txt @@ -19,6 +19,10 @@ add_library( libzim/query.cpp libzim/search.cpp libzim/search_iterator.cpp + libzim/suggestion_searcher.cpp + libzim/suggestion_search.cpp + libzim/suggestion_iterator.cpp + libzim/suggestion_item.cpp ) find_library(libzim diff --git a/lib/src/main/cpp/libzim/suggestion_item.cpp b/lib/src/main/cpp/libzim/suggestion_item.cpp new file mode 100644 index 0000000..254d8bc --- /dev/null +++ b/lib/src/main/cpp/libzim/suggestion_item.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SuggestionItem.h" + +#include + +#include + +#include + +#define NATIVE_TYPE zim::SuggestionItem + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_SuggestionItem_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS GET_PTR(NATIVE_TYPE) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_SuggestionItem__##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + + +GETTER(jstring, getTitle) +GETTER(jstring, getPath) +GETTER(jstring, getSnippet) +GETTER(jboolean, hasSnippet) diff --git a/lib/src/main/cpp/libzim/suggestion_iterator.cpp b/lib/src/main/cpp/libzim/suggestion_iterator.cpp new file mode 100644 index 0000000..9c1bff0 --- /dev/null +++ b/lib/src/main/cpp/libzim/suggestion_iterator.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SuggestionIterator.h" + +#include + +#include + +#include + +#define NATIVE_TYPE zim::SuggestionIterator + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_SuggestionIterator_dispose(JNIEnv* env, jobject thisObj) +{ + // Delete end iterator + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); +} + +#define THIS GET_PTR(NATIVE_TYPE) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_SuggestionIterator__##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + +METHOD0(jboolean, SearchIterator, hasNext) { + NATIVE_TYPE next(*THIS); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; +} + +METHOD0(jobject, SearchIterator, next) { + (*THIS)++; + zim::SuggestionItem item = **THIS; + auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionItem"); + SET_HANDLE(zim::SuggestionItem, obj, item); + return obj; +} + diff --git a/lib/src/main/cpp/libzim/suggestion_search.cpp b/lib/src/main/cpp/libzim/suggestion_search.cpp new file mode 100644 index 0000000..0b545d7 --- /dev/null +++ b/lib/src/main/cpp/libzim/suggestion_search.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SuggestionSearch.h" + +#include + +#include + +#include + +#define NATIVE_TYPE zim::SuggestionSearch + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_SuggestionSearch_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS GET_PTR(NATIVE_TYPE) + +METHOD(jobject, SuggestionSearch, getResults, jint start, jint maxResults) { + auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); + auto obj = NEW_OBJECT("ork/kiwix/libzim/SuggestionIterator"); + SET_HANDLE(zim::SuggestionIterator, obj, results.begin()); + + // We have to set the nativeHandleEnd but no macro ease our work here. + auto end_ptr = std::make_shared(results.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD0(jlong, SuggestionSearch, getEstimatedMatches) { + return TO_JNI(THIS->getEstimatedMatches()); +} + diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp new file mode 100644 index 0000000..7c8ad2e --- /dev/null +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_SuggestionSearcher.h" + +#include + +#include + +#include + +#define NATIVE_TYPE zim::SuggestionSearcher + +JNIEXPORT void JNICALL Java_org_kiwix_libzim_SuggestionSearcher_setNativeSearcher( + JNIEnv* env, jobject thisObj, jobject archive) +{ + + Lock l; + auto cArchive = getPtr(env, archive); + try { + auto searcher = std::make_shared(*cArchive); + SET_PTR(searcher); + } catch (std::exception& e) { + LOG("Cannot create searcher"); + LOG("%s", e.what()); + } +} + + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_SuggestionSearcher_dispose(JNIEnv* env, jobject thisObj) +{ + dispose(env, thisObj); +} + +#define THIS GET_PTR(NATIVE_TYPE) +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libzim_SuggestionSearcher__##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ +} + +METHOD(jobject, SuggestionSearcher, suggest, jstring query) { + auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionSearch"); + SET_HANDLE(zim::SuggestionSearch, obj, THIS->suggest(TO_C(query))); + return obj; +} + +METHOD(void, SuggestionSearcher, setVerbose, jboolean verbose) { + THIS->setVerbose(TO_C(verbose)); +} + diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java b/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java new file mode 100644 index 0000000..eef51a7 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +public class SuggestionItem +{ + public native String getTitle(); + public native String getPath(); + public native String getSnippet(); + public native boolean hasSnippet(); + + protected void finalize() { + dispose(); + } + +///--------- The wrapper thing + // To delete our native wrapper + private native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java b/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java new file mode 100644 index 0000000..553974b --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SuggestionItem; +import java.util.Iterator; + +public class SuggestionIterator implements Iterator +{ + public native boolean hasNext(); + public native SuggestionItem next(); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; + + // A pointer (as a long) to the native end + private long nativeHandleEnd; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java b/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java new file mode 100644 index 0000000..427f425 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.SuggestionIterator; + +public class SuggestionSearch +{ + public native SuggestionIterator getResults(int start, int maxResults); + public native long getEstimatedMatches(); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java b/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java new file mode 100644 index 0000000..c455022 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import org.kiwix.libzim.ZimFileFormatException; +import org.kiwix.libzim.Archive; +import org.kiwix.libzim.SuggestionSearch; +import java.io.FileDescriptor; + +public class SuggestionSearcher +{ + + public SuggestionSearcher(Archive archive) throws Exception + { + setNativeSearcher(archive); + if (nativeHandle == 0) { + throw new Exception("Cannot create searcher"); + } + } + + public native SuggestionSearch suggest(String query); + public native void setVerbose(boolean verbose); + + private native void setNativeSearcher(Archive archive); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private long nativeHandle; +} From 4aa8eeedfb1c5232d8cb6c863874faf40cbfc60e Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 17 Jan 2023 16:37:42 +0100 Subject: [PATCH 07/32] Wrap `Archive::iterByFoo` and `Archive::findByFoo` --- lib/src/main/cpp/CMakeLists.txt | 1 + lib/src/main/cpp/libzim/archive.cpp | 60 ++++++++++ lib/src/main/cpp/libzim/entry_iterator.cpp | 111 ++++++++++++++++++ .../main/java/org/kiwix/libzim/Archive.java | 7 ++ .../java/org/kiwix/libzim/EntryIterator.java | 44 +++++++ 5 files changed, 223 insertions(+) create mode 100644 lib/src/main/cpp/libzim/entry_iterator.cpp create mode 100644 lib/src/main/java/org/kiwix/libzim/EntryIterator.java diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt index 9e87b1d..db24255 100644 --- a/lib/src/main/cpp/CMakeLists.txt +++ b/lib/src/main/cpp/CMakeLists.txt @@ -13,6 +13,7 @@ add_library( common.cpp libzim/archive.cpp libzim/entry.cpp + libzim/entry_iterator.cpp libzim/item.cpp libzim/blob.cpp libzim/searcher.cpp diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index c6a4763..4a93f53 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -223,5 +223,65 @@ GETTER(jboolean, check) GETTER(jboolean, isMultiPart) GETTER(jboolean, hasNewNamespaceScheme) +#define ITER_BY_PATH 0 +#define ITER_BY_TITLE 1 +#define ITER_EFFICIENT 2 +METHOD0(jobject, Archive, iterByPath) { + auto range = THIS->iterByPath(); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_BY_PATH); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD0(jobject, Archive, iterByTitle) { + auto range = THIS->iterByTitle(); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_BY_TITLE); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD0(jobject, Archive, iterEfficient) { + auto range = THIS->iterEfficient(); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_EFFICIENT); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} + +METHOD(jobject, Archive, findByPath, jstring path) { + auto range = THIS->findByPath(TO_C(path)); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_BY_PATH); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} +METHOD(jobject, Archive, findByTitle, jstring title) { + auto range = THIS->findByTitle(TO_C(title)); + jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); + jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); + jobject obj = env->NewObject(objClass, initMethod, ITER_BY_TITLE); + SET_HANDLE(zim::Archive::iterator, obj, range.begin()); + auto end_ptr = std::make_shared>(range.end()); + setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd"); + return obj; +} diff --git a/lib/src/main/cpp/libzim/entry_iterator.cpp b/lib/src/main/cpp/libzim/entry_iterator.cpp new file mode 100644 index 0000000..88a904e --- /dev/null +++ b/lib/src/main/cpp/libzim/entry_iterator.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include +#include +#include "org_kiwix_libzim_EntryIterator.h" + +#include + +#include + +#include +#include + +#define PATH_NATIVE_TYPE zim::Archive::iterator +#define TITLE_NATIVE_TYPE zim::Archive::iterator +#define EFFICIENT_NATIVE_TYPE zim::Archive::iterator + +inline int get_order(JNIEnv* env, jobject thisObj) { + jclass thisClass = env->GetObjectClass(thisObj); + jfieldID fieldId = env->GetFieldID(thisClass, "order", "I"); + return TO_C(env->GetIntField(thisObj, fieldId)); +} + +JNIEXPORT void JNICALL +Java_org_kiwix_kiwixlib_libzim_EntryIterotar_dispose(JNIEnv* env, jobject thisObj) +{ + // Delete end iterator + switch (get_order(env, thisObj)) { + case 0: + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); + break; + case 1: + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); + break; + case 2: + dispose(env, thisObj, "nativeHandleEnd"); + dispose(env, thisObj); + break; + } +} + + +METHOD0(jboolean, EntryIterator, hasNext) { + switch (get_order(env, thisObj)) { + case 0: { + PATH_NATIVE_TYPE next(*GET_PTR(PATH_NATIVE_TYPE)); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; + } + case 1: { + TITLE_NATIVE_TYPE next(*GET_PTR(TITLE_NATIVE_TYPE)); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; + } + case 2: { + EFFICIENT_NATIVE_TYPE next(*GET_PTR(EFFICIENT_NATIVE_TYPE)); + next++; + auto end = getPtr(env, thisObj, "nativeHandleEnd"); + return next == *end; + } + } +} + +METHOD0(jobject, EntryIterator, next) { + switch (get_order(env, thisObj)) { + case 0: { + (*GET_PTR(PATH_NATIVE_TYPE))++; + zim::Entry entry = **GET_PTR(PATH_NATIVE_TYPE); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, entry); + return obj; + } + case 1: { + (*GET_PTR(TITLE_NATIVE_TYPE))++; + zim::Entry entry = **GET_PTR(TITLE_NATIVE_TYPE); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, entry); + return obj; + } + case 2: { + (*GET_PTR(EFFICIENT_NATIVE_TYPE))++; + zim::Entry entry = **GET_PTR(EFFICIENT_NATIVE_TYPE); + auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); + SET_HANDLE(zim::Entry, obj, entry); + return obj; + } + } +} + diff --git a/lib/src/main/java/org/kiwix/libzim/Archive.java b/lib/src/main/java/org/kiwix/libzim/Archive.java index a663c65..86d4129 100644 --- a/lib/src/main/java/org/kiwix/libzim/Archive.java +++ b/lib/src/main/java/org/kiwix/libzim/Archive.java @@ -22,6 +22,7 @@ import org.kiwix.libzim.ZimFileFormatException; import org.kiwix.libzim.Entry; import org.kiwix.libzim.Item; +import org.kiwix.libzim.EntryIterator; import java.io.FileDescriptor; public class Archive @@ -92,6 +93,12 @@ public Archive(FileDescriptor fd, long offset, long size) public native boolean isMultiPart(); public native boolean hasNewNamespaceScheme(); + public native EntryIterator iterByPath(); + public native EntryIterator iterByTitle(); + public native EntryIterator iterEfficient(); + public native EntryIterator findByPath(String path); + public native EntryIterator findByTitle(String path); + private native void setNativeArchive(String filename); private native void setNativeArchiveByFD(FileDescriptor fd); diff --git a/lib/src/main/java/org/kiwix/libzim/EntryIterator.java b/lib/src/main/java/org/kiwix/libzim/EntryIterator.java new file mode 100644 index 0000000..56aa6e3 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libzim/EntryIterator.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libzim; + +import java.util.Iterator; + +public class EntryIterator implements Iterator +{ + private EntryIterator(int order) { + this.order = order; + } + public native boolean hasNext(); + public native Entry next(); + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A marker of the order used for this iterator + private int order; + + // A pointer (as a long) to a native Handle + private long nativeHandle; + + // A pointer (as a long) to the native end + private long nativeHandleEnd; +} From 763731a5e156207e4de47a93227954546c01044f Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 17 Jan 2023 16:42:20 +0100 Subject: [PATCH 08/32] Rename kiwixlib to libkiwix --- lib/src/main/cpp/book.cpp | 2 +- lib/src/main/cpp/filter.cpp | 2 +- lib/src/main/cpp/kiwixicu.cpp | 2 +- lib/src/main/cpp/kiwixserver.cpp | 2 +- lib/src/main/cpp/library.cpp | 2 +- lib/src/main/cpp/manager.cpp | 2 +- .../main/java/org/kiwix/{kiwixlib => libkiwix}/Book.java | 2 +- .../org/kiwix/{kiwixlib => libkiwix}/DirectAccessInfo.java | 2 +- .../main/java/org/kiwix/{kiwixlib => libkiwix}/Filter.java | 2 +- .../main/java/org/kiwix/{kiwixlib => libkiwix}/JNIICU.java | 2 +- .../java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwix.java | 4 ++-- .../java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixBool.java | 2 +- .../org/kiwix/{kiwixlib => libkiwix}/JNIKiwixException.java | 2 +- .../java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixInt.java | 2 +- .../org/kiwix/{kiwixlib => libkiwix}/JNIKiwixReader.java | 0 .../org/kiwix/{kiwixlib => libkiwix}/JNIKiwixSearcher.java | 0 .../org/kiwix/{kiwixlib => libkiwix}/JNIKiwixServer.java | 6 +++--- .../org/kiwix/{kiwixlib => libkiwix}/JNIKiwixString.java | 2 +- .../main/java/org/kiwix/{kiwixlib => libkiwix}/Library.java | 6 +++--- .../main/java/org/kiwix/{kiwixlib => libkiwix}/Manager.java | 4 ++-- 20 files changed, 24 insertions(+), 24 deletions(-) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/Book.java (97%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/DirectAccessInfo.java (96%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/Filter.java (98%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIICU.java (97%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwix.java (94%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixBool.java (96%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixException.java (96%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixInt.java (96%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixReader.java (100%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixSearcher.java (100%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixServer.java (92%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/JNIKiwixString.java (97%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/Library.java (93%) rename lib/src/main/java/org/kiwix/{kiwixlib => libkiwix}/Manager.java (97%) diff --git a/lib/src/main/cpp/book.cpp b/lib/src/main/cpp/book.cpp index 238f490..e766abf 100644 --- a/lib/src/main/cpp/book.cpp +++ b/lib/src/main/cpp/book.cpp @@ -19,7 +19,7 @@ #include -#include "org_kiwix_kiwixlib_Book.h" +#include "org_kiwix_libkiwix_Book.h" #include "utils.h" #include "book.h" diff --git a/lib/src/main/cpp/filter.cpp b/lib/src/main/cpp/filter.cpp index 09c0734..b3b5113 100644 --- a/lib/src/main/cpp/filter.cpp +++ b/lib/src/main/cpp/filter.cpp @@ -19,7 +19,7 @@ #include -#include "org_kiwix_kiwixlib_Filter.h" +#include "org_kiwix_libkiwix_Filter.h" #include "library.h" #include "utils.h" diff --git a/lib/src/main/cpp/kiwixicu.cpp b/lib/src/main/cpp/kiwixicu.cpp index a10b578..e5ecb46 100644 --- a/lib/src/main/cpp/kiwixicu.cpp +++ b/lib/src/main/cpp/kiwixicu.cpp @@ -19,7 +19,7 @@ */ #include -#include "org_kiwix_kiwixlib_JNIICU.h" +#include "org_kiwix_libkiwix_JNIICU.h" #include #include diff --git a/lib/src/main/cpp/kiwixserver.cpp b/lib/src/main/cpp/kiwixserver.cpp index 830fdc6..f9437c5 100644 --- a/lib/src/main/cpp/kiwixserver.cpp +++ b/lib/src/main/cpp/kiwixserver.cpp @@ -20,7 +20,7 @@ #include -#include "org_kiwix_kiwixlib_JNIKiwixServer.h" +#include "org_kiwix_libkiwix_JNIKiwixServer.h" #include "tools/base64.h" #include "server.h" diff --git a/lib/src/main/cpp/library.cpp b/lib/src/main/cpp/library.cpp index f2a0846..9039db2 100644 --- a/lib/src/main/cpp/library.cpp +++ b/lib/src/main/cpp/library.cpp @@ -19,7 +19,7 @@ #include -#include "org_kiwix_kiwixlib_Library.h" +#include "org_kiwix_libkiwix_Library.h" #include "library.h" #include "reader.h" diff --git a/lib/src/main/cpp/manager.cpp b/lib/src/main/cpp/manager.cpp index 4395609..0ca6dbc 100644 --- a/lib/src/main/cpp/manager.cpp +++ b/lib/src/main/cpp/manager.cpp @@ -19,7 +19,7 @@ #include -#include "org_kiwix_kiwixlib_Manager.h" +#include "org_kiwix_libkiwix_Manager.h" #include "manager.h" #include "utils.h" diff --git a/lib/src/main/java/org/kiwix/kiwixlib/Book.java b/lib/src/main/java/org/kiwix/libkiwix/Book.java similarity index 97% rename from lib/src/main/java/org/kiwix/kiwixlib/Book.java rename to lib/src/main/java/org/kiwix/libkiwix/Book.java index 693e833..7961526 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/Book.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Book.java @@ -1,5 +1,5 @@ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class Book { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/DirectAccessInfo.java b/lib/src/main/java/org/kiwix/libkiwix/DirectAccessInfo.java similarity index 96% rename from lib/src/main/java/org/kiwix/kiwixlib/DirectAccessInfo.java rename to lib/src/main/java/org/kiwix/libkiwix/DirectAccessInfo.java index 4ba137d..157fb1d 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/DirectAccessInfo.java +++ b/lib/src/main/java/org/kiwix/libkiwix/DirectAccessInfo.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class DirectAccessInfo { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/Filter.java b/lib/src/main/java/org/kiwix/libkiwix/Filter.java similarity index 98% rename from lib/src/main/java/org/kiwix/kiwixlib/Filter.java rename to lib/src/main/java/org/kiwix/libkiwix/Filter.java index bf9e719..458856d 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/Filter.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Filter.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class Filter { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIICU.java b/lib/src/main/java/org/kiwix/libkiwix/JNIICU.java similarity index 97% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIICU.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIICU.java index db8ba6e..f4ff419 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIICU.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIICU.java @@ -18,7 +18,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class JNIICU { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwix.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java similarity index 94% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwix.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java index 57e148b..2d63938 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwix.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java @@ -18,11 +18,11 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; import android.content.Context; import com.getkeepsafe.relinker.ReLinker; -import org.kiwix.kiwixlib.JNIICU; +import org.kiwix.libkiwix.JNIICU; public class JNIKiwix { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixBool.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixBool.java similarity index 96% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixBool.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwixBool.java index 74563d3..90ffa65 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixBool.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixBool.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class JNIKiwixBool { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixException.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java similarity index 96% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixException.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java index dd0e214..da77973 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixException.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class JNIKiwixException extends Exception { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixInt.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixInt.java similarity index 96% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixInt.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwixInt.java index a66c937..691424e 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixInt.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixInt.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class JNIKiwixInt { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixReader.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixReader.java similarity index 100% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixReader.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwixReader.java diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixSearcher.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixSearcher.java similarity index 100% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixSearcher.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwixSearcher.java diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixServer.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixServer.java similarity index 92% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixServer.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwixServer.java index 11c5f0d..df8e0eb 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixServer.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixServer.java @@ -17,10 +17,10 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; -import org.kiwix.kiwixlib.JNIKiwixException; -import org.kiwix.kiwixlib.Library; +import org.kiwix.libkiwix.JNIKiwixException; +import org.kiwix.libkiwix.Library; public class JNIKiwixServer { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixString.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixString.java similarity index 97% rename from lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixString.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwixString.java index 601fb9d..fd0e052 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/JNIKiwixString.java +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixString.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; public class JNIKiwixString { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/Library.java b/lib/src/main/java/org/kiwix/libkiwix/Library.java similarity index 93% rename from lib/src/main/java/org/kiwix/kiwixlib/Library.java rename to lib/src/main/java/org/kiwix/libkiwix/Library.java index 68aa7d2..60f3af6 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/Library.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Library.java @@ -17,10 +17,10 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; -import org.kiwix.kiwixlib.Book; -import org.kiwix.kiwixlib.JNIKiwixException; +import org.kiwix.libkiwix.Book; +import org.kiwix.libkiwix.JNIKiwixException; public class Library { diff --git a/lib/src/main/java/org/kiwix/kiwixlib/Manager.java b/lib/src/main/java/org/kiwix/libkiwix/Manager.java similarity index 97% rename from lib/src/main/java/org/kiwix/kiwixlib/Manager.java rename to lib/src/main/java/org/kiwix/libkiwix/Manager.java index 2772ca9..5ddbc08 100644 --- a/lib/src/main/java/org/kiwix/kiwixlib/Manager.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Manager.java @@ -17,9 +17,9 @@ * MA 02110-1301, USA. */ -package org.kiwix.kiwixlib; +package org.kiwix.libkiwix; -import org.kiwix.kiwixlib.Library; +import org.kiwix.libkiwix.Library; public class Manager { From 0be73ef8e96428890c72f7a5a9ee9f46b1537d06 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 17 Jan 2023 16:43:29 +0100 Subject: [PATCH 09/32] Remove wrapper around obsolete kiwix::Reader and kiwix::Searcher. --- .../main/java/org/kiwix/libkiwix/Book.java | 4 +- .../java/org/kiwix/libkiwix/JNIKiwixBool.java | 25 --- .../java/org/kiwix/libkiwix/JNIKiwixInt.java | 25 --- .../org/kiwix/libkiwix/JNIKiwixReader.java | 182 ------------------ .../org/kiwix/libkiwix/JNIKiwixSearcher.java | 67 ------- .../org/kiwix/libkiwix/JNIKiwixString.java | 37 ---- 6 files changed, 3 insertions(+), 337 deletions(-) delete mode 100644 lib/src/main/java/org/kiwix/libkiwix/JNIKiwixBool.java delete mode 100644 lib/src/main/java/org/kiwix/libkiwix/JNIKiwixInt.java delete mode 100644 lib/src/main/java/org/kiwix/libkiwix/JNIKiwixReader.java delete mode 100644 lib/src/main/java/org/kiwix/libkiwix/JNIKiwixSearcher.java delete mode 100644 lib/src/main/java/org/kiwix/libkiwix/JNIKiwixString.java diff --git a/lib/src/main/java/org/kiwix/libkiwix/Book.java b/lib/src/main/java/org/kiwix/libkiwix/Book.java index 7961526..9c6edde 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/Book.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Book.java @@ -1,13 +1,15 @@ package org.kiwix.libkiwix; +import org.kiwix.libzim.Archive; + public class Book { public Book() { allocate(); } public native void update(Book book); - public native void update(JNIKiwixReader reader); + public native void update(Archive archive); @Override protected void finalize() { dispose(); } diff --git a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixBool.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixBool.java deleted file mode 100644 index 90ffa65..0000000 --- a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixBool.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -package org.kiwix.libkiwix; - -public class JNIKiwixBool -{ - public boolean value; -} diff --git a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixInt.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixInt.java deleted file mode 100644 index 691424e..0000000 --- a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixInt.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -package org.kiwix.libkiwix; - -public class JNIKiwixInt -{ - public int value; -} diff --git a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixReader.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixReader.java deleted file mode 100644 index e11d721..0000000 --- a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixReader.java +++ /dev/null @@ -1,182 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -package org.kiwix.kiwixlib; - -import org.kiwix.kiwixlib.JNIKiwixException; -import org.kiwix.kiwixlib.JNIKiwixString; -import org.kiwix.kiwixlib.JNIKiwixInt; -import org.kiwix.kiwixlib.JNIKiwixSearcher; -import org.kiwix.kiwixlib.DirectAccessInfo; -import java.io.FileDescriptor; - -public class JNIKiwixReader -{ - public native String getMainPage(); - - public native String getTitle(); - - public native String getId(); - - public native String getLanguage(); - - public native String getMimeType(String url); - - /** - * Check if a url exists and is a redirect or not. - * - * Return an empty string if the url doesn't exist in the reader. - * Return the url of the "final" entry. - * - equal to the input url if the entry is not a redirection. - * - different if the url is a redirection (and the webview should redirect to it). - */ - public native String checkUrl(String url); - - /** - * Get the content of a article. - * - * Return a byte array of the content of the article. - * Set the title, mimeType to the title and mimeType of the article. - * Set the size to the size of the returned array. - * - * If the entry doesn't exist : - * - return a empty byte array - * - set all arguments (except url) to empty/0. - * If the entry exist but is a redirection : - * - return an empty byte array - * - set all arguments (including url) to information of the targeted article. - */ - public native byte[] getContent(JNIKiwixString url, - JNIKiwixString title, - JNIKiwixString mimeType, - JNIKiwixInt size); - - /** - * getContentPart. - * - * Get only a part of the content of the article. - * Return a byte array of `len` size starting from offset `offset`. - * Set `size` to the number of bytes read - * (`len` if everything is ok, 0 in case of error). - * If `len` == 0, no bytes are read but `size` is set to the total size of the - * article. - */ - public native byte[] getContentPart(String url, - int offest, - int len, - JNIKiwixInt size); - - /** - * - * Get the size of an article. - * - * @param url The url of the article. - * @return The size of the final (redirections are resolved) article (in byte). - * Return 0 if the article is not found. - */ - public native long getArticleSize(String url); - - /** - * getDirectAccessInformation. - * - * Return information giving where the content is located in the zim file. - * - * Some contents (binary content) are stored uncompressed in the zim file. - * Knowing this information, it could be interesting to directly open - * the zim file (or zim part) and directly read the content from it (and so - * bypassing the libzim). - * - * Return a `DirectAccessInfo` (filename, offset) where the content is located. - * - * If the content cannot be directly accessed (content is compressed or zim - * file is cut in the middle of the content), the filename is an empty string - * and offset is zero. - */ - public native DirectAccessInfo getDirectAccessInformation(String url); - - public native boolean searchSuggestions(String prefix, int count); - - public native boolean getNextSuggestion(JNIKiwixString title, JNIKiwixString url); - - public native boolean getPageUrlFromTitle(String title, JNIKiwixString url); - - public native String getDescription(); - - public native String getDate(); - - public native String getFavicon(); - - public native String getCreator(); - - public native String getPublisher(); - - public native String getName(); - - public native int getFileSize(); - - public native int getArticleCount(); - - public native int getMediaCount(); - - public native boolean getRandomPage(JNIKiwixString url); - - public JNIKiwixSearcher search(String query, int count) - { - JNIKiwixSearcher searcher = new JNIKiwixSearcher(); - searcher.addKiwixReader(this); - searcher.search(query, count); - return searcher; - } - - public JNIKiwixReader(String filename) throws JNIKiwixException - { - nativeHandle = getNativeReader(filename); - if (nativeHandle == 0) { - throw new JNIKiwixException("Cannot open zimfile "+filename); - } - } - - public JNIKiwixReader(FileDescriptor fd) throws JNIKiwixException - { - nativeHandle = getNativeReaderByFD(fd); - if (nativeHandle == 0) { - throw new JNIKiwixException("Cannot open zimfile by fd "+fd.toString()); - } - } - - public JNIKiwixReader(FileDescriptor fd, long offset, long size) - throws JNIKiwixException - { - nativeHandle = getNativeReaderEmbedded(fd, offset, size); - if (nativeHandle == 0) { - throw new JNIKiwixException(String.format("Cannot open embedded zimfile (fd=%s, offset=%d, size=%d)", fd, offset, size)); - } - } - - public JNIKiwixReader() { - - } - public native void dispose(); - - private native long getNativeReader(String filename); - private native long getNativeReaderByFD(FileDescriptor fd); - private native long getNativeReaderEmbedded(FileDescriptor fd, long offset, long size); - private long nativeHandle; -} diff --git a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixSearcher.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixSearcher.java deleted file mode 100644 index 30d7afb..0000000 --- a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixSearcher.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -package org.kiwix.kiwixlib; - -import org.kiwix.kiwixlib.JNIKiwixReader; -import java.util.Vector; - -public class JNIKiwixSearcher -{ - public class Result - { - private long nativeHandle; - private JNIKiwixSearcher searcher; - public Result(long handle, JNIKiwixSearcher _searcher) - { - nativeHandle = handle; - searcher = _searcher; - } - public native String getUrl(); - public native String getTitle(); - public native String getContent(); - public native String getSnippet(); - public native void dispose(); - } - - public JNIKiwixSearcher() - { - nativeHandle = getNativeHandle(); - usedReaders = new Vector(); - } - public native void dispose(); - - private native long getNativeHandle(); - private long nativeHandle; - private Vector usedReaders; - - public native void addReader(JNIKiwixReader reader); - public void addKiwixReader(JNIKiwixReader reader) - { - addReader(reader); - usedReaders.addElement(reader); - }; - - public native void search(String query, int count); - - public native Result getNextResult(); - public native boolean hasMoreResult(); -} diff --git a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixString.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixString.java deleted file mode 100644 index fd0e052..0000000 --- a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixString.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - -package org.kiwix.libkiwix; - -public class JNIKiwixString -{ - public String value; - - public JNIKiwixString(String value) { - this.value = value; - } - - public JNIKiwixString() { - this(""); - } - - public String getValue() { - return value; - } -} From 4e840ba5a9bb3ab201832e25c280b60b8e2668ae Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jan 2023 11:32:48 +0100 Subject: [PATCH 10/32] Add compilation of libkiwix wrapper. JNIKiwix.java is not compiled as it needs modules not found. To be fixed. --- lib/build.gradle | 2 +- lib/src/main/cpp/CMakeLists.txt | 40 +- lib/src/main/cpp/kiwixreader.cpp | 561 ------------------ lib/src/main/cpp/kiwixsearcher.cpp | 123 ---- lib/src/main/cpp/{ => libkiwix}/book.cpp | 30 +- lib/src/main/cpp/{ => libkiwix}/filter.cpp | 11 +- lib/src/main/cpp/{ => libkiwix}/kiwixicu.cpp | 9 +- .../main/cpp/{ => libkiwix}/kiwixserver.cpp | 52 +- lib/src/main/cpp/{ => libkiwix}/library.cpp | 53 +- lib/src/main/cpp/{ => libkiwix}/manager.cpp | 28 +- lib/src/main/cpp/meson.build | 55 -- .../{JNIKiwix.java => JNIKiwix.java_} | 0 12 files changed, 123 insertions(+), 841 deletions(-) delete mode 100644 lib/src/main/cpp/kiwixreader.cpp delete mode 100644 lib/src/main/cpp/kiwixsearcher.cpp rename lib/src/main/cpp/{ => libkiwix}/book.cpp (71%) rename lib/src/main/cpp/{ => libkiwix}/filter.cpp (88%) rename lib/src/main/cpp/{ => libkiwix}/kiwixicu.cpp (86%) rename lib/src/main/cpp/{ => libkiwix}/kiwixserver.cpp (67%) rename lib/src/main/cpp/{ => libkiwix}/library.cpp (62%) rename lib/src/main/cpp/{ => libkiwix}/manager.cpp (83%) delete mode 100644 lib/src/main/cpp/meson.build rename lib/src/main/java/org/kiwix/libkiwix/{JNIKiwix.java => JNIKiwix.java_} (100%) diff --git a/lib/build.gradle b/lib/build.gradle index ef7d759..667e59c 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -285,5 +285,5 @@ task checkCurrentJavaVersion() { task generateHeaderFilesFromJavaWrapper(type: Exec) { workingDir "${projectDir}/src/main/java/org/kiwix/" - commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ libzim/*.java" + commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ libzim/*.java libkiwix/*.java" } diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt index db24255..27858e5 100644 --- a/lib/src/main/cpp/CMakeLists.txt +++ b/lib/src/main/cpp/CMakeLists.txt @@ -4,10 +4,10 @@ cmake_minimum_required(VERSION 3.18.1) set(CMAKE_ANDROID_STL_TYPE llvm-libc++_static) -project("libzim_wrapper") +project("libkiwix_wrapper") add_library( - libzim_wrapper + zim_wrapper SHARED common.cpp @@ -41,6 +41,34 @@ set_property(TARGET IMPORTED_LOCATION ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libzim/libzim.so) + +add_library( + kiwix_wrapper + + SHARED + libkiwix/book.cpp + libkiwix/filter.cpp + libkiwix/kiwixicu.cpp + libkiwix/kiwixserver.cpp + libkiwix/library.cpp + libkiwix/manager.cpp +) + +find_library(libkiwix + kiwix + PATHS + ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libkiwix) +if (NOT libkiwix) + message(FATAL_ERROR "libkiwix not found!") +endif() +add_library(libkiwix SHARED IMPORTED) + +set_property(TARGET + libkiwix + PROPERTY + IMPORTED_LOCATION + ${BUILD_DIR}/jniLibs/${CMAKE_ANDROID_ARCH_ABI}/libkiwix/libkiwix.so) + include_directories( ${CMAKE_SOURCE_DIR} ${BUILD_DIR}/include/libkiwix @@ -54,9 +82,15 @@ find_library( log) target_link_libraries( - libzim_wrapper + zim_wrapper libzim + ${log-lib} +) +target_link_libraries( + kiwix_wrapper + libkiwix + libzim ${log-lib} ) diff --git a/lib/src/main/cpp/kiwixreader.cpp b/lib/src/main/cpp/kiwixreader.cpp deleted file mode 100644 index cc3c277..0000000 --- a/lib/src/main/cpp/kiwixreader.cpp +++ /dev/null @@ -1,561 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -#include -#include -#include "org_kiwix_kiwixlib_JNIKiwixReader.h" - -#include "tools/base64.h" -#include "reader.h" -#include "utils.h" - -/* Kiwix Reader JNI functions */ -JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReader( - JNIEnv* env, jobject obj, jstring filename) -{ - std::string cPath = jni2c(filename, env); - - LOG("Attempting to create reader with: %s", cPath.c_str()); - Lock l; - try { - kiwix::Reader* reader = new kiwix::Reader(cPath); - return reinterpret_cast(new Handle(reader)); - } catch (std::exception& e) { - LOG("Error opening ZIM file"); - LOG(e.what()); - return 0; - } -} - -namespace -{ - -int jni2fd(const jobject& fdObj, JNIEnv* env) -{ - jclass class_fdesc = env->FindClass("java/io/FileDescriptor"); - jfieldID field_fd = env->GetFieldID(class_fdesc, "fd", "I"); - if ( field_fd == NULL ) - { - env->ExceptionClear(); - // Under Android the (private) 'fd' field of java.io.FileDescriptor has been - // renamed to 'descriptor'. See, for example, - // https://android.googlesource.com/platform/libcore/+/refs/tags/android-8.1.0_r1/ojluni/src/main/java/java/io/FileDescriptor.java#55 - field_fd = env->GetFieldID(class_fdesc, "descriptor", "I"); - } - return env->GetIntField(fdObj, field_fd); -} - -} // unnamed namespace - -JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReaderByFD( - JNIEnv* env, jobject obj, jobject fdObj) -{ -#ifndef _WIN32 - int fd = jni2fd(fdObj, env); - - LOG("Attempting to create reader with fd: %d", fd); - Lock l; - try { - kiwix::Reader* reader = new kiwix::Reader(fd); - return reinterpret_cast(new Handle(reader)); - } catch (std::exception& e) { - LOG("Error opening ZIM file"); - LOG(e.what()); - return 0; - } -#else - jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); - env->ThrowNew(exception, "org.kiwix.kiwixlib.JNIKiwixReader.getNativeReaderByFD() is not supported under Windows"); - return 0; -#endif -} - -JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getNativeReaderEmbedded( - JNIEnv* env, jobject obj, jobject fdObj, jlong offset, jlong size) -{ -#ifndef _WIN32 - int fd = jni2fd(fdObj, env); - - LOG("Attempting to create reader with fd: %d", fd); - Lock l; - try { - kiwix::Reader* reader = new kiwix::Reader(fd, offset, size); - return reinterpret_cast(new Handle(reader)); - } catch (std::exception& e) { - LOG("Error opening ZIM file"); - LOG(e.what()); - return 0; - } -#else - jclass exception = env->FindClass("java/lang/UnsupportedOperationException"); - env->ThrowNew(exception, "org.kiwix.kiwixlib.JNIKiwixReader.getNativeReaderEmbedded() is not supported under Windows"); - return 0; -#endif -} - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_dispose(JNIEnv* env, jobject obj) -{ - Handle::dispose(env, obj); -} - -#define READER (Handle::getHandle(env, obj)) - -/* Kiwix library functions */ -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getMainPage(JNIEnv* env, jobject obj) -{ - jstring url; - - try { - std::string cUrl = READER->getMainPage().getPath(); - url = c2jni(cUrl, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM main page"); - LOG(e.what()); - url = NULL; - } - return url; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getId(JNIEnv* env, jobject obj) -{ - jstring id; - - try { - std::string cId = READER->getId(); - id = c2jni(cId, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM id"); - LOG(e.what()); - id = NULL; - } - - return id; -} - -JNIEXPORT jint JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getFileSize(JNIEnv* env, jobject obj) -{ - jint size = 0; - - try { - int cSize = READER->getFileSize(); - size = c2jni(cSize, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM file size"); - LOG(e.what()); - } - - return size; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getCreator(JNIEnv* env, jobject obj) -{ - jstring creator; - - try { - std::string cCreator = READER->getCreator(); - creator = c2jni(cCreator, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM creator"); - LOG(e.what()); - creator = NULL; - } - - return creator; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getPublisher(JNIEnv* env, jobject obj) -{ - jstring publisher; - - try { - std::string cPublisher = READER->getPublisher(); - publisher = c2jni(cPublisher, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM publish"); - LOG(e.what()); - publisher = NULL; - } - return publisher; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getName(JNIEnv* env, jobject obj) -{ - jstring name; - - try { - std::string cName = READER->getName(); - name = c2jni(cName, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM name"); - LOG(e.what()); - name = NULL; - } - return name; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getFavicon(JNIEnv* env, jobject obj) -{ - jstring favicon; - - try { - std::string cContent; - std::string cMime; - READER->getFavicon(cContent, cMime); - favicon = c2jni( - base64_encode(cContent), - env); - } catch (std::exception& e) { - LOG("Unable to get ZIM favicon"); - LOG(e.what()); - favicon = NULL; - } - return favicon; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getDate(JNIEnv* env, jobject obj) -{ - jstring date; - - try { - std::string cDate = READER->getDate(); - date = c2jni(cDate, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM date"); - LOG(e.what()); - date = NULL; - } - return date; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getLanguage(JNIEnv* env, jobject obj) -{ - jstring language; - - try { - std::string cLanguage = READER->getLanguage(); - language = c2jni(cLanguage, env); - } catch (std::exception& e) { - LOG("Unable to get ZIM language"); - LOG(e.what()); - language = NULL; - } - - return language; -} - -JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getMimeType( - JNIEnv* env, jobject obj, jstring url) -{ - jstring mimeType; - - std::string cUrl = jni2c(url, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - auto cMimeType = entry.getMimetype(); - mimeType = c2jni(cMimeType, env); - } catch (std::exception& e) { - LOG("Unable to get mime-type for url: %s", cUrl.c_str()); - LOG(e.what()); - mimeType = NULL; - } - return mimeType; -} - -JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_checkUrl( - JNIEnv* env, jobject obj, jstring url) -{ - jstring finalUrl; - std::string cUrl = jni2c(url, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - entry = entry.getFinalEntry(); - finalUrl = c2jni(entry.getPath(), env); - } catch (std::exception& e) { - finalUrl = c2jni(std::string(), env); - } - return finalUrl; -} - -JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContent( - JNIEnv* env, jobject obj, jobject url, jobject titleObj, jobject mimeTypeObj, jobject sizeObj) -{ - /* Default values */ - setStringObjValue("", titleObj, env); - setStringObjValue("", mimeTypeObj, env); - setIntObjValue(0, sizeObj, env); - jbyteArray data = env->NewByteArray(0); - - /* Retrieve the content */ - std::string cUrl = getStringObjValue(url, env); - unsigned int cSize = 0; - - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - bool isRedirect = entry.isRedirect(); - entry = entry.getFinalEntry(); - cSize = entry.getSize(); - setIntObjValue(cSize, sizeObj, env); - setStringObjValue(entry.getMimetype(), mimeTypeObj, env); - setStringObjValue(entry.getTitle(), titleObj, env); - if (isRedirect) { - setStringObjValue(entry.getPath(), url, env); - } else { - data = env->NewByteArray(cSize); - env->SetByteArrayRegion( - data, 0, cSize, reinterpret_cast(entry.getBlob().data())); - } - } catch (std::exception& e) { - LOG("Unable to get content for url: %s", cUrl.c_str()); - LOG(e.what()); - } - - return data; -} - -JNIEXPORT jbyteArray JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getContentPart( - JNIEnv* env, jobject obj, jstring url, jint offset, jint len, jobject sizeObj) -{ - jbyteArray data = env->NewByteArray(0); - setIntObjValue(0, sizeObj, env); - - /* Default values */ - /* Retrieve the content */ - std::string cUrl = jni2c(url, env); - unsigned int cOffset = jni2c(offset, env); - unsigned int cLen = jni2c(len, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - entry = entry.getFinalEntry(); - - if (cLen == 0) { - setIntObjValue(entry.getSize(), sizeObj, env); - } else if (cOffset+cLen < entry.getSize()) { - auto blob = entry.getBlob(cOffset, cLen); - data = env->NewByteArray(cLen); - env->SetByteArrayRegion( - data, 0, cLen, reinterpret_cast(blob.data())); - setIntObjValue(cLen, sizeObj, env); - } - } catch (std::exception& e) { - LOG("Unable to get partial content for url: %s (%u : %u)", cUrl.c_str(), cOffset, cLen); - LOG(e.what()); - } - return data; -} - -JNIEXPORT jlong JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getArticleSize( - JNIEnv* env, jobject obj, jstring url) -{ - std::string cUrl = jni2c(url, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - entry = entry.getFinalEntry(); - return c2jni(entry.getSize(), env); - } catch(std::exception& e) { - LOG("Unable to get size for url : %s", cUrl.c_str()); - LOG(e.what()); - } - return c2jni(0, env); -} - -JNIEXPORT jobject JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getDirectAccessInformation( - JNIEnv* env, jobject obj, jstring url) -{ - jclass daiClass = env->FindClass("org/kiwix/kiwixlib/DirectAccessInfo"); - jmethodID daiInitMethod = env->GetMethodID(daiClass, "", "()V"); - jobject dai = env->NewObject(daiClass, daiInitMethod); - setDaiObjValue("", 0, dai, env); - - std::string cUrl = jni2c(url, env); - try { - auto entry = READER->getEntryFromEncodedPath(cUrl); - entry = entry.getFinalEntry(); - auto part_info = entry.getDirectAccessInfo(); - setDaiObjValue(part_info.first, part_info.second, dai, env); - } catch (std::exception& e) { - LOG("Unable to get direct access info for url: %s", cUrl.c_str()); - LOG(e.what()); - } - return dai; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_searchSuggestions(JNIEnv* env, - jobject obj, - jstring prefix, - jint count) -{ - jboolean retVal = JNI_FALSE; - std::string cPrefix = jni2c(prefix, env); - unsigned int cCount = jni2c(count, env); - - try { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" - if (READER->searchSuggestionsSmart(cPrefix, cCount)) { - retVal = JNI_TRUE; - } -#pragma GCC diagnostic pop - } catch (std::exception& e) { - LOG("Unable to get search results for pattern: %s", cPrefix.c_str()); - LOG(e.what()); - } - - return retVal; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getNextSuggestion(JNIEnv* env, - jobject obj, - jobject titleObj, - jobject urlObj) -{ - jboolean retVal = JNI_FALSE; - std::string cTitle; - std::string cUrl; - - try { -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" - if (READER->getNextSuggestion(cTitle, cUrl)) { - setStringObjValue(cTitle, titleObj, env); - setStringObjValue(cUrl, urlObj, env); - retVal = JNI_TRUE; - } -#pragma GCC diagnostic pop - } catch (std::exception& e) { - LOG("Unable to get next suggestion"); - LOG(e.what()); - } - - return retVal; -} - -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getPageUrlFromTitle(JNIEnv* env, - jobject obj, - jstring title, - jobject urlObj) -{ - std::string cTitle = jni2c(title, env); - - try { - auto entry = READER->getEntryFromTitle(cTitle); - entry = entry.getFinalEntry(); - setStringObjValue(entry.getPath(), urlObj, env); - return JNI_TRUE; - } catch (std::exception& e) { - LOG("Unable to get url for title %s: ", cTitle.c_str()); - LOG(e.what()); - } - - return JNI_FALSE; -} - -JNIEXPORT jstring JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getTitle( - JNIEnv* env, jobject obj) -{ - jstring title; - - try { - std::string cTitle = READER->getTitle(); - title = c2jni(cTitle, env); - } catch (std::exception& e) { - LOG("Unable to get zim title"); - LOG(e.what()); - title = NULL; - } - return title; -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getDescription(JNIEnv* env, jobject obj) -{ - jstring description; - - try { - std::string cDescription = READER->getDescription(); - description = c2jni(cDescription, env); - } catch (std::exception& e) { - LOG("Unable to get zim description"); - LOG(e.what()); - description = NULL; - } - return description; -} - -JNIEXPORT jint JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getArticleCount(JNIEnv* env, jobject obj) -{ - jint articleCount = 0; - try { - auto cArticleCount = READER->getArticleCount(); - articleCount = c2jni(cArticleCount, env); - } catch (std::exception& e) { - LOG("Unable to get article count."); - LOG(e.what()); - } - return articleCount; -} - -JNIEXPORT jint JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixReader_getMediaCount(JNIEnv* env, jobject obj) -{ - jint mediaCount = 0; - try { - auto cMediaCount = READER->getMediaCount(); - mediaCount = c2jni(cMediaCount, env); - } catch (std::exception& e) { - LOG("Unable to get media count."); - LOG(e.what()); - } - return mediaCount; -} - - -JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixlib_JNIKiwixReader_getRandomPage( - JNIEnv* env, jobject obj, jobject urlObj) -{ - jboolean retVal = JNI_FALSE; - std::string cUrl; - - try { - std::string cUrl = READER->getRandomPage().getPath(); - setStringObjValue(cUrl, urlObj, env); - retVal = JNI_TRUE; - } catch (std::exception& e) { - LOG("Unable to get random page"); - LOG(e.what()); - } - return retVal; -} diff --git a/lib/src/main/cpp/kiwixsearcher.cpp b/lib/src/main/cpp/kiwixsearcher.cpp deleted file mode 100644 index 68241b3..0000000 --- a/lib/src/main/cpp/kiwixsearcher.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (C) 2013 Emmanuel Engelhart - * Copyright (C) 2017 Matthieu Gautier - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - * MA 02110-1301, USA. - */ - - -#include "org_kiwix_kiwixlib_JNIKiwixSearcher.h" -#include "org_kiwix_kiwixlib_JNIKiwixSearcher_Result.h" - -#include "reader.h" -#include "searcher.h" -#include "utils.h" - -#define SEARCHER (Handle::getHandle(env, obj)) -#define RESULT (Handle::getHandle(env, obj)) - - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_dispose(JNIEnv* env, jobject obj) -{ - Handle::dispose(env, obj); -} - -/* Kiwix Reader JNI functions */ -JNIEXPORT jlong JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_getNativeHandle(JNIEnv* env, - jobject obj) -{ - kiwix::Searcher* searcher = new kiwix::Searcher(); - return reinterpret_cast(new Handle(searcher)); -} - -/* Kiwix library functions */ -JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixSearcher_addReader( - JNIEnv* env, jobject obj, jobject reader) -{ - auto searcher = SEARCHER; - - searcher->add_reader(*(Handle::getHandle(env, reader))); -} - -JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixSearcher_search( - JNIEnv* env, jobject obj, jstring query, jint count) -{ - std::string cquery = jni2c(query, env); - unsigned int ccount = jni2c(count, env); - - SEARCHER->search(cquery, 0, ccount); -} - -JNIEXPORT jobject JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_getNextResult(JNIEnv* env, - jobject obj) -{ - jobject result = nullptr; - - kiwix::Result* cresult = SEARCHER->getNextResult(); - if (cresult != nullptr) { - jclass resultclass - = env->FindClass("org/kiwix/kiwixlib/JNIKiwixSearcher$Result"); - jmethodID ctor = env->GetMethodID( - resultclass, "", "(Lorg/kiwix/kiwixlib/JNIKiwixSearcher;JLorg/kiwix/kiwixlib/JNIKiwixSearcher;)V"); - result = env->NewObject(resultclass, ctor, obj, reinterpret_cast(new Handle(cresult)), obj); - } - return result; -} - -JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_dispose( - JNIEnv* env, jobject obj) -{ - Handle::dispose(env, obj); -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_getUrl(JNIEnv* env, - jobject obj) -{ - try { - return c2jni(RESULT->get_url(), env); - } catch (...) { - return nullptr; - } -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_getTitle(JNIEnv* env, - jobject obj) -{ - try { - return c2jni(RESULT->get_title(), env); - } catch (...) { - return nullptr; - } -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_getSnippet(JNIEnv* env, - jobject obj) -{ - return c2jni(RESULT->get_snippet(), env); -} - -JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixSearcher_00024Result_getContent(JNIEnv* env, - jobject obj) -{ - return c2jni(RESULT->get_content(), env); -} diff --git a/lib/src/main/cpp/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp similarity index 71% rename from lib/src/main/cpp/book.cpp rename to lib/src/main/cpp/libkiwix/book.cpp index e766abf..f148877 100644 --- a/lib/src/main/cpp/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -25,37 +25,36 @@ #include "book.h" #include +#define NATIVE_TYPE kiwix::Book +#define THIS GET_PTR(NATIVE_TYPE) + JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Book_allocate( +Java_org_kiwix_libkiwix_Book_allocate( JNIEnv* env, jobject thisObj) { - allocate(env, thisObj); + SET_PTR(std::make_shared()); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Book_dispose(JNIEnv* env, jobject thisObj) +Java_org_kiwix_libkiwix_Book_dispose(JNIEnv* env, jobject thisObj) { - dispose(env, thisObj); + dispose(env, thisObj); } -#define BOOK (getPtr(env, thisObj)) - -METHOD(void, Book, update__Lorg_kiwix_kiwixlib_Book_2, jobject otherBook) +METHOD(void, Book, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook) { - BOOK->update(*getPtr(env, otherBook)); + THIS->update(*getPtr(env, otherBook)); } -METHOD(void, Book, update__Lorg_kiwix_kiwixlib_JNIKiwixReader_2, jobject reader) +METHOD(void, Book, update__Lorg_kiwix_libkiwix_JNIKiwixReader_2, jobject archive) { - BOOK->update(**Handle::getHandle(env, reader)); + THIS->update(*getPtr(env, archive)); } #define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_kiwixlib_Book_##name (JNIEnv* env, jobject thisObj) \ +Java_org_kiwix_libkiwix_Book_##name (JNIEnv* env, jobject thisObj) \ { \ - auto cRet = BOOK->name(); \ - retType ret = c2jni(cRet, env); \ - return ret; \ + return TO_JNI(THIS->name()); \ } GETTER(jstring, getId) @@ -80,8 +79,7 @@ GETTER(jstring, getFaviconUrl) GETTER(jstring, getFaviconMimeType) METHOD(jstring, Book, getTagStr, jstring tagName) try { - auto cRet = BOOK->getTagStr(jni2c(tagName, env)); - return c2jni(cRet, env); + return TO_JNI(THIS->getTagStr(TO_C(tagName))); } catch(...) { return c2jni("", env); } diff --git a/lib/src/main/cpp/filter.cpp b/lib/src/main/cpp/libkiwix/filter.cpp similarity index 88% rename from lib/src/main/cpp/filter.cpp rename to lib/src/main/cpp/libkiwix/filter.cpp index b3b5113..2e29896 100644 --- a/lib/src/main/cpp/filter.cpp +++ b/lib/src/main/cpp/libkiwix/filter.cpp @@ -24,26 +24,29 @@ #include "library.h" #include "utils.h" +#define NATIVE_TYPE kiwix::Filter +#define THIS GET_PTR(NATIVE_TYPE) + + /* Kiwix Reader JNI functions */ METHOD0(void, Filter, allocate) { - allocate(env, thisObj); + SET_PTR(std::make_shared()); } METHOD0(void, Filter, dispose) { dispose(env, thisObj); } -#define FILTER (getPtr(env, thisObj)) #define FORWARD(name, args_type) \ METHOD(jobject, Filter, name, args_type value) { \ - FILTER->name(jni2c(value, env)); \ + THIS->name(jni2c(value, env)); \ return thisObj; \ } #define FORWARDA(name, args_type) \ METHOD(jobject, Filter, name, jobjectArray value) { \ - FILTER->name(jni2c(value, env)); \ + THIS->name(jni2c(value, env)); \ return thisObj; \ } diff --git a/lib/src/main/cpp/kiwixicu.cpp b/lib/src/main/cpp/libkiwix/kiwixicu.cpp similarity index 86% rename from lib/src/main/cpp/kiwixicu.cpp rename to lib/src/main/cpp/libkiwix/kiwixicu.cpp index e5ecb46..3cc2a72 100644 --- a/lib/src/main/cpp/kiwixicu.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixicu.cpp @@ -24,21 +24,18 @@ #include #include -#include "unicode/putil.h" - #include "utils.h" +#include "zim/tools.h" std::mutex globalLock; JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory( JNIEnv* env, jclass kclass, jstring dirStr) { - std::string cPath = jni2c(dirStr, env); - Lock l; try { - u_setDataDirectory(cPath.c_str()); + zim::setICUDataDirectory(TO_C(dirStr)); } catch (...) { - std::cerr << "Unable to set data directory " << cPath << std::endl; + std::cerr << "Unable to set data directory " << TO_C(dirStr) << std::endl; } } diff --git a/lib/src/main/cpp/kiwixserver.cpp b/lib/src/main/cpp/libkiwix/kiwixserver.cpp similarity index 67% rename from lib/src/main/cpp/kiwixserver.cpp rename to lib/src/main/cpp/libkiwix/kiwixserver.cpp index f9437c5..750d09a 100644 --- a/lib/src/main/cpp/kiwixserver.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixserver.cpp @@ -22,82 +22,78 @@ #include #include "org_kiwix_libkiwix_JNIKiwixServer.h" -#include "tools/base64.h" #include "server.h" #include "utils.h" +#define NATIVE_TYPE kiwix::Server +#define THIS GET_PTR(NATIVE_TYPE) + /* Kiwix Reader JNI functions */ -JNIEXPORT jlong JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_getNativeServer( - JNIEnv* env, jobject obj, jobject jLibrary) +JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_setNativeServer( + JNIEnv* env, jobject thisObj, jobject jLibrary) { LOG("Attempting to create server"); Lock l; try { auto library = getPtr(env, jLibrary); - kiwix::Server* server = new kiwix::Server(library); - return reinterpret_cast(new Handle(server)); + SET_PTR(std::make_shared(library.get())); } catch (std::exception& e) { LOG("Error creating the server"); - LOG(e.what()); - return 0; + LOG("%s", e.what()); } } JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_dispose(JNIEnv* env, jobject obj) { - Handle::dispose(env, obj); + dispose(env, obj); } -#define SERVER (Handle::getHandle(env, obj)) - /* Kiwix library functions */ JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setRoot(JNIEnv* env, jobject obj, jstring jRoot) +Java_org_kiwix_kiwixlib_JNIKiwixServer_setRoot(JNIEnv* env, jobject thisObj, jstring root) { - std::string root = jni2c(jRoot, env); - SERVER->setRoot(root); + THIS->setRoot(TO_C(root)); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setAddress(JNIEnv* env, jobject obj, jstring jAddress) +Java_org_kiwix_kiwixlib_JNIKiwixServer_setAddress(JNIEnv* env, jobject thisObj, jstring address) { - std::string address = jni2c(jAddress, env); - SERVER->setAddress(address); + THIS->setAddress(TO_C(address)); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setPort(JNIEnv* env, jobject obj, int port) +Java_org_kiwix_kiwixlib_JNIKiwixServer_setPort(JNIEnv* env, jobject thisObj, int port) { - SERVER->setPort(port); + THIS->setPort(TO_C(port)); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setNbThreads(JNIEnv* env, jobject obj, int threads) +Java_org_kiwix_kiwixlib_JNIKiwixServer_setNbThreads(JNIEnv* env, jobject thisObj, int threads) { - SERVER->setNbThreads(threads); + THIS->setNbThreads(TO_C(threads)); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject obj, jboolean withTaskbar, jboolean withLibraryButton) +Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject thisObj, jboolean withTaskbar, jboolean withLibraryButton) { - SERVER->setTaskbar(withTaskbar, withLibraryButton); + THIS->setTaskbar(TO_C(withTaskbar), TO_C(withLibraryButton)); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setBlockExternalLinks(JNIEnv* env, jobject obj, jboolean blockExternalLinks) +Java_org_kiwix_kiwixlib_JNIKiwixServer_setBlockExternalLinks(JNIEnv* env, jobject thisObj, jboolean blockExternalLinks) { - SERVER->setBlockExternalLinks(blockExternalLinks); + THIS->setBlockExternalLinks(TO_C(blockExternalLinks)); } JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_start(JNIEnv* env, jobject obj) +Java_org_kiwix_kiwixlib_JNIKiwixServer_start(JNIEnv* env, jobject thisObj) { - return SERVER->start(); + return THIS->start(); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_stop(JNIEnv* env, jobject obj) +Java_org_kiwix_kiwixlib_JNIKiwixServer_stop(JNIEnv* env, jobject thisObj) { - SERVER->stop(); + THIS->stop(); } diff --git a/lib/src/main/cpp/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp similarity index 62% rename from lib/src/main/cpp/library.cpp rename to lib/src/main/cpp/libkiwix/library.cpp index 9039db2..047367c 100644 --- a/lib/src/main/cpp/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -22,31 +22,31 @@ #include "org_kiwix_libkiwix_Library.h" #include "library.h" -#include "reader.h" #include "utils.h" +#define NATIVE_TYPE kiwix::Library +#define THIS GET_PTR(NATIVE_TYPE) + /* Kiwix Reader JNI functions */ JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_Library_allocate( JNIEnv* env, jobject thisObj) { - allocate(env, thisObj); + SET_PTR(std::make_shared()); } JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_Library_dispose(JNIEnv* env, jobject thisObj) { - dispose(env, thisObj); + dispose(env, thisObj); } -#define LIBRARY (getPtr(env, thisObj)) - /* Kiwix library functions */ -JNIEXPORT jboolean JNICALL +/*JNIEXPORT jboolean JNICALL Java_org_kiwix_kiwixlib_Library_addBook( JNIEnv* env, jobject thisObj, jstring path) { - auto cPath = jni2c(path, env); + auto cPath = TO_C(path); try { kiwix::Reader reader(cPath); @@ -57,40 +57,31 @@ Java_org_kiwix_kiwixlib_Library_addBook( LOG("Unable to add the book"); LOG(e.what()); } return false; -} +}*/ METHOD(jobject, Library, getBookById, jstring id) { - auto cId = jni2c(id, env); - auto cBook = new kiwix::Book(LIBRARY->getBookById(cId)); - jclass cls = env->FindClass("org/kiwix/kiwixlib/Book"); - jmethodID constructorId = env->GetMethodID(cls, "", "()V"); - jobject book = env->NewObject(cls, constructorId); - setPtr(env, book, cBook); - return book; + auto obj = NEW_OBJECT("org/kiwix/libkiwix/Book"); + SET_HANDLE(kiwix::Book, obj, THIS->getBookById(TO_C(id))); + return obj; } METHOD(jint, Library, getBookCount, jboolean localBooks, jboolean remoteBooks) { - return LIBRARY->getBookCount(localBooks, remoteBooks); + return THIS->getBookCount(localBooks, remoteBooks); } -METHOD0(jobjectArray, Library, getBooksIds) { - return c2jni(LIBRARY->getBooksIds(), env); +#define GETTER(retType, name) JNIEXPORT retType JNICALL \ +Java_org_kiwix_libkiwix_Library_##name (JNIEnv* env, jobject thisObj) \ +{ \ + return TO_JNI(THIS->name()); \ } +GETTER(jobjectArray, getBooksIds) +GETTER(jobjectArray, getBooksLanguages) +GETTER(jobjectArray, getBooksCreators) +GETTER(jobjectArray, getBooksPublishers) + METHOD(jobjectArray, Library, filter, jobject filterObj) { auto filter = getPtr(env, filterObj); - return c2jni(LIBRARY->filter(*filter), env); -} - -METHOD0(jobjectArray, Library, getBooksLanguages) { - return c2jni(LIBRARY->getBooksLanguages(), env); -} - -METHOD0(jobjectArray, Library, getBooksCreators) { - return c2jni(LIBRARY->getBooksCreators(), env); -} - -METHOD0(jobjectArray, Library, getBooksPublisher) { - return c2jni(LIBRARY->getBooksPublishers(), env); + return c2jni(THIS->filter(*filter), env); } diff --git a/lib/src/main/cpp/manager.cpp b/lib/src/main/cpp/libkiwix/manager.cpp similarity index 83% rename from lib/src/main/cpp/manager.cpp rename to lib/src/main/cpp/libkiwix/manager.cpp index 0ca6dbc..48bc340 100644 --- a/lib/src/main/cpp/manager.cpp +++ b/lib/src/main/cpp/libkiwix/manager.cpp @@ -24,17 +24,19 @@ #include "manager.h" #include "utils.h" +#define NATIVE_TYPE kiwix::Manager +#define THIS GET_PTR(NATIVE_TYPE) JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Manager_allocate( +Java_org_kiwix_libkiwix_Manager_allocate( JNIEnv* env, jobject thisObj, jobject libraryObj) { auto lib = getPtr(env, libraryObj); - allocate(env, thisObj, lib); + SET_PTR(std::make_shared(lib.get())); } JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Manager_dispose(JNIEnv* env, jobject thisObj) +Java_org_kiwix_libkiwix_Manager_dispose(JNIEnv* env, jobject thisObj) { dispose(env, thisObj); } @@ -43,7 +45,7 @@ Java_org_kiwix_kiwixlib_Manager_dispose(JNIEnv* env, jobject thisObj) /* Kiwix manager functions */ JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Manager_readFile( +Java_org_kiwix_libkiwix_Manager_readFile( JNIEnv* env, jobject thisObj, jstring path) { auto cPath = jni2c(path, env); @@ -52,13 +54,13 @@ Java_org_kiwix_kiwixlib_Manager_readFile( return MANAGER->readFile(cPath); } catch (std::exception& e) { LOG("Unable to get readFile"); - LOG(e.what()); + LOG("%s", e.what()); } return false; } JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Manager_readXml( +Java_org_kiwix_libkiwix_Manager_readXml( JNIEnv* env, jobject thisObj, jstring content, jstring libraryPath) { auto cContent = jni2c(content, env); @@ -68,14 +70,14 @@ Java_org_kiwix_kiwixlib_Manager_readXml( return MANAGER->readXml(cContent, false, cPath); } catch (std::exception& e) { LOG("Unable to get ZIM id"); - LOG(e.what()); + LOG("%s", e.what()); } return false; } JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Manager_readOpds( +Java_org_kiwix_libkiwix_Manager_readOpds( JNIEnv* env, jobject thisObj, jstring content, jstring urlHost) { auto cContent = jni2c(content, env); @@ -85,14 +87,14 @@ Java_org_kiwix_kiwixlib_Manager_readOpds( return MANAGER->readOpds(cContent, cUrl); } catch (std::exception& e) { LOG("Unable to get ZIM id"); - LOG(e.what()); + LOG("%s", e.what()); } return false; } JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Manager_readBookmarkFile( +Java_org_kiwix_libkiwix_Manager_readBookmarkFile( JNIEnv* env, jobject thisObj, jstring path) { auto cPath = jni2c(path, env); @@ -101,14 +103,14 @@ Java_org_kiwix_kiwixlib_Manager_readBookmarkFile( return MANAGER->readBookmarkFile(cPath); } catch (std::exception& e) { LOG("Unable to get ZIM id"); - LOG(e.what()); + LOG("%s", e.what()); } return false; } JNIEXPORT jstring JNICALL -Java_org_kiwix_kiwixlib_Manager_addBookFromPath( +Java_org_kiwix_libkiwix_Manager_addBookFromPath( JNIEnv* env, jobject thisObj, jstring pathToOpen, jstring pathToSave, jstring url, jboolean checkMetaData) { @@ -124,7 +126,7 @@ Java_org_kiwix_kiwixlib_Manager_addBookFromPath( } } catch (std::exception& e) { LOG("Unable to get ZIM file size"); - LOG(e.what()); + LOG("%s", e.what()); } return id; diff --git a/lib/src/main/cpp/meson.build b/lib/src/main/cpp/meson.build deleted file mode 100644 index de72e49..0000000 --- a/lib/src/main/cpp/meson.build +++ /dev/null @@ -1,55 +0,0 @@ - -java_sources = files([ - 'org/kiwix/kiwixlib/JNIICU.java', - 'org/kiwix/kiwixlib/Book.java', - 'org/kiwix/kiwixlib/JNIKiwixReader.java', - 'org/kiwix/kiwixlib/Library.java', - 'org/kiwix/kiwixlib/Manager.java', - 'org/kiwix/kiwixlib/Filter.java', - 'org/kiwix/kiwixlib/JNIKiwixSearcher.java', - 'org/kiwix/kiwixlib/JNIKiwixServer.java', - 'org/kiwix/kiwixlib/JNIKiwixInt.java', - 'org/kiwix/kiwixlib/JNIKiwixString.java', - 'org/kiwix/kiwixlib/JNIKiwixBool.java', - 'org/kiwix/kiwixlib/JNIKiwixException.java', - 'org/kiwix/kiwixlib/DirectAccessInfo.java' -]) - -kiwix_jni = custom_target('jni', - input: java_sources, - output: ['org_kiwix_kiwixlib_JNIKiwix.h', - 'org_kiwix_kiwixlib_Book.h', - 'org_kiwix_kiwixlib_JNIKiwixReader.h', - 'org_kiwix_kiwixlib_Library.h', - 'org_kiwix_kiwixlib_Manager.h', - 'org_kiwix_kiwixlib_Filter.h', - 'org_kiwix_kiwixlib_JNIKiwixServer.h', - 'org_kiwix_kiwixlib_JNIKiwixSearcher.h', - 'org_kiwix_kiwixlib_JNIKiwixSearcher_Result.h'], - command:['javac', '-d', '@OUTDIR@', '-h', '@OUTDIR@', '@INPUT@'] -) - -jni_sources = files([ - 'kiwixicu.cpp', - 'book.cpp', - 'kiwixreader.cpp', - 'library.cpp', - 'manager.cpp', - 'filter.cpp', - 'kiwixsearcher.cpp', - 'kiwixserver.cpp', -]) - -kiwix_sources += jni_sources + [kiwix_jni] - -if 'java' in wrapper - kiwix_jar = jar('kiwixlib', java_sources) - #junit_jar = files('org/kiwix/testing/junit-4.13.jar') - #test_jar = jar('testing', 'org/kiwix/testing/test.java', - # link_with: [kiwix_jar, junit_jar]) - #test('javatest', test_jar) -endif - -install_subdir('org', install_dir: 'kiwix-lib/java', exclude_directories: ['kiwix/testing']) -install_subdir('res', install_dir: 'kiwix-lib') -install_data('AndroidManifest.xml', install_dir: 'kiwix-lib') diff --git a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java_ similarity index 100% rename from lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java_ From ee5a6b3eeba407fcb45f27e97c4af2b3284568f9 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jan 2023 15:24:47 +0100 Subject: [PATCH 11/32] Make `METHOD` macro not using libzim by default. (Adapt only libzim part) --- lib/src/main/cpp/libzim/archive.cpp | 43 +++++++++---------- lib/src/main/cpp/libzim/blob.cpp | 5 +-- lib/src/main/cpp/libzim/entry.cpp | 9 ++-- lib/src/main/cpp/libzim/entry_iterator.cpp | 7 ++- lib/src/main/cpp/libzim/item.cpp | 5 +-- lib/src/main/cpp/libzim/query.cpp | 4 +- lib/src/main/cpp/libzim/search.cpp | 7 ++- lib/src/main/cpp/libzim/search_iterator.cpp | 12 +++--- lib/src/main/cpp/libzim/searcher.cpp | 12 +++--- lib/src/main/cpp/libzim/suggestion_item.cpp | 6 +-- .../main/cpp/libzim/suggestion_iterator.cpp | 10 ++--- lib/src/main/cpp/libzim/suggestion_search.cpp | 7 ++- .../main/cpp/libzim/suggestion_searcher.cpp | 10 ++--- lib/src/main/cpp/utils.h | 4 +- 14 files changed, 62 insertions(+), 79 deletions(-) diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index 4a93f53..8ca09d1 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -116,8 +116,7 @@ Java_org_kiwix_libzim_Archive_dispose(JNIEnv* env, jobject thisObj) } #define THIS GET_PTR(zim::Archive) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_Archive_##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_Archive, name) \ { \ return TO_JNI(THIS->name()); \ } @@ -130,15 +129,15 @@ GETTER(jint, getEntryCount) GETTER(jint, getArticleCount) GETTER(jint, getMediaCount) -METHOD0(jstring, Archive, getUuid) { +METHOD0(jstring, libzim_Archive, getUuid) { return TO_JNI(std::string(THIS->getUuid())); } -METHOD(jstring, Archive, getMetadata, jstring name) { +METHOD(jstring, libzim_Archive, getMetadata, jstring name) { return TO_JNI(THIS->getMetadata(TO_C(name))); } -METHOD(jobject, Archive, getMetadataItem, jstring name) { +METHOD(jobject, libzim_Archive, getMetadataItem, jstring name) { auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); SET_HANDLE(zim::Item, obj, THIS->getMetadataItem(TO_C(name))); return obj; @@ -146,71 +145,71 @@ METHOD(jobject, Archive, getMetadataItem, jstring name) { GETTER(jobjectArray, getMetadataKeys) -METHOD(jobject, Archive, getIllustrationItem, jint size) { +METHOD(jobject, libzim_Archive, getIllustrationItem, jint size) { auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); SET_HANDLE(zim::Item, obj, THIS->getIllustrationItem(TO_C(size))); return obj; } -METHOD(jboolean, Archive, hasIllustration, jint size) { +METHOD(jboolean, libzim_Archive, hasIllustration, jint size) { return TO_JNI(THIS->hasIllustration(TO_C(size))); } GETTER(jlongArray, getIllustrationSizes) -METHOD(jobject, Archive, getEntryByPath, jlong index) { +METHOD(jobject, libzim_Archive, getEntryByPath, jlong index) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(index))); return obj; } -METHOD(jobject, Archive, getEntryByPath, jstring path) { +METHOD(jobject, libzim_Archive, getEntryByPath, jstring path) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(path))); return obj; } -METHOD(jobject, Archive, getEntryByTitle, jlong index) { +METHOD(jobject, libzim_Archive, getEntryByTitle, jlong index) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(index))); return obj; } -METHOD(jobject, Archive, getEntryByTitle, jstring title) { +METHOD(jobject, libzim_Archive, getEntryByTitle, jstring title) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(title))); return obj; } -METHOD(jobject, Archive, getEntryByClusterOrder, jlong index) { +METHOD(jobject, libzim_Archive, getEntryByClusterOrder, jlong index) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByClusterOrder(TO_C(index))); return obj; } -METHOD0(jobject, Archive, getMainEntry) { +METHOD0(jobject, libzim_Archive, getMainEntry) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getMainEntry()); return obj; } -METHOD0(jobject, Archive, getRandomEntry) { +METHOD0(jobject, libzim_Archive, getRandomEntry) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getRandomEntry()); return obj; } -METHOD(jboolean, Archive, hasEntryByPath, jstring path) { +METHOD(jboolean, libzim_Archive, hasEntryByPath, jstring path) { return TO_JNI(THIS->hasEntryByPath(TO_C(path))); } -METHOD(jboolean, Archive, hasEntryByTitle, jstring title) { +METHOD(jboolean, libzim_Archive, hasEntryByTitle, jstring title) { return TO_JNI(THIS->hasEntryByPath(TO_C(title))); } GETTER(jboolean, hasMainEntry) -METHOD(jboolean, Archive, hasIllustration, jlong size) { +METHOD(jboolean, libzim_Archive, hasIllustration, jlong size) { return TO_JNI(THIS->hasIllustration(TO_C(size))); } @@ -226,7 +225,7 @@ GETTER(jboolean, hasNewNamespaceScheme) #define ITER_BY_PATH 0 #define ITER_BY_TITLE 1 #define ITER_EFFICIENT 2 -METHOD0(jobject, Archive, iterByPath) { +METHOD0(jobject, libzim_Archive, iterByPath) { auto range = THIS->iterByPath(); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); @@ -238,7 +237,7 @@ METHOD0(jobject, Archive, iterByPath) { return obj; } -METHOD0(jobject, Archive, iterByTitle) { +METHOD0(jobject, libzim_Archive, iterByTitle) { auto range = THIS->iterByTitle(); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); @@ -250,7 +249,7 @@ METHOD0(jobject, Archive, iterByTitle) { return obj; } -METHOD0(jobject, Archive, iterEfficient) { +METHOD0(jobject, libzim_Archive, iterEfficient) { auto range = THIS->iterEfficient(); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); @@ -262,7 +261,7 @@ METHOD0(jobject, Archive, iterEfficient) { return obj; } -METHOD(jobject, Archive, findByPath, jstring path) { +METHOD(jobject, libzim_Archive, findByPath, jstring path) { auto range = THIS->findByPath(TO_C(path)); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); @@ -274,7 +273,7 @@ METHOD(jobject, Archive, findByPath, jstring path) { return obj; } -METHOD(jobject, Archive, findByTitle, jstring title) { +METHOD(jobject, libzim_Archive, findByTitle, jstring title) { auto range = THIS->findByTitle(TO_C(title)); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); diff --git a/lib/src/main/cpp/libzim/blob.cpp b/lib/src/main/cpp/libzim/blob.cpp index 39bed81..70ce387 100644 --- a/lib/src/main/cpp/libzim/blob.cpp +++ b/lib/src/main/cpp/libzim/blob.cpp @@ -36,13 +36,12 @@ Java_org_kiwix_kiwixlib_libzim_Blob_dispose(JNIEnv* env, jobject thisObj) dispose(env, thisObj); } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_Blob__##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_Blob, name) \ { \ return TO_JNI(THIS->name()); \ } -METHOD0(jstring, Blob, getData) { +METHOD0(jstring, libzim_Blob, getData) { return TO_JNI(std::string(*THIS)); } GETTER(jlong, size) diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp index 6e5ce73..52da201 100644 --- a/lib/src/main/cpp/libzim/entry.cpp +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -38,8 +38,7 @@ Java_org_kiwix_kiwixlib_libzim_Entry_dispose(JNIEnv* env, jobject thisObj) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_Entry__##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_Entry, name) \ { \ return TO_JNI(THIS->name()); \ } @@ -48,19 +47,19 @@ Java_org_kiwix_libzim_Entry__##name (JNIEnv* env, jobject thisObj) \ GETTER(jboolean, isRedirect) GETTER(jstring, getTitle) GETTER(jstring, getPath) -METHOD(jobject, Entry, getItem, jboolean follow) { +METHOD(jobject, libzim_Entry, getItem, jboolean follow) { auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); SET_HANDLE(zim::Item, obj, THIS->getItem(TO_C(follow))); return obj; } -METHOD0(jobject, Entry, getRedirect) { +METHOD0(jobject, libzim_Entry, getRedirect) { auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); SET_HANDLE(zim::Item, obj, THIS->getRedirect()); return obj; } -METHOD0(jobject, Entry, getRedirectEntry) { +METHOD0(jobject, libzim_Entry, getRedirectEntry) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getRedirectEntry()); return obj; diff --git a/lib/src/main/cpp/libzim/entry_iterator.cpp b/lib/src/main/cpp/libzim/entry_iterator.cpp index 88a904e..1984a2b 100644 --- a/lib/src/main/cpp/libzim/entry_iterator.cpp +++ b/lib/src/main/cpp/libzim/entry_iterator.cpp @@ -39,8 +39,7 @@ inline int get_order(JNIEnv* env, jobject thisObj) { return TO_C(env->GetIntField(thisObj, fieldId)); } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_EntryIterotar_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, libzim_EntryIterotar, dispose) { // Delete end iterator switch (get_order(env, thisObj)) { @@ -60,7 +59,7 @@ Java_org_kiwix_kiwixlib_libzim_EntryIterotar_dispose(JNIEnv* env, jobject thisOb } -METHOD0(jboolean, EntryIterator, hasNext) { +METHOD0(jboolean, libzim_EntryIterator, hasNext) { switch (get_order(env, thisObj)) { case 0: { PATH_NATIVE_TYPE next(*GET_PTR(PATH_NATIVE_TYPE)); @@ -83,7 +82,7 @@ METHOD0(jboolean, EntryIterator, hasNext) { } } -METHOD0(jobject, EntryIterator, next) { +METHOD0(jobject, libzim_EntryIterator, next) { switch (get_order(env, thisObj)) { case 0: { (*GET_PTR(PATH_NATIVE_TYPE))++; diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp index 81d7aaf..90bc493 100644 --- a/lib/src/main/cpp/libzim/item.cpp +++ b/lib/src/main/cpp/libzim/item.cpp @@ -37,8 +37,7 @@ Java_org_kiwix_kiwixlib_libzim_Item_dispose(JNIEnv* env, jobject thisObj) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_Item__##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_Item, name) \ { \ return TO_JNI(THIS->name()); \ } @@ -47,7 +46,7 @@ GETTER(jstring, getTitle) GETTER(jstring, getPath) GETTER(jstring, getMimetype) -METHOD0(jobject, Item, getData) { +METHOD0(jobject, libzim_Item, getData) { auto obj = NEW_OBJECT("org/kiwix/libzim/Blob"); SET_HANDLE(zim::Blob, obj, THIS->getData()); return obj; diff --git a/lib/src/main/cpp/libzim/query.cpp b/lib/src/main/cpp/libzim/query.cpp index c39e0cd..575844a 100644 --- a/lib/src/main/cpp/libzim/query.cpp +++ b/lib/src/main/cpp/libzim/query.cpp @@ -53,12 +53,12 @@ Java_org_kiwix_kiwixlib_libzim_Query_dispose(JNIEnv* env, jobject thisObj) #define THIS GET_PTR(NATIVE_TYPE) -METHOD(jobject, Query, setQuery, jstring query) { +METHOD(jobject, libzim_Query, setQuery, jstring query) { THIS->setQuery(TO_C(query)); return thisObj; } -METHOD(jobject, Query, setGeorange, jfloat latitude, jfloat longitude, jfloat distance) { +METHOD(jobject, libzim_Query, setGeorange, jfloat latitude, jfloat longitude, jfloat distance) { THIS->setGeorange(TO_C(latitude), TO_C(longitude), TO_C(distance)); return thisObj; } diff --git a/lib/src/main/cpp/libzim/search.cpp b/lib/src/main/cpp/libzim/search.cpp index 057aa06..a791321 100644 --- a/lib/src/main/cpp/libzim/search.cpp +++ b/lib/src/main/cpp/libzim/search.cpp @@ -30,15 +30,14 @@ #define NATIVE_TYPE zim::Search -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_Search_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, libzim_Search, dispose) { dispose(env, thisObj); } #define THIS GET_PTR(NATIVE_TYPE) -METHOD(jobject, Search, getResults, jint start, jint maxResults) { +METHOD(jobject, libzim_Search, getResults, jint start, jint maxResults) { auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); auto obj = NEW_OBJECT("ork/kiwix/libzim/SearchIterator"); SET_HANDLE(zim::SearchIterator, obj, results.begin()); @@ -49,7 +48,7 @@ METHOD(jobject, Search, getResults, jint start, jint maxResults) { return obj; } -METHOD0(jlong, Search, getEstimatedMatches) { +METHOD0(jlong, libzim_Search, getEstimatedMatches) { return TO_JNI(THIS->getEstimatedMatches()); } diff --git a/lib/src/main/cpp/libzim/search_iterator.cpp b/lib/src/main/cpp/libzim/search_iterator.cpp index 9b1fa18..3375366 100644 --- a/lib/src/main/cpp/libzim/search_iterator.cpp +++ b/lib/src/main/cpp/libzim/search_iterator.cpp @@ -31,8 +31,7 @@ #define NATIVE_TYPE zim::SearchIterator -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_SearchIterotar_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, libzim_SearchIterator, dispose) { // Delete end iterator dispose(env, thisObj, "nativeHandleEnd"); @@ -40,8 +39,7 @@ Java_org_kiwix_kiwixlib_libzim_SearchIterotar_dispose(JNIEnv* env, jobject thisO } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_SearchIterator__##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_SearchIterator, name) \ { \ return TO_JNI(THIS->name()); \ } @@ -54,18 +52,18 @@ GETTER(jint, getWordCount) GETTER(jint, getFileIndex) GETTER(jint, getSize) -METHOD0(jstring, SearchIterator, getZimId) { +METHOD0(jstring, libzim_SearchIterator, getZimId) { return TO_JNI(std::string(THIS->getZimId())); } -METHOD0(jboolean, SearchIterator, hasNext) { +METHOD0(jboolean, libzim_SearchIterator, hasNext) { zim::SearchIterator next(*THIS); next++; auto end = getPtr(env, thisObj, "nativeHandleEnd"); return next == *end; } -METHOD0(jobject, SearchIterator, next) { +METHOD0(jobject, libzim_SearchIterator, next) { (*THIS)++; zim::Entry entry = **THIS; auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp index 2ce9cd4..071782b 100644 --- a/lib/src/main/cpp/libzim/searcher.cpp +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -46,33 +46,31 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Searcher_setNativeSearcher( } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_Searcher_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, libzim_Searcher, dispose) { dispose(env, thisObj); } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_Searcher__##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_Searcher, name) \ { \ return TO_JNI(THIS->name()); \ } -METHOD(jobject, Searcher, addArchive, jobject archive) { +METHOD(jobject, libzim_Searcher, addArchive, jobject archive) { auto cArchive = getPtr(env, archive); THIS->addArchive(*cArchive); return thisObj; } -METHOD(jobject, Searcher, search, jobject query) { +METHOD(jobject, libzim_Searcher, search, jobject query) { auto cQuery = getPtr(env, query); auto obj = NEW_OBJECT("org/kiwix/libzim/Search"); SET_HANDLE(zim::Search, obj, THIS->search(*cQuery)); return obj; } -METHOD(void, Searcher, setVerbose, jboolean verbose) { +METHOD(void, libzim_Searcher, setVerbose, jboolean verbose) { THIS->setVerbose(TO_C(verbose)); } diff --git a/lib/src/main/cpp/libzim/suggestion_item.cpp b/lib/src/main/cpp/libzim/suggestion_item.cpp index 254d8bc..d0f051c 100644 --- a/lib/src/main/cpp/libzim/suggestion_item.cpp +++ b/lib/src/main/cpp/libzim/suggestion_item.cpp @@ -30,15 +30,13 @@ #define NATIVE_TYPE zim::SuggestionItem -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_SuggestionItem_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, libzim_SuggestionItem, dispose) { dispose(env, thisObj); } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_SuggestionItem__##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_SuggestionItem, name) \ { \ return TO_JNI(THIS->name()); \ } diff --git a/lib/src/main/cpp/libzim/suggestion_iterator.cpp b/lib/src/main/cpp/libzim/suggestion_iterator.cpp index 9c1bff0..b0e1be1 100644 --- a/lib/src/main/cpp/libzim/suggestion_iterator.cpp +++ b/lib/src/main/cpp/libzim/suggestion_iterator.cpp @@ -30,8 +30,7 @@ #define NATIVE_TYPE zim::SuggestionIterator -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_SuggestionIterator_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, libzim_SuggestionIterator, dispose) { // Delete end iterator dispose(env, thisObj, "nativeHandleEnd"); @@ -39,20 +38,19 @@ Java_org_kiwix_kiwixlib_libzim_SuggestionIterator_dispose(JNIEnv* env, jobject t } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_SuggestionIterator__##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_SuggestionIterator, name) \ { \ return TO_JNI(THIS->name()); \ } -METHOD0(jboolean, SearchIterator, hasNext) { +METHOD0(jboolean, libzim_SearchIterator, hasNext) { NATIVE_TYPE next(*THIS); next++; auto end = getPtr(env, thisObj, "nativeHandleEnd"); return next == *end; } -METHOD0(jobject, SearchIterator, next) { +METHOD0(jobject, libzim_SearchIterator, next) { (*THIS)++; zim::SuggestionItem item = **THIS; auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionItem"); diff --git a/lib/src/main/cpp/libzim/suggestion_search.cpp b/lib/src/main/cpp/libzim/suggestion_search.cpp index 0b545d7..78000d6 100644 --- a/lib/src/main/cpp/libzim/suggestion_search.cpp +++ b/lib/src/main/cpp/libzim/suggestion_search.cpp @@ -30,15 +30,14 @@ #define NATIVE_TYPE zim::SuggestionSearch -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_SuggestionSearch_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, libzim_SuggestionSearch, dispose) { dispose(env, thisObj); } #define THIS GET_PTR(NATIVE_TYPE) -METHOD(jobject, SuggestionSearch, getResults, jint start, jint maxResults) { +METHOD(jobject, libzim_SuggestionSearch, getResults, jint start, jint maxResults) { auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); auto obj = NEW_OBJECT("ork/kiwix/libzim/SuggestionIterator"); SET_HANDLE(zim::SuggestionIterator, obj, results.begin()); @@ -49,7 +48,7 @@ METHOD(jobject, SuggestionSearch, getResults, jint start, jint maxResults) { return obj; } -METHOD0(jlong, SuggestionSearch, getEstimatedMatches) { +METHOD0(jlong, libzim_SuggestionSearch, getEstimatedMatches) { return TO_JNI(THIS->getEstimatedMatches()); } diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp index 7c8ad2e..15cad3e 100644 --- a/lib/src/main/cpp/libzim/suggestion_searcher.cpp +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -46,26 +46,24 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_SuggestionSearcher_setNativeSearche } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_SuggestionSearcher_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, libzim_SuggestionSearcher, dispose) { dispose(env, thisObj); } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libzim_SuggestionSearcher__##name (JNIEnv* env, jobject thisObj) \ +#define GETTER(retType, name) METHOD0(retType, libzim_SuggestionSearcher, name) \ { \ return TO_JNI(THIS->name()); \ } -METHOD(jobject, SuggestionSearcher, suggest, jstring query) { +METHOD(jobject, libzim_SuggestionSearcher, suggest, jstring query) { auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionSearch"); SET_HANDLE(zim::SuggestionSearch, obj, THIS->suggest(TO_C(query))); return obj; } -METHOD(void, SuggestionSearcher, setVerbose, jboolean verbose) { +METHOD(void, libzim_SuggestionSearcher, setVerbose, jboolean verbose) { THIS->setVerbose(TO_C(verbose)); } diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index 7557637..301d6bc 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -109,11 +109,11 @@ void dispose(JNIEnv* env, jobject thisObj, const char* handleName = "nativeHandl } #define METHOD0(retType, class, name) \ -JNIEXPORT retType JNICALL Java_org_kiwix_libzim_##class##_##name( \ +JNIEXPORT retType JNICALL Java_org_kiwix_##class##_##name( \ JNIEnv* env, jobject thisObj) #define METHOD(retType, class, name, ...) \ -JNIEXPORT retType JNICALL Java_org_kiwix_libzim_##class##_##name( \ +JNIEXPORT retType JNICALL Java_org_kiwix_##class##_##name( \ JNIEnv* env, jobject thisObj, __VA_ARGS__) inline jfieldID getHandleField(JNIEnv* env, jobject obj, const char* handleName) From edea64863488800079844009c5320bcece992be5 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jan 2023 15:34:00 +0100 Subject: [PATCH 12/32] Introduce GETTER_METHOD in utils.h --- lib/src/main/cpp/libkiwix/book.cpp | 6 +----- lib/src/main/cpp/libkiwix/library.cpp | 6 +----- lib/src/main/cpp/libzim/archive.cpp | 5 +---- lib/src/main/cpp/libzim/blob.cpp | 5 +---- lib/src/main/cpp/libzim/entry.cpp | 5 +---- lib/src/main/cpp/libzim/item.cpp | 5 +---- lib/src/main/cpp/libzim/search_iterator.cpp | 5 +---- lib/src/main/cpp/libzim/searcher.cpp | 5 +---- lib/src/main/cpp/libzim/suggestion_item.cpp | 5 +---- lib/src/main/cpp/libzim/suggestion_iterator.cpp | 5 +---- lib/src/main/cpp/libzim/suggestion_searcher.cpp | 5 +---- lib/src/main/cpp/utils.h | 4 ++++ 12 files changed, 15 insertions(+), 46 deletions(-) diff --git a/lib/src/main/cpp/libkiwix/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp index f148877..a3c9022 100644 --- a/lib/src/main/cpp/libkiwix/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -51,11 +51,7 @@ METHOD(void, Book, update__Lorg_kiwix_libkiwix_JNIKiwixReader_2, jobject archive THIS->update(*getPtr(env, archive)); } -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libkiwix_Book_##name (JNIEnv* env, jobject thisObj) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libkiwix_Book, THIS, name) GETTER(jstring, getId) GETTER(jstring, getPath) diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index 047367c..9dace9c 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -69,11 +69,7 @@ METHOD(jint, Library, getBookCount, jboolean localBooks, jboolean remoteBooks) { return THIS->getBookCount(localBooks, remoteBooks); } -#define GETTER(retType, name) JNIEXPORT retType JNICALL \ -Java_org_kiwix_libkiwix_Library_##name (JNIEnv* env, jobject thisObj) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libkiwix_Library, THIS, name) GETTER(jobjectArray, getBooksIds) GETTER(jobjectArray, getBooksLanguages) diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index 8ca09d1..8585780 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -116,10 +116,7 @@ Java_org_kiwix_libzim_Archive_dispose(JNIEnv* env, jobject thisObj) } #define THIS GET_PTR(zim::Archive) -#define GETTER(retType, name) METHOD0(retType, libzim_Archive, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Archive, THIS, name) GETTER(jstring, getFilename) diff --git a/lib/src/main/cpp/libzim/blob.cpp b/lib/src/main/cpp/libzim/blob.cpp index 70ce387..3d0eff0 100644 --- a/lib/src/main/cpp/libzim/blob.cpp +++ b/lib/src/main/cpp/libzim/blob.cpp @@ -36,10 +36,7 @@ Java_org_kiwix_kiwixlib_libzim_Blob_dispose(JNIEnv* env, jobject thisObj) dispose(env, thisObj); } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) METHOD0(retType, libzim_Blob, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Blob, THIS, name) METHOD0(jstring, libzim_Blob, getData) { return TO_JNI(std::string(*THIS)); diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp index 52da201..88a1024 100644 --- a/lib/src/main/cpp/libzim/entry.cpp +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -38,10 +38,7 @@ Java_org_kiwix_kiwixlib_libzim_Entry_dispose(JNIEnv* env, jobject thisObj) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) METHOD0(retType, libzim_Entry, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Entry, THIS, name) GETTER(jboolean, isRedirect) diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp index 90bc493..9135ce1 100644 --- a/lib/src/main/cpp/libzim/item.cpp +++ b/lib/src/main/cpp/libzim/item.cpp @@ -37,10 +37,7 @@ Java_org_kiwix_kiwixlib_libzim_Item_dispose(JNIEnv* env, jobject thisObj) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) METHOD0(retType, libzim_Item, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Item, THIS, name) GETTER(jstring, getTitle) GETTER(jstring, getPath) diff --git a/lib/src/main/cpp/libzim/search_iterator.cpp b/lib/src/main/cpp/libzim/search_iterator.cpp index 3375366..e3889eb 100644 --- a/lib/src/main/cpp/libzim/search_iterator.cpp +++ b/lib/src/main/cpp/libzim/search_iterator.cpp @@ -39,10 +39,7 @@ METHOD0(void, libzim_SearchIterator, dispose) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) METHOD0(retType, libzim_SearchIterator, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_SearchIterator, THIS, name) GETTER(jstring, getPath) GETTER(jstring, getTitle) diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp index 071782b..0483cc4 100644 --- a/lib/src/main/cpp/libzim/searcher.cpp +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -52,10 +52,7 @@ METHOD0(void, libzim_Searcher, dispose) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) METHOD0(retType, libzim_Searcher, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Searcher, THIS, name) METHOD(jobject, libzim_Searcher, addArchive, jobject archive) { auto cArchive = getPtr(env, archive); diff --git a/lib/src/main/cpp/libzim/suggestion_item.cpp b/lib/src/main/cpp/libzim/suggestion_item.cpp index d0f051c..9776579 100644 --- a/lib/src/main/cpp/libzim/suggestion_item.cpp +++ b/lib/src/main/cpp/libzim/suggestion_item.cpp @@ -36,10 +36,7 @@ METHOD0(void, libzim_SuggestionItem, dispose) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) METHOD0(retType, libzim_SuggestionItem, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_SuggestionItem, THIS, name) GETTER(jstring, getTitle) diff --git a/lib/src/main/cpp/libzim/suggestion_iterator.cpp b/lib/src/main/cpp/libzim/suggestion_iterator.cpp index b0e1be1..9541ab8 100644 --- a/lib/src/main/cpp/libzim/suggestion_iterator.cpp +++ b/lib/src/main/cpp/libzim/suggestion_iterator.cpp @@ -38,10 +38,7 @@ METHOD0(void, libzim_SuggestionIterator, dispose) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) METHOD0(retType, libzim_SuggestionIterator, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_SuggestionIterator, THIS, name) METHOD0(jboolean, libzim_SearchIterator, hasNext) { NATIVE_TYPE next(*THIS); diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp index 15cad3e..15c9455 100644 --- a/lib/src/main/cpp/libzim/suggestion_searcher.cpp +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -52,10 +52,7 @@ METHOD0(void, libzim_SuggestionSearcher, dispose) } #define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) METHOD0(retType, libzim_SuggestionSearcher, name) \ -{ \ - return TO_JNI(THIS->name()); \ -} +#define GETTER(retType, name) GETTER_METHOD(retType, libzim_SuggestionSearcher, THIS, name) METHOD(jobject, libzim_SuggestionSearcher, suggest, jstring query) { auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionSearch"); diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index 301d6bc..ed15d57 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -116,6 +116,10 @@ JNIEXPORT retType JNICALL Java_org_kiwix_##class##_##name( \ JNIEXPORT retType JNICALL Java_org_kiwix_##class##_##name( \ JNIEnv* env, jobject thisObj, __VA_ARGS__) +#define GETTER_METHOD(retType, class, THIS, name) METHOD0(retType, class, name) { \ + return TO_JNI(THIS->name()); \ +} + inline jfieldID getHandleField(JNIEnv* env, jobject obj, const char* handleName) { jclass c = env->GetObjectClass(obj); From 29518279dce71f1915531c74f9b56706b7524a6a Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jan 2023 16:55:15 +0100 Subject: [PATCH 13/32] Introduce macros.h to define common macro --- lib/src/main/cpp/libkiwix/book.cpp | 20 ++---- lib/src/main/cpp/libkiwix/filter.cpp | 12 ++-- lib/src/main/cpp/libkiwix/kiwixserver.cpp | 37 +++++------ lib/src/main/cpp/libkiwix/library.cpp | 18 ++---- lib/src/main/cpp/libkiwix/manager.cpp | 64 +++++++------------ lib/src/main/cpp/libzim/archive.cpp | 52 +++++++-------- lib/src/main/cpp/libzim/blob.cpp | 12 ++-- lib/src/main/cpp/libzim/entry.cpp | 17 +++-- lib/src/main/cpp/libzim/entry_iterator.cpp | 10 ++- lib/src/main/cpp/libzim/item.cpp | 11 ++-- lib/src/main/cpp/libzim/query.cpp | 16 ++--- lib/src/main/cpp/libzim/search.cpp | 15 ++--- lib/src/main/cpp/libzim/search_iterator.cpp | 13 ++-- lib/src/main/cpp/libzim/searcher.cpp | 17 +++-- lib/src/main/cpp/libzim/suggestion_item.cpp | 9 ++- .../main/cpp/libzim/suggestion_iterator.cpp | 13 ++-- lib/src/main/cpp/libzim/suggestion_search.cpp | 14 ++-- .../main/cpp/libzim/suggestion_searcher.cpp | 15 ++--- lib/src/main/cpp/macros.h | 41 ++++++++++++ lib/src/main/cpp/utils.h | 12 ---- 20 files changed, 207 insertions(+), 211 deletions(-) create mode 100644 lib/src/main/cpp/macros.h diff --git a/lib/src/main/cpp/libkiwix/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp index a3c9022..dd9c21c 100644 --- a/lib/src/main/cpp/libkiwix/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -26,33 +26,29 @@ #include #define NATIVE_TYPE kiwix::Book -#define THIS GET_PTR(NATIVE_TYPE) +#define TYPENAME libkiwix_Book +#include -JNIEXPORT void JNICALL -Java_org_kiwix_libkiwix_Book_allocate( - JNIEnv* env, jobject thisObj) +METHOD0(void, allocate) { SET_PTR(std::make_shared()); } -JNIEXPORT void JNICALL -Java_org_kiwix_libkiwix_Book_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, dispose) { dispose(env, thisObj); } -METHOD(void, Book, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook) +METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook) { THIS->update(*getPtr(env, otherBook)); } -METHOD(void, Book, update__Lorg_kiwix_libkiwix_JNIKiwixReader_2, jobject archive) +METHOD(void, update__Lorg_kiwix_libkiwix_JNIKiwixReader_2, jobject archive) { THIS->update(*getPtr(env, archive)); } -#define GETTER(retType, name) GETTER_METHOD(retType, libkiwix_Book, THIS, name) - GETTER(jstring, getId) GETTER(jstring, getPath) GETTER(jboolean, isPathValid) @@ -74,10 +70,8 @@ GETTER(jstring, getFavicon) GETTER(jstring, getFaviconUrl) GETTER(jstring, getFaviconMimeType) -METHOD(jstring, Book, getTagStr, jstring tagName) try { +METHOD(jstring, getTagStr, jstring tagName) try { return TO_JNI(THIS->getTagStr(TO_C(tagName))); } catch(...) { return c2jni("", env); } - -#undef GETTER diff --git a/lib/src/main/cpp/libkiwix/filter.cpp b/lib/src/main/cpp/libkiwix/filter.cpp index 2e29896..12312ff 100644 --- a/lib/src/main/cpp/libkiwix/filter.cpp +++ b/lib/src/main/cpp/libkiwix/filter.cpp @@ -25,27 +25,29 @@ #include "utils.h" #define NATIVE_TYPE kiwix::Filter -#define THIS GET_PTR(NATIVE_TYPE) +#define TYPENAME libkiwix_Filter +#include "macros.h" + /* Kiwix Reader JNI functions */ -METHOD0(void, Filter, allocate) { +METHOD0(void, allocate) { SET_PTR(std::make_shared()); } -METHOD0(void, Filter, dispose) { +METHOD0(void, dispose) { dispose(env, thisObj); } #define FORWARD(name, args_type) \ -METHOD(jobject, Filter, name, args_type value) { \ +METHOD(jobject, name, args_type value) { \ THIS->name(jni2c(value, env)); \ return thisObj; \ } #define FORWARDA(name, args_type) \ -METHOD(jobject, Filter, name, jobjectArray value) { \ +METHOD(jobject, name, jobjectArray value) { \ THIS->name(jni2c(value, env)); \ return thisObj; \ } diff --git a/lib/src/main/cpp/libkiwix/kiwixserver.cpp b/lib/src/main/cpp/libkiwix/kiwixserver.cpp index 750d09a..4aaff05 100644 --- a/lib/src/main/cpp/libkiwix/kiwixserver.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixserver.cpp @@ -26,11 +26,13 @@ #include "utils.h" #define NATIVE_TYPE kiwix::Server -#define THIS GET_PTR(NATIVE_TYPE) +#define TYPENAME libkiwix_Server +#include + + /* Kiwix Reader JNI functions */ -JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_setNativeServer( - JNIEnv* env, jobject thisObj, jobject jLibrary) +METHOD(void, setNativeServer, jobject jLibrary) { LOG("Attempting to create server"); Lock l; @@ -43,57 +45,48 @@ JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIKiwixServer_setNativeServer( } } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_dispose(JNIEnv* env, jobject obj) +METHOD0(void, dispose) { - dispose(env, obj); + dispose(env, thisObj); } /* Kiwix library functions */ -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setRoot(JNIEnv* env, jobject thisObj, jstring root) +METHOD(void, setRoot, jstring root) { THIS->setRoot(TO_C(root)); } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setAddress(JNIEnv* env, jobject thisObj, jstring address) +METHOD(void, setAddress, jstring address) { THIS->setAddress(TO_C(address)); } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setPort(JNIEnv* env, jobject thisObj, int port) +METHOD(void, setPort, int port) { THIS->setPort(TO_C(port)); } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setNbThreads(JNIEnv* env, jobject thisObj, int threads) +METHOD(void, setNbThreads, int threads) { THIS->setNbThreads(TO_C(threads)); } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setTaskbar(JNIEnv* env, jobject thisObj, jboolean withTaskbar, jboolean withLibraryButton) +METHOD(void, setTaskbar, jboolean withTaskbar, jboolean withLibraryButton) { THIS->setTaskbar(TO_C(withTaskbar), TO_C(withLibraryButton)); } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_setBlockExternalLinks(JNIEnv* env, jobject thisObj, jboolean blockExternalLinks) +METHOD(void, setBlockExternalLinks, jboolean blockExternalLinks) { THIS->setBlockExternalLinks(TO_C(blockExternalLinks)); } -JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_start(JNIEnv* env, jobject thisObj) +METHOD0(jboolean, start) { return THIS->start(); } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_JNIKiwixServer_stop(JNIEnv* env, jobject thisObj) +METHOD0(void, stop) { THIS->stop(); } diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index 9dace9c..2e61b23 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -25,18 +25,16 @@ #include "utils.h" #define NATIVE_TYPE kiwix::Library -#define THIS GET_PTR(NATIVE_TYPE) +#define TYPENAME libkiwix_Library +#include "macros.h" /* Kiwix Reader JNI functions */ -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Library_allocate( - JNIEnv* env, jobject thisObj) +METHOD0(void, allocate) { SET_PTR(std::make_shared()); } -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_Library_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, dispose) { dispose(env, thisObj); } @@ -59,24 +57,22 @@ Java_org_kiwix_kiwixlib_Library_addBook( return false; }*/ -METHOD(jobject, Library, getBookById, jstring id) { +METHOD(jobject, getBookById, jstring id) { auto obj = NEW_OBJECT("org/kiwix/libkiwix/Book"); SET_HANDLE(kiwix::Book, obj, THIS->getBookById(TO_C(id))); return obj; } -METHOD(jint, Library, getBookCount, jboolean localBooks, jboolean remoteBooks) { +METHOD(jint, getBookCount, jboolean localBooks, jboolean remoteBooks) { return THIS->getBookCount(localBooks, remoteBooks); } -#define GETTER(retType, name) GETTER_METHOD(retType, libkiwix_Library, THIS, name) - GETTER(jobjectArray, getBooksIds) GETTER(jobjectArray, getBooksLanguages) GETTER(jobjectArray, getBooksCreators) GETTER(jobjectArray, getBooksPublishers) -METHOD(jobjectArray, Library, filter, jobject filterObj) { +METHOD(jobjectArray, filter, jobject filterObj) { auto filter = getPtr(env, filterObj); return c2jni(THIS->filter(*filter), env); } diff --git a/lib/src/main/cpp/libkiwix/manager.cpp b/lib/src/main/cpp/libkiwix/manager.cpp index 48bc340..7e983f4 100644 --- a/lib/src/main/cpp/libkiwix/manager.cpp +++ b/lib/src/main/cpp/libkiwix/manager.cpp @@ -25,33 +25,26 @@ #include "utils.h" #define NATIVE_TYPE kiwix::Manager -#define THIS GET_PTR(NATIVE_TYPE) +#define TYPENAME libkiwix_Manager +#include -JNIEXPORT void JNICALL -Java_org_kiwix_libkiwix_Manager_allocate( - JNIEnv* env, jobject thisObj, jobject libraryObj) +METHOD(void, allocate, jobject libraryObj) { auto lib = getPtr(env, libraryObj); SET_PTR(std::make_shared(lib.get())); } -JNIEXPORT void JNICALL -Java_org_kiwix_libkiwix_Manager_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, dispose) { - dispose(env, thisObj); + dispose(env, thisObj); } - -#define MANAGER (getPtr(env, thisObj)) - /* Kiwix manager functions */ -JNIEXPORT jboolean JNICALL -Java_org_kiwix_libkiwix_Manager_readFile( - JNIEnv* env, jobject thisObj, jstring path) +METHOD(jboolean, readFile, jstring path) { - auto cPath = jni2c(path, env); + auto cPath = TO_C(path); try { - return MANAGER->readFile(cPath); + return THIS->readFile(cPath); } catch (std::exception& e) { LOG("Unable to get readFile"); LOG("%s", e.what()); @@ -59,15 +52,13 @@ Java_org_kiwix_libkiwix_Manager_readFile( return false; } -JNIEXPORT jboolean JNICALL -Java_org_kiwix_libkiwix_Manager_readXml( - JNIEnv* env, jobject thisObj, jstring content, jstring libraryPath) +METHOD(jboolean, readXml, jstring content, jstring libraryPath) { - auto cContent = jni2c(content, env); - auto cPath = jni2c(libraryPath, env); + auto cContent = TO_C(content); + auto cPath = TO_C(libraryPath); try { - return MANAGER->readXml(cContent, false, cPath); + return THIS->readXml(cContent, false, cPath); } catch (std::exception& e) { LOG("Unable to get ZIM id"); LOG("%s", e.what()); @@ -76,15 +67,13 @@ Java_org_kiwix_libkiwix_Manager_readXml( return false; } -JNIEXPORT jboolean JNICALL -Java_org_kiwix_libkiwix_Manager_readOpds( - JNIEnv* env, jobject thisObj, jstring content, jstring urlHost) +METHOD(jboolean, readOpds, jstring content, jstring urlHost) { - auto cContent = jni2c(content, env); - auto cUrl = jni2c(urlHost, env); + auto cContent = TO_C(content); + auto cUrl = TO_C(urlHost); try { - return MANAGER->readOpds(cContent, cUrl); + return THIS->readOpds(cContent, cUrl); } catch (std::exception& e) { LOG("Unable to get ZIM id"); LOG("%s", e.what()); @@ -93,14 +82,12 @@ Java_org_kiwix_libkiwix_Manager_readOpds( return false; } -JNIEXPORT jboolean JNICALL -Java_org_kiwix_libkiwix_Manager_readBookmarkFile( - JNIEnv* env, jobject thisObj, jstring path) +METHOD(jboolean, readBookmarkFile, jstring path) { - auto cPath = jni2c(path, env); + auto cPath = TO_C(path); try { - return MANAGER->readBookmarkFile(cPath); + return THIS->readBookmarkFile(cPath); } catch (std::exception& e) { LOG("Unable to get ZIM id"); LOG("%s", e.what()); @@ -109,18 +96,15 @@ Java_org_kiwix_libkiwix_Manager_readBookmarkFile( return false; } -JNIEXPORT jstring JNICALL -Java_org_kiwix_libkiwix_Manager_addBookFromPath( - JNIEnv* env, jobject thisObj, - jstring pathToOpen, jstring pathToSave, jstring url, jboolean checkMetaData) +METHOD(jstring, addBookFromPath, jstring pathToOpen, jstring pathToSave, jstring url, jboolean checkMetaData) { - auto cPathToOpen = jni2c(pathToOpen, env); - auto cPathToSave = jni2c(pathToSave, env); - auto cUrl = jni2c(url, env); + auto cPathToOpen = TO_C(pathToOpen); + auto cPathToSave = TO_C(pathToSave); + auto cUrl = TO_C(url); jstring id = NULL; try { - auto cId = MANAGER->addBookFromPathAndGetId(cPathToOpen, cPathToSave, cUrl, checkMetaData); + auto cId = THIS->addBookFromPathAndGetId(cPathToOpen, cPathToSave, cUrl, checkMetaData); if ( !cId.empty() ) { id = c2jni(cId, env); } diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index 8585780..1bd8c1b 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -30,9 +30,13 @@ #include #include +#define CLASSNAME "org/kiwix/libzim/Archive" +#define NATIVE_TYPE zim::Archive +#define TYPENAME libzim_Archive +#include + /* Kiwix Reader JNI functions */ -JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchive( - JNIEnv* env, jobject thisObj, jstring filename) +METHOD(void, setNativeArchive, jstring filename) { std::string cPath = TO_C(filename); @@ -115,10 +119,6 @@ Java_org_kiwix_libzim_Archive_dispose(JNIEnv* env, jobject thisObj) dispose(env, thisObj); } -#define THIS GET_PTR(zim::Archive) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Archive, THIS, name) - - GETTER(jstring, getFilename) GETTER(jlong, getFilesize) GETTER(jint, getAllEntryCount) @@ -126,15 +126,15 @@ GETTER(jint, getEntryCount) GETTER(jint, getArticleCount) GETTER(jint, getMediaCount) -METHOD0(jstring, libzim_Archive, getUuid) { +METHOD0(jstring, getUuid) { return TO_JNI(std::string(THIS->getUuid())); } -METHOD(jstring, libzim_Archive, getMetadata, jstring name) { +METHOD(jstring, getMetadata, jstring name) { return TO_JNI(THIS->getMetadata(TO_C(name))); } -METHOD(jobject, libzim_Archive, getMetadataItem, jstring name) { +METHOD(jobject, getMetadataItem, jstring name) { auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); SET_HANDLE(zim::Item, obj, THIS->getMetadataItem(TO_C(name))); return obj; @@ -142,71 +142,71 @@ METHOD(jobject, libzim_Archive, getMetadataItem, jstring name) { GETTER(jobjectArray, getMetadataKeys) -METHOD(jobject, libzim_Archive, getIllustrationItem, jint size) { +METHOD(jobject, getIllustrationItem, jint size) { auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); SET_HANDLE(zim::Item, obj, THIS->getIllustrationItem(TO_C(size))); return obj; } -METHOD(jboolean, libzim_Archive, hasIllustration, jint size) { +METHOD(jboolean, hasIllustration, jint size) { return TO_JNI(THIS->hasIllustration(TO_C(size))); } GETTER(jlongArray, getIllustrationSizes) -METHOD(jobject, libzim_Archive, getEntryByPath, jlong index) { +METHOD(jobject, getEntryByPath, jlong index) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(index))); return obj; } -METHOD(jobject, libzim_Archive, getEntryByPath, jstring path) { +METHOD(jobject, getEntryByPath, jstring path) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(path))); return obj; } -METHOD(jobject, libzim_Archive, getEntryByTitle, jlong index) { +METHOD(jobject, getEntryByTitle, jlong index) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(index))); return obj; } -METHOD(jobject, libzim_Archive, getEntryByTitle, jstring title) { +METHOD(jobject, getEntryByTitle, jstring title) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(title))); return obj; } -METHOD(jobject, libzim_Archive, getEntryByClusterOrder, jlong index) { +METHOD(jobject, getEntryByClusterOrder, jlong index) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getEntryByClusterOrder(TO_C(index))); return obj; } -METHOD0(jobject, libzim_Archive, getMainEntry) { +METHOD0(jobject, getMainEntry) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getMainEntry()); return obj; } -METHOD0(jobject, libzim_Archive, getRandomEntry) { +METHOD0(jobject, getRandomEntry) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getRandomEntry()); return obj; } -METHOD(jboolean, libzim_Archive, hasEntryByPath, jstring path) { +METHOD(jboolean, hasEntryByPath, jstring path) { return TO_JNI(THIS->hasEntryByPath(TO_C(path))); } -METHOD(jboolean, libzim_Archive, hasEntryByTitle, jstring title) { +METHOD(jboolean, hasEntryByTitle, jstring title) { return TO_JNI(THIS->hasEntryByPath(TO_C(title))); } GETTER(jboolean, hasMainEntry) -METHOD(jboolean, libzim_Archive, hasIllustration, jlong size) { +METHOD(jboolean, hasIllustration, jlong size) { return TO_JNI(THIS->hasIllustration(TO_C(size))); } @@ -222,7 +222,7 @@ GETTER(jboolean, hasNewNamespaceScheme) #define ITER_BY_PATH 0 #define ITER_BY_TITLE 1 #define ITER_EFFICIENT 2 -METHOD0(jobject, libzim_Archive, iterByPath) { +METHOD0(jobject, iterByPath) { auto range = THIS->iterByPath(); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); @@ -234,7 +234,7 @@ METHOD0(jobject, libzim_Archive, iterByPath) { return obj; } -METHOD0(jobject, libzim_Archive, iterByTitle) { +METHOD0(jobject, iterByTitle) { auto range = THIS->iterByTitle(); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); @@ -246,7 +246,7 @@ METHOD0(jobject, libzim_Archive, iterByTitle) { return obj; } -METHOD0(jobject, libzim_Archive, iterEfficient) { +METHOD0(jobject, iterEfficient) { auto range = THIS->iterEfficient(); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); @@ -258,7 +258,7 @@ METHOD0(jobject, libzim_Archive, iterEfficient) { return obj; } -METHOD(jobject, libzim_Archive, findByPath, jstring path) { +METHOD(jobject, findByPath, jstring path) { auto range = THIS->findByPath(TO_C(path)); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); @@ -270,7 +270,7 @@ METHOD(jobject, libzim_Archive, findByPath, jstring path) { return obj; } -METHOD(jobject, libzim_Archive, findByTitle, jstring title) { +METHOD(jobject, findByTitle, jstring title) { auto range = THIS->findByTitle(TO_C(title)); jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator"); jmethodID initMethod = env->GetMethodID(objClass, "", "(I)V"); diff --git a/lib/src/main/cpp/libzim/blob.cpp b/lib/src/main/cpp/libzim/blob.cpp index 3d0eff0..2ac159d 100644 --- a/lib/src/main/cpp/libzim/blob.cpp +++ b/lib/src/main/cpp/libzim/blob.cpp @@ -28,17 +28,19 @@ #include +#define CLASSNAME "org/kiwix/libzim/Blob" #define NATIVE_TYPE zim::Blob +#define TYPENAME libzim_Blob +#include -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_Blob_dispose(JNIEnv* env, jobject thisObj) + + +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Blob, THIS, name) -METHOD0(jstring, libzim_Blob, getData) { +METHOD0(jstring, getData) { return TO_JNI(std::string(*THIS)); } GETTER(jlong, size) diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp index 88a1024..03cfc3a 100644 --- a/lib/src/main/cpp/libzim/entry.cpp +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -29,34 +29,33 @@ #include #include +#define CLASSNAME "org/kiwix/libzim/Entry" #define NATIVE_TYPE zim::Entry +#define TYPENAME libzim_Entry +#include -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_Entry_dispose(JNIEnv* env, jobject thisObj) + +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Entry, THIS, name) - - GETTER(jboolean, isRedirect) GETTER(jstring, getTitle) GETTER(jstring, getPath) -METHOD(jobject, libzim_Entry, getItem, jboolean follow) { +METHOD(jobject, getItem, jboolean follow) { auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); SET_HANDLE(zim::Item, obj, THIS->getItem(TO_C(follow))); return obj; } -METHOD0(jobject, libzim_Entry, getRedirect) { +METHOD0(jobject, getRedirect) { auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); SET_HANDLE(zim::Item, obj, THIS->getRedirect()); return obj; } -METHOD0(jobject, libzim_Entry, getRedirectEntry) { +METHOD0(jobject, getRedirectEntry) { auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); SET_HANDLE(zim::Entry, obj, THIS->getRedirectEntry()); return obj; diff --git a/lib/src/main/cpp/libzim/entry_iterator.cpp b/lib/src/main/cpp/libzim/entry_iterator.cpp index 1984a2b..6920a0b 100644 --- a/lib/src/main/cpp/libzim/entry_iterator.cpp +++ b/lib/src/main/cpp/libzim/entry_iterator.cpp @@ -29,6 +29,10 @@ #include #include +#define CLASSNAME "org/kiwix/libzim/EntryIterator" +#define TYPENAME libzim_EntryIterator +#include "macros.h" + #define PATH_NATIVE_TYPE zim::Archive::iterator #define TITLE_NATIVE_TYPE zim::Archive::iterator #define EFFICIENT_NATIVE_TYPE zim::Archive::iterator @@ -39,7 +43,7 @@ inline int get_order(JNIEnv* env, jobject thisObj) { return TO_C(env->GetIntField(thisObj, fieldId)); } -METHOD0(void, libzim_EntryIterotar, dispose) +METHOD0(void, dispose) { // Delete end iterator switch (get_order(env, thisObj)) { @@ -59,7 +63,7 @@ METHOD0(void, libzim_EntryIterotar, dispose) } -METHOD0(jboolean, libzim_EntryIterator, hasNext) { +METHOD0(jboolean, hasNext) { switch (get_order(env, thisObj)) { case 0: { PATH_NATIVE_TYPE next(*GET_PTR(PATH_NATIVE_TYPE)); @@ -82,7 +86,7 @@ METHOD0(jboolean, libzim_EntryIterator, hasNext) { } } -METHOD0(jobject, libzim_EntryIterator, next) { +METHOD0(jobject, next) { switch (get_order(env, thisObj)) { case 0: { (*GET_PTR(PATH_NATIVE_TYPE))++; diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp index 9135ce1..fef3f91 100644 --- a/lib/src/main/cpp/libzim/item.cpp +++ b/lib/src/main/cpp/libzim/item.cpp @@ -28,22 +28,21 @@ #include +#define CLASSNAME "org/kiwix/libzim/Item" #define NATIVE_TYPE zim::Item +#define TYPENAME libzim_Item +#include -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_Item_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Item, THIS, name) - GETTER(jstring, getTitle) GETTER(jstring, getPath) GETTER(jstring, getMimetype) -METHOD0(jobject, libzim_Item, getData) { +METHOD0(jobject, getData) { auto obj = NEW_OBJECT("org/kiwix/libzim/Blob"); SET_HANDLE(zim::Blob, obj, THIS->getData()); return obj; diff --git a/lib/src/main/cpp/libzim/query.cpp b/lib/src/main/cpp/libzim/query.cpp index 575844a..b36983e 100644 --- a/lib/src/main/cpp/libzim/query.cpp +++ b/lib/src/main/cpp/libzim/query.cpp @@ -28,10 +28,12 @@ #include +#define CLASSNAME "org/kiwix/libzim/Query" #define NATIVE_TYPE zim::Query +#define TYPENAME libzim_Query +#include -JNIEXPORT void JNICALL Java_org_kiwix_libzim_Query_getNativeQuery( - JNIEnv* env, jobject thisObj, jstring query) +METHOD(void, setNativeQuery, jstring query) { auto cQuery = TO_C(query); Lock l; @@ -44,21 +46,17 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Query_getNativeQuery( } } - -JNIEXPORT void JNICALL -Java_org_kiwix_kiwixlib_libzim_Query_dispose(JNIEnv* env, jobject thisObj) +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) - -METHOD(jobject, libzim_Query, setQuery, jstring query) { +METHOD(jobject, setQuery, jstring query) { THIS->setQuery(TO_C(query)); return thisObj; } -METHOD(jobject, libzim_Query, setGeorange, jfloat latitude, jfloat longitude, jfloat distance) { +METHOD(jobject, setGeorange, jfloat latitude, jfloat longitude, jfloat distance) { THIS->setGeorange(TO_C(latitude), TO_C(longitude), TO_C(distance)); return thisObj; } diff --git a/lib/src/main/cpp/libzim/search.cpp b/lib/src/main/cpp/libzim/search.cpp index a791321..2bcb828 100644 --- a/lib/src/main/cpp/libzim/search.cpp +++ b/lib/src/main/cpp/libzim/search.cpp @@ -28,16 +28,18 @@ #include +#define CLASSNAME "org/kiwix/libzim/Search" #define NATIVE_TYPE zim::Search +#define TYPENAME libzim_Search +#include -METHOD0(void, libzim_Search, dispose) + +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) - -METHOD(jobject, libzim_Search, getResults, jint start, jint maxResults) { +METHOD(jobject, getResults, jint start, jint maxResults) { auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); auto obj = NEW_OBJECT("ork/kiwix/libzim/SearchIterator"); SET_HANDLE(zim::SearchIterator, obj, results.begin()); @@ -48,7 +50,4 @@ METHOD(jobject, libzim_Search, getResults, jint start, jint maxResults) { return obj; } -METHOD0(jlong, libzim_Search, getEstimatedMatches) { - return TO_JNI(THIS->getEstimatedMatches()); -} - +GETTER(jlong, getEstimatedMatches) diff --git a/lib/src/main/cpp/libzim/search_iterator.cpp b/lib/src/main/cpp/libzim/search_iterator.cpp index e3889eb..561a616 100644 --- a/lib/src/main/cpp/libzim/search_iterator.cpp +++ b/lib/src/main/cpp/libzim/search_iterator.cpp @@ -29,17 +29,18 @@ #include #include +#define CLASSNAME "org/kiwix/libzim/SearchIterator" #define NATIVE_TYPE zim::SearchIterator +#define TYPENAME libzim_SearchIterator +#include -METHOD0(void, libzim_SearchIterator, dispose) +METHOD0(void, dispose) { // Delete end iterator dispose(env, thisObj, "nativeHandleEnd"); dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_SearchIterator, THIS, name) GETTER(jstring, getPath) GETTER(jstring, getTitle) @@ -49,18 +50,18 @@ GETTER(jint, getWordCount) GETTER(jint, getFileIndex) GETTER(jint, getSize) -METHOD0(jstring, libzim_SearchIterator, getZimId) { +METHOD0(jstring, getZimId) { return TO_JNI(std::string(THIS->getZimId())); } -METHOD0(jboolean, libzim_SearchIterator, hasNext) { +METHOD0(jboolean, hasNext) { zim::SearchIterator next(*THIS); next++; auto end = getPtr(env, thisObj, "nativeHandleEnd"); return next == *end; } -METHOD0(jobject, libzim_SearchIterator, next) { +METHOD0(jobject, next) { (*THIS)++; zim::Entry entry = **THIS; auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp index 0483cc4..6ba813d 100644 --- a/lib/src/main/cpp/libzim/searcher.cpp +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -28,10 +28,12 @@ #include +#define CLASSNAME "org/kiwix/libzim/Searcher" #define NATIVE_TYPE zim::Searcher +#define TYPENAME libzim_Searcher +#include -JNIEXPORT void JNICALL Java_org_kiwix_libzim_Searcher_setNativeSearcher( - JNIEnv* env, jobject thisObj, jobject archive) +METHOD(void, setNativeSearcher, jobject archive) { Lock l; @@ -46,28 +48,25 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Searcher_setNativeSearcher( } -METHOD0(void, libzim_Searcher, dispose) +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_Searcher, THIS, name) - -METHOD(jobject, libzim_Searcher, addArchive, jobject archive) { +METHOD(jobject, addArchive, jobject archive) { auto cArchive = getPtr(env, archive); THIS->addArchive(*cArchive); return thisObj; } -METHOD(jobject, libzim_Searcher, search, jobject query) { +METHOD(jobject, search, jobject query) { auto cQuery = getPtr(env, query); auto obj = NEW_OBJECT("org/kiwix/libzim/Search"); SET_HANDLE(zim::Search, obj, THIS->search(*cQuery)); return obj; } -METHOD(void, libzim_Searcher, setVerbose, jboolean verbose) { +METHOD(void, setVerbose, jboolean verbose) { THIS->setVerbose(TO_C(verbose)); } diff --git a/lib/src/main/cpp/libzim/suggestion_item.cpp b/lib/src/main/cpp/libzim/suggestion_item.cpp index 9776579..82ff34d 100644 --- a/lib/src/main/cpp/libzim/suggestion_item.cpp +++ b/lib/src/main/cpp/libzim/suggestion_item.cpp @@ -28,17 +28,16 @@ #include +#define CLASSNAME "org/kiwix/libzim/SuggestionItem" #define NATIVE_TYPE zim::SuggestionItem +#define TYPENAME libzim_SuggestionItem +#include -METHOD0(void, libzim_SuggestionItem, dispose) +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_SuggestionItem, THIS, name) - - GETTER(jstring, getTitle) GETTER(jstring, getPath) GETTER(jstring, getSnippet) diff --git a/lib/src/main/cpp/libzim/suggestion_iterator.cpp b/lib/src/main/cpp/libzim/suggestion_iterator.cpp index 9541ab8..38bd9bc 100644 --- a/lib/src/main/cpp/libzim/suggestion_iterator.cpp +++ b/lib/src/main/cpp/libzim/suggestion_iterator.cpp @@ -28,26 +28,27 @@ #include +#define CLASSNAME "org/kiwix/libzim/SuggestionIterator" #define NATIVE_TYPE zim::SuggestionIterator +#define TYPENAME libzim_SuggestionIterator +#include -METHOD0(void, libzim_SuggestionIterator, dispose) + +METHOD0(void, dispose) { // Delete end iterator dispose(env, thisObj, "nativeHandleEnd"); dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_SuggestionIterator, THIS, name) - -METHOD0(jboolean, libzim_SearchIterator, hasNext) { +METHOD0(jboolean, hasNext) { NATIVE_TYPE next(*THIS); next++; auto end = getPtr(env, thisObj, "nativeHandleEnd"); return next == *end; } -METHOD0(jobject, libzim_SearchIterator, next) { +METHOD0(jobject, next) { (*THIS)++; zim::SuggestionItem item = **THIS; auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionItem"); diff --git a/lib/src/main/cpp/libzim/suggestion_search.cpp b/lib/src/main/cpp/libzim/suggestion_search.cpp index 78000d6..05dff7f 100644 --- a/lib/src/main/cpp/libzim/suggestion_search.cpp +++ b/lib/src/main/cpp/libzim/suggestion_search.cpp @@ -28,16 +28,17 @@ #include +#define CLASSNAME "org/kiwix/libzim/SuggestionSearch" #define NATIVE_TYPE zim::SuggestionSearch +#define TYPENAME libzim_SuggestionSearch +#include -METHOD0(void, libzim_SuggestionSearch, dispose) +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) - -METHOD(jobject, libzim_SuggestionSearch, getResults, jint start, jint maxResults) { +METHOD(jobject, getResults, jint start, jint maxResults) { auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); auto obj = NEW_OBJECT("ork/kiwix/libzim/SuggestionIterator"); SET_HANDLE(zim::SuggestionIterator, obj, results.begin()); @@ -48,7 +49,4 @@ METHOD(jobject, libzim_SuggestionSearch, getResults, jint start, jint maxResults return obj; } -METHOD0(jlong, libzim_SuggestionSearch, getEstimatedMatches) { - return TO_JNI(THIS->getEstimatedMatches()); -} - +GETTER(jlong, getEstimatedMatches) diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp index 15c9455..4f62185 100644 --- a/lib/src/main/cpp/libzim/suggestion_searcher.cpp +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -29,9 +29,11 @@ #include #define NATIVE_TYPE zim::SuggestionSearcher +#define TYPENAME libzim_SuggestionSearcher +#include -JNIEXPORT void JNICALL Java_org_kiwix_libzim_SuggestionSearcher_setNativeSearcher( - JNIEnv* env, jobject thisObj, jobject archive) + +METHOD(void, setNativeSearcher, jobject archive) { Lock l; @@ -46,21 +48,18 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_SuggestionSearcher_setNativeSearche } -METHOD0(void, libzim_SuggestionSearcher, dispose) +METHOD0(void, dispose) { dispose(env, thisObj); } -#define THIS GET_PTR(NATIVE_TYPE) -#define GETTER(retType, name) GETTER_METHOD(retType, libzim_SuggestionSearcher, THIS, name) - -METHOD(jobject, libzim_SuggestionSearcher, suggest, jstring query) { +METHOD(jobject, suggest, jstring query) { auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionSearch"); SET_HANDLE(zim::SuggestionSearch, obj, THIS->suggest(TO_C(query))); return obj; } -METHOD(void, libzim_SuggestionSearcher, setVerbose, jboolean verbose) { +METHOD(void, setVerbose, jboolean verbose) { THIS->setVerbose(TO_C(verbose)); } diff --git a/lib/src/main/cpp/macros.h b/lib/src/main/cpp/macros.h new file mode 100644 index 0000000..807ab34 --- /dev/null +++ b/lib/src/main/cpp/macros.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2013 Emmanuel Engelhart + * Copyright (C) 2017 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +// You must define NATIVE_TYPE("zim::Archive") and TYPENAME("zim__Archive") + + +#define BUILD_METHOD_NX(TYPE, NAME) Java_org_kiwix_ ## TYPE ## _ ## NAME +#define BUILD_METHOD(TYPE, NAME) BUILD_METHOD_NX(TYPE, NAME) + +#define THIS getPtr(env, thisObj) + +#define METHOD0(retType, name) \ +JNIEXPORT retType JNICALL BUILD_METHOD(TYPENAME, name) ( \ + JNIEnv* env, jobject thisObj) + +#define METHOD(retType, name, ...) \ +JNIEXPORT retType JNICALL BUILD_METHOD(TYPENAME ,name) ( \ + JNIEnv* env, jobject thisObj, __VA_ARGS__) + +#define GETTER(retType, name) METHOD0(retType, name) { \ + return TO_JNI(THIS->name()); \ +} + diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index ed15d57..940387a 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -108,18 +108,6 @@ void dispose(JNIEnv* env, jobject thisObj, const char* handleName = "nativeHandl env->SetLongField(thisObj, fieldId, 0); } -#define METHOD0(retType, class, name) \ -JNIEXPORT retType JNICALL Java_org_kiwix_##class##_##name( \ - JNIEnv* env, jobject thisObj) - -#define METHOD(retType, class, name, ...) \ -JNIEXPORT retType JNICALL Java_org_kiwix_##class##_##name( \ - JNIEnv* env, jobject thisObj, __VA_ARGS__) - -#define GETTER_METHOD(retType, class, THIS, name) METHOD0(retType, class, name) { \ - return TO_JNI(THIS->name()); \ -} - inline jfieldID getHandleField(JNIEnv* env, jobject obj, const char* handleName) { jclass c = env->GetObjectClass(obj); From d63b84a5ccfce5af47bb3482b4377df3e0f50f64 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jan 2023 17:09:42 +0100 Subject: [PATCH 14/32] Repass on kiwix::Library wrapper --- lib/src/main/cpp/libkiwix/library.cpp | 34 ++++++++++++------- .../main/java/org/kiwix/libkiwix/Library.java | 27 ++++++++++----- 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index 2e61b23..a239dd4 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -29,7 +29,7 @@ #include "macros.h" /* Kiwix Reader JNI functions */ -METHOD0(void, allocate) +METHOD0(void, setNativeHandler) { SET_PTR(std::make_shared()); } @@ -40,22 +40,17 @@ METHOD0(void, dispose) } /* Kiwix library functions */ -/*JNIEXPORT jboolean JNICALL -Java_org_kiwix_kiwixlib_Library_addBook( - JNIEnv* env, jobject thisObj, jstring path) +METHOD(jboolean, addBook, jobject book) { - auto cPath = TO_C(path); + auto cBook = getPtr(env, book); try { - kiwix::Reader reader(cPath); - kiwix::Book book; - book.update(reader); - return LIBRARY->addBook(book); + return THIS->addBook(*cBook); } catch (std::exception& e) { LOG("Unable to add the book"); - LOG(e.what()); } + LOG("%s", e.what()); } return false; -}*/ +} METHOD(jobject, getBookById, jstring id) { auto obj = NEW_OBJECT("org/kiwix/libkiwix/Book"); @@ -63,12 +58,27 @@ METHOD(jobject, getBookById, jstring id) { return obj; } +METHOD(jobject, getArchiveById, jstring id) { + auto obj = NEW_OBJECT("org/kiwix/libzim/Archive"); + setPtr(env, obj, THIS->getArchiveById(TO_C(id))); + return obj; +} + +METHOD(jboolean, removeBookById, jstring id) { + return TO_JNI(THIS->removeBookById(TO_C(id))); +} + +METHOD(jboolean, writeToFile, jstring path) { + return TO_JNI(THIS->writeToFile(TO_C(path))); +} + METHOD(jint, getBookCount, jboolean localBooks, jboolean remoteBooks) { - return THIS->getBookCount(localBooks, remoteBooks); + return TO_JNI(THIS->getBookCount(TO_C(localBooks), TO_C(remoteBooks))); } GETTER(jobjectArray, getBooksIds) GETTER(jobjectArray, getBooksLanguages) +GETTER(jobjectArray, getBooksCategories) GETTER(jobjectArray, getBooksCreators) GETTER(jobjectArray, getBooksPublishers) diff --git a/lib/src/main/java/org/kiwix/libkiwix/Library.java b/lib/src/main/java/org/kiwix/libkiwix/Library.java index 60f3af6..5d0b213 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/Library.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Library.java @@ -19,31 +19,42 @@ package org.kiwix.libkiwix; +import org.kiwix.libzim.Archive; +import org.kiwix.libzim.Searcher; import org.kiwix.libkiwix.Book; import org.kiwix.libkiwix.JNIKiwixException; public class Library { - public native boolean addBook(String path) throws JNIKiwixException; + public Library() + { + setNativeHandler(); + } + public native boolean addBook(Book book) throws JNIKiwixException; public native Book getBookById(String id); - public native int getBookCount(boolean localBooks, boolean remoteBooks); + + public native Archive getArchiveById(String id); + //public native Searcher getSearcherById(String id); + //public native Searcher getSearcherByIds(String ids[]); + + public native boolean removeBookById(String id); + + public native boolean writeToFile(String path); + + public native int getBookCount(boolean localBooks, boolean remoteBooks); public native String[] getBooksIds(); public native String[] filter(Filter filter); public native String[] getBooksLanguages(); + public native String[] getBooksCategories(); public native String[] getBooksCreators(); public native String[] getBooksPublishers(); - public Library() - { - allocate(); - } - @Override protected void finalize() { dispose(); } - private native void allocate(); + private native void setNativeHandler(); private native void dispose(); private long nativeHandle; } From 876d38b9f35f15196681bcc6be40c31364473361 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jan 2023 19:21:35 +0100 Subject: [PATCH 15/32] Introduce BUILD_WRAPPER macro --- lib/src/main/cpp/libkiwix/library.cpp | 8 ++--- lib/src/main/cpp/libzim/archive.cpp | 36 +++++-------------- lib/src/main/cpp/libzim/entry.cpp | 12 ++----- lib/src/main/cpp/libzim/entry_iterator.cpp | 12 ++----- lib/src/main/cpp/libzim/item.cpp | 4 +-- lib/src/main/cpp/libzim/search_iterator.cpp | 4 +-- lib/src/main/cpp/libzim/searcher.cpp | 4 +-- .../main/cpp/libzim/suggestion_iterator.cpp | 4 +-- .../main/cpp/libzim/suggestion_searcher.cpp | 4 +-- lib/src/main/cpp/utils.h | 9 +++++ 10 files changed, 31 insertions(+), 66 deletions(-) diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index a239dd4..2ba168f 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -53,15 +53,11 @@ METHOD(jboolean, addBook, jobject book) } METHOD(jobject, getBookById, jstring id) { - auto obj = NEW_OBJECT("org/kiwix/libkiwix/Book"); - SET_HANDLE(kiwix::Book, obj, THIS->getBookById(TO_C(id))); - return obj; + return BUILD_WRAPPER("org/kiwix/libkiwix/Book", THIS->getBookById(TO_C(id))); } METHOD(jobject, getArchiveById, jstring id) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Archive"); - setPtr(env, obj, THIS->getArchiveById(TO_C(id))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Archive", THIS->getArchiveById(TO_C(id))); } METHOD(jboolean, removeBookById, jstring id) { diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index 1bd8c1b..0b2332d 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -135,17 +135,13 @@ METHOD(jstring, getMetadata, jstring name) { } METHOD(jobject, getMetadataItem, jstring name) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); - SET_HANDLE(zim::Item, obj, THIS->getMetadataItem(TO_C(name))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getMetadataItem(TO_C(name))); } GETTER(jobjectArray, getMetadataKeys) METHOD(jobject, getIllustrationItem, jint size) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); - SET_HANDLE(zim::Item, obj, THIS->getIllustrationItem(TO_C(size))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getIllustrationItem(TO_C(size))); } METHOD(jboolean, hasIllustration, jint size) { @@ -155,45 +151,31 @@ METHOD(jboolean, hasIllustration, jint size) { GETTER(jlongArray, getIllustrationSizes) METHOD(jobject, getEntryByPath, jlong index) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(index))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(index))); } METHOD(jobject, getEntryByPath, jstring path) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, THIS->getEntryByPath(TO_C(path))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(path))); } METHOD(jobject, getEntryByTitle, jlong index) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(index))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(index))); } METHOD(jobject, getEntryByTitle, jstring title) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, THIS->getEntryByTitle(TO_C(title))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(title))); } METHOD(jobject, getEntryByClusterOrder, jlong index) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, THIS->getEntryByClusterOrder(TO_C(index))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByClusterOrder(TO_C(index))); } METHOD0(jobject, getMainEntry) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, THIS->getMainEntry()); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getMainEntry()); } METHOD0(jobject, getRandomEntry) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, THIS->getRandomEntry()); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getRandomEntry()); } METHOD(jboolean, hasEntryByPath, jstring path) { diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp index 03cfc3a..1cf4258 100644 --- a/lib/src/main/cpp/libzim/entry.cpp +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -44,19 +44,13 @@ GETTER(jboolean, isRedirect) GETTER(jstring, getTitle) GETTER(jstring, getPath) METHOD(jobject, getItem, jboolean follow) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); - SET_HANDLE(zim::Item, obj, THIS->getItem(TO_C(follow))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getItem(TO_C(follow))); } METHOD0(jobject, getRedirect) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Item"); - SET_HANDLE(zim::Item, obj, THIS->getRedirect()); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Item", THIS->getRedirect()); } METHOD0(jobject, getRedirectEntry) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, THIS->getRedirectEntry()); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getRedirectEntry()); } diff --git a/lib/src/main/cpp/libzim/entry_iterator.cpp b/lib/src/main/cpp/libzim/entry_iterator.cpp index 6920a0b..0fcf4d5 100644 --- a/lib/src/main/cpp/libzim/entry_iterator.cpp +++ b/lib/src/main/cpp/libzim/entry_iterator.cpp @@ -91,23 +91,17 @@ METHOD0(jobject, next) { case 0: { (*GET_PTR(PATH_NATIVE_TYPE))++; zim::Entry entry = **GET_PTR(PATH_NATIVE_TYPE); - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, entry); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); } case 1: { (*GET_PTR(TITLE_NATIVE_TYPE))++; zim::Entry entry = **GET_PTR(TITLE_NATIVE_TYPE); - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, entry); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); } case 2: { (*GET_PTR(EFFICIENT_NATIVE_TYPE))++; zim::Entry entry = **GET_PTR(EFFICIENT_NATIVE_TYPE); - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, entry); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); } } } diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp index fef3f91..edbc168 100644 --- a/lib/src/main/cpp/libzim/item.cpp +++ b/lib/src/main/cpp/libzim/item.cpp @@ -43,9 +43,7 @@ GETTER(jstring, getPath) GETTER(jstring, getMimetype) METHOD0(jobject, getData) { - auto obj = NEW_OBJECT("org/kiwix/libzim/Blob"); - SET_HANDLE(zim::Blob, obj, THIS->getData()); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Blob", THIS->getData()); } GETTER(jlong, getSize) diff --git a/lib/src/main/cpp/libzim/search_iterator.cpp b/lib/src/main/cpp/libzim/search_iterator.cpp index 561a616..7ebb84d 100644 --- a/lib/src/main/cpp/libzim/search_iterator.cpp +++ b/lib/src/main/cpp/libzim/search_iterator.cpp @@ -64,8 +64,6 @@ METHOD0(jboolean, hasNext) { METHOD0(jobject, next) { (*THIS)++; zim::Entry entry = **THIS; - auto obj = NEW_OBJECT("org/kiwix/libzim/Entry"); - SET_HANDLE(zim::Entry, obj, entry); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); } diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp index 6ba813d..eb848fa 100644 --- a/lib/src/main/cpp/libzim/searcher.cpp +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -61,9 +61,7 @@ METHOD(jobject, addArchive, jobject archive) { METHOD(jobject, search, jobject query) { auto cQuery = getPtr(env, query); - auto obj = NEW_OBJECT("org/kiwix/libzim/Search"); - SET_HANDLE(zim::Search, obj, THIS->search(*cQuery)); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/Search", THIS->search(*cQuery)); } METHOD(void, setVerbose, jboolean verbose) { diff --git a/lib/src/main/cpp/libzim/suggestion_iterator.cpp b/lib/src/main/cpp/libzim/suggestion_iterator.cpp index 38bd9bc..6637780 100644 --- a/lib/src/main/cpp/libzim/suggestion_iterator.cpp +++ b/lib/src/main/cpp/libzim/suggestion_iterator.cpp @@ -51,8 +51,6 @@ METHOD0(jboolean, hasNext) { METHOD0(jobject, next) { (*THIS)++; zim::SuggestionItem item = **THIS; - auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionItem"); - SET_HANDLE(zim::SuggestionItem, obj, item); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/SuggestionItem", item); } diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp index 4f62185..8df8f3c 100644 --- a/lib/src/main/cpp/libzim/suggestion_searcher.cpp +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -54,9 +54,7 @@ METHOD0(void, dispose) } METHOD(jobject, suggest, jstring query) { - auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionSearch"); - SET_HANDLE(zim::SuggestionSearch, obj, THIS->suggest(TO_C(query))); - return obj; + return BUILD_WRAPPER("org/kiwix/libzim/SuggestionSearch", THIS->suggest(TO_C(query))); } METHOD(void, setVerbose, jboolean verbose) { diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index 940387a..ee77347 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -121,6 +121,15 @@ inline jobjectArray createArray(JNIEnv* env, size_t length, const std::string& t return env->NewObjectArray(length, c, NULL); } +template +inline jobject buildWrapper(JNIEnv* env, const char* class_name, T&& obj, const char* handleName = "nativeHandle") { + auto wrapper = newObject(class_name, env); + auto ptr = std::make_shared(std::move(obj)); + setPtr(env, wrapper, std::move(ptr)); + return wrapper; +} +#define BUILD_WRAPPER(CLASSNAME, OBJ) buildWrapper(env, CLASSNAME, std::move(OBJ)) + // A mixin class which will lock the globalLock when a instance is created // This avoid the cration of two instance inheriting from Lock in the same time. From 9426c6fcb8600daa136d30e65cd0410a334c86d5 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jan 2023 19:23:16 +0100 Subject: [PATCH 16/32] Use Illustration api instead of deprecated favicon for Book --- lib/src/main/cpp/CMakeLists.txt | 1 + lib/src/main/cpp/libkiwix/book.cpp | 17 ++++-- lib/src/main/cpp/libkiwix/illustration.cpp | 53 +++++++++++++++++++ lib/src/main/cpp/utils.h | 2 + .../main/java/org/kiwix/libkiwix/Book.java | 6 +-- .../java/org/kiwix/libkiwix/Illustration.java | 17 ++++++ 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 lib/src/main/cpp/libkiwix/illustration.cpp create mode 100644 lib/src/main/java/org/kiwix/libkiwix/Illustration.java diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt index 27858e5..d72816b 100644 --- a/lib/src/main/cpp/CMakeLists.txt +++ b/lib/src/main/cpp/CMakeLists.txt @@ -52,6 +52,7 @@ add_library( libkiwix/kiwixserver.cpp libkiwix/library.cpp libkiwix/manager.cpp + libkiwix/illustration.cpp ) find_library(libkiwix diff --git a/lib/src/main/cpp/libkiwix/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp index dd9c21c..e95b295 100644 --- a/lib/src/main/cpp/libkiwix/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -66,10 +66,21 @@ GETTER(jstring, getTags) GETTER(jlong, getArticleCount) GETTER(jlong, getMediaCount) GETTER(jlong, getSize) -GETTER(jstring, getFavicon) -GETTER(jstring, getFaviconUrl) -GETTER(jstring, getFaviconMimeType) +METHOD0(jobjectArray, getIllustrations) { + auto illustrations = THIS->getIllustrations(); + jobjectArray retArray = createArray(env, illustrations.size(), "org/kiwix/libkiwix/Illustration"); + size_t index = 0; + for (auto illu: illustrations) { + auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwx/Illustration", illu); + env->SetObjectArrayElement(retArray, index++, wrapper); + } + return retArray; +} + +METHOD(jobject, getIllustration, jint size) { + return BUILD_WRAPPER("org/kiwix/libkiwix/Illustration", THIS->getIllustration(TO_C(size))); +} METHOD(jstring, getTagStr, jstring tagName) try { return TO_JNI(THIS->getTagStr(TO_C(tagName))); } catch(...) { diff --git a/lib/src/main/cpp/libkiwix/illustration.cpp b/lib/src/main/cpp/libkiwix/illustration.cpp new file mode 100644 index 0000000..f4730d9 --- /dev/null +++ b/lib/src/main/cpp/libkiwix/illustration.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2020 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include "org_kiwix_libkiwix_Book.h" + +#include "utils.h" +#include "book.h" +#include + +#define NATIVE_TYPE kiwix::Book::Illustration +#define TYPENAME libkiwix_Illustration +#include + +METHOD0(void, dispose) +{ + dispose(env, thisObj); +} + +METHOD0(jint, width) { + return TO_JNI(THIS->width); +} + +METHOD0(jint, height) { + return TO_JNI(THIS->width); +} + +METHOD0(jstring, mimeType) { + return TO_JNI(THIS->mimeType); +} + +METHOD0(jstring, url) { + return TO_JNI(THIS->url); +} + +GETTER(jstring, getData) diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index ee77347..220a317 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -147,8 +147,10 @@ template struct JType { }; template<> struct JType{ typedef jboolean type_t; }; +template<> struct JType{ typedef jint type_t; }; template<> struct JType{ typedef jint type_t; }; template<> struct JType{ typedef jlong type_t; }; +template<> struct JType{ typedef jint type_t; }; template<> struct JType { typedef jlong type_t; }; template<> struct JType { typedef jlong type_t; }; template<> struct JType{ typedef jstring type_t; }; diff --git a/lib/src/main/java/org/kiwix/libkiwix/Book.java b/lib/src/main/java/org/kiwix/libkiwix/Book.java index 9c6edde..5d3e042 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/Book.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Book.java @@ -2,6 +2,7 @@ package org.kiwix.libkiwix; import org.kiwix.libzim.Archive; +import org.kiwix.libkiwix.Illustration; public class Book { @@ -40,9 +41,8 @@ public class Book public native long getMediaCount(); public native long getSize(); - public native String getFavicon(); - public native String getFaviconUrl(); - public native String getFaviconMimeType(); + public native Illustration[] getIllustrations(); + public native Illustration getIllustration(int size); private native void allocate(); private native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libkiwix/Illustration.java b/lib/src/main/java/org/kiwix/libkiwix/Illustration.java new file mode 100644 index 0000000..771a4e2 --- /dev/null +++ b/lib/src/main/java/org/kiwix/libkiwix/Illustration.java @@ -0,0 +1,17 @@ + +package org.kiwix.libkiwix; + +public class Illustration +{ + public native int width(); + public native int height(); + public native String mimeType(); + public native String url(); + + public native String getData(); + @Override + protected void finalize() { dispose(); } + + private native void dispose(); + private long nativeHandle; +} From 17b85bfff60c5a23da0239c9227cbea1683effa0 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jan 2023 19:23:33 +0100 Subject: [PATCH 17/32] Remove some warning. --- lib/src/main/cpp/libzim/entry_iterator.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/src/main/cpp/libzim/entry_iterator.cpp b/lib/src/main/cpp/libzim/entry_iterator.cpp index 0fcf4d5..9f19d56 100644 --- a/lib/src/main/cpp/libzim/entry_iterator.cpp +++ b/lib/src/main/cpp/libzim/entry_iterator.cpp @@ -83,6 +83,9 @@ METHOD0(jboolean, hasNext) { auto end = getPtr(env, thisObj, "nativeHandleEnd"); return next == *end; } + default: + // unreachable!() + return false; } } @@ -103,6 +106,9 @@ METHOD0(jobject, next) { zim::Entry entry = **GET_PTR(EFFICIENT_NATIVE_TYPE); return BUILD_WRAPPER("org/kiwix/libzim/Entry", entry); } + default: + // unreachable!() + return nullptr; } } From fbdea0dda2f72866fe11e7009c995a9775c29b58 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:07:49 +0100 Subject: [PATCH 18/32] Rename `JNIKiwixServer` to `Server`. --- lib/src/main/cpp/libkiwix/kiwixserver.cpp | 2 +- .../kiwix/libkiwix/{JNIKiwixServer.java => Server.java} | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) rename lib/src/main/java/org/kiwix/libkiwix/{JNIKiwixServer.java => Server.java} (88%) diff --git a/lib/src/main/cpp/libkiwix/kiwixserver.cpp b/lib/src/main/cpp/libkiwix/kiwixserver.cpp index 4aaff05..30e9ba8 100644 --- a/lib/src/main/cpp/libkiwix/kiwixserver.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixserver.cpp @@ -20,7 +20,7 @@ #include -#include "org_kiwix_libkiwix_JNIKiwixServer.h" +#include "org_kiwix_libkiwix_Server.h" #include "server.h" #include "utils.h" diff --git a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixServer.java b/lib/src/main/java/org/kiwix/libkiwix/Server.java similarity index 88% rename from lib/src/main/java/org/kiwix/libkiwix/JNIKiwixServer.java rename to lib/src/main/java/org/kiwix/libkiwix/Server.java index df8e0eb..58a13f1 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwixServer.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Server.java @@ -22,7 +22,7 @@ import org.kiwix.libkiwix.JNIKiwixException; import org.kiwix.libkiwix.Library; -public class JNIKiwixServer +public class Server { public native void setRoot(String root); @@ -40,11 +40,12 @@ public class JNIKiwixServer public native void stop(); - public JNIKiwixServer(Library library) + public Server(Library library) { - nativeHandle = getNativeServer(library); + setNativeServer(library); } - private native long getNativeServer(Library library); + private native void setNativeServer(Library library); + private native void dispose(); private long nativeHandle; } From fe1de03b2365a6f37fe4a7003b0325890ad87ba7 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:10:17 +0100 Subject: [PATCH 19/32] =?UTF-8?q?Intrudoce=20DISPOSE=C2=A0macro=20to=20imp?= =?UTF-8?q?lement=20the=20dispose=20method.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/main/cpp/libkiwix/book.cpp | 5 +---- lib/src/main/cpp/libkiwix/filter.cpp | 5 +---- lib/src/main/cpp/libkiwix/kiwixserver.cpp | 6 ++---- lib/src/main/cpp/libkiwix/library.cpp | 5 +---- lib/src/main/cpp/libkiwix/manager.cpp | 6 ++---- lib/src/main/cpp/libzim/archive.cpp | 6 +----- lib/src/main/cpp/libzim/blob.cpp | 7 +------ lib/src/main/cpp/libzim/entry.cpp | 5 +---- lib/src/main/cpp/libzim/item.cpp | 5 +---- lib/src/main/cpp/libzim/query.cpp | 5 +---- lib/src/main/cpp/libzim/search.cpp | 6 +----- lib/src/main/cpp/libzim/search_iterator.cpp | 2 ++ lib/src/main/cpp/libzim/searcher.cpp | 6 ++---- lib/src/main/cpp/libzim/suggestion_item.cpp | 5 +---- lib/src/main/cpp/libzim/suggestion_iterator.cpp | 2 +- lib/src/main/cpp/libzim/suggestion_search.cpp | 5 +---- lib/src/main/cpp/libzim/suggestion_searcher.cpp | 6 +----- lib/src/main/cpp/macros.h | 1 + 18 files changed, 22 insertions(+), 66 deletions(-) diff --git a/lib/src/main/cpp/libkiwix/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp index e95b295..b021565 100644 --- a/lib/src/main/cpp/libkiwix/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -34,10 +34,7 @@ METHOD0(void, allocate) SET_PTR(std::make_shared()); } -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook) { diff --git a/lib/src/main/cpp/libkiwix/filter.cpp b/lib/src/main/cpp/libkiwix/filter.cpp index 12312ff..4e4a5f3 100644 --- a/lib/src/main/cpp/libkiwix/filter.cpp +++ b/lib/src/main/cpp/libkiwix/filter.cpp @@ -35,10 +35,7 @@ METHOD0(void, allocate) { SET_PTR(std::make_shared()); } -METHOD0(void, dispose) { - dispose(env, thisObj); -} - +DISPOSE #define FORWARD(name, args_type) \ METHOD(jobject, name, args_type value) { \ diff --git a/lib/src/main/cpp/libkiwix/kiwixserver.cpp b/lib/src/main/cpp/libkiwix/kiwixserver.cpp index 30e9ba8..e584adb 100644 --- a/lib/src/main/cpp/libkiwix/kiwixserver.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixserver.cpp @@ -45,10 +45,8 @@ METHOD(void, setNativeServer, jobject jLibrary) } } -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} + +DISPOSE /* Kiwix library functions */ METHOD(void, setRoot, jstring root) diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index 2ba168f..76dbb18 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -34,10 +34,7 @@ METHOD0(void, setNativeHandler) SET_PTR(std::make_shared()); } -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE /* Kiwix library functions */ METHOD(jboolean, addBook, jobject book) diff --git a/lib/src/main/cpp/libkiwix/manager.cpp b/lib/src/main/cpp/libkiwix/manager.cpp index 7e983f4..cc9a9bd 100644 --- a/lib/src/main/cpp/libkiwix/manager.cpp +++ b/lib/src/main/cpp/libkiwix/manager.cpp @@ -34,10 +34,8 @@ METHOD(void, allocate, jobject libraryObj) SET_PTR(std::make_shared(lib.get())); } -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE + /* Kiwix manager functions */ METHOD(jboolean, readFile, jstring path) { diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index 0b2332d..856a1ea 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -113,11 +113,7 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded( #endif } -JNIEXPORT void JNICALL -Java_org_kiwix_libzim_Archive_dispose(JNIEnv* env, jobject thisObj) -{ - dispose(env, thisObj); -} +DISPOSE GETTER(jstring, getFilename) GETTER(jlong, getFilesize) diff --git a/lib/src/main/cpp/libzim/blob.cpp b/lib/src/main/cpp/libzim/blob.cpp index 2ac159d..a197e11 100644 --- a/lib/src/main/cpp/libzim/blob.cpp +++ b/lib/src/main/cpp/libzim/blob.cpp @@ -33,12 +33,7 @@ #define TYPENAME libzim_Blob #include - - -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE METHOD0(jstring, getData) { return TO_JNI(std::string(*THIS)); diff --git a/lib/src/main/cpp/libzim/entry.cpp b/lib/src/main/cpp/libzim/entry.cpp index 1cf4258..2652e25 100644 --- a/lib/src/main/cpp/libzim/entry.cpp +++ b/lib/src/main/cpp/libzim/entry.cpp @@ -35,10 +35,7 @@ #include -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE GETTER(jboolean, isRedirect) GETTER(jstring, getTitle) diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp index edbc168..e31b5c4 100644 --- a/lib/src/main/cpp/libzim/item.cpp +++ b/lib/src/main/cpp/libzim/item.cpp @@ -33,10 +33,7 @@ #define TYPENAME libzim_Item #include -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE GETTER(jstring, getTitle) GETTER(jstring, getPath) diff --git a/lib/src/main/cpp/libzim/query.cpp b/lib/src/main/cpp/libzim/query.cpp index b36983e..2578d66 100644 --- a/lib/src/main/cpp/libzim/query.cpp +++ b/lib/src/main/cpp/libzim/query.cpp @@ -46,10 +46,7 @@ METHOD(void, setNativeQuery, jstring query) } } -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE METHOD(jobject, setQuery, jstring query) { THIS->setQuery(TO_C(query)); diff --git a/lib/src/main/cpp/libzim/search.cpp b/lib/src/main/cpp/libzim/search.cpp index 2bcb828..763bb2c 100644 --- a/lib/src/main/cpp/libzim/search.cpp +++ b/lib/src/main/cpp/libzim/search.cpp @@ -33,11 +33,7 @@ #define TYPENAME libzim_Search #include - -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE METHOD(jobject, getResults, jint start, jint maxResults) { auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); diff --git a/lib/src/main/cpp/libzim/search_iterator.cpp b/lib/src/main/cpp/libzim/search_iterator.cpp index 7ebb84d..6c04c7d 100644 --- a/lib/src/main/cpp/libzim/search_iterator.cpp +++ b/lib/src/main/cpp/libzim/search_iterator.cpp @@ -34,6 +34,8 @@ #define TYPENAME libzim_SearchIterator #include + +// We cannot use the default macro to implement `dispose` as we need to delete the end handle METHOD0(void, dispose) { // Delete end iterator diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp index eb848fa..eaaff98 100644 --- a/lib/src/main/cpp/libzim/searcher.cpp +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -48,10 +48,8 @@ METHOD(void, setNativeSearcher, jobject archive) } -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} + +DISPOSE METHOD(jobject, addArchive, jobject archive) { auto cArchive = getPtr(env, archive); diff --git a/lib/src/main/cpp/libzim/suggestion_item.cpp b/lib/src/main/cpp/libzim/suggestion_item.cpp index 82ff34d..35b1c4f 100644 --- a/lib/src/main/cpp/libzim/suggestion_item.cpp +++ b/lib/src/main/cpp/libzim/suggestion_item.cpp @@ -33,10 +33,7 @@ #define TYPENAME libzim_SuggestionItem #include -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE GETTER(jstring, getTitle) GETTER(jstring, getPath) diff --git a/lib/src/main/cpp/libzim/suggestion_iterator.cpp b/lib/src/main/cpp/libzim/suggestion_iterator.cpp index 6637780..5181ae1 100644 --- a/lib/src/main/cpp/libzim/suggestion_iterator.cpp +++ b/lib/src/main/cpp/libzim/suggestion_iterator.cpp @@ -33,7 +33,7 @@ #define TYPENAME libzim_SuggestionIterator #include - +// We cannot use the default macro to implement `dispose` as we need to delete the end handle METHOD0(void, dispose) { // Delete end iterator diff --git a/lib/src/main/cpp/libzim/suggestion_search.cpp b/lib/src/main/cpp/libzim/suggestion_search.cpp index 05dff7f..26fa783 100644 --- a/lib/src/main/cpp/libzim/suggestion_search.cpp +++ b/lib/src/main/cpp/libzim/suggestion_search.cpp @@ -33,10 +33,7 @@ #define TYPENAME libzim_SuggestionSearch #include -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE METHOD(jobject, getResults, jint start, jint maxResults) { auto results = THIS->getResults(TO_C(start), TO_C(maxResults)); diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp index 8df8f3c..7e34c4f 100644 --- a/lib/src/main/cpp/libzim/suggestion_searcher.cpp +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -47,11 +47,7 @@ METHOD(void, setNativeSearcher, jobject archive) } } - -METHOD0(void, dispose) -{ - dispose(env, thisObj); -} +DISPOSE METHOD(jobject, suggest, jstring query) { return BUILD_WRAPPER("org/kiwix/libzim/SuggestionSearch", THIS->suggest(TO_C(query))); diff --git a/lib/src/main/cpp/macros.h b/lib/src/main/cpp/macros.h index 807ab34..bef647b 100644 --- a/lib/src/main/cpp/macros.h +++ b/lib/src/main/cpp/macros.h @@ -39,3 +39,4 @@ JNIEXPORT retType JNICALL BUILD_METHOD(TYPENAME ,name) ( \ return TO_JNI(THIS->name()); \ } +#define DISPOSE METHOD0(void, dispose) { dispose(env, thisObj); } From 28f0acde249e40e1912df8cbafdaed92bde439ed Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:12:15 +0100 Subject: [PATCH 20/32] Reorder methods' definitions. For better comparaison with the generated header. --- lib/src/main/cpp/libkiwix/book.cpp | 27 ++++++++++++++++++++++----- lib/src/main/cpp/libkiwix/library.cpp | 8 ++++---- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/lib/src/main/cpp/libkiwix/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp index b021565..a1fff5c 100644 --- a/lib/src/main/cpp/libkiwix/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -47,21 +47,43 @@ METHOD(void, update__Lorg_kiwix_libkiwix_JNIKiwixReader_2, jobject archive) } GETTER(jstring, getId) + GETTER(jstring, getPath) + GETTER(jboolean, isPathValid) + GETTER(jstring, getTitle) + GETTER(jstring, getDescription) + GETTER(jstring, getLanguage) + GETTER(jstring, getCreator) + GETTER(jstring, getPublisher) + GETTER(jstring, getDate) + GETTER(jstring, getUrl) + GETTER(jstring, getName) + GETTER(jstring, getFlavour) + GETTER(jstring, getCategory) + GETTER(jstring, getTags) + +METHOD(jstring, getTagStr, jstring tagName) try { + return TO_JNI(THIS->getTagStr(TO_C(tagName))); +} catch(...) { + return c2jni("", env); +} + GETTER(jlong, getArticleCount) + GETTER(jlong, getMediaCount) + GETTER(jlong, getSize) METHOD0(jobjectArray, getIllustrations) { @@ -78,8 +100,3 @@ METHOD0(jobjectArray, getIllustrations) { METHOD(jobject, getIllustration, jint size) { return BUILD_WRAPPER("org/kiwix/libkiwix/Illustration", THIS->getIllustration(TO_C(size))); } -METHOD(jstring, getTagStr, jstring tagName) try { - return TO_JNI(THIS->getTagStr(TO_C(tagName))); -} catch(...) { - return c2jni("", env); -} diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index 76dbb18..909ca01 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -70,13 +70,13 @@ METHOD(jint, getBookCount, jboolean localBooks, jboolean remoteBooks) { } GETTER(jobjectArray, getBooksIds) -GETTER(jobjectArray, getBooksLanguages) -GETTER(jobjectArray, getBooksCategories) -GETTER(jobjectArray, getBooksCreators) -GETTER(jobjectArray, getBooksPublishers) METHOD(jobjectArray, filter, jobject filterObj) { auto filter = getPtr(env, filterObj); return c2jni(THIS->filter(*filter), env); } +GETTER(jobjectArray, getBooksLanguages) +GETTER(jobjectArray, getBooksCategories) +GETTER(jobjectArray, getBooksCreators) +GETTER(jobjectArray, getBooksPublishers) From 1c963273b7eefa5489ed832bd9abeab8e4e015fe Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:18:17 +0100 Subject: [PATCH 21/32] Fix typos in Item.java. The cpp method name is `getMimetype`, not `getMimeType`. --- lib/src/main/java/org/kiwix/libzim/Item.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/org/kiwix/libzim/Item.java b/lib/src/main/java/org/kiwix/libzim/Item.java index 1c198fc..fa75778 100644 --- a/lib/src/main/java/org/kiwix/libzim/Item.java +++ b/lib/src/main/java/org/kiwix/libzim/Item.java @@ -25,7 +25,7 @@ public class Item { public native String getTitle(); public native String getPath(); - public native String getMimeType(); + public native String getMimetype(); public native Blob getData(); public native long getSize(); From 2ef64ac8b37645bae87e4f78539a131b368d8b8d Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:18:47 +0100 Subject: [PATCH 22/32] Use correct method in SearchIterator. --- lib/src/main/java/org/kiwix/libzim/SearchIterator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/java/org/kiwix/libzim/SearchIterator.java b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java index f623f15..318039e 100644 --- a/lib/src/main/java/org/kiwix/libzim/SearchIterator.java +++ b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java @@ -30,7 +30,7 @@ public class SearchIterator implements Iterator public native String getSnippet(); public native int getWordCount(); public native int getFileIndex(); - public native int size(); + public native int getSize(); public native String getZimId(); public native boolean hasNext(); From 897c78a718287a3eb45de4e2cf840270d00db26f Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:19:09 +0100 Subject: [PATCH 23/32] Add missing method in Book. --- lib/src/main/cpp/libkiwix/book.cpp | 2 ++ lib/src/main/java/org/kiwix/libkiwix/Book.java | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/src/main/cpp/libkiwix/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp index a1fff5c..ba69a74 100644 --- a/lib/src/main/cpp/libkiwix/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -50,6 +50,8 @@ GETTER(jstring, getId) GETTER(jstring, getPath) +GETTER(jstring, getHumanReadableIdFromPath) + GETTER(jboolean, isPathValid) GETTER(jstring, getTitle) diff --git a/lib/src/main/java/org/kiwix/libkiwix/Book.java b/lib/src/main/java/org/kiwix/libkiwix/Book.java index 5d3e042..a7ddc88 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/Book.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Book.java @@ -12,11 +12,13 @@ public class Book public native void update(Book book); public native void update(Archive archive); + @Override protected void finalize() { dispose(); } public native String getId(); public native String getPath(); + public native String getHumanReadableIdFromPath(); public native boolean isPathValid(); public native String getTitle(); public native String getDescription(); From 775b55c3b0e0b401e87fc83aa6504b38a90932ae Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:21:22 +0100 Subject: [PATCH 24/32] Fix wrong implementation in Book. As `update` is overloaded, the jni method name include the argument type. And we have changed the type of one version of `update`, so we need to change its name. --- lib/src/main/cpp/libkiwix/book.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/main/cpp/libkiwix/book.cpp b/lib/src/main/cpp/libkiwix/book.cpp index ba69a74..81ec03b 100644 --- a/lib/src/main/cpp/libkiwix/book.cpp +++ b/lib/src/main/cpp/libkiwix/book.cpp @@ -41,7 +41,7 @@ METHOD(void, update__Lorg_kiwix_libkiwix_Book_2, jobject otherBook) THIS->update(*getPtr(env, otherBook)); } -METHOD(void, update__Lorg_kiwix_libkiwix_JNIKiwixReader_2, jobject archive) +METHOD(void, update__Lorg_kiwix_libzim_Archive_2, jobject archive) { THIS->update(*getPtr(env, archive)); } From 6fd7849241c3db1a7ba6284248592dcede037124 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:22:51 +0100 Subject: [PATCH 25/32] Fix implementation of method in `Archive`. A lot of methods are overloaded, so we must include the argument type in the name. --- lib/src/main/cpp/libzim/archive.cpp | 39 +++++++++++++---------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index 856a1ea..bb49ee3 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -146,46 +146,42 @@ METHOD(jboolean, hasIllustration, jint size) { GETTER(jlongArray, getIllustrationSizes) -METHOD(jobject, getEntryByPath, jlong index) { - return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(index))); -} - -METHOD(jobject, getEntryByPath, jstring path) { +METHOD(jobject, getEntryByPath__Ljava_lang_String_2, jstring path) { return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(path))); } -METHOD(jobject, getEntryByTitle, jlong index) { - return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(index))); +METHOD(jobject, getEntryByPath__I, jint index) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByPath(TO_C(index))); } -METHOD(jobject, getEntryByTitle, jstring title) { - return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(title))); +METHOD(jboolean, hasEntryByPath, jstring path) { + return TO_JNI(THIS->hasEntryByPath(TO_C(path))); } -METHOD(jobject, getEntryByClusterOrder, jlong index) { - return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByClusterOrder(TO_C(index))); +METHOD(jobject, getEntryByTitle__Ljava_lang_String_2, jstring title) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(title))); } -METHOD0(jobject, getMainEntry) { - return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getMainEntry()); +METHOD(jobject, getEntryByTitle__I, jint index) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByTitle(TO_C(index))); } -METHOD0(jobject, getRandomEntry) { - return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getRandomEntry()); +METHOD(jboolean, hasEntryByTitle, jstring title) { + return TO_JNI(THIS->hasEntryByPath(TO_C(title))); } -METHOD(jboolean, hasEntryByPath, jstring path) { - return TO_JNI(THIS->hasEntryByPath(TO_C(path))); +METHOD(jobject, getEntryByClusterOrder, jint index) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getEntryByClusterOrder(TO_C(index))); } -METHOD(jboolean, hasEntryByTitle, jstring title) { - return TO_JNI(THIS->hasEntryByPath(TO_C(title))); +METHOD0(jobject, getMainEntry) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getMainEntry()); } GETTER(jboolean, hasMainEntry) -METHOD(jboolean, hasIllustration, jlong size) { - return TO_JNI(THIS->hasIllustration(TO_C(size))); +METHOD0(jobject, getRandomEntry) { + return BUILD_WRAPPER("org/kiwix/libzim/Entry", THIS->getRandomEntry()); } GETTER(jboolean, hasFulltextIndex) @@ -193,7 +189,6 @@ GETTER(jboolean, hasTitleIndex) GETTER(jboolean, hasChecksum) GETTER(jstring, getChecksum) GETTER(jboolean, check) - GETTER(jboolean, isMultiPart) GETTER(jboolean, hasNewNamespaceScheme) From 1dd170bd597ae03d2cfe38e7d63565748c08c266 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 15:56:01 +0100 Subject: [PATCH 26/32] Add missing implementation of Searcher setNativeSearcherMulti --- lib/src/main/cpp/libzim/searcher.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp index eaaff98..cbad5c3 100644 --- a/lib/src/main/cpp/libzim/searcher.cpp +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -47,7 +47,23 @@ METHOD(void, setNativeSearcher, jobject archive) } } - +METHOD(void, setNativeSearcherMulti, jobjectArray archives) +{ + std::vector cArchives; + jsize nbArchives = env->GetArrayLength(archives); + for(int i=0; iGetObjectArrayElement(archives, i); + auto cArchive = getPtr(env, archive); + cArchives.push_back(*cArchive); + } + try { + auto searcher = std::make_shared(cArchives); + SET_PTR(searcher); + } catch (std::exception& e) { + LOG("Cannot create searcher"); + LOG("%s", e.what()); + } +} DISPOSE From 150af85a7abec916e4afe2f829ff43522a2e460b Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 16:01:47 +0100 Subject: [PATCH 27/32] All java class wrapping a native object must call dispose at destruction. --- lib/src/main/java/org/kiwix/libkiwix/Server.java | 4 ++++ lib/src/main/java/org/kiwix/libzim/Archive.java | 3 +++ lib/src/main/java/org/kiwix/libzim/Blob.java | 7 ++++--- lib/src/main/java/org/kiwix/libzim/Entry.java | 5 ++--- lib/src/main/java/org/kiwix/libzim/EntryIterator.java | 4 ++++ lib/src/main/java/org/kiwix/libzim/Item.java | 5 ++--- lib/src/main/java/org/kiwix/libzim/Query.java | 4 ++++ lib/src/main/java/org/kiwix/libzim/Search.java | 4 ++++ lib/src/main/java/org/kiwix/libzim/SearchIterator.java | 4 ++++ lib/src/main/java/org/kiwix/libzim/Searcher.java | 4 ++++ lib/src/main/java/org/kiwix/libzim/SuggestionItem.java | 5 ++--- lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java | 4 ++++ lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java | 4 ++++ lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java | 4 ++++ 14 files changed, 49 insertions(+), 12 deletions(-) diff --git a/lib/src/main/java/org/kiwix/libkiwix/Server.java b/lib/src/main/java/org/kiwix/libkiwix/Server.java index 58a13f1..b8b5a78 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/Server.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Server.java @@ -45,6 +45,10 @@ public Server(Library library) setNativeServer(library); } + @Override + protected void finalize() { dispose(); } + + private native void setNativeServer(Library library); private native void dispose(); private long nativeHandle; diff --git a/lib/src/main/java/org/kiwix/libzim/Archive.java b/lib/src/main/java/org/kiwix/libzim/Archive.java index 86d4129..735cc79 100644 --- a/lib/src/main/java/org/kiwix/libzim/Archive.java +++ b/lib/src/main/java/org/kiwix/libzim/Archive.java @@ -104,6 +104,9 @@ public Archive(FileDescriptor fd, long offset, long size) private native void setNativeArchiveByFD(FileDescriptor fd); private native void setNativeArchiveEmbedded(FileDescriptor fd, long offset, long size); + @Override + protected void finalize() { dispose(); } + ///--------- The wrapper thing // To delete our native wrapper diff --git a/lib/src/main/java/org/kiwix/libzim/Blob.java b/lib/src/main/java/org/kiwix/libzim/Blob.java index fc8390a..93852f2 100644 --- a/lib/src/main/java/org/kiwix/libzim/Blob.java +++ b/lib/src/main/java/org/kiwix/libzim/Blob.java @@ -26,9 +26,10 @@ public class Blob public native String getData(); public native long size(); - protected void finalize() { - dispose(); - } + + @Override + protected void finalize() { dispose(); } + ///--------- The wrapper thing // To delete our native wrapper diff --git a/lib/src/main/java/org/kiwix/libzim/Entry.java b/lib/src/main/java/org/kiwix/libzim/Entry.java index 67b47f6..052a9df 100644 --- a/lib/src/main/java/org/kiwix/libzim/Entry.java +++ b/lib/src/main/java/org/kiwix/libzim/Entry.java @@ -31,9 +31,8 @@ public class Entry public native Item getRedirect(); public native Entry getRedirectEntry(); - protected void finalize() { - dispose(); - } + @Override + protected void finalize() { dispose(); } ///--------- The wrapper thing // To delete our native wrapper diff --git a/lib/src/main/java/org/kiwix/libzim/EntryIterator.java b/lib/src/main/java/org/kiwix/libzim/EntryIterator.java index 56aa6e3..b072de1 100644 --- a/lib/src/main/java/org/kiwix/libzim/EntryIterator.java +++ b/lib/src/main/java/org/kiwix/libzim/EntryIterator.java @@ -29,6 +29,10 @@ private EntryIterator(int order) { public native boolean hasNext(); public native Entry next(); + @Override + protected void finalize() { dispose(); } + + ///--------- The wrapper thing // To delete our native wrapper public native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libzim/Item.java b/lib/src/main/java/org/kiwix/libzim/Item.java index fa75778..58e89b3 100644 --- a/lib/src/main/java/org/kiwix/libzim/Item.java +++ b/lib/src/main/java/org/kiwix/libzim/Item.java @@ -30,9 +30,8 @@ public class Item public native Blob getData(); public native long getSize(); - protected void finalize() { - dispose(); - } + @Override + protected void finalize() { dispose(); } ///--------- The wrapper thing // To delete our native wrapper diff --git a/lib/src/main/java/org/kiwix/libzim/Query.java b/lib/src/main/java/org/kiwix/libzim/Query.java index 1a71408..75599b5 100644 --- a/lib/src/main/java/org/kiwix/libzim/Query.java +++ b/lib/src/main/java/org/kiwix/libzim/Query.java @@ -28,6 +28,10 @@ public Query(String query) { public native Query setQuery(String query); public native Query setGeorange(float latitude, float longitute, float distance); + @Override + protected void finalize() { dispose(); } + + ///--------- The wrapper thing // To delete our native wrapper public native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libzim/Search.java b/lib/src/main/java/org/kiwix/libzim/Search.java index 8901344..d078886 100644 --- a/lib/src/main/java/org/kiwix/libzim/Search.java +++ b/lib/src/main/java/org/kiwix/libzim/Search.java @@ -26,6 +26,10 @@ public class Search public native SearchIterator getResults(int start, int maxResults); public native long getEstimatedMatches(); + @Override + protected void finalize() { dispose(); } + + ///--------- The wrapper thing // To delete our native wrapper public native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libzim/SearchIterator.java b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java index 318039e..0a73912 100644 --- a/lib/src/main/java/org/kiwix/libzim/SearchIterator.java +++ b/lib/src/main/java/org/kiwix/libzim/SearchIterator.java @@ -36,6 +36,10 @@ public class SearchIterator implements Iterator public native boolean hasNext(); public native Entry next(); + + @Override + protected void finalize() { dispose(); } + ///--------- The wrapper thing // To delete our native wrapper public native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libzim/Searcher.java b/lib/src/main/java/org/kiwix/libzim/Searcher.java index 13dc73e..cd3c4d6 100644 --- a/lib/src/main/java/org/kiwix/libzim/Searcher.java +++ b/lib/src/main/java/org/kiwix/libzim/Searcher.java @@ -51,6 +51,10 @@ public Searcher(Archive[] archives) throws Exception private native void setNativeSearcher(Archive archive); private native void setNativeSearcherMulti(Archive[] archives); + @Override + protected void finalize() { dispose(); } + + ///--------- The wrapper thing // To delete our native wrapper public native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java b/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java index eef51a7..4021cd4 100644 --- a/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionItem.java @@ -26,9 +26,8 @@ public class SuggestionItem public native String getSnippet(); public native boolean hasSnippet(); - protected void finalize() { - dispose(); - } + @Override + protected void finalize() { dispose(); } ///--------- The wrapper thing // To delete our native wrapper diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java b/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java index 553974b..20d0298 100644 --- a/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionIterator.java @@ -27,6 +27,10 @@ public class SuggestionIterator implements Iterator public native boolean hasNext(); public native SuggestionItem next(); + @Override + protected void finalize() { dispose(); } + + ///--------- The wrapper thing // To delete our native wrapper public native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java b/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java index 427f425..5d2976b 100644 --- a/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionSearch.java @@ -26,6 +26,10 @@ public class SuggestionSearch public native SuggestionIterator getResults(int start, int maxResults); public native long getEstimatedMatches(); + + @Override + protected void finalize() { dispose(); } + ///--------- The wrapper thing // To delete our native wrapper public native void dispose(); diff --git a/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java b/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java index c455022..c45961e 100644 --- a/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java +++ b/lib/src/main/java/org/kiwix/libzim/SuggestionSearcher.java @@ -40,6 +40,10 @@ public SuggestionSearcher(Archive archive) throws Exception private native void setNativeSearcher(Archive archive); + + @Override + protected void finalize() { dispose(); } + ///--------- The wrapper thing // To delete our native wrapper public native void dispose(); From b2b7dad84f835b1d2d19a03c0897e0e830491330 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 16:12:02 +0100 Subject: [PATCH 28/32] Add missing `getDirectAccessInformation` in libzim.Item. --- lib/src/main/cpp/libzim/item.cpp | 10 ++++++++++ .../kiwix/{libkiwix => libzim}/DirectAccessInfo.java | 2 +- lib/src/main/java/org/kiwix/libzim/Item.java | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) rename lib/src/main/java/org/kiwix/{libkiwix => libzim}/DirectAccessInfo.java (96%) diff --git a/lib/src/main/cpp/libzim/item.cpp b/lib/src/main/cpp/libzim/item.cpp index e31b5c4..500797f 100644 --- a/lib/src/main/cpp/libzim/item.cpp +++ b/lib/src/main/cpp/libzim/item.cpp @@ -44,3 +44,13 @@ METHOD0(jobject, getData) { } GETTER(jlong, getSize) + + +METHOD0(jobject, getDirectAccessInformation) { + jobject directObjInfo = newObject("org/kiwix/libzim/DirectAccessInfo", env); + setDaiObjValue("", 0, directObjInfo, env); + + auto cDirectObjInfo = THIS->getDirectAccessInformation(); + setDaiObjValue(cDirectObjInfo.first, cDirectObjInfo.second, directObjInfo, env); + return directObjInfo; +} diff --git a/lib/src/main/java/org/kiwix/libkiwix/DirectAccessInfo.java b/lib/src/main/java/org/kiwix/libzim/DirectAccessInfo.java similarity index 96% rename from lib/src/main/java/org/kiwix/libkiwix/DirectAccessInfo.java rename to lib/src/main/java/org/kiwix/libzim/DirectAccessInfo.java index 157fb1d..61950a5 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/DirectAccessInfo.java +++ b/lib/src/main/java/org/kiwix/libzim/DirectAccessInfo.java @@ -17,7 +17,7 @@ * MA 02110-1301, USA. */ -package org.kiwix.libkiwix; +package org.kiwix.libzim; public class DirectAccessInfo { diff --git a/lib/src/main/java/org/kiwix/libzim/Item.java b/lib/src/main/java/org/kiwix/libzim/Item.java index 58e89b3..2b59b82 100644 --- a/lib/src/main/java/org/kiwix/libzim/Item.java +++ b/lib/src/main/java/org/kiwix/libzim/Item.java @@ -20,6 +20,7 @@ package org.kiwix.libzim; import org.kiwix.libzim.Blob; +import org.kiwix.libzim.DirectAccessInfo; public class Item { @@ -30,6 +31,8 @@ public class Item public native Blob getData(); public native long getSize(); + public native DirectAccessInfo getDirectAccessInformation(); + @Override protected void finalize() { dispose(); } From 06638d46b85710b4e95c44e60580e02d7a16a375 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Tue, 24 Jan 2023 17:10:08 +0100 Subject: [PATCH 29/32] Remove thread lock. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This make the JNI wrapping *somehow* NOT threadsafe. Few things are threadsafe "by nature": - A lot of native method in libzim are threadsafe. - Wrapping internal are threadsafe (shared_ptr). What is not threadsafe is accessing the SAME java object from different thread. But accessing the same wrapped cpp object using two different java wrapper in two different thread is ok (assuming that the called cpp method is threadsafe itself). --- lib/src/main/cpp/CMakeLists.txt | 1 - lib/src/main/cpp/common.cpp | 4 ---- lib/src/main/cpp/libkiwix/kiwixicu.cpp | 3 --- lib/src/main/cpp/libkiwix/kiwixserver.cpp | 1 - lib/src/main/cpp/libzim/archive.cpp | 3 --- lib/src/main/cpp/libzim/query.cpp | 1 - lib/src/main/cpp/libzim/searcher.cpp | 2 -- lib/src/main/cpp/libzim/suggestion_searcher.cpp | 2 -- lib/src/main/cpp/utils.h | 8 -------- 9 files changed, 25 deletions(-) delete mode 100644 lib/src/main/cpp/common.cpp diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt index d72816b..c335b10 100644 --- a/lib/src/main/cpp/CMakeLists.txt +++ b/lib/src/main/cpp/CMakeLists.txt @@ -10,7 +10,6 @@ add_library( zim_wrapper SHARED - common.cpp libzim/archive.cpp libzim/entry.cpp libzim/entry_iterator.cpp diff --git a/lib/src/main/cpp/common.cpp b/lib/src/main/cpp/common.cpp deleted file mode 100644 index f510798..0000000 --- a/lib/src/main/cpp/common.cpp +++ /dev/null @@ -1,4 +0,0 @@ - -#include - -std::mutex globalLock; diff --git a/lib/src/main/cpp/libkiwix/kiwixicu.cpp b/lib/src/main/cpp/libkiwix/kiwixicu.cpp index 3cc2a72..bc94cf6 100644 --- a/lib/src/main/cpp/libkiwix/kiwixicu.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixicu.cpp @@ -27,12 +27,9 @@ #include "utils.h" #include "zim/tools.h" -std::mutex globalLock; - JNIEXPORT void JNICALL Java_org_kiwix_kiwixlib_JNIICU_setDataDirectory( JNIEnv* env, jclass kclass, jstring dirStr) { - Lock l; try { zim::setICUDataDirectory(TO_C(dirStr)); } catch (...) { diff --git a/lib/src/main/cpp/libkiwix/kiwixserver.cpp b/lib/src/main/cpp/libkiwix/kiwixserver.cpp index e584adb..289e203 100644 --- a/lib/src/main/cpp/libkiwix/kiwixserver.cpp +++ b/lib/src/main/cpp/libkiwix/kiwixserver.cpp @@ -35,7 +35,6 @@ METHOD(void, setNativeServer, jobject jLibrary) { LOG("Attempting to create server"); - Lock l; try { auto library = getPtr(env, jLibrary); SET_PTR(std::make_shared(library.get())); diff --git a/lib/src/main/cpp/libzim/archive.cpp b/lib/src/main/cpp/libzim/archive.cpp index bb49ee3..3392977 100644 --- a/lib/src/main/cpp/libzim/archive.cpp +++ b/lib/src/main/cpp/libzim/archive.cpp @@ -41,7 +41,6 @@ METHOD(void, setNativeArchive, jstring filename) std::string cPath = TO_C(filename); LOG("Attempting to create reader with: %s", cPath.c_str()); - Lock l; try { auto archive = std::make_shared(cPath); SET_PTR(archive); @@ -78,7 +77,6 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD( int fd = jni2fd(fdObj, env); LOG("Attempting to create reader with fd: %d", fd); - Lock l; try { auto archive = std::make_shared(fd); SET_PTR(archive); @@ -99,7 +97,6 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded( int fd = jni2fd(fdObj, env); LOG("Attempting to create reader with fd: %d", fd); - Lock l; try { auto archive = std::make_shared(fd, offset, size); SET_PTR(archive); diff --git a/lib/src/main/cpp/libzim/query.cpp b/lib/src/main/cpp/libzim/query.cpp index 2578d66..6117b4d 100644 --- a/lib/src/main/cpp/libzim/query.cpp +++ b/lib/src/main/cpp/libzim/query.cpp @@ -36,7 +36,6 @@ METHOD(void, setNativeQuery, jstring query) { auto cQuery = TO_C(query); - Lock l; try { auto query = std::make_shared(cQuery); SET_PTR(query); diff --git a/lib/src/main/cpp/libzim/searcher.cpp b/lib/src/main/cpp/libzim/searcher.cpp index cbad5c3..9810ba2 100644 --- a/lib/src/main/cpp/libzim/searcher.cpp +++ b/lib/src/main/cpp/libzim/searcher.cpp @@ -35,8 +35,6 @@ METHOD(void, setNativeSearcher, jobject archive) { - - Lock l; auto cArchive = getPtr(env, archive); try { auto searcher = std::make_shared(*cArchive); diff --git a/lib/src/main/cpp/libzim/suggestion_searcher.cpp b/lib/src/main/cpp/libzim/suggestion_searcher.cpp index 7e34c4f..792447e 100644 --- a/lib/src/main/cpp/libzim/suggestion_searcher.cpp +++ b/lib/src/main/cpp/libzim/suggestion_searcher.cpp @@ -35,8 +35,6 @@ METHOD(void, setNativeSearcher, jobject archive) { - - Lock l; auto cArchive = getPtr(env, archive); try { auto searcher = std::make_shared(*cArchive); diff --git a/lib/src/main/cpp/utils.h b/lib/src/main/cpp/utils.h index 220a317..00529dd 100644 --- a/lib/src/main/cpp/utils.h +++ b/lib/src/main/cpp/utils.h @@ -37,7 +37,6 @@ #define LOG(...) #endif -extern std::mutex globalLock; using std::shared_ptr; // Here is the wrapping structure. @@ -131,13 +130,6 @@ inline jobject buildWrapper(JNIEnv* env, const char* class_name, T&& obj, const #define BUILD_WRAPPER(CLASSNAME, OBJ) buildWrapper(env, CLASSNAME, std::move(OBJ)) -// A mixin class which will lock the globalLock when a instance is created -// This avoid the cration of two instance inheriting from Lock in the same time. -class Lock : public std::unique_lock -{ - public: - Lock() : std::unique_lock(globalLock) { } -}; // --------------------------------------------------------------------------- // Convert things to JAVA From 7c95917a52e7b3af8ade834fb99b378c5f93e33c Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 1 Feb 2023 18:20:36 +0100 Subject: [PATCH 30/32] Add bookmark wrapper. --- lib/src/main/cpp/CMakeLists.txt | 1 + lib/src/main/cpp/libkiwix/bookmark.cpp | 72 +++++++++++++++++++ lib/src/main/cpp/libkiwix/library.cpp | 23 ++++++ .../java/org/kiwix/libkiwix/Bookmark.java | 53 ++++++++++++++ .../main/java/org/kiwix/libkiwix/Library.java | 6 ++ 5 files changed, 155 insertions(+) create mode 100644 lib/src/main/cpp/libkiwix/bookmark.cpp create mode 100644 lib/src/main/java/org/kiwix/libkiwix/Bookmark.java diff --git a/lib/src/main/cpp/CMakeLists.txt b/lib/src/main/cpp/CMakeLists.txt index c335b10..f768ff2 100644 --- a/lib/src/main/cpp/CMakeLists.txt +++ b/lib/src/main/cpp/CMakeLists.txt @@ -50,6 +50,7 @@ add_library( libkiwix/kiwixicu.cpp libkiwix/kiwixserver.cpp libkiwix/library.cpp + libkiwix/bookmark.cpp libkiwix/manager.cpp libkiwix/illustration.cpp ) diff --git a/lib/src/main/cpp/libkiwix/bookmark.cpp b/lib/src/main/cpp/libkiwix/bookmark.cpp new file mode 100644 index 0000000..f14300c --- /dev/null +++ b/lib/src/main/cpp/libkiwix/bookmark.cpp @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2020 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + + +#include +#include "org_kiwix_libkiwix_Bookmark.h" + +#include "utils.h" +#include "bookmark.h" + +#define NATIVE_TYPE kiwix::Bookmark +#define TYPENAME libkiwix_Bookmark +#include + +METHOD0(void, setNativeBookmark) +{ + SET_PTR(std::make_shared()); +} + +DISPOSE + +GETTER(jstring, getBookId) + +GETTER(jstring, getBookTitle) + +GETTER(jstring, getUrl) + +GETTER(jstring, getTitle) + +GETTER(jstring, getLanguage) + +GETTER(jstring, getDate) + +METHOD(void, setBookId, jstring bookId) { + THIS->setBookId(TO_C(bookId)); +} + +METHOD(void, setBookTitle, jstring bookTitle) { + THIS->setBookTitle(TO_C(bookTitle)); +} + +METHOD(void, setUrl, jstring url) { + THIS->setUrl(TO_C(url)); +} + +METHOD(void, setTitle, jstring title) { + THIS->setTitle(TO_C(title)); +} + +METHOD(void, setLanguage, jstring lang) { + THIS->setLanguage(TO_C(lang)); +} + +METHOD(void, setDate, jstring date) { + THIS->setDate(TO_C(date)); +} diff --git a/lib/src/main/cpp/libkiwix/library.cpp b/lib/src/main/cpp/libkiwix/library.cpp index 909ca01..e27e60d 100644 --- a/lib/src/main/cpp/libkiwix/library.cpp +++ b/lib/src/main/cpp/libkiwix/library.cpp @@ -64,6 +64,9 @@ METHOD(jboolean, removeBookById, jstring id) { METHOD(jboolean, writeToFile, jstring path) { return TO_JNI(THIS->writeToFile(TO_C(path))); } +METHOD(jboolean, writeBookmarksToFile, jstring path) { + return TO_JNI(THIS->writeBookmarksToFile(TO_C(path))); +} METHOD(jint, getBookCount, jboolean localBooks, jboolean remoteBooks) { return TO_JNI(THIS->getBookCount(TO_C(localBooks), TO_C(remoteBooks))); @@ -80,3 +83,23 @@ GETTER(jobjectArray, getBooksLanguages) GETTER(jobjectArray, getBooksCategories) GETTER(jobjectArray, getBooksCreators) GETTER(jobjectArray, getBooksPublishers) + +METHOD(void, addBookmark, jobject bookmark) { + auto cBookmark = getPtr(env, bookmark); + THIS->addBookmark(*cBookmark); +} + +METHOD(jboolean, removeBookmark, jstring zimId, jstring url) { + return TO_JNI(THIS->removeBookmark(TO_C(zimId), TO_C(url))); +} + +METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) { + auto bookmarks = THIS->getBookmarks(TO_C(onlyValidBookmarks)); + jobjectArray retArray = createArray(env, bookmarks.size(), "org/kiwix/libkiwix/Bookmark"); + size_t index = 0; + for (auto bookmark: bookmarks) { + auto wrapper = BUILD_WRAPPER("org/kiwix/libkiwx/Bookmark", bookmark); + env->SetObjectArrayElement(retArray, index++, wrapper); + } + return retArray; +} diff --git a/lib/src/main/java/org/kiwix/libkiwix/Bookmark.java b/lib/src/main/java/org/kiwix/libkiwix/Bookmark.java new file mode 100644 index 0000000..a6c1d4c --- /dev/null +++ b/lib/src/main/java/org/kiwix/libkiwix/Bookmark.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2022 Matthieu Gautier + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +package org.kiwix.libkiwix; + +public class Bookmark +{ + public Bookmark() { + setNativeBookmark(); + } + + public native void setBookId(String bookId); + public native void setBookTitle(String bookTitle); + public native void setUrl(String url); + public native void setTitle(String title); + public native void setLanguage(String language); + public native void setDate(String Date); + + public native String getBookId(); + public native String getBookTitle(); + public native String getUrl(); + public native String getTitle(); + public native String getLanguage(); + public native String getDate(); + + @Override + protected void finalize() { dispose(); } + + +///--------- The wrapper thing + // To delete our native wrapper + public native void dispose(); + + // A pointer (as a long) to a native Handle + private native void setNativeBookmark(); + private long nativeHandle; +} diff --git a/lib/src/main/java/org/kiwix/libkiwix/Library.java b/lib/src/main/java/org/kiwix/libkiwix/Library.java index 5d0b213..e9f4c47 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/Library.java +++ b/lib/src/main/java/org/kiwix/libkiwix/Library.java @@ -23,6 +23,7 @@ import org.kiwix.libzim.Searcher; import org.kiwix.libkiwix.Book; import org.kiwix.libkiwix.JNIKiwixException; +import org.kiwix.libkiwix.Bookmark; public class Library { @@ -41,6 +42,7 @@ public Library() public native boolean removeBookById(String id); public native boolean writeToFile(String path); + public native boolean writeBookmarksToFile(String path); public native int getBookCount(boolean localBooks, boolean remoteBooks); @@ -52,6 +54,10 @@ public Library() public native String[] getBooksCreators(); public native String[] getBooksPublishers(); + public native void addBookmark(Bookmark bookmark); + public native boolean removeBookmark(String zimId, String url); + public native Bookmark[] getBookmarks(boolean onlyValidBookmarks); + @Override protected void finalize() { dispose(); } private native void setNativeHandler(); From 12a68980605451a2ab6a2cc3d4d849cbc8770f35 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Fri, 3 Feb 2023 15:52:20 +0100 Subject: [PATCH 31/32] Rename back JNIKiwix.java_ to JNIKiwix.java --- .../java/org/kiwix/libkiwix/{JNIKiwix.java_ => JNIKiwix.java} | 1 + 1 file changed, 1 insertion(+) rename lib/src/main/java/org/kiwix/libkiwix/{JNIKiwix.java_ => JNIKiwix.java} (96%) diff --git a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java_ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java similarity index 96% rename from lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java_ rename to lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java index 2d63938..ea3c639 100644 --- a/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java_ +++ b/lib/src/main/java/org/kiwix/libkiwix/JNIKiwix.java @@ -28,6 +28,7 @@ public class JNIKiwix { public JNIKiwix(final Context context){ ReLinker.loadLibrary(context, "kiwix"); + ReLinker.loadLibrary(context, "zim"); } public void setDataDirectory(String icuDataDir) { From 929ae6d1c3bb841749628e26f7ff8a1cebeebb7e Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Thu, 9 Feb 2023 14:35:59 +0100 Subject: [PATCH 32/32] Explicitly list the java files for what we need to generate header. --- lib/build.gradle | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/lib/build.gradle b/lib/build.gradle index 667e59c..f4b5115 100644 --- a/lib/build.gradle +++ b/lib/build.gradle @@ -285,5 +285,35 @@ task checkCurrentJavaVersion() { task generateHeaderFilesFromJavaWrapper(type: Exec) { workingDir "${projectDir}/src/main/java/org/kiwix/" - commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ libzim/*.java libkiwix/*.java" + commandLine 'bash', '-c', "javac -h ${buildDir}/include/javah_generated/ -d ${buildDir}/libzim/ ${getLibzimFiles()} ${getLibkiwixFiles()}" +} + +String getLibkiwixFiles() { + return "${projectDir}/src/main/java/org/kiwix/libkiwix/Book.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Bookmark.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Filter.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/JNIICU.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Illustration.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/JNIKiwixException.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Library.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Manager.java " + + "${projectDir}/src/main/java/org/kiwix/libkiwix/Server.java" +} + +String getLibzimFiles() { + return "${projectDir}/src/main/java/org/kiwix/libzim/Archive.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Blob.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/DirectAccessInfo.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/EntryIterator.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Entry.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Item.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Query.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Searcher.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SearchIterator.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/Search.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionItem.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionIterator.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearcher.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/SuggestionSearch.java " + + "${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java" }