use of com.mongodb.DBCollection in project graylog2-server by Graylog2.
the class MongodbSeed method loadDataset.
public void loadDataset(URL dbPath, String nodeId) throws IOException, URISyntaxException {
Map<String, List<DBObject>> collections = parseDatabaseDump(dbPath.toURI());
collections = updateNodeIdFirstNode(collections, nodeId);
collections = updateNodeIdInputs(collections, nodeId);
for (Map.Entry<String, List<DBObject>> collection : collections.entrySet()) {
final String collectionName = collection.getKey();
if (mongoDatabase.getCollection(collectionName) == null) {
mongoDatabase.createCollection(collectionName, new BasicDBObject());
}
final DBCollection mongoCollection = mongoDatabase.getCollection(collectionName);
if (!collection.getValue().isEmpty()) {
mongoCollection.insert(collection.getValue());
}
}
}
use of com.mongodb.DBCollection in project GNS by MobilityFirst.
the class MongoRecords method doUpdate.
private void doUpdate(String collectionName, String guid, BasicDBObject updates) throws FailedDBOperationException {
String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
DBCollection collection = db.getCollection(collectionName);
BasicDBObject query = new BasicDBObject(primaryKey, guid);
if (updates.keySet().size() > 0) {
long startTime = System.currentTimeMillis();
try {
collection.update(query, new BasicDBObject("$set", updates));
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.SEVERE, "{0} doUpdate failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, updates.toString(), "Original mongo exception:" + e.getMessage());
}
DelayProfiler.updateDelay("mongoSetUpdate", startTime);
long finishTime = System.currentTimeMillis();
if (finishTime - startTime > 10) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} Long latency mongoUpdate {1}", new Object[] { dbName, (finishTime - startTime) });
}
}
}
use of com.mongodb.DBCollection in project GNS by MobilityFirst.
the class MongoRecords method selectRecords.
private MongoRecordCursor selectRecords(String collectionName, ColumnField valuesMapField, String key, Object value, boolean explain) throws FailedDBOperationException {
db.requestEnsureConnection();
DBCollection collection = db.getCollection(collectionName);
// note that if the value of the key in the database is a list (which it is) this
// query will find all records where the value (a list) *contains* an element whose value is the value
//
//FROM MONGO DOC: Match an Array Element
//Equality matches can specify a single element in the array to match. These specifications match
//if the array contains at least one element with the specified value.
//In the following example, the query matches all documents where the value of the field tags is
//an array that contains 'fruit' as one of its elements:
//db.inventory.find( { tags: 'fruit' } )
String fieldName = valuesMapField.getName() + "." + key;
BasicDBObject query = new BasicDBObject(fieldName, value);
//System.out.println("***GNSProtocol.QUERY.toString()***: " + query.toString());
DBCursor cursor = null;
try {
cursor = collection.find(query);
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} selectRecords 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.DBCollection in project GNS by MobilityFirst.
the class MongoRecords method bulkUpdate.
/**
*
* @param collectionName
* @param values
* @throws FailedDBOperationException
* @throws RecordExistsException
*/
public void bulkUpdate(String collectionName, Map<String, JSONObject> values) throws FailedDBOperationException, RecordExistsException {
//String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
DBCollection collection = db.getCollection(collectionName);
String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
db.requestEnsureConnection();
BulkWriteOperation unordered = collection.initializeUnorderedBulkOperation();
for (Map.Entry<String, JSONObject> entry : values.entrySet()) {
BasicDBObject query = new BasicDBObject(primaryKey, entry.getKey());
JSONObject value = entry.getValue();
if (value != null) {
DBObject document;
try {
document = (DBObject) JSON.parse(value.toString());
} catch (Exception e) {
throw new FailedDBOperationException(collectionName, "bulkUpdate", "Unable to parse json" + e.getMessage());
}
unordered.find(query).upsert().replaceOne(document);
} else {
unordered.find(query).removeOne();
}
}
// Maybe check the result?
BulkWriteResult result = unordered.execute();
}
use of com.mongodb.DBCollection in project GNS by MobilityFirst.
the class MongoRecords method contains.
@Override
public boolean contains(String collectionName, String guid) throws FailedDBOperationException {
db.requestStart();
try {
String primaryKey = mongoCollectionSpecs.getCollectionSpec(collectionName).getPrimaryKey().getName();
db.requestEnsureConnection();
DBCollection collection = db.getCollection(collectionName);
BasicDBObject query = new BasicDBObject(primaryKey, guid);
DBCursor cursor = collection.find(query);
return cursor.hasNext();
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} contains failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
} finally {
db.requestDone();
}
}
Aggregations