Home | History | Annotate | Download | only in syncable
      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.
      4 
      5 #ifndef SYNC_SYNCABLE_DIRECTORY_H_
      6 #define SYNC_SYNCABLE_DIRECTORY_H_
      7 
      8 #include <deque>
      9 #include <set>
     10 #include <string>
     11 #include <vector>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/containers/hash_tables.h"
     15 #include "base/files/file_util.h"
     16 #include "base/gtest_prod_util.h"
     17 #include "base/values.h"
     18 #include "sync/api/attachments/attachment_id.h"
     19 #include "sync/base/sync_export.h"
     20 #include "sync/internal_api/public/util/report_unrecoverable_error_function.h"
     21 #include "sync/internal_api/public/util/weak_handle.h"
     22 #include "sync/syncable/dir_open_result.h"
     23 #include "sync/syncable/entry.h"
     24 #include "sync/syncable/entry_kernel.h"
     25 #include "sync/syncable/metahandle_set.h"
     26 #include "sync/syncable/parent_child_index.h"
     27 #include "sync/syncable/syncable_delete_journal.h"
     28 
     29 namespace syncer {
     30 
     31 class Cryptographer;
     32 class TestUserShare;
     33 class UnrecoverableErrorHandler;
     34 
     35 namespace syncable {
     36 
     37 class BaseTransaction;
     38 class BaseWriteTransaction;
     39 class DirectoryChangeDelegate;
     40 class DirectoryBackingStore;
     41 class NigoriHandler;
     42 class ScopedKernelLock;
     43 class TransactionObserver;
     44 class WriteTransaction;
     45 
     46 enum InvariantCheckLevel {
     47   OFF = 0,            // No checking.
     48   VERIFY_CHANGES = 1, // Checks only mutated entries.  Does not check hierarchy.
     49   FULL_DB_VERIFICATION = 2 // Check every entry.  This can be expensive.
     50 };
     51 
     52 // Directory stores and manages EntryKernels.
     53 //
     54 // This class is tightly coupled to several other classes (see friends).
     55 class SYNC_EXPORT Directory {
     56   friend class BaseTransaction;
     57   friend class Entry;
     58   friend class ModelNeutralMutableEntry;
     59   friend class MutableEntry;
     60   friend class ReadTransaction;
     61   friend class ScopedKernelLock;
     62   friend class WriteTransaction;
     63   friend class SyncableDirectoryTest;
     64   friend class syncer::TestUserShare;
     65   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, ManageDeleteJournals);
     66   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest,
     67                            TakeSnapshotGetsAllDirtyHandlesTest);
     68   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest,
     69                            TakeSnapshotGetsOnlyDirtyHandlesTest);
     70   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest,
     71                            TakeSnapshotGetsMetahandlesToPurge);
     72 
     73  public:
     74   typedef std::vector<int64> Metahandles;
     75 
     76   // Be careful when using these hash_map containers.  According to the spec,
     77   // inserting into them may invalidate all iterators.
     78   //
     79   // It gets worse, though.  The Anroid STL library has a bug that means it may
     80   // invalidate all iterators when you erase from the map, too.  That means that
     81   // you can't iterate while erasing.  STLDeleteElements(), std::remove_if(),
     82   // and other similar functions are off-limits too, until this bug is fixed.
     83   //
     84   // See http://sourceforge.net/p/stlport/bugs/239/.
     85   typedef base::hash_map<int64, EntryKernel*> MetahandlesMap;
     86   typedef base::hash_map<std::string, EntryKernel*> IdsMap;
     87   typedef base::hash_map<std::string, EntryKernel*> TagsMap;
     88   typedef std::string AttachmentIdUniqueId;
     89   typedef base::hash_map<AttachmentIdUniqueId, MetahandleSet>
     90       IndexByAttachmentId;
     91 
     92   static const base::FilePath::CharType kSyncDatabaseFilename[];
     93 
     94   // The dirty/clean state of kernel fields backed by the share_info table.
     95   // This is public so it can be used in SaveChangesSnapshot for persistence.
     96   enum KernelShareInfoStatus {
     97     KERNEL_SHARE_INFO_INVALID,
     98     KERNEL_SHARE_INFO_VALID,
     99     KERNEL_SHARE_INFO_DIRTY
    100   };
    101 
    102   // Various data that the Directory::Kernel we are backing (persisting data
    103   // for) needs saved across runs of the application.
    104   struct SYNC_EXPORT_PRIVATE PersistedKernelInfo {
    105     PersistedKernelInfo();
    106     ~PersistedKernelInfo();
    107 
    108     // Set the |download_progress| entry for the given model to a
    109     // "first sync" start point.  When such a value is sent to the server,
    110     // a full download of all objects of the model will be initiated.
    111     void ResetDownloadProgress(ModelType model_type);
    112 
    113     // Whether a valid progress marker exists for |model_type|.
    114     bool HasEmptyDownloadProgress(ModelType model_type);
    115 
    116     // Last sync timestamp fetched from the server.
    117     sync_pb::DataTypeProgressMarker download_progress[MODEL_TYPE_COUNT];
    118     // Sync-side transaction version per data type. Monotonically incremented
    119     // when updating native model. A copy is also saved in native model.
    120     // Later out-of-sync models can be detected and fixed by comparing
    121     // transaction versions of sync model and native model.
    122     // TODO(hatiaol): implement detection and fixing of out-of-sync models.
    123     //                Bug 154858.
    124     int64 transaction_version[MODEL_TYPE_COUNT];
    125     // The store birthday we were given by the server. Contents are opaque to
    126     // the client.
    127     std::string store_birthday;
    128     // The next local ID that has not been used with this cache-GUID.
    129     int64 next_id;
    130     // The serialized bag of chips we were given by the server. Contents are
    131     // opaque to the client. This is the serialization of a message of type
    132     // ChipBag defined in sync.proto. It can contains NULL characters.
    133     std::string bag_of_chips;
    134     // The per-datatype context.
    135     sync_pb::DataTypeContext datatype_context[MODEL_TYPE_COUNT];
    136   };
    137 
    138   // What the Directory needs on initialization to create itself and its Kernel.
    139   // Filled by DirectoryBackingStore::Load.
    140   struct KernelLoadInfo {
    141     PersistedKernelInfo kernel_info;
    142     std::string cache_guid;  // Created on first initialization, never changes.
    143     int64 max_metahandle;    // Computed (using sql MAX aggregate) on init.
    144     KernelLoadInfo() : max_metahandle(0) {
    145     }
    146   };
    147 
    148   // When the Directory is told to SaveChanges, a SaveChangesSnapshot is
    149   // constructed and forms a consistent snapshot of what needs to be sent to
    150   // the backing store.
    151   struct SYNC_EXPORT_PRIVATE SaveChangesSnapshot {
    152     SaveChangesSnapshot();
    153     ~SaveChangesSnapshot();
    154 
    155     KernelShareInfoStatus kernel_info_status;
    156     PersistedKernelInfo kernel_info;
    157     EntryKernelSet dirty_metas;
    158     MetahandleSet metahandles_to_purge;
    159     EntryKernelSet delete_journals;
    160     MetahandleSet delete_journals_to_purge;
    161   };
    162 
    163   // Does not take ownership of |encryptor|.
    164   // |report_unrecoverable_error_function| may be NULL.
    165   // Takes ownership of |store|.
    166   Directory(
    167       DirectoryBackingStore* store,
    168       UnrecoverableErrorHandler* unrecoverable_error_handler,
    169       ReportUnrecoverableErrorFunction
    170           report_unrecoverable_error_function,
    171       NigoriHandler* nigori_handler,
    172       Cryptographer* cryptographer);
    173   virtual ~Directory();
    174 
    175   // Does not take ownership of |delegate|, which must not be NULL.
    176   // Starts sending events to |delegate| if the returned result is
    177   // OPENED.  Note that events to |delegate| may be sent from *any*
    178   // thread.  |transaction_observer| must be initialized.
    179   DirOpenResult Open(const std::string& name,
    180                      DirectoryChangeDelegate* delegate,
    181                      const WeakHandle<TransactionObserver>&
    182                          transaction_observer);
    183 
    184   // Stops sending events to the delegate and the transaction
    185   // observer.
    186   void Close();
    187 
    188   int64 NextMetahandle();
    189   // Returns a negative integer unique to this client.
    190   syncable::Id NextId();
    191 
    192   bool good() const { return NULL != kernel_; }
    193 
    194   // The download progress is an opaque token provided by the sync server
    195   // to indicate the continuation state of the next GetUpdates operation.
    196   void GetDownloadProgress(
    197       ModelType type,
    198       sync_pb::DataTypeProgressMarker* value_out) const;
    199   void GetDownloadProgressAsString(
    200       ModelType type,
    201       std::string* value_out) const;
    202   size_t GetEntriesCount() const;
    203   void SetDownloadProgress(
    204       ModelType type,
    205       const sync_pb::DataTypeProgressMarker& value);
    206 
    207   // Gets/Increments transaction version of a model type. Must be called when
    208   // holding kernel mutex.
    209   int64 GetTransactionVersion(ModelType type) const;
    210   void IncrementTransactionVersion(ModelType type);
    211 
    212   // Getter/setters for the per datatype context.
    213   void GetDataTypeContext(BaseTransaction* trans,
    214                           ModelType type,
    215                           sync_pb::DataTypeContext* context) const;
    216   void SetDataTypeContext(BaseWriteTransaction* trans,
    217                           ModelType type,
    218                           const sync_pb::DataTypeContext& context);
    219 
    220   ModelTypeSet InitialSyncEndedTypes();
    221   bool InitialSyncEndedForType(ModelType type);
    222   bool InitialSyncEndedForType(BaseTransaction* trans, ModelType type);
    223 
    224   const std::string& name() const { return kernel_->name; }
    225 
    226   // (Account) Store birthday is opaque to the client, so we keep it in the
    227   // format it is in the proto buffer in case we switch to a binary birthday
    228   // later.
    229   std::string store_birthday() const;
    230   void set_store_birthday(const std::string& store_birthday);
    231 
    232   // (Account) Bag of chip is an opaque state used by the server to track the
    233   // client.
    234   std::string bag_of_chips() const;
    235   void set_bag_of_chips(const std::string& bag_of_chips);
    236 
    237   // Unique to each account / client pair.
    238   std::string cache_guid() const;
    239 
    240   // Returns a pointer to our Nigori node handler.
    241   NigoriHandler* GetNigoriHandler();
    242 
    243   // Returns a pointer to our cryptographer. Does not transfer ownership.
    244   // Not thread safe, so should only be accessed while holding a transaction.
    245   Cryptographer* GetCryptographer(const BaseTransaction* trans);
    246 
    247   // Returns true if the directory had encountered an unrecoverable error.
    248   // Note: Any function in |Directory| that can be called without holding a
    249   // transaction need to check if the Directory already has an unrecoverable
    250   // error on it.
    251   bool unrecoverable_error_set(const BaseTransaction* trans) const;
    252 
    253   // Called to immediately report an unrecoverable error (but don't
    254   // propagate it up).
    255   void ReportUnrecoverableError() {
    256     if (report_unrecoverable_error_function_) {
    257       report_unrecoverable_error_function_();
    258     }
    259   }
    260 
    261   // Called to set the unrecoverable error on the directory and to propagate
    262   // the error to upper layers.
    263   void OnUnrecoverableError(const BaseTransaction* trans,
    264                             const tracked_objects::Location& location,
    265                             const std::string & message);
    266 
    267   DeleteJournal* delete_journal();
    268 
    269   // Returns the child meta handles (even those for deleted/unlinked
    270   // nodes) for given parent id.  Clears |result| if there are no
    271   // children.
    272   bool GetChildHandlesById(BaseTransaction*, const Id& parent_id,
    273       Metahandles* result);
    274 
    275   // Counts all items under the given node, including the node itself.
    276   int GetTotalNodeCount(BaseTransaction*, EntryKernel* kernel_) const;
    277 
    278   // Returns this item's position within its parent folder.
    279   // The left-most item is 0, second left-most is 1, etc.
    280   int GetPositionIndex(BaseTransaction*, EntryKernel* kernel_) const;
    281 
    282   // Returns true iff |id| has children.
    283   bool HasChildren(BaseTransaction* trans, const Id& id);
    284 
    285   // Find the first child in the positional ordering under a parent,
    286   // and fill in |*first_child_id| with its id.  Fills in a root Id if
    287   // parent has no children.  Returns true if the first child was
    288   // successfully found, or false if an error was encountered.
    289   Id GetFirstChildId(BaseTransaction* trans, const EntryKernel* parent);
    290 
    291   // These functions allow one to fetch the next or previous item under
    292   // the same folder.  Returns the "root" ID if there is no predecessor
    293   // or successor.
    294   //
    295   // TODO(rlarocque): These functions are used mainly for tree traversal.  We
    296   // should replace these with an iterator API.  See crbug.com/178275.
    297   syncable::Id GetPredecessorId(EntryKernel*);
    298   syncable::Id GetSuccessorId(EntryKernel*);
    299 
    300   // Places |e| as a successor to |predecessor|.  If |predecessor| is NULL,
    301   // |e| will be placed as the left-most item in its folder.
    302   //
    303   // Both |e| and |predecessor| must be valid entries under the same parent.
    304   //
    305   // TODO(rlarocque): This function includes limited support for placing items
    306   // with valid positions (ie. Bookmarks) as siblings of items that have no set
    307   // ordering (ie. Autofill items).  This support is required only for tests,
    308   // and should be removed.  See crbug.com/178282.
    309   void PutPredecessor(EntryKernel* e, EntryKernel* predecessor);
    310 
    311   // SaveChanges works by taking a consistent snapshot of the current Directory
    312   // state and indices (by deep copy) under a ReadTransaction, passing this
    313   // snapshot to the backing store under no transaction, and finally cleaning
    314   // up by either purging entries no longer needed (this part done under a
    315   // WriteTransaction) or rolling back the dirty bits.  It also uses
    316   // internal locking to enforce SaveChanges operations are mutually exclusive.
    317   //
    318   // WARNING: THIS METHOD PERFORMS SYNCHRONOUS I/O VIA SQLITE.
    319   bool SaveChanges();
    320 
    321   // Returns the number of entities with the unsynced bit set.
    322   int64 unsynced_entity_count() const;
    323 
    324   // Get GetUnsyncedMetaHandles should only be called after SaveChanges and
    325   // before any new entries have been created. The intention is that the
    326   // syncer should call it from its PerformSyncQueries member.
    327   void GetUnsyncedMetaHandles(BaseTransaction* trans,
    328                               Metahandles* result);
    329 
    330   // Returns whether or not this |type| has unapplied updates.
    331   bool TypeHasUnappliedUpdates(ModelType type);
    332 
    333   // Get all the metahandles for unapplied updates for a given set of
    334   // server types.
    335   void GetUnappliedUpdateMetaHandles(BaseTransaction* trans,
    336                                      FullModelTypeSet server_types,
    337                                      std::vector<int64>* result);
    338 
    339   // Get all the metahandles of entries of |type|.
    340   void GetMetaHandlesOfType(BaseTransaction* trans,
    341                             ModelType type,
    342                             Metahandles* result);
    343 
    344   // Get metahandle counts for various criteria to show on the
    345   // about:sync page. The information is computed on the fly
    346   // each time. If this results in a significant performance hit,
    347   // additional data structures can be added to cache results.
    348   void CollectMetaHandleCounts(std::vector<int>* num_entries_by_type,
    349                                std::vector<int>* num_to_delete_entries_by_type);
    350 
    351   // Returns a ListValue serialization of all nodes for the given type.
    352   scoped_ptr<base::ListValue> GetNodeDetailsForType(
    353       BaseTransaction* trans,
    354       ModelType type);
    355 
    356   // Sets the level of invariant checking performed after transactions.
    357   void SetInvariantCheckLevel(InvariantCheckLevel check_level);
    358 
    359   // Checks tree metadata consistency following a transaction.  It is intended
    360   // to provide a reasonable tradeoff between performance and comprehensiveness
    361   // and may be used in release code.
    362   bool CheckInvariantsOnTransactionClose(
    363       syncable::BaseTransaction* trans,
    364       const MetahandleSet& modified_handles);
    365 
    366   // Forces a full check of the directory.  This operation may be slow and
    367   // should not be invoked outside of tests.
    368   bool FullyCheckTreeInvariants(BaseTransaction *trans);
    369 
    370   // Purges data associated with any entries whose ModelType or ServerModelType
    371   // is found in |disabled_types|, from sync directory _both_ in memory and on
    372   // disk. Only valid, "real" model types are allowed in |disabled_types| (see
    373   // model_type.h for definitions).
    374   // 1. Data associated with |types_to_journal| is saved in the delete journal
    375   // to help prevent back-from-dead problem due to offline delete in the next
    376   // sync session. |types_to_journal| must be a subset of |disabled_types|.
    377   // 2. Data associated with |types_to_unapply| is reset to an "unapplied"
    378   // state, wherein all local data is deleted and IS_UNAPPLIED is set to true.
    379   // This is useful when there's no benefit in discarding the currently
    380   // downloaded state, such as when there are cryptographer errors.
    381   // |types_to_unapply| must be a subset of |disabled_types|.
    382   // 3. All other data is purged entirely.
    383   // Note: "Purge" is just meant to distinguish from "deleting" entries, which
    384   // means something different in the syncable namespace.
    385   // WARNING! This can be real slow, as it iterates over all entries.
    386   // WARNING! Performs synchronous I/O.
    387   // Returns: true on success, false if an error was encountered.
    388   virtual bool PurgeEntriesWithTypeIn(ModelTypeSet disabled_types,
    389                                       ModelTypeSet types_to_journal,
    390                                       ModelTypeSet types_to_unapply);
    391 
    392   // Resets the base_versions and server_versions of all synced entities
    393   // associated with |type| to 1.
    394   // WARNING! This can be slow, as it iterates over all entries for a type.
    395   bool ResetVersionsForType(BaseWriteTransaction* trans, ModelType type);
    396 
    397   // Returns true iff the attachment identified by |attachment_id_proto| is
    398   // linked to an entry.
    399   //
    400   // An attachment linked to a deleted entry is still considered linked if the
    401   // entry hasn't yet been purged.
    402   bool IsAttachmentLinked(
    403       const sync_pb::AttachmentIdProto& attachment_id_proto) const;
    404 
    405   // Given attachment id return metahandles to all entries that reference this
    406   // attachment.
    407   void GetMetahandlesByAttachmentId(
    408       BaseTransaction* trans,
    409       const sync_pb::AttachmentIdProto& attachment_id_proto,
    410       Metahandles* result);
    411 
    412   // Change entry to not dirty. Used in special case when we don't want to
    413   // persist modified entry on disk. e.g. SyncBackupManager uses this to
    414   // preserve sync preferences in DB on disk.
    415   void UnmarkDirtyEntry(WriteTransaction* trans, Entry* entry);
    416 
    417   // Clears |id_set| and fills it with the ids of attachments that need to be
    418   // uploaded to the sync server.
    419   void GetAttachmentIdsToUpload(BaseTransaction* trans,
    420                                 ModelType type,
    421                                 AttachmentIdSet* id_set);
    422 
    423  private:
    424   struct Kernel {
    425     // |delegate| must not be NULL.  |transaction_observer| must be
    426     // initialized.
    427     Kernel(const std::string& name, const KernelLoadInfo& info,
    428            DirectoryChangeDelegate* delegate,
    429            const WeakHandle<TransactionObserver>& transaction_observer);
    430 
    431     ~Kernel();
    432 
    433     // Implements ReadTransaction / WriteTransaction using a simple lock.
    434     base::Lock transaction_mutex;
    435 
    436     // Protected by transaction_mutex.  Used by WriteTransactions.
    437     int64 next_write_transaction_id;
    438 
    439     // The name of this directory.
    440     std::string const name;
    441 
    442     // Protects all members below.
    443     // The mutex effectively protects all the indices, but not the
    444     // entries themselves.  So once a pointer to an entry is pulled
    445     // from the index, the mutex can be unlocked and entry read or written.
    446     //
    447     // Never hold the mutex and do anything with the database or any
    448     // other buffered IO.  Violating this rule will result in deadlock.
    449     base::Lock mutex;
    450 
    451     // Entries indexed by metahandle.  This container is considered to be the
    452     // owner of all EntryKernels, which may be referened by the other
    453     // containers.  If you remove an EntryKernel from this map, you probably
    454     // want to remove it from all other containers and delete it, too.
    455     MetahandlesMap metahandles_map;
    456 
    457     // Entries indexed by id
    458     IdsMap ids_map;
    459 
    460     // Entries indexed by server tag.
    461     // This map does not include any entries with non-existent server tags.
    462     TagsMap server_tags_map;
    463 
    464     // Entries indexed by client tag.
    465     // This map does not include any entries with non-existent client tags.
    466     // IS_DEL items are included.
    467     TagsMap client_tags_map;
    468 
    469     // Contains non-deleted items, indexed according to parent and position
    470     // within parent.  Protected by the ScopedKernelLock.
    471     ParentChildIndex parent_child_index;
    472 
    473     // This index keeps track of which metahandles refer to a given attachment.
    474     // Think of it as the inverse of EntryKernel's AttachmentMetadata Records.
    475     //
    476     // Because entries can be undeleted (e.g. PutIsDel(false)), entries should
    477     // not removed from the index until they are actually deleted from memory.
    478     //
    479     // All access should go through IsAttachmentLinked,
    480     // RemoveFromAttachmentIndex, AddToAttachmentIndex, and
    481     // UpdateAttachmentIndex methods to avoid iterator invalidation errors.
    482     IndexByAttachmentId index_by_attachment_id;
    483 
    484     // 3 in-memory indices on bits used extremely frequently by the syncer.
    485     // |unapplied_update_metahandles| is keyed by the server model type.
    486     MetahandleSet unapplied_update_metahandles[MODEL_TYPE_COUNT];
    487     MetahandleSet unsynced_metahandles;
    488     // Contains metahandles that are most likely dirty (though not
    489     // necessarily).  Dirtyness is confirmed in TakeSnapshotForSaveChanges().
    490     MetahandleSet dirty_metahandles;
    491 
    492     // When a purge takes place, we remove items from all our indices and stash
    493     // them in here so that SaveChanges can persist their permanent deletion.
    494     MetahandleSet metahandles_to_purge;
    495 
    496     KernelShareInfoStatus info_status;
    497 
    498     // These 3 members are backed in the share_info table, and
    499     // their state is marked by the flag above.
    500 
    501     // A structure containing the Directory state that is written back into the
    502     // database on SaveChanges.
    503     PersistedKernelInfo persisted_info;
    504 
    505     // A unique identifier for this account's cache db, used to generate
    506     // unique server IDs. No need to lock, only written at init time.
    507     const std::string cache_guid;
    508 
    509     // It doesn't make sense for two threads to run SaveChanges at the same
    510     // time; this mutex protects that activity.
    511     base::Lock save_changes_mutex;
    512 
    513     // The next metahandle is protected by kernel mutex.
    514     int64 next_metahandle;
    515 
    516     // The delegate for directory change events.  Must not be NULL.
    517     DirectoryChangeDelegate* const delegate;
    518 
    519     // The transaction observer.
    520     const WeakHandle<TransactionObserver> transaction_observer;
    521   };
    522 
    523   // You'll notice that some of these methods have two forms.  One that takes a
    524   // ScopedKernelLock and one that doesn't.  The general pattern is that those
    525   // without a ScopedKernelLock parameter construct one internally before
    526   // calling the form that takes one.
    527 
    528   virtual EntryKernel* GetEntryByHandle(int64 handle);
    529   virtual EntryKernel* GetEntryByHandle(const ScopedKernelLock& lock,
    530                                         int64 metahandle);
    531 
    532   virtual EntryKernel* GetEntryById(const Id& id);
    533   virtual EntryKernel* GetEntryById(const ScopedKernelLock& lock, const Id& id);
    534 
    535   EntryKernel* GetEntryByServerTag(const std::string& tag);
    536   virtual EntryKernel* GetEntryByClientTag(const std::string& tag);
    537 
    538   // For new entry creation only
    539   bool InsertEntry(BaseWriteTransaction* trans, EntryKernel* entry);
    540   bool InsertEntry(const ScopedKernelLock& lock,
    541                    BaseWriteTransaction* trans,
    542                    EntryKernel* entry);
    543 
    544   bool ReindexId(BaseWriteTransaction* trans, EntryKernel* const entry,
    545                  const Id& new_id);
    546 
    547   bool ReindexParentId(BaseWriteTransaction* trans, EntryKernel* const entry,
    548                        const Id& new_parent_id);
    549 
    550   // Update the attachment index for |metahandle| removing it from the index
    551   // under |old_metadata| entries and add it under |new_metadata| entries.
    552   void UpdateAttachmentIndex(const int64 metahandle,
    553                              const sync_pb::AttachmentMetadata& old_metadata,
    554                              const sync_pb::AttachmentMetadata& new_metadata);
    555 
    556   // Remove each of |metahandle|'s attachment ids from index_by_attachment_id.
    557   void RemoveFromAttachmentIndex(
    558       const ScopedKernelLock& lock,
    559       const int64 metahandle,
    560       const sync_pb::AttachmentMetadata& attachment_metadata);
    561 
    562   // Add each of |metahandle|'s attachment ids to the index_by_attachment_id.
    563   void AddToAttachmentIndex(
    564       const ScopedKernelLock& lock,
    565       const int64 metahandle,
    566       const sync_pb::AttachmentMetadata& attachment_metadata);
    567 
    568   void ClearDirtyMetahandles(const ScopedKernelLock& lock);
    569 
    570   DirOpenResult OpenImpl(
    571       const std::string& name,
    572       DirectoryChangeDelegate* delegate,
    573       const WeakHandle<TransactionObserver>& transaction_observer);
    574 
    575   // A helper that implements the logic of checking tree invariants.
    576   bool CheckTreeInvariants(syncable::BaseTransaction* trans,
    577                            const MetahandleSet& handles);
    578 
    579   // Helper to prime metahandles_map, ids_map, parent_child_index,
    580   // unsynced_metahandles, unapplied_update_metahandles, server_tags_map and
    581   // client_tags_map from metahandles_index.  The input |handles_map| will be
    582   // cleared during the initialization process.
    583   void InitializeIndices(MetahandlesMap* handles_map);
    584 
    585   // Constructs a consistent snapshot of the current Directory state and
    586   // indices (by deep copy) under a ReadTransaction for use in |snapshot|.
    587   // See SaveChanges() for more information.
    588   void TakeSnapshotForSaveChanges(SaveChangesSnapshot* snapshot);
    589 
    590   // Purges from memory any unused, safe to remove entries that were
    591   // successfully deleted on disk as a result of the SaveChanges that processed
    592   // |snapshot|.  See SaveChanges() for more information.
    593   bool VacuumAfterSaveChanges(const SaveChangesSnapshot& snapshot);
    594 
    595   // Rolls back dirty bits in the event that the SaveChanges that
    596   // processed |snapshot| failed, for example, due to no disk space.
    597   void HandleSaveChangesFailure(const SaveChangesSnapshot& snapshot);
    598 
    599   // Used by CheckTreeInvariants
    600   void GetAllMetaHandles(BaseTransaction* trans, MetahandleSet* result);
    601   bool SafeToPurgeFromMemory(WriteTransaction* trans,
    602                              const EntryKernel* const entry) const;
    603 
    604   // A helper used by GetTotalNodeCount.
    605   void GetChildSetForKernel(
    606       BaseTransaction*,
    607       EntryKernel* kernel_,
    608       std::deque<const OrderedChildSet*>* child_sets) const;
    609 
    610   // Append the handles of the children of |parent_id| to |result|.
    611   void AppendChildHandles(const ScopedKernelLock& lock,
    612                           const Id& parent_id,
    613                           Directory::Metahandles* result);
    614 
    615   // Helper methods used by PurgeDisabledTypes.
    616   void UnapplyEntry(EntryKernel* entry);
    617   void DeleteEntry(const ScopedKernelLock& lock,
    618                    bool save_to_journal,
    619                    EntryKernel* entry,
    620                    EntryKernelSet* entries_to_journal);
    621 
    622   // A private version of the public GetMetaHandlesOfType for when you already
    623   // have a ScopedKernelLock.
    624   void GetMetaHandlesOfType(const ScopedKernelLock& lock,
    625                             BaseTransaction* trans,
    626                             ModelType type,
    627                             std::vector<int64>* result);
    628 
    629   Kernel* kernel_;
    630 
    631   scoped_ptr<DirectoryBackingStore> store_;
    632 
    633   UnrecoverableErrorHandler* const unrecoverable_error_handler_;
    634   const ReportUnrecoverableErrorFunction report_unrecoverable_error_function_;
    635   bool unrecoverable_error_set_;
    636 
    637   // Not owned.
    638   NigoriHandler* const nigori_handler_;
    639   Cryptographer* const cryptographer_;
    640 
    641   InvariantCheckLevel invariant_check_level_;
    642 
    643   // Maintain deleted entries not in |kernel_| until it's verified that they
    644   // are deleted in native models as well.
    645   scoped_ptr<DeleteJournal> delete_journal_;
    646 
    647   DISALLOW_COPY_AND_ASSIGN(Directory);
    648 };
    649 
    650 }  // namespace syncable
    651 }  // namespace syncer
    652 
    653 #endif // SYNC_SYNCABLE_DIRECTORY_H_
    654