Add proper per-file copyright notices/licenses and top-level license.
[bluesky.git] / kvstore / backend.h
1 #ifndef _BACKEND_H_
2 #define _BACKEND_H_ 1
3
4 extern "C"
5 {
6 #include <db.h>
7 }
8
9 #include <string>
10 #include <map>
11
12 #include <boost/shared_ptr.hpp>
13
14 #include "util.h"
15
16 using namespace std;
17 using boost::shared_ptr;
18
19 namespace kvstore
20 {
21
22 class Backend
23 {
24 public:
25     virtual ~Backend() {};
26
27     virtual bool Put(const string &key,
28                      const string &value) = 0;
29
30     virtual bool Get(const string &key,
31                      string *value) = 0;
32
33 };
34
35 class MemoryBackend : public Backend
36 {
37 public:
38     virtual ~MemoryBackend();
39
40     virtual bool Put(const string &key,
41                      const string &value);
42
43     virtual bool Get(const string &key,
44                      string *value);
45
46 private:
47     boost::mutex _lock;
48     typedef map<string, string> map_t;
49     map_t _map;
50 };
51
52 class BDBBackend : public Backend
53 {
54 public:
55     BDBBackend(const vector<string> &paths,
56                bool flush=true,
57                bool log_in_memory = false);
58
59     virtual ~BDBBackend();
60     virtual bool Put(const string &key,
61                      const string &value);
62
63     virtual bool Get(const string &key,
64                      string *value);
65
66 private:
67     class BDBDatabase;
68
69     inline size_t LookupKeyDB(const string &key);
70
71     vector< shared_ptr<BDBDatabase> > _dbs;
72 };
73
74 } // namespace kvstore
75
76 #endif