metadata_database_index_on_disk.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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_ON_DISK_H_
6#define CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_ON_DISK_H_
7
8#include <map>
9#include <set>
10#include <string>
11#include <vector>
12
13#include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_interface.h"
14#include "chrome/browser/sync_file_system/drive_backend/tracker_id_set.h"
15
16namespace sync_file_system {
17namespace drive_backend {
18
19class FileMetadata;
20class FileTracker;
21class LevelDBWrapper;
22class ServiceMetadata;
23struct DatabaseContents;
24// TODO(peria): Migrate implementation of ParentIDAndTitle structure from
25//     metadata_database_index.{cc,h} to here, on removing the files.
26struct ParentIDAndTitle;
27
28// Maintains indexes of MetadataDatabase on disk.
29class MetadataDatabaseIndexOnDisk : public MetadataDatabaseIndexInterface {
30 public:
31  static scoped_ptr<MetadataDatabaseIndexOnDisk>  Create(LevelDBWrapper* db);
32
33  virtual ~MetadataDatabaseIndexOnDisk();
34
35  // MetadataDatabaseIndexInterface overrides.
36  virtual bool GetFileMetadata(
37      const std::string& file_id, FileMetadata* metadata) const OVERRIDE;
38  virtual bool GetFileTracker(
39      int64 tracker_id, FileTracker* tracker) const OVERRIDE;
40  virtual void StoreFileMetadata(scoped_ptr<FileMetadata> metadata) OVERRIDE;
41  virtual void StoreFileTracker(scoped_ptr<FileTracker> tracker) OVERRIDE;
42  virtual void RemoveFileMetadata(const std::string& file_id) OVERRIDE;
43  virtual void RemoveFileTracker(int64 tracker_id) OVERRIDE;
44  virtual TrackerIDSet GetFileTrackerIDsByFileID(
45      const std::string& file_id) const OVERRIDE;
46  virtual int64 GetAppRootTracker(const std::string& app_id) const OVERRIDE;
47  virtual TrackerIDSet GetFileTrackerIDsByParentAndTitle(
48      int64 parent_tracker_id, const std::string& title) const OVERRIDE;
49  virtual std::vector<int64> GetFileTrackerIDsByParent(
50      int64 parent_tracker_id) const OVERRIDE;
51  virtual std::string PickMultiTrackerFileID() const OVERRIDE;
52  virtual ParentIDAndTitle PickMultiBackingFilePath() const OVERRIDE;
53  virtual int64 PickDirtyTracker() const OVERRIDE;
54  virtual void DemoteDirtyTracker(int64 tracker_id) OVERRIDE;
55  virtual bool HasDemotedDirtyTracker() const OVERRIDE;
56  virtual void PromoteDemotedDirtyTrackers() OVERRIDE;
57  virtual size_t CountDirtyTracker() const OVERRIDE;
58  virtual size_t CountFileMetadata() const OVERRIDE;
59  virtual size_t CountFileTracker() const OVERRIDE;
60  virtual void SetSyncRootTrackerID(int64 sync_root_id) const OVERRIDE;
61  virtual void SetLargestChangeID(int64 largest_change_id) const OVERRIDE;
62  virtual void SetNextTrackerID(int64 next_tracker_id) const OVERRIDE;
63  virtual int64 GetSyncRootTrackerID() const OVERRIDE;
64  virtual int64 GetLargestChangeID() const OVERRIDE;
65  virtual int64 GetNextTrackerID() const OVERRIDE;
66  virtual std::vector<std::string> GetRegisteredAppIDs() const OVERRIDE;
67  virtual std::vector<int64> GetAllTrackerIDs() const OVERRIDE;
68  virtual std::vector<std::string> GetAllMetadataIDs() const OVERRIDE;
69
70  // Builds on-disk indexes from FileTracker entries on disk.
71  void BuildTrackerIndexes();
72
73  LevelDBWrapper* GetDBForTesting();
74
75 private:
76  enum NumEntries {
77    NONE,      // No entries are found.
78    SINGLE,    // One entry is found.
79    MULTIPLE,  // Two or more entires are found.
80  };
81
82  explicit MetadataDatabaseIndexOnDisk(LevelDBWrapper* db);
83
84  // Maintain indexes from AppIDs to tracker IDs.
85  void AddToAppIDIndex(const FileTracker& new_tracker);
86  void UpdateInAppIDIndex(const FileTracker& old_tracker,
87                          const FileTracker& new_tracker);
88  void RemoveFromAppIDIndex(const FileTracker& tracker);
89
90  // Maintain indexes from remote file IDs to tracker ID sets.
91  void AddToFileIDIndexes(const FileTracker& new_tracker);
92  void UpdateInFileIDIndexes(const FileTracker& old_tracker,
93                             const FileTracker& new_tracker);
94  void RemoveFromFileIDIndexes(const FileTracker& tracker);
95
96  // Maintain indexes from path indexes to tracker ID sets
97  void AddToPathIndexes(const FileTracker& new_tracker);
98  void UpdateInPathIndexes(const FileTracker& old_tracker,
99                           const FileTracker& new_tracker);
100  void RemoveFromPathIndexes(const FileTracker& tracker);
101
102  // Maintain dirty tracker IDs.
103  void AddToDirtyTrackerIndexes(const FileTracker& new_tracker);
104  void UpdateInDirtyTrackerIndexes(const FileTracker& old_tracker,
105                                   const FileTracker& new_tracker);
106  void RemoveFromDirtyTrackerIndexes(const FileTracker& tracker);
107
108  // Returns a TrackerIDSet built from IDs which are found with given key
109  // and key prefix.
110  TrackerIDSet GetTrackerIDSetByPrefix(
111      const std::string& active_tracker_key,
112      const std::string& key_prefix) const;
113
114
115  // Simulate behavior of TrackerIDSet class.
116
117  // Adds an entry with |key_prefix| and tracker ID of |tracker|.  If |tracker|
118  // is active, an entry for |active_key| is added.
119  void AddToTrackerIDSetWithPrefix(
120      const std::string& active_tracker_key,
121      const std::string& key_prefix,
122      const FileTracker& tracker);
123
124  // Returns true if |tracker_id| is removed successfully.  If no other entries
125  // are stored with |key_prefix|, the entry for |active_key| is also removed.
126  bool EraseInTrackerIDSetWithPrefix(
127      const std::string& active_tracker_key,
128      const std::string& key_prefix,
129      int64 tracker_id);
130
131  // Adds an entry for |active_key| on DB, if |tracker_id| has an entry with
132  // |key_prefix|.
133  void ActivateInTrackerIDSetWithPrefix(
134      const std::string& active_tracker_key,
135      const std::string& key_prefix,
136      int64 tracker_id);
137
138  // Removes an entry for |active_key| on DB, if the value of |active_key| key
139  // is |tracker_id|.
140  void DeactivateInTrackerIDSetWithPrefix(
141      const std::string& active_tracker_key,
142      const std::string& key_prefix,
143      int64 tracker_id);
144
145  // Checks if |db_| has an entry whose key is |key|.
146  bool DBHasKey(const std::string& key);
147
148  // Returns the number of entries starting with |prefix| in NumEntries format.
149  // Entries for |ignored_id| are not counted in.
150  NumEntries CountWithPrefix(const std::string& prefix, int64 ignored_id);
151
152  LevelDBWrapper* db_;  // Not owned.
153  scoped_ptr<ServiceMetadata> service_metadata_;
154
155  DISALLOW_COPY_AND_ASSIGN(MetadataDatabaseIndexOnDisk);
156};
157
158}  // namespace drive_backend
159}  // namespace sync_file_system
160
161#endif  // CHROME_BROWSER_SYNC_FILE_SYSTEM_DRIVE_BACKEND_METADATA_DATABASE_INDEX_ON_DISK_H_
162