Swarm-NG  1.1
bdb_database.hpp
1 #pragma once
2 
3 
4 #include "log.hpp"
5 #include <db_cxx.h>
6 
7 
8 namespace swarm { namespace log {
9 
10 struct primary_cursor_t;
11 
12 class bdb_database {
13 
14 public:
15  bdb_database(DbEnv* e):
16  env(e),
17  metadata(e, 0),
18  primary(e, 0),
19  system_idx(e, 0),
20  time_idx(e, 0),
21  event_idx(e, 0)
22  {}
23 
24  static DbEnv* createDefaultEnv();
25  void openEnv(const std::string& basedir);
26  void openForReading(const std::string& fileName);
27  void create(const std::string& fileName);
28  void createEmpty(const std::string& fileName);
29 
30  void put(gpulog::logrecord& lr);
31 
32  void flush();
33 
34  void close();
35 
36  shared_ptr<primary_cursor_t> primary_cursor();
37 
38  // Methods for accessing metadata
39  void addMetaData(const std::string name, const std::string value);
40  std::string getMetaData(const std::string name) ;
41  void fillVersionInfo() ;
42  bool validateVersionInfo() ;
43 
44 
45 private:
46  void openInternal(const std::string& fileName, int open_mode);
47 
48 
49  DbEnv* env;
50  Db metadata,
51  primary,
52  system_idx,
53  time_idx,
54  event_idx;
55 
56 };
57 
58 typedef uint32_t sysid_t;
59 typedef uint8_t evtid_t;
60 struct pkey_t {
61  float time;
62  uint32_t system_event_id;
63 
64  pkey_t(const float& t = 0.0,const int& sysid = 0, const int& evid = 0)
65  :time(t),system_event_id((uint32_t)evid << 24 | sysid){}
66 
67  sysid_t system_id()const{ return (system_event_id & ~ (255 << 24)); }
68  evtid_t event_id()const{ return (evtid_t) (system_event_id >> 24); }
69 };
70 
71 struct lrw_t {
72  char* ptr;
73  int len;
74  gpulog::logrecord lr() { return gpulog::logrecord(ptr); }
75  lrw_t(const int len):ptr(new char[len]),len(len){
77  hdr->len = 3; hdr->msgid = -1;
78  }
79 };
80 
81 struct primary_cursor_t {
82  Dbc* _c;
83  void close();
88  bool get(pkey_t& key,lrw_t& lr, uint32_t flags);
89  bool first(pkey_t& key,lrw_t& lr){ return get(key,lr,DB_FIRST); }
90  bool position_at(pkey_t& key,lrw_t& lr);
91  bool prev(pkey_t& key,lrw_t& lr){ return get(key,lr,DB_PREV); }
92  bool next(pkey_t& key,lrw_t& lr){ return get(key,lr,DB_NEXT); }
93  bool current(pkey_t& key,lrw_t& lr){ return get(key,lr,DB_CURRENT); }
94 };
95 typedef shared_ptr<primary_cursor_t> Pprimary_cursor_t;
96 
97 
98 } } // close namespace log :: swarm