use of com.mongodb.MongoException in project jackrabbit-oak by apache.
the class MongoDocumentStore method create.
@Override
public <T extends Document> boolean create(Collection<T> collection, List<UpdateOp> updateOps) {
log("create", updateOps);
List<T> docs = new ArrayList<T>();
DBObject[] inserts = new DBObject[updateOps.size()];
List<String> ids = Lists.newArrayListWithCapacity(updateOps.size());
for (int i = 0; i < updateOps.size(); i++) {
inserts[i] = new BasicDBObject();
UpdateOp update = updateOps.get(i);
inserts[i].put(Document.ID, update.getId());
UpdateUtils.assertUnconditional(update);
T target = collection.newDocument(this);
UpdateUtils.applyChanges(target, update);
docs.add(target);
ids.add(updateOps.get(i).getId());
for (Entry<Key, Operation> entry : update.getChanges().entrySet()) {
Key k = entry.getKey();
Operation op = entry.getValue();
switch(op.type) {
case SET:
case MAX:
case INCREMENT:
{
inserts[i].put(k.toString(), op.value);
break;
}
case SET_MAP_ENTRY:
{
Revision r = k.getRevision();
if (r == null) {
throw new IllegalStateException("SET_MAP_ENTRY must not have null revision");
}
DBObject value = (DBObject) inserts[i].get(k.getName());
if (value == null) {
value = new RevisionEntry(r, op.value);
inserts[i].put(k.getName(), value);
} else if (value.keySet().size() == 1) {
String key = value.keySet().iterator().next();
Object val = value.get(key);
value = new BasicDBObject(key, val);
value.put(r.toString(), op.value);
inserts[i].put(k.getName(), value);
} else {
value.put(r.toString(), op.value);
}
break;
}
case REMOVE:
case REMOVE_MAP_ENTRY:
// nothing to do for new entries
break;
}
}
if (!inserts[i].containsField(Document.MOD_COUNT)) {
inserts[i].put(Document.MOD_COUNT, 1L);
target.put(Document.MOD_COUNT, 1L);
}
}
DBCollection dbCollection = getDBCollection(collection);
final Stopwatch watch = startWatch();
boolean insertSuccess = false;
try {
try {
dbCollection.insert(inserts);
if (collection == Collection.NODES) {
for (T doc : docs) {
nodesCache.putIfAbsent((NodeDocument) doc);
updateLocalChanges((NodeDocument) doc);
}
}
insertSuccess = true;
return true;
} catch (MongoException e) {
return false;
}
} finally {
stats.doneCreate(watch.elapsed(TimeUnit.NANOSECONDS), collection, ids, insertSuccess);
}
}
use of com.mongodb.MongoException in project jackrabbit-oak by apache.
the class MongoDocumentStore method update.
@Override
public <T extends Document> void update(Collection<T> collection, List<String> keys, UpdateOp updateOp) {
log("update", keys, updateOp);
UpdateUtils.assertUnconditional(updateOp);
DBCollection dbCollection = getDBCollection(collection);
QueryBuilder query = QueryBuilder.start(Document.ID).in(keys);
// make sure we don't modify the original updateOp
updateOp = updateOp.copy();
DBObject update = createUpdate(updateOp, false);
final Stopwatch watch = startWatch();
try {
Map<String, NodeDocument> cachedDocs = Collections.emptyMap();
if (collection == Collection.NODES) {
cachedDocs = Maps.newHashMap();
for (String key : keys) {
cachedDocs.put(key, nodesCache.getIfPresent(key));
}
}
try {
dbCollection.update(query.get(), update, false, true);
if (collection == Collection.NODES) {
Map<String, ModificationStamp> modCounts = getModStamps(filterValues(cachedDocs, notNull()).keySet());
// update cache
for (Entry<String, NodeDocument> entry : cachedDocs.entrySet()) {
// the cachedDocs is not empty, so the collection = NODES
Lock lock = nodeLocks.acquire(entry.getKey());
try {
ModificationStamp postUpdateModStamp = modCounts.get(entry.getKey());
if (postUpdateModStamp != null && entry.getValue() != null && entry.getValue() != NodeDocument.NULL && Long.valueOf(postUpdateModStamp.modCount - 1).equals(entry.getValue().getModCount())) {
// post update modCount is one higher than
// what we currently see in the cache. we can
// replace the cached document
NodeDocument newDoc = applyChanges(Collection.NODES, entry.getValue(), updateOp.shallowCopy(entry.getKey()));
nodesCache.replaceCachedDocument(entry.getValue(), newDoc);
} else {
// make sure concurrently loaded document is
// invalidated
nodesCache.invalidate(entry.getKey());
}
} finally {
lock.unlock();
}
}
}
} catch (MongoException e) {
throw handleException(e, collection, keys);
}
} finally {
stats.doneUpdate(watch.elapsed(TimeUnit.NANOSECONDS), collection, keys.size());
}
}
Aggregations