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; | |
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; | |
12 | import be.nikiroo.utils.Progress; | |
13 | ||
14 | /** | |
15 | * This library will cache another pre-existing {@link BasicLibrary}. | |
16 | * | |
17 | * @author niki | |
18 | */ | |
19 | public class CacheLibrary extends BasicLibrary { | |
20 | private List<MetaData> metas; | |
21 | private BasicLibrary lib; | |
22 | private LocalLibrary cacheLib; | |
23 | ||
24 | /** | |
25 | * Create a cache library around the given one. | |
26 | * <p> | |
27 | * It will return the same result, but those will be saved to disk at the | |
28 | * same time to be fetched quicker the next time. | |
29 | * | |
30 | * @param cacheDir | |
31 | * the cache directory where to save the files to disk | |
32 | * @param lib | |
33 | * the original library to wrap | |
34 | */ | |
35 | public CacheLibrary(File cacheDir, BasicLibrary lib) { | |
36 | this.cacheLib = new LocalLibrary(cacheDir, Instance.getUiConfig() | |
37 | .getString(UiConfig.GUI_NON_IMAGES_DOCUMENT_TYPE), Instance | |
38 | .getUiConfig().getString(UiConfig.GUI_IMAGES_DOCUMENT_TYPE), | |
39 | true); | |
40 | this.lib = lib; | |
41 | } | |
42 | ||
43 | @Override | |
44 | public String getLibraryName() { | |
45 | return lib.getLibraryName(); | |
46 | } | |
47 | ||
e6249b0f NR |
48 | @Override |
49 | public Status getStatus() { | |
50 | return lib.getStatus(); | |
51 | } | |
52 | ||
ff05b828 NR |
53 | @Override |
54 | protected List<MetaData> getMetas(Progress pg) { | |
55 | if (pg == null) { | |
56 | pg = new Progress(); | |
57 | } | |
58 | ||
59 | if (metas == null) { | |
60 | metas = lib.getMetas(pg); | |
61 | } | |
62 | ||
63 | pg.done(); | |
64 | return metas; | |
65 | } | |
66 | ||
67 | @Override | |
68 | public synchronized File getFile(final String luid, Progress pg) { | |
69 | if (pg == null) { | |
70 | pg = new Progress(); | |
71 | } | |
72 | ||
73 | Progress pgImport = new Progress(); | |
74 | Progress pgGet = new Progress(); | |
75 | Progress pgRecall = new Progress(); | |
76 | ||
77 | pg.setMinMax(0, 5); | |
78 | pg.addProgress(pgImport, 3); | |
79 | pg.addProgress(pgGet, 1); | |
80 | pg.addProgress(pgRecall, 1); | |
81 | ||
82 | if (!isCached(luid)) { | |
83 | try { | |
84 | cacheLib.imprt(lib, luid, pgImport); | |
85 | pgImport.done(); | |
085a2f9a | 86 | clearCache(); |
ff05b828 | 87 | } catch (IOException e) { |
62c63b07 | 88 | Instance.getTraceHandler().error(e); |
ff05b828 NR |
89 | } |
90 | ||
91 | pgImport.done(); | |
92 | pgGet.done(); | |
93 | } | |
94 | ||
95 | File file = cacheLib.getFile(luid, pgRecall); | |
96 | pgRecall.done(); | |
97 | ||
98 | pg.done(); | |
99 | return file; | |
100 | } | |
101 | ||
102 | @Override | |
103 | public BufferedImage getCover(final String luid) { | |
ff05b828 NR |
104 | if (isCached(luid)) { |
105 | return cacheLib.getCover(luid); | |
106 | } | |
107 | ||
085a2f9a | 108 | // We could update the cache here, but it's not easy |
ff05b828 NR |
109 | return lib.getCover(luid); |
110 | } | |
111 | ||
085a2f9a NR |
112 | @Override |
113 | public BufferedImage getSourceCover(String source) { | |
114 | // no cache for the source cover | |
115 | return lib.getSourceCover(source); | |
116 | } | |
117 | ||
118 | @Override | |
119 | public void setSourceCover(String source, String luid) { | |
120 | lib.setSourceCover(source, luid); | |
121 | cacheLib.setSourceCover(source, getSourceCover(source)); | |
122 | } | |
123 | ||
ff05b828 NR |
124 | @Override |
125 | protected void clearCache() { | |
126 | metas = null; | |
127 | cacheLib.clearCache(); | |
128 | lib.clearCache(); | |
129 | } | |
130 | ||
131 | @Override | |
132 | public synchronized Story save(Story story, String luid, Progress pg) | |
133 | throws IOException { | |
03c1cede NR |
134 | Progress pgLib = new Progress(); |
135 | Progress pgCacheLib = new Progress(); | |
136 | ||
137 | if (pg == null) { | |
138 | pg = new Progress(); | |
139 | } | |
140 | ||
141 | pg.setMinMax(0, 2); | |
142 | pg.addProgress(pgLib, 1); | |
143 | pg.addProgress(pgCacheLib, 1); | |
144 | ||
145 | story = lib.save(story, luid, pgLib); | |
0fa0fe95 | 146 | story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib); |
03c1cede | 147 | |
ff05b828 | 148 | clearCache(); |
03c1cede | 149 | |
ff05b828 NR |
150 | return story; |
151 | } | |
152 | ||
153 | @Override | |
154 | public synchronized void delete(String luid) throws IOException { | |
085a2f9a NR |
155 | if (isCached(luid)) { |
156 | cacheLib.delete(luid); | |
157 | } | |
ff05b828 NR |
158 | lib.delete(luid); |
159 | clearCache(); | |
160 | } | |
161 | ||
ff05b828 NR |
162 | @Override |
163 | public synchronized void changeSource(String luid, String newSource, | |
164 | Progress pg) throws IOException { | |
165 | if (pg == null) { | |
166 | pg = new Progress(); | |
167 | } | |
168 | ||
169 | Progress pgCache = new Progress(); | |
170 | Progress pgOrig = new Progress(); | |
171 | pg.setMinMax(0, 2); | |
172 | pg.addProgress(pgCache, 1); | |
173 | pg.addProgress(pgOrig, 1); | |
174 | ||
e06632ee NR |
175 | if (isCached(luid)) { |
176 | cacheLib.changeSource(luid, newSource, pgCache); | |
177 | } | |
ff05b828 NR |
178 | pgCache.done(); |
179 | lib.changeSource(luid, newSource, pgOrig); | |
180 | pgOrig.done(); | |
181 | ||
182 | pg.done(); | |
183 | } | |
184 | ||
185 | /** | |
186 | * Check if the {@link Story} denoted by this Library UID is present in the | |
187 | * cache. | |
188 | * | |
189 | * @param luid | |
190 | * the Library UID | |
191 | * | |
192 | * @return TRUE if it is | |
193 | */ | |
194 | public boolean isCached(String luid) { | |
195 | return cacheLib.getInfo(luid) != null; | |
196 | } | |
197 | ||
198 | /** | |
199 | * Clear the {@link Story} from the cache. | |
200 | * | |
201 | * @param luid | |
202 | * the story to clear | |
203 | * | |
204 | * @throws IOException | |
205 | * in case of I/O error | |
206 | */ | |
207 | public void clearFromCache(String luid) throws IOException { | |
e06632ee NR |
208 | if (isCached(luid)) { |
209 | cacheLib.delete(luid); | |
210 | clearCache(); | |
211 | } | |
ff05b828 NR |
212 | } |
213 | ||
214 | // All the following methods are only used by Save and Delete in | |
215 | // BasicLibrary: | |
216 | ||
217 | @Override | |
218 | protected int getNextId() { | |
219 | throw new java.lang.InternalError("Should not have been called"); | |
220 | } | |
221 | ||
222 | @Override | |
223 | protected void doDelete(String luid) throws IOException { | |
224 | throw new java.lang.InternalError("Should not have been called"); | |
225 | } | |
226 | ||
227 | @Override | |
228 | protected Story doSave(Story story, Progress pg) throws IOException { | |
229 | throw new java.lang.InternalError("Should not have been called"); | |
230 | } | |
231 | } |