From e272f05fd49f01e9fb6bd71c7b74a32839bbcc38 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sat, 2 Dec 2017 21:30:46 +0100 Subject: [PATCH] Invalidate info for one luid instead of all --- .../nikiroo/fanfix/library/BasicLibrary.java | 20 ++++- .../nikiroo/fanfix/library/CacheLibrary.java | 54 +++++++++--- .../nikiroo/fanfix/library/LocalLibrary.java | 13 +-- .../nikiroo/fanfix/library/RemoteLibrary.java | 85 +++++++++++++------ .../fanfix/library/RemoteLibraryServer.java | 11 ++- 5 files changed, 132 insertions(+), 51 deletions(-) diff --git a/src/be/nikiroo/fanfix/library/BasicLibrary.java b/src/be/nikiroo/fanfix/library/BasicLibrary.java index df283db..9e7d464 100644 --- a/src/be/nikiroo/fanfix/library/BasicLibrary.java +++ b/src/be/nikiroo/fanfix/library/BasicLibrary.java @@ -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); } /** diff --git a/src/be/nikiroo/fanfix/library/CacheLibrary.java b/src/be/nikiroo/fanfix/library/CacheLibrary.java index 2ef5b1e..7f4bb37 100644 --- a/src/be/nikiroo/fanfix/library/CacheLibrary.java +++ b/src/be/nikiroo/fanfix/library/CacheLibrary.java @@ -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 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 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); } } diff --git a/src/be/nikiroo/fanfix/library/LocalLibrary.java b/src/be/nikiroo/fanfix/library/LocalLibrary.java index e6018df..92676b3 100644 --- a/src/be/nikiroo/fanfix/library/LocalLibrary.java +++ b/src/be/nikiroo/fanfix/library/LocalLibrary.java @@ -131,7 +131,7 @@ public class LocalLibrary extends BasicLibrary { } @Override - protected void clearCache() { + protected void invalidateInfo(String luid) { stories = null; sourceCovers = new HashMap(); } @@ -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}. *

* Will use a cached list when possible (see - * {@link BasicLibrary#clearCache()}). + * {@link BasicLibrary#invalidateInfo()}). * * @param pg * the optional {@link Progress} diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index 10cc44d..326a488 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -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 metas = getMetasList(luid, null); + if (!metas.isEmpty()) { + return metas.get(0); + } + + return null; + } + @Override protected List 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 getMetasList(final String luid, Progress pg) { final Progress pgF = pg; final List metas = new ArrayList(); @@ -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"); - } } diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java index adef7ed..dae73e4 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java @@ -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; *

    *
  • [md5] PING: will return PONG if the key is accepted
  • *
  • [md5] GET_METADATA *: will return the metadata of all the stories in the - * library
  • + * library (array) + * * + *
  • [md5] GET_METADATA [luid]: will return the metadata of the story of LUID + * luid
  • *
  • [md5] GET_STORY [luid]: will return the given story if it exists (or NULL * if not)
  • *
  • [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(); -- 2.27.0