Class FileTrsStore
- java.lang.Object
-
- com.sodius.oslc.server.trs.store.AbstractTrsStore
-
- com.sodius.oslc.server.trs.store.FileTrsStore
-
- All Implemented Interfaces:
ResourceEventStore,Store,TrsStore
public class FileTrsStore extends AbstractTrsStore
A file based implementation of aTrsStoreandResourceEventStore. 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
-
-
Field Summary
-
Fields inherited from class com.sodius.oslc.server.trs.store.AbstractTrsStore
BASE_BUFFER_SIZE, EVENTS_BUFFER_SIZE
-
-
Constructor Summary
Constructors Constructor Description FileTrsStore(URI domain, File location)Constructs an instance specifying the directory of this storage in the local file system.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBaseMembers(Function<Consumer<ResourceSeed>,?> membersProvider)Adds members to the base.Optional<Long>collectBaseMembers(Optional<Long> startingMemberId, Optional<Integer> size, Optional<Integer> resourceType, Optional<String> resourceProject, Consumer<ResourceSeed> baseMembersConsumer)Collects base members, either all of them or just a page.voidcollectChangeEventSeeds(Optional<ChangeEventSeed> since, Optional<ResourceEvent.Type> expectedEventType, Consumer<ChangeEventSeed> changeEventsConsumer)Collects (forwards) all existingeventsintrs:ChangeLogsince (optionally) a particular event.voidcorrupt()Forces the corruption of this store.longcountChangeEventSeeds(Optional<ChangeEventSeed> since)Counts how manyresource eventsexist intrs:ChangeLogsince (optionally) a particular event.ExecutorcreateSynchronizer()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.protected FilegetBaseFile()Gets the reference of the trs:Base storage file.protected FilegetChangeLogFile()Gets the reference of the trs:changeLog storage file.TrsServiceControlgetControl()Retrieves the current state of a TRS service control.protected FilegetControlFile()Gets the reference of the trs:control storage file.Optional<ChangeEventSeed>getFirstChangeEventSince(Date date)Determines the oldest change event on or after a given a date.protected FilegetResourceEventsFile()Gets the reference of the trs:resourceEvents storage file.protected FilegetStagingEventsFile(String id)Gets the reference of the storage file for staging events of given identifier.StagingResourceEventStoregetStagingResourceEventStore(String id)Gets astaging resource event storefor a given identifier.booleanisCorrupted()Determines whether this store is corrupted.booleanisResourceExposed(ResourceSeed seed)Determines whether a resource is exposed or not.Optional<Integer>pollChangeEventSeeds(int startingOrder, int count, Consumer<ChangeEventSeed> changeEventsConsumer)Collects (backwards) a given number ofeventsfrom thetrs:ChangeLog.voidpushResourceEvents(Function<Consumer<ResourceEvent>,?> eventsProvider)Stores the latesteventsin thetrs:ChangeLog.voidrebase(Function<Consumer<ChangeEventSeed>,?> changesProvider)Refreshes members in base, to add or remove members given some events in the change log.intremoveChangeEventSeedsBefore(int order)Removeseventsintrs:ChangeLogprior or equal to a given order.voidrestore()Restores the store's state to the starting stage.voidsaveControl(TrsServiceControl control)Saves the current state of a TRS service control.voidstore(Function<Consumer<ResourceEvent>,?> eventsProvider)Storesresource events.voidsynchronizeResourceEvents()Synchronizes all the stored events (since the last synchronization) so all of them get their rightorderand become visible to the TRS feed.-
Methods inherited from class com.sodius.oslc.server.trs.store.AbstractTrsStore
assertNoOrderOverflow, getDomain, isStoringEventsOf, store
-
-
-
-
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:TrsStoreCreates 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
LockExecutorsto 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:StoreDetermines whether this store is corrupted.- Returns:
trueif this store is corrupted;falseotherwise.
-
corrupt
public final void corrupt()
Description copied from interface:StoreForces 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-basefile reference.
-
getChangeLogFile
protected File getChangeLogFile()
Gets the reference of the trs:changeLog storage file.- Returns:
- the
trs-changeLogfile reference.
-
getControlFile
protected File getControlFile()
Gets the reference of the trs:control storage file.- Returns:
- the
trs-controlfile reference.
-
getResourceEventsFile
protected File getResourceEventsFile()
Gets the reference of the trs:resourceEvents storage file.- Returns:
- the
trs-resourceEventsfile reference.
-
getStagingEventsFile
protected File getStagingEventsFile(String id)
Gets the reference of the storage file for staging events of given identifier.- Parameters:
id- the staging store identifier- Returns:
- the file reference.
- Since:
- 3.11.0
-
restore
public void restore() throws TrsStoreExceptionDescription copied from interface:TrsStoreRestores 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:TrsStoreRetrieves 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:TrsStoreSaves 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 void addBaseMembers(Function<Consumer<ResourceSeed>,?> membersProvider) throws TrsStoreException
Description copied from interface:TrsStoreAdds members to the base.To support a consistent pagination, this method is expected to keep an order between members to add and existing members. First appended members must have lesser members ids than those appended last. When assigning ids to members to add, implementation shall rely on
TrsServiceControl.getLastBaseMemberId()as starting point. Once members are added, implementation must callTrsServiceControl.setLastBaseMemberId(long)to keep track of the last identifier used.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 aConsumerto get notified of members.Implementation is advised to use a
BufferedConsumerfor optimal performance. Implementation is also advised to use a transaction per collection of members to add.- Parameters:
membersProvider- a function that provides access to ResourceSeeds to add as base members.- Throws:
TrsStoreException- if any other TRS store error occurs.- See Also:
BufferedConsumer
-
rebase
public void rebase(Function<Consumer<ChangeEventSeed>,?> changesProvider) throws TrsStoreException
Description copied from interface:TrsStoreRefreshes members in base, to add or remove members given some events in the change log.Implementation must call the
apply()method of this function and use as argument aConsumerto get notified of change events. Implementation must apply the following changes in base members:- Add as base member each resource whose
ResourceEvent.getType()isCREATED - Remove from base members each resource whose
ResourceEvent.getType()isDELETED
TrsServiceControl.setBaseCutoffEvent(ChangeEventSeed), with the last event processed within the transaction as argument, andTrsServiceControl.setBaseUpdate(Date), with current time as argument. If base members were added, implementation must also callTrsServiceControl.setLastBaseMemberId(long).Implementation is advised to use a
BufferedConsumerfor optimal performance. Implementation is also advised to use a transaction per collection of events.- Parameters:
changesProvider- a function that provides access to change events that should impact base members.- Throws:
TrsStoreException- if any other TRS store error occurs.
- Add as base member each resource whose
-
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:TrsStoreCollects base members, either all of them or just a page. Consumers may raiseCancellationExceptionto 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 includingstartingMemberIdif 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.
-
pushResourceEvents
public void pushResourceEvents(Function<Consumer<ResourceEvent>,?> eventsProvider) throws TrsStoreException
Description copied from interface:TrsStoreStores the latesteventsin thetrs:ChangeLog.The function argument provides access to resource events to be added in this store. Implementation must call the
apply()method of this function and use as argument aConsumerto get notified of events.For each provided resource event, implementation must assign an event order when creating and storing the corresponding change event. Implementation shall rely on
TrsServiceControl.getLastEventOrder()as starting point. Once events are added, implementation must callTrsServiceControl.setLastEventOrder(int)to keep track of the last event order used.Implementation is advised to use a
BufferedConsumerfor optimal performance. Implementation is also advised to use a transaction per collection of events to add.- Parameters:
eventsProvider- a function that provides access to resource events to become part of thetrs: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:TrsStoreCollects (backwards) a given number ofeventsfrom thetrs:ChangeLog. This method is not forced to resolve all requested events, it must just provide as many as it has from requested and fromstartingOrder. Implementations must also consider that acountof zero may be requested. In this case, no events must be added totarget, 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 raiseCancellationExceptionto 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- startingevent orderinclusive.count- number of events to poll.changeEventsConsumer- changes events consumer.- Returns:
- event order of previous event (not included in the extraction) if present; empty
Optionalif 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:TrsStoreCollects (forwards) all existingeventsintrs:ChangeLogsince (optionally) a particular event. Consumers may raiseCancellationExceptionto 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 lasttrs:Base cutoff event.expectedEventType- the type of event to collect; all types of events ifOptional.empty().changeEventsConsumer- changes events consumer.- Throws:
TrsStoreException- if any other TRS store error occurs.
-
countChangeEventSeeds
public long countChangeEventSeeds(Optional<ChangeEventSeed> since) throws TrsStoreException
Description copied from interface:TrsStoreCounts how manyresource eventsexist intrs:ChangeLogsince (optionally) a particular event.- Parameters:
since- starting point to count (exclusive). If present, it is typically the lasttrs:Base cutoff event.- Returns:
- number of events in
trs:ChangeLogsincechangeEvent. - Throws:
TrsStoreException- if any other TRS store error occurs.
-
store
public void store(Function<Consumer<ResourceEvent>,?> eventsProvider)
Description copied from interface:ResourceEventStoreStoresresource events. If only one or a few events are to report at once, client may use the simplerResourceEventStore.store(ResourceEvent...)method.Implementations MUST store events, without blocking the application, only if the
ResourceEventStore.isStoringEventsOf(Optional)method returnstrue, for each of the givenevent seeds, and must keep them hidden from the TRS feed up to theTrsStore.synchronizeResourceEvents()method is called.This method is intended to avoid raising
TrsStoreException, if something went wrong preventing the storage of the event, implementations must handle it 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, aConsumerto get notified of events. Implementation is advised to use aBufferedConsumerfor optimal performance.- Parameters:
eventsProvider- a function that provides access to change events to become part of thetrs:ChangeLog.- See Also:
BufferedConsumer,ResourceEventStore.store(ResourceEvent...)
-
removeChangeEventSeedsBefore
public int removeChangeEventSeedsBefore(int order) throws TrsStoreExceptionDescription copied from interface:TrsStoreRemoveseventsintrs:ChangeLogprior 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:TrsStoreDetermines 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
Optionalif 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:TrsStoreDetermines 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.Implementation may return
falseif the store iscorrupted. However it must not check whether base wasupdated, as testing resource exposure is used to recover from a base partially initialized.- Parameters:
seed- the resource seed.- Returns:
trueif resource is exposed,false otherwise.- Throws:
TrsStoreException- if a TRS store error occurs.
-
synchronizeResourceEvents
public void synchronizeResourceEvents() throws TrsStoreExceptionDescription copied from interface:TrsStoreSynchronizes all the stored events (since the last synchronization) so all of them get their rightorderand become visible to the TRS feed.When assigning orders to events, implementation shall rely on
TrsServiceControl.getLastEventOrder()as starting point. Once event are synchronized, implementation must callTrsServiceControl.setLastEventOrder(int)to keep track of the last event order used.- Throws:
TrsStoreException- if the synchronization fails.
-
getStagingResourceEventStore
public StagingResourceEventStore getStagingResourceEventStore(String id) throws TrsStoreException
Description copied from interface:TrsStoreGets astaging resource event storefor a given identifier.- Parameters:
id- the given identifier.- Returns:
- the corresponding staging resource event store.
- Throws:
TrsStoreException- if the store cannot be retrieved.
-
-