Search in sources :

Example 41 with BSONObject

use of org.bson.BSONObject in project Mycat-Server by MyCATApache.

the class SequoiaSQLParser method InsertData.

private int InsertData(SQLInsertStatement state) {
    if (state.getValues().getValues().size() == 0) {
        throw new RuntimeException("number of  columns error");
    }
    if (state.getValues().getValues().size() != state.getColumns().size()) {
        throw new RuntimeException("number of values and columns have to match");
    }
    SQLTableSource table = state.getTableSource();
    BSONObject o = new BasicBSONObject();
    int i = 0;
    for (SQLExpr col : state.getColumns()) {
        o.put(getFieldName2(col), getExpValue(state.getValues().getValues().get(i)));
        i++;
    }
    DBCollection coll = this._db.getCollection(table.toString());
    //coll.insert(new DBObject[] { o });
    coll.insert(o);
    return 1;
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) DBCollection(com.sequoiadb.base.DBCollection) BasicBSONObject(org.bson.BasicBSONObject) BSONObject(org.bson.BSONObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 42 with BSONObject

use of org.bson.BSONObject 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 43 with BSONObject

use of org.bson.BSONObject in project v7files by thiloplanz.

the class V7GridFS method insertContents.

void insertContents(DBObject metaData, ContentPointer newContents) throws IOException {
    String filename = (String) metaData.get("filename");
    String contentType = (String) metaData.get("contentType");
    Object fileId = metaData.get("_id");
    if (newContents != null) {
        BSONObject newContent = storage.updateBackRefs(newContents, fileId, filename, contentType);
        metaData.removeField("sha");
        metaData.removeField("length");
        metaData.removeField("in");
        metaData.putAll(newContent);
    }
    insertMetaData(metaData);
}
Also used : BSONObject(org.bson.BSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject)

Example 44 with BSONObject

use of org.bson.BSONObject in project v7files by thiloplanz.

the class V7GridFS method updateContents.

private void updateContents(DBObject metaData, InputStream contents) throws IOException {
    Object fileId = metaData.get("_id");
    ContentPointer oldContents = getContentPointer(metaData);
    String filename = (String) metaData.get("filename");
    String contentType = (String) metaData.get("contentType");
    BSONObject newContent = storage.insertContentsAndBackRefs(contents, fileId, filename, contentType);
    // check if it has changed
    ContentPointer newContents = getContentPointer(newContent);
    if (newContents.contentEquals(oldContents))
        return;
    metaData.removeField("sha");
    metaData.removeField("length");
    metaData.removeField("in");
    metaData.putAll(newContent);
    updateMetaData(metaData);
}
Also used : ContentPointer(v7db.files.spi.ContentPointer) BSONObject(org.bson.BSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject)

Example 45 with BSONObject

use of org.bson.BSONObject in project v7files by thiloplanz.

the class V7GridFS method updateContents.

private void updateContents(DBObject metaData, byte[] contents, int offset, int len) throws IOException {
    Object fileId = metaData.get("_id");
    ContentPointer oldContents = getContentPointer(metaData);
    String filename = (String) metaData.get("filename");
    String contentType = (String) metaData.get("contentType");
    // for up to 55 bytes, storing the complete file inline
    // takes less space than just storing the SHA-1 and length
    // 20 (SHA-1) + 1 (sha - in) + 6 (length) + 4 (int32) + 2*12
    // (ObjectId back-references)
    BSONObject newContent = storage.inlineOrInsertContentsAndBackRefs(55, contents, offset, len, fileId, filename, contentType);
    // check if it has changed
    ContentPointer newContents = getContentPointer(newContent);
    if (newContents.contentEquals(oldContents))
        return;
    metaData.removeField("sha");
    metaData.removeField("length");
    metaData.removeField("in");
    metaData.putAll(newContent);
    updateMetaData(metaData);
}
Also used : ContentPointer(v7db.files.spi.ContentPointer) BSONObject(org.bson.BSONObject) BasicDBObject(com.mongodb.BasicDBObject) BSONObject(org.bson.BSONObject) DBObject(com.mongodb.DBObject)

Aggregations

BSONObject (org.bson.BSONObject)66 BasicBSONObject (org.bson.BasicBSONObject)31 BasicDBObject (com.mongodb.BasicDBObject)19 DBObject (com.mongodb.DBObject)10 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)6 LazyBSONObject (org.bson.LazyBSONObject)5 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)4 BSONFileSplit (com.mongodb.hadoop.input.BSONFileSplit)4 BSONWritable (com.mongodb.hadoop.io.BSONWritable)4 DBCollection (com.sequoiadb.base.DBCollection)4 Map (java.util.Map)4 ObjectId (org.bson.types.ObjectId)4 DBRef (com.mongodb.DBRef)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Configuration (org.apache.hadoop.conf.Configuration)3 Tuple (org.apache.pig.data.Tuple)3