| 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.ArrayList; |
| 7 | import java.util.List; |
| 8 | import java.util.TreeSet; |
| 9 | |
| 10 | import be.nikiroo.fanfix.Instance; |
| 11 | import be.nikiroo.fanfix.bundles.UiConfig; |
| 12 | import be.nikiroo.fanfix.bundles.UiConfigBundle; |
| 13 | import be.nikiroo.fanfix.data.MetaData; |
| 14 | import be.nikiroo.fanfix.data.Story; |
| 15 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
| 16 | import be.nikiroo.utils.Image; |
| 17 | import be.nikiroo.utils.Progress; |
| 18 | |
| 19 | /** |
| 20 | * This library will cache another pre-existing {@link BasicLibrary}. |
| 21 | * |
| 22 | * @author niki |
| 23 | */ |
| 24 | public class CacheLibrary extends BasicLibrary { |
| 25 | private List<MetaData> metasReal; |
| 26 | private List<MetaData> metasMixed; |
| 27 | private BasicLibrary lib; |
| 28 | private LocalLibrary cacheLib; |
| 29 | |
| 30 | /** |
| 31 | * Create a cache library around the given one. |
| 32 | * <p> |
| 33 | * It will return the same result, but those will be saved to disk at the same |
| 34 | * time to be fetched quicker the next time. |
| 35 | * |
| 36 | * @param cacheDir the cache directory where to save the files to disk |
| 37 | * @param lib the original library to wrap |
| 38 | * @param config the configuration used to know which kind of default |
| 39 | * {@link OutputType} to use for images and non-images stories |
| 40 | */ |
| 41 | public CacheLibrary(File cacheDir, BasicLibrary lib, UiConfigBundle config) { |
| 42 | this.cacheLib = new LocalLibrary(cacheDir, // |
| 43 | config.getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), |
| 44 | config.getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), true); |
| 45 | this.lib = lib; |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public String getLibraryName() { |
| 50 | return lib.getLibraryName(); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Status getStatus() { |
| 55 | return lib.getStatus(); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected synchronized List<MetaData> getMetas(Progress pg) throws IOException { |
| 60 | // We make sure that cached metas have precedence |
| 61 | |
| 62 | if (pg == null) { |
| 63 | pg = new Progress(); |
| 64 | } |
| 65 | |
| 66 | if (metasMixed == null) { |
| 67 | if (metasReal == null) { |
| 68 | metasReal = lib.getMetas(pg); |
| 69 | } |
| 70 | |
| 71 | metasMixed = new ArrayList<MetaData>(); |
| 72 | TreeSet<String> cachedLuids = new TreeSet<String>(); |
| 73 | for (MetaData cachedMeta : cacheLib.getMetas(null)) { |
| 74 | metasMixed.add(cachedMeta); |
| 75 | cachedLuids.add(cachedMeta.getLuid()); |
| 76 | } |
| 77 | for (MetaData realMeta : metasReal) { |
| 78 | if (!cachedLuids.contains(realMeta.getLuid())) { |
| 79 | metasMixed.add(realMeta); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pg.done(); |
| 85 | return new ArrayList<MetaData>(metasMixed); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public synchronized Story getStory(String luid, MetaData meta, Progress pg) throws IOException { |
| 90 | if (pg == null) { |
| 91 | pg = new Progress(); |
| 92 | } |
| 93 | |
| 94 | Progress pgImport = new Progress(); |
| 95 | Progress pgGet = new Progress(); |
| 96 | |
| 97 | pg.setMinMax(0, 4); |
| 98 | pg.addProgress(pgImport, 3); |
| 99 | pg.addProgress(pgGet, 1); |
| 100 | |
| 101 | if (!isCached(luid)) { |
| 102 | try { |
| 103 | cacheLib.imprt(lib, luid, pgImport); |
| 104 | updateMetaCache(metasMixed, cacheLib.getInfo(luid)); |
| 105 | pgImport.done(); |
| 106 | } catch (IOException e) { |
| 107 | Instance.getInstance().getTraceHandler().error(e); |
| 108 | } |
| 109 | |
| 110 | pgImport.done(); |
| 111 | pgGet.done(); |
| 112 | } |
| 113 | |
| 114 | String type = cacheLib.getOutputType(meta.isImageDocument()); |
| 115 | MetaData cachedMeta = meta.clone(); |
| 116 | cachedMeta.setType(type); |
| 117 | |
| 118 | return cacheLib.getStory(luid, cachedMeta, pg); |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public synchronized File getFile(final String luid, Progress pg) throws IOException { |
| 123 | if (pg == null) { |
| 124 | pg = new Progress(); |
| 125 | } |
| 126 | |
| 127 | Progress pgGet = new Progress(); |
| 128 | Progress pgRecall = new Progress(); |
| 129 | |
| 130 | pg.setMinMax(0, 5); |
| 131 | pg.addProgress(pgGet, 4); |
| 132 | pg.addProgress(pgRecall, 1); |
| 133 | |
| 134 | if (!isCached(luid)) { |
| 135 | getStory(luid, pgGet); |
| 136 | pgGet.done(); |
| 137 | } |
| 138 | |
| 139 | File file = cacheLib.getFile(luid, pgRecall); |
| 140 | pgRecall.done(); |
| 141 | |
| 142 | pg.done(); |
| 143 | return file; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | public Image getCover(final String luid) throws IOException { |
| 148 | if (isCached(luid)) { |
| 149 | return cacheLib.getCover(luid); |
| 150 | } |
| 151 | |
| 152 | // We could update the cache here, but it's not easy |
| 153 | return lib.getCover(luid); |
| 154 | } |
| 155 | |
| 156 | @Override |
| 157 | public Image getSourceCover(String source) throws IOException { |
| 158 | Image custom = getCustomSourceCover(source); |
| 159 | if (custom != null) { |
| 160 | return custom; |
| 161 | } |
| 162 | |
| 163 | Image cached = cacheLib.getSourceCover(source); |
| 164 | if (cached != null) { |
| 165 | return cached; |
| 166 | } |
| 167 | |
| 168 | return lib.getSourceCover(source); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public Image getAuthorCover(String author) throws IOException { |
| 173 | Image custom = getCustomAuthorCover(author); |
| 174 | if (custom != null) { |
| 175 | return custom; |
| 176 | } |
| 177 | |
| 178 | Image cached = cacheLib.getAuthorCover(author); |
| 179 | if (cached != null) { |
| 180 | return cached; |
| 181 | } |
| 182 | |
| 183 | return lib.getAuthorCover(author); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public Image getCustomSourceCover(String source) throws IOException { |
| 188 | Image custom = cacheLib.getCustomSourceCover(source); |
| 189 | if (custom == null) { |
| 190 | custom = lib.getCustomSourceCover(source); |
| 191 | if (custom != null) { |
| 192 | cacheLib.setSourceCover(source, custom); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return custom; |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public Image getCustomAuthorCover(String author) throws IOException { |
| 201 | Image custom = cacheLib.getCustomAuthorCover(author); |
| 202 | if (custom == null) { |
| 203 | custom = lib.getCustomAuthorCover(author); |
| 204 | if (custom != null) { |
| 205 | cacheLib.setAuthorCover(author, custom); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return custom; |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public void setSourceCover(String source, String luid) throws IOException { |
| 214 | lib.setSourceCover(source, luid); |
| 215 | cacheLib.setSourceCover(source, getCover(luid)); |
| 216 | } |
| 217 | |
| 218 | @Override |
| 219 | public void setAuthorCover(String author, String luid) throws IOException { |
| 220 | lib.setAuthorCover(author, luid); |
| 221 | cacheLib.setAuthorCover(author, getCover(luid)); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Invalidate the {@link Story} cache (when the content has changed, but we |
| 226 | * already have it) with the new given meta. |
| 227 | * <p> |
| 228 | * <b>Make sure to always use {@link MetaData} from the cached library |
| 229 | * in priority, here.</b> |
| 230 | * |
| 231 | * @param meta |
| 232 | * the {@link Story} to clear from the cache |
| 233 | * |
| 234 | * @throws IOException |
| 235 | * in case of IOException |
| 236 | */ |
| 237 | @Override |
| 238 | @Deprecated |
| 239 | protected void updateInfo(MetaData meta) throws IOException { |
| 240 | throw new IOException( |
| 241 | "This method is not supported in a CacheLibrary, please use updateMetaCache"); |
| 242 | } |
| 243 | |
| 244 | // relplace the meta in Metas by Meta, add it if needed |
| 245 | // return TRUE = added |
| 246 | private boolean updateMetaCache(List<MetaData> metas, MetaData meta) { |
| 247 | if (meta != null && metas != null) { |
| 248 | boolean changed = false; |
| 249 | for (int i = 0; i < metas.size(); i++) { |
| 250 | if (metas.get(i).getLuid().equals(meta.getLuid())) { |
| 251 | metas.set(i, meta); |
| 252 | changed = true; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | if (!changed) { |
| 257 | metas.add(meta); |
| 258 | return true; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | return false; |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | protected void invalidateInfo(String luid) { |
| 267 | if (luid == null) { |
| 268 | metasReal = null; |
| 269 | metasMixed = null; |
| 270 | } else { |
| 271 | invalidateInfo(metasReal, luid); |
| 272 | invalidateInfo(metasMixed, luid); |
| 273 | } |
| 274 | |
| 275 | cacheLib.invalidateInfo(luid); |
| 276 | lib.invalidateInfo(luid); |
| 277 | } |
| 278 | |
| 279 | // luid cannot be null |
| 280 | private void invalidateInfo(List<MetaData> metas, String luid) { |
| 281 | if (metas != null) { |
| 282 | for (int i = 0; i < metas.size(); i++) { |
| 283 | if (metas.get(i).getLuid().equals(luid)) { |
| 284 | metas.remove(i--); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public synchronized Story save(Story story, String luid, Progress pg) throws IOException { |
| 292 | Progress pgLib = new Progress(); |
| 293 | Progress pgCacheLib = new Progress(); |
| 294 | |
| 295 | if (pg == null) { |
| 296 | pg = new Progress(); |
| 297 | } |
| 298 | |
| 299 | pg.setMinMax(0, 2); |
| 300 | pg.addProgress(pgLib, 1); |
| 301 | pg.addProgress(pgCacheLib, 1); |
| 302 | |
| 303 | story = lib.save(story, luid, pgLib); |
| 304 | updateMetaCache(metasReal, story.getMeta()); |
| 305 | |
| 306 | story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); |
| 307 | updateMetaCache(metasMixed, story.getMeta()); |
| 308 | |
| 309 | return story; |
| 310 | } |
| 311 | |
| 312 | @Override |
| 313 | public synchronized void delete(String luid) throws IOException { |
| 314 | if (isCached(luid)) { |
| 315 | cacheLib.delete(luid); |
| 316 | } |
| 317 | lib.delete(luid); |
| 318 | |
| 319 | invalidateInfo(luid); |
| 320 | } |
| 321 | |
| 322 | @Override |
| 323 | protected synchronized void changeSTA(String luid, String newSource, String newTitle, String newAuthor, Progress pg) |
| 324 | throws IOException { |
| 325 | if (pg == null) { |
| 326 | pg = new Progress(); |
| 327 | } |
| 328 | |
| 329 | Progress pgCache = new Progress(); |
| 330 | Progress pgOrig = new Progress(); |
| 331 | pg.setMinMax(0, 2); |
| 332 | pg.addProgress(pgCache, 1); |
| 333 | pg.addProgress(pgOrig, 1); |
| 334 | |
| 335 | MetaData meta = getInfo(luid); |
| 336 | if (meta == null) { |
| 337 | throw new IOException("Story not found: " + luid); |
| 338 | } |
| 339 | |
| 340 | if (isCached(luid)) { |
| 341 | cacheLib.changeSTA(luid, newSource, newTitle, newAuthor, pgCache); |
| 342 | } |
| 343 | pgCache.done(); |
| 344 | |
| 345 | lib.changeSTA(luid, newSource, newTitle, newAuthor, pgOrig); |
| 346 | pgOrig.done(); |
| 347 | |
| 348 | meta.setSource(newSource); |
| 349 | meta.setTitle(newTitle); |
| 350 | meta.setAuthor(newAuthor); |
| 351 | pg.done(); |
| 352 | |
| 353 | if (isCached(luid)) { |
| 354 | updateMetaCache(metasMixed, meta); |
| 355 | updateMetaCache(metasReal, lib.getInfo(luid)); |
| 356 | } else { |
| 357 | updateMetaCache(metasReal, meta); |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | @Override |
| 362 | public boolean isCached(String luid) { |
| 363 | try { |
| 364 | return cacheLib.getInfo(luid) != null; |
| 365 | } catch (IOException e) { |
| 366 | return false; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | @Override |
| 371 | public void clearFromCache(String luid) throws IOException { |
| 372 | if (isCached(luid)) { |
| 373 | cacheLib.delete(luid); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | @Override |
| 378 | public synchronized MetaData imprt(URL url, Progress pg) throws IOException { |
| 379 | if (pg == null) { |
| 380 | pg = new Progress(); |
| 381 | } |
| 382 | |
| 383 | Progress pgImprt = new Progress(); |
| 384 | Progress pgCache = new Progress(); |
| 385 | pg.setMinMax(0, 10); |
| 386 | pg.addProgress(pgImprt, 7); |
| 387 | pg.addProgress(pgCache, 3); |
| 388 | |
| 389 | MetaData meta = lib.imprt(url, pgImprt); |
| 390 | updateMetaCache(metasReal, meta); |
| 391 | metasMixed = null; |
| 392 | clearFromCache(meta.getLuid()); |
| 393 | |
| 394 | pg.done(); |
| 395 | return meta; |
| 396 | } |
| 397 | |
| 398 | // All the following methods are only used by Save and Delete in |
| 399 | // BasicLibrary: |
| 400 | |
| 401 | @Override |
| 402 | protected int getNextId() { |
| 403 | throw new java.lang.InternalError("Should not have been called"); |
| 404 | } |
| 405 | |
| 406 | @Override |
| 407 | protected void doDelete(String luid) throws IOException { |
| 408 | throw new java.lang.InternalError("Should not have been called"); |
| 409 | } |
| 410 | |
| 411 | @Override |
| 412 | protected Story doSave(Story story, Progress pg) throws IOException { |
| 413 | throw new java.lang.InternalError("Should not have been called"); |
| 414 | } |
| 415 | } |