Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
0d64e2e8
Unverified
Commit
0d64e2e8
authored
Sep 18, 2022
by
Melledy
Browse files
Use reflection to map database classes and add missing mapped classes to the dispatch datastore
parent
794350aa
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/database/DatabaseManager.java
View file @
0d64e2e8
...
...
@@ -10,34 +10,21 @@ import com.mongodb.client.MongoIterable;
import
dev.morphia.Datastore
;
import
dev.morphia.Morphia
;
import
dev.morphia.annotations.Entity
;
import
dev.morphia.mapping.Mapper
;
import
dev.morphia.mapping.MapperOptions
;
import
dev.morphia.query.experimental.filters.Filters
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.Grasscutter.ServerRunMode
;
import
emu.grasscutter.game.Account
;
import
emu.grasscutter.game.activity.PlayerActivityData
;
import
emu.grasscutter.game.activity.musicgame.MusicGameBeatmap
;
import
emu.grasscutter.game.avatar.Avatar
;
import
emu.grasscutter.game.battlepass.BattlePassManager
;
import
emu.grasscutter.game.friends.Friendship
;
import
emu.grasscutter.game.gacha.GachaRecord
;
import
emu.grasscutter.game.home.GameHome
;
import
emu.grasscutter.game.inventory.GameItem
;
import
emu.grasscutter.game.mail.Mail
;
import
emu.grasscutter.game.player.Player
;
import
emu.grasscutter.game.quest.GameMainQuest
;
import
emu.grasscutter.game.quest.GameQuest
;
import
org.reflections.Reflections
;
public
final
class
DatabaseManager
{
private
static
Datastore
gameDatastore
;
private
static
Datastore
dispatchDatastore
;
private
static
final
Class
<?>[]
mappedClasses
=
new
Class
<?>[]
{
DatabaseCounter
.
class
,
Account
.
class
,
Player
.
class
,
Avatar
.
class
,
GameItem
.
class
,
Friendship
.
class
,
GachaRecord
.
class
,
Mail
.
class
,
GameMainQuest
.
class
,
GameHome
.
class
,
BattlePassManager
.
class
,
PlayerActivityData
.
class
,
MusicGameBeatmap
.
class
};
public
static
Datastore
getGameDatastore
()
{
return
gameDatastore
;
}
...
...
@@ -63,47 +50,54 @@ public final class DatabaseManager {
// Set mapper options.
MapperOptions
mapperOptions
=
MapperOptions
.
builder
()
.
storeEmpties
(
true
).
storeNulls
(
false
).
build
();
// Create data store.
gameDatastore
=
Morphia
.
createDatastore
(
gameMongoClient
,
DATABASE
.
game
.
collection
,
mapperOptions
);
// Map classes.
gameDatastore
.
getMapper
().
map
(
mappedClasses
);
Class
<?>[]
entities
=
new
Reflections
(
Grasscutter
.
class
.
getPackageName
())
.
getTypesAnnotatedWith
(
Entity
.
class
)
.
stream
()
.
filter
(
cls
->
{
Entity
e
=
cls
.
getAnnotation
(
Entity
.
class
);
return
e
!=
null
&&
!
e
.
value
().
equals
(
Mapper
.
IGNORED_FIELDNAME
);
})
.
toArray
(
Class
<?>[]::
new
);
gameDatastore
.
getMapper
().
map
(
entities
);
// Ensure indexes
// Ensure indexes for the game datastore
ensureIndexes
(
gameDatastore
);
if
(
SERVER
.
runMode
==
ServerRunMode
.
GAME_ONLY
)
{
MongoClient
dispatchMongoClient
=
MongoClients
.
create
(
DATABASE
.
server
.
connectionUri
);
dispatchDatastore
=
Morphia
.
createDatastore
(
dispatchMongoClient
,
DATABASE
.
server
.
collection
,
mapperOptions
);
dispatchDatastore
.
getMapper
().
map
(
new
Class
<?>[]
{
DatabaseCounter
.
class
,
Account
.
class
});
// Ensure indexes for dispatch datastore
ensureIndexes
(
dispatchDatastore
);
}
}
/**
* Ensures the database indexes exist and rebuilds them if there is an error with them
* @param datastore The datastore to ensure indexes on
*/
private
static
void
ensureIndexes
(
Datastore
datastore
)
{
try
{
gameD
atastore
.
ensureIndexes
();
}
catch
(
MongoCommandException
e
xception
)
{
Grasscutter
.
getLogger
().
info
(
"Mongo index error: "
,
e
xception
);
d
atastore
.
ensureIndexes
();
}
catch
(
MongoCommandException
e
)
{
Grasscutter
.
getLogger
().
info
(
"Mongo index error: "
,
e
);
// Duplicate index error
if
(
e
xception
.
getCode
()
==
85
)
{
if
(
e
.
getCode
()
==
85
)
{
// Drop all indexes and re add them
MongoIterable
<
String
>
collections
=
gameD
atastore
.
getDatabase
().
listCollectionNames
();
MongoIterable
<
String
>
collections
=
d
atastore
.
getDatabase
().
listCollectionNames
();
for
(
String
name
:
collections
)
{
gameD
atastore
.
getDatabase
().
getCollection
(
name
).
dropIndexes
();
d
atastore
.
getDatabase
().
getCollection
(
name
).
dropIndexes
();
}
// Add back indexes
gameDatastore
.
ensureIndexes
();
}
}
if
(
SERVER
.
runMode
==
ServerRunMode
.
GAME_ONLY
)
{
MongoClient
dispatchMongoClient
=
MongoClients
.
create
(
DATABASE
.
server
.
connectionUri
);
dispatchDatastore
=
Morphia
.
createDatastore
(
dispatchMongoClient
,
DATABASE
.
server
.
collection
);
// Ensure indexes for dispatch server
try
{
dispatchDatastore
.
ensureIndexes
();
}
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
();
}
datastore
.
ensureIndexes
();
}
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment