BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
Journal.h
1 #pragma once
2 
3 #include <list>
4 #include <set>
5 #include <string>
6 
7 #include <Poco/File.h>
8 #include <Poco/Mutex.h>
9 #include <Poco/Nullable.h>
10 #include <Poco/Path.h>
11 #include <Poco/SharedPtr.h>
12 
13 #include "util/Loggable.h"
14 
15 namespace BeeeOn {
16 
60 class Journal : protected virtual Loggable {
61 public:
62  typedef Poco::SharedPtr<Journal> Ptr;
63 
67  struct Record {
68  const std::string key;
69  const std::string value;
70 
71  Record &operator =(const Record &other)
72  {
73  const_cast<std::string&>(this->key) = other.key;
74  const_cast<std::string&>(this->value) = other.value;
75  return *this;
76  }
77 
78  bool operator ==(const Record &other) const
79  {
80  return key == other.key && value == other.value;
81  }
82  };
83 
84  Journal(
85  const Poco::Path &file,
86  double duplicatesFactor = 3,
87  size_t minimalRewritesSize = 4096);
88  virtual ~Journal();
89 
94  bool createEmpty();
95 
108  void checkExisting(bool regularFile = true, bool writable = true) const;
109 
115  void load(bool recover = false);
116 
126  void load(std::istream &in, bool recover = false);
127 
132  void checkConsistent() const;
133 
138  void checkConsistent(std::istream &in) const;
139 
144  void append(
145  const std::string &key,
146  const std::string &value,
147  bool flush = true)
148  {
149  append({key, value}, flush);
150  }
151 
157  void append(const Record &record, bool flush = true);
158 
163  void drop(const std::string &key, bool flush = true)
164  {
165  drop(std::set<std::string>{key}, flush);
166  }
167 
175  void drop(const std::set<std::string> &keys, bool flush = true);
176 
183  void flush();
184 
189  std::list<Record> records() const;
190 
194  Poco::Nullable<std::string> operator [](const std::string &key) const;
195 
200  double currentDuplicatesFactor() const;
201 
202 protected:
203  void parseStream(std::istream &in, std::list<Record> &records) const;
204  void parseStreamRecover(std::istream &in, std::list<Record> &records) const;
205  void appendDrop(const std::string &key, bool flush);
206  void dropInPlace(std::list<Record> &records, const std::string &key) const;
207 
208  double duplicatesFactor(const std::list<Record> &records) const;
209  bool overMinimalSize() const;
210  std::list<Record> recordsRaw() const;
211  void interpret(std::list<Record> &records) const;
212  void interpretAndFlush();
213  void appendFlush();
214  void rewriteAndFlush(const std::list<Record> &records);
215 
216  std::list<Record> &committed();
217  std::list<Record> &dirty();
218 
219  void handleFailure(std::ostream &o) const;
220 
221  void checkRecord(const Record &record) const;
222  std::string format(const Record &record, bool zeroSum = false) const;
223  Record parse(const std::string &line, size_t lineno) const;
224  size_t bytes(const std::list<Record> &records) const;
225 
226 private:
227  mutable Poco::Mutex m_lock;
228  const Poco::File m_file;
229  double m_duplicatesFactor;
230  size_t m_minimalRewriteSize;
231  std::list<Record> m_records;
232  std::list<Record> m_dirty;
233 };
234 
235 }
void drop(const std::string &key, bool flush=true)
Mark the given key as dropped.
Definition: Journal.h:163
std::list< Record > records() const
Definition: Journal.cpp:332
Journal implements a simple journaling principle in filesystem. A Journal instance represents an appe...
Definition: Journal.h:60
A single record in journal.
Definition: Journal.h:67
void checkConsistent() const
Check that the RAM journal representation is equivalent to the persistent representation in the under...
Definition: Journal.cpp:125
void flush()
Flush all records in the waiting list. If the current duplicates factor is high enough and the size o...
Definition: Journal.cpp:212
double currentDuplicatesFactor() const
Definition: Journal.cpp:224
void checkExisting(bool regularFile=true, bool writable=true) const
Check for common situations that might be a symptom of an invalid setup. We check whether the underly...
Definition: Journal.cpp:51
void append(const std::string &key, const std::string &value, bool flush=true)
Append the key-value pair into the journal.
Definition: Journal.h:144
Poco::Nullable< std::string > operator[](const std::string &key) const
Definition: Journal.cpp:340
Definition: Loggable.h:19
void load(bool recover=false)
Load the journal from the underlying file. If the parameter recover is true, the method skips malform...
Definition: Journal.cpp:101
bool createEmpty()
Create empty journal if it does not exists yet.
Definition: Journal.cpp:46