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