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>
53 void MessageCollection<M>::insert(const M& msg, Metadata::Ptr metadata)
54 {
55  if (!md5sum_matches_)
56  throw Md5SumException("Cannot insert additional elements.");
57 
58  metadata->append("creation_time", ros::Time::now().toSec());
59 
61  size_t serial_size = ros::serialization::serializationLength(msg);
62  boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
63  ros::serialization::OStream stream(buffer.get(), serial_size);
64  ros::serialization::serialize(stream, msg);
65  char* data = (char*)buffer.get();
66 
67  collection_->insert(data, serial_size, metadata);
68 }
69 
70 template <class M>
72  const std::string& sort_by, bool ascending) const
73 {
74  if (!md5sum_matches_ && !metadata_only)
75  throw Md5SumException("Can only query metadata.");
76 
77  ResultIteratorHelper::Ptr results = collection_->query(query, sort_by, ascending);
78  return typename QueryResults<M>::range_t(ResultIterator<M>(results, metadata_only), ResultIterator<M>());
79 }
80 
81 template <class M>
82 std::vector<typename MessageWithMetadata<M>::ConstPtr> MessageCollection<M>::queryList(Query::ConstPtr query,
83  bool metadata_only,
84  const std::string& sort_by,
85  bool ascending) const
86 {
87  typename QueryResults<M>::range_t res = this->query(query, metadata_only, sort_by, ascending);
88  return std::vector<typename MessageWithMetadata<M>::ConstPtr>(res.first, res.second);
89 }
90 
91 template <class M>
93  const bool metadata_only) const
94 {
95  typename QueryResults<M>::range_t res = this->query(query, metadata_only);
96  if (res.first == res.second)
97  throw NoMatchingMessageException(collection_->collectionName());
98  return *res.first;
99 }
100 
101 template <class M>
103 {
104  return collection_->removeMessages(query);
105 }
106 
107 template <class M>
109 {
110  collection_->modifyMetadata(q, m);
111 }
112 
113 template <class M>
115 {
116  return collection_->count();
117 }
118 
119 template <class M>
121 {
122  return md5sum_matches_;
123 }
124 
125 template <class M>
127 {
128  return collection_->createQuery();
129 }
130 
131 template <class M>
133 {
134  return collection_->createMetadata();
135 }
136 
137 } // namespace warehouse_ros
warehouse_ros::Md5SumException
Different md5 sum for messages.
Definition: exceptions.h:77
ros::serialization::OStream
warehouse_ros::MessageCollection::removeMessages
unsigned removeMessages(Query::ConstPtr query)
Remove messages matching query.
Definition: message_collection_impl.hpp:102
boost::shared_ptr< MessageCollectionHelper >
warehouse_ros::MessageCollection::MessageCollection
MessageCollection()=default
Default constructor.
warehouse_ros::MessageCollection::createQuery
Query::Ptr createQuery() const
Definition: message_collection_impl.hpp:126
warehouse_ros::MessageCollection::count
unsigned count()
Count messages in collection.
Definition: message_collection_impl.hpp:114
ros::serialization::serializationLength
uint32_t serializationLength(const boost::array< T, N > &t)
ros::message_traits::DataType
warehouse_ros::ResultIterator
Definition: query_results.h:61
warehouse_ros::MessageCollection
Definition: message_collection.h:68
warehouse_ros::MessageCollection::createMetadata
Metadata::Ptr createMetadata() const
Definition: message_collection_impl.hpp:132
ros::message_traits::MD5Sum::value
static const char * value()
warehouse_ros::QueryResults::range_t
std::pair< ResultIterator< M >, ResultIterator< M > > range_t
Definition: query_results.h:87
boost::shared_array< uint8_t >
ros::message_traits::MD5Sum
datatype
const char * datatype()
warehouse_ros::NoMatchingMessageException
Couldn't find matching message in collection.
Definition: exceptions.h:59
warehouse_ros
Definition: database_connection.h:44
warehouse_ros::MessageCollection::modifyMetadata
void modifyMetadata(Query::ConstPtr q, Metadata::ConstPtr m)
Modify metadata Find message matching q and update its metadata using m In other words,...
Definition: message_collection_impl.hpp:108
ros::serialization::serialize
void serialize(Stream &stream, const boost::array< T, N > &t)
ros::Time::now
static Time now()


warehouse_ros
Author(s): Bhaskara Marthi , Connor Brew
autogenerated on Wed Oct 16 2024 02:42:07