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 protected List
<MetaData
> getMetas(Progress pg
) {
55 metas
= lib
.getMetas(pg
);
63 public synchronized File
getFile(final String luid
, Progress pg
) {
68 Progress pgImport
= new Progress();
69 Progress pgGet
= new Progress();
70 Progress pgRecall
= new Progress();
73 pg
.addProgress(pgImport
, 3);
74 pg
.addProgress(pgGet
, 1);
75 pg
.addProgress(pgRecall
, 1);
77 if (!isCached(luid
)) {
79 cacheLib
.imprt(lib
, luid
, pgImport
);
82 } catch (IOException e
) {
90 File file
= cacheLib
.getFile(luid
, pgRecall
);
98 public BufferedImage
getCover(final String luid
) {
100 return cacheLib
.getCover(luid
);
103 // We could update the cache here, but it's not easy
104 return lib
.getCover(luid
);
108 public BufferedImage
getSourceCover(String source
) {
109 // no cache for the source cover
110 return lib
.getSourceCover(source
);
114 public void setSourceCover(String source
, String luid
) {
115 lib
.setSourceCover(source
, luid
);
116 cacheLib
.setSourceCover(source
, getSourceCover(source
));
120 protected void clearCache() {
122 cacheLib
.clearCache();
127 public synchronized Story
save(Story story
, String luid
, Progress pg
)
129 story
= lib
.save(story
, luid
, pg
);
135 public synchronized void delete(String luid
) throws IOException
{
136 if (isCached(luid
)) {
137 cacheLib
.delete(luid
);
144 public synchronized void changeSource(String luid
, String newSource
,
145 Progress pg
) throws IOException
{
150 Progress pgCache
= new Progress();
151 Progress pgOrig
= new Progress();
153 pg
.addProgress(pgCache
, 1);
154 pg
.addProgress(pgOrig
, 1);
156 if (isCached(luid
)) {
157 cacheLib
.changeSource(luid
, newSource
, pgCache
);
160 lib
.changeSource(luid
, newSource
, pgOrig
);
167 * Check if the {@link Story} denoted by this Library UID is present in the
173 * @return TRUE if it is
175 public boolean isCached(String luid
) {
176 return cacheLib
.getInfo(luid
) != null;
180 * Clear the {@link Story} from the cache.
185 * @throws IOException
186 * in case of I/O error
188 public void clearFromCache(String luid
) throws IOException
{
189 if (isCached(luid
)) {
190 cacheLib
.delete(luid
);
195 // All the following methods are only used by Save and Delete in
199 protected int getNextId() {
200 throw new java
.lang
.InternalError("Should not have been called");
204 protected void doDelete(String luid
) throws IOException
{
205 throw new java
.lang
.InternalError("Should not have been called");
209 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
210 throw new java
.lang
.InternalError("Should not have been called");