Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved. | 1 // Copyright (c) 2011 The LevelDB 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. See the AUTHORS file for names of contributors. | 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. |
| 4 | 4 |
| 5 #include "third_party/leveldatabase/env_chromium.h" | |
| 6 | |
| 7 #if defined(OS_WIN) | |
| 8 #include <io.h> | |
| 9 #endif | |
| 10 | |
| 5 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
| 6 #include "base/file_util.h" | 12 #include "base/file_util.h" |
| 7 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 8 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 10 #include "env_chromium_stdio.h" | 16 #include "third_party/leveldatabase/env_chromium_stdio.h" |
| 11 #include "third_party/re2/re2/re2.h" | 17 #include "third_party/re2/re2/re2.h" |
| 12 | 18 |
| 13 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
| 14 #include <io.h> | |
| 15 #include "base/command_line.h" | 20 #include "base/command_line.h" |
| 16 #include "base/win/win_util.h" | 21 #include "base/win/win_util.h" |
| 17 #include "env_chromium_win.h" | 22 #include "third_parth/leveldatabase/env_chromium_win.h" |
|
jsbell
2014/07/23 19:09:09
typo: third_part**h**
| |
| 18 #endif | 23 #endif |
| 19 | 24 |
| 20 using namespace leveldb; | 25 using leveldb::FileLock; |
| 26 using leveldb::Slice; | |
| 27 using leveldb::Status; | |
| 21 | 28 |
| 22 namespace leveldb_env { | 29 namespace leveldb_env { |
| 23 | 30 |
| 24 namespace { | 31 namespace { |
| 25 | 32 |
| 26 const base::FilePath::CharType backup_table_extension[] = | 33 const base::FilePath::CharType backup_table_extension[] = |
| 27 FILE_PATH_LITERAL(".bak"); | 34 FILE_PATH_LITERAL(".bak"); |
| 28 const base::FilePath::CharType table_extension[] = FILE_PATH_LITERAL(".ldb"); | 35 const base::FilePath::CharType table_extension[] = FILE_PATH_LITERAL(".ldb"); |
| 29 | 36 |
| 30 static const base::FilePath::CharType kLevelDBTestDirectoryPrefix[] | 37 static const base::FilePath::CharType kLevelDBTestDirectoryPrefix[] |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 45 last_(start_), | 52 last_(start_), |
| 46 time_to_sleep_(base::TimeDelta::FromMilliseconds(10)), | 53 time_to_sleep_(base::TimeDelta::FromMilliseconds(10)), |
| 47 success_(true), | 54 success_(true), |
| 48 method_(method), | 55 method_(method), |
| 49 last_error_(base::File::FILE_OK), | 56 last_error_(base::File::FILE_OK), |
| 50 provider_(provider) {} | 57 provider_(provider) {} |
| 51 ~Retrier() { | 58 ~Retrier() { |
| 52 if (success_) { | 59 if (success_) { |
| 53 provider_->GetRetryTimeHistogram(method_)->AddTime(last_ - start_); | 60 provider_->GetRetryTimeHistogram(method_)->AddTime(last_ - start_); |
| 54 if (last_error_ != base::File::FILE_OK) { | 61 if (last_error_ != base::File::FILE_OK) { |
| 55 DCHECK(last_error_ < 0); | 62 DCHECK_LT(last_error_, 0); |
| 56 provider_->GetRecoveredFromErrorHistogram(method_)->Add(-last_error_); | 63 provider_->GetRecoveredFromErrorHistogram(method_)->Add(-last_error_); |
| 57 } | 64 } |
| 58 } | 65 } |
| 59 } | 66 } |
| 60 bool ShouldKeepTrying(base::File::Error last_error) { | 67 bool ShouldKeepTrying(base::File::Error last_error) { |
| 61 DCHECK_NE(last_error, base::File::FILE_OK); | 68 DCHECK_NE(last_error, base::File::FILE_OK); |
| 62 last_error_ = last_error; | 69 last_error_ = last_error; |
| 63 if (last_ < limit_) { | 70 if (last_ < limit_) { |
| 64 base::PlatformThread::Sleep(time_to_sleep_); | 71 base::PlatformThread::Sleep(time_to_sleep_); |
| 65 last_ = base::TimeTicks::Now(); | 72 last_ = base::TimeTicks::Now(); |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 method, | 182 method, |
| 176 MethodIDToString(method), | 183 MethodIDToString(method), |
| 177 saved_errno); | 184 saved_errno); |
| 178 return Status::IOError(filename, buf); | 185 return Status::IOError(filename, buf); |
| 179 } | 186 } |
| 180 | 187 |
| 181 Status MakeIOError(Slice filename, | 188 Status MakeIOError(Slice filename, |
| 182 const char* message, | 189 const char* message, |
| 183 MethodID method, | 190 MethodID method, |
| 184 base::File::Error error) { | 191 base::File::Error error) { |
| 185 DCHECK(error < 0); | 192 DCHECK_LT(error, 0); |
| 186 char buf[512]; | 193 char buf[512]; |
| 187 snprintf(buf, | 194 snprintf(buf, |
| 188 sizeof(buf), | 195 sizeof(buf), |
| 189 "%s (ChromeMethodPFE: %d::%s::%d)", | 196 "%s (ChromeMethodPFE: %d::%s::%d)", |
| 190 message, | 197 message, |
| 191 method, | 198 method, |
| 192 MethodIDToString(method), | 199 MethodIDToString(method), |
| 193 -error); | 200 -error); |
| 194 return Status::IOError(filename, buf); | 201 return Status::IOError(filename, buf); |
| 195 } | 202 } |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 #endif | 344 #endif |
| 338 } | 345 } |
| 339 | 346 |
| 340 bool ChromiumEnv::MakeBackup(const std::string& fname) { | 347 bool ChromiumEnv::MakeBackup(const std::string& fname) { |
| 341 base::FilePath original_table_name = CreateFilePath(fname); | 348 base::FilePath original_table_name = CreateFilePath(fname); |
| 342 base::FilePath backup_table_name = | 349 base::FilePath backup_table_name = |
| 343 original_table_name.ReplaceExtension(backup_table_extension); | 350 original_table_name.ReplaceExtension(backup_table_extension); |
| 344 return base::CopyFile(original_table_name, backup_table_name); | 351 return base::CopyFile(original_table_name, backup_table_name); |
| 345 } | 352 } |
| 346 | 353 |
| 347 bool ChromiumEnv::HasTableExtension(const base::FilePath& path) | 354 bool ChromiumEnv::HasTableExtension(const base::FilePath& path) { |
| 348 { | |
| 349 return path.MatchesExtension(table_extension); | 355 return path.MatchesExtension(table_extension); |
| 350 } | 356 } |
| 351 | 357 |
| 352 ChromiumEnv::ChromiumEnv() | 358 ChromiumEnv::ChromiumEnv() |
| 353 : name_("LevelDBEnv"), | 359 : name_("LevelDBEnv"), |
| 354 make_backup_(false), | 360 make_backup_(false), |
| 355 bgsignal_(&mu_), | 361 bgsignal_(&mu_), |
| 356 started_bgthread_(false), | 362 started_bgthread_(false), |
| 357 kMaxRetryTimeMillis(1000) { | 363 kMaxRetryTimeMillis(1000) { |
| 358 } | 364 } |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 666 void ChromiumEnv::RecordErrorAt(MethodID method) const { | 672 void ChromiumEnv::RecordErrorAt(MethodID method) const { |
| 667 GetMethodIOErrorHistogram()->Add(method); | 673 GetMethodIOErrorHistogram()->Add(method); |
| 668 } | 674 } |
| 669 | 675 |
| 670 void ChromiumEnv::RecordLockFileAncestors(int num_missing_ancestors) const { | 676 void ChromiumEnv::RecordLockFileAncestors(int num_missing_ancestors) const { |
| 671 GetLockFileAncestorHistogram()->Add(num_missing_ancestors); | 677 GetLockFileAncestorHistogram()->Add(num_missing_ancestors); |
| 672 } | 678 } |
| 673 | 679 |
| 674 void ChromiumEnv::RecordOSError(MethodID method, | 680 void ChromiumEnv::RecordOSError(MethodID method, |
| 675 base::File::Error error) const { | 681 base::File::Error error) const { |
| 676 DCHECK(error < 0); | 682 DCHECK_LT(error, 0); |
| 677 RecordErrorAt(method); | 683 RecordErrorAt(method); |
| 678 GetOSErrorHistogram(method, -base::File::FILE_ERROR_MAX)->Add(-error); | 684 GetOSErrorHistogram(method, -base::File::FILE_ERROR_MAX)->Add(-error); |
| 679 } | 685 } |
| 680 | 686 |
| 681 void ChromiumEnv::RecordOSError(MethodID method, int error) const { | 687 void ChromiumEnv::RecordOSError(MethodID method, int error) const { |
| 682 DCHECK(error > 0); | 688 DCHECK_GT(error, 0); |
| 683 RecordErrorAt(method); | 689 RecordErrorAt(method); |
| 684 GetOSErrorHistogram(method, ERANGE + 1)->Add(error); | 690 GetOSErrorHistogram(method, ERANGE + 1)->Add(error); |
| 685 } | 691 } |
| 686 | 692 |
| 687 void ChromiumEnv::RecordBackupResult(bool result) const { | 693 void ChromiumEnv::RecordBackupResult(bool result) const { |
| 688 std::string uma_name(name_); | 694 std::string uma_name(name_); |
| 689 uma_name.append(".TableBackup"); | 695 uma_name.append(".TableBackup"); |
| 690 base::BooleanHistogram::FactoryGet( | 696 base::BooleanHistogram::FactoryGet( |
| 691 uma_name, base::Histogram::kUmaTargetedHistogramFlag)->AddBoolean(result); | 697 uma_name, base::Histogram::kUmaTargetedHistogramFlag)->AddBoolean(result); |
| 692 } | 698 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 767 virtual void ThreadMain() { | 773 virtual void ThreadMain() { |
| 768 (*function_)(arg_); | 774 (*function_)(arg_); |
| 769 delete this; | 775 delete this; |
| 770 } | 776 } |
| 771 | 777 |
| 772 private: | 778 private: |
| 773 void (*function_)(void* arg); | 779 void (*function_)(void* arg); |
| 774 void* arg_; | 780 void* arg_; |
| 775 }; | 781 }; |
| 776 | 782 |
| 777 void ChromiumEnv::Schedule(void (*function)(void*), void* arg) { | 783 void ChromiumEnv::Schedule(ScheduleFunc* function, void* arg) { |
| 778 mu_.Acquire(); | 784 mu_.Acquire(); |
| 779 | 785 |
| 780 // Start background thread if necessary | 786 // Start background thread if necessary |
| 781 if (!started_bgthread_) { | 787 if (!started_bgthread_) { |
| 782 started_bgthread_ = true; | 788 started_bgthread_ = true; |
| 783 StartThread(&ChromiumEnv::BGThreadWrapper, this); | 789 StartThread(&ChromiumEnv::BGThreadWrapper, this); |
| 784 } | 790 } |
| 785 | 791 |
| 786 // If the queue is currently empty, the background thread may currently be | 792 // If the queue is currently empty, the background thread may currently be |
| 787 // waiting. | 793 // waiting. |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 811 void* arg = queue_.front().arg; | 817 void* arg = queue_.front().arg; |
| 812 queue_.pop_front(); | 818 queue_.pop_front(); |
| 813 | 819 |
| 814 mu_.Release(); | 820 mu_.Release(); |
| 815 TRACE_EVENT0("leveldb", "ChromiumEnv::BGThread-Task"); | 821 TRACE_EVENT0("leveldb", "ChromiumEnv::BGThread-Task"); |
| 816 (*function)(arg); | 822 (*function)(arg); |
| 817 } | 823 } |
| 818 } | 824 } |
| 819 | 825 |
| 820 void ChromiumEnv::StartThread(void (*function)(void* arg), void* arg) { | 826 void ChromiumEnv::StartThread(void (*function)(void* arg), void* arg) { |
| 821 new Thread(function, arg); // Will self-delete. | 827 new Thread(function, arg); // Will self-delete. |
| 822 } | 828 } |
| 823 | 829 |
| 824 static std::string GetDirName(const std::string& filename) { | 830 static std::string GetDirName(const std::string& filename) { |
| 825 base::FilePath file = base::FilePath::FromUTF8Unsafe(filename); | 831 base::FilePath file = base::FilePath::FromUTF8Unsafe(filename); |
| 826 return FilePathToString(file.DirName()); | 832 return FilePathToString(file.DirName()); |
| 827 } | 833 } |
| 828 | 834 |
| 829 void ChromiumEnv::DidCreateNewFile(const std::string& filename) { | 835 void ChromiumEnv::DidCreateNewFile(const std::string& filename) { |
| 830 base::AutoLock auto_lock(map_lock_); | 836 base::AutoLock auto_lock(map_lock_); |
| 831 needs_sync_map_[GetDirName(filename)] = true; | 837 needs_sync_map_[GetDirName(filename)] = true; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 848 Env* IDBEnv() { | 854 Env* IDBEnv() { |
| 849 return leveldb_env::idb_env.Pointer(); | 855 return leveldb_env::idb_env.Pointer(); |
| 850 } | 856 } |
| 851 | 857 |
| 852 Env* Env::Default() { | 858 Env* Env::Default() { |
| 853 return leveldb_env::default_env.Pointer(); | 859 return leveldb_env::default_env.Pointer(); |
| 854 } | 860 } |
| 855 | 861 |
| 856 } // namespace leveldb | 862 } // namespace leveldb |
| 857 | 863 |
| OLD | NEW |