diff --git a/src/main/java/org/caosdb/server/database/access/AbstractAccess.java b/src/main/java/org/caosdb/server/database/access/AbstractAccess.java index 9d00a876e8db21446b88b6ab00dfeb12ee043c0d..46e10adb476112a04d09a0d577eca810088044c0 100644 --- a/src/main/java/org/caosdb/server/database/access/AbstractAccess.java +++ b/src/main/java/org/caosdb/server/database/access/AbstractAccess.java @@ -25,6 +25,7 @@ package org.caosdb.server.database.access; import java.util.HashMap; import org.caosdb.server.database.misc.DBHelper; import org.caosdb.server.database.misc.RollBackHandler; +import org.caosdb.server.query.NoCache; import org.caosdb.server.transaction.TransactionInterface; public abstract class AbstractAccess<T extends TransactionInterface> implements Access { @@ -77,7 +78,13 @@ public abstract class AbstractAccess<T extends TransactionInterface> implements public void setUseCache(final Boolean useCache) { this.useCache = useCache; } - + /** + * Whether the transaction allows to use the query cache or other caches. This is controlled + * by the "cache" flag. + * + * @see {@link NoCache} + * @return true if caching is encouraged. + */ @Override public boolean useCache() { return this.useCache; diff --git a/src/main/java/org/caosdb/server/query/Query.java b/src/main/java/org/caosdb/server/query/Query.java index 80274dc7448e954d36cc131a980fd4c51535bc9a..ee3a971292ea3ecde3c68ca1c34ff935a6301770 100644 --- a/src/main/java/org/caosdb/server/query/Query.java +++ b/src/main/java/org/caosdb/server/query/Query.java @@ -70,6 +70,7 @@ import org.caosdb.server.permissions.EntityPermission; import org.caosdb.server.query.CQLParser.CqContext; import org.caosdb.server.query.CQLParsingErrorListener.ParsingError; import org.caosdb.server.transaction.TransactionInterface; +import org.caosdb.server.transaction.WriteTransaction; import org.caosdb.server.utils.ResultSetIterator; import org.jdom2.Element; import org.slf4j.Logger; @@ -279,15 +280,14 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac /** * This key-value cache stores lists of of (id, version hash, acl string) triplets. Those values * are the result sets of queries. The keys are created such that they are different for different - * for different queries (@see {@link getCacheKey}). The key includes realm and username of a + * for different queries (@see {@link #getCacheKey}). The key includes realm and username of a * subject, if the query result must not be shared among users. If intermediate permission checks * are done (e.g. in a subproperty query filter), the query result will be stored using a user * specific key. The final permission check has not yet been applied to the result set that is * stored in the cache. This allows (some) cache entries to be shared among users since the final * check is applied after the retrieval of the result set from the cache (@see {@link * filterEntitiesWithoutRetrievePermission}) The cache is invalidated whenever there is a write - * operation (@see {@link writeTODO}) TODO replace writeTODO and describe how invalidation is - * done. + * operation (@see {@link #clearCache} which is called in the {@link WriteTransaction#commit}). */ private static ICacheAccess<String, Serializable> cache = Cache.getCache("HIGH_LEVEL_QUERY_CACHE"); @@ -692,18 +692,6 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac } } - /** - * Whether the transaction allows this query instance to use the query cache. This is controlled - * by the "cache" flag. - * - * @see {@link NoCache} - * @return true if caching is encouraged. - * <p>TODO: Why is this a property of the Access class? Suggestion: Either remove useCache - * here and call directly the function of Access or move the property here. - */ - private boolean useCache() { - return getAccess().useCache(); - } /** * Try to set the `resultSet` member variable using the values stored in the high level query @@ -722,9 +710,9 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac // Decide whether user specific cache needs to be used or not // Currently, this is solely determined via filteredIntermediateResult. if (this.filteredIntermediateResult) { - setCache(getCacheKey(true), this.resultSet); + cacheItem(getCacheKey(true), this.resultSet); } else { - setCache(getCacheKey(false), this.resultSet); + cacheItem(getCacheKey(false), this.resultSet); } } /** Fill entities from `resultSet` into `container`. */ @@ -759,7 +747,7 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac try { parse(); setAccess(access); - if (useCache()) { + if (access.useCache()) { getResultFromCache(); } if (this.resultSet != null) { @@ -792,13 +780,11 @@ public class Query implements QueryInterface, ToElementable, TransactionInterfac * @param key * @param resultSet */ - // TODO is this a good name? - private void setCache(final String key, final List<IdVersionAclTriplet> resultSet) { + private void cacheItem(final String key, final List<IdVersionAclTriplet> resultSet) { synchronized (cache) { if (resultSet instanceof Serializable) { cache.put(key, (Serializable) resultSet); } else { - // TODO is this ever used? cache.put(key, new ArrayList<>(resultSet)); } }