Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(427)

Unified Diff: chrome/browser/value_store/leveldb_value_store.cc

Issue 165223003: Add a Restore() method to ValueStore and make StorageAPI use it (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/value_store/leveldb_value_store.cc
diff --git a/chrome/browser/value_store/leveldb_value_store.cc b/chrome/browser/value_store/leveldb_value_store.cc
index 043bcb3d1915f4e3a316f871d9ff06f47aa8f1cb..d6712eaa3bbe90f3ce1879cc38b8e91cf4f2dce7 100644
--- a/chrome/browser/value_store/leveldb_value_store.cc
+++ b/chrome/browser/value_store/leveldb_value_store.cc
@@ -269,6 +269,56 @@ ValueStore::WriteResult LeveldbValueStore::Clear() {
return MakeWriteResult(changes.Pass());
}
+bool LeveldbValueStore::Restore() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+
+ ReadResult result = Get();
+ std::string previous_key;
+ while (result->HasError() && result->error().code == CORRUPTION) {
+ // If we don't have a specific corrupted key, or we've tried and failed to
+ // clear this specific key, or we fail to restore the key, then wipe the
+ // whole database.
+ if (!result->error().key.get() || *result->error().key == previous_key ||
+ !RestoreKey(*result->error().key)) {
+ DeleteDbFile();
+ result = Get();
+ break;
+ }
+
+ // Otherwise, re-Get() the database to check if there is still any
+ // corruption.
+ previous_key = *result->error().key;
+ result = Get();
+ }
+
+ // If we still have an error, it means we've tried deleting the database file,
+ // and failed. There's nothing more we can do.
+ return !(result->HasError() && result->error().code == CORRUPTION);
+}
+
+bool LeveldbValueStore::RestoreKey(const std::string& key) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
+
+ ReadResult result = Get(key);
+ if (result->HasError() && result->error().code == CORRUPTION) {
not at google - send to devlin 2014/02/14 19:35:56 all my talk of making corruption a switch... turns
Devlin 2014/02/18 23:55:22 Done.
+ leveldb::WriteBatch batch;
+ batch.Delete(key);
+ scoped_ptr<ValueStore::Error> error = WriteToDb(&batch);
+ // If we can't delete the key, the restore failed.
+ if (error.get())
+ return false;
+ result = Get(key);
+ }
+
+ // The restore succeeded if there is no corruption error.
+ return !(result->HasError() && result->error().code == CORRUPTION);
not at google - send to devlin 2014/02/14 19:35:56 return !result->HasError() || result->error().code
Devlin 2014/02/18 23:55:22 Done.
+}
+
+bool LeveldbValueStore::WriteToDbForTest(leveldb::WriteBatch* batch) {
+ scoped_ptr<ValueStore::Error> error = WriteToDb(batch);
not at google - send to devlin 2014/02/14 19:35:56 inline this
Devlin 2014/02/18 23:55:22 Done.
+ return !error.get();
+}
+
scoped_ptr<ValueStore::Error> LeveldbValueStore::EnsureDbIsOpen() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

Powered by Google App Engine
This is Rietveld 408576698