Class AbstractDataManager<T extends RecordProducer,K>

java.lang.Object
wbs.utils.util.database.AbstractDataManager<T,K>
Type Parameters:
T - The type that can be saved to the database.
K - The type used as the primary key in the database.

public abstract class AbstractDataManager<T extends RecordProducer,K> extends Object
Represents an object that can be used to save & retrieve WbsRecords from a database.

Retrieves from a given table by default, but can be extended to read complex objects across multiple.

  • Field Details

  • Constructor Details

    • AbstractDataManager

      public AbstractDataManager(WbsPlugin plugin, WbsTable table)
      Parameters:
      plugin - The related WbsPlugin
      table - The WbsTable to use as the default table for simple operations.
  • Method Details

    • getCache

      public Map<K,T> getCache()
      Gets a shallow copy of the merged cache, including both the guaranteed cache and the volatile cache.
      Returns:
      The merged cache.
    • getDefaultTable

      protected WbsTable getDefaultTable()
      Returns:
      The default table, as specified on construction.
    • setCacheSize

      public void setCacheSize(int size)
      Set the number of objects that are cached in active memory to prevent unneeded calls to the database. Changing the cache size while populated with more entries than the new size will not remove excess entries.
      This is the number of records guaranteed to exist in the cache once populated, however more may be stored as SoftReferences.
      Parameters:
      size - The new size of the cache.
    • clearCache

      public int clearCache()
      Clear the cache.
      Returns:
      The number of entries the cache contained prior to being cleared.
    • addToCache

      protected void addToCache(K key, T value)
      Adds a key/value entry to the cache directly
      Parameters:
      key - The key for the cache entry
      value - The value to add to the cache
    • get

      @NotNull public T get(K key)
      Synchronously get a record based on it's key. It's recommended to use getAsync(Object, Consumer) to avoid freezing the server, or use this in an asynchronous thread.
      Parameters:
      key - The key to retrieve by.
      Returns:
      The value, or a new value based on the key.
    • getCached

      @Nullable public T getCached(K key)
      Get a value from the cache if it exists
      Parameters:
      key - The key to retrieve by
      Returns:
      The value to which the key is mapped, or null otherwise.
    • getAsync

      public int getAsync(K key, @NotNull @NotNull Consumer<T> callback)
      Get a record asynchronously, or synchronously if the value is cached.
      Parameters:
      key - The key to retrieve by.
      callback - The consumer to be called when the value is available.
      Returns:
      The Id of the scheduled BukkitTask. To get the retrieved object, accept it in the callback.
    • saveCacheAsync

      public void saveCacheAsync()
      Write all cached values to the database
    • saveCache

      public void saveCache()
      Write all cached values to the database
    • saveAsync

      public void saveAsync(Collection<T> toInsert)
      Write a collection of objects to the database asynchronously.
      Parameters:
      toInsert - The objects to insert
    • saveAsync

      public void saveAsync(Collection<T> toInsert, Runnable callback)
      Write a collection of objects to the database asynchronously, with a callback for when the operation has been completed.
      Parameters:
      toInsert - The objects to insert.
      callback - A callback to run once the operation is complete.
    • save

      public void save(Collection<T> toInsert)
      Save the given collection of AbstractDataManager to the database synchronously.
      Parameters:
      toInsert - The records to save to the database.
    • select

      @Nullable protected @Nullable WbsRecord select(List<K> keys)
      Overrideable field for selecting from the database. Just reads from the default table by default.
      Parameters:
      keys - An ordered list of keys to use against the default table's primary keys.
      Returns:
      The found record, or null if none found.
    • fromRecord

      @NotNull protected abstract T fromRecord(@NotNull @NotNull WbsRecord record)
      Create an object based on the record representing it.
      Parameters:
      record - The record to be read into a new object
      Returns:
      A non-null object of type T
    • produceDefault

      @NotNull protected abstract T produceDefault(K key)
      Create an object based on the key. Used for when no record was found in the database.
      Parameters:
      key - The key to use to generate the new object.
      Returns:
      A possibly null object of type T