Skip to content

Commit c32bc00

Browse files
committed
Add wrapping around Search
1 parent 73cab14 commit c32bc00

11 files changed

+303
-53
lines changed

lib/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -285,5 +285,5 @@ task checkCurrentJavaVersion() {
285285

286286
task generateHeaderFilesFromJavaWrapper(type: Exec) {
287287
workingDir "${projectDir}/src/main/java/org/kiwix/"
288-
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"
288+
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"
289289
}

lib/src/main/cpp/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ add_library(
1515
libzim/entry.cpp
1616
libzim/item.cpp
1717
libzim/blob.cpp
18+
libzim/searcher.cpp
19+
libzim/query.cpp
20+
libzim/search.cpp
21+
libzim/search_iterator.cpp
1822
)
1923

2024
find_library(libzim

lib/src/main/cpp/libzim/query.cpp

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright (C) 2013 Emmanuel Engelhart <[email protected]>
3+
* Copyright (C) 2017 Matthieu Gautier <[email protected]>
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_Searcher.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/search.h>
30+
31+
#define NATIVE_TYPE zim::Query
32+
33+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Query_getNativeQuery(
34+
JNIEnv* env, jobject thisObj, jstring query)
35+
{
36+
auto cQuery = TO_C(query);
37+
Lock l;
38+
try {
39+
auto query = std::make_shared<NATIVE_TYPE>(cQuery);
40+
SET_PTR(query);
41+
} catch (std::exception& e) {
42+
LOG("Cannot create query");
43+
LOG("%s", e.what());
44+
}
45+
}
46+
47+
48+
JNIEXPORT void JNICALL
49+
Java_org_kiwix_kiwixlib_libzim_Query_dispose(JNIEnv* env, jobject thisObj)
50+
{
51+
dispose<NATIVE_TYPE>(env, thisObj);
52+
}
53+
54+
#define THIS GET_PTR(NATIVE_TYPE)
55+
56+
METHOD(jobject, Query, setQuery, jstring query) {
57+
THIS->setQuery(TO_C(query));
58+
return thisObj;
59+
}
60+
61+
METHOD(jobject, Query, setGeorange, jfloat latitude, jfloat longitude, jfloat distance) {
62+
THIS->setGeorange(TO_C(latitude), TO_C(longitude), TO_C(distance));
63+
return thisObj;
64+
}
65+

lib/src/main/cpp/libzim/search.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (C) 2013 Emmanuel Engelhart <[email protected]>
3+
* Copyright (C) 2017 Matthieu Gautier <[email protected]>
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_Search.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/search.h>
30+
31+
#define NATIVE_TYPE zim::Search
32+
33+
JNIEXPORT void JNICALL
34+
Java_org_kiwix_kiwixlib_libzim_Search_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, Search, getResults, jint start, jint maxResults) {
42+
auto results = THIS->getResults(TO_C(start), TO_C(maxResults));
43+
auto obj = NEW_OBJECT("ork/kiwix/libzim/SearchIterator");
44+
SET_HANDLE(zim::SearchIterator, 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::SearchIterator>(results.end());
48+
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
49+
return obj;
50+
}
51+
52+
METHOD0(jlong, Search, getEstimatedMatches) {
53+
return TO_JNI(THIS->getEstimatedMatches());
54+
}
55+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (C) 2013 Emmanuel Engelhart <[email protected]>
3+
* Copyright (C) 2017 Matthieu Gautier <[email protected]>
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_SearchIterator.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/entry.h>
30+
#include <zim/search.h>
31+
32+
#define NATIVE_TYPE zim::SearchIterator
33+
34+
JNIEXPORT void JNICALL
35+
Java_org_kiwix_kiwixlib_libzim_SearchIterotar_dispose(JNIEnv* env, jobject thisObj)
36+
{
37+
// Delete end iterator
38+
dispose<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
39+
dispose<NATIVE_TYPE>(env, thisObj);
40+
}
41+
42+
#define THIS GET_PTR(NATIVE_TYPE)
43+
#define GETTER(retType, name) JNIEXPORT retType JNICALL \
44+
Java_org_kiwix_libzim_SearchIterator__##name (JNIEnv* env, jobject thisObj) \
45+
{ \
46+
return TO_JNI(THIS->name()); \
47+
}
48+
49+
GETTER(jstring, getPath)
50+
GETTER(jstring, getTitle)
51+
GETTER(jint, getScore)
52+
GETTER(jstring, getSnippet)
53+
GETTER(jint, getWordCount)
54+
GETTER(jint, getFileIndex)
55+
GETTER(jint, getSize)
56+
57+
METHOD0(jstring, SearchIterator, getZimId) {
58+
return TO_JNI(std::string(THIS->getZimId()));
59+
}
60+
61+
METHOD0(jboolean, SearchIterator, hasNext) {
62+
zim::SearchIterator next(*THIS);
63+
next++;
64+
auto end = getPtr<NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
65+
return next == *end;
66+
}
67+
68+
METHOD0(jobject, SearchIterator, next) {
69+
(*THIS)++;
70+
zim::Entry entry = **THIS;
71+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
72+
SET_HANDLE(zim::Entry, obj, entry);
73+
return obj;
74+
}
75+

lib/src/main/cpp/libzim/searcher.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2013 Emmanuel Engelhart <[email protected]>
3+
* Copyright (C) 2017 Matthieu Gautier <[email protected]>
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_Searcher.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/search.h>
30+
31+
#define NATIVE_TYPE zim::Searcher
32+
33+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Searcher_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::Searcher>(*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_Searcher_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_Searcher__##name (JNIEnv* env, jobject thisObj) \
58+
{ \
59+
return TO_JNI(THIS->name()); \
60+
}
61+
62+
METHOD(jobject, Searcher, addArchive, jobject archive) {
63+
auto cArchive = getPtr<zim::Archive>(env, archive);
64+
THIS->addArchive(*cArchive);
65+
return thisObj;
66+
}
67+
68+
METHOD(jobject, Searcher, search, jobject query) {
69+
auto cQuery = getPtr<zim::Query>(env, query);
70+
auto obj = NEW_OBJECT("org/kiwix/libzim/Search");
71+
SET_HANDLE(zim::Search, obj, THIS->search(*cQuery));
72+
return obj;
73+
}
74+
75+
METHOD(void, Searcher, setVerbose, jboolean verbose) {
76+
THIS->setVerbose(TO_C(verbose));
77+
}
78+

lib/src/main/java/org/kiwix/libzim/Query.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,18 @@
2121

2222
public class Query
2323
{
24-
public Query(string query);
25-
public native Query setQuery(string query);
24+
public Query(String query) {
25+
setNativeQuery(query);
26+
}
27+
28+
public native Query setQuery(String query);
2629
public native Query setGeorange(float latitude, float longitute, float distance);
2730

2831
///--------- The wrapper thing
2932
// To delete our native wrapper
3033
public native void dispose();
3134

3235
// A pointer (as a long) to a native Handle
36+
private native long setNativeQuery(String query);
3337
private long nativeHandle;
3438
}

lib/src/main/java/org/kiwix/libzim/Search.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
package org.kiwix.libzim;
2121

22-
import org.kiwix.libzim.SearchResultSet;
22+
import org.kiwix.libzim.SearchIterator;
2323

2424
public class Search
2525
{
26-
public native SearchResultSet getResults(int start, int maxResults);
26+
public native SearchIterator getResults(int start, int maxResults);
2727
public native long getEstimatedMatches();
2828

2929
///--------- The wrapper thing

lib/src/main/java/org/kiwix/libzim/SearchIterator.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
package org.kiwix.libzim;
2121

2222
import org.kiwix.libzim.SearchIterator;
23+
import java.util.Iterator;
2324

24-
public class SearchIterator implement Iterator<Entry>
25+
public class SearchIterator implements Iterator<Entry>
2526
{
26-
public native string getPath();
27-
public native string getTitle();
27+
public native String getPath();
28+
public native String getTitle();
2829
public native int getScore();
29-
public native string getSnippet();
30+
public native String getSnippet();
3031
public native int getWordCount();
3132
public native int getFileIndex();
3233
public native int size();
33-
public native string getZimId();
34+
public native String getZimId();
3435

3536
public native boolean hasNext();
3637
public native Entry next();
@@ -41,4 +42,7 @@ public class SearchIterator implement Iterator<Entry>
4142

4243
// A pointer (as a long) to a native Handle
4344
private long nativeHandle;
45+
46+
// A pointer (as a long) to the native end
47+
private long nativeHandleEnd;
4448
}

lib/src/main/java/org/kiwix/libzim/SearchResultSet.java

-35
This file was deleted.

0 commit comments

Comments
 (0)