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