Java Code Examples for com.mongodb.DBCollection#ensureIndex()

The following examples show how to use com.mongodb.DBCollection#ensureIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SparkCache.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private SparkCache() {
	sparkMap = DBMaker.newTempTreeMap();
	dataMap = DBMaker.newTempTreeMap();
	
	// If we're running in a cluster, where the API is load balanced, we actually
	// need to store the spark bytes in a DB. In memory cache wouldn't work.
	Mongo mongo;
	try {
		mongo = Configuration.getInstance().getMongoConnection();
	} catch (UnknownHostException e) {
		e.printStackTrace();
		return;
	}
	DB db = mongo.getDB("scava");
	DBCollection col = db.getCollection("sparks");
	col.ensureIndex("sparkid");
	col.ensureIndex(new BasicDBObject("created_at", 1), new BasicDBObject("expireAfterSeconds", 3600));
}
 
Example 2
Source File: FeatureRepositoryCustomImpl.java    From osiris with Apache License 2.0 6 votes vote down vote up
@Override
public Feature save(String idApplication, Feature feature) throws MongoGeospatialException {
	// TODO Auto-generated method stub
	if(!mongoTemplate.collectionExists(collectionName+idApplication)){
		DBCollection dbCollection = mongoTemplate.getCollection(collectionName+idApplication); 
		DBObject obj = new BasicDBObject();		
		obj.put("geometry", "2dsphere");
		dbCollection.ensureIndex(obj);
	}
	try{
		mongoTemplate.save(feature, collectionName+idApplication);
	}
	catch(UncategorizedMongoDbException uncategorizedMongoDbException){
		throw new MongoGeospatialException();
	}
	return feature;
}
 
Example 3
Source File: ImportRepositoryCustomImpl.java    From osiris with Apache License 2.0 6 votes vote down vote up
private void createCollection(String collectionName){
	
	 if(mongoTemplate.collectionExists(collectionName)){
		 mongoTemplate.dropCollection(collectionName);
		 mongoTemplate.createCollection(collectionName);			 
	 }else{
		 mongoTemplate.createCollection(collectionName);
	 }
	  				 
			 		
	 DBCollection dbCollection = mongoTemplate.getCollection(collectionName); 
	 DBObject obj = new BasicDBObject();		
	 obj.put("geometry", "2dsphere");		
	// DBObject objoptions = new BasicDBObject();
	// objoptions.put("2dsphereIndexVersion", 1);
	 dbCollection.ensureIndex(obj/*, objoptions*/);	
	 dbCollection.resetIndexCache();
	 
	
}
 
Example 4
Source File: MongoDBUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Make sure an index is created. If the index is already created, it will
 * do nothing.
 * 
 * @param index
 * @param databaseName
 * @param namespace
 * @param collection
 */
public static final void ensureIndex(String databaseName,
		String namespace, String collection, String columnKey, boolean unique) {

	DBObject keys = createDBObject();
	keys.put(columnKey, ONE);
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	if ( coll == null ) {
		logger.warn("collection:{} does not exist.", collection);
		return;
	}
	if ( !unique ) {
		coll.ensureIndex(keys);
	} else {
		DBObject option = createDBObject();
		option.put("unique", Boolean.TRUE);
		coll.ensureIndex(keys, columnKey, unique);
	}
}
 
Example 5
Source File: MongoUtil.java    From gameserver with Apache License 2.0 5 votes vote down vote up
/**
 * Make sure an index is created. If the index is already created, it will
 * do nothing.
 * 
 * @param index
 * @param databaseName
 * @param namespace
 * @param collection
 */
public static final void ensureIndex(String databaseName,
		String namespace, String collection, String columnKey, boolean unique) {

	DBObject keys = createDBObject();
	keys.put(columnKey, ONE);
	DBCollection coll = getDBCollection(databaseName, namespace, collection);
	if ( !unique ) {
		coll.ensureIndex(keys);
	} else {
		DBObject option = createDBObject();
		option.put("unique", Boolean.TRUE);
		coll.ensureIndex(keys, columnKey, unique);
	}
}