Search in sources :

Example 91 with DBCollection

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

the class MongoDBVDatabaseBenchmark method plainOldInsert.

private void plainOldInsert(int offset) {
    DBCollection coll = _db.getCollection("persons");
    for (long i = 0; i < DOCUMENTS; ++i) {
        BasicDBObject obj = new BasicDBObject();
        obj.put("name", String.valueOf(i));
        obj.put("age", i + offset);
        obj.put("_id", 1 + i);
        coll.insert(obj);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject)

Example 92 with DBCollection

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

the class MongoDBVMaintenanceTest method pruneUnreferencedDocuments.

/**
	 * Tests if unreferenced documents can be deleted
	 * @throws Exception if something goes wrong
	 */
@Test
public void pruneUnreferencedDocuments() throws Exception {
    Object[] ud = makeUnreferencedDocuments();
    long[] unreferenced = (long[]) ud[0];
    long count = _db.getMaintenance().pruneUnreferencedDocuments("persons", 0, TimeUnit.MILLISECONDS);
    assertEquals(3, count);
    Mongo mongo = new Mongo();
    DB db = mongo.getDB("mvcctest");
    DBCollection personsColl = db.getCollection("persons");
    for (int i = 0; i < 3; ++i) {
        DBCursor cursor = personsColl.find(new BasicDBObject(MongoDBConstants.ID, unreferenced[i]));
        assertEquals(0, cursor.size());
    }
    assertEquals(3, personsColl.find().size());
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) Mongo(com.mongodb.Mongo) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB) Test(org.junit.Test)

Example 93 with DBCollection

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

the class MongoSQLParser 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();
    BasicDBObject o = new BasicDBObject();
    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 });
    return 1;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 94 with DBCollection

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

the class MongoSQLParser method query.

public MongoData query() throws MongoSQLException {
    if (!(statement instanceof SQLSelectStatement)) {
        //return null;
        throw new IllegalArgumentException("not a query sql statement");
    }
    MongoData mongo = new MongoData();
    DBCursor c = null;
    SQLSelectStatement selectStmt = (SQLSelectStatement) statement;
    SQLSelectQuery sqlSelectQuery = selectStmt.getSelect().getQuery();
    int icount = 0;
    if (sqlSelectQuery instanceof MySqlSelectQueryBlock) {
        MySqlSelectQueryBlock mysqlSelectQuery = (MySqlSelectQueryBlock) selectStmt.getSelect().getQuery();
        BasicDBObject fields = new BasicDBObject();
        //显示的字段
        for (SQLSelectItem item : mysqlSelectQuery.getSelectList()) {
            //System.out.println(item.toString());
            if (!(item.getExpr() instanceof SQLAllColumnExpr)) {
                if (item.getExpr() instanceof SQLAggregateExpr) {
                    SQLAggregateExpr expr = (SQLAggregateExpr) item.getExpr();
                    if (expr.getMethodName().equals("COUNT")) {
                        icount = 1;
                        mongo.setField(getExprFieldName(expr), Types.BIGINT);
                    }
                    fields.put(getExprFieldName(expr), Integer.valueOf(1));
                } else {
                    fields.put(getFieldName(item), Integer.valueOf(1));
                }
            }
        }
        //表名
        SQLTableSource table = mysqlSelectQuery.getFrom();
        DBCollection coll = this._db.getCollection(table.toString());
        mongo.setTable(table.toString());
        SQLExpr expr = mysqlSelectQuery.getWhere();
        DBObject query = parserWhere(expr);
        //System.out.println(query);
        SQLSelectGroupByClause groupby = mysqlSelectQuery.getGroupBy();
        BasicDBObject gbkey = new BasicDBObject();
        if (groupby != null) {
            for (SQLExpr gbexpr : groupby.getItems()) {
                if (gbexpr instanceof SQLIdentifierExpr) {
                    String name = ((SQLIdentifierExpr) gbexpr).getName();
                    gbkey.put(name, Integer.valueOf(1));
                }
            }
            icount = 2;
        }
        int limitoff = 0;
        int limitnum = 0;
        if (mysqlSelectQuery.getLimit() != null) {
            limitoff = getSQLExprToInt(mysqlSelectQuery.getLimit().getOffset());
            limitnum = getSQLExprToInt(mysqlSelectQuery.getLimit().getRowCount());
        }
        if (icount == 1) {
            mongo.setCount(coll.count(query));
        } else if (icount == 2) {
            BasicDBObject initial = new BasicDBObject();
            initial.put("num", 0);
            String reduce = "function (obj, prev) { " + "  prev.num++}";
            mongo.setGrouyBy(coll.group(gbkey, query, initial, reduce));
        } else {
            if ((limitoff > 0) || (limitnum > 0)) {
                c = coll.find(query, fields).skip(limitoff).limit(limitnum);
            } else {
                c = coll.find(query, fields);
            }
            SQLOrderBy orderby = mysqlSelectQuery.getOrderBy();
            if (orderby != null) {
                BasicDBObject order = new BasicDBObject();
                for (int i = 0; i < orderby.getItems().size(); i++) {
                    SQLSelectOrderByItem orderitem = orderby.getItems().get(i);
                    order.put(orderitem.getExpr().toString(), Integer.valueOf(getSQLExprToAsc(orderitem.getType())));
                }
                c.sort(order);
            // System.out.println(order);
            }
        }
        mongo.setCursor(c);
    }
    return mongo;
}
Also used : MySqlSelectQueryBlock(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) BasicDBObject(com.mongodb.BasicDBObject) DBCollection(com.mongodb.DBCollection) DBCursor(com.mongodb.DBCursor)

Example 95 with DBCollection

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

the class MongoSQLParser method DeleteDate.

private int DeleteDate(SQLDeleteStatement state) {
    SQLTableSource table = state.getTableSource();
    DBCollection coll = this._db.getCollection(table.toString());
    SQLExpr expr = state.getWhere();
    if (expr == null) {
        throw new RuntimeException("not where of sql");
    }
    DBObject query = parserWhere(expr);
    coll.remove(query);
    return 1;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Aggregations

DBCollection (com.mongodb.DBCollection)174 DBObject (com.mongodb.DBObject)92 BasicDBObject (com.mongodb.BasicDBObject)91 Test (org.junit.Test)70 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)29 DBCursor (com.mongodb.DBCursor)25 DB (com.mongodb.DB)23 MongoException (com.mongodb.MongoException)22 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)17 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 MongoClientURI (com.mongodb.MongoClientURI)11 List (java.util.List)11 Map (java.util.Map)11 QueryBuilder (com.mongodb.QueryBuilder)10 HashMap (java.util.HashMap)10 Stopwatch (com.google.common.base.Stopwatch)9 WriteResult (com.mongodb.WriteResult)9 MongoClient (com.mongodb.MongoClient)8 IOException (java.io.IOException)8