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_SYNCABLE_DELETE_JOURNAL_H_
      6 #define SYNC_SYNCABLE_SYNCABLE_DELETE_JOURNAL_H_
      7 
      8 #include <set>
      9 
     10 #include "base/gtest_prod_util.h"
     11 #include "base/synchronization/lock.h"
     12 #include "sync/base/sync_export.h"
     13 #include "sync/syncable/metahandle_set.h"
     14 #include "sync/syncable/syncable-inl.h"
     15 
     16 namespace syncer {
     17 namespace syncable {
     18 
     19 class BaseTransaction;
     20 struct EntryKernel;
     21 
     22 typedef std::set<const EntryKernel*, LessField<IdField, ID> > JournalIndex;
     23 
     24 // DeleteJournal manages deleted entries that are not in sync directory until
     25 // it's safe to drop them after the deletion is confirmed with native models.
     26 // DeleteJournal is thread-safe and can be accessed on any thread. Has to hold
     27 // a valid transaction object when calling methods of DeleteJournal, thus each
     28 // method requires a non-null |trans| parameter.
     29 class SYNC_EXPORT_PRIVATE DeleteJournal {
     30  public:
     31   FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, ManageDeleteJournals);
     32 
     33   // Initialize |delete_journals_| using |intitial_journal|, whose content is
     34   // destroyed during initialization.
     35   explicit DeleteJournal(JournalIndex* initial_journal);
     36   ~DeleteJournal();
     37 
     38   // For testing only.
     39   size_t GetDeleteJournalSize(BaseTransaction* trans) const;
     40 
     41   // Add/remove |entry| to/from |delete_journals_| according to its
     42   // SERVER_IS_DEL field and |was_deleted|. Called on sync thread.
     43   void UpdateDeleteJournalForServerDelete(BaseTransaction* trans,
     44                                           bool was_deleted,
     45                                           const EntryKernel& entry);
     46 
     47   // Return entries of specified type in |delete_journals_|. This should be
     48   // called ONCE in model association. |deleted_entries| can be used to
     49   // detect deleted sync data that's not persisted in native model to
     50   // prevent back-from-dead problem. |deleted_entries| are only valid during
     51   // lifetime of |trans|. |type| is added to |passive_delete_journal_types_| to
     52   // enable periodically saving/clearing of delete journals of |type| because
     53   // new journals added later are not needed until next model association.
     54   // Can be called on any thread.
     55   void GetDeleteJournals(BaseTransaction* trans, ModelType type,
     56                          EntryKernelSet* deleted_entries);
     57 
     58   // Purge entries of specified type in |delete_journals_| if their handles are
     59   // in |to_purge|. This should be called after model association and
     60   // |to_purge| should contain handles of the entries whose deletions are
     61   // confirmed in native model. Can be called on any thread.
     62   void PurgeDeleteJournals(BaseTransaction* trans,
     63                            const MetahandleSet& to_purge);
     64 
     65   // Move entries in |delete_journals_| whose types are in
     66   // |passive_delete_journal_types_| to |journal_entries|. Move handles in
     67   // |delete_journals_to_purge_| to |journals_to_purge|. Called on sync thread.
     68   void TakeSnapshotAndClear(BaseTransaction* trans,
     69                             EntryKernelSet* journal_entries,
     70                             MetahandleSet* journals_to_purge);
     71 
     72   // Add |entries| to |delete_journals_| regardless of their SERVER_IS_DEL
     73   // value. This is used to:
     74   // * restore delete journals from snapshot if snapshot failed to save.
     75   // * batch add entries of a data type with unrecoverable error to delete
     76   //   journal before purging them.
     77   // Called on sync thread.
     78   void AddJournalBatch(BaseTransaction* trans, const EntryKernelSet& entries);
     79 
     80   // Return true if delete journals of |type| are maintained.
     81   static bool IsDeleteJournalEnabled(ModelType type);
     82 
     83  private:
     84   // Contains deleted entries that may not be persisted in native models. And
     85   // in case of unrecoverable error, all purged entries are moved here for
     86   // bookkeeping to prevent back-from-dead entries that are deleted elsewhere
     87   // when sync's down.
     88   JournalIndex delete_journals_;
     89 
     90   // Contains meta handles of deleted entries that have been persisted or
     91   // undeleted, thus can be removed from database.
     92   MetahandleSet delete_journals_to_purge_;
     93 
     94   // Delete journals of these types can be cleared from memory after being
     95   // saved to database.
     96   ModelTypeSet passive_delete_journal_types_;
     97 
     98   DISALLOW_COPY_AND_ASSIGN(DeleteJournal);
     99 };
    100 
    101 }  // namespace syncable
    102 }  // namespace syncer
    103 
    104 #endif  // SYNC_SYNCABLE_SYNCABLE_DELETE_JOURNAL_H_
    105