Wrap suggestion searcher.

This commit is contained in:
Matthieu Gautier 2023-01-17 15:14:26 +01:00
parent f3ac28f56a
commit 3d2f3083b0
9 changed files with 404 additions and 0 deletions

View File

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

View File

@ -0,0 +1,50 @@
/*
* 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_SuggestionItem.h"
#include <utils.h>
#include <string>
#include <zim/suggestion.h>
#define NATIVE_TYPE zim::SuggestionItem
JNIEXPORT void JNICALL
Java_org_kiwix_kiwixlib_libzim_SuggestionItem_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_SuggestionItem__##name (JNIEnv* env, jobject thisObj) \
{ \
return TO_JNI(THIS->name()); \
}
GETTER(jstring, getTitle)
GETTER(jstring, getPath)
GETTER(jstring, getSnippet)
GETTER(jboolean, hasSnippet)

View File

@ -0,0 +1,62 @@
/*
* 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_SuggestionIterator.h"
#include <utils.h>
#include <string>
#include <zim/suggestion.h>
#define NATIVE_TYPE zim::SuggestionIterator
JNIEXPORT void JNICALL
Java_org_kiwix_kiwixlib_libzim_SuggestionIterator_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_SuggestionIterator__##name (JNIEnv* env, jobject thisObj) \
{ \
return TO_JNI(THIS->name()); \
}
METHOD0(jboolean, SearchIterator, hasNext) {
NATIVE_TYPE next(*THIS);
next++;
auto end = getPtr<NATIVE_TYPE>(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;
}

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_SuggestionSearch.h"
#include <utils.h>
#include <string>
#include <zim/suggestion.h>
#define NATIVE_TYPE zim::SuggestionSearch
JNIEXPORT void JNICALL
Java_org_kiwix_kiwixlib_libzim_SuggestionSearch_dispose(JNIEnv* env, jobject thisObj)
{
dispose<NATIVE_TYPE>(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<zim::SuggestionIterator>(results.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
METHOD0(jlong, SuggestionSearch, getEstimatedMatches) {
return TO_JNI(THIS->getEstimatedMatches());
}

View File

@ -0,0 +1,71 @@
/*
* 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_SuggestionSearcher.h"
#include <utils.h>
#include <string>
#include <zim/suggestion.h>
#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<zim::Archive>(env, archive);
try {
auto searcher = std::make_shared<zim::SuggestionSearcher>(*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<NATIVE_TYPE>(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));
}

View File

@ -0,0 +1,39 @@
/*
* 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;
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;
}

View File

@ -0,0 +1,39 @@
/*
* 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.SuggestionItem;
import java.util.Iterator;
public class SuggestionIterator implements Iterator<SuggestionItem>
{
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;
}

View File

@ -0,0 +1,35 @@
/*
* 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.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;
}

View File

@ -0,0 +1,49 @@
/*
* 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.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;
}