OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | 5 #ifndef CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
6 #define CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | 6 #define CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
7 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <bitset> | 9 #include <bitset> |
10 #include <iosfwd> | 10 #include <iosfwd> |
(...skipping 552 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
563 // the case with a GetUpdates request. | 563 // the case with a GetUpdates request. |
564 struct MultiTypeTimeStamp { | 564 struct MultiTypeTimeStamp { |
565 syncable::ModelTypeBitSet data_types; | 565 syncable::ModelTypeBitSet data_types; |
566 int64 timestamp; | 566 int64 timestamp; |
567 }; | 567 }; |
568 | 568 |
569 // a WriteTransaction has a writer tag describing which body of code is doing | 569 // a WriteTransaction has a writer tag describing which body of code is doing |
570 // the write. This is defined up here since DirectoryChangeEvent also contains | 570 // the write. This is defined up here since DirectoryChangeEvent also contains |
571 // one. | 571 // one. |
572 enum WriterTag { | 572 enum WriterTag { |
573 INVALID, SYNCER, AUTHWATCHER, UNITTEST, VACUUM_AFTER_SAVE, SYNCAPI | 573 INVALID, |
| 574 SYNCER, |
| 575 AUTHWATCHER, |
| 576 UNITTEST, |
| 577 VACUUM_AFTER_SAVE, |
| 578 PURGE_ENTRIES, |
| 579 SYNCAPI |
574 }; | 580 }; |
575 | 581 |
576 // A separate Event type and channel for very frequent changes, caused | 582 // A separate Event type and channel for very frequent changes, caused |
577 // by anything, not just the user. | 583 // by anything, not just the user. |
578 struct DirectoryChangeEvent { | 584 struct DirectoryChangeEvent { |
579 enum { | 585 enum { |
580 // Means listener should go through list of original entries and | 586 // Means listener should go through list of original entries and |
581 // calculate what it needs to notify. It should *not* call any | 587 // calculate what it needs to notify. It should *not* call any |
582 // callbacks or attempt to lock anything because a | 588 // callbacks or attempt to lock anything because a |
583 // WriteTransaction is being held until the listener returns. | 589 // WriteTransaction is being held until the listener returns. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 friend class ReadTransactionWithoutDB; | 643 friend class ReadTransactionWithoutDB; |
638 friend class ScopedKernelLock; | 644 friend class ScopedKernelLock; |
639 friend class ScopedKernelUnlock; | 645 friend class ScopedKernelUnlock; |
640 friend class WriteTransaction; | 646 friend class WriteTransaction; |
641 friend class SyncableDirectoryTest; | 647 friend class SyncableDirectoryTest; |
642 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, | 648 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, |
643 TakeSnapshotGetsAllDirtyHandlesTest); | 649 TakeSnapshotGetsAllDirtyHandlesTest); |
644 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, | 650 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, |
645 TakeSnapshotGetsOnlyDirtyHandlesTest); | 651 TakeSnapshotGetsOnlyDirtyHandlesTest); |
646 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, TestPurgeEntriesWithTypeIn); | 652 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, TestPurgeEntriesWithTypeIn); |
| 653 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, |
| 654 TakeSnapshotGetsMetahandlesToPurge); |
647 | 655 |
648 public: | 656 public: |
649 class EventListenerHookup; | 657 class EventListenerHookup; |
650 | 658 |
651 // Various data that the Directory::Kernel we are backing (persisting data | 659 // Various data that the Directory::Kernel we are backing (persisting data |
652 // for) needs saved across runs of the application. | 660 // for) needs saved across runs of the application. |
653 struct PersistedKernelInfo { | 661 struct PersistedKernelInfo { |
654 // Last sync timestamp fetched from the server. | 662 // Last sync timestamp fetched from the server. |
655 int64 last_download_timestamp[MODEL_TYPE_COUNT]; | 663 int64 last_download_timestamp[MODEL_TYPE_COUNT]; |
656 // true iff we ever reached the end of the changelog. | 664 // true iff we ever reached the end of the changelog. |
(...skipping 28 matching lines...) Expand all Loading... |
685 KERNEL_SHARE_INFO_DIRTY | 693 KERNEL_SHARE_INFO_DIRTY |
686 }; | 694 }; |
687 | 695 |
688 // When the Directory is told to SaveChanges, a SaveChangesSnapshot is | 696 // When the Directory is told to SaveChanges, a SaveChangesSnapshot is |
689 // constructed and forms a consistent snapshot of what needs to be sent to | 697 // constructed and forms a consistent snapshot of what needs to be sent to |
690 // the backing store. | 698 // the backing store. |
691 struct SaveChangesSnapshot { | 699 struct SaveChangesSnapshot { |
692 KernelShareInfoStatus kernel_info_status; | 700 KernelShareInfoStatus kernel_info_status; |
693 PersistedKernelInfo kernel_info; | 701 PersistedKernelInfo kernel_info; |
694 OriginalEntries dirty_metas; | 702 OriginalEntries dirty_metas; |
| 703 MetahandleSet metahandles_to_purge; |
695 SaveChangesSnapshot() : kernel_info_status(KERNEL_SHARE_INFO_INVALID) { | 704 SaveChangesSnapshot() : kernel_info_status(KERNEL_SHARE_INFO_INVALID) { |
696 } | 705 } |
697 }; | 706 }; |
698 | 707 |
699 Directory(); | 708 Directory(); |
700 virtual ~Directory(); | 709 virtual ~Directory(); |
701 | 710 |
702 DirOpenResult Open(const FilePath& file_path, const std::string& name); | 711 DirOpenResult Open(const FilePath& file_path, const std::string& name); |
703 | 712 |
704 void Close(); | 713 void Close(); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
766 EntryKernel* GetEntryByClientTag(const std::string& tag); | 775 EntryKernel* GetEntryByClientTag(const std::string& tag); |
767 EntryKernel* GetRootEntry(); | 776 EntryKernel* GetRootEntry(); |
768 bool ReindexId(EntryKernel* const entry, const Id& new_id); | 777 bool ReindexId(EntryKernel* const entry, const Id& new_id); |
769 void ReindexParentId(EntryKernel* const entry, const Id& new_parent_id); | 778 void ReindexParentId(EntryKernel* const entry, const Id& new_parent_id); |
770 void ClearDirtyMetahandles(); | 779 void ClearDirtyMetahandles(); |
771 | 780 |
772 // These don't do semantic checking. | 781 // These don't do semantic checking. |
773 // The semantic checking is implemented higher up. | 782 // The semantic checking is implemented higher up. |
774 void Undelete(EntryKernel* const entry); | 783 void Undelete(EntryKernel* const entry); |
775 void Delete(EntryKernel* const entry); | 784 void Delete(EntryKernel* const entry); |
| 785 void UnlinkEntryFromOrder(EntryKernel* entry, |
| 786 WriteTransaction* trans, |
| 787 ScopedKernelLock* lock); |
776 | 788 |
777 // Overridden by tests. | 789 // Overridden by tests. |
778 virtual DirectoryBackingStore* CreateBackingStore( | 790 virtual DirectoryBackingStore* CreateBackingStore( |
779 const std::string& dir_name, | 791 const std::string& dir_name, |
780 const FilePath& backing_filepath); | 792 const FilePath& backing_filepath); |
781 | 793 |
782 private: | 794 private: |
783 // These private versions expect the kernel lock to already be held | 795 // These private versions expect the kernel lock to already be held |
784 // before calling. | 796 // before calling. |
785 EntryKernel* GetEntryById(const Id& id, ScopedKernelLock* const lock); | 797 EntryKernel* GetEntryById(const Id& id, ScopedKernelLock* const lock); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 const MetahandleSet& handles, | 867 const MetahandleSet& handles, |
856 const IdFilter& idfilter); | 868 const IdFilter& idfilter); |
857 | 869 |
858 // Purges all data associated with any entries whose ModelType or | 870 // Purges all data associated with any entries whose ModelType or |
859 // ServerModelType is found in |types|, from _both_ memory and disk. | 871 // ServerModelType is found in |types|, from _both_ memory and disk. |
860 // Only valid, "real" model types are allowed in |types| (see model_type.h | 872 // Only valid, "real" model types are allowed in |types| (see model_type.h |
861 // for definitions). "Purge" is just meant to distinguish from "deleting" | 873 // for definitions). "Purge" is just meant to distinguish from "deleting" |
862 // entries, which means something different in the syncable namespace. | 874 // entries, which means something different in the syncable namespace. |
863 // WARNING! This can be real slow, as it iterates over all entries. | 875 // WARNING! This can be real slow, as it iterates over all entries. |
864 // WARNING! Performs synchronous I/O. | 876 // WARNING! Performs synchronous I/O. |
865 void PurgeEntriesWithTypeIn(const std::set<ModelType>& types); | 877 virtual void PurgeEntriesWithTypeIn(const std::set<ModelType>& types); |
866 | 878 |
867 private: | 879 private: |
868 // Helper to prime ids_index, parent_id_and_names_index, unsynced_metahandles | 880 // Helper to prime ids_index, parent_id_and_names_index, unsynced_metahandles |
869 // and unapplied_metahandles from metahandles_index. | 881 // and unapplied_metahandles from metahandles_index. |
870 void InitializeIndices(); | 882 void InitializeIndices(); |
871 | 883 |
872 // Constructs a consistent snapshot of the current Directory state and | 884 // Constructs a consistent snapshot of the current Directory state and |
873 // indices (by deep copy) under a ReadTransaction for use in |snapshot|. | 885 // indices (by deep copy) under a ReadTransaction for use in |snapshot|. |
874 // See SaveChanges() for more information. | 886 // See SaveChanges() for more information. |
875 void TakeSnapshotForSaveChanges(SaveChangesSnapshot* snapshot); | 887 void TakeSnapshotForSaveChanges(SaveChangesSnapshot* snapshot); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
919 // It allows efficient lookup of the children of a given parent. | 931 // It allows efficient lookup of the children of a given parent. |
920 typedef std::set<EntryKernel*, LessParentIdAndHandle> ParentIdChildIndex; | 932 typedef std::set<EntryKernel*, LessParentIdAndHandle> ParentIdChildIndex; |
921 | 933 |
922 // Contains both deleted and existing entries with tags. | 934 // Contains both deleted and existing entries with tags. |
923 // We can't store only existing tags because the client would create | 935 // We can't store only existing tags because the client would create |
924 // items that had a duplicated ID in the end, resulting in a DB key | 936 // items that had a duplicated ID in the end, resulting in a DB key |
925 // violation. ID reassociation would fail after an attempted commit. | 937 // violation. ID reassociation would fail after an attempted commit. |
926 typedef std::set<EntryKernel*, | 938 typedef std::set<EntryKernel*, |
927 LessField<StringField, UNIQUE_CLIENT_TAG> > ClientTagIndex; | 939 LessField<StringField, UNIQUE_CLIENT_TAG> > ClientTagIndex; |
928 | 940 |
| 941 protected: |
| 942 // Used by tests. |
| 943 void init_kernel(const std::string& name); |
| 944 |
929 private: | 945 private: |
930 | 946 |
931 struct Kernel { | 947 struct Kernel { |
932 Kernel(const FilePath& db_path, const std::string& name, | 948 Kernel(const FilePath& db_path, const std::string& name, |
933 const KernelLoadInfo& info); | 949 const KernelLoadInfo& info); |
934 | 950 |
935 ~Kernel(); | 951 ~Kernel(); |
936 | 952 |
937 void AddRef(); // For convenience. | 953 void AddRef(); // For convenience. |
938 void Release(); | 954 void Release(); |
(...skipping 24 matching lines...) Expand all Loading... |
963 // look something up in an index. Needle in haystack metaphor. | 979 // look something up in an index. Needle in haystack metaphor. |
964 EntryKernel needle; | 980 EntryKernel needle; |
965 | 981 |
966 // 3 in-memory indices on bits used extremely frequently by the syncer. | 982 // 3 in-memory indices on bits used extremely frequently by the syncer. |
967 MetahandleSet* const unapplied_update_metahandles; | 983 MetahandleSet* const unapplied_update_metahandles; |
968 MetahandleSet* const unsynced_metahandles; | 984 MetahandleSet* const unsynced_metahandles; |
969 // Contains metahandles that are most likely dirty (though not | 985 // Contains metahandles that are most likely dirty (though not |
970 // necessarily). Dirtyness is confirmed in TakeSnapshotForSaveChanges(). | 986 // necessarily). Dirtyness is confirmed in TakeSnapshotForSaveChanges(). |
971 MetahandleSet* const dirty_metahandles; | 987 MetahandleSet* const dirty_metahandles; |
972 | 988 |
| 989 // When a purge takes place, we remove items from all our indices and stash |
| 990 // them in here so that SaveChanges can persist their permanent deletion. |
| 991 MetahandleSet* const metahandles_to_purge; |
| 992 |
973 // TODO(ncarter): Figure out what the hell this is, and comment it. | 993 // TODO(ncarter): Figure out what the hell this is, and comment it. |
974 Channel* const channel; | 994 Channel* const channel; |
975 | 995 |
976 // The changes channel mutex is explicit because it must be locked | 996 // The changes channel mutex is explicit because it must be locked |
977 // while holding the transaction mutex and released after | 997 // while holding the transaction mutex and released after |
978 // releasing the transaction mutex. | 998 // releasing the transaction mutex. |
979 browser_sync::Channel<DirectoryChangeEvent> changes_channel; | 999 browser_sync::Channel<DirectoryChangeEvent> changes_channel; |
980 | 1000 |
981 Lock changes_channel_mutex; | 1001 Lock changes_channel_mutex; |
982 KernelShareInfoStatus info_status; | 1002 KernelShareInfoStatus info_status; |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1102 void ZeroFields(EntryKernel* entry, int first_field); | 1122 void ZeroFields(EntryKernel* entry, int first_field); |
1103 | 1123 |
1104 } // namespace syncable | 1124 } // namespace syncable |
1105 | 1125 |
1106 std::ostream& operator <<(std::ostream&, const syncable::Blob&); | 1126 std::ostream& operator <<(std::ostream&, const syncable::Blob&); |
1107 | 1127 |
1108 browser_sync::FastDump& operator << | 1128 browser_sync::FastDump& operator << |
1109 (browser_sync::FastDump&, const syncable::Blob&); | 1129 (browser_sync::FastDump&, const syncable::Blob&); |
1110 | 1130 |
1111 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ | 1131 #endif // CHROME_BROWSER_SYNC_SYNCABLE_SYNCABLE_H_ |
OLD | NEW |