1 package be
.nikiroo
.fanfix
.library
;
4 import java
.io
.IOException
;
6 import java
.util
.ArrayList
;
8 import java
.util
.TreeSet
;
10 import be
.nikiroo
.fanfix
.Instance
;
11 import be
.nikiroo
.fanfix
.bundles
.UiConfig
;
12 import be
.nikiroo
.fanfix
.bundles
.UiConfigBundle
;
13 import be
.nikiroo
.fanfix
.data
.MetaData
;
14 import be
.nikiroo
.fanfix
.data
.Story
;
15 import be
.nikiroo
.fanfix
.output
.BasicOutput
.OutputType
;
16 import be
.nikiroo
.utils
.Image
;
17 import be
.nikiroo
.utils
.Progress
;
20 * This library will cache another pre-existing {@link BasicLibrary}.
24 public class CacheLibrary
extends BasicLibrary
{
25 private List
<MetaData
> metasReal
;
26 private List
<MetaData
> metasMixed
;
27 private Object metasLock
= new Object();
29 private BasicLibrary lib
;
30 private LocalLibrary cacheLib
;
33 * Create a cache library around the given one.
35 * It will return the same result, but those will be saved to disk at the
36 * same time to be fetched quicker the next time.
39 * the cache directory where to save the files to disk
41 * the original library to wrap
43 * the configuration used to know which kind of default
44 * {@link OutputType} to use for images and non-images stories
46 public CacheLibrary(File cacheDir
, BasicLibrary lib
,
47 UiConfigBundle config
) {
48 this.cacheLib
= new LocalLibrary(cacheDir
, //
49 config
.getString(UiConfig
.GUI_NON_IMAGES_DOCUMENT_TYPE
),
50 config
.getString(UiConfig
.GUI_IMAGES_DOCUMENT_TYPE
), true);
55 public String
getLibraryName() {
56 return lib
.getLibraryName();
60 public Status
getStatus() {
61 return lib
.getStatus();
65 protected List
<MetaData
> getMetas(Progress pg
) throws IOException
{
71 synchronized (metasLock
) {
72 // We make sure that cached metas have precedence
73 if (metasMixed
== null) {
74 if (metasReal
== null) {
75 metasReal
= lib
.getMetas(pg
);
78 metasMixed
= new ArrayList
<MetaData
>();
79 TreeSet
<String
> cachedLuids
= new TreeSet
<String
>();
80 for (MetaData cachedMeta
: cacheLib
.getMetas(null)) {
81 metasMixed
.add(cachedMeta
);
82 cachedLuids
.add(cachedMeta
.getLuid());
84 for (MetaData realMeta
: metasReal
) {
85 if (!cachedLuids
.contains(realMeta
.getLuid())) {
86 metasMixed
.add(realMeta
);
91 copy
= new ArrayList
<MetaData
>(metasMixed
);
99 public synchronized Story
getStory(String luid
, MetaData meta
, Progress pg
)
105 Progress pgImport
= new Progress();
106 Progress pgGet
= new Progress();
109 pg
.addProgress(pgImport
, 3);
110 pg
.addProgress(pgGet
, 1);
112 if (!isCached(luid
)) {
114 cacheLib
.imprt(lib
, luid
, pgImport
);
115 updateMetaCache(metasMixed
, cacheLib
.getInfo(luid
));
117 } catch (IOException e
) {
118 Instance
.getInstance().getTraceHandler().error(e
);
125 String type
= cacheLib
.getOutputType(meta
.isImageDocument());
126 MetaData cachedMeta
= meta
.clone();
127 cachedMeta
.setType(type
);
129 return cacheLib
.getStory(luid
, cachedMeta
, pg
);
133 public synchronized File
getFile(final String luid
, Progress pg
)
139 Progress pgGet
= new Progress();
140 Progress pgRecall
= new Progress();
143 pg
.addProgress(pgGet
, 4);
144 pg
.addProgress(pgRecall
, 1);
146 if (!isCached(luid
)) {
147 getStory(luid
, pgGet
);
151 File file
= cacheLib
.getFile(luid
, pgRecall
);
159 public Image
getCover(final String luid
) throws IOException
{
160 if (isCached(luid
)) {
161 return cacheLib
.getCover(luid
);
164 // We could update the cache here, but it's not easy
165 return lib
.getCover(luid
);
169 public Image
getSourceCover(String source
) throws IOException
{
170 Image custom
= getCustomSourceCover(source
);
171 if (custom
!= null) {
175 Image cached
= cacheLib
.getSourceCover(source
);
176 if (cached
!= null) {
180 return lib
.getSourceCover(source
);
184 public Image
getAuthorCover(String author
) throws IOException
{
185 Image custom
= getCustomAuthorCover(author
);
186 if (custom
!= null) {
190 Image cached
= cacheLib
.getAuthorCover(author
);
191 if (cached
!= null) {
195 return lib
.getAuthorCover(author
);
199 public Image
getCustomSourceCover(String source
) throws IOException
{
200 Image custom
= cacheLib
.getCustomSourceCover(source
);
201 if (custom
== null) {
202 custom
= lib
.getCustomSourceCover(source
);
203 if (custom
!= null) {
204 cacheLib
.setSourceCover(source
, custom
);
212 public Image
getCustomAuthorCover(String author
) throws IOException
{
213 Image custom
= cacheLib
.getCustomAuthorCover(author
);
214 if (custom
== null) {
215 custom
= lib
.getCustomAuthorCover(author
);
216 if (custom
!= null) {
217 cacheLib
.setAuthorCover(author
, custom
);
225 public void setSourceCover(String source
, String luid
) throws IOException
{
226 lib
.setSourceCover(source
, luid
);
227 cacheLib
.setSourceCover(source
, getCover(luid
));
231 public void setAuthorCover(String author
, String luid
) throws IOException
{
232 lib
.setAuthorCover(author
, luid
);
233 cacheLib
.setAuthorCover(author
, getCover(luid
));
237 * Invalidate the {@link Story} cache (when the content has changed, but we
238 * already have it) with the new given meta.
240 * <b>Make sure to always use {@link MetaData} from the cached library in
241 * priority, here.</b>
244 * the {@link Story} to clear from the cache
246 * @throws IOException
247 * in case of IOException
251 protected void updateInfo(MetaData meta
) throws IOException
{
252 throw new IOException(
253 "This method is not supported in a CacheLibrary, please use updateMetaCache");
256 // relplace the meta in Metas by Meta, add it if needed
257 // return TRUE = added
258 private boolean updateMetaCache(List
<MetaData
> metas
, MetaData meta
) {
259 if (meta
!= null && metas
!= null) {
260 synchronized (metasLock
) {
261 boolean changed
= false;
262 for (int i
= 0; i
< metas
.size(); i
++) {
263 if (metas
.get(i
).getLuid().equals(meta
.getLuid())) {
280 protected void invalidateInfo(String luid
) {
282 synchronized (metasLock
) {
287 invalidateInfo(metasReal
, luid
);
288 invalidateInfo(metasMixed
, luid
);
291 cacheLib
.invalidateInfo(luid
);
292 lib
.invalidateInfo(luid
);
295 // luid cannot be null
296 private void invalidateInfo(List
<MetaData
> metas
, String luid
) {
298 synchronized (metasLock
) {
299 for (int i
= 0; i
< metas
.size(); i
++) {
300 if (metas
.get(i
).getLuid().equals(luid
)) {
309 public synchronized Story
save(Story story
, String luid
, Progress pg
)
311 Progress pgLib
= new Progress();
312 Progress pgCacheLib
= new Progress();
319 pg
.addProgress(pgLib
, 1);
320 pg
.addProgress(pgCacheLib
, 1);
322 story
= lib
.save(story
, luid
, pgLib
);
323 updateMetaCache(metasReal
, story
.getMeta());
325 story
= cacheLib
.save(story
, story
.getMeta().getLuid(), pgCacheLib
);
326 updateMetaCache(metasMixed
, story
.getMeta());
332 public synchronized void delete(String luid
) throws IOException
{
333 if (isCached(luid
)) {
334 cacheLib
.delete(luid
);
338 invalidateInfo(luid
);
342 protected synchronized void changeSTA(String luid
, String newSource
,
343 String newTitle
, String newAuthor
, Progress pg
) throws IOException
{
348 Progress pgCache
= new Progress();
349 Progress pgOrig
= new Progress();
351 pg
.addProgress(pgCache
, 1);
352 pg
.addProgress(pgOrig
, 1);
354 MetaData meta
= getInfo(luid
);
356 throw new IOException("Story not found: " + luid
);
359 if (isCached(luid
)) {
360 cacheLib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgCache
);
364 lib
.changeSTA(luid
, newSource
, newTitle
, newAuthor
, pgOrig
);
367 meta
.setSource(newSource
);
368 meta
.setTitle(newTitle
);
369 meta
.setAuthor(newAuthor
);
372 if (isCached(luid
)) {
373 updateMetaCache(metasMixed
, meta
);
374 updateMetaCache(metasReal
, lib
.getInfo(luid
));
376 updateMetaCache(metasReal
, meta
);
381 public boolean isCached(String luid
) {
383 return cacheLib
.getInfo(luid
) != null;
384 } catch (IOException e
) {
390 public void clearFromCache(String luid
) throws IOException
{
391 if (isCached(luid
)) {
392 cacheLib
.delete(luid
);
397 public MetaData
imprt(URL url
, Progress pg
) throws IOException
{
402 Progress pgImprt
= new Progress();
403 Progress pgCache
= new Progress();
405 pg
.addProgress(pgImprt
, 7);
406 pg
.addProgress(pgCache
, 3);
408 MetaData meta
= lib
.imprt(url
, pgImprt
);
409 updateMetaCache(metasReal
, meta
);
412 clearFromCache(meta
.getLuid());
418 // All the following methods are only used by Save and Delete in
422 protected String
getNextId() {
423 throw new java
.lang
.InternalError("Should not have been called");
427 protected void doDelete(String luid
) throws IOException
{
428 throw new java
.lang
.InternalError("Should not have been called");
432 protected Story
doSave(Story story
, Progress pg
) throws IOException
{
433 throw new java
.lang
.InternalError("Should not have been called");