Skip to content

Commit 3d2f308

Browse files
committed
Wrap suggestion searcher.
1 parent f3ac28f commit 3d2f308

9 files changed

Lines changed: 404 additions & 0 deletions

lib/src/main/cpp/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ add_library(
1919
libzim/query.cpp
2020
libzim/search.cpp
2121
libzim/search_iterator.cpp
22+
libzim/suggestion_searcher.cpp
23+
libzim/suggestion_search.cpp
24+
libzim/suggestion_iterator.cpp
25+
libzim/suggestion_item.cpp
2226
)
2327

2428
find_library(libzim
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (C) 2013 Emmanuel Engelhart <kelson@kiwix.org>
3+
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 3 of the License, or
8+
* any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
#include <jni.h>
22+
#include <exception>
23+
#include "org_kiwix_libzim_SuggestionItem.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/suggestion.h>
30+
31+
#define NATIVE_TYPE zim::SuggestionItem
32+
33+
JNIEXPORT void JNICALL
34+
Java_org_kiwix_kiwixlib_libzim_SuggestionItem_dispose(JNIEnv* env, jobject thisObj)
35+
{
36+
dispose<NATIVE_TYPE>(env, thisObj);
37+
}
38+
39+
#define THIS GET_PTR(NATIVE_TYPE)
40+
#define GETTER(retType, name) JNIEXPORT retType JNICALL \
41+
Java_org_kiwix_libzim_SuggestionItem__##name (JNIEnv* env, jobject thisObj) \
42+
{ \
43+
return TO_JNI(THIS->name()); \
44+
}
45+
46+
47+
GETTER(jstring, getTitle)
48+
GETTER(jstring, getPath)
49+
GETTER(jstring, getSnippet)
50+
GETTER(jboolean, hasSnippet)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright (C) 2013 Emmanuel Engelhart <kelson@kiwix.org>
3+
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 3 of the License, or
8+
* any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
#include <jni.h>
22+
#include <exception>
23+
#include "org_kiwix_libzim_SuggestionIterator.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/suggestion.h>
30+
31+
#define NATIVE_TYPE zim::SuggestionIterator
32+
33+
JNIEXPORT void JNICALL
34+
Java_org_kiwix_kiwixlib_libzim_SuggestionIterator_dispose(JNIEnv* env, jobject thisObj)
35+
{
36+
// Delete end iterator
37+
dispose<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
38+
dispose<NATIVE_TYPE>(env, thisObj);
39+
}
40+
41+
#define THIS GET_PTR(NATIVE_TYPE)
42+
#define GETTER(retType, name) JNIEXPORT retType JNICALL \
43+
Java_org_kiwix_libzim_SuggestionIterator__##name (JNIEnv* env, jobject thisObj) \
44+
{ \
45+
return TO_JNI(THIS->name()); \
46+
}
47+
48+
METHOD0(jboolean, SearchIterator, hasNext) {
49+
NATIVE_TYPE next(*THIS);
50+
next++;
51+
auto end = getPtr<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
52+
return next == *end;
53+
}
54+
55+
METHOD0(jobject, SearchIterator, next) {
56+
(*THIS)++;
57+
zim::SuggestionItem item = **THIS;
58+
auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionItem");
59+
SET_HANDLE(zim::SuggestionItem, obj, item);
60+
return obj;
61+
}
62+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2013 Emmanuel Engelhart <kelson@kiwix.org>
3+
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 3 of the License, or
8+
* any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
#include <jni.h>
22+
#include <exception>
23+
#include "org_kiwix_libzim_SuggestionSearch.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/suggestion.h>
30+
31+
#define NATIVE_TYPE zim::SuggestionSearch
32+
33+
JNIEXPORT void JNICALL
34+
Java_org_kiwix_kiwixlib_libzim_SuggestionSearch_dispose(JNIEnv* env, jobject thisObj)
35+
{
36+
dispose<NATIVE_TYPE>(env, thisObj);
37+
}
38+
39+
#define THIS GET_PTR(NATIVE_TYPE)
40+
41+
METHOD(jobject, SuggestionSearch, getResults, jint start, jint maxResults) {
42+
auto results = THIS->getResults(TO_C(start), TO_C(maxResults));
43+
auto obj = NEW_OBJECT("ork/kiwix/libzim/SuggestionIterator");
44+
SET_HANDLE(zim::SuggestionIterator, obj, results.begin());
45+
46+
// We have to set the nativeHandleEnd but no macro ease our work here.
47+
auto end_ptr = std::make_shared<zim::SuggestionIterator>(results.end());
48+
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
49+
return obj;
50+
}
51+
52+
METHOD0(jlong, SuggestionSearch, getEstimatedMatches) {
53+
return TO_JNI(THIS->getEstimatedMatches());
54+
}
55+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright (C) 2013 Emmanuel Engelhart <kelson@kiwix.org>
3+
* Copyright (C) 2017 Matthieu Gautier <mgautier@kymeria.fr>
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 3 of the License, or
8+
* any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
#include <jni.h>
22+
#include <exception>
23+
#include "org_kiwix_libzim_SuggestionSearcher.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/suggestion.h>
30+
31+
#define NATIVE_TYPE zim::SuggestionSearcher
32+
33+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_SuggestionSearcher_setNativeSearcher(
34+
JNIEnv* env, jobject thisObj, jobject archive)
35+
{
36+
37+
Lock l;
38+
auto cArchive = getPtr<zim::Archive>(env, archive);
39+
try {
40+
auto searcher = std::make_shared<zim::SuggestionSearcher>(*cArchive);
41+
SET_PTR(searcher);
42+
} catch (std::exception& e) {
43+
LOG("Cannot create searcher");
44+
LOG("%s", e.what());
45+
}
46+
}
47+
48+
49+
JNIEXPORT void JNICALL
50+
Java_org_kiwix_kiwixlib_libzim_SuggestionSearcher_dispose(JNIEnv* env, jobject thisObj)
51+
{
52+
dispose<NATIVE_TYPE>(env, thisObj);
53+
}
54+
55+
#define THIS GET_PTR(NATIVE_TYPE)
56+
#define GETTER(retType, name) JNIEXPORT retType JNICALL \
57+
Java_org_kiwix_libzim_SuggestionSearcher__##name (JNIEnv* env, jobject thisObj) \
58+
{ \
59+
return TO_JNI(THIS->name()); \
60+
}
61+
62+
METHOD(jobject, SuggestionSearcher, suggest, jstring query) {
63+
auto obj = NEW_OBJECT("org/kiwix/libzim/SuggestionSearch");
64+
SET_HANDLE(zim::SuggestionSearch, obj, THIS->suggest(TO_C(query)));
65+
return obj;
66+
}
67+
68+
METHOD(void, SuggestionSearcher, setVerbose, jboolean verbose) {
69+
THIS->setVerbose(TO_C(verbose));
70+
}
71+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301, USA.
18+
*/
19+
20+
package org.kiwix.libzim;
21+
22+
public class SuggestionItem
23+
{
24+
public native String getTitle();
25+
public native String getPath();
26+
public native String getSnippet();
27+
public native boolean hasSnippet();
28+
29+
protected void finalize() {
30+
dispose();
31+
}
32+
33+
///--------- The wrapper thing
34+
// To delete our native wrapper
35+
private native void dispose();
36+
37+
// A pointer (as a long) to a native Handle
38+
private long nativeHandle;
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301, USA.
18+
*/
19+
20+
package org.kiwix.libzim;
21+
22+
import org.kiwix.libzim.SuggestionItem;
23+
import java.util.Iterator;
24+
25+
public class SuggestionIterator implements Iterator<SuggestionItem>
26+
{
27+
public native boolean hasNext();
28+
public native SuggestionItem next();
29+
30+
///--------- The wrapper thing
31+
// To delete our native wrapper
32+
public native void dispose();
33+
34+
// A pointer (as a long) to a native Handle
35+
private long nativeHandle;
36+
37+
// A pointer (as a long) to the native end
38+
private long nativeHandleEnd;
39+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (C) 2022 Matthieu Gautier <mgautier@kymeria.fr>
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 3 of the License, or
7+
* any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301, USA.
18+
*/
19+
20+
package org.kiwix.libzim;
21+
22+
import org.kiwix.libzim.SuggestionIterator;
23+
24+
public class SuggestionSearch
25+
{
26+
public native SuggestionIterator getResults(int start, int maxResults);
27+
public native long getEstimatedMatches();
28+
29+
///--------- The wrapper thing
30+
// To delete our native wrapper
31+
public native void dispose();
32+
33+
// A pointer (as a long) to a native Handle
34+
private long nativeHandle;
35+
}

0 commit comments

Comments
 (0)