Remote, JPG/PNG:
[nikiroo-utils.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;
6import java.util.List;
7
8import be.nikiroo.fanfix.Instance;
9import be.nikiroo.fanfix.bundles.UiConfig;
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Story;
12import be.nikiroo.utils.Progress;
13
14/**
15 * This library will cache another pre-existing {@link BasicLibrary}.
16 *
17 * @author niki
18 */
19public 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();
085a2f9a 81 clearCache();
ff05b828 82 } catch (IOException e) {
62c63b07 83 Instance.getTraceHandler().error(e);
ff05b828
NR
84 }
85
86 pgImport.done();
87 pgGet.done();
88 }
89
90 File file = cacheLib.getFile(luid, pgRecall);
91 pgRecall.done();
92
93 pg.done();
94 return file;
95 }
96
97 @Override
98 public BufferedImage getCover(final String luid) {
ff05b828
NR
99 if (isCached(luid)) {
100 return cacheLib.getCover(luid);
101 }
102
085a2f9a 103 // We could update the cache here, but it's not easy
ff05b828
NR
104 return lib.getCover(luid);
105 }
106
085a2f9a
NR
107 @Override
108 public BufferedImage getSourceCover(String source) {
109 // no cache for the source cover
110 return lib.getSourceCover(source);
111 }
112
113 @Override
114 public void setSourceCover(String source, String luid) {
115 lib.setSourceCover(source, luid);
116 cacheLib.setSourceCover(source, getSourceCover(source));
117 }
118
ff05b828
NR
119 @Override
120 protected void clearCache() {
121 metas = null;
122 cacheLib.clearCache();
123 lib.clearCache();
124 }
125
126 @Override
127 public synchronized Story save(Story story, String luid, Progress pg)
128 throws IOException {
129 story = lib.save(story, luid, pg);
130 clearCache();
131 return story;
132 }
133
134 @Override
135 public synchronized void delete(String luid) throws IOException {
085a2f9a
NR
136 if (isCached(luid)) {
137 cacheLib.delete(luid);
138 }
ff05b828
NR
139 lib.delete(luid);
140 clearCache();
141 }
142
ff05b828
NR
143 @Override
144 public synchronized void changeSource(String luid, String newSource,
145 Progress pg) throws IOException {
146 if (pg == null) {
147 pg = new Progress();
148 }
149
150 Progress pgCache = new Progress();
151 Progress pgOrig = new Progress();
152 pg.setMinMax(0, 2);
153 pg.addProgress(pgCache, 1);
154 pg.addProgress(pgOrig, 1);
155
e06632ee
NR
156 if (isCached(luid)) {
157 cacheLib.changeSource(luid, newSource, pgCache);
158 }
ff05b828
NR
159 pgCache.done();
160 lib.changeSource(luid, newSource, pgOrig);
161 pgOrig.done();
162
163 pg.done();
164 }
165
166 /**
167 * Check if the {@link Story} denoted by this Library UID is present in the
168 * cache.
169 *
170 * @param luid
171 * the Library UID
172 *
173 * @return TRUE if it is
174 */
175 public boolean isCached(String luid) {
176 return cacheLib.getInfo(luid) != null;
177 }
178
179 /**
180 * Clear the {@link Story} from the cache.
181 *
182 * @param luid
183 * the story to clear
184 *
185 * @throws IOException
186 * in case of I/O error
187 */
188 public void clearFromCache(String luid) throws IOException {
e06632ee
NR
189 if (isCached(luid)) {
190 cacheLib.delete(luid);
191 clearCache();
192 }
ff05b828
NR
193 }
194
195 // All the following methods are only used by Save and Delete in
196 // BasicLibrary:
197
198 @Override
199 protected int getNextId() {
200 throw new java.lang.InternalError("Should not have been called");
201 }
202
203 @Override
204 protected void doDelete(String luid) throws IOException {
205 throw new java.lang.InternalError("Should not have been called");
206 }
207
208 @Override
209 protected Story doSave(Story story, Progress pg) throws IOException {
210 throw new java.lang.InternalError("Should not have been called");
211 }
212}