Recovery.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2010-2017, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
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  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include <rtabmap/core/Recovery.h>
29 #include <rtabmap/core/Rtabmap.h>
30 #include <rtabmap/core/DBDriver.h>
31 #include <rtabmap/core/DBReader.h>
32 #include <rtabmap/core/Graph.h>
34 #include <rtabmap/utilite/UFile.h>
35 #include <rtabmap/utilite/UStl.h>
36 
37 namespace rtabmap {
38 
40  const std::string & corruptedDatabase,
41  bool keepCorruptedDatabase,
42  std::string * errorMsg,
43  ProgressState * progressState)
44 {
45  UDEBUG("Recovering \"%s\"", corruptedDatabase.c_str());
46 
47  std::string databasePath = uReplaceChar(corruptedDatabase, '~', UDirectory::homeDir());
48  if(!UFile::exists(databasePath))
49  {
50  if(errorMsg)
51  *errorMsg = uFormat("File \"%s\" doesn't exist!", databasePath.c_str());
52  return false;
53  }
54 
55  std::string backupPath;
56  if(UFile::getExtension(databasePath).compare("db") != 0)
57  {
58  if(errorMsg)
59  *errorMsg = uFormat("File \"%s\" is not a database (*.db)!", databasePath.c_str());
60  return false;
61  }
62  std::list<std::string> strList = uSplit(databasePath, '.');
63  strList.pop_back();
64  backupPath = uJoin(strList, ".") + ".backup.db";
65  if(UFile::exists(backupPath))
66  {
67  if(errorMsg)
68  *errorMsg = uFormat("Backup file \"%s\" already exists!", backupPath.c_str());
69  return false;
70  }
71 
72  DBDriver * dbDriver = DBDriver::create();
73  if(!dbDriver->openConnection(databasePath, false))
74  {
75  if(errorMsg)
76  *errorMsg = uFormat("Failed opening database!");
77  delete dbDriver;
78  return false;
79  }
80 
81  ParametersMap parameters = dbDriver->getLastParameters();
82  if(parameters.empty())
83  {
84  if(errorMsg)
85  *errorMsg = uFormat("Failed getting parameters from database, recovery cannot be done.");
86  dbDriver->closeConnection(false);
87  delete dbDriver;
88  return false;
89  }
90  std::set<int> ids;
91  dbDriver->getAllNodeIds(ids);
92  if(ids.empty())
93  {
94  if(errorMsg)
95  *errorMsg = uFormat("Input database doesn't have any nodes saved in it.");
96  dbDriver->closeConnection(false);
97  delete dbDriver;
98  return false;
99  }
100  if(progressState)
101  progressState->callback(uFormat("Found %d nodes to recover.", (int)ids.size()));
102 
103  //Detect if the database is corrupted
104  std::multimap<int, Link> links;
105  dbDriver->getAllLinks(links, true);
106  bool corrupted = false;
107  for(std::multimap<int, Link>::iterator iter=links.begin(); iter!=links.end(); ++iter)
108  {
109  if(iter->second.type() == Link::kNeighbor &&
110  graph::findLink(links, iter->second.to(), iter->second.from(), false) == links.end())
111  {
112  corrupted = true;
113  break;
114  }
115  }
116 
117  if(progressState)
118  {
119  if(corrupted)
120  progressState->callback("Database is indeed corrupted, found one or more neighbor links missing.");
121  else
122  progressState->callback("Database doesn't seem to be corrupted, still recovering it.");
123  }
124 
125  dbDriver->closeConnection(false);
126  delete dbDriver;
127 
128  if(progressState)
129  progressState->callback(uFormat("Renaming \"%s\" to \"%s\"...", UFile::getName(databasePath).c_str(), UFile::getName(backupPath).c_str()));
130  if(UFile::rename(databasePath, backupPath) != 0)
131  {
132  if(errorMsg)
133  *errorMsg = uFormat("Failed renaming database file from \"%s\" to \"%s\". Is it opened by another app?", UFile::getName(databasePath).c_str(), UFile::getName(backupPath).c_str());
134  return false;
135  }
137  rtabmap.init(parameters, databasePath);
138 
139  bool rgbdEnabled = Parameters::defaultRGBDEnabled();
140  Parameters::parse(parameters, Parameters::kRGBDEnabled(), rgbdEnabled);
141  bool odometryIgnored = !rgbdEnabled;
142  DBReader dbReader(backupPath, 0, odometryIgnored);
143  dbReader.init();
144 
145  CameraInfo info;
146  SensorData data = dbReader.takeImage(&info);
147  int processed = 0;
148  if(progressState)
149  progressState->callback(uFormat("Recovering data of \"%s\"...", backupPath.c_str()));
150  while(data.isValid() && (progressState==0 || !progressState->isCanceled()))
151  {
152  std::string status;
153  if(!odometryIgnored && info.odomPose.isNull())
154  {
155  status = uFormat("Skipping node %d as it doesn't have odometry pose set.", data.id());
156  }
157  else
158  {
159  if(!odometryIgnored && !info.odomCovariance.empty() && info.odomCovariance.at<double>(0,0)>=9999)
160  {
161  status = uFormat("High variance detected, triggering a new map...");
162  rtabmap.triggerNewMap();
163  }
164  if(!rtabmap.process(data, info.odomPose, info.odomCovariance, info.odomVelocity))
165  {
166  status = uFormat("Failed processing node %d.", data.id());
167  }
168  }
169  if(status.empty())
170  {
171  if(progressState)
172  progressState->callback(status);
173  }
174 
175  data = dbReader.takeImage(&info);
176 
177  if(progressState)
178  progressState->callback(uFormat("Processed %d/%d nodes...", ++processed, (int)ids.size()));
179  }
180 
181  if(progressState)
182  {
183  if(progressState->isCanceled())
184  {
185  rtabmap.close(false);
186  if(errorMsg)
187  *errorMsg = uFormat("Recovery canceled, renaming back \"%s\" to \"%s\".", backupPath.c_str(), databasePath.c_str());
188 
189  // put back the file as before
190  UFile::erase(databasePath);
191  UFile::rename(backupPath, databasePath);
192  return false;
193  }
194  }
195 
196  if(progressState)
197  progressState->callback(uFormat("Closing database \"%s\"...", databasePath.c_str()));
198  rtabmap.close(true);
199  if(progressState)
200  progressState->callback(uFormat("Closing database \"%s\"... done!", databasePath.c_str()));
201 
202  if(!keepCorruptedDatabase)
203  {
204  UFile::erase(backupPath);
205  }
206 
207  return true;
208 }
209 
210 }
211 
212 
213 
214 
static std::string homeDir()
Definition: UDirectory.cpp:355
static bool parse(const ParametersMap &parameters, const std::string &key, bool &value)
Definition: Parameters.cpp:446
cv::Mat odomCovariance
Definition: CameraInfo.h:69
std::string getName()
Definition: UFile.h:135
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")
Definition: DBReader.cpp:100
void getAllNodeIds(std::set< int > &ids, bool ignoreChildren=false, bool ignoreBadSignatures=false) const
Definition: DBDriver.cpp:813
void getAllLinks(std::multimap< int, Link > &links, bool ignoreNullLinks=true) const
Definition: DBDriver.cpp:852
static int erase(const std::string &filePath)
Definition: UFile.cpp:58
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:41
std::vector< float > odomVelocity
Definition: CameraInfo.h:70
std::string getExtension()
Definition: UFile.h:140
int triggerNewMap()
Definition: Rtabmap.cpp:685
std::list< std::string > uSplit(const std::string &str, char separator= ' ')
Definition: UStl.h:566
bool openConnection(const std::string &url, bool overwritten=false)
Definition: DBDriver.cpp:86
bool isValid() const
Definition: SensorData.h:134
SensorData takeImage(CameraInfo *info=0)
Definition: Camera.cpp:67
Wrappers of STL for convenient functions.
virtual bool callback(const std::string &msg) const
Definition: ProgressState.h:39
std::string uJoin(const std::list< std::string > &strings, const std::string &separator="")
Definition: UStl.h:603
void close(bool databaseSaved=true, const std::string &ouputDatabasePath="")
Definition: Rtabmap.cpp:348
bool isNull() const
Definition: Transform.cpp:107
void closeConnection(bool save=true, const std::string &outputUrl="")
Definition: DBDriver.cpp:64
bool process(const SensorData &data, Transform odomPose, const cv::Mat &odomCovariance=cv::Mat::eye(6, 6, CV_64FC1), const std::vector< float > &odomVelocity=std::vector< float >(), const std::map< std::string, float > &externalStats=std::map< std::string, float >())
Main loop of rtabmap.
Definition: Rtabmap.cpp:897
std::string UTILITE_EXP uReplaceChar(const std::string &str, char before, char after)
Definition: UConversion.cpp:32
int id() const
Definition: SensorData.h:152
void init(const ParametersMap &parameters, const std::string &databasePath="")
Definition: Rtabmap.cpp:282
static DBDriver * create(const ParametersMap &parameters=ParametersMap())
Definition: DBDriver.cpp:41
#define UDEBUG(...)
std::multimap< int, Link >::iterator RTABMAP_EXP findLink(std::multimap< int, Link > &links, int from, int to, bool checkBothWays=true)
Definition: Graph.cpp:825
bool exists()
Definition: UFile.h:104
bool RTABMAP_EXP databaseRecovery(const std::string &corruptedDatabase, bool keepCorruptedDatabase=true, std::string *errorMsg=0, ProgressState *progressState=0)
Definition: Recovery.cpp:39
bool isCanceled() const
Definition: ProgressState.h:51
ParametersMap getLastParameters() const
Definition: DBDriver.cpp:239
static int rename(const std::string &oldFilePath, const std::string &newFilePath)
Definition: UFile.cpp:63
Transform odomPose
Definition: CameraInfo.h:68
std::string UTILITE_EXP uFormat(const char *fmt,...)


rtabmap
Author(s): Mathieu Labbe
autogenerated on Wed Jun 5 2019 22:41:32