[Cronet] Delay StartNetLog and StopNetLog until native request context is initialized
[chromium-blink-merge.git] / components / leveldb_proto / leveldb_database.cc
blobb3e1a05a97afb440771b92c52eb58fc65527a293
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/include/leveldb/db.h"
17 #include "third_party/leveldatabase/src/include/leveldb/iterator.h"
18 #include "third_party/leveldatabase/src/include/leveldb/options.h"
19 #include "third_party/leveldatabase/src/include/leveldb/slice.h"
20 #include "third_party/leveldatabase/src/include/leveldb/status.h"
21 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
23 namespace leveldb_proto {
25 LevelDB::LevelDB() {}
27 LevelDB::~LevelDB() {
28 DFAKE_SCOPED_LOCK(thread_checker_);
31 bool LevelDB::InitWithOptions(const base::FilePath& database_dir,
32 const leveldb::Options& options) {
33 DFAKE_SCOPED_LOCK(thread_checker_);
35 std::string path = database_dir.AsUTF8Unsafe();
37 leveldb::DB* db = NULL;
38 leveldb::Status status = leveldb::DB::Open(options, path, &db);
39 if (status.IsCorruption()) {
40 base::DeleteFile(database_dir, true);
41 status = leveldb::DB::Open(options, path, &db);
44 if (status.ok()) {
45 CHECK(db);
46 db_.reset(db);
47 return true;
50 LOG(WARNING) << "Unable to open " << database_dir.value() << ": "
51 << status.ToString();
52 return false;
55 bool LevelDB::Init(const base::FilePath& database_dir) {
56 leveldb::Options options;
57 options.create_if_missing = true;
58 options.max_open_files = 0; // Use minimum.
59 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue;
60 return InitWithOptions(database_dir, options);
63 bool LevelDB::Save(const base::StringPairs& entries_to_save,
64 const std::vector<std::string>& keys_to_remove) {
65 DFAKE_SCOPED_LOCK(thread_checker_);
66 if (!db_) {
67 return false;
70 leveldb::WriteBatch updates;
71 for (base::StringPairs::const_iterator it = entries_to_save.begin();
72 it != entries_to_save.end();
73 ++it) {
74 updates.Put(leveldb::Slice(it->first), leveldb::Slice(it->second));
76 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin();
77 it != keys_to_remove.end(); ++it) {
78 updates.Delete(leveldb::Slice(*it));
81 leveldb::WriteOptions options;
82 options.sync = true;
83 leveldb::Status status = db_->Write(options, &updates);
84 if (status.ok()) return true;
86 DLOG(WARNING) << "Failed writing leveldb_proto entries: "
87 << status.ToString();
88 return false;
91 bool LevelDB::Load(std::vector<std::string>* entries) {
92 DFAKE_SCOPED_LOCK(thread_checker_);
93 if (!db_) {
94 return false;
97 leveldb::ReadOptions options;
98 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options));
99 for (db_iterator->SeekToFirst(); db_iterator->Valid(); db_iterator->Next()) {
100 leveldb::Slice value_slice = db_iterator->value();
101 std::string entry(value_slice.data(), value_slice.size());
102 entries->push_back(entry);
104 return true;
107 } // namespace leveldb_proto