NaCl: Update revision in DEPS, 2363d1a -> 66f4b73
[chromium-blink-merge.git] / components / leveldb_proto / leveldb_database.cc
blob45f220e9576c565e6dfcbbdec81d34def93849c5
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/src/include/leveldb/db.h"
16 #include "third_party/leveldatabase/src/include/leveldb/iterator.h"
17 #include "third_party/leveldatabase/src/include/leveldb/options.h"
18 #include "third_party/leveldatabase/src/include/leveldb/slice.h"
19 #include "third_party/leveldatabase/src/include/leveldb/status.h"
20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
22 namespace leveldb_proto {
24 LevelDB::LevelDB() {}
26 LevelDB::~LevelDB() {
27 DFAKE_SCOPED_LOCK(thread_checker_);
30 bool LevelDB::InitWithOptions(const base::FilePath& database_dir,
31 const leveldb::Options& options) {
32 DFAKE_SCOPED_LOCK(thread_checker_);
34 std::string path = database_dir.AsUTF8Unsafe();
36 leveldb::DB* db = NULL;
37 leveldb::Status status = leveldb::DB::Open(options, path, &db);
38 if (status.IsCorruption()) {
39 base::DeleteFile(database_dir, true);
40 status = leveldb::DB::Open(options, path, &db);
43 if (status.ok()) {
44 CHECK(db);
45 db_.reset(db);
46 return true;
49 LOG(WARNING) << "Unable to open " << database_dir.value() << ": "
50 << status.ToString();
51 return false;
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);
61 bool LevelDB::Save(const base::StringPairs& entries_to_save,
62 const std::vector<std::string>& keys_to_remove) {
63 DFAKE_SCOPED_LOCK(thread_checker_);
64 if (!db_) {
65 return false;
68 leveldb::WriteBatch updates;
69 for (base::StringPairs::const_iterator it = entries_to_save.begin();
70 it != entries_to_save.end();
71 ++it) {
72 updates.Put(leveldb::Slice(it->first), leveldb::Slice(it->second));
74 for (std::vector<std::string>::const_iterator it = keys_to_remove.begin();
75 it != keys_to_remove.end(); ++it) {
76 updates.Delete(leveldb::Slice(*it));
79 leveldb::WriteOptions options;
80 options.sync = true;
81 leveldb::Status status = db_->Write(options, &updates);
82 if (status.ok()) return true;
84 DLOG(WARNING) << "Failed writing leveldb_proto entries: "
85 << status.ToString();
86 return false;
89 bool LevelDB::Load(std::vector<std::string>* entries) {
90 DFAKE_SCOPED_LOCK(thread_checker_);
91 if (!db_) {
92 return false;
95 leveldb::ReadOptions options;
96 scoped_ptr<leveldb::Iterator> db_iterator(db_->NewIterator(options));
97 for (db_iterator->SeekToFirst(); db_iterator->Valid(); db_iterator->Next()) {
98 leveldb::Slice value_slice = db_iterator->value();
99 std::string entry(value_slice.data(), value_slice.size());
100 entries->push_back(entry);
102 return true;
105 } // namespace leveldb_proto