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

Side by Side Diff: components/leveldb_proto/leveldb_database.cc

Issue 735823004: Make ProtoDatabase properly handle failed Init (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month 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 unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "components/leveldb_proto/leveldb_database.h" 5 #include "components/leveldb_proto/leveldb_database.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_split.h" 13 #include "base/strings/string_split.h"
14 #include "base/threading/thread_checker.h" 14 #include "base/threading/thread_checker.h"
15 #include "third_party/leveldatabase/src/include/leveldb/db.h" 15 #include "third_party/leveldatabase/src/include/leveldb/db.h"
16 #include "third_party/leveldatabase/src/include/leveldb/iterator.h" 16 #include "third_party/leveldatabase/src/include/leveldb/iterator.h"
17 #include "third_party/leveldatabase/src/include/leveldb/options.h" 17 #include "third_party/leveldatabase/src/include/leveldb/options.h"
18 #include "third_party/leveldatabase/src/include/leveldb/slice.h" 18 #include "third_party/leveldatabase/src/include/leveldb/slice.h"
19 #include "third_party/leveldatabase/src/include/leveldb/status.h" 19 #include "third_party/leveldatabase/src/include/leveldb/status.h"
20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
21 21
22 namespace leveldb_proto { 22 namespace leveldb_proto {
23 23
24 LevelDB::LevelDB() {} 24 LevelDB::LevelDB() {}
25 25
26 LevelDB::~LevelDB() { DFAKE_SCOPED_LOCK(thread_checker_); } 26 LevelDB::~LevelDB() {
27 DFAKE_SCOPED_LOCK(thread_checker_);
28 }
27 29
28 bool LevelDB::Init(const base::FilePath& database_dir) { 30 bool LevelDB::InitWithOptions(const base::FilePath& database_dir,
31 const leveldb::Options& options) {
29 DFAKE_SCOPED_LOCK(thread_checker_); 32 DFAKE_SCOPED_LOCK(thread_checker_);
30 33
31 leveldb::Options options;
32 options.create_if_missing = true;
33 options.max_open_files = 0; // Use minimum.
34
35 std::string path = database_dir.AsUTF8Unsafe(); 34 std::string path = database_dir.AsUTF8Unsafe();
36 35
37 leveldb::DB* db = NULL; 36 leveldb::DB* db = NULL;
38 leveldb::Status status = leveldb::DB::Open(options, path, &db); 37 leveldb::Status status = leveldb::DB::Open(options, path, &db);
39 if (status.IsCorruption()) { 38 if (status.IsCorruption()) {
40 base::DeleteFile(database_dir, true); 39 base::DeleteFile(database_dir, true);
41 status = leveldb::DB::Open(options, path, &db); 40 status = leveldb::DB::Open(options, path, &db);
42 } 41 }
43 42
44 if (status.ok()) { 43 if (status.ok()) {
45 CHECK(db); 44 CHECK(db);
46 db_.reset(db); 45 db_.reset(db);
47 return true; 46 return true;
48 } 47 }
49 48
50 LOG(WARNING) << "Unable to open " << database_dir.value() << ": " 49 LOG(WARNING) << "Unable to open " << database_dir.value() << ": "
51 << status.ToString(); 50 << status.ToString();
52 return false; 51 return false;
53 } 52 }
54 53
54 bool LevelDB::Init(const base::FilePath& database_dir) {
55 leveldb::Options options;
56 options.create_if_missing = true;
57 options.max_open_files = 0; // Use minimum.
58 return InitWithOptions(database_dir, options);
59 }
60
55 bool LevelDB::Save(const base::StringPairs& entries_to_save, 61 bool LevelDB::Save(const base::StringPairs& entries_to_save,
56 const std::vector<std::string>& keys_to_remove) { 62 const std::vector<std::string>& keys_to_remove) {
57 DFAKE_SCOPED_LOCK(thread_checker_); 63 DFAKE_SCOPED_LOCK(thread_checker_);
64 if (!db_) {
65 return false;
66 }
58 67
59 leveldb::WriteBatch updates; 68 leveldb::WriteBatch updates;
60 for (base::StringPairs::const_iterator it = entries_to_save.begin(); 69 for (base::StringPairs::const_iterator it = entries_to_save.begin();
61 it != entries_to_save.end(); 70 it != entries_to_save.end();
62 ++it) { 71 ++it) {
63 updates.Put(leveldb::Slice(it->first), leveldb::Slice(it->second)); 72 updates.Put(leveldb::Slice(it->first), leveldb::Slice(it->second));
64 } 73 }
65 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin(); 74 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin();
66 it != keys_to_remove.end(); ++it) { 75 it != keys_to_remove.end(); ++it) {
67 updates.Delete(leveldb::Slice(*it)); 76 updates.Delete(leveldb::Slice(*it));
68 } 77 }
69 78
70 leveldb::WriteOptions options; 79 leveldb::WriteOptions options;
71 options.sync = true; 80 options.sync = true;
72 leveldb::Status status = db_->Write(options, &updates); 81 leveldb::Status status = db_->Write(options, &updates);
73 if (status.ok()) return true; 82 if (status.ok()) return true;
74 83
75 DLOG(WARNING) << "Failed writing leveldb_proto entries: " 84 DLOG(WARNING) << "Failed writing leveldb_proto entries: "
76 << status.ToString(); 85 << status.ToString();
77 return false; 86 return false;
78 } 87 }
79 88
80 bool LevelDB::Load(std::vector<std::string>* entries) { 89 bool LevelDB::Load(std::vector<std::string>* entries) {
81 DFAKE_SCOPED_LOCK(thread_checker_); 90 DFAKE_SCOPED_LOCK(thread_checker_);
91 if (!db_) {
92 return false;
93 }
82 94
83 leveldb::ReadOptions options; 95 leveldb::ReadOptions options;
84 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options)); 96 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options));
85 for (db_iterator->SeekToFirst(); db_iterator->Valid(); db_iterator->Next()) { 97 for (db_iterator->SeekToFirst(); db_iterator->Valid(); db_iterator->Next()) {
86 leveldb::Slice value_slice = db_iterator->value(); 98 leveldb::Slice value_slice = db_iterator->value();
87 std::string entry(value_slice.data(), value_slice.size()); 99 std::string entry(value_slice.data(), value_slice.size());
88 entries->push_back(entry); 100 entries->push_back(entry);
89 } 101 }
90 return true; 102 return true;
91 } 103 }
92 104
93 } // namespace leveldb_proto 105 } // namespace leveldb_proto
OLDNEW
« no previous file with comments | « components/leveldb_proto/leveldb_database.h ('k') | components/leveldb_proto/proto_database_impl_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698