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

Side by Side Diff: chrome/browser/sync_file_system/local/local_file_change_tracker.cc

Issue 145693005: [FileAPI] Replace default leveldb::Env with leveldb::MemEnv in tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 10 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 | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 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 "chrome/browser/sync_file_system/local/local_file_change_tracker.h" 5 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h"
6 6
7 #include <queue> 7 #include <queue>
8 8
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/sequenced_task_runner.h" 11 #include "base/sequenced_task_runner.h"
12 #include "base/stl_util.h" 12 #include "base/stl_util.h"
13 #include "chrome/browser/sync_file_system/local/local_file_sync_status.h" 13 #include "chrome/browser/sync_file_system/local/local_file_sync_status.h"
14 #include "chrome/browser/sync_file_system/syncable_file_system_util.h" 14 #include "chrome/browser/sync_file_system/syncable_file_system_util.h"
15 #include "third_party/leveldatabase/src/helpers/memenv/memenv.h"
15 #include "third_party/leveldatabase/src/include/leveldb/db.h" 16 #include "third_party/leveldatabase/src/include/leveldb/db.h"
17 #include "third_party/leveldatabase/src/include/leveldb/env.h"
16 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" 18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
17 #include "webkit/browser/fileapi/file_system_context.h" 19 #include "webkit/browser/fileapi/file_system_context.h"
18 #include "webkit/browser/fileapi/file_system_file_util.h" 20 #include "webkit/browser/fileapi/file_system_file_util.h"
19 #include "webkit/browser/fileapi/file_system_operation_context.h" 21 #include "webkit/browser/fileapi/file_system_operation_context.h"
20 #include "webkit/common/fileapi/file_system_util.h" 22 #include "webkit/common/fileapi/file_system_util.h"
21 23
22 using fileapi::FileSystemContext; 24 using fileapi::FileSystemContext;
23 using fileapi::FileSystemFileUtil; 25 using fileapi::FileSystemFileUtil;
24 using fileapi::FileSystemOperationContext; 26 using fileapi::FileSystemOperationContext;
25 using fileapi::FileSystemURL; 27 using fileapi::FileSystemURL;
26 using fileapi::FileSystemURLSet; 28 using fileapi::FileSystemURLSet;
27 29
28 namespace sync_file_system { 30 namespace sync_file_system {
29 31
30 namespace { 32 namespace {
31 const base::FilePath::CharType kDatabaseName[] = 33 const base::FilePath::CharType kDatabaseName[] =
32 FILE_PATH_LITERAL("LocalFileChangeTracker"); 34 FILE_PATH_LITERAL("LocalFileChangeTracker");
33 const char kMark[] = "d"; 35 const char kMark[] = "d";
34 } // namespace 36 } // namespace
35 37
36 // A database class that stores local file changes in a local database. This 38 // A database class that stores local file changes in a local database. This
37 // object must be destructed on file_task_runner. 39 // object must be destructed on file_task_runner.
38 class LocalFileChangeTracker::TrackerDB { 40 class LocalFileChangeTracker::TrackerDB {
39 public: 41 public:
40 explicit TrackerDB(const base::FilePath& base_path); 42 TrackerDB(const base::FilePath& base_path,
43 leveldb::Env* env_override);
41 44
42 SyncStatusCode MarkDirty(const std::string& url); 45 SyncStatusCode MarkDirty(const std::string& url);
43 SyncStatusCode ClearDirty(const std::string& url); 46 SyncStatusCode ClearDirty(const std::string& url);
44 SyncStatusCode GetDirtyEntries( 47 SyncStatusCode GetDirtyEntries(
45 std::queue<FileSystemURL>* dirty_files); 48 std::queue<FileSystemURL>* dirty_files);
46 SyncStatusCode WriteBatch(scoped_ptr<leveldb::WriteBatch> batch); 49 SyncStatusCode WriteBatch(scoped_ptr<leveldb::WriteBatch> batch);
47 50
48 private: 51 private:
49 enum RecoveryOption { 52 enum RecoveryOption {
50 REPAIR_ON_CORRUPTION, 53 REPAIR_ON_CORRUPTION,
51 FAIL_ON_CORRUPTION, 54 FAIL_ON_CORRUPTION,
52 }; 55 };
53 56
54 SyncStatusCode Init(RecoveryOption recovery_option); 57 SyncStatusCode Init(RecoveryOption recovery_option);
55 SyncStatusCode Repair(const std::string& db_path); 58 SyncStatusCode Repair(const std::string& db_path);
56 void HandleError(const tracked_objects::Location& from_here, 59 void HandleError(const tracked_objects::Location& from_here,
57 const leveldb::Status& status); 60 const leveldb::Status& status);
58 61
59 const base::FilePath base_path_; 62 const base::FilePath base_path_;
63 leveldb::Env* env_override_;
60 scoped_ptr<leveldb::DB> db_; 64 scoped_ptr<leveldb::DB> db_;
61 SyncStatusCode db_status_; 65 SyncStatusCode db_status_;
62 66
63 DISALLOW_COPY_AND_ASSIGN(TrackerDB); 67 DISALLOW_COPY_AND_ASSIGN(TrackerDB);
64 }; 68 };
65 69
66 LocalFileChangeTracker::ChangeInfo::ChangeInfo() : change_seq(-1) {} 70 LocalFileChangeTracker::ChangeInfo::ChangeInfo() : change_seq(-1) {}
67 LocalFileChangeTracker::ChangeInfo::~ChangeInfo() {} 71 LocalFileChangeTracker::ChangeInfo::~ChangeInfo() {}
68 72
69 // LocalFileChangeTracker ------------------------------------------------------ 73 // LocalFileChangeTracker ------------------------------------------------------
70 74
71 LocalFileChangeTracker::LocalFileChangeTracker( 75 LocalFileChangeTracker::LocalFileChangeTracker(
72 const base::FilePath& base_path, 76 const base::FilePath& base_path,
77 leveldb::Env* env_override,
73 base::SequencedTaskRunner* file_task_runner) 78 base::SequencedTaskRunner* file_task_runner)
74 : initialized_(false), 79 : initialized_(false),
75 file_task_runner_(file_task_runner), 80 file_task_runner_(file_task_runner),
76 tracker_db_(new TrackerDB(base_path)), 81 tracker_db_(new TrackerDB(base_path, env_override)),
77 current_change_seq_(0), 82 current_change_seq_(0),
78 num_changes_(0) { 83 num_changes_(0) {
79 } 84 }
80 85
81 LocalFileChangeTracker::~LocalFileChangeTracker() { 86 LocalFileChangeTracker::~LocalFileChangeTracker() {
82 DCHECK(file_task_runner_->RunsTasksOnCurrentThread()); 87 DCHECK(file_task_runner_->RunsTasksOnCurrentThread());
83 tracker_db_.reset(); 88 tracker_db_.reset();
84 } 89 }
85 90
86 void LocalFileChangeTracker::OnStartUpdate(const FileSystemURL& url) { 91 void LocalFileChangeTracker::OnStartUpdate(const FileSystemURL& url) {
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 changes->erase(url); 421 changes->erase(url);
417 return; 422 return;
418 } 423 }
419 info.change_seq = new_change_seq; 424 info.change_seq = new_change_seq;
420 if (change_seqs) 425 if (change_seqs)
421 (*change_seqs)[info.change_seq] = url; 426 (*change_seqs)[info.change_seq] = url;
422 } 427 }
423 428
424 // TrackerDB ------------------------------------------------------------------- 429 // TrackerDB -------------------------------------------------------------------
425 430
426 LocalFileChangeTracker::TrackerDB::TrackerDB(const base::FilePath& base_path) 431 LocalFileChangeTracker::TrackerDB::TrackerDB(const base::FilePath& base_path,
432 leveldb::Env* env_override)
427 : base_path_(base_path), 433 : base_path_(base_path),
434 env_override_(env_override),
428 db_status_(SYNC_STATUS_OK) {} 435 db_status_(SYNC_STATUS_OK) {}
429 436
430 SyncStatusCode LocalFileChangeTracker::TrackerDB::Init( 437 SyncStatusCode LocalFileChangeTracker::TrackerDB::Init(
431 RecoveryOption recovery_option) { 438 RecoveryOption recovery_option) {
432 if (db_.get() && db_status_ == SYNC_STATUS_OK) 439 if (db_.get() && db_status_ == SYNC_STATUS_OK)
433 return SYNC_STATUS_OK; 440 return SYNC_STATUS_OK;
434 441
435 std::string path = fileapi::FilePathToString( 442 std::string path = fileapi::FilePathToString(
436 base_path_.Append(kDatabaseName)); 443 base_path_.Append(kDatabaseName));
437 leveldb::Options options; 444 leveldb::Options options;
438 options.max_open_files = 0; // Use minimum. 445 options.max_open_files = 0; // Use minimum.
439 options.create_if_missing = true; 446 options.create_if_missing = true;
447 if (env_override_)
448 options.env = env_override_;
440 leveldb::DB* db; 449 leveldb::DB* db;
441 leveldb::Status status = leveldb::DB::Open(options, path, &db); 450 leveldb::Status status = leveldb::DB::Open(options, path, &db);
442 if (status.ok()) { 451 if (status.ok()) {
443 db_.reset(db); 452 db_.reset(db);
444 return SYNC_STATUS_OK; 453 return SYNC_STATUS_OK;
445 } 454 }
446 455
447 HandleError(FROM_HERE, status); 456 HandleError(FROM_HERE, status);
448 if (!status.IsCorruption()) 457 if (!status.IsCorruption())
449 return LevelDBStatusToSyncStatusCode(status); 458 return LevelDBStatusToSyncStatusCode(status);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
564 if (!status.ok() && !status.IsNotFound()) { 573 if (!status.ok() && !status.IsNotFound()) {
565 HandleError(FROM_HERE, status); 574 HandleError(FROM_HERE, status);
566 db_status_ = LevelDBStatusToSyncStatusCode(status); 575 db_status_ = LevelDBStatusToSyncStatusCode(status);
567 db_.reset(); 576 db_.reset();
568 return db_status_; 577 return db_status_;
569 } 578 }
570 return SYNC_STATUS_OK; 579 return SYNC_STATUS_OK;
571 } 580 }
572 581
573 } // namespace sync_file_system 582 } // namespace sync_file_system
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698