Class PostArchive

java.lang.Object
io.github.mastodonContentMover.PostArchive

public class PostArchive extends Object
Manages a collection of 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 Type
    Method
    Description
    protected Post
    addPost(String createdAt, String mid)
    Creates a new Post object in this archive for a status with the specified creation date and time, and the specified mastodonId.
    protected Collection<Post>
    Obtains a Collection view of all Post objects held in this archive, from oldest to newest when iterated.
    protected String
    Obtain the filesystem directory name within which data for this PostArchive instance is stored.
    protected static String
    Retrieves the Mastodon instance hostname or address from a concatenated mastodonId String value that also contains a separator and the id used for a status on that instance.
    protected static String
    Retrieves the id used for a status on a Mastodon instance from a concatenated mastodonId String value that also contains the Mastodon instance hostname or address.
    protected static String
    getMastodonId(String instanceAddress, String id)
    Obtains a mastodonId 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 retrieve Post objects held in this archive and reconcile them with statuses on Mastodon instances.
    protected Post
    Retrieves a reference to the Post object with the specified archiveId, based on the creation date and time of a Mastodon status, stored in this PostArchive

    protected Post
    Retrieves a reference to the Post object in this PostArchive with the specified mastodonId, which is comprised of the address or hostname of the instance from which the Post was saved or has been reposted, and the internal id of the corresponding status on that instance.
    protected int
    Obtain the number of Post objects currently stored within this PostArchive instance.
    protected static PostArchive
    getSingletonInstance(String archiveName, boolean loadOK)
    Obtains a reference to the currently instantiated PostArchive 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 given archiveId is based on a date and time after a specified ISO 8601 compliant String.
    protected boolean
    postIsBeforeDateTime(String postId, String isoDateTime)
    Determines whether a given archiveId is based on a date and time before a specified ISO 8601 compliant String.
    protected void
    Adds a reference to a Post object to the index maintained by this archive according to MastodonContentMover internal archiveId.
    protected void
    registerPostByMastodonId(Post postObject, String mastodonId)
    Adds a reference to a Post object to the index maintained by this archive according to mastodonId, which is comprised of the address or hostname of the instance from which the Post was saved or has been reposted, and the internal id of the corresponding status on that instance.

    Methods inherited from class java.lang.Object

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

    • getSingletonInstance

      protected static PostArchive getSingletonInstance(String archiveName, boolean loadOK) throws jakarta.xml.bind.JAXBException, IOException
      Obtains a reference to the currently instantiated PostArchive 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 stored
      loadOK - true to load existing archive data from the filesystem, or false otherwise

      Returns:
      a reference to an instance of PostArchive for the specified archive name if no other PostArchive (for another archive name) was already instantiated

      Throws:
      IllegalStateException - if a PostArchive singleton is already instantiated for a different archive name
      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
    • getDirectory

      protected String getDirectory()
      Obtain the filesystem directory name within which data for this PostArchive instance is stored.

      Returns:
      a String containing the filesystem directory name within which data for this PostArchive instance is stored

      Since:
      0.01.00
    • getAllPosts

      protected Collection<Post> getAllPosts()
      Obtains a Collection view of all Post objects held in this archive, from oldest to newest when iterated.

      An iterator on this Collection gives the Post objects in ascending order of archiveId (with the underlying implementation being a TreeMap.

      Returns:
      a Collection view of all Post objects held in this archive, from oldest to newest when iterated

      Since:
      0.01.00
      See Also:
    • getPostCount

      protected int getPostCount()
      Obtain the number of Post objects currently stored within this PostArchive instance.

      Returns:
      the number of Post objects currently stored within this PostArchive instance

      Since:
      0.01.00
    • addPost

      protected Post addPost(String createdAt, String mid) throws jakarta.xml.bind.JAXBException, IOException
      Creates a new Post object in this archive for a status with the specified creation date and time, and the specified mastodonId.

      Suspect this should actually accept the instance hostname/address and instance id rather than the mastodonId, and then generate the mastodonId 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 a Post 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 existing Post 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 compliant String representing the date and time in UTC (with the "Z") when a status was created
      mid - a mastodonId, 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 this PostArchive for the status creation date and mastodonId 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

      protected void registerPostByArchiveId(Post postObject) throws RuntimeException
      Adds a reference to a Post object to the index maintained by this archive according to MastodonContentMover internal archiveId.

      TODO: Does this need to be separate to the method for mastodonId? Check and rationalise if needed. Exception should also be more specific.

      Parameters:
      postObject - the Post for which a reference should be added to the archiveId index

      Throws:
      RuntimeException - if the index already contains a reference for the archiveId of the Post object passed as a parameter

      Since:
      0.01.00
      See Also:
    • getMastodonId

      protected static String getMastodonId(String instanceAddress, String id)
      Obtains a mastodonId 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 retrieve Post objects held in this archive and reconcile them with statuses on Mastodon instances.

      Parameters:
      instanceAddress - the hostname or address of a Mastodon instance
      id - 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

      protected static String getInstanceAddressFromMastodonId(String mastodonId)
      Retrieves the Mastodon instance hostname or address from a concatenated mastodonId 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

      protected static String getInstanceIdFromMastodonId(String mastodonId)
      Retrieves the id used for a status on a Mastodon instance from a concatenated mastodonId 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

      protected void registerPostByMastodonId(Post postObject, String mastodonId) throws RuntimeException
      Adds a reference to a Post object to the index maintained by this archive according to mastodonId, which is comprised of the address or hostname of the instance from which the Post 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 for archiveId? Check and rationalise if needed. Exception should also be more specific.

      Parameters:
      postObject - the Post for which a reference should be added to the mastodonId index
      mastodonId - the mastodonId that should reference the Post object specified in the index

      Throws:
      RuntimeException - if the index already contains a reference for the mastodonId of the Post object passed as a parameter

      Since:
      0.01.00
      See Also:
    • getPostByArchiveId

      protected Post getPostByArchiveId(String id)
      Retrieves a reference to the Post object with the specified archiveId, based on the creation date and time of a Mastodon status, stored in this PostArchive

      Parameters:
      id - a valid archiveId

      Returns:
      a Post object with the corresponding archiveId, or null if no matching object is in the archive

      Since:
      0.01.00
      See Also:
    • getPostByMastodonId

      protected Post getPostByMastodonId(String mid)
      Retrieves a reference to the Post object in this PostArchive with the specified mastodonId, which is comprised of the address or hostname of the instance from which the Post was saved or has been reposted, and the internal id of the corresponding status on that instance.

      Parameters:
      mid - a valid mastodonId

      Returns:
      a Post object that was saved from or has been reposted to a Mastodon instance matching the specified mastodonId, or null if no matching object is in the archive

      Since:
      0.01.00
      See Also:
    • postIsBeforeDateTime

      protected boolean postIsBeforeDateTime(String postId, String isoDateTime)
      Determines whether a given archiveId is based on a date and time before a specified ISO 8601 compliant String.

      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.

      Parameters:
      postId - the archiveId to compare to the specified date and time
      isoDateTime - an ISO 8601 compliant String
      Returns:
      true if the given archiveId represents a date and time before the specified ISO 8601 compliant String, or false otherwise

      Since:
      0.01.00
      See Also:
    • postIsAfterDateTime

      protected boolean postIsAfterDateTime(String postId, String isoDateTime)
      Determines whether a given archiveId is based on a date and time after a specified ISO 8601 compliant String.

      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.

      Parameters:
      postId - the archiveId to compare to the specified date and time
      isoDateTime - an ISO 8601 compliant String
      Returns:
      true if the given archiveId represents a date and time after the specified ISO 8601 compliant String, or false otherwise

      Since:
      0.01.00
      See Also: