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. 4 5 #ifndef SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ 6 #define SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ 7 8 #include <string> 9 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" 19 20 namespace sync_pb { 21 class EntitySpecifics; 22 } 23 24 namespace syncer { 25 namespace syncable { 26 27 SYNC_EXPORT_PRIVATE extern const int32 kCurrentDBVersion; 28 29 struct ColumnSpec; 30 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. 36 // 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. 42 // 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(); 50 51 // Loads and drops all currently persisted meta entries into |handles_map| 52 // and loads appropriate persisted kernel info into |info_bucket|. 53 // 54 // This function can perform some cleanup tasks behind the scenes. It will 55 // clean up unused entries from the database and migrate to the latest 56 // database version. The caller can safely ignore these details. 57 // 58 // NOTE: On success (return value of OPENED), the buckets are populated with 59 // newly allocated items, meaning ownership is bestowed upon the caller. 60 virtual DirOpenResult Load(Directory::MetahandlesMap* handles_map, 61 JournalIndex* delete_journals, 62 Directory::KernelLoadInfo* kernel_load_info) = 0; 63 64 // Updates the on-disk store with the input |snapshot| as a database 65 // transaction. Does NOT open any syncable transactions as this would cause 66 // opening transactions elsewhere to block on synchronous I/O. 67 // DO NOT CALL THIS FROM MORE THAN ONE THREAD EVER. Also, whichever thread 68 // calls SaveChanges *must* be the thread that owns/destroys |this|. 69 virtual bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot); 70 71 protected: 72 // For test classes. 73 DirectoryBackingStore(const std::string& dir_name, 74 sql::Connection* connection); 75 76 // General Directory initialization and load helpers. 77 bool InitializeTables(); 78 bool CreateTables(); 79 80 // Create 'share_info' or 'temp_share_info' depending on value of 81 // is_temporary. Returns an sqlite 82 bool CreateShareInfoTable(bool is_temporary); 83 84 bool CreateShareInfoTableVersion71(bool is_temporary); 85 // Create 'metas' or 'temp_metas' depending on value of is_temporary. Also 86 // create a 'deleted_metas' table using same schema. 87 bool CreateMetasTable(bool is_temporary); 88 bool CreateModelsTable(); 89 bool CreateV71ModelsTable(); 90 bool CreateV75ModelsTable(); 91 92 // We don't need to load any synced and applied deleted entries, we can 93 // in fact just purge them forever on startup. 94 bool DropDeletedEntries(); 95 // Drops a table if it exists, harmless if the table did not already exist. 96 bool SafeDropTable(const char* table_name); 97 98 // Load helpers for entries and attributes. 99 bool LoadEntries(Directory::MetahandlesMap* handles_map); 100 bool LoadDeleteJournals(JournalIndex* delete_journals); 101 bool LoadInfo(Directory::KernelLoadInfo* info); 102 103 // Save/update helpers for entries. Return false if sqlite commit fails. 104 static bool SaveEntryToDB(sql::Statement* save_statement, 105 const EntryKernel& entry); 106 bool SaveNewEntryToDB(const EntryKernel& entry); 107 bool UpdateEntryToDB(const EntryKernel& entry); 108 109 // Close save_dbhandle_. Broken out for testing. 110 void EndSave(); 111 112 enum EntryTable { 113 METAS_TABLE, 114 DELETE_JOURNAL_TABLE, 115 }; 116 // Removes each entry whose metahandle is in |handles| from the table 117 // specified by |from| table. Does synchronous I/O. Returns false on error. 118 bool DeleteEntries(EntryTable from, const MetahandleSet& handles); 119 120 // Drop all tables in preparation for reinitialization. 121 void DropAllTables(); 122 123 // Serialization helpers for ModelType. These convert between 124 // the ModelType enum and the values we persist in the database to identify 125 // a model. We persist a default instance of the specifics protobuf as the 126 // ID, rather than the enum value. 127 static ModelType ModelIdToModelTypeEnum(const void* data, int length); 128 static std::string ModelTypeEnumToModelId(ModelType model_type); 129 130 static std::string GenerateCacheGUID(); 131 132 // Runs an integrity check on the current database. If the 133 // integrity check fails, false is returned and error is populated 134 // with an error message. 135 bool CheckIntegrity(sqlite3* handle, std::string* error) const; 136 137 // Checks that the references between sync nodes is consistent. 138 static bool VerifyReferenceIntegrity( 139 const Directory::MetahandlesMap* handles_map); 140 141 // Migration utilities. 142 bool RefreshColumns(); 143 bool SetVersion(int version); 144 int GetVersion(); 145 146 bool MigrateToSpecifics(const char* old_columns, 147 const char* specifics_column, 148 void(*handler_function) ( 149 sql::Statement* old_value_query, 150 int old_value_column, 151 sync_pb::EntitySpecifics* mutable_new_value)); 152 153 // Individual version migrations. 154 bool MigrateVersion67To68(); 155 bool MigrateVersion68To69(); 156 bool MigrateVersion69To70(); 157 bool MigrateVersion70To71(); 158 bool MigrateVersion71To72(); 159 bool MigrateVersion72To73(); 160 bool MigrateVersion73To74(); 161 bool MigrateVersion74To75(); 162 bool MigrateVersion75To76(); 163 bool MigrateVersion76To77(); 164 bool MigrateVersion77To78(); 165 bool MigrateVersion78To79(); 166 bool MigrateVersion79To80(); 167 bool MigrateVersion80To81(); 168 bool MigrateVersion81To82(); 169 bool MigrateVersion82To83(); 170 bool MigrateVersion83To84(); 171 bool MigrateVersion84To85(); 172 bool MigrateVersion85To86(); 173 174 scoped_ptr<sql::Connection> db_; 175 sql::Statement save_meta_statment_; 176 sql::Statement save_delete_journal_statment_; 177 std::string dir_name_; 178 179 // Set to true if migration left some old columns around that need to be 180 // discarded. 181 bool needs_column_refresh_; 182 183 private: 184 // Prepares |save_statement| for saving entries in |table|. 185 void PrepareSaveEntryStatement(EntryTable table, 186 sql::Statement* save_statement); 187 188 DISALLOW_COPY_AND_ASSIGN(DirectoryBackingStore); 189 }; 190 191 } // namespace syncable 192 } // namespace syncer 193 194 #endif // SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ 195