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 | ||
48 | @Override | |
49 | protected List<MetaData> getMetas(Progress pg) { | |
50 | if (pg == null) { | |
51 | pg = new Progress(); | |
52 | } | |
53 | ||
54 | if (metas == null) { | |
55 | metas = lib.getMetas(pg); | |
56 | } | |
57 | ||
58 | pg.done(); | |
59 | return metas; | |
60 | } | |
61 | ||
62 | @Override | |
63 | public synchronized File getFile(final String luid, Progress pg) { | |
64 | if (pg == null) { | |
65 | pg = new Progress(); | |
66 | } | |
67 | ||
68 | Progress pgImport = new Progress(); | |
69 | Progress pgGet = new Progress(); | |
70 | Progress pgRecall = new Progress(); | |
71 | ||
72 | pg.setMinMax(0, 5); | |
73 | pg.addProgress(pgImport, 3); | |
74 | pg.addProgress(pgGet, 1); | |
75 | pg.addProgress(pgRecall, 1); | |
76 | ||
77 | if (!isCached(luid)) { | |
78 | try { | |
79 | cacheLib.imprt(lib, luid, pgImport); | |
80 | pgImport.done(); | |
81 | Story story = cacheLib.getStory(luid, pgGet); | |
82 | metas.remove(story.getMeta()); | |
83 | metas.add(story.getMeta()); | |
84 | } catch (IOException e) { | |
85 | Instance.syserr(e); | |
86 | } | |
87 | ||
88 | pgImport.done(); | |
89 | pgGet.done(); | |
90 | } | |
91 | ||
92 | File file = cacheLib.getFile(luid, pgRecall); | |
93 | pgRecall.done(); | |
94 | ||
95 | pg.done(); | |
96 | return file; | |
97 | } | |
98 | ||
99 | @Override | |
100 | public BufferedImage getCover(final String luid) { | |
101 | // Retrieve it from the cache if possible: | |
102 | if (isCached(luid)) { | |
103 | return cacheLib.getCover(luid); | |
104 | } | |
105 | ||
106 | return lib.getCover(luid); | |
107 | } | |
108 | ||
109 | @Override | |
110 | protected void clearCache() { | |
111 | metas = null; | |
112 | cacheLib.clearCache(); | |
113 | lib.clearCache(); | |
114 | } | |
115 | ||
116 | @Override | |
117 | public synchronized Story save(Story story, String luid, Progress pg) | |
118 | throws IOException { | |
119 | story = lib.save(story, luid, pg); | |
120 | clearCache(); | |
121 | return story; | |
122 | } | |
123 | ||
124 | @Override | |
125 | public synchronized void delete(String luid) throws IOException { | |
126 | cacheLib.delete(luid); | |
127 | lib.delete(luid); | |
128 | clearCache(); | |
129 | } | |
130 | ||
131 | @Override | |
132 | public void setSourceCover(String source, String luid) { | |
133 | cacheLib.setSourceCover(source, luid); | |
134 | lib.setSourceCover(source, luid); | |
135 | } | |
136 | ||
137 | @Override | |
138 | public synchronized void changeSource(String luid, String newSource, | |
139 | Progress pg) throws IOException { | |
140 | if (pg == null) { | |
141 | pg = new Progress(); | |
142 | } | |
143 | ||
144 | Progress pgCache = new Progress(); | |
145 | Progress pgOrig = new Progress(); | |
146 | pg.setMinMax(0, 2); | |
147 | pg.addProgress(pgCache, 1); | |
148 | pg.addProgress(pgOrig, 1); | |
149 | ||
150 | cacheLib.changeSource(luid, newSource, pgCache); | |
151 | pgCache.done(); | |
152 | lib.changeSource(luid, newSource, pgOrig); | |
153 | pgOrig.done(); | |
154 | ||
155 | pg.done(); | |
156 | } | |
157 | ||
158 | /** | |
159 | * Check if the {@link Story} denoted by this Library UID is present in the | |
160 | * cache. | |
161 | * | |
162 | * @param luid | |
163 | * the Library UID | |
164 | * | |
165 | * @return TRUE if it is | |
166 | */ | |
167 | public boolean isCached(String luid) { | |
168 | return cacheLib.getInfo(luid) != null; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Clear the {@link Story} from the cache. | |
173 | * | |
174 | * @param luid | |
175 | * the story to clear | |
176 | * | |
177 | * @throws IOException | |
178 | * in case of I/O error | |
179 | */ | |
180 | public void clearFromCache(String luid) throws IOException { | |
181 | cacheLib.delete(luid); | |
182 | clearCache(); | |
183 | } | |
184 | ||
185 | // All the following methods are only used by Save and Delete in | |
186 | // BasicLibrary: | |
187 | ||
188 | @Override | |
189 | protected int getNextId() { | |
190 | throw new java.lang.InternalError("Should not have been called"); | |
191 | } | |
192 | ||
193 | @Override | |
194 | protected void doDelete(String luid) throws IOException { | |
195 | throw new java.lang.InternalError("Should not have been called"); | |
196 | } | |
197 | ||
198 | @Override | |
199 | protected Story doSave(Story story, Progress pg) throws IOException { | |
200 | throw new java.lang.InternalError("Should not have been called"); | |
201 | } | |
202 | } |