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); | |
86 | pgImport.done(); | |
e272f05f | 87 | invalidateInfo(luid); |
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) { |
085a2f9a NR |
115 | // no cache for the source cover |
116 | return lib.getSourceCover(source); | |
117 | } | |
118 | ||
119 | @Override | |
120 | public void setSourceCover(String source, String luid) { | |
121 | lib.setSourceCover(source, luid); | |
122 | cacheLib.setSourceCover(source, getSourceCover(source)); | |
123 | } | |
124 | ||
ff05b828 | 125 | @Override |
e272f05f | 126 | protected void invalidateInfo(String luid) { |
e272f05f | 127 | if (luid == null) { |
cbd62024 | 128 | metas = null; |
e272f05f NR |
129 | } else if (metas != null) { |
130 | MetaData meta = lib.getInfo(luid); | |
131 | for (int i = 0; i < metas.size(); i++) { | |
132 | if (metas.get(i).getLuid().equals(luid)) { | |
cbd62024 | 133 | metas.remove(i--); |
e272f05f NR |
134 | } |
135 | } | |
136 | ||
137 | if (meta != null) { | |
138 | metas.add(meta); | |
139 | } | |
140 | } | |
141 | ||
142 | cacheLib.invalidateInfo(luid); | |
143 | lib.invalidateInfo(luid); | |
ff05b828 NR |
144 | } |
145 | ||
146 | @Override | |
147 | public synchronized Story save(Story story, String luid, Progress pg) | |
148 | throws IOException { | |
03c1cede NR |
149 | Progress pgLib = new Progress(); |
150 | Progress pgCacheLib = new Progress(); | |
151 | ||
152 | if (pg == null) { | |
153 | pg = new Progress(); | |
154 | } | |
155 | ||
156 | pg.setMinMax(0, 2); | |
157 | pg.addProgress(pgLib, 1); | |
158 | pg.addProgress(pgCacheLib, 1); | |
159 | ||
160 | story = lib.save(story, luid, pgLib); | |
0fa0fe95 | 161 | story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); |
03c1cede | 162 | |
e272f05f | 163 | invalidateInfo(story.getMeta().getLuid()); |
03c1cede | 164 | |
ff05b828 NR |
165 | return story; |
166 | } | |
167 | ||
168 | @Override | |
169 | public synchronized void delete(String luid) throws IOException { | |
085a2f9a NR |
170 | if (isCached(luid)) { |
171 | cacheLib.delete(luid); | |
172 | } | |
ff05b828 | 173 | lib.delete(luid); |
e272f05f | 174 | |
cbd62024 NR |
175 | MetaData meta = getInfo(luid); |
176 | if (meta != null) { | |
177 | metas.remove(meta); | |
e272f05f | 178 | } |
ff05b828 NR |
179 | } |
180 | ||
ff05b828 NR |
181 | @Override |
182 | public synchronized void changeSource(String luid, String newSource, | |
183 | Progress pg) throws IOException { | |
184 | if (pg == null) { | |
185 | pg = new Progress(); | |
186 | } | |
187 | ||
188 | Progress pgCache = new Progress(); | |
189 | Progress pgOrig = new Progress(); | |
190 | pg.setMinMax(0, 2); | |
191 | pg.addProgress(pgCache, 1); | |
192 | pg.addProgress(pgOrig, 1); | |
193 | ||
e272f05f NR |
194 | MetaData meta = getInfo(luid); |
195 | if (meta == null) { | |
196 | throw new IOException("Story not found: " + luid); | |
197 | } | |
198 | ||
e06632ee NR |
199 | if (isCached(luid)) { |
200 | cacheLib.changeSource(luid, newSource, pgCache); | |
201 | } | |
ff05b828 | 202 | pgCache.done(); |
e272f05f | 203 | |
ff05b828 NR |
204 | lib.changeSource(luid, newSource, pgOrig); |
205 | pgOrig.done(); | |
206 | ||
e272f05f | 207 | meta.setSource(newSource); |
ff05b828 NR |
208 | pg.done(); |
209 | } | |
210 | ||
211 | /** | |
212 | * Check if the {@link Story} denoted by this Library UID is present in the | |
213 | * cache. | |
214 | * | |
215 | * @param luid | |
216 | * the Library UID | |
217 | * | |
218 | * @return TRUE if it is | |
219 | */ | |
220 | public boolean isCached(String luid) { | |
221 | return cacheLib.getInfo(luid) != null; | |
222 | } | |
223 | ||
224 | /** | |
225 | * Clear the {@link Story} from the cache. | |
226 | * | |
227 | * @param luid | |
228 | * the story to clear | |
229 | * | |
230 | * @throws IOException | |
231 | * in case of I/O error | |
232 | */ | |
233 | public void clearFromCache(String luid) throws IOException { | |
e06632ee NR |
234 | if (isCached(luid)) { |
235 | cacheLib.delete(luid); | |
e272f05f | 236 | invalidateInfo(luid); |
e06632ee | 237 | } |
ff05b828 NR |
238 | } |
239 | ||
edf79e5e NR |
240 | @Override |
241 | public Story imprt(URL url, Progress pg) throws IOException { | |
242 | if (pg == null) { | |
243 | pg = new Progress(); | |
244 | } | |
245 | ||
246 | Progress pgImprt = new Progress(); | |
247 | Progress pgCache = new Progress(); | |
248 | pg.setMinMax(0, 10); | |
249 | pg.addProgress(pgImprt, 7); | |
250 | pg.addProgress(pgCache, 3); | |
251 | ||
252 | Story story = lib.imprt(url, pgImprt); | |
253 | cacheLib.save(story, story.getMeta().getLuid(), pgCache); | |
254 | ||
cbd62024 NR |
255 | invalidateInfo(story.getMeta().getLuid()); |
256 | ||
edf79e5e NR |
257 | pg.done(); |
258 | return story; | |
259 | } | |
260 | ||
ff05b828 NR |
261 | // All the following methods are only used by Save and Delete in |
262 | // BasicLibrary: | |
263 | ||
264 | @Override | |
265 | protected int getNextId() { | |
266 | throw new java.lang.InternalError("Should not have been called"); | |
267 | } | |
268 | ||
269 | @Override | |
270 | protected void doDelete(String luid) throws IOException { | |
271 | throw new java.lang.InternalError("Should not have been called"); | |
272 | } | |
273 | ||
274 | @Override | |
275 | protected Story doSave(Story story, Progress pg) throws IOException { | |
276 | throw new java.lang.InternalError("Should not have been called"); | |
277 | } | |
278 | } |