160 likes | 264 Views
Merged Tree Overview (Tree Visitor Pattern). Given The Following 3 Folders. The Following Visitors Are Required If We Want To Synchronize Them. BuilderVisitor XMLMetadataVisitor ProcessMetadataVisitor FolderRenameVisitor ComparerVisitor SyncerVisitor XMLWriterVisitor.
E N D
The Following Visitors Are Required If We Want To Synchronize Them • BuilderVisitor • XMLMetadataVisitor • ProcessMetadataVisitor • FolderRenameVisitor • ComparerVisitor • SyncerVisitor • XMLWriterVisitor
For The Sake Of Simplification, We Will Look At The Following • BuilderVisitor • Builds the merged tree. • ComparerVisitor • Compares and updates the state of each node. • SyncerVisitor • Takes the updated state of each node and performs an action to keep them synchronized.
BuilderVisitor Step 1
Traverse and build or populate tree for “C:\Users\Wysie\Desktop\A” Since “A” is the first folder to be build, all the FileCompareObjects or FolderCompareObjects will be created. Build FolderCompareObject with name “School Work”, and populate information at index 0. Now, traverse the contents of “School Work” and build new objects. In this case, build “Final.pdf” and “Proposal.docx” and populate index 0. Nothing else to build, so we return all the way back to RootCompareObject. Now, we build “CueSheet.docx” and populate information at index 0. Finally, we build “Results.txt” and populate the information at index 0. Index 0: C:\Users\Wysie\Desktop\A Index 1: C:\Users\Wysie\Desktop\B Index 2: C:\Users\Wysie\Desktop\C
Traverse and build or populate tree for “C:\Users\Wysie\Desktop\B” Since “School Work” does not exist in “B”, the entire portion remains unchanged. Since “School Work” does not exist in “B”, the entire portion remains unchanged. “CueSheet.docx” is found in “B”. We now populate index 1 of “CueSheet.docx” with information. “Results.txt” is also found in “B”. We now populate index 1 of “Results.txt” with information. Index 0: C:\Users\Wysie\Desktop\A Index 1: C:\Users\Wysie\Desktop\B Index 2: C:\Users\Wysie\Desktop\C
Traverse and build or populate tree for “C:\Users\Wysie\Desktop\C” “School Work” exists in “C”. Populate index 2 in “C”. “Final.pdf” does not exist. However, “Proposal.docx” exists. Populate at index 2. Index 0: C:\Users\Wysie\Desktop\A Index 1: C:\Users\Wysie\Desktop\B Index 2: C:\Users\Wysie\Desktop\C
Further Explanation • There are many other attributes that are left out, such as priority, new name, creation time, among many others. For a more detailed explanation, please take a look at the source code comments.
ComparerVisitor Step 2
ComparerVisitor will now visit the tree. It will first visit “School Work”. Since it exists in index 0 and index 2, but not in index 1, the algorithm will set source position to 0. Now, it will traverse the contents of “School Work”. For “Final.pdf”, since it only exists in index 0, source position will again be set to 0. For “Proposal.docx”, it exists in 0 and 2, but not in 1. Comparing the hash of index 0 and 2 shows that they are identical. Thus, source position will be set to 0. It will now visit “CueSheet.docx”. Since it exists in index 0 and 1 but not in 2, comparison will be done between 0 and 1. Based on the last modified date, the one at index 0 is more updated then the one at index 1. Thus, source position will be set to 0. Finally, “Results.txt” is visited. Again, it exists in index 0 and 1. This time round, the one at index 1 is found to be more updated. Source position is 1. Index 0: C:\Users\Wysie\Desktop\A Index 1: C:\Users\Wysie\Desktop\B Index 2: C:\Users\Wysie\Desktop\C
Further Explanation • The purpose of source position is for SyncerVisitor to find out which file to propagate. • In actual fact, there is another array called priority. All files which are determined to be equal (same hash if create/update, same new name if rename, or file does not exist if delete) are given equal priority. • During actual synchronization, only lower priority indexes than that of the source position will be affected. This is to prevent redundant file or folder changes.
SyncerVisitor Step 3
SyncerVisitor will now visit the tree. It will first visit “School Work”, determine the source position, and then create a folder when necessary. The state of the node will then be updated. Next it will visit “Final.pdf”. Since source position is 0, it will be copied over to the other positions, and the state of the node will then be updated. Then, it will visit “Proposal.docx”. Since source position is 0, but index 2 is determined to be equal, only 1 will be updated. The next node to be visited is “CueSheet.docx”. Since the source position is 0, and it is the most updated, it will propagate to the other 2 locations. Finally, “Results.txt” will be visited. Since the source position is 1, and it is more updated then the rest, it will propagate to the other nodes. With that, all the files and folders are now synchronized. Index 0: C:\Users\Wysie\Desktop\A Index 1: C:\Users\Wysie\Desktop\B Index 2: C:\Users\Wysie\Desktop\C
Further Explanation • As stated earlier, a priority array in each node is used to determine if a file is actually copied over.
Summary • We hope this simple presentation will give you a good overview of how the visitor pattern, and in particular, how our merged tree, works. • Many other functions and visitors were not explained, please look at the detailed comments in the source code, as well as the developer guide, if you are interested. • Traversing the tree can be pre, post, or level-order traversal, depending on what is needed.