Search in sources :

Example 6 with DBCollection

use of com.mongodb.DBCollection in project qi4j-sdk by Qi4j.

the class MongoMapEntityStoreMixin method applyChanges.

@Override
public void applyChanges(MapChanges changes) throws IOException {
    db.requestStart();
    final DBCollection entities = db.getCollection(collectionName);
    changes.visitMap(new MapChanger() {

        @Override
        public Writer newEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
            return new StringWriter(1000) {

                @Override
                public void close() throws IOException {
                    super.close();
                    String jsonState = toString();
                    DBObject bsonState = (DBObject) JSON.parse(jsonState);
                    BasicDBObject entity = new BasicDBObject();
                    entity.put(IDENTITY_COLUMN, ref.identity());
                    entity.put(STATE_COLUMN, bsonState);
                    entities.insert(entity, writeConcern);
                }
            };
        }

        @Override
        public Writer updateEntity(final EntityReference ref, EntityDescriptor entityDescriptor) throws IOException {
            return new StringWriter(1000) {

                @Override
                public void close() throws IOException {
                    super.close();
                    DBObject bsonState = (DBObject) JSON.parse(toString());
                    BasicDBObject entity = new BasicDBObject();
                    entity.put(IDENTITY_COLUMN, ref.identity());
                    entity.put(STATE_COLUMN, bsonState);
                    entities.update(byIdentity(ref), entity, false, false, writeConcern);
                }
            };
        }

        @Override
        public void removeEntity(EntityReference ref, EntityDescriptor entityDescriptor) throws EntityNotFoundException {
            DBObject entity = entities.findOne(byIdentity(ref));
            if (entity == null) {
                throw new EntityNotFoundException(ref);
            }
            entities.remove(entity, writeConcern);
        }
    });
    db.requestDone();
}
Also used : DBCollection(com.mongodb.DBCollection) EntityDescriptor(org.qi4j.api.entity.EntityDescriptor) BasicDBObject(com.mongodb.BasicDBObject) StringWriter(java.io.StringWriter) EntityReference(org.qi4j.api.entity.EntityReference) IOException(java.io.IOException) EntityNotFoundException(org.qi4j.spi.entitystore.EntityNotFoundException) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) StringWriter(java.io.StringWriter) Writer(java.io.Writer)

Example 7 with DBCollection

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

the class MongoSQLParser method dropTable.

private int dropTable(SQLDropTableStatement state) {
    for (SQLTableSource table : state.getTableSources()) {
        DBCollection coll = this._db.getCollection(table.toString());
        coll.drop();
    }
    return 1;
}
Also used : DBCollection(com.mongodb.DBCollection)

Example 8 with DBCollection

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

the class MongoSQLParser method UpData.

private int UpData(SQLUpdateStatement state) {
    SQLTableSource table = state.getTableSource();
    DBCollection coll = this._db.getCollection(table.toString());
    SQLExpr expr = state.getWhere();
    DBObject query = parserWhere(expr);
    BasicDBObject set = new BasicDBObject();
    for (SQLUpdateSetItem col : state.getItems()) {
        set.put(getFieldName2(col.getColumn()), getExpValue(col.getValue()));
    }
    DBObject mod = new BasicDBObject("$set", set);
    coll.updateMulti(query, mod);
    //System.out.println("changs count:"+coll.getStats().size());
    return 1;
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 9 with DBCollection

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

the class MongoDBVDatabaseBenchmark method plainOldInsertDelete.

/**
	 * Inserts a lot of documents using plain old MongoDB and then deletes them
	 */
@Test
@BenchmarkOptions(benchmarkRounds = 2, warmupRounds = 1)
public void plainOldInsertDelete() {
    plainOldInsert();
    DBCollection coll = _db.getCollection("persons");
    for (long i = 0; i < DOCUMENTS; ++i) {
        coll.remove(new BasicDBObject("_id", 1 + i));
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) Test(org.junit.Test) BenchmarkOptions(com.carrotsearch.junitbenchmarks.BenchmarkOptions)

Example 10 with DBCollection

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

the class MongoDBVDatabaseBenchmark method plainOldInsertDeleteInsertQuery.

/**
	 * Queries a lot of objects after inserting, deleting and inserting again
	 */
@Test
@BenchmarkOptions(benchmarkRounds = 2, warmupRounds = 1)
public void plainOldInsertDeleteInsertQuery() {
    plainOldInsertDeleteInsert();
    _master.commit();
    DBCollection coll = _db.getCollection("persons");
    for (DBObject o : coll.find()) {
        assertTrue(((Long) o.get("age")).longValue() >= DOCUMENTS);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) Test(org.junit.Test) BenchmarkOptions(com.carrotsearch.junitbenchmarks.BenchmarkOptions)

Aggregations

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