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  }
136 
137  bool incrementalMemory = true;
138  Parameters::parse(parameters, Parameters::kMemIncrementalMemory(), incrementalMemory);
139  if(!incrementalMemory)
140  {
141  if(progressState)
142  {
143  progressState->callback("Database is in localization mode, setting it to mapping mode to recover...");
144  }
145  uInsert(parameters, ParametersPair(Parameters::kMemIncrementalMemory(), "true"));
146  }
147 
149  rtabmap.init(parameters, databasePath);
150 
151  bool rgbdEnabled = Parameters::defaultRGBDEnabled();
152  Parameters::parse(parameters, Parameters::kRGBDEnabled(), rgbdEnabled);
153  bool odometryIgnored = !rgbdEnabled;
154  DBReader dbReader(backupPath, 0, odometryIgnored);
155  dbReader.init();
156 
157  CameraInfo info;
158  SensorData data = dbReader.takeImage(&info);
159  int processed = 0;
160  if(progressState)
161  progressState->callback(uFormat("Recovering data of \"%s\"...", backupPath.c_str()));
162  while(data.isValid() && (progressState==0 || !progressState->isCanceled()))
163  {
164  std::string status;
165  if(!odometryIgnored && info.odomPose.isNull())
166  {
167  status = uFormat("Skipping node %d as it doesn't have odometry pose set.", data.id());
168  }
169  else
170  {
171  if(!odometryIgnored && !info.odomCovariance.empty() && info.odomCovariance.at<double>(0,0)>=9999)
172  {
173  status = uFormat("High variance detected, triggering a new map...");
174  rtabmap.triggerNewMap();
175  }
176  if(!rtabmap.process(data, info.odomPose, info.odomCovariance, info.odomVelocity))
177  {
178  status = uFormat("Failed processing node %d.", data.id());
179  }
180  }
181  if(status.empty())
182  {
183  if(progressState)
184  progressState->callback(status);
185  }
186 
187  data = dbReader.takeImage(&info);
188 
189  if(progressState)
190  progressState->callback(uFormat("Processed %d/%d nodes...", ++processed, (int)ids.size()));
191  }
192 
193  if(progressState)
194  {
195  if(progressState->isCanceled())
196  {
197  rtabmap.close(false);
198  if(errorMsg)
199  *errorMsg = uFormat("Recovery canceled, renaming back \"%s\" to \"%s\".", backupPath.c_str(), databasePath.c_str());
200 
201  // put back the file as before
202  UFile::erase(databasePath);
203  UFile::rename(backupPath, databasePath);
204  return false;
205  }
206  }
207 
208  if(progressState)
209  progressState->callback(uFormat("Closing database \"%s\"...", databasePath.c_str()));
210  rtabmap.close(true);
211  if(progressState)
212  progressState->callback(uFormat("Closing database \"%s\"... done!", databasePath.c_str()));
213 
214  if(!keepCorruptedDatabase)
215  {
216  UFile::erase(backupPath);
217  }
218 
219  return true;
220 }
221 
222 }
223 
224 
225 
226 
static std::string homeDir()
Definition: UDirectory.cpp:355
static bool parse(const ParametersMap &parameters, const std::string &key, bool &value)
Definition: Parameters.cpp:476
cv::Mat odomCovariance
Definition: CameraInfo.h:70
std::string getName()
Definition: UFile.h:135
virtual bool init(const std::string &calibrationFolder=".", const std::string &cameraName="")
Definition: DBReader.cpp:112
void getAllNodeIds(std::set< int > &ids, bool ignoreChildren=false, bool ignoreBadSignatures=false) const
Definition: DBDriver.cpp:876
std::pair< std::string, std::string > ParametersPair
Definition: Parameters.h:44
static int erase(const std::string &filePath)
Definition: UFile.cpp:58
std::map< std::string, std::string > ParametersMap
Definition: Parameters.h:43
void init(const ParametersMap &parameters, const std::string &databasePath="", bool loadDatabaseParameters=false)
Definition: Rtabmap.cpp:292
std::vector< float > odomVelocity
Definition: CameraInfo.h:71
std::string getExtension()
Definition: UFile.h:140
int triggerNewMap()
Definition: Rtabmap.cpp:737
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:137
SensorData takeImage(CameraInfo *info=0)
Definition: Camera.cpp:72
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:392
bool isNull() const
Definition: Transform.cpp:107
void getAllLinks(std::multimap< int, Link > &links, bool ignoreNullLinks=true, bool withLandmarks=false) const
Definition: DBDriver.cpp:915
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:992
std::string UTILITE_EXP uReplaceChar(const std::string &str, char before, char after)
Definition: UConversion.cpp:32
int id() const
Definition: SensorData.h:155
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:969
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:69
std::string UTILITE_EXP uFormat(const char *fmt,...)
void uInsert(std::map< K, V > &map, const std::pair< K, V > &pair)
Definition: UStl.h:443


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:35:00