Class VariablesStorage

java.lang.Object
ch.njol.skript.variables.VariablesStorage
All Implemented Interfaces:
Closeable
Direct Known Subclasses:
FlatFileStorage, SQLStorage

public abstract class VariablesStorage extends Object implements Closeable
A variable storage is holds the means and methods of storing variables.

This is usually some sort of database, and could be as simply as a text file.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected @Nullable Task
    The backup task, or null if automatic backups are disabled.
    protected boolean
    Whether this variable storage has been closed.
    protected final Object
    Must be locked after Variables.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

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new variable storage with the given name.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected abstract void
    Called after all storages have been loaded, and variables have been redistributed if settings have changed.
    protected void
    Clears the queue of unsaved variables.
    void
    Called when Skript gets disabled.
    protected abstract boolean
    (Re)connects to the database.
    protected abstract void
    Disconnects from the database.
    protected abstract File
    getFile(String fileName)
    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
    save(String name, @Nullable String type, @org.jetbrains.annotations.Nullable byte[] value)
    Saves a variable.
    void
    startBackupTask(Timespan backupInterval)
    Starts the backup task, with the given backup interval.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • closed

      protected volatile boolean closed
      Whether this variable storage has been closed.
    • databaseName

      protected final String databaseName
      The name of the database, i.e. this storage.
    • file

      @Nullable protected @Nullable File file
      The file associated with this variable storage. Can be null if no file is required.
    • connectionLock

      protected final Object connectionLock
      Must be locked after Variables.getReadLock() (if that lock is used at all).
    • backupTask

      @Nullable protected @Nullable Task backupTask
      The backup task, or null if automatic backups are disabled.
  • Constructor Details

    • VariablesStorage

      protected VariablesStorage(String name)
      Creates a new variable storage with the given name.

      This will also create the writeThread, but it must be started with load(SectionNode).

      Parameters:
      name - the name.
  • Method Details

    • getValue

      @Nullable protected @Nullable String getValue(SectionNode sectionNode, String key)
      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

      @Nullable 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.
      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

      public final boolean load(SectionNode sectionNode)
      Loads the configuration for this variable storage from the given section node.
      Parameters:
      sectionNode - the section node.
      Returns:
      whether the loading succeeded.
    • load_i

      protected abstract boolean load_i(SectionNode n)
      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

      protected abstract File getFile(String fileName)
      Gets the file needed for this variable storage from the given file name.

      Will only be called if requiresFile() is true.

      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 returning false.

      Returns:
      whether the connection could be re-established.
    • disconnect

      protected abstract void disconnect()
      Disconnects from the database.
    • startBackupTask

      public void startBackupTask(Timespan backupInterval)
      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 to true and stopping the write thread.

      Therefore, make sure to call super.close() if this method is overridden.

      Specified by:
      close in interface Closeable
    • clearChangesQueue

      protected void clearChangesQueue()
      Clears the queue 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 and value are both null 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.