Search in sources :

Example 26 with MongoCollection

use of com.mongodb.client.MongoCollection in project nifi by apache.

the class RunMongoAggregation method onTrigger.

@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
    FlowFile flowFile = null;
    if (context.hasIncomingConnection()) {
        flowFile = session.get();
        if (flowFile == null && context.hasNonLoopConnection()) {
            return;
        }
    }
    String query = context.getProperty(QUERY).evaluateAttributeExpressions(flowFile).getValue();
    String queryAttr = context.getProperty(QUERY_ATTRIBUTE).evaluateAttributeExpressions(flowFile).getValue();
    Integer batchSize = context.getProperty(BATCH_SIZE).asInteger();
    Integer resultsPerFlowfile = context.getProperty(RESULTS_PER_FLOWFILE).asInteger();
    Map attrs = new HashMap();
    if (queryAttr != null && queryAttr.trim().length() > 0) {
        attrs.put(queryAttr, query);
    }
    MongoCollection collection = getCollection(context);
    MongoCursor iter = null;
    try {
        List<Bson> aggQuery = buildAggregationQuery(query);
        AggregateIterable it = collection.aggregate(aggQuery);
        it.batchSize(batchSize != null ? batchSize : 1);
        iter = it.iterator();
        List batch = new ArrayList();
        while (iter.hasNext()) {
            batch.add(iter.next());
            if (batch.size() == resultsPerFlowfile) {
                writeBatch(buildBatch(batch), flowFile, context, session, attrs, REL_RESULTS);
                batch = new ArrayList();
            }
        }
        if (batch.size() > 0) {
            writeBatch(buildBatch(batch), flowFile, context, session, attrs, REL_RESULTS);
        }
        if (flowFile != null) {
            session.transfer(flowFile, REL_ORIGINAL);
        }
    } catch (Exception e) {
        getLogger().error("Error running MongoDB aggregation query.", e);
        if (flowFile != null) {
            session.transfer(flowFile, REL_FAILURE);
        }
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException) Bson(org.bson.conversions.Bson) MongoCollection(com.mongodb.client.MongoCollection) ArrayList(java.util.ArrayList) List(java.util.List) MongoCursor(com.mongodb.client.MongoCursor) AggregateIterable(com.mongodb.client.AggregateIterable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with MongoCollection

use of com.mongodb.client.MongoCollection in project LuckPerms by lucko.

the class MongoDao method savePlayerData.

@Override
public PlayerSaveResult savePlayerData(UUID uuid, String username) {
    username = username.toLowerCase();
    MongoCollection<Document> c = this.database.getCollection(this.prefix + "uuid");
    // find any existing mapping
    String oldUsername = getPlayerName(uuid);
    // do the insert
    if (!username.equalsIgnoreCase(oldUsername)) {
        c.replaceOne(new Document("_id", uuid), new Document("_id", uuid).append("name", username), new UpdateOptions().upsert(true));
    }
    PlayerSaveResult result = PlayerSaveResult.determineBaseResult(username, oldUsername);
    Set<UUID> conflicting = new HashSet<>();
    try (MongoCursor<Document> cursor = c.find(new Document("name", username)).iterator()) {
        if (cursor.hasNext()) {
            conflicting.add(cursor.next().get("_id", UUID.class));
        }
    }
    conflicting.remove(uuid);
    if (!conflicting.isEmpty()) {
        // remove the mappings for conflicting uuids
        c.deleteMany(Filters.and(conflicting.stream().map(u -> Filters.eq("_id", u)).collect(Collectors.toList())));
        result = result.withOtherUuidsPresent(conflicting);
    }
    return result;
}
Also used : PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) LogEntry(me.lucko.luckperms.api.LogEntry) Document(org.bson.Document) MongoClientURI(com.mongodb.MongoClientURI) AbstractDao(me.lucko.luckperms.common.storage.dao.AbstractDao) MongoCredential(com.mongodb.MongoCredential) MongoCollection(com.mongodb.client.MongoCollection) UserIdentifier(me.lucko.luckperms.common.references.UserIdentifier) MongoDatabase(com.mongodb.client.MongoDatabase) MutableContextSet(me.lucko.luckperms.api.context.MutableContextSet) BulkUpdate(me.lucko.luckperms.common.bulkupdate.BulkUpdate) ArrayList(java.util.ArrayList) Filters(com.mongodb.client.model.Filters) LegacyNodeFactory(me.lucko.luckperms.common.node.LegacyNodeFactory) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) Strings(com.google.common.base.Strings) PlayerSaveResult(me.lucko.luckperms.common.storage.PlayerSaveResult) NodeModel(me.lucko.luckperms.common.node.NodeModel) MongoCursor(com.mongodb.client.MongoCursor) MongoClientOptions(com.mongodb.MongoClientOptions) Map(java.util.Map) LuckPermsPlugin(me.lucko.luckperms.common.plugin.LuckPermsPlugin) HeldPermission(me.lucko.luckperms.api.HeldPermission) UpdateOptions(com.mongodb.client.model.UpdateOptions) ServerAddress(com.mongodb.ServerAddress) ExtendedLogEntry(me.lucko.luckperms.common.actionlog.ExtendedLogEntry) GroupManager(me.lucko.luckperms.common.managers.group.GroupManager) StorageCredentials(me.lucko.luckperms.common.storage.StorageCredentials) NodeHeldPermission(me.lucko.luckperms.common.node.NodeHeldPermission) Set(java.util.Set) ImmutableContextSet(me.lucko.luckperms.api.context.ImmutableContextSet) Log(me.lucko.luckperms.common.actionlog.Log) NodeFactory(me.lucko.luckperms.common.node.NodeFactory) UUID(java.util.UUID) ContextSet(me.lucko.luckperms.api.context.ContextSet) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) Track(me.lucko.luckperms.common.model.Track) List(java.util.List) TrackManager(me.lucko.luckperms.common.managers.track.TrackManager) MongoClient(com.mongodb.MongoClient) Group(me.lucko.luckperms.common.model.Group) Optional(java.util.Optional) Node(me.lucko.luckperms.api.Node) User(me.lucko.luckperms.common.model.User) Document(org.bson.Document) UUID(java.util.UUID) UpdateOptions(com.mongodb.client.model.UpdateOptions) HashSet(java.util.HashSet)

Example 28 with MongoCollection

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

the class CleanProjectList method main.

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"));
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) Repository(fr.inria.spirals.jtravis.entities.Repository) Build(fr.inria.spirals.jtravis.entities.Build) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) File(java.io.File) MongoConnection(fr.inria.spirals.repairnator.serializer.mongodb.MongoConnection) MongoDatabase(com.mongodb.client.MongoDatabase) BufferedWriter(java.io.BufferedWriter)

Example 29 with MongoCollection

use of com.mongodb.client.MongoCollection in project jackrabbit-oak by apache.

the class MongoUtilsTest method createIndex.

@Test
public void createIndex() {
    MongoConnection c = connectionFactory.getConnection();
    c.getDB().dropDatabase();
    MongoCollection collection = c.getDatabase().getCollection("test");
    MongoUtils.createIndex(collection, "foo", true, false, true);
    MongoUtils.createIndex(collection, "bar", false, true, false);
    MongoUtils.createIndex(collection, new String[] { "baz", "qux" }, new boolean[] { true, false }, false, false);
    assertTrue(MongoUtils.hasIndex(collection, "_id"));
    assertTrue(MongoUtils.hasIndex(collection, "foo"));
    assertFalse(MongoUtils.hasIndex(collection, "foo", "bar"));
    assertTrue(MongoUtils.hasIndex(collection, "baz", "qux"));
    List<Document> indexes = new ArrayList<>();
    collection.listIndexes().into(indexes);
    assertEquals(4, indexes.size());
    for (Document info : indexes) {
        Document key = (Document) info.get("key");
        if (key.keySet().contains("foo")) {
            assertEquals(1, key.keySet().size());
            assertEquals(1, key.get("foo"));
            assertEquals(Boolean.TRUE, info.get("sparse"));
        } else if (key.keySet().contains("bar")) {
            assertEquals(1, key.keySet().size());
            assertEquals(-1, key.get("bar"));
            assertEquals(Boolean.TRUE, info.get("unique"));
        } else if (key.keySet().contains("baz")) {
            assertEquals(2, key.keySet().size());
            assertEquals(1, key.get("baz"));
            assertEquals(-1, key.get("qux"));
        }
    }
    c.getDB().dropDatabase();
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) ArrayList(java.util.ArrayList) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection) Test(org.junit.Test)

Example 30 with MongoCollection

use of com.mongodb.client.MongoCollection in project jackrabbit-oak by apache.

the class MongoUtilsTest method checkArguments.

@Test(expected = IllegalArgumentException.class)
public void checkArguments() {
    MongoConnection c = connectionFactory.getConnection();
    MongoCollection collection = c.getDatabase().getCollection("test");
    MongoUtils.createIndex(collection, new String[] { "foo", "bar" }, new boolean[] { true }, false, true);
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection) Test(org.junit.Test)

Aggregations

MongoCollection (com.mongodb.client.MongoCollection)53 Document (org.bson.Document)39 MongoDatabase (com.mongodb.client.MongoDatabase)21 List (java.util.List)16 FindIterable (com.mongodb.client.FindIterable)14 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)11 BasicDBObject (com.mongodb.BasicDBObject)10 Set (java.util.Set)9 ObjectId (org.bson.types.ObjectId)9 MongoCursor (com.mongodb.client.MongoCursor)7 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Bson (org.bson.conversions.Bson)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 BsonDocument (org.bson.BsonDocument)6 Block (com.mongodb.Block)5 UpdateOptions (com.mongodb.client.model.UpdateOptions)5