Package dev.getelements.elements.rt
Interface ResourceService
- All Known Implementing Classes:
TransactionalResourceService
public interface ResourceService
This is the service responsible for maintaining a set of
Resource
instances. This contains code to handle a
the path hierarchy for the resources housed in the service.
Resources can be added, moved, or deleted as needed by external services.
Note that implementations of this interface should be considered thread safe. Each call must return or throw
exceptions leaving the object in a consistent state. This may be accomplished using locking. All operations are to
be considered atomic unless otherwise specified. Keep in mind that that while this instance may provided thread
safety, the specification for Resource
is not. Therefore, locking is necessary when performing operations
on individual Resource
instances themselves. This means that the internals of this service may lock the
individual Resource
instances to perform work as well.
Created by patricktwohig on 7/24/15.-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interface
Contains the association between thePath
andResourceId
.static interface
Acquires aResource
, guaranteeing it will stay in memory until this object is closed.static interface
Represents a transaction against a single instance of aResource
.static interface
The return-value for methods such asunlinkPath(Path)
andunlinkPath(Path, Consumer)
-
Method Summary
Modifier and TypeMethodDescriptionacquire
(ResourceId resourceId) Acquires aResource
guaranteeing it will remain in-memory until the resource is released.Acquires aResource
guaranteeing it will remain in-memory until the resource is released.acquireWithTransaction
(ResourceId resourceId) Opens aResourceService.ResourceTransaction
.acquireWithTransaction
(Path path) Opens aResourceService.ResourceTransaction
.addAndAcquireResource
(Path path, Resource resource) Adds an acquires thisResource
.void
addAndReleaseResource
(Path path, Resource resource) Adds aResource
to this resource service.default void
destroy
(ResourceId resourceId) Removes aResource
and then immediately closes it.default void
Removes aResource
and then immediately closes it.default List
<ResourceId> destroyResources
(Path path, int max) boolean
exists
(ResourceId resourceId) Deprecated.not used anywherelong
void
link
(ResourceId sourceResourceId, Path destination) Given the providedResourceId
, this will create an additional alias for the provided destinationPath
.void
listStream
(Path path) Returns aStream<ResourceId>
matching the providedPath
.Removes all resources from.default void
Removes all resources from the service and closes them.removeResource
(ResourceId resourceId) Removes aResource
instance from this resource service.default Resource
removeResource
(String resourceIdString) Removes aResource
instance from this resource service.default List
<ResourceId> removeResources
(Path path, int max) removeResources
(Path path, int max, Consumer<Resource> removed) default void
start()
Called on start-up to ensure that theResourceService
has created and started any internal processes that it may need to perform its work.default void
stop()
Releases all memory associated with thisResourceService
.default List
<ResourceService.Unlink> unlinkMultiple
(Path path, int max) Unlinks multipleResource
s.unlinkMultiple
(Path path, int max, Consumer<Resource> removed) Unlinks multipleResource
s.default ResourceService.Unlink
unlinkPath
(Path path) Similar tounlinkPath(Path, Consumer)
, however, this assumes that the final action should remove and destroy the associatedResource
.unlinkPath
(Path path, Consumer<Resource> removed)
-
Method Details
-
start
default void start()Called on start-up to ensure that theResourceService
has created and started any internal processes that it may need to perform its work. -
stop
default void stop()Releases all memory associated with thisResourceService
. The actual action that happens here is dependent on the specific implementation. For in-memory implementations, this will simply close all resources and exit. For persistence-backed implementations this should flush all resources to disk before closing all. Once closed this instance may only be used after it has been restarted. -
exists
Deprecated.not used anywhereWithout affecting acquisition or releases, this performs a simple check to see if theResource
with the suppliedResourceId
exists in thisResourceService
.- Parameters:
resourceId
- theResourceId
- Returns:
- true if exists, false otherwise.
-
acquire
Acquires aResource
guaranteeing it will remain in-memory until the resource is released.- Returns:
- the
ResourceService.ResourceAcquisition
-
acquire
Acquires aResource
guaranteeing it will remain in-memory until the resource is released.- Returns:
- the
ResourceService.ResourceAcquisition
-
acquireWithTransaction
Opens aResourceService.ResourceTransaction
.- Returns:
- the
ResourceService.ResourceTransaction
-
acquireWithTransaction
Opens aResourceService.ResourceTransaction
.- Returns:
- the
ResourceService.ResourceTransaction
-
addAndReleaseResource
Adds aResource
to this resource service. This is used for the initial insert into theResourceService
. If linking to an additionalPath
is necessary, then the methodslinkPath(Path, Path)
orlink(ResourceId, Path)
must be used to perform additional aliasing operations. It is strongly recommended that newly insertedResource
instances be given a globally unique path initially, which can be thought of as the primary path, and then subsequent aliases or links be maintained, even if those particularPath
s may collide. Once aResource
is passed ot this method, thisResourceService
will take ownership of it. This means the theResource
may be closed after this call. Therefore, subsequent operations may require theResource
be fetched from serialization later for subsequent operations.- Parameters:
path
- the initial path for theResource
resource
- the resource to insert- Throws:
DuplicateException
- if a resource is already presentIllegalArgumentException
- if the path is a wildcard path
-
addAndAcquireResource
Adds an acquires thisResource
. This is used for the initial insert into theResourceService
. If linking to an additionalPath
is necessary, then the methodslinkPath(Path, Path)
orlink(ResourceId, Path)
must be used to perform additional aliasing operations. It is strongly recommended that newly insertedResource
instances be given a globally unique path initially, which can be thought of as the primary path, and then subsequent aliases or links be maintained, even if those particularPath
s may collide. This is only safe to use with freshly createdResource
instances whoseResourceId
has never before been seen by the system.- Parameters:
path
-resource
-- Returns:
- the managed version of the supplied
Resource
-
listStream
Returns aStream<ResourceId>
matching the providedPath
.- Parameters:
path
- thePath
to match- Returns:
- a
Stream<ResourceId>
-
link
Given the providedResourceId
, this will create an additional alias for the provided destinationPath
.- Parameters:
sourceResourceId
-destination
-- Throws:
DuplicateException
- if an alias already exists for the destinationResourceNotFoundException
- if theResource
can't be found
-
linkPath
-
unlinkPath
Similar tounlinkPath(Path, Consumer)
, however, this assumes that the final action should remove and destroy the associatedResource
. -
unlinkPath
Unlinks theResource
for the providedPath
. If the unlinkedPath
is the final path, then this will remove thePath
from theResourceService
. If thisPath
is the last alias for the associatedResource
, then the removedResourceId
will be passed to the suppliedConsumer
. The suppliedConsumer
will not be called if there still exist aliases for the associatedResource
. Only oneResource
may be unlinked at a time. Therefore, the supplied,Path
most not be a wildcard path.- Parameters:
path
- aPath
to unlinkremoved
- a Consumer which will receive the removedResource
- Returns:
- true if the
Resource
associated with thePath
was removed, false otherwise - Throws:
IllegalArgumentException
- if the path is a wildcard path
-
unlinkMultiple
- Parameters:
path
-max
-- Returns:
-
unlinkMultiple
- Parameters:
path
- the path to removemax
- the maximum count to removeremoved
- aConsumer<Resource>
to process each removal- Returns:
- the final
ResourceService.Unlink
operations
-
removeResource
Removes aResource
instance from this resource service.- Parameters:
resourceId
- the resourceId to the resource- Throws:
ResourceNotFoundException
- if no resource exists at that pathIllegalArgumentException
- if the path is a wildcard path
-
removeResource
Removes aResource
instance from this resource service.- Parameters:
resourceIdString
- the path as a string- Returns:
- the removed
Resource
- Throws:
IllegalArgumentException
- if the path is a wildcard path
-
removeResources
- Parameters:
path
- thePath
for the resource.max
-
-
removeResources
-
destroy
Removes aResource
and then immediately closes it.- Parameters:
resourceId
-- Throws:
ResourceNotFoundException
- if no resource exists at that pathIllegalArgumentException
- if the path is a wildcard path
-
destroy
Removes aResource
and then immediately closes it.- Parameters:
resourceIdString
- theString
representation of aResourceId
- Throws:
ResourceNotFoundException
- if no resource exists at that pathIllegalArgumentException
- if the path is a wildcard path
-
destroyResources
- Parameters:
path
- thePath
-
removeAllResources
Removes all resources from. The returnedStream<Resource>
returns anyResource
s that are still occupying memory and must be closed. The returned stream may be empty if all have been persisted. This operation may lock the wholeResourceService
to accomplish its task. -
removeAndCloseAllResources
default void removeAndCloseAllResources()Removes all resources from the service and closes them. Any exceptions encountered are logged and all resources are attempted to be closed. -
getInMemoryResourceCount
long getInMemoryResourceCount()
-