NivixX / NDatabase

💾 NDatabase is a lightweight and easy to use indexed key-value store database framework mainly aimed for minecraft bukkit/spigot servers and is multi-platform
MIT License
48 stars 2 forks source link
async database indexed java keyvaluestore minecraft mongodb mysql orm spigot sqlite



Issues Closed issues Forks Stars jitpack License

NDatabase

NDatabase is a lightweight and easy to use indexed key-value store database framework mainly aimed for minecraft servers and is multi-platform (currently supporting Bukkit / Spigot servers). It can be used as a plugin, so you can install it once and use it everywhere without having to configure a database and duplicate connection pool each time you develop a new plugin. The API provide a fluent way to handle Async data fetch and server main thread callback with async to sync mechanism. NDatabase can support multiple databases (currently MySQL, MariaDB, SQLite, and MongoDB implementation). NDatabase can support java 8 from 18 and higher and all minecraft server version (tested from 1.8 to 1.19+).

NDatabase WIKI - Spigot page

I used NDatabase in my own server that could handle 500 concurrent players, you can have more interesting technical details in this repo

Benefits of using NDatabase

Repository<UUID, PlayerStats> repository = NDatabase.api().getOrCreateRepository(PlayerStats.class);

Your repository is now ready, and you can now use it for operations such as insert, update, delete, get, find, etc. Note that you can recall the same method to get your repository from anywhere as the repository instance for this class type is cached.

Here is an overview about how it works and how can it be used with multiple plugins.

drawing

Fluent async to sync API

As you may know, a minecraft server has a main thread which handle the logic game tick and synchronisation, the server can tick up to 20 times per seconds (50 ms per tick) which mean, if you are doing heavy process in the main thread and it lead the server to take more than 50 ms to tick, your server will lag and tick less often.

That's why you should always process heavy task and I/O task asynchronously, but there is another issue, you can't/should not mute the game state (eg: call Bukkit methods) asynchronously because it will break the synchronization or even crash your server. Most of server software will simply prevent you from doing that.

In the scenario where you want to retrieve data asynchronously and use it inside your game context, you can do that by using the bukkit scheduler. The idea is to get the data in another thread and then schedule in the main thread, a task that is consuming your retrived data. It's doable by using the Bukkit methods but NDatabase provide you a fluent API to do that.

Async and Sync examples:


Repository<String, BlockDTO> blockRepository = NDatabase.api().getOrCreateRepository(BlockDTO.class);
// Async to Sync (get data async and consume it in the main thread)
blockRepository.getAsync("id")
.thenSync((bloc, exception) -> {
if(exception != null) {
// Handle exception
return;
}
placeBlockInWorld(bloc);
});

Repository<UUID, PlayerDTO> playerRepository = NDatabase.api().getOrCreateRepository(PlayerDTO.class); // Full Async (get data async and consume it in the same async thread) playerRepository.getAsync(joinedPlayer.getUUID()) .thenAsync((playerDTO, exception) -> { if(exception != null) { // Handle exception return; } loadPlayer(playerDTO); });


> Query Example : Get best players, which have score >= 100 or a specific discord id
```java
List<PlayerData> bestPlayers = repository.find(NQuery.predicate("$.statistics.score >= 100 || $.discordId == 3432487284963298"));
drawing

Documentation

NDatabase is designed to be fast and easy to use and still support value indexed as well. This framework will cover most of your use-case, but I recommend you to read the documentation to know about general best practices.

NDatabase WIKI

Build jar or API

mvn clean install -DSkipTests

It will create the complete jar in ndatabase-packaging-jar/target and the API in ndatabase-api/target

Future objectives

WIP