Search in sources :

Example 11 with MongoCollection

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

the class MongoGroupScan method init.

@SuppressWarnings({ "rawtypes" })
private void init() {
    List<String> h = storagePluginConfig.getHosts();
    List<ServerAddress> addresses = Lists.newArrayList();
    for (String host : h) {
        addresses.add(new ServerAddress(host));
    }
    MongoClient client = storagePlugin.getClient();
    chunksMapping = Maps.newHashMap();
    chunksInverseMapping = Maps.newLinkedHashMap();
    if (useAggregate && isShardedCluster(client)) {
        handleUnshardedCollection(getPrimaryShardInfo());
    } else if (isShardedCluster(client)) {
        MongoDatabase db = client.getDatabase(CONFIG);
        MongoCollection<Document> chunksCollection = db.getCollection(CHUNKS);
        Document filter = new Document();
        filter.put(NS, this.scanSpec.getDbName() + "." + this.scanSpec.getCollectionName());
        Document projection = new Document();
        projection.put(SHARD, SELECT);
        projection.put(MIN, SELECT);
        projection.put(MAX, SELECT);
        FindIterable<Document> chunkCursor = chunksCollection.find(filter).projection(projection);
        MongoCursor<Document> iterator = chunkCursor.iterator();
        MongoCollection<Document> shardsCollection = db.getCollection(SHARDS);
        projection = new Document();
        projection.put(HOST, SELECT);
        boolean hasChunks = false;
        while (iterator.hasNext()) {
            Document chunkObj = iterator.next();
            String shardName = (String) chunkObj.get(SHARD);
            // creates hexadecimal string representation of ObjectId
            String chunkId = chunkObj.get(ID).toString();
            filter = new Document(ID, shardName);
            FindIterable<Document> hostCursor = shardsCollection.find(filter).projection(projection);
            for (Document hostObj : hostCursor) {
                String hostEntry = (String) hostObj.get(HOST);
                String[] tagAndHost = StringUtils.split(hostEntry, '/');
                String[] hosts = tagAndHost.length > 1 ? StringUtils.split(tagAndHost[1], ',') : StringUtils.split(tagAndHost[0], ',');
                Set<ServerAddress> addressList = getPreferredHosts(storagePlugin.getClient(addresses));
                if (addressList == null) {
                    addressList = Sets.newHashSet();
                    for (String host : hosts) {
                        addressList.add(new ServerAddress(host));
                    }
                }
                chunksMapping.put(chunkId, addressList);
                ServerAddress address = addressList.iterator().next();
                List<ChunkInfo> chunkList = chunksInverseMapping.computeIfAbsent(address.getHost(), k -> new ArrayList<>());
                List<String> chunkHostsList = new ArrayList<>();
                for (ServerAddress serverAddr : addressList) {
                    chunkHostsList.add(serverAddr.toString());
                }
                ChunkInfo chunkInfo = new ChunkInfo(chunkHostsList, chunkId);
                Document minMap = (Document) chunkObj.get(MIN);
                Map<String, Object> minFilters = Maps.newHashMap();
                Set keySet = minMap.keySet();
                for (Object keyObj : keySet) {
                    Object object = minMap.get(keyObj);
                    if (!(object instanceof MinKey)) {
                        minFilters.put(keyObj.toString(), object);
                    }
                }
                chunkInfo.setMinFilters(minFilters);
                Map<String, Object> maxFilters = Maps.newHashMap();
                Map maxMap = (Document) chunkObj.get(MAX);
                keySet = maxMap.keySet();
                for (Object keyObj : keySet) {
                    Object object = maxMap.get(keyObj);
                    if (!(object instanceof MaxKey)) {
                        maxFilters.put(keyObj.toString(), object);
                    }
                }
                chunkInfo.setMaxFilters(maxFilters);
                chunkList.add(chunkInfo);
            }
            hasChunks = true;
        }
        // unsharded collection and it will be stored in the primary shard of that database.
        if (!hasChunks) {
            handleUnshardedCollection(getPrimaryShardInfo());
        }
    } else {
        handleUnshardedCollection(storagePluginConfig.getHosts());
    }
}
Also used : Document(org.bson.Document) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) DrillbitEndpoint(org.apache.drill.exec.proto.CoordinationProtos.DrillbitEndpoint) PriorityQueue(java.util.PriorityQueue) LoggerFactory(org.slf4j.LoggerFactory) Sets(org.apache.drill.shaded.guava.com.google.common.collect.Sets) MongoDatabase(com.mongodb.client.MongoDatabase) VisibleForTesting(org.apache.drill.shaded.guava.com.google.common.annotations.VisibleForTesting) StringUtils(org.apache.commons.lang3.StringUtils) PhysicalOperator(org.apache.drill.exec.physical.base.PhysicalOperator) Map(java.util.Map) Joiner(org.apache.drill.shaded.guava.com.google.common.base.Joiner) JacksonInject(com.fasterxml.jackson.annotation.JacksonInject) MongoSubScanSpec(org.apache.drill.exec.store.mongo.MongoSubScan.MongoSubScanSpec) MaxKey(org.bson.types.MaxKey) SchemaPath(org.apache.drill.common.expression.SchemaPath) Set(java.util.Set) Objects(java.util.Objects) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Entry(java.util.Map.Entry) Stopwatch(org.apache.drill.shaded.guava.com.google.common.base.Stopwatch) Preconditions(org.apache.drill.shaded.guava.com.google.common.base.Preconditions) Queue(java.util.Queue) ReadPreference(com.mongodb.ReadPreference) StoragePluginRegistry(org.apache.drill.exec.store.StoragePluginRegistry) MongoClient(com.mongodb.client.MongoClient) PlanStringBuilder(org.apache.drill.common.PlanStringBuilder) MongoCollection(com.mongodb.client.MongoCollection) DrillRuntimeException(org.apache.drill.common.exceptions.DrillRuntimeException) AbstractGroupScan(org.apache.drill.exec.physical.base.AbstractGroupScan) ShardedMongoSubScanSpec(org.apache.drill.exec.store.mongo.MongoSubScan.ShardedMongoSubScanSpec) ArrayList(java.util.ArrayList) GroupScanProperty(org.apache.drill.exec.physical.base.ScanStats.GroupScanProperty) Bson(org.bson.conversions.Bson) Maps(org.apache.drill.shaded.guava.com.google.common.collect.Maps) JsonTypeName(com.fasterxml.jackson.annotation.JsonTypeName) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) MongoCursor(com.mongodb.client.MongoCursor) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) MinKey(org.bson.types.MinKey) DocumentCodec(org.bson.codecs.DocumentCodec) LinkedList(java.util.LinkedList) ServerAddress(com.mongodb.ServerAddress) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) ScanStats(org.apache.drill.exec.physical.base.ScanStats) TimeUnit(java.util.concurrent.TimeUnit) Lists(org.apache.drill.shaded.guava.com.google.common.collect.Lists) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) EndpointAffinity(org.apache.drill.exec.physical.EndpointAffinity) GroupScan(org.apache.drill.exec.physical.base.GroupScan) ChunkInfo(org.apache.drill.exec.store.mongo.common.ChunkInfo) Comparator(java.util.Comparator) Collections(java.util.Collections) Set(java.util.Set) ChunkInfo(org.apache.drill.exec.store.mongo.common.ChunkInfo) ServerAddress(com.mongodb.ServerAddress) ArrayList(java.util.ArrayList) MaxKey(org.bson.types.MaxKey) FindIterable(com.mongodb.client.FindIterable) Document(org.bson.Document) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) MinKey(org.bson.types.MinKey) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) MongoCursor(com.mongodb.client.MongoCursor) Map(java.util.Map) BsonTypeClassMap(org.bson.codecs.BsonTypeClassMap) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 12 with MongoCollection

use of com.mongodb.client.MongoCollection in project legendarybot by greatman.

the class WoWLinkPlugin method start.

@Override
public void start() {
    // Load the configuration
    props = new Properties();
    try {
        props.load(new FileInputStream("app.properties"));
    } catch (java.io.IOException e) {
        e.printStackTrace();
        getBot().getStacktraceHandler().sendStacktrace(e);
    }
    path("/auth", () -> get("/battlenetcallback", (req, res) -> {
        String state = req.queryParams("state");
        String region = state.split(":")[0];
        OAuth20Service service = new ServiceBuilder(props.getProperty("battlenetoauth.key")).apiSecret(props.getProperty("battlenetoauth.secret")).scope("wow.profile").callback("https://legendarybot.greatmancode.com/auth/battlenetcallback").build(new OAuthBattleNetApi(region));
        String oAuthCode = req.queryParams("code");
        // TODO: Save oauth code to do a character refresh.
        OAuth2AccessToken token = service.getAccessToken(oAuthCode);
        OAuthRequest request = new OAuthRequest(Verb.GET, "https://" + region + ".api.battle.net/wow/user/characters");
        service.signRequest(token, request);
        Response response = service.execute(request);
        JSONParser parser = new JSONParser();
        JSONObject obj = (JSONObject) parser.parse(response.getBody());
        JSONArray charactersArray = (JSONArray) obj.get("characters");
        List<WoWCharacter> characterList = new ArrayList<>();
        charactersArray.forEach((c) -> {
            JSONObject jsonObject = (JSONObject) c;
            if (jsonObject.containsKey("guild")) {
                characterList.add(new WoWCharacter((String) jsonObject.get("name"), ((String) jsonObject.get("realm")).toLowerCase(), (String) jsonObject.get("guild"), region, HeroClass.values()[((Long) jsonObject.get("class")).intValue()]));
                log.info("User " + state.split(":")[1] + " user have the character " + jsonObject.get("name") + " in guild " + jsonObject.get("guild"));
            }
        });
        if (characterList.size() > 0) {
            MongoCollection<Document> collection = getBot().getMongoDatabase().getCollection(MONGO_WOW_CHARACTERS_COLLECTION);
            characterList.forEach((c) -> collection.updateOne(and(eq("region", c.getRegion()), eq("realm", c.getRealm()), eq("name", c.getCharacterName())), and(set("guild", c.getGuild()), set("owner", state.split(":")[1])), new UpdateOptions().upsert(true)));
        }
        return "Your WoW characters are now synced to LegendaryBot!";
    }));
    getBot().getCommandHandler().addCommand("linkwowchars", new LinkWoWCharsCommand(this), "World of Warcraft Character");
    getBot().getCommandHandler().addCommand("guildchars", new GuildCharsCommand(this), "World of Warcraft Character");
    getBot().getCommandHandler().addCommand("setmainchar", new SetMainCharacterCommand(this), "World of Warcraft Character");
    getBot().getCommandHandler().addCommand("enableautorank", new EnableAutoRankCommand(this), "WoW Admin Commands");
    getBot().getCommandHandler().addCommand("disableautorank", new DisableAutoRankCommand(this), "WoW Admin Commands");
    getBot().getCommandHandler().addCommand("setwowrank", new SetWoWRankCommand(this), "WoW Admin Commands");
    getBot().getCommandHandler().addCommand("syncrank", new SyncRankCommand(this), "World of Warcraft Character");
    getBot().getCommandHandler().addCommand("syncguild", new SyncGuildCommand(this), "WoW Admin Commands");
    getBot().getCommandHandler().addCommand("enableautorankupdate", new EnableAutoRankUpdateCommand(this), "WoW Admin Commands");
    getBot().getCommandHandler().addCommand("disableautorankupdate", new DisableAutoRankUpdateCommand(this), "WoW Admin Commands");
    // We load the scheduler
    getBot().getJDA().forEach((jda -> {
        jda.getGuilds().forEach(guild -> {
            if (getBot().getGuildSettings(guild).getSetting(SETTING_SCHEDULER) != null && getBot().getGuildSettings(guild).getSetting(SETTING_RANKSET_ENABLED) != null) {
                scheduler.put(guild.getId(), new SyncRankScheduler(this, guild));
            }
        });
    }));
}
Also used : OAuthBattleNetApi(com.greatmancode.legendarybot.plugins.wowlink.utils.OAuthBattleNetApi) Document(org.bson.Document) Spark.get(spark.Spark.get) java.util(java.util) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) WoWCharacter(com.greatmancode.legendarybot.plugins.wowlink.utils.WoWCharacter) MongoCollection(com.mongodb.client.MongoCollection) LegendaryBotPlugin(com.greatmancode.legendarybot.api.plugin.LegendaryBotPlugin) LoggerFactory(org.slf4j.LoggerFactory) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) BattleNetAPIInterceptor(com.greatmancode.legendarybot.api.utils.BattleNetAPIInterceptor) HeroClass(com.greatmancode.legendarybot.api.utils.HeroClass) Spark.path(spark.Spark.path) JSONArray(org.json.simple.JSONArray) PluginWrapper(org.pf4j.PluginWrapper) com.greatmancode.legendarybot.plugins.wowlink.commands(com.greatmancode.legendarybot.plugins.wowlink.commands) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) Filters.and(com.mongodb.client.model.Filters.and) ParseException(org.json.simple.parser.ParseException) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) GuildSettings(com.greatmancode.legendarybot.api.server.GuildSettings) ResultSet(java.sql.ResultSet) Filters.eq(com.mongodb.client.model.Filters.eq) UpdateOptions(com.mongodb.client.model.UpdateOptions) Updates.unset(com.mongodb.client.model.Updates.unset) Role(net.dv8tion.jda.core.entities.Role) Request(okhttp3.Request) Logger(org.slf4j.Logger) JSONParser(org.json.simple.parser.JSONParser) Verb(com.github.scribejava.core.model.Verb) IOException(java.io.IOException) Filters.exists(com.mongodb.client.model.Filters.exists) FileInputStream(java.io.FileInputStream) Updates.set(com.mongodb.client.model.Updates.set) Guild(net.dv8tion.jda.core.entities.Guild) OkHttpClient(okhttp3.OkHttpClient) JSONObject(org.json.simple.JSONObject) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Block(com.mongodb.Block) User(net.dv8tion.jda.core.entities.User) Response(com.github.scribejava.core.model.Response) HttpUrl(okhttp3.HttpUrl) Spark(spark.Spark) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) UpdateOptions(com.mongodb.client.model.UpdateOptions) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service) ServiceBuilder(com.github.scribejava.core.builder.ServiceBuilder) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) WoWCharacter(com.greatmancode.legendarybot.plugins.wowlink.utils.WoWCharacter) JSONArray(org.json.simple.JSONArray) IOException(java.io.IOException) OAuthBattleNetApi(com.greatmancode.legendarybot.plugins.wowlink.utils.OAuthBattleNetApi) FileInputStream(java.io.FileInputStream) Response(com.github.scribejava.core.model.Response) MongoCollection(com.mongodb.client.MongoCollection) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser)

Example 13 with MongoCollection

use of com.mongodb.client.MongoCollection in project logging-log4j2 by apache.

the class MongoDb4Connection method getOrCreateMongoCollection.

private static MongoCollection<Document> getOrCreateMongoCollection(final MongoDatabase database, final String collectionName, final boolean isCapped, final Integer sizeInBytes) {
    try {
        LOGGER.debug("Getting collection '{}'...", collectionName);
        // throws IllegalArgumentException if collectionName is invalid
        final MongoCollection<Document> found = database.getCollection(collectionName);
        LOGGER.debug("Got collection {}", found);
        return found;
    } catch (final IllegalStateException e) {
        LOGGER.debug("Collection '{}' does not exist.", collectionName);
        final CreateCollectionOptions options = new CreateCollectionOptions().capped(isCapped).sizeInBytes(sizeInBytes);
        LOGGER.debug("Creating collection '{}' with options {}...", collectionName, options);
        database.createCollection(collectionName, options);
        LOGGER.debug("Created collection.");
        final MongoCollection<Document> created = database.getCollection(collectionName);
        LOGGER.debug("Got created collection {}", created);
        return created;
    }
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) Document(org.bson.Document)

Example 14 with MongoCollection

use of com.mongodb.client.MongoCollection in project morphia by mongodb.

the class TestDatastore method testBulkInsert.

@Test
public void testBulkInsert() {
    MongoCollection collection = getDs().getCollection(TestEntity.class);
    this.getDs().insert(asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertManyOptions().writeConcern(WriteConcern.ACKNOWLEDGED));
    assertEquals(collection.countDocuments(), 5);
    collection.drop();
    this.getDs().insert(asList(new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity(), new TestEntity()), new InsertManyOptions().writeConcern(WriteConcern.ACKNOWLEDGED));
    assertEquals(collection.countDocuments(), 5);
}
Also used : TestEntity(dev.morphia.test.models.TestEntity) MongoCollection(com.mongodb.client.MongoCollection) InsertManyOptions(dev.morphia.InsertManyOptions) Test(org.testng.annotations.Test)

Example 15 with MongoCollection

use of com.mongodb.client.MongoCollection in project morphia by mongodb.

the class TestDatastore method testInsert.

@Test
public void testInsert() {
    MongoCollection collection = getDs().getCollection(TestEntity.class);
    this.getDs().insert(new TestEntity());
    assertEquals(collection.countDocuments(), 1);
    this.getDs().insert(new TestEntity(), new InsertOneOptions().writeConcern(WriteConcern.ACKNOWLEDGED));
    assertEquals(collection.countDocuments(), 2);
}
Also used : TestEntity(dev.morphia.test.models.TestEntity) MongoCollection(com.mongodb.client.MongoCollection) InsertOneOptions(dev.morphia.InsertOneOptions) Test(org.testng.annotations.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