Example usage for com.mongodb BasicDBObjectBuilder get

List of usage examples for com.mongodb BasicDBObjectBuilder get

Introduction

In this page you can find the example usage for com.mongodb BasicDBObjectBuilder get.

Prototype

public DBObject get() 

Source Link

Document

Gets the top level document.

Usage

From source file:de.flapdoodle.mongoom.datastore.Caps.java

License:Apache License

public static void ensureCaps(DB db, String collectionName, ICollectionCap capCollection) {

    if (capCollection != null) {
        long count = capCollection.count();
        long size = capCollection.size();

        if (size == 0)
            throw new ObjectMapperException("Size == 0");
        if ((size < Long.MAX_VALUE) && (count < Long.MAX_VALUE)) {
            try {
                db.requestStart();//from   w  w w . j  a va  2 s .  c o  m
                BasicDBObjectBuilder dbCapOpts = BasicDBObjectBuilder.start("capped", true);
                dbCapOpts.add("size", size);
                if (count > 0)
                    dbCapOpts.add("max", count);
                DBCollection dbColl = db.getCollection(collectionName);

                if (db.getCollectionNames().contains(collectionName)) {
                    DBObject dbResult = db
                            .command(BasicDBObjectBuilder.start("collstats", collectionName).get());
                    if (dbResult.containsField("capped")) {
                        // TODO: check the cap options.
                        _logger.warning(
                                "DBCollection already exists is cap'd already; doing nothing. " + dbResult);
                    } else {
                        _logger.warning("DBCollection already exists with same name(" + collectionName
                                + ") and is not cap'd; not creating cap'd version!");
                    }
                } else {
                    db.createCollection(collectionName, dbCapOpts.get());
                    _logger.info(
                            "Created cap'd DBCollection (" + collectionName + ") with opts " + capCollection);
                }
            } finally {
                Errors.checkError(db, Operation.Insert);
                db.requestDone();
            }
        }
    }
}

From source file:de.flapdoodle.mongoom.datastore.Indexes.java

License:Apache License

public static void ensureIndex(DB db, IndexDef index, String collectionName) {

    // public <T> void ensureIndex(Class<T> clazz, String name,
    // Set<IndexFieldDef> defs, boolean unique,
    // boolean dropDupsOnCreate) {
    BasicDBObjectBuilder keys = BasicDBObjectBuilder.start();
    BasicDBObjectBuilder keyOpts = null;
    List<FieldIndex> indexSorted = Lists.newArrayList(index.fields());
    Collections.sort(indexSorted, new Comparator<FieldIndex>() {
        @Override/*from  w ww  .  j av a 2 s  .c o m*/
        public int compare(FieldIndex o1, FieldIndex o2) {
            if (o1.priority() == o2.priority())
                return 0;
            if (o1.priority() < o2.priority())
                return 1;
            return -1;
        }
    });

    for (FieldIndex def : indexSorted) {
        String fieldName = def.name();
        Direction dir = def.direction();
        if (dir == Direction.BOTH)
            keys.add(fieldName, 1).add(fieldName, -1);
        else
            keys.add(fieldName, (dir == Direction.ASC) ? 1 : -1);
    }

    String name = index.name();

    if (name != null && !name.isEmpty()) {
        if (keyOpts == null)
            keyOpts = new BasicDBObjectBuilder();
        keyOpts.add("name", name);
    }
    if (index.unique()) {
        if (keyOpts == null)
            keyOpts = new BasicDBObjectBuilder();
        keyOpts.add("unique", true);
        if (index.dropDups())
            keyOpts.add("dropDups", true);
    }
    if (index.sparse()) {
        if (keyOpts == null)
            keyOpts = new BasicDBObjectBuilder();
        keyOpts.add("sparse", true);
    }

    try {
        db.requestStart();
        DBCollection dbColl = db.getCollection(collectionName);
        DBObject indexKeys = keys.get();
        //         DatastoreImpl._logger.info("Ensuring index for " + dbColl.getName() + "." + index + " with keys " + indexKeys);
        if (keyOpts == null) {
            _logger.info("Ensuring index for " + dbColl.getName() + "." + index + " with keys " + indexKeys);
            dbColl.ensureIndex(indexKeys);
        } else {
            DBObject options = keyOpts.get();
            _logger.info("Ensuring index for " + dbColl.getName() + "." + index + " with keys " + indexKeys
                    + " and opts " + options);
            dbColl.ensureIndex(indexKeys, options);
        }
    } finally {
        Errors.checkError(db, Operation.Insert);
        db.requestDone();
    }
}

From source file:es.bsc.amon.controller.EventsDBMapper.java

License:Open Source License

public String getString(String id) {
    BasicDBObjectBuilder b = BasicDBObjectBuilder.start();
    b.add(_ID, new ObjectId(id));

    DBCursor cur = colEvents.find(b.get());
    if (cur.hasNext()) {
        return cur.next().toString();
    }/*from w  w w . j  ava  2 s  .c  om*/
    return null;
}

From source file:es.bsc.amon.controller.EventsDBMapper.java

License:Open Source License

public void delete(String id) {
    BasicDBObjectBuilder q = BasicDBObjectBuilder.start();
    q.add(_ID, new ObjectId(id));
    colEvents.remove(q.get());
}

From source file:es.bsc.amon.controller.EventsDBMapper.java

License:Open Source License

public ObjectNode markAsFinished(String id) {
    BasicDBObjectBuilder q = BasicDBObjectBuilder.start();
    q.add(_ID, new ObjectId(id));
    long timestamp = Calendar.getInstance().getTimeInMillis();

    BasicDBObjectBuilder m = BasicDBObjectBuilder.start();
    m.add("$set", BasicDBObjectBuilder.start(ENDTIME, timestamp).get());

    colEvents.update(q.get(), m.get(), false, false);

    ObjectNode on = new ObjectNode(JsonNodeFactory.instance);
    on.put(_ID, id);//from   ww w.  j  av a2s .  c  om
    on.put(ENDTIME, timestamp);

    return on;
}

From source file:es.bsc.amon.controller.EventsDBMapper.java

License:Open Source License

public void remove(String id) {
    BasicDBObjectBuilder q = BasicDBObjectBuilder.start().add(_ID, new ObjectId(id));
    colEvents.findAndRemove(q.get());
}

From source file:essex.bigessexnew.TwitterStreamHandler.java

@Override
public void run() {
    StatusListener sl = new StatusListener() {
        @Override//from  w  w  w  .  j av  a2s . c o m
        public void onStatus(Status status) {
            /*String s = TwitterObjectFactory.getRawJSON(status);
            System.out.println(s);*/
            BasicDBObjectBuilder documentBuilder = BasicDBObjectBuilder.start();
            documentBuilder.add("content", status.getText());
            collection.insert(documentBuilder.get());

            /*DBObject dbObject = (DBObject) JSON.parse(json);
            collection.insert(dbObject);*/
            //System.out.println(json.toString());

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {

        }

        @Override
        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {

        }

        @Override
        public void onScrubGeo(long l, long l2) {

        }

        @Override
        public void onStallWarning(StallWarning stallWarning) {

        }

        @Override
        public void onException(Exception ex) {

        }
    };
    TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
    twitterStream.addListener(sl);
    twitterStream.filter(Params.getProperty("hashtag"));
}

From source file:ezbake.services.centralPurge.helpers.EzCentralPurgeServiceHelpers.java

License:Apache License

public static DBObject encodeDateTime(DateTime dateTime) {
    BasicDBObjectBuilder timeZoneBuilder = BasicDBObjectBuilder.start();

    Date date = dateTime.getDate();
    Time time = dateTime.getTime();
    TimeZone timeZone = time.getTz();

    timeZoneBuilder.add(Hour, timeZone.getHour());
    timeZoneBuilder.add(Minute, timeZone.getMinute());
    timeZoneBuilder.add(AfterUTC, timeZone.isAfterUTC());

    BasicDBObjectBuilder basicDBObjectBuilder = BasicDBObjectBuilder.start().add(Year, date.getYear())
            .add(Month, date.getMonth()).add(Day, date.getDay()).add(Hour, time.getHour())
            .add(Minute, time.getMinute()).add(Second, time.getSecond()).add(Millisecond, time.getMillisecond())
            .add(Timezone, timeZoneBuilder.get());

    return basicDBObjectBuilder.get();
}

From source file:ezbake.services.centralPurge.helpers.EzCentralPurgeServiceHelpers.java

License:Apache License

public static DBObject encodePurgeInitiationResult(PurgeInitiationResult purgeInitiationResult) {
    BasicDBList basicDBList = new BasicDBList();
    for (String uri : purgeInitiationResult.getUrisNotFound()) {
        BasicDBObject dbURI = new BasicDBObject();
        dbURI.append(URI, uri);//from w  w w .  j a  v  a 2 s  .  c  om
        basicDBList.add(dbURI);
    }

    DBObject setToBePurged = encodeURISet(purgeInitiationResult.getToBePurged());

    BasicDBObjectBuilder purgeInitiationResultBuilder = BasicDBObjectBuilder.start()
            .add(PurgeId, purgeInitiationResult.getPurgeId()).add(ToBePurged, setToBePurged)
            .add(URIsNotFoundSet, basicDBList);

    return purgeInitiationResultBuilder.get();
}

From source file:ezbake.services.centralPurge.helpers.EzCentralPurgeServiceHelpers.java

License:Apache License

public static DBObject encodePurgeState(PurgeState purgeState) {
    BasicDBObjectBuilder purgeStateBuilder = BasicDBObjectBuilder.start().add(PurgeId, purgeState.getPurgeId())
            .add(ApplicationPurgeStatus, purgeState.getPurgeStatus().getValue())
            .add(Purged, encodeURISet(purgeState.getPurged()))
            .add(NotPurgedSet, encodeURISet(purgeState.getNotPurged()))
            .add(SuggestedPollPeriord, purgeState.getSuggestedPollPeriod())
            .add(TimeStampString, encodeDateTime(purgeState.getTimeStamp()))
            .add(Cancel_Status, purgeState.getCancelStatus().getValue());

    return purgeStateBuilder.get();
}