Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / sync / syncable / directory_backing_store.h
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   bool CreateV81ModelsTable();
92
93   // We don't need to load any synced and applied deleted entries, we can
94   // in fact just purge them forever on startup.
95   bool DropDeletedEntries();
96   // Drops a table if it exists, harmless if the table did not already exist.
97   bool SafeDropTable(const char* table_name);
98
99   // Load helpers for entries and attributes.
100   bool LoadEntries(Directory::MetahandlesMap* handles_map);
101   bool LoadDeleteJournals(JournalIndex* delete_journals);
102   bool LoadInfo(Directory::KernelLoadInfo* info);
103
104   // Save/update helpers for entries.  Return false if sqlite commit fails.
105   static bool SaveEntryToDB(sql::Statement* save_statement,
106                             const EntryKernel& entry);
107   bool SaveNewEntryToDB(const EntryKernel& entry);
108   bool UpdateEntryToDB(const EntryKernel& entry);
109
110   // Close save_dbhandle_.  Broken out for testing.
111   void EndSave();
112
113   enum EntryTable {
114     METAS_TABLE,
115     DELETE_JOURNAL_TABLE,
116   };
117   // Removes each entry whose metahandle is in |handles| from the table
118   // specified by |from| table. Does synchronous I/O.  Returns false on error.
119   bool DeleteEntries(EntryTable from, const MetahandleSet& handles);
120
121   // Drop all tables in preparation for reinitialization.
122   void DropAllTables();
123
124   // Serialization helpers for ModelType.  These convert between
125   // the ModelType enum and the values we persist in the database to identify
126   // a model.  We persist a default instance of the specifics protobuf as the
127   // ID, rather than the enum value.
128   static ModelType ModelIdToModelTypeEnum(const void* data, int length);
129   static std::string ModelTypeEnumToModelId(ModelType model_type);
130
131   static std::string GenerateCacheGUID();
132
133   // Runs an integrity check on the current database.  If the
134   // integrity check fails, false is returned and error is populated
135   // with an error message.
136   bool CheckIntegrity(sqlite3* handle, std::string* error) const;
137
138   // Checks that the references between sync nodes is consistent.
139   static bool VerifyReferenceIntegrity(
140       const Directory::MetahandlesMap* handles_map);
141
142   // Migration utilities.
143   bool RefreshColumns();
144   bool SetVersion(int version);
145   int GetVersion();
146
147   bool MigrateToSpecifics(const char* old_columns,
148                           const char* specifics_column,
149                           void(*handler_function) (
150                               sql::Statement* old_value_query,
151                               int old_value_column,
152                               sync_pb::EntitySpecifics* mutable_new_value));
153
154   // Individual version migrations.
155   bool MigrateVersion67To68();
156   bool MigrateVersion68To69();
157   bool MigrateVersion69To70();
158   bool MigrateVersion70To71();
159   bool MigrateVersion71To72();
160   bool MigrateVersion72To73();
161   bool MigrateVersion73To74();
162   bool MigrateVersion74To75();
163   bool MigrateVersion75To76();
164   bool MigrateVersion76To77();
165   bool MigrateVersion77To78();
166   bool MigrateVersion78To79();
167   bool MigrateVersion79To80();
168   bool MigrateVersion80To81();
169   bool MigrateVersion81To82();
170   bool MigrateVersion82To83();
171   bool MigrateVersion83To84();
172   bool MigrateVersion84To85();
173   bool MigrateVersion85To86();
174   bool MigrateVersion86To87();
175   bool MigrateVersion87To88();
176   bool MigrateVersion88To89();
177
178   scoped_ptr<sql::Connection> db_;
179   sql::Statement save_meta_statment_;
180   sql::Statement save_delete_journal_statment_;
181   std::string dir_name_;
182
183   // Set to true if migration left some old columns around that need to be
184   // discarded.
185   bool needs_column_refresh_;
186
187  private:
188   // Prepares |save_statement| for saving entries in |table|.
189   void PrepareSaveEntryStatement(EntryTable table,
190                                  sql::Statement* save_statement);
191
192   DISALLOW_COPY_AND_ASSIGN(DirectoryBackingStore);
193 };
194
195 }  // namespace syncable
196 }  // namespace syncer
197
198 #endif  // SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_