redis3m  1.0.0
 All Classes Functions Variables Enumerations Pages
simple_obj_store.h
1 // Copyright (c) 2014 Luca Marturana. All rights reserved.
2 // Licensed under Apache 2.0, see LICENSE for details
3 
4 #pragma once
5 
6 #include <string>
7 #include <redis3m/connection.h>
8 #include <map>
9 #include <stdexcept>
10 #include <boost/foreach.hpp>
11 
12 namespace redis3m
13 {
14 namespace patterns
15 {
16 
17 template<typename Model>
23 {
24 public:
25 
33  Model find(connection::ptr_t connection, const std::string& id)
34  {
35  redis3m::reply r = connection->run(redis3m::command("HGETALL")(Model::model_name() + ":" + id));
36 
37  const std::vector<redis3m::reply>& key_values = r.elements();
38  if (key_values.size() > 0)
39  {
40  std::map<std::string, std::string> serialized;
41 
42  for (int i=0; i < key_values.size(); i+=2)
43  {
44  serialized[key_values.at(i).str()] = key_values.at(i+1).str();
45  }
46  return Model(id, serialized);
47  }
48  else
49  {
50  return Model();
51  }
52  }
53 
61  void append_save(connection::ptr_t connection, const Model& m)
62  {
63  std::map<std::string, std::string> serialized = m.to_map();
64 
65  std::vector<std::string> hmset_command;
66  hmset_command.push_back("HMSET");
67  hmset_command.push_back(Model::model_name() + ":" + m.id());
68 
69  std::pair<std::string, std::string> item;
70  BOOST_FOREACH(item, serialized)
71  {
72  hmset_command.push_back(item.first);
73  hmset_command.push_back(item.second);
74  }
75 
76  connection->append(hmset_command);
77  }
78 
84  void save(connection::ptr_t connection, const Model& m)
85  {
86  append_save(connection, m);
87  connection->get_reply();
88  }
89 
97  void append_remove(connection::ptr_t connection, const Model& m)
98  {
99  connection->append(command("DEL")(m.model_name() + ":" + m.id()));
100  }
101 
107  void remove(connection::ptr_t connection, const Model& m)
108  {
111  }
112 
118  inline std::string model_key(const std::string& id)
119  {
120  return Model::model_name() + ":" + id;
121  }
122 };
123 }
124 }
Model find(connection::ptr_t connection, const std::string &id)
As the name says, get an object from id specifying it's unique identifier.
Definition: simple_obj_store.h:33
Simple object storage, ready to use save, find and remove of model classes. id management is not prov...
Definition: simple_obj_store.h:22
The connection class, represent a connection to a Redis server.
Definition: connection.h:27
void append_remove(connection::ptr_t connection, const Model &m)
Remove object and all related data from database. Append only method, for using inside a transaction ...
Definition: simple_obj_store.h:97
void save(connection::ptr_t connection, const Model &m)
Save an object on database, or update it if it's already present.
Definition: simple_obj_store.h:84
std::string model_key(const std::string &id)
Returns Redis Key used to save object data.
Definition: simple_obj_store.h:118
const std::vector< reply > & elements() const
Returns a vector of sub-replies if present, otherwise an empty one.
Definition: reply.h:53
void append_save(connection::ptr_t connection, const Model &m)
Save an object on database, or update it if it's already present. Append only method, for using inside a transaction for example. You should take care of calling connection::get_reply() after it.
Definition: simple_obj_store.h:61
Represent a reply received from redis server.
Definition: reply.h:18
reply get_reply()
Get a reply from server, blocking call if no reply is ready.
Definition: connection.cpp:45