Debian Codegraph @

utils/merkletrie/doubleiter.go

raw
1 package merkletrie
2
3 import (
4 "fmt"
5 "io"
6
7 "github.com/go-git/go-git/v5/utils/merkletrie/noder"
8 )
9
10 // A doubleIter is a convenience type to keep track of the current
11 // noders in two merkletries that are going to be iterated in parallel.
12 // It has methods for:
13 //
14 // - iterating over the merkletries, both at the same time or
15 // individually: nextFrom, nextTo, nextBoth, stepBoth
16 //
17 // - checking if there are noders left in one or both of them with the
18 // remaining method and its associated returned type.
19 //
20 // - comparing the current noders of both merkletries in several ways,
21 // with the compare method and its associated returned type.
22 type doubleIter struct {
23 from struct {
24 iter *Iter
25 current noder.Path // nil if no more nodes
26 }
27 to struct {
28 iter *Iter
29 current noder.Path // nil if no more nodes
30 }
31 hashEqual noder.Equal
32 }
33
34 // NewdoubleIter returns a new doubleIter for the merkletries "from" and
35 // "to". The hashEqual callback function will be used by the doubleIter
36 // to compare the hash of the noders in the merkletries. The doubleIter
37 // will be initialized to the first elements in each merkletrie if any.
38 func newDoubleIter(from, to noder.Noder, hashEqual noder.Equal) (
39 *doubleIter, error) {
40 var ii doubleIter
41 var err error
42
43 if ii.from.iter, err = NewIter(from); err != nil {
44 return nil, fmt.Errorf("from: %s", err)
45 }
46 if ii.from.current, err = ii.from.iter.Next(); turnEOFIntoNil(err) != nil {
47 return nil, fmt.Errorf("from: %s", err)
48 }
49
50 if ii.to.iter, err = NewIter(to); err != nil {
51 return nil, fmt.Errorf("to: %s", err)
52 }
53 if ii.to.current, err = ii.to.iter.Next(); turnEOFIntoNil(err) != nil {
54 return nil, fmt.Errorf("to: %s", err)
55 }
56
57 ii.hashEqual = hashEqual
58
59 return &ii, nil
60 }
61
62 func turnEOFIntoNil(e error) error {
63 if e != nil && e != io.EOF {
64 return e
65 }
66 return nil
67 }
68
69 // NextBoth makes d advance to the next noder in both merkletries. If
70 // any of them is a directory, it skips its contents.
71 func (d *doubleIter) nextBoth() error {
72 if err := d.nextFrom(); err != nil {
73 return err
74 }
75 if err := d.nextTo(); err != nil {
76 return err
77 }
78
79 return nil
80 }
81
82 // NextFrom makes d advance to the next noder in the "from" merkletrie,
83 // skipping its contents if it is a directory.
84 func (d *doubleIter) nextFrom() (err error) {
85 d.from.current, err = d.from.iter.Next()
86 return turnEOFIntoNil(err)
87 }
88
89 // NextTo makes d advance to the next noder in the "to" merkletrie,
90 // skipping its contents if it is a directory.
91 func (d *doubleIter) nextTo() (err error) {
92 d.to.current, err = d.to.iter.Next()
93 return turnEOFIntoNil(err)
94 }
95
96 // StepBoth makes d advance to the next noder in both merkletries,
97 // getting deeper into directories if that is the case.
98 func (d *doubleIter) stepBoth() (err error) {
99 if d.from.current, err = d.from.iter.Step(); turnEOFIntoNil(err) != nil {
100 return err
101 }
102 if d.to.current, err = d.to.iter.Step(); turnEOFIntoNil(err) != nil {
103 return err
104 }
105 return nil
106 }
107
108 // Remaining returns if there are no more noders in the tree, if both
109 // have noders or if one of them doesn't.
110 func (d *doubleIter) remaining() remaining {
111 if d.from.current == nil && d.to.current == nil {
112 return noMoreNoders
113 }
114
115 if d.from.current == nil && d.to.current != nil {
116 return onlyToRemains
117 }
118
119 if d.from.current != nil && d.to.current == nil {
120 return onlyFromRemains
121 }
122
123 return bothHaveNodes
124 }
125
126 // Remaining values tells you whether both trees still have noders, or
127 // only one of them or none of them.
128 type remaining int
129
130 const (
131 noMoreNoders remaining = iota
132 onlyToRemains
133 onlyFromRemains
134 bothHaveNodes
135 )
136
137 // Compare returns the comparison between the current elements in the
138 // merkletries.
139 func (d *doubleIter) compare() (s comparison, err error) {
140 s.sameHash = d.hashEqual(d.from.current, d.to.current)
141
142 fromIsDir := d.from.current.IsDir()
143 toIsDir := d.to.current.IsDir()
144
145 s.bothAreDirs = fromIsDir && toIsDir
146 s.bothAreFiles = !fromIsDir && !toIsDir
147 s.fileAndDir = !s.bothAreDirs && !s.bothAreFiles
148
149 fromNumChildren, err := d.from.current.NumChildren()
150 if err != nil {
151 return comparison{}, fmt.Errorf("from: %s", err)
152 }
153
154 toNumChildren, err := d.to.current.NumChildren()
155 if err != nil {
156 return comparison{}, fmt.Errorf("to: %s", err)
157 }
158
159 s.fromIsEmptyDir = fromIsDir && fromNumChildren == 0
160 s.toIsEmptyDir = toIsDir && toNumChildren == 0
161
162 return
163 }
164
165 // Answers to a lot of questions you can ask about how to noders are
166 // equal or different.
167 type comparison struct {
168 // the following are only valid if both nodes have the same name
169 // (i.e. nameComparison == 0)
170
171 // Do both nodes have the same hash?
172 sameHash bool
173 // Are both nodes files?
174 bothAreFiles bool
175
176 // the following are only valid if any of the noders are dirs,
177 // this is, if !bothAreFiles
178
179 // Is one a file and the other a dir?
180 fileAndDir bool
181 // Are both nodes dirs?
182 bothAreDirs bool
183 // Is the from node an empty dir?
184 fromIsEmptyDir bool
185 // Is the to Node an empty dir?
186 toIsEmptyDir bool
187 }