Search in sources :

Example 36 with MongoException

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());
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 37 with MongoException

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();
    }
}
Also used : RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) DBCollection(com.mongodb.DBCollection) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DuplicateKeyException(com.mongodb.DuplicateKeyException)

Example 38 with MongoException

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());
}
Also used : DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 39 with MongoException

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);
    }
}
Also used : MongoException(com.mongodb.MongoException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DuplicateKeyException(com.mongodb.DuplicateKeyException) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) RecordExistsException(edu.umass.cs.gnscommon.exceptions.server.RecordExistsException) JSONException(org.json.JSONException) MongoException(com.mongodb.MongoException) UnknownHostException(java.net.UnknownHostException) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

Example 40 with MongoException

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;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) MongoException(com.mongodb.MongoException) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

MongoException (com.mongodb.MongoException)42 BasicDBObject (com.mongodb.BasicDBObject)22 DBObject (com.mongodb.DBObject)21 DBCollection (com.mongodb.DBCollection)16 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 DBCursor (com.mongodb.DBCursor)8 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)4 IOException (java.io.IOException)4 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 JSONException (org.json.JSONException)4 Stopwatch (com.google.common.base.Stopwatch)3 BasicDBList (com.mongodb.BasicDBList)3 DuplicateKeyException (com.mongodb.DuplicateKeyException)3 MongoClient (com.mongodb.MongoClient)3 BulkWriteOperation (com.mongodb.BulkWriteOperation)2 CommandResult (com.mongodb.CommandResult)2 MongoClientURI (com.mongodb.MongoClientURI)2