Class ReferenceManager<G>
Utility class to safely share instances of a certain type across multiple threads, while periodically refreshing them. This class ensures each reference is closed only once all threads have finished using it. It is recommended to consult the documentation of ReferenceManager<G> implementations for their MaybeRefresh() semantics.
Note
This API is experimental and might change in incompatible ways in the next release.
Implements
Inherited Members
Namespace: Lucene.Net.Search
Assembly: Lucene.Net.dll
Syntax
public abstract class ReferenceManager<G> : IDisposable where G : class
Type Parameters
Name | Description |
---|---|
G | The concrete type that will be Acquire()d and Release(G)d. |
Properties
Current
The current reference
Declaration
protected G Current { get; set; }
Property Value
Type | Description |
---|---|
G |
Methods
Acquire()
Obtain the current reference. You must match every call to acquire with one
call to Release(G); it's best to do so in a finally clause, and set
the reference to null
to prevent accidental usage after it has been
released.
Declaration
public G Acquire()
Returns
Type | Description |
---|---|
G |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | If the reference manager has been Dispose()d. |
AddListener(IRefreshListener)
Adds a listener, to be notified when a reference is refreshed/swapped.
Declaration
public virtual void AddListener(ReferenceManager.IRefreshListener listener)
Parameters
Type | Name | Description |
---|---|---|
ReferenceManager.IRefreshListener | listener |
AfterMaybeRefresh()
Called after a refresh was attempted, regardless of whether a new reference was in fact created.
Declaration
protected virtual void AfterMaybeRefresh()
Exceptions
Type | Condition |
---|---|
IOException | if a low level I/O exception occurs |
DecRef(G)
Decrement reference counting on the given reference.
Declaration
protected abstract void DecRef(G reference)
Parameters
Type | Name | Description |
---|---|---|
G | reference |
Exceptions
Type | Condition |
---|---|
IOException | If reference decrement on the given resource failed. |
Dispose()
Closes this ReferenceManager to prevent future Acquire()ing. A reference manager should be disposed if the reference to the managed resource should be disposed or the application using the ReferenceManager<G> is shutting down. The managed resource might not be released immediately, if the ReferenceManager<G> user is holding on to a previously Acquire()d reference. The resource will be released once when the last reference is Release(G)d. Those references can still be used as if the manager was still active.
Applications should not Acquire() new references from this manager once this method has been called. Acquire()ing a resource on a disposed ReferenceManager<G> will throw an ObjectDisposedException.
Declaration
public void Dispose()
Exceptions
Type | Condition |
---|---|
IOException | If the underlying reader of the current reference could not be disposed |
Dispose(bool)
Called after Dispose(), so subclass can free any resources.
When overriding, be sure to include a call tobase.Dispose(disposing)
in your implementation.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
Type | Name | Description |
---|---|---|
bool | disposing |
Exceptions
Type | Condition |
---|---|
IOException | if the after dispose operation in a sub-class throws an IOException |
GetRefCount(G)
Returns the current reference count of the given reference.
Declaration
protected abstract int GetRefCount(G reference)
Parameters
Type | Name | Description |
---|---|---|
G | reference |
Returns
Type | Description |
---|---|
int |
MaybeRefresh()
You must call this (or MaybeRefreshBlocking()), periodically, if you want that Acquire() will return refreshed instances.
Threads: it's fine for more than one thread to call this at once. Only the first thread will attempt the refresh; subsequent threads will see that another thread is already handling refresh and will return immediately. Note that this means if another thread is already refreshing then subsequent threads will return right away without waiting for the refresh to complete.
If this method returns true
it means the calling thread either refreshed or
that there were no changes to refresh. If it returns false
it means another
thread is currently refreshing.
Declaration
public bool MaybeRefresh()
Returns
Type | Description |
---|---|
bool |
Exceptions
Type | Condition |
---|---|
IOException | If refreshing the resource causes an IOException |
ObjectDisposedException | If the reference manager has been Dispose()d. |
MaybeRefreshBlocking()
You must call this (or MaybeRefresh()), periodically, if you want that Acquire() will return refreshed instances.
Threads: unlike MaybeRefresh(), if another thread is currently refreshing, this method blocks until that thread completes. It is useful if you want to guarantee that the next call to Acquire() will return a refreshed instance. Otherwise, consider using the non-blocking MaybeRefresh().Declaration
public void MaybeRefreshBlocking()
Exceptions
Type | Condition |
---|---|
IOException | If refreshing the resource causes an IOException |
ObjectDisposedException | If the reference manager has been Dispose()d. |
RefreshIfNeeded(G)
Refresh the given reference if needed. Returns null
if no refresh
was needed, otherwise a new refreshed reference.
Declaration
protected abstract G RefreshIfNeeded(G referenceToRefresh)
Parameters
Type | Name | Description |
---|---|---|
G | referenceToRefresh |
Returns
Type | Description |
---|---|
G |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | If the reference manager has been Dispose()d. |
IOException | If the refresh operation failed |
Release(G)
Release the reference previously obtained via Acquire().
NOTE: it's safe to call this after Dispose().Declaration
public void Release(G reference)
Parameters
Type | Name | Description |
---|---|---|
G | reference |
Exceptions
Type | Condition |
---|---|
IOException | If the release operation on the given resource throws an IOException |
RemoveListener(IRefreshListener)
Remove a listener added with AddListener(IRefreshListener).
Declaration
public virtual void RemoveListener(ReferenceManager.IRefreshListener listener)
Parameters
Type | Name | Description |
---|---|---|
ReferenceManager.IRefreshListener | listener |
TryIncRef(G)
Try to increment reference counting on the given reference. Returns true
if
the operation was successful.
Declaration
protected abstract bool TryIncRef(G reference)
Parameters
Type | Name | Description |
---|---|---|
G | reference |
Returns
Type | Description |
---|---|
bool |
Exceptions
Type | Condition |
---|---|
ObjectDisposedException | if the reference manager has been Dispose()d. |