Skip to content

Commit

Permalink
core: Fixed metadata modified time to be unset for cache storage
Browse files Browse the repository at this point in the history
This is necessary to retrieve the proper modified time from the
source storage, instead of using the object activation time.
  • Loading branch information
cederberg committed Dec 31, 2023
1 parent 8798db2 commit bf4ca8e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 39 deletions.
7 changes: 4 additions & 3 deletions src/java/org/rapidcontext/app/proc/StorageCopyProcedure.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.rapidcontext.app.ApplicationContext;
import org.rapidcontext.core.data.Binary;
Expand Down Expand Up @@ -154,9 +155,9 @@ public static boolean copyObject(Path src, Path dst, boolean update, String ext)
if (update) {
Metadata srcMeta = storage.lookup(src);
Metadata dstMeta = storage.lookup(dst);
Date srcTime = (srcMeta == null) ? new Date(0) : srcMeta.modified();
Date dstTime = (dstMeta == null) ? new Date(0) : dstMeta.modified();
if (!dstTime.before(srcTime)) {
Date srcTime = (srcMeta == null) ? null : srcMeta.modified();
Date dstTime = (dstMeta == null) ? null : dstMeta.modified();
if (ObjectUtils.compare(srcTime, dstTime) <= 0) {
return false;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/java/org/rapidcontext/core/proc/Library.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.TreeSet;
import java.util.logging.Logger;

import org.apache.commons.lang3.ObjectUtils;
import org.rapidcontext.core.data.Dict;
import org.rapidcontext.core.storage.Metadata;
import org.rapidcontext.core.storage.Path;
Expand Down Expand Up @@ -253,7 +254,7 @@ public Procedure getProcedure(String name) throws ProcedureException {
} else if (meta == null) {
throw new ProcedureException("no procedure '" + name + "' found");
}
if (proc == null || meta.modified().after(proc.getLastModified())) {
if (proc == null || ObjectUtils.compare(meta.modified(), proc.getLastModified()) > 0) {
return loadProcedure(name);
} else {
return proc;
Expand Down
14 changes: 6 additions & 8 deletions src/java/org/rapidcontext/core/storage/DirStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ public File dir() {
@Override
public Metadata lookup(Path path) {
if (PATH_STORAGEINFO.equals(path)) {
Metadata m = new Metadata(Dict.class, PATH_STORAGEINFO, path());
return m.modified(mountTime());
return new Metadata(Dict.class, PATH_STORAGEINFO, path(), mountTime());
}
File file = locateFile(path);
if (file == null) {
Expand All @@ -98,15 +97,14 @@ public Metadata lookup(Path path) {
Date modified = new Date(file.lastModified());
long size = file.length();
if (file.isDirectory()) {
Metadata m = new Metadata(Index.class, toPath(file, true), path());
return m.modified(modified);
return new Metadata(Index.class, toPath(file, true), path(), modified);
} else if (!path.name().equalsIgnoreCase(file.getName())) {
File f = new File(file.getParentFile(), objectName(file.getName()));
Metadata m = new Metadata(Dict.class, toPath(f, false), path());
return m.mimeType(mime).modified(modified).size(size);
Metadata m = new Metadata(Dict.class, toPath(f, false), path(), modified);
return m.mimeType(mime).size(size);
} else {
Metadata m = new Metadata(Binary.class, toPath(file, false), path());
return m.mimeType(mime).modified(modified).size(size);
Metadata m = new Metadata(Binary.class, toPath(file, false), path(), modified);
return m.mimeType(mime).size(size);
}
}

Expand Down
15 changes: 5 additions & 10 deletions src/java/org/rapidcontext/core/storage/MemoryStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public MemoryStorage(String id, boolean readWrite, boolean storageInfo) {
this.storageInfo = storageInfo;
if (storageInfo) {
objects.put(PATH_STORAGEINFO, dict);
meta.put(PATH_STORAGEINFO, new Metadata(Dict.class, PATH_STORAGEINFO, Path.ROOT));
meta.put(PATH_STORAGEINFO, new Metadata(Dict.class, PATH_STORAGEINFO, Path.ROOT, null));
indexInsert(PATH_STORAGEINFO);
}
}
Expand Down Expand Up @@ -115,8 +115,7 @@ public synchronized void destroy() {
@Override
public synchronized Metadata lookup(Path path) {
if (storageInfo && PATH_STORAGEINFO.equals(path)) {
Metadata m = new Metadata(Dict.class, PATH_STORAGEINFO, path());
return m.modified(mountTime());
return new Metadata(Dict.class, PATH_STORAGEINFO, path(), mountTime());
}
return meta.get(path);
}
Expand Down Expand Up @@ -180,7 +179,7 @@ public synchronized void store(Path path, Object data) throws StorageException {
remove(path);
}
objects.put(path, data);
meta.put(path, new Metadata(data.getClass(), path, path()));
meta.put(path, new Metadata(data.getClass(), path, path(), null));
indexInsert(path);
}

Expand Down Expand Up @@ -244,11 +243,9 @@ private void indexInsert(Path path) {
idx.addObject(path.name());
}
idx.setModified(null);
if (objects.containsKey(parent)) {
meta.get(parent).modified(null);
} else {
if (!objects.containsKey(parent)) {
objects.put(parent, idx);
meta.put(parent, new Metadata(Index.class, parent, Path.ROOT));
meta.put(parent, new Metadata(Index.class, parent, Path.ROOT, null));
if (!parent.isRoot()) {
indexInsert(parent);
}
Expand Down Expand Up @@ -277,8 +274,6 @@ private void indexRemove(Path path) {
if (!parent.isRoot()) {
indexRemove(parent);
}
} else {
meta.get(parent).modified(null);
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/java/org/rapidcontext/core/storage/Metadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.Date;

import org.apache.commons.lang3.ObjectUtils;
import org.rapidcontext.core.data.Array;
import org.rapidcontext.core.data.Binary;
import org.rapidcontext.core.data.Dict;
Expand Down Expand Up @@ -149,8 +150,8 @@ public Metadata(Path path, Metadata meta) {
/**
* Creates a new metadata container that is a merged copy of two
* others. The first will serve as the base, adding additional
* storage paths as needed. For index objects, the last modified
* date be adjusted to the maximum of the two objects.
* storage paths as needed. The last modified date be adjusted to
* the maximum of the two objects.
*
* @param meta1 the first metadata object
* @param meta2 the second metadata object
Expand All @@ -162,9 +163,7 @@ private Metadata(Metadata meta1, Metadata meta2) {
if (mimeType() == null) {
mimeType(meta2.mimeType());
}
if (isIndex() && meta2.modified().after(meta1.modified())) {
modified(meta2.modified());
}
modified(ObjectUtils.max(meta1.modified(), meta2.modified()));
size(Math.max(meta1.size(), meta2.size()));
}

Expand All @@ -174,14 +173,15 @@ private Metadata(Metadata meta1, Metadata meta2) {
* @param clazz the object class
* @param path the absolute object path
* @param storagePath the absolute storage path
* @param modified the last modified date, or null for unknown
*/
public Metadata(Class<?> clazz, Path path, Path storagePath) {
public Metadata(Class<?> clazz, Path path, Path storagePath, Date modified) {
super(path.toString(), "metadata");
dict.set(KEY_CATEGORY, category(clazz));
dict.set(KEY_CLASS, clazz);
dict.set(KEY_PATH, path);
dict.set(KEY_MIMETYPE, null);
dict.set(KEY_MODIFIED, new Date());
dict.set(KEY_MODIFIED, modified);
dict.set(KEY_SIZE, 0L);
dict.set(PREFIX_COMPUTED + KEY_STORAGES, Array.of(storagePath));
}
Expand Down Expand Up @@ -304,7 +304,8 @@ protected Metadata mimeType(String mime) {
/**
* Returns the last modified date for the object.
*
* @return the last modified date for the object
* @return the last modified date for the object, or
* null if unknown
*/
public Date modified() {
return dict.get(KEY_MODIFIED, Date.class);
Expand All @@ -313,12 +314,12 @@ public Date modified() {
/**
* Sets the last modified date for the object.
*
* @param date the date to set, or null for now
* @param date the date to set, or null for unknown
*
* @return this metadata object (for chaining)
*/
protected Metadata modified(Date date) {
dict.set(KEY_MODIFIED, (date == null) ? new Date() : date);
dict.set(KEY_MODIFIED, date);
return this;
}

Expand Down Expand Up @@ -358,6 +359,7 @@ public Dict serialize() {
copy.remove(KEY_ID);
copy.remove(KEY_TYPE);
copy.remove(PREFIX_COMPUTED + KEY_ACTIVATED_TIME);
copy.setIfNull(KEY_MODIFIED, () -> new Date());
copy.add(PREFIX_COMPUTED + "plugin", Plugin.source(this));
return copy;
}
Expand Down
13 changes: 6 additions & 7 deletions src/java/org/rapidcontext/core/storage/ZipStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,20 @@ public final File file() {
@Override
public Metadata lookup(Path path) {
if (PATH_STORAGEINFO.equals(path)) {
Metadata m = new Metadata(Dict.class, PATH_STORAGEINFO, path());
return m.modified(mountTime());
return new Metadata(Dict.class, PATH_STORAGEINFO, path(), mountTime());
}
Path match = locatePath(path);
Object obj = (match == null) ? null : entries.get(match);
if (obj instanceof Index) {
Metadata m = new Metadata(Index.class, match, path());
return m.modified(((Index) obj).modified());
Index idx = (Index) obj;
return new Metadata(Index.class, match, path(), idx.modified());
} else if (obj instanceof ZipEntry) {
ZipEntry entry = (ZipEntry) obj;
Date modified = new Date(entry.getTime());
Metadata m = path.equals(match) ?
new Metadata(Binary.class, match, path()) :
new Metadata(Dict.class, objectPath(match), path());
new Metadata(Binary.class, match, path(), modified) :
new Metadata(Dict.class, objectPath(match), path(), modified);
m.mimeType(Mime.type(entry.getName()));
m.modified(new Date(entry.getTime()));
m.size(entry.getSize());
return m;
} else {
Expand Down

0 comments on commit bf4ca8e

Please sign in to comment.