Merge branch 'master' into subtree
[nikiroo-utils.git] / library / CacheLibrary.java
index e8743b63cd9d18ca0ef0ba3316aa204433b4bf34..e184c1bd6555e7d79547ab61ab774d92e5b608ff 100644 (file)
@@ -3,12 +3,16 @@ package be.nikiroo.fanfix.library;
 import java.io.File;
 import java.io.IOException;
 import java.net.URL;
+import java.util.ArrayList;
 import java.util.List;
+import java.util.TreeSet;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.bundles.UiConfig;
+import be.nikiroo.fanfix.bundles.UiConfigBundle;
 import be.nikiroo.fanfix.data.MetaData;
 import be.nikiroo.fanfix.data.Story;
+import be.nikiroo.fanfix.output.BasicOutput.OutputType;
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.Progress;
 
@@ -18,7 +22,10 @@ import be.nikiroo.utils.Progress;
  * @author niki
  */
 public class CacheLibrary extends BasicLibrary {
-       private List<MetaData> metas;
+       private List<MetaData> metasReal;
+       private List<MetaData> metasMixed;
+       private Object metasLock = new Object();
+
        private BasicLibrary lib;
        private LocalLibrary cacheLib;
 
@@ -32,12 +39,15 @@ public class CacheLibrary extends BasicLibrary {
         *            the cache directory where to save the files to disk
         * @param lib
         *            the original library to wrap
+        * @param config
+        *            the configuration used to know which kind of default
+        *            {@link OutputType} to use for images and non-images stories
         */
-       public CacheLibrary(File cacheDir, BasicLibrary lib) {
-               this.cacheLib = new LocalLibrary(cacheDir, Instance.getUiConfig()
-                               .getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance
-                               .getUiConfig().getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE),
-                               true);
+       public CacheLibrary(File cacheDir, BasicLibrary lib,
+                       UiConfigBundle config) {
+               this.cacheLib = new LocalLibrary(cacheDir, //
+                               config.getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE),
+                               config.getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), true);
                this.lib = lib;
        }
 
@@ -57,22 +67,32 @@ public class CacheLibrary extends BasicLibrary {
                        pg = new Progress();
                }
 
-               if (metas == null) {
-                       metas = lib.getMetas(pg);
-               }
+               List<MetaData> copy;
+               synchronized (metasLock) {
+                       // We make sure that cached metas have precedence
+                       if (metasMixed == null) {
+                               if (metasReal == null) {
+                                       metasReal = lib.getMetas(pg);
+                               }
 
-               pg.done();
-               return metas;
-       }
+                               metasMixed = new ArrayList<MetaData>();
+                               TreeSet<String> cachedLuids = new TreeSet<String>();
+                               for (MetaData cachedMeta : cacheLib.getMetas(null)) {
+                                       metasMixed.add(cachedMeta);
+                                       cachedLuids.add(cachedMeta.getLuid());
+                               }
+                               for (MetaData realMeta : metasReal) {
+                                       if (!cachedLuids.contains(realMeta.getLuid())) {
+                                               metasMixed.add(realMeta);
+                                       }
+                               }
+                       }
 
-       @Override
-       public synchronized MetaData getInfo(String luid) throws IOException {
-               MetaData info = cacheLib.getInfo(luid);
-               if (info == null) {
-                       info = lib.getInfo(luid);
+                       copy = new ArrayList<MetaData>(metasMixed);
                }
 
-               return info;
+               pg.done();
+               return copy;
        }
 
        @Override
@@ -92,10 +112,10 @@ public class CacheLibrary extends BasicLibrary {
                if (!isCached(luid)) {
                        try {
                                cacheLib.imprt(lib, luid, pgImport);
-                               updateInfo(cacheLib.getInfo(luid));
+                               updateMetaCache(metasMixed, cacheLib.getInfo(luid));
                                pgImport.done();
                        } catch (IOException e) {
-                               Instance.getTraceHandler().error(e);
+                               Instance.getInstance().getTraceHandler().error(e);
                        }
 
                        pgImport.done();
@@ -213,42 +233,78 @@ public class CacheLibrary extends BasicLibrary {
                cacheLib.setAuthorCover(author, getCover(luid));
        }
 
+       /**
+        * Invalidate the {@link Story} cache (when the content has changed, but we
+        * already have it) with the new given meta.
+        * <p>
+        * <b>Make sure to always use {@link MetaData} from the cached library in
+        * priority, here.</b>
+        * 
+        * @param meta
+        *            the {@link Story} to clear from the cache
+        * 
+        * @throws IOException
+        *             in case of IOException
+        */
        @Override
+       @Deprecated
        protected void updateInfo(MetaData meta) throws IOException {
+               throw new IOException(
+                               "This method is not supported in a CacheLibrary, please use updateMetaCache");
+       }
+
+       // relplace the meta in Metas by Meta, add it if needed
+       // return TRUE = added
+       private boolean updateMetaCache(List<MetaData> metas, MetaData meta) {
                if (meta != null && metas != null) {
-                       boolean changed = false;
-                       for (int i = 0; i < metas.size(); i++) {
-                               if (metas.get(i).getLuid().equals(meta.getLuid())) {
-                                       metas.set(i, meta);
-                                       changed = true;
+                       synchronized (metasLock) {
+                               boolean changed = false;
+                               for (int i = 0; i < metas.size(); i++) {
+                                       if (metas.get(i).getLuid().equals(meta.getLuid())) {
+                                               metas.set(i, meta);
+                                               changed = true;
+                                       }
                                }
-                       }
 
-                       if (!changed) {
-                               metas.add(meta);
+                               if (!changed) {
+                                       metas.add(meta);
+                                       return true;
+                               }
                        }
                }
 
-               cacheLib.updateInfo(meta);
-               lib.updateInfo(meta);
+               return false;
        }
 
        @Override
        protected void invalidateInfo(String luid) {
                if (luid == null) {
-                       metas = null;
-               } else if (metas != null) {
-                       for (int i = 0; i < metas.size(); i++) {
-                               if (metas.get(i).getLuid().equals(luid)) {
-                                       metas.remove(i--);
-                               }
+                       synchronized (metasLock) {
+                               metasReal = null;
+                               metasMixed = null;
                        }
+               } else {
+                       invalidateInfo(metasReal, luid);
+                       invalidateInfo(metasMixed, luid);
                }
 
                cacheLib.invalidateInfo(luid);
                lib.invalidateInfo(luid);
        }
 
+       // luid cannot be null
+       private void invalidateInfo(List<MetaData> metas, String luid) {
+               if (metas != null) {
+                       synchronized (metasLock) {
+                               for (int i = 0; i < metas.size(); i++) {
+                                       if (metas.get(i).getLuid().equals(luid)) {
+                                               metas.remove(i--);
+                                       }
+                               }
+                       }
+               }
+       }
+
        @Override
        public synchronized Story save(Story story, String luid, Progress pg)
                        throws IOException {
@@ -264,9 +320,10 @@ public class CacheLibrary extends BasicLibrary {
                pg.addProgress(pgCacheLib, 1);
 
                story = lib.save(story, luid, pgLib);
-               story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib);
+               updateMetaCache(metasReal, story.getMeta());
 
-               updateInfo(story.getMeta());
+               story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib);
+               updateMetaCache(metasMixed, story.getMeta());
 
                return story;
        }
@@ -312,18 +369,15 @@ public class CacheLibrary extends BasicLibrary {
                meta.setAuthor(newAuthor);
                pg.done();
 
-               invalidateInfo(luid);
+               if (isCached(luid)) {
+                       updateMetaCache(metasMixed, meta);
+                       updateMetaCache(metasReal, lib.getInfo(luid));
+               } else {
+                       updateMetaCache(metasReal, meta);
+               }
        }
 
-       /**
-        * Check if the {@link Story} denoted by this Library UID is present in the
-        * cache.
-        * 
-        * @param luid
-        *            the Library UID
-        * 
-        * @return TRUE if it is
-        */
+       @Override
        public boolean isCached(String luid) {
                try {
                        return cacheLib.getInfo(luid) != null;
@@ -332,18 +386,7 @@ public class CacheLibrary extends BasicLibrary {
                }
        }
 
-       /**
-        * Clear the {@link Story} from the cache.
-        * <p>
-        * The next time we try to retrieve the {@link Story}, it may be required to
-        * cache it again.
-        * 
-        * @param luid
-        *            the story to clear
-        * 
-        * @throws IOException
-        *             in case of I/O error
-        */
+       @Override
        public void clearFromCache(String luid) throws IOException {
                if (isCached(luid)) {
                        cacheLib.delete(luid);
@@ -363,10 +406,11 @@ public class CacheLibrary extends BasicLibrary {
                pg.addProgress(pgCache, 3);
 
                MetaData meta = lib.imprt(url, pgImprt);
-               updateInfo(meta);
-               
+               updateMetaCache(metasReal, meta);
+               metasMixed = null;
+
                clearFromCache(meta.getLuid());
-               
+
                pg.done();
                return meta;
        }
@@ -375,7 +419,7 @@ public class CacheLibrary extends BasicLibrary {
        // BasicLibrary:
 
        @Override
-       protected int getNextId() {
+       protected String getNextId() {
                throw new java.lang.InternalError("Should not have been called");
        }