Class VariablesStorage
- All Implemented Interfaces:
Closeable
- Direct Known Subclasses:
FlatFileStorage
,SQLStorage
This is usually some sort of database, and could be as simply as a text file.
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionprotected @Nullable Task
The backup task, ornull
if automatic backups are disabled.protected boolean
Whether this variable storage has beenclosed
.protected final Object
Must be locked afterVariables.getReadLock()
(if that lock is used at all).protected final String
The name of the database, i.e.protected @Nullable File
The file associated with this variable storage. -
Constructor Summary
ModifierConstructorDescriptionprotected
VariablesStorage
(String name) Creates a new variable storage with the given name. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract void
Called after all storages have been loaded, and variables have been redistributed if settings have changed.protected void
Clears thequeue
of unsaved variables.void
close()
Called when Skript gets disabled.protected abstract boolean
connect()
(Re)connects to the database.protected abstract void
Disconnects from the database.protected abstract File
Gets the file needed for this variable storage from the given file name.protected @Nullable String
getValue
(SectionNode sectionNode, String key) Gets the string value at the given key of the given section node.protected <T> T
getValue
(SectionNode sectionNode, String key, Class<T> type) Gets the value at the given key of the given section node, parsed with the given type.final boolean
load
(SectionNode sectionNode) Loads the configuration for this variable storage from the given section node.protected abstract boolean
Loads variables stored here.protected abstract boolean
Checks if this storage requires a file for storing its data.protected abstract boolean
Saves a variable.void
startBackupTask
(Timespan backupInterval) Starts the backup task, with the given backup interval.
-
Field Details
-
closed
protected volatile boolean closedWhether this variable storage has beenclosed
. -
databaseName
The name of the database, i.e. this storage. -
file
The file associated with this variable storage. Can benull
if no file is required. -
connectionLock
Must be locked afterVariables.getReadLock()
(if that lock is used at all). -
backupTask
The backup task, ornull
if automatic backups are disabled.
-
-
Constructor Details
-
VariablesStorage
Creates a new variable storage with the given name.This will also create the
writeThread
, but it must be started withload(SectionNode)
.- Parameters:
name
- the name.
-
-
Method Details
-
getValue
Gets the string value at the given key of the given section node.- Parameters:
sectionNode
- the section node.key
- the key.- Returns:
- the value, or
null
if the value was invalid, or not found.
-
getValue
Gets the value at the given key of the given section node, parsed with the given type.- Type Parameters:
T
- the type.- Parameters:
sectionNode
- the section node.key
- the key.type
- the type.- Returns:
- the parsed value, or
null
if the value was invalid, or not found.
-
load
Loads the configuration for this variable storage from the given section node.- Parameters:
sectionNode
- the section node.- Returns:
- whether the loading succeeded.
-
load_i
Loads variables stored here.- Returns:
- Whether the database could be loaded successfully, i.e. whether the config is correct and all variables could be loaded.
-
allLoaded
protected abstract void allLoaded()Called after all storages have been loaded, and variables have been redistributed if settings have changed. This should commit the first transaction (which is not empty if variables have been moved from another database to this one or vice versa), and start repeating transactions if applicable. -
requiresFile
protected abstract boolean requiresFile()Checks if this storage requires a file for storing its data.- Returns:
- if this storage needs a file.
-
getFile
Gets the file needed for this variable storage from the given file name.Will only be called if
requiresFile()
istrue
.- Parameters:
fileName
- the given file name.- Returns:
- the
File
object.
-
connect
protected abstract boolean connect()(Re)connects to the database.Not called on the first connect: do this in
load_i(SectionNode)
. An error should be printed by this method prior to returningfalse
.- Returns:
- whether the connection could be re-established.
-
disconnect
protected abstract void disconnect()Disconnects from the database. -
startBackupTask
Starts the backup task, with the given backup interval.- Parameters:
backupInterval
- the backup interval.
-
close
public void close()Called when Skript gets disabled.The default implementation will wait for all variables to be saved before setting
closed
totrue
and stopping thewrite thread
.Therefore, make sure to call
super.close()
if this method is overridden. -
clearChangesQueue
protected void clearChangesQueue()Clears thequeue
of unsaved variables.Only used if all variables are saved immediately after calling this method.
-
save
protected abstract boolean save(String name, @Nullable @Nullable String type, @Nullable @org.jetbrains.annotations.Nullable byte[] value) Saves a variable.This is called from the main thread while variables are transferred between databases, and from the
writeThread
afterwards.type
andvalue
are bothnull
iff this call is to delete the variable.- Parameters:
name
- the name of the variable.type
- the type of the variable.value
- the serialized value of the variable.- Returns:
- Whether the variable was saved.
-