[Extension API Extern Generation] Support inline object definitions
[chromium-blink-merge.git] / sync / syncable / directory_backing_store.h
blob79920378ce7c3ab65e53149ff980a01dd18be330
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 friend class DirectoryBackingStoreTest;
48 FRIEND_TEST_ALL_PREFIXES(DirectoryBackingStoreTest,
49 IncreaseDatabasePageSizeFrom4KTo32K);
51 public:
52 explicit DirectoryBackingStore(const std::string& dir_name);
53 virtual ~DirectoryBackingStore();
55 // Loads and drops all currently persisted meta entries into |handles_map|
56 // and loads appropriate persisted kernel info into |kernel_load_info|.
57 // The function determines which entries can be safely dropped and inserts
58 // their keys into |metahandles_to_purge|. It is up to the caller to
59 // perform the actual cleanup.
61 // This function will migrate to the latest database version.
63 // NOTE: On success (return value of OPENED), the buckets are populated with
64 // newly allocated items, meaning ownership is bestowed upon the caller.
65 virtual DirOpenResult Load(Directory::MetahandlesMap* handles_map,
66 JournalIndex* delete_journals,
67 MetahandleSet* metahandles_to_purge,
68 Directory::KernelLoadInfo* kernel_load_info) = 0;
70 // Updates the on-disk store with the input |snapshot| as a database
71 // transaction. Does NOT open any syncable transactions as this would cause
72 // opening transactions elsewhere to block on synchronous I/O.
73 // DO NOT CALL THIS FROM MORE THAN ONE THREAD EVER. Also, whichever thread
74 // calls SaveChanges *must* be the thread that owns/destroys |this|.
75 virtual bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot);
77 protected:
78 // For test classes.
79 DirectoryBackingStore(const std::string& dir_name,
80 sql::Connection* connection);
82 // General Directory initialization and load helpers.
83 bool InitializeTables();
84 bool CreateTables();
86 // Create 'share_info' or 'temp_share_info' depending on value of
87 // is_temporary. Returns an sqlite
88 bool CreateShareInfoTable(bool is_temporary);
90 bool CreateShareInfoTableVersion71(bool is_temporary);
91 // Create 'metas' or 'temp_metas' depending on value of is_temporary. Also
92 // create a 'deleted_metas' table using same schema.
93 bool CreateMetasTable(bool is_temporary);
94 bool CreateModelsTable();
95 bool CreateV71ModelsTable();
96 bool CreateV75ModelsTable();
97 bool CreateV81ModelsTable();
99 // Drops a table if it exists, harmless if the table did not already exist.
100 bool SafeDropTable(const char* table_name);
102 // Load helpers for entries and attributes.
103 bool LoadEntries(Directory::MetahandlesMap* handles_map,
104 MetahandleSet* metahandles_to_purge);
105 bool LoadDeleteJournals(JournalIndex* delete_journals);
106 bool LoadInfo(Directory::KernelLoadInfo* info);
107 bool SafeToPurgeOnLoading(const EntryKernel& entry) const;
109 // Save/update helpers for entries. Return false if sqlite commit fails.
110 static bool SaveEntryToDB(sql::Statement* save_statement,
111 const EntryKernel& entry);
112 bool SaveNewEntryToDB(const EntryKernel& entry);
113 bool UpdateEntryToDB(const EntryKernel& entry);
115 // Close save_dbhandle_. Broken out for testing.
116 void EndSave();
118 enum EntryTable {
119 METAS_TABLE,
120 DELETE_JOURNAL_TABLE,
122 // Removes each entry whose metahandle is in |handles| from the table
123 // specified by |from| table. Does synchronous I/O. Returns false on error.
124 bool DeleteEntries(EntryTable from, const MetahandleSet& handles);
126 // Drop all tables in preparation for reinitialization.
127 void DropAllTables();
129 // Serialization helpers for ModelType. These convert between
130 // the ModelType enum and the values we persist in the database to identify
131 // a model. We persist a default instance of the specifics protobuf as the
132 // ID, rather than the enum value.
133 static ModelType ModelIdToModelTypeEnum(const void* data, int length);
134 static std::string ModelTypeEnumToModelId(ModelType model_type);
136 static std::string GenerateCacheGUID();
138 // Runs an integrity check on the current database. If the
139 // integrity check fails, false is returned and error is populated
140 // with an error message.
141 bool CheckIntegrity(sqlite3* handle, std::string* error) const;
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 bool GetDatabasePageSize(int* page_size);
153 bool IsSyncBackingDatabase32KEnabled();
154 bool IncreasePageSizeTo32K();
155 bool Vacuum();
156 int databasePageSize_;
158 bool MigrateToSpecifics(const char* old_columns,
159 const char* specifics_column,
160 void(*handler_function) (
161 sql::Statement* old_value_query,
162 int old_value_column,
163 sync_pb::EntitySpecifics* mutable_new_value));
165 // Individual version migrations.
166 bool MigrateVersion67To68();
167 bool MigrateVersion68To69();
168 bool MigrateVersion69To70();
169 bool MigrateVersion70To71();
170 bool MigrateVersion71To72();
171 bool MigrateVersion72To73();
172 bool MigrateVersion73To74();
173 bool MigrateVersion74To75();
174 bool MigrateVersion75To76();
175 bool MigrateVersion76To77();
176 bool MigrateVersion77To78();
177 bool MigrateVersion78To79();
178 bool MigrateVersion79To80();
179 bool MigrateVersion80To81();
180 bool MigrateVersion81To82();
181 bool MigrateVersion82To83();
182 bool MigrateVersion83To84();
183 bool MigrateVersion84To85();
184 bool MigrateVersion85To86();
185 bool MigrateVersion86To87();
186 bool MigrateVersion87To88();
187 bool MigrateVersion88To89();
189 scoped_ptr<sql::Connection> db_;
190 sql::Statement save_meta_statment_;
191 sql::Statement save_delete_journal_statment_;
192 std::string dir_name_;
194 // Set to true if migration left some old columns around that need to be
195 // discarded.
196 bool needs_column_refresh_;
198 private:
199 // Prepares |save_statement| for saving entries in |table|.
200 void PrepareSaveEntryStatement(EntryTable table,
201 sql::Statement* save_statement);
203 DISALLOW_COPY_AND_ASSIGN(DirectoryBackingStore);
206 } // namespace syncable
207 } // namespace syncer
209 #endif // SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_