Example usage for com.mongodb DBCollection createIndex

List of usage examples for com.mongodb DBCollection createIndex

Introduction

In this page you can find the example usage for com.mongodb DBCollection createIndex.

Prototype

public void createIndex(final DBObject keys) 

Source Link

Document

Creates an index on the field specified, if that index does not already exist.

Usage

From source file:net.kamradtfamily.mongorest.IndexServlet.java

License:GNU General Public License

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doPut()");

    InputStream is = req.getInputStream();
    String db_name = req.getParameter("dbname");
    String col_name = req.getParameter("colname");
    if (db_name == null || col_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;/*from w  w w .  j  av  a 2  s .  co  m*/
    }

    DB db = mongo.getDB(db_name);

    // mongo auth
    String user = req.getParameter("user");
    String passwd = req.getParameter("passwd");
    if (user != null && passwd != null && (!db.isAuthenticated())) {
        boolean auth = db.authenticate(user, passwd.toCharArray());
        if (!auth) {
            res.sendError(SC_UNAUTHORIZED);
            return;
        }
    }

    DBCollection col = db.getCollection(col_name);

    BufferedReader r = null;
    String data = null;

    try {

        r = new BufferedReader(new InputStreamReader(is));
        data = r.readLine();

    } finally {
        if (r != null)
            r.close();
    }
    if (data == null) {
        error(res, SC_BAD_REQUEST, Status.get("no data"));
        return;
    }

    DBObject o;
    try {
        o = (DBObject) JSON.parse(data);
    } catch (JSONParseException e) {
        error(res, SC_BAD_REQUEST, Status.get("can not parse data"));
        return;
    }

    col.createIndex(o);

    res.setStatus(SC_CREATED);

}

From source file:org.apache.camel.component.gridfs.GridFsConsumer.java

License:Apache License

@Override
public void run() {
    DBCursor c = null;/*from   ww w .  j  a va  2s.  c om*/
    java.util.Date fromDate = null;

    QueryStrategy s = endpoint.getQueryStrategy();
    boolean usesTimestamp = (s != QueryStrategy.FileAttribute);
    boolean persistsTimestamp = (s == QueryStrategy.PersistentTimestamp
            || s == QueryStrategy.PersistentTimestampAndFileAttribute);
    boolean usesAttribute = (s == QueryStrategy.FileAttribute || s == QueryStrategy.TimeStampAndFileAttribute
            || s == QueryStrategy.PersistentTimestampAndFileAttribute);

    DBCollection ptsCollection = null;
    DBObject persistentTimestamp = null;
    if (persistsTimestamp) {
        ptsCollection = endpoint.getDB().getCollection(endpoint.getPersistentTSCollection());
        // ensure standard indexes as long as collections are small
        try {
            if (ptsCollection.count() < 1000) {
                ptsCollection.createIndex(new BasicDBObject("id", 1));
            }
        } catch (MongoException e) {
            //TODO: Logging
        }
        persistentTimestamp = ptsCollection.findOne(new BasicDBObject("id", endpoint.getPersistentTSObject()));
        if (persistentTimestamp == null) {
            persistentTimestamp = new BasicDBObject("id", endpoint.getPersistentTSObject());
            fromDate = new java.util.Date();
            persistentTimestamp.put("timestamp", fromDate);
            ptsCollection.save(persistentTimestamp);
        }
        fromDate = (java.util.Date) persistentTimestamp.get("timestamp");
    } else if (usesTimestamp) {
        fromDate = new java.util.Date();
    }
    try {
        Thread.sleep(endpoint.getInitialDelay());
        while (isStarted()) {
            if (c == null || c.getCursorId() == 0) {
                if (c != null) {
                    c.close();
                }
                String queryString = endpoint.getQuery();
                DBObject query;
                if (queryString == null) {
                    query = new BasicDBObject();
                } else {
                    query = (DBObject) JSON.parse(queryString);
                }
                if (usesTimestamp) {
                    query.put("uploadDate", new BasicDBObject("$gt", fromDate));
                }
                if (usesAttribute) {
                    query.put(endpoint.getFileAttributeName(), null);
                }
                c = endpoint.getFilesCollection().find(query);
            }
            boolean dateModified = false;
            while (c.hasNext() && isStarted()) {
                GridFSDBFile file = (GridFSDBFile) c.next();
                GridFSDBFile forig = file;
                if (usesAttribute) {
                    file.put(endpoint.getFileAttributeName(), "processing");
                    DBObject q = BasicDBObjectBuilder.start("_id", file.getId()).append("camel-processed", null)
                            .get();
                    forig = (GridFSDBFile) endpoint.getFilesCollection().findAndModify(q, null, null, false,
                            file, true, false);
                }
                if (forig != null) {
                    file = endpoint.getGridFs().findOne(new BasicDBObject("_id", file.getId()));

                    Exchange exchange = endpoint.createExchange();
                    exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA,
                            JSON.serialize(file.getMetaData()));
                    exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
                    exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
                    exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
                    exchange.getIn().setBody(file.getInputStream(), InputStream.class);
                    try {
                        getProcessor().process(exchange);
                        //System.out.println("Processing " + file.getFilename());
                        if (usesAttribute) {
                            forig.put(endpoint.getFileAttributeName(), "done");
                            endpoint.getFilesCollection().save(forig);
                        }
                        if (usesTimestamp) {
                            if (file.getUploadDate().compareTo(fromDate) > 0) {
                                fromDate = file.getUploadDate();
                                dateModified = true;
                            }
                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            if (persistsTimestamp && dateModified) {
                persistentTimestamp.put("timestamp", fromDate);
                ptsCollection.save(persistentTimestamp);
            }
            Thread.sleep(endpoint.getDelay());
        }
    } catch (Throwable e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    if (c != null) {
        c.close();
    }
}

From source file:org.apache.rya.indexing.geotemporal.mongo.GeoTemporalMongoDBStorageStrategy.java

License:Apache License

@Override
public void createIndices(final DBCollection coll) {
    coll.createIndex(new BasicDBObject(GEO_KEY, "2dsphere"));
    coll.createIndex(TIME_KEY);/*  w w w.  j  a  v a2s . c o  m*/
}

From source file:org.apache.rya.indexing.mongodb.freetext.TextMongoDBStorageStrategy.java

License:Apache License

@Override
public void createIndices(final DBCollection coll) {
    final BasicDBObject basicDBObject = new BasicDBObject();
    basicDBObject.append(text, "text");
    coll.createIndex(basicDBObject);
}

From source file:org.apache.rya.indexing.mongodb.geo.GeoMongoDBStorageStrategy.java

License:Apache License

@Override
public void createIndices(final DBCollection coll) {
    coll.createIndex(new BasicDBObject(GEO, "2dsphere"));
}

From source file:org.apache.rya.indexing.mongodb.temporal.TemporalMongoDBStorageStrategy.java

License:Apache License

@Override
public void createIndices(final DBCollection coll) {
    coll.createIndex(INTERVAL_START);
    coll.createIndex(INTERVAL_END);/*from   w w w  .  ja v a 2s  . com*/
    coll.createIndex(INSTANT);
}

From source file:org.apache.rya.mongodb.dao.SimpleMongoDBNamespaceManager.java

License:Apache License

@Override
public void createIndices(final DBCollection coll) {
    coll.createIndex(PREFIX);
    coll.createIndex(NAMESPACE);
}

From source file:org.apache.rya.mongodb.dao.SimpleMongoDBStorageStrategy.java

License:Apache License

@Override
public void createIndices(final DBCollection coll) {
    BasicDBObject doc = new BasicDBObject();
    doc.put(SUBJECT_HASH, 1);//  ww w.j  av a 2 s.co m
    doc.put(PREDICATE_HASH, 1);
    doc.put(OBJECT_HASH, 1);
    doc.put(OBJECT_TYPE, 1);
    coll.createIndex(doc);
    doc = new BasicDBObject(PREDICATE_HASH, 1);
    doc.put(OBJECT_HASH, 1);
    doc.put(OBJECT_TYPE, 1);
    coll.createIndex(doc);
    doc = new BasicDBObject(OBJECT_HASH, 1);
    doc.put(OBJECT_TYPE, 1);
    doc.put(SUBJECT_HASH, 1);
    coll.createIndex(doc);
}

From source file:org.eclipse.linuxtools.tmf.totalads.dbms.DBMS.java

License:Open Source License

/**
 * Creates an index on a collection (table)
 * @param database Database name//from   w ww  .j a v a 2  s.  co m
 * @param collection Collection name
 * @param field Field name on which to create an index
 */
public void createAscendingIndex(String dataBase, String collection, String field) {
    DBCollection coll = mongoClient.getDB(dataBase).getCollection(collection);
    coll.createIndex(new BasicDBObject(field, 1));
}

From source file:org.ecloudmanager.monitoring.rrd.RrdMongoDBBackendFactory.java

License:Open Source License

/**
 * Creates a RrdMongoDBBackendFactory. Make sure that the passed {@link com.mongodb.DBCollection} has a safe write
 * concern, is capped (if needed) and slaveOk() called if applicable.
 *
 * @param rrdCollection the collection to use for storing RRD byte data
 *///from   www.j av  a 2  s .com
public RrdMongoDBBackendFactory(DBCollection rrdCollection) {
    this.rrdCollection = rrdCollection;

    // make sure we have an index on the path field
    rrdCollection.createIndex(new BasicDBObject("path", 1));

    // set the RRD backend factory
    RrdBackendFactory.registerAndSetAsDefaultFactory(this);
}