DatabaseManager.java 3.89 KB
Newer Older
Melledy's avatar
Melledy committed
1
2
3
4
package emu.grasscutter.database;

import com.mongodb.MongoClientURI;
import com.mongodb.MongoCommandException;
KingRainbow44's avatar
KingRainbow44 committed
5
6
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
Melledy's avatar
Melledy committed
7
8
9
10
11
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.MongoIterable;

import dev.morphia.Datastore;
import dev.morphia.Morphia;
KingRainbow44's avatar
KingRainbow44 committed
12
13
import dev.morphia.mapping.MapperOptions;
import dev.morphia.query.experimental.filters.Filters;
Melledy's avatar
Melledy committed
14
15
16
17
18
19
20
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.avatar.GenshinAvatar;
import emu.grasscutter.game.friends.Friendship;
import emu.grasscutter.game.inventory.GenshinItem;

KingRainbow44's avatar
KingRainbow44 committed
21
public final class DatabaseManager {
22

Melledy's avatar
Melledy committed
23
	private static MongoClient mongoClient;
24
25
	private static MongoClient dispatchMongoClient;

Melledy's avatar
Melledy committed
26
	private static Datastore datastore;
27
	private static Datastore dispatchDatastore;
Melledy's avatar
Melledy committed
28
	
KingRainbow44's avatar
KingRainbow44 committed
29
	private static final Class<?>[] mappedClasses = new Class<?>[] {
Melledy's avatar
Melledy committed
30
31
32
33
34
35
36
37
38
39
		DatabaseCounter.class, Account.class, GenshinPlayer.class, GenshinAvatar.class, GenshinItem.class, Friendship.class
	};
    
    public static Datastore getDatastore() {
    	return datastore;
    }
    
    public static MongoDatabase getDatabase() {
    	return getDatastore().getDatabase();
    }
40

Benjamin Elsdon's avatar
Benjamin Elsdon committed
41
42
	// Yes. I very dislike this method. However, this will be good for now.
	// TODO: Add dispatch routes for player account management
43
44
45
46
47
48
49
	public static Datastore getAccountDatastore() {
		if(Grasscutter.getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
			return dispatchDatastore;
		} else {
			return datastore;
		}
	}
Melledy's avatar
Melledy committed
50
51
52
	
	public static void initialize() {
		// Initialize
53
		MongoClient mongoClient = MongoClients.create(Grasscutter.getConfig().DatabaseUrl);
Melledy's avatar
Melledy committed
54
		
KingRainbow44's avatar
KingRainbow44 committed
55
56
57
58
59
60
61
		// Set mapper options.
		MapperOptions mapperOptions = MapperOptions.builder()
				.storeEmpties(true).storeNulls(false).build();
		// Create data store.
		datastore = Morphia.createDatastore(mongoClient, Grasscutter.getConfig().DatabaseCollection, mapperOptions);
		// Map classes.
		datastore.getMapper().map(mappedClasses);
Melledy's avatar
Melledy committed
62
63
64
65
		
		// Ensure indexes
		try {
			datastore.ensureIndexes();
KingRainbow44's avatar
KingRainbow44 committed
66
67
		} catch (MongoCommandException exception) {
			Grasscutter.getLogger().info("Mongo index error: ", exception);
Melledy's avatar
Melledy committed
68
			// Duplicate index error
KingRainbow44's avatar
KingRainbow44 committed
69
			if (exception.getCode() == 85) {
Melledy's avatar
Melledy committed
70
71
72
73
74
75
76
77
78
				// Drop all indexes and re add them
				MongoIterable<String> collections = datastore.getDatabase().listCollectionNames();
				for (String name : collections) {
					datastore.getDatabase().getCollection(name).dropIndexes();
				}
				// Add back indexes
				datastore.ensureIndexes();
			}
		}
79

Benjamin Elsdon's avatar
Benjamin Elsdon committed
80
		if(Grasscutter.getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
81
82
			dispatchMongoClient = MongoClients.create(Grasscutter.getConfig().getGameServerOptions().DispatchServerDatabaseUrl);
			dispatchDatastore = Morphia.createDatastore(dispatchMongoClient, Grasscutter.getConfig().getGameServerOptions().DispatchServerDatabaseCollection);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
83
84
85

			// Ensure indexes for dispatch server
			try {
86
				dispatchDatastore.ensureIndexes();
Benjamin Elsdon's avatar
Benjamin Elsdon committed
87
88
89
90
91
92
93
94
95
96
97
98
			} catch (MongoCommandException e) {
				Grasscutter.getLogger().info("Mongo index error: ", e);
				// Duplicate index error
				if (e.getCode() == 85) {
					// Drop all indexes and re add them
					MongoIterable<String> collections = dispatchDatastore.getDatabase().listCollectionNames();
					for (String name : collections) {
						dispatchDatastore.getDatabase().getCollection(name).dropIndexes();
					}
					// Add back indexes
					dispatchDatastore.ensureIndexes();
				}
99
100
			}
		}
Melledy's avatar
Melledy committed
101
	}
KingRainbow44's avatar
KingRainbow44 committed
102

Melledy's avatar
Melledy committed
103
	public static synchronized int getNextId(Class<?> c) {
Miyucchi's avatar
Miyucchi committed
104
		DatabaseCounter counter = getDatastore().find(DatabaseCounter.class).filter(Filters.eq("_id", c.getSimpleName())).first();
Melledy's avatar
Melledy committed
105
106
107
108
109
110
111
112
113
		if (counter == null) {
			counter = new DatabaseCounter(c.getSimpleName());
		}
		try {
			return counter.getNextId();
		} finally {
			getDatastore().save(counter);
		}
	}
KingRainbow44's avatar
KingRainbow44 committed
114

Melledy's avatar
Melledy committed
115
116
117
118
	public static synchronized int getNextId(Object o) {
		return getNextId(o.getClass());
	}
}