Search in sources :

Example 16 with BasicDBList

use of com.mongodb.BasicDBList in project mongomvcc by igd-geo.

the class MongoDBVBranch method makeQueryObject.

private Map<String, Object> makeQueryObject() {
    BasicDBList l = new BasicDBList();
    String lba = MongoDBConstants.LIFETIME + "." + getRootCid();
    String iba = MongoDBConstants.LIFETIME + ".i" + getRootCid();
    //(1) check if there is information about the document's insertion
    //    available. if not just include this object.
    //    TODO this condition must be removed once we know the whole branching history
    l.add(new BasicDBObject(iba, new BasicDBObject("$exists", false)));
    if (!CompatibilityHelper.supportsAnd(getDB())) {
        //(2) check if the object has been deleted after this commit.
        //    we use a $not here, so the query will also return 'true' if
        //    the attribute is not set.
        l.add(new BasicDBObject(lba, new BasicDBObject("$not", new BasicDBObject("$lte", getHead()))));
    } else {
        BasicDBList l2 = new BasicDBList();
        //(2) check if the object has been inserted in this commit or later
        l2.add(new BasicDBObject(iba, new BasicDBObject("$lte", getHead())));
        //(3) check if the object has been deleted after this commit.
        //    we use a $not here, so the query will also return 'true' if
        //    the attribute is not set.
        l2.add(new BasicDBObject(lba, new BasicDBObject("$not", new BasicDBObject("$lte", getHead()))));
        l.add(new BasicDBObject("$and", l2));
    }
    return Collections.unmodifiableMap(new BasicDBObject("$or", l));
}
Also used : BasicDBList(com.mongodb.BasicDBList) BasicDBObject(com.mongodb.BasicDBObject)

Example 17 with BasicDBList

use of com.mongodb.BasicDBList in project Mycat-Server by MyCATApache.

the class MongoData method setGrouyBy.

public void setGrouyBy(DBObject gb) {
    this.groupby = gb;
    this.type = true;
    if (gb instanceof BasicDBList) {
        Object gb2 = ((BasicDBList) gb).get(0);
        if (gb2 instanceof DBObject) {
            for (String field : ((DBObject) gb2).keySet()) {
                Object val = ((DBObject) gb2).get(field);
                setField(field, getObjectToType(val));
            }
        }
    }
}
Also used : BasicDBList(com.mongodb.BasicDBList) DBObject(com.mongodb.DBObject) DBObject(com.mongodb.DBObject)

Example 18 with BasicDBList

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

the class MongodbSerializerTest method dblist.

public static BasicDBList dblist(Object... contents) {
    BasicDBList list = new BasicDBList();
    list.addAll(Arrays.asList(contents));
    return list;
}
Also used : BasicDBList(com.mongodb.BasicDBList)

Example 19 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 20 with BasicDBList

use of com.mongodb.BasicDBList in project mongo-hadoop by mongodb.

the class MongoRecordReaderTest method testGetCurrentKey.

@Test
public void testGetCurrentKey() throws Exception {
    MongoClient client = new MongoClient("localhost", 27017);
    MongoClientURI uri = new MongoClientURIBuilder().collection("mongo_hadoop", "mongo_record_reader_test").build();
    DBCollection collection = client.getDB(uri.getDatabase()).getCollection(uri.getCollection());
    collection.drop();
    BasicDBList colors = new BasicDBList() {

        {
            add(new BasicBSONObject("red", 255));
            add(new BasicBSONObject("blue", 255));
            add(new BasicBSONObject("green", 0));
        }
    };
    collection.insert(new BasicDBObject("_id", 0).append("address", new BasicDBObject("street", "foo street")).append("colors", colors));
    // Default case: "_id" is used as inputKey.
    MongoInputSplit split = new MongoInputSplit();
    split.setInputURI(uri);
    MongoRecordReader reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), 0);
    // Use a nested field as inputKey.
    split = new MongoInputSplit();
    split.setInputURI(uri);
    split.setKeyField("address.street");
    reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), "foo street");
    // Use a key within an array as the inputKey.
    split = new MongoInputSplit();
    split.setInputURI(uri);
    split.setKeyField("colors.1");
    reader = new MongoRecordReader(split);
    assertTrue(reader.nextKeyValue());
    assertEquals(reader.getCurrentKey(), new BasicBSONObject("blue", 255));
}
Also used : MongoClient(com.mongodb.MongoClient) DBCollection(com.mongodb.DBCollection) BasicDBList(com.mongodb.BasicDBList) BasicBSONObject(org.bson.BasicBSONObject) BasicDBObject(com.mongodb.BasicDBObject) MongoInputSplit(com.mongodb.hadoop.input.MongoInputSplit) MongoClientURIBuilder(com.mongodb.hadoop.util.MongoClientURIBuilder) MongoRecordReader(com.mongodb.hadoop.input.MongoRecordReader) MongoClientURI(com.mongodb.MongoClientURI) 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