1 #ifndef _RAPID_NAME_DB_H_
2 #define _RAPID_NAME_DB_H_
7 #include "boost/shared_ptr.hpp"
8 #include "mongodb_store/message_store.h"
9 #include "ros/node_handle.h"
36 const std::string& collection);
38 template <
typename MsgType>
39 void Insert(
const std::string& name,
const MsgType& msg);
41 template <
typename MsgType>
42 void List(std::vector<std::string>* names);
44 template <
typename MsgType>
45 bool Get(
const std::string& name, MsgType* msg);
47 template <
typename MsgType>
48 bool Update(
const std::string& name,
const MsgType& msg);
50 template <
typename MsgType>
51 bool Delete(
const std::string& name);
53 mongodb_store::MessageStoreProxy*
proxy();
57 std::string database_;
58 std::string collection_;
59 mongodb_store::MessageStoreProxy proxy_;
62 template <
typename MsgType>
64 std::string
id = proxy_.insertNamed(name, msg);
67 mongo::BSONObj meta = BSON(
"_id" <<
id);
68 proxy_.updateID(
id, msg, meta);
71 template <
typename MsgType>
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;
81 bool success = proxy_.queryWithProjection(messages, message_query, meta_query,
82 sort_query, projection_query,
83 kFindOne, kDecodeMetas, kLimit);
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);
96 template <
typename MsgType>
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) {
104 *msg = *(results[0]);
108 template <
typename MsgType>
110 return proxy_.updateNamed(name, msg);
113 template <
typename MsgType>
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) {
130 const mongo::BSONObj& meta = messages[0].second;
131 const std::string&
id = meta.getStringField(
"_id");
132 return proxy_.deleteID(
id);
137 #endif // _RAPID_DB_NAME_DB_H_
bool Update(const std::string &name, const MsgType &msg)
bool Get(const std::string &name, MsgType *msg)
void List(std::vector< std::string > *names)
mongodb_store::MessageStoreProxy * proxy()
NameDb(const ros::NodeHandle &nh, const std::string &database, const std::string &collection)
void Insert(const std::string &name, const MsgType &msg)
bool Delete(const std::string &name)