Search in sources :

Example 56 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class SecondaryStoreCache method getDocumentNodeState.

@CheckForNull
@Override
public AbstractDocumentNodeState getDocumentNodeState(String path, RevisionVector rootRevision, RevisionVector lastRev) {
    //TODO We might need skip the calls if they occur due to SecondaryStoreObserver
    //doing the diff or in the startup when we try to sync the state
    PathFilter.Result result = pathFilter.filter(path);
    if (result != PathFilter.Result.INCLUDE) {
        unknownPaths.mark();
        return null;
    }
    if (!DelegatingDocumentNodeState.hasMetaProps(store.getRoot())) {
        return null;
    }
    AbstractDocumentNodeState currentRoot = DelegatingDocumentNodeState.wrap(store.getRoot(), differ);
    //not have the matching result
    if (lastRev.compareTo(currentRoot.getLastRevision()) > 0) {
        return null;
    }
    AbstractDocumentNodeState nodeState = findByMatchingLastRev(currentRoot, path, lastRev);
    if (nodeState != null) {
        headRevMatched.mark();
        return nodeState;
    }
    AbstractDocumentNodeState matchingRoot = findMatchingRoot(rootRevision);
    if (matchingRoot != null) {
        NodeState state = NodeStateUtils.getNode(matchingRoot, path);
        if (state.exists()) {
            AbstractDocumentNodeState docState = asDocState(state);
            prevRevMatched.mark();
            return docState;
        }
    }
    knownMissed.mark();
    return null;
}
Also used : PathFilter(org.apache.jackrabbit.oak.plugins.index.PathFilter) NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) AbstractDocumentNodeState(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentNodeState) AbstractDocumentNodeState(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentNodeState) CheckForNull(javax.annotation.CheckForNull)

Example 57 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class SecondaryStoreCache method findByMatchingLastRev.

@CheckForNull
private AbstractDocumentNodeState findByMatchingLastRev(AbstractDocumentNodeState root, String path, RevisionVector lastRev) {
    NodeState state = root;
    for (String name : PathUtils.elements(path)) {
        state = state.getChildNode(name);
        if (!state.exists()) {
            return null;
        }
        //requested lastRev is > current node lastRev then no need to check further
        if (lastRev.compareTo(asDocState(state).getLastRevision()) > 0) {
            return null;
        }
    }
    AbstractDocumentNodeState docState = asDocState(state);
    if (lastRev.equals(docState.getLastRevision())) {
        headRevMatched.mark();
        return docState;
    }
    return null;
}
Also used : NodeState(org.apache.jackrabbit.oak.spi.state.NodeState) AbstractDocumentNodeState(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentNodeState) AbstractDocumentNodeState(org.apache.jackrabbit.oak.plugins.document.AbstractDocumentNodeState) CheckForNull(javax.annotation.CheckForNull)

Example 58 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class IndexUtils method getAsyncLaneName.

@CheckForNull
public static String getAsyncLaneName(NodeState idxState, String indexPath) {
    PropertyState async = idxState.getProperty(IndexConstants.ASYNC_PROPERTY_NAME);
    if (async != null) {
        Set<String> asyncNames = Sets.newHashSet(async.getValue(Type.STRINGS));
        asyncNames.remove(IndexConstants.INDEXING_MODE_NRT);
        asyncNames.remove(IndexConstants.INDEXING_MODE_SYNC);
        checkArgument(!asyncNames.isEmpty(), "No valid async name found for " + "index [%s], definition %s", indexPath, idxState);
        return Iterables.getOnlyElement(asyncNames);
    }
    return null;
}
Also used : PropertyState(org.apache.jackrabbit.oak.api.PropertyState) CheckForNull(javax.annotation.CheckForNull)

Example 59 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class MongoDocumentStore method findAndModify.

@SuppressWarnings("unchecked")
@CheckForNull
private <T extends Document> T findAndModify(Collection<T> collection, UpdateOp updateOp, boolean upsert, boolean checkConditions) {
    DBCollection dbCollection = getDBCollection(collection);
    // make sure we don't modify the original updateOp
    updateOp = updateOp.copy();
    DBObject update = createUpdate(updateOp, false);
    Lock lock = null;
    if (collection == Collection.NODES) {
        lock = nodeLocks.acquire(updateOp.getId());
    }
    final Stopwatch watch = startWatch();
    boolean newEntry = false;
    try {
        // get modCount of cached document
        Long modCount = null;
        T cachedDoc = null;
        if (collection == Collection.NODES) {
            cachedDoc = (T) nodesCache.getIfPresent(updateOp.getId());
            if (cachedDoc != null) {
                modCount = cachedDoc.getModCount();
            }
        }
        // if we have a matching modCount
        if (modCount != null) {
            QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions());
            query.and(Document.MOD_COUNT).is(modCount);
            WriteResult result = dbCollection.update(query.get(), update);
            if (result.getN() > 0) {
                // success, update cached document
                if (collection == Collection.NODES) {
                    NodeDocument newDoc = (NodeDocument) applyChanges(collection, cachedDoc, updateOp);
                    nodesCache.put(newDoc);
                }
                // return previously cached document
                return cachedDoc;
            }
        }
        // conditional update failed or not possible
        // perform operation and get complete document
        QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions());
        DBObject oldNode = dbCollection.findAndModify(query.get(), null, null, /*sort*/
        false, /*remove*/
        update, false, /*returnNew*/
        upsert);
        if (oldNode == null) {
            newEntry = true;
        }
        if (checkConditions && oldNode == null) {
            return null;
        }
        T oldDoc = convertFromDBObject(collection, oldNode);
        if (oldDoc != null) {
            if (collection == Collection.NODES) {
                NodeDocument newDoc = (NodeDocument) applyChanges(collection, oldDoc, updateOp);
                nodesCache.put(newDoc);
                updateLocalChanges(newDoc);
            }
            oldDoc.seal();
        } else if (upsert) {
            if (collection == Collection.NODES) {
                NodeDocument doc = (NodeDocument) collection.newDocument(this);
                UpdateUtils.applyChanges(doc, updateOp);
                nodesCache.putIfAbsent(doc);
                updateLocalChanges(doc);
            }
        } else {
        // updateOp without conditions and not an upsert
        // this means the document does not exist
        }
        return oldDoc;
    } catch (Exception e) {
        throw handleException(e, collection, updateOp.getId());
    } finally {
        if (lock != null) {
            lock.unlock();
        }
        stats.doneFindAndModify(watch.elapsed(TimeUnit.NANOSECONDS), collection, updateOp.getId(), newEntry, true, 0);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) BulkWriteResult(com.mongodb.BulkWriteResult) Stopwatch(com.google.common.base.Stopwatch) QueryBuilder(com.mongodb.QueryBuilder) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) BulkWriteException(com.mongodb.BulkWriteException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Lock(java.util.concurrent.locks.Lock) CheckForNull(javax.annotation.CheckForNull)

Example 60 with CheckForNull

use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.

the class MongoDocumentStore method findUncachedWithRetry.

/**
     * Finds a document and performs a number of retries if the read fails with
     * an exception.
     *
     * @param collection the collection to read from.
     * @param key the key of the document to find.
     * @param docReadPref the read preference.
     * @param retries the number of retries. Must not be negative.
     * @param <T> the document type of the given collection.
     * @return the document or {@code null} if the document doesn't exist.
     */
@CheckForNull
private <T extends Document> T findUncachedWithRetry(Collection<T> collection, String key, DocumentReadPreference docReadPref, int retries) {
    checkArgument(retries >= 0, "retries must not be negative");
    if (key.equals("0:/")) {
        LOG.trace("root node");
    }
    int numAttempts = retries + 1;
    MongoException ex = null;
    for (int i = 0; i < numAttempts; i++) {
        if (i > 0) {
            LOG.warn("Retrying read of " + key);
        }
        try {
            return findUncached(collection, key, docReadPref);
        } catch (MongoException e) {
            ex = e;
        }
    }
    if (ex != null) {
        throw ex;
    } else {
        // impossible to get here
        throw new IllegalStateException();
    }
}
Also used : MongoException(com.mongodb.MongoException) CheckForNull(javax.annotation.CheckForNull)

Aggregations

CheckForNull (javax.annotation.CheckForNull)158 IOException (java.io.IOException)21 Tree (org.apache.jackrabbit.oak.api.Tree)16 PropertyState (org.apache.jackrabbit.oak.api.PropertyState)12 ArrayList (java.util.ArrayList)9 NodeState (org.apache.jackrabbit.oak.spi.state.NodeState)9 Stopwatch (com.google.common.base.Stopwatch)8 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)8 Date (java.util.Date)7 SnapshotDto (org.sonar.db.component.SnapshotDto)7 Period (org.sonar.server.computation.task.projectanalysis.period.Period)7 File (java.io.File)6 SQLException (java.sql.SQLException)6 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)6 ExecutionException (java.util.concurrent.ExecutionException)5 ValidationModel (org.apache.sling.validation.model.ValidationModel)5 InputStream (java.io.InputStream)4 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)4 Root (org.apache.jackrabbit.oak.api.Root)4 Utils.resolveCommitRevision (org.apache.jackrabbit.oak.plugins.document.util.Utils.resolveCommitRevision)4