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
, FileMetadata
> MetadataByID
;
99 typedef base::ScopedPtrHashMap
<int64
, FileTracker
> TrackerByID
;
100 typedef base::hash_map
<std::string
, TrackerIDSet
> TrackerIDsByFileID
;
101 typedef base::hash_map
<std::string
, TrackerIDSet
> TrackerIDsByTitle
;
102 typedef std::map
<int64
, TrackerIDsByTitle
> TrackerIDsByParentAndTitle
;
103 typedef base::hash_map
<std::string
, int64
> TrackerIDByAppID
;
104 typedef base::hash_set
<std::string
> FileIDSet
;
105 typedef base::hash_set
<ParentIDAndTitle
> PathSet
;
106 typedef std::set
<int64
> DirtyTrackers
;
108 friend class MetadataDatabaseTest
;
110 explicit MetadataDatabaseIndex(LevelDBWrapper
* db
);
111 void Initialize(scoped_ptr
<ServiceMetadata
> service_metadata
,
112 DatabaseContents
* contents
);
114 // Maintains |app_root_by_app_id_|.
115 void AddToAppIDIndex(const FileTracker
& new_tracker
);
116 void UpdateInAppIDIndex(const FileTracker
& old_tracker
,
117 const FileTracker
& new_tracker
);
118 void RemoveFromAppIDIndex(const FileTracker
& tracker
);
120 // Maintains |trackers_by_file_id_| and |multi_tracker_file_ids_|.
121 void AddToFileIDIndexes(const FileTracker
& new_tracker
);
122 void UpdateInFileIDIndexes(const FileTracker
& old_tracker
,
123 const FileTracker
& new_tracker
);
124 void RemoveFromFileIDIndexes(const FileTracker
& tracker
);
126 // Maintains |trackers_by_parent_and_title_| and |multi_backing_file_paths_|.
127 void AddToPathIndexes(const FileTracker
& new_tracker
);
128 void UpdateInPathIndexes(const FileTracker
& old_tracker
,
129 const FileTracker
& new_tracker1
);
130 void RemoveFromPathIndexes(const FileTracker
& tracker
);
132 // Maintains |dirty_trackers_| and |demoted_dirty_trackers_|.
133 void AddToDirtyTrackerIndexes(const FileTracker
& new_tracker
);
134 void UpdateInDirtyTrackerIndexes(const FileTracker
& old_tracker
,
135 const FileTracker
& new_tracker
);
136 void RemoveFromDirtyTrackerIndexes(const FileTracker
& tracker
);
138 scoped_ptr
<ServiceMetadata
> service_metadata_
;
139 LevelDBWrapper
* db_
; // Not owned
141 MetadataByID metadata_by_id_
;
142 TrackerByID tracker_by_id_
;
144 TrackerIDByAppID app_root_by_app_id_
;
146 TrackerIDsByFileID trackers_by_file_id_
;
147 FileIDSet multi_tracker_file_ids_
;
149 TrackerIDsByParentAndTitle trackers_by_parent_and_title_
;
150 PathSet multi_backing_file_paths_
;
152 DirtyTrackers dirty_trackers_
;
153 DirtyTrackers demoted_dirty_trackers_
;
155 DISALLOW_COPY_AND_ASSIGN(MetadataDatabaseIndex
);
158 } // namespace drive_backend
159 } // namespace sync_file_system
161 #endif // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_H_