1 // Copyright 2013 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_H_
6 #define SYNC_SYNCABLE_DIRECTORY_H_
13 #include "base/basictypes.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/file_util.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/values.h"
18 #include "sync/base/sync_export.h"
19 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h"
20 #include "sync/internal_api/public/util/weak_handle.h"
21 #include "sync/syncable/dir_open_result.h"
22 #include "sync/syncable/entry_kernel.h"
23 #include "sync/syncable/metahandle_set.h"
24 #include "sync/syncable/parent_child_index.h"
25 #include "sync/syncable/syncable_delete_journal.h"
31 class UnrecoverableErrorHandler
;
35 class BaseTransaction
;
36 class BaseWriteTransaction
;
37 class DirectoryChangeDelegate
;
38 class DirectoryBackingStore
;
40 class ScopedKernelLock
;
41 class TransactionObserver
;
42 class WriteTransaction
;
44 enum InvariantCheckLevel
{
45 OFF
= 0, // No checking.
46 VERIFY_CHANGES
= 1, // Checks only mutated entries. Does not check hierarchy.
47 FULL_DB_VERIFICATION
= 2 // Check every entry. This can be expensive.
50 class SYNC_EXPORT Directory
{
51 friend class BaseTransaction
;
53 friend class ModelNeutralMutableEntry
;
54 friend class MutableEntry
;
55 friend class ReadTransaction
;
56 friend class ScopedKernelLock
;
57 friend class WriteTransaction
;
58 friend class SyncableDirectoryTest
;
59 friend class syncer::TestUserShare
;
60 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest
, ManageDeleteJournals
);
61 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest
,
62 TakeSnapshotGetsAllDirtyHandlesTest
);
63 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest
,
64 TakeSnapshotGetsOnlyDirtyHandlesTest
);
65 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest
,
66 TakeSnapshotGetsMetahandlesToPurge
);
69 typedef std::vector
<int64
> Metahandles
;
71 // Be careful when using these hash_map containers. According to the spec,
72 // inserting into them may invalidate all iterators.
74 // It gets worse, though. The Anroid STL library has a bug that means it may
75 // invalidate all iterators when you erase from the map, too. That means that
76 // you can't iterate while erasing. STLDeleteElements(), std::remove_if(),
77 // and other similar functions are off-limits too, until this bug is fixed.
79 // See http://sourceforge.net/p/stlport/bugs/239/.
80 typedef base::hash_map
<int64
, EntryKernel
*> MetahandlesMap
;
81 typedef base::hash_map
<std::string
, EntryKernel
*> IdsMap
;
82 typedef base::hash_map
<std::string
, EntryKernel
*> TagsMap
;
84 static const base::FilePath::CharType kSyncDatabaseFilename
[];
86 // The dirty/clean state of kernel fields backed by the share_info table.
87 // This is public so it can be used in SaveChangesSnapshot for persistence.
88 enum KernelShareInfoStatus
{
89 KERNEL_SHARE_INFO_INVALID
,
90 KERNEL_SHARE_INFO_VALID
,
91 KERNEL_SHARE_INFO_DIRTY
94 // Various data that the Directory::Kernel we are backing (persisting data
95 // for) needs saved across runs of the application.
96 struct SYNC_EXPORT_PRIVATE PersistedKernelInfo
{
97 PersistedKernelInfo();
98 ~PersistedKernelInfo();
100 // Set the |download_progress| entry for the given model to a
101 // "first sync" start point. When such a value is sent to the server,
102 // a full download of all objects of the model will be initiated.
103 void reset_download_progress(ModelType model_type
);
105 // Last sync timestamp fetched from the server.
106 sync_pb::DataTypeProgressMarker download_progress
[MODEL_TYPE_COUNT
];
107 // Sync-side transaction version per data type. Monotonically incremented
108 // when updating native model. A copy is also saved in native model.
109 // Later out-of-sync models can be detected and fixed by comparing
110 // transaction versions of sync model and native model.
111 // TODO(hatiaol): implement detection and fixing of out-of-sync models.
113 int64 transaction_version
[MODEL_TYPE_COUNT
];
114 // The store birthday we were given by the server. Contents are opaque to
116 std::string store_birthday
;
117 // The next local ID that has not been used with this cache-GUID.
119 // The serialized bag of chips we were given by the server. Contents are
120 // opaque to the client. This is the serialization of a message of type
121 // ChipBag defined in sync.proto. It can contains NULL characters.
122 std::string bag_of_chips
;
125 // What the Directory needs on initialization to create itself and its Kernel.
126 // Filled by DirectoryBackingStore::Load.
127 struct KernelLoadInfo
{
128 PersistedKernelInfo kernel_info
;
129 std::string cache_guid
; // Created on first initialization, never changes.
130 int64 max_metahandle
; // Computed (using sql MAX aggregate) on init.
131 KernelLoadInfo() : max_metahandle(0) {
135 // When the Directory is told to SaveChanges, a SaveChangesSnapshot is
136 // constructed and forms a consistent snapshot of what needs to be sent to
137 // the backing store.
138 struct SYNC_EXPORT_PRIVATE SaveChangesSnapshot
{
139 SaveChangesSnapshot();
140 ~SaveChangesSnapshot();
142 KernelShareInfoStatus kernel_info_status
;
143 PersistedKernelInfo kernel_info
;
144 EntryKernelSet dirty_metas
;
145 MetahandleSet metahandles_to_purge
;
146 EntryKernelSet delete_journals
;
147 MetahandleSet delete_journals_to_purge
;
150 // Does not take ownership of |encryptor|.
151 // |report_unrecoverable_error_function| may be NULL.
152 // Takes ownership of |store|.
154 DirectoryBackingStore
* store
,
155 UnrecoverableErrorHandler
* unrecoverable_error_handler
,
156 ReportUnrecoverableErrorFunction
157 report_unrecoverable_error_function
,
158 NigoriHandler
* nigori_handler
,
159 Cryptographer
* cryptographer
);
160 virtual ~Directory();
162 // Does not take ownership of |delegate|, which must not be NULL.
163 // Starts sending events to |delegate| if the returned result is
164 // OPENED. Note that events to |delegate| may be sent from *any*
165 // thread. |transaction_observer| must be initialized.
166 DirOpenResult
Open(const std::string
& name
,
167 DirectoryChangeDelegate
* delegate
,
168 const WeakHandle
<TransactionObserver
>&
169 transaction_observer
);
171 // Stops sending events to the delegate and the transaction
175 int64
NextMetahandle();
176 // Returns a negative integer unique to this client.
177 syncable::Id
NextId();
179 bool good() const { return NULL
!= kernel_
; }
181 // The download progress is an opaque token provided by the sync server
182 // to indicate the continuation state of the next GetUpdates operation.
183 void GetDownloadProgress(
185 sync_pb::DataTypeProgressMarker
* value_out
) const;
186 void GetDownloadProgressAsString(
188 std::string
* value_out
) const;
189 size_t GetEntriesCount() const;
190 void SetDownloadProgress(
192 const sync_pb::DataTypeProgressMarker
& value
);
194 // Gets/Increments transaction version of a model type. Must be called when
195 // holding kernel mutex.
196 int64
GetTransactionVersion(ModelType type
) const;
197 void IncrementTransactionVersion(ModelType type
);
199 ModelTypeSet
InitialSyncEndedTypes();
200 bool InitialSyncEndedForType(ModelType type
);
201 bool InitialSyncEndedForType(BaseTransaction
* trans
, ModelType type
);
203 const std::string
& name() const { return kernel_
->name
; }
205 // (Account) Store birthday is opaque to the client, so we keep it in the
206 // format it is in the proto buffer in case we switch to a binary birthday
208 std::string
store_birthday() const;
209 void set_store_birthday(const std::string
& store_birthday
);
211 // (Account) Bag of chip is an opaque state used by the server to track the
213 std::string
bag_of_chips() const;
214 void set_bag_of_chips(const std::string
& bag_of_chips
);
216 // Unique to each account / client pair.
217 std::string
cache_guid() const;
219 // Returns a pointer to our Nigori node handler.
220 NigoriHandler
* GetNigoriHandler();
222 // Returns a pointer to our cryptographer. Does not transfer ownership.
223 // Not thread safe, so should only be accessed while holding a transaction.
224 Cryptographer
* GetCryptographer(const BaseTransaction
* trans
);
226 // Returns true if the directory had encountered an unrecoverable error.
227 // Note: Any function in |Directory| that can be called without holding a
228 // transaction need to check if the Directory already has an unrecoverable
230 bool unrecoverable_error_set(const BaseTransaction
* trans
) const;
232 // Called to immediately report an unrecoverable error (but don't
234 void ReportUnrecoverableError() {
235 if (report_unrecoverable_error_function_
) {
236 report_unrecoverable_error_function_();
240 // Called to set the unrecoverable error on the directory and to propagate
241 // the error to upper layers.
242 void OnUnrecoverableError(const BaseTransaction
* trans
,
243 const tracked_objects::Location
& location
,
244 const std::string
& message
);
246 DeleteJournal
* delete_journal();
248 // Returns the child meta handles (even those for deleted/unlinked
249 // nodes) for given parent id. Clears |result| if there are no
251 bool GetChildHandlesById(BaseTransaction
*, const Id
& parent_id
,
252 Metahandles
* result
);
254 // Counts all items under the given node, including the node itself.
255 int GetTotalNodeCount(BaseTransaction
*, EntryKernel
* kernel_
) const;
257 // Returns this item's position within its parent folder.
258 // The left-most item is 0, second left-most is 1, etc.
259 int GetPositionIndex(BaseTransaction
*, EntryKernel
* kernel_
) const;
261 // Returns true iff |id| has children.
262 bool HasChildren(BaseTransaction
* trans
, const Id
& id
);
264 // Find the first child in the positional ordering under a parent,
265 // and fill in |*first_child_id| with its id. Fills in a root Id if
266 // parent has no children. Returns true if the first child was
267 // successfully found, or false if an error was encountered.
268 Id
GetFirstChildId(BaseTransaction
* trans
, const EntryKernel
* parent
);
270 // These functions allow one to fetch the next or previous item under
271 // the same folder. Returns the "root" ID if there is no predecessor
274 // TODO(rlarocque): These functions are used mainly for tree traversal. We
275 // should replace these with an iterator API. See crbug.com/178275.
276 syncable::Id
GetPredecessorId(EntryKernel
*);
277 syncable::Id
GetSuccessorId(EntryKernel
*);
279 // Places |e| as a successor to |predecessor|. If |predecessor| is NULL,
280 // |e| will be placed as the left-most item in its folder.
282 // Both |e| and |predecessor| must be valid entries under the same parent.
284 // TODO(rlarocque): This function includes limited support for placing items
285 // with valid positions (ie. Bookmarks) as siblings of items that have no set
286 // ordering (ie. Autofill items). This support is required only for tests,
287 // and should be removed. See crbug.com/178282.
288 void PutPredecessor(EntryKernel
* e
, EntryKernel
* predecessor
);
290 // SaveChanges works by taking a consistent snapshot of the current Directory
291 // state and indices (by deep copy) under a ReadTransaction, passing this
292 // snapshot to the backing store under no transaction, and finally cleaning
293 // up by either purging entries no longer needed (this part done under a
294 // WriteTransaction) or rolling back the dirty bits. It also uses
295 // internal locking to enforce SaveChanges operations are mutually exclusive.
297 // WARNING: THIS METHOD PERFORMS SYNCHRONOUS I/O VIA SQLITE.
300 // Returns the number of entities with the unsynced bit set.
301 int64
unsynced_entity_count() const;
303 // Get GetUnsyncedMetaHandles should only be called after SaveChanges and
304 // before any new entries have been created. The intention is that the
305 // syncer should call it from its PerformSyncQueries member.
306 void GetUnsyncedMetaHandles(BaseTransaction
* trans
,
307 Metahandles
* result
);
309 // Returns whether or not this |type| has unapplied updates.
310 bool TypeHasUnappliedUpdates(ModelType type
);
312 // Get all the metahandles for unapplied updates for a given set of
314 void GetUnappliedUpdateMetaHandles(BaseTransaction
* trans
,
315 FullModelTypeSet server_types
,
316 std::vector
<int64
>* result
);
318 // Get all the metahandles of entries of |type|.
319 void GetMetaHandlesOfType(BaseTransaction
* trans
,
321 Metahandles
* result
);
323 // Get metahandle counts for various criteria to show on the
324 // about:sync page. The information is computed on the fly
325 // each time. If this results in a significant performance hit,
326 // additional data structures can be added to cache results.
327 void CollectMetaHandleCounts(std::vector
<int>* num_entries_by_type
,
328 std::vector
<int>* num_to_delete_entries_by_type
);
330 scoped_ptr
<base::ListValue
> GetAllNodeDetails(BaseTransaction
* trans
);
332 // Sets the level of invariant checking performed after transactions.
333 void SetInvariantCheckLevel(InvariantCheckLevel check_level
);
335 // Checks tree metadata consistency following a transaction. It is intended
336 // to provide a reasonable tradeoff between performance and comprehensiveness
337 // and may be used in release code.
338 bool CheckInvariantsOnTransactionClose(
339 syncable::BaseTransaction
* trans
,
340 const MetahandleSet
& modified_handles
);
342 // Forces a full check of the directory. This operation may be slow and
343 // should not be invoked outside of tests.
344 bool FullyCheckTreeInvariants(BaseTransaction
*trans
);
346 // Purges data associated with any entries whose ModelType or ServerModelType
347 // is found in |disabled_types|, from sync directory _both_ in memory and on
348 // disk. Only valid, "real" model types are allowed in |disabled_types| (see
349 // model_type.h for definitions).
350 // 1. Data associated with |types_to_journal| is saved in the delete journal
351 // to help prevent back-from-dead problem due to offline delete in the next
352 // sync session. |types_to_journal| must be a subset of |disabled_types|.
353 // 2. Data associated with |types_to_unapply| is reset to an "unapplied"
354 // state, wherein all local data is deleted and IS_UNAPPLIED is set to true.
355 // This is useful when there's no benefit in discarding the currently
356 // downloaded state, such as when there are cryptographer errors.
357 // |types_to_unapply| must be a subset of |disabled_types|.
358 // 3. All other data is purged entirely.
359 // Note: "Purge" is just meant to distinguish from "deleting" entries, which
360 // means something different in the syncable namespace.
361 // WARNING! This can be real slow, as it iterates over all entries.
362 // WARNING! Performs synchronous I/O.
363 // Returns: true on success, false if an error was encountered.
364 virtual bool PurgeEntriesWithTypeIn(ModelTypeSet disabled_types
,
365 ModelTypeSet types_to_journal
,
366 ModelTypeSet types_to_unapply
);
368 protected: // for friends, mainly used by Entry constructors
369 virtual EntryKernel
* GetEntryByHandle(int64 handle
);
370 virtual EntryKernel
* GetEntryByHandle(int64 metahandle
,
371 ScopedKernelLock
* lock
);
372 virtual EntryKernel
* GetEntryById(const Id
& id
);
373 EntryKernel
* GetEntryByServerTag(const std::string
& tag
);
374 virtual EntryKernel
* GetEntryByClientTag(const std::string
& tag
);
375 EntryKernel
* GetRootEntry();
376 bool ReindexId(BaseWriteTransaction
* trans
, EntryKernel
* const entry
,
378 bool ReindexParentId(BaseWriteTransaction
* trans
, EntryKernel
* const entry
,
379 const Id
& new_parent_id
);
380 void ClearDirtyMetahandles();
382 DirOpenResult
OpenImpl(
383 const std::string
& name
,
384 DirectoryChangeDelegate
* delegate
,
385 const WeakHandle
<TransactionObserver
>& transaction_observer
);
389 // |delegate| must not be NULL. |transaction_observer| must be
391 Kernel(const std::string
& name
, const KernelLoadInfo
& info
,
392 DirectoryChangeDelegate
* delegate
,
393 const WeakHandle
<TransactionObserver
>& transaction_observer
);
397 // Implements ReadTransaction / WriteTransaction using a simple lock.
398 base::Lock transaction_mutex
;
400 // Protected by transaction_mutex. Used by WriteTransactions.
401 int64 next_write_transaction_id
;
403 // The name of this directory.
404 std::string
const name
;
406 // Protects all members below.
407 // The mutex effectively protects all the indices, but not the
408 // entries themselves. So once a pointer to an entry is pulled
409 // from the index, the mutex can be unlocked and entry read or written.
411 // Never hold the mutex and do anything with the database or any
412 // other buffered IO. Violating this rule will result in deadlock.
415 // Entries indexed by metahandle. This container is considered to be the
416 // owner of all EntryKernels, which may be referened by the other
417 // containers. If you remove an EntryKernel from this map, you probably
418 // want to remove it from all other containers and delete it, too.
419 MetahandlesMap metahandles_map
;
421 // Entries indexed by id
424 // Entries indexed by server tag.
425 // This map does not include any entries with non-existent server tags.
426 TagsMap server_tags_map
;
428 // Entries indexed by client tag.
429 // This map does not include any entries with non-existent client tags.
430 // IS_DEL items are included.
431 TagsMap client_tags_map
;
433 // Contains non-deleted items, indexed according to parent and position
434 // within parent. Protected by the ScopedKernelLock.
435 ParentChildIndex parent_child_index
;
437 // 3 in-memory indices on bits used extremely frequently by the syncer.
438 // |unapplied_update_metahandles| is keyed by the server model type.
439 MetahandleSet unapplied_update_metahandles
[MODEL_TYPE_COUNT
];
440 MetahandleSet unsynced_metahandles
;
441 // Contains metahandles that are most likely dirty (though not
442 // necessarily). Dirtyness is confirmed in TakeSnapshotForSaveChanges().
443 MetahandleSet dirty_metahandles
;
445 // When a purge takes place, we remove items from all our indices and stash
446 // them in here so that SaveChanges can persist their permanent deletion.
447 MetahandleSet metahandles_to_purge
;
449 KernelShareInfoStatus info_status
;
451 // These 3 members are backed in the share_info table, and
452 // their state is marked by the flag above.
454 // A structure containing the Directory state that is written back into the
455 // database on SaveChanges.
456 PersistedKernelInfo persisted_info
;
458 // A unique identifier for this account's cache db, used to generate
459 // unique server IDs. No need to lock, only written at init time.
460 const std::string cache_guid
;
462 // It doesn't make sense for two threads to run SaveChanges at the same
463 // time; this mutex protects that activity.
464 base::Lock save_changes_mutex
;
466 // The next metahandle is protected by kernel mutex.
467 int64 next_metahandle
;
469 // The delegate for directory change events. Must not be NULL.
470 DirectoryChangeDelegate
* const delegate
;
472 // The transaction observer.
473 const WeakHandle
<TransactionObserver
> transaction_observer
;
476 // These private versions expect the kernel lock to already be held
478 EntryKernel
* GetEntryById(const Id
& id
, ScopedKernelLock
* const lock
);
480 // A helper that implements the logic of checking tree invariants.
481 bool CheckTreeInvariants(syncable::BaseTransaction
* trans
,
482 const MetahandleSet
& handles
);
484 // Helper to prime metahandles_map, ids_map, parent_child_index,
485 // unsynced_metahandles, unapplied_update_metahandles, server_tags_map and
486 // client_tags_map from metahandles_index. The input |handles_map| will be
487 // cleared during the initialization process.
488 void InitializeIndices(MetahandlesMap
* handles_map
);
490 // Constructs a consistent snapshot of the current Directory state and
491 // indices (by deep copy) under a ReadTransaction for use in |snapshot|.
492 // See SaveChanges() for more information.
493 void TakeSnapshotForSaveChanges(SaveChangesSnapshot
* snapshot
);
495 // Purges from memory any unused, safe to remove entries that were
496 // successfully deleted on disk as a result of the SaveChanges that processed
497 // |snapshot|. See SaveChanges() for more information.
498 bool VacuumAfterSaveChanges(const SaveChangesSnapshot
& snapshot
);
500 // Rolls back dirty bits in the event that the SaveChanges that
501 // processed |snapshot| failed, for example, due to no disk space.
502 void HandleSaveChangesFailure(const SaveChangesSnapshot
& snapshot
);
504 // For new entry creation only
505 bool InsertEntry(BaseWriteTransaction
* trans
,
506 EntryKernel
* entry
, ScopedKernelLock
* lock
);
507 bool InsertEntry(BaseWriteTransaction
* trans
, EntryKernel
* entry
);
509 // Used by CheckTreeInvariants
510 void GetAllMetaHandles(BaseTransaction
* trans
, MetahandleSet
* result
);
511 bool SafeToPurgeFromMemory(WriteTransaction
* trans
,
512 const EntryKernel
* const entry
) const;
514 // A helper used by GetTotalNodeCount.
515 void GetChildSetForKernel(
517 EntryKernel
* kernel_
,
518 std::deque
<const OrderedChildSet
*>* child_sets
) const;
520 // Append the handles of the children of |parent_id| to |result|.
521 void AppendChildHandles(
522 const ScopedKernelLock
& lock
,
523 const Id
& parent_id
, Directory::Metahandles
* result
);
525 // Helper methods used by PurgeDisabledTypes.
526 void UnapplyEntry(EntryKernel
* entry
);
527 void DeleteEntry(bool save_to_journal
,
529 EntryKernelSet
* entries_to_journal
);
533 scoped_ptr
<DirectoryBackingStore
> store_
;
535 UnrecoverableErrorHandler
* const unrecoverable_error_handler_
;
536 const ReportUnrecoverableErrorFunction report_unrecoverable_error_function_
;
537 bool unrecoverable_error_set_
;
540 NigoriHandler
* const nigori_handler_
;
541 Cryptographer
* const cryptographer_
;
543 InvariantCheckLevel invariant_check_level_
;
545 // Maintain deleted entries not in |kernel_| until it's verified that they
546 // are deleted in native models as well.
547 scoped_ptr
<DeleteJournal
> delete_journal_
;
549 DISALLOW_COPY_AND_ASSIGN(Directory
);
552 } // namespace syncable
553 } // namespace syncer
555 #endif // SYNC_SYNCABLE_DIRECTORY_H_