Search in sources :

Example 31 with MongoException

use of com.mongodb.MongoException in project v7files by thiloplanz.

the class UploadCommand method main.

public static void main(String[] args) throws MongoException, IOException {
    if (args.length < 2) {
        System.err.println("Upload the contents of one or more files and print their SHA digests:");
        System.err.println("   upload <file> [file] [file] [...]");
        System.exit(1);
    }
    ContentStorage storage = new MongoContentStorage(Configuration.getMongo().getDB(Configuration.getProperty("mongo.db")));
    for (int i = 1; i < args.length; i++) {
        File f = new File(args[i]);
        if (f.isFile() && f.canRead()) {
            try {
                ContentSHA up = storage.storeContent(new FileInputStream(f));
                // TODO: display if chunked or not
                System.out.format("-      %10d %80s %40s\n", f.length(), f.getName(), up.getDigest());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentSHA(v7db.files.spi.ContentSHA) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) ContentStorage(v7db.files.spi.ContentStorage) File(java.io.File) FileInputStream(java.io.FileInputStream) MongoException(com.mongodb.MongoException) IOException(java.io.IOException)

Example 32 with MongoException

use of com.mongodb.MongoException in project GNS by MobilityFirst.

the class MongoRecords method removeMapKeys.

@Override
public void removeMapKeys(String collectionName, String name, ColumnField mapField, ArrayList<ColumnField> mapKeys) throws FailedDBOperationException {
    String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
    DBCollection collection = db.getCollection(collectionName);
    BasicDBObject query = new BasicDBObject(primaryKey, name);
    BasicDBObject updates = new BasicDBObject();
    if (mapField != null && mapKeys != null) {
        for (int i = 0; i < mapKeys.size(); i++) {
            String fieldName = mapField.getName() + "." + mapKeys.get(i).getName();
            updates.append(fieldName, 1);
        }
    }
    if (updates.keySet().size() > 0) {
        try {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} <============>unset{1}<============>", new Object[] { dbName, updates.toString() });
            collection.update(query, new BasicDBObject("$unset", updates));
        } catch (MongoException e) {
            DatabaseConfig.getLogger().log(Level.FINE, "{0} removeMapKeys failed: {1}", new Object[] { dbName, e.getMessage() });
            throw new FailedDBOperationException(collectionName, updates.toString(), "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 33 with MongoException

use of com.mongodb.MongoException in project GNS by MobilityFirst.

the class MongoRecords method lookupSomeFields.

@Override
public HashMap<ColumnField, Object> lookupSomeFields(String collectionName, String guid, ColumnField nameField, ColumnField valuesMapField, ArrayList<ColumnField> valuesMapKeys) throws RecordNotFoundException, FailedDBOperationException {
    if (guid == null) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} GUID is null: {1}", new Object[] { dbName, guid });
        throw new RecordNotFoundException(guid);
    }
    db.requestStart();
    try {
        String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
        db.requestEnsureConnection();
        DBCollection collection = db.getCollection(collectionName);
        BasicDBObject query = new BasicDBObject(primaryKey, guid);
        BasicDBObject projection = new BasicDBObject().append("_id", 0);
        if (valuesMapField != null && valuesMapKeys != null) {
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String fieldName = valuesMapField.getName() + "." + valuesMapKeys.get(i).getName();
                projection.append(fieldName, 1);
            }
        }
        DBObject dbObject = collection.findOne(query, projection);
        if (dbObject == null) {
            throw new RecordNotFoundException(guid);
        }
        HashMap<ColumnField, Object> hashMap = new HashMap<>();
        // put the name in the hashmap!! very important!!
        hashMap.put(nameField, guid);
        if (valuesMapField != null && valuesMapKeys != null) {
            // first we pull all the user values from the dbObject and put in a bson object
            // FIXME: Why not convert this to a JSONObject right now? We know that's what it is.
            BasicDBObject bson = (BasicDBObject) dbObject.get(valuesMapField.getName());
            DatabaseConfig.getLogger().log(Level.FINER, "{0} @@@@@@@@ {1}", new Object[] { dbName, bson });
            // then we run thru each userkey in the valuesMapKeys and pull the
            // value put stuffing it into the values map
            ValuesMap valuesMap = new ValuesMap();
            for (int i = 0; i < valuesMapKeys.size(); i++) {
                String userKey = valuesMapKeys.get(i).getName();
                if (containsFieldDotNotation(userKey, bson) == false) {
                    DatabaseConfig.getLogger().log(Level.FINE, "{0} DBObject doesn't contain {1}", new Object[] { dbName, userKey });
                    continue;
                }
                try {
                    switch(valuesMapKeys.get(i).type()) {
                        case USER_JSON:
                            Object value = getWithDotNotation(userKey, bson);
                            DatabaseConfig.getLogger().log(Level.FINE, "{0} Object is {1}", new Object[] { dbName, value.toString() });
                            valuesMap.put(userKey, value);
                            break;
                        case LIST_STRING:
                            valuesMap.putAsArray(userKey, JSONUtils.JSONArrayToResultValue(new JSONArray(getWithDotNotation(userKey, bson).toString())));
                            break;
                        default:
                            DatabaseConfig.getLogger().log(Level.SEVERE, "{0} ERROR: Error: User keys field {1} is not a known type: {2}", new Object[] { dbName, userKey, valuesMapKeys.get(i).type() });
                            break;
                    }
                } catch (JSONException e) {
                    DatabaseConfig.getLogger().log(Level.SEVERE, "{0} Error parsing json: {1}", new Object[] { dbName, e.getMessage() });
                    e.printStackTrace();
                }
            }
            hashMap.put(valuesMapField, valuesMap);
        }
        return hashMap;
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} lookupSomeFields failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
    } finally {
        db.requestDone();
    }
}
Also used : MongoException(com.mongodb.MongoException) HashMap(java.util.HashMap) ValuesMap(edu.umass.cs.gnsserver.utils.ValuesMap) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) RecordNotFoundException(edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 34 with MongoException

use of com.mongodb.MongoException in project GNS by MobilityFirst.

the class MongoRecords method selectRecordsWithin.

private MongoRecordCursor selectRecordsWithin(String collectionName, ColumnField valuesMapField, String key, String value, boolean explain) throws FailedDBOperationException {
    db.requestEnsureConnection();
    DBCollection collection = db.getCollection(collectionName);
    BasicDBList box = parseJSONArrayLocationStringIntoDBList(value);
    String fieldName = valuesMapField.getName() + "." + key;
    BasicDBObject shapeClause = new BasicDBObject("$box", box);
    BasicDBObject withinClause = new BasicDBObject("$geoWithin", shapeClause);
    BasicDBObject query = new BasicDBObject(fieldName, withinClause);
    DBCursor cursor = null;
    try {
        cursor = collection.find(query);
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecordsWithin failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, fieldName, "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) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) 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 35 with MongoException

use of com.mongodb.MongoException in project GNS by MobilityFirst.

the class MongoRecords method selectRecordsNear.

private MongoRecordCursor selectRecordsNear(String collectionName, ColumnField valuesMapField, String key, String value, Double maxDistance, boolean explain) throws FailedDBOperationException {
    db.requestEnsureConnection();
    DBCollection collection = db.getCollection(collectionName);
    double maxDistanceInRadians = maxDistance / METERS_PER_DEGREE;
    BasicDBList tuple = new BasicDBList();
    try {
        JSONArray json = new JSONArray(value);
        tuple.add(json.getDouble(0));
        tuple.add(json.getDouble(1));
    } catch (JSONException e) {
        DatabaseConfig.getLogger().log(Level.SEVERE, "{0} Unable to parse JSON: {1}", new Object[] { dbName, e.getMessage() });
    }
    String fieldName = valuesMapField.getName() + "." + key;
    BasicDBObject nearClause = new BasicDBObject("$near", tuple).append("$maxDistance", maxDistanceInRadians);
    BasicDBObject query = new BasicDBObject(fieldName, nearClause);
    DBCursor cursor = null;
    try {
        cursor = collection.find(query);
    } catch (MongoException e) {
        DatabaseConfig.getLogger().log(Level.FINE, "{0} selectNear failed: {1}", new Object[] { dbName, e.getMessage() });
        throw new FailedDBOperationException(collectionName, fieldName, "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) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) MongoException(com.mongodb.MongoException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) FailedDBOperationException(edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)

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