2021-01-03 01:21:15 -06:00
|
|
|
package dev.plex.storage;
|
2020-10-27 11:14:34 -07:00
|
|
|
|
2022-04-07 00:37:31 -07:00
|
|
|
import com.mongodb.*;
|
2020-10-27 23:47:02 -05:00
|
|
|
import com.mongodb.client.MongoClient;
|
|
|
|
import com.mongodb.client.MongoClients;
|
2020-10-27 22:49:56 -05:00
|
|
|
import dev.morphia.Datastore;
|
|
|
|
import dev.morphia.Morphia;
|
2020-10-27 23:47:02 -05:00
|
|
|
import dev.morphia.mapping.MapperOptions;
|
2022-03-03 17:24:15 -06:00
|
|
|
import dev.plex.PlexBase;
|
2021-01-03 01:21:15 -06:00
|
|
|
import dev.plex.player.PlexPlayer;
|
2022-04-07 00:37:31 -07:00
|
|
|
import dev.plex.util.PlexLog;
|
2020-10-27 11:14:34 -07:00
|
|
|
|
2022-03-03 17:24:15 -06:00
|
|
|
public class MongoConnection extends PlexBase
|
2020-10-27 11:14:34 -07:00
|
|
|
{
|
|
|
|
// USE MORPHIA API FOR MONGO <3
|
|
|
|
|
|
|
|
public Datastore getDatastore()
|
|
|
|
{
|
2020-10-27 21:11:23 -07:00
|
|
|
if (!plugin.config.getString("data.central.storage").equalsIgnoreCase("mongodb"))
|
2020-10-27 11:14:34 -07:00
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2020-10-27 21:11:23 -07:00
|
|
|
String host = plugin.config.getString("data.central.hostname");
|
|
|
|
int port = plugin.config.getInt("data.central.port");
|
|
|
|
String username = plugin.config.getString("data.central.user");
|
|
|
|
String password = plugin.config.getString("data.central.password");
|
|
|
|
String database = plugin.config.getString("data.central.db");
|
2020-10-27 11:14:34 -07:00
|
|
|
|
2022-04-07 00:37:31 -07:00
|
|
|
String connectionString;
|
|
|
|
if (username != null && password != null && !username.isEmpty() && !password.isEmpty())
|
|
|
|
{
|
|
|
|
if (database != null && !database.isEmpty())
|
|
|
|
{
|
|
|
|
connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/?authSource=" + database;
|
|
|
|
} else {
|
|
|
|
connectionString = "mongodb://" + username + ":" + password + "@" + host + ":" + port + "/";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
connectionString = "mongodb://" + host + ":" + port + "/";
|
|
|
|
}
|
|
|
|
connectionString += "?uuidRepresentation=STANDARD";
|
|
|
|
PlexLog.debug("Using mongo connection string: " + connectionString);
|
2020-10-27 23:47:02 -05:00
|
|
|
MongoClient client = MongoClients.create(connectionString);
|
2022-04-07 00:37:31 -07:00
|
|
|
Datastore datastore = Morphia.createDatastore(client, database == null ? "admin" : database, MapperOptions.DEFAULT);
|
2020-10-27 23:47:02 -05:00
|
|
|
datastore.getMapper().map(PlexPlayer.class);
|
2020-10-27 11:14:34 -07:00
|
|
|
datastore.ensureIndexes();
|
2021-01-03 01:21:15 -06:00
|
|
|
plugin.setStorageType(StorageType.MONGODB);
|
2020-10-27 11:14:34 -07:00
|
|
|
return datastore;
|
|
|
|
}
|
|
|
|
}
|