From e1de8087ab1623f7624018af905ea3bb0ef45802 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 3 Mar 2019 17:59:24 +0100 Subject: [PATCH] try better perf for GetCover --- .../nikiroo/fanfix/library/BasicLibrary.java | 22 ++++- .../nikiroo/fanfix/library/CacheLibrary.java | 21 ++++- .../nikiroo/fanfix/library/LocalLibrary.java | 91 ++++++++----------- .../nikiroo/fanfix/library/RemoteLibrary.java | 6 +- .../fanfix/library/RemoteLibraryServer.java | 6 +- 5 files changed, 85 insertions(+), 61 deletions(-) diff --git a/src/be/nikiroo/fanfix/library/BasicLibrary.java b/src/be/nikiroo/fanfix/library/BasicLibrary.java index 129c8fb..d753267 100644 --- a/src/be/nikiroo/fanfix/library/BasicLibrary.java +++ b/src/be/nikiroo/fanfix/library/BasicLibrary.java @@ -94,7 +94,8 @@ abstract public class BasicLibrary { /** * Return the cover image associated to this source. *

- * 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 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. + *

+ * 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. * diff --git a/src/be/nikiroo/fanfix/library/CacheLibrary.java b/src/be/nikiroo/fanfix/library/CacheLibrary.java index 977331f..29920e8 100644 --- a/src/be/nikiroo/fanfix/library/CacheLibrary.java +++ b/src/be/nikiroo/fanfix/library/CacheLibrary.java @@ -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 diff --git a/src/be/nikiroo/fanfix/library/LocalLibrary.java b/src/be/nikiroo/fanfix/library/LocalLibrary.java index 838c97c..584fa1e 100644 --- a/src/be/nikiroo/fanfix/library/LocalLibrary.java +++ b/src/be/nikiroo/fanfix/library/LocalLibrary.java @@ -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(); + } + + 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(); - sourceCovers = new HashMap(); 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); - } - } } diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index 44d39ad..dc95f0d 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -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; } diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java index 0378723..7d5a0ae 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java @@ -41,7 +41,7 @@ import be.nikiroo.utils.serial.server.ServerObject; * the LUID *

  • [md5] DELETE_STORY [luid]: delete the story of LUID luid
  • *
  • [md5] GET_COVER [luid]: return the cover of the story
  • - *
  • [md5] GET_SOURCE_COVER [source]: return the cover for this source
  • + *
  • [md5] GET_CUSTOM_SOURCE_COVER [source]: return the cover for this source
  • *
  • [md5] SET_SOURCE_COVER [source], [luid]: set the default cover for the * given source to the cover of the story denoted by luid
  • *
  • [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]); -- 2.27.0