Class NodeDocumentCache

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class NodeDocumentCache
    extends java.lang.Object
    implements java.io.Closeable
    Cache for the NodeDocuments. This class is thread-safe and uses the provided NodeDocumentLock.
    • Constructor Detail

      • NodeDocumentCache

        public NodeDocumentCache​(@NotNull
                                 @NotNull org.apache.jackrabbit.guava.common.cache.Cache<CacheValue,​NodeDocument> nodeDocumentsCache,
                                 @NotNull
                                 @NotNull CacheStats nodeDocumentsCacheStats,
                                 @NotNull
                                 @NotNull org.apache.jackrabbit.guava.common.cache.Cache<StringValue,​NodeDocument> prevDocumentsCache,
                                 @NotNull
                                 @NotNull CacheStats prevDocumentsCacheStats,
                                 @NotNull
                                 @NotNull NodeDocumentLocks locks)
    • Method Detail

      • invalidate

        public void invalidate​(@NotNull
                               @NotNull java.lang.String key)
        Invalidate document with given key.
        Parameters:
        key - to invalidate
      • markChanged

        public void markChanged​(@NotNull
                                @NotNull java.lang.String key)
        Mark that the document with the given key is being changed.
        Parameters:
        key - to mark
      • invalidateOutdated

        public int invalidateOutdated​(@NotNull
                                      @NotNull java.util.Map<java.lang.String,​ModificationStamp> modStamps)
        Invalidate document with given keys iff their modification stamps are different as passed in the map.
        Parameters:
        modStamps - map where key is the document id and the value is the modification stamps.
        Returns:
        number of invalidated entries
      • getIfPresent

        @Nullable
        public @Nullable NodeDocument getIfPresent​(@NotNull
                                                   @NotNull java.lang.String key)
        Return the cached value or null.
        Parameters:
        key - document key
        Returns:
        cached value of null if there's no document with given key cached
      • get

        @NotNull
        public @NotNull NodeDocument get​(@NotNull
                                         @NotNull java.lang.String key,
                                         @NotNull
                                         @NotNull java.util.concurrent.Callable<NodeDocument> valueLoader)
                                  throws java.util.concurrent.ExecutionException
        Return the document matching given key, optionally loading it from an external source.

        This method can modify the cache, so it's synchronized. The getIfPresent(String) is not synchronized and will be faster if you need to get the cached value outside the critical section.

        Parameters:
        key - document key
        valueLoader - object used to retrieve the document
        Returns:
        document matching given key
        Throws:
        java.util.concurrent.ExecutionException
        See Also:
        Cache.get(Object, Callable)
      • put

        public void put​(@NotNull
                        @NotNull NodeDocument doc)
        Puts document into cache.
        Parameters:
        doc - document to put
      • putIfNewer

        @NotNull
        public @NotNull NodeDocument putIfNewer​(@NotNull
                                                @NotNull NodeDocument doc)
        Puts document into cache iff no entry with the given key is cached already or the cached document is older (has smaller Document.MOD_COUNT).
        Parameters:
        doc - the document to add to the cache
        Returns:
        either the given doc or the document already present in the cache if it's newer
      • putIfAbsent

        @NotNull
        public @NotNull NodeDocument putIfAbsent​(@NotNull
                                                 @NotNull NodeDocument doc)
        Puts document into cache iff no entry with the given key is cached already. This operation is atomic.
        Parameters:
        doc - the document to add to the cache.
        Returns:
        either the given doc or the document already present in the cache.
      • replaceCachedDocument

        public void replaceCachedDocument​(@NotNull
                                          @NotNull NodeDocument oldDoc,
                                          @NotNull
                                          @NotNull NodeDocument newDoc)
        Replaces the cached value if the old document is currently present in the cache. If the oldDoc is not cached, nothing will happen. If oldDoc does not match the document currently in the cache, then the cached document is invalidated.
        Parameters:
        oldDoc - the old document
        newDoc - the replacement
      • keys

        public java.lang.Iterable<CacheValue> keys()
        Returns:
        keys stored in cache
      • values

        public java.lang.Iterable<NodeDocument> values()
        Returns:
        values stored in cache
      • getCacheStats

        public java.lang.Iterable<CacheStats> getCacheStats()
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • registerTracker

        public CacheChangesTracker registerTracker​(java.lang.String fromKey,
                                                   java.lang.String toKey)
        Registers a new CacheChangesTracker that records all puts and invalidations related to children of the given parent.
        Parameters:
        fromKey - only keys larger than this key will be tracked
        toKey - only keys smaller than this key will be tracked
        Returns:
        new tracker
      • registerTracker

        public CacheChangesTracker registerTracker​(java.util.Set<java.lang.String> keys)
        Registers a new CacheChangesTracker that records all puts and invalidations related to the given documents
        Parameters:
        keys - these documents will be tracked
        Returns:
        new tracker
      • putNonConflictingDocs

        public void putNonConflictingDocs​(CacheChangesTracker tracker,
                                          java.lang.Iterable<NodeDocument> docs)
        Updates the cache with all the documents that: (1) currently have their older versions in the cache or (2) have been neither put nor invalidated during the tracker lifetime. We can't cache documents that has been invalidated during the tracker lifetime, as it's possible that the invalidated version was newer than the one passed in the docs parameter. If the document has been added during the tracker lifetime, but it is not present in the cache anymore, it means it may have been evicted, so we can't re-add it for the same reason as above.
        Parameters:
        tracker - used to decide whether the docs should be put into cache
        docs - to put into cache
      • putInternal

        protected final void putInternal​(@NotNull
                                         @NotNull NodeDocument doc)
        Puts a document into the cache without acquiring a lock. All trackers will be updated.
        Parameters:
        doc - the document to put into the cache.
      • putInternal

        protected final void putInternal​(@NotNull
                                         @NotNull NodeDocument doc,
                                         @Nullable
                                         @Nullable CacheChangesTracker trackerToSkip)
        Puts a document into the cache without acquiring a lock. All trackers will be updated, apart from the trackerToSkip.
        Parameters:
        doc - the document to put into the cache.
        trackerToSkip - this tracker won't be updated. pass null to update all trackers.