List of usage examples for com.mongodb WriteResult getN
public int getN()
From source file:org.alfresco.bm.cm.FileFolderService.java
License:Open Source License
/** * Increment the count of the files in a folder. * /*from ww w . ja v a 2s. c o m*/ * @param context the context in which the folder path is valid (mandatory) * @param path the folder path relative to the given context * @param fileCountInc the file count increment (can be negative) */ public void incrementFileCount(String context, String path, long fileCountInc) { DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_CONTEXT, context).add(FIELD_PATH, path).get(); DBObject updateObj = BasicDBObjectBuilder.start().push("$inc").add(FIELD_FILE_COUNT, fileCountInc).pop() .get(); WriteResult result = collection.update(queryObj, updateObj); if (result.getN() != 1) { throw new RuntimeException("Failed to update folder's file count: \n" + " Context: " + context + "\n" + " Path: " + path + "\n" + " Result: " + result); } // Done if (logger.isDebugEnabled()) { logger.debug("Incremented the file count on " + context + "/" + path + " by " + fileCountInc); } }
From source file:org.alfresco.bm.event.mongo.MongoEventService.java
License:Open Source License
@Override public boolean deleteEvent(Event event) { String id = event.getId();// ww w . j a v a 2 s .c o m DBObject queryObj = BasicDBObjectBuilder.start().add(Event.FIELD_ID, new ObjectId(id)).get(); // Drop any associated memory data runLocalData.remove(id); WriteResult wr = collection.remove(queryObj); if (wr.getN() != 1) { // Done if (logger.isDebugEnabled()) { logger.debug("Failed to removed event: \n" + " Event: " + event + "\n" + " Result: " + wr); } return false; } else { // Done if (logger.isDebugEnabled()) { logger.debug("Removed event: " + event); } return true; } }
From source file:org.alfresco.bm.file.FileDataServiceImpl.java
License:Open Source License
@Override public void removeFile(String fileset, String remoteName) { DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_FILESET, fileset) .add(FIELD_REMOTE_NAME, remoteName).get(); WriteResult result = collection.remove(queryObj); if (logger.isDebugEnabled()) { logger.debug("Removed " + fileset + "." + remoteName + " and hit " + result.getN() + " rows"); }/* w ww .java 2 s . com*/ }
From source file:org.alfresco.bm.session.MongoSessionService.java
License:Open Source License
@Override protected boolean updateSessionData(String sessionId, DBObject data) { DBObject queryObj = BasicDBObjectBuilder.start().add(FIELD_ID, new ObjectId(sessionId)).get(); DBObject updateObj = BasicDBObjectBuilder.start().push("$set").add(FIELD_DATA, data).pop().get(); try {/* w w w . ja v a2 s . c om*/ WriteResult wr = collection.update(queryObj, updateObj); return wr.getN() > 0; } catch (MongoException e) { throw new RuntimeException("Failed to update session data: \n" + " Session: " + sessionId + "\n" + " Data: " + data, e); } }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Update an existing test to use new test details * /*from w w w . j a va 2 s . co m*/ * @param name * the name of the test (must exist) * @param version * the version of the test for concurrency checking * @param newName * the new test name * @param newDescription * the new description or <tt>null</tt> ot leave it * @param newRelease * the new software release or <tt>null</tt> to leave it * @param newSchema * the new schema number or <tt>null</tt> to leave it * @return <tt>true</tt> if the test run was modified or <tt>false</tt> if * not */ public boolean updateTest(String name, int version, String newName, String newDescription, String newRelease, Integer newSchema) { if (name == null) { throw new IllegalArgumentException("Updated requires a name and version."); } // Find the test by name and version DBObject queryObj = QueryBuilder.start().and(FIELD_NAME).is(name).and(FIELD_VERSION).is(version).get(); // Handle version wrap-around Integer newVersion = version >= Short.MAX_VALUE ? 1 : version + 1; // Gather all the setters required BasicDBObjectBuilder setObjBuilder = BasicDBObjectBuilder.start().add(FIELD_VERSION, newVersion); if (newName != null) { Pattern pattern = Pattern.compile(TEST_NAME_REGEX); Matcher matcher = pattern.matcher(newName); if (!matcher.matches()) { throw new IllegalArgumentException("The test name '" + newName + "' is invalid. " + "Test names must start with a character and contain only characters, numbers or underscore."); } setObjBuilder.add(FIELD_NAME, newName); } if (newDescription != null) { setObjBuilder.add(FIELD_DESCRIPTION, newDescription); } if (newRelease != null) { setObjBuilder.add(FIELD_RELEASE, newRelease); } if (newSchema != null) { setObjBuilder.add(FIELD_SCHEMA, newSchema); } DBObject setObj = setObjBuilder.get(); // Now push the values to set into the update DBObject updateObj = BasicDBObjectBuilder.start().add("$set", setObj).get(); WriteResult result = tests.update(queryObj, updateObj); boolean written = (result.getN() > 0); // Done if (logger.isDebugEnabled()) { if (written) { logger.debug("Updated test: \n" + " Test: " + name + "\n" + " Update: " + updateObj); } else { logger.debug("Did not update test: " + name); } } return written; }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Delete an existing test/*from w ww. j av a 2 s. c om*/ * * @param test * the name of the test (must exist) * @return <tt>true</tt> if the test was deleted or <tt>false</tt> if not */ public boolean deleteTest(String test) { DBObject testObj = getTest(test, false); if (testObj == null) { // The test no longer exists, so the run effectively doesn't either logger.warn("Test not found: " + test); return false; } ObjectId testObjId = (ObjectId) testObj.get(FIELD_ID); // Find the test by name and version DBObject testDelObj = QueryBuilder.start().and(FIELD_ID).is(testObjId).get(); WriteResult result = tests.remove(testDelObj); boolean written = (result.getN() > 0); // Clean up test-related runs DBObject runDelObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).get(); testRuns.remove(runDelObj); // Clean up properties DBObject propDelObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).get(); testProps.remove(propDelObj); // Done if (logger.isDebugEnabled()) { if (written) { logger.debug("Deleted test: " + test); } else { logger.debug("Did not delete test: " + test); } } return written; }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Update an existing test run with new details * * @param test//w ww .ja v a 2s . c o m * the name of the test * @param run * the name of the test run (must exist) * @param version * the version of the test for concurrency checking * @param newName * the new name of the test run * @param newDescription * the new description or <tt>null</tt> ot leave it * @return <tt>true</tt> if the test run was modified or <tt>false</tt> if * not */ public boolean updateTestRun(String test, String run, int version, String newName, String newDescription) { if (test == null || run == null) { throw new IllegalArgumentException("Updated requires a name and version."); } // Get the test DBObject testObj = getTest(test, false); if (testObj == null) { if (logger.isDebugEnabled()) { logger.debug("Unable to update test run; test not found: " + test); } return false; } // Find the test run by name and version DBObject queryObj = QueryBuilder.start().and(FIELD_TEST).is(testObj.get(FIELD_ID)).and(FIELD_NAME).is(run) .and(FIELD_VERSION).is(version).get(); // Handle version wrap-around Integer newVersion = version >= Short.MAX_VALUE ? 1 : version + 1; // Gather all the setters required BasicDBObjectBuilder setObjBuilder = BasicDBObjectBuilder.start().add(FIELD_VERSION, newVersion); if (newName != null) { Pattern pattern = Pattern.compile(RUN_NAME_REGEX); Matcher matcher = pattern.matcher(newName); if (!matcher.matches()) { throw new IllegalArgumentException("The test run name '" + newName + "' is invalid. " + "Test run names may only contain characters, numbers or underscore."); } setObjBuilder.add(FIELD_NAME, newName); } if (newDescription != null) { setObjBuilder.add(FIELD_DESCRIPTION, newDescription); } DBObject setObj = setObjBuilder.get(); // Now push the values to set into the update DBObject updateObj = BasicDBObjectBuilder.start().add("$set", setObj).get(); WriteResult result = testRuns.update(queryObj, updateObj); boolean written = (result.getN() > 0); // Done if (logger.isDebugEnabled()) { if (written) { logger.debug("Updated test run: \n" + " Test: " + test + "\n" + " Run: " + run + "\n" + " Update: " + updateObj); } else { logger.debug("Did not update test run: " + test + "." + run); } } return written; }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Update the run state of a test run.//from w w w . j av a2s.c o m * <p/> * The test run {@link TestConstants#FIELD_STATE state} will be set based on * the values. * <p/> * Note that a test run can either be stopped or completed but not both. In * both cases, * though, the test run must have been scheduled and then started. * * @param test * the name of the test * @param run * the name of the test run (must exist) * @param version * the version of the test for concurrency checking * @param testRunState * the test run state to set (<null> to ignore) * @param scheduled * the time when the test run is scheduled to start (<null> to * ignore) * @param started * the time when the test run started (<null> to ignore) * @param stopped * the time when the test run was stopped (<null> to ignore) * @param completed * the time when the test run was completed (<null> to ignore) * @param duration * the time the test has been running for in ms (<null> to ignore) * @param progress * the new progress for the test run (<null> to ignore) * @param resultsSuccess * the number of successful results (<null> to ignore) * @param resultsFailure * the number of failed results (<null> to ignore) * @return <tt>true</tt> if the test run was modified or <tt>false</tt> if * not */ public boolean updateTestRunState(ObjectId runId, int version, TestRunState testRunState, Long scheduled, Long started, Long stopped, Long completed, Long duration, Double progress, Long resultsSuccess, Long resultsFail) { // Find the test run by name and version DBObject queryObj = QueryBuilder.start().and(FIELD_ID).is(runId).and(FIELD_VERSION).is(version).get(); // Gather all the setters required BasicDBObjectBuilder setObjBuilder = BasicDBObjectBuilder.start(); if (testRunState != null) { setObjBuilder.add(FIELD_STATE, testRunState.toString()); } if (scheduled != null) { setObjBuilder.add(FIELD_SCHEDULED, scheduled); } if (started != null) { setObjBuilder.add(FIELD_STARTED, started); } if (stopped != null) { setObjBuilder.add(FIELD_STOPPED, stopped); } if (completed != null) { setObjBuilder.add(FIELD_COMPLETED, completed); } if (duration != null) { setObjBuilder.add(FIELD_DURATION, duration); } if (progress != null) { // Adjust accuracy of the progress long progressLong = Math.round(progress * 10000.0); if (progressLong < 0L || progressLong > 10000L) { throw new IllegalArgumentException("Progress must be expressed as a double in range [0.0, 1.0]."); } progress = progressLong / 10000.0; // Accuracy setObjBuilder.add(FIELD_PROGRESS, progress); } if (resultsSuccess != null || resultsFail != null) { if (resultsSuccess == null || resultsFail == null) { throw new IllegalArgumentException("resultsSuccess and resultsFail must be updated together."); } long resultsTotal = Long.valueOf(resultsSuccess.longValue() + resultsFail.longValue()); double successRate = (resultsTotal == 0) ? 1.0 : (resultsSuccess / (double) resultsTotal); setObjBuilder.add(FIELD_RESULTS_SUCCESS, resultsSuccess); setObjBuilder.add(FIELD_RESULTS_FAIL, resultsFail); setObjBuilder.add(FIELD_RESULTS_TOTAL, resultsTotal); setObjBuilder.add(FIELD_SUCCESS_RATE, successRate); } if (resultsFail != null) { setObjBuilder.add(FIELD_RESULTS_FAIL, resultsFail); } // Check that we are actually going to do something if (setObjBuilder.get().keySet().size() == 0) { if (logger.isDebugEnabled()) { logger.debug("No updates provided for test run: " + runId); } return false; } // Handle version wrap-around Integer newVersion = version >= Short.MAX_VALUE ? 1 : version + 1; setObjBuilder.add(FIELD_VERSION, newVersion); // Get the object containing the set values DBObject setObj = setObjBuilder.get(); // Now push the values to set into the update DBObject updateObj = BasicDBObjectBuilder.start().add("$set", setObj).get(); WriteResult result = testRuns.update(queryObj, updateObj); boolean written = (result.getN() > 0); // Done if (logger.isDebugEnabled()) { if (written) { logger.debug("Updated test run state: \n" + " Run ID: " + runId + "\n" + " Update: " + updateObj); } else { logger.debug("Did not update test run state: " + runId); } } return written; }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Delete an existing test run//from w w w.j a v a 2 s. co m * * @param runObjId * the ID of the test run * @return <tt>true</tt> if the test run was deleted or <tt>false</tt> if * not */ public boolean deleteTestRun(ObjectId runObjId) { // Get the test run DBObject runObj; try { runObj = getTestRun(runObjId, false); } catch (ObjectNotFoundException e) { logger.warn("Unable to delete test run as it does not exist: " + runObjId, e); return false; } ObjectId testObjId = (ObjectId) runObj.get(FIELD_TEST); // Find the test run DBObject queryObj = QueryBuilder.start().and(FIELD_ID).is(runObjId).get(); WriteResult result = testRuns.remove(queryObj); boolean written = (result.getN() > 0); // Clean up properties DBObject propDelObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).add(FIELD_RUN, runObjId) .get(); testProps.remove(propDelObj); // Done if (logger.isDebugEnabled()) { if (written) { logger.debug("Deleted test run: " + queryObj); } else { logger.debug("Did not delete test run: " + runObjId); } } return written; }
From source file:org.alfresco.bm.test.mongo.MongoTestDAO.java
License:Open Source License
/** * Override a specific test property value. * <p/>//from w ww .ja v a2 s .com * A version number of zero indicates that there is no existing override * defined.<br/> * A value of <tt>null</tt> indicates that the existing override should be * removed. * * @param test * the name of the test * @param run * the name of the test run (<tt>null</tt> to reference the test * alone) * @param propertyName * the name of the property * @param version * the current version of the property * @param value * the new value to set or <tt>null</tt> to remove any override * @throws IllegalStateException * if the test has started */ public boolean setPropertyOverride(String test, String run, String propertyName, int version, String value) { // Handle version wrap-around int newVersion = (version >= Short.MAX_VALUE) ? 1 : version + 1; // We need to keep the IDs ObjectId runObjId = null; ObjectId testObjId = null; String origin = null; if (run == null) { origin = TestPropertyOrigin.TEST.name(); // Get the test DBObject testObj = getTest(test, false); if (testObj == null) { logger.warn("Unable to set property override for test as it was not found: " + test); return false; } // Get the ID testObjId = (ObjectId) testObj.get(FIELD_ID); } else { origin = TestPropertyOrigin.RUN.name(); // Get the test run DBObject runObj; try { runObj = getTestRun(test, run, false); } catch (ObjectNotFoundException e1) { logger.warn("Test run not found: " + test + "." + run, e1); return false; } // Check the state of the run try { TestRunState runState = TestRunState.valueOf((String) runObj.get(FIELD_STATE)); if (runState != TestRunState.NOT_SCHEDULED && runState != TestRunState.SCHEDULED) { throw new IllegalStateException( "Property overrides can only be set for test runs that have not started: \n" + " Run: " + runObj + "\n" + " Property: " + propertyName); } } catch (IllegalArgumentException e) { logger.error("Test run state is unknown: " + runObj); this.deleteTestRun(runObjId); return false; } // Get the ID runObjId = (ObjectId) runObj.get(FIELD_ID); testObjId = (ObjectId) runObj.get(FIELD_TEST); } DBObject queryObj = QueryBuilder.start().and(FIELD_TEST).is(testObjId).and(FIELD_RUN).is(runObjId) .and(FIELD_NAME).is(propertyName).and(FIELD_VERSION).is(Integer.valueOf(version)).get(); DBObject updateObj = BasicDBObjectBuilder.start().add(FIELD_TEST, testObjId).add(FIELD_RUN, runObjId) .add(FIELD_NAME, propertyName).add(FIELD_VERSION, Integer.valueOf(newVersion)) .add(FIELD_VALUE, value).add(FIELD_ORIGIN, origin).get(); WriteResult result = null; boolean written = false; try { if (value == null) { // remove property result = testProps.remove(queryObj); written = (result.getN() > 0); } else { // A value was provided, so either INSERT or UPDATE if (version == 0) { // This indicates that no override should exist, yet result = testProps.insert(updateObj); written = true; } else { // There must an update result = testProps.update(queryObj, updateObj); written = result.getN() > 0; } } } catch (DuplicateKeyException e) { written = false; } // Done if (logger.isDebugEnabled()) { if (written) { logger.debug("Wrote property override: \n" + " Test: " + test + "\n" + " Run: " + run + "\n" + " Property: " + propertyName + "\n" + " Version: " + version); } else { logger.debug( "Did not update property override: \n" + " Test: " + test + "\n" + " Run: " + run + "\n" + " Property: " + propertyName + "\n" + " Version: " + version); } } return written; }