creativity  v1.3.0
Agent-based model of creativity and piracy
StorageBackend.hpp
1 #pragma once
2 #include <queue>
3 #include <condition_variable>
4 #include <cstddef>
5 #include <eris/types.hpp>
6 #include <memory>
7 #include <mutex>
8 #include <thread>
9 #include <eris/noncopyable.hpp>
10 #include "creativity/state/State.hpp"
11 
12 namespace creativity { struct CreativitySettings; }
13 
14 namespace creativity { namespace state {
15 
26 class StorageBackend : private eris::noncopyable {
27  public:
31  StorageBackend(CreativitySettings &settings) : settings_{settings} {}
32 
38  virtual size_t size() = 0;
39 
41  bool empty();
42 
46  virtual void reserve(size_t T);
47 
51  virtual void writeSettings() = 0;
52 
60  void flush(bool flush_buffers = true);
61 
69  bool flush_for(long milliseconds, bool flush_buffers = true);
70 
75  virtual void enqueue(std::shared_ptr<const State> &&s);
76 
78  virtual size_t pending() const;
79 
86  virtual std::shared_ptr<const State> load(eris::eris_time_t t) = 0;
87 
89  virtual ~StorageBackend();
90 
91  protected:
98  virtual void thread_insert(std::shared_ptr<const State> &&s);
99 
103  virtual void storage_flush();
104 
107 
108  private:
109 
113  std::queue<std::shared_ptr<const State>> queue_;
114 
117  unsigned queue_pending_ = 0;
118 
122  bool flush_pending_ = false;
123 
128  mutable std::mutex queue_mutex_;
129 
133  std::condition_variable queue_cv_;
134 
137  std::condition_variable queue_finished_cv_;
138 
142  std::thread thread_;
143 
149  bool thread_quit_ = false;
150 
152  void thread_loop_();
153 
154 };
155 
156 }}
virtual void writeSettings()=0
Writes the settings in settings_ to the storage medium (if appropriate).
Primary namespace for all Creativity library code.
Definition: config.hpp:4
virtual void thread_insert(std::shared_ptr< const State > &&s)
Called from the queue thread to write the given State to the underlying storage.
StorageBackend(CreativitySettings &settings)
Constructor: takes a CreativitySettings reference.
Definition: StorageBackend.hpp:31
bool empty()
Returns true if the container is empty; exactly equivalent to size() == 0
virtual void enqueue(std::shared_ptr< const State > &&s)
Adds a state to this storage container.
void flush(bool flush_buffers=true)
Flushes changes.
virtual void reserve(size_t T)
Optional method that subclasses can override if knowing the number of states that will be stored in a...
virtual size_t pending() const
Returns the number of states still pending in the queue.
virtual size_t size()=0
Returns the number of states stored in the storage object, not including queued but not-yet-added sta...
CreativitySettings & settings_
CreativitySettings reference.
Definition: StorageBackend.hpp:106
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...
virtual ~StorageBackend()
Virtual destructor that tells the thread (if created) to quit and waits for it.
Simulation parameters that are used to configure the simulation when calling setup().
Definition: CreativitySettings.hpp:10
Base class for state storage which accesses State values.
Definition: StorageBackend.hpp:26
virtual std::shared_ptr< const State > load(eris::eris_time_t t)=0
Gets the requested state from the backend.
virtual void storage_flush()
Flush any buffered data to the underlying storage system.