From 9b971ce4dcb049f097e5c107cf51f2c43d35c1ba Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 11 Jan 2023 16:17:31 +0100 Subject: [PATCH] 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 @@ package org.kiwix.libzim; 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 @@ import java.io.FileDescriptor; 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 class Searcher 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