knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
QueryWatch.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021, Daniel Beßler
3  * All rights reserved.
4  *
5  * This file is part of KnowRob, please consult
6  * https://github.com/knowrob/knowrob for license details.
7  */
8 
9 #ifndef KNOWROB_MONGO_QUERY_WATCH_H
10 #define KNOWROB_MONGO_QUERY_WATCH_H
11 
12 #include <mongoc.h>
13 #include <string>
14 #include <mutex>
15 #include <thread>
16 #include <chrono>
17 #include <atomic>
18 #include <map>
19 #include <knowrob/storage/mongo/ChangeStream.h>
20 
21 namespace knowrob::mongo {
26  class QueryWatch {
27  public:
29 
30  QueryWatch(const QueryWatch&) = delete;
31 
33 
38  void setWatchRate(uint32_t rate) { watchRate_ = rate; }
39 
47  long watch(const std::shared_ptr<Collection> &collection,
48  const bson_t *query,
49  const ChangeStreamCallback &callback);
50 
55  void unwatch(long watcher_id);
56 
57  protected:
58  std::map<long, std::unique_ptr<ChangeStream>> watcher_map_;
59 
60  std::thread *thread_;
61  bool isRunning_;
62  std::mutex lock_;
63  uint32_t watchRate_;
64  static std::atomic<long> id_counter_;
65 
68  void loop();
69  };
70 }
71 
72 #endif //KNOWROB_MONGO_QUERY_WATCH_H
static std::atomic< long > id_counter_
Definition: QueryWatch.h:64
std::map< long, std::unique_ptr< ChangeStream > > watcher_map_
Definition: QueryWatch.h:58
long watch(const std::shared_ptr< Collection > &collection, const bson_t *query, const ChangeStreamCallback &callback)
void unwatch(long watcher_id)
QueryWatch(const QueryWatch &)=delete
void setWatchRate(uint32_t rate)
Definition: QueryWatch.h:38
std::function< void(const bson_wrapper_ptr &)> ChangeStreamCallback
Definition: ChangeStream.h:21