* Invalidate the {@link Story} cache (when the content should be re-read
* because it was changed).
*/
- protected abstract void clearCache();
+ protected void invalidateInfo() {
+ invalidateInfo(null);
+ }
+
+ /**
+ * Invalidate the {@link Story} cache (when the content should be re-read
+ * because it was changed).
+ *
+ * @param luid
+ * the luid of the {@link Story} to clear from the cache, or NULL
+ * for all stories
+ */
+ protected abstract void invalidateInfo(String luid);
/**
* Return the next LUID that can be used.
meta.setLuid(luid);
}
- if (getInfo(luid) != null) {
+ if (luid != null && getInfo(luid) != null) {
delete(luid);
}
doSave(story, pg);
- clearCache();
+ invalidateInfo(luid);
return story;
}
*/
public synchronized void delete(String luid) throws IOException {
doDelete(luid);
- clearCache();
+ invalidateInfo(luid);
}
/**
try {
cacheLib.imprt(lib, luid, pgImport);
pgImport.done();
- clearCache();
+ invalidateInfo(luid);
} catch (IOException e) {
Instance.getTraceHandler().error(e);
}
}
@Override
- protected void clearCache() {
- metas = null;
- cacheLib.clearCache();
- lib.clearCache();
+ protected void invalidateInfo(String luid) {
+ List<MetaData> metas = this.metas;
+
+ if (luid == null) {
+ this.metas = null;
+ } else if (metas != null) {
+ MetaData meta = lib.getInfo(luid);
+ for (int i = 0; i < metas.size(); i++) {
+ if (metas.get(i).getLuid().equals(luid)) {
+ if (meta != null) {
+ metas.set(i, meta);
+ meta = null;
+ } else {
+ metas.remove(i--);
+ }
+ }
+ }
+
+ if (meta != null) {
+ metas.add(meta);
+ }
+ }
+
+ cacheLib.invalidateInfo(luid);
+ lib.invalidateInfo(luid);
}
@Override
story = lib.save(story, luid, pgLib);
story = cacheLib.save(story, story.getMeta().getLuid(), pgCacheLib);
- clearCache();
+ invalidateInfo(story.getMeta().getLuid());
return story;
}
cacheLib.delete(luid);
}
lib.delete(luid);
- clearCache();
+
+ List<MetaData> metas = this.metas;
+ if (metas != null) {
+ for (int i = 0; i < metas.size(); i++) {
+ if (metas.get(i).getLuid().equals(luid)) {
+ metas.set(i, lib.getInfo(luid));
+ }
+ }
+ }
}
@Override
pg.addProgress(pgCache, 1);
pg.addProgress(pgOrig, 1);
+ MetaData meta = getInfo(luid);
+ if (meta == null) {
+ throw new IOException("Story not found: " + luid);
+ }
+
if (isCached(luid)) {
cacheLib.changeSource(luid, newSource, pgCache);
}
pgCache.done();
+
lib.changeSource(luid, newSource, pgOrig);
pgOrig.done();
- getInfo(luid).setSource(newSource);
-
+ meta.setSource(newSource);
pg.done();
}
public void clearFromCache(String luid) throws IOException {
if (isCached(luid)) {
cacheLib.delete(luid);
- clearCache();
+ invalidateInfo(luid);
}
}
}
@Override
- protected void clearCache() {
+ protected void invalidateInfo(String luid) {
stories = null;
sourceCovers = new HashMap<String, BufferedImage>();
}
for (File file : getRelatedFiles(luid)) {
// TODO: throw an IOException if we cannot delete the files?
IOUtils.deltree(file);
+ file.getParentFile().delete();
}
}
"\\.info$", "");
InfoCover.writeInfo(newDir, name, meta);
relatedFile.delete();
+ relatedFile.getParentFile().delete();
} catch (IOException e) {
Instance.getTraceHandler().error(e);
}
} else {
relatedFile.renameTo(new File(newDir, relatedFile.getName()));
+ relatedFile.getParentFile().delete();
}
}
- clearCache();
+ invalidateInfo();
}
@Override
pg.add(1);
}
- clearCache();
+ invalidateInfo();
pg.done();
return;
}
super.imprt(other, luid, pg);
- clearCache();
+ invalidateInfo();
}
/**
* {@link LocalLibrary#baseDir}.
* <p>
* Will use a cached list when possible (see
- * {@link BasicLibrary#clearCache()}).
+ * {@link BasicLibrary#invalidateInfo()}).
*
* @param pg
* the optional {@link Progress}
}.connect();
// because the meta changed:
- clearCache();
- refresh(pgRefresh);
+ invalidateInfo(luidSaved[0]);
MetaData meta = getInfo(luidSaved[0]);
meta.setCover(story.getMeta().getCover());
rep = send(null);
}
-
- getInfo(luid).setSource(newSource);
}
@Override
}
}
+ @Override
+ public synchronized MetaData getInfo(String luid) {
+ List<MetaData> metas = getMetasList(luid, null);
+ if (!metas.isEmpty()) {
+ return metas.get(0);
+ }
+
+ return null;
+ }
+
@Override
protected List<MetaData> getMetas(Progress pg) {
+ return getMetasList("*", pg);
+ }
+
+ @Override
+ protected void invalidateInfo(String luid) {
+ }
+
+ // The following methods are only used by Save and Delete in BasicLibrary:
+
+ @Override
+ protected int getNextId() {
+ throw new java.lang.InternalError("Should not have been called");
+ }
+
+ @Override
+ protected void doDelete(String luid) throws IOException {
+ throw new java.lang.InternalError("Should not have been called");
+ }
+
+ @Override
+ protected Story doSave(Story story, Progress pg) throws IOException {
+ throw new java.lang.InternalError("Should not have been called");
+ }
+
+ //
+
+ /**
+ * Return the meta of the given story or a list of all known metas if the
+ * luid is "*".
+ *
+ * @param luid
+ * the luid of the story or *
+ * @param pg
+ * the optional progress
+ *
+ *
+ * @return the metas
+ */
+ private List<MetaData> getMetasList(final String luid, Progress pg) {
final Progress pgF = pg;
final List<MetaData> metas = new ArrayList<MetaData>();
pg = new Progress();
}
- Object rep = send(new Object[] { md5, "GET_METADATA", "*" });
+ Object rep = send(new Object[] { md5, "GET_METADATA", luid });
while (true) {
if (!RemoteLibraryServer.updateProgress(pg, rep)) {
rep = send(null);
}
- for (MetaData meta : (MetaData[]) rep) {
- metas.add(meta);
+ if (rep instanceof MetaData[]) {
+ for (MetaData meta : (MetaData[]) rep) {
+ metas.add(meta);
+ }
+ } else if (rep != null) {
+ metas.add((MetaData) rep);
}
}
return metas;
}
-
- @Override
- protected void clearCache() {
- }
-
- // The following methods are only used by Save and Delete in BasicLibrary:
-
- @Override
- protected int getNextId() {
- throw new java.lang.InternalError("Should not have been called");
- }
-
- @Override
- protected void doDelete(String luid) throws IOException {
- throw new java.lang.InternalError("Should not have been called");
- }
-
- @Override
- protected Story doSave(Story story, Progress pg) throws IOException {
- throw new java.lang.InternalError("Should not have been called");
- }
}
import java.io.IOException;
import java.net.URL;
-import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.List;
* <ul>
* <li>[md5] PING: will return PONG if the key is accepted</li>
* <li>[md5] GET_METADATA *: will return the metadata of all the stories in the
- * library</li>
+ * library (array)</li>
+ * *
+ * <li>[md5] GET_METADATA [luid]: will return the metadata of the story of LUID
+ * luid</li>
* <li>[md5] GET_STORY [luid]: will return the given story if it exists (or NULL
* if not)</li>
* <li>[md5] SAVE_STORY [luid]: save the story (that must be sent just after the
createPgForwarder(action));
return metas.toArray(new MetaData[] {});
}
- throw new InvalidParameterException(
- "only * is valid here, but you passed: " + args[0]);
+
+ return new MetaData[] { Instance.getLibrary().getInfo(
+ (String) args[0]) };
} else if ("GET_STORY".equals(command)) {
MetaData meta = Instance.getLibrary().getInfo((String) args[0]);
meta = meta.clone();