rapid
A ROS robotics library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
name_db.hpp
Go to the documentation of this file.
1 #ifndef _RAPID_NAME_DB_H_
2 #define _RAPID_NAME_DB_H_
3 
4 #include <string>
5 #include <vector>
6 
7 #include "boost/shared_ptr.hpp"
8 #include "mongodb_store/message_store.h"
9 #include "ros/node_handle.h"
10 
11 namespace rapid {
12 namespace db {
13 // NameDb is a simple database for named ROS messages based on mongodb_store.
14 //
15 // In NameDb, messages are stored in flat collections. Collections are
16 // themselves stored in databases. As a guideline, each project should use its
17 // own database, and messages are organized within that database as needed. We
18 // also recommend that point clouds be stored separately from other data
19 // associated with the point cloud.
20 //
21 // All messages have a name. You insert, query, and delete messages by name.
22 // Names should be unique. If they are not, then Get, Update, and Delete will
23 // operate on an arbitrary message of that name.
24 //
25 // Because we are using names, operations may be slow if the collection grows
26 // large. In that case, you should create an index manually through the MongoDB
27 // shell. For example, if your database is "mydb" and your collection is
28 // "mycollection":
29 // use mydb;
30 // db.mycollection.createIndex({"_meta.name": 1})
31 //
32 // ROS must be running to use this class.
33 class NameDb {
34  public:
35  NameDb(const ros::NodeHandle& nh, const std::string& database,
36  const std::string& collection);
37 
38  template <typename MsgType>
39  void Insert(const std::string& name, const MsgType& msg);
40 
41  template <typename MsgType>
42  void List(std::vector<std::string>* names);
43 
44  template <typename MsgType>
45  bool Get(const std::string& name, MsgType* msg);
46 
47  template <typename MsgType>
48  bool Update(const std::string& name, const MsgType& msg);
49 
50  template <typename MsgType>
51  bool Delete(const std::string& name);
52 
53  mongodb_store::MessageStoreProxy* proxy();
54 
55  private:
56  ros::NodeHandle nh_;
57  std::string database_;
58  std::string collection_;
59  mongodb_store::MessageStoreProxy proxy_;
60 };
61 
62 template <typename MsgType>
63 void NameDb::Insert(const std::string& name, const MsgType& msg) {
64  std::string id = proxy_.insertNamed(name, msg);
65  // Duplicate the ID in the _meta document, since there's no other way to
66  // retrieve the _id just by knowing the name.
67  mongo::BSONObj meta = BSON("_id" << id);
68  proxy_.updateID(id, msg, meta);
69 }
70 
71 template <typename MsgType>
72 void NameDb::List(std::vector<std::string>* names) {
73  std::vector<std::pair<boost::shared_ptr<MsgType>, mongo::BSONObj> > messages;
74  mongo::BSONObj message_query;
75  mongo::BSONObj meta_query;
76  mongo::BSONObj sort_query;
77  mongo::BSONObj projection_query = BSON("_meta" << 1);
78  const bool kFindOne = false;
79  const bool kDecodeMetas = true;
80  const int kLimit = 0;
81  bool success = proxy_.queryWithProjection(messages, message_query, meta_query,
82  sort_query, projection_query,
83  kFindOne, kDecodeMetas, kLimit);
84  if (!success) {
85  return;
86  }
87 
88  names->clear();
89  for (size_t i = 0; i < messages.size(); ++i) {
90  const mongo::BSONObj& meta = messages[i].second;
91  const std::string& name = meta.getStringField("name");
92  names->push_back(name);
93  }
94 }
95 
96 template <typename MsgType>
97 bool NameDb::Get(const std::string& name, MsgType* msg) {
98  const bool kFindOne = true;
99  std::vector<boost::shared_ptr<MsgType> > results;
100  bool success = proxy_.queryNamed(name, results, kFindOne);
101  if (!success || results.size() == 0) {
102  return false;
103  }
104  *msg = *(results[0]);
105  return true;
106 }
107 
108 template <typename MsgType>
109 bool NameDb::Update(const std::string& name, const MsgType& msg) {
110  return proxy_.updateNamed(name, msg);
111 }
112 
113 template <typename MsgType>
114 bool NameDb::Delete(const std::string& name) {
115  std::vector<std::pair<boost::shared_ptr<MsgType>, mongo::BSONObj> > messages;
116  mongo::BSONObj message_query;
117  mongo::BSONObj meta_query = BSON("name" << name);
118  mongo::BSONObj sort_query;
119  mongo::BSONObj projection_query = BSON("_id" << 1 << "_meta" << 1);
120  const bool kFindOne = false;
121  const bool kDecodeMetas = true;
122  const int kLimit = 0;
123  bool success = proxy_.queryWithProjection(messages, message_query, meta_query,
124  sort_query, projection_query,
125  kFindOne, kDecodeMetas, kLimit);
126  if (!success || messages.size() == 0) {
127  return false;
128  }
129 
130  const mongo::BSONObj& meta = messages[0].second;
131  const std::string& id = meta.getStringField("_id");
132  return proxy_.deleteID(id);
133 }
134 } // namespace db
135 } // namespace rapid
136 
137 #endif // _RAPID_DB_NAME_DB_H_
bool Update(const std::string &name, const MsgType &msg)
Definition: name_db.hpp:109
bool Get(const std::string &name, MsgType *msg)
Definition: name_db.hpp:97
void List(std::vector< std::string > *names)
Definition: name_db.hpp:72
mongodb_store::MessageStoreProxy * proxy()
tuple database
NameDb(const ros::NodeHandle &nh, const std::string &database, const std::string &collection)
void Insert(const std::string &name, const MsgType &msg)
Definition: name_db.hpp:63
bool Delete(const std::string &name)
Definition: name_db.hpp:114