本文整理汇总了Java中com.mongodb.CommandResult.getErrorMessage方法的典型用法代码示例。如果您正苦于以下问题:Java CommandResult.getErrorMessage方法的具体用法?Java CommandResult.getErrorMessage怎么用?Java CommandResult.getErrorMessage使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mongodb.CommandResult
的用法示例。
在下文中一共展示了CommandResult.getErrorMessage方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setBalancerState
import com.mongodb.CommandResult; //导入方法依赖的package包/类
/**
* set the state of balancer.
*
* @param dbConn
* @param state
* @return Error description, or null if no errors
*/
private static String setBalancerState(DB dbConn, boolean state) {
DBObject balancer = new BasicDBObject(ID, "balancer");
DBObject updateObj = new BasicDBObject();
String stopped = state ? "false" : "true";
updateObj.put("$set", new BasicDBObject("stopped", stopped));
WriteResult wresult = dbConn.getSisterDB("config").getCollection("settings").update(balancer, updateObj, true, false);
if (wresult != null) {
CommandResult result = wresult.getLastError();
if (!result.ok()) {
LOG.error("Error setting balancer state to {}: {}", state, result.getErrorMessage());
return result.getErrorMessage();
}
}
return null;
}
示例2: insert
import com.mongodb.CommandResult; //导入方法依赖的package包/类
@Override
public String insert(String statement, Object parameter) {
logger.debug("Execute 'insert' mongodb command. Statement '" + statement + "'.");
InsertConfig config = (InsertConfig) configuration.getConfig(statement);
if (config == null) {
throw new MongoDaoException(statement, "Insert statement id '" + statement + "' not found.");
}
String collection = config.getCollection();
NodeEntry document = config.getDocument();
Entry selectKey = config.getSelectKey();
DB db = factory.getDataSource().getDB();
DBCollection coll = db.getCollection(collection);
Map<String, Object> doc = (Map<String, Object>) document.executorNode(configuration, parameter);
DBObject docDbo = new BasicDBObject(doc);
logger.debug("Execute 'insert' mongodb command. Doc '" + docDbo + "'.");
WriteResult writeResult = coll.insert(docDbo, WriteConcern.SAFE);
CommandResult commandResult = writeResult.getLastError(WriteConcern.SAFE);
if (commandResult.getException() != null) {
throw new MongoDaoException(statement, "Execute 'insert' mongodb command has exception. Cause: " + commandResult.getErrorMessage());
}
String newId = docDbo.get("_id").toString();
logger.debug("Execute 'insert' mongodb command. ObjectId is '" + newId + "'.");
if (selectKey != null) {
helper.setSelectKey(selectKey, newId, parameter);
}
return newId;
}
示例3: update
import com.mongodb.CommandResult; //导入方法依赖的package包/类
@Override
public int update(String statement, Object parameter) {
logger.debug("Execute 'update' mongodb command. Statement '" + statement + "'.");
UpdateConfig config = (UpdateConfig) configuration.getConfig(statement);
if (config == null) {
throw new MongoDaoException(statement, "Update statement id '" + statement + "' not found.");
}
String collection = config.getCollection();
NodeEntry query = config.getQuery();
NodeEntry action = config.getAction();
DB db = factory.getDataSource().getDB();
DBCollection coll = db.getCollection(collection);
Map<String, Object> q = (Map<String, Object>) query.executorNode(configuration, parameter);
Map<String, Object> a = (Map<String, Object>) action.executorNode(configuration, parameter);
DBObject queryDbo = new BasicDBObject(q);
logger.debug("Execute 'update' mongodb command. Query '" + queryDbo + "'.");
DBObject actionDbo = (a == null) ? null : new BasicDBObject(a);
logger.debug("Execute 'update' mongodb command. Action '" + actionDbo + "'.");
WriteResult writeResult = coll.update(queryDbo, actionDbo, false, true, WriteConcern.SAFE);
CommandResult commandResult = writeResult.getLastError(WriteConcern.SAFE);
if (commandResult.getException() != null) {
throw new MongoDaoException(statement, "Execute 'update' mongodb command has exception. Cause: " + commandResult.getErrorMessage());
}
return writeResult.getN();
}
示例4: delete
import com.mongodb.CommandResult; //导入方法依赖的package包/类
@Override
public int delete(String statement, Object parameter) {
logger.debug("Execute 'delete' mongodb command. Statement '" + statement + "'.");
DeleteConfig config = (DeleteConfig) configuration.getConfig(statement);
if (config == null) {
throw new MongoDaoException(statement, "Delete statement id '" + statement + "' not found.");
}
String collection = config.getCollection();
NodeEntry query = config.getQuery();
DB db = factory.getDataSource().getDB();
DBCollection coll = db.getCollection(collection);
Map<String, Object> q = (Map<String, Object>) query.executorNode(configuration, parameter);
DBObject queryDbo = new BasicDBObject(q);
logger.debug("Execute 'delete' mongodb command. Query '" + queryDbo + "'.");
WriteResult writeResult = coll.remove(queryDbo, WriteConcern.SAFE);
CommandResult commandResult = writeResult.getLastError(WriteConcern.SAFE);
if (commandResult.getException() != null) {
throw new MongoDaoException(statement, "Execute 'delete' mongodb command has exception. Cause: " + commandResult.getErrorMessage());
}
return writeResult.getN();
}
示例5: update
import com.mongodb.CommandResult; //导入方法依赖的package包/类
@Override
public <T> int update(Class<T> entityClass, String[] fields, Object[] fieldValues, Serializable id) {
WriteResult result = operations.updateFirst(new Query(where("_id").is(id)), setFieldValue(fields, fieldValues), entityClass);
CommandResult commandResult = result.getLastError();
if(commandResult.ok()){
return 1;
}
throw new DaoException(commandResult.getErrorMessage());
}
示例6: preSplit
import com.mongodb.CommandResult; //导入方法依赖的package包/类
/**
* Pre-split database
* @param shardCollections: the set of collections to be split
* @param dbName
* @param mongoTemplate
* @return Error description, or null if no errors
*/
public static String preSplit(Set<String> shardCollections, String dbName, MongoTemplate mongoTemplate) {
DB dbConn = mongoTemplate.getDb().getSisterDB("admin");
//Don't do anything if it is non-sharded
List<String> shards = getShards(dbConn);
if (shards.size() == 0) {
return null;
}
//set balancer off
String sresult = setBalancerState(dbConn, false);
if (sresult != null) {
return sresult;
}
// Enable sharding for this database
DBObject enableShard = new BasicDBObject("enableSharding", dbName);
CommandResult result = dbConn.command(enableShard);
if (!result.ok()) {
LOG.error("Error enabling sharding on {}: {}", dbConn, result.getErrorMessage());
return result.getErrorMessage();
}
for (String coll : shardCollections) {
String collection = dbName + "." + coll;
// Enable sharding for this collection, sharding on _id
DBObject shardColl = new BasicDBObject();
shardColl.put("shardCollection", collection);
shardColl.put("key", new BasicDBObject(ID, 1));
result = dbConn.command(shardColl);
if (!result.ok()) {
LOG.error("Error enabling shard'ing on {}: {}", collection, result.getErrorMessage());
return result.getErrorMessage();
}
sresult = moveChunks(collection, shards, dbConn);
if (sresult != null) {
return sresult;
}
}
return preSplitBinCollections(dbName, mongoTemplate);
}
示例7: preSplitBinCollections
import com.mongodb.CommandResult; //导入方法依赖的package包/类
private static String preSplitBinCollections(String dbName, MongoTemplate mongoTemplate) {
DB dbConn = mongoTemplate.getDb().getSisterDB("admin");
List<String> shards = getShards(dbConn);
if (shards != null && shards.size() > 0) {
int numShards = shards.size();
List<String> collections = Arrays.asList("recordHash", "deltas");
for (String collectionName: collections) {
LOG.info("Shard count = {}. Setting up sharding config for {}!", numShards, collectionName);
String collection = dbName + "." + collectionName;
DBObject shardColl = new BasicDBObject();
shardColl.put("shardCollection", collection);
shardColl.put("key", new BasicDBObject(ID, 1));
CommandResult result = dbConn.command(shardColl);
if (!result.ok()) {
LOG.error("Error enabling shard'ing on recordHash: {}", result.getErrorMessage());
return result.getErrorMessage();
}
int charOffset = 256 / numShards;
List<byte[]> shardPoints = new ArrayList<byte[]>();
shardPoints.add(null);
for (int shard = 1; shard < numShards; shard++) {
String splitString = Integer.toHexString(charOffset * shard);
if (splitString.length() < 2) {
splitString = "0" + splitString;
}
splitString = splitString + STR_0X38;
byte[] splitPoint = RecordHash.hex2Binary(splitString);
shardPoints.add(splitPoint);
LOG.info("Adding recordHash splitPoint [" + RecordHash.binary2Hex(splitPoint) + "]");
DBObject splitCmd = new BasicDBObject();
splitCmd.put("split", collection);
splitCmd.put("middle", new BasicDBObject(ID, splitPoint));
result = dbConn.command(splitCmd);
if (!result.ok()) {
LOG.error("Error splitting chunk in recordHash: {}", result.getErrorMessage());
return result.getErrorMessage();
}
}
for (int index = 0; index < numShards; index++) {
DBObject moveCommand = new BasicDBObject();
moveCommand.put("moveChunk", collection);
moveCommand.put("find", new BasicDBObject(ID, index == 0 ? "$minkey" : shardPoints.get(index)));
moveCommand.put("to", shards.get(index));
result = dbConn.command(moveCommand);
if (!result.ok()) {
if (!result.getErrorMessage().equals("that chunk is already on that shard")) {
LOG.error("Error moving chunk in recordHash: {}", result.getErrorMessage());
return result.getErrorMessage();
}
}
}
}
} else {
LOG.info("No shards or shard count < 0. Not setting sharding config for recordHash!");
}
return null;
}