merged tree overview (tree visitor pattern)

16

Upload: mckenzie-mckee

Post on 30-Dec-2015

25 views

Category:

Documents


2 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Merged Tree Overview (Tree Visitor Pattern)
Page 2: Merged Tree Overview (Tree Visitor Pattern)
Page 3: Merged Tree Overview (Tree Visitor Pattern)

1. BuilderVisitor2. XMLMetadataVisitor3. ProcessMetadataVisitor4. FolderRenameVisitor5. ComparerVisitor6. SyncerVisitor7. XMLWriterVisitor

Page 4: Merged Tree Overview (Tree Visitor Pattern)

1. BuilderVisitor Builds the merged tree.

2. ComparerVisitor Compares and updates the state of

each node.3. SyncerVisitor

Takes the updated state of each node and performs an action to keep them synchronized.

Page 5: Merged Tree Overview (Tree Visitor Pattern)

BuilderVisitor

Page 6: Merged Tree Overview (Tree Visitor Pattern)

Traverse and build or populate tree for “C:\Users\Wysie\Desktop\A”

RootCompareObject

string[] pathspaths[0] = “C:\Users\Wysie\Desktop\A”paths[1] = “C:\Users\Wysie\Desktop\B”paths[2] = “C:\Users\Wysie\Desktop\C”

FileCompareObject

name = “CueSheet.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “QWERTY”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 99999;long[1] = 0;long[2] = 0;

FileCompareObject

name = “Results.txt”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “AAAAA”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 70000;long[1] = 0;long[2] = 0;

FolderCompareObject

name = “School Work”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

FileCompareObject

name = “Final.pdf”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ZXCVB”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 30000;long[1] = 0;long[2] = 0;

FileCompareObject

name = “Proposal.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ASDFG”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 10000;long[1] = 0;long[2] = 0;

Since “A” is the first folder to be build, all the FileCompareObjects or FolderCompareObjects will be created.

Index 0: C:\Users\Wysie\Desktop\AIndex 1: C:\Users\Wysie\Desktop\BIndex 2: C:\Users\Wysie\Desktop\C

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.

Page 7: Merged Tree Overview (Tree Visitor Pattern)

Traverse and build or populate tree for “C:\Users\Wysie\Desktop\B”

RootCompareObject

string[] pathspaths[0] = “C:\Users\Wysie\Desktop\A”paths[1] = “C:\Users\Wysie\Desktop\B”paths[2] = “C:\Users\Wysie\Desktop\C”

FileCompareObject

name = “CueSheet.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “QWERTY”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 99999;long[1] = 0;long[2] = 0;

FileCompareObject

name = “Results.txt”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “AAAAA”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 70000;long[1] = 0;long[2] = 0;

FolderCompareObject

name = “School Work”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

FileCompareObject

name = “Final.pdf”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ZXCVB”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 30000;long[1] = 0;long[2] = 0;

FileCompareObject

name = “Proposal.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ASDFG”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 10000;long[1] = 0;long[2] = 0;

Index 0: C:\Users\Wysie\Desktop\AIndex 1: C:\Users\Wysie\Desktop\BIndex 2: C:\Users\Wysie\Desktop\C

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.

FileCompareObject

name = “CueSheet.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “QWERTY”;hash[1] = “MNBVC”;hash[2] = null;

long[] lastModified;long[0] = 99999;long[1] = 88888;long[2] = 0;

FileCompareObject

name = “Results.txt”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “AAAAA”;hash[1] = “VVBBB”;hash[2] = null;

long[] lastModified;long[0] = 70000;long[1] = 90000;long[2] = 0;

“Results.txt” is also found in “B”. We now populate index 1 of “Results.txt” with information.

Page 8: Merged Tree Overview (Tree Visitor Pattern)

FileCompareObject

name = “Results.txt”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “AAAAA”;hash[1] = “VVBBB”;hash[2] = null;

long[] lastModified;long[0] = 70000;long[1] = 90000;long[2] = 0;

FileCompareObject

name = “CueSheet.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “QWERTY”;hash[1] = “MNBVC”;hash[2] = null;

long[] lastModified;long[0] = 99999;long[1] = 88888;long[2] = 0;

Traverse and build or populate tree for “C:\Users\Wysie\Desktop\C”

RootCompareObject

string[] pathspaths[0] = “C:\Users\Wysie\Desktop\A”paths[1] = “C:\Users\Wysie\Desktop\B”paths[2] = “C:\Users\Wysie\Desktop\C”

FolderCompareObject

name = “School Work”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

FileCompareObject

name = “Final.pdf”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ZXCVB”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 30000;long[1] = 0;long[2] = 0;

FileCompareObject

name = “Proposal.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ASDFG”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 10000;long[1] = 0;long[2] = 0;

Index 0: C:\Users\Wysie\Desktop\AIndex 1: C:\Users\Wysie\Desktop\BIndex 2: C:\Users\Wysie\Desktop\C

“School Work” exists in “C”. Populate index 2 in “C”.

FolderCompareObject

name = “School Work”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = true

“Final.pdf” does not exist. However, “Proposal.docx” exists. Populate at index 2.

FileCompareObject

name = “Proposal.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = true

string[] hash;hash[0] = “ASDFG”;hash[1] = null;hash[2] = “DFGHJK”;

long[] lastModified;long[0] = 10000;long[1] = 0;long[2] = 10000;

Page 9: Merged Tree Overview (Tree Visitor Pattern)

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.

Page 10: Merged Tree Overview (Tree Visitor Pattern)

ComparerVisitor

Page 11: Merged Tree Overview (Tree Visitor Pattern)

FileCompareObject

name = “Proposal.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = true

string[] hash;hash[0] = “ASDFG”;hash[1] = null;hash[2] = “DFGHJK”;

long[] lastModified;long[0] = 10000;long[1] = 0;long[2] = 10000;

FolderCompareObject

name = “School Work”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = true

FileCompareObject

name = “Results.txt”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “AAAAA”;hash[1] = “VVBBB”;hash[2] = null;

long[] lastModified;long[0] = 70000;long[1] = 90000;long[2] = 0;

FileCompareObject

name = “CueSheet.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “QWERTY”;hash[1] = “MNBVC”;hash[2] = null;

long[] lastModified;long[0] = 99999;long[1] = 88888;long[2] = 0;

ComparerVisitor will now visit the tree.

RootCompareObject

string[] pathspaths[0] = “C:\Users\Wysie\Desktop\A”paths[1] = “C:\Users\Wysie\Desktop\B”paths[2] = “C:\Users\Wysie\Desktop\C”

FileCompareObject

name = “Final.pdf”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ZXCVB”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 30000;long[1] = 0;long[2] = 0;

Index 0: C:\Users\Wysie\Desktop\AIndex 1: C:\Users\Wysie\Desktop\BIndex 2: C:\Users\Wysie\Desktop\C

FileCompareObject

name = “CueSheet.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “QWERTY”;hash[1] = “MNBVC”;hash[2] = null;

long[] lastModified;long[0] = 99999;long[1] = 88888;long[2] = 0;

srcPosition = 0

FileCompareObject

name = “Results.txt”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “AAAAA”;hash[1] = “VVBBB”;hash[2] = null;

long[] lastModified;long[0] = 70000;long[1] = 90000;long[2] = 0;

srcPosition = 1

FolderCompareObject

name = “School Work”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = true

srcPosition = 0

FileCompareObject

name = “Final.pdf”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ZXCVB”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 30000;long[1] = 0;long[2] = 0;

srcPosition = 0

FileCompareObject

name = “Proposal.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = true

string[] hash;hash[0] = “ASDFG”;hash[1] = null;hash[2] = “ASDFG”;

long[] lastModified;long[0] = 10000;long[1] = 0;long[2] = 10000;

srcPosition = 0

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.

Page 12: Merged Tree Overview (Tree Visitor Pattern)

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.

Page 13: Merged Tree Overview (Tree Visitor Pattern)

SyncerVisitor

Page 14: Merged Tree Overview (Tree Visitor Pattern)

FolderCompareObject

name = “School Work”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = true

srcPosition = 0

FileCompareObject

name = “Final.pdf”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = false

string[] hash;hash[0] = “ZXCVB”;hash[1] = null;hash[2] = null;

long[] lastModified;long[0] = 30000;long[1] = 0;long[2] = 0;

srcPosition = 0

FileCompareObject

name = “Proposal.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = falseexists[2] = true

string[] hash;hash[0] = “ASDFG”;hash[1] = null;hash[2] = “ASDFG”;

long[] lastModified;long[0] = 10000;long[1] = 0;long[2] = 10000;

srcPosition = 0

FileCompareObject

name = “Results.txt”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “AAAAA”;hash[1] = “VVBBB”;hash[2] = null;

long[] lastModified;long[0] = 70000;long[1] = 90000;long[2] = 0;

srcPosition = 1

FileCompareObject

name = “CueSheet.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = false

string[] hash;hash[0] = “QWERTY”;hash[1] = “MNBVC”;hash[2] = null;

long[] lastModified;long[0] = 99999;long[1] = 88888;long[2] = 0;

srcPosition = 0

SyncerVisitor will now visit the tree.

RootCompareObject

string[] pathspaths[0] = “C:\Users\Wysie\Desktop\A”paths[1] = “C:\Users\Wysie\Desktop\B”paths[2] = “C:\Users\Wysie\Desktop\C”

Index 0: C:\Users\Wysie\Desktop\AIndex 1: C:\Users\Wysie\Desktop\BIndex 2: C:\Users\Wysie\Desktop\C

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.

FolderCompareObject

name = “School Work”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = true

srcPosition = 0

FileCompareObject

name = “Final.pdf”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = true

string[] hash;hash[0] = “ZXCVB”;hash[1] = “ZXCVB”;;hash[2] = “ZXCVB”;;

long[] lastModified;long[0] = 30000;long[1] = 30000;long[2] = 30000;

srcPosition = 0

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.

FileCompareObject

name = “Proposal.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = true

string[] hash;hash[0] = “ASDFG”;hash[1] = “ASDFG”;hash[2] = “ASDFG”;

long[] lastModified;long[0] = 10000;long[1] = 10000;long[2] = 10000;

srcPosition = 0

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.

FileCompareObject

name = “CueSheet.docx”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = true

string[] hash;hash[0] = “QWERTY”;hash[1] = “QWERTY”;hash[2] = “QWERTY”;

long[] lastModified;long[0] = 99999;long[1] = 99999;long[2] = 99999;

srcPosition = 0

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.

FileCompareObject

name = “Results.txt”

numOfPaths = 3

bool[] exists;exists[0] = trueexists[1] = trueexists[2] = true

string[] hash;hash[0] = “VVBBB”;hash[1] = “VVBBB”;hash[2] = “VVBBB”;

long[] lastModified;long[0] = 90000;long[1] = 90000;long[2] = 90000;

srcPosition = 1

With that, all the files and folders are now synchronized.

Page 15: Merged Tree Overview (Tree Visitor Pattern)

As stated earlier, a priority array in each node is used to determine if a file is actually copied over.

Page 16: Merged Tree Overview (Tree Visitor Pattern)

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.