Skip to content

Commit

Permalink
Wrap new methods in kiwix::Library.
Browse files Browse the repository at this point in the history
- migrateBookmarks
- getBestTargetBookId
  • Loading branch information
mgautierfr committed Feb 26, 2024
1 parent 2eb8aa5 commit fa44690
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
1 change: 1 addition & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ task buildHeaders(type: Exec) {
String getLibkiwixFiles() {
return "${projectDir}/src/main/java/org/kiwix/libkiwix/Book.java " +
"${projectDir}/src/main/java/org/kiwix/libkiwix/Bookmark.java " +
"${projectDir}/src/main/java/org/kiwix/libkiwix/BookmarkMigrationResult.java " +
"${projectDir}/src/main/java/org/kiwix/libkiwix/Filter.java " +
"${projectDir}/src/main/java/org/kiwix/libkiwix/JNIICU.java " +
"${projectDir}/src/main/java/org/kiwix/libkiwix/Illustration.java " +
Expand Down
33 changes: 33 additions & 0 deletions lib/src/main/cpp/libkiwix/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ METHOD(jboolean, removeBookmark, jstring zimId, jstring url) {
return TO_JNI(THIS->removeBookmark(TO_C(zimId), TO_C(url)));
} CATCH_EXCEPTION(false)

METHOD(jobject, migrateBookmarks__Z, jboolean allowDowngrade) {
jobject migrationResult = newObject("org/kiwix/libkiwix/BookmarkMigrationResult", env);
int nbMigrated, nbTotal;
std::tie(nbMigrated, nbTotal) = THIS->migrateBookmarks(
TO_C(allowDowngrade) ? kiwix::ALLOW_DOWNGRADE : kiwix::UPGRADE_ONLY
);
setMigrationResultValue(nbMigrated, nbTotal, migrationResult, env);
return migrationResult;
} CATCH_EXCEPTION(nullptr)

METHOD(jint, migrateBookmarks__Ljava_lang_String_2Z, jstring sourceBookId, jboolean allowDowngrade) {
return TO_JNI(THIS->migrateBookmarks(
TO_C(sourceBookId),
allowDowngrade? kiwix::ALLOW_DOWNGRADE : kiwix::UPGRADE_ONLY
));
} CATCH_EXCEPTION(0)

METHOD(jint, migrateBookmarks__Ljava_lang_String_2Ljava_lang_String_2, jstring sourceBookId, jstring targetBookId) {
return TO_JNI(THIS->migrateBookmarks(TO_C(sourceBookId), TO_C(targetBookId)));
} CATCH_EXCEPTION(0)

METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) {
auto bookmarks = THIS->getBookmarks(TO_C(onlyValidBookmarks));
jobjectArray retArray = createArray(env, bookmarks.size(), "org/kiwix/libkiwix/Bookmark");
Expand All @@ -108,3 +129,15 @@ METHOD(jobjectArray, getBookmarks, jboolean onlyValidBookmarks) {
}
return retArray;
} CATCH_EXCEPTION(nullptr)

METHOD(jstring, getBestTargetBookId__Lorg_kiwix_libkiwix_Bookmark_2Z, jobject bookmark, jboolean allowDowngrade) {
auto cBookmark = getPtr<kiwix::Bookmark>(env, bookmark);
return TO_JNI(THIS->getBestTargetBookId(
*cBookmark,
allowDowngrade ? kiwix::ALLOW_DOWNGRADE : kiwix::UPGRADE_ONLY
));
} CATCH_EXCEPTION(nullptr)

METHOD(jstring, getBestTargetBookId__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2, jstring bookName, jstring preferedFlavour, jstring minDate) {
return TO_JNI(THIS->getBestTargetBookId(TO_C(bookName), TO_C(preferedFlavour), TO_C(minDate)));
} CATCH_EXCEPTION(nullptr)
9 changes: 9 additions & 0 deletions lib/src/main/cpp/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ inline void setDaiObjValue(const std::string& filename, const long offset,
env->SetLongField(obj, offsetFid, offset);
}

inline void setMigrationResultValue(long nbMigrated, long nbTotalInvalid, const jobject obj, JNIEnv* env)
{
jclass objClass = env->GetObjectClass(obj);
jfieldID nbMigratedFid = env->GetFieldID(objClass, "nbMigratedBookmarks", "J");
env->SetLongField(obj, nbMigratedFid, nbMigrated);
jfieldID nbTotalFid = env->GetFieldID(objClass, "nbInvalidBookmarks", "J");
env->SetLongField(obj, nbTotalFid, nbTotalInvalid);
}

inline int throwException(JNIEnv* env, const char* exception, const char* message) {
return env->ThrowNew(env->FindClass(exception), message);
}
Expand Down
26 changes: 26 additions & 0 deletions lib/src/main/java/org/kiwix/libkiwix/BookmarkMigrationResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (C) 2024 Matthieu Gautier <[email protected]>
*
* 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.libkiwix;

public class BookmarkMigrationResult
{
public long nbMigratedBookmarks;
public long nbInvalidBookmarks;
}
13 changes: 13 additions & 0 deletions lib/src/main/java/org/kiwix/libkiwix/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.kiwix.libkiwix.Book;
import org.kiwix.libkiwix.JNIKiwixException;
import org.kiwix.libkiwix.Bookmark;
import org.kiwix.libkiwix.BookmarkMigrationResult;

public class Library
{
Expand Down Expand Up @@ -56,8 +57,20 @@ public Library()

public native void addBookmark(Bookmark bookmark);
public native boolean removeBookmark(String zimId, String url);
public native BookmarkMigrationResult migrateBookmarks(boolean allowDowngrade);
public native int migrateBookmarks(String sourceBookId, boolean allowDowngrade);
public native int migrateBookmarks(String sourceBookId, String targetBookId);
public native Bookmark[] getBookmarks(boolean onlyValidBookmarks);

public native String getBestTargetBookId(Bookmark bookmark, boolean allowDowngrade);
public String getBestTargetBookId(String bookName) {
return getBestTargetBookId(bookName, "", "");
}
public String getBestTargetBookId(String bookName, String preferedFlavour) {
return getBestTargetBookId(bookName, preferedFlavour, "");
}
public native String getBestTargetBookId(String bookName, String preferedFlavour, String minDate);

@Override
protected void finalize() { dispose(); }
private native void setNativeHandler();
Expand Down
9 changes: 9 additions & 0 deletions lib/src/test/org/kiwix/test/libkiwix/TestLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.kiwix.libzim.Searcher;
import org.kiwix.libkiwix.Filter;
import org.kiwix.libkiwix.JNIKiwixException;
import org.kiwix.libkiwix.BookmarkMigrationResult;
import java.util.stream.Stream;

public class TestLibrary
Expand Down Expand Up @@ -58,5 +59,13 @@ public class TestLibrary

public void addBookmark(TestBookmark bookmark) { inner.addBookmark(bookmark.inner()); }
public boolean removeBookmark(String zimId, String url) { return inner.removeBookmark(zimId, url); }
public BookmarkMigrationResult migrateBookmarks(boolean allowDowngrade) { return inner.migrateBookmarks(allowDowngrade); }
public int migrateBookmarks(String sourceBookId, boolean allowDowngrade) { return inner.migrateBookmarks(sourceBookId, allowDowngrade); }
public int migrateBookmarks(String sourceBookId, String targetBookId) { return inner.migrateBookmarks(sourceBookId, targetBookId); }
public TestBookmark[] getBookmarks(boolean onlyValidBookmarks) { return Stream.of(inner.getBookmarks(onlyValidBookmarks)).map(b -> new TestBookmark(b)).toArray(TestBookmark[]::new); }

public String getBestTargetBookId(TestBookmark bookmark, boolean allowDowngrade) { return inner.getBestTargetBookId(bookmark.inner(), allowDowngrade); }
public String getBestTargetBookId(String bookname) { return inner.getBestTargetBookId(bookname); }
public String getBestTargetBookId(String bookname, String preferedFlavour) { return inner.getBestTargetBookId(bookname, preferedFlavour); }
public String getBestTargetBookId(String bookname, String preferedFlavour, String minDate) { return inner.getBestTargetBookId(bookname, preferedFlavour, minDate); }
}
15 changes: 14 additions & 1 deletion lib/src/test/test.java
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,21 @@ public void testBookMark() throws ZimFileFormatException, JNIKiwixException {
assertEquals(bookmark.getBookTitle(), book.getTitle());
assertEquals(bookmark.getBookName(), book.getName());
assertEquals(bookmark.getBookFlavour(), book.getFlavour());

BookmarkMigrationResult result = lib.migrateBookmarks(true);
assertEquals(result.nbMigratedBookmarks, 0);
assertEquals(result.nbInvalidBookmarks, 0);

assertEquals(lib.migrateBookmarks(book.getId(), true), 0);
assertEquals(lib.migrateBookmarks(book.getId(), "new-id"), 1);

assertEquals(lib.getBestTargetBookId(bookmark, true), book.getId());
assertEquals(lib.getBestTargetBookId(book.getName()), book.getId());
assertEquals(lib.getBestTargetBookId(book.getName(), "someflavour"), book.getId());
assertEquals(lib.getBestTargetBookId(book.getName(), "someflavour", "20230105"), "");
assertEquals(lib.getBestTargetBookId(book.getName(), "someflavour", "20190105"), book.getId());
// remove bookmark from library
lib.removeBookmark(bookmark.getBookId(), bookmark.getUrl());
lib.removeBookmark("new-id", bookmark.getUrl());
bookmarkArray = lib.getBookmarks(true);
assertEquals(0, bookmarkArray.length);
}
Expand Down

0 comments on commit fa44690

Please sign in to comment.