Package io.github.mastodonContentMover
Class PostArchive
java.lang.Object
io.github.mastodonContentMover.PostArchive
Manages a collection of
Uses the singleton model - for current (and anticipated) use cases, there should not be a need for more than one
The
Currently an archive can only be written once; a second attempt to write to the same archive name will throw an exception. Later it will hopefully be possible to archive incrementally, choosing whether to overwrite or add to existing data (with the add perhaps doing something intelligent around modification dates of the XML file compared to the most recent edited timestamp for a status on the Mastodon instance).
Post
objects
associated with an archive of Mastodon statuses, including loading them from file
on startup, maintaining indexes on them, and handling date/time processing related to
the internal archiveId
used as the main reference for all Post
objects.
Uses the singleton model - for current (and anticipated) use cases, there should not be a need for more than one
PostArchive
object
to be instantiated simultaneously. This also means that synchronized methods can be
relied on to be synchronized on the one singleton object.
The
PostArchive
object itself does not persist
but instead uses the archive name provided on instantiation to locate a set of
persisted Post
objects in the filesystem and
load them into memory, populating two lookup indexes for them as it goes.
Currently an archive can only be written once; a second attempt to write to the same archive name will throw an exception. Later it will hopefully be possible to archive incrementally, choosing whether to overwrite or add to existing data (with the add perhaps doing something intelligent around modification dates of the XML file compared to the most recent edited timestamp for a status on the Mastodon instance).
- Since:
- 0.01.00
-
Method Summary
Modifier and TypeMethodDescriptionprotected Post
Creates a newPost
object in this archive for a status with the specified creation date and time, and the specifiedmastodonId
.protected Collection<Post>
Obtains aCollection
view of allPost
objects held in this archive, from oldest to newest when iterated.protected String
Obtain the filesystem directory name within which data for thisPostArchive
instance is stored.protected static String
getInstanceAddressFromMastodonId
(String mastodonId) Retrieves the Mastodon instance hostname or address from a concatenatedmastodonId
String
value that also contains a separator and the id used for a status on that instance.protected static String
getInstanceIdFromMastodonId
(String mastodonId) Retrieves the id used for a status on a Mastodon instance from a concatenatedmastodonId
String
value that also contains the Mastodon instance hostname or address.protected static String
getMastodonId
(String instanceAddress, String id) protected Post
Retrieves a reference to thePost
object with the specifiedarchiveId
, based on the creation date and time of a Mastodon status, stored in thisPostArchive
protected Post
Retrieves a reference to thePost
object in thisPostArchive
with the specifiedmastodonId
, which is comprised of the address or hostname of the instance from which thePost
was saved or has been reposted, and the internal id of the corresponding status on that instance.protected int
Obtain the number ofPost
objects currently stored within thisPostArchive
instance.protected static PostArchive
getSingletonInstance
(String archiveName, boolean loadOK) Obtains a reference to the currently instantiatedPostArchive
singleton object, if the specified archive name is the same, or instantiates one if that has not already been done.protected boolean
postIsAfterDateTime
(String postId, String isoDateTime) Determines whether a givenarchiveId
is based on a date and time after a specified ISO 8601 compliantString
.protected boolean
postIsBeforeDateTime
(String postId, String isoDateTime) Determines whether a givenarchiveId
is based on a date and time before a specified ISO 8601 compliantString
.protected void
registerPostByArchiveId
(Post postObject) Adds a reference to aPost
object to the index maintained by this archive according to MastodonContentMover internalarchiveId
.protected void
registerPostByMastodonId
(Post postObject, String mastodonId)
-
Method Details
-
getSingletonInstance
protected static PostArchive getSingletonInstance(String archiveName, boolean loadOK) throws jakarta.xml.bind.JAXBException, IOException Obtains a reference to the currently instantiatedPostArchive
singleton object, if the specified archive name is the same, or instantiates one if that has not already been done.- Parameters:
archiveName
- the name of the archive, also used for the directory in which archive data is storedloadOK
-true
to load existing archive data from the filesystem, orfalse
otherwise- Returns:
- a reference to an instance of
PostArchive
for the specified archive name if no otherPostArchive
(for another archive name) was already instantiated - Throws:
IllegalStateException
- if aPostArchive
singleton is already instantiated for a different archive namejakarta.xml.bind.JAXBException
- (TODO: add more info here on when this happens)IOException
- (TODO: add more info here on when this happens)- Since:
- 0.01.00
-
getDirectory
- Returns:
- a
String
containing the filesystem directory name within which data for thisPostArchive
instance is stored - Since:
- 0.01.00
-
getAllPosts
Obtains aCollection
view of allPost
objects held in this archive, from oldest to newest when iterated.
An iterator on thisCollection
gives thePost
objects in ascending order ofarchiveId
(with the underlying implementation being aTreeMap
.- Returns:
- a
Collection
view of allPost
objects held in this archive, from oldest to newest when iterated - Since:
- 0.01.00
- See Also:
-
getPostCount
protected int getPostCount()- Returns:
- the number of
Post
objects currently stored within thisPostArchive
instance - Since:
- 0.01.00
-
addPost
protected Post addPost(String createdAt, String mid) throws jakarta.xml.bind.JAXBException, IOException Creates a newPost
object in this archive for a status with the specified creation date and time, and the specifiedmastodonId
.
Suspect this should actually accept the instance hostname/address and instance id rather than themastodonId
, and then generate themastodonId
internally to this class (as it does with the archiveId from the creation date/time) to keep the logic where it should be. Should fix this (TODO) This method may either need to be substantially overhauled or another method created to check first whether aPost
object already exists in the archive for a given creation date and time when the tool is adjusted to allow for incremental archiving (TODO), when encountering a status during saving will need to then either overwrite or add to an existingPost
record if one already exists. Right now that never happens. One option is to add a parameter to specify incremental overwriting or addition (or create a whole separate method).- Parameters:
createdAt
- an ISO 8601 compliantString
representing the date and time in UTC (with the "Z") when a status was createdmid
- amastodonId
, comprised of the address or hostname for a Mastodon instance and the internal id for a status on that instance- Returns:
- a
Post
object in thisPostArchive
for the status creation date andmastodonId
specified - Throws:
jakarta.xml.bind.JAXBException
- (TODO: add more info here on when this happens)IOException
- (TODO: add more info here on when this happens)- Since:
- 0.01.00
-
registerPostByArchiveId
Adds a reference to aPost
object to the index maintained by this archive according to MastodonContentMover internalarchiveId
.
TODO: Does this need to be separate to the method formastodonId
? Check and rationalise if needed. Exception should also be more specific.- Parameters:
postObject
- thePost
for which a reference should be added to thearchiveId
index- Throws:
RuntimeException
- if the index already contains a reference for thearchiveId
of thePost
object passed as a parameter- Since:
- 0.01.00
- See Also:
-
getMastodonId
Obtains amastodonId
String
value, comprised of the Mastodon instance hostname/address concatenated with a separator and the id used for a status on that instance, used to index and retrievePost
objects held in this archive and reconcile them with statuses on Mastodon instances.- Parameters:
instanceAddress
- the hostname or address of a Mastodon instanceid
- the instance-specific id used by Mastodon for a status on the specified instance- Returns:
- the Mastodon instance hostname/address concatenated with a separator and
the id used for a status on that instance
- Since:
- 0.01.00
-
getInstanceAddressFromMastodonId
Retrieves the Mastodon instance hostname or address from a concatenatedmastodonId
String
value that also contains a separator and the id used for a status on that instance.- Parameters:
mastodonId
- a Mastodon instance hostname/address concatenated with a separator and the id used for a status on that instance- Returns:
- the hostname or address of a Mastodon instance
- Since:
- 0.01.00
-
getInstanceIdFromMastodonId
Retrieves the id used for a status on a Mastodon instance from a concatenatedmastodonId
String
value that also contains the Mastodon instance hostname or address.- Parameters:
mastodonId
- a Mastodon instance hostname/address concatenated with a separator and the id used for a status on that instance- Returns:
- the instance-specific id used by Mastodon for a status on the specified instance
- Since:
- 0.01.00
-
registerPostByMastodonId
Adds a reference to aPost
object to the index maintained by this archive according tomastodonId
, which is comprised of the address or hostname of the instance from which thePost
was saved or has been reposted, and the internal id of the corresponding status on that instance.
TODO: Does this need to be separate to the method forarchiveId
? Check and rationalise if needed. Exception should also be more specific.- Parameters:
postObject
- thePost
for which a reference should be added to themastodonId
indexmastodonId
- themastodonId
that should reference thePost
object specified in the index- Throws:
RuntimeException
- if the index already contains a reference for themastodonId
of thePost
object passed as a parameter- Since:
- 0.01.00
- See Also:
-
getPostByArchiveId
Retrieves a reference to thePost
object with the specifiedarchiveId
, based on the creation date and time of a Mastodon status, stored in thisPostArchive
- Parameters:
id
- a validarchiveId
- Returns:
- a
Post
object with the correspondingarchiveId
, ornull
if no matching object is in the archive - Since:
- 0.01.00
- See Also:
-
getPostByMastodonId
Retrieves a reference to thePost
object in thisPostArchive
with the specifiedmastodonId
, which is comprised of the address or hostname of the instance from which thePost
was saved or has been reposted, and the internal id of the corresponding status on that instance.- Parameters:
mid
- a validmastodonId
- Returns:
- a
Post
object that was saved from or has been reposted to a Mastodon instance matching the specifiedmastodonId
, ornull
if no matching object is in the archive - Since:
- 0.01.00
- See Also:
-
postIsBeforeDateTime
Determines whether a givenarchiveId
is based on a date and time before a specified ISO 8601 compliantString
.
TODO: This method and parameter should be renamed since it's more about the archiveId than a post. Also the debug needs to be clearer.
Method is non-static so that if any processing related to a specific post in an archive instance is needed, it can be performed. -
postIsAfterDateTime
Determines whether a givenarchiveId
is based on a date and time after a specified ISO 8601 compliantString
.
TODO: This method and parameter should be renamed since it's more about the archiveId than a post. Also the debug needs to be clearer.
Method is non-static so that if any processing related to a specific post in an archive instance is needed, it can be performed.
-