metadata_database_index.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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.
4
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_
7
8#include <map>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/containers/hash_tables.h"
14#include "base/containers/scoped_ptr_hash_map.h"
15#include "chrome/browser/sync_file_system/drive_backend/metadata_database.h"
16
17namespace sync_file_system {
18namespace drive_backend {
19
20struct ParentIDAndTitle {
21  int64 parent_id;
22  std::string title;
23
24  ParentIDAndTitle();
25  ParentIDAndTitle(int64 parent_id, const std::string& title);
26};
27
28bool operator==(const ParentIDAndTitle& left, const ParentIDAndTitle& right);
29bool operator<(const ParentIDAndTitle& left, const ParentIDAndTitle& right);
30
31}  // namespace drive_backend
32}  // namespace sync_file_system
33
34namespace BASE_HASH_NAMESPACE {
35
36#if defined(COMPILER_GCC)
37template<> struct hash<sync_file_system::drive_backend::ParentIDAndTitle> {
38  std::size_t operator()(
39      const sync_file_system::drive_backend::ParentIDAndTitle& v) const {
40    return base::HashInts64(v.parent_id, hash<std::string>()(v.title));
41  }
42};
43#elif defined(COMPILER_MSVC)
44inline size_t hash_value(
45    const sync_file_system::drive_backend::ParentIDAndTitle& v) {
46  return base::HashInts64(v.parent_id, hash_value(v.title));
47}
48#endif  // COMPILER
49
50}  // namespace BASE_HASH_NAMESPACE
51
52namespace sync_file_system {
53namespace drive_backend {
54
55class FileMetadata;
56class FileTracker;
57struct DatabaseContents;
58
59// Maintains indexes of MetadataDatabase.  This class doesn't modify database
60// entries on memory nor on disk.
61class MetadataDatabaseIndex {
62 public:
63  explicit MetadataDatabaseIndex(DatabaseContents* content);
64  ~MetadataDatabaseIndex();
65
66  // Returns FileMetadata identified by |file_id| if exists, otherwise returns
67  // NULL.
68  const FileMetadata* GetFileMetadata(const std::string& file_id) const;
69
70  // Returns FileTracker identified by |tracker_id| if exists, otherwise returns
71  // NULL.
72  const FileTracker* GetFileTracker(int64 tracker_id) const;
73
74  // Stores |metadata| and updates indexes.
75  // This overwrites existing FileMetadata for the same |file_id|.
76  void StoreFileMetadata(scoped_ptr<FileMetadata> metadata);
77
78  // Stores |tracker| and updates indexes.
79  // This overwrites existing FileTracker for the same |tracker_id|.
80  void StoreFileTracker(scoped_ptr<FileTracker> tracker);
81
82  // Removes FileMetadata identified by |file_id| from indexes.
83  void RemoveFileMetadata(const std::string& file_id);
84
85  // Removes FileTracker identified by |tracker_id| from indexes.
86  void RemoveFileTracker(int64 tracker_id);
87
88  // Returns a set of FileTracker that have |file_id| as its own.
89  TrackerIDSet GetFileTrackerIDsByFileID(const std::string& file_id) const;
90
91  // Returns an app-root tracker identified by |app_id|.  Returns 0 if not
92  // found.
93  int64 GetAppRootTracker(const std::string& app_id) const;
94
95  // Returns a set of FileTracker that have |parent_tracker_id| and |title|.
96  TrackerIDSet GetFileTrackerIDsByParentAndTitle(
97      int64 parent_tracker_id,
98      const std::string& title) const;
99
100  std::vector<int64> GetFileTrackerIDsByParent(int64 parent_tracker_id) const;
101
102  // Returns the |file_id| of a file that has multiple trackers.
103  std::string PickMultiTrackerFileID() const;
104
105  // Returns a pair of |parent_tracker_id| and |title| that has multiple file
106  // at the path.
107  ParentIDAndTitle PickMultiBackingFilePath() const;
108
109  // Returns a FileTracker whose |dirty| is set and which isn't demoted.
110  // Returns 0 if not found.
111  int64 PickDirtyTracker() const;
112
113  // Demotes a dirty tracker.
114  void DemoteDirtyTracker(int64 tracker_id);
115
116  bool HasDemotedDirtyTracker() const;
117
118  // Promotes all demoted dirty trackers to normal dirty trackers.
119  void PromoteDemotedDirtyTrackers();
120
121  std::vector<std::string> GetRegisteredAppIDs() const;
122
123 private:
124  typedef base::ScopedPtrHashMap<std::string, FileMetadata> MetadataByID;
125  typedef base::ScopedPtrHashMap<int64, FileTracker> TrackerByID;
126  typedef base::hash_map<std::string, TrackerIDSet> TrackerIDsByFileID;
127  typedef base::hash_map<std::string, TrackerIDSet> TrackerIDsByTitle;
128  typedef std::map<int64, TrackerIDsByTitle> TrackerIDsByParentAndTitle;
129  typedef base::hash_map<std::string, int64> TrackerIDByAppID;
130  typedef base::hash_set<std::string> FileIDSet;
131  typedef base::hash_set<ParentIDAndTitle> PathSet;
132  typedef std::set<int64> DirtyTrackers;
133
134  friend class MetadataDatabase;
135  friend class MetadataDatabaseTest;
136
137  // Maintains |app_root_by_app_id_|.
138  void AddToAppIDIndex(const FileTracker& new_tracker);
139  void UpdateInAppIDIndex(const FileTracker& old_tracker,
140                          const FileTracker& new_tracker);
141  void RemoveFromAppIDIndex(const FileTracker& tracker);
142
143  // Maintains |trackers_by_file_id_| and |multi_tracker_file_ids_|.
144  void AddToFileIDIndexes(const FileTracker& new_tracker);
145  void UpdateInFileIDIndexes(const FileTracker& old_tracker,
146                             const FileTracker& new_tracker);
147  void RemoveFromFileIDIndexes(const FileTracker& tracker);
148
149  // Maintains |trackers_by_parent_and_title_| and |multi_backing_file_paths_|.
150  void AddToPathIndexes(const FileTracker& new_tracker);
151  void UpdateInPathIndexes(const FileTracker& old_tracker,
152                           const FileTracker& new_tracker1);
153  void RemoveFromPathIndexes(const FileTracker& tracker);
154
155  // Maintains |dirty_trackers_| and |demoted_dirty_trackers_|.
156  void AddToDirtyTrackerIndexes(const FileTracker& new_tracker);
157  void UpdateInDirtyTrackerIndexes(const FileTracker& old_tracker,
158                                   const FileTracker& new_tracker);
159  void RemoveFromDirtyTrackerIndexes(const FileTracker& tracker);
160
161  MetadataByID metadata_by_id_;
162  TrackerByID tracker_by_id_;
163
164  TrackerIDByAppID app_root_by_app_id_;
165
166  TrackerIDsByFileID trackers_by_file_id_;
167  FileIDSet multi_tracker_file_ids_;
168
169  TrackerIDsByParentAndTitle trackers_by_parent_and_title_;
170  PathSet multi_backing_file_paths_;
171
172  DirtyTrackers dirty_trackers_;
173  DirtyTrackers demoted_dirty_trackers_;
174
175  DISALLOW_COPY_AND_ASSIGN(MetadataDatabaseIndex);
176};
177
178}  // namespace drive_backend
179}  // namespace sync_file_system
180
181#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_H_
182