Commit | Line | Data |
---|---|---|
ff05b828 NR |
1 | package be.nikiroo.fanfix.library; |
2 | ||
ff05b828 NR |
3 | import java.io.File; |
4 | import java.io.IOException; | |
edf79e5e | 5 | import java.net.URL; |
ff05b828 NR |
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; | |
16a81ef7 | 12 | import be.nikiroo.utils.Image; |
ff05b828 NR |
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 | ||
e6249b0f NR |
49 | @Override |
50 | public Status getStatus() { | |
51 | return lib.getStatus(); | |
52 | } | |
53 | ||
ff05b828 | 54 | @Override |
0bb51c9c | 55 | protected List<MetaData> getMetas(Progress pg) throws IOException { |
ff05b828 NR |
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 | ||
3828c808 | 68 | @Override |
0bb51c9c | 69 | public synchronized MetaData getInfo(String luid) throws IOException { |
3828c808 NR |
70 | MetaData info = cacheLib.getInfo(luid); |
71 | if (info == null) { | |
72 | info = lib.getInfo(luid); | |
73 | } | |
74 | ||
75 | return info; | |
76 | } | |
77 | ||
60f72311 | 78 | @Override |
0bb51c9c NR |
79 | public synchronized Story getStory(String luid, MetaData meta, Progress pg) |
80 | throws IOException { | |
ff05b828 NR |
81 | if (pg == null) { |
82 | pg = new Progress(); | |
83 | } | |
84 | ||
85 | Progress pgImport = new Progress(); | |
86 | Progress pgGet = new Progress(); | |
ff05b828 | 87 | |
d4449e96 | 88 | pg.setMinMax(0, 4); |
ff05b828 NR |
89 | pg.addProgress(pgImport, 3); |
90 | pg.addProgress(pgGet, 1); | |
ff05b828 NR |
91 | |
92 | if (!isCached(luid)) { | |
93 | try { | |
94 | cacheLib.imprt(lib, luid, pgImport); | |
efa3c511 | 95 | updateInfo(cacheLib.getInfo(luid)); |
ff05b828 | 96 | pgImport.done(); |
ff05b828 | 97 | } catch (IOException e) { |
62c63b07 | 98 | Instance.getTraceHandler().error(e); |
ff05b828 NR |
99 | } |
100 | ||
101 | pgImport.done(); | |
102 | pgGet.done(); | |
103 | } | |
104 | ||
d4449e96 NR |
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 | |
0bb51c9c NR |
113 | public synchronized File getFile(final String luid, Progress pg) |
114 | throws IOException { | |
d4449e96 NR |
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 | ||
ff05b828 NR |
131 | File file = cacheLib.getFile(luid, pgRecall); |
132 | pgRecall.done(); | |
133 | ||
134 | pg.done(); | |
135 | return file; | |
136 | } | |
137 | ||
138 | @Override | |
0bb51c9c | 139 | public Image getCover(final String luid) throws IOException { |
ff05b828 NR |
140 | if (isCached(luid)) { |
141 | return cacheLib.getCover(luid); | |
142 | } | |
143 | ||
085a2f9a | 144 | // We could update the cache here, but it's not easy |
ff05b828 NR |
145 | return lib.getCover(luid); |
146 | } | |
147 | ||
085a2f9a | 148 | @Override |
0bb51c9c | 149 | public Image getSourceCover(String source) throws IOException { |
e1de8087 NR |
150 | Image custom = getCustomSourceCover(source); |
151 | if (custom != null) { | |
152 | return custom; | |
153 | } | |
154 | ||
cf45a4c4 NR |
155 | Image cached = cacheLib.getSourceCover(source); |
156 | if (cached != null) { | |
157 | return cached; | |
158 | } | |
159 | ||
160 | return lib.getSourceCover(source); | |
e1de8087 NR |
161 | } |
162 | ||
3989dfc5 | 163 | @Override |
0bb51c9c | 164 | public Image getAuthorCover(String author) throws IOException { |
c956ff52 | 165 | Image custom = getCustomAuthorCover(author); |
3989dfc5 NR |
166 | if (custom != null) { |
167 | return custom; | |
168 | } | |
169 | ||
c956ff52 | 170 | Image cached = cacheLib.getAuthorCover(author); |
3989dfc5 NR |
171 | if (cached != null) { |
172 | return cached; | |
173 | } | |
174 | ||
c956ff52 | 175 | return lib.getAuthorCover(author); |
3989dfc5 NR |
176 | } |
177 | ||
e1de8087 | 178 | @Override |
0bb51c9c | 179 | public Image getCustomSourceCover(String source) throws IOException { |
e1de8087 NR |
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; | |
085a2f9a | 189 | } |
c956ff52 | 190 | |
3989dfc5 | 191 | @Override |
0bb51c9c | 192 | public Image getCustomAuthorCover(String author) throws IOException { |
3989dfc5 NR |
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 | } | |
085a2f9a NR |
203 | |
204 | @Override | |
0bb51c9c | 205 | public void setSourceCover(String source, String luid) throws IOException { |
085a2f9a | 206 | lib.setSourceCover(source, luid); |
0dc195de | 207 | cacheLib.setSourceCover(source, getCover(luid)); |
085a2f9a NR |
208 | } |
209 | ||
3989dfc5 | 210 | @Override |
0bb51c9c | 211 | public void setAuthorCover(String author, String luid) throws IOException { |
3989dfc5 NR |
212 | lib.setAuthorCover(author, luid); |
213 | cacheLib.setAuthorCover(author, getCover(luid)); | |
214 | } | |
215 | ||
ff05b828 | 216 | @Override |
0bb51c9c | 217 | protected void updateInfo(MetaData meta) throws IOException { |
b56c9d60 | 218 | if (meta != null && metas != null) { |
efa3c511 NR |
219 | for (int i = 0; i < metas.size(); i++) { |
220 | if (metas.get(i).getLuid().equals(meta.getLuid())) { | |
221 | metas.set(i, meta); | |
222 | } | |
223 | } | |
224 | } | |
225 | ||
226 | cacheLib.updateInfo(meta); | |
227 | lib.updateInfo(meta); | |
228 | } | |
229 | ||
230 | @Override | |
c8d48938 | 231 | protected void invalidateInfo(String luid) { |
e272f05f | 232 | if (luid == null) { |
cbd62024 | 233 | metas = null; |
e272f05f | 234 | } else if (metas != null) { |
e272f05f NR |
235 | for (int i = 0; i < metas.size(); i++) { |
236 | if (metas.get(i).getLuid().equals(luid)) { | |
cbd62024 | 237 | metas.remove(i--); |
e272f05f NR |
238 | } |
239 | } | |
e272f05f NR |
240 | } |
241 | ||
c8d48938 NR |
242 | cacheLib.invalidateInfo(luid); |
243 | lib.invalidateInfo(luid); | |
ff05b828 NR |
244 | } |
245 | ||
246 | @Override | |
247 | public synchronized Story save(Story story, String luid, Progress pg) | |
248 | throws IOException { | |
03c1cede NR |
249 | Progress pgLib = new Progress(); |
250 | Progress pgCacheLib = new Progress(); | |
251 | ||
252 | if (pg == null) { | |
253 | pg = new Progress(); | |
254 | } | |
255 | ||
256 | pg.setMinMax(0, 2); | |
257 | pg.addProgress(pgLib, 1); | |
258 | pg.addProgress(pgCacheLib, 1); | |
259 | ||
260 | story = lib.save(story, luid, pgLib); | |
0fa0fe95 | 261 | story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); |
03c1cede | 262 | |
efa3c511 | 263 | updateInfo(story.getMeta()); |
03c1cede | 264 | |
ff05b828 NR |
265 | return story; |
266 | } | |
267 | ||
268 | @Override | |
269 | public synchronized void delete(String luid) throws IOException { | |
085a2f9a NR |
270 | if (isCached(luid)) { |
271 | cacheLib.delete(luid); | |
272 | } | |
ff05b828 | 273 | lib.delete(luid); |
e272f05f | 274 | |
3828c808 | 275 | invalidateInfo(luid); |
ff05b828 NR |
276 | } |
277 | ||
ff05b828 | 278 | @Override |
c8d48938 NR |
279 | protected synchronized void changeSTA(String luid, String newSource, |
280 | String newTitle, String newAuthor, Progress pg) throws IOException { | |
ff05b828 NR |
281 | if (pg == null) { |
282 | pg = new Progress(); | |
283 | } | |
284 | ||
285 | Progress pgCache = new Progress(); | |
286 | Progress pgOrig = new Progress(); | |
287 | pg.setMinMax(0, 2); | |
288 | pg.addProgress(pgCache, 1); | |
289 | pg.addProgress(pgOrig, 1); | |
290 | ||
e272f05f NR |
291 | MetaData meta = getInfo(luid); |
292 | if (meta == null) { | |
293 | throw new IOException("Story not found: " + luid); | |
294 | } | |
295 | ||
e06632ee | 296 | if (isCached(luid)) { |
c8d48938 | 297 | cacheLib.changeSTA(luid, newSource, newTitle, newAuthor, pgCache); |
e06632ee | 298 | } |
ff05b828 | 299 | pgCache.done(); |
e272f05f | 300 | |
c8d48938 | 301 | lib.changeSTA(luid, newSource, newTitle, newAuthor, pgOrig); |
ff05b828 NR |
302 | pgOrig.done(); |
303 | ||
e272f05f | 304 | meta.setSource(newSource); |
c8d48938 NR |
305 | meta.setTitle(newTitle); |
306 | meta.setAuthor(newAuthor); | |
ff05b828 | 307 | pg.done(); |
3828c808 NR |
308 | |
309 | invalidateInfo(luid); | |
ff05b828 NR |
310 | } |
311 | ||
312 | /** | |
313 | * Check if the {@link Story} denoted by this Library UID is present in the | |
314 | * cache. | |
315 | * | |
316 | * @param luid | |
317 | * the Library UID | |
318 | * | |
319 | * @return TRUE if it is | |
320 | */ | |
321 | public boolean isCached(String luid) { | |
0bb51c9c NR |
322 | try { |
323 | return cacheLib.getInfo(luid) != null; | |
324 | } catch (IOException e) { | |
325 | return false; | |
326 | } | |
ff05b828 NR |
327 | } |
328 | ||
329 | /** | |
330 | * Clear the {@link Story} from the cache. | |
c3b229a1 NR |
331 | * <p> |
332 | * The next time we try to retrieve the {@link Story}, it may be required to | |
333 | * cache it again. | |
ff05b828 NR |
334 | * |
335 | * @param luid | |
336 | * the story to clear | |
337 | * | |
338 | * @throws IOException | |
339 | * in case of I/O error | |
340 | */ | |
341 | public void clearFromCache(String luid) throws IOException { | |
e06632ee NR |
342 | if (isCached(luid)) { |
343 | cacheLib.delete(luid); | |
e06632ee | 344 | } |
ff05b828 NR |
345 | } |
346 | ||
edf79e5e NR |
347 | @Override |
348 | public Story imprt(URL url, Progress pg) throws IOException { | |
349 | if (pg == null) { | |
350 | pg = new Progress(); | |
351 | } | |
352 | ||
353 | Progress pgImprt = new Progress(); | |
354 | Progress pgCache = new Progress(); | |
355 | pg.setMinMax(0, 10); | |
356 | pg.addProgress(pgImprt, 7); | |
357 | pg.addProgress(pgCache, 3); | |
358 | ||
359 | Story story = lib.imprt(url, pgImprt); | |
360 | cacheLib.save(story, story.getMeta().getLuid(), pgCache); | |
361 | ||
efa3c511 | 362 | updateInfo(story.getMeta()); |
cbd62024 | 363 | |
edf79e5e NR |
364 | pg.done(); |
365 | return story; | |
366 | } | |
367 | ||
ff05b828 NR |
368 | // All the following methods are only used by Save and Delete in |
369 | // BasicLibrary: | |
370 | ||
371 | @Override | |
372 | protected int getNextId() { | |
373 | throw new java.lang.InternalError("Should not have been called"); | |
374 | } | |
375 | ||
376 | @Override | |
377 | protected void doDelete(String luid) throws IOException { | |
378 | throw new java.lang.InternalError("Should not have been called"); | |
379 | } | |
380 | ||
381 | @Override | |
382 | protected Story doSave(Story story, Progress pg) throws IOException { | |
383 | throw new java.lang.InternalError("Should not have been called"); | |
384 | } | |
385 | } |