return custom;
}
- List<MetaData> metas = getListBySource(source);
+ List<MetaData> metas = getList().filter(source, null, null);
if (metas.size() > 0) {
return getCover(metas.get(0).getLuid());
}
return custom;
}
- List<MetaData> metas = getListByAuthor(author);
+ List<MetaData> metas = getList().filter(null, author, null);
if (metas.size() > 0) {
return getCover(metas.get(0).getLuid());
}
* @param pg
* the optional progress reporter
*/
- public void refresh(Progress pg) {
+ public synchronized void refresh(Progress pg) {
try {
getMetas(pg);
} catch (IOException e) {
* @throws IOException
* in case of IOException
*/
- public synchronized List<MetaData> getList() throws IOException {
- return getMetas(null);
- }
-
- /**
- * List all the stories of the given source type in the {@link BasicLibrary} ,
- * or all the stories if NULL is passed as a type.
- * <p>
- * Cover images not included.
- *
- * @param source the type of story to retrieve, or NULL for all
- *
- * @return the stories
- *
- * @throws IOException in case of IOException
- *
- * @deprecated use {@link BasicLibrary#getList(Progress)} and
- * {@link MetaResultList#filter(String, String, String)}
- */
- @Deprecated
- public synchronized List<MetaData> getListBySource(String source) throws IOException {
- return getList(null).filter(source, null, null);
- }
-
- /**
- * List all the stories of the given author in the {@link BasicLibrary}, or
- * all the stories if NULL is passed as an author.
- * <p>
- * Cover images not included.
- *
- * @param author
- * the author of the stories to retrieve, or NULL for all
- *
- * @return the stories
- *
- * @throws IOException
- * in case of IOException
- *
- * @deprecated use {@link BasicLibrary#getList(Progress)} and
- * {@link MetaResultList#filter(String, String, String)}
- */
- public synchronized List<MetaData> getListByAuthor(String author) throws IOException {
- return getList(null).filter(null, author, null);
+ public MetaResultList getList() throws IOException {
+ return getList(null);
}
/**
meta.setTitle(newTitle);
meta.setAuthor(newAuthor);
saveMeta(meta, pg);
-
- invalidateInfo(luid);
}
/**
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
+import java.util.TreeSet;
import be.nikiroo.fanfix.Instance;
import be.nikiroo.fanfix.bundles.UiConfig;
* @author niki
*/
public class CacheLibrary extends BasicLibrary {
- private List<MetaData> metas;
+ private List<MetaData> metasReal;
+ private List<MetaData> metasMixed;
private BasicLibrary lib;
private LocalLibrary cacheLib;
}
@Override
- protected List<MetaData> getMetas(Progress pg) throws IOException {
+ protected synchronized List<MetaData> getMetas(Progress pg) throws IOException {
+ // We make sure that cached metas have precedence
+
if (pg == null) {
pg = new Progress();
}
- if (metas == null) {
- metas = lib.getMetas(pg);
+ if (metasMixed == null) {
+ if (metasReal == null) {
+ metasReal = lib.getMetas(pg);
+ }
+
+ metasMixed = new ArrayList<MetaData>();
+ TreeSet<String> cachedLuids = new TreeSet<String>();
+ for (MetaData cachedMeta : cacheLib.getMetas(null)) {
+ metasMixed.add(cachedMeta);
+ cachedLuids.add(cachedMeta.getLuid());
+ }
+ for (MetaData realMeta : metasReal) {
+ if (!cachedLuids.contains(realMeta.getLuid())) {
+ metasMixed.add(realMeta);
+ }
+ }
}
pg.done();
- return new ArrayList<MetaData>(metas);
- }
-
- @Override
- public synchronized MetaData getInfo(String luid) throws IOException {
- MetaData info = cacheLib.getInfo(luid);
- if (info == null) {
- info = lib.getInfo(luid);
- }
-
- return info;
+ return new ArrayList<MetaData>(metasMixed);
}
@Override
- public synchronized Story getStory(String luid, MetaData meta, Progress pg)
- throws IOException {
+ public synchronized Story getStory(String luid, MetaData meta, Progress pg) throws IOException {
if (pg == null) {
pg = new Progress();
}
if (!isCached(luid)) {
try {
cacheLib.imprt(lib, luid, pgImport);
- updateInfo(cacheLib.getInfo(luid));
+ updateMetaCache(metasMixed, cacheLib.getInfo(luid));
pgImport.done();
} catch (IOException e) {
Instance.getInstance().getTraceHandler().error(e);
}
@Override
- public synchronized File getFile(final String luid, Progress pg)
- throws IOException {
+ public synchronized File getFile(final String luid, Progress pg) throws IOException {
if (pg == null) {
pg = new Progress();
}
cacheLib.setAuthorCover(author, getCover(luid));
}
+ /**
+ * Invalidate the {@link Story} cache (when the content has changed, but we
+ * already have it) with the new given meta.
+ * <p>
+ * <b>Make sure to always use {@link MetaData} from the cached library
+ * in priority, here.</b>
+ *
+ * @param meta
+ * the {@link Story} to clear from the cache
+ *
+ * @throws IOException
+ * in case of IOException
+ */
@Override
+ @Deprecated
protected void updateInfo(MetaData meta) throws IOException {
+ throw new IOException(
+ "This method is not supported in a CacheLibrary, please use updateMetaCache");
+ }
+
+ // relplace the meta in Metas by Meta, add it if needed
+ // return TRUE = added
+ private boolean updateMetaCache(List<MetaData> metas, MetaData meta) {
if (meta != null && metas != null) {
boolean changed = false;
for (int i = 0; i < metas.size(); i++) {
if (!changed) {
metas.add(meta);
+ return true;
}
}
-
- cacheLib.updateInfo(meta);
- lib.updateInfo(meta);
+
+ return false;
}
@Override
protected void invalidateInfo(String luid) {
if (luid == null) {
- metas = null;
- } else if (metas != null) {
+ metasReal = null;
+ metasMixed = null;
+ } else {
+ invalidateInfo(metasReal, luid);
+ invalidateInfo(metasMixed, luid);
+ }
+
+ cacheLib.invalidateInfo(luid);
+ lib.invalidateInfo(luid);
+ }
+
+ // luid cannot be null
+ private void invalidateInfo(List<MetaData> metas, String luid) {
+ if (metas != null) {
for (int i = 0; i < metas.size(); i++) {
if (metas.get(i).getLuid().equals(luid)) {
metas.remove(i--);
}
}
}
-
- cacheLib.invalidateInfo(luid);
- lib.invalidateInfo(luid);
}
@Override
- public synchronized Story save(Story story, String luid, Progress pg)
- throws IOException {
+ public synchronized Story save(Story story, String luid, Progress pg) throws IOException {
Progress pgLib = new Progress();
Progress pgCacheLib = new Progress();
pg.addProgress(pgCacheLib, 1);
story = lib.save(story, luid, pgLib);
+ updateMetaCache(metasReal, story.getMeta());
+
story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib);
-
- updateInfo(story.getMeta());
+ updateMetaCache(metasMixed, story.getMeta());
return story;
}
}
@Override
- protected synchronized void changeSTA(String luid, String newSource,
- String newTitle, String newAuthor, Progress pg) throws IOException {
+ protected synchronized void changeSTA(String luid, String newSource, String newTitle, String newAuthor, Progress pg)
+ throws IOException {
if (pg == null) {
pg = new Progress();
}
meta.setAuthor(newAuthor);
pg.done();
- invalidateInfo(luid);
+ if (isCached(luid)) {
+ updateMetaCache(metasMixed, meta);
+ updateMetaCache(metasReal, lib.getInfo(luid));
+ } else {
+ updateMetaCache(metasReal, meta);
+ }
}
@Override
}
@Override
- public MetaData imprt(URL url, Progress pg) throws IOException {
+ public synchronized MetaData imprt(URL url, Progress pg) throws IOException {
if (pg == null) {
pg = new Progress();
}
pg.addProgress(pgCache, 3);
MetaData meta = lib.imprt(url, pgImprt);
- updateInfo(meta);
-
+ updateMetaCache(metasReal, meta);
+ metasMixed = null;
clearFromCache(meta.getLuid());
-
+
pg.done();
return meta;
}
}
@Override
- protected List<MetaData> getMetas(Progress pg) {
+ protected synchronized List<MetaData> getMetas(Progress pg) {
return new ArrayList<MetaData>(getStories(pg).keySet());
}
}
}
- invalidateInfo();
+ updateInfo(meta);
}
@Override
}
@Override
- protected List<MetaData> getMetas(Progress pg) throws IOException {
+ protected synchronized List<MetaData> getMetas(Progress pg) throws IOException {
return getMetasList("*", pg);
}
@Override
public void browse(String source) throws IOException {
- List<MetaData> stories = getLibrary().getListBySource(source);
+ List<MetaData> stories = getLibrary().getList().filter(source, null, null);
for (MetaData story : stories) {
String author = "";
try {
if (mode == Mode.SOURCE) {
- metas = reader.getLibrary().getListBySource(target);
+ metas = reader.getLibrary().getList().filter(target, null, null);
} else if (mode == Mode.AUTHOR) {
- metas = reader.getLibrary().getListByAuthor(target);
+ metas = reader.getLibrary().getList().filter(null, target, null);
} else {
- metas = reader.getLibrary().getList();
+ metas = reader.getLibrary().getList().getMetas();
}
} catch (IOException e) {
Instance.getInstance().getTraceHandler().error(e);
* @author niki
*/
public class GuiReaderBookInfo {
+ /**
+ * The type of {@link GuiReaderBook} (i.e., related to a story or to something else that
+ * can encompass stories).
+ *
+ * @author niki
+ */
public enum Type {
/** A normal story, which can be "read". */
STORY,
int size = 0;
try {
- size = lib.getListBySource(source).size();
+ size = lib.getList().filter(source, null, null).size();
} catch (IOException e) {
}
int size = 0;
try {
- size = lib.getListByAuthor(author).size();
+ size = lib.getList().filter(null, author, null).size();
} catch (IOException e) {
}
List<MetaData> metas;
try {
if (currentType) {
- metas = lib.getListBySource(value);
+ metas = lib.getList().filter(value, null, null);
} else {
- metas = lib.getListByAuthor(value);
+ metas = lib.getList().filter(null, value, null);
}
} catch (IOException e) {
error(e.getLocalizedMessage(), "IOException", e);
break;
case SOURCE:
for (MetaData meta : helper.getReader().getLibrary()
- .getListBySource(book.getInfo().getMainInfo())) {
+ .getList().filter(book.getInfo().getMainInfo(), null, null)) {
luids.add(meta.getLuid());
}
break;
case AUTHOR:
for (MetaData meta : helper.getReader().getLibrary()
- .getListByAuthor(book.getInfo().getMainInfo())) {
+ .getList().filter(null, book.getInfo().getMainInfo(), null)) {
luids.add(meta.getLuid());
}
break;
addTest(new TestCase("getList") {
@Override
public void test() throws Exception {
- List<MetaData> metas = lib.getList();
+ List<MetaData> metas = lib.getList().getMetas();
assertEquals(errMess, Arrays.asList(),
titlesAsList(metas));
}
lib.save(story(luid1, name1, source1, author1), luid1,
null);
- List<MetaData> metas = lib.getList();
+ List<MetaData> metas = lib.getList().getMetas();
assertEquals(errMess, Arrays.asList(name1),
titlesAsList(metas));
}
lib.save(story(luid2, name2, source2, author1), luid2,
null);
- metas = lib.getList();
+ metas = lib.getList().getMetas();
assertEquals(errMess, Arrays.asList(name1, name2),
titlesAsList(metas));
lib.save(story(luid3, name3, source2, author1), luid3,
null);
- metas = lib.getList();
+ metas = lib.getList().getMetas();
assertEquals(errMess,
Arrays.asList(name1, name2, name3),
titlesAsList(metas));
lib.save(story(luid3, name3ex, source2, author2),
luid3, null);
- List<MetaData> metas = lib.getList();
+ List<MetaData> metas = lib.getList().getMetas();
assertEquals(errMess,
Arrays.asList(name1, name2, name3ex),
titlesAsList(metas));
addTest(new TestCase("getList with results") {
@Override
public void test() throws Exception {
- List<MetaData> metas = lib.getList();
+ List<MetaData> metas = lib.getList().getMetas();
assertEquals(3, metas.size());
}
});
public void test() throws Exception {
List<MetaData> metas = null;
- metas = lib.getListBySource(source1);
+ metas = lib.getList().filter(source1, null, null);
assertEquals(1, metas.size());
- metas = lib.getListBySource(source2);
+ metas = lib.getList().filter(source2, null, null);
assertEquals(2, metas.size());
- metas = lib.getListBySource(null);
+ metas = lib.getList().filter((String)null, null, null);
assertEquals(3, metas.size());
}
});
public void test() throws Exception {
List<MetaData> metas = null;
- metas = lib.getListByAuthor(author1);
+ metas = lib.getList().filter(null, author1, null);
assertEquals(2, metas.size());
- metas = lib.getListByAuthor(author2);
+ metas = lib.getList().filter(null, author2, null);
assertEquals(1, metas.size());
- metas = lib.getListByAuthor(null);
+ metas = lib.getList().filter((String)null, null, null);
assertEquals(3, metas.size());
}
});
lib.changeSource(luid3, source1, null);
- metas = lib.getListBySource(source1);
+ metas = lib.getList().filter(source1, null, null);
assertEquals(2, metas.size());
- metas = lib.getListBySource(source2);
+ metas = lib.getList().filter(source2, null, null);
assertEquals(1, metas.size());
- metas = lib.getListBySource(null);
+ metas = lib.getList().filter((String)null, null, null);
assertEquals(3, metas.size());
}
});
lib.save(story(luid3, "My story 3", source2, author2),
luid3, null);
- metas = lib.getListBySource(source1);
+ metas = lib.getList().filter(source1, null, null);
assertEquals(1, metas.size());
- metas = lib.getListBySource(source2);
+ metas = lib.getList().filter(source2, null, null);
assertEquals(2, metas.size());
- metas = lib.getListBySource(null);
+ metas = lib.getList().filter((String)null, null, null);
assertEquals(3, metas.size());
}
});