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 PostCreates a newPostobject in this archive for a status with the specified creation date and time, and the specifiedmastodonId.protected Collection<Post>Obtains aCollectionview of allPostobjects held in this archive, from oldest to newest when iterated.protected StringObtain the filesystem directory name within which data for thisPostArchiveinstance is stored.protected static StringgetInstanceAddressFromMastodonId(String mastodonId) Retrieves the Mastodon instance hostname or address from a concatenatedmastodonIdStringvalue that also contains a separator and the id used for a status on that instance.protected static StringgetInstanceIdFromMastodonId(String mastodonId) Retrieves the id used for a status on a Mastodon instance from a concatenatedmastodonIdStringvalue that also contains the Mastodon instance hostname or address.protected static StringgetMastodonId(String instanceAddress, String id) protected PostRetrieves a reference to thePostobject with the specifiedarchiveId, based on the creation date and time of a Mastodon status, stored in thisPostArchiveprotected PostRetrieves a reference to thePostobject in thisPostArchivewith the specifiedmastodonId, which is comprised of the address or hostname of the instance from which thePostwas saved or has been reposted, and the internal id of the corresponding status on that instance.protected intObtain the number ofPostobjects currently stored within thisPostArchiveinstance.protected static PostArchivegetSingletonInstance(String archiveName, boolean loadOK) Obtains a reference to the currently instantiatedPostArchivesingleton object, if the specified archive name is the same, or instantiates one if that has not already been done.protected booleanpostIsAfterDateTime(String postId, String isoDateTime) Determines whether a givenarchiveIdis based on a date and time after a specified ISO 8601 compliantString.protected booleanpostIsBeforeDateTime(String postId, String isoDateTime) Determines whether a givenarchiveIdis based on a date and time before a specified ISO 8601 compliantString.protected voidregisterPostByArchiveId(Post postObject) Adds a reference to aPostobject to the index maintained by this archive according to MastodonContentMover internalarchiveId.protected voidregisterPostByMastodonId(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 instantiatedPostArchivesingleton 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-trueto load existing archive data from the filesystem, orfalseotherwise- Returns:
- a reference to an instance of
PostArchivefor the specified archive name if no otherPostArchive(for another archive name) was already instantiated - Throws:
IllegalStateException- if aPostArchivesingleton 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
Stringcontaining the filesystem directory name within which data for thisPostArchiveinstance is stored - Since:
- 0.01.00
-
getAllPosts
Obtains aCollectionview of allPostobjects held in this archive, from oldest to newest when iterated.
An iterator on thisCollectiongives thePostobjects in ascending order ofarchiveId(with the underlying implementation being aTreeMap.- Returns:
- a
Collectionview of allPostobjects held in this archive, from oldest to newest when iterated - Since:
- 0.01.00
- See Also:
-
getPostCount
protected int getPostCount()- Returns:
- the number of
Postobjects currently stored within thisPostArchiveinstance - Since:
- 0.01.00
-
addPost
protected Post addPost(String createdAt, String mid) throws jakarta.xml.bind.JAXBException, IOException Creates a newPostobject 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 themastodonIdinternally 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 aPostobject 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 existingPostrecord 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 compliantStringrepresenting 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
Postobject in thisPostArchivefor the status creation date andmastodonIdspecified - 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 aPostobject 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- thePostfor which a reference should be added to thearchiveIdindex- Throws:
RuntimeException- if the index already contains a reference for thearchiveIdof thePostobject passed as a parameter- Since:
- 0.01.00
- See Also:
-
getMastodonId
Obtains amastodonIdStringvalue, 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 retrievePostobjects 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 concatenatedmastodonIdStringvalue 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 concatenatedmastodonIdStringvalue 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 aPostobject to the index maintained by this archive according tomastodonId, which is comprised of the address or hostname of the instance from which thePostwas 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- thePostfor which a reference should be added to themastodonIdindexmastodonId- themastodonIdthat should reference thePostobject specified in the index- Throws:
RuntimeException- if the index already contains a reference for themastodonIdof thePostobject passed as a parameter- Since:
- 0.01.00
- See Also:
-
getPostByArchiveId
Retrieves a reference to thePostobject with the specifiedarchiveId, based on the creation date and time of a Mastodon status, stored in thisPostArchive- Parameters:
id- a validarchiveId- Returns:
- a
Postobject with the correspondingarchiveId, ornullif no matching object is in the archive - Since:
- 0.01.00
- See Also:
-
getPostByMastodonId
Retrieves a reference to thePostobject in thisPostArchivewith the specifiedmastodonId, which is comprised of the address or hostname of the instance from which thePostwas saved or has been reposted, and the internal id of the corresponding status on that instance.- Parameters:
mid- a validmastodonId- Returns:
- a
Postobject that was saved from or has been reposted to a Mastodon instance matching the specifiedmastodonId, ornullif no matching object is in the archive - Since:
- 0.01.00
- See Also:
-
postIsBeforeDateTime
Determines whether a givenarchiveIdis 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 givenarchiveIdis 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.
-