summaryrefslogtreecommitdiff
path: root/tools/shell/DBClientProxy.h
blob: 3be7d1a73f4fe4f15c1a6d851e11480f4a6964c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef TOOLS_SHELL_DBCLIENTPROXY
#define TOOLS_SHELL_DBCLIENTPROXY

#include <vector>
#include <map>
#include <string>
#include <boost/utility.hpp>
#include <boost/shared_ptr.hpp>

#include "DB.h"

/*
 * class DBClientProxy maintains:
 * 1. a connection to leveldb service
 * 2. a map from db names to opened db handles
 *
 * it's client codes' responsibility to catch all possible exceptions.
 */

namespace leveldb {

class DBClientProxy : private boost::noncopyable {
 public:
  // connect to host_:port_
  void connect(void);

  // return true on success, false otherwise
  bool get(const std::string & db,
           const std::string & key,
           std::string & value);

  // return true on success, false otherwise
  bool put(const std::string & db,
           const std::string & key,
           const std::string & value);

  // return true on success, false otherwise
  bool scan(const std::string & db,
            const std::string & start_key,
            const std::string & end_key,
            const std::string & limit,
            std::vector<std::pair<std::string, std::string> > & kvs);

  // return true on success, false otherwise
  bool create(const std::string & db);

  DBClientProxy(const std::string & host, int port);
  ~DBClientProxy();

 private:
  // some internal help functions
  void cleanUp(void);
  void open(const std::string & db);
  std::map<std::string, Tleveldb::DBHandle>::iterator getHandle(const std::string & db);

  const std::string host_;
  const int port_;
  std::map<std::string, Tleveldb::DBHandle> dbToHandle_;
  boost::shared_ptr<Tleveldb::DBClient> dbClient_;
};

} // namespace
#endif