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