Skip to content

Commit 1f53d04

Browse files
committed
iterator: calculate real size for LinksTo(Fixed)
1 parent 4344a55 commit 1f53d04

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

graph/iterator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ type Iterator interface {
175175

176176
// DescribeIterator returns a description of the iterator tree.
177177
func DescribeIterator(it Iterator) Description {
178-
sz, _ := it.Size()
178+
sz, exact := it.Size()
179179
d := Description{
180180
UID: it.UID(),
181181
Name: it.String(),
182182
Type: it.Type(),
183183
Tags: it.Tagger().Tags(),
184-
Size: sz,
184+
Size: sz, Exact: exact,
185185
}
186186
if sub := it.SubIterators(); len(sub) != 0 {
187187
d.Iterators = make([]Description, 0, len(sub))
@@ -198,6 +198,7 @@ type Description struct {
198198
Type Type `json:",omitempty"`
199199
Tags []string `json:",omitempty"`
200200
Size int64 `json:",omitempty"`
201+
Exact bool `json:",omitempty"`
201202
Iterators []Description `json:",omitempty"`
202203
}
203204

graph/iterator/fixed.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ func (it *Fixed) Add(v graph.Value) {
8585
it.values = append(it.values, v)
8686
}
8787

88+
// Values returns a list of values stored in iterator. Slice should not be modified.
89+
func (it *Fixed) Values() []graph.Value {
90+
return it.values
91+
}
92+
8893
func (it *Fixed) String() string {
8994
return fmt.Sprintf("Fixed(%v)", it.values)
9095
}

graph/iterator/linksto.go

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (it *LinksTo) Tagger() *graph.Tagger {
8585
func (it *LinksTo) Clone() graph.Iterator {
8686
out := NewLinksTo(it.qs, it.primaryIt.Clone(), it.dir)
8787
out.tags.CopyFrom(it)
88+
out.runstats.Size, out.runstats.ExactSize = it.runstats.Size, it.runstats.ExactSize
8889
return out
8990
}
9091

@@ -211,21 +212,43 @@ func (it *LinksTo) Type() graph.Type { return graph.LinksTo }
211212
func (it *LinksTo) Stats() graph.IteratorStats {
212213
subitStats := it.primaryIt.Stats()
213214
// TODO(barakmich): These should really come from the quadstore itself
214-
fanoutFactor := int64(20)
215215
checkConstant := int64(1)
216216
nextConstant := int64(2)
217-
return graph.IteratorStats{
217+
st := graph.IteratorStats{
218218
NextCost: nextConstant + subitStats.NextCost,
219219
ContainsCost: checkConstant + subitStats.ContainsCost,
220-
Size: fanoutFactor * subitStats.Size,
221-
ExactSize: false,
222220
Next: it.runstats.Next,
223221
Contains: it.runstats.Contains,
224222
ContainsNext: it.runstats.ContainsNext,
225223
}
224+
st.Size, st.ExactSize = it.Size()
225+
return st
226226
}
227227

228228
func (it *LinksTo) Size() (int64, bool) {
229-
st := it.Stats()
230-
return st.Size, st.ExactSize
229+
if it.runstats.Size != 0 {
230+
return it.runstats.Size, it.runstats.ExactSize
231+
}
232+
if fixed, ok := it.primaryIt.(*Fixed); ok {
233+
// get real sizes from sub iterators
234+
var (
235+
sz int64
236+
exact = true
237+
)
238+
for _, v := range fixed.Values() {
239+
sit := it.qs.QuadIterator(it.dir, v)
240+
n, ex := sit.Size()
241+
sit.Close()
242+
sz += n
243+
exact = exact && ex
244+
}
245+
it.runstats.Size, it.runstats.ExactSize = sz, exact
246+
return sz, exact
247+
}
248+
// TODO(barakmich): It should really come from the quadstore itself
249+
const fanoutFactor = 20
250+
sz, _ := it.primaryIt.Size()
251+
sz *= fanoutFactor
252+
it.runstats.Size, it.runstats.ExactSize = sz, false
253+
return sz, false
231254
}

0 commit comments

Comments
 (0)