Invalidate info for one luid instead of all
authorNiki Roo <niki@nikiroo.be>
Sat, 2 Dec 2017 20:30:46 +0000 (21:30 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 2 Dec 2017 20:30:46 +0000 (21:30 +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 df283db258304a475714156de90abec37f344945..9e7d4644494e5b51d871091868c3c1e47d882f53 100644 (file)
@@ -135,7 +135,19 @@ abstract public class BasicLibrary {
         * Invalidate the {@link Story} cache (when the content should be re-read
         * because it was changed).
         */
-       protected abstract void clearCache();
+       protected void invalidateInfo() {
+               invalidateInfo(null);
+       }
+
+       /**
+        * Invalidate the {@link Story} cache (when the content should be re-read
+        * because it was changed).
+        * 
+        * @param luid
+        *            the luid of the {@link Story} to clear from the cache, or NULL
+        *            for all stories
+        */
+       protected abstract void invalidateInfo(String luid);
 
        /**
         * Return the next LUID that can be used.
@@ -505,13 +517,13 @@ abstract public class BasicLibrary {
                        meta.setLuid(luid);
                }
 
-               if (getInfo(luid) != null) {
+               if (luid != null && getInfo(luid) != null) {
                        delete(luid);
                }
 
                doSave(story, pg);
 
-               clearCache();
+               invalidateInfo(luid);
 
                return story;
        }
@@ -527,7 +539,7 @@ abstract public class BasicLibrary {
         */
        public synchronized void delete(String luid) throws IOException {
                doDelete(luid);
-               clearCache();
+               invalidateInfo(luid);
        }
 
        /**
index 2ef5b1ebaf1febb82fd2566290deb2648f794138..7f4bb370d5a4fb0ad98e1c4c0882390cb5374512 100644 (file)
@@ -84,7 +84,7 @@ public class CacheLibrary extends BasicLibrary {
                        try {
                                cacheLib.imprt(lib, luid, pgImport);
                                pgImport.done();
-                               clearCache();
+                               invalidateInfo(luid);
                        } catch (IOException e) {
                                Instance.getTraceHandler().error(e);
                        }
@@ -123,10 +123,31 @@ public class CacheLibrary extends BasicLibrary {
        }
 
        @Override
-       protected void clearCache() {
-               metas = null;
-               cacheLib.clearCache();
-               lib.clearCache();
+       protected void invalidateInfo(String luid) {
+               List<MetaData> metas = this.metas;
+
+               if (luid == null) {
+                       this.metas = null;
+               } else if (metas != null) {
+                       MetaData meta = lib.getInfo(luid);
+                       for (int i = 0; i < metas.size(); i++) {
+                               if (metas.get(i).getLuid().equals(luid)) {
+                                       if (meta != null) {
+                                               metas.set(i, meta);
+                                               meta = null;
+                                       } else {
+                                               metas.remove(i--);
+                                       }
+                               }
+                       }
+
+                       if (meta != null) {
+                               metas.add(meta);
+                       }
+               }
+
+               cacheLib.invalidateInfo(luid);
+               lib.invalidateInfo(luid);
        }
 
        @Override
@@ -146,7 +167,7 @@ public class CacheLibrary extends BasicLibrary {
                story = lib.save(story, luid, pgLib);
                story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib);
 
-               clearCache();
+               invalidateInfo(story.getMeta().getLuid());
 
                return story;
        }
@@ -157,7 +178,15 @@ public class CacheLibrary extends BasicLibrary {
                        cacheLib.delete(luid);
                }
                lib.delete(luid);
-               clearCache();
+
+               List<MetaData> metas = this.metas;
+               if (metas != null) {
+                       for (int i = 0; i < metas.size(); i++) {
+                               if (metas.get(i).getLuid().equals(luid)) {
+                                       metas.set(i, lib.getInfo(luid));
+                               }
+                       }
+               }
        }
 
        @Override
@@ -173,15 +202,20 @@ public class CacheLibrary extends BasicLibrary {
                pg.addProgress(pgCache, 1);
                pg.addProgress(pgOrig, 1);
 
+               MetaData meta = getInfo(luid);
+               if (meta == null) {
+                       throw new IOException("Story not found: " + luid);
+               }
+
                if (isCached(luid)) {
                        cacheLib.changeSource(luid, newSource, pgCache);
                }
                pgCache.done();
+
                lib.changeSource(luid, newSource, pgOrig);
                pgOrig.done();
 
-               getInfo(luid).setSource(newSource);
-
+               meta.setSource(newSource);
                pg.done();
        }
 
@@ -210,7 +244,7 @@ public class CacheLibrary extends BasicLibrary {
        public void clearFromCache(String luid) throws IOException {
                if (isCached(luid)) {
                        cacheLib.delete(luid);
-                       clearCache();
+                       invalidateInfo(luid);
                }
        }
 
index e6018dfc20654a0f9b109032a1acf427586da6d5..92676b3530288eac94244b9b84c4f23c24bd82c8 100644 (file)
@@ -131,7 +131,7 @@ public class LocalLibrary extends BasicLibrary {
        }
 
        @Override
-       protected void clearCache() {
+       protected void invalidateInfo(String luid) {
                stories = null;
                sourceCovers = new HashMap<String, BufferedImage>();
        }
@@ -147,6 +147,7 @@ public class LocalLibrary extends BasicLibrary {
                for (File file : getRelatedFiles(luid)) {
                        // TODO: throw an IOException if we cannot delete the files?
                        IOUtils.deltree(file);
+                       file.getParentFile().delete();
                }
        }
 
@@ -182,15 +183,17 @@ public class LocalLibrary extends BasicLibrary {
                                                        "\\.info$", "");
                                        InfoCover.writeInfo(newDir, name, meta);
                                        relatedFile.delete();
+                                       relatedFile.getParentFile().delete();
                                } catch (IOException e) {
                                        Instance.getTraceHandler().error(e);
                                }
                        } else {
                                relatedFile.renameTo(new File(newDir, relatedFile.getName()));
+                               relatedFile.getParentFile().delete();
                        }
                }
 
-               clearCache();
+               invalidateInfo();
        }
 
        @Override
@@ -261,7 +264,7 @@ public class LocalLibrary extends BasicLibrary {
                                        pg.add(1);
                                }
 
-                               clearCache();
+                               invalidateInfo();
                                pg.done();
                                return;
                        }
@@ -269,7 +272,7 @@ public class LocalLibrary extends BasicLibrary {
 
                super.imprt(other, luid, pg);
 
-               clearCache();
+               invalidateInfo();
        }
 
        /**
@@ -404,7 +407,7 @@ public class LocalLibrary extends BasicLibrary {
         * {@link LocalLibrary#baseDir}.
         * <p>
         * Will use a cached list when possible (see
-        * {@link BasicLibrary#clearCache()}).
+        * {@link BasicLibrary#invalidateInfo()}).
         * 
         * @param pg
         *            the optional {@link Progress}
index 10cc44de423db239067e7318a667d7dd1e2df468..326a488b1766a8fc9ddf5052dae8c8fdb32b11cc 100644 (file)
@@ -233,8 +233,7 @@ public class RemoteLibrary extends BasicLibrary {
                }.connect();
 
                // because the meta changed:
-               clearCache();
-               refresh(pgRefresh);
+               invalidateInfo(luidSaved[0]);
 
                MetaData meta = getInfo(luidSaved[0]);
                meta.setCover(story.getMeta().getCover());
@@ -357,8 +356,6 @@ public class RemoteLibrary extends BasicLibrary {
 
                                                rep = send(null);
                                        }
-
-                                       getInfo(luid).setSource(newSource);
                                }
 
                                @Override
@@ -398,8 +395,57 @@ public class RemoteLibrary extends BasicLibrary {
                }
        }
 
+       @Override
+       public synchronized MetaData getInfo(String luid) {
+               List<MetaData> metas = getMetasList(luid, null);
+               if (!metas.isEmpty()) {
+                       return metas.get(0);
+               }
+
+               return null;
+       }
+
        @Override
        protected List<MetaData> getMetas(Progress pg) {
+               return getMetasList("*", pg);
+       }
+
+       @Override
+       protected void invalidateInfo(String luid) {
+       }
+
+       // The following methods are only used by Save and Delete in BasicLibrary:
+
+       @Override
+       protected int getNextId() {
+               throw new java.lang.InternalError("Should not have been called");
+       }
+
+       @Override
+       protected void doDelete(String luid) throws IOException {
+               throw new java.lang.InternalError("Should not have been called");
+       }
+
+       @Override
+       protected Story doSave(Story story, Progress pg) throws IOException {
+               throw new java.lang.InternalError("Should not have been called");
+       }
+
+       //
+
+       /**
+        * Return the meta of the given story or a list of all known metas if the
+        * luid is "*".
+        * 
+        * @param luid
+        *            the luid of the story or *
+        * @param pg
+        *            the optional progress
+        * 
+        * 
+        * @return the metas
+        */
+       private List<MetaData> getMetasList(final String luid, Progress pg) {
                final Progress pgF = pg;
                final List<MetaData> metas = new ArrayList<MetaData>();
 
@@ -412,7 +458,7 @@ public class RemoteLibrary extends BasicLibrary {
                                                pg = new Progress();
                                        }
 
-                                       Object rep = send(new Object[] { md5, "GET_METADATA", "*" });
+                                       Object rep = send(new Object[] { md5, "GET_METADATA", luid });
 
                                        while (true) {
                                                if (!RemoteLibraryServer.updateProgress(pg, rep)) {
@@ -422,8 +468,12 @@ public class RemoteLibrary extends BasicLibrary {
                                                rep = send(null);
                                        }
 
-                                       for (MetaData meta : (MetaData[]) rep) {
-                                               metas.add(meta);
+                                       if (rep instanceof MetaData[]) {
+                                               for (MetaData meta : (MetaData[]) rep) {
+                                                       metas.add(meta);
+                                               }
+                                       } else if (rep != null) {
+                                               metas.add((MetaData) rep);
                                        }
                                }
 
@@ -438,25 +488,4 @@ public class RemoteLibrary extends BasicLibrary {
 
                return metas;
        }
-
-       @Override
-       protected void clearCache() {
-       }
-
-       // The following methods are only used by Save and Delete in BasicLibrary:
-
-       @Override
-       protected int getNextId() {
-               throw new java.lang.InternalError("Should not have been called");
-       }
-
-       @Override
-       protected void doDelete(String luid) throws IOException {
-               throw new java.lang.InternalError("Should not have been called");
-       }
-
-       @Override
-       protected Story doSave(Story story, Progress pg) throws IOException {
-               throw new java.lang.InternalError("Should not have been called");
-       }
 }
index adef7edea06e5ca7f9d0bbc0ff1323252f8f7ff5..dae73e4b4c4e0c4c787b8c05ecb19ab34f362ceb 100644 (file)
@@ -2,7 +2,6 @@ package be.nikiroo.fanfix.library;
 
 import java.io.IOException;
 import java.net.URL;
-import java.security.InvalidParameterException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -29,7 +28,10 @@ import be.nikiroo.utils.serial.server.ServerObject;
  * <ul>
  * <li>[md5] PING: will return PONG if the key is accepted</li>
  * <li>[md5] GET_METADATA *: will return the metadata of all the stories in the
- * library</li>
+ * library (array)</li>
+ * *
+ * <li>[md5] GET_METADATA [luid]: will return the metadata of the story of LUID
+ * luid</li>
  * <li>[md5] GET_STORY [luid]: will return the given story if it exists (or NULL
  * if not)</li>
  * <li>[md5] SAVE_STORY [luid]: save the story (that must be sent just after the
@@ -108,8 +110,9 @@ public class RemoteLibraryServer extends ServerObject {
                                                createPgForwarder(action));
                                return metas.toArray(new MetaData[] {});
                        }
-                       throw new InvalidParameterException(
-                                       "only * is valid here, but you passed: " + args[0]);
+
+                       return new MetaData[] { Instance.getLibrary().getInfo(
+                                       (String) args[0]) };
                } else if ("GET_STORY".equals(command)) {
                        MetaData meta = Instance.getLibrary().getInfo((String) args[0]);
                        meta = meta.clone();