Roll src/third_party/WebKit 9301d6f:4619053 (svn 201058:201059)
[chromium-blink-merge.git] / sync / syncable / directory_backing_store.h
blob5e0c80bc4b9cd1d948ec9d7de24844969eea4f29
1 // Copyright 2012 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 SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_
6 #define SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/threading/non_thread_safe.h"
12 #include "sql/connection.h"
13 #include "sql/statement.h"
14 #include "sync/base/sync_export.h"
15 #include "sync/internal_api/public/base/model_type.h"
16 #include "sync/syncable/dir_open_result.h"
17 #include "sync/syncable/directory.h"
18 #include "sync/syncable/metahandle_set.h"
20 namespace sync_pb {
21 class EntitySpecifics;
24 namespace syncer {
25 namespace syncable {
27 SYNC_EXPORT_PRIVATE extern const int32 kCurrentDBVersion;
29 struct ColumnSpec;
31 // Interface that provides persistence for a syncable::Directory object. You can
32 // load all the persisted data to prime a syncable::Directory on startup by
33 // invoking Load. The only other thing you (or more correctly, a Directory) can
34 // do here is save any changes that have occurred since calling Load, which can
35 // be done periodically as often as desired.
37 // The DirectoryBackingStore will own an sqlite lock on its database for most of
38 // its lifetime. You must not have two DirectoryBackingStore objects accessing
39 // the database simultaneously. Because the lock exists at the database level,
40 // not even two separate browser instances would be able to acquire it
41 // simultaneously.
43 // This class is abstract so that we can extend it in interesting ways for use
44 // in tests. The concrete class used in non-test scenarios is
45 // OnDiskDirectoryBackingStore.
46 class SYNC_EXPORT_PRIVATE DirectoryBackingStore : public base::NonThreadSafe {
47 public:
48 explicit DirectoryBackingStore(const std::string& dir_name);
49 virtual ~DirectoryBackingStore();
51 // Loads and drops all currently persisted meta entries into |handles_map|
52 // and loads appropriate persisted kernel info into |kernel_load_info|.
53 // The function determines which entries can be safely dropped and inserts
54 // their keys into |metahandles_to_purge|. It is up to the caller to
55 // perform the actual cleanup.
57 // This function will migrate to the latest database version.
59 // NOTE: On success (return value of OPENED), the buckets are populated with
60 // newly allocated items, meaning ownership is bestowed upon the caller.
61 virtual DirOpenResult Load(Directory::MetahandlesMap* handles_map,
62 JournalIndex* delete_journals,
63 MetahandleSet* metahandles_to_purge,
64 Directory::KernelLoadInfo* kernel_load_info) = 0;
66 // Updates the on-disk store with the input |snapshot| as a database
67 // transaction. Does NOT open any syncable transactions as this would cause
68 // opening transactions elsewhere to block on synchronous I/O.
69 // DO NOT CALL THIS FROM MORE THAN ONE THREAD EVER. Also, whichever thread
70 // calls SaveChanges *must* be the thread that owns/destroys |this|.
72 // Returns true if the changes were saved successfully. Returns false if an
73 // error (of any kind) occurred. See also |SetCatastrophicErrorHandler| for a
74 // systematic way of handling underlying DB errors.
75 virtual bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot);
77 // Set the catastrophic error handler.
79 // When a catastrophic error is encountered while accessing the underlying DB,
80 // |catastrophic_error_handler| will be invoked (via PostTask) on the thread
81 // on which this DirectoryBackingStore object lives.
83 // For a definition of what's catastrophic, see sql::IsErrorCatastrophic.
85 // |catastrophic_error_handler| must be initialized (i.e. !is_null()).
87 // A single operation (like Load or SaveChanges) may result in the
88 // |catastrophic_error_handler| being invoked several times.
90 // There can be at most one handler. If this method is invoked when there is
91 // already a handler, the existing handler is overwritten with
92 // |catastrophic_error_handler|.
93 virtual void SetCatastrophicErrorHandler(
94 const base::Closure& catastrophic_error_handler);
96 protected:
97 // For test classes.
98 DirectoryBackingStore(const std::string& dir_name,
99 sql::Connection* connection);
101 // An accessor for the underlying sql::Connection. Avoid using outside of
102 // tests.
103 sql::Connection* db();
105 // Return true if the DB is open.
106 bool IsOpen() const;
108 // Open the DB at |path|.
109 // Return true on success, false on failure.
110 bool Open(const base::FilePath& path);
112 // Open an in memory DB.
113 // Return true on success, false on failure.
114 bool OpenInMemory();
116 // Initialize database tables. Return true on success, false on error.
117 bool InitializeTables();
119 // Load helpers for entries and attributes. Return true on success, false on
120 // error.
121 bool LoadEntries(Directory::MetahandlesMap* handles_map,
122 MetahandleSet* metahandles_to_purge);
123 bool LoadDeleteJournals(JournalIndex* delete_journals);
124 bool LoadInfo(Directory::KernelLoadInfo* info);
126 enum EntryTable {
127 METAS_TABLE,
128 DELETE_JOURNAL_TABLE,
130 // Removes each entry whose metahandle is in |handles| from the table
131 // specified by |from| table. Does synchronous I/O. Returns false on error.
132 bool DeleteEntries(EntryTable from, const MetahandleSet& handles);
134 // Serialization helpers for ModelType. These convert between
135 // the ModelType enum and the values we persist in the database to identify
136 // a model. We persist a default instance of the specifics protobuf as the
137 // ID, rather than the enum value.
138 static ModelType ModelIdToModelTypeEnum(const void* data, int length);
139 static std::string ModelTypeEnumToModelId(ModelType model_type);
141 static std::string GenerateCacheGUID();
143 // Checks that the references between sync nodes is consistent.
144 static bool VerifyReferenceIntegrity(
145 const Directory::MetahandlesMap* handles_map);
147 // Migration utilities.
148 bool RefreshColumns();
149 bool SetVersion(int version);
150 int GetVersion();
152 // Individual version migrations.
153 bool MigrateVersion67To68();
154 bool MigrateVersion68To69();
155 bool MigrateVersion69To70();
156 bool MigrateVersion70To71();
157 bool MigrateVersion71To72();
158 bool MigrateVersion72To73();
159 bool MigrateVersion73To74();
160 bool MigrateVersion74To75();
161 bool MigrateVersion75To76();
162 bool MigrateVersion76To77();
163 bool MigrateVersion77To78();
164 bool MigrateVersion78To79();
165 bool MigrateVersion79To80();
166 bool MigrateVersion80To81();
167 bool MigrateVersion81To82();
168 bool MigrateVersion82To83();
169 bool MigrateVersion83To84();
170 bool MigrateVersion84To85();
171 bool MigrateVersion85To86();
172 bool MigrateVersion86To87();
173 bool MigrateVersion87To88();
174 bool MigrateVersion88To89();
176 // Accessor for needs_column_refresh_. Used in tests.
177 bool needs_column_refresh() const;
179 // Destroys the existing Connection and creates a new one.
180 void ResetAndCreateConnection();
182 private:
183 friend class TestDirectoryBackingStore;
184 friend class DirectoryBackingStoreTest;
185 FRIEND_TEST_ALL_PREFIXES(DirectoryBackingStoreTest,
186 IncreaseDatabasePageSizeFrom4KTo32K);
187 FRIEND_TEST_ALL_PREFIXES(DirectoryBackingStoreTest,
188 CatastrophicErrorHandler_KeptAcrossReset);
189 FRIEND_TEST_ALL_PREFIXES(DirectoryBackingStoreTest,
190 CatastrophicErrorHandler_InvocationDuringLoad);
191 FRIEND_TEST_ALL_PREFIXES(
192 DirectoryBackingStoreTest,
193 CatastrophicErrorHandler_InvocationDuringSaveChanges);
195 // Drop all tables in preparation for reinitialization.
196 void DropAllTables();
198 bool SafeToPurgeOnLoading(const EntryKernel& entry) const;
200 // Drops a table if it exists, harmless if the table did not already exist.
201 bool SafeDropTable(const char* table_name);
203 bool CreateTables();
205 // Create 'share_info' or 'temp_share_info' depending on value of
206 // is_temporary. Returns true on success, false on error.
207 bool CreateShareInfoTable(bool is_temporary);
208 bool CreateShareInfoTableVersion71(bool is_temporary);
210 // Create 'metas' or 'temp_metas' depending on value of is_temporary. Also
211 // create a 'deleted_metas' table using same schema. Returns true on success,
212 // false on error.
213 bool CreateMetasTable(bool is_temporary);
215 // Returns true on success, false on error.
216 bool CreateModelsTable();
217 bool CreateV71ModelsTable();
218 bool CreateV75ModelsTable();
219 bool CreateV81ModelsTable();
221 // Returns true on success, false on error.
222 bool MigrateToSpecifics(const char* old_columns,
223 const char* specifics_column,
224 void(*handler_function) (
225 sql::Statement* old_value_query,
226 int old_value_column,
227 sync_pb::EntitySpecifics* mutable_new_value));
229 // Returns true on success, false on error.
230 bool Vacuum();
232 // Returns true on success, false on error.
233 bool IncreasePageSizeTo32K();
235 // Returns true on success, false on error.
236 bool GetDatabasePageSize(int* page_size);
238 // Prepares |save_statement| for saving entries in |table|.
239 void PrepareSaveEntryStatement(EntryTable table,
240 sql::Statement* save_statement);
242 const std::string dir_name_;
243 const int database_page_size_;
245 scoped_ptr<sql::Connection> db_;
246 sql::Statement save_meta_statement_;
247 sql::Statement save_delete_journal_statement_;
249 // Set to true if migration left some old columns around that need to be
250 // discarded.
251 bool needs_column_refresh_;
253 // We keep a copy of the Closure so we reinstall it when the underlying
254 // sql::Connection is destroyed/recreated.
255 base::Closure catastrophic_error_handler_;
257 DISALLOW_COPY_AND_ASSIGN(DirectoryBackingStore);
260 } // namespace syncable
261 } // namespace syncer
263 #endif // SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_