| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SQL_RECOVERY_H_ | 5 #ifndef SQL_RECOVERY_H_ |
| 6 #define SQL_RECOVERY_H_ | 6 #define SQL_RECOVERY_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <memory> |
| 11 |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "sql/connection.h" | 13 #include "sql/connection.h" |
| 12 | 14 |
| 13 namespace base { | 15 namespace base { |
| 14 class FilePath; | 16 class FilePath; |
| 15 } | 17 } |
| 16 | 18 |
| 17 namespace sql { | 19 namespace sql { |
| 18 | 20 |
| 19 // Recovery module for sql/. The basic idea is to create a fresh | 21 // Recovery module for sql/. The basic idea is to create a fresh |
| 20 // database and populate it with the recovered contents of the | 22 // database and populate it with the recovered contents of the |
| 21 // original database. If recovery is successful, the recovered | 23 // original database. If recovery is successful, the recovered |
| 22 // database is backed up over the original database. If recovery is | 24 // database is backed up over the original database. If recovery is |
| 23 // not successful, the original database is razed. In either case, | 25 // not successful, the original database is razed. In either case, |
| 24 // the original handle is poisoned so that operations on the stack do | 26 // the original handle is poisoned so that operations on the stack do |
| 25 // not accidentally disrupt the restored data. | 27 // not accidentally disrupt the restored data. |
| 26 // | 28 // |
| 27 // { | 29 // { |
| 28 // scoped_ptr<sql::Recovery> r = | 30 // std::unique_ptr<sql::Recovery> r = |
| 29 // sql::Recovery::Begin(orig_db, orig_db_path); | 31 // sql::Recovery::Begin(orig_db, orig_db_path); |
| 30 // if (r) { | 32 // if (r) { |
| 31 // // Create the schema to recover to. On failure, clear the | 33 // // Create the schema to recover to. On failure, clear the |
| 32 // // database. | 34 // // database. |
| 33 // if (!r.db()->Execute(kCreateSchemaSql)) { | 35 // if (!r.db()->Execute(kCreateSchemaSql)) { |
| 34 // sql::Recovery::Unrecoverable(std::move(r)); | 36 // sql::Recovery::Unrecoverable(std::move(r)); |
| 35 // return; | 37 // return; |
| 36 // } | 38 // } |
| 37 // | 39 // |
| 38 // // Recover data in "mytable". | 40 // // Recover data in "mytable". |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // and attach the existing database to it at "corrupt". To prevent | 75 // and attach the existing database to it at "corrupt". To prevent |
| 74 // deadlock, all transactions on |connection| are rolled back. | 76 // deadlock, all transactions on |connection| are rolled back. |
| 75 // | 77 // |
| 76 // Returns NULL in case of failure, with no cleanup done on the | 78 // Returns NULL in case of failure, with no cleanup done on the |
| 77 // original connection (except for breaking the transactions). The | 79 // original connection (except for breaking the transactions). The |
| 78 // caller should Raze() or otherwise cleanup as appropriate. | 80 // caller should Raze() or otherwise cleanup as appropriate. |
| 79 // | 81 // |
| 80 // TODO(shess): Later versions of SQLite allow extracting the path | 82 // TODO(shess): Later versions of SQLite allow extracting the path |
| 81 // from the connection. | 83 // from the connection. |
| 82 // TODO(shess): Allow specifying the connection point? | 84 // TODO(shess): Allow specifying the connection point? |
| 83 static scoped_ptr<Recovery> Begin( | 85 static std::unique_ptr<Recovery> Begin(Connection* connection, |
| 84 Connection* connection, | 86 const base::FilePath& db_path) |
| 85 const base::FilePath& db_path) WARN_UNUSED_RESULT; | 87 WARN_UNUSED_RESULT; |
| 86 | 88 |
| 87 // Mark recovery completed by replicating the recovery database over | 89 // Mark recovery completed by replicating the recovery database over |
| 88 // the original database, then closing the recovery database. The | 90 // the original database, then closing the recovery database. The |
| 89 // original database handle is poisoned, causing future calls | 91 // original database handle is poisoned, causing future calls |
| 90 // against it to fail. | 92 // against it to fail. |
| 91 // | 93 // |
| 92 // If Recovered() is not called, the destructor will call | 94 // If Recovered() is not called, the destructor will call |
| 93 // Unrecoverable(). | 95 // Unrecoverable(). |
| 94 // | 96 // |
| 95 // TODO(shess): At this time, this function can fail while leaving | 97 // TODO(shess): At this time, this function can fail while leaving |
| 96 // the original database intact. Figure out which failure cases | 98 // the original database intact. Figure out which failure cases |
| 97 // should go to RazeAndClose() instead. | 99 // should go to RazeAndClose() instead. |
| 98 static bool Recovered(scoped_ptr<Recovery> r) WARN_UNUSED_RESULT; | 100 static bool Recovered(std::unique_ptr<Recovery> r) WARN_UNUSED_RESULT; |
| 99 | 101 |
| 100 // Indicate that the database is unrecoverable. The original | 102 // Indicate that the database is unrecoverable. The original |
| 101 // database is razed, and the handle poisoned. | 103 // database is razed, and the handle poisoned. |
| 102 static void Unrecoverable(scoped_ptr<Recovery> r); | 104 static void Unrecoverable(std::unique_ptr<Recovery> r); |
| 103 | 105 |
| 104 // When initially developing recovery code, sometimes the possible | 106 // When initially developing recovery code, sometimes the possible |
| 105 // database states are not well-understood without further | 107 // database states are not well-understood without further |
| 106 // diagnostics. Abandon recovery but do not raze the original | 108 // diagnostics. Abandon recovery but do not raze the original |
| 107 // database. | 109 // database. |
| 108 // NOTE(shess): Only call this when adding recovery support. In the | 110 // NOTE(shess): Only call this when adding recovery support. In the |
| 109 // steady state, all databases should progress to recovered or razed. | 111 // steady state, all databases should progress to recovered or razed. |
| 110 static void Rollback(scoped_ptr<Recovery> r); | 112 static void Rollback(std::unique_ptr<Recovery> r); |
| 111 | 113 |
| 112 // Handle to the temporary recovery database. | 114 // Handle to the temporary recovery database. |
| 113 sql::Connection* db() { return &recover_db_; } | 115 sql::Connection* db() { return &recover_db_; } |
| 114 | 116 |
| 115 // Attempt to recover the named table from the corrupt database into | 117 // Attempt to recover the named table from the corrupt database into |
| 116 // the recovery database using a temporary recover virtual table. | 118 // the recovery database using a temporary recover virtual table. |
| 117 // The virtual table schema is derived from the named table's schema | 119 // The virtual table schema is derived from the named table's schema |
| 118 // in database [main]. Data is copied using INSERT OR IGNORE, so | 120 // in database [main]. Data is copied using INSERT OR IGNORE, so |
| 119 // duplicates are dropped. | 121 // duplicates are dropped. |
| 120 // | 122 // |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 | 172 |
| 171 Connection* db_; // Original database connection. | 173 Connection* db_; // Original database connection. |
| 172 Connection recover_db_; // Recovery connection. | 174 Connection recover_db_; // Recovery connection. |
| 173 | 175 |
| 174 DISALLOW_COPY_AND_ASSIGN(Recovery); | 176 DISALLOW_COPY_AND_ASSIGN(Recovery); |
| 175 }; | 177 }; |
| 176 | 178 |
| 177 } // namespace sql | 179 } // namespace sql |
| 178 | 180 |
| 179 #endif // SQL_RECOVERY_H_ | 181 #endif // SQL_RECOVERY_H_ |
| OLD | NEW |