com.mongodb.client.MongoDatabase Java Examples

The following examples show how to use com.mongodb.client.MongoDatabase. 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: RealMongoFactory.java    From baleen with Apache License 2.0 6 votes vote down vote up
@Override
public MongoDatabase createDatabase() {

  int port =
      argumentsMap.containsKey(PORT) ? Integer.parseInt(argumentsMap.get(PORT)) : DEFAULT_PORT;
  String host = argumentsMap.getOrDefault(HOST, DEFAULT_HOST);
  String databaseName = argumentsMap.getOrDefault(DATABASE_NAME, DEFAULT_DATABASE_NAME);
  String username = argumentsMap.getOrDefault(USERNAME, DEFAULT_USERNAME);
  String password = argumentsMap.getOrDefault(PASSWORD, DEFAULT_PASSWORD);

  List<ServerAddress> seeds = new ArrayList<>();
  List<MongoCredential> credentials = new ArrayList<>();
  seeds.add(new ServerAddress(host, port));
  credentials.add(
      MongoCredential.createScramSha1Credential(username, databaseName, password.toCharArray()));

  Boolean useAuthentication =
      Boolean.valueOf(argumentsMap.getOrDefault(USE_AUTHENTICATION, FALSE));

  client = useAuthentication ? new MongoClient(seeds, credentials) : new MongoClient(seeds);

  String dbName = argumentsMap.getOrDefault(DATABASE_NAME, DEFAULT_DATABASE_NAME);
  return client.getDatabase(dbName);
}
 
Example #2
Source File: MongoWrapperDefaultHandler.java    From DBus with Apache License 2.0 6 votes vote down vote up
/**
 * 根据oid去数据库回查数据
 *
 * @param oid
 * @return
 */
private Document fetchData(String schemaName, String tableName, String oid) {
    Document result = null;
    DbusDatasource datasource = GlobalCache.getDatasource();
    MongoClientURI uri = new MongoClientURI(datasource.getMasterUrl());
    MongoClient client = new MongoClient(uri);
    MongoDatabase database = client.getDatabase(schemaName);
    MongoCollection<Document> collection = database.getCollection(tableName);
    MongoCursor<Document> cursor = collection.find(new BasicDBObject().append("_id", new ObjectId(oid))).iterator();
    if (cursor.hasNext()) {
        result = cursor.next();
    } else {
        logger.error("get source data error. schemaName:{}, tableName:{}, oid:{}", schemaName, tableName, oid);
    }
    client.close();
    return result;

}
 
Example #3
Source File: MongoCompensableLock.java    From ByteTCC with GNU Lesser General Public License v3.0 6 votes vote down vote up
private boolean takeOverTransactionInMongoDB(TransactionXid transactionXid, String source, String target) {
	byte[] global = transactionXid.getGlobalTransactionId();
	String instanceId = ByteUtils.byteArrayToString(global);

	try {
		String application = CommonUtils.getApplication(this.endpoint);
		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS);

		Bson globalFilter = Filters.eq(CONSTANTS_FD_GLOBAL, instanceId);
		Bson instIdFilter = Filters.eq("identifier", source);

		Document document = new Document("$set", new Document("identifier", target));

		UpdateResult result = collection.updateOne(Filters.and(globalFilter, instIdFilter), document);
		return result.getMatchedCount() == 1;
	} catch (RuntimeException rex) {
		logger.error("Error occurred while locking transaction(gxid= {}).", instanceId, rex);
		return false;
	}
}
 
Example #4
Source File: MongoDbDecoratorTest.java    From jpa-unit with Apache License 2.0 6 votes vote down vote up
@Test
public void testMongoClientIsClosedEvenIfExecuteAfterTestFails() throws Throwable {
    // GIVEN
    when(invocation.getException()).thenReturn(Optional.of(new Exception()));
    doThrow(RuntimeException.class).when(executor).executeAfterTest(any(MongoDatabase.class), anyBoolean());

    // WHEN
    try {
        decorator.afterTest(invocation);
        fail("Exception expected");
    } catch (final Exception e) {
        // expected
    }

    // THEN
    verifyZeroInteractions(mongoClient);
}
 
Example #5
Source File: MongoDBAppender.java    From heimdall with Apache License 2.0 6 votes vote down vote up
@Override
public void start() {
	log.info("Initializing Mongodb Appender");
	if (this.uri != null) {
		this.mongoClient = new MongoClient(new MongoClientURI(this.uri));
	} else {
		MongoClientOptions options = new MongoClientOptions.Builder().build();
		ServerAddress address = new ServerAddress(this.url, this.port.intValue());
		this.mongoClient = new MongoClient(address, options);
	}

	MongoDatabase database = this.mongoClient.getDatabase(this.dataBase);
	this.collection = database.getCollection(this.collectionName);
	log.info("Starting connection with url: {} - port: {}", this.url, this.port);
	log.info("Database used: {} - Collection: {}", this.dataBase, this.collectionName);
	super.start();
}
 
Example #6
Source File: BsonToJsonLiveTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenBsonDocument_whenUsingCustomJsonTransformation_thenJsonDateIsStringField() {

    String json = null;
    try (MongoClient mongoClient = new MongoClient()) {
        MongoDatabase mongoDatabase = mongoClient.getDatabase(DB_NAME);
        Document bson = mongoDatabase.getCollection("Books").find().first();
        json = bson.toJson(JsonWriterSettings
            .builder()
            .dateTimeConverter(new JsonDateTimeConverter())
            .build());
    }

    String expectedJson = "{\"_id\": \"isbn\", " + 
        "\"className\": \"com.baeldung.bsontojson.Book\", " + 
        "\"title\": \"title\", " + 
        "\"author\": \"author\", " + 
        "\"publisher\": {\"_id\": {\"$oid\": \"fffffffffffffffffffffffa\"}, " + 
        "\"name\": \"publisher\"}, " + 
        "\"price\": 3.95, " + 
        "\"publishDate\": \"2020-01-01T17:13:32Z\"}";

    assertEquals(expectedJson, json);

}
 
Example #7
Source File: ChangeEntryDaoTest.java    From mongobee with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldReleaseLockFromLockDao() throws Exception {

  // given
  MongoClient mongoClient = mock(MongoClient.class);
  MongoDatabase db = new Fongo(TEST_SERVER).getDatabase(DB_NAME);
  when(mongoClient.getDatabase(anyString())).thenReturn(db);

  ChangeEntryDao dao = new ChangeEntryDao(CHANGELOG_COLLECTION_NAME, LOCK_COLLECTION_NAME, WAIT_FOR_LOCK,
      CHANGE_LOG_LOCK_WAIT_TIME, CHANGE_LOG_LOCK_POLL_RATE, THROW_EXCEPTION_IF_CANNOT_OBTAIN_LOCK);

  LockDao lockDao = mock(LockDao.class);
  dao.setLockDao(lockDao);

  dao.connectMongoDb(mongoClient, DB_NAME);

  // when
  dao.releaseProcessLock();

  // then
  verify(lockDao).releaseLock(any(MongoDatabase.class));
}
 
Example #8
Source File: ReadWriteTest.java    From hazelcast-simulator with Apache License 2.0 6 votes vote down vote up
@Setup
public void setUp() {
    if (itemCount <= 0) {
        throw new IllegalStateException("size must be larger than 0");
    }

    MongoDatabase database = client.getDatabase(databaseName);
    col = database.getCollection(collectionName);

    values = new Document[idArraySize][itemCount];
    for (int i = 0; i < idArraySize; i++) {
        for (int j = 0; j < itemCount; j++) {
            values[i][j] = createEntry(j);
        }
    }
}
 
Example #9
Source File: MongoCollectionStats.java    From openbd-core with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings( "rawtypes" )
public cfData execute(cfSession _session, cfArgStructData argStruct ) throws cfmRunTimeException {
	MongoDatabase	db	= getMongoDatabase( _session, argStruct );
	
	String collection	= getNamedStringParam(argStruct, "collection", null);
	if ( collection == null )
		throwException(_session, "please specify a collection");
	
	try{
		Document result	= db.runCommand( new Document("collection",collection).append( "verbose", true ) );
		return tagUtils.convertToCfData((Map)result);	 	
	} catch (MongoException me){
		throwException(_session, me.getMessage());
		return null;
	}

}
 
Example #10
Source File: RegisterActivity.java    From medical-data-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Integer doInBackground(User... params) {
    try {
        MongoClientURI mongoClientURI = new MongoClientURI(Variables.mongo_uri);
        MongoClient mongoClient = new MongoClient(mongoClientURI);
        MongoDatabase dbMongo = mongoClient.getDatabase(mongoClientURI.getDatabase());
        MongoCollection<Document> coll = dbMongo.getCollection("users");
        User local_user = params[0];
        if (coll.find(eq("email", local_user.getEmail())).first() != null) {
            mongoClient.close();
            return 1; // Repeated email
        }
        Document document = local_user.getRegisterDocument();
        coll.insertOne(document);
        local_user.setId(document.getObjectId("_id").toString());
        mongoClient.close();
        return 0; //Successfully saved
    } catch (Exception e) {
        return 2; // Error
    }
}
 
Example #11
Source File: ProfileActivity.java    From medical-data-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
protected Integer doInBackground(User... params) {
    try {
        MongoClientURI mongoClientURI = new MongoClientURI(Variables.mongo_uri);
        MongoClient mongoClient = new MongoClient(mongoClientURI);
        MongoDatabase dbMongo = mongoClient.getDatabase(mongoClientURI.getDatabase());
        MongoCollection<Document> coll = dbMongo.getCollection("users");
        User local_user = params[0];
        if (!local_user.getEmail().equals(original_email)) {
            Document user = coll.find(eq("email", local_user.getEmail())).first();
            if (user != null) {
                return 1; // Repeated email
            }
        }

        Document search = new Document("_id", new ObjectId(local_user.getId()));
        Document replacement = new Document("$set", local_user.getRegisterDocument());
        // We update some fields of the documents without affecting the rest
        coll.updateOne(search, replacement);
        mongoClient.close();
        return 0; //Successfully saved
    } catch (Exception e) {
        return 2; // Error
    }
}
 
Example #12
Source File: ConnectorValidationTest.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
private void dropUserAndRoles() {
  if (isAuthEnabled()) {
    List<MongoDatabase> databases =
        asList(
            getMongoClient().getDatabase(getConnectionString().getCredential().getSource()),
            getMongoClient().getDatabase(CUSTOM_DATABASE));

    for (final MongoDatabase database : databases) {
      tryAndIgnore(
          () -> database.runCommand(Document.parse(format("{dropUser: '%s'}", CUSTOM_USER))));
      tryAndIgnore(
          () -> database.runCommand(Document.parse(format("{dropRole: '%s'}", CUSTOM_ROLE))));
      tryAndIgnore(() -> database.runCommand(Document.parse("{invalidateUserCache: 1}")));
    }
  }
}
 
Example #13
Source File: MongoCompensableLock.java    From ByteTCC with GNU Lesser General Public License v3.0 6 votes vote down vote up
private String getTransactionOwnerInMongoDB(TransactionXid transactionXid) {
	byte[] global = transactionXid.getGlobalTransactionId();
	String instanceId = ByteUtils.byteArrayToString(global);

	try {
		String application = CommonUtils.getApplication(this.endpoint);
		String databaseName = application.replaceAll("\\W", "_");
		MongoDatabase mdb = this.mongoClient.getDatabase(databaseName);
		MongoCollection<Document> collection = mdb.getCollection(CONSTANTS_TB_LOCKS);

		FindIterable<Document> findIterable = collection.find(Filters.eq(CONSTANTS_FD_GLOBAL, instanceId));
		MongoCursor<Document> cursor = findIterable.iterator();
		if (cursor.hasNext()) {
			Document document = cursor.next();
			return document.getString("identifier");
		} else {
			return null;
		}
	} catch (RuntimeException rex) {
		logger.error("Error occurred while querying the lock-owner of transaction(gxid= {}).", instanceId, rex);
		return null;
	}
}
 
Example #14
Source File: ChangeEntryDaoTest.java    From mongobee with Apache License 2.0 6 votes vote down vote up
@Test(expected = MongobeeLockException.class)
public void shouldThrowLockExceptionIfThrowExceptionIsTrue() throws Exception {
  // given
  MongoClient mongoClient = mock(MongoClient.class);
  MongoDatabase db = new Fongo(TEST_SERVER).getDatabase(DB_NAME);
  when(mongoClient.getDatabase(anyString())).thenReturn(db);

  ChangeEntryDao dao = new ChangeEntryDao(CHANGELOG_COLLECTION_NAME, LOCK_COLLECTION_NAME, WAIT_FOR_LOCK,
      CHANGE_LOG_LOCK_WAIT_TIME, CHANGE_LOG_LOCK_POLL_RATE, true);

  LockDao lockDao = mock(LockDao.class);
  when(lockDao.acquireLock(any(MongoDatabase.class))).thenReturn(false);
  dao.setLockDao(lockDao);

  dao.connectMongoDb(mongoClient, DB_NAME);

  // when
  boolean hasLock = dao.acquireProcessLock();

  // then
  assertFalse(hasLock);
}
 
Example #15
Source File: RepositorySetup.java    From immutables with Apache License 2.0 5 votes vote down vote up
private RepositorySetup(ListeningExecutorService executor, MongoDatabase database,
                        CodecRegistry codecRegistry,
                        FieldNamingStrategy fieldNamingStrategy) {
  this.executor = executor;
  this.database = database;
  this.codecRegistry = codecRegistry;
  this.fieldNamingStrategy = Preconditions.checkNotNull(fieldNamingStrategy, "fieldNamingStrategy");
}
 
Example #16
Source File: TopicModelTrainer.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
  super.initialize(context);

  MongoDatabase db = mongo.getDB();
  documentsCollection = db.getCollection(documentCollectionName);
  stopwords = stopwordResource.getStopwords(stoplist);
}
 
Example #17
Source File: MongoDbIO.java    From beam with Apache License 2.0 5 votes vote down vote up
private long getEstimatedSizeBytes(
    MongoClient mongoClient, String database, String collection) {
  MongoDatabase mongoDatabase = mongoClient.getDatabase(database);

  // get the Mongo collStats object
  // it gives the size for the entire collection
  BasicDBObject stat = new BasicDBObject();
  stat.append("collStats", collection);
  Document stats = mongoDatabase.runCommand(stat);

  return stats.get("size", Number.class).longValue();
}
 
Example #18
Source File: MongoImpl.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
private MongoDatabase createDatabase(CodecRegistry registry) {
    if (uri == null) throw new Error("uri must not be null");
    String database = uri.getDatabase();
    if (database == null) throw new Error("uri must have database, uri=" + uri);
    var watch = new StopWatch();
    try {
        connectionPoolSettings.maxWaitTime(timeoutInMs, TimeUnit.MILLISECONDS); // pool checkout timeout
        var socketSettings = SocketSettings.builder()
                                           .connectTimeout((int) timeoutInMs, TimeUnit.MILLISECONDS)
                                           .readTimeout((int) timeoutInMs, TimeUnit.MILLISECONDS)
                                           .build();
        var clusterSettings = ClusterSettings.builder()
                                             .serverSelectionTimeout(timeoutInMs * 3, TimeUnit.MILLISECONDS)    // able to try 3 servers
                                             .build();
        var settings = MongoClientSettings.builder()
                                          .applicationName(LogManager.APP_NAME)
                                          .codecRegistry(registry)
                                          .applyToConnectionPoolSettings(builder -> builder.applySettings(connectionPoolSettings.build()))
                                          .applyToSocketSettings(builder -> builder.applySettings(socketSettings))
                                          .applyToClusterSettings(builder -> builder.applySettings(clusterSettings))
                                          .applyConnectionString(uri)
                                          .build();
        mongoClient = MongoClients.create(settings);
        return mongoClient.getDatabase(database);
    } finally {
        logger.info("create mongo client, uri={}, elapsed={}", uri, watch.elapsed());
    }
}
 
Example #19
Source File: MongoDBSourceIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
private void insertDocsWithDateField(String collectionName) throws Exception
{
  MongoClient mongo = new MongoClient(mongoContainerIp, mongoContainerMappedPort);
  MongoDatabase db = mongo.getDatabase(DATABASE_NAME);

  MongoCollection<Document> collection = db.getCollection(collectionName);
  collection.insertOne(new Document("date", dateFormatter.parse("2015-06-01 00:00:00")));
  collection.insertOne(new Document("date", dateFormatter.parse("2015-06-02 00:00:00")));
  collection.insertOne(new Document("date", dateFormatter.parse("2015-06-03 00:00:00")));

  mongo.close();
}
 
Example #20
Source File: LockDaoTest.java    From mongobee with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldGetLockWhenNotPreviouslyHeld() throws Exception {

  // given
  MongoDatabase db = new Fongo(TEST_SERVER).getDatabase(DB_NAME);
  LockDao dao = new LockDao(LOCK_COLLECTION_NAME);
  dao.intitializeLock(db);

  // when
  boolean hasLock = dao.acquireLock(db);

  // then
  assertTrue(hasLock);

}
 
Example #21
Source File: JaversMongoAutoConfiguration.java    From javers with Apache License 2.0 5 votes vote down vote up
private MongoRepository createMongoRepository(MongoDatabase mongoDatabase) {
    if (javersMongoProperties.isDocumentDbCompatibilityEnabled()) {
        logger.info("enabling Amazon DocumentDB compatibility");
        return mongoRepositoryWithDocumentDBCompatibility(mongoDatabase, javersMongoProperties.getSnapshotsCacheSize());
    }
    return new MongoRepository(mongoDatabase, javersMongoProperties.getSnapshotsCacheSize());
}
 
Example #22
Source File: MongoOperations.java    From quarkus with Apache License 2.0 5 votes vote down vote up
private static MongoDatabase mongoDatabase(MongoEntity entity) {
    MongoClient mongoClient = mongoClient(entity);
    if (entity != null && !entity.database().isEmpty()) {
        return mongoClient.getDatabase(entity.database());
    }
    String databaseName = getDefaultDatabaseName();
    return mongoClient.getDatabase(databaseName);
}
 
Example #23
Source File: MongoDbFeatureExecutorTest.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testCleanupFeatureExecution() throws DbFeatureException {
    // GIVEN
    when(cleanupStrategy.provide(any(CleanupStrategy.StrategyProvider.class))).thenReturn(cleanupStrategyExecutor);
    final List<Document> initialDataSets = Arrays.asList(new Document());

    // WHEN
    final DbFeature<MongoDatabase> feature = featureExecutor.createCleanupFeature(cleanupStrategy, initialDataSets);
    assertThat(feature, notNullValue());
    feature.execute(connection);

    // THEN
    verify(cleanupStrategyExecutor).execute(eq(connection), eq(initialDataSets));
}
 
Example #24
Source File: MongoDBRyaDAOIT.java    From rya with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd() throws RyaDAOException, MongoException, IOException {
    final MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();

        final RyaStatementBuilder builder = new RyaStatementBuilder();
        builder.setPredicate(new RyaIRI("http://temp.com"));
        builder.setSubject(new RyaIRI("http://subject.com"));
        builder.setObject(new RyaIRI("http://object.com"));
        builder.setColumnVisibility(new DocumentVisibility("B").flatten());

        final MongoDatabase db = conf.getMongoClient().getDatabase(conf.get(MongoDBRdfConfiguration.MONGO_DB_NAME));
        final MongoCollection<Document> coll = db.getCollection(conf.getTriplesCollectionName());

        dao.add(builder.build());

        assertEquals(coll.countDocuments(), 1);

        final Document doc = coll.find().first();
        assertTrue(doc.containsKey(DOCUMENT_VISIBILITY));
        assertTrue(doc.containsKey(TIMESTAMP));
    }  finally {
        dao.destroy();
    }
}
 
Example #25
Source File: MongoSourceConnectorTest.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Ensure source can survive a restart when copying existing")
void testSourceSurvivesARestartWhenCopyingExisting() {
  assumeTrue(isGreaterThanThreeDotSix());
  MongoDatabase db = getDatabaseWithPostfix();
  MongoCollection<Document> coll0 = db.getCollection("coll0");
  MongoCollection<Document> coll1 = db.getCollection("coll1");

  insertMany(rangeClosed(1, 100000), coll0);
  insertMany(rangeClosed(1, 5000), coll1);

  Properties sourceProperties = new Properties();
  sourceProperties.put(MongoSourceConfig.DATABASE_CONFIG, db.getName());
  sourceProperties.put(MongoSourceConfig.POLL_MAX_BATCH_SIZE_CONFIG, "100");
  sourceProperties.put(MongoSourceConfig.POLL_AWAIT_TIME_MS_CONFIG, "1000");
  sourceProperties.put(MongoSourceConfig.COPY_EXISTING_CONFIG, "true");
  sourceProperties.put(MongoSourceConfig.COPY_EXISTING_MAX_THREADS_CONFIG, "1");
  sourceProperties.put(MongoSourceConfig.COPY_EXISTING_QUEUE_SIZE_CONFIG, "5");

  addSourceConnector(sourceProperties);
  restartSourceConnector(sourceProperties);

  insertMany(rangeClosed(10001, 10050), coll1);

  List<ChangeStreamOperation> inserts = createInserts(1, 5000);
  inserts.addAll(createInserts(10001, 10050));

  assertEventuallyProduces(inserts, coll1);
}
 
Example #26
Source File: CamelSourceMongoDBITCase.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() {
    mongoClient = mongoDBService.getClient();

    MongoDatabase database = mongoClient.getDatabase("testDatabase");

    /*
     The consume operation needs taliable cursors which require capped
     collections
     */
    CreateCollectionOptions options = new CreateCollectionOptions();
    options.capped(true);
    options.sizeInBytes(1024 * 1024);

    database.createCollection("testCollection", options);

    MongoCollection<Document> collection = database.getCollection("testCollection");

    List<Document> documents = new ArrayList<>(expect);
    for (int i = 0; i < expect; i++) {
        Document doc = new Document();

        doc.append("name", "test");
        doc.append("value", "value " + i);

        documents.add(doc);
    }

    collection.insertMany(documents);
}
 
Example #27
Source File: MongoDbChangeAuditDaoIT.java    From obevo with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    this.mongoClient = MongoClientFactory.getInstance().getMongoClient(MongoDbTestHelper.HOST, MongoDbTestHelper.PORT);
    MongoDatabase mydb = mongoClient.getDatabase("mydb");
    mydb.getCollection("ARTIFACTDEPLOYMENT").drop();
    mydb.getCollection("ARTIFACTEXECUTION").drop();
}
 
Example #28
Source File: CleanProjectList.java    From repairnator with MIT License 5 votes vote down vote up
public static void main(String[] args) throws IOException {
    String projectPath = args[0];
    String dbUrl = args[1];
    String dbName = args[2];
    String collectionName = args[3];
    String destList = args[4];

    List<String> allProjects = Files.readAllLines(new File(projectPath).toPath());
    MongoConnection mongoConnection = new MongoConnection(dbUrl, dbName);
    MongoDatabase database = mongoConnection.getMongoDatabase();
    MongoCollection collection = database.getCollection(collectionName);

    List<String> selectedProjects = new ArrayList<>();
    for (String project : allProjects) {
        Repository repo = RepositoryHelper.getRepositoryFromSlug(project);
        if (repo != null) {
            Build b = repo.getLastBuild(false);
            if (b != null) {
                if (b.getBuildTool() == BuildTool.MAVEN) {
                    long results = collection.count(and(
                            eq("repositoryName", project),
                            ne("typeOfFailures", null)
                    ));

                    if (results > 0) {
                        selectedProjects.add(project);
                    }
                }
            }
        }
    }

    File outputFile = new File(destList);
    BufferedWriter buffer = new BufferedWriter(new FileWriter(outputFile));
    buffer.write(StringUtils.join(selectedProjects,"\n"));
    buffer.close();

    System.out.println("Read projects: "+allProjects.size()+" | Selected projects : "+selectedProjects.size());
    System.out.println(StringUtils.join(selectedProjects, "\n"));
}
 
Example #29
Source File: MongoBackendImpl.java    From fiware-cygnus with GNU Affero General Public License v3.0 5 votes vote down vote up
private void insertContextDataAggregatedForResoultion(String dbName, String collectionName,
        GregorianCalendar calendar, String entityId, String entityType, String attrName, String attrType,
        HashMap<String, Integer> counts, Resolution resolution) {
    // Get database and collection
    MongoDatabase db = getDatabase(dbName);
    MongoCollection collection = db.getCollection(collectionName);

    // Build the query
    BasicDBObject query = buildQueryForInsertAggregated(calendar, entityId, entityType, attrName, resolution);

    // Prepopulate if needed
    BasicDBObject insert = buildInsertForPrepopulate(attrType, resolution, false);
    UpdateResult res = collection.updateOne(query, insert, new UpdateOptions().upsert(true));

    if (res.getMatchedCount() == 0) {
        LOGGER.debug("Prepopulating data, database=" + dbName + ", collection=" + collectionName + ", query="
                + query.toString() + ", insert=" + insert.toString());
    } // if

    // Do the update
    for (String key : counts.keySet()) {
        int count = counts.get(key);
        BasicDBObject update = buildUpdateForUpdate(attrType, resolution, calendar, key, count);
        LOGGER.debug("Updating data, database=" + dbName + ", collection=" + collectionName + ", query="
                + query.toString() + ", update=" + update.toString());
        collection.updateOne(query, update);
    } // for
}
 
Example #30
Source File: CleanupStrategyProviderIT.java    From jpa-unit with Apache License 2.0 5 votes vote down vote up
@Test
public void testStrictCleanupWithInitialDataSets() throws DbFeatureException {
    // GIVEN
    final CleanupStrategyExecutor<MongoDatabase, Document> strategyExecutor = provider.strictStrategy();
    assertThat(strategyExecutor, notNullValue());

    // WHEN
    strategyExecutor.execute(connection, Arrays.asList(initialDataSet));

    // THEN
    assertThat(connection.getCollection("JSON_COLLECTION_1").count(), equalTo(0l));
    assertThat(connection.getCollection("JSON_COLLECTION_2").count(), equalTo(0l));
    assertThat(connection.getCollection("JSON_COLLECTION_3").count(), equalTo(0l));
}