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; +}