Class AbstractSailConnection

  • All Implemented Interfaces:
    java.lang.AutoCloseable, SailConnection
    Direct Known Subclasses:
    AbstractNotifyingSailConnection

    public abstract class AbstractSailConnection
    extends java.lang.Object
    implements SailConnection
    Abstract Class offering base functionality for SailConnection implementations.
    • Field Detail

      • assertsEnabled

        private static boolean assertsEnabled
      • BLOCK_SIZE

        private static final int BLOCK_SIZE
        Size of write queue before auto flushing changes within write operation
        See Also:
        Constant Field Values
      • logger

        private static final org.slf4j.Logger logger
      • debugEnabled

        private final boolean debugEnabled
      • txnActive

        private volatile boolean txnActive
      • txnPrepared

        private volatile boolean txnPrepared
      • blockClose

        private final java.util.concurrent.atomic.LongAdder blockClose
        Lock used to give the close() method exclusive access to a connection.
        • write lock: close()
        • read lock: all other (public) methods
      • unblockClose

        private final java.util.concurrent.atomic.LongAdder unblockClose
      • isOpen

        private boolean isOpen
      • IS_OPEN

        private static final java.lang.invoke.VarHandle IS_OPEN
      • owner

        private java.lang.Thread owner
      • updateLock

        @Deprecated(since="4.1.0")
        protected final java.util.concurrent.locks.ReentrantLock updateLock
        Deprecated.
        Will be made private.
        Lock used to prevent concurrent calls to update methods like addStatement, clear, commit, etc. within a transaction.
      • connectionLock

        @Deprecated(since="4.1.0",
                    forRemoval=true)
        protected final java.util.concurrent.locks.ReentrantReadWriteLock connectionLock
        Deprecated, for removal: This API element is subject to removal in a future version.
      • iterationsOpened

        private final java.util.concurrent.atomic.LongAdder iterationsOpened
      • iterationsClosed

        private final java.util.concurrent.atomic.LongAdder iterationsClosed
      • activeIterationsDebug

        private final java.util.Map<SailBaseIteration<?,​?>,​java.lang.Throwable> activeIterationsDebug
      • removed

        private final java.util.Map<UpdateContext,​java.util.Collection<Statement>> removed
        Statements that are currently being removed, but not yet realized, by an active operation.
      • added

        private final java.util.Map<UpdateContext,​java.util.Collection<Statement>> added
        Statements that are currently being added, but not yet realized, by an active operation.
      • wildContext

        private static final BNode wildContext
        Used to indicate a removed statement from all contexts.
      • transactionIsolationLevel

        private IsolationLevel transactionIsolationLevel
      • statementsAdded

        private volatile boolean statementsAdded
      • statementsRemoved

        private volatile boolean statementsRemoved
    • Constructor Detail

      • AbstractSailConnection

        public AbstractSailConnection​(AbstractSail sailBase)
    • Method Detail

      • verifyIsActive

        protected void verifyIsActive()
                               throws SailException
        Verifies if a transaction is currently active. Throws a SailException if no transaction is active.
        Throws:
        SailException - if no transaction is active.
      • close

        public final void close()
                         throws SailException
        Description copied from interface: SailConnection
        Closes the connection. Any updates that haven't been committed yet will be rolled back. The connection can no longer be used once it is closed.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface SailConnection
        Throws:
        SailException
      • startDeadlockPreventionThread

        private java.lang.Thread startDeadlockPreventionThread()
        If the current thread is not the owner, starts a thread to handle potential deadlocks by interrupting the owner.
        Returns:
        The started deadlock prevention thread or null if the current thread is the owner.
      • evaluate

        public final CloseableIteration<? extends BindingSet,​QueryEvaluationException> evaluate​(TupleExpr tupleExpr,
                                                                                                      Dataset dataset,
                                                                                                      BindingSet bindings,
                                                                                                      boolean includeInferred)
                                                                                               throws SailException
        Description copied from interface: SailConnection
        Evaluates the supplied TupleExpr on the data contained in this Sail object, using the (optional) dataset and supplied bindings as input parameters.
        Specified by:
        evaluate in interface SailConnection
        Parameters:
        tupleExpr - The tuple expression to evaluate.
        dataset - The dataset to use for evaluating the query, null to use the Sail's default dataset.
        bindings - A set of input parameters for the query evaluation. The keys reference variable names that should be bound to the value they map to.
        includeInferred - Indicates whether inferred triples are to be considered in the query result. If false, no inferred statements are returned; if true, inferred statements are returned if available
        Returns:
        The TupleQueryResult.
        Throws:
        SailException - If the Sail object encountered an error or unexpected situation internally.
      • getStatements

        public final CloseableIteration<? extends Statement,​SailException> getStatements​(Resource subj,
                                                                                               IRI pred,
                                                                                               Value obj,
                                                                                               boolean includeInferred,
                                                                                               Resource... contexts)
                                                                                        throws SailException
        Description copied from interface: SailConnection
        Gets all statements from the specified contexts that have a specific subject, predicate and/or object. All three parameters may be null to indicate wildcards. The includeInferred parameter can be used to control which statements are fetched: all statements or only the statements that have been added explicitly.
        Specified by:
        getStatements in interface SailConnection
        Parameters:
        subj - A Resource specifying the subject, or null for a wildcard.
        pred - A URI specifying the predicate, or null for a wildcard.
        obj - A Value specifying the object, or null for a wildcard.
        includeInferred - if false, no inferred statements are returned; if true, inferred statements are returned if available
        contexts - The context(s) to get the data from. Note that this parameter is a vararg and as such is optional. If no contexts are specified the method operates on the entire repository. A null value can be used to match context-less statements.
        Returns:
        The statements matching the specified pattern.
        Throws:
        SailException - If the Sail object encountered an error or unexpected situation internally.
      • hasStatement

        public final boolean hasStatement​(Resource subj,
                                          IRI pred,
                                          Value obj,
                                          boolean includeInferred,
                                          Resource... contexts)
                                   throws SailException
        Description copied from interface: SailConnection
        Determines if the store contains any statements from the specified contexts that have a specific subject, predicate and/or object. All three parameters may be null to indicate wildcards. The includeInferred parameter can be used to control which statements are checked: all statements or only the statements that have been added explicitly.
        Specified by:
        hasStatement in interface SailConnection
        Parameters:
        subj - A Resource specifying the subject, or null for a wildcard.
        pred - An IRI specifying the predicate, or null for a wildcard.
        obj - A Value specifying the object, or null for a wildcard.
        includeInferred - if false, no inferred statements are returned; if true, inferred statements are returned if available
        contexts - The context(s) to get the data from. Note that this parameter is a vararg and as such is optional. If no contexts are specified the method operates on the entire repository. A null value can be used to match context-less statements.
        Returns:
        true iff the store contains any statements matching the supplied criteria, false otherwise.
        Throws:
        SailException - If the Sail object encountered an error or unexpected situation internally.
      • hasStatementInternal

        protected boolean hasStatementInternal​(Resource subj,
                                               IRI pred,
                                               Value obj,
                                               boolean includeInferred,
                                               Resource[] contexts)
      • size

        public final long size​(Resource... contexts)
                        throws SailException
        Description copied from interface: SailConnection
        Returns the number of (explicit) statements in the store, or in specific contexts.
        Specified by:
        size in interface SailConnection
        Parameters:
        contexts - The context(s) to determine the size of. Note that this parameter is a vararg and as such is optional. If no contexts are specified the method operates on the entire repository. A null value can be used to match context-less statements.
        Returns:
        The number of explicit statements in this store, or in the specified context(s).
        Throws:
        SailException
      • transactionActive

        protected final boolean transactionActive()
      • autoStartTransaction

        @Deprecated(since="2.7.0")
        protected void autoStartTransaction()
                                     throws SailException
        Deprecated.
        Use verifyIsActive() instead. We should not automatically start a transaction at the sail level. Instead, an exception should be thrown when an update is executed without first starting a transaction.
        IMPORTANT Since Sesame 2.7.0. this method no longer automatically starts a transaction, but instead verifies if a transaction is active and if not throws an exception. The method is left in for transitional purposes only. Sail implementors are advised that by contract, any update operation on the Sail should check if a transaction has been started via SailConnection.isActive() and throw a SailException if not. Implementors can use verifyIsActive() as a convenience method for this check.
        Throws:
        SailException - if no transaction is active.
      • flush

        public void flush()
                   throws SailException
        Description copied from interface: SailConnection
        Flushes any pending updates and notify changes to listeners as appropriate. This is an optional call; calling or not calling this method should have no effect on the outcome of other calls. This method exists to give the caller more control over the efficiency when calling SailConnection.prepare(). This method may be called multiple times within the same transaction.
        Specified by:
        flush in interface SailConnection
        Throws:
        SailException - If the updates could not be processed, for example because no transaction is active.
      • rollback

        public final void rollback()
                            throws SailException
        Description copied from interface: SailConnection
        Rolls back the transaction, discarding any uncommitted changes that have been made in this SailConnection.
        Specified by:
        rollback in interface SailConnection
        Throws:
        UnknownSailTransactionStateException - If the transaction state can not be determined (this can happen for instance when communication between client and server fails or times-out). It does not indicate a problem with the integrity of the store.
        SailException - If the SailConnection could not be rolled back.
      • addStatement

        public final void addStatement​(Resource subj,
                                       IRI pred,
                                       Value obj,
                                       Resource... contexts)
                                throws SailException
        Description copied from interface: SailConnection
        Adds a statement to the store.
        Specified by:
        addStatement in interface SailConnection
        Parameters:
        subj - The subject of the statement to add.
        pred - The predicate of the statement to add.
        obj - The object of the statement to add.
        contexts - The context(s) to add the statement to. Note that this parameter is a vararg and as such is optional. If no contexts are specified, a context-less statement will be added.
        Throws:
        SailException - If the statement could not be added, for example because no transaction is active.
      • removeStatements

        public final void removeStatements​(Resource subj,
                                           IRI pred,
                                           Value obj,
                                           Resource... contexts)
                                    throws SailException
        Description copied from interface: SailConnection
        Removes all statements matching the specified subject, predicate and object from the repository. All three parameters may be null to indicate wildcards.
        Specified by:
        removeStatements in interface SailConnection
        Parameters:
        subj - The subject of the statement that should be removed, or null to indicate a wildcard.
        pred - The predicate of the statement that should be removed, or null to indicate a wildcard.
        obj - The object of the statement that should be removed , or null to indicate a wildcard. *
        contexts - The context(s) from which to remove the statement. Note that this parameter is a vararg and as such is optional. If no contexts are specified the method operates on the entire repository. A null value can be used to match context-less statements.
        Throws:
        SailException - If the statement could not be removed, for example because no transaction is active.
      • addStatement

        public void addStatement​(UpdateContext op,
                                 Resource subj,
                                 IRI pred,
                                 Value obj,
                                 Resource... contexts)
                          throws SailException
        The default implementation buffers added statements until the update operation is complete.
        Specified by:
        addStatement in interface SailConnection
        Parameters:
        op - operation properties of the UpdateExpr operation producing these statements.
        subj - The subject of the statement to add.
        pred - The predicate of the statement to add.
        obj - The object of the statement to add.
        contexts - The context(s) to add the statement to. Note that this parameter is a vararg and as such is optional. If no contexts are specified, a context-less statement will be added.
        Throws:
        SailException - If the statement could not be added, for example because no transaction is active.
      • removeStatement

        public void removeStatement​(UpdateContext op,
                                    Resource subj,
                                    IRI pred,
                                    Value obj,
                                    Resource... contexts)
                             throws SailException
        The default implementation buffers removed statements until the update operation is complete.
        Specified by:
        removeStatement in interface SailConnection
        Parameters:
        op - operation properties of the UpdateExpr operation removing these statements.
        subj - The subject of the statement that should be removed.
        pred - The predicate of the statement that should be removed.
        obj - The object of the statement that should be removed.
        contexts - The context(s) from which to remove the statement. Note that this parameter is a vararg and as such is optional. If no contexts are specified the method operates on the entire repository. A null value can be used to match context-less statements.
        Throws:
        SailException - If the statement could not be removed, for example because no transaction is active.
      • clear

        public final void clear​(Resource... contexts)
                         throws SailException
        Description copied from interface: SailConnection
        Removes all statements from the specified/all contexts. If no contexts are specified the method operates on the entire repository.
        Specified by:
        clear in interface SailConnection
        Parameters:
        contexts - The context(s) from which to remove the statements. Note that this parameter is a vararg and as such is optional. If no contexts are specified the method operates on the entire repository. A null value can be used to match context-less statements.
        Throws:
        SailException - If the statements could not be removed.
      • getNamespace

        public final java.lang.String getNamespace​(java.lang.String prefix)
                                            throws SailException
        Description copied from interface: SailConnection
        Gets the namespace that is associated with the specified prefix, if any.
        Specified by:
        getNamespace in interface SailConnection
        Parameters:
        prefix - A namespace prefix, or an empty string in case of the default namespace.
        Returns:
        The namespace name that is associated with the specified prefix, or null if there is no such namespace.
        Throws:
        SailException - If the Sail object encountered an error or unexpected situation internally.
      • setNamespace

        public final void setNamespace​(java.lang.String prefix,
                                       java.lang.String name)
                                throws SailException
        Description copied from interface: SailConnection
        Sets the prefix for a namespace.
        Specified by:
        setNamespace in interface SailConnection
        Parameters:
        prefix - The new prefix, or an empty string in case of the default namespace.
        name - The namespace name that the prefix maps to.
        Throws:
        SailException - If the Sail object encountered an error or unexpected situation internally.
      • removeNamespace

        public final void removeNamespace​(java.lang.String prefix)
                                   throws SailException
        Description copied from interface: SailConnection
        Removes a namespace declaration by removing the association between a prefix and a namespace name.
        Specified by:
        removeNamespace in interface SailConnection
        Parameters:
        prefix - The namespace prefix, or an empty string in case of the default namespace.
        Throws:
        SailException - If the Sail object encountered an error or unexpected situation internally.
      • pendingRemovals

        public boolean pendingRemovals()
        Description copied from interface: SailConnection
        Indicates if the Sail has any statement removal operations pending (not yet flushed) for the current transaction.
        Specified by:
        pendingRemovals in interface SailConnection
        Returns:
        true if any statement removal operations have not yet been flushed, false otherwise.
        See Also:
        SailConnection.flush()
      • pendingAdds

        protected boolean pendingAdds()
      • setStatementsAdded

        protected void setStatementsAdded()
      • setStatementsRemoved

        protected void setStatementsRemoved()
      • getSharedConnectionLock

        @Deprecated(forRemoval=true)
        protected Lock getSharedConnectionLock()
                                        throws SailException
        Deprecated, for removal: This API element is subject to removal in a future version.
        Throws:
        SailException
      • getExclusiveConnectionLock

        @Deprecated(forRemoval=true)
        protected Lock getExclusiveConnectionLock()
                                           throws SailException
        Deprecated, for removal: This API element is subject to removal in a future version.
        Throws:
        SailException
      • getTransactionLock

        @Deprecated(forRemoval=true)
        protected Lock getTransactionLock()
                                   throws SailException
        Deprecated, for removal: This API element is subject to removal in a future version.
        Throws:
        SailException
      • getOwner

        @InternalUseOnly
        public java.lang.Thread getOwner()
        This is for internal use only. It returns the thread that opened this connection.
        Returns:
        the thread that opened this connection.
      • registerIteration

        protected <T,​E extends java.lang.Exception> CloseableIteration<T,​E> registerIteration​(CloseableIteration<T,​E> iter)
        Registers an iteration as active by wrapping it in a SailBaseIteration object and adding it to the list of active iterations.
      • getNamespaceInternal

        protected abstract java.lang.String getNamespaceInternal​(java.lang.String prefix)
                                                          throws SailException
        Throws:
        SailException
      • setNamespaceInternal

        protected abstract void setNamespaceInternal​(java.lang.String prefix,
                                                     java.lang.String name)
                                              throws SailException
        Throws:
        SailException
      • removeNamespaceInternal

        protected abstract void removeNamespaceInternal​(java.lang.String prefix)
                                                 throws SailException
        Throws:
        SailException
      • isActiveOperation

        protected boolean isActiveOperation()
      • flushPendingUpdates

        private void flushPendingUpdates()
                                  throws SailException
        If there are no open operations.
        Throws:
        SailException