1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.IOException
;
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
.Image
;
13 import be
.nikiroo
.utils
.Progress
;
16 * This library will cache another pre-existing {@link BasicLibrary}.
20 public class CacheLibrary
extends BasicLibrary
{
21 private List
<MetaData
> metas
;
22 private BasicLibrary lib
;
23 private LocalLibrary cacheLib
;
26 * Create a cache library around the given one.
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.
32 * the cache directory where to save the files to disk
34 * the original library to wrap
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
),
45 public String
getLibraryName() {
46 return lib
.getLibraryName();
50 public Status
getStatus() {
51 return lib
.getStatus();
55 protected List
<MetaData
> getMetas(Progress pg
) {
61 metas
= lib
.getMetas(pg
);
69 public synchronized File
getFile(final String luid
, Progress pg
) {
74 Progress pgImport
= new Progress();
75 Progress pgGet
= new Progress();
76 Progress pgRecall
= new Progress();
79 pg
.addProgress(pgImport
, 3);
80 pg
.addProgress(pgGet
, 1);
81 pg
.addProgress(pgRecall
, 1);
83 if (!isCached(luid
)) {
85 cacheLib
.imprt(lib
, luid
, pgImport
);
88 } catch (IOException e
) {
89 Instance
.getTraceHandler().error(e
);
96 File file
= cacheLib
.getFile(luid
, pgRecall
);
104 public Image
getCover(final String luid
) {
105 if (isCached(luid
)) {
106 return cacheLib
.getCover(luid
);
109 // We could update the cache here, but it's not easy
110 return lib
.getCover(luid
);
114 public Image
getSourceCover(String source
) {
115 // no cache for the source cover
116 return lib
.getSourceCover(source
);
120 public void setSourceCover(String source
, String luid
) {
121 lib
.setSourceCover(source
, luid
);
122 cacheLib
.setSourceCover(source
, getSourceCover(source
));
126 protected void invalidateInfo(String luid
) {
129 } else if (metas
!= null) {
130 MetaData meta
= lib
.getInfo(luid
);
131 for (int i
= 0; i
< metas
.size(); i
++) {
132 if (metas
.get(i
).getLuid().equals(luid
)) {
142 cacheLib
.invalidateInfo(luid
);
143 lib
.invalidateInfo(luid
);
147 public synchronized Story
save(Story story
, String luid
, Progress pg
)
149 Progress pgLib
= new Progress();
150 Progress pgCacheLib
= new Progress();
157 pg
.addProgress(pgLib
, 1);
158 pg
.addProgress(pgCacheLib
, 1);
160 story
= lib
.save(story
, luid
, pgLib
);
161 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
163 invalidateInfo(story
.getMeta().getLuid());
169 public synchronized void delete(String luid
) throws IOException
{
170 if (isCached(luid
)) {
171 cacheLib
.delete(luid
);
175 MetaData meta
= getInfo(luid
);
182 public synchronized void changeSource(String luid
, String newSource
,
183 Progress pg
) throws IOException
{
188 Progress pgCache
= new Progress();
189 Progress pgOrig
= new Progress();
191 pg
.addProgress(pgCache
, 1);
192 pg
.addProgress(pgOrig
, 1);
194 MetaData meta
= getInfo(luid
);
196 throw new IOException("Story not found: " + luid
);
199 if (isCached(luid
)) {
200 cacheLib
.changeSource(luid
, newSource
, pgCache
);
204 lib
.changeSource(luid
, newSource
, pgOrig
);
207 meta
.setSource(newSource
);
212 * Check if the {@link Story} denoted by this Library UID is present in the
218 * @return TRUE if it is
220 public boolean isCached(String luid
) {
221 return cacheLib
.getInfo(luid
) != null;
225 * Clear the {@link Story} from the cache.
230 * @throws IOException
231 * in case of I/O error
233 public void clearFromCache(String luid
) throws IOException
{
234 if (isCached(luid
)) {
235 cacheLib
.delete(luid
);
236 invalidateInfo(luid
);
241 public Story
imprt(URL url
, Progress pg
) throws IOException
{
246 Progress pgImprt
= new Progress();
247 Progress pgCache
= new Progress();
249 pg
.addProgress(pgImprt
, 7);
250 pg
.addProgress(pgCache
, 3);
252 Story story
= lib
.imprt(url
, pgImprt
);
253 cacheLib
.save(story
, story
.getMeta().getLuid(), pgCache
);
255 invalidateInfo(story
.getMeta().getLuid());
261 // All the following methods are only used by Save and Delete in
265 protected int getNextId() {
266 throw new java
.lang
.InternalError("Should not have been called");
270 protected void doDelete(String luid
) throws IOException
{
271 throw new java
.lang
.InternalError("Should not have been called");
275 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
276 throw new java
.lang
.InternalError("Should not have been called");