database_connection.cpp
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 
39 #include <mongo/client/init.h>
42 
43 namespace warehouse_ros_mongo
44 {
45 using std::string;
46 
47 MongoDatabaseConnection::MongoDatabaseConnection() : host_("localhost"), port_(27017), timeout_(60.0)
48 {
49  static bool initialized = false; // Initialize only once
50  // libmongoclient 1.1.2 (in Bionic) doesn't require this anymore
51  if (!initialized)
52  {
53  initialized = true;
54  mongo::client::initialize();
55  }
56 }
57 
58 bool MongoDatabaseConnection::setParams(const string& host, unsigned port, float timeout)
59 {
60  host_ = host;
61  port_ = port;
62  timeout_ = timeout;
63  return true;
64 }
65 
67 {
68  timeout_ = timeout;
69  return true;
70 }
71 
73 {
74  const string db_address = (boost::format("%1%:%2%") % host_ % port_).str();
76 
77  while (ros::ok() && ros::WallTime::now() < end)
78  {
79  conn_.reset(new mongo::DBClientConnection());
80  try
81  {
82  ROS_DEBUG_STREAM_NAMED("db_connect", "Attempting to connect to MongoDB at " << db_address);
83  conn_->connect(db_address);
84  if (!conn_->isFailed())
85  break;
86  }
87  catch (mongo::ConnectException& e)
88  {
89  ros::Duration(1.0).sleep();
90  }
91  }
92  if (!conn_ || conn_->isFailed())
93  {
94  ROS_ERROR_STREAM("Unable to connect to the database at '"
95  << db_address << "'. If you just created the database, it could take a while for initial setup.");
96  return false;
97  }
98 
99  ROS_DEBUG_STREAM_NAMED("db_connect", "Successfully connected to the DB");
100  return true;
101 }
102 
104 {
105  return ((bool)conn_ && !conn_->isFailed());
106 }
107 
108 void MongoDatabaseConnection::dropDatabase(const string& db_name)
109 {
110  if (!isConnected())
111  throw warehouse_ros::DbConnectException("Cannot drop database");
112  conn_->dropDatabase(db_name);
113 }
114 
115 string MongoDatabaseConnection::messageType(const string& db, const string& coll)
116 {
117  if (!isConnected())
118  throw warehouse_ros::DbConnectException("Cannot look up metatable.");
119  const string meta_ns = db + ".ros_message_collections";
120  CursorPtr cursor = conn_->query(meta_ns, BSON("name" << coll));
121  mongo::BSONObj obj = cursor->next();
122  return obj.getStringField("type");
123 }
124 
126  const std::string& collection_name)
127 {
128  return typename MessageCollectionHelper::Ptr(new MongoMessageCollection(conn_, db_name, collection_name));
129 }
130 
131 } // namespace warehouse_ros_mongo
132 
std::unique_ptr< mongo::DBClientCursor > CursorPtr
Definition: query_results.h:50
std::string messageType(const std::string &db_name, const std::string &collection_name)
#define ROS_DEBUG_STREAM_NAMED(name, args)
bool setParams(const std::string &host, unsigned port, float timeout)
MessageCollectionHelper::Ptr openCollectionHelper(const std::string &db_name, const std::string &collection_name)
boost::shared_ptr< MessageCollectionHelper > Ptr
ROSCPP_DECL bool ok()
void dropDatabase(const std::string &db_name)
static WallTime now()
std::shared_ptr< mongo::DBClientConnection > conn_
bool sleep() const
#define ROS_ERROR_STREAM(args)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)


warehouse_ros_mongo
Author(s): Bhaskara Marthi , Connor Brew
autogenerated on Sat Apr 2 2022 02:29:13