| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/drive_backend/metadata_database_index.
h" | 5 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index.
h" |
| 6 | 6 |
| 7 #include <tuple> | 7 #include <tuple> |
| 8 | 8 |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/threading/thread_restrictions.h" | 12 #include "base/threading/thread_restrictions.h" |
| 13 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.
h" | 13 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_constants.
h" |
| 14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h" | 14 #include "chrome/browser/sync_file_system/drive_backend/drive_backend_util.h" |
| 15 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" | 15 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" |
| 16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" | 16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.h" |
| 17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" | 17 #include "chrome/browser/sync_file_system/drive_backend/metadata_database.pb.h" |
| 18 #include "chrome/browser/sync_file_system/logger.h" | 18 #include "chrome/browser/sync_file_system/logger.h" |
| 19 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 19 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 20 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
| 21 | 21 |
| 22 // LevelDB database schema | 22 // LevelDB database schema |
| 23 // ======================= | 23 // ======================= |
| 24 // | 24 // |
| 25 // NOTE | 25 // NOTE |
| 26 // - Entries are sorted by keys. | 26 // - Entries are sorted by keys. |
| 27 // - int64 value is serialized as a string by base::Int64ToString(). | 27 // - int64_t value is serialized as a string by base::Int64ToString(). |
| 28 // - ServiceMetadata, FileMetadata, and FileTracker values are serialized | 28 // - ServiceMetadata, FileMetadata, and FileTracker values are serialized |
| 29 // as a string by SerializeToString() of protocol buffers. | 29 // as a string by SerializeToString() of protocol buffers. |
| 30 // | 30 // |
| 31 // Version 3 | 31 // Version 3 |
| 32 // # Version of this schema | 32 // # Version of this schema |
| 33 // key: "VERSION" | 33 // key: "VERSION" |
| 34 // value: "3" | 34 // value: "3" |
| 35 // | 35 // |
| 36 // # Metadata of the SyncFS service | 36 // # Metadata of the SyncFS service |
| 37 // key: "SERVICE" | 37 // key: "SERVICE" |
| 38 // value: <ServiceMetadata 'service_metadata'> | 38 // value: <ServiceMetadata 'service_metadata'> |
| 39 // | 39 // |
| 40 // # Metadata of remote files | 40 // # Metadata of remote files |
| 41 // key: "FILE: " + <string 'file_id'> | 41 // key: "FILE: " + <string 'file_id'> |
| 42 // value: <FileMetadata 'metadata'> | 42 // value: <FileMetadata 'metadata'> |
| 43 // | 43 // |
| 44 // # Trackers of local file updates | 44 // # Trackers of local file updates |
| 45 // key: "TRACKER: " + <int64 'tracker_id'> | 45 // key: "TRACKER: " + <int64_t 'tracker_id'> |
| 46 // value: <FileTracker 'tracker'> | 46 // value: <FileTracker 'tracker'> |
| 47 | 47 |
| 48 namespace sync_file_system { | 48 namespace sync_file_system { |
| 49 namespace drive_backend { | 49 namespace drive_backend { |
| 50 | 50 |
| 51 ParentIDAndTitle::ParentIDAndTitle() : parent_id(0) {} | 51 ParentIDAndTitle::ParentIDAndTitle() : parent_id(0) {} |
| 52 ParentIDAndTitle::ParentIDAndTitle(int64 parent_id, | 52 ParentIDAndTitle::ParentIDAndTitle(int64_t parent_id, const std::string& title) |
| 53 const std::string& title) | |
| 54 : parent_id(parent_id), title(title) {} | 53 : parent_id(parent_id), title(title) {} |
| 55 | 54 |
| 56 bool operator==(const ParentIDAndTitle& left, const ParentIDAndTitle& right) { | 55 bool operator==(const ParentIDAndTitle& left, const ParentIDAndTitle& right) { |
| 57 return left.parent_id == right.parent_id && left.title == right.title; | 56 return left.parent_id == right.parent_id && left.title == right.title; |
| 58 } | 57 } |
| 59 | 58 |
| 60 bool operator<(const ParentIDAndTitle& left, const ParentIDAndTitle& right) { | 59 bool operator<(const ParentIDAndTitle& left, const ParentIDAndTitle& right) { |
| 61 return std::tie(left.parent_id, left.title) < | 60 return std::tie(left.parent_id, left.title) < |
| 62 std::tie(right.parent_id, right.title); | 61 std::tie(right.parent_id, right.title); |
| 63 } | 62 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 "Failed to parse a FileMetadata"); | 94 "Failed to parse a FileMetadata"); |
| 96 continue; | 95 continue; |
| 97 } | 96 } |
| 98 | 97 |
| 99 contents->file_metadata.push_back(metadata.release()); | 98 contents->file_metadata.push_back(metadata.release()); |
| 100 continue; | 99 continue; |
| 101 } | 100 } |
| 102 | 101 |
| 103 std::string tracker_id_str; | 102 std::string tracker_id_str; |
| 104 if (RemovePrefix(key, kFileTrackerKeyPrefix, &tracker_id_str)) { | 103 if (RemovePrefix(key, kFileTrackerKeyPrefix, &tracker_id_str)) { |
| 105 int64 tracker_id = 0; | 104 int64_t tracker_id = 0; |
| 106 if (!base::StringToInt64(tracker_id_str, &tracker_id)) { | 105 if (!base::StringToInt64(tracker_id_str, &tracker_id)) { |
| 107 util::Log(logging::LOG_WARNING, FROM_HERE, | 106 util::Log(logging::LOG_WARNING, FROM_HERE, |
| 108 "Failed to parse TrackerID"); | 107 "Failed to parse TrackerID"); |
| 109 continue; | 108 continue; |
| 110 } | 109 } |
| 111 | 110 |
| 112 scoped_ptr<FileTracker> tracker(new FileTracker); | 111 scoped_ptr<FileTracker> tracker(new FileTracker); |
| 113 if (!tracker->ParseFromString(itr->value().ToString())) { | 112 if (!tracker->ParseFromString(itr->value().ToString())) { |
| 114 util::Log(logging::LOG_WARNING, FROM_HERE, | 113 util::Log(logging::LOG_WARNING, FROM_HERE, |
| 115 "Failed to parse a Tracker"); | 114 "Failed to parse a Tracker"); |
| 116 continue; | 115 continue; |
| 117 } | 116 } |
| 118 contents->file_trackers.push_back(tracker.release()); | 117 contents->file_trackers.push_back(tracker.release()); |
| 119 continue; | 118 continue; |
| 120 } | 119 } |
| 121 } | 120 } |
| 122 } | 121 } |
| 123 | 122 |
| 124 void RemoveUnreachableItemsFromDB(DatabaseContents* contents, | 123 void RemoveUnreachableItemsFromDB(DatabaseContents* contents, |
| 125 int64 sync_root_tracker_id, | 124 int64_t sync_root_tracker_id, |
| 126 LevelDBWrapper* db) { | 125 LevelDBWrapper* db) { |
| 127 typedef std::map<int64, std::set<int64> > ChildTrackersByParent; | 126 typedef std::map<int64_t, std::set<int64_t>> ChildTrackersByParent; |
| 128 ChildTrackersByParent trackers_by_parent; | 127 ChildTrackersByParent trackers_by_parent; |
| 129 | 128 |
| 130 // Set up links from parent tracker to child trackers. | 129 // Set up links from parent tracker to child trackers. |
| 131 for (size_t i = 0; i < contents->file_trackers.size(); ++i) { | 130 for (size_t i = 0; i < contents->file_trackers.size(); ++i) { |
| 132 const FileTracker& tracker = *contents->file_trackers[i]; | 131 const FileTracker& tracker = *contents->file_trackers[i]; |
| 133 int64 parent_tracker_id = tracker.parent_tracker_id(); | 132 int64_t parent_tracker_id = tracker.parent_tracker_id(); |
| 134 int64 tracker_id = tracker.tracker_id(); | 133 int64_t tracker_id = tracker.tracker_id(); |
| 135 | 134 |
| 136 trackers_by_parent[parent_tracker_id].insert(tracker_id); | 135 trackers_by_parent[parent_tracker_id].insert(tracker_id); |
| 137 } | 136 } |
| 138 | 137 |
| 139 // Drop links from inactive trackers. | 138 // Drop links from inactive trackers. |
| 140 for (size_t i = 0; i < contents->file_trackers.size(); ++i) { | 139 for (size_t i = 0; i < contents->file_trackers.size(); ++i) { |
| 141 const FileTracker& tracker = *contents->file_trackers[i]; | 140 const FileTracker& tracker = *contents->file_trackers[i]; |
| 142 | 141 |
| 143 if (!tracker.active()) | 142 if (!tracker.active()) |
| 144 trackers_by_parent.erase(tracker.tracker_id()); | 143 trackers_by_parent.erase(tracker.tracker_id()); |
| 145 } | 144 } |
| 146 | 145 |
| 147 std::vector<int64> pending; | 146 std::vector<int64_t> pending; |
| 148 if (sync_root_tracker_id != kInvalidTrackerID) | 147 if (sync_root_tracker_id != kInvalidTrackerID) |
| 149 pending.push_back(sync_root_tracker_id); | 148 pending.push_back(sync_root_tracker_id); |
| 150 | 149 |
| 151 // Traverse tracker tree from sync-root. | 150 // Traverse tracker tree from sync-root. |
| 152 std::set<int64> visited_trackers; | 151 std::set<int64_t> visited_trackers; |
| 153 while (!pending.empty()) { | 152 while (!pending.empty()) { |
| 154 int64 tracker_id = pending.back(); | 153 int64_t tracker_id = pending.back(); |
| 155 DCHECK_NE(kInvalidTrackerID, tracker_id); | 154 DCHECK_NE(kInvalidTrackerID, tracker_id); |
| 156 pending.pop_back(); | 155 pending.pop_back(); |
| 157 | 156 |
| 158 if (!visited_trackers.insert(tracker_id).second) { | 157 if (!visited_trackers.insert(tracker_id).second) { |
| 159 NOTREACHED(); | 158 NOTREACHED(); |
| 160 continue; | 159 continue; |
| 161 } | 160 } |
| 162 | 161 |
| 163 AppendContents( | 162 AppendContents( |
| 164 LookUpMap(trackers_by_parent, tracker_id, std::set<int64>()), | 163 LookUpMap(trackers_by_parent, tracker_id, std::set<int64_t>()), |
| 165 &pending); | 164 &pending); |
| 166 } | 165 } |
| 167 | 166 |
| 168 // Delete all unreachable trackers. | 167 // Delete all unreachable trackers. |
| 169 ScopedVector<FileTracker> reachable_trackers; | 168 ScopedVector<FileTracker> reachable_trackers; |
| 170 for (size_t i = 0; i < contents->file_trackers.size(); ++i) { | 169 for (size_t i = 0; i < contents->file_trackers.size(); ++i) { |
| 171 FileTracker* tracker = contents->file_trackers[i]; | 170 FileTracker* tracker = contents->file_trackers[i]; |
| 172 if (ContainsKey(visited_trackers, tracker->tracker_id())) { | 171 if (ContainsKey(visited_trackers, tracker->tracker_id())) { |
| 173 reachable_trackers.push_back(tracker); | 172 reachable_trackers.push_back(tracker); |
| 174 contents->file_trackers[i] = nullptr; | 173 contents->file_trackers[i] = nullptr; |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 bool MetadataDatabaseIndex::GetFileMetadata( | 259 bool MetadataDatabaseIndex::GetFileMetadata( |
| 261 const std::string& file_id, FileMetadata* metadata) const { | 260 const std::string& file_id, FileMetadata* metadata) const { |
| 262 FileMetadata* identified = metadata_by_id_.get(file_id); | 261 FileMetadata* identified = metadata_by_id_.get(file_id); |
| 263 if (!identified) | 262 if (!identified) |
| 264 return false; | 263 return false; |
| 265 if (metadata) | 264 if (metadata) |
| 266 metadata->CopyFrom(*identified); | 265 metadata->CopyFrom(*identified); |
| 267 return true; | 266 return true; |
| 268 } | 267 } |
| 269 | 268 |
| 270 bool MetadataDatabaseIndex::GetFileTracker( | 269 bool MetadataDatabaseIndex::GetFileTracker(int64_t tracker_id, |
| 271 int64 tracker_id, FileTracker* tracker) const { | 270 FileTracker* tracker) const { |
| 272 FileTracker* identified = tracker_by_id_.get(tracker_id); | 271 FileTracker* identified = tracker_by_id_.get(tracker_id); |
| 273 if (!identified) | 272 if (!identified) |
| 274 return false; | 273 return false; |
| 275 if (tracker) | 274 if (tracker) |
| 276 tracker->CopyFrom(*identified); | 275 tracker->CopyFrom(*identified); |
| 277 return true; | 276 return true; |
| 278 } | 277 } |
| 279 | 278 |
| 280 void MetadataDatabaseIndex::StoreFileMetadata( | 279 void MetadataDatabaseIndex::StoreFileMetadata( |
| 281 scoped_ptr<FileMetadata> metadata) { | 280 scoped_ptr<FileMetadata> metadata) { |
| 282 PutFileMetadataToDB(*metadata.get(), db_); | 281 PutFileMetadataToDB(*metadata.get(), db_); |
| 283 if (!metadata) { | 282 if (!metadata) { |
| 284 NOTREACHED(); | 283 NOTREACHED(); |
| 285 return; | 284 return; |
| 286 } | 285 } |
| 287 | 286 |
| 288 std::string file_id = metadata->file_id(); | 287 std::string file_id = metadata->file_id(); |
| 289 metadata_by_id_.set(file_id, metadata.Pass()); | 288 metadata_by_id_.set(file_id, metadata.Pass()); |
| 290 } | 289 } |
| 291 | 290 |
| 292 void MetadataDatabaseIndex::StoreFileTracker( | 291 void MetadataDatabaseIndex::StoreFileTracker( |
| 293 scoped_ptr<FileTracker> tracker) { | 292 scoped_ptr<FileTracker> tracker) { |
| 294 PutFileTrackerToDB(*tracker.get(), db_); | 293 PutFileTrackerToDB(*tracker.get(), db_); |
| 295 if (!tracker) { | 294 if (!tracker) { |
| 296 NOTREACHED(); | 295 NOTREACHED(); |
| 297 return; | 296 return; |
| 298 } | 297 } |
| 299 | 298 |
| 300 int64 tracker_id = tracker->tracker_id(); | 299 int64_t tracker_id = tracker->tracker_id(); |
| 301 FileTracker* old_tracker = tracker_by_id_.get(tracker_id); | 300 FileTracker* old_tracker = tracker_by_id_.get(tracker_id); |
| 302 | 301 |
| 303 if (!old_tracker) { | 302 if (!old_tracker) { |
| 304 DVLOG(3) << "Adding new tracker: " << tracker->tracker_id() | 303 DVLOG(3) << "Adding new tracker: " << tracker->tracker_id() |
| 305 << " " << GetTrackerTitle(*tracker); | 304 << " " << GetTrackerTitle(*tracker); |
| 306 | 305 |
| 307 AddToAppIDIndex(*tracker); | 306 AddToAppIDIndex(*tracker); |
| 308 AddToPathIndexes(*tracker); | 307 AddToPathIndexes(*tracker); |
| 309 AddToFileIDIndexes(*tracker); | 308 AddToFileIDIndexes(*tracker); |
| 310 AddToDirtyTrackerIndexes(*tracker); | 309 AddToDirtyTrackerIndexes(*tracker); |
| 311 } else { | 310 } else { |
| 312 DVLOG(3) << "Updating tracker: " << tracker->tracker_id() | 311 DVLOG(3) << "Updating tracker: " << tracker->tracker_id() |
| 313 << " " << GetTrackerTitle(*tracker); | 312 << " " << GetTrackerTitle(*tracker); |
| 314 | 313 |
| 315 UpdateInAppIDIndex(*old_tracker, *tracker); | 314 UpdateInAppIDIndex(*old_tracker, *tracker); |
| 316 UpdateInPathIndexes(*old_tracker, *tracker); | 315 UpdateInPathIndexes(*old_tracker, *tracker); |
| 317 UpdateInFileIDIndexes(*old_tracker, *tracker); | 316 UpdateInFileIDIndexes(*old_tracker, *tracker); |
| 318 UpdateInDirtyTrackerIndexes(*old_tracker, *tracker); | 317 UpdateInDirtyTrackerIndexes(*old_tracker, *tracker); |
| 319 } | 318 } |
| 320 | 319 |
| 321 tracker_by_id_.set(tracker_id, tracker.Pass()); | 320 tracker_by_id_.set(tracker_id, tracker.Pass()); |
| 322 } | 321 } |
| 323 | 322 |
| 324 void MetadataDatabaseIndex::RemoveFileMetadata(const std::string& file_id) { | 323 void MetadataDatabaseIndex::RemoveFileMetadata(const std::string& file_id) { |
| 325 PutFileMetadataDeletionToDB(file_id, db_); | 324 PutFileMetadataDeletionToDB(file_id, db_); |
| 326 metadata_by_id_.erase(file_id); | 325 metadata_by_id_.erase(file_id); |
| 327 } | 326 } |
| 328 | 327 |
| 329 void MetadataDatabaseIndex::RemoveFileTracker(int64 tracker_id) { | 328 void MetadataDatabaseIndex::RemoveFileTracker(int64_t tracker_id) { |
| 330 PutFileTrackerDeletionToDB(tracker_id, db_); | 329 PutFileTrackerDeletionToDB(tracker_id, db_); |
| 331 | 330 |
| 332 FileTracker* tracker = tracker_by_id_.get(tracker_id); | 331 FileTracker* tracker = tracker_by_id_.get(tracker_id); |
| 333 if (!tracker) { | 332 if (!tracker) { |
| 334 NOTREACHED(); | 333 NOTREACHED(); |
| 335 return; | 334 return; |
| 336 } | 335 } |
| 337 | 336 |
| 338 DVLOG(3) << "Removing tracker: " | 337 DVLOG(3) << "Removing tracker: " |
| 339 << tracker->tracker_id() << " " << GetTrackerTitle(*tracker); | 338 << tracker->tracker_id() << " " << GetTrackerTitle(*tracker); |
| 340 | 339 |
| 341 RemoveFromAppIDIndex(*tracker); | 340 RemoveFromAppIDIndex(*tracker); |
| 342 RemoveFromPathIndexes(*tracker); | 341 RemoveFromPathIndexes(*tracker); |
| 343 RemoveFromFileIDIndexes(*tracker); | 342 RemoveFromFileIDIndexes(*tracker); |
| 344 RemoveFromDirtyTrackerIndexes(*tracker); | 343 RemoveFromDirtyTrackerIndexes(*tracker); |
| 345 | 344 |
| 346 tracker_by_id_.erase(tracker_id); | 345 tracker_by_id_.erase(tracker_id); |
| 347 } | 346 } |
| 348 | 347 |
| 349 TrackerIDSet MetadataDatabaseIndex::GetFileTrackerIDsByFileID( | 348 TrackerIDSet MetadataDatabaseIndex::GetFileTrackerIDsByFileID( |
| 350 const std::string& file_id) const { | 349 const std::string& file_id) const { |
| 351 return FindItem(trackers_by_file_id_, file_id); | 350 return FindItem(trackers_by_file_id_, file_id); |
| 352 } | 351 } |
| 353 | 352 |
| 354 int64 MetadataDatabaseIndex::GetAppRootTracker( | 353 int64_t MetadataDatabaseIndex::GetAppRootTracker( |
| 355 const std::string& app_id) const { | 354 const std::string& app_id) const { |
| 356 return FindItem(app_root_by_app_id_, app_id); | 355 return FindItem(app_root_by_app_id_, app_id); |
| 357 } | 356 } |
| 358 | 357 |
| 359 TrackerIDSet MetadataDatabaseIndex::GetFileTrackerIDsByParentAndTitle( | 358 TrackerIDSet MetadataDatabaseIndex::GetFileTrackerIDsByParentAndTitle( |
| 360 int64 parent_tracker_id, | 359 int64_t parent_tracker_id, |
| 361 const std::string& title) const { | 360 const std::string& title) const { |
| 362 TrackerIDsByParentAndTitle::const_iterator found = | 361 TrackerIDsByParentAndTitle::const_iterator found = |
| 363 trackers_by_parent_and_title_.find(parent_tracker_id); | 362 trackers_by_parent_and_title_.find(parent_tracker_id); |
| 364 if (found == trackers_by_parent_and_title_.end()) | 363 if (found == trackers_by_parent_and_title_.end()) |
| 365 return TrackerIDSet(); | 364 return TrackerIDSet(); |
| 366 return FindItem(found->second, title); | 365 return FindItem(found->second, title); |
| 367 } | 366 } |
| 368 | 367 |
| 369 std::vector<int64> MetadataDatabaseIndex::GetFileTrackerIDsByParent( | 368 std::vector<int64_t> MetadataDatabaseIndex::GetFileTrackerIDsByParent( |
| 370 int64 parent_tracker_id) const { | 369 int64_t parent_tracker_id) const { |
| 371 std::vector<int64> result; | 370 std::vector<int64_t> result; |
| 372 TrackerIDsByParentAndTitle::const_iterator found = | 371 TrackerIDsByParentAndTitle::const_iterator found = |
| 373 trackers_by_parent_and_title_.find(parent_tracker_id); | 372 trackers_by_parent_and_title_.find(parent_tracker_id); |
| 374 if (found == trackers_by_parent_and_title_.end()) | 373 if (found == trackers_by_parent_and_title_.end()) |
| 375 return result; | 374 return result; |
| 376 | 375 |
| 377 for (TrackerIDsByTitle::const_iterator itr = found->second.begin(); | 376 for (TrackerIDsByTitle::const_iterator itr = found->second.begin(); |
| 378 itr != found->second.end(); ++itr) { | 377 itr != found->second.end(); ++itr) { |
| 379 result.insert(result.end(), itr->second.begin(), itr->second.end()); | 378 result.insert(result.end(), itr->second.begin(), itr->second.end()); |
| 380 } | 379 } |
| 381 | 380 |
| 382 return result; | 381 return result; |
| 383 } | 382 } |
| 384 | 383 |
| 385 std::string MetadataDatabaseIndex::PickMultiTrackerFileID() const { | 384 std::string MetadataDatabaseIndex::PickMultiTrackerFileID() const { |
| 386 if (multi_tracker_file_ids_.empty()) | 385 if (multi_tracker_file_ids_.empty()) |
| 387 return std::string(); | 386 return std::string(); |
| 388 return *multi_tracker_file_ids_.begin(); | 387 return *multi_tracker_file_ids_.begin(); |
| 389 } | 388 } |
| 390 | 389 |
| 391 ParentIDAndTitle MetadataDatabaseIndex::PickMultiBackingFilePath() const { | 390 ParentIDAndTitle MetadataDatabaseIndex::PickMultiBackingFilePath() const { |
| 392 if (multi_backing_file_paths_.empty()) | 391 if (multi_backing_file_paths_.empty()) |
| 393 return ParentIDAndTitle(kInvalidTrackerID, std::string()); | 392 return ParentIDAndTitle(kInvalidTrackerID, std::string()); |
| 394 return *multi_backing_file_paths_.begin(); | 393 return *multi_backing_file_paths_.begin(); |
| 395 } | 394 } |
| 396 | 395 |
| 397 int64 MetadataDatabaseIndex::PickDirtyTracker() const { | 396 int64_t MetadataDatabaseIndex::PickDirtyTracker() const { |
| 398 if (dirty_trackers_.empty()) | 397 if (dirty_trackers_.empty()) |
| 399 return kInvalidTrackerID; | 398 return kInvalidTrackerID; |
| 400 return *dirty_trackers_.begin(); | 399 return *dirty_trackers_.begin(); |
| 401 } | 400 } |
| 402 | 401 |
| 403 void MetadataDatabaseIndex::DemoteDirtyTracker(int64 tracker_id) { | 402 void MetadataDatabaseIndex::DemoteDirtyTracker(int64_t tracker_id) { |
| 404 if (dirty_trackers_.erase(tracker_id)) | 403 if (dirty_trackers_.erase(tracker_id)) |
| 405 demoted_dirty_trackers_.insert(tracker_id); | 404 demoted_dirty_trackers_.insert(tracker_id); |
| 406 } | 405 } |
| 407 | 406 |
| 408 bool MetadataDatabaseIndex::HasDemotedDirtyTracker() const { | 407 bool MetadataDatabaseIndex::HasDemotedDirtyTracker() const { |
| 409 return !demoted_dirty_trackers_.empty(); | 408 return !demoted_dirty_trackers_.empty(); |
| 410 } | 409 } |
| 411 | 410 |
| 412 bool MetadataDatabaseIndex::IsDemotedDirtyTracker(int64 tracker_id) const { | 411 bool MetadataDatabaseIndex::IsDemotedDirtyTracker(int64_t tracker_id) const { |
| 413 return demoted_dirty_trackers_.find(tracker_id) != | 412 return demoted_dirty_trackers_.find(tracker_id) != |
| 414 demoted_dirty_trackers_.end(); | 413 demoted_dirty_trackers_.end(); |
| 415 } | 414 } |
| 416 | 415 |
| 417 void MetadataDatabaseIndex::PromoteDemotedDirtyTracker(int64 tracker_id) { | 416 void MetadataDatabaseIndex::PromoteDemotedDirtyTracker(int64_t tracker_id) { |
| 418 if (demoted_dirty_trackers_.erase(tracker_id) == 1) | 417 if (demoted_dirty_trackers_.erase(tracker_id) == 1) |
| 419 dirty_trackers_.insert(tracker_id); | 418 dirty_trackers_.insert(tracker_id); |
| 420 } | 419 } |
| 421 | 420 |
| 422 bool MetadataDatabaseIndex::PromoteDemotedDirtyTrackers() { | 421 bool MetadataDatabaseIndex::PromoteDemotedDirtyTrackers() { |
| 423 bool promoted = !demoted_dirty_trackers_.empty(); | 422 bool promoted = !demoted_dirty_trackers_.empty(); |
| 424 dirty_trackers_.insert(demoted_dirty_trackers_.begin(), | 423 dirty_trackers_.insert(demoted_dirty_trackers_.begin(), |
| 425 demoted_dirty_trackers_.end()); | 424 demoted_dirty_trackers_.end()); |
| 426 demoted_dirty_trackers_.clear(); | 425 demoted_dirty_trackers_.clear(); |
| 427 return promoted; | 426 return promoted; |
| 428 } | 427 } |
| 429 | 428 |
| 430 size_t MetadataDatabaseIndex::CountDirtyTracker() const { | 429 size_t MetadataDatabaseIndex::CountDirtyTracker() const { |
| 431 return dirty_trackers_.size(); | 430 return dirty_trackers_.size(); |
| 432 } | 431 } |
| 433 | 432 |
| 434 size_t MetadataDatabaseIndex::CountFileMetadata() const { | 433 size_t MetadataDatabaseIndex::CountFileMetadata() const { |
| 435 return metadata_by_id_.size(); | 434 return metadata_by_id_.size(); |
| 436 } | 435 } |
| 437 | 436 |
| 438 size_t MetadataDatabaseIndex::CountFileTracker() const { | 437 size_t MetadataDatabaseIndex::CountFileTracker() const { |
| 439 return tracker_by_id_.size(); | 438 return tracker_by_id_.size(); |
| 440 } | 439 } |
| 441 | 440 |
| 442 void MetadataDatabaseIndex::SetSyncRootRevalidated() const { | 441 void MetadataDatabaseIndex::SetSyncRootRevalidated() const { |
| 443 service_metadata_->set_sync_root_revalidated(true); | 442 service_metadata_->set_sync_root_revalidated(true); |
| 444 PutServiceMetadataToDB(*service_metadata_, db_); | 443 PutServiceMetadataToDB(*service_metadata_, db_); |
| 445 } | 444 } |
| 446 | 445 |
| 447 void MetadataDatabaseIndex::SetSyncRootTrackerID( | 446 void MetadataDatabaseIndex::SetSyncRootTrackerID(int64_t sync_root_id) const { |
| 448 int64 sync_root_id) const { | |
| 449 service_metadata_->set_sync_root_tracker_id(sync_root_id); | 447 service_metadata_->set_sync_root_tracker_id(sync_root_id); |
| 450 PutServiceMetadataToDB(*service_metadata_, db_); | 448 PutServiceMetadataToDB(*service_metadata_, db_); |
| 451 } | 449 } |
| 452 | 450 |
| 453 void MetadataDatabaseIndex::SetLargestChangeID( | 451 void MetadataDatabaseIndex::SetLargestChangeID( |
| 454 int64 largest_change_id) const { | 452 int64_t largest_change_id) const { |
| 455 service_metadata_->set_largest_change_id(largest_change_id); | 453 service_metadata_->set_largest_change_id(largest_change_id); |
| 456 PutServiceMetadataToDB(*service_metadata_, db_); | 454 PutServiceMetadataToDB(*service_metadata_, db_); |
| 457 } | 455 } |
| 458 | 456 |
| 459 void MetadataDatabaseIndex::SetNextTrackerID( | 457 void MetadataDatabaseIndex::SetNextTrackerID(int64_t next_tracker_id) const { |
| 460 int64 next_tracker_id) const { | |
| 461 service_metadata_->set_next_tracker_id(next_tracker_id); | 458 service_metadata_->set_next_tracker_id(next_tracker_id); |
| 462 PutServiceMetadataToDB(*service_metadata_, db_); | 459 PutServiceMetadataToDB(*service_metadata_, db_); |
| 463 } | 460 } |
| 464 | 461 |
| 465 bool MetadataDatabaseIndex::IsSyncRootRevalidated() const { | 462 bool MetadataDatabaseIndex::IsSyncRootRevalidated() const { |
| 466 return service_metadata_->has_sync_root_revalidated() && | 463 return service_metadata_->has_sync_root_revalidated() && |
| 467 service_metadata_->sync_root_revalidated(); | 464 service_metadata_->sync_root_revalidated(); |
| 468 } | 465 } |
| 469 | 466 |
| 470 int64 MetadataDatabaseIndex::GetSyncRootTrackerID() const { | 467 int64_t MetadataDatabaseIndex::GetSyncRootTrackerID() const { |
| 471 if (!service_metadata_->has_sync_root_tracker_id()) | 468 if (!service_metadata_->has_sync_root_tracker_id()) |
| 472 return kInvalidTrackerID; | 469 return kInvalidTrackerID; |
| 473 return service_metadata_->sync_root_tracker_id(); | 470 return service_metadata_->sync_root_tracker_id(); |
| 474 } | 471 } |
| 475 | 472 |
| 476 int64 MetadataDatabaseIndex::GetLargestChangeID() const { | 473 int64_t MetadataDatabaseIndex::GetLargestChangeID() const { |
| 477 if (!service_metadata_->has_largest_change_id()) | 474 if (!service_metadata_->has_largest_change_id()) |
| 478 return kInvalidTrackerID; | 475 return kInvalidTrackerID; |
| 479 return service_metadata_->largest_change_id(); | 476 return service_metadata_->largest_change_id(); |
| 480 } | 477 } |
| 481 | 478 |
| 482 int64 MetadataDatabaseIndex::GetNextTrackerID() const { | 479 int64_t MetadataDatabaseIndex::GetNextTrackerID() const { |
| 483 if (!service_metadata_->has_next_tracker_id()) { | 480 if (!service_metadata_->has_next_tracker_id()) { |
| 484 NOTREACHED(); | 481 NOTREACHED(); |
| 485 return kInvalidTrackerID; | 482 return kInvalidTrackerID; |
| 486 } | 483 } |
| 487 return service_metadata_->next_tracker_id(); | 484 return service_metadata_->next_tracker_id(); |
| 488 } | 485 } |
| 489 | 486 |
| 490 std::vector<std::string> MetadataDatabaseIndex::GetRegisteredAppIDs() const { | 487 std::vector<std::string> MetadataDatabaseIndex::GetRegisteredAppIDs() const { |
| 491 std::vector<std::string> result; | 488 std::vector<std::string> result; |
| 492 result.reserve(app_root_by_app_id_.size()); | 489 result.reserve(app_root_by_app_id_.size()); |
| 493 for (TrackerIDByAppID::const_iterator itr = app_root_by_app_id_.begin(); | 490 for (TrackerIDByAppID::const_iterator itr = app_root_by_app_id_.begin(); |
| 494 itr != app_root_by_app_id_.end(); ++itr) | 491 itr != app_root_by_app_id_.end(); ++itr) |
| 495 result.push_back(itr->first); | 492 result.push_back(itr->first); |
| 496 return result; | 493 return result; |
| 497 } | 494 } |
| 498 | 495 |
| 499 std::vector<int64> MetadataDatabaseIndex::GetAllTrackerIDs() const { | 496 std::vector<int64_t> MetadataDatabaseIndex::GetAllTrackerIDs() const { |
| 500 std::vector<int64> result; | 497 std::vector<int64_t> result; |
| 501 for (TrackerByID::const_iterator itr = tracker_by_id_.begin(); | 498 for (TrackerByID::const_iterator itr = tracker_by_id_.begin(); |
| 502 itr != tracker_by_id_.end(); ++itr) { | 499 itr != tracker_by_id_.end(); ++itr) { |
| 503 result.push_back(itr->first); | 500 result.push_back(itr->first); |
| 504 } | 501 } |
| 505 return result; | 502 return result; |
| 506 } | 503 } |
| 507 | 504 |
| 508 std::vector<std::string> MetadataDatabaseIndex::GetAllMetadataIDs() const { | 505 std::vector<std::string> MetadataDatabaseIndex::GetAllMetadataIDs() const { |
| 509 std::vector<std::string> result; | 506 std::vector<std::string> result; |
| 510 for (MetadataByID::const_iterator itr = metadata_by_id_.begin(); | 507 for (MetadataByID::const_iterator itr = metadata_by_id_.begin(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 << " Remove from multi_tracker_file_ids_: " << tracker.file_id(); | 605 << " Remove from multi_tracker_file_ids_: " << tracker.file_id(); |
| 609 multi_tracker_file_ids_.erase(tracker.file_id()); | 606 multi_tracker_file_ids_.erase(tracker.file_id()); |
| 610 } | 607 } |
| 611 | 608 |
| 612 if (found->second.empty()) | 609 if (found->second.empty()) |
| 613 trackers_by_file_id_.erase(found); | 610 trackers_by_file_id_.erase(found); |
| 614 } | 611 } |
| 615 | 612 |
| 616 void MetadataDatabaseIndex::AddToPathIndexes( | 613 void MetadataDatabaseIndex::AddToPathIndexes( |
| 617 const FileTracker& new_tracker) { | 614 const FileTracker& new_tracker) { |
| 618 int64 parent = new_tracker.parent_tracker_id(); | 615 int64_t parent = new_tracker.parent_tracker_id(); |
| 619 std::string title = GetTrackerTitle(new_tracker); | 616 std::string title = GetTrackerTitle(new_tracker); |
| 620 | 617 |
| 621 DVLOG(3) << " Add to trackers_by_parent_and_title_: " | 618 DVLOG(3) << " Add to trackers_by_parent_and_title_: " |
| 622 << parent << " " << title; | 619 << parent << " " << title; |
| 623 | 620 |
| 624 trackers_by_parent_and_title_[parent][title].Insert(new_tracker); | 621 trackers_by_parent_and_title_[parent][title].Insert(new_tracker); |
| 625 | 622 |
| 626 if (trackers_by_parent_and_title_[parent][title].size() > 1 && | 623 if (trackers_by_parent_and_title_[parent][title].size() > 1 && |
| 627 !title.empty()) { | 624 !title.empty()) { |
| 628 DVLOG_IF(3, !ContainsKey(multi_backing_file_paths_, | 625 DVLOG_IF(3, !ContainsKey(multi_backing_file_paths_, |
| 629 ParentIDAndTitle(parent, title))) | 626 ParentIDAndTitle(parent, title))) |
| 630 << " Add to multi_backing_file_paths_: " << parent << " " << title; | 627 << " Add to multi_backing_file_paths_: " << parent << " " << title; |
| 631 multi_backing_file_paths_.insert(ParentIDAndTitle(parent, title)); | 628 multi_backing_file_paths_.insert(ParentIDAndTitle(parent, title)); |
| 632 } | 629 } |
| 633 } | 630 } |
| 634 | 631 |
| 635 void MetadataDatabaseIndex::UpdateInPathIndexes( | 632 void MetadataDatabaseIndex::UpdateInPathIndexes( |
| 636 const FileTracker& old_tracker, | 633 const FileTracker& old_tracker, |
| 637 const FileTracker& new_tracker) { | 634 const FileTracker& new_tracker) { |
| 638 DCHECK_EQ(old_tracker.tracker_id(), new_tracker.tracker_id()); | 635 DCHECK_EQ(old_tracker.tracker_id(), new_tracker.tracker_id()); |
| 639 DCHECK_EQ(old_tracker.parent_tracker_id(), new_tracker.parent_tracker_id()); | 636 DCHECK_EQ(old_tracker.parent_tracker_id(), new_tracker.parent_tracker_id()); |
| 640 DCHECK(GetTrackerTitle(old_tracker) == GetTrackerTitle(new_tracker) || | 637 DCHECK(GetTrackerTitle(old_tracker) == GetTrackerTitle(new_tracker) || |
| 641 !old_tracker.has_synced_details()); | 638 !old_tracker.has_synced_details()); |
| 642 | 639 |
| 643 int64 tracker_id = new_tracker.tracker_id(); | 640 int64_t tracker_id = new_tracker.tracker_id(); |
| 644 int64 parent = new_tracker.parent_tracker_id(); | 641 int64_t parent = new_tracker.parent_tracker_id(); |
| 645 std::string old_title = GetTrackerTitle(old_tracker); | 642 std::string old_title = GetTrackerTitle(old_tracker); |
| 646 std::string title = GetTrackerTitle(new_tracker); | 643 std::string title = GetTrackerTitle(new_tracker); |
| 647 | 644 |
| 648 TrackerIDsByTitle* trackers_by_title = &trackers_by_parent_and_title_[parent]; | 645 TrackerIDsByTitle* trackers_by_title = &trackers_by_parent_and_title_[parent]; |
| 649 | 646 |
| 650 if (old_title != title) { | 647 if (old_title != title) { |
| 651 TrackerIDsByTitle::iterator found = trackers_by_title->find(old_title); | 648 TrackerIDsByTitle::iterator found = trackers_by_title->find(old_title); |
| 652 if (found != trackers_by_title->end()) { | 649 if (found != trackers_by_title->end()) { |
| 653 DVLOG(3) << " Remove from trackers_by_parent_and_title_: " | 650 DVLOG(3) << " Remove from trackers_by_parent_and_title_: " |
| 654 << parent << " " << old_title; | 651 << parent << " " << old_title; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 } | 683 } |
| 687 | 684 |
| 688 if (old_tracker.active() && !new_tracker.active()) | 685 if (old_tracker.active() && !new_tracker.active()) |
| 689 trackers_by_parent_and_title_[parent][title].Deactivate(tracker_id); | 686 trackers_by_parent_and_title_[parent][title].Deactivate(tracker_id); |
| 690 else if (!old_tracker.active() && new_tracker.active()) | 687 else if (!old_tracker.active() && new_tracker.active()) |
| 691 trackers_by_parent_and_title_[parent][title].Activate(tracker_id); | 688 trackers_by_parent_and_title_[parent][title].Activate(tracker_id); |
| 692 } | 689 } |
| 693 | 690 |
| 694 void MetadataDatabaseIndex::RemoveFromPathIndexes( | 691 void MetadataDatabaseIndex::RemoveFromPathIndexes( |
| 695 const FileTracker& tracker) { | 692 const FileTracker& tracker) { |
| 696 int64 tracker_id = tracker.tracker_id(); | 693 int64_t tracker_id = tracker.tracker_id(); |
| 697 int64 parent = tracker.parent_tracker_id(); | 694 int64_t parent = tracker.parent_tracker_id(); |
| 698 std::string title = GetTrackerTitle(tracker); | 695 std::string title = GetTrackerTitle(tracker); |
| 699 | 696 |
| 700 DCHECK(ContainsKey(trackers_by_parent_and_title_, parent)); | 697 DCHECK(ContainsKey(trackers_by_parent_and_title_, parent)); |
| 701 DCHECK(ContainsKey(trackers_by_parent_and_title_[parent], title)); | 698 DCHECK(ContainsKey(trackers_by_parent_and_title_[parent], title)); |
| 702 | 699 |
| 703 DVLOG(3) << " Remove from trackers_by_parent_and_title_: " | 700 DVLOG(3) << " Remove from trackers_by_parent_and_title_: " |
| 704 << parent << " " << title; | 701 << parent << " " << title; |
| 705 | 702 |
| 706 trackers_by_parent_and_title_[parent][title].Erase(tracker_id); | 703 trackers_by_parent_and_title_[parent][title].Erase(tracker_id); |
| 707 | 704 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 730 DVLOG(3) << " Add to dirty_trackers_: " << new_tracker.tracker_id(); | 727 DVLOG(3) << " Add to dirty_trackers_: " << new_tracker.tracker_id(); |
| 731 dirty_trackers_.insert(new_tracker.tracker_id()); | 728 dirty_trackers_.insert(new_tracker.tracker_id()); |
| 732 } | 729 } |
| 733 } | 730 } |
| 734 | 731 |
| 735 void MetadataDatabaseIndex::UpdateInDirtyTrackerIndexes( | 732 void MetadataDatabaseIndex::UpdateInDirtyTrackerIndexes( |
| 736 const FileTracker& old_tracker, | 733 const FileTracker& old_tracker, |
| 737 const FileTracker& new_tracker) { | 734 const FileTracker& new_tracker) { |
| 738 DCHECK_EQ(old_tracker.tracker_id(), new_tracker.tracker_id()); | 735 DCHECK_EQ(old_tracker.tracker_id(), new_tracker.tracker_id()); |
| 739 | 736 |
| 740 int64 tracker_id = new_tracker.tracker_id(); | 737 int64_t tracker_id = new_tracker.tracker_id(); |
| 741 if (old_tracker.dirty() && !new_tracker.dirty()) { | 738 if (old_tracker.dirty() && !new_tracker.dirty()) { |
| 742 DCHECK(ContainsKey(dirty_trackers_, tracker_id) || | 739 DCHECK(ContainsKey(dirty_trackers_, tracker_id) || |
| 743 ContainsKey(demoted_dirty_trackers_, tracker_id)); | 740 ContainsKey(demoted_dirty_trackers_, tracker_id)); |
| 744 | 741 |
| 745 DVLOG(3) << " Remove from dirty_trackers_: " << tracker_id; | 742 DVLOG(3) << " Remove from dirty_trackers_: " << tracker_id; |
| 746 | 743 |
| 747 dirty_trackers_.erase(tracker_id); | 744 dirty_trackers_.erase(tracker_id); |
| 748 demoted_dirty_trackers_.erase(tracker_id); | 745 demoted_dirty_trackers_.erase(tracker_id); |
| 749 } else if (!old_tracker.dirty() && new_tracker.dirty()) { | 746 } else if (!old_tracker.dirty() && new_tracker.dirty()) { |
| 750 DCHECK(!ContainsKey(dirty_trackers_, tracker_id)); | 747 DCHECK(!ContainsKey(dirty_trackers_, tracker_id)); |
| 751 DCHECK(!ContainsKey(demoted_dirty_trackers_, tracker_id)); | 748 DCHECK(!ContainsKey(demoted_dirty_trackers_, tracker_id)); |
| 752 | 749 |
| 753 DVLOG(3) << " Add to dirty_trackers_: " << tracker_id; | 750 DVLOG(3) << " Add to dirty_trackers_: " << tracker_id; |
| 754 | 751 |
| 755 dirty_trackers_.insert(tracker_id); | 752 dirty_trackers_.insert(tracker_id); |
| 756 } | 753 } |
| 757 } | 754 } |
| 758 | 755 |
| 759 void MetadataDatabaseIndex::RemoveFromDirtyTrackerIndexes( | 756 void MetadataDatabaseIndex::RemoveFromDirtyTrackerIndexes( |
| 760 const FileTracker& tracker) { | 757 const FileTracker& tracker) { |
| 761 if (tracker.dirty()) { | 758 if (tracker.dirty()) { |
| 762 int64 tracker_id = tracker.tracker_id(); | 759 int64_t tracker_id = tracker.tracker_id(); |
| 763 DCHECK(ContainsKey(dirty_trackers_, tracker_id) || | 760 DCHECK(ContainsKey(dirty_trackers_, tracker_id) || |
| 764 ContainsKey(demoted_dirty_trackers_, tracker_id)); | 761 ContainsKey(demoted_dirty_trackers_, tracker_id)); |
| 765 | 762 |
| 766 DVLOG(3) << " Remove from dirty_trackers_: " << tracker_id; | 763 DVLOG(3) << " Remove from dirty_trackers_: " << tracker_id; |
| 767 dirty_trackers_.erase(tracker_id); | 764 dirty_trackers_.erase(tracker_id); |
| 768 | 765 |
| 769 demoted_dirty_trackers_.erase(tracker_id); | 766 demoted_dirty_trackers_.erase(tracker_id); |
| 770 } | 767 } |
| 771 } | 768 } |
| 772 | 769 |
| 773 } // namespace drive_backend | 770 } // namespace drive_backend |
| 774 } // namespace sync_file_system | 771 } // namespace sync_file_system |
| OLD | NEW |