message_collection_impl.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
40 namespace warehouse_ros
41 {
42 template <class M>
44 {
45  typedef typename ros::message_traits::DataType<M> DataType;
46  const std::string datatype = DataType().value();
47  typedef typename ros::message_traits::MD5Sum<M> Md5;
48  const std::string md5 = Md5().value();
49  md5sum_matches_ = collection_->initialize(datatype, md5);
50 }
51 
52 template <class M>
55 {
56 }
57 
58 template <class M>
60 {
61 }
62 
63 template <class M>
65 {
66  collection_ = other.collection_;
68  return *this;
69 }
70 
71 template <class M>
72 void MessageCollection<M>::insert(const M& msg, Metadata::Ptr metadata)
73 {
74  if (!md5sum_matches_)
75  throw Md5SumException("Cannot insert additional elements.");
76 
77  metadata->append("creation_time", ros::Time::now().toSec());
78 
80  size_t serial_size = ros::serialization::serializationLength(msg);
81  boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
82  ros::serialization::OStream stream(buffer.get(), serial_size);
83  ros::serialization::serialize(stream, msg);
84  char* data = (char*)buffer.get();
85 
86  collection_->insert(data, serial_size, metadata);
87 }
88 
89 template <class M>
91  const std::string& sort_by, bool ascending) const
92 {
93  if (!md5sum_matches_ && !metadata_only)
94  throw Md5SumException("Can only query metadata.");
95 
96  ResultIteratorHelper::Ptr results = collection_->query(query, sort_by, ascending);
97  return typename QueryResults<M>::range_t(ResultIterator<M>(results, metadata_only), ResultIterator<M>());
98 }
99 
100 template <class M>
101 std::vector<typename MessageWithMetadata<M>::ConstPtr> MessageCollection<M>::queryList(Query::ConstPtr query,
102  bool metadata_only,
103  const std::string& sort_by,
104  bool ascending) const
105 {
106  typename QueryResults<M>::range_t res = this->query(query, metadata_only, sort_by, ascending);
107  return std::vector<typename MessageWithMetadata<M>::ConstPtr>(res.first, res.second);
108 }
109 
110 template <class M>
112  const bool metadata_only) const
113 {
114  typename QueryResults<M>::range_t res = this->query(query, metadata_only);
115  if (res.first == res.second)
116  throw NoMatchingMessageException(collection_->collectionName());
117  return *res.first;
118 }
119 
120 template <class M>
122 {
123  return collection_->removeMessages(query);
124 }
125 
126 template <class M>
128 {
129  collection_->modifyMetadata(q, m);
130 }
131 
132 template <class M>
134 {
135  return collection_->count();
136 }
137 
138 template <class M>
140 {
141  return md5sum_matches_;
142 }
143 
144 template <class M>
146 {
147  return collection_->createQuery();
148 }
149 
150 template <class M>
152 {
153  return collection_->createMetadata();
154 }
155 
156 } // namespace warehouse_ros
Different md5 sum for messages.
Definition: exceptions.h:77
void insert(const M &msg, Metadata::Ptr metadata)
Insert a ROS message, together with some optional metadata, into the db.
unsigned removeMessages(Query::ConstPtr query)
Remove messages matching query.
QueryResults< M >::range_t query(Query::ConstPtr query, bool metadata_only=false, const std::string &sort_by="", bool ascending=true) const
MessageCollection(MessageCollectionHelper::Ptr collection)
Will connect to given database and collection. Collection is created if it doesn&#39;t exist...
std::vector< typename MessageWithMetadata< M >::ConstPtr > queryList(Query::ConstPtr query, bool metadata_only=false, const std::string &sort_by="", bool ascending=true) const
Convenience function that calls queryResult and puts the results into a vector.
std::pair< ResultIterator< M >, ResultIterator< M > > range_t
Definition: query_results.h:94
bool md5SumMatches() const
Check if the md5 sum of the messages previously stored in the database matches that of the compiled m...
unsigned count()
Count messages in collection.
MessageCollection & operator=(const MessageCollection &other)
void serialize(Stream &stream, const T &t)
MessageCollectionHelper::Ptr collection_
static const char * value()
Couldn&#39;t find matching message in collection.
Definition: exceptions.h:59
void modifyMetadata(Query::ConstPtr q, Metadata::ConstPtr m)
Modify metadata Find message matching q and update its metadata using m In other words, overwrite keys in the message using m, but keep keys that don&#39;t occur in m.
uint32_t serializationLength(const T &t)
static Time now()
MessageWithMetadata< M >::ConstPtr findOne(Query::ConstPtr query, bool metadata_only=false) const
Convenience function that returns a single matching result for the query.


warehouse_ros
Author(s): Bhaskara Marthi , Connor Brew
autogenerated on Mon Apr 26 2021 02:23:26