creativity
v1.3.0
Agent-based model of creativity and piracy
|
Base class for state storage which accesses State values. More...
#include <creativity/state/StorageBackend.hpp>
Public Member Functions | |
StorageBackend (CreativitySettings &settings) | |
Constructor: takes a CreativitySettings reference. More... | |
virtual size_t | size ()=0 |
Returns the number of states stored in the storage object, not including queued but not-yet-added states. More... | |
bool | empty () |
Returns true if the container is empty; exactly equivalent to size() == 0 | |
virtual void | reserve (size_t T) |
Optional method that subclasses can override if knowing the number of states that will be stored in advance is useful. More... | |
virtual void | writeSettings ()=0 |
Writes the settings in settings_ to the storage medium (if appropriate). More... | |
void | flush (bool flush_buffers=true) |
Flushes changes. More... | |
bool | flush_for (long milliseconds, bool flush_buffers=true) |
Attempts to flush changes, waiting at most the given number of milliseconds for the flush to complete. More... | |
virtual void | enqueue (std::shared_ptr< const State > &&s) |
Adds a state to this storage container. More... | |
virtual size_t | pending () const |
Returns the number of states still pending in the queue. More... | |
virtual std::shared_ptr< const State > | load (eris::eris_time_t t)=0 |
Gets the requested state from the backend. More... | |
virtual | ~StorageBackend () |
Virtual destructor that tells the thread (if created) to quit and waits for it. | |
Protected Member Functions | |
virtual void | thread_insert (std::shared_ptr< const State > &&s) |
Called from the queue thread to write the given State to the underlying storage. More... | |
virtual void | storage_flush () |
Flush any buffered data to the underlying storage system. More... | |
Protected Attributes | |
CreativitySettings & | settings_ |
CreativitySettings reference. More... | |
Base class for state storage which accesses State values.
Subclasses must set size_ when initialized; after initialization, this class will take care of updating it as new states are added.
Subclasses must implement the subscript [] operator, size() method, and push_back(&&) method.
|
inline |
Constructor: takes a CreativitySettings reference.
The reference must stay valid for the duration of the StorageBackend.
|
virtual |
Adds a state to this storage container.
The default implementation spawns a thread then adds it to queue_. Subclasses not using threaded storage MUST override this method to perform the storage immediately.
void creativity::state::StorageBackend::flush | ( | bool | flush_buffers = true | ) |
Flushes changes.
This method blocks until the current thread has finished writing all queued states and the device has been flushed to storage (if applicable and the given argument is true).
If a subclass supports additional flushing capabilities (e.g. syncing a file to disk), it should override storage_flush(), which this method calls.
bool creativity::state::StorageBackend::flush_for | ( | long | milliseconds, |
bool | flush_buffers = true |
||
) |
Attempts to flush changes, waiting at most the given number of milliseconds for the flush to complete.
Returns true if the flush completed, false if the duration expired before the flush completed.
|
pure virtual |
Gets the requested state from the backend.
This is not required to check for objects in the queue: the caller is responsible for caching queued objects.
If the state does not exist in the storage medium, a subclass should return a void shared_ptr.
Implemented in creativity::state::FileStorage.
|
virtual |
Returns the number of states still pending in the queue.
|
virtual |
Optional method that subclasses can override if knowing the number of states that will be stored in advance is useful.
The default implementation does nothing.
|
pure virtual |
Returns the number of states stored in the storage object, not including queued but not-yet-added states.
This is typically called just once by Storage immediately after constructing the object to determine the number of initial storage states; Storage internally tracks (by the number of enqueue calls) the number of states after that.
Implemented in creativity::state::FileStorage.
|
protectedvirtual |
Flush any buffered data to the underlying storage system.
The default does nothing; storage backends with such a concept should override.
Reimplemented in creativity::state::FileStorage.
|
protectedvirtual |
Called from the queue thread to write the given State to the underlying storage.
Threading subclasses MUST override this: the base class implementation throws an exception if called.
std::logic_error | if called: threading subclasses must override this method. |
|
pure virtual |
Writes the settings in settings_ to the storage medium (if appropriate).
Existing stored settings should be replaced with the new values.
Implemented in creativity::state::FileStorage.
|
protected |
CreativitySettings reference.