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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_H_
6 #define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_H_
13 #include "base/containers/hash_tables.h"
14 #include "base/containers/scoped_ptr_hash_map.h"
15 #include "base/memory/scoped_vector.h"
16 #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_interface.h"
17 #include "chrome/browser/sync_file_system/drive_backend/tracker_id_set.h"
19 namespace sync_file_system
{
20 namespace drive_backend
{
25 class ServiceMetadata
;
27 } // namespace drive_backend
28 } // namespace sync_file_system
30 namespace BASE_HASH_NAMESPACE
{
32 template<> struct hash
<sync_file_system::drive_backend::ParentIDAndTitle
> {
33 std::size_t operator()(
34 const sync_file_system::drive_backend::ParentIDAndTitle
& v
) const {
35 return base::HashInts64(v
.parent_id
, hash
<std::string
>()(v
.title
));
39 } // namespace BASE_HASH_NAMESPACE
41 namespace sync_file_system
{
42 namespace drive_backend
{
44 struct DatabaseContents
{
47 ScopedVector
<FileMetadata
> file_metadata
;
48 ScopedVector
<FileTracker
> file_trackers
;
51 // Maintains indexes of MetadataDatabase on memory.
52 class MetadataDatabaseIndex
: public MetadataDatabaseIndexInterface
{
54 ~MetadataDatabaseIndex() override
;
56 static scoped_ptr
<MetadataDatabaseIndex
> Create(LevelDBWrapper
* db
);
57 static scoped_ptr
<MetadataDatabaseIndex
> CreateForTesting(
58 DatabaseContents
* contents
, LevelDBWrapper
* db
);
60 // MetadataDatabaseIndexInterface overrides.
61 bool GetFileMetadata(const std::string
& file_id
,
62 FileMetadata
* metadata
) const override
;
63 bool GetFileTracker(int64 tracker_id
, FileTracker
* tracker
) const override
;
64 void StoreFileMetadata(scoped_ptr
<FileMetadata
> metadata
) override
;
65 void StoreFileTracker(scoped_ptr
<FileTracker
> tracker
) override
;
66 void RemoveFileMetadata(const std::string
& file_id
) override
;
67 void RemoveFileTracker(int64 tracker_id
) override
;
68 TrackerIDSet
GetFileTrackerIDsByFileID(
69 const std::string
& file_id
) const override
;
70 int64
GetAppRootTracker(const std::string
& app_id
) const override
;
71 TrackerIDSet
GetFileTrackerIDsByParentAndTitle(
72 int64 parent_tracker_id
,
73 const std::string
& title
) const override
;
74 std::vector
<int64
> GetFileTrackerIDsByParent(
75 int64 parent_tracker_id
) const override
;
76 std::string
PickMultiTrackerFileID() const override
;
77 ParentIDAndTitle
PickMultiBackingFilePath() const override
;
78 int64
PickDirtyTracker() const override
;
79 void DemoteDirtyTracker(int64 tracker_id
) override
;
80 bool HasDemotedDirtyTracker() const override
;
81 bool IsDemotedDirtyTracker(int64 tracker_id
) const override
;
82 void PromoteDemotedDirtyTracker(int64 tracker_id
) override
;
83 bool PromoteDemotedDirtyTrackers() override
;
84 size_t CountDirtyTracker() const override
;
85 size_t CountFileMetadata() const override
;
86 size_t CountFileTracker() const override
;
87 void SetSyncRootTrackerID(int64 sync_root_id
) const override
;
88 void SetLargestChangeID(int64 largest_change_id
) const override
;
89 void SetNextTrackerID(int64 next_tracker_id
) const override
;
90 int64
GetSyncRootTrackerID() const override
;
91 int64
GetLargestChangeID() const override
;
92 int64
GetNextTrackerID() const override
;
93 std::vector
<std::string
> GetRegisteredAppIDs() const override
;
94 std::vector
<int64
> GetAllTrackerIDs() const override
;
95 std::vector
<std::string
> GetAllMetadataIDs() const override
;
98 typedef base::ScopedPtrHashMap
<std::string
, scoped_ptr
<FileMetadata
>>
100 typedef base::ScopedPtrHashMap
<int64
, scoped_ptr
<FileTracker
>> TrackerByID
;
101 typedef base::hash_map
<std::string
, TrackerIDSet
> TrackerIDsByFileID
;
102 typedef base::hash_map
<std::string
, TrackerIDSet
> TrackerIDsByTitle
;
103 typedef std::map
<int64
, TrackerIDsByTitle
> TrackerIDsByParentAndTitle
;
104 typedef base::hash_map
<std::string
, int64
> TrackerIDByAppID
;
105 typedef base::hash_set
<std::string
> FileIDSet
;
106 typedef base::hash_set
<ParentIDAndTitle
> PathSet
;
107 typedef std::set
<int64
> DirtyTrackers
;
109 friend class MetadataDatabaseTest
;
111 explicit MetadataDatabaseIndex(LevelDBWrapper
* db
);
112 void Initialize(scoped_ptr
<ServiceMetadata
> service_metadata
,
113 DatabaseContents
* contents
);
115 // Maintains |app_root_by_app_id_|.
116 void AddToAppIDIndex(const FileTracker
& new_tracker
);
117 void UpdateInAppIDIndex(const FileTracker
& old_tracker
,
118 const FileTracker
& new_tracker
);
119 void RemoveFromAppIDIndex(const FileTracker
& tracker
);
121 // Maintains |trackers_by_file_id_| and |multi_tracker_file_ids_|.
122 void AddToFileIDIndexes(const FileTracker
& new_tracker
);
123 void UpdateInFileIDIndexes(const FileTracker
& old_tracker
,
124 const FileTracker
& new_tracker
);
125 void RemoveFromFileIDIndexes(const FileTracker
& tracker
);
127 // Maintains |trackers_by_parent_and_title_| and |multi_backing_file_paths_|.
128 void AddToPathIndexes(const FileTracker
& new_tracker
);
129 void UpdateInPathIndexes(const FileTracker
& old_tracker
,
130 const FileTracker
& new_tracker1
);
131 void RemoveFromPathIndexes(const FileTracker
& tracker
);
133 // Maintains |dirty_trackers_| and |demoted_dirty_trackers_|.
134 void AddToDirtyTrackerIndexes(const FileTracker
& new_tracker
);
135 void UpdateInDirtyTrackerIndexes(const FileTracker
& old_tracker
,
136 const FileTracker
& new_tracker
);
137 void RemoveFromDirtyTrackerIndexes(const FileTracker
& tracker
);
139 scoped_ptr
<ServiceMetadata
> service_metadata_
;
140 LevelDBWrapper
* db_
; // Not owned
142 MetadataByID metadata_by_id_
;
143 TrackerByID tracker_by_id_
;
145 TrackerIDByAppID app_root_by_app_id_
;
147 TrackerIDsByFileID trackers_by_file_id_
;
148 FileIDSet multi_tracker_file_ids_
;
150 TrackerIDsByParentAndTitle trackers_by_parent_and_title_
;
151 PathSet multi_backing_file_paths_
;
153 DirtyTrackers dirty_trackers_
;
154 DirtyTrackers demoted_dirty_trackers_
;
156 DISALLOW_COPY_AND_ASSIGN(MetadataDatabaseIndex
);
159 } // namespace drive_backend
160 } // namespace sync_file_system
162 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_H_