Skip to content

Commit ee448ab

Browse files
authored
Merge pull request #126 from kiwix/Fixes#125
Added new methods in our binding that introduced in new libkiwix and libzim.
2 parents 2423bd0 + dcaec1b commit ee448ab

File tree

9 files changed

+355
-2
lines changed

9 files changed

+355
-2
lines changed

lib/build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ext.libzim_version = "9.4.0"
2929
apply from: 'publish.gradle'
3030
android {
3131
compileSdk 34
32-
namespace = "kiwix.org.kiwixlib"
32+
namespace = "org.kiwix.libkiwix"
3333
defaultConfig {
3434

3535
minSdk 24
@@ -386,7 +386,8 @@ String getLibzimFiles() {
386386
"${projectDir}/src/main/java/org/kiwix/libzim/ZimFileFormatException.java " +
387387
"${projectDir}/src/main/java/org/kiwix/libzim/EntryNotFoundException.java " +
388388
"${projectDir}/src/main/java/org/kiwix/libzim/FdInput.java " +
389-
"${projectDir}/src/main/java/org/kiwix/libzim/IllustrationInfo.java"
389+
"${projectDir}/src/main/java/org/kiwix/libzim/IllustrationInfo.java " +
390+
"${projectDir}/src/main/java/org/kiwix/libzim/OpenConfig.java"
390391
}
391392

392393
tasks.register('buildLinuxBinding', Exec) {

lib/src/main/cpp/libkiwix/kiwixserver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ METHOD(void, setAddress, jstring address)
5454
THIS->setAddress(TO_C(address));
5555
} CATCH_EXCEPTION()
5656

57+
METHOD0(jobjectArray, getServerAccessUrls)
58+
{
59+
const auto urls = THIS->getServerAccessUrls();
60+
return c2jni(urls, env);
61+
} CATCH_EXCEPTION(nullptr)
62+
5763
METHOD(void, setPort, int port)
5864
{
5965
THIS->setPort(TO_C(port));

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

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,34 @@ zim::FdInput jni2fdInput(const jobject& fdInputObj, JNIEnv* env)
7979
return zim::FdInput(fd, offset, size);
8080
}
8181

82+
zim::OpenConfig jni2openConfig(const jobject& openConfigObj, JNIEnv* env)
83+
{
84+
jclass cls = env->GetObjectClass(openConfigObj);
85+
86+
jfieldID preloadXapianDbFid = env->GetFieldID(cls, "preloadXapianDb", "Z");
87+
jboolean preloadXapianDb = env->GetBooleanField(openConfigObj, preloadXapianDbFid);
88+
jfieldID preloadDirentRangesFid = env->GetFieldID(cls, "preloadDirentRanges", "I");
89+
jint preloadDirentRanges = env->GetIntField(openConfigObj, preloadDirentRangesFid);
90+
zim::OpenConfig config;
91+
config.preloadXapianDb(preloadXapianDb);
92+
if (preloadDirentRanges > 0)
93+
config.preloadDirentRanges(preloadDirentRanges);
94+
return config;
95+
}
96+
8297
} // unnamed namespace
8398

99+
METHOD(void, setNativeArchiveWithConfig, jstring filename, jobject openConfigObj)
100+
{
101+
std::string cPath = TO_C(filename);
102+
zim::OpenConfig config = jni2openConfig(openConfigObj, env);
103+
104+
// Create archive with custom configuration
105+
auto archive = std::make_shared<zim::Archive>(cPath, config);
106+
SET_PTR(archive);
107+
}
108+
CATCH_EXCEPTION()
109+
84110
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD(
85111
JNIEnv* env, jobject thisObj, jobject fdObj) try
86112
{
@@ -96,6 +122,22 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFD(
96122
#endif
97123
} CATCH_EXCEPTION()
98124

125+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveByFDWithConfig(
126+
JNIEnv* env, jobject thisObj, jobject fdObj, jobject openConfigObj) try
127+
{
128+
#ifndef _WIN32
129+
int fd = jni2fd(fdObj, env);
130+
zim::OpenConfig config = jni2openConfig(openConfigObj, env);
131+
132+
LOG("Attempting to create reader with fd: %d", fd);
133+
auto archive = std::make_shared<zim::Archive>(fd, config);
134+
SET_PTR(archive);
135+
#else
136+
jclass exception = env->FindClass("java/lang/UnsupportedOperationException");
137+
env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveByFD() is not supported under Windows");
138+
#endif
139+
} CATCH_EXCEPTION()
140+
99141
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded(
100142
JNIEnv* env, jobject thisObj, jobject fdObj, jlong offset, jlong size) try
101143
{
@@ -111,6 +153,22 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbedded(
111153
#endif
112154
} CATCH_EXCEPTION()
113155

156+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedWithConfig(
157+
JNIEnv* env, jobject thisObj, jobject fdObj, jlong offset, jlong size, jobject openConfigObj) try
158+
{
159+
#ifndef _WIN32
160+
int fd = jni2fd(fdObj, env);
161+
zim::OpenConfig config = jni2openConfig(openConfigObj, env);
162+
163+
LOG("Attempting to create reader with fd: %d", fd);
164+
auto archive = std::make_shared<zim::Archive>(fd, offset, size, config);
165+
SET_PTR(archive);
166+
#else
167+
jclass exception = env->FindClass("java/lang/UnsupportedOperationException");
168+
env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows");
169+
#endif
170+
} CATCH_EXCEPTION()
171+
114172
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedFd(
115173
JNIEnv* env, jobject thisObj, jobject fdObj) try
116174
{
@@ -125,6 +183,21 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedFd(
125183
#endif
126184
} CATCH_EXCEPTION()
127185

186+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedFdWithConfig(
187+
JNIEnv* env, jobject thisObj, jobject fdObj, jobject openConfigObj) try
188+
{
189+
#ifndef _WIN32
190+
auto fdInput = jni2fdInput(fdObj, env);
191+
zim::OpenConfig config = jni2openConfig(openConfigObj, env);
192+
193+
auto archive = std::make_shared<zim::Archive>(fdInput, config);
194+
SET_PTR(archive);
195+
#else
196+
jclass exception = env->FindClass("java/lang/UnsupportedOperationException");
197+
env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows");
198+
#endif
199+
} CATCH_EXCEPTION()
200+
128201

129202
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedFds(
130203
JNIEnv* env, jobject thisObj, jobjectArray fdsObj) try
@@ -149,6 +222,29 @@ JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedFds
149222
#endif
150223
} CATCH_EXCEPTION()
151224

225+
JNIEXPORT void JNICALL Java_org_kiwix_libzim_Archive_setNativeArchiveEmbeddedFdsWithConfig(
226+
JNIEnv* env, jobject thisObj, jobjectArray fdsObj, jobject openConfigObj) try
227+
{
228+
#ifndef _WIN32
229+
230+
jsize length = env->GetArrayLength(fdsObj);
231+
std::vector<zim::FdInput> v;
232+
233+
int i;
234+
for(i = 0; i<length; i++) {
235+
jobject fdObj = env->GetObjectArrayElement(fdsObj, i);
236+
auto fdInput = jni2fdInput(fdObj, env);
237+
v.push_back(fdInput);
238+
}
239+
zim::OpenConfig config = jni2openConfig(openConfigObj, env);
240+
auto archive = std::make_shared<zim::Archive>(v, config);
241+
SET_PTR(archive);
242+
#else
243+
jclass exception = env->FindClass("java/lang/UnsupportedOperationException");
244+
env->ThrowNew(exception, "org.kiwix.libzim.Archive.setNativeArchiveEmbedded() is not supported under Windows");
245+
#endif
246+
} CATCH_EXCEPTION()
247+
152248
DISPOSE
153249

154250
GETTER(jstring, getFilename)
@@ -245,6 +341,12 @@ GETTER(jstring, getChecksum)
245341
GETTER(jboolean, check)
246342
GETTER(jboolean, isMultiPart)
247343
GETTER(jboolean, hasNewNamespaceScheme)
344+
GETTER(jlong, getDirentCacheMaxSize)
345+
GETTER(jlong, getDirentCacheCurrentSize)
346+
347+
METHOD(void, setDirentCacheMaxSize, jlong nbDirents) {
348+
THIS->setDirentCacheMaxSize(static_cast<size_t>(nbDirents));
349+
} CATCH_EXCEPTION()
248350

249351
#define ITER_BY_PATH 0
250352
#define ITER_BY_TITLE 1
@@ -308,3 +410,15 @@ METHOD(jobject, findByTitle, jstring title) {
308410
setPtr(env, obj, std::move(end_ptr), "nativeHandleEnd");
309411
return obj;
310412
} CATCH_EXCEPTION(nullptr)
413+
414+
METHOD0(jlong, getClusterCacheMaxSize) {
415+
return static_cast<jlong>(zim::getClusterCacheMaxSize());
416+
} CATCH_EXCEPTION(0)
417+
418+
METHOD0(jlong, getClusterCacheCurrentSize) {
419+
return static_cast<jlong>(zim::getClusterCacheCurrentSize());
420+
} CATCH_EXCEPTION(0)
421+
422+
METHOD(void, setClusterCacheMaxSize, jlong sizeInBytes) {
423+
zim::setClusterCacheMaxSize(static_cast<size_t>(sizeInBytes));
424+
} CATCH_EXCEPTION()

lib/src/main/java/org/kiwix/libkiwix/Server.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class Server
2727
public native void setRoot(String root);
2828

2929
public native void setAddress(String address);
30+
public native String[] getServerAccessUrls();
3031

3132
public native void setPort(int port);
3233

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.kiwix.libzim.EntryIterator;
2626
import org.kiwix.libzim.FdInput;
2727
import org.kiwix.libzim.IllustrationInfo;
28+
import org.kiwix.libzim.OpenConfig;
2829
import java.io.FileDescriptor;
2930

3031
public class Archive
@@ -35,29 +36,56 @@ public Archive(String filename) throws ZimFileFormatException
3536
setNativeArchive(filename);
3637
}
3738

39+
public Archive(String filename, OpenConfig config) throws ZimFileFormatException {
40+
setNativeArchiveWithConfig(filename, config);
41+
}
42+
3843
public Archive(FileDescriptor fd) throws ZimFileFormatException
3944
{
4045
setNativeArchiveByFD(fd);
4146
}
4247

48+
public Archive(FileDescriptor fd, OpenConfig openConfig) throws ZimFileFormatException
49+
{
50+
setNativeArchiveByFDWithConfig(fd, openConfig);
51+
}
52+
4353
public Archive(FileDescriptor fd, long offset, long size)
4454
throws ZimFileFormatException
4555
{
4656
setNativeArchiveEmbedded(fd, offset, size);
4757
}
4858

59+
public Archive(FileDescriptor fd, long offset, long size, OpenConfig openConfig)
60+
throws ZimFileFormatException
61+
{
62+
setNativeArchiveEmbeddedWithConfig(fd, offset, size, openConfig);
63+
}
64+
4965
public Archive(FdInput fd)
5066
throws ZimFileFormatException
5167
{
5268
setNativeArchiveEmbeddedFd(fd);
5369
}
5470

71+
public Archive(FdInput fd, OpenConfig openConfig)
72+
throws ZimFileFormatException
73+
{
74+
setNativeArchiveEmbeddedFdWithConfig(fd, openConfig);
75+
}
76+
5577
public Archive(FdInput[] fds)
5678
throws ZimFileFormatException
5779
{
5880
setNativeArchiveEmbeddedFds(fds);
5981
}
6082

83+
public Archive(FdInput[] fds, OpenConfig openConfig)
84+
throws ZimFileFormatException
85+
{
86+
setNativeArchiveEmbeddedFdsWithConfig(fds, openConfig);
87+
}
88+
6189
public native String getFilename();
6290
public native long getFilesize();
6391
public native int getAllEntryCount();
@@ -104,13 +132,24 @@ public Archive(FdInput[] fds)
104132
public native EntryIterator iterEfficient();
105133
public native EntryIterator findByPath(String path);
106134
public native EntryIterator findByTitle(String path);
135+
public native long getDirentCacheMaxSize();
136+
public native long getDirentCacheCurrentSize();
137+
public native void setDirentCacheMaxSize(long nbDirents);
138+
public native long getClusterCacheMaxSize();
139+
public native long getClusterCacheCurrentSize();
140+
public native void setClusterCacheMaxSize(long sizeInBytes);
107141

108142

109143
private native void setNativeArchive(String filename);
144+
private native void setNativeArchiveWithConfig(String filename, OpenConfig openConfig);
110145
private native void setNativeArchiveByFD(FileDescriptor fd);
146+
private native void setNativeArchiveByFDWithConfig(FileDescriptor fd, OpenConfig openConfig);
111147
private native void setNativeArchiveEmbedded(FileDescriptor fd, long offset, long size);
148+
private native void setNativeArchiveEmbeddedWithConfig(FileDescriptor fd, long offset, long size, OpenConfig openConfig);
112149
private native void setNativeArchiveEmbeddedFd(FdInput fd);
150+
private native void setNativeArchiveEmbeddedFdWithConfig(FdInput fd, OpenConfig openConfig);
113151
private native void setNativeArchiveEmbeddedFds(FdInput[] fds);
152+
private native void setNativeArchiveEmbeddedFdsWithConfig(FdInput[] fds, OpenConfig openConfig);
114153

115154
@Override
116155
protected void finalize() { dispose(); }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.kiwix.libzim;
2+
3+
public class OpenConfig {
4+
public boolean preloadXapianDb;
5+
public int preloadDirentRanges;
6+
7+
public OpenConfig(boolean preloadXapianDb, int preloadDirentRanges) {
8+
this.preloadXapianDb = preloadXapianDb;
9+
this.preloadDirentRanges = preloadDirentRanges;
10+
}
11+
}

lib/src/test/org/kiwix/test/libkiwix/TestServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class TestServer
2828

2929
public void setRoot(String root) { inner.setRoot(root); }
3030
public void setAddress(String address) { inner.setAddress(address); }
31+
32+
public String[] getServerAccessUrls() { return inner.getServerAccessUrls(); }
3133
public void setPort(int port) { inner.setPort(port); }
3234
public void setNbThreads(int nbTreads) { inner.setNbThreads(nbTreads); }
3335
public void setTaskbar(boolean withTaskBar, boolean witLibraryButton) { inner.setTaskbar(withTaskBar, witLibraryButton); }

lib/src/test/org/kiwix/test/libzim/TestArchive.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,57 @@ public TestArchive(String filename) throws ZimFileFormatException
3737
inner = new Archive(filename);
3838
}
3939

40+
public TestArchive(String filename, OpenConfig openConfig) throws ZimFileFormatException
41+
{
42+
inner = new Archive(filename, openConfig);
43+
}
44+
4045
public TestArchive(FileDescriptor fd) throws ZimFileFormatException
4146
{
4247
inner = new Archive(fd);
4348
}
4449

50+
public TestArchive(FileDescriptor fd, OpenConfig openConfig) throws ZimFileFormatException
51+
{
52+
inner = new Archive(fd, openConfig);
53+
}
54+
4555
public TestArchive(FileDescriptor fd, long offset, long size)
4656
throws ZimFileFormatException
4757
{
4858
inner = new Archive(fd, offset, size);
4959
}
5060

61+
public TestArchive(FileDescriptor fd, long offset, long size, OpenConfig openConfig)
62+
throws ZimFileFormatException
63+
{
64+
inner = new Archive(fd, offset, size, openConfig);
65+
}
66+
5167
public TestArchive(FdInput fd)
5268
throws ZimFileFormatException
5369
{
5470
inner = new Archive(fd);
5571
}
5672

73+
public TestArchive(FdInput fd, OpenConfig openConfig)
74+
throws ZimFileFormatException
75+
{
76+
inner = new Archive(fd, openConfig);
77+
}
78+
5779
public TestArchive(FdInput[] fds)
5880
throws ZimFileFormatException
5981
{
6082
inner = new Archive(fds);
6183
}
6284

85+
public TestArchive(FdInput[] fds, OpenConfig openConfig)
86+
throws ZimFileFormatException
87+
{
88+
inner = new Archive(fds, openConfig);
89+
}
90+
6391
public String getFilename() { return inner.getFilename(); }
6492
public long getFilesize() { return inner.getFilesize(); }
6593
public int getAllEntryCount() { return inner.getAllEntryCount(); }
@@ -108,5 +136,13 @@ public TestArchive(FdInput[] fds)
108136
public TestEntryIterator findByPath(String path) { return new TestEntryIterator(inner.findByPath(path)); }
109137
public TestEntryIterator findByTitle(String path) { return new TestEntryIterator(inner.findByTitle(path)); }
110138

139+
public long getDirentCacheMaxSize() { return inner.getDirentCacheMaxSize(); }
140+
public long getDirentCacheCurrentSize() { return inner.getDirentCacheCurrentSize(); }
141+
public void setDirentCacheMaxSize(long nbDirents) { inner.setDirentCacheMaxSize(nbDirents); }
142+
143+
public long getClusterCacheMaxSize() { return inner.getClusterCacheMaxSize(); }
144+
public long getClusterCacheCurrentSize() { return inner.getClusterCacheCurrentSize(); }
145+
public void setClusterCacheMaxSize(long sizeInBytes) { inner.setClusterCacheMaxSize(sizeInBytes); }
146+
111147
public void dispose() { inner.dispose(); }
112148
}

0 commit comments

Comments
 (0)