try better perf for GetCover
authorNiki Roo <niki@nikiroo.be>
Sun, 3 Mar 2019 16:59:24 +0000 (17:59 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 3 Mar 2019 16:59:24 +0000 (17:59 +0100)
src/be/nikiroo/fanfix/library/BasicLibrary.java
src/be/nikiroo/fanfix/library/CacheLibrary.java
src/be/nikiroo/fanfix/library/LocalLibrary.java
src/be/nikiroo/fanfix/library/RemoteLibrary.java
src/be/nikiroo/fanfix/library/RemoteLibraryServer.java

index 129c8fb33b1fc764c18a28661c3d45216b724f21..d753267ee5bce6082cfb4c0f277a2b0244efe3bb 100644 (file)
@@ -94,7 +94,8 @@ abstract public class BasicLibrary {
        /**
         * Return the cover image associated to this source.
         * <p>
-        * By default, return the cover of the first story with this source.
+        * By default, return the custom cover if any, and if not, return the cover
+        * of the first story with this source.
         * 
         * @param source
         *            the source
@@ -102,6 +103,11 @@ abstract public class BasicLibrary {
         * @return the cover image or NULL
         */
        public Image getSourceCover(String source) {
+               Image custom = getCustomSourceCover(source);
+               if (custom != null) {
+                       return custom;
+               }
+
                List<MetaData> metas = getListBySource(source);
                if (metas.size() > 0) {
                        return getCover(metas.get(0).getLuid());
@@ -110,6 +116,20 @@ abstract public class BasicLibrary {
                return null;
        }
 
+       /**
+        * Return the custom cover image associated to this source.
+        * <p>
+        * By default, return NULL.
+        * 
+        * @param source
+        *            the source to look for
+        * 
+        * @return the custom cover or NULL if none
+        */
+       public Image getCustomSourceCover(@SuppressWarnings("unused") String source) {
+               return null;
+       }
+
        /**
         * Fix the source cover to the given story cover.
         * 
index 977331f33ecf0783084f730414b0862dd1de86c1..29920e8902b0090354ae7d82680fe7d4bdf866d3 100644 (file)
@@ -112,8 +112,25 @@ public class CacheLibrary extends BasicLibrary {
 
        @Override
        public Image getSourceCover(String source) {
-               // no cache for the source cover
-               return lib.getSourceCover(source);
+               Image custom = getCustomSourceCover(source);
+               if (custom != null) {
+                       return custom;
+               }
+
+               return cacheLib.getSourceCover(source);
+       }
+
+       @Override
+       public Image getCustomSourceCover(String source) {
+               Image custom = cacheLib.getCustomSourceCover(source);
+               if (custom == null) {
+                       custom = lib.getCustomSourceCover(source);
+                       if (custom != null) {
+                               cacheLib.setSourceCover(source, custom);
+                       }
+               }
+
+               return custom;
        }
 
        @Override
index 838c97cf3e8b55363ab4683b07596c6857b74ac0..584fa1ed2c7b9834d2afba224fef96dad537d95b 100644 (file)
@@ -3,6 +3,7 @@ package be.nikiroo.fanfix.library;
 import java.io.File;
 import java.io.FileFilter;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -210,13 +211,32 @@ public class LocalLibrary extends BasicLibrary {
        }
 
        @Override
-       public Image getSourceCover(String source) {
+       public synchronized Image getCustomSourceCover(String source) {
                if (sourceCovers == null) {
-                       getStories(null);
+                       sourceCovers = new HashMap<String, Image>();
+               }
+
+               Image img = sourceCovers.get(source);
+               if (img != null) {
+                       return img;
                }
 
-               if (!sourceCovers.containsKey(source)) {
-                       sourceCovers.put(source, super.getSourceCover(source));
+               File coverDir = new File(baseDir, source);
+               if (coverDir.isDirectory()) {
+                       File cover = new File(coverDir, ".cover.png");
+                       InputStream in;
+                       try {
+                               in = new FileInputStream(cover);
+                               try {
+                                       sourceCovers.put(source, new Image(in));
+                               } finally {
+                                       in.close();
+                               }
+                       } catch (FileNotFoundException e) {
+                               e.printStackTrace();
+                       } catch (IOException e) {
+                               e.printStackTrace();
+                       }
                }
 
                return sourceCovers.get(source);
@@ -224,18 +244,26 @@ public class LocalLibrary extends BasicLibrary {
 
        @Override
        public void setSourceCover(String source, String luid) {
-               if (sourceCovers == null) {
-                       getStories(null);
-               }
+               setSourceCover(source, getCover(luid));
+       }
 
-               sourceCovers.put(source, getCover(luid));
+       /**
+        * Fix the source cover to the given story cover.
+        * 
+        * @param source
+        *            the source to change
+        * @param coverImage
+        *            the cover image
+        */
+       synchronized void setSourceCover(String source, Image coverImage) {
                File cover = new File(getExpectedDir(source), ".cover");
                try {
-                       Instance.getCache().saveAsImage(sourceCovers.get(source), cover,
-                                       true);
+                       Instance.getCache().saveAsImage(coverImage, cover, true);
+                       if (sourceCovers != null) {
+                               sourceCovers.put(source, coverImage);
+                       }
                } catch (IOException e) {
                        Instance.getTraceHandler().error(e);
-                       sourceCovers.remove(source);
                }
        }
 
@@ -444,7 +472,6 @@ public class LocalLibrary extends BasicLibrary {
 
                if (stories == null) {
                        stories = new HashMap<MetaData, File[]>();
-                       sourceCovers = new HashMap<String, Image>();
 
                        lastId = 0;
 
@@ -473,13 +500,11 @@ public class LocalLibrary extends BasicLibrary {
                                        pgDirs.addProgress(pgFiles, 100);
                                        pgDirs.setName("Loading from: " + dir.getName());
 
-                                       String source = null;
                                        for (File infoFile : infoFiles) {
                                                pgFiles.setName(infoFile.getName());
                                                try {
                                                        MetaData meta = InfoReader
                                                                        .readMeta(infoFile, false);
-                                                       source = meta.getSource();
                                                        try {
                                                                int id = Integer.parseInt(meta.getLuid());
                                                                if (id > lastId) {
@@ -506,20 +531,6 @@ public class LocalLibrary extends BasicLibrary {
                                                pgFiles.add(1);
                                        }
 
-                                       File cover = new File(dir, ".cover.png");
-                                       if (cover.exists()) {
-                                               try {
-                                                       InputStream in = new FileInputStream(cover);
-                                                       try {
-                                                               sourceCovers.put(source, new Image(in));
-                                                       } finally {
-                                                               in.close();
-                                                       }
-                                               } catch (IOException e) {
-                                                       Instance.getTraceHandler().error(e);
-                                               }
-                                       }
-
                                        pgFiles.setName(null);
                                }
 
@@ -530,28 +541,4 @@ public class LocalLibrary extends BasicLibrary {
                pg.done();
                return stories;
        }
-
-       /**
-        * Fix the source cover to the given story cover.
-        * 
-        * @param source
-        *            the source to change
-        * @param coverImage
-        *            the cover image
-        */
-       void setSourceCover(String source, Image coverImage) {
-               if (sourceCovers == null) {
-                       getStories(null);
-               }
-
-               sourceCovers.put(source, coverImage);
-               File cover = new File(getExpectedDir(source), ".cover");
-               try {
-                       Instance.getCache().saveAsImage(sourceCovers.get(source), cover,
-                                       true);
-               } catch (IOException e) {
-                       Instance.getTraceHandler().error(e);
-                       sourceCovers.remove(source);
-               }
-       }
 }
index 44d39adbcc30d8b6867aaf5fd4961ac0dc00bac9..dc95f0d4ca761229e451ab714bb979155b771345 100644 (file)
@@ -119,15 +119,15 @@ public class RemoteLibrary extends BasicLibrary {
        }
 
        @Override
-       public Image getSourceCover(final String source) {
+       public Image getCustomSourceCover(final String source) {
                final Image[] result = new Image[1];
 
                try {
                        new ConnectActionClientObject(host, port, true) {
                                @Override
                                public void action(Version serverVersion) throws Exception {
-                                       Object rep = send(new Object[] { md5, "GET_SOURCE_COVER",
-                                                       source });
+                                       Object rep = send(new Object[] { md5,
+                                                       "GET_CUSTOM_SOURCE_COVER", source });
                                        result[0] = (Image) rep;
                                }
 
index 0378723a403da3942f02322fa0454c5b2c4a63e2..7d5a0ae10d988f1a79f52f7732dfb0411e5f057e 100644 (file)
@@ -41,7 +41,7 @@ import be.nikiroo.utils.serial.server.ServerObject;
  * the LUID</li>
  * <li>[md5] DELETE_STORY [luid]: delete the story of LUID luid</li>
  * <li>[md5] GET_COVER [luid]: return the cover of the story</li>
- * <li>[md5] GET_SOURCE_COVER [source]: return the cover for this source</li>
+ * <li>[md5] GET_CUSTOM_SOURCE_COVER [source]: return the cover for this source</li>
  * <li>[md5] SET_SOURCE_COVER [source], [luid]: set the default cover for the
  * given source to the cover of the story denoted by luid</li>
  * <li>[md5] CHANGE_SOURCE [luid] [new source]: change the source of the story
@@ -153,8 +153,8 @@ public class RemoteLibraryServer extends ServerObject {
                        Instance.getLibrary().delete((String) args[0]);
                } else if ("GET_COVER".equals(command)) {
                        return Instance.getLibrary().getCover((String) args[0]);
-               } else if ("GET_SOURCE_COVER".equals(command)) {
-                       return Instance.getLibrary().getSourceCover((String) args[0]);
+               } else if ("GET_CUSTOM_SOURCE_COVER".equals(command)) {
+                       return Instance.getLibrary().getCustomSourceCover((String) args[0]);
                } else if ("SET_SOURCE_COVER".equals(command)) {
                        Instance.getLibrary().setSourceCover((String) args[0],
                                        (String) args[1]);