Example usage for com.mongodb.client MongoCollection find

List of usage examples for com.mongodb.client MongoCollection find

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection find.

Prototype

FindIterable<TDocument> find(ClientSession clientSession);

Source Link

Document

Finds all documents in the collection.

Usage

From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java

License:Open Source License

@Override
public boolean loadTrack(String name) {
    Track track = plugin.getTrackManager().getOrMake(name);
    track.getIoLock().lock();/*  www.  j a va 2 s .  c  om*/
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("tracks");

            try (MongoCursor<Document> cursor = c.find(new Document("_id", track.getName())).iterator()) {
                if (cursor.hasNext()) {
                    Document d = cursor.next();
                    track.setGroups((List<String>) d.get("groups"));
                    return true;
                }
                return false;
            }
        }, false);
    } finally {
        track.getIoLock().unlock();
    }
}

From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java

License:Open Source License

@Override
public boolean saveUUIDData(String username, UUID uuid) {
    return call(() -> {
        MongoCollection<Document> c = database.getCollection("uuid");

        try (MongoCursor<Document> cursor = c.find(new Document("_id", uuid)).iterator()) {
            if (cursor.hasNext()) {
                c.replaceOne(new Document("_id", uuid),
                        new Document("_id", uuid).append("name", username.toLowerCase()));
            } else {
                c.insertOne(new Document("_id", uuid).append("name", username.toLowerCase()));
            }// www  .  ja v  a2  s.c o  m
        }

        return true;
    }, false);
}

From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java

License:Open Source License

@Override
public UUID getUUID(String username) {
    return call(() -> {
        MongoCollection<Document> c = database.getCollection("uuid");

        try (MongoCursor<Document> cursor = c.find(new Document("name", username.toLowerCase())).iterator()) {
            if (cursor.hasNext()) {
                return cursor.next().get("_id", UUID.class);
            }/*from  www.  j a  v  a 2s  .  c om*/
        }
        return null;
    }, null);
}

From source file:me.lucko.luckperms.common.storage.backing.MongoDBBacking.java

License:Open Source License

@Override
public String getName(UUID uuid) {
    return call(() -> {
        MongoCollection<Document> c = database.getCollection("uuid");

        try (MongoCursor<Document> cursor = c.find(new Document("_id", uuid)).iterator()) {
            if (cursor.hasNext()) {
                return cursor.next().get("name", String.class);
            }//from  w  ww  . ja va2s  . c  o  m
        }
        return null;
    }, null);
}

From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java

License:MIT License

@Override
public boolean loadUser(UUID uuid, String username) {
    User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
    user.getIoLock().lock();/*from ww  w  . j  av  a2s . c  om*/
    try {
        MongoCollection<Document> c = database.getCollection(prefix + "users");
        try (MongoCursor<Document> cursor = c.find(new Document("_id", user.getUuid())).iterator()) {
            if (cursor.hasNext()) {
                // User exists, let's load.
                Document d = cursor.next();

                String name = d.getString("name");
                user.getPrimaryGroup().setStoredValue(d.getString("primaryGroup"));

                Set<Node> nodes = nodesFromDoc(d).stream().map(NodeModel::toNode).collect(Collectors.toSet());
                user.setEnduringNodes(nodes);
                user.setName(name, true);

                boolean save = plugin.getUserManager().giveDefaultIfNeeded(user, false);
                if (user.getName().isPresent()
                        && (name == null || !user.getName().get().equalsIgnoreCase(name))) {
                    save = true;
                }

                if (save) {
                    c.replaceOne(new Document("_id", user.getUuid()), userToDoc(user));
                }
            } else {
                if (GenericUserManager.shouldSave(user)) {
                    user.clearNodes();
                    user.getPrimaryGroup().setStoredValue(null);
                    plugin.getUserManager().giveDefaultIfNeeded(user, false);
                }
            }
        }
    } finally {
        user.getIoLock().unlock();
    }
    user.getRefreshBuffer().requestDirectly();
    return true;
}

From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java

License:MIT License

@Override
public boolean createAndLoadGroup(String name) {
    Group group = plugin.getGroupManager().getOrMake(name);
    group.getIoLock().lock();/*from   w  ww.  ja  v  a 2 s . co m*/
    try {
        MongoCollection<Document> c = database.getCollection(prefix + "groups");
        try (MongoCursor<Document> cursor = c.find(new Document("_id", group.getName())).iterator()) {
            if (cursor.hasNext()) {
                Document d = cursor.next();
                Set<Node> nodes = nodesFromDoc(d).stream().map(NodeModel::toNode).collect(Collectors.toSet());
                group.setEnduringNodes(nodes);
            } else {
                c.insertOne(groupToDoc(group));
            }
        }
    } finally {
        group.getIoLock().unlock();
    }
    group.getRefreshBuffer().requestDirectly();
    return true;
}

From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java

License:MIT License

@Override
public boolean loadGroup(String name) {
    Group group = plugin.getGroupManager().getOrMake(name);
    group.getIoLock().lock();//from  w  w  w  .j  ava 2 s.co m
    try {
        MongoCollection<Document> c = database.getCollection(prefix + "groups");
        try (MongoCursor<Document> cursor = c.find(new Document("_id", group.getName())).iterator()) {
            if (!cursor.hasNext()) {
                return false;
            }

            Document d = cursor.next();
            Set<Node> nodes = nodesFromDoc(d).stream().map(NodeModel::toNode).collect(Collectors.toSet());
            group.setEnduringNodes(nodes);
        }
    } finally {
        group.getIoLock().unlock();
    }
    group.getRefreshBuffer().requestDirectly();
    return true;
}

From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java

License:MIT License

@Override
public boolean createAndLoadTrack(String name) {
    Track track = plugin.getTrackManager().getOrMake(name);
    track.getIoLock().lock();/*from ww w  .ja  va 2s  . c  o  m*/
    try {
        MongoCollection<Document> c = database.getCollection(prefix + "tracks");
        try (MongoCursor<Document> cursor = c.find(new Document("_id", track.getName())).iterator()) {
            if (!cursor.hasNext()) {
                c.insertOne(trackToDoc(track));
            } else {
                Document d = cursor.next();
                //noinspection unchecked
                track.setGroups((List<String>) d.get("groups"));
            }
        }
    } finally {
        track.getIoLock().unlock();
    }
    return true;
}

From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java

License:MIT License

@Override
public boolean loadTrack(String name) {
    Track track = plugin.getTrackManager().getOrMake(name);
    track.getIoLock().lock();/* ww  w.  j a  v  a 2  s  .  co  m*/
    try {
        MongoCollection<Document> c = database.getCollection(prefix + "tracks");
        try (MongoCursor<Document> cursor = c.find(new Document("_id", track.getName())).iterator()) {
            if (cursor.hasNext()) {
                Document d = cursor.next();
                //noinspection unchecked
                track.setGroups((List<String>) d.get("groups"));
                return true;
            }
            return false;
        }
    } finally {
        track.getIoLock().unlock();
    }
}

From source file:me.lucko.luckperms.common.storage.dao.mongodb.MongoDao.java

License:MIT License

@Override
public UUID getUUID(String username) {
    MongoCollection<Document> c = database.getCollection(prefix + "uuid");
    try (MongoCursor<Document> cursor = c.find(new Document("name", username.toLowerCase())).iterator()) {
        if (cursor.hasNext()) {
            return cursor.next().get("_id", UUID.class);
        }/*from w w w.ja  v  a  2 s .  c  o m*/
    }
    return null;
}