1 package be
.nikiroo
.fanfix
.library
;
3 import java
.awt
.image
.BufferedImage
;
5 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
.Progress
;
15 * This library will cache another pre-existing {@link BasicLibrary}.
19 public class CacheLibrary
extends BasicLibrary
{
20 private List
<MetaData
> metas
;
21 private BasicLibrary lib
;
22 private LocalLibrary cacheLib
;
25 * Create a cache library around the given one.
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.
31 * the cache directory where to save the files to disk
33 * the original library to wrap
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
),
44 public String
getLibraryName() {
45 return lib
.getLibraryName();
49 public Status
getStatus() {
50 return lib
.getStatus();
54 protected List
<MetaData
> getMetas(Progress pg
) {
60 metas
= lib
.getMetas(pg
);
68 public synchronized File
getFile(final String luid
, Progress pg
) {
73 Progress pgImport
= new Progress();
74 Progress pgGet
= new Progress();
75 Progress pgRecall
= new Progress();
78 pg
.addProgress(pgImport
, 3);
79 pg
.addProgress(pgGet
, 1);
80 pg
.addProgress(pgRecall
, 1);
82 if (!isCached(luid
)) {
84 cacheLib
.imprt(lib
, luid
, pgImport
);
87 } catch (IOException e
) {
88 Instance
.getTraceHandler().error(e
);
95 File file
= cacheLib
.getFile(luid
, pgRecall
);
103 public BufferedImage
getCover(final String luid
) {
104 if (isCached(luid
)) {
105 return cacheLib
.getCover(luid
);
108 // We could update the cache here, but it's not easy
109 return lib
.getCover(luid
);
113 public BufferedImage
getSourceCover(String source
) {
114 // no cache for the source cover
115 return lib
.getSourceCover(source
);
119 public void setSourceCover(String source
, String luid
) {
120 lib
.setSourceCover(source
, luid
);
121 cacheLib
.setSourceCover(source
, getSourceCover(source
));
125 protected void clearCache() {
127 cacheLib
.clearCache();
132 public synchronized Story
save(Story story
, String luid
, Progress pg
)
134 Progress pgLib
= new Progress();
135 Progress pgCacheLib
= new Progress();
142 pg
.addProgress(pgLib
, 1);
143 pg
.addProgress(pgCacheLib
, 1);
145 story
= lib
.save(story
, luid
, pgLib
);
146 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
154 public synchronized void delete(String luid
) throws IOException
{
155 if (isCached(luid
)) {
156 cacheLib
.delete(luid
);
163 public synchronized void changeSource(String luid
, String newSource
,
164 Progress pg
) throws IOException
{
169 Progress pgCache
= new Progress();
170 Progress pgOrig
= new Progress();
172 pg
.addProgress(pgCache
, 1);
173 pg
.addProgress(pgOrig
, 1);
175 if (isCached(luid
)) {
176 cacheLib
.changeSource(luid
, newSource
, pgCache
);
179 lib
.changeSource(luid
, newSource
, pgOrig
);
186 * Check if the {@link Story} denoted by this Library UID is present in the
192 * @return TRUE if it is
194 public boolean isCached(String luid
) {
195 return cacheLib
.getInfo(luid
) != null;
199 * Clear the {@link Story} from the cache.
204 * @throws IOException
205 * in case of I/O error
207 public void clearFromCache(String luid
) throws IOException
{
208 if (isCached(luid
)) {
209 cacheLib
.delete(luid
);
214 // All the following methods are only used by Save and Delete in
218 protected int getNextId() {
219 throw new java
.lang
.InternalError("Should not have been called");
223 protected void doDelete(String luid
) throws IOException
{
224 throw new java
.lang
.InternalError("Should not have been called");
228 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
229 throw new java
.lang
.InternalError("Should not have been called");