forked from Grasscutters/Grasscutter
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31b5307
commit 676302c
Showing
12 changed files
with
220 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ tmp/ | |
/languages | ||
/proto | ||
/lua | ||
lib/kcp-1.5.1.jar | ||
|
||
/*.jar | ||
/*.sh | ||
|
72 changes: 0 additions & 72 deletions
72
src/generated/main/java/emu/grasscutter/net/proto/GetPlayerTokenReqOuterClass.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package emu.grasscutter.database; | ||
|
||
import emu.grasscutter.Grasscutter; | ||
import emu.grasscutter.game.avatar.Avatar; | ||
import emu.grasscutter.utils.objects.DatabaseObject; | ||
import org.slf4j.*; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.*; | ||
|
||
/** | ||
* Complicated manager of the MongoDB database. | ||
* Handles caching, data operations, and more. | ||
*/ | ||
public interface Database { | ||
Logger logger = LoggerFactory.getLogger("Database"); | ||
List<DatabaseObject<?>> objects = new CopyOnWriteArrayList<>(); | ||
|
||
/** | ||
* Queues an object to be saved. | ||
* | ||
* @param object The object to save. | ||
*/ | ||
static void save(DatabaseObject<?> object) { | ||
if (object.saveImmediately()) { | ||
object.save(); | ||
} else { | ||
objects.add(object); | ||
} | ||
} | ||
|
||
/** | ||
* Performs a bulk save of all deferred objects. | ||
*/ | ||
static void saveAll() { | ||
var size = objects.size(); | ||
Database.saveAll(objects); | ||
|
||
logger.debug("Performed auto save on {} objects.", size); | ||
} | ||
|
||
/** | ||
* Performs a bulk save of all deferred objects. | ||
* | ||
* @param objects The objects to save. | ||
*/ | ||
static void saveAll(List<? extends DatabaseObject<?>> objects) { | ||
// Sort all objects into their respective databases. | ||
var gameObjects = objects.stream() | ||
.filter(DatabaseObject::isGameObject) | ||
.toList(); | ||
var accountObjects = objects.stream() | ||
.filter(o -> !o.isGameObject()) | ||
.toList(); | ||
|
||
// Clear the collective list. | ||
objects.clear(); | ||
|
||
// Save all objects. | ||
var executor = DatabaseHelper.getEventExecutor(); | ||
if (Grasscutter.getRunMode() != Grasscutter.ServerRunMode.DISPATCH_ONLY) { | ||
executor.submit(() -> { | ||
DatabaseManager.getGameDatastore().save(gameObjects); | ||
}); | ||
} | ||
if (Grasscutter.getRunMode() != Grasscutter.ServerRunMode.GAME_ONLY) { | ||
executor.submit(() -> { | ||
DatabaseManager.getAccountDatastore().save(accountObjects); | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Starts the auto-save thread. | ||
* Runs every 10s 1000为1秒. | ||
*/ | ||
static void startSaveThread() { | ||
var timer = new Timer(); | ||
timer.scheduleAtFixedRate(new TimerTask() { | ||
@Override | ||
public void run() { | ||
Database.saveAll(); | ||
} | ||
}, 0, 1000 * 10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.