use of com.mongodb.WriteResult in project jetty.project by eclipse.
the class MongoSessionDataStore method delete.
/**
* @see org.eclipse.jetty.server.session.SessionDataStore#delete(String)
*/
@Override
public boolean delete(String id) throws Exception {
if (LOG.isDebugEnabled())
LOG.debug("Remove:session {} for context ", id, _context);
/*
* Check if the session exists and if it does remove the context
* associated with this session
*/
BasicDBObject mongoKey = new BasicDBObject(__ID, id);
//DBObject sessionDocument = _dbSessions.findOne(mongoKey,_version_1);
DBObject sessionDocument = _dbSessions.findOne(new BasicDBObject(__ID, id));
if (sessionDocument != null) {
DBObject c = (DBObject) getNestedValue(sessionDocument, __CONTEXT);
if (c == null) {
//delete whole doc
_dbSessions.remove(mongoKey, WriteConcern.SAFE);
return false;
}
Set<String> contexts = c.keySet();
if (contexts.isEmpty()) {
//delete whole doc
_dbSessions.remove(mongoKey, WriteConcern.SAFE);
return false;
}
if (contexts.size() == 1 && contexts.iterator().next().equals(getCanonicalContextId())) {
//delete whole doc
_dbSessions.remove(new BasicDBObject(__ID, id), WriteConcern.SAFE);
return true;
}
//just remove entry for my context
BasicDBObject remove = new BasicDBObject();
BasicDBObject unsets = new BasicDBObject();
unsets.put(getContextField(), 1);
remove.put("$unset", unsets);
WriteResult result = _dbSessions.update(mongoKey, remove, false, false, WriteConcern.SAFE);
return true;
} else {
return false;
}
}
use of com.mongodb.WriteResult in project jetty.project by eclipse.
the class MongoTest method main.
public static void main(String... args) throws Exception {
Mongo m = new Mongo("127.0.0.1", 27017);
DB db = m.getDB("mydb");
Set<String> colls = db.getCollectionNames();
System.err.println("Colls=" + colls);
DBCollection coll = db.getCollection("testCollection");
BasicDBObject key = new BasicDBObject("id", "1234");
BasicDBObject sets = new BasicDBObject("name", "value");
BasicDBObject upsert = new BasicDBObject("$set", sets);
WriteResult result = coll.update(key, upsert, true, false);
System.err.println(result.getLastError());
while (coll.count() > 0) {
DBObject docZ = coll.findOne();
System.err.println("removing " + docZ);
if (docZ != null)
coll.remove(docZ);
}
}
use of com.mongodb.WriteResult in project morphia by mongodb.
the class DatastoreImpl method save.
protected <T> Key<T> save(final DBCollection dbColl, final T entity, final InsertOptions options) {
if (entity == null) {
throw new UpdateException("Can not persist a null entity");
}
final MappedClass mc = mapper.getMappedClass(entity);
if (mc.getAnnotation(NotSaved.class) != null) {
throw new MappingException(format("Entity type: %s is marked as NotSaved which means you should not try to save it!", mc.getClazz().getName()));
}
// involvedObjects is used not only as a cache but also as a list of what needs to be called for life-cycle methods at the end.
final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
final DBObject document = entityToDBObj(entity, involvedObjects);
// try to do an update if there is a @Version field
final Object idValue = document.get(Mapper.ID_KEY);
WriteResult wr = tryVersionedUpdate(dbColl, entity, document, idValue, enforceWriteConcern(options, entity.getClass()), mc);
if (wr == null) {
saveDocument(dbColl, document, options);
}
return postSaveOperations(singletonList(entity), involvedObjects, dbColl).get(0);
}
use of com.mongodb.WriteResult in project morphia by mongodb.
the class DatastoreImpl method merge.
@Override
@SuppressWarnings("unchecked")
public <T> Key<T> merge(final T entity, final WriteConcern wc) {
T unwrapped = entity;
final LinkedHashMap<Object, DBObject> involvedObjects = new LinkedHashMap<Object, DBObject>();
final DBObject dbObj = mapper.toDBObject(unwrapped, involvedObjects);
final Key<T> key = mapper.getKey(unwrapped);
unwrapped = ProxyHelper.unwrap(unwrapped);
final Object id = mapper.getId(unwrapped);
if (id == null) {
throw new MappingException("Could not get id for " + unwrapped.getClass().getName());
}
// remove (immutable) _id field for update.
final Object idValue = dbObj.get(Mapper.ID_KEY);
dbObj.removeField(Mapper.ID_KEY);
WriteResult wr;
final MappedClass mc = mapper.getMappedClass(unwrapped);
final DBCollection dbColl = getCollection(unwrapped);
// try to do an update if there is a @Version field
wr = tryVersionedUpdate(dbColl, unwrapped, dbObj, idValue, new InsertOptions().writeConcern(wc), mc);
if (wr == null) {
final Query<T> query = (Query<T>) createQuery(unwrapped.getClass()).filter(Mapper.ID_KEY, id);
wr = update(query, new BasicDBObject("$set", dbObj), false, false, wc).getWriteResult();
}
final UpdateResults res = new UpdateResults(wr);
if (res.getUpdatedCount() == 0) {
throw new UpdateException("Nothing updated");
}
dbObj.put(Mapper.ID_KEY, idValue);
postSaveOperations(Collections.<Object>singletonList(entity), involvedObjects, dbColl, false);
return key;
}
use of com.mongodb.WriteResult in project morphia by mongodb.
the class DatastoreImpl method tryVersionedUpdate.
private <T> WriteResult tryVersionedUpdate(final DBCollection dbColl, final T entity, final DBObject dbObj, final Object idValue, final InsertOptions options, final MappedClass mc) {
WriteResult wr;
if (mc.getFieldsAnnotatedWith(Version.class).isEmpty()) {
return null;
}
final MappedField mfVersion = mc.getMappedVersionField();
final String versionKeyName = mfVersion.getNameToStore();
Long oldVersion = (Long) mfVersion.getFieldValue(entity);
long newVersion = nextValue(oldVersion);
dbObj.put(versionKeyName, newVersion);
if (idValue != null && newVersion != 1) {
final Query<?> query = find(dbColl.getName(), entity.getClass()).disableValidation().filter(Mapper.ID_KEY, idValue).enableValidation().filter(versionKeyName, oldVersion);
final UpdateResults res = update(query, dbObj, new UpdateOptions().bypassDocumentValidation(options.getBypassDocumentValidation()).writeConcern(options.getWriteConcern()));
wr = res.getWriteResult();
if (res.getUpdatedCount() != 1) {
throw new ConcurrentModificationException(format("Entity of class %s (id='%s',version='%d') was concurrently updated.", entity.getClass().getName(), idValue, oldVersion));
}
} else {
wr = saveDocument(dbColl, dbObj, options);
}
return wr;
}
Aggregations