JODA  0.13.1 (59b41972)
JSON On-Demand Analysis
JSONStorage.h
Go to the documentation of this file.
1 //
2 // Created by Nico Schäfer on 9/7/17.
3 //
4 
5 #ifndef JODA_JSONSTORAGE_H
6 #define JODA_JSONSTORAGE_H
7 
10 
13 #include <future>
14 #include <mutex>
15 #include <unordered_map>
16 class QueryPlan;
17 
24 class JSONStorage {
25  public:
26  virtual ~JSONStorage();
27  void preparePurge();
28  explicit JSONStorage(std::string query_string);
45  size_t &insertedDocs, size_t &insertedConts);
46 
54  void insertDocuments(
55  moodycamel::ConcurrentQueue<std::unique_ptr<JSONContainer>> &queue,
56  const std::atomic_bool &cf, std::atomic_uint &cs);
57 
62  void insertDocuments(std::unique_ptr<JSONContainer> &&cont);
63 
69 
74  unsigned long size() const;
79  unsigned long contSize() const;
80 
81  // Memory
82 
87  size_t estimatedSize() const;
93  size_t estimatedCapacity() const;
94 
100  void freeAllMemory();
101 
102  size_t parsedSize() const;
103 
109  const std::string &getName() const;
110 
115  void writeFile(const std::string &file);
120  void writeFiles(const std::string &file);
121 
129  std::vector<std::string> stringify(unsigned long start = 0,
130  unsigned long end = ULONG_MAX);
131 
138  std::vector<std::shared_ptr<RJDocument>> getRaw(
139  unsigned long start = 0, unsigned long end = ULONG_MAX);
140 
148  template <class Handler>
149  std::vector<bool> AcceptDocuments(Handler &handler, unsigned long start = 0,
150  unsigned long end = ULONG_MAX) {
151  std::lock_guard<std::mutex> lock(documentMutex);
152  std::vector<bool> ret;
153  if (docCount == 0) return ret;
154  end = std::min(end, docCount - 1);
155  if (start > end) return ret;
156  auto count = (end - start) + 1;
157  ret.reserve(count);
158  auto it = container.begin();
159 
160  unsigned long skippedSize = 0;
161  // Browse container until designated is reached
162  while (start >= (*it)->size() + skippedSize) {
163  skippedSize += (*it)->size();
164  it++;
165  DCHECK(it != container.end());
166  }
167 
168  while (ret.size() < count) {
169  size_t contStart = start - skippedSize;
170  contStart = std::max(contStart, (size_t)0);
171  unsigned long contEnd = contStart + count - ret.size() - 1;
172  if (it == container.end()) {
173  DCHECK(false) << "If all Container contributed to the raws, this "
174  "should not be possible";
175  return ret;
176  }
177 
178  if ((*it)->size() != 0) {
179  auto tmp = (*it)->AcceptDocuments(handler, contStart, contEnd);
180  DCHECK_EQ(tmp.size(), contEnd - contStart + 1);
181  std::move(tmp.begin(), tmp.end(), std::back_inserter(ret));
182  it++;
183  } else {
184  DLOG(INFO) << "Skipping empty container";
185  }
186  }
187  return ret;
188  }
189 
194  const std::string &getRegtmpdir() const;
195 
199  unsigned long getLastUsed() const;
200 
205  void addQueryString(const std::string &query_string);
206 
212  const std::string &getQueryString() const;
213 
214 
215 
216  protected:
217  friend QueryPlan;
218  const std::vector<std::unique_ptr<JSONContainer>> &getContainer() const;
219 
220  std::string name;
221  std::string regtmpdir;
222 
223  // Stored Queries
224  std::string query;
225 
226  std::mutex documentMutex;
227  std::vector<std::unique_ptr<JSONContainer>> container;
228  unsigned long docCount = 0;
229 
230  // Multithread
231  size_t threadCount(size_t containerSize) const;
232 
233  std::string getStorageID() const;
234 };
235 
236 #endif // JODA_JSONSTORAGE_H
Definition: JSONStorage.h:24
unsigned long docCount
Definition: JSONStorage.h:228
friend QueryPlan
Definition: JSONStorage.h:217
std::string query
Definition: JSONStorage.h:224
void insertDocumentsQueue(JsonContainerQueue::queue_t *queue)
Definition: JSONStorage.cpp:281
std::vector< std::unique_ptr< JSONContainer > > container
Definition: JSONStorage.h:227
const std::string & getRegtmpdir() const
Definition: JSONStorage.cpp:279
const std::string & getQueryString() const
Definition: JSONStorage.cpp:357
void freeAllMemory()
Definition: JSONStorage.cpp:80
size_t estimatedSize() const
Definition: JSONStorage.cpp:56
void writeFile(const std::string &file)
Definition: JSONStorage.cpp:127
std::string getStorageID() const
Definition: JSONStorage.cpp:87
void insertDocuments(moodycamel::ConcurrentQueue< std::unique_ptr< JSONContainer >> &queue, const std::atomic_bool &cf, std::atomic_uint &cs)
Definition: JSONStorage.cpp:13
JSONStorage(std::string query_string)
Definition: JSONStorage.cpp:94
size_t parsedSize() const
Definition: JSONStorage.cpp:64
std::vector< std::shared_ptr< RJDocument > > getRaw(unsigned long start=0, unsigned long end=ULONG_MAX)
Definition: JSONStorage.cpp:234
const std::string & getName() const
Definition: JSONStorage.cpp:125
std::mutex documentMutex
Definition: JSONStorage.h:226
void getDocumentsQueue(JsonContainerRefQueue::queue_t *queue)
Definition: JSONStorage.cpp:325
void addQueryString(const std::string &query_string)
Definition: JSONStorage.cpp:350
void writeFiles(const std::string &file)
Definition: JSONStorage.cpp:135
std::string regtmpdir
Definition: JSONStorage.h:221
const std::vector< std::unique_ptr< JSONContainer > > & getContainer() const
Definition: JSONStorage.cpp:345
unsigned long contSize() const
Definition: JSONStorage.cpp:47
unsigned long getLastUsed() const
Definition: JSONStorage.cpp:335
std::string name
Definition: JSONStorage.h:220
size_t threadCount(size_t containerSize) const
Definition: JSONStorage.cpp:49
void preparePurge()
Definition: JSONStorage.cpp:118
std::vector< bool > AcceptDocuments(Handler &handler, unsigned long start=0, unsigned long end=ULONG_MAX)
Definition: JSONStorage.h:149
virtual ~JSONStorage()
Definition: JSONStorage.cpp:103
unsigned long size() const
Definition: JSONStorage.cpp:45
size_t estimatedCapacity() const
Definition: JSONStorage.cpp:72
std::vector< std::string > stringify(unsigned long start=0, unsigned long end=ULONG_MAX)
Definition: JSONStorage.cpp:181
Definition: QueryPlan.h:25
Definition: concurrentqueue.h:802
Definition: Queue.h:19