creativity  v1.3.0
Agent-based model of creativity and piracy
FileStorage.hpp
1 #pragma once
2 #include "creativity/state/StorageBackend.hpp"
3 #include "creativity/BookCopy.hpp"
4 #include "creativity/CreativitySettings.hpp"
5 #include <eris/learning/BayesianLinearRestricted.hpp>
6 #include <eris/types.hpp>
7 #include <eris/serialize/serializer.hpp>
8 #include <eris/serialize/Serialization.hpp>
9 #include <Eigen/Core>
10 #include <boost/detail/endian.hpp>
11 #include <cstddef>
12 #include <cstdint>
13 #include <functional>
14 #include <vector>
15 #include <iostream>
16 #include <memory>
17 #include <set>
18 #include <stdexcept>
19 #include <string>
20 #include <map>
21 #include <utility>
22 
23 namespace creativity { namespace state {
24 
33 class FileStorage : public StorageBackend, public eris::serialize::Serialization {
34  public:
36  enum TYPE : uint8_t {
37  TYPE_DONE = 0,
39  TYPE_BOOKS = 2,
41  };
42 
51  memory();
52  }
53 
63  template <typename... Args>
64  FileStorage(CreativitySettings &settings, const std::string &filename, Mode mode, Args&&... args)
65  : StorageBackend(settings)
66  {
67  open(filename, mode, std::forward<Args>(args)...);
68  }
69 
74  FileStorage(CreativitySettings &settings, std::unique_ptr<std::stringstream> &&s, Mode mode = Mode::READONLY);
75 
77  //FileStorage(FileStorage&&) = default;
78 
80  //FileStorage& operator=(FileStorage&&) = default;
81 
86  void throwParseError(const std::string& message) const;
87 
91  virtual std::shared_ptr<const State> load(eris::eris_time_t t) override;
92 
94  virtual size_t size() override;
95 
104  virtual void storage_flush() override;
105 
106  protected:
107 
109  void configureHeaderFields() override;
110 
112  void writeSettings() override;
113 
115  void writeExtraHeader() override;
116 
118  void readExtraHeader() override;
119 
120  private:
121 
123  uint64_t state_pointer_block_, readerlib_block_;
124 
125  // Sorting method that sorts by the library item's acquired date
126  class lib_comp_less final {
127  public:
128  using ref = const std::pair<const eris::eris_id_t, BookCopy>&;
129  bool operator()(ref a, ref b) const {
130  return a.second.acquired < b.second.acquired;
131  }
132  };
133 
134  // Reader library data
135  struct lib_data {
136  // book id to BookCopy
137  std::map<uint64_t, BookCopy> library;
138  // library, but sorted by acquired date (for fast retrieval)
139  std::multiset<std::reference_wrapper<std::pair<const uint64_t, BookCopy>>, lib_comp_less> library_acq_sorted;
140  };
141 
143  //id, the value is the pair of / the block location and the library data content.
144  std::map<eris::eris_id_t, std::pair<uint64_t, lib_data>> reader_lib_;
145 
154  virtual uint32_t appFileVersion() const override { return 2; }
155 
157  virtual std::string appName() const override { return "creativity"; }
158 
160  virtual void thread_insert(std::shared_ptr<const State> &&s) override;
161 
170  void updateLibraries(const std::map<eris::eris_id_t, ReaderState> &r);
171 
186  std::shared_ptr<const State> readState();
187 
189  void writeState(const State &state);
190 
225  std::pair<eris::eris_id_t, ReaderState> readReader(eris::eris_time_t t);
226 
228  void writeReader(const ReaderState& reader);
229 
231  typedef struct {
232  uint32_t K = 0;
233  bool noninformative = true;
234  Eigen::VectorXd beta;
235  double s2;
236  double n;
237  Eigen::MatrixXd Vinv;
238  eris::learning::BayesianLinearRestricted::DrawMode last_draw_mode;
239  uint32_t draw_success_cumulative,
240  draw_discards_cumulative;
241  } belief_data;
242 
281  belief_data readBelief();
282 
294  void writeBelief(const eris::learning::BayesianLinear &belief);
295 
319  std::pair<eris::eris_id_t, BookState> readBook();
320 
322  void writeBook(const BookState &book);
323 
333  std::unique_ptr<PublicTrackerState> readPublicTracker();
334 
336  void writePublicTracker(const PublicTrackerState &pt);
337 };
338 
339 }}
Records the various variables associated with a reader.
Definition: ReaderState.hpp:18
Primary namespace for all Creativity library code.
Definition: config.hpp:4
virtual size_t size() override
Returns the number of states currently stored in the file.
FileStorage(CreativitySettings &settings)
Constructs a FileStorage that stores file content in an in-memory buffer.
Definition: FileStorage.hpp:50
An array of books.
Definition: FileStorage.hpp:39
Class storing the state of the simulation at the end of a simulation period.
Definition: State.hpp:19
An array of readers.
Definition: FileStorage.hpp:38
Class for file-based storage.
Definition: FileStorage.hpp:33
void writeSettings() override
Calls updateHeaderFields() to rewrite settings.
A PublicTracker state.
Definition: FileStorage.hpp:40
virtual void storage_flush() override
Flushes the file stream.
virtual std::shared_ptr< const State > load(eris::eris_time_t t) override
Loads the requested state data from the open file into a State object and returns it (wrapped in a sh...
Psuedo-type indicating the end of the record list.
Definition: FileStorage.hpp:37
void writeExtraHeader() override
Adds a state pointer block to the end of the header.
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
void configureHeaderFields() override
Registers the various creativity parameters as header fields.
Records the various variables associated with a reader.
Definition: PublicTrackerState.hpp:11
void readExtraHeader() override
Stores the location of the state pointer block at the end of the head.
TYPE
The supported record types within a state.
Definition: FileStorage.hpp:36
FileStorage(CreativitySettings &settings, const std::string &filename, Mode mode, Args &&... args)
Constructs and returns a FileStorage object that uses the given file for reading and (optionally) wri...
Definition: FileStorage.hpp:64
void throwParseError(const std::string &message) const
Default move constructor.
Records the various variables associated with a book.
Definition: BookState.hpp:12