-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
MongoDBDatabaseHandler.java
204 lines (185 loc) · 7.98 KB
/
MongoDBDatabaseHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package world.bentobox.bentobox.database.mongodb;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bukkit.Bukkit;
import org.eclipse.jdt.annotation.NonNull;
import com.google.gson.Gson;
import com.mongodb.MongoClientException;
import com.mongodb.MongoNamespace;
import com.mongodb.MongoTimeoutException;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.FindOneAndReplaceOptions;
import com.mongodb.client.model.IndexOptions;
import com.mongodb.client.model.Indexes;
import com.mongodb.util.JSON;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.database.DatabaseConnector;
import world.bentobox.bentobox.database.json.AbstractJSONDatabaseHandler;
import world.bentobox.bentobox.database.objects.DataObject;
import world.bentobox.bentobox.database.objects.Table;
/**
*
* Class that inserts a <T> into the corresponding database-table.
*
* @author tastybento
*
* @param <T>
*/
@SuppressWarnings("deprecation")
public class MongoDBDatabaseHandler<T> extends AbstractJSONDatabaseHandler<T> {
private static final String UNIQUEID = "uniqueId";
private static final String MONGO_ID = "_id";
private MongoCollection<Document> collection;
private final DatabaseConnector dbConnecter;
/**
* Handles the connection to the database and creation of the initial database schema (tables) for
* the class that will be stored.
* @param plugin - plugin object
* @param type - the type of class to be stored in the database. Must inherit DataObject
* @param dbConnecter - authentication details for the database
*/
MongoDBDatabaseHandler(BentoBox plugin, Class<T> type, DatabaseConnector dbConnecter) {
super(plugin, type, dbConnecter);
this.dbConnecter = dbConnecter;
boolean connected = true; // if it is set to false, it will consider there has been an error upon connecting.
try {
// Connection to the database
MongoDatabase database = (MongoDatabase) dbConnecter.createConnection(dataObject);
if (database == null) {
plugin.logError("Could not connect to the database. Are the credentials in the config.yml file correct?");
connected = false;
} else {
// Check for old collections
String oldName = plugin.getSettings().getDatabasePrefix() + type.getCanonicalName();
String newName = getName(plugin, dataObject);
if (!oldName.equals((newName)) && collectionExists(database, oldName) && !collectionExists(database, newName)){
collection = database.getCollection(oldName);
collection.renameCollection(new MongoNamespace(database.getName(), newName));
} else {
collection = database.getCollection(newName);
}
IndexOptions indexOptions = new IndexOptions().unique(true);
collection.createIndex(Indexes.text(UNIQUEID), indexOptions);
}
} catch (MongoTimeoutException e) {
plugin.logError("Could not connect to the database. MongoDB timed out.");
plugin.logError("Error code: " + e.getCode());
plugin.logError("Errors: " + String.join(", ", e.getErrorLabels()));
connected = false;
} catch (MongoClientException e) {
plugin.logError("Could not connect to the database. An unhandled error occurred.");
plugin.logStacktrace(e);
connected = false;
}
if (!connected) {
plugin.logWarning("Disabling BentoBox...");
Bukkit.getPluginManager().disablePlugin(plugin);
}
}
private boolean collectionExists(MongoDatabase database, final String collectionName) {
for (final String name : database.listCollectionNames()) {
if (name.equalsIgnoreCase(collectionName)) {
return true;
}
}
return false;
}
private String getName(BentoBox plugin, Class<T> type) {
return plugin.getSettings().getDatabasePrefix() +
(type.getAnnotation(Table.class) == null ?
type.getCanonicalName()
: type.getAnnotation(Table.class)
.name());
}
@Override
public List<T> loadObjects() {
List<T> list = new ArrayList<>();
Gson gson = getGson();
for (Document document : collection.find(new Document())) {
// The deprecated serialize option does not have a viable alternative without involving a huge amount of custom code
String json = JSON.serialize(document);
json = json.replaceFirst(MONGO_ID, UNIQUEID);
try {
list.add(gson.fromJson(json, dataObject));
} catch (Exception e) {
plugin.logError("Could not load object :" + e.getMessage());
}
}
return list;
}
@Override
public T loadObject(@NonNull String uniqueId) {
Document doc = collection.find(new Document(MONGO_ID, uniqueId)).limit(1).first();
Gson gson = getGson();
String json = JSON.serialize(doc).replaceFirst(MONGO_ID, UNIQUEID);
// load single object
return gson.fromJson(json, dataObject);
}
@Override
public CompletableFuture<Boolean> saveObject(T instance) {
CompletableFuture<Boolean> completableFuture = new CompletableFuture<>();
// Null check
if (instance == null) {
plugin.logError("MongoDB database request to store a null. ");
completableFuture.complete(false);
return completableFuture;
}
if (!(instance instanceof DataObject dataObj)) {
plugin.logError("This class is not a DataObject: " + instance.getClass().getName());
completableFuture.complete(false);
return completableFuture;
}
try {
Gson gson = getGson();
String toStore = gson.toJson(instance);
// Change uniqueId to _id
toStore = toStore.replaceFirst(UNIQUEID, MONGO_ID);
// This parses JSON to a Mongo Document
Document document = Document.parse(toStore);
// Filter based on the id
Bson filter = new Document(MONGO_ID, dataObj.getUniqueId());
// Set the options to upsert (update or insert if doc is not there)
FindOneAndReplaceOptions options = new FindOneAndReplaceOptions().upsert(true);
// Do the deed
collection.findOneAndReplace(filter, document, options);
completableFuture.complete(true);
} catch (Exception e) {
plugin.logError("Could not save object " + instance.getClass().getName() + " " + e.getMessage());
completableFuture.complete(false);
}
return completableFuture;
}
@Override
public void deleteID(String uniqueId) {
try {
collection.findOneAndDelete(new Document(MONGO_ID, uniqueId));
} catch (Exception e) {
plugin.logError("Could not delete object " + getName(plugin, dataObject) + " " + uniqueId + " " + e.getMessage());
}
}
@Override
public void deleteObject(T instance) {
// Null check
if (instance == null) {
plugin.logError("MondDB database request to delete a null. ");
return;
}
if (!(instance instanceof DataObject)) {
plugin.logError("This class is not a DataObject: " + instance.getClass().getName());
return;
}
deleteID(((DataObject)instance).getUniqueId());
}
@Override
public boolean objectExists(String uniqueId) {
return collection.find(new Document(MONGO_ID, uniqueId)).first() != null;
}
@Override
public void close() {
dbConnecter.closeConnection(dataObject);
}
}