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:fr.wseduc.gridfs.GridFSPersistor.java

License:Apache License

private void getChunk(Message<Buffer> message, JsonObject json) {
    String filesId = json.getString("files_id");
    if (filesId == null || filesId.trim().isEmpty()) {
        replyError(message, "Invalid file id.");
        return;//from www  .java  2 s . c  o m
    }
    Long n = json.getLong("n");
    if (n == null) {
        replyError(message, "Invalid chunk number.");
        return;
    }
    DBCollection collection = db.getCollection(bucket + CHUNKS);
    DBObject chunk = collection.findOne(BasicDBObjectBuilder.start("files_id", filesId).add("n", n).get());
    if (chunk == null) {
        replyError(message, "Error getting chunk number " + n + " in file  " + filesId);
        return;
    }
    logger.debug("chunk : " + chunk);
    message.reply(new Buffer((byte[]) chunk.get("data")));
}

From source file:io.gravitee.repository.mongodb.ratelimit.MongoRateLimitRepository.java

License:Apache License

@Override
public Iterator<RateLimit> findAsyncAfter(long timestamp) {
    DBObject query = BasicDBObjectBuilder.start(FIELD_ASYNC, true)
            .add(FIELD_UPDATED_AT, BasicDBObjectBuilder.start("$gte", timestamp).get()).get();

    final Iterator<DBObject> dbObjects = mongoOperations.getCollection(RATE_LIMIT_COLLECTION).find(query)
            .iterator();//w  w  w .  j ava 2  s.co  m

    return new Iterator<RateLimit>() {

        @Override
        public boolean hasNext() {
            return dbObjects.hasNext();
        }

        @Override
        public RateLimit next() {
            DBObject dbObject = dbObjects.next();
            RateLimit rateLimit = new RateLimit((String) dbObject.get(FIELD_KEY));
            rateLimit.setCounter((long) dbObject.get(FIELD_COUNTER));
            rateLimit.setLastRequest((long) dbObject.get(FIELD_LAST_REQUEST));
            rateLimit.setResetTime(((Date) dbObject.get(FIELD_RESET_TIME)).getTime());
            rateLimit.setUpdatedAt((long) dbObject.get(FIELD_UPDATED_AT));
            rateLimit.setCreatedAt((long) dbObject.get(FIELD_CREATED_AT));
            rateLimit.setAsync((boolean) dbObject.get(FIELD_ASYNC));
            return rateLimit;
        }
    };
}

From source file:mongoDB.MongoDbClientAllImagesAsByteArrays.java

License:Open Source License

@Override
public void createSchema(Properties props) {

    // drop all collections
    com.mongodb.DB db = null;/*from www .j  ava  2  s .  co  m*/
    try {
        // drop database and collections
        // db = mongo.getDB(database);
        db = mongo.getDB("admin");
        db.requestStart();
        // add shards
        /*
         * BasicDBObject addShard = new BasicDBObject("addshard",
         * "10.0.0.138:27011"); CommandResult shardAddResult =
         * db.command(addShard); shardAddResult.getException();
         * System.out.println(shardAddResult.toString()); addShard = new
         * BasicDBObject("addshard", "10.0.0.138:27012"); shardAddResult =
         * db.command(addShard);
         * System.out.println(shardAddResult.toString()); //db.command(
         * " { addshard : \"10.0.0.138:27011\" , allowLocal : true } )");
         */

        db = mongo.getDB(database);
        DBCollection collection = db.getCollection("users");
        collection.drop();
        collection = db.getCollection("resources");
        collection.drop();
        collection = db.getCollection("manipulation");
        collection.drop();

        if (Boolean.parseBoolean(
                props.getProperty(MONGODB_SHARDING_PROPERTY, MONGODB_SHARDING_PROPERTY_DEFAULT)) == true) {
            // enable sharding on the database in the admin user
            db = mongo.getDB("admin");
            BasicDBObject s = new BasicDBObject("enablesharding",
                    props.getProperty(MONGODB_DB_PROPERTY, "benchmark"));
            CommandResult cr = db.command(s);

            // enable sharding on each collection
            cr = db.command(BasicDBObjectBuilder.start("shardCollection", "benchmark.users").push("key")
                    .add("_id", 1).pop().get());
            if (Boolean.parseBoolean(props.getProperty(MONGODB_MANIPULATION_ARRAY_PROPERTY,
                    MONGODB_MANIPULATION_ARRAY_PROPERTY_DEFAULT)) == false) {
                cr = db.command(BasicDBObjectBuilder.start("shardCollection", "benchmark.resources").push("key")
                        .add("walluserid", 1).pop().get());
                cr = db.command(BasicDBObjectBuilder.start("shardCollection", "benchmark.manipulation")
                        .push("key").add("rid", 1).pop().get());
            } else {
                cr = db.command(BasicDBObjectBuilder.start("shardCollection", "benchmark.resources").push("key")
                        .add("_id", 1).pop().get());
            }
            //force move a chunk   
            //BasicDBObject mov = new BasicDBObject("moveChunk","benchmark.users");
            //BasicDBObject mo2 = new BasicDBObject("_id", 1); //this is the chunkid not the userid
            //mov.put("find", mo2);
            //mov.put("to", "shard0001");
            //CommandResult result2 = mongo.getDB("admin").command(mov);
            //System.out.println(result2);
            //check if migration is happening
            //            com.mongodb.DB db2 = mongo.getDB("config");
            //            DBCollection cl = db2.getCollection("locks");
            //            DBObject q = new BasicDBObject().append("_id", "balancer");
            //            DBObject qres = cl.findOne(q);
            //            while( Integer.parseInt(qres.get("state").toString()) == 1 )
            //               System.out.println("still migrating");
        }

        // create indexes on collection
        db = mongo.getDB(database);
        collection = db.getCollection("users");
        collection.createIndex(new BasicDBObject("users.wallResources", 1)); // create
        // index
        // on
        collection = db.getCollection("resources");
        collection.createIndex(new BasicDBObject("walluserid", 1)); // create
        // index
        // on
        // "i",
        // ascending
        if (Boolean.parseBoolean(props.getProperty(MONGODB_MANIPULATION_ARRAY_PROPERTY,
                MONGODB_MANIPULATION_ARRAY_PROPERTY_DEFAULT)) == false) {
            collection = db.getCollection("manipulation");
            collection.createIndex(new BasicDBObject("mid", 1));
            collection.createIndex(new BasicDBObject("rid", 1)); // create
            // index
            // on
            // "i",
            // ascending
        } else {
            collection.createIndex(new BasicDBObject("resources.Manipulations", 1)); // create index on
            // "i", ascending
        }

    } catch (Exception e) {
        System.out.println(e.toString());
        return;
    } finally {
        if (db != null) {
            db.requestDone();
        }
    }
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Reports an {@link java.lang.Exception} without bindings
 *
 * @param ex {@link java.lang.Exception} to report
 *//*w  ww  . j a v a2 s  . com*/
public static void reportError(Exception ex) {
    storeErrorReport(BasicDBObjectBuilder.start("error", formatException(ex)).get());
    ex.printStackTrace();
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Reports an error for a {@link net.tbnr.gearz.server.Server} and {@link java.lang.Exception}
 *
 * @param serverInfo The {@link net.md_5.bungee.api.config.ServerInfo} with the error
 * @param ex         The {@link java.lang.Exception} involved in the error
 *//*from   w  w  w. j  a v a2  s.  c om*/
@SuppressWarnings("unused")
public static void reportError(ServerInfo serverInfo, Exception ex) {
    storeErrorReport(
            BasicDBObjectBuilder.start("server", serverInfo.getName()).add("error", formatException(ex)).get());
    ex.printStackTrace();
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Reports an error for a {@link net.md_5.bungee.api.connection.ProxiedPlayer}, their active {@link net.tbnr.gearz.server.Server}, and the {@link java.lang.Exception} related
 *
 * @param player The {@link net.md_5.bungee.api.connection.ProxiedPlayer} who is involved
 * @param ex     The {@link java.lang.Exception}
 *//*  w w  w.ja  va2  s.co  m*/
@SuppressWarnings("unused")
public static void reportError(ProxiedPlayer player, Exception ex) {
    storeErrorReport(BasicDBObjectBuilder.start("player", player.getName())
            .add("server", player.getServer().getInfo().getName()).add("error", formatException(ex)).get());
    ex.printStackTrace();
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Reports an error without a key/*from   www.  j  av  a  2s .c om*/
 *
 * @param error Error to report
 */
@SuppressWarnings("unused")
public static void reportError(String error) {
    storeErrorReport(BasicDBObjectBuilder.start("error", error).get());
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Reports an error with a key./*w  ww. j  av a  2 s .  c o  m*/
 *
 * @param serverInfo {@link net.md_5.bungee.api.config.ServerInfo} error relevant to
 * @param error      The error itself.
 */
@SuppressWarnings("unused")
public static void reportError(ServerInfo serverInfo, String error) {
    storeErrorReport(BasicDBObjectBuilder.start("server", serverInfo.getName()).add("error", error).get());
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Reports an error with a key.//from w  w w .j a  va2 s .co m
 *
 * @param key Easy reference to find error with a certain key
 * @param ex  The error itself.
 */
@SuppressWarnings("unused")
public static void reportError(String key, Exception ex) {
    storeErrorReport(BasicDBObjectBuilder.start("server", key).add("error", formatException(ex)).get());
}

From source file:net.tbnr.util.ErrorHandler.java

License:Open Source License

/**
 * Reports an error/*from w w  w  .  j a va  2 s  .c om*/
 *
 * @param player The {@link net.md_5.bungee.api.connection.ProxiedPlayer} to report the error on
 * @param error  The error string.
 */
@SuppressWarnings("unused")
public static void reportError(ProxiedPlayer player, String error) {
    storeErrorReport(BasicDBObjectBuilder.start("player", player.getName())
            .add("server", player.getServer().getInfo().getName()).add("error", error).get());
}