Example usage for com.mongodb BasicDBObjectBuilder start

List of usage examples for com.mongodb BasicDBObjectBuilder start

Introduction

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

Prototype

public static BasicDBObjectBuilder start(final String key, final Object val) 

Source Link

Document

Creates a builder initialized with the given key/value.

Usage

From source file:cc.acs.mongofs.gridfs.GridFSDBFile.java

License:Apache License

byte[] getChunk(int i) {
    if (_fs == null)
        throw new RuntimeException("no gridfs!");

    DBObject chunk = _fs._chunkCollection
            .findOne(BasicDBObjectBuilder.start("files_id", _id).add("n", i).get());
    if (chunk == null)
        throw new MongoException("can't find a chunk!  file id: " + _id + " chunk: " + i);

    return (byte[]) chunk.get("data");
}

From source file:ch.windmobile.server.mongo.MongoDataSource.java

License:Open Source License

private BasicDBObject findStationJson(String stationId) throws DataSourceException {
    DBCollection stationsCollection = database.getCollection(getStationsCollectionName());
    BasicDBObject stationJson = (BasicDBObject) stationsCollection
            .findOne(BasicDBObjectBuilder.start("_id", stationId).get());
    if (stationJson != null) {
        return stationJson;
    } else {//w  ww .  j  ava2  s .  co m
        throw new DataSourceException(DataSourceException.Error.INVALID_DATA,
                "Unable to find station with id '" + stationId + "'");
    }
}

From source file:ch.windmobile.server.mongo.MongoDataSource.java

License:Open Source License

private List<BasicDBObject> getHistoricData(String stationId, DateTime lastUpdate, int duration) {
    DBCollection dataCollection = database.getCollection(getDataCollectionName(stationId));
    long startTime = lastUpdate.getMillis() - duration * 1000;
    DBObject query = BasicDBObjectBuilder
            .start("_id", BasicDBObjectBuilder.start("$gte", startTime / 1000).get()).get();

    List<BasicDBObject> datas = new ArrayList<BasicDBObject>();
    DBCursor cursor = dataCollection.find(query);
    while (cursor.hasNext()) {
        datas.add((BasicDBObject) cursor.next());

    }/*from   w w w  . j a  va  2  s .  com*/
    return datas;
}

From source file:ch.windmobile.server.mongo.MongoDataSource.java

License:Open Source License

@Override
public List<StationInfo> getStationInfoList(boolean allStation) throws DataSourceException {
    try {/*from  w ww .  j a v  a 2  s. c  o m*/
        DBCollection stations = database.getCollection(getStationsCollectionName());

        List<String> list = new ArrayList<String>();
        if (allStation == true) {
            list.add(Status.RED.value());
            list.add(Status.ORANGE.value());
            list.add(Status.GREEN.value());
        } else {
            list.add(Status.GREEN.value());
        }
        DBObject query = BasicDBObjectBuilder.start("prov", getProvider())
                .add("status", new BasicDBObject("$in", list)).get();
        DBCursor cursor = stations.find(query);

        List<StationInfo> stationInfoList = new ArrayList<StationInfo>();
        while (cursor.hasNext()) {
            try {
                BasicDBObject stationJson = (BasicDBObject) cursor.next();
                if (getStationsFilter() != null) {
                    String stationId = stationJson.getString("_id");
                    if (getStationsFilter().contains(stationId)) {
                        stationInfoList.add(createStationInfo(stationJson));
                    }
                } else {
                    stationInfoList.add(createStationInfo(stationJson));
                }
            } catch (Exception e) {
                log.warn("Station was ignored because:", e);
            }
        }

        return stationInfoList;
    } catch (Exception e) {
        throw exceptionHandler(e);
    }
}

From source file:ch.windmobile.server.social.mongodb.ChatServiceImpl.java

License:Open Source License

private DBCollection createChatCappedCollection(String collectionName) {
    DBObject options = BasicDBObjectBuilder.start("capped", true).add("size", 10000).get();
    // create the capped chat room of 10K
    return db.createCollection(collectionName, options);
}

From source file:ch.windmobile.server.social.mongodb.ChatServiceImpl.java

License:Open Source License

@Override
public Messages findMessages(String chatRoomId, int maxCount) {
    if (maxCount <= 0) {
        throw new IllegalArgumentException("maxCount arg[1] must be greater than 0");
    }/*from  ww  w .  j  av a  2s .c  o  m*/
    final String collectionName = computeCollectionName(chatRoomId);

    final DBObject fields = BasicDBObjectBuilder.start("_id", 1).add(MongoDBConstants.CHAT_PROP_USER, 1)
            .add(MongoDBConstants.CHAT_PROP_TEXT, 1).add(MongoDBConstants.CHAT_PROP_TIME, 1)
            .add(MongoDBConstants.CHAT_PROP_EMAIL_HASH, 1).get();
    DBCursor result = db.getCollection(collectionName).find(null, fields)
            .sort(new BasicDBObject("$natural", -1)).limit(maxCount);
    List<DBObject> all = result.toArray();

    Messages messages = new Messages();
    for (DBObject dbObject : all) {
        Message message = new Message();
        message.setId(dbObject.get("_id").toString());
        DateTime dateTime = ISODateTimeFormat.dateTime().parseDateTime((String) dbObject.get("time"));
        message.setDate(dateTime);
        message.setPseudo((String) dbObject.get(MongoDBConstants.CHAT_PROP_USER));
        message.setText((String) dbObject.get(MongoDBConstants.CHAT_PROP_TEXT));
        message.setEmailHash((String) dbObject.get(MongoDBConstants.CHAT_PROP_EMAIL_HASH));

        messages.getMessages().add(message);
    }
    return messages;
}

From source file:cn.vlabs.clb.server.storage.mongo.extend.MyGridFSDBFile.java

License:Apache License

byte[] getChunk(int i) {
    if (_fs == null) {
        throw new RuntimeException("no gridfs!");
    }//from  w  w  w  .j a  va 2s  . c o  m

    DBObject chunk = _fs._chunkCollection
            .findOne(BasicDBObjectBuilder.start("files_id", _id).add("n", i).get());
    if (chunk == null)
        throw new MongoException("can't find a chunk!  file id: " + _id + " chunk: " + i);

    return (byte[]) chunk.get("data");
}

From source file:com.avanza.ymer.MirrorEnvironment.java

License:Apache License

public void removeFormatVersion(Class<?> dataType, Object id) {
    String collectionName = dataType.getSimpleName().substring(0, 1).toLowerCase()
            + dataType.getSimpleName().substring(1);
    DBCollection collection = getMongoDb().getCollection(collectionName);
    DBObject idQuery = BasicDBObjectBuilder.start("_id", id).get();
    DBCursor cursor = collection.find(idQuery);

    cursor.next();//from w  w w . j a va2 s.c  o m
    DBObject obj = cursor.curr();
    cursor.close();

    obj.removeField("_formatVersion");
    collection.update(idQuery, obj);
}

From source file:com.bosscs.spark.mongodb.extractor.MongoNativeExtractor.java

License:Apache License

/**
 * Gets split data.//from   ww  w.jav  a2  s .  c o m
 *
 * @param collection the collection
 * @return the split data
 */
private BasicDBList getSplitData(DBCollection collection) {

    final DBObject cmd = BasicDBObjectBuilder.start("splitVector", collection.getFullName())
            .add("keyPattern", new BasicDBObject(MONGO_DEFAULT_ID, 1)).add("force", false)
            .add("maxChunkSize", splitSize).get();

    CommandResult splitVectorResult = collection.getDB().getSisterDB("admin").command(cmd);
    return (BasicDBList) splitVectorResult.get(SPLIT_KEYS);

}

From source file:com.englishtown.vertx.GridFSModule.java

License:Open Source License

public void getChunk(Message<JsonObject> message, final JsonObject jsonObject) {

    ObjectId id = getObjectId(message, jsonObject, "files_id");

    Integer n = getRequiredInt("n", message, jsonObject, 0);
    if (n == null) {
        return;//from  ww w  . j  a  va2s . c  o m
    }

    String bucket = jsonObject.getString("bucket", GridFS.DEFAULT_BUCKET);

    DBCollection collection = db.getCollection(bucket + ".chunks");
    DBObject dbObject = BasicDBObjectBuilder.start("files_id", id).add("n", n).get();

    DBObject result = collection.findOne(dbObject);

    if (result == null) {
        message.reply(new byte[0]);
        return;
    }

    byte[] data = (byte[]) result.get("data");
    boolean reply = jsonObject.getBoolean("reply", false);
    Handler<Message<JsonObject>> replyHandler = null;

    if (reply) {
        replyHandler = new Handler<Message<JsonObject>>() {
            @Override
            public void handle(Message<JsonObject> reply) {
                int n = jsonObject.getInteger("n") + 1;
                jsonObject.putNumber("n", n);
                getChunk(reply, jsonObject);
            }
        };
    }

    // TODO: Change to reply with a Buffer instead of a byte[]?
    message.reply(data, replyHandler);

}