X-Git-Url: http://git.nikiroo.be/?p=nikiroo-utils.git;a=blobdiff_plain;f=library%2FCacheLibrary.java;h=e184c1bd6555e7d79547ab61ab774d92e5b608ff;hp=694f9ec267501d546ba8b44413cfeb346ef8963d;hb=258e065f81071a861711ef935dca3ec5563f4360;hpb=371a36bd5f5d0e46038491ecfdd18653fe4d6f61 diff --git a/library/CacheLibrary.java b/library/CacheLibrary.java index 694f9ec..e184c1b 100644 --- a/library/CacheLibrary.java +++ b/library/CacheLibrary.java @@ -24,21 +24,27 @@ import be.nikiroo.utils.Progress; public class CacheLibrary extends BasicLibrary { private List metasReal; private List metasMixed; + private Object metasLock = new Object(); + private BasicLibrary lib; private LocalLibrary cacheLib; /** * Create a cache library around the given one. *

- * It will return the same result, but those will be saved to disk at the same - * time to be fetched quicker the next time. + * It will return the same result, but those will be saved to disk at the + * same time to be fetched quicker the next time. * - * @param cacheDir 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 + * @param cacheDir + * 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, UiConfigBundle config) { + 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); @@ -56,37 +62,42 @@ public class CacheLibrary extends BasicLibrary { } @Override - protected synchronized List getMetas(Progress pg) throws IOException { - // We make sure that cached metas have precedence - + protected List getMetas(Progress pg) throws IOException { if (pg == null) { pg = new Progress(); } - if (metasMixed == null) { - if (metasReal == null) { - metasReal = lib.getMetas(pg); - } - - metasMixed = new ArrayList(); - TreeSet cachedLuids = new TreeSet(); - 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); + List copy; + synchronized (metasLock) { + // We make sure that cached metas have precedence + if (metasMixed == null) { + if (metasReal == null) { + metasReal = lib.getMetas(pg); + } + + metasMixed = new ArrayList(); + TreeSet cachedLuids = new TreeSet(); + 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); + } } } + + copy = new ArrayList(metasMixed); } pg.done(); - return new ArrayList(metasMixed); + return copy; } @Override - public synchronized Story getStory(String luid, MetaData meta, Progress pg) throws IOException { + public synchronized Story getStory(String luid, MetaData meta, Progress pg) + throws IOException { if (pg == null) { pg = new Progress(); } @@ -119,7 +130,8 @@ public class CacheLibrary extends BasicLibrary { } @Override - public synchronized File getFile(final String luid, Progress pg) throws IOException { + public synchronized File getFile(final String luid, Progress pg) + throws IOException { if (pg == null) { pg = new Progress(); } @@ -225,8 +237,8 @@ public class CacheLibrary extends BasicLibrary { * Invalidate the {@link Story} cache (when the content has changed, but we * already have it) with the new given meta. *

- * Make sure to always use {@link MetaData} from the cached library - * in priority, here. + * Make sure to always use {@link MetaData} from the cached library in + * priority, here. * * @param meta * the {@link Story} to clear from the cache @@ -240,33 +252,37 @@ public class CacheLibrary extends BasicLibrary { 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 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); - return true; + if (!changed) { + metas.add(meta); + return true; + } } } - + return false; } @Override protected void invalidateInfo(String luid) { if (luid == null) { - metasReal = null; - metasMixed = null; + synchronized (metasLock) { + metasReal = null; + metasMixed = null; + } } else { invalidateInfo(metasReal, luid); invalidateInfo(metasMixed, luid); @@ -279,16 +295,19 @@ public class CacheLibrary extends BasicLibrary { // luid cannot be null private void invalidateInfo(List metas, String luid) { if (metas != null) { - for (int i = 0; i < metas.size(); i++) { - if (metas.get(i).getLuid().equals(luid)) { - metas.remove(i--); + 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 { + public synchronized Story save(Story story, String luid, Progress pg) + throws IOException { Progress pgLib = new Progress(); Progress pgCacheLib = new Progress(); @@ -302,7 +321,7 @@ public class CacheLibrary extends BasicLibrary { story = lib.save(story, luid, pgLib); updateMetaCache(metasReal, story.getMeta()); - + story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); updateMetaCache(metasMixed, story.getMeta()); @@ -320,8 +339,8 @@ public class CacheLibrary extends BasicLibrary { } @Override - protected synchronized void changeSTA(String luid, String newSource, String newTitle, String newAuthor, Progress pg) - throws IOException { + protected synchronized void changeSTA(String luid, String newSource, + String newTitle, String newAuthor, Progress pg) throws IOException { if (pg == null) { pg = new Progress(); } @@ -375,7 +394,7 @@ public class CacheLibrary extends BasicLibrary { } @Override - public synchronized MetaData imprt(URL url, Progress pg) throws IOException { + public MetaData imprt(URL url, Progress pg) throws IOException { if (pg == null) { pg = new Progress(); } @@ -389,6 +408,7 @@ public class CacheLibrary extends BasicLibrary { MetaData meta = lib.imprt(url, pgImprt); updateMetaCache(metasReal, meta); metasMixed = null; + clearFromCache(meta.getLuid()); pg.done(); @@ -399,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"); }