Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / leveldb_proto / leveldb_database.cc
blob80df1157a0e74af4af8ed5d18e693f037b1bddf3
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
3 // found in the LICENSE file.
5 #include "components/leveldb_proto/leveldb_database.h"
7 #include <string>
8 #include <vector>
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string_split.h"
14 #include "base/threading/thread_checker.h"
15 #include "third_party/leveldatabase/env_chromium.h"
16 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
17 #include "third_party/leveldatabase/src/include/leveldb/db.h"
18 #include "third_party/leveldatabase/src/include/leveldb/env.h"
19 #include "third_party/leveldatabase/src/include/leveldb/iterator.h"
20 #include "third_party/leveldatabase/src/include/leveldb/options.h"
21 #include "third_party/leveldatabase/src/include/leveldb/slice.h"
22 #include "third_party/leveldatabase/src/include/leveldb/status.h"
23 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
25 namespace leveldb_proto {
27 LevelDB::LevelDB() {}
29 LevelDB::~LevelDB() {
30 DFAKE_SCOPED_LOCK(thread_checker_);
33 bool LevelDB::InitWithOptions(const base::FilePath& database_dir,
34 const leveldb::Options& options) {
35 DFAKE_SCOPED_LOCK(thread_checker_);
37 std::string path = database_dir.AsUTF8Unsafe();
39 leveldb::DB* db = NULL;
40 leveldb::Status status = leveldb::DB::Open(options, path, &db);
41 if (status.IsCorruption()) {
42 base::DeleteFile(database_dir, true);
43 status = leveldb::DB::Open(options, path, &db);
46 if (status.ok()) {
47 CHECK(db);
48 db_.reset(db);
49 return true;
52 LOG(WARNING) << "Unable to open " << database_dir.value() << ": "
53 << status.ToString();
54 return false;
57 bool LevelDB::Init(const base::FilePath& database_dir) {
58 leveldb::Options options;
59 options.create_if_missing = true;
60 options.max_open_files = 0; // Use minimum.
61 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue;
62 if (database_dir.empty()) {
63 env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
64 options.env = env_.get();
67 return InitWithOptions(database_dir, options);
70 bool LevelDB::Save(const base::StringPairs& entries_to_save,
71 const std::vector<std::string>& keys_to_remove) {
72 DFAKE_SCOPED_LOCK(thread_checker_);
73 if (!db_)
74 return false;
76 leveldb::WriteBatch updates;
77 for (const auto& pair : entries_to_save)
78 updates.Put(leveldb::Slice(pair.first), leveldb::Slice(pair.second));
80 for (const auto& key : keys_to_remove)
81 updates.Delete(leveldb::Slice(key));
83 leveldb::WriteOptions options;
84 options.sync = true;
86 leveldb::Status status = db_->Write(options, &updates);
87 if (status.ok())
88 return true;
90 DLOG(WARNING) << "Failed writing leveldb_proto entries: "
91 << status.ToString();
92 return false;
95 bool LevelDB::Load(std::vector<std::string>* entries) {
96 DFAKE_SCOPED_LOCK(thread_checker_);
97 if (!db_)
98 return false;
100 leveldb::ReadOptions options;
101 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options));
102 for (db_iterator->SeekToFirst(); db_iterator->Valid(); db_iterator->Next()) {
103 leveldb::Slice value_slice = db_iterator->value();
104 std::string entry(value_slice.data(), value_slice.size());
105 entries->push_back(entry);
107 return true;
110 } // namespace leveldb_proto