Search in sources :

Example 1 with Block

use of com.mongodb.Block in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManagerAsync method select.

@Override
public void select(DocumentQuery query, Consumer<List<DocumentEntity>> callBack) throws ExecuteAsyncQueryException, UnsupportedOperationException {
    String collectionName = query.getDocumentCollection();
    MongoCollection<Document> collection = asyncMongoDatabase.getCollection(collectionName);
    Bson mongoDBQuery = query.getCondition().map(DocumentQueryConversor::convert).orElse(EMPTY);
    List<DocumentEntity> entities = new CopyOnWriteArrayList<>();
    FindIterable<Document> result = collection.find(mongoDBQuery);
    Block<Document> documentBlock = d -> entities.add(createEntity(collectionName, d));
    SingleResultCallback<Void> voidSingleResultCallback = (v, e) -> callBack.accept(entities);
    result.forEach(documentBlock, voidSingleResultCallback);
}
Also used : FindIterable(com.mongodb.async.client.FindIterable) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) ExecuteAsyncQueryException(org.jnosql.diana.api.ExecuteAsyncQueryException) DocumentCollectionManagerAsync(org.jnosql.diana.api.document.DocumentCollectionManagerAsync) MongoCollection(com.mongodb.async.client.MongoCollection) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BsonDocument(org.bson.BsonDocument) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Bson(org.bson.conversions.Bson) Consumer(java.util.function.Consumer) List(java.util.List) Documents(org.jnosql.diana.api.document.Documents) Block(com.mongodb.Block) MongoDatabase(com.mongodb.async.client.MongoDatabase) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) DeleteResult(com.mongodb.client.result.DeleteResult) ID_FIELD(org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD) SingleResultCallback(com.mongodb.async.SingleResultCallback) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Example 2 with Block

use of com.mongodb.Block in project repairnator by Spirals-Team.

the class CheckIfFutureBuildIdWhereWellComputed method main.

public static void main(String[] args) throws IOException {
    List<String> allIds = Files.readAllLines(new File(args[0]).toPath());
    String dbCollectionUrl = args[1];
    String dbName = args[2];
    String collectionName = args[3];
    final List<String> projectsNames = Files.readAllLines(new File(args[4]).toPath());
    String githubLogin = args[5];
    String githubToken = args[6];
    RepairnatorConfig.getInstance().setGithubLogin(githubLogin);
    RepairnatorConfig.getInstance().setGithubToken(githubToken);
    MongoConnection mongoConnection = new MongoConnection(dbCollectionUrl, dbName);
    MongoDatabase database = mongoConnection.getMongoDatabase();
    MongoCollection collection = database.getCollection(collectionName);
    Calendar limitDateMay = Calendar.getInstance();
    limitDateMay.set(2017, Calendar.MAY, 10);
    HashMap<String, List<Integer>> results = new HashMap<>();
    Block<Document> block = new Block<Document>() {

        @Override
        public void apply(Document document) {
            Object pBuildId = document.get("previousBuildId");
            if (pBuildId instanceof Integer) {
                int previousBuildId = document.getInteger("previousBuildId", -1);
                int nextBuildId = document.getInteger("buildId", -1);
                if (previousBuildId != -1 && nextBuildId != -1) {
                    Build previousBuild = BuildHelper.getBuildFromId(previousBuildId, null);
                    Build nextBuild = BuildHelper.getNextBuildOfSameBranchOfStatusAfterBuild(previousBuild, null);
                    if (nextBuild.getId() != nextBuildId) {
                        String projectName = previousBuild.getRepository().getSlug();
                        if (projectsNames == null || projectsNames.contains(projectName)) {
                            if (!results.containsKey(projectName)) {
                                results.put(projectName, new ArrayList<>());
                            }
                            results.get(projectName).add(previousBuildId);
                            i++;
                        }
                    }
                }
            }
        }
    };
    for (String s : allIds) {
        int buildId = Integer.parseInt(s);
        collection.find(and(gt("buildReproductionDate", limitDateMay.getTime()), eq("previousBuildId", buildId), eq("lastReproducedBuggyBuild", true))).forEach(block);
    }
    System.out.println(allIds.size() + " ids read, and got: " + i);
    System.out.println(results.keySet().size() + " detected projects: (" + StringUtils.join(results.keySet(), ",") + ")");
    System.out.println("Results:");
    for (String s : results.keySet()) {
        System.out.println("Project " + s + " : ");
        System.out.println(StringUtils.join(results.get(s), "\n"));
        System.out.println("\n");
    }
}
Also used : HashMap(java.util.HashMap) Calendar(java.util.Calendar) Document(org.bson.Document) MongoCollection(com.mongodb.client.MongoCollection) Build(fr.inria.spirals.jtravis.entities.Build) Block(com.mongodb.Block) ArrayList(java.util.ArrayList) List(java.util.List) File(java.io.File) MongoConnection(fr.inria.spirals.repairnator.serializer.mongodb.MongoConnection) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 3 with Block

use of com.mongodb.Block in project repairnator by Spirals-Team.

the class UpdateDataForSpecifyingLastReproduction method main.

public static void main(String[] args) {
    String dbConnectionUrl = args[0];
    String dbName = args[1];
    String collectionName = args[2];
    MongoConnection mongoConnection = new MongoConnection(dbConnectionUrl, dbName);
    MongoDatabase database = mongoConnection.getMongoDatabase();
    MongoCollection collection = database.getCollection(collectionName);
    Calendar limitDateMay = Calendar.getInstance();
    limitDateMay.set(2017, Calendar.MAY, 10);
    final List<ObjectId> updatedDocs = new ArrayList<>();
    Set<Integer> ids = new HashSet<>();
    Block<Document> block = new Block<Document>() {

        @Override
        public void apply(Document document) {
            ObjectId documentId = document.getObjectId("_id");
            Object pBuildId = document.get("previousBuildId");
            if (pBuildId instanceof Integer) {
                int previousBuildId = document.getInteger("previousBuildId", -1);
                if (previousBuildId != -1) {
                    boolean lastReproducedBuggyBuild = !ids.contains(previousBuildId);
                    ids.add(previousBuildId);
                    document.append("lastReproducedBuggyBuild", lastReproducedBuggyBuild);
                    collection.replaceOne(eq("_id", documentId), document, new UpdateOptions().upsert(true));
                    updatedDocs.add(documentId);
                }
            }
        }
    };
    collection.find().sort(orderBy(descending("buildReproductionDate"))).forEach(block);
    System.out.println("Updated docs: " + updatedDocs.size());
}
Also used : ObjectId(org.bson.types.ObjectId) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions) MongoCollection(com.mongodb.client.MongoCollection) Block(com.mongodb.Block) MongoConnection(fr.inria.spirals.repairnator.serializer.mongodb.MongoConnection) MongoDatabase(com.mongodb.client.MongoDatabase) HashSet(java.util.HashSet)

Example 4 with Block

use of com.mongodb.Block in project repairnator by Spirals-Team.

the class CleanNopolCollection method main.

public static void main(String[] args) {
    Map<Integer, Set<String>> presentInDb = new HashMap<>();
    String dbCollectionUrl = args[0];
    String dbName = args[1];
    String collectionName = args[2];
    MongoConnection mongoConnection = new MongoConnection(dbCollectionUrl, dbName);
    MongoDatabase database = mongoConnection.getMongoDatabase();
    MongoCollection collection = database.getCollection(collectionName);
    Block<Document> block = new Block<Document>() {

        @Override
        public void apply(Document document) {
            int buildId = document.getInteger("buildId");
            String location = document.getString("testClassLocation");
            ObjectId id = document.getObjectId("_id");
            if (presentInDb.containsKey(buildId)) {
                Set<String> localSet = presentInDb.get(buildId);
                if (localSet.contains(location)) {
                    collection.deleteOne(eq("_id", id));
                    counterDeleted++;
                    return;
                } else {
                    localSet.add(location);
                }
            } else {
                Set<String> localSet = new HashSet<>();
                localSet.add(location);
                presentInDb.put(buildId, localSet);
            }
            counterKept++;
        }
    };
    collection.find().forEach(block);
    System.out.println(counterDeleted + " entries deleted and " + counterKept + " kept.");
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ObjectId(org.bson.types.ObjectId) Document(org.bson.Document) MongoCollection(com.mongodb.client.MongoCollection) Block(com.mongodb.Block) MongoConnection(fr.inria.spirals.repairnator.serializer.mongodb.MongoConnection) MongoDatabase(com.mongodb.client.MongoDatabase) HashSet(java.util.HashSet)

Example 5 with Block

use of com.mongodb.Block in project repairnator by Spirals-Team.

the class RemoveDuplicatedBuildsId method main.

public static void main(String[] args) {
    Set<Integer> buildIds = new HashSet<>();
    String dbCollectionUrl = args[0];
    String dbName = args[1];
    String collectionName = args[2];
    MongoConnection mongoConnection = new MongoConnection(dbCollectionUrl, dbName);
    MongoDatabase database = mongoConnection.getMongoDatabase();
    MongoCollection collection = database.getCollection(collectionName);
    Block<Document> block = new Block<Document>() {

        @Override
        public void apply(Document document) {
            int buildId = document.getInteger("buildId");
            ObjectId id = document.getObjectId("_id");
            if (buildIds.contains(buildId)) {
                collection.deleteOne(eq("_id", id));
                counterDeleted++;
                return;
            } else {
                buildIds.add(buildId);
                counterKept++;
            }
        }
    };
    collection.find().sort(orderBy(descending("buildReproductionDate"))).forEach(block);
    System.out.println(counterDeleted + " entries deleted and " + counterKept + " kept.");
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) ObjectId(org.bson.types.ObjectId) Block(com.mongodb.Block) Document(org.bson.Document) MongoConnection(fr.inria.spirals.repairnator.serializer.mongodb.MongoConnection) HashSet(java.util.HashSet) MongoDatabase(com.mongodb.client.MongoDatabase)

Aggregations

Block (com.mongodb.Block)17 Document (org.bson.Document)13 MongoClientSettings (com.mongodb.MongoClientSettings)7 MongoCollection (com.mongodb.client.MongoCollection)7 MongoDatabase (com.mongodb.client.MongoDatabase)6 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 Fixture.getMongoClientSettingsBuilder (com.mongodb.client.Fixture.getMongoClientSettingsBuilder)5 List (java.util.List)5 BsonDocument (org.bson.BsonDocument)5 ServerSettings (com.mongodb.connection.ServerSettings)4 MongoConnection (fr.inria.spirals.repairnator.serializer.mongodb.MongoConnection)4 Map (java.util.Map)4 Test (org.junit.Test)4 ClusterFixture.isServerlessTest (com.mongodb.ClusterFixture.isServerlessTest)3 ConnectionString (com.mongodb.ConnectionString)3 MongoException (com.mongodb.MongoException)3 SocketSettings (com.mongodb.connection.SocketSettings)3 HashSet (java.util.HashSet)3 ObjectId (org.bson.types.ObjectId)3