Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00040 namespace warehouse_ros
00041 {
00042
00043 template<class M>
00044 MessageCollection<M>::MessageCollection(MessageCollectionHelper::Ptr collection) :
00045 collection_(collection)
00046 {
00047 typedef typename ros::message_traits::DataType<M> DataType;
00048 const std::string datatype = DataType().value();
00049 typedef typename ros::message_traits::MD5Sum<M> Md5;
00050 const std::string md5 = Md5().value();
00051 md5sum_matches_ = collection_->initialize(datatype, md5);
00052 }
00053
00054 template<class M>
00055 MessageCollection<M>::MessageCollection(const MessageCollection<M>& other) :
00056 collection_(other.collection_), md5sum_matches_(other.md5sum_matches_)
00057 {
00058 }
00059
00060 template<class M>
00061 MessageCollection<M>::~MessageCollection()
00062 {
00063 }
00064
00065 template<class M>
00066 MessageCollection<M>& MessageCollection<M>::operator=(const MessageCollection& other)
00067 {
00068 collection_ = other.collection_;
00069 md5sum_matches_ = other.md5sum_matches_;
00070 return *this;
00071 }
00072
00073 template<class M>
00074 void MessageCollection<M>::insert(const M& msg, Metadata::Ptr metadata)
00075 {
00076 if (!md5sum_matches_)
00077 throw Md5SumException("Cannot insert additional elements.");
00078
00079 metadata->append("creation_time", ros::Time::now().toSec());
00080
00082 size_t serial_size = ros::serialization::serializationLength(msg);
00083 boost::shared_array<uint8_t> buffer(new uint8_t[serial_size]);
00084 ros::serialization::OStream stream(buffer.get(), serial_size);
00085 ros::serialization::serialize(stream, msg);
00086 char* data = (char*)buffer.get();
00087
00088 collection_->insert(data, serial_size, metadata);
00089 }
00090
00091 template<class M>
00092 typename QueryResults<M>::range_t MessageCollection<M>::query(Query::ConstPtr query, bool metadata_only,
00093 const std::string& sort_by, bool ascending) const
00094 {
00095 if (!md5sum_matches_ && !metadata_only)
00096 throw Md5SumException("Can only query metadata.");
00097
00098 ResultIteratorHelper::Ptr results = collection_->query(query, sort_by, ascending);
00099 return typename QueryResults<M>::range_t(ResultIterator<M>(results, metadata_only), ResultIterator<M>());
00100 }
00101
00102 template<class M>
00103 std::vector<typename MessageWithMetadata<M>::ConstPtr> MessageCollection<M>::queryList(Query::ConstPtr query,
00104 bool metadata_only,
00105 const std::string& sort_by,
00106 bool ascending) const
00107 {
00108 typename QueryResults<M>::range_t res = this->query(query, metadata_only, sort_by, ascending);
00109 return std::vector<typename MessageWithMetadata<M>::ConstPtr>(res.first, res.second);
00110 }
00111
00112 template<class M>
00113 typename MessageWithMetadata<M>::ConstPtr MessageCollection<M>::findOne(Query::ConstPtr query,
00114 const bool metadata_only) const
00115 {
00116 typename QueryResults<M>::range_t res = this->query(query, metadata_only);
00117 if (res.first == res.second)
00118 throw NoMatchingMessageException(collection_->collectionName());
00119 return *res.first;
00120 }
00121
00122 template<class M>
00123 unsigned MessageCollection<M>::removeMessages(Query::ConstPtr query)
00124 {
00125 return collection_->removeMessages(query);
00126 }
00127
00128 template<class M>
00129 void MessageCollection<M>::modifyMetadata(Query::ConstPtr q, Metadata::ConstPtr m)
00130 {
00131 collection_->modifyMetadata(q, m);
00132 }
00133
00134 template<class M>
00135 unsigned MessageCollection<M>::count()
00136 {
00137 return collection_->count();
00138 }
00139
00140 template<class M>
00141 bool MessageCollection<M>::md5SumMatches() const
00142 {
00143 return md5sum_matches_;
00144 }
00145
00146 template<class M>
00147 Query::Ptr MessageCollection<M>::createQuery() const
00148 {
00149 return collection_->createQuery();
00150 }
00151
00152 template<class M>
00153 Metadata::Ptr MessageCollection<M>::createMetadata() const
00154 {
00155 return collection_->createMetadata();
00156 }
00157
00158 }