forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.go
159 lines (135 loc) · 3.09 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package iterator
import (
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/quad"
)
var (
_ graph.Value = fetchedValue{}
_ graph.PreFetchedValue = fetchedValue{}
)
type fetchedValue struct {
Val quad.Value
}
func (v fetchedValue) IsNode() bool { return true }
func (v fetchedValue) NameOf() quad.Value { return v.Val }
// 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) {
for _, tag := range it.tags.Tags() {
dst[tag] = it.Result()
}
for tag, value := range it.tags.Fixed() {
dst[tag] = value
}
}
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() bool {
if it.done {
return false
}
size, exact := it.it.Size()
if !exact {
size = 0
for ; it.it.Next(); 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 fetchedValue{Val: it.result}
}
func (it *Count) Contains(val graph.Value) bool {
if !it.done {
it.Next()
}
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() 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) Describe() graph.Description {
subIts := []graph.Description{
it.it.Describe(),
}
return graph.Description{
UID: it.UID(),
Type: it.Type(),
Tags: it.Tagger().Tags(),
Iterators: subIts,
}
}
var _ graph.Iterator = &Count{}