[Android] Cache Folders count in RAM

Geoffrey Métais git at videolan.org
Wed Feb 26 14:01:40 CET 2020


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Feb 25 16:11:52 2020 +0100| [283f3e170f1dc2868c46f99916130b155bc57358] | committer: Nicolas Pomepuy

Cache Folders count in RAM

> https://code.videolan.org/videolan/vlc-android/commit/283f3e170f1dc2868c46f99916130b155bc57358
---

 medialibrary/jni/medialibrary.cpp                          | 14 ++++++++++----
 medialibrary/jni/utils.cpp                                 |  4 ++--
 medialibrary/jni/utils.h                                   |  2 +-
 .../src/org/videolan/medialibrary/MLServiceLocator.java    |  6 +++---
 .../org/videolan/medialibrary/interfaces/media/Folder.java |  6 +++++-
 .../src/org/videolan/medialibrary/media/FolderImpl.java    |  5 +++--
 .../org/videolan/medialibrary/stubs/StubDataSource.java    |  4 ++--
 .../src/org/videolan/medialibrary/stubs/StubFolder.java    |  4 ++--
 8 files changed, 28 insertions(+), 17 deletions(-)

diff --git a/medialibrary/jni/medialibrary.cpp b/medialibrary/jni/medialibrary.cpp
index 276a09bc9..de13c6830 100644
--- a/medialibrary/jni/medialibrary.cpp
+++ b/medialibrary/jni/medialibrary.cpp
@@ -1717,7 +1717,10 @@ subFolders(JNIEnv* env, jobject thiz, jobject medialibrary, jlong id, jint sorti
     jobjectArray foldersRefs = (jobjectArray) env->NewObjectArray(foldersList.size(), ml_fields.Folder.clazz, NULL);
     int index = -1;
     for(medialibrary::FolderPtr const& folder : foldersList) {
-        jobject item = convertFolderObject(env, &ml_fields, folder);
+        const auto query = aml->mediaFromFolder(folder->id(), medialibrary::IMedia::Type::Video);
+        int count = (query != nullptr ? query->count() : 0);
+
+        jobject item = convertFolderObject(env, &ml_fields, folder, count);
         env->SetObjectArrayElement(foldersRefs, ++index, item);
         env->DeleteLocalRef(item);
     }
@@ -1745,7 +1748,10 @@ folders(JNIEnv* env, jobject thiz, jint type, jint sortingCriteria, jboolean des
     for(medialibrary::FolderPtr const& folder : foldersList) {
         try
         {
-            jobject item = convertFolderObject(env, &ml_fields, folder);
+            const auto query = aml->mediaFromFolder(folder->id(), (medialibrary::IMedia::Type)type);
+            int count = (query != nullptr ? query->count() : 0);
+
+            jobject item = convertFolderObject(env, &ml_fields, folder, count);
             env->SetObjectArrayElement(foldersRefs, ++index, item);
             env->DeleteLocalRef(item);
         }
@@ -2124,7 +2130,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
     GET_ID(GetMethodID,
            ml_fields.Folder.initID,
            ml_fields.Folder.clazz,
-           "<init>", "(JLjava/lang/String;Ljava/lang/String;)V");
+           "<init>", "(JLjava/lang/String;Ljava/lang/String;I)V");
 
     GET_CLASS(ml_fields.Genre.clazz, "org/videolan/medialibrary/media/GenreImpl", true);
     if (env->RegisterNatives(ml_fields.Genre.clazz, genre_methods, sizeof(genre_methods) / sizeof(genre_methods[0])) < 0) {
@@ -2181,7 +2187,7 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved)
     GET_ID(GetMethodID,
            ml_fields.Folder.initID,
            ml_fields.Folder.clazz,
-           "<init>", "(JLjava/lang/String;Ljava/lang/String;)V");
+           "<init>", "(JLjava/lang/String;Ljava/lang/String;I)V");
 
     GET_CLASS(ml_fields.VideoGroup.clazz, "org/videolan/medialibrary/media/VideoGroupImpl", true);
     if (env->RegisterNatives(ml_fields.VideoGroup.clazz, videogroup_methods, sizeof(videogroup_methods) / sizeof(videogroup_methods[0])) < 0) {
diff --git a/medialibrary/jni/utils.cpp b/medialibrary/jni/utils.cpp
index 0182a07ae..1a7b26b54 100644
--- a/medialibrary/jni/utils.cpp
+++ b/medialibrary/jni/utils.cpp
@@ -148,12 +148,12 @@ convertPlaylistObject(JNIEnv* env, fields *fields, medialibrary::PlaylistPtr con
 }
 
 jobject
-convertFolderObject(JNIEnv* env, fields *fields, medialibrary::FolderPtr const& folderPtr)
+convertFolderObject(JNIEnv* env, fields *fields, medialibrary::FolderPtr const& folderPtr, int count)
 {
     jstring name = env->NewStringUTF(folderPtr->name().c_str());
     jstring mrl = env->NewStringUTF(folderPtr->mrl().c_str());
     jobject item = env->NewObject(fields->Folder.clazz, fields->Folder.initID,
-                          (jlong) folderPtr->id(), name, mrl);
+                          (jlong) folderPtr->id(), name, mrl, (jint) count);
     env->DeleteLocalRef(name);
     env->DeleteLocalRef(mrl);
     return item;
diff --git a/medialibrary/jni/utils.h b/medialibrary/jni/utils.h
index a6a00f6b5..e5dcdf9a2 100644
--- a/medialibrary/jni/utils.h
+++ b/medialibrary/jni/utils.h
@@ -129,7 +129,7 @@ jobject convertAlbumObject(JNIEnv* env, fields *fields, medialibrary::AlbumPtr c
 jobject convertArtistObject(JNIEnv* env, fields *fields, medialibrary::ArtistPtr const& artistPtr);
 jobject convertGenreObject(JNIEnv* env, fields *fields, medialibrary::GenrePtr const& genrePtr);
 jobject convertPlaylistObject(JNIEnv* env, fields *fields, medialibrary::PlaylistPtr const& genrePtr);
-jobject convertFolderObject(JNIEnv* env, fields *fields, medialibrary::FolderPtr const& folderPtr);
+jobject convertFolderObject(JNIEnv* env, fields *fields, medialibrary::FolderPtr const& folderPtr, int count);
 jobject convertVideoGroupObject(JNIEnv* env, fields *fields, medialibrary::MediaGroupPtr const& videogroupPtr);
 jobject convertSearchAggregateObject(JNIEnv* env, fields *fields, medialibrary::SearchAggregate const& searchAggregatePtr);
 jobjectArray filteredArray(JNIEnv* env, jobjectArray array, jclass clazz, int removalCount = -1);
diff --git a/medialibrary/src/org/videolan/medialibrary/MLServiceLocator.java b/medialibrary/src/org/videolan/medialibrary/MLServiceLocator.java
index a655ade38..95c9bb275 100644
--- a/medialibrary/src/org/videolan/medialibrary/MLServiceLocator.java
+++ b/medialibrary/src/org/videolan/medialibrary/MLServiceLocator.java
@@ -171,11 +171,11 @@ public class MLServiceLocator {
     }
 
     //FolderImpl
-    public static Folder getAbstractFolder(long id, String name, String mrl) {
+    public static Folder getAbstractFolder(long id, String name, String mrl, int count) {
         if (sMode == LocatorMode.VLC_ANDROID) {
-            return new FolderImpl(id, name, mrl);
+            return new FolderImpl(id, name, mrl, count);
         } else {
-            return new StubFolder(id, name, mrl);
+            return new StubFolder(id, name, mrl, count);
         }
     }
 
diff --git a/medialibrary/src/org/videolan/medialibrary/interfaces/media/Folder.java b/medialibrary/src/org/videolan/medialibrary/interfaces/media/Folder.java
index 67750177d..56dd4ca40 100644
--- a/medialibrary/src/org/videolan/medialibrary/interfaces/media/Folder.java
+++ b/medialibrary/src/org/videolan/medialibrary/interfaces/media/Folder.java
@@ -14,10 +14,12 @@ public abstract class Folder extends MediaLibraryItem {
     public static int TYPE_FOLDER_STREAM = 4;
 
     public String mMrl;
+    protected int mMediaCount;
 
-    public Folder(long id, String name, String mrl) {
+    public Folder(long id, String name, String mrl, int count) {
         super(id, name);
         mMrl = mrl;
+        mMediaCount = count;
     }
 
     abstract public MediaWrapper[] media(int type, int sort, boolean desc, int nbItems, int offset);
@@ -50,6 +52,7 @@ public abstract class Folder extends MediaLibraryItem {
     public void writeToParcel(Parcel parcel, int i) {
         super.writeToParcel(parcel, i);
         parcel.writeString(mMrl);
+        parcel.writeInt(mMediaCount);
     }
 
     public static Parcelable.Creator<Folder> CREATOR = new Parcelable.Creator<Folder>() {
@@ -67,6 +70,7 @@ public abstract class Folder extends MediaLibraryItem {
     public Folder(Parcel in) {
         super(in);
         this.mMrl = in.readString();
+        this.mMediaCount = in.readInt();
     }
 
     public boolean equals(Folder other) {
diff --git a/medialibrary/src/org/videolan/medialibrary/media/FolderImpl.java b/medialibrary/src/org/videolan/medialibrary/media/FolderImpl.java
index a0809971c..a61a54ec8 100644
--- a/medialibrary/src/org/videolan/medialibrary/media/FolderImpl.java
+++ b/medialibrary/src/org/videolan/medialibrary/media/FolderImpl.java
@@ -9,8 +9,8 @@ import org.videolan.medialibrary.interfaces.media.MediaWrapper;
 @SuppressWarnings("JniMissingFunction")
 public class FolderImpl extends Folder {
 
-    public FolderImpl(long id, String name, String mrl) {
-        super(id, name, mrl);
+    public FolderImpl(long id, String name, String mrl, int count) {
+        super(id, name, mrl, count);
     }
 
     public FolderImpl(Parcel in) {
@@ -23,6 +23,7 @@ public class FolderImpl extends Folder {
     }
 
     public int mediaCount(int type) {
+        if (type == TYPE_FOLDER_VIDEO) return mMediaCount;
         final Medialibrary ml = Medialibrary.getInstance();
         return ml.isInitiated() ? nativeMediaCount(ml, mId, type) : 0;
     }
diff --git a/medialibrary/src/org/videolan/medialibrary/stubs/StubDataSource.java b/medialibrary/src/org/videolan/medialibrary/stubs/StubDataSource.java
index dddd4fe47..2324ce00b 100644
--- a/medialibrary/src/org/videolan/medialibrary/stubs/StubDataSource.java
+++ b/medialibrary/src/org/videolan/medialibrary/stubs/StubDataSource.java
@@ -121,7 +121,7 @@ public class StubDataSource {
     }
 
     public Folder createFolder(String name) {
-        Folder folder = MLServiceLocator.getAbstractFolder(getUUID(), name, baseMrl + name);
+        Folder folder = MLServiceLocator.getAbstractFolder(getUUID(), name, baseMrl + name, 1);
         mFolders.add(folder);
         return folder;
     }
@@ -570,7 +570,7 @@ public class StubDataSource {
             ArrayList<String> mlFolders = new ArrayList<>(Arrays.asList(getFoldersString()));
             if (!mlFolders.contains(mrl)) {
                 final String name = folderArray[folderArray.length - 1];
-                mFolders.add(MLServiceLocator.getAbstractFolder(getUUID(), name, mrl));
+                mFolders.add(MLServiceLocator.getAbstractFolder(getUUID(), name, mrl, 1));
             }
         }
     }
diff --git a/medialibrary/src/org/videolan/medialibrary/stubs/StubFolder.java b/medialibrary/src/org/videolan/medialibrary/stubs/StubFolder.java
index b3781b1d8..3df9ee16a 100644
--- a/medialibrary/src/org/videolan/medialibrary/stubs/StubFolder.java
+++ b/medialibrary/src/org/videolan/medialibrary/stubs/StubFolder.java
@@ -14,8 +14,8 @@ public class StubFolder extends Folder {
 
     private StubDataSource dt = StubDataSource.getInstance();
 
-    public StubFolder(long id, String name, String mrl) {
-        super(id, name, mrl);
+    public StubFolder(long id, String name, String mrl, int count) {
+        super(id, name, mrl, count);
     }
     public StubFolder(Parcel in) {
         super(in);



More information about the Android mailing list