Class VariablesStorage
- All Implemented Interfaces:
Closeable,AutoCloseable
- 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
FieldsModifier and TypeFieldDescriptionprotected @Nullable TaskThe backup task, ornullif automatic backups are disabled.protected booleanWhether this variable storage has beenclosed.protected final ObjectMust be locked afterVariables.getReadLock()(if that lock is used at all).protected @Nullable FileThe file associated with this variable storage. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedVariablesStorage(String type) Creates a new variable storage with the given name. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voidCalled after all storages have been loaded, and variables have been redistributed if settings have changed.protected voidClears thequeueof unsaved variables.voidclose()Called when Skript gets disabled.protected abstract booleanconnect()(Re)connects to the database.protected abstract voidDisconnects from the database.protected final StringGet the config type of a databaseprotected abstract FileGets the file needed for this variable storage from the given file name.@Nullable PatternReturns the name pattern accepted by this variable storageprotected final StringGet the config name of a databaseprotected @Nullable StringgetValue(SectionNode sectionNode, String key) Gets the string value at the given key of the given section node.protected <T> TgetValue(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 booleanload(SectionNode sectionNode) Loads the configuration for this variable storage from the given section node.protected abstract booleanLoads variables stored here.protected abstract booleanChecks if this storage requires a file for storing its data.protected abstract booleanSaves a variable.voidstartBackupTask(Timespan backupInterval, boolean removeBackups, int toKeep) Starts the backup task, with the given backup interval.
-
Field Details
-
closed
protected volatile boolean closedWhether this variable storage has beenclosed. -
file
The file associated with this variable storage. Can benullif no file is required. -
connectionLock
Must be locked afterVariables.getReadLock()(if that lock is used at all). -
backupTask
The backup task, ornullif 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:
type- the database type i.e. CSV.
-
-
Method Details
-
getUserConfigurationName
Get the config name of a databaseNote: Returns the user set name for the database, ex:
default: <- Config Name type: CSV- Returns:
- name of database
-
getDatabaseType
Get the config type of a database- Returns:
- type of databse
-
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
nullif 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
nullif 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
Fileobject.
-
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.
-
getNamePattern
Returns the name pattern accepted by this variable storage- Returns:
- the name pattern, or null if accepting all
-
close
public void close()Called when Skript gets disabled.The default implementation will wait for all variables to be saved before setting
closedtotrueand stopping thewrite thread.Therefore, make sure to call
super.close()if this method is overridden.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-
clearChangesQueue
protected void clearChangesQueue()Clears thequeueof 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
writeThreadafterwards.typeandvalueare bothnulliff 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.
-