| OLD | NEW |
| 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/chromeos/drive/resource_metadata_storage.h" | 5 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | |
| 8 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 8 #include "base/logging.h" |
| 10 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 11 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
| 12 #include "chrome/browser/chromeos/drive/drive.pb.h" | 11 #include "chrome/browser/chromeos/drive/drive.pb.h" |
| 13 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 12 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 14 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 13 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 15 | 14 |
| 16 namespace drive { | 15 namespace drive { |
| 17 | 16 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 return DB_INIT_NOT_FOUND; | 57 return DB_INIT_NOT_FOUND; |
| 59 if (status.IsCorruption()) | 58 if (status.IsCorruption()) |
| 60 return DB_INIT_CORRUPTION; | 59 return DB_INIT_CORRUPTION; |
| 61 if (status.IsIOError()) | 60 if (status.IsIOError()) |
| 62 return DB_INIT_IO_ERROR; | 61 return DB_INIT_IO_ERROR; |
| 63 return DB_INIT_FAILED; | 62 return DB_INIT_FAILED; |
| 64 } | 63 } |
| 65 | 64 |
| 66 } // namespace | 65 } // namespace |
| 67 | 66 |
| 67 ResourceMetadataStorage::Iterator::Iterator(scoped_ptr<leveldb::Iterator> it) |
| 68 : it_(it.Pass()) { |
| 69 DCHECK(it_); |
| 70 |
| 71 // Skip the header entry. |
| 72 // Note: The header entry comes before all other entries because its key |
| 73 // starts with kDBKeyDelimeter. (i.e. '\0') |
| 74 it_->Seek(leveldb::Slice(GetHeaderDBKey())); |
| 75 |
| 76 Advance(); |
| 77 } |
| 78 |
| 79 ResourceMetadataStorage::Iterator::~Iterator() { |
| 80 } |
| 81 |
| 82 bool ResourceMetadataStorage::Iterator::IsAtEnd() const { |
| 83 return !it_->Valid(); |
| 84 } |
| 85 |
| 86 const ResourceEntry& ResourceMetadataStorage::Iterator::Get() const { |
| 87 DCHECK(!IsAtEnd()); |
| 88 return entry_; |
| 89 } |
| 90 |
| 91 void ResourceMetadataStorage::Iterator::Advance() { |
| 92 DCHECK(!IsAtEnd()); |
| 93 |
| 94 for (it_->Next() ; it_->Valid(); it_->Next()) { |
| 95 if (!IsChildEntryKey(it_->key()) && |
| 96 entry_.ParseFromArray(it_->value().data(), it_->value().size())) |
| 97 break; |
| 98 } |
| 99 } |
| 100 |
| 101 bool ResourceMetadataStorage::Iterator::HasError() const { |
| 102 return !it_->status().ok(); |
| 103 } |
| 104 |
| 68 ResourceMetadataStorage::ResourceMetadataStorage( | 105 ResourceMetadataStorage::ResourceMetadataStorage( |
| 69 const base::FilePath& directory_path) | 106 const base::FilePath& directory_path) |
| 70 : directory_path_(directory_path) { | 107 : directory_path_(directory_path) { |
| 71 } | 108 } |
| 72 | 109 |
| 73 ResourceMetadataStorage::~ResourceMetadataStorage() { | 110 ResourceMetadataStorage::~ResourceMetadataStorage() { |
| 74 base::ThreadRestrictions::AssertIOAllowed(); | 111 base::ThreadRestrictions::AssertIOAllowed(); |
| 75 } | 112 } |
| 76 | 113 |
| 77 bool ResourceMetadataStorage::Initialize() { | 114 bool ResourceMetadataStorage::Initialize() { |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 entry->base_name())); | 279 entry->base_name())); |
| 243 } | 280 } |
| 244 // Remove the entry itself. | 281 // Remove the entry itself. |
| 245 batch.Delete(resource_id); | 282 batch.Delete(resource_id); |
| 246 | 283 |
| 247 const leveldb::Status status = resource_map_->Write(leveldb::WriteOptions(), | 284 const leveldb::Status status = resource_map_->Write(leveldb::WriteOptions(), |
| 248 &batch); | 285 &batch); |
| 249 return status.ok(); | 286 return status.ok(); |
| 250 } | 287 } |
| 251 | 288 |
| 252 void ResourceMetadataStorage::Iterate(const IterateCallback& callback) { | 289 scoped_ptr<ResourceMetadataStorage::Iterator> |
| 290 ResourceMetadataStorage::GetIterator() { |
| 253 base::ThreadRestrictions::AssertIOAllowed(); | 291 base::ThreadRestrictions::AssertIOAllowed(); |
| 254 DCHECK(!callback.is_null()); | |
| 255 | 292 |
| 256 scoped_ptr<leveldb::Iterator> it( | 293 scoped_ptr<leveldb::Iterator> it( |
| 257 resource_map_->NewIterator(leveldb::ReadOptions())); | 294 resource_map_->NewIterator(leveldb::ReadOptions())); |
| 258 | 295 return make_scoped_ptr(new Iterator(it.Pass())); |
| 259 // Skip the header entry. | |
| 260 // Note: The header entry comes before all other entries because its key | |
| 261 // starts with kDBKeyDelimeter. (i.e. '\0') | |
| 262 it->Seek(leveldb::Slice(GetHeaderDBKey())); | |
| 263 it->Next(); | |
| 264 | |
| 265 ResourceEntry entry; | |
| 266 for (; it->Valid(); it->Next()) { | |
| 267 if (!IsChildEntryKey(it->key()) && | |
| 268 entry.ParseFromArray(it->value().data(), it->value().size())) | |
| 269 callback.Run(entry); | |
| 270 } | |
| 271 } | 296 } |
| 272 | 297 |
| 273 std::string ResourceMetadataStorage::GetChild( | 298 std::string ResourceMetadataStorage::GetChild( |
| 274 const std::string& parent_resource_id, | 299 const std::string& parent_resource_id, |
| 275 const std::string& child_name) { | 300 const std::string& child_name) { |
| 276 base::ThreadRestrictions::AssertIOAllowed(); | 301 base::ThreadRestrictions::AssertIOAllowed(); |
| 277 | 302 |
| 278 std::string child_resource_id; | 303 std::string child_resource_id; |
| 279 resource_map_->Get( | 304 resource_map_->Get( |
| 280 leveldb::ReadOptions(), | 305 leveldb::ReadOptions(), |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 } | 438 } |
| 414 if (!it->status().ok() || num_child_entries != num_entries_with_parent) { | 439 if (!it->status().ok() || num_child_entries != num_entries_with_parent) { |
| 415 DLOG(ERROR) << "Error during checking resource map. status = " | 440 DLOG(ERROR) << "Error during checking resource map. status = " |
| 416 << it->status().ToString(); | 441 << it->status().ToString(); |
| 417 return false; | 442 return false; |
| 418 } | 443 } |
| 419 return true; | 444 return true; |
| 420 } | 445 } |
| 421 | 446 |
| 422 } // namespace drive | 447 } // namespace drive |
| OLD | NEW |