Class StoreArchiver


  • public class StoreArchiver
    extends Object
    Class to encapsulate archive CRUD operations for a store. Having it here lets us use it in other places, e.g. QDL.

    Created by Jeff Gaynor
    on 10/19/21 at 6:14 AM

    • Field Detail

      • ARCHIVE_VERSION_TAG

        public static String ARCHIVE_VERSION_TAG
        Key in the fragment for the version
      • ARCHIVE_VERSION_SEPARATOR_TAG

        public static String ARCHIVE_VERSION_SEPARATOR_TAG
        Separator between the version tag and the version number.
    • Constructor Detail

      • StoreArchiver

        public StoreArchiver​(Store store)
    • Method Detail

      • getStore

        public Store getStore()
      • getVersionNumber

        public long getVersionNumber​(Identifier id)
        Given a version id (of form URI#version=number), return the number. If the item is not versioned this returns a -1.
        Parameters:
        id -
        Returns:
      • isVersion

        public boolean isVersion​(Identifier id)
        Boolean to test if the ID represents a versioned item. Used in OA4MP.
        Parameters:
        id -
        Returns:
      • createVersionedID

        public Identifier createVersionedID​(Identifier id,
                                            long version)
        Given a base id and the new version number, create the identifier
             URI#version=number
         
        This is to make sure everythign is created identically.
        Parameters:
        id -
        version -
        Returns:
      • main

        public static void main​(String[] args)
      • getVersions

        protected DoubleHashMap<URI,​Long> getVersions​(Identifier identifier)
        For a given object in the store, return all the versions associated with it in a DoubleHashMap. Note that the keys are of the form
             URI#version=number
         
        and the value is the number. As a double hash map then you can do a reverse lookup by version number and get the unique identifier.
        Parameters:
        identifier -
        Returns:
      • getVersionsMap

        public TreeMap<Long,​Identifiable> getVersionsMap​(Identifiable identifiable)
        For a given object, get all the versions (not just their identifiers) and return in a map keyed by version number. This sorts the entries. Rather similar to getVersions(Identifier), this has key = number and value =URI#version=number. Use this where you need all the versions sorted by number for, e.g., display purposes.
        Parameters:
        identifiable -
        Returns:
      • getLatestVersionNumber

        protected Long getLatestVersionNumber​(DoubleHashMap<URI,​Long> versionNumbers)
        Get the latest version number or return a -1 if no versions present. Remember that versions increase, so version 0 is the first made, 1 is the next,... and the highest number is the most recent version.
        Parameters:
        versionNumbers -
        Returns:
      • getVersion

        public Identifiable getVersion​(Identifier targetID,
                                       long version)
                                throws IOException
        Given the raw id and a version number (which may be -1 to indicate using the latest) get the stored version. Might return null of there is no such version.
        Parameters:
        targetID -
        version -
        Returns:
        Throws:
        IOException
      • remove

        public void remove​(Identifier identifier,
                           long version)
        Removed the version of the object from the store. This either returns or throws a runtime exception from the store.

        Note

        There is not a version with signature remove(Identifier) because of the risk of removing the base object if the wrong .
        Parameters:
        identifier -
        version -
      • create

        protected Long create​(Identifier identifier)
        Create a new version. This returns the overloaded identifier of the versioned object. Note that this does not require mucking around with e.g. QDL connections and works on every generic Store.
        Parameters:
        identifier -
        Returns:
      • getBaseID

        public Identifier getBaseID​(Identifier overloadedID)
        Removes the fragment (with the version number).
        Parameters:
        overloadedID -
        Returns:
      • restore

        public boolean restore​(Identifier id,
                               Long version)
        For a
        Parameters:
        id -
        version -
        Returns:
      • createVersionStatement

        public String createVersionStatement()
        Creates the batch update statement for this archive store. You use this as follows.

        Nota Bene: It only works for SQL stores!
        1. Get this statement as a string, call it stmt
        2. get a connection, call it c
        3. Create a PreparedStatement as pstmt =c.Connection.prepareStatement(String) using stmt
        4. As you get values, set them with
          addToBatch(PreparedStatement, Identifier) (if you need the system to figure out the new version) or addToBatch(PreparedStatement, Identifier, boolean) with a last argument of true to create a completely new version
        5. Nota Bene: do not add to the batch statement using the standard SQL call, since this does not get the versioned id right!
        6. When you are done, call the standard SQL pstmt.executeBatch()
        The major argument for doing this is that the processing happens almost exclusively on the server. For very large numbers of archives, this makes a huge difference.
        Returns:
      • addToBatch

        public void addToBatch​(PreparedStatement stmt,
                               Identifier oldID,
                               boolean isNew)
                        throws SQLException
        If you know this is a completely new version, then set isNew to true and the version will be set to 0. This will avoid a call to the database to check the version. If you get this wrong, you will get a primary key exception on update, since the version exists. This is quite useful if you are, e.g., migrating a store.
        Parameters:
        stmt -
        oldID -
        isNew -
        Throws:
        SQLException