Database management for games
###RxJava ExoData uses RxJava for observables. ALL observables are cold, this means the operation will only occur once the observable gets subscribed. IO operations are done on the io threadpool of RxJava, you can always do Observable#toBlocking() to ensure you observe it synchronously.
//Connects to MongoDB on the default port. This operation requires IO (I may make it an observable in the future)
DataAPI dataAPI = DataAPI.create();
//Let's get a database, we make this a blocking operation for simplicity. This means your thread will be blocked until the database is fetched.
ExoDatabase database = dataAPI.getDatabase("name").timeout(3, TimeUnit.SECONDS).toBlocking().first();
//Let's get a collection.
ExoCollection collection = database.getCollection("name").timeout(3, TimeUnit.SECONDS).toBlocking().first();
//Let's get our first Document. This operation does not do any IO.
ExoDocument document = collection.getDocument("testid");
//If the document does not exist yet, a document will be created and returned
Document bsonDoc = document.fetch().timeout(3, TimeUnit.SECONDS).toBlocking().first();
//Now I did not add a toBlocking, this means our thread will not wait for the operation to finish
document.inc("key", 123).subscribe();
//Now I did not add a toBlocking, this means our thread will not wait for the operation to finish
document.decrementIfSufficient("key", 123).subscribe(updateResult -> {
//We check if there was a modified document or a document was created. I may write another method that returns a boolean to simplify this.
if(updateResult.getModifiedCount() > 0 || updateResult.getUpsertedId() != null)
System.out.println("Decremented key");
else
System.out.println("Failed to decrement key, the value was not large enough");
});
##TODO
- Observable for external changes (for example if your application's website has updated a field)
- More tests
- Example projects