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);
}
}
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());
}
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;
}
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;
}
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;
}
Aggregations