forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.go
135 lines (113 loc) · 2.68 KB
/
count.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package iterator
import (
"context"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/quad"
)
var _ graph.Iterator = &Count{}
// Count iterator returns one element with size of underlying iterator.
type Count struct {
uid uint64
it graph.Iterator
done bool
tags graph.Tagger
result quad.Value
qs graph.QuadStore
}
// NewCount creates a new iterator to count a number of results from a provided subiterator.
// qs may be nil - it's used to check if count Contains (is) a given value.
func NewCount(it graph.Iterator, qs graph.QuadStore) *Count {
return &Count{
uid: NextUID(),
it: it, qs: qs,
}
}
func (it *Count) UID() uint64 {
return it.uid
}
// Reset resets the internal iterators and the iterator itself.
func (it *Count) Reset() {
it.done = false
it.result = nil
it.it.Reset()
}
func (it *Count) Tagger() *graph.Tagger {
return &it.tags
}
func (it *Count) TagResults(dst map[string]graph.Value) {
it.tags.TagResult(dst, it.Result())
}
func (it *Count) Clone() graph.Iterator {
it2 := NewCount(it.it.Clone(), it.qs)
it2.Tagger().CopyFrom(it)
return it2
}
// SubIterators returns a slice of the sub iterators.
func (it *Count) SubIterators() []graph.Iterator {
return []graph.Iterator{it.it}
}
// Next counts a number of results in underlying iterator.
func (it *Count) Next(ctx context.Context) bool {
if it.done {
return false
}
size, exact := it.it.Size()
if !exact {
for size = 0; it.it.Next(ctx); size++ {
for ; it.it.NextPath(ctx); size++ {
}
}
}
it.result = quad.Int(size)
it.done = true
return true
}
func (it *Count) Err() error {
return it.it.Err()
}
func (it *Count) Result() graph.Value {
if it.result == nil {
return nil
}
return graph.PreFetched(it.result)
}
func (it *Count) Contains(ctx context.Context, val graph.Value) bool {
if !it.done {
it.Next(ctx)
}
if v, ok := val.(graph.PreFetchedValue); ok {
return v.NameOf() == it.result
}
if it.qs != nil {
return it.qs.NameOf(val) == it.result
}
return false
}
func (it *Count) NextPath(ctx context.Context) bool {
return false
}
func (it *Count) Close() error {
return it.it.Close()
}
func (it *Count) Type() graph.Type { return graph.Count }
func (it *Count) Optimize() (graph.Iterator, bool) {
sub, optimized := it.it.Optimize()
it.it = sub
return it, optimized
}
func (it *Count) Stats() graph.IteratorStats {
stats := graph.IteratorStats{
NextCost: 1,
Size: 1,
ExactSize: true,
}
if sub := it.it.Stats(); !sub.ExactSize {
stats.NextCost = sub.NextCost * sub.Size
}
stats.ContainsCost = stats.NextCost
return stats
}
func (it *Count) Size() (int64, bool) {
return 1, true
}
func (it *Count) String() string { return "Count" }