Wrap Archive::iterByFoo and Archive::findByFoo

This commit is contained in:
Matthieu Gautier 2023-01-17 16:37:42 +01:00
parent 3d2f3083b0
commit 4aa8eeedfb
5 changed files with 223 additions and 0 deletions

View File

@ -13,6 +13,7 @@ add_library(
common.cpp
libzim/archive.cpp
libzim/entry.cpp
libzim/entry_iterator.cpp
libzim/item.cpp
libzim/blob.cpp
libzim/searcher.cpp

View File

@ -223,5 +223,65 @@ GETTER(jboolean, check)
GETTER(jboolean, isMultiPart)
GETTER(jboolean, hasNewNamespaceScheme)
#define ITER_BY_PATH 0
#define ITER_BY_TITLE 1
#define ITER_EFFICIENT 2
METHOD0(jobject, Archive, iterByPath) {
auto range = THIS->iterByPath();
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
jobject obj = env->NewObject(objClass, initMethod, ITER_BY_PATH);
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::pathOrder>, obj, range.begin());
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::pathOrder>>(range.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
METHOD0(jobject, Archive, iterByTitle) {
auto range = THIS->iterByTitle();
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
jobject obj = env->NewObject(objClass, initMethod, ITER_BY_TITLE);
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::titleOrder>, obj, range.begin());
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::titleOrder>>(range.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
METHOD0(jobject, Archive, iterEfficient) {
auto range = THIS->iterEfficient();
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
jobject obj = env->NewObject(objClass, initMethod, ITER_EFFICIENT);
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::efficientOrder>, obj, range.begin());
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::efficientOrder>>(range.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
METHOD(jobject, Archive, findByPath, jstring path) {
auto range = THIS->findByPath(TO_C(path));
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
jobject obj = env->NewObject(objClass, initMethod, ITER_BY_PATH);
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::pathOrder>, obj, range.begin());
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::pathOrder>>(range.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}
METHOD(jobject, Archive, findByTitle, jstring title) {
auto range = THIS->findByTitle(TO_C(title));
jclass objClass = env->FindClass("org/kiwix/libzim/EntryIterator");
jmethodID initMethod = env->GetMethodID(objClass, "<init>", "(I)V");
jobject obj = env->NewObject(objClass, initMethod, ITER_BY_TITLE);
SET_HANDLE(zim::Archive::iterator<zim::EntryOrder::titleOrder>, obj, range.begin());
auto end_ptr = std::make_shared<zim::Archive::iterator<zim::EntryOrder::titleOrder>>(range.end());
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
return obj;
}

View File

@ -0,0 +1,111 @@
/*
* 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_EntryIterator.h"
#include <utils.h>
#include <string>
#include <zim/entry.h>
#include <zim/search.h>
#define PATH_NATIVE_TYPE zim::Archive::iterator<zim::EntryOrder::pathOrder>
#define TITLE_NATIVE_TYPE zim::Archive::iterator<zim::EntryOrder::titleOrder>
#define EFFICIENT_NATIVE_TYPE zim::Archive::iterator<zim::EntryOrder::efficientOrder>
inline int get_order(JNIEnv* env, jobject thisObj) {
jclass thisClass = env->GetObjectClass(thisObj);
jfieldID fieldId = env->GetFieldID(thisClass, "order", "I");
return TO_C(env->GetIntField(thisObj, fieldId));
}
JNIEXPORT void JNICALL
Java_org_kiwix_kiwixlib_libzim_EntryIterotar_dispose(JNIEnv* env, jobject thisObj)
{
// Delete end iterator
switch (get_order(env, thisObj)) {
case 0:
dispose<PATH_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
dispose<PATH_NATIVE_TYPE>(env, thisObj);
break;
case 1:
dispose<TITLE_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
dispose<TITLE_NATIVE_TYPE>(env, thisObj);
break;
case 2:
dispose<EFFICIENT_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
dispose<EFFICIENT_NATIVE_TYPE>(env, thisObj);
break;
}
}
METHOD0(jboolean, EntryIterator, hasNext) {
switch (get_order(env, thisObj)) {
case 0: {
PATH_NATIVE_TYPE next(*GET_PTR(PATH_NATIVE_TYPE));
next++;
auto end = getPtr<PATH_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
return next == *end;
}
case 1: {
TITLE_NATIVE_TYPE next(*GET_PTR(TITLE_NATIVE_TYPE));
next++;
auto end = getPtr<TITLE_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
return next == *end;
}
case 2: {
EFFICIENT_NATIVE_TYPE next(*GET_PTR(EFFICIENT_NATIVE_TYPE));
next++;
auto end = getPtr<EFFICIENT_NATIVE_TYPE>(env, thisObj, "nativeHandleEnd");
return next == *end;
}
}
}
METHOD0(jobject, EntryIterator, next) {
switch (get_order(env, thisObj)) {
case 0: {
(*GET_PTR(PATH_NATIVE_TYPE))++;
zim::Entry entry = **GET_PTR(PATH_NATIVE_TYPE);
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
SET_HANDLE(zim::Entry, obj, entry);
return obj;
}
case 1: {
(*GET_PTR(TITLE_NATIVE_TYPE))++;
zim::Entry entry = **GET_PTR(TITLE_NATIVE_TYPE);
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
SET_HANDLE(zim::Entry, obj, entry);
return obj;
}
case 2: {
(*GET_PTR(EFFICIENT_NATIVE_TYPE))++;
zim::Entry entry = **GET_PTR(EFFICIENT_NATIVE_TYPE);
auto obj = NEW_OBJECT("org/kiwix/libzim/Entry");
SET_HANDLE(zim::Entry, obj, entry);
return obj;
}
}
}

View File

@ -22,6 +22,7 @@ package org.kiwix.libzim;
import org.kiwix.libzim.ZimFileFormatException;
import org.kiwix.libzim.Entry;
import org.kiwix.libzim.Item;
import org.kiwix.libzim.EntryIterator;
import java.io.FileDescriptor;
public class Archive
@ -92,6 +93,12 @@ public class Archive
public native boolean isMultiPart();
public native boolean hasNewNamespaceScheme();
public native EntryIterator iterByPath();
public native EntryIterator iterByTitle();
public native EntryIterator iterEfficient();
public native EntryIterator findByPath(String path);
public native EntryIterator findByTitle(String path);
private native void setNativeArchive(String filename);
private native void setNativeArchiveByFD(FileDescriptor fd);

View File

@ -0,0 +1,44 @@
/*
* 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 java.util.Iterator;
public class EntryIterator implements Iterator<Entry>
{
private EntryIterator(int order) {
this.order = order;
}
public native boolean hasNext();
public native Entry next();
///--------- The wrapper thing
// To delete our native wrapper
public native void dispose();
// A marker of the order used for this iterator
private int order;
// A pointer (as a long) to a native Handle
private long nativeHandle;
// A pointer (as a long) to the native end
private long nativeHandleEnd;
}