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

Side by Side Diff: content/browser/indexed_db/leveldb/leveldb_database.cc

Issue 2233153002: IndexedDB: WrapUnique(new T(args..)) -> MakeUnique<T>(args...) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review feedback Created 4 years, 4 months 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 (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "content/browser/indexed_db/leveldb/leveldb_database.h" 5 #include "content/browser/indexed_db/leveldb/leveldb_database.h"
6 6
7 #include <inttypes.h> 7 #include <inttypes.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <cerrno> 10 #include <cerrno>
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
109 db_.reset(); 109 db_.reset();
110 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.CloseTime", 110 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.CloseTime",
111 base::TimeTicks::Now() - begin_time); 111 base::TimeTicks::Now() - begin_time);
112 } 112 }
113 } 113 }
114 114
115 static leveldb::Status OpenDB( 115 static leveldb::Status OpenDB(
116 leveldb::Comparator* comparator, 116 leveldb::Comparator* comparator,
117 leveldb::Env* env, 117 leveldb::Env* env,
118 const base::FilePath& path, 118 const base::FilePath& path,
119 leveldb::DB** db, 119 std::unique_ptr<leveldb::DB>* db,
120 std::unique_ptr<const leveldb::FilterPolicy>* filter_policy) { 120 std::unique_ptr<const leveldb::FilterPolicy>* filter_policy) {
121 filter_policy->reset(leveldb::NewBloomFilterPolicy(10)); 121 filter_policy->reset(leveldb::NewBloomFilterPolicy(10));
122 leveldb::Options options; 122 leveldb::Options options;
123 options.comparator = comparator; 123 options.comparator = comparator;
124 options.create_if_missing = true; 124 options.create_if_missing = true;
125 options.paranoid_checks = true; 125 options.paranoid_checks = true;
126 options.filter_policy = filter_policy->get(); 126 options.filter_policy = filter_policy->get();
127 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue; 127 options.reuse_logs = leveldb_env::kDefaultLogReuseOptionValue;
128 options.compression = leveldb::kSnappyCompression; 128 options.compression = leveldb::kSnappyCompression;
129 129
130 // For info about the troubles we've run into with this parameter, see: 130 // For info about the troubles we've run into with this parameter, see:
131 // https://code.google.com/p/chromium/issues/detail?id=227313#c11 131 // https://code.google.com/p/chromium/issues/detail?id=227313#c11
132 options.max_open_files = 80; 132 options.max_open_files = 80;
133 options.env = env; 133 options.env = env;
134 134
135 // ChromiumEnv assumes UTF8, converts back to FilePath before using. 135 // ChromiumEnv assumes UTF8, converts back to FilePath before using.
136 leveldb::Status s = leveldb::DB::Open(options, path.AsUTF8Unsafe(), db); 136 leveldb::DB* db_ptr = nullptr;
137 leveldb::Status s = leveldb::DB::Open(options, path.AsUTF8Unsafe(), &db_ptr);
138 db->reset(db_ptr);
137 139
138 return s; 140 return s;
139 } 141 }
140 142
141 leveldb::Status LevelDBDatabase::Destroy(const base::FilePath& file_name) { 143 leveldb::Status LevelDBDatabase::Destroy(const base::FilePath& file_name) {
142 leveldb::Options options; 144 leveldb::Options options;
143 options.env = LevelDBEnv::Get(); 145 options.env = LevelDBEnv::Get();
144 // ChromiumEnv assumes UTF8, converts back to FilePath before using. 146 // ChromiumEnv assumes UTF8, converts back to FilePath before using.
145 return leveldb::DestroyDB(file_name.AsUTF8Unsafe(), options); 147 return leveldb::DestroyDB(file_name.AsUTF8Unsafe(), options);
146 } 148 }
(...skipping 15 matching lines...) Expand all
162 164
163 std::unique_ptr<LevelDBLock> LevelDBDatabase::LockForTesting( 165 std::unique_ptr<LevelDBLock> LevelDBDatabase::LockForTesting(
164 const base::FilePath& file_name) { 166 const base::FilePath& file_name) {
165 leveldb::Env* env = LevelDBEnv::Get(); 167 leveldb::Env* env = LevelDBEnv::Get();
166 base::FilePath lock_path = file_name.AppendASCII("LOCK"); 168 base::FilePath lock_path = file_name.AppendASCII("LOCK");
167 leveldb::FileLock* lock = NULL; 169 leveldb::FileLock* lock = NULL;
168 leveldb::Status status = env->LockFile(lock_path.AsUTF8Unsafe(), &lock); 170 leveldb::Status status = env->LockFile(lock_path.AsUTF8Unsafe(), &lock);
169 if (!status.ok()) 171 if (!status.ok())
170 return std::unique_ptr<LevelDBLock>(); 172 return std::unique_ptr<LevelDBLock>();
171 DCHECK(lock); 173 DCHECK(lock);
172 return std::unique_ptr<LevelDBLock>(new LockImpl(env, lock)); 174 return base::MakeUnique<LockImpl>(env, lock);
173 } 175 }
174 176
175 static int CheckFreeSpace(const char* const type, 177 static int CheckFreeSpace(const char* const type,
176 const base::FilePath& file_name) { 178 const base::FilePath& file_name) {
177 std::string name = 179 std::string name =
178 std::string("WebCore.IndexedDB.LevelDB.Open") + type + "FreeDiskSpace"; 180 std::string("WebCore.IndexedDB.LevelDB.Open") + type + "FreeDiskSpace";
179 int64_t free_disk_space_in_k_bytes = 181 int64_t free_disk_space_in_k_bytes =
180 base::SysInfo::AmountOfFreeDiskSpace(file_name) / 1024; 182 base::SysInfo::AmountOfFreeDiskSpace(file_name) / 1024;
181 if (free_disk_space_in_k_bytes < 0) { 183 if (free_disk_space_in_k_bytes < 0) {
182 base::Histogram::FactoryGet( 184 base::Histogram::FactoryGet(
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
282 } 284 }
283 285
284 leveldb::Status LevelDBDatabase::Open(const base::FilePath& file_name, 286 leveldb::Status LevelDBDatabase::Open(const base::FilePath& file_name,
285 const LevelDBComparator* comparator, 287 const LevelDBComparator* comparator,
286 std::unique_ptr<LevelDBDatabase>* result, 288 std::unique_ptr<LevelDBDatabase>* result,
287 bool* is_disk_full) { 289 bool* is_disk_full) {
288 IDB_TRACE("LevelDBDatabase::Open"); 290 IDB_TRACE("LevelDBDatabase::Open");
289 base::TimeTicks begin_time = base::TimeTicks::Now(); 291 base::TimeTicks begin_time = base::TimeTicks::Now();
290 292
291 std::unique_ptr<ComparatorAdapter> comparator_adapter( 293 std::unique_ptr<ComparatorAdapter> comparator_adapter(
292 new ComparatorAdapter(comparator)); 294 base::MakeUnique<ComparatorAdapter>(comparator));
293 295
294 leveldb::DB* db; 296 std::unique_ptr<leveldb::DB> db;
295 std::unique_ptr<const leveldb::FilterPolicy> filter_policy; 297 std::unique_ptr<const leveldb::FilterPolicy> filter_policy;
296 const leveldb::Status s = OpenDB(comparator_adapter.get(), LevelDBEnv::Get(), 298 const leveldb::Status s = OpenDB(comparator_adapter.get(), LevelDBEnv::Get(),
297 file_name, &db, &filter_policy); 299 file_name, &db, &filter_policy);
298 300
299 if (!s.ok()) { 301 if (!s.ok()) {
300 HistogramLevelDBError("WebCore.IndexedDB.LevelDBOpenErrors", s); 302 HistogramLevelDBError("WebCore.IndexedDB.LevelDBOpenErrors", s);
301 int free_space_k_bytes = CheckFreeSpace("Failure", file_name); 303 int free_space_k_bytes = CheckFreeSpace("Failure", file_name);
302 // Disks with <100k of free space almost never succeed in opening a 304 // Disks with <100k of free space almost never succeed in opening a
303 // leveldb database. 305 // leveldb database.
304 if (is_disk_full) 306 if (is_disk_full)
305 *is_disk_full = free_space_k_bytes >= 0 && free_space_k_bytes < 100; 307 *is_disk_full = free_space_k_bytes >= 0 && free_space_k_bytes < 100;
306 308
307 LOG(ERROR) << "Failed to open LevelDB database from " 309 LOG(ERROR) << "Failed to open LevelDB database from "
308 << file_name.AsUTF8Unsafe() << "," << s.ToString(); 310 << file_name.AsUTF8Unsafe() << "," << s.ToString();
309 return s; 311 return s;
310 } 312 }
311 313
312 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.OpenTime", 314 UMA_HISTOGRAM_MEDIUM_TIMES("WebCore.IndexedDB.LevelDB.OpenTime",
313 base::TimeTicks::Now() - begin_time); 315 base::TimeTicks::Now() - begin_time);
314 316
315 CheckFreeSpace("Success", file_name); 317 CheckFreeSpace("Success", file_name);
316 318
317 (*result).reset(new LevelDBDatabase); 319 (*result) = base::WrapUnique(new LevelDBDatabase());
318 (*result)->db_ = base::WrapUnique(db); 320 (*result)->db_ = std::move(db);
319 (*result)->comparator_adapter_ = std::move(comparator_adapter); 321 (*result)->comparator_adapter_ = std::move(comparator_adapter);
320 (*result)->comparator_ = comparator; 322 (*result)->comparator_ = comparator;
321 (*result)->filter_policy_ = std::move(filter_policy); 323 (*result)->filter_policy_ = std::move(filter_policy);
322 (*result)->file_name_for_tracing = file_name.BaseName().AsUTF8Unsafe(); 324 (*result)->file_name_for_tracing = file_name.BaseName().AsUTF8Unsafe();
323 325
324 return s; 326 return s;
325 } 327 }
326 328
327 std::unique_ptr<LevelDBDatabase> LevelDBDatabase::OpenInMemory( 329 std::unique_ptr<LevelDBDatabase> LevelDBDatabase::OpenInMemory(
328 const LevelDBComparator* comparator) { 330 const LevelDBComparator* comparator) {
329 std::unique_ptr<ComparatorAdapter> comparator_adapter( 331 std::unique_ptr<ComparatorAdapter> comparator_adapter(
330 new ComparatorAdapter(comparator)); 332 base::MakeUnique<ComparatorAdapter>(comparator));
331 std::unique_ptr<leveldb::Env> in_memory_env( 333 std::unique_ptr<leveldb::Env> in_memory_env(
332 leveldb::NewMemEnv(LevelDBEnv::Get())); 334 leveldb::NewMemEnv(LevelDBEnv::Get()));
333 335
334 leveldb::DB* db; 336 std::unique_ptr<leveldb::DB> db;
335 std::unique_ptr<const leveldb::FilterPolicy> filter_policy; 337 std::unique_ptr<const leveldb::FilterPolicy> filter_policy;
336 const leveldb::Status s = OpenDB(comparator_adapter.get(), 338 const leveldb::Status s = OpenDB(comparator_adapter.get(),
337 in_memory_env.get(), 339 in_memory_env.get(),
338 base::FilePath(), 340 base::FilePath(),
339 &db, 341 &db,
340 &filter_policy); 342 &filter_policy);
341 343
342 if (!s.ok()) { 344 if (!s.ok()) {
343 LOG(ERROR) << "Failed to open in-memory LevelDB database: " << s.ToString(); 345 LOG(ERROR) << "Failed to open in-memory LevelDB database: " << s.ToString();
344 return std::unique_ptr<LevelDBDatabase>(); 346 return std::unique_ptr<LevelDBDatabase>();
345 } 347 }
346 348
347 std::unique_ptr<LevelDBDatabase> result(new LevelDBDatabase); 349 std::unique_ptr<LevelDBDatabase> result =
350 base::WrapUnique(new LevelDBDatabase());
348 result->env_ = std::move(in_memory_env); 351 result->env_ = std::move(in_memory_env);
349 result->db_ = base::WrapUnique(db); 352 result->db_ = std::move(db);
350 result->comparator_adapter_ = std::move(comparator_adapter); 353 result->comparator_adapter_ = std::move(comparator_adapter);
351 result->comparator_ = comparator; 354 result->comparator_ = comparator;
352 result->filter_policy_ = std::move(filter_policy); 355 result->filter_policy_ = std::move(filter_policy);
353 result->file_name_for_tracing = "in-memory-database"; 356 result->file_name_for_tracing = "in-memory-database";
354 357
355 return result; 358 return result;
356 } 359 }
357 360
358 leveldb::Status LevelDBDatabase::Put(const StringPiece& key, 361 leveldb::Status LevelDBDatabase::Put(const StringPiece& key,
359 std::string* value) { 362 std::string* value) {
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 dump->AddString("file_name", "", file_name_for_tracing); 471 dump->AddString("file_name", "", file_name_for_tracing);
469 472
470 // Memory is allocated from system allocator (malloc). 473 // Memory is allocated from system allocator (malloc).
471 pmd->AddSuballocation(dump->guid(), 474 pmd->AddSuballocation(dump->guid(),
472 base::trace_event::MemoryDumpManager::GetInstance() 475 base::trace_event::MemoryDumpManager::GetInstance()
473 ->system_allocator_pool_name()); 476 ->system_allocator_pool_name());
474 return true; 477 return true;
475 } 478 }
476 479
477 } // namespace content 480 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/indexed_db/indexed_db_unittest.cc ('k') | content/browser/indexed_db/leveldb/leveldb_transaction.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698