public class FileTrsStore extends Object implements TrsStore
TrsStore
and ResourceEventStore
. This implementation is only for testing environments, not for use
in productive environments. The location of the storage is a directory in the local file system where TRS files can be stored.Constructor and Description |
---|
FileTrsStore(URI domain,
File location)
Constructs an instance specifying the directory of this storage in the local file system.
|
Modifier and Type | Method and Description |
---|---|
long |
addBaseMembers(long lastMemberId,
Function<Consumer<ResourceSeed>,?> membersProvider)
Adds members to the base.
|
Optional<Long> |
collectBaseMembers(Optional<Long> startingMemberId,
Optional<Integer> size,
Optional<Integer> resourceType,
Consumer<ResourceSeed> baseMembersConsumer)
Collects base members, either all of them or just a page.
|
void |
collectChangeEventSeeds(Optional<ChangeEventSeed> since,
Optional<ResourceEvent.Type> expectedEventType,
Consumer<ChangeEventSeed> changeEventsConsumer)
Collects (forwards) all existing
events in trs:ChangeLog since (optionally) a particular event. |
void |
corrupt()
Forces the corruption of this store.
|
long |
countChangeEventSeeds(Optional<ChangeEventSeed> since)
Counts how many
resource events exist in trs:ChangeLog since (optionally) a particular event. |
Executor |
createSynchronizer()
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 File |
getBaseFile()
Gets the reference of the trs:Base storage file.
|
protected File |
getChangeLogFile()
Gets the reference of the trs:changeLog storage file.
|
TrsServiceControl |
getControl()
Retrieves the current state of a TRS service control.
|
protected File |
getControlFile()
Gets the reference of the trs:control storage file.
|
boolean |
isCorrupted()
Determines whether this store is corrupted.
|
boolean |
isResourceExposed(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 of
events from the trs:ChangeLog . |
void |
pushChangeEventSeeds(Function<Consumer<ChangeEventSeed>,?> eventsProvider)
Stores the latest
events in the trs:ChangeLog . |
int |
removeBaseMembers(Function<Consumer<ResourceSeed>,?> membersProvider)
Removes members from the base.
|
int |
removeChangeEventSeedsBefore(Date date)
Removes
events in trs:ChangeLog prior or equal to a given date. |
void |
restore()
Restores the store's state to the starting stage.
|
void |
saveControl(TrsServiceControl control)
Saves the current state of a TRS service control.
|
void |
store(Function<Consumer<ResourceEvent>,?> eventsProvider)
Stores
resource events . |
void |
store(ResourceEvent... resourceEvents)
Stores one or a few
resource events . |
public FileTrsStore(URI domain, File location)
domain
- the underlying TRS domain URL this instance handles.location
- the directory of this storage.IllegalArgumentException
- if the directory does not exist and cannot be created or it exists but cannot be read or written.public Executor createSynchronizer()
TrsStore
createSynchronizer
in interface TrsStore
TrsStoreSynchronizer
public final boolean isCorrupted()
Store
isCorrupted
in interface Store
true
if this store is corrupted; false
otherwise.public final void corrupt()
Store
protected File getBaseFile()
trs-base
file reference.protected File getChangeLogFile()
trs-changeLog
file reference.protected File getControlFile()
trs-control
file reference.public void restore() throws TrsStoreException
TrsStore
restore
in interface TrsStore
TrsStoreException
- if the storage cannot be reestablished to its original state by any other reason.public TrsServiceControl getControl()
TrsStore
getControl
in interface TrsStore
public void saveControl(TrsServiceControl control) throws TrsStoreException
TrsStore
saveControl
in interface TrsStore
control
- the control to be saved.TrsStoreException
- if the control cannot be saved.public long addBaseMembers(long lastMemberId, Function<Consumer<ResourceSeed>,?> membersProvider) throws TrsStoreException
TrsStore
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.
addBaseMembers
in interface TrsStore
lastMemberId
- last stored member id.membersProvider
- a function that provides access to ResourceSeeds to add as base members.TrsStoreException
- if any other TRS store error occurs.BufferedConsumer
public int removeBaseMembers(Function<Consumer<ResourceSeed>,?> membersProvider) throws TrsStoreException
TrsStore
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.
removeBaseMembers
in interface TrsStore
membersProvider
- a function that provides access to URIs to add as base members.TrsStoreException
- if any other TRS store error occurs.BufferedConsumer
public Optional<Long> collectBaseMembers(Optional<Long> startingMemberId, Optional<Integer> size, Optional<Integer> resourceType, Consumer<ResourceSeed> baseMembersConsumer) throws TrsStoreException
TrsStore
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.collectBaseMembers
in interface TrsStore
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.baseMembersConsumer
- base members consumer.TrsStoreException
- if any other TRS store error occurs.public void pushChangeEventSeeds(Function<Consumer<ChangeEventSeed>,?> eventsProvider) throws TrsStoreException
TrsStore
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.
pushChangeEventSeeds
in interface TrsStore
eventsProvider
- a function that provides access to change events to become part of the trs:ChangeLog
.TrsStoreException
- if any other TRS store error occurs.BufferedConsumer
public Optional<Integer> pollChangeEventSeeds(int startingOrder, int count, Consumer<ChangeEventSeed> changeEventsConsumer) throws TrsStoreException
TrsStore
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.pollChangeEventSeeds
in interface TrsStore
startingOrder
- starting event order
inclusive.count
- number of events to poll.changeEventsConsumer
- changes events consumer.Optional
if no more events exists.TrsStoreException
- if any other TRS store error occurs.public void collectChangeEventSeeds(Optional<ChangeEventSeed> since, Optional<ResourceEvent.Type> expectedEventType, Consumer<ChangeEventSeed> changeEventsConsumer) throws TrsStoreException
TrsStore
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.collectChangeEventSeeds
in interface TrsStore
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.TrsStoreException
- if any other TRS store error occurs.public long countChangeEventSeeds(Optional<ChangeEventSeed> since) throws TrsStoreException
TrsStore
resource events
exist in trs:ChangeLog
since (optionally) a particular event.countChangeEventSeeds
in interface TrsStore
since
- starting point to count (exclusive). If present, it is typically the last trs:Base cutoff event
.trs:ChangeLog
since changeEvent
.TrsStoreException
- if any other TRS store error occurs.public int removeChangeEventSeedsBefore(Date date) throws TrsStoreException
TrsStore
events
in trs:ChangeLog
prior or equal to a given date.
Implementations should compare only the date part, not the time.removeChangeEventSeedsBefore
in interface TrsStore
date
- point of reference (inclusive) to remove events.TrsStoreException
- if any other TRS store error occurs.public void store(ResourceEvent... resourceEvents)
ResourceEventStore
resource events
.
If a larger volume of events is to send at once, client is recommended to rather use the ResourceEventStore.store(Function)
method.
Implementations must not store events if base is not computed
and must assign properly the
event order
before saving (resourceEvents
must be provided chronologically sorted as they
occurred).
This method is intended to avoid raising TrsStoreException
, if something went wrong preventing the storage of the event,
implementations must corrupt
the storage silently.
store
in interface ResourceEventStore
resourceEvents
- events to store.public void store(Function<Consumer<ResourceEvent>,?> eventsProvider)
ResourceEventStore
resource events
.
If only one or a few events are to report at once, client may use the simpler ResourceEventStore.store(ResourceEvent...)
method.
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.
Implementations must not store events if base is not computed
and must assign properly the
event order
before saving (resourceEvents
must be provided chronologically sorted as they
occurred).
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.
store
in interface ResourceEventStore
eventsProvider
- a function that provides access to change events to become part of the trs:ChangeLog
.BufferedConsumer
,
ResourceEventStore.store(ResourceEvent...)
public boolean isResourceExposed(ResourceSeed seed) throws TrsStoreException
TrsStore
isResourceExposed
in interface TrsStore
seed
- the resource seed.true
if resource is exposed, false otherwise
.TrsStoreException
- if a TRS store error occurs.