Example usage for com.mongodb.client MongoCollection replaceOne

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

Introduction

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

Prototype

UpdateResult replaceOne(Bson filter, TDocument replacement);

Source Link

Document

Replace a document in the collection according to the specified arguments.

Usage

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

License:Open Source License

@Override
public boolean loadUser(UUID uuid, String username) {
    User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
    user.getIoLock().lock();/*  w  w w . ja v a  2  s.c o m*/
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("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();
                    user.setNodes(revert((Map<String, Boolean>) d.get("perms")));
                    user.setPrimaryGroup(d.getString("primaryGroup"));

                    boolean save = plugin.getUserManager().giveDefaultIfNeeded(user, false);

                    if (user.getName() == null || user.getName().equalsIgnoreCase("null")) {
                        user.setName(d.getString("name"));
                    } else {
                        if (!d.getString("name").equalsIgnoreCase(user.getName())) {
                            save = true;
                        }
                    }

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

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

License:Open Source License

@Override
public boolean saveUser(User user) {
    if (!GenericUserManager.shouldSave(user)) {
        user.getIoLock().lock();//  w  w  w. j a  v a  2s. c  o  m
        try {
            return call(() -> {
                MongoCollection<Document> c = database.getCollection("users");
                return c.deleteOne(new Document("_id", user.getUuid())).wasAcknowledged();
            }, false);
        } finally {
            user.getIoLock().unlock();
        }
    }

    user.getIoLock().lock();
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("users");
            try (MongoCursor<Document> cursor = c.find(new Document("_id", user.getUuid())).iterator()) {
                if (!cursor.hasNext()) {
                    c.insertOne(fromUser(user));
                } else {
                    c.replaceOne(new Document("_id", user.getUuid()), fromUser(user));
                }
            }
            return true;
        }, false);
    } finally {
        user.getIoLock().unlock();
    }
}

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

License:Open Source License

@Override
public boolean saveGroup(Group group) {
    group.getIoLock().lock();/* w  w  w  . j  a  v  a  2 s.  c o  m*/
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("groups");
            return c.replaceOne(new Document("_id", group.getName()), fromGroup(group)).wasAcknowledged();
        }, false);
    } finally {
        group.getIoLock().unlock();
    }
}

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

License:Open Source License

@Override
public boolean saveTrack(Track track) {
    track.getIoLock().lock();/*from  www  . jav  a 2s  .co m*/
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("tracks");
            return c.replaceOne(new Document("_id", track.getName()), fromTrack(track)).wasAcknowledged();
        }, 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()));
            }/*from w  w w  .j a va  2s. co  m*/
        }

        return true;
    }, false);
}

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

License:MIT License

@Override
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) {
    if (bulkUpdate.getDataType().isIncludingUsers()) {
        MongoCollection<Document> c = database.getCollection(prefix + "users");
        try (MongoCursor<Document> cursor = c.find().iterator()) {
            while (cursor.hasNext()) {
                Document d = cursor.next();

                UUID uuid = d.get("_id", UUID.class);
                Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
                Set<NodeModel> results = nodes.stream().map(bulkUpdate::apply).filter(Objects::nonNull)
                        .collect(Collectors.toSet());

                if (!nodes.equals(results)) {
                    List<Document> newNodes = results.stream().map(MongoDao::nodeToDoc)
                            .collect(Collectors.toList());

                    d.append("permissions", newNodes).remove("perms");
                    c.replaceOne(new Document("_id", uuid), d);
                }// w w  w  .  jav a 2 s  .  c om
            }
        }
    }

    if (bulkUpdate.getDataType().isIncludingGroups()) {
        MongoCollection<Document> c = database.getCollection(prefix + "groups");
        try (MongoCursor<Document> cursor = c.find().iterator()) {
            while (cursor.hasNext()) {
                Document d = cursor.next();

                String holder = d.getString("_id");
                Set<NodeModel> nodes = new HashSet<>(nodesFromDoc(d));
                Set<NodeModel> results = nodes.stream().map(bulkUpdate::apply).filter(Objects::nonNull)
                        .collect(Collectors.toSet());

                if (!nodes.equals(results)) {
                    List<Document> newNodes = results.stream().map(MongoDao::nodeToDoc)
                            .collect(Collectors.toList());

                    d.append("permissions", newNodes).remove("perms");
                    c.replaceOne(new Document("_id", holder), d);
                }
            }
        }
    }
    return true;
}

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();/*w  w  w  .ja v a  2  s .  co m*/
    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 saveTrack(Track track) {
    track.getIoLock().lock();//from w  ww .  jav  a 2s.  c o  m
    try {
        MongoCollection<Document> c = database.getCollection(prefix + "tracks");
        return c.replaceOne(new Document("_id", track.getName()), trackToDoc(track)).wasAcknowledged();
    } finally {
        track.getIoLock().unlock();
    }
}

From source file:me.lucko.luckperms.storage.methods.MongoDBDatastore.java

License:Open Source License

@Override
public boolean loadUser(UUID uuid, String username) {
    User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
    user.getIoLock().lock();/*from w  ww.j  a v  a 2  s.c  o  m*/
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("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();
                    user.setNodes(revert((Map<String, Boolean>) d.get("perms")));
                    user.setPrimaryGroup(d.getString("primaryGroup"));

                    boolean save = plugin.getUserManager().giveDefaultIfNeeded(user, false);

                    if (user.getName().equalsIgnoreCase("null")) {
                        user.setName(d.getString("name"));
                    } else {
                        if (!d.getString("name").equals(user.getName())) {
                            save = true;
                        }
                    }

                    if (save) {
                        c.replaceOne(new Document("_id", user.getUuid()), fromUser(user));
                    }
                } else {
                    if (plugin.getUserManager().shouldSave(user)) {
                        user.clearNodes();
                        user.setPrimaryGroup(null);
                        plugin.getUserManager().giveDefaultIfNeeded(user, false);
                    }
                }
            }
            return true;
        }, false);
    } finally {
        user.getIoLock().unlock();
        user.refreshPermissions();
    }
}

From source file:me.lucko.luckperms.storage.methods.MongoDBDatastore.java

License:Open Source License

@Override
public boolean saveUser(User user) {
    if (!plugin.getUserManager().shouldSave(user)) {
        user.getIoLock().lock();//  w  w  w .  ja v a  2 s. co  m
        try {
            return call(() -> {
                MongoCollection<Document> c = database.getCollection("users");
                return c.deleteOne(new Document("_id", user.getUuid())).wasAcknowledged();
            }, false);
        } finally {
            user.getIoLock().unlock();
        }
    }

    user.getIoLock().lock();
    try {
        return call(() -> {
            MongoCollection<Document> c = database.getCollection("users");
            try (MongoCursor<Document> cursor = c.find(new Document("_id", user.getUuid())).iterator()) {
                if (!cursor.hasNext()) {
                    c.insertOne(fromUser(user));
                } else {
                    c.replaceOne(new Document("_id", user.getUuid()), fromUser(user));
                }
            }
            return true;
        }, false);
    } finally {
        user.getIoLock().unlock();
    }
}