OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/data_reduction_proxy/core/browser/data_store_impl.h" | 5 #include "components/data_reduction_proxy/core/browser/data_store_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 } // namespace | 40 } // namespace |
41 | 41 |
42 namespace data_reduction_proxy { | 42 namespace data_reduction_proxy { |
43 | 43 |
44 DataStoreImpl::DataStoreImpl(const base::FilePath& profile_path) | 44 DataStoreImpl::DataStoreImpl(const base::FilePath& profile_path) |
45 : profile_path_(profile_path) { | 45 : profile_path_(profile_path) { |
46 sequence_checker_.DetachFromSequence(); | 46 sequence_checker_.DetachFromSequence(); |
47 } | 47 } |
48 | 48 |
49 DataStoreImpl::~DataStoreImpl() { | 49 DataStoreImpl::~DataStoreImpl() { |
50 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 50 DCHECK(sequence_checker_.CalledOnValidSequence()); |
51 } | 51 } |
52 | 52 |
53 void DataStoreImpl::InitializeOnDBThread() { | 53 void DataStoreImpl::InitializeOnDBThread() { |
54 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 54 DCHECK(sequence_checker_.CalledOnValidSequence()); |
55 DCHECK(!db_); | 55 DCHECK(!db_); |
56 | 56 |
57 DataStore::Status status = OpenDB(); | 57 DataStore::Status status = OpenDB(); |
58 if (status == CORRUPTED) | 58 if (status == CORRUPTED) |
59 RecreateDB(); | 59 RecreateDB(); |
60 } | 60 } |
61 | 61 |
62 DataStore::Status DataStoreImpl::Get(base::StringPiece key, | 62 DataStore::Status DataStoreImpl::Get(base::StringPiece key, |
63 std::string* value) { | 63 std::string* value) { |
64 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 64 DCHECK(sequence_checker_.CalledOnValidSequence()); |
65 | 65 |
66 if (!db_) | 66 if (!db_) |
67 return MISC_ERROR; | 67 return MISC_ERROR; |
68 | 68 |
69 leveldb::ReadOptions read_options; | 69 leveldb::ReadOptions read_options; |
70 read_options.verify_checksums = true; | 70 read_options.verify_checksums = true; |
71 leveldb::Slice slice(key.data(), key.size()); | 71 leveldb::Slice slice(key.data(), key.size()); |
72 leveldb::Status status = db_->Get(read_options, slice, value); | 72 leveldb::Status status = db_->Get(read_options, slice, value); |
73 if (status.IsCorruption()) | 73 if (status.IsCorruption()) |
74 RecreateDB(); | 74 RecreateDB(); |
75 | 75 |
76 return LevelDbToDRPStoreStatus(status); | 76 return LevelDbToDRPStoreStatus(status); |
77 } | 77 } |
78 | 78 |
79 DataStore::Status DataStoreImpl::Put( | 79 DataStore::Status DataStoreImpl::Put( |
80 const std::map<std::string, std::string>& map) { | 80 const std::map<std::string, std::string>& map) { |
81 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 81 DCHECK(sequence_checker_.CalledOnValidSequence()); |
82 | 82 |
83 if (!db_) | 83 if (!db_) |
84 return MISC_ERROR; | 84 return MISC_ERROR; |
85 | 85 |
86 leveldb::WriteBatch batch; | 86 leveldb::WriteBatch batch; |
87 for (const auto& iter : map) { | 87 for (const auto& iter : map) { |
88 batch.Put(iter.first, iter.second); | 88 batch.Put(iter.first, iter.second); |
89 } | 89 } |
90 | 90 |
91 leveldb::WriteOptions write_options; | 91 leveldb::WriteOptions write_options; |
92 leveldb::Status status = db_->Write(write_options, &batch); | 92 leveldb::Status status = db_->Write(write_options, &batch); |
93 if (status.IsCorruption()) | 93 if (status.IsCorruption()) |
94 RecreateDB(); | 94 RecreateDB(); |
95 | 95 |
96 return LevelDbToDRPStoreStatus(status); | 96 return LevelDbToDRPStoreStatus(status); |
97 } | 97 } |
98 | 98 |
99 DataStore::Status DataStoreImpl::Delete(base::StringPiece key) { | 99 DataStore::Status DataStoreImpl::Delete(base::StringPiece key) { |
100 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 100 DCHECK(sequence_checker_.CalledOnValidSequence()); |
101 | 101 |
102 if (!db_) | 102 if (!db_) |
103 return MISC_ERROR; | 103 return MISC_ERROR; |
104 | 104 |
105 leveldb::Slice slice(key.data(), key.size()); | 105 leveldb::Slice slice(key.data(), key.size()); |
106 leveldb::WriteOptions write_options; | 106 leveldb::WriteOptions write_options; |
107 leveldb::Status status = db_->Delete(write_options, slice); | 107 leveldb::Status status = db_->Delete(write_options, slice); |
108 if (status.IsCorruption()) | 108 if (status.IsCorruption()) |
109 RecreateDB(); | 109 RecreateDB(); |
110 | 110 |
111 return LevelDbToDRPStoreStatus(status); | 111 return LevelDbToDRPStoreStatus(status); |
112 } | 112 } |
113 | 113 |
114 DataStore::Status DataStoreImpl::OpenDB() { | 114 DataStore::Status DataStoreImpl::OpenDB() { |
115 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 115 DCHECK(sequence_checker_.CalledOnValidSequence()); |
116 | 116 |
117 leveldb::Options options; | 117 leveldb::Options options; |
118 options.create_if_missing = true; | 118 options.create_if_missing = true; |
119 options.paranoid_checks = true; | 119 options.paranoid_checks = true; |
120 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; | 120 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; |
121 std::string db_name = profile_path_.Append(kDBName).AsUTF8Unsafe(); | 121 std::string db_name = profile_path_.Append(kDBName).AsUTF8Unsafe(); |
122 leveldb::DB* dbptr = nullptr; | 122 leveldb::DB* dbptr = nullptr; |
123 Status status = | 123 Status status = |
124 LevelDbToDRPStoreStatus(leveldb::DB::Open(options, db_name, &dbptr)); | 124 LevelDbToDRPStoreStatus(leveldb::DB::Open(options, db_name, &dbptr)); |
125 UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.LevelDBOpenStatus", status, | 125 UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.LevelDBOpenStatus", status, |
(...skipping 12 matching lines...) Expand all Loading... |
138 range.start = ""; | 138 range.start = ""; |
139 range.limit = "z"; // Keys starting with 'z' will not be included. | 139 range.limit = "z"; // Keys starting with 'z' will not be included. |
140 dbptr->GetApproximateSizes(&range, 1, &size); | 140 dbptr->GetApproximateSizes(&range, 1, &size); |
141 UMA_HISTOGRAM_MEMORY_KB("DataReductionProxy.LevelDBSize", size / 1024); | 141 UMA_HISTOGRAM_MEMORY_KB("DataReductionProxy.LevelDBSize", size / 1024); |
142 } | 142 } |
143 | 143 |
144 return status; | 144 return status; |
145 } | 145 } |
146 | 146 |
147 void DataStoreImpl::RecreateDB() { | 147 void DataStoreImpl::RecreateDB() { |
148 DCHECK(sequence_checker_.CalledOnValidSequencedThread()); | 148 DCHECK(sequence_checker_.CalledOnValidSequence()); |
149 | 149 |
150 LOG(WARNING) << "Deleting corrupt Data Reduction Proxy LevelDB"; | 150 LOG(WARNING) << "Deleting corrupt Data Reduction Proxy LevelDB"; |
151 db_.reset(nullptr); | 151 db_.reset(nullptr); |
152 base::DeleteFile(profile_path_.Append(kDBName), true); | 152 base::DeleteFile(profile_path_.Append(kDBName), true); |
153 | 153 |
154 OpenDB(); | 154 OpenDB(); |
155 } | 155 } |
156 | 156 |
157 } // namespace data_reduction_proxy | 157 } // namespace data_reduction_proxy |
OLD | NEW |