com.mongodb.DB Java Examples

The following examples show how to use com.mongodb.DB. 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: MongodbLocalServerIntegrationTest.java    From hadoop-mini-clusters with Apache License 2.0 7 votes vote down vote up
@Test
public void testMongodbLocalServer() throws Exception {
    MongoClient mongo = new MongoClient(mongodbLocalServer.getIp(), mongodbLocalServer.getPort());

    DB db = mongo.getDB(propertyParser.getProperty(ConfigVars.MONGO_DATABASE_NAME_KEY));
    DBCollection col = db.createCollection(propertyParser.getProperty(ConfigVars.MONGO_COLLECTION_NAME_KEY),
            new BasicDBObject());
    
    col.save(new BasicDBObject("testDoc", new Date()));
    LOG.info("MONGODB: Number of items in collection: {}", col.count());
    assertEquals(1, col.count());
    
    DBCursor cursor = col.find();
    while(cursor.hasNext()) {
        LOG.info("MONGODB: Document output: {}", cursor.next());
    }
    cursor.close();
}
 
Example #2
Source File: SourceForgeManager.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public CommunicationChannelDelta getDelta(DB db, Discussion discussion, Date date) throws Exception {

	java.util.Date day = date.toJavaDate();

	Cache<SourceForgeArticle, String> articleCache = articleCaches.getCache(discussion, true);
	Iterable<SourceForgeArticle> articles = articleCache.getItemsAfterDate(day);

	SourceForgeCommunicationChannelDelta delta = new SourceForgeCommunicationChannelDelta();
	delta.setNewsgroup(discussion);
	for (SourceForgeArticle article : articles) {
		java.util.Date articleDate = article.getDate();
		if (article.getUpdateDate() != null)
			articleDate = article.getUpdateDate();
		if (DateUtils.isSameDay(articleDate, day)) {
			article.setText(getContents(db, discussion, article));
			delta.getArticles().add(article);
		}
	}
	System.err.println("Delta for date " + date + " contains " + delta.getArticles().size());
	return delta;
}
 
Example #3
Source File: FreemarkerRenderer.java    From act with GNU General Public License v3.0 6 votes vote down vote up
private void init(String dbHost, Integer dbPort, String dbName, String dnaCollection, String pathwayCollection) throws IOException {
  cfg = new Configuration(Configuration.VERSION_2_3_23);

  cfg.setClassLoaderForTemplateLoading(
      this.getClass().getClassLoader(), "/act/installer/reachablesexplorer/templates");
  cfg.setDefaultEncoding("UTF-8");

  cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  cfg.setLogTemplateExceptions(true);

  reachableTemplate = cfg.getTemplate(reachableTemplateName);
  pathwayTemplate = cfg.getTemplate(pathwayTemplateName);

  // TODO: move this elsewhere.
  MongoClient client = new MongoClient(new ServerAddress(dbHost, dbPort));
  DB db = client.getDB(dbName);

  dnaDesignCollection = JacksonDBCollection.wrap(db.getCollection(dnaCollection), DNADesign.class, String.class);
  Cascade.setCollectionName(pathwayCollection);
}
 
Example #4
Source File: MongoDBUtil.java    From gameserver with Apache License 2.0 6 votes vote down vote up
/**
 * Get the proper DB object by given database and collection.
 * @param databaseName
 * @param namespace
 * @param collection
 * @return
 */
public static final DB getDB(String databaseName,
		String namespace, String collection) {
	String key = null;
	if ( namespace == null ) {
		key = StringUtil.concat(collection);
	} else {
		key = StringUtil.concat(namespace, DOT, collection);
	}
	Mongo mongo = mongoMap.get(key);
	if ( mongo == null ) {
		logger.warn("Failed to find Mongo by key:{}. Need refresh", key);
		return null;
	} else {
		DB db = mongo.getDB(databaseName);
		return db;
	}
}
 
Example #5
Source File: RascalMetricProvider.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public IValue getMetricResult(Project project, IMetricProvider provider, RascalManager man) {
	Type type = provider instanceof RascalMetricProvider ? ((RascalMetricProvider) provider).getReturnType() : ((RascalMetricHistoryWrapper) provider).getValueType();
	if (context == null) {
		return null;
	}
	// FIXME: For some reason, the following code
	// returns "This database has been closed" after
	// some time.
	// Quick and very dirty fix: re-open the DB from
	// scratch.
	//DB db = context.getProjectDB(project);
	Mongo mongo;
	try {
//		mongo = new Mongo();
		mongo = Configuration.getInstance().getMongoConnection();
		DB db = mongo.getDB(project.getShortName());
		RascalMetrics rascalMetrics = new RascalMetrics(db, provider.getIdentifier());
		return PongoToRascal.toValue(rascalMetrics, type, provider instanceof RascalMetricHistoryWrapper);
	} catch (UnknownHostException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}
}
 
Example #6
Source File: BugzillaManager.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
@Override
	public BugTrackingSystemDelta getDelta(DB db, Bugzilla bugzilla, Date date) throws Exception {
		
		System.err.println("Date: " + date.toString());
		BugzillaSession session = new BugzillaSession(bugzilla.getUrl());
		
		BugTrackingSystemDelta delta = new BugTrackingSystemDelta();
		delta.setBugTrackingSystem(bugzilla);
		// Get bugs started on date
		List<Bug> bugs = getBugs(bugzilla, delta, session, date);
		// Get the comments of bugs that were started before date
//		PROBLEM: NOW WE SEARCH ONLY THE COMMENTS 
		getUpdatedBugsComments(bugzilla, delta, session, date);
		// Get the comments of bugs that were started on date
		getComments(bugzilla, delta, session, bugs, date);
		getUpdatedBugsAttachments(bugzilla, delta, session, date);
		return delta;
	}
 
Example #7
Source File: PlatformBugTrackingSystemManager.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String getContents(DB db, BugTrackingSystem bugTrackingSystem, BugTrackingSystemBug bug) throws Exception {
	String cache = getContentsCache().getCachedContents(bug);
	if (cache != null) {
		System.err.println("BugTrackingSystemBug CACHE HIT!");
		return cache;
	}

	IBugTrackingSystemManager bugTrackingSystemManager =
	getBugTrackingSystemManager((bug.getBugTrackingSystem()));
	
	if (bugTrackingSystemManager != null) {
		String contents = bugTrackingSystemManager.getContents(db, bugTrackingSystem, bug);
		getContentsCache().putContents(bug, contents);
		return contents;
	}
	return null;
}
 
Example #8
Source File: Grapher.java    From mongodb-slow-operations-profiler with GNU Affero General Public License v3.0 6 votes vote down vote up
private MongoCollection getProfilingCollection() {
        CollectorServerDto serverDto = ConfigReader.getCollectorServer();
;
        try{
            MongoDbAccessor mongo = new MongoDbAccessor(60000, -1, true, serverDto.getAdminUser(), serverDto.getAdminPw(), serverDto.getSsl(), serverDto.getHosts());
            DB db = mongo.getMongoDB(serverDto.getDb());
            Jongo jongo = new Jongo(db);
            MongoCollection result =  jongo.getCollection(serverDto.getCollection());

            if(result == null) {
                throw new IllegalArgumentException("Can't continue without profile collection for " + serverDto.getHosts());
            }

            return result;
        } catch (MongoException e) {
            LOG.error("Exception while connecting to: {}", serverDto.getHosts(), e);
        }
        return null;
    }
 
Example #9
Source File: PlatformBugTrackingSystemManager.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Date getFirstDate(DB db, BugTrackingSystem bugTrackingSystem)
		throws Exception {
	IBugTrackingSystemManager bugTrackingSystemManager = getBugTrackingSystemManager(bugTrackingSystem);
	if (bugTrackingSystemManager != null) {
		ManagerAnalysis mAnal = ManagerAnalysis.create(bugTrackingSystemManager.toString(), 
				"getFirstDate",
				bugTrackingSystem.getUrl(),
				null,
				new java.util.Date());
		platform.getProjectRepositoryManager().getProjectRepository().getManagerAnalysis().add(mAnal);
		long start = System.currentTimeMillis();
		
		Date firstDate = bugTrackingSystemManager.getFirstDate(db, bugTrackingSystem);
		
		mAnal.setMillisTaken(System.currentTimeMillis() - start);
		platform.getProjectRepositoryManager().getProjectRepository().getManagerAnalysis().sync();

		return firstDate;
	}
	
	return null;
}
 
Example #10
Source File: PlatformCommunicationChannelManager.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Date getFirstDate(DB db, CommunicationChannel communicationChannel)
		throws Exception {
	for (ICommunicationChannelManager communicationChannelManager : getCommunicationChannelManagers()) {
		if (communicationChannelManager.appliesTo(communicationChannel)) {
			ManagerAnalysis mAnal = ManagerAnalysis.create(communicationChannelManager.toString(), 
					"getFirstDate",
					communicationChannel.getUrl(),
					null,
					new java.util.Date());
			platform.getProjectRepositoryManager().getProjectRepository().getManagerAnalysis().add(mAnal);
			long start = System.currentTimeMillis();
			
			Date firstDate = communicationChannelManager.getFirstDate(db, communicationChannel);
			
			mAnal.setMillisTaken(System.currentTimeMillis() - start);
			platform.getProjectRepositoryManager().getProjectRepository().getManagerAnalysis().sync();

			return firstDate;
		}
	}
	return null;
}
 
Example #11
Source File: MongoNativeExtractor.java    From deep-spark with Apache License 2.0 5 votes vote down vote up
/**
 * Is sharded collection.
 *
 * @param collection the collection
 * @return the boolean
 */
private boolean isShardedCollection(DBCollection collection) {

    DB config = collection.getDB().getMongo().getDB("config");
    DBCollection configCollections = config.getCollection("collections");

    DBObject dbObject = configCollections.findOne(new BasicDBObject(MONGO_DEFAULT_ID, collection.getFullName()));
    return dbObject != null;
}
 
Example #12
Source File: PeriodicAllocator.java    From hvdf with Apache License 2.0 5 votes vote down vote up
public PeriodicAllocator(PluginConfiguration config){
	
	TimePeriod tPeriod = config.get(TIME_PERIOD, TimePeriod.class);
	period = tPeriod.getAs(TimeUnit.MILLISECONDS);
	prefix = config.get(HVDF.PREFIX, String.class);
	prefixLength = prefix.length();
	db = config.get(HVDF.DB, DB.class);
}
 
Example #13
Source File: MetricHistoryManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public void store(Project project, Date date, IHistoricalMetricProvider provider) {
	DB db = platform.getMetricsRepository(project).getDb();
	
	DBCollection collection = db.getCollection(provider.getCollectionName());

	MetricProviderContext context = new MetricProviderContext(platform, OssmeterLoggerFactory.getInstance().makeNewLoggerInstance(provider.getIdentifier()));
	context.setDate(date);
	provider.setMetricProviderContext(context);
	Pongo metric = provider.measure(project);
	DBObject dbObject = metric.getDbObject();
	
	dbObject.put("__date", date.toString());
	dbObject.put("__datetime", date.toJavaDate());
	collection.save(dbObject);
}
 
Example #14
Source File: TestProjectResource.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testPostInsert() throws Exception {
	Request request = new Request(Method.POST, "http://localhost:8182/projects/");

	ObjectMapper mapper = new ObjectMapper();
	
	ObjectNode p = mapper.createObjectNode();
	p.put("name", "test");
	p.put("shortName", "test-short");
	p.put("description", "this is a description");

	request.setEntity(p.toString(), MediaType.APPLICATION_JSON);
	
	Client client = new Client(Protocol.HTTP);
	Response response = client.handle(request);
	
	System.out.println(response.getEntity().getText() + " " + response.isEntityAvailable());
	
	validateResponse(response, 201);
	
	// Now try again, it should fail
	response = client.handle(request);
	validateResponse(response, 409);
	
	// Clean up
	Mongo mongo = new Mongo();
	DB db = mongo.getDB("scava");
	DBCollection col = db.getCollection("projects");
	BasicDBObject query = new BasicDBObject("name", "test");
	col.remove(query);

	mongo.close();
}
 
Example #15
Source File: MongoDBTestHelper.java    From brooklyn-library with Apache License 2.0 5 votes vote down vote up
/**
 * Inserts a new object with { key: value } at given server.
 * @return The new document's id
 */
public static String insert(AbstractMongoDBServer entity, String key, Object value) {
    LOG.info("Inserting {}:{} at {}", new Object[]{key, value, entity});
    MongoClient mongoClient = clientForServer(entity);
    try {
        DB db = mongoClient.getDB(TEST_DB);
        DBCollection testCollection = db.getCollection(TEST_COLLECTION);
        BasicDBObject doc = new BasicDBObject(key, value);
        testCollection.insert(doc);
        ObjectId id = (ObjectId) doc.get("_id");
        return id.toString();
    } finally {
        mongoClient.close();
    }
}
 
Example #16
Source File: ZendeskManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String getContents(DB db, Zendesk communicationChannel,
		CommunicationChannelArticle article) throws Exception {

	org.eclipse.scava.platform.communicationchannel.zendesk.Zendesk zendesk;
	zendesk = new org.eclipse.scava.platform.communicationchannel.zendesk.Zendesk.Builder(
			communicationChannel.getUrl())
			.setUsername(communicationChannel.getUsername())
			.setPassword(communicationChannel.getPassword()).build();
	String contents = zendesk.getTicket(article.getArticleNumber())
			.getDescription();
	zendesk.close();
	return contents;
}
 
Example #17
Source File: BugzillaManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Override
	public String getContents(DB db, Bugzilla bugzilla, BugTrackingSystemBug bug) throws Exception {
		BugzillaSession session = new BugzillaSession(bugzilla.getUrl());
		Bug retrievedBug = session.getBugById(Integer.parseInt(bug.getBugId()));
//		System.err.println("getContents:\tbug retrieved");
		return retrievedBug.getSummary();
	}
 
Example #18
Source File: DBusMongoClient.java    From DBus with Apache License 2.0 5 votes vote down vote up
private boolean isReplSet() {
    boolean ret = false;
    DB db = new DB(mongoClient, "admin");
    CommandResult cr = db.command("replSetGetStatus");
    logger.info("isReplSet: {}", cr.toJson());
    if (cr.containsField("set") && cr.containsField("members")) {
        ret = true;
    }
    return ret;
}
 
Example #19
Source File: MongodbSourceApplicationTests.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
	DB db = mongo.getDB("test");
	DBCollection col = db.createCollection("testing", new BasicDBObject());
	col.save(new BasicDBObject("greeting", "hello"));
	col.save(new BasicDBObject("greeting", "hola"));
}
 
Example #20
Source File: EclipseForumsManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String getContents(DB db, EclipseForum communicationChannel, CommunicationChannelArticle article)
		throws Exception {

	// NOT USED
	return null;
}
 
Example #21
Source File: MongoSinkUpdateInsteadReplaceTest.java    From ingestion with Apache License 2.0 5 votes vote down vote up
private void injectFongo(MongoSink mongoSink) {
    try {
        MongoClient mongoClient = fongo.getMongo();
        DB mongoDefaultDb = mongoClient.getDB("test");
        DBCollection mongoDefaultCollection = mongoDefaultDb.getCollection("test");
        setField(mongoSink, "mongoClient", mongoClient);
        setField(mongoSink, "mongoDefaultDb", mongoDefaultDb);
        setField(mongoSink, "mongoDefaultCollection", mongoDefaultCollection);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
 
Example #22
Source File: TenantDBIndexValidatorTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
    tenantDBIndexValidator.setTenantDA(tenantDA);

    Mockito.doCallRealMethod().when(tenantDBIndexValidator).isValid(Matchers.any(DB.class), Matchers.any(List.class), Matchers.any(AbstractMessageReport.class), Matchers.any(ReportStats.class), Matchers.any(Source.class));

    AbstractMessageReport report = Mockito.mock(AbstractMessageReport.class);
    ReportStats reportStats = Mockito.mock(ReportStats.class);
    Source source = Mockito.mock(Source.class);

    tenantDBIndexValidator.isValid(db, report, reportStats, source, null);

    Mockito.verify(report, Mockito.atLeast(1)).info(Matchers.eq(reportStats), Matchers.eq(source), Matchers.eq(CoreMessageCode.CORE_0018), Matchers.eq("assessment"), Matchers.any(Map.class), Matchers.eq(false));
    Mockito.verify(report, Mockito.atLeast(1)).error(Matchers.eq(reportStats), Matchers.eq(source), Matchers.eq(CoreMessageCode.CORE_0038), Matchers.eq("assessmentFamilyCollection"), Matchers.any(Map.class), Matchers.eq(false));
}
 
Example #23
Source File: AbstractHistoricalMetricProvider.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
public List<Pongo> getHistoricalMeasurements(MetricProviderContext context, Project project, Date start, Date end) {
	
	DB db = context.getProjectDB(project);
	DBCollection collection = db.getCollection(this.getCollectionName());
	
	QueryBuilder builder = QueryBuilder.start();
	if (start != null) {
		builder.and("__datetime").greaterThanEquals(start.toJavaDate());
	}
	if (end != null) {
		builder.and("__datetime").lessThanEquals(end.toJavaDate());
	}
	 
	BasicDBObject query = (BasicDBObject) builder.get(); 

	Iterator<DBObject> it = collection.find(query).iterator();
	
	List<Pongo> pongoList = new ArrayList<Pongo>();
	
	while (it.hasNext()) {
		DBObject dbObject = it.next();
		pongoList.add(PongoFactory.getInstance().createPongo(dbObject));
	}
	
	return pongoList;
	
}
 
Example #24
Source File: TenantAwareMongoDbFactoryTest.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
public void testGetTenantConnection(String tenantId) {        
    TenantContext.setTenantId(tenantId);

    Mongo mongo = Mockito.mock(Mongo.class);
    DB db = Mockito.mock(DB.class);

    Mockito.when(db.getMongo()).thenReturn(mongo);
    Mockito.when(db.getName()).thenReturn("tenant");
    Mockito.when(mongo.getDB(TenantAwareMongoDbFactory.getTenantDatabaseName(tenantId))).thenReturn(db);

    TenantAwareMongoDbFactory cm = new TenantAwareMongoDbFactory(mongo, "System");

    Assert.assertNotNull(cm.getDb());
    Assert.assertSame("tenant", cm.getDb().getName());
}
 
Example #25
Source File: MongoBenchmark.java    From gameserver with Apache License 2.0 5 votes vote down vote up
public static void testMyUserId(int max, DB db) {
	String collName = "testmyuserid";
	DBCollection coll = db.getCollection(collName);
	
	//Setup a sharded collection
	BasicDBObject command = new BasicDBObject();
	command.put("shardcollection", collName);
	DBObject key = new BasicDBObject();
	key.put("_id", 1);
	command.put("key", key);
	command.put("unique", true);
	db.command(command);
	
	long startM = System.currentTimeMillis();
	BasicDBObject obj = new BasicDBObject();
	for ( int i=0; i<max; i++ ) {
		UserId userId = new UserId("username"+i);
		obj.put("_id",  userId.getInternal());
		obj.put("test", "value-"+i);
		coll.save(obj);
	}
	long endM = System.currentTimeMillis();
	
	System.out.println("Insert " + max + " my objectid. time: " + (endM-startM) + " benchmark()");
	
	CommandResult result = db.getStats();
	System.out.println(result);
}
 
Example #26
Source File: MongoConfiguration.java    From Decision with Apache License 2.0 5 votes vote down vote up
@Bean
@Lazy
public DB mongoDB(){

    log.debug("Creating Spring Bean for mongoDB");
   return mongoClient().getDB(STREAMING.STREAMING_KEYSPACE_NAME);

}
 
Example #27
Source File: JiraManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String getContents(DB db, JiraBugTrackingSystem bts, BugTrackingSystemBug bug) throws Exception {

	JiraRestClient jira = getJiraRestClient(bts);
	// Request only the description field in the rest response
	JiraIssue issue = jira.getIssue(bug.getBugId(), "description");
	if (null != issue) {
		return issue.getDescription();
	}

	return null;
}
 
Example #28
Source File: FindOne.java    From openbd-core with GNU General Public License v3.0 5 votes vote down vote up
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	
	// Get the necessary Mongo references
	DB			db	= getDB(_session, argStruct);
	GridFS	gridfs	= getGridFS(_session, argStruct, db);

	
	// Get the file information
	String filename	= getNamedStringParam(argStruct, "filename", null);
	if ( filename != null ){
		return toStruct( gridfs.findOne(filename) );
	} else {
		
		String _id	= getNamedStringParam(argStruct, "_id", null);
		if ( _id != null ){
			return toStruct( gridfs.findOne( new ObjectId(_id) ) );
		} else {
			
			cfData mTmp	= getNamedParam(argStruct, "query", null);
			if ( mTmp != null )
				return toStruct( gridfs.findOne(getDBObject(mTmp)) );
		}
	}

	throwException(_session, "Please specify file, _id or a query");
	return null;
}
 
Example #29
Source File: MongoDbPluginIT.java    From glowroot with Apache License 2.0 5 votes vote down vote up
@Override
public void transactionMarker() {
    DB database = mongoClient.getDB("testdb");
    DBCollection collection = database.getCollection("test");
    BasicDBObject document = new BasicDBObject("test1", "test2")
            .append("test3", "test4");
    collection.insert(document);
}
 
Example #30
Source File: GitHubManager.java    From scava with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Date getFirstDate(DB db, GitHubBugTracker ghbt) throws Exception {

	 Date earliestDate = getEarliestIssueDate(ghbt);
	//
	// getAllData(ghbt, earliestDate); //TODO POTENTIALLY REMOVE

	return earliestDate;
}