use of com.mongodb.MongoException in project GNS by MobilityFirst.
the class MongoRecords method removeEntireRecord.
@Override
public void removeEntireRecord(String collectionName, String guid) throws FailedDBOperationException {
String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
try {
DBCollection collection = db.getCollection(collectionName);
BasicDBObject query = new BasicDBObject(primaryKey, guid);
collection.remove(query);
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} removeEntireRecord failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
}
}
use of com.mongodb.MongoException in project GNS by MobilityFirst.
the class MongoRecords method insert.
@Override
public void insert(String collectionName, String guid, JSONObject value) throws FailedDBOperationException, RecordExistsException {
db.requestStart();
try {
db.requestEnsureConnection();
DBCollection collection = db.getCollection(collectionName);
DBObject dbObject;
try {
dbObject = (DBObject) JSON.parse(value.toString());
} catch (Exception e) {
throw new FailedDBOperationException(collectionName, guid, "Unable to parse json" + e.getMessage());
}
try {
collection.insert(dbObject);
} catch (DuplicateKeyException e) {
throw new RecordExistsException(collectionName, guid);
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} insert failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, dbObject.toString(), "Original mongo exception:" + e.getMessage());
}
} finally {
db.requestDone();
}
}
use of com.mongodb.MongoException in project GNS by MobilityFirst.
the class MongoRecords method selectRecordsQuery.
private MongoRecordCursor selectRecordsQuery(String collectionName, ColumnField valuesMapField, String query, List<String> projection, boolean explain) throws FailedDBOperationException {
db.requestEnsureConnection();
DBCollection collection = db.getCollection(collectionName);
DBCursor cursor = null;
try {
if (projection == null || // in the projection
(!projection.isEmpty() && projection.get(0).equals(GNSProtocol.ENTIRE_RECORD.toString()))) {
cursor = collection.find(parseMongoQuery(query, valuesMapField));
} else {
cursor = collection.find(parseMongoQuery(query, valuesMapField), generateProjection(projection));
}
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecordsQuery failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, query, "Original mongo exception:" + e.getMessage());
}
if (explain) {
System.out.println(cursor.explain().toString());
}
return new MongoRecordCursor(cursor, mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey());
}
use of com.mongodb.MongoException in project GNS by MobilityFirst.
the class MongoRecords method parseMongoQuery.
private DBObject parseMongoQuery(String query, ColumnField valuesMapField) {
// convert something like this: ~fred : ($gt: 0) into the queryable
// format, namely this: {~nr_valuesMap.fred : ($gt: 0)}
String edittedQuery = query;
edittedQuery = "{" + edittedQuery + "}";
edittedQuery = edittedQuery.replace("(", "{");
edittedQuery = edittedQuery.replace(")", "}");
edittedQuery = edittedQuery.replace("~", valuesMapField.getName() + ".");
// Filter out HRN records
String guidFilter = "{" + NameRecord.VALUES_MAP.getName() + "." + AccountAccess.GUID_INFO + ": { $exists: true}}";
edittedQuery = buildAndQuery(guidFilter, edittedQuery);
try {
DatabaseConfig.getLogger().log(Level.FINE, "{0} Edited query = {1}", new Object[] { dbName, edittedQuery });
DBObject parse = (DBObject) JSON.parse(edittedQuery);
DatabaseConfig.getLogger().log(Level.FINE, "{0} Parse = {1}", new Object[] { dbName, parse.toString() });
return parse;
} catch (Exception e) {
throw new MongoException("Unable to parse query", e);
}
}
use of com.mongodb.MongoException in project jackrabbit-oak by apache.
the class ReplicaSetInfo method getHiddenMembers.
List<String> getHiddenMembers() {
BasicDBObject result;
try {
result = getReplicaConfig();
} catch (MongoException e) {
LOG.error("Can't get replica configuration", e);
return null;
}
@SuppressWarnings("unchecked") Iterable<BasicBSONObject> members = (Iterable<BasicBSONObject>) result.get("members");
if (members == null) {
members = Collections.emptyList();
}
List<String> hiddenMembers = new ArrayList<String>();
for (BasicBSONObject member : members) {
if (member.getBoolean("hidden")) {
hiddenMembers.add(member.getString("host"));
}
}
return hiddenMembers;
}
Aggregations