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

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

import dev.morphia.Datastore;
import dev.morphia.Morphia;
KingRainbow44's avatar
KingRainbow44 committed
11
12
import dev.morphia.mapping.MapperOptions;
import dev.morphia.query.experimental.filters.Filters;
Melledy's avatar
Melledy committed
13
import emu.grasscutter.Grasscutter;
14
import emu.grasscutter.Grasscutter.ServerRunMode;
Melledy's avatar
Melledy committed
15
import emu.grasscutter.game.Account;
16
import emu.grasscutter.game.avatar.Avatar;
Melledy's avatar
Melledy committed
17
import emu.grasscutter.game.friends.Friendship;
18
import emu.grasscutter.game.gacha.GachaRecord;
19
import emu.grasscutter.game.inventory.GameItem;
20
import emu.grasscutter.game.mail.Mail;
Melledy's avatar
Melledy committed
21
import emu.grasscutter.game.player.Player;
Melledy's avatar
Melledy committed
22
23
import emu.grasscutter.game.quest.GameMainQuest;
import emu.grasscutter.game.quest.GameQuest;
Melledy's avatar
Melledy committed
24

25
import static emu.grasscutter.Configuration.*;
26

27
public final class DatabaseManager {
Melledy's avatar
Melledy committed
28
	private static Datastore datastore;
29
	private static Datastore dispatchDatastore;
Melledy's avatar
Melledy committed
30
	
KingRainbow44's avatar
KingRainbow44 committed
31
	private static final Class<?>[] mappedClasses = new Class<?>[] {
Melledy's avatar
Melledy committed
32
33
		DatabaseCounter.class, Account.class, Player.class, Avatar.class, GameItem.class, Friendship.class, 
		GachaRecord.class, Mail.class, GameMainQuest.class
Melledy's avatar
Melledy committed
34
35
36
37
38
39
40
41
42
	};
    
    public static Datastore getDatastore() {
    	return datastore;
    }
    
    public static MongoDatabase getDatabase() {
    	return getDatastore().getDatabase();
    }
43

Benjamin Elsdon's avatar
Benjamin Elsdon committed
44
45
	// Yes. I very dislike this method. However, this will be good for now.
	// TODO: Add dispatch routes for player account management
46
	public static Datastore getAccountDatastore() {
47
		if(SERVER.runMode == ServerRunMode.GAME_ONLY) {
48
49
50
51
52
			return dispatchDatastore;
		} else {
			return datastore;
		}
	}
Melledy's avatar
Melledy committed
53
54
55
	
	public static void initialize() {
		// Initialize
56
		MongoClient mongoClient = MongoClients.create(DATABASE.connectionUri);
Melledy's avatar
Melledy committed
57
		
KingRainbow44's avatar
KingRainbow44 committed
58
59
60
61
		// Set mapper options.
		MapperOptions mapperOptions = MapperOptions.builder()
				.storeEmpties(true).storeNulls(false).build();
		// Create data store.
62
		datastore = Morphia.createDatastore(mongoClient, DATABASE.collection, mapperOptions);
KingRainbow44's avatar
KingRainbow44 committed
63
64
		// Map classes.
		datastore.getMapper().map(mappedClasses);
Melledy's avatar
Melledy committed
65
66
67
68
		
		// Ensure indexes
		try {
			datastore.ensureIndexes();
KingRainbow44's avatar
KingRainbow44 committed
69
70
		} catch (MongoCommandException exception) {
			Grasscutter.getLogger().info("Mongo index error: ", exception);
Melledy's avatar
Melledy committed
71
			// Duplicate index error
KingRainbow44's avatar
KingRainbow44 committed
72
			if (exception.getCode() == 85) {
Melledy's avatar
Melledy committed
73
74
75
76
77
78
79
80
81
				// 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();
			}
		}
82

83
84
85
		if(SERVER.runMode == ServerRunMode.GAME_ONLY) {
			MongoClient dispatchMongoClient = MongoClients.create(GAME_OPTIONS.databaseInfo.connectionUri);
			dispatchDatastore = Morphia.createDatastore(dispatchMongoClient, GAME_OPTIONS.databaseInfo.collection);
Benjamin Elsdon's avatar
Benjamin Elsdon committed
86
87
88

			// Ensure indexes for dispatch server
			try {
89
				dispatchDatastore.ensureIndexes();
Benjamin Elsdon's avatar
Benjamin Elsdon committed
90
91
92
93
94
95
96
97
98
99
100
101
			} 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();
				}
102
103
			}
		}
Melledy's avatar
Melledy committed
104
	}
KingRainbow44's avatar
KingRainbow44 committed
105

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

Melledy's avatar
Melledy committed
118
119
120
121
	public static synchronized int getNextId(Object o) {
		return getNextId(o.getClass());
	}
}