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 interfaceContains the association between thePathandResourceId.static interfaceAcquires aResource, guaranteeing it will stay in memory until this object is closed.static interfaceRepresents a transaction against a single instance of aResource.static interfaceThe return-value for methods such asunlinkPath(Path)andunlinkPath(Path, Consumer) -
Method Summary
Modifier and TypeMethodDescriptionacquire(ResourceId resourceId) Acquires aResourceguaranteeing it will remain in-memory until the resource is released.Acquires aResourceguaranteeing 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.voidaddAndReleaseResource(Path path, Resource resource) Adds aResourceto this resource service.default voiddestroy(ResourceId resourceId) Removes aResourceand then immediately closes it.default voidRemoves aResourceand then immediately closes it.default List<ResourceId> destroyResources(Path path, int max) booleanexists(ResourceId resourceId) Deprecated.not used anywherelongvoidlink(ResourceId sourceResourceId, Path destination) Given the providedResourceId, this will create an additional alias for the provided destinationPath.voidlistStream(Path path) Returns aStream<ResourceId>matching the providedPath.Removes all resources from.default voidRemoves all resources from the service and closes them.removeResource(ResourceId resourceId) Removes aResourceinstance from this resource service.default ResourceremoveResource(String resourceIdString) Removes aResourceinstance from this resource service.default List<ResourceId> removeResources(Path path, int max) removeResources(Path path, int max, Consumer<Resource> removed) default voidstart()Called on start-up to ensure that theResourceServicehas created and started any internal processes that it may need to perform its work.default voidstop()Releases all memory associated with thisResourceService.default List<ResourceService.Unlink> unlinkMultiple(Path path, int max) Unlinks multipleResources.unlinkMultiple(Path path, int max, Consumer<Resource> removed) Unlinks multipleResources.default ResourceService.UnlinkunlinkPath(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 theResourceServicehas 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 theResourcewith the suppliedResourceIdexists in thisResourceService.- Parameters:
resourceId- theResourceId- Returns:
- true if exists, false otherwise.
-
acquire
Acquires aResourceguaranteeing it will remain in-memory until the resource is released.- Returns:
- the
ResourceService.ResourceAcquisition
-
acquire
Acquires aResourceguaranteeing 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 aResourceto this resource service. This is used for the initial insert into theResourceService. If linking to an additionalPathis necessary, then the methodslinkPath(Path, Path)orlink(ResourceId, Path)must be used to perform additional aliasing operations. It is strongly recommended that newly insertedResourceinstances 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 particularPaths may collide. Once aResourceis passed ot this method, thisResourceServicewill take ownership of it. This means the theResourcemay be closed after this call. Therefore, subsequent operations may require theResourcebe fetched from serialization later for subsequent operations.- Parameters:
path- the initial path for theResourceresource- 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 additionalPathis necessary, then the methodslinkPath(Path, Path)orlink(ResourceId, Path)must be used to perform additional aliasing operations. It is strongly recommended that newly insertedResourceinstances 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 particularPaths may collide. This is only safe to use with freshly createdResourceinstances whoseResourceIdhas 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- thePathto 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 theResourcecan't be found
-
linkPath
-
unlinkPath
Similar tounlinkPath(Path, Consumer), however, this assumes that the final action should remove and destroy the associatedResource. -
unlinkPath
Unlinks theResourcefor the providedPath. If the unlinkedPathis the final path, then this will remove thePathfrom theResourceService. If thisPathis the last alias for the associatedResource, then the removedResourceIdwill be passed to the suppliedConsumer. The suppliedConsumerwill not be called if there still exist aliases for the associatedResource. Only oneResourcemay be unlinked at a time. Therefore, the supplied,Pathmost not be a wildcard path.- Parameters:
path- aPathto unlinkremoved- a Consumer which will receive the removedResource- Returns:
- true if the
Resourceassociated with thePathwas 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.Unlinkoperations
-
removeResource
Removes aResourceinstance 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 aResourceinstance 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- thePathfor the resource.max-
-
removeResources
-
destroy
Removes aResourceand then immediately closes it.- Parameters:
resourceId-- Throws:
ResourceNotFoundException- if no resource exists at that pathIllegalArgumentException- if the path is a wildcard path
-
destroy
Removes aResourceand then immediately closes it.- Parameters:
resourceIdString- theStringrepresentation 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 anyResources 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 wholeResourceServiceto 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()
-