Skip to content

Commit 4aa8eee

Browse files
committed
Wrap Archive::iterByFoo and Archive::findByFoo
1 parent 3d2f308 commit 4aa8eee

5 files changed

Lines changed: 223 additions & 0 deletions

File tree

lib/src/main/cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ add_library(
1313
common.cpp
1414
libzim/archive.cpp
1515
libzim/entry.cpp
16+
libzim/entry_iterator.cpp
1617
libzim/item.cpp
1718
libzim/blob.cpp
1819
libzim/searcher.cpp

lib/src/main/cpp/libzim/archive.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,5 +223,65 @@ GETTER(jboolean, check)
223223
GETTER(jboolean, isMultiPart)
224224
GETTER(jboolean, hasNewNamespaceScheme)
225225

226+
#define ITER_BY_PATH 0
227+
#define ITER_BY_TITLE 1
228+
#define ITER_EFFICIENT 2
229+
METHOD0(jobject, Archive, iterByPath) {
230+
auto range = THIS->iterByPath();
231+
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
232+
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
233+
jobject obj = env->NewObject(objClass, initMethod, ITER_BY_PATH);
234+
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::pathOrder>, obj, range.begin());
235+
236+
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::pathOrder>>(range.end());
237+
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
238+
return obj;
239+
}
240+
241+
METHOD0(jobject, Archive, iterByTitle) {
242+
auto range = THIS->iterByTitle();
243+
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
244+
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
245+
jobject obj = env->NewObject(objClass, initMethod, ITER_BY_TITLE);
246+
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::titleOrder>, obj, range.begin());
247+
248+
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::titleOrder>>(range.end());
249+
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
250+
return obj;
251+
}
252+
253+
METHOD0(jobject, Archive, iterEfficient) {
254+
auto range = THIS->iterEfficient();
255+
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
256+
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
257+
jobject obj = env->NewObject(objClass, initMethod, ITER_EFFICIENT);
258+
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::efficientOrder>, obj, range.begin());
259+
260+
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::efficientOrder>>(range.end());
261+
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
262+
return obj;
263+
}
264+
265+
METHOD(jobject, Archive, findByPath, jstring path) {
266+
auto range = THIS->findByPath(TO_C(path));
267+
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
268+
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
269+
jobject obj = env->NewObject(objClass, initMethod, ITER_BY_PATH);
270+
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::pathOrder>, obj, range.begin());
271+
272+
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::pathOrder>>(range.end());
273+
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
274+
return obj;
275+
}
226276

277+
METHOD(jobject, Archive, findByTitle, jstring title) {
278+
auto range = THIS->findByTitle(TO_C(title));
279+
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
280+
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
281+
jobject obj = env->NewObject(objClass, initMethod, ITER_BY_TITLE);
282+
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::titleOrder>, obj, range.begin());
227283

284+
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::titleOrder>>(range.end());
285+
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
286+
return obj;
287+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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_EntryIterator.h"
24+
25+
#include <utils.h>
26+
27+
#include <string>
28+
29+
#include <zim/entry.h>
30+
#include <zim/search.h>
31+
32+
#define PATH_NATIVE_TYPE zim::Archive::iterator<zim::EntryOrder::pathOrder>
33+
#define TITLE_NATIVE_TYPE zim::Archive::iterator<zim::EntryOrder::titleOrder>
34+
#define EFFICIENT_NATIVE_TYPE zim::Archive::iterator<zim::EntryOrder::efficientOrder>
35+
36+
inline int get_order(JNIEnv* env, jobject thisObj) {
37+
jclass thisClass = env->GetObjectClass(thisObj);
38+
jfieldID fieldId = env->GetFieldID(thisClass, "order", "I");
39+
return TO_C(env->GetIntField(thisObj, fieldId));
40+
}
41+
42+
JNIEXPORT void JNICALL
43+
Java_org_kiwix_kiwixlib_libzim_EntryIterotar_dispose(JNIEnv* env, jobject thisObj)
44+
{
45+
// Delete end iterator
46+
switch (get_order(env, thisObj)) {
47+
case 0:
48+
dispose<PATH_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
49+
dispose<PATH_NATIVE_TYPE>(env, thisObj);
50+
break;
51+
case 1:
52+
dispose<TITLE_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
53+
dispose<TITLE_NATIVE_TYPE>(env, thisObj);
54+
break;
55+
case 2:
56+
dispose<EFFICIENT_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
57+
dispose<EFFICIENT_NATIVE_TYPE>(env, thisObj);
58+
break;
59+
}
60+
}
61+
62+
63+
METHOD0(jboolean, EntryIterator, hasNext) {
64+
switch (get_order(env, thisObj)) {
65+
case 0: {
66+
PATH_NATIVE_TYPE next(*GET_PTR(PATH_NATIVE_TYPE));
67+
next++;
68+
auto end = getPtr<PATH_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
69+
return next == *end;
70+
}
71+
case 1: {
72+
TITLE_NATIVE_TYPE next(*GET_PTR(TITLE_NATIVE_TYPE));
73+
next++;
74+
auto end = getPtr<TITLE_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
75+
return next == *end;
76+
}
77+
case 2: {
78+
EFFICIENT_NATIVE_TYPE next(*GET_PTR(EFFICIENT_NATIVE_TYPE));
79+
next++;
80+
auto end = getPtr<EFFICIENT_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
81+
return next == *end;
82+
}
83+
}
84+
}
85+
86+
METHOD0(jobject, EntryIterator, next) {
87+
switch (get_order(env, thisObj)) {
88+
case 0: {
89+
(*GET_PTR(PATH_NATIVE_TYPE))++;
90+
zim::Entry entry = **GET_PTR(PATH_NATIVE_TYPE);
91+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
92+
SET_HANDLE(zim::Entry, obj, entry);
93+
return obj;
94+
}
95+
case 1: {
96+
(*GET_PTR(TITLE_NATIVE_TYPE))++;
97+
zim::Entry entry = **GET_PTR(TITLE_NATIVE_TYPE);
98+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
99+
SET_HANDLE(zim::Entry, obj, entry);
100+
return obj;
101+
}
102+
case 2: {
103+
(*GET_PTR(EFFICIENT_NATIVE_TYPE))++;
104+
zim::Entry entry = **GET_PTR(EFFICIENT_NATIVE_TYPE);
105+
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
106+
SET_HANDLE(zim::Entry, obj, entry);
107+
return obj;
108+
}
109+
}
110+
}
111+

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.kiwix.libzim.ZimFileFormatException;
2323
import org.kiwix.libzim.Entry;
2424
import org.kiwix.libzim.Item;
25+
import org.kiwix.libzim.EntryIterator;
2526
import java.io.FileDescriptor;
2627

2728
public class Archive
@@ -92,6 +93,12 @@ public Archive(FileDescriptor fd, long offset, long size)
9293
public native boolean isMultiPart();
9394
public native boolean hasNewNamespaceScheme();
9495

96+
public native EntryIterator iterByPath();
97+
public native EntryIterator iterByTitle();
98+
public native EntryIterator iterEfficient();
99+
public native EntryIterator findByPath(String path);
100+
public native EntryIterator findByTitle(String path);
101+
95102

96103
private native void setNativeArchive(String filename);
97104
private native void setNativeArchiveByFD(FileDescriptor fd);
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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 java.util.Iterator;
23+
24+
public class EntryIterator implements Iterator<Entry>
25+
{
26+
private EntryIterator(int order) {
27+
this.order = order;
28+
}
29+
public native boolean hasNext();
30+
public native Entry next();
31+
32+
///--------- The wrapper thing
33+
// To delete our native wrapper
34+
public native void dispose();
35+
36+
// A marker of the order used for this iterator
37+
private int order;
38+
39+
// A pointer (as a long) to a native Handle
40+
private long nativeHandle;
41+
42+
// A pointer (as a long) to the native end
43+
private long nativeHandleEnd;
44+
}

0 commit comments

Comments
 (0)