Class FileTrsStore

  • All Implemented Interfaces:
    ResourceEventStore, Store, TrsStore

    public class FileTrsStore
    extends AbstractTrsStore
    A file based implementation of a TrsStore and ResourceEventStore. The location of the storage is a directory in the local file system where TRS files can be stored.

    CAUTION: This implementation is for testing environments only, not to use in production environments. It is notably not compatible with clusters and has limited performances.

    Since:
    1.10.0
    • Constructor Detail

      • FileTrsStore

        public FileTrsStore​(URI domain,
                            File location)
        Constructs an instance specifying the directory of this storage in the local file system.
        Parameters:
        domain - the underlying TRS domain URL this instance handles.
        location - the directory of this storage.
        Throws:
        IllegalArgumentException - if the directory does not exist and cannot be created or it exists but cannot be read or written.
    • Method Detail

      • createSynchronizer

        public Executor createSynchronizer()
        Description copied from interface: TrsStore
        Creates an executor that guarantees runnable instances handling data related to this store are never executed concurrently but one at a time across the whole web application. If the web application is deployed in a cluster, the executor must ensure only one cluster node can execute at a time the runnable instances.

        Subclasses may choose to use LockExecutors to obtain an executor.

        Returns:
        an executor that guarantees runnable instances are executed one at a time.
        See Also:
        TrsStoreSynchronizer, LockExecutors
      • isCorrupted

        public final boolean isCorrupted()
        Description copied from interface: Store
        Determines whether this store is corrupted.
        Returns:
        true if this store is corrupted; false otherwise.
      • corrupt

        public final void corrupt()
        Description copied from interface: Store
        Forces the corruption of this store. This method may be called if something serious happened on the system and the store is therefore known to be unreliable.
      • getBaseFile

        protected File getBaseFile()
        Gets the reference of the trs:Base storage file.
        Returns:
        the trs-base file reference.
      • getChangeLogFile

        protected File getChangeLogFile()
        Gets the reference of the trs:changeLog storage file.
        Returns:
        the trs-changeLog file reference.
      • getControlFile

        protected File getControlFile()
        Gets the reference of the trs:control storage file.
        Returns:
        the trs-control file reference.
      • getResourceEventsFile

        protected File getResourceEventsFile()
        Gets the reference of the trs:resourceEvents storage file.
        Returns:
        the trs-resourceEvents file reference.
      • restore

        public void restore()
                     throws TrsStoreException
        Description copied from interface: TrsStore
        Restores the store's state to the starting stage.
        Throws:
        TrsStoreException - if the storage cannot be reestablished to its original state by any other reason.
      • getControl

        public TrsServiceControl getControl()
        Description copied from interface: TrsStore
        Retrieves the current state of a TRS service control. This method should always return an instance even if an internal implementation-specific exception occurs, in such case, a default instance should be returned to restart over the TRS feed.
        Returns:
        the TRS service control.
      • saveControl

        public void saveControl​(TrsServiceControl control)
                         throws TrsStoreException
        Description copied from interface: TrsStore
        Saves the current state of a TRS service control.
        Parameters:
        control - the control to be saved.
        Throws:
        TrsStoreException - if the control cannot be saved.
      • addBaseMembers

        public long addBaseMembers​(long lastMemberId,
                                   Function<Consumer<ResourceSeed>,​?> membersProvider)
                            throws TrsStoreException
        Description copied from interface: TrsStore
        Adds members to the base. To support a consistent pagination, this method is expected to keep an order between this member and existing members, such order must be reflected by the returned id: first appended members must have lesser members ids than those appended last.

        The function argument provides access to ResourceSeeds to be added in this store as base members. Implementation must call the apply() method of this function and use as argument a Consumer to get notified of members. Implementation is advised to use a BufferedConsumer for optimal performance.

        Parameters:
        lastMemberId - last stored member id.
        membersProvider - a function that provides access to ResourceSeeds to add as base members.
        Returns:
        last stored member id (relative to its position).
        Throws:
        TrsStoreException - if any other TRS store error occurs.
        See Also:
        BufferedConsumer
      • removeBaseMembers

        public int removeBaseMembers​(Function<Consumer<ResourceSeed>,​?> membersProvider)
                              throws TrsStoreException
        Description copied from interface: TrsStore
        Removes members from the base.

        The function argument provides access to ResourceSeeds to be removed as base members from this store. Implementation must call the apply() method of this function and use as argument a Consumer to get notified of members. Implementation is advised to use a BufferedConsumer for optimal performance.

        Parameters:
        membersProvider - a function that provides access to URIs to add as base members.
        Returns:
        number of members removed.
        Throws:
        TrsStoreException - if any other TRS store error occurs.
        See Also:
        BufferedConsumer
      • collectBaseMembers

        public Optional<Long> collectBaseMembers​(Optional<Long> startingMemberId,
                                                 Optional<Integer> size,
                                                 Optional<Integer> resourceType,
                                                 Optional<String> resourceProject,
                                                 Consumer<ResourceSeed> baseMembersConsumer)
                                          throws TrsStoreException
        Description copied from interface: TrsStore
        Collects base members, either all of them or just a page. Consumers may raise CancellationException to indicate the collecting process must stop, but implementations must no propagate this exception, this is a convenient way to stop a searching process.
        Parameters:
        startingMemberId - optional, the initial member (id) on the page. If not present, starts from first page (member).
        size - optional, number of members to populate including startingMemberId if present (used for pagination).
        resourceType - optional, only members of this type will be collected.
        resourceProject - optional, only members of this project area will be collected.
        baseMembersConsumer - base members consumer.
        Returns:
        if present, the id of the next member in the next page, otherwise it means no more pages exist (or pagination was not used).
        Throws:
        TrsStoreException - if any other TRS store error occurs.
      • pushChangeEventSeeds

        public void pushChangeEventSeeds​(Function<Consumer<ChangeEventSeed>,​?> eventsProvider)
                                  throws TrsStoreException
        Description copied from interface: TrsStore
        Stores the latest events in the trs:ChangeLog.

        The function argument provides access to change events to be added in this store. Implementation must call the apply() method of this function and use as argument a Consumer to get notified of events. Implementation is advised to use a BufferedConsumer for optimal performance.

        Parameters:
        eventsProvider - a function that provides access to change events to become part of the trs:ChangeLog.
        Throws:
        TrsStoreException - if any other TRS store error occurs.
        See Also:
        BufferedConsumer
      • pollChangeEventSeeds

        public Optional<Integer> pollChangeEventSeeds​(int startingOrder,
                                                      int count,
                                                      Consumer<ChangeEventSeed> changeEventsConsumer)
                                               throws TrsStoreException
        Description copied from interface: TrsStore
        Collects (backwards) a given number of events from the trs:ChangeLog. This method is not forced to resolve all requested events, it must just provide as many as it has from requested and from startingOrder. Implementations must also consider that a count of zero may be requested. In this case, no events must be added to target, but the existence of a further event must be resolved in order to determine whether or not a next page exists. This latter must be reflected in the return value. Consumers may raise CancellationException to indicate the collecting process must stop, but implementations must no propagate this exception, this is a convenient way to stop a searching process.
        Parameters:
        startingOrder - starting event order inclusive.
        count - number of events to poll.
        changeEventsConsumer - changes events consumer.
        Returns:
        event order of previous event (not included in the extraction) if present; empty Optional if no more events exists.
        Throws:
        TrsStoreException - if any other TRS store error occurs.
      • collectChangeEventSeeds

        public void collectChangeEventSeeds​(Optional<ChangeEventSeed> since,
                                            Optional<ResourceEvent.Type> expectedEventType,
                                            Consumer<ChangeEventSeed> changeEventsConsumer)
                                     throws TrsStoreException
        Description copied from interface: TrsStore
        Collects (forwards) all existing events in trs:ChangeLog since (optionally) a particular event. Consumers may raise CancellationException to indicate the collecting process must stop, but implementations must no propagate this exception, this is a convenient way to stop a searching process.
        Parameters:
        since - starting event (exclusive) to be consumed. If present, typically the last trs:Base cutoff event.
        expectedEventType - the type of event to collect; all types of events if Optional.empty().
        changeEventsConsumer - changes events consumer.
        Throws:
        TrsStoreException - if any other TRS store error occurs.
      • store

        protected void store​(Function<Consumer<ResourceEvent>,​?> eventsProvider,
                             int bufferSize,
                             boolean assertStoring)
        Description copied from class: AbstractTrsStore
        Stores the provided events using a BufferedConsumer of the given size and indicating whether to call the AbstractTrsStore.isStoringEventsOf(Optional) method or not.

        The function implementation must be thread-safe, as the store will call the Function.apply(Object) method in a dedicated thread, so that calls to this method are non blocking.

        This method is intended to avoid raising TrsStoreException, if something went wrong preventing the storage of the event, implementations must corrupt the storage silently.

        The function argument provides access to change events to be added in this store. Implementation must call the apply() method of this function and use as argument a Consumer to get notified of events. Implementation is advised to use a BufferedConsumer for optimal performance.

        Specified by:
        store in class AbstractTrsStore
        Parameters:
        eventsProvider - the events provider.
        bufferSize - the size for the BufferedConsumer.
        assertStoring - whether or not to call the AbstractTrsStore.isStoringEventsOf(Optional) method for each event seed.
      • removeChangeEventSeedsBefore

        public int removeChangeEventSeedsBefore​(int order)
                                         throws TrsStoreException
        Description copied from interface: TrsStore
        Removes events in trs:ChangeLog prior or equal to a given order.
        Parameters:
        order - event order of reference (inclusive) to remove events.
        Returns:
        number of deleted events.
        Throws:
        TrsStoreException - if any other TRS store error occurs.
      • getFirstChangeEventSince

        public Optional<ChangeEventSeed> getFirstChangeEventSince​(Date date)
                                                           throws TrsStoreException
        Description copied from interface: TrsStore
        Determines the oldest change event on or after a given a date. Implementations should compare only the date part, not the time.
        Parameters:
        date - point of reference to find the event
        Returns:
        an empty Optional if no event exists; the contained reference otherwise.
        Throws:
        TrsStoreException - if any other TRS store error occurs.
      • isResourceExposed

        public boolean isResourceExposed​(ResourceSeed seed)
                                  throws TrsStoreException
        Description copied from interface: TrsStore
        Determines whether a resource is exposed or not. An exposed resource is one that is part of the TRS feed, this means there has not been a DELETION event for it and it is either part of the trs:base or there is a CREATION event in the trs:changeLog.
        Parameters:
        seed - the resource seed.
        Returns:
        true if resource is exposed, false otherwise.
        Throws:
        TrsStoreException - if a TRS store error occurs.
      • synchronizeResourceEvents

        public int synchronizeResourceEvents​(int lastEventOrder)
                                      throws TrsStoreException
        Description copied from interface: TrsStore
        Synchronizes all the stored events (since the last synchronization) so all of them get their right order and become visible to the TRS feed.
        Parameters:
        lastEventOrder - the last stored order.
        Returns:
        the new last event order to be saved in the TrsServiceControl class.
        Throws:
        TrsStoreException - if the synchronization fails.