Search in sources :

Example 6 with DBObject

use of com.mongodb.DBObject in project jetty.project by eclipse.

the class MongoSessionDataStore method doGetExpired.

/** 
     * @see org.eclipse.jetty.server.session.SessionDataStore#getExpired(Set)
     */
@Override
public Set<String> doGetExpired(Set<String> candidates) {
    long now = System.currentTimeMillis();
    long upperBound = now;
    Set<String> expiredSessions = new HashSet<>();
    //firstly ask mongo to verify if these candidate ids have expired - all of
    //these candidates will be for our node
    BasicDBObject query = new BasicDBObject();
    query.append(__ID, new BasicDBObject("$in", candidates));
    query.append(__EXPIRY, new BasicDBObject("$gt", 0).append("$lt", upperBound));
    DBCursor verifiedExpiredSessions = null;
    try {
        verifiedExpiredSessions = _dbSessions.find(query, new BasicDBObject(__ID, 1));
        for (DBObject session : verifiedExpiredSessions) {
            String id = (String) session.get(__ID);
            if (LOG.isDebugEnabled())
                LOG.debug("{} Mongo confirmed expired session {}", _context, id);
            expiredSessions.add(id);
        }
    } finally {
        if (verifiedExpiredSessions != null)
            verifiedExpiredSessions.close();
    }
    //if this is our first expiry check, make sure that we only grab really old sessions
    if (_lastExpiryCheckTime <= 0)
        upperBound = (now - (3 * (1000L * _gracePeriodSec)));
    else
        upperBound = _lastExpiryCheckTime - (1000L * _gracePeriodSec);
    query = new BasicDBObject();
    BasicDBObject gt = new BasicDBObject(__EXPIRY, new BasicDBObject("$gt", 0));
    BasicDBObject lt = new BasicDBObject(__EXPIRY, new BasicDBObject("$lt", upperBound));
    BasicDBList list = new BasicDBList();
    list.add(gt);
    list.add(lt);
    query.append("and", list);
    DBCursor oldExpiredSessions = null;
    try {
        BasicDBObject bo = new BasicDBObject(__ID, 1);
        bo.append(__EXPIRY, 1);
        oldExpiredSessions = _dbSessions.find(query, bo);
        for (DBObject session : oldExpiredSessions) {
            String id = (String) session.get(__ID);
            if (LOG.isDebugEnabled())
                LOG.debug("{} Mongo found old expired session {}", _context, id + " exp=" + session.get(__EXPIRY));
            expiredSessions.add(id);
        }
    } finally {
        oldExpiredSessions.close();
    }
    return expiredSessions;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicDBList(com.mongodb.BasicDBList) DBCursor(com.mongodb.DBCursor) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) HashSet(java.util.HashSet)

Example 7 with DBObject

use of com.mongodb.DBObject in project jetty.project by eclipse.

the class MongoTest method main.

public static void main(String... args) throws Exception {
    Mongo m = new Mongo("127.0.0.1", 27017);
    DB db = m.getDB("mydb");
    Set<String> colls = db.getCollectionNames();
    System.err.println("Colls=" + colls);
    DBCollection coll = db.getCollection("testCollection");
    BasicDBObject key = new BasicDBObject("id", "1234");
    BasicDBObject sets = new BasicDBObject("name", "value");
    BasicDBObject upsert = new BasicDBObject("$set", sets);
    WriteResult result = coll.update(key, upsert, true, false);
    System.err.println(result.getLastError());
    while (coll.count() > 0) {
        DBObject docZ = coll.findOne();
        System.err.println("removing    " + docZ);
        if (docZ != null)
            coll.remove(docZ);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) WriteResult(com.mongodb.WriteResult) Mongo(com.mongodb.Mongo) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) DB(com.mongodb.DB)

Example 8 with DBObject

use of com.mongodb.DBObject in project jetty.project by eclipse.

the class SessionExpiryTest method getSessionMaxInactiveInterval.

public long getSessionMaxInactiveInterval(DBCollection sessions, String id) throws Exception {
    assertNotNull(sessions);
    assertNotNull(id);
    DBObject o = sessions.findOne(new BasicDBObject(MongoSessionDataStore.__ID, id));
    assertNotNull(o);
    Long inactiveInterval = (Long) o.get(MongoSessionDataStore.__MAX_IDLE);
    return (inactiveInterval == null ? null : inactiveInterval.longValue());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject)

Example 9 with DBObject

use of com.mongodb.DBObject 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 10 with DBObject

use of com.mongodb.DBObject 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)

Aggregations

DBObject (com.mongodb.DBObject)545 BasicDBObject (com.mongodb.BasicDBObject)386 Test (org.junit.Test)214 DBCollection (com.mongodb.DBCollection)83 YearFilterPagingRequest (org.devgateway.ocds.web.rest.controller.request.YearFilterPagingRequest)54 Aggregation (org.springframework.data.mongodb.core.aggregation.Aggregation)52 ApiOperation (io.swagger.annotations.ApiOperation)47 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)46 Aggregation.newAggregation (org.springframework.data.mongodb.core.aggregation.Aggregation.newAggregation)41 DBCursor (com.mongodb.DBCursor)40 ArrayList (java.util.ArrayList)38 HashMap (java.util.HashMap)38 List (java.util.List)31 CustomProjectionOperation (org.devgateway.toolkit.persistence.mongo.aggregate.CustomProjectionOperation)31 Map (java.util.Map)26 ObjectId (org.bson.types.ObjectId)26 BasicDBList (com.mongodb.BasicDBList)24 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)20 BSONObject (org.bson.BSONObject)19 MongoException (com.mongodb.MongoException)18