We started redesigning GpuMemoryBuffer interface to handle multiple buffers [0].
[chromium-blink-merge.git] / sync / syncable / directory_backing_store.h
blob81add28ec358e2c4523b22584a44ccd9e3be0a45
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 MigrateToSpecifics(const char* old_columns,
153 const char* specifics_column,
154 void(*handler_function) (
155 sql::Statement* old_value_query,
156 int old_value_column,
157 sync_pb::EntitySpecifics* mutable_new_value));
159 // Individual version migrations.
160 bool MigrateVersion67To68();
161 bool MigrateVersion68To69();
162 bool MigrateVersion69To70();
163 bool MigrateVersion70To71();
164 bool MigrateVersion71To72();
165 bool MigrateVersion72To73();
166 bool MigrateVersion73To74();
167 bool MigrateVersion74To75();
168 bool MigrateVersion75To76();
169 bool MigrateVersion76To77();
170 bool MigrateVersion77To78();
171 bool MigrateVersion78To79();
172 bool MigrateVersion79To80();
173 bool MigrateVersion80To81();
174 bool MigrateVersion81To82();
175 bool MigrateVersion82To83();
176 bool MigrateVersion83To84();
177 bool MigrateVersion84To85();
178 bool MigrateVersion85To86();
179 bool MigrateVersion86To87();
180 bool MigrateVersion87To88();
181 bool MigrateVersion88To89();
183 // Accessor for needs_column_refresh_. Used in tests.
184 bool needs_column_refresh() const;
186 // Destroys the existing Connection and creates a new one.
187 void ResetAndCreateConnection();
189 scoped_ptr<sql::Connection> db_;
191 private:
192 bool Vacuum();
193 bool IncreasePageSizeTo32K();
194 bool GetDatabasePageSize(int* page_size);
196 // Prepares |save_statement| for saving entries in |table|.
197 void PrepareSaveEntryStatement(EntryTable table,
198 sql::Statement* save_statement);
200 const std::string dir_name_;
201 const int database_page_size_;
202 sql::Statement save_meta_statement_;
203 sql::Statement save_delete_journal_statement_;
205 // Set to true if migration left some old columns around that need to be
206 // discarded.
207 bool needs_column_refresh_;
209 DISALLOW_COPY_AND_ASSIGN(DirectoryBackingStore);
212 } // namespace syncable
213 } // namespace syncer
215 #endif // SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_