Package wbs.utils.util.database
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.
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumWhat type of volatile caching to use, where:AbstractDataManager.VolatileCacheType.SOFTusesSoftReferences;AbstractDataManager.VolatileCacheType.WEAKusesWeakReferences; andAbstractDataManager.VolatileCacheType.DISABLEDdisables the use of the volatile cache entirely -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final WbsPluginThe relatedWbsPlugin.protected AbstractDataManager.VolatileCacheTypeRepresents which caching type to use for the volatile cache. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidaddToCache(K key, T value) Adds a key/value entry to the cache directlyintClear the cache.protected abstract TfromRecord(@NotNull WbsRecord record) Create an object based on the record representing it.Synchronously get a record based on it's key.intGet a record asynchronously, or synchronously if the value is cached.getCache()Gets a shallow copy of the merged cache, including both the guaranteed cache and the volatile cache.Get a value from the cache if it existsprotected WbsTableprotected abstract TproduceDefault(K key) Create an object based on the key.voidsave(Collection<T> toInsert) Save the given collection ofAbstractDataManagerto the database synchronously.voidsaveAsync(Collection<T> toInsert) Write a collection of objects to the database asynchronously.voidsaveAsync(Collection<T> toInsert, Runnable callback) Write a collection of objects to the database asynchronously, with a callback for when the operation has been completed.voidWrite all cached values to the databasevoidWrite all cached values to the databaseprotected @Nullable WbsRecordOverrideable field for selecting from the database.voidsetCacheSize(int size) Set the number of objects that are cached in active memory to prevent unneeded calls to the database.
-
Field Details
-
plugin
The relatedWbsPlugin. -
volatileCacheType
Represents which caching type to use for the volatile cache.
-
-
Constructor Details
-
Method Details
-
getCache
Gets a shallow copy of the merged cache, including both the guaranteed cache and the volatile cache.- Returns:
- The merged cache.
-
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 asSoftReferences.- 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
Adds a key/value entry to the cache directly- Parameters:
key- The key for the cache entryvalue- The value to add to the cache
-
get
Synchronously get a record based on it's key. It's recommended to usegetAsync(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
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
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
Write a collection of objects to the database asynchronously.- Parameters:
toInsert- The objects to insert
-
saveAsync
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
Save the given collection ofAbstractDataManagerto the database synchronously.- Parameters:
toInsert- The records to save to the database.
-
select
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
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
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
-