List of usage examples for com.mongodb WriteResult getN
public int getN()
From source file:org.jumpmind.symmetric.io.MongoDatabaseWriter.java
License:Open Source License
@Override protected LoadStatus delete(CsvData data, boolean useConflictDetection) { statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS); try {//from w ww.j a v a 2s . c o m DB db = clientManager.getDB(objectMapper.mapToDatabase(this.targetTable)); DBCollection collection = db.getCollection(objectMapper.mapToCollection(this.targetTable)); String[] columnNames = sourceTable.getColumnNames(); Map<String, String> newData = data.toColumnNameValuePairs(columnNames, CsvData.ROW_DATA); Map<String, String> oldData = data.toColumnNameValuePairs(columnNames, CsvData.OLD_DATA); Map<String, String> pkData = data.toKeyColumnValuePairs(this.sourceTable); DBObject query = objectMapper.mapToDBObject(sourceTable, newData, oldData, pkData, true); WriteResult results = collection.remove(query, WriteConcern.ACKNOWLEDGED); if (results.getN() != 1) { log.warn("Attempted to remove a single object" + query.toString() + ". Instead removed: " + results.getN()); } return LoadStatus.SUCCESS; } finally { statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS); } }
From source file:org.jwebsocket.plugins.loadbalancer.mongodb.MongoDBClusterManager.java
License:Apache License
@Override public int removeConnectorEndPoints(String aConnectorId) { WriteResult lResult = mEndPoints.remove(new BasicDBObject().append(CONNECTOR_ID, aConnectorId)); return lResult.getN(); }
From source file:org.keycloak.connections.mongo.lock.MongoDBLockProvider.java
License:Apache License
private boolean acquireLock() { DBObject query = new BasicDBObject("locked", false); BasicDBObject update = new BasicDBObject("locked", true); update.append("_id", 1); update.append("lockedSince", Time.toMillis(Time.currentTime())); update.append("lockedBy", HostUtils.getHostName()); // Maybe replace with something better, but doesn't matter for now try {//from ww w . j a va 2 s .c o m WriteResult wr = db.getCollection(DB_LOCK_COLLECTION).update(query, update, true, false); if (wr.getN() == 1) { logger.debugf("Successfully acquired DB lock"); factory.setHasLock(true); return true; } else { return false; } } catch (DuplicateKeyException dke) { logger.debugf("Failed acquire lock. Reason: %s", dke.getMessage()); } return false; }
From source file:org.keycloak.connections.mongo.lock.MongoDBLockProvider.java
License:Apache License
@Override public void releaseLock() { DBObject query = new BasicDBObject("locked", true); BasicDBObject update = new BasicDBObject("locked", false); update.append("_id", 1); update.append("lockedBy", null); update.append("lockedSince", null); try {/*w ww . j a v a 2 s. c o m*/ WriteResult wr = db.getCollection(DB_LOCK_COLLECTION).update(query, update, true, false); if (wr.getN() > 0) { factory.setHasLock(false); logger.debugf("Successfully released DB lock"); } else { logger.warnf("Attempt to release DB lock, but nothing was released"); } } catch (DuplicateKeyException dke) { logger.debugf("Failed release lock. Reason: %s", dke.getMessage()); } }
From source file:org.keycloak.connections.mongo.updater.impl.updates.Update1_8_0.java
License:Apache License
@Override public void update(KeycloakSession session) { BasicDBList orArgs = new BasicDBList(); orArgs.add(new BasicDBObject("type", UserCredentialModel.PASSWORD)); orArgs.add(new BasicDBObject("type", UserCredentialModel.PASSWORD_HISTORY)); BasicDBObject elemMatch = new BasicDBObject("$or", orArgs); elemMatch.put("algorithm", new BasicDBObject("$exists", false)); BasicDBObject query = new BasicDBObject("credentials", new BasicDBObject("$elemMatch", elemMatch)); BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("credentials.$.algorithm", Pbkdf2PasswordHashProvider.ID)); DBCollection users = db.getCollection("users"); // Not sure how to do in single query int countModified = 1; while (countModified > 0) { WriteResult wr = users.update(query, update, false, true); countModified = wr.getN(); log.debugf("%d credentials modified in current iteration during upgrade to 1.8", countModified); }/*from w w w.j av a 2 s .c om*/ }
From source file:org.keycloak.connections.mongo.updater.impl.updates.Update1_9_2.java
License:Apache License
@Override public void update(KeycloakSession session) { BasicDBList orArgs = new BasicDBList(); orArgs.add(new BasicDBObject("type", UserCredentialModel.PASSWORD)); orArgs.add(new BasicDBObject("type", UserCredentialModel.PASSWORD_HISTORY)); BasicDBObject elemMatch = new BasicDBObject("$or", orArgs); elemMatch.put("algorithm", HmacOTP.HMAC_SHA1); BasicDBObject query = new BasicDBObject("credentials", new BasicDBObject("$elemMatch", elemMatch)); BasicDBObject update = new BasicDBObject("$set", new BasicDBObject("credentials.$.algorithm", Pbkdf2PasswordHashProvider.ID)); DBCollection users = db.getCollection("users"); // Not sure how to do in single query int countModified = 1; while (countModified > 0) { WriteResult wr = users.update(query, update, false, true); countModified = wr.getN(); log.debugf("%d credentials modified in current iteration during upgrade to 1.8", countModified); }// w w w.ja v a 2 s.co m }
From source file:org.mongoste.core.impl.mongodb.MongoStatsEngine.java
License:Open Source License
@SuppressWarnings("finally") private boolean countRawTarget(StatEvent event) throws StatsEngineException { boolean processed = false; try {/* w w w . java 2 s .c om*/ BasicDBObject q = new BasicDBObject(); q.put(EVENT_CLIENT_ID, event.getClientId()); q.put(EVENT_TARGET, event.getTarget()); q.put(EVENT_TARGET_TYPE, event.getTargetType()); q.put(EVENT_ACTION, event.getAction()); q.put(EVENT_DATE, event.getYearMonthDate().toDate()); q.put(TARGET_YEAR, event.getYear()); q.put(TARGET_MONTH, event.getMonth()); BasicDBObject doc = new BasicDBObject(); //BasicDBObject docSet = new BasicDBObject(); doc.put("$addToSet", createAddToSetOwnersTagsDoc(event)); BasicDBObject incDoc = new BasicDBObject(); incDoc.put(FIELD_COUNT, 1); //Month count String metaBaseKey = ""; TimeScope precision = getTimeScopePrecision(); if (precision == TimeScope.DAILY || precision == TimeScope.HOURLY) { String dayKey = createDotPath(FIELD_DAYS, event.getDay()); incDoc.put(createDotPath(dayKey, FIELD_COUNT), 1); //Day count if (precision == TimeScope.HOURLY) { String hourKey = createDotPath(dayKey, FIELD_HOURS, event.getHour()); incDoc.put(createDotPath(hourKey, FIELD_COUNT), 1);//Hour count metaBaseKey = hourKey; } else { metaBaseKey = dayKey; } } //Count metadata Map<String, Object> metadata = event.getMetadata(); for (String metaKey : metadata.keySet()) { incDoc.put(createDotPath(metaBaseKey, FIELD_META, metaKey, metaKeyValue(metaKey, metadata.get(metaKey))), 1); } doc.put("$inc", incDoc); DBCollection targets = getTargetCollection(event, TimeScope.GLOBAL); //TODO externalize write concern to configuration properties: WriteResult wr = targets.update(q, doc, true, true, WriteConcern.FSYNC_SAFE); processed = wr.getN() > 0; } catch (MongoException ex) { int errorCode = ex.getCode(); if (errorCode == ERROR_DUPKEY || errorCode == ERROR_DUPKEY_INSERT) { throw new DuplicateEventException("Duplicate event " + event); } throw new StatsEngineException("countRawTarget failed", ex); } return processed; }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepository.java
License:Apache License
@Override public void updateState(String id, StateDiff diff, ChangeTokenUpdater changeTokenUpdater) { List<DBObject> updates = converter.diffToBson(diff); for (DBObject update : updates) { DBObject query = new BasicDBObject(idKey, id); if (changeTokenUpdater == null) { if (log.isTraceEnabled()) { log.trace("MongoDB: UPDATE " + id + ": " + update); }//from ww w . j a v a2 s . co m } else { // assume bson is identical to dbs internals // condition works even if value is null Map<String, Serializable> conditions = changeTokenUpdater.getConditions(); Map<String, Serializable> tokenUpdates = changeTokenUpdater.getUpdates(); if (update.containsField(MONGODB_SET)) { ((DBObject) update.get(MONGODB_SET)).putAll(tokenUpdates); } else { DBObject set = new BasicDBObject(); set.putAll(tokenUpdates); update.put(MONGODB_SET, set); } if (log.isTraceEnabled()) { log.trace("MongoDB: UPDATE " + id + ": IF " + conditions + " THEN " + update); } query.putAll(conditions); } WriteResult w = coll.update(query, update); if (w.getN() != 1) { log.trace("MongoDB: -> CONCURRENT UPDATE: " + id); throw new ConcurrentUpdateException(id); } // TODO dupe exception // throw new DocumentException("Missing: " + id); } }
From source file:org.nuxeo.ecm.core.storage.mongodb.MongoDBRepository.java
License:Apache License
@Override public void deleteStates(Set<String> ids) { DBObject query = new BasicDBObject(idKey, new BasicDBObject(QueryOperators.IN, ids)); if (log.isTraceEnabled()) { log.trace("MongoDB: REMOVE " + ids); }//from ww w. jav a2 s . c o m WriteResult w = coll.remove(query); if (w.getN() != ids.size()) { log.error("Removed " + w.getN() + " docs for " + ids.size() + " ids: " + ids); } }
From source file:org.opencb.opencga.catalog.db.mongodb.CatalogMongoDBAdaptor.java
License:Apache License
/** * At the moment it does not clean external references to itself. *///from w w w . ja v a 2s . com @Override public QueryResult<Integer> deleteFile(int fileId) throws CatalogDBException { long startTime = startQuery(); WriteResult id = fileCollection.remove(new BasicDBObject(_ID, fileId), null).getResult().get(0); List<Integer> deletes = new LinkedList<>(); if (id.getN() == 0) { throw CatalogDBException.idNotFound("File", fileId); } else { deletes.add(id.getN()); return endQuery("delete file", startTime, deletes); } }