| 1 | package be.nikiroo.fanfix.library; |
| 2 | |
| 3 | import java.io.File; |
| 4 | import java.io.IOException; |
| 5 | import java.net.URL; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import be.nikiroo.fanfix.Instance; |
| 9 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 10 | import be.nikiroo.fanfix.data.MetaData; |
| 11 | import be.nikiroo.fanfix.data.Story; |
| 12 | import be.nikiroo.utils.Image; |
| 13 | import be.nikiroo.utils.Progress; |
| 14 | |
| 15 | /** |
| 16 | * This library will cache another pre-existing {@link BasicLibrary}. |
| 17 | * |
| 18 | * @author niki |
| 19 | */ |
| 20 | public class CacheLibrary extends BasicLibrary { |
| 21 | private List<MetaData> metas; |
| 22 | private BasicLibrary lib; |
| 23 | private LocalLibrary cacheLib; |
| 24 | |
| 25 | /** |
| 26 | * Create a cache library around the given one. |
| 27 | * <p> |
| 28 | * It will return the same result, but those will be saved to disk at the |
| 29 | * same time to be fetched quicker the next time. |
| 30 | * |
| 31 | * @param cacheDir |
| 32 | * the cache directory where to save the files to disk |
| 33 | * @param lib |
| 34 | * the original library to wrap |
| 35 | */ |
| 36 | public CacheLibrary(File cacheDir, BasicLibrary lib) { |
| 37 | this.cacheLib = new LocalLibrary(cacheDir, Instance.getUiConfig() |
| 38 | .getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance |
| 39 | .getUiConfig().getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), |
| 40 | true); |
| 41 | this.lib = lib; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public String getLibraryName() { |
| 46 | return lib.getLibraryName(); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public Status getStatus() { |
| 51 | return lib.getStatus(); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | protected List<MetaData> getMetas(Progress pg) { |
| 56 | if (pg == null) { |
| 57 | pg = new Progress(); |
| 58 | } |
| 59 | |
| 60 | if (metas == null) { |
| 61 | metas = lib.getMetas(pg); |
| 62 | } |
| 63 | |
| 64 | pg.done(); |
| 65 | return metas; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public synchronized File getFile(final String luid, Progress pg) { |
| 70 | if (pg == null) { |
| 71 | pg = new Progress(); |
| 72 | } |
| 73 | |
| 74 | Progress pgImport = new Progress(); |
| 75 | Progress pgGet = new Progress(); |
| 76 | Progress pgRecall = new Progress(); |
| 77 | |
| 78 | pg.setMinMax(0, 5); |
| 79 | pg.addProgress(pgImport, 3); |
| 80 | pg.addProgress(pgGet, 1); |
| 81 | pg.addProgress(pgRecall, 1); |
| 82 | |
| 83 | if (!isCached(luid)) { |
| 84 | try { |
| 85 | cacheLib.imprt(lib, luid, pgImport); |
| 86 | pgImport.done(); |
| 87 | invalidateInfo(luid); |
| 88 | } catch (IOException e) { |
| 89 | Instance.getTraceHandler().error(e); |
| 90 | } |
| 91 | |
| 92 | pgImport.done(); |
| 93 | pgGet.done(); |
| 94 | } |
| 95 | |
| 96 | File file = cacheLib.getFile(luid, pgRecall); |
| 97 | pgRecall.done(); |
| 98 | |
| 99 | pg.done(); |
| 100 | return file; |
| 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public Image getCover(final String luid) { |
| 105 | if (isCached(luid)) { |
| 106 | return cacheLib.getCover(luid); |
| 107 | } |
| 108 | |
| 109 | // We could update the cache here, but it's not easy |
| 110 | return lib.getCover(luid); |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public Image getSourceCover(String source) { |
| 115 | // no cache for the source cover |
| 116 | return lib.getSourceCover(source); |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public void setSourceCover(String source, String luid) { |
| 121 | lib.setSourceCover(source, luid); |
| 122 | cacheLib.setSourceCover(source, getSourceCover(source)); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | protected void invalidateInfo(String luid) { |
| 127 | List<MetaData> metas = this.metas; |
| 128 | |
| 129 | if (luid == null) { |
| 130 | this.metas = null; |
| 131 | } else if (metas != null) { |
| 132 | MetaData meta = lib.getInfo(luid); |
| 133 | for (int i = 0; i < metas.size(); i++) { |
| 134 | if (metas.get(i).getLuid().equals(luid)) { |
| 135 | if (meta != null) { |
| 136 | metas.set(i, meta); |
| 137 | meta = null; |
| 138 | } else { |
| 139 | metas.remove(i--); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if (meta != null) { |
| 145 | metas.add(meta); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | cacheLib.invalidateInfo(luid); |
| 150 | lib.invalidateInfo(luid); |
| 151 | } |
| 152 | |
| 153 | @Override |
| 154 | public synchronized Story save(Story story, String luid, Progress pg) |
| 155 | throws IOException { |
| 156 | Progress pgLib = new Progress(); |
| 157 | Progress pgCacheLib = new Progress(); |
| 158 | |
| 159 | if (pg == null) { |
| 160 | pg = new Progress(); |
| 161 | } |
| 162 | |
| 163 | pg.setMinMax(0, 2); |
| 164 | pg.addProgress(pgLib, 1); |
| 165 | pg.addProgress(pgCacheLib, 1); |
| 166 | |
| 167 | story = lib.save(story, luid, pgLib); |
| 168 | story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); |
| 169 | |
| 170 | invalidateInfo(story.getMeta().getLuid()); |
| 171 | |
| 172 | return story; |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public synchronized void delete(String luid) throws IOException { |
| 177 | if (isCached(luid)) { |
| 178 | cacheLib.delete(luid); |
| 179 | } |
| 180 | lib.delete(luid); |
| 181 | |
| 182 | List<MetaData> metas = this.metas; |
| 183 | if (metas != null) { |
| 184 | for (int i = 0; i < metas.size(); i++) { |
| 185 | if (metas.get(i).getLuid().equals(luid)) { |
| 186 | metas.set(i, lib.getInfo(luid)); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public synchronized void changeSource(String luid, String newSource, |
| 194 | Progress pg) throws IOException { |
| 195 | if (pg == null) { |
| 196 | pg = new Progress(); |
| 197 | } |
| 198 | |
| 199 | Progress pgCache = new Progress(); |
| 200 | Progress pgOrig = new Progress(); |
| 201 | pg.setMinMax(0, 2); |
| 202 | pg.addProgress(pgCache, 1); |
| 203 | pg.addProgress(pgOrig, 1); |
| 204 | |
| 205 | MetaData meta = getInfo(luid); |
| 206 | if (meta == null) { |
| 207 | throw new IOException("Story not found: " + luid); |
| 208 | } |
| 209 | |
| 210 | if (isCached(luid)) { |
| 211 | cacheLib.changeSource(luid, newSource, pgCache); |
| 212 | } |
| 213 | pgCache.done(); |
| 214 | |
| 215 | lib.changeSource(luid, newSource, pgOrig); |
| 216 | pgOrig.done(); |
| 217 | |
| 218 | meta.setSource(newSource); |
| 219 | pg.done(); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Check if the {@link Story} denoted by this Library UID is present in the |
| 224 | * cache. |
| 225 | * |
| 226 | * @param luid |
| 227 | * the Library UID |
| 228 | * |
| 229 | * @return TRUE if it is |
| 230 | */ |
| 231 | public boolean isCached(String luid) { |
| 232 | return cacheLib.getInfo(luid) != null; |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Clear the {@link Story} from the cache. |
| 237 | * |
| 238 | * @param luid |
| 239 | * the story to clear |
| 240 | * |
| 241 | * @throws IOException |
| 242 | * in case of I/O error |
| 243 | */ |
| 244 | public void clearFromCache(String luid) throws IOException { |
| 245 | if (isCached(luid)) { |
| 246 | cacheLib.delete(luid); |
| 247 | invalidateInfo(luid); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | @Override |
| 252 | public Story imprt(URL url, Progress pg) throws IOException { |
| 253 | if (pg == null) { |
| 254 | pg = new Progress(); |
| 255 | } |
| 256 | |
| 257 | Progress pgImprt = new Progress(); |
| 258 | Progress pgCache = new Progress(); |
| 259 | pg.setMinMax(0, 10); |
| 260 | pg.addProgress(pgImprt, 7); |
| 261 | pg.addProgress(pgCache, 3); |
| 262 | |
| 263 | Story story = lib.imprt(url, pgImprt); |
| 264 | cacheLib.save(story, story.getMeta().getLuid(), pgCache); |
| 265 | |
| 266 | pg.done(); |
| 267 | return story; |
| 268 | } |
| 269 | |
| 270 | // All the following methods are only used by Save and Delete in |
| 271 | // BasicLibrary: |
| 272 | |
| 273 | @Override |
| 274 | protected int getNextId() { |
| 275 | throw new java.lang.InternalError("Should not have been called"); |
| 276 | } |
| 277 | |
| 278 | @Override |
| 279 | protected void doDelete(String luid) throws IOException { |
| 280 | throw new java.lang.InternalError("Should not have been called"); |
| 281 | } |
| 282 | |
| 283 | @Override |
| 284 | protected Story doSave(Story story, Progress pg) throws IOException { |
| 285 | throw new java.lang.InternalError("Should not have been called"); |
| 286 | } |
| 287 | } |