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();
}
}
}
}
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());
}
}
}
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();
}
}
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());
}
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());
}
Aggregations