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