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);
}
}
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();
}
}
use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.
the class MemoryDocumentStore method internalCreateOrUpdate.
@CheckForNull
private <T extends Document> T internalCreateOrUpdate(Collection<T> collection, UpdateOp update, boolean checkConditions) {
ConcurrentSkipListMap<String, T> map = getMap(collection);
T oldDoc;
Lock lock = rwLock.writeLock();
lock.lock();
try {
// get the node if it's there
oldDoc = map.get(update.getId());
T doc = collection.newDocument(this);
if (oldDoc == null) {
if (!update.isNew()) {
throw new DocumentStoreException("Document does not exist: " + update.getId());
}
} else {
oldDoc.deepCopy(doc);
}
if (checkConditions && !checkConditions(doc, update.getConditions())) {
return null;
}
// update the document
UpdateUtils.applyChanges(doc, update);
doc.seal();
map.put(update.getId(), doc);
return oldDoc;
} finally {
lock.unlock();
}
}
use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.
the class AccessControlImporter method getACL.
@CheckForNull
private JackrabbitAccessControlList getACL(Tree tree) throws RepositoryException {
String nodeName = tree.getName();
JackrabbitAccessControlList acList = null;
if (!tree.isRoot()) {
Tree parent = tree.getParent();
if (AccessControlConstants.REP_POLICY.equals(nodeName) && ntMgr.isNodeType(tree, AccessControlConstants.NT_REP_ACL)) {
String path = parent.getPath();
acList = getACL(path);
} else if (AccessControlConstants.REP_REPO_POLICY.equals(nodeName) && ntMgr.isNodeType(tree, AccessControlConstants.NT_REP_ACL) && parent.isRoot()) {
acList = getACL((String) null);
}
}
if (acList != null) {
// clear all existing entries
for (AccessControlEntry ace : acList.getAccessControlEntries()) {
acList.removeAccessControlEntry(ace);
}
}
return acList;
}
use of javax.annotation.CheckForNull in project jackrabbit-oak by apache.
the class LoginModuleImpl method getUserAuthentication.
@CheckForNull
private Authentication getUserAuthentication(@Nullable String loginName) {
SecurityProvider securityProvider = getSecurityProvider();
Root root = getRoot();
if (securityProvider != null && root != null) {
UserConfiguration uc = securityProvider.getConfiguration(UserConfiguration.class);
UserAuthenticationFactory factory = uc.getParameters().getConfigValue(UserConstants.PARAM_USER_AUTHENTICATION_FACTORY, null, UserAuthenticationFactory.class);
if (factory != null) {
return factory.getAuthentication(uc, root, loginName);
} else {
log.error("No user authentication factory configured in user configuration.");
}
}
return null;
}
Aggregations