creativity  v1.3.0
Agent-based model of creativity and piracy
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
creativity::state::StorageBackend Class Referenceabstract

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 Stateload (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

CreativitySettingssettings_
 CreativitySettings reference. More...
 

Detailed Description

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.

See also
creativity::state::MemoryStorage
creativity::state::FileStorage

Constructor & Destructor Documentation

§ StorageBackend()

creativity::state::StorageBackend::StorageBackend ( CreativitySettings settings)
inline

Constructor: takes a CreativitySettings reference.

The reference must stay valid for the duration of the StorageBackend.

Member Function Documentation

§ enqueue()

virtual void creativity::state::StorageBackend::enqueue ( std::shared_ptr< const State > &&  s)
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.

§ flush()

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.

§ flush_for()

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.

Returns
true if the flush completed, false if the duration expired without the flush finishing.

§ load()

virtual std::shared_ptr<const State> creativity::state::StorageBackend::load ( eris::eris_time_t  t)
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.

§ pending()

virtual size_t creativity::state::StorageBackend::pending ( ) const
virtual

Returns the number of states still pending in the queue.

§ reserve()

virtual void creativity::state::StorageBackend::reserve ( size_t  T)
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.

§ size()

virtual size_t creativity::state::StorageBackend::size ( )
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.

§ storage_flush()

virtual void creativity::state::StorageBackend::storage_flush ( )
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.

§ thread_insert()

virtual void creativity::state::StorageBackend::thread_insert ( std::shared_ptr< const State > &&  s)
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.

Exceptions
std::logic_errorif called: threading subclasses must override this method.

§ writeSettings()

virtual void creativity::state::StorageBackend::writeSettings ( )
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.

Member Data Documentation

§ settings_

CreativitySettings& creativity::state::StorageBackend::settings_
protected

CreativitySettings reference.

Inheritance diagram for creativity::state::StorageBackend:
[legend]
Collaboration diagram for creativity::state::StorageBackend:
[legend]

The documentation for this class was generated from the following file: