Search in sources :

Example 36 with MongoCollection

use of com.mongodb.client.MongoCollection in project spring-data-mongodb by spring-projects.

the class SessionBoundMongoTemplateTests method setUp.

@BeforeEach
public void setUp() {
    MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory(client, "session-bound-mongo-template-tests") {

        @Override
        public MongoDatabase getMongoDatabase() throws DataAccessException {
            MongoDatabase spiedDatabse = Mockito.spy(super.getMongoDatabase());
            spiedDatabases.add(spiedDatabse);
            return spiedDatabse;
        }
    };
    session = client.startSession(ClientSessionOptions.builder().build());
    this.template = new MongoTemplate(factory);
    this.sessionBoundTemplate = new SessionBoundMongoTemplate(session, new MongoTemplate(factory, getDefaultMongoConverter(factory))) {

        @Override
        protected MongoCollection<Document> prepareCollection(MongoCollection<Document> collection) {
            injectCollectionSpy(collection);
            return super.prepareCollection(collection);
        }

        @SuppressWarnings({ "ConstantConditions", "unchecked" })
        private void injectCollectionSpy(MongoCollection<Document> collection) {
            InvocationHandler handler = Proxy.getInvocationHandler(collection);
            Advised advised = (Advised) ReflectionTestUtils.getField(handler, "advised");
            for (Advisor advisor : advised.getAdvisors()) {
                Advice advice = advisor.getAdvice();
                if (advice instanceof SessionAwareMethodInterceptor) {
                    MongoCollection<Document> spiedCollection = Mockito.spy((MongoCollection<Document>) ReflectionTestUtils.getField(advice, "target"));
                    spiedCollections.add(spiedCollection);
                    ReflectionTestUtils.setField(advice, "target", spiedCollection);
                }
            }
        }
    };
}
Also used : SessionAwareMethodInterceptor(org.springframework.data.mongodb.SessionAwareMethodInterceptor) Advisor(org.springframework.aop.Advisor) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) Document(org.bson.Document) InvocationHandler(java.lang.reflect.InvocationHandler) MongoCollection(com.mongodb.client.MongoCollection) Advised(org.springframework.aop.framework.Advised) MongoDatabaseFactory(org.springframework.data.mongodb.MongoDatabaseFactory) Advice(org.aopalliance.aop.Advice) SessionBoundMongoTemplate(org.springframework.data.mongodb.core.MongoTemplate.SessionBoundMongoTemplate) MongoDatabase(com.mongodb.client.MongoDatabase) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 37 with MongoCollection

use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.

the class MongoDbRepository method getAllMyPetPlayers.

@Override
@SuppressWarnings("unchecked")
public List<MyPetPlayer> getAllMyPetPlayers() {
    MongoCollection playerCollection = this.db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
    final List<MyPetPlayer> playerList = new ArrayList<>();
    playerCollection.find().forEach((Block<Document>) document -> {
        MyPetPlayer player = documentToPlayer(document);
        if (player != null) {
            playerList.add(player);
        }
    });
    return playerList;
}
Also used : Document(org.bson.Document) java.util(java.util) MongoCollection(com.mongodb.client.MongoCollection) Repository(de.Keyle.MyPet.api.repository.Repository) RepositoryCallback(de.Keyle.MyPet.api.repository.RepositoryCallback) MongoDatabase(com.mongodb.client.MongoDatabase) Player(org.bukkit.entity.Player) TagStream(de.keyle.knbt.TagStream) Lists(com.google.common.collect.Lists) Configuration(de.Keyle.MyPet.api.Configuration) com.mongodb(com.mongodb) MyPetPlayerImpl(de.Keyle.MyPet.util.player.MyPetPlayerImpl) RepositoryInitException(de.Keyle.MyPet.api.repository.RepositoryInitException) Binary(org.bson.types.Binary) ZipException(java.util.zip.ZipException) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) MyPetApi(de.Keyle.MyPet.MyPetApi) IOException(java.io.IOException) InactiveMyPet(de.Keyle.MyPet.entity.InactiveMyPet) Skilltree(de.Keyle.MyPet.api.skill.skilltree.Skilltree) StoredMyPet(de.Keyle.MyPet.api.entity.StoredMyPet) FindIterable(com.mongodb.client.FindIterable) RepositoryMyPetConverterService(de.Keyle.MyPet.api.util.service.types.RepositoryMyPetConverterService) MyPetType(de.Keyle.MyPet.api.entity.MyPetType) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) MyPetVersion(de.Keyle.MyPet.api.MyPetVersion) MongoCollection(com.mongodb.client.MongoCollection) MyPetPlayer(de.Keyle.MyPet.api.player.MyPetPlayer) Document(org.bson.Document)

Example 38 with MongoCollection

use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.

the class MongoDbRepository method initStructure.

@SuppressWarnings("unchecked")
private void initStructure() {
    db.createCollection(Configuration.Repository.MongoDB.PREFIX + "info");
    db.createCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
    db.createCollection(Configuration.Repository.MongoDB.PREFIX + "players");
    MongoCollection petCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "pets");
    petCollection.createIndex(new BasicDBObject("uuid", 1));
    petCollection.createIndex(new BasicDBObject("owner_uuid", 1));
    MongoCollection playerCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
    playerCollection.createIndex(new BasicDBObject("internal_uuid", 1));
    playerCollection.createIndex(new BasicDBObject("offline_uuid", 1));
    playerCollection.createIndex(new BasicDBObject("mojang_uuid", 1));
    Document info = new Document();
    updateInfoDocument(info);
    MongoCollection infoCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "info");
    infoCollection.insertOne(info);
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) Document(org.bson.Document)

Example 39 with MongoCollection

use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.

the class MongoDbRepository method addMyPetPlayer.

@SuppressWarnings("unchecked")
public boolean addMyPetPlayer(MyPetPlayer player) {
    Document playerDocument = new Document();
    setPlayerData(player, playerDocument);
    MongoCollection playerCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
    playerCollection.insertOne(playerDocument);
    return true;
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) Document(org.bson.Document)

Example 40 with MongoCollection

use of com.mongodb.client.MongoCollection in project MyPet by xXKeyleXx.

the class MongoDbRepository method updateToV4.

private void updateToV4() {
    MongoCollection playerCollection = db.getCollection(Configuration.Repository.MongoDB.PREFIX + "players");
    Document filter = new Document();
    Document data = new Document("$set", new Document("last_update", System.currentTimeMillis()));
    playerCollection.updateMany(filter, data);
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) Document(org.bson.Document)

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