OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ | 5 #ifndef SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ |
6 #define SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ | 6 #define SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 // not even two separate browser instances would be able to acquire it | 40 // not even two separate browser instances would be able to acquire it |
41 // simultaneously. | 41 // simultaneously. |
42 // | 42 // |
43 // This class is abstract so that we can extend it in interesting ways for use | 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 | 44 // in tests. The concrete class used in non-test scenarios is |
45 // OnDiskDirectoryBackingStore. | 45 // OnDiskDirectoryBackingStore. |
46 class SYNC_EXPORT_PRIVATE DirectoryBackingStore : public base::NonThreadSafe { | 46 class SYNC_EXPORT_PRIVATE DirectoryBackingStore : public base::NonThreadSafe { |
47 friend class DirectoryBackingStoreTest; | 47 friend class DirectoryBackingStoreTest; |
48 FRIEND_TEST_ALL_PREFIXES(DirectoryBackingStoreTest, | 48 FRIEND_TEST_ALL_PREFIXES(DirectoryBackingStoreTest, |
49 IncreaseDatabasePageSizeFrom4KTo32K); | 49 IncreaseDatabasePageSizeFrom4KTo32K); |
| 50 FRIEND_TEST_ALL_PREFIXES(DirectoryBackingStoreTest, |
| 51 CatastrophicErrorHandler_KeptAcrossReset); |
| 52 FRIEND_TEST_ALL_PREFIXES(DirectoryBackingStoreTest, |
| 53 CatastrophicErrorHandler_Invocation); |
50 | 54 |
51 public: | 55 public: |
52 explicit DirectoryBackingStore(const std::string& dir_name); | 56 explicit DirectoryBackingStore(const std::string& dir_name); |
53 virtual ~DirectoryBackingStore(); | 57 virtual ~DirectoryBackingStore(); |
54 | 58 |
55 // Loads and drops all currently persisted meta entries into |handles_map| | 59 // Loads and drops all currently persisted meta entries into |handles_map| |
56 // and loads appropriate persisted kernel info into |kernel_load_info|. | 60 // and loads appropriate persisted kernel info into |kernel_load_info|. |
57 // The function determines which entries can be safely dropped and inserts | 61 // 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 | 62 // their keys into |metahandles_to_purge|. It is up to the caller to |
59 // perform the actual cleanup. | 63 // perform the actual cleanup. |
60 // | 64 // |
61 // This function will migrate to the latest database version. | 65 // This function will migrate to the latest database version. |
62 // | 66 // |
63 // NOTE: On success (return value of OPENED), the buckets are populated with | 67 // NOTE: On success (return value of OPENED), the buckets are populated with |
64 // newly allocated items, meaning ownership is bestowed upon the caller. | 68 // newly allocated items, meaning ownership is bestowed upon the caller. |
65 virtual DirOpenResult Load(Directory::MetahandlesMap* handles_map, | 69 virtual DirOpenResult Load(Directory::MetahandlesMap* handles_map, |
66 JournalIndex* delete_journals, | 70 JournalIndex* delete_journals, |
67 MetahandleSet* metahandles_to_purge, | 71 MetahandleSet* metahandles_to_purge, |
68 Directory::KernelLoadInfo* kernel_load_info) = 0; | 72 Directory::KernelLoadInfo* kernel_load_info) = 0; |
69 | 73 |
70 // Updates the on-disk store with the input |snapshot| as a database | 74 // Updates the on-disk store with the input |snapshot| as a database |
71 // transaction. Does NOT open any syncable transactions as this would cause | 75 // transaction. Does NOT open any syncable transactions as this would cause |
72 // opening transactions elsewhere to block on synchronous I/O. | 76 // opening transactions elsewhere to block on synchronous I/O. |
73 // DO NOT CALL THIS FROM MORE THAN ONE THREAD EVER. Also, whichever thread | 77 // DO NOT CALL THIS FROM MORE THAN ONE THREAD EVER. Also, whichever thread |
74 // calls SaveChanges *must* be the thread that owns/destroys |this|. | 78 // calls SaveChanges *must* be the thread that owns/destroys |this|. |
| 79 // |
| 80 // Returns true if the changes were saved successfully. Returns false if an |
| 81 // error (of any kind) occurred. See also |SetCatastrophicErrorHandler| for a |
| 82 // systematic way of handling underlying DB errors. |
75 virtual bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot); | 83 virtual bool SaveChanges(const Directory::SaveChangesSnapshot& snapshot); |
76 | 84 |
| 85 // Set the catastrophic error handler. |
| 86 // |
| 87 // When a catastrophic error is encountered while accessing the underlying DB, |
| 88 // |catastrophic_error_handler| will be invoked (via PostTask) on the thread |
| 89 // on which this DirectoryBackingStore object lives. |
| 90 // |
| 91 // For a definition of what's catastrophic, see sql::IsErrorCatastrophic. |
| 92 // |
| 93 // |catastrophic_error_handler| must be initialized (i.e. !is_null()). |
| 94 // |
| 95 // A single operation (like Load or SaveChanges) may result in the |
| 96 // |catastrophic_error_handler| being invoked several times. |
| 97 // |
| 98 // There can be at most one handler. If this method is invoked when there is |
| 99 // already a handler, the existing handler is overwritten with |
| 100 // |catastrophic_error_handler|. |
| 101 virtual void SetCatastrophicErrorHandler( |
| 102 const base::Closure& catastrophic_error_handler); |
| 103 |
77 protected: | 104 protected: |
78 // For test classes. | 105 // For test classes. |
79 DirectoryBackingStore(const std::string& dir_name, | 106 DirectoryBackingStore(const std::string& dir_name, |
80 sql::Connection* connection); | 107 sql::Connection* connection); |
81 | 108 |
82 // General Directory initialization and load helpers. | 109 // General Directory initialization and load helpers. |
83 bool InitializeTables(); | 110 bool InitializeTables(); |
84 bool CreateTables(); | 111 bool CreateTables(); |
85 | 112 |
86 // Create 'share_info' or 'temp_share_info' depending on value of | 113 // Create 'share_info' or 'temp_share_info' depending on value of |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 | 226 |
200 const std::string dir_name_; | 227 const std::string dir_name_; |
201 const int database_page_size_; | 228 const int database_page_size_; |
202 sql::Statement save_meta_statement_; | 229 sql::Statement save_meta_statement_; |
203 sql::Statement save_delete_journal_statement_; | 230 sql::Statement save_delete_journal_statement_; |
204 | 231 |
205 // Set to true if migration left some old columns around that need to be | 232 // Set to true if migration left some old columns around that need to be |
206 // discarded. | 233 // discarded. |
207 bool needs_column_refresh_; | 234 bool needs_column_refresh_; |
208 | 235 |
| 236 // We keep a copy of the Closure so we reinstall it when the underlying |
| 237 // sql::Connection is destroyed/recreated. |
| 238 base::Closure catastrophic_error_handler_; |
| 239 |
209 DISALLOW_COPY_AND_ASSIGN(DirectoryBackingStore); | 240 DISALLOW_COPY_AND_ASSIGN(DirectoryBackingStore); |
210 }; | 241 }; |
211 | 242 |
212 } // namespace syncable | 243 } // namespace syncable |
213 } // namespace syncer | 244 } // namespace syncer |
214 | 245 |
215 #endif // SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ | 246 #endif // SYNC_SYNCABLE_DIRECTORY_BACKING_STORE_H_ |
OLD | NEW |