SSDB C++ API
The C++ Client API for SSDB server
SSDB.h
1 #ifndef SSDB_API_CPP
2 #define SSDB_API_CPP
3 
4 #ifndef __STDC_FORMAT_MACROS
5 #define __STDC_FORMAT_MACROS
6 #endif
7 
8 #include <inttypes.h>
9 #include <string>
10 #include <vector>
11 
12 namespace ssdb{
13 
17 class Status{
18 public:
22  bool ok(){
23  return code_ == "ok";
24  }
29  bool not_found(){
30  return code_ == "not_found";
31  }
36  bool error(){
37  return code_ != "ok";
38  }
42  std::string code(){
43  return code_;
44  }
45 
46  Status(){}
47  Status(const std::string &code){
48  code_ = code;
49  }
50  Status(const std::vector<std::string> *resp){
51  if(resp && resp->size() > 0){
52  code_ = resp->at(0);
53  }else{
54  code_ = "error";
55  }
56  }
57 private:
58  std::string code_;
59 };
60 
64 class Client{
65 public:
66  static Client* connect(const char *ip, int port);
67  Client(){};
68  virtual ~Client(){};
69 
74  virtual const std::vector<std::string>* request(const std::vector<std::string> &req) = 0;
75  virtual const std::vector<std::string>* request(const std::string &cmd) = 0;
76  virtual const std::vector<std::string>* request(const std::string &cmd, const std::string &s2) = 0;
77  virtual const std::vector<std::string>* request(const std::string &cmd, const std::string &s2, const std::string &s3) = 0;
78  virtual const std::vector<std::string>* request(const std::string &cmd, const std::string &s2, const std::string &s3, const std::string &s4) = 0;
79  virtual const std::vector<std::string>* request(const std::string &cmd, const std::string &s2, const std::string &s3, const std::string &s4, const std::string &s5) = 0;
80  virtual const std::vector<std::string>* request(const std::string &cmd, const std::string &s2, const std::string &s3, const std::string &s4, const std::string &s5, const std::string &s6) = 0;
82 
85  virtual Status get(const std::string &key, std::string *val) = 0;
86  virtual Status set(const std::string &key, const std::string &val) = 0;
90  virtual Status setx(const std::string &key, const std::string &val, int ttl) = 0;
91  virtual Status del(const std::string &key) = 0;
92  virtual Status incr(const std::string &key, int64_t incrby, int64_t *ret) = 0;
97  virtual Status keys(const std::string &key_start, const std::string &key_end,
98  uint64_t limit, std::vector<std::string> *ret) = 0;
103  virtual Status scan(const std::string &key_start, const std::string &key_end,
104  uint64_t limit, std::vector<std::string> *ret) = 0;
109  virtual Status rscan(const std::string &key_start, const std::string &key_end,
110  uint64_t limit, std::vector<std::string> *ret) = 0;
112 
113 
116  virtual Status hget(const std::string &name, const std::string &key, std::string *val) = 0;
117  virtual Status hset(const std::string &name, const std::string &key, const std::string &val) = 0;
118  virtual Status hdel(const std::string &name, const std::string &key) = 0;
119  virtual Status hincr(const std::string &name, const std::string &key, int64_t incrby, int64_t *ret) = 0;
120  virtual Status hsize(const std::string &name, int64_t *ret) = 0;
124  virtual Status hclear(const std::string &name, int64_t *ret) = 0;
129  virtual Status hkeys(const std::string &name,
130  const std::string &key_start, const std::string &key_end,
131  uint64_t limit, std::vector<std::string> *ret) = 0;
136  virtual Status hscan(const std::string &name,
137  const std::string &key_start, const std::string &key_end,
138  uint64_t limit, std::vector<std::string> *ret) = 0;
143  virtual Status hrscan(const std::string &name,
144  const std::string &key_start, const std::string &key_end,
145  uint64_t limit, std::vector<std::string> *ret) = 0;
147 
148 
151  virtual Status zget(const std::string &name, const std::string &key, int64_t *ret) = 0;
152  virtual Status zset(const std::string &name, const std::string &key, int64_t score) = 0;
153  virtual Status zdel(const std::string &name, const std::string &key) = 0;
154  virtual Status zincr(const std::string &name, const std::string &key, int64_t incrby, int64_t *ret) = 0;
155  virtual Status zsize(const std::string &name, int64_t *ret) = 0;
159  virtual Status zclear(const std::string &name, int64_t *ret) = 0;
163  virtual Status zrank(const std::string &name, const std::string &key, int64_t *ret) = 0;
167  virtual Status zrrank(const std::string &name, const std::string &key, int64_t *ret) = 0;
171  virtual Status zrange(const std::string &name,
172  uint64_t offset, uint64_t limit,
173  std::vector<std::string> *ret) = 0;
177  virtual Status zrrange(const std::string &name,
178  uint64_t offset, uint64_t limit,
179  std::vector<std::string> *ret) = 0;
184  virtual Status zkeys(const std::string &name, const std::string &key_start,
185  int64_t *score_start, int64_t *score_end,
186  uint64_t limit, std::vector<std::string> *ret) = 0;
191  virtual Status zscan(const std::string &name, const std::string &key_start,
192  int64_t *score_start, int64_t *score_end,
193  uint64_t limit, std::vector<std::string> *ret) = 0;
198  virtual Status zrscan(const std::string &name, const std::string &key_start,
199  int64_t *score_start, int64_t *score_end,
200  uint64_t limit, std::vector<std::string> *ret) = 0;
202 
203 private:
204  // No copying allowed
205  Client(const Client&);
206  void operator=(const Client&);
207 };
208 
209 }; // namespace ssdb
210 
211 #endif
virtual Status setx(const std::string &key, const std::string &val, int ttl)=0
Set the value of the key, with a time to live.
virtual Status zrange(const std::string &name, uint64_t offset, uint64_t limit, std::vector< std::string > *ret)=0
Important! This method is SLOW for large offset!
virtual Status hclear(const std::string &name, int64_t *ret)=0
Delete all of the keys in a hashmap, return the number of keys deleted.
bool error()
Returns true if error occurs during the request.
Definition: SSDB.h:36
virtual Status hscan(const std::string &name, const std::string &key_start, const std::string &key_end, uint64_t limit, std::vector< std::string > *ret)=0
Return key-value pairs.
virtual Status hrscan(const std::string &name, const std::string &key_start, const std::string &key_end, uint64_t limit, std::vector< std::string > *ret)=0
Return key-value pairs.
bool not_found()
Returns true if the requested key is not found.
Definition: SSDB.h:29
virtual Status zscan(const std::string &name, const std::string &key_start, int64_t *score_start, int64_t *score_end, uint64_t limit, std::vector< std::string > *ret)=0
Return key-score pairs.
virtual Status hkeys(const std::string &name, const std::string &key_start, const std::string &key_end, uint64_t limit, std::vector< std::string > *ret)=0
virtual Status rscan(const std::string &key_start, const std::string &key_end, uint64_t limit, std::vector< std::string > *ret)=0
Return key-value pairs.
bool ok()
Returns true if the request succeeded.
Definition: SSDB.h:22
virtual Status scan(const std::string &key_start, const std::string &key_end, uint64_t limit, std::vector< std::string > *ret)=0
Return key-value pairs.
virtual Status zrrank(const std::string &name, const std::string &key, int64_t *ret)=0
Important! This method may be extremly SLOW!
virtual Status zrscan(const std::string &name, const std::string &key_start, int64_t *score_start, int64_t *score_end, uint64_t limit, std::vector< std::string > *ret)=0
Return key-score pairs.
virtual Status zrank(const std::string &name, const std::string &key, int64_t *ret)=0
Important! This method may be extremly SLOW!
std::string code()
The response code.
Definition: SSDB.h:42
virtual Status keys(const std::string &key_start, const std::string &key_end, uint64_t limit, std::vector< std::string > *ret)=0
Client requests&#39; return status.
Definition: SSDB.h:17
virtual Status zrrange(const std::string &name, uint64_t offset, uint64_t limit, std::vector< std::string > *ret)=0
Important! This method is SLOW for large offset!
virtual Status zkeys(const std::string &name, const std::string &key_start, int64_t *score_start, int64_t *score_end, uint64_t limit, std::vector< std::string > *ret)=0
The SSDB client used to connect to SSDB server.
Definition: SSDB.h:64
virtual Status zclear(const std::string &name, int64_t *ret)=0
Delete all of the keys in a zset, return the number of keys deleted.