Add wrapping around Search

This commit is contained in:
Matthieu Gautier 2023-01-11 16:17:31 +01:00
parent 7887202eb0
commit 9b971ce4dc
11 changed files with 303 additions and 53 deletions

View File

@ -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"
}

View File

@ -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

View File

@ -0,0 +1,65 @@
/*
* Copyright (C) 2013 Emmanuel Engelhart <kelson@kiwix.org>
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
*
* 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 <jni.h>
#include <exception>
#include "org_kiwix_libzim_Searcher.h"
#include <utils.h>
#include <string>
#include <zim/search.h>
#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<NATIVE_TYPE>(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<NATIVE_TYPE>(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;
}

View File

@ -0,0 +1,55 @@
/*
* Copyright (C) 2013 Emmanuel Engelhart <kelson@kiwix.org>
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
*
* 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 <jni.h>
#include <exception>
#include "org_kiwix_libzim_Search.h"
#include <utils.h>
#include <string>
#include <zim/search.h>
#define NATIVE_TYPE zim::Search
JNIEXPORT void JNICALL
Java_org_kiwix_kiwixlib_libzim_Search_dispose(JNIEnv* env, jobject thisObj)
{
dispose<NATIVE_TYPE>(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<zim::SearchIterator>(results.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
METHOD0(jlong, Search, getEstimatedMatches) {
return TO_JNI(THIS->getEstimatedMatches());
}

View File

@ -0,0 +1,75 @@
/*
* Copyright (C) 2013 Emmanuel Engelhart <kelson@kiwix.org>
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
*
* 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 <jni.h>
#include <exception>
#include "org_kiwix_libzim_SearchIterator.h"
#include <utils.h>
#include <string>
#include <zim/entry.h>
#include <zim/search.h>
#define NATIVE_TYPE zim::SearchIterator
JNIEXPORT void JNICALL
Java_org_kiwix_kiwixlib_libzim_SearchIterotar_dispose(JNIEnv* env, jobject thisObj)
{
// Delete end iterator
dispose<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
dispose<NATIVE_TYPE>(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<NATIVE_TYPE>(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;
}

View File

@ -0,0 +1,78 @@
/*
* Copyright (C) 2013 Emmanuel Engelhart <kelson@kiwix.org>
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
*
* 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 <jni.h>
#include <exception>
#include "org_kiwix_libzim_Searcher.h"
#include <utils.h>
#include <string>
#include <zim/search.h>
#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<zim::Archive>(env, archive);
try {
auto searcher = std::make_shared<zim::Searcher>(*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<NATIVE_TYPE>(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<zim::Archive>(env, archive);
THIS->addArchive(*cArchive);
return thisObj;
}
METHOD(jobject, Searcher, search, jobject query) {
auto cQuery = getPtr<zim::Query>(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));
}

View File

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

View File

@ -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

View File

@ -20,17 +20,18 @@
package org.kiwix.libzim;
import org.kiwix.libzim.SearchIterator;
import java.util.Iterator;
public class SearchIterator implement Iterator<Entry>
public class SearchIterator implements Iterator<Entry>
{
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<Entry>
// A pointer (as a long) to a native Handle
private long nativeHandle;
// A pointer (as a long) to the native end
private long nativeHandleEnd;
}

View File

@ -1,35 +0,0 @@
/*
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
*
* 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;
}

View File

@ -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<Archive> 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<Archive> archives);
private native void setNativeSearcher(Archive archive);
private native void setNativeSearcherMulti(Archive[] archives);
///--------- The wrapper thing
// To delete our native wrapper