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; |
1f2a7d5f | 6 | import java.util.ArrayList; |
ff05b828 | 7 | import java.util.List; |
ef98466f | 8 | import java.util.TreeSet; |
ff05b828 NR |
9 | |
10 | import be.nikiroo.fanfix.Instance; | |
11 | import be.nikiroo.fanfix.bundles.UiConfig; | |
d66deb8d | 12 | import be.nikiroo.fanfix.bundles.UiConfigBundle; |
ff05b828 NR |
13 | import be.nikiroo.fanfix.data.MetaData; |
14 | import be.nikiroo.fanfix.data.Story; | |
d66deb8d | 15 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
16a81ef7 | 16 | import be.nikiroo.utils.Image; |
ff05b828 NR |
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 { | |
ef98466f NR |
25 | private List<MetaData> metasReal; |
26 | private List<MetaData> metasMixed; | |
ff05b828 NR |
27 | private BasicLibrary lib; |
28 | private LocalLibrary cacheLib; | |
29 | ||
30 | /** | |
31 | * Create a cache library around the given one. | |
32 | * <p> | |
d66deb8d NR |
33 | * It will return the same result, but those will be saved to disk at the same |
34 | * time to be fetched quicker the next time. | |
ff05b828 | 35 | * |
d66deb8d NR |
36 | * @param cacheDir the cache directory where to save the files to disk |
37 | * @param lib the original library to wrap | |
38 | * @param config the configuration used to know which kind of default | |
39 | * {@link OutputType} to use for images and non-images stories | |
ff05b828 | 40 | */ |
d66deb8d NR |
41 | public CacheLibrary(File cacheDir, BasicLibrary lib, UiConfigBundle config) { |
42 | this.cacheLib = new LocalLibrary(cacheDir, // | |
43 | config.getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), | |
44 | config.getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), true); | |
ff05b828 NR |
45 | this.lib = lib; |
46 | } | |
47 | ||
48 | @Override | |
49 | public String getLibraryName() { | |
50 | return lib.getLibraryName(); | |
51 | } | |
52 | ||
e6249b0f NR |
53 | @Override |
54 | public Status getStatus() { | |
55 | return lib.getStatus(); | |
56 | } | |
57 | ||
ff05b828 | 58 | @Override |
ef98466f NR |
59 | protected synchronized List<MetaData> getMetas(Progress pg) throws IOException { |
60 | // We make sure that cached metas have precedence | |
61 | ||
ff05b828 NR |
62 | if (pg == null) { |
63 | pg = new Progress(); | |
64 | } | |
65 | ||
ef98466f NR |
66 | if (metasMixed == null) { |
67 | if (metasReal == null) { | |
68 | metasReal = lib.getMetas(pg); | |
69 | } | |
70 | ||
71 | metasMixed = new ArrayList<MetaData>(); | |
72 | TreeSet<String> cachedLuids = new TreeSet<String>(); | |
73 | for (MetaData cachedMeta : cacheLib.getMetas(null)) { | |
74 | metasMixed.add(cachedMeta); | |
75 | cachedLuids.add(cachedMeta.getLuid()); | |
76 | } | |
77 | for (MetaData realMeta : metasReal) { | |
78 | if (!cachedLuids.contains(realMeta.getLuid())) { | |
79 | metasMixed.add(realMeta); | |
80 | } | |
81 | } | |
ff05b828 NR |
82 | } |
83 | ||
84 | pg.done(); | |
ef98466f | 85 | return new ArrayList<MetaData>(metasMixed); |
3828c808 NR |
86 | } |
87 | ||
60f72311 | 88 | @Override |
ef98466f | 89 | public synchronized Story getStory(String luid, MetaData meta, Progress pg) throws IOException { |
ff05b828 NR |
90 | if (pg == null) { |
91 | pg = new Progress(); | |
92 | } | |
93 | ||
94 | Progress pgImport = new Progress(); | |
95 | Progress pgGet = new Progress(); | |
ff05b828 | 96 | |
d4449e96 | 97 | pg.setMinMax(0, 4); |
ff05b828 NR |
98 | pg.addProgress(pgImport, 3); |
99 | pg.addProgress(pgGet, 1); | |
ff05b828 NR |
100 | |
101 | if (!isCached(luid)) { | |
102 | try { | |
103 | cacheLib.imprt(lib, luid, pgImport); | |
ef98466f | 104 | updateMetaCache(metasMixed, cacheLib.getInfo(luid)); |
ff05b828 | 105 | pgImport.done(); |
ff05b828 | 106 | } catch (IOException e) { |
d66deb8d | 107 | Instance.getInstance().getTraceHandler().error(e); |
ff05b828 NR |
108 | } |
109 | ||
110 | pgImport.done(); | |
111 | pgGet.done(); | |
112 | } | |
113 | ||
d4449e96 NR |
114 | String type = cacheLib.getOutputType(meta.isImageDocument()); |
115 | MetaData cachedMeta = meta.clone(); | |
116 | cachedMeta.setType(type); | |
117 | ||
118 | return cacheLib.getStory(luid, cachedMeta, pg); | |
119 | } | |
120 | ||
121 | @Override | |
ef98466f | 122 | public synchronized File getFile(final String luid, Progress pg) throws IOException { |
d4449e96 NR |
123 | if (pg == null) { |
124 | pg = new Progress(); | |
125 | } | |
126 | ||
127 | Progress pgGet = new Progress(); | |
128 | Progress pgRecall = new Progress(); | |
129 | ||
130 | pg.setMinMax(0, 5); | |
131 | pg.addProgress(pgGet, 4); | |
132 | pg.addProgress(pgRecall, 1); | |
133 | ||
134 | if (!isCached(luid)) { | |
135 | getStory(luid, pgGet); | |
136 | pgGet.done(); | |
137 | } | |
138 | ||
ff05b828 NR |
139 | File file = cacheLib.getFile(luid, pgRecall); |
140 | pgRecall.done(); | |
141 | ||
142 | pg.done(); | |
143 | return file; | |
144 | } | |
145 | ||
146 | @Override | |
0bb51c9c | 147 | public Image getCover(final String luid) throws IOException { |
ff05b828 NR |
148 | if (isCached(luid)) { |
149 | return cacheLib.getCover(luid); | |
150 | } | |
151 | ||
085a2f9a | 152 | // We could update the cache here, but it's not easy |
ff05b828 NR |
153 | return lib.getCover(luid); |
154 | } | |
155 | ||
085a2f9a | 156 | @Override |
0bb51c9c | 157 | public Image getSourceCover(String source) throws IOException { |
e1de8087 NR |
158 | Image custom = getCustomSourceCover(source); |
159 | if (custom != null) { | |
160 | return custom; | |
161 | } | |
162 | ||
cf45a4c4 NR |
163 | Image cached = cacheLib.getSourceCover(source); |
164 | if (cached != null) { | |
165 | return cached; | |
166 | } | |
167 | ||
168 | return lib.getSourceCover(source); | |
e1de8087 NR |
169 | } |
170 | ||
3989dfc5 | 171 | @Override |
0bb51c9c | 172 | public Image getAuthorCover(String author) throws IOException { |
c956ff52 | 173 | Image custom = getCustomAuthorCover(author); |
3989dfc5 NR |
174 | if (custom != null) { |
175 | return custom; | |
176 | } | |
177 | ||
c956ff52 | 178 | Image cached = cacheLib.getAuthorCover(author); |
3989dfc5 NR |
179 | if (cached != null) { |
180 | return cached; | |
181 | } | |
182 | ||
c956ff52 | 183 | return lib.getAuthorCover(author); |
3989dfc5 NR |
184 | } |
185 | ||
e1de8087 | 186 | @Override |
0bb51c9c | 187 | public Image getCustomSourceCover(String source) throws IOException { |
e1de8087 NR |
188 | Image custom = cacheLib.getCustomSourceCover(source); |
189 | if (custom == null) { | |
190 | custom = lib.getCustomSourceCover(source); | |
191 | if (custom != null) { | |
192 | cacheLib.setSourceCover(source, custom); | |
193 | } | |
194 | } | |
195 | ||
196 | return custom; | |
085a2f9a | 197 | } |
c956ff52 | 198 | |
3989dfc5 | 199 | @Override |
0bb51c9c | 200 | public Image getCustomAuthorCover(String author) throws IOException { |
3989dfc5 NR |
201 | Image custom = cacheLib.getCustomAuthorCover(author); |
202 | if (custom == null) { | |
203 | custom = lib.getCustomAuthorCover(author); | |
204 | if (custom != null) { | |
205 | cacheLib.setAuthorCover(author, custom); | |
206 | } | |
207 | } | |
208 | ||
209 | return custom; | |
210 | } | |
085a2f9a NR |
211 | |
212 | @Override | |
0bb51c9c | 213 | public void setSourceCover(String source, String luid) throws IOException { |
085a2f9a | 214 | lib.setSourceCover(source, luid); |
0dc195de | 215 | cacheLib.setSourceCover(source, getCover(luid)); |
085a2f9a NR |
216 | } |
217 | ||
3989dfc5 | 218 | @Override |
0bb51c9c | 219 | public void setAuthorCover(String author, String luid) throws IOException { |
3989dfc5 NR |
220 | lib.setAuthorCover(author, luid); |
221 | cacheLib.setAuthorCover(author, getCover(luid)); | |
222 | } | |
223 | ||
ef98466f NR |
224 | /** |
225 | * Invalidate the {@link Story} cache (when the content has changed, but we | |
226 | * already have it) with the new given meta. | |
227 | * <p> | |
228 | * <b>Make sure to always use {@link MetaData} from the cached library | |
229 | * in priority, here.</b> | |
230 | * | |
231 | * @param meta | |
232 | * the {@link Story} to clear from the cache | |
233 | * | |
234 | * @throws IOException | |
235 | * in case of IOException | |
236 | */ | |
ff05b828 | 237 | @Override |
ef98466f | 238 | @Deprecated |
0bb51c9c | 239 | protected void updateInfo(MetaData meta) throws IOException { |
ef98466f NR |
240 | throw new IOException( |
241 | "This method is not supported in a CacheLibrary, please use updateMetaCache"); | |
242 | } | |
243 | ||
244 | // relplace the meta in Metas by Meta, add it if needed | |
245 | // return TRUE = added | |
246 | private boolean updateMetaCache(List<MetaData> metas, MetaData meta) { | |
b56c9d60 | 247 | if (meta != null && metas != null) { |
c747c1f2 | 248 | boolean changed = false; |
efa3c511 NR |
249 | for (int i = 0; i < metas.size(); i++) { |
250 | if (metas.get(i).getLuid().equals(meta.getLuid())) { | |
251 | metas.set(i, meta); | |
c747c1f2 | 252 | changed = true; |
efa3c511 NR |
253 | } |
254 | } | |
c747c1f2 NR |
255 | |
256 | if (!changed) { | |
257 | metas.add(meta); | |
ef98466f | 258 | return true; |
c747c1f2 | 259 | } |
efa3c511 | 260 | } |
ef98466f NR |
261 | |
262 | return false; | |
efa3c511 NR |
263 | } |
264 | ||
265 | @Override | |
c8d48938 | 266 | protected void invalidateInfo(String luid) { |
e272f05f | 267 | if (luid == null) { |
ef98466f NR |
268 | metasReal = null; |
269 | metasMixed = null; | |
270 | } else { | |
271 | invalidateInfo(metasReal, luid); | |
272 | invalidateInfo(metasMixed, luid); | |
273 | } | |
274 | ||
275 | cacheLib.invalidateInfo(luid); | |
276 | lib.invalidateInfo(luid); | |
277 | } | |
278 | ||
279 | // luid cannot be null | |
280 | private void invalidateInfo(List<MetaData> metas, String luid) { | |
281 | if (metas != null) { | |
e272f05f NR |
282 | for (int i = 0; i < metas.size(); i++) { |
283 | if (metas.get(i).getLuid().equals(luid)) { | |
cbd62024 | 284 | metas.remove(i--); |
e272f05f NR |
285 | } |
286 | } | |
e272f05f | 287 | } |
ff05b828 NR |
288 | } |
289 | ||
290 | @Override | |
ef98466f | 291 | public synchronized Story save(Story story, String luid, Progress pg) throws IOException { |
03c1cede NR |
292 | Progress pgLib = new Progress(); |
293 | Progress pgCacheLib = new Progress(); | |
294 | ||
295 | if (pg == null) { | |
296 | pg = new Progress(); | |
297 | } | |
298 | ||
299 | pg.setMinMax(0, 2); | |
300 | pg.addProgress(pgLib, 1); | |
301 | pg.addProgress(pgCacheLib, 1); | |
302 | ||
303 | story = lib.save(story, luid, pgLib); | |
ef98466f NR |
304 | updateMetaCache(metasReal, story.getMeta()); |
305 | ||
0fa0fe95 | 306 | story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); |
ef98466f | 307 | updateMetaCache(metasMixed, story.getMeta()); |
03c1cede | 308 | |
ff05b828 NR |
309 | return story; |
310 | } | |
311 | ||
312 | @Override | |
313 | public synchronized void delete(String luid) throws IOException { | |
085a2f9a NR |
314 | if (isCached(luid)) { |
315 | cacheLib.delete(luid); | |
316 | } | |
ff05b828 | 317 | lib.delete(luid); |
e272f05f | 318 | |
3828c808 | 319 | invalidateInfo(luid); |
ff05b828 NR |
320 | } |
321 | ||
ff05b828 | 322 | @Override |
ef98466f NR |
323 | protected synchronized void changeSTA(String luid, String newSource, String newTitle, String newAuthor, Progress pg) |
324 | throws IOException { | |
ff05b828 NR |
325 | if (pg == null) { |
326 | pg = new Progress(); | |
327 | } | |
328 | ||
329 | Progress pgCache = new Progress(); | |
330 | Progress pgOrig = new Progress(); | |
331 | pg.setMinMax(0, 2); | |
332 | pg.addProgress(pgCache, 1); | |
333 | pg.addProgress(pgOrig, 1); | |
334 | ||
e272f05f NR |
335 | MetaData meta = getInfo(luid); |
336 | if (meta == null) { | |
337 | throw new IOException("Story not found: " + luid); | |
338 | } | |
339 | ||
e06632ee | 340 | if (isCached(luid)) { |
c8d48938 | 341 | cacheLib.changeSTA(luid, newSource, newTitle, newAuthor, pgCache); |
e06632ee | 342 | } |
ff05b828 | 343 | pgCache.done(); |
e272f05f | 344 | |
c8d48938 | 345 | lib.changeSTA(luid, newSource, newTitle, newAuthor, pgOrig); |
ff05b828 NR |
346 | pgOrig.done(); |
347 | ||
e272f05f | 348 | meta.setSource(newSource); |
c8d48938 NR |
349 | meta.setTitle(newTitle); |
350 | meta.setAuthor(newAuthor); | |
ff05b828 | 351 | pg.done(); |
3828c808 | 352 | |
ef98466f NR |
353 | if (isCached(luid)) { |
354 | updateMetaCache(metasMixed, meta); | |
355 | updateMetaCache(metasReal, lib.getInfo(luid)); | |
356 | } else { | |
357 | updateMetaCache(metasReal, meta); | |
358 | } | |
ff05b828 NR |
359 | } |
360 | ||
4452446c | 361 | @Override |
ff05b828 | 362 | public boolean isCached(String luid) { |
0bb51c9c NR |
363 | try { |
364 | return cacheLib.getInfo(luid) != null; | |
365 | } catch (IOException e) { | |
366 | return false; | |
367 | } | |
ff05b828 NR |
368 | } |
369 | ||
4452446c | 370 | @Override |
ff05b828 | 371 | public void clearFromCache(String luid) throws IOException { |
e06632ee NR |
372 | if (isCached(luid)) { |
373 | cacheLib.delete(luid); | |
e06632ee | 374 | } |
ff05b828 NR |
375 | } |
376 | ||
edf79e5e | 377 | @Override |
ef98466f | 378 | public synchronized MetaData imprt(URL url, Progress pg) throws IOException { |
edf79e5e NR |
379 | if (pg == null) { |
380 | pg = new Progress(); | |
381 | } | |
382 | ||
383 | Progress pgImprt = new Progress(); | |
384 | Progress pgCache = new Progress(); | |
385 | pg.setMinMax(0, 10); | |
386 | pg.addProgress(pgImprt, 7); | |
387 | pg.addProgress(pgCache, 3); | |
388 | ||
b6b65795 | 389 | MetaData meta = lib.imprt(url, pgImprt); |
ef98466f NR |
390 | updateMetaCache(metasReal, meta); |
391 | metasMixed = null; | |
b6b65795 | 392 | clearFromCache(meta.getLuid()); |
ef98466f | 393 | |
edf79e5e | 394 | pg.done(); |
b6b65795 | 395 | return meta; |
edf79e5e NR |
396 | } |
397 | ||
ff05b828 NR |
398 | // All the following methods are only used by Save and Delete in |
399 | // BasicLibrary: | |
400 | ||
401 | @Override | |
402 | protected int getNextId() { | |
403 | throw new java.lang.InternalError("Should not have been called"); | |
404 | } | |
405 | ||
406 | @Override | |
407 | protected void doDelete(String luid) throws IOException { | |
408 | throw new java.lang.InternalError("Should not have been called"); | |
409 | } | |
410 | ||
411 | @Override | |
412 | protected Story doSave(Story story, Progress pg) throws IOException { | |
413 | throw new java.lang.InternalError("Should not have been called"); | |
414 | } | |
415 | } |