From b9fc8f4b5fb405088e4c455be0635cd17a902cdb Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 5 Jun 2017 16:09:02 +0100 Subject: [PATCH] Add MongoDB collection prefix option --- bukkit/src/main/resources/config.yml | 4 ++ bungee/src/main/resources/config.yml | 4 ++ .../luckperms/common/config/ConfigKeys.java | 5 ++ .../common/storage/StorageFactory.java | 6 ++- .../storage/backing/MongoDBBacking.java | 54 ++++++++++--------- .../common/storage/backing/SQLBacking.java | 1 + sponge/src/main/resources/luckperms.conf | 4 ++ 7 files changed, 53 insertions(+), 25 deletions(-) diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index 923cf1155..c02ba736d 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -250,6 +250,10 @@ data: # This should *not* be set to "lp_" if you have previously ran LuckPerms v2.16.81 or earlier with this database. table_prefix: 'luckperms_' + # The prefix to use for all LuckPerms collections. Change this if you want to use different collections for different servers. + # The default is no prefix. + mongodb_collection_prefix: '' + # This option controls how frequently LuckPerms will perform a sync task. # A sync task will refresh all data from the storage, and ensure that the most up-to-date data is being used by the plugin. # diff --git a/bungee/src/main/resources/config.yml b/bungee/src/main/resources/config.yml index f55ca7af7..154699542 100644 --- a/bungee/src/main/resources/config.yml +++ b/bungee/src/main/resources/config.yml @@ -185,6 +185,10 @@ data: # This should *not* be set to "lp_" if you have previously ran LuckPerms v2.16.81 or earlier with this database. table_prefix: 'luckperms_' + # The prefix to use for all LuckPerms collections. Change this if you want to use different collections for different servers. + # The default is no prefix. + mongodb_collection_prefix: '' + # This option controls how frequently LuckPerms will perform a sync task. # A sync task will refresh all data from the storage, and ensure that the most up-to-date data is being used by the plugin. # diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index b00ee306d..e8e027e69 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -342,6 +342,11 @@ public class ConfigKeys { */ public static final ConfigKey SQL_TABLE_PREFIX = EnduringKey.wrap(StringKey.of("data.table_prefix", "luckperms_")); + /** + * The prefix for any MongoDB collections + */ + public static final ConfigKey MONGODB_COLLECTION_PREFIX = EnduringKey.wrap(StringKey.of("data.mongodb_collection_prefix", "")); + /** * The name of the storage method being used */ diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java index fa2921fe4..6bbbc978e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java @@ -149,7 +149,11 @@ public class StorageFactory { plugin.getConfiguration().get(ConfigKeys.SQL_TABLE_PREFIX) ); case MONGODB: - return new MongoDBBacking(plugin, plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES)); + return new MongoDBBacking( + plugin, + plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES), + plugin.getConfiguration().get(ConfigKeys.MONGODB_COLLECTION_PREFIX) + ); case YAML: return new YAMLBacking(plugin, plugin.getDataDirectory(), "yaml-storage"); default: diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java index 56a68a822..fac9fae7b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/MongoDBBacking.java @@ -25,6 +25,8 @@ package me.lucko.luckperms.common.storage.backing; +import lombok.Getter; + import com.google.common.collect.ImmutableList; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; @@ -130,9 +132,13 @@ public class MongoDBBacking extends AbstractBacking { private MongoClient mongoClient; private MongoDatabase database; - public MongoDBBacking(LuckPermsPlugin plugin, DatastoreConfiguration configuration) { + @Getter + private final String prefix; + + public MongoDBBacking(LuckPermsPlugin plugin, DatastoreConfiguration configuration, String prefix) { super(plugin, "MongoDB"); this.configuration = configuration; + this.prefix = prefix; } @Override @@ -179,7 +185,7 @@ public class MongoDBBacking extends AbstractBacking { @Override public boolean logAction(LogEntry entry) { return call(() -> { - MongoCollection c = database.getCollection("action"); + MongoCollection c = database.getCollection(prefix + "action"); Document doc = new Document() .append("timestamp", entry.getTimestamp()) @@ -202,7 +208,7 @@ public class MongoDBBacking extends AbstractBacking { public Log getLog() { return call(() -> { final Log.Builder log = Log.builder(); - MongoCollection c = database.getCollection("action"); + MongoCollection c = database.getCollection(prefix + "action"); try (MongoCursor cursor = c.find().iterator()) { while (cursor.hasNext()) { @@ -234,7 +240,7 @@ public class MongoDBBacking extends AbstractBacking { public boolean applyBulkUpdate(BulkUpdate bulkUpdate) { return call(() -> { if (bulkUpdate.getDataType().isIncludingUsers()) { - MongoCollection c = database.getCollection("users"); + MongoCollection c = database.getCollection(prefix + "users"); try (MongoCursor cursor = c.find().iterator()) { while (cursor.hasNext()) { @@ -268,7 +274,7 @@ public class MongoDBBacking extends AbstractBacking { } if (bulkUpdate.getDataType().isIncludingGroups()) { - MongoCollection c = database.getCollection("groups"); + MongoCollection c = database.getCollection(prefix + "groups"); try (MongoCursor cursor = c.find().iterator()) { while (cursor.hasNext()) { @@ -311,7 +317,7 @@ public class MongoDBBacking extends AbstractBacking { user.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("users"); + MongoCollection c = database.getCollection(prefix + "users"); try (MongoCursor cursor = c.find(new Document("_id", user.getUuid())).iterator()) { if (cursor.hasNext()) { @@ -354,7 +360,7 @@ public class MongoDBBacking extends AbstractBacking { user.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("users"); + MongoCollection c = database.getCollection(prefix + "users"); return c.deleteOne(new Document("_id", user.getUuid())).wasAcknowledged(); }, false); } finally { @@ -365,7 +371,7 @@ public class MongoDBBacking extends AbstractBacking { user.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("users"); + MongoCollection c = database.getCollection(prefix + "users"); try (MongoCursor cursor = c.find(new Document("_id", user.getUuid())).iterator()) { if (!cursor.hasNext()) { c.insertOne(fromUser(user)); @@ -389,7 +395,7 @@ public class MongoDBBacking extends AbstractBacking { public Set getUniqueUsers() { Set uuids = new HashSet<>(); boolean success = call(() -> { - MongoCollection c = database.getCollection("users"); + MongoCollection c = database.getCollection(prefix + "users"); try (MongoCursor cursor = c.find().iterator()) { while (cursor.hasNext()) { @@ -408,7 +414,7 @@ public class MongoDBBacking extends AbstractBacking { public List> getUsersWithPermission(String permission) { ImmutableList.Builder> held = ImmutableList.builder(); boolean success = call(() -> { - MongoCollection c = database.getCollection("users"); + MongoCollection c = database.getCollection(prefix + "users"); try (MongoCursor cursor = c.find().iterator()) { while (cursor.hasNext()) { @@ -439,7 +445,7 @@ public class MongoDBBacking extends AbstractBacking { group.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("groups"); + MongoCollection c = database.getCollection(prefix + "groups"); try (MongoCursor cursor = c.find(new Document("_id", group.getName())).iterator()) { if (cursor.hasNext()) { @@ -466,7 +472,7 @@ public class MongoDBBacking extends AbstractBacking { group.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("groups"); + MongoCollection c = database.getCollection(prefix + "groups"); try (MongoCursor cursor = c.find(new Document("_id", group.getName())).iterator()) { if (cursor.hasNext()) { @@ -490,7 +496,7 @@ public class MongoDBBacking extends AbstractBacking { public boolean loadAllGroups() { List groups = new ArrayList<>(); boolean success = call(() -> { - MongoCollection c = database.getCollection("groups"); + MongoCollection c = database.getCollection(prefix + "groups"); boolean b = true; try (MongoCursor cursor = c.find().iterator()) { @@ -519,7 +525,7 @@ public class MongoDBBacking extends AbstractBacking { group.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("groups"); + MongoCollection c = database.getCollection(prefix + "groups"); return c.replaceOne(new Document("_id", group.getName()), fromGroup(group)).wasAcknowledged(); }, false); } finally { @@ -533,7 +539,7 @@ public class MongoDBBacking extends AbstractBacking { boolean success; try { success = call(() -> { - MongoCollection c = database.getCollection("groups"); + MongoCollection c = database.getCollection(prefix + "groups"); return c.deleteOne(new Document("_id", group.getName())).wasAcknowledged(); }, false); } finally { @@ -548,7 +554,7 @@ public class MongoDBBacking extends AbstractBacking { public List> getGroupsWithPermission(String permission) { ImmutableList.Builder> held = ImmutableList.builder(); boolean success = call(() -> { - MongoCollection c = database.getCollection("groups"); + MongoCollection c = database.getCollection(prefix + "groups"); try (MongoCursor cursor = c.find().iterator()) { while (cursor.hasNext()) { @@ -579,7 +585,7 @@ public class MongoDBBacking extends AbstractBacking { track.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("tracks"); + MongoCollection c = database.getCollection(prefix + "tracks"); try (MongoCursor cursor = c.find(new Document("_id", track.getName())).iterator()) { if (!cursor.hasNext()) { @@ -602,7 +608,7 @@ public class MongoDBBacking extends AbstractBacking { track.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("tracks"); + MongoCollection c = database.getCollection(prefix + "tracks"); try (MongoCursor cursor = c.find(new Document("_id", track.getName())).iterator()) { if (cursor.hasNext()) { @@ -622,7 +628,7 @@ public class MongoDBBacking extends AbstractBacking { public boolean loadAllTracks() { List tracks = new ArrayList<>(); boolean success = call(() -> { - MongoCollection c = database.getCollection("tracks"); + MongoCollection c = database.getCollection(prefix + "tracks"); boolean b = true; try (MongoCursor cursor = c.find().iterator()) { @@ -651,7 +657,7 @@ public class MongoDBBacking extends AbstractBacking { track.getIoLock().lock(); try { return call(() -> { - MongoCollection c = database.getCollection("tracks"); + MongoCollection c = database.getCollection(prefix + "tracks"); return c.replaceOne(new Document("_id", track.getName()), fromTrack(track)).wasAcknowledged(); }, false); } finally { @@ -665,7 +671,7 @@ public class MongoDBBacking extends AbstractBacking { boolean success; try { success = call(() -> { - MongoCollection c = database.getCollection("tracks"); + MongoCollection c = database.getCollection(prefix + "tracks"); return c.deleteOne(new Document("_id", track.getName())).wasAcknowledged(); }, false); } finally { @@ -679,7 +685,7 @@ public class MongoDBBacking extends AbstractBacking { @Override public boolean saveUUIDData(String username, UUID uuid) { return call(() -> { - MongoCollection c = database.getCollection("uuid"); + MongoCollection c = database.getCollection(prefix + "uuid"); try (MongoCursor cursor = c.find(new Document("_id", uuid)).iterator()) { if (cursor.hasNext()) { @@ -696,7 +702,7 @@ public class MongoDBBacking extends AbstractBacking { @Override public UUID getUUID(String username) { return call(() -> { - MongoCollection c = database.getCollection("uuid"); + MongoCollection c = database.getCollection(prefix + "uuid"); try (MongoCursor cursor = c.find(new Document("name", username.toLowerCase())).iterator()) { if (cursor.hasNext()) { @@ -710,7 +716,7 @@ public class MongoDBBacking extends AbstractBacking { @Override public String getName(UUID uuid) { return call(() -> { - MongoCollection c = database.getCollection("uuid"); + MongoCollection c = database.getCollection(prefix + "uuid"); try (MongoCursor cursor = c.find(new Document("_id", uuid)).iterator()) { if (cursor.hasNext()) { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java b/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java index b5ed07a7a..9dc0c826e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/backing/SQLBacking.java @@ -117,6 +117,7 @@ public class SQLBacking extends AbstractBacking { @Getter private final SQLProvider provider; + @Getter private final Function prefix; diff --git a/sponge/src/main/resources/luckperms.conf b/sponge/src/main/resources/luckperms.conf index b15da948f..c62b75d65 100644 --- a/sponge/src/main/resources/luckperms.conf +++ b/sponge/src/main/resources/luckperms.conf @@ -204,6 +204,10 @@ data { # This should *not* be set to "lp_" if you have previously ran LuckPerms v2.16.81 or earlier with this database. table_prefix="luckperms_" + # The prefix to use for all LuckPerms collections. Change this if you want to use different collections for different servers. + # The default is no prefix. + mongodb_collection_prefix="" + # This option controls how frequently LuckPerms will perform a sync task. # A sync task will refresh all data from the storage, and ensure that the most up-to-date data is being used by the plugin. #