Class StoreArchiver
- java.lang.Object
-
- edu.uiuc.ncsa.security.storage.cli.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 Summary
Fields Modifier and Type Field Description static String
ARCHIVE_VERSION_SEPARATOR_TAG
Separator between the version tag and the version number.static String
ARCHIVE_VERSION_TAG
Key in the fragment for the version
-
Constructor Summary
Constructors Constructor Description StoreArchiver(Store store)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addToBatch(PreparedStatement stmt, Identifier oldID)
void
addToBatch(PreparedStatement stmt, Identifier oldID, boolean isNew)
If you know this is a completely new version, then set isNew to true and the version will be set to 0.protected Long
create(Identifier identifier)
Create a new version.Identifier
createVersionedID(Identifier id, long version)
Given a base id and the new version number, create the identifierString
createVersionStatement()
Creates the batch update statement for this archive store.Identifier
getBaseID(Identifier overloadedID)
Removes the fragment (with the version number).protected Long
getLatestVersionNumber(DoubleHashMap<URI,Long> versionNumbers)
Get the latest version number or return a -1 if no versions present.MapConverter
getMapConverter()
Store
getStore()
Identifiable
getVersion(Identifier targetID, long version)
Given the raw id and a version number (which may be -1 to indicate using the latest) get the stored version.long
getVersionNumber(Identifier id)
Given a version id (of form URI#version=number), return the number.List<Long>
getVersionNumbers(Identifier id)
protected DoubleHashMap<URI,Long>
getVersions(Identifier identifier)
For a given object in the store, return all the versions associated with it in aDoubleHashMap
.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.boolean
isVersion(Identifier id)
Boolean to test if the ID represents a versioned item.static void
main(String[] args)
void
remove(Identifier identifier, long version)
Removed the version of the object from the store.boolean
restore(Identifier id, Long version)
For a
-
-
-
Constructor Detail
-
StoreArchiver
public StoreArchiver(Store store)
-
-
Method Detail
-
getStore
public Store getStore()
-
getMapConverter
public MapConverter getMapConverter()
-
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 identifierURI#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 aDoubleHashMap
. Note that the keys are of the formURI#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 togetVersions(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 genericStore
.- Parameters:
identifier
-- Returns:
-
getBaseID
public Identifier getBaseID(Identifier overloadedID)
Removes the fragment (with the version number).- Parameters:
overloadedID
-- Returns:
-
getVersionNumbers
public List<Long> getVersionNumbers(Identifier id)
-
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!- Get this statement as a string, call it stmt
- get a connection, call it c
- Create a
PreparedStatement
as pstmt =c.Connection.prepareStatement(String)
using stmt - As you get values, set them with
addToBatch(PreparedStatement, Identifier)
(if you need the system to figure out the new version) oraddToBatch(PreparedStatement, Identifier, boolean)
with a last argument of true to create a completely new version - Nota Bene: do not add to the batch statement using the standard SQL call, since this does not get the versioned id right!
- When you are done, call the standard SQL pstmt.executeBatch()
- Returns:
-
addToBatch
public void addToBatch(PreparedStatement stmt, Identifier oldID) throws SQLException
- Throws:
SQLException
-
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
-
-