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