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