Commit | Line | Data |
---|---|---|
ff05b828 NR |
1 | package be.nikiroo.fanfix.library; |
2 | ||
3 | import java.awt.image.BufferedImage; | |
4 | import java.io.File; | |
5 | import java.io.IOException; | |
edf79e5e | 6 | import java.net.URL; |
ff05b828 NR |
7 | import java.util.List; |
8 | ||
9 | import be.nikiroo.fanfix.Instance; | |
10 | import be.nikiroo.fanfix.bundles.UiConfig; | |
11 | import be.nikiroo.fanfix.data.MetaData; | |
12 | import be.nikiroo.fanfix.data.Story; | |
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(); | |
085a2f9a | 87 | clearCache(); |
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 | |
104 | public BufferedImage 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 NR |
113 | @Override |
114 | public BufferedImage getSourceCover(String source) { | |
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 NR |
125 | @Override |
126 | protected void clearCache() { | |
127 | metas = null; | |
128 | cacheLib.clearCache(); | |
129 | lib.clearCache(); | |
130 | } | |
131 | ||
132 | @Override | |
133 | public synchronized Story save(Story story, String luid, Progress pg) | |
134 | throws IOException { | |
03c1cede NR |
135 | Progress pgLib = new Progress(); |
136 | Progress pgCacheLib = new Progress(); | |
137 | ||
138 | if (pg == null) { | |
139 | pg = new Progress(); | |
140 | } | |
141 | ||
142 | pg.setMinMax(0, 2); | |
143 | pg.addProgress(pgLib, 1); | |
144 | pg.addProgress(pgCacheLib, 1); | |
145 | ||
146 | story = lib.save(story, luid, pgLib); | |
0fa0fe95 | 147 | story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); |
03c1cede | 148 | |
ff05b828 | 149 | clearCache(); |
03c1cede | 150 | |
ff05b828 NR |
151 | return story; |
152 | } | |
153 | ||
154 | @Override | |
155 | public synchronized void delete(String luid) throws IOException { | |
085a2f9a NR |
156 | if (isCached(luid)) { |
157 | cacheLib.delete(luid); | |
158 | } | |
ff05b828 NR |
159 | lib.delete(luid); |
160 | clearCache(); | |
161 | } | |
162 | ||
ff05b828 NR |
163 | @Override |
164 | public synchronized void changeSource(String luid, String newSource, | |
165 | Progress pg) throws IOException { | |
166 | if (pg == null) { | |
167 | pg = new Progress(); | |
168 | } | |
169 | ||
170 | Progress pgCache = new Progress(); | |
171 | Progress pgOrig = new Progress(); | |
172 | pg.setMinMax(0, 2); | |
173 | pg.addProgress(pgCache, 1); | |
174 | pg.addProgress(pgOrig, 1); | |
175 | ||
e06632ee NR |
176 | if (isCached(luid)) { |
177 | cacheLib.changeSource(luid, newSource, pgCache); | |
178 | } | |
ff05b828 NR |
179 | pgCache.done(); |
180 | lib.changeSource(luid, newSource, pgOrig); | |
181 | pgOrig.done(); | |
182 | ||
183 | pg.done(); | |
184 | } | |
185 | ||
186 | /** | |
187 | * Check if the {@link Story} denoted by this Library UID is present in the | |
188 | * cache. | |
189 | * | |
190 | * @param luid | |
191 | * the Library UID | |
192 | * | |
193 | * @return TRUE if it is | |
194 | */ | |
195 | public boolean isCached(String luid) { | |
196 | return cacheLib.getInfo(luid) != null; | |
197 | } | |
198 | ||
199 | /** | |
200 | * Clear the {@link Story} from the cache. | |
201 | * | |
202 | * @param luid | |
203 | * the story to clear | |
204 | * | |
205 | * @throws IOException | |
206 | * in case of I/O error | |
207 | */ | |
208 | public void clearFromCache(String luid) throws IOException { | |
e06632ee NR |
209 | if (isCached(luid)) { |
210 | cacheLib.delete(luid); | |
211 | clearCache(); | |
212 | } | |
ff05b828 NR |
213 | } |
214 | ||
edf79e5e NR |
215 | @Override |
216 | public Story imprt(URL url, Progress pg) throws IOException { | |
217 | if (pg == null) { | |
218 | pg = new Progress(); | |
219 | } | |
220 | ||
221 | Progress pgImprt = new Progress(); | |
222 | Progress pgCache = new Progress(); | |
223 | pg.setMinMax(0, 10); | |
224 | pg.addProgress(pgImprt, 7); | |
225 | pg.addProgress(pgCache, 3); | |
226 | ||
227 | Story story = lib.imprt(url, pgImprt); | |
228 | cacheLib.save(story, story.getMeta().getLuid(), pgCache); | |
229 | ||
230 | pg.done(); | |
231 | return story; | |
232 | } | |
233 | ||
ff05b828 NR |
234 | // All the following methods are only used by Save and Delete in |
235 | // BasicLibrary: | |
236 | ||
237 | @Override | |
238 | protected int getNextId() { | |
239 | throw new java.lang.InternalError("Should not have been called"); | |
240 | } | |
241 | ||
242 | @Override | |
243 | protected void doDelete(String luid) throws IOException { | |
244 | throw new java.lang.InternalError("Should not have been called"); | |
245 | } | |
246 | ||
247 | @Override | |
248 | protected Story doSave(Story story, Progress pg) throws IOException { | |
249 | throw new java.lang.InternalError("Should not have been called"); | |
250 | } | |
251 | } |