Search in sources :

Example 11 with BasicDBList

use of com.mongodb.BasicDBList 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;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) DBCursor(com.mongodb.DBCursor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 12 with BasicDBList

use of com.mongodb.BasicDBList 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;
}
Also used : HashMap(java.util.HashMap) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) DBCursor(com.mongodb.DBCursor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Extractor(org.graylog2.plugin.inputs.Extractor)

Example 13 with BasicDBList

use of com.mongodb.BasicDBList in project graylog2-server by Graylog2.

the class InputServiceImpl method getExtractors.

@Override
@SuppressWarnings("unchecked")
public List<Extractor> getExtractors(Input input) {
    if (input.getFields().get(InputImpl.EMBEDDED_EXTRACTORS) == null) {
        return Collections.emptyList();
    }
    final ImmutableList.Builder<Extractor> listBuilder = ImmutableList.builder();
    final BasicDBList mEx = (BasicDBList) input.getFields().get(InputImpl.EMBEDDED_EXTRACTORS);
    for (final Object element : mEx) {
        final DBObject ex = (BasicDBObject) element;
        // SOFT MIGRATION: does this extractor have an order set? Implemented for issue: #726
        Long order = 0L;
        if (ex.containsField(Extractor.FIELD_ORDER)) {
            // mongodb driver gives us a java.lang.Long
            order = (Long) ex.get(Extractor.FIELD_ORDER);
        }
        try {
            final Extractor extractor = extractorFactory.factory((String) ex.get(Extractor.FIELD_ID), (String) ex.get(Extractor.FIELD_TITLE), order.intValue(), Extractor.CursorStrategy.valueOf(((String) ex.get(Extractor.FIELD_CURSOR_STRATEGY)).toUpperCase(Locale.ENGLISH)), Extractor.Type.valueOf(((String) ex.get(Extractor.FIELD_TYPE)).toUpperCase(Locale.ENGLISH)), (String) ex.get(Extractor.FIELD_SOURCE_FIELD), (String) ex.get(Extractor.FIELD_TARGET_FIELD), (Map<String, Object>) ex.get(Extractor.FIELD_EXTRACTOR_CONFIG), (String) ex.get(Extractor.FIELD_CREATOR_USER_ID), getConvertersOfExtractor(ex), Extractor.ConditionType.valueOf(((String) ex.get(Extractor.FIELD_CONDITION_TYPE)).toUpperCase(Locale.ENGLISH)), (String) ex.get(Extractor.FIELD_CONDITION_VALUE));
            listBuilder.add(extractor);
        } catch (Exception e) {
            LOG.error("Cannot build extractor from persisted data. Skipping.", e);
        }
    }
    return listBuilder.build();
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ImmutableList(com.google.common.collect.ImmutableList) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) Extractor(org.graylog2.plugin.inputs.Extractor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException)

Example 14 with BasicDBList

use of com.mongodb.BasicDBList in project graylog2-server by Graylog2.

the class MongoConnectionImpl method getMongoVersion.

@Nullable
private Version getMongoVersion(DB adminDb) {
    final CommandResult buildInfoResult = adminDb.command("buildInfo");
    if (buildInfoResult.ok()) {
        final BasicDBList versionArray = (BasicDBList) buildInfoResult.get("versionArray");
        if (versionArray == null || versionArray.size() < 3) {
            LOG.debug("Couldn't retrieve MongoDB version");
            return null;
        }
        final int majorVersion = (int) versionArray.get(0);
        final int minorVersion = (int) versionArray.get(1);
        final int patchVersion = (int) versionArray.get(2);
        return Version.forIntegers(majorVersion, minorVersion, patchVersion);
    } else {
        LOG.debug("Couldn't retrieve MongoDB buildInfo: {}", buildInfoResult.getErrorMessage());
        return null;
    }
}
Also used : BasicDBList(com.mongodb.BasicDBList) CommandResult(com.mongodb.CommandResult) Nullable(javax.annotation.Nullable)

Example 15 with BasicDBList

use of com.mongodb.BasicDBList in project gora by apache.

the class TestMongoStore method testFromMongoList_empty.

@Test
public void testFromMongoList_empty() throws Exception {
    MongoStore store = new MongoStore();
    String field = "myField";
    BasicDBObject emptyField = new BasicDBObject(field, new BasicDBList());
    Object item = store.fromMongoList(field, null, new BSONDecorator(emptyField), null);
    assertNotNull(item);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test)

Aggregations

BasicDBList (com.mongodb.BasicDBList)41 BasicDBObject (com.mongodb.BasicDBObject)33 DBObject (com.mongodb.DBObject)26 DBCollection (com.mongodb.DBCollection)8 Test (org.junit.Test)8 DBCursor (com.mongodb.DBCursor)6 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 NotFoundException (org.graylog2.database.NotFoundException)5 Map (java.util.Map)4 ImmutableList (com.google.common.collect.ImmutableList)3 MongoClient (com.mongodb.MongoClient)3 MongoException (com.mongodb.MongoException)3 MongoInputSplit (com.mongodb.hadoop.input.MongoInputSplit)3 List (java.util.List)3 DataBag (org.apache.pig.data.DataBag)3 Tuple (org.apache.pig.data.Tuple)3 ValidationException (org.graylog2.plugin.database.ValidationException)3 NoSuchInputTypeException (org.graylog2.shared.inputs.NoSuchInputTypeException)3 JSONObject (org.json.JSONObject)3