Skip to content

Exorath/ExoData

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ExoData

Database management for games

Maven:

How to use

###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.

Getting a collection

//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");

Fetching your first document

//If the document does not exist yet, a document will be created and returned
Document bsonDoc = document.fetch().timeout(3, TimeUnit.SECONDS).toBlocking().first();

Incrementing a field

//Now I did not add a toBlocking, this means our thread will not wait for the operation to finish
document.inc("key", 123).subscribe();

Decrementing a field if there is enough

//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