use of com.mongodb.DBCursor 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();
}
}
use of com.mongodb.DBCursor in project GNS by MobilityFirst.
the class MongoRecords method lookupEntireRecord.
private JSONObject lookupEntireRecord(String collectionName, String guid, boolean explain) throws RecordNotFoundException, FailedDBOperationException {
long startTime = System.currentTimeMillis();
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);
if (explain) {
System.out.println(cursor.explain().toString());
}
if (cursor.hasNext()) {
DBObject obj = cursor.next();
// arun: optimized for the common case of Map
@SuppressWarnings("unchecked") JSONObject json = obj instanceof Map ? DiskMapRecords.recursiveCopyMap((Map<String, ?>) obj) : new JSONObject(obj.toString());
// instrumentation
DelayProfiler.updateDelay("lookupEntireRecord", startTime);
// older style
int lookupTime = (int) (System.currentTimeMillis() - startTime);
if (lookupTime > 20) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} mongoLookup Long delay {1}", new Object[] { dbName, lookupTime });
}
return json;
} else {
throw new RecordNotFoundException(guid);
}
} catch (JSONException e) {
DatabaseConfig.getLogger().log(Level.WARNING, "{0} Unable to parse JSON: {1}", new Object[] { dbName, e.getMessage() });
return null;
} catch (MongoException e) {
DatabaseConfig.getLogger().log(Level.FINE, "{0} lookupEntireRecord failed: {1}", new Object[] { dbName, e.getMessage() });
throw new FailedDBOperationException(collectionName, guid, "Original mongo exception:" + e.getMessage());
} finally {
db.requestDone();
}
}
use of com.mongodb.DBCursor 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.DBCursor in project graylog2-server by Graylog2.
the class InputServiceImpl method totalExtractorCount.
@Override
public long totalExtractorCount() {
final DBObject query = new BasicDBObject(InputImpl.EMBEDDED_EXTRACTORS, new BasicDBObject("$exists", true));
final DBCursor inputs = collection(InputImpl.class).find(query, new BasicDBObject(InputImpl.EMBEDDED_EXTRACTORS, 1));
long extractorsCount = 0;
for (DBObject input : inputs) {
final BasicDBList extractors = (BasicDBList) input.get(InputImpl.EMBEDDED_EXTRACTORS);
extractorsCount += extractors.size();
}
return extractorsCount;
}
use of com.mongodb.DBCursor in project graylog2-server by Graylog2.
the class InputServiceImpl method totalExtractorCountByType.
@Override
public Map<Extractor.Type, Long> totalExtractorCountByType() {
final Map<Extractor.Type, Long> extractorsCountByType = new HashMap<>();
final DBObject query = new BasicDBObject(InputImpl.EMBEDDED_EXTRACTORS, new BasicDBObject("$exists", true));
final DBCursor inputs = collection(InputImpl.class).find(query, new BasicDBObject(InputImpl.EMBEDDED_EXTRACTORS, 1));
for (DBObject input : inputs) {
final BasicDBList extractors = (BasicDBList) input.get(InputImpl.EMBEDDED_EXTRACTORS);
for (Object fuckYouMongoDb : extractors) {
final DBObject extractor = (DBObject) fuckYouMongoDb;
final Extractor.Type type = Extractor.Type.fuzzyValueOf(((String) extractor.get(Extractor.FIELD_TYPE)));
if (type != null) {
final Long oldValue = extractorsCountByType.get(type);
final Long newValue = (oldValue == null) ? 1 : oldValue + 1;
extractorsCountByType.put(type, newValue);
}
}
}
return extractorsCountByType;
}
Aggregations