Search in sources :

Example 31 with BasicDBList

use of com.mongodb.BasicDBList in project querydsl by querydsl.

the class MongodbSerializer method visit.

@SuppressWarnings("unchecked")
@Override
public Object visit(Operation<?> expr, Void context) {
    Operator op = expr.getOperator();
    if (op == Ops.EQ) {
        if (expr.getArg(0) instanceof Operation) {
            Operation<?> lhs = (Operation<?>) expr.getArg(0);
            if (lhs.getOperator() == Ops.COL_SIZE || lhs.getOperator() == Ops.ARRAY_SIZE) {
                return asDBObject(asDBKey(lhs, 0), asDBObject("$size", asDBValue(expr, 1)));
            } else {
                throw new UnsupportedOperationException("Illegal operation " + expr);
            }
        } else if (expr.getArg(0) instanceof Path) {
            Path<?> path = (Path<?>) expr.getArg(0);
            Constant<?> constant = (Constant<?>) expr.getArg(1);
            return asDBObject(asDBKey(expr, 0), convert(path, constant));
        }
    } else if (op == Ops.STRING_IS_EMPTY) {
        return asDBObject(asDBKey(expr, 0), "");
    } else if (op == Ops.AND) {
        BSONObject lhs = (BSONObject) handle(expr.getArg(0));
        BSONObject rhs = (BSONObject) handle(expr.getArg(1));
        if (Sets.intersection(lhs.keySet(), rhs.keySet()).isEmpty()) {
            lhs.putAll(rhs);
            return lhs;
        } else {
            BasicDBList list = new BasicDBList();
            list.add(handle(expr.getArg(0)));
            list.add(handle(expr.getArg(1)));
            return asDBObject("$and", list);
        }
    } else if (op == Ops.NOT) {
        //Handle the not's child
        Operation<?> subOperation = (Operation<?>) expr.getArg(0);
        Operator subOp = subOperation.getOperator();
        if (subOp == Ops.IN) {
            return visit(ExpressionUtils.operation(Boolean.class, Ops.NOT_IN, subOperation.getArg(0), subOperation.getArg(1)), context);
        } else {
            BasicDBObject arg = (BasicDBObject) handle(expr.getArg(0));
            return negate(arg);
        }
    } else if (op == Ops.OR) {
        BasicDBList list = new BasicDBList();
        list.add(handle(expr.getArg(0)));
        list.add(handle(expr.getArg(1)));
        return asDBObject("$or", list);
    } else if (op == Ops.NE) {
        Path<?> path = (Path<?>) expr.getArg(0);
        Constant<?> constant = (Constant<?>) expr.getArg(1);
        return asDBObject(asDBKey(expr, 0), asDBObject("$ne", convert(path, constant)));
    } else if (op == Ops.STARTS_WITH) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1)));
    } else if (op == Ops.STARTS_WITH_IC) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1), Pattern.CASE_INSENSITIVE));
    } else if (op == Ops.ENDS_WITH) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(regexValue(expr, 1) + "$"));
    } else if (op == Ops.ENDS_WITH_IC) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE));
    } else if (op == Ops.EQ_IGNORE_CASE) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile("^" + regexValue(expr, 1) + "$", Pattern.CASE_INSENSITIVE));
    } else if (op == Ops.STRING_CONTAINS) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(".*" + regexValue(expr, 1) + ".*"));
    } else if (op == Ops.STRING_CONTAINS_IC) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(".*" + regexValue(expr, 1) + ".*", Pattern.CASE_INSENSITIVE));
    } else if (op == Ops.MATCHES) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(asDBValue(expr, 1).toString()));
    } else if (op == Ops.MATCHES_IC) {
        return asDBObject(asDBKey(expr, 0), Pattern.compile(asDBValue(expr, 1).toString(), Pattern.CASE_INSENSITIVE));
    } else if (op == Ops.LIKE) {
        String regex = ExpressionUtils.likeToRegex((Expression) expr.getArg(1)).toString();
        return asDBObject(asDBKey(expr, 0), Pattern.compile(regex));
    } else if (op == Ops.BETWEEN) {
        BasicDBObject value = new BasicDBObject("$gte", asDBValue(expr, 1));
        value.append("$lte", asDBValue(expr, 2));
        return asDBObject(asDBKey(expr, 0), value);
    } else if (op == Ops.IN) {
        int constIndex = 0;
        int exprIndex = 1;
        if (expr.getArg(1) instanceof Constant<?>) {
            constIndex = 1;
            exprIndex = 0;
        }
        if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) {
            //guarded by previous check
            @SuppressWarnings("unchecked") Collection<?> values = ((Constant<? extends Collection<?>>) expr.getArg(constIndex)).getConstant();
            return asDBObject(asDBKey(expr, exprIndex), asDBObject("$in", values.toArray()));
        } else {
            Path<?> path = (Path<?>) expr.getArg(exprIndex);
            Constant<?> constant = (Constant<?>) expr.getArg(constIndex);
            return asDBObject(asDBKey(expr, exprIndex), convert(path, constant));
        }
    } else if (op == Ops.NOT_IN) {
        int constIndex = 0;
        int exprIndex = 1;
        if (expr.getArg(1) instanceof Constant<?>) {
            constIndex = 1;
            exprIndex = 0;
        }
        if (Collection.class.isAssignableFrom(expr.getArg(constIndex).getType())) {
            //guarded by previous check
            @SuppressWarnings("unchecked") Collection<?> values = ((Constant<? extends Collection<?>>) expr.getArg(constIndex)).getConstant();
            return asDBObject(asDBKey(expr, exprIndex), asDBObject("$nin", values.toArray()));
        } else {
            Path<?> path = (Path<?>) expr.getArg(exprIndex);
            Constant<?> constant = (Constant<?>) expr.getArg(constIndex);
            return asDBObject(asDBKey(expr, exprIndex), asDBObject("$ne", convert(path, constant)));
        }
    } else if (op == Ops.COL_IS_EMPTY) {
        BasicDBList list = new BasicDBList();
        list.add(asDBObject(asDBKey(expr, 0), new BasicDBList()));
        list.add(asDBObject(asDBKey(expr, 0), asDBObject("$exists", false)));
        return asDBObject("$or", list);
    } else if (op == Ops.LT) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$lt", asDBValue(expr, 1)));
    } else if (op == Ops.GT) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$gt", asDBValue(expr, 1)));
    } else if (op == Ops.LOE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$lte", asDBValue(expr, 1)));
    } else if (op == Ops.GOE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$gte", asDBValue(expr, 1)));
    } else if (op == Ops.IS_NULL) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$exists", false));
    } else if (op == Ops.IS_NOT_NULL) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$exists", true));
    } else if (op == Ops.CONTAINS_KEY) {
        Path<?> path = (Path<?>) expr.getArg(0);
        Expression<?> key = expr.getArg(1);
        return asDBObject(visit(path, context) + "." + key.toString(), asDBObject("$exists", true));
    } else if (op == MongodbOps.NEAR) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$near", asDBValue(expr, 1)));
    } else if (op == MongodbOps.NEAR_SPHERE) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$nearSphere", asDBValue(expr, 1)));
    } else if (op == MongodbOps.ELEM_MATCH) {
        return asDBObject(asDBKey(expr, 0), asDBObject("$elemMatch", asDBValue(expr, 1)));
    }
    throw new UnsupportedOperationException("Illegal operation " + expr);
}
Also used : BSONObject(org.bson.BSONObject) BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) Collection(java.util.Collection)

Example 32 with BasicDBList

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

the class LegacyMongoIndexRangeService method findAll.

@Override
public SortedSet<IndexRange> findAll() {
    final BasicDBList subQueries = new BasicDBList();
    subQueries.add(new BasicDBObject(FIELD_INDEX, new BasicDBObject("$exists", true)));
    subQueries.add(new BasicDBObject(FIELD_START, new BasicDBObject("$exists", true)));
    final DBObject query = new BasicDBObject("$and", subQueries);
    final List<DBObject> result = query(query, COLLECTION_NAME);
    final ImmutableSortedSet.Builder<IndexRange> indexRanges = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR);
    for (DBObject dbo : result) {
        try {
            indexRanges.add(buildIndexRange(dbo));
        } catch (Exception e) {
            LOG.debug("Couldn't add index range to result set: " + dbo, e);
        }
    }
    return indexRanges.build();
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) NotFoundException(org.graylog2.database.NotFoundException)

Example 33 with BasicDBList

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

the class ClusterEventPeriodical method eventCursor.

private DBCursor<ClusterEvent> eventCursor(NodeId nodeId) {
    // Resorting to ugly MongoDB Java Client because of https://github.com/devbliss/mongojack/issues/88
    final BasicDBList consumersList = new BasicDBList();
    consumersList.add(nodeId.toString());
    final DBObject query = new BasicDBObject("consumers", new BasicDBObject("$nin", consumersList));
    return dbCollection.find(query).sort(DBSort.asc("timestamp"));
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 34 with BasicDBList

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

the class InputServiceImpl method getConvertersOfExtractor.

@SuppressWarnings("unchecked")
private List<Converter> getConvertersOfExtractor(DBObject extractor) {
    final ImmutableList.Builder<Converter> listBuilder = ImmutableList.builder();
    final BasicDBList converters = (BasicDBList) extractor.get(Extractor.FIELD_CONVERTERS);
    for (final Object element : converters) {
        final DBObject c = (BasicDBObject) element;
        try {
            listBuilder.add(ConverterFactory.factory(Converter.Type.valueOf(((String) c.get(Extractor.FIELD_CONVERTER_TYPE)).toUpperCase(Locale.ENGLISH)), (Map<String, Object>) c.get(Extractor.FIELD_CONVERTER_CONFIG)));
        } catch (ConverterFactory.NoSuchConverterException e1) {
            LOG.error("Cannot build converter from persisted data. No such converter.", e1);
        } catch (Exception e) {
            LOG.error("Cannot build converter from persisted data.", e);
        }
    }
    return listBuilder.build();
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject) ImmutableList(com.google.common.collect.ImmutableList) Converter(org.graylog2.plugin.inputs.Converter) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) ConverterFactory(org.graylog2.inputs.converters.ConverterFactory) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException)

Example 35 with BasicDBList

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

the class InputServiceImpl method getStaticFields.

@Override
public List<Map.Entry<String, String>> getStaticFields(Input input) {
    if (input.getFields().get(InputImpl.EMBEDDED_STATIC_FIELDS) == null) {
        return Collections.emptyList();
    }
    final ImmutableList.Builder<Map.Entry<String, String>> listBuilder = ImmutableList.builder();
    final BasicDBList mSF = (BasicDBList) input.getFields().get(InputImpl.EMBEDDED_STATIC_FIELDS);
    for (final Object element : mSF) {
        final DBObject ex = (BasicDBObject) element;
        try {
            final Map.Entry<String, String> staticField = Maps.immutableEntry((String) ex.get(InputImpl.FIELD_STATIC_FIELD_KEY), (String) ex.get(InputImpl.FIELD_STATIC_FIELD_VALUE));
            listBuilder.add(staticField);
        } catch (Exception e) {
            LOG.error("Cannot build static field 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) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) NoSuchInputTypeException(org.graylog2.shared.inputs.NoSuchInputTypeException) NotFoundException(org.graylog2.database.NotFoundException) ValidationException(org.graylog2.plugin.database.ValidationException)

Aggregations

BasicDBList (com.mongodb.BasicDBList)69 BasicDBObject (com.mongodb.BasicDBObject)40 DBObject (com.mongodb.DBObject)33 Test (org.junit.Test)27 Document (org.springframework.data.mongodb.core.mapping.Document)16 ArrayList (java.util.ArrayList)13 List (java.util.List)9 DBCollection (com.mongodb.DBCollection)8 Document (org.bson.Document)7 DBCursor (com.mongodb.DBCursor)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 DBRef (com.mongodb.DBRef)5 Collection (java.util.Collection)4 NotFoundException (org.graylog2.database.NotFoundException)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 LinkedHashMap (java.util.LinkedHashMap)3