Class UriCache


  • public class UriCache
    extends Object
    Component hiding details of how QNames are stored in QUri. Following prefixes are used for its methods: * `get` returns URI or ID for ID or URI, may return null, accesses DB if not found (multi-node safety); * `search` like `get` but returns UNKNOWN_ID instead of null, used for query predicates, accesses DB if not found (multi-node safety); * `resolve` returns URI/ID for ID/URI or throws exception if not found, this is for situations where the entry for URI is expected to exist already, accesses DB if not found in cache (multi-node safety); * finally, processCacheableUri(Object) accesses the database if the URI is not found in the cache in order to write it there. URIs are stored either as is when provided as a String or using QNameUtil.qNameToUri(QName) when provided as a QName. This component does not know anything about relations (represented as QName-s), but these are systematically normalized before they get here (if not, it's surely a bug). [NOTE] URI is added in the database in its own separate transaction. It is tempting to add cached URI as part of the existing transaction, but when the provided transaction is rolled back, the cache still thinks the URI row is already there. This could be avoided if the runtime maps were updated *only* after the row was successfully read from the DB in other operations - which beats the purposes of those fast operations. Instead, we risk adding the row that is not used, it is no harm; it will likely be used later.
    • Field Detail

      • UNKNOWN_ID

        public static final int UNKNOWN_ID
        Unknown id placeholder, not actually in DB but returned when URI is not in the cache and returning `null` or throwing exception would not make sense. Typical case is using it for query predicate when searching for unknown URI should result in a condition comparing URI ID attribute (e.g. relationId) to id that will not be found. This is completely transient and can be changed if the need arises.
        See Also:
        Constant Field Values
    • Constructor Detail

      • UriCache

        public UriCache()
    • Method Detail

      • initialize

        public void initialize​(Supplier<JdbcSession> jdbcSessionSupplier)
        Initializes the URI cache. Provided JdbcSession supplier will be used for later writes as well.
      • getId

        @Nullable
        public @Nullable Integer getId​(@NotNull
                                       @NotNull QName qName)
        Returns ID for QName, possibly null - does not work with underlying database.
      • resolveUriToId

        @NotNull
        public @NotNull Integer resolveUriToId​(@NotNull
                                               @NotNull QName qName)
        Returns ID for QName or throws exception - does not work with underlying database.
      • getId

        @Nullable
        public @Nullable Integer getId​(@NotNull
                                       @NotNull String uri)
        Returns ID for string, possibly null - does not work with underlying database.
      • searchId

        @NotNull
        public @NotNull Integer searchId​(@NotNull
                                         @NotNull Object uri)
        Returns ID for provided URI value of `QName/String/Object#toString`.
      • searchId

        @NotNull
        public @NotNull Integer searchId​(@NotNull
                                         @NotNull String uri)
        Returns ID for string or UNKNOWN_ID - does not work with underlying database.
      • resolveUriToId

        @NotNull
        public @NotNull Integer resolveUriToId​(@NotNull
                                               @NotNull String uri)
        Returns ID for QName or throws exception - does not work with underlying database.
      • getUri

        public String getUri​(Integer id)
        Returns URI string for ID or null - does not work with underlying database.
      • resolveToUri

        @NotNull
        public @NotNull String resolveToUri​(Integer id)
        Returns URI string for ID or throws exception - does not work with underlying database.
      • processCacheableUri

        @Nullable
        public @Nullable Integer processCacheableUri​(@Nullable
                                                     @Nullable Object uri)
        Returns ID for URI creating new cache row in DB as needed. Returns null for null URI parameter.