forked from moveit/warehouse_ros_mongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.h
313 lines (266 loc) · 7.6 KB
/
metadata.h
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2008 Willow Garage, Inc.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
/**
* \file
*
* Define a couple of classes that wrap Mongo's BSON type
*
* \author Bhaskara Marthi
*/
#ifndef WAREHOUSE_ROS_MONGO_METADATA_H
#define WAREHOUSE_ROS_MONGO_METADATA_H
// We have to include this top-level include here because
// the mongo c++ library is not robust to reincludes
#ifdef __APPLE__
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#include <warehouse_ros/metadata.h>
#include <warehouse_ros_mongo/config.h>
#include <mongo/db/json.h>
namespace warehouse_ros_mongo
{
using mongo::BSONObj;
using mongo::BSONObjBuilder;
/// \brief Internal parent class
///
/// This allows the user to not have to deal with separate BSONObj and
/// BSONObj builder objects
class WrappedBSON : public BSONObj
{
public:
WrappedBSON() : BSONObj(), builder_(new BSONObjBuilder())
{
}
WrappedBSON(const WrappedBSON& other) : BSONObj(), builder_(other.builder_)
{
update();
}
WrappedBSON(const BSONObj& other) : BSONObj(), builder_(new BSONObjBuilder())
{
builder_->appendElements(other);
update();
}
WrappedBSON(const std::string& json) : BSONObj(), builder_(new BSONObjBuilder())
{
builder_->appendElements(mongo::fromjson(json.c_str()));
update();
}
protected:
std::shared_ptr<BSONObjBuilder> builder_;
void update()
{
BSONObj::operator=(builder_->asTempObj());
}
};
/// \brief Represents a query to the db
///
/// Usage:
/// Query q("foo", 42);
/// Query q2("bar", LT, 24); // bar less than 24
/// Templated so you can have different types of values
///
/// Or:
/// q = Query().append("foo", 42).append("bar", LT, 24);
class MongoQuery : public WrappedBSON, public warehouse_ros::Query
{
public:
MongoQuery() : WrappedBSON()
{
}
MongoQuery(const MongoQuery& other) : WrappedBSON(other)
{
}
MongoQuery(const BSONObj& other) : WrappedBSON(other)
{
}
void append(const std::string& name, const std::string& val)
{
*builder_ << name << val;
WrappedBSON::update();
}
void append(const std::string& name, const double val)
{
*builder_ << name << val;
WrappedBSON::update();
}
void append(const std::string& name, const int val)
{
*builder_ << name << val;
WrappedBSON::update();
}
void append(const std::string& name, const bool val)
{
*builder_ << name << val;
WrappedBSON::update();
}
void appendLT(const std::string& name, const double val)
{
*builder_ << name << mongo::LT << val;
WrappedBSON::update();
}
void appendLT(const std::string& name, const int val)
{
*builder_ << name << mongo::LT << val;
WrappedBSON::update();
}
void appendLTE(const std::string& name, const double val)
{
*builder_ << name << mongo::LTE << val;
WrappedBSON::update();
}
void appendLTE(const std::string& name, const int val)
{
*builder_ << name << mongo::LTE << val;
WrappedBSON::update();
}
void appendGT(const std::string& name, const double val)
{
*builder_ << name << mongo::GT << val;
WrappedBSON::update();
}
void appendGT(const std::string& name, const int val)
{
*builder_ << name << mongo::GT << val;
WrappedBSON::update();
}
void appendGTE(const std::string& name, const double val)
{
*builder_ << name << mongo::GTE << val;
WrappedBSON::update();
}
void appendGTE(const std::string& name, const int val)
{
*builder_ << name << mongo::GTE << val;
WrappedBSON::update();
}
void appendRange(const std::string& name, const double lower, const double upper)
{
*builder_ << name << mongo::GT << lower << mongo::LT << upper;
WrappedBSON::update();
}
void appendRange(const std::string& name, const int lower, const int upper)
{
*builder_ << name << mongo::GT << lower << mongo::LT << upper;
WrappedBSON::update();
}
void appendRangeInclusive(const std::string& name, const double lower, const double upper)
{
*builder_ << name << mongo::GTE << lower << mongo::LTE << upper;
WrappedBSON::update();
}
void appendRangeInclusive(const std::string& name, const int lower, const int upper)
{
*builder_ << name << mongo::GTE << lower << mongo::LTE << upper;
WrappedBSON::update();
}
};
/// \brief Represents metadata attached to a message. Automatically
/// includes a unique id and creation time.
///
/// Usage:
///
/// Metadata m("x", 24, "y", 42);
/// (templated so you can use varying number of fields, numeric or string values)
///
/// Or:
/// m = Metadata().append("x", 24).append("name", "foo");
class MongoMetadata : public warehouse_ros::Metadata, public WrappedBSON
{
public:
MongoMetadata() : WrappedBSON()
{
initialize();
}
MongoMetadata(const std::string& json) : WrappedBSON(json)
{
}
MongoMetadata(const MongoMetadata& other) : WrappedBSON(other)
{
}
MongoMetadata(const BSONObj& other) : WrappedBSON(other)
{
}
void append(const std::string& name, const std::string& val)
{
*builder_ << name << val;
WrappedBSON::update();
}
void append(const std::string& name, const double val)
{
*builder_ << name << val;
WrappedBSON::update();
}
void append(const std::string& name, const int val)
{
*builder_ << name << val;
WrappedBSON::update();
}
void append(const std::string& name, const bool val)
{
*builder_ << name << val;
WrappedBSON::update();
}
std::string lookupString(const std::string& name) const
{
return getStringField(name.c_str());
}
double lookupDouble(const std::string& name) const
{
double d;
(*this)[name.c_str()].Val(d);
return d;
}
int lookupInt(const std::string& name) const
{
return getIntField(name.c_str());
}
bool lookupBool(const std::string& name) const
{
return getBoolField(name.c_str());
}
bool lookupField(const std::string& name) const
{
return BSONObj::hasField(name.c_str());
}
std::set<std::string> lookupFieldNames() const
{
std::set<std::string> fields;
BSONObj::getFieldNames(fields);
return fields;
}
private:
void initialize()
{
builder_->genOID();
WrappedBSON::update();
}
};
} // namespace warehouse_ros_mongo
#endif // include guard