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