forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaggregation_query_base.go
157 lines (133 loc) · 3.74 KB
/
aggregation_query_base.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
package ravendb
import (
"reflect"
"time"
)
// Note: AggregationQueryBase also includes AggregationDocumentQuery
type aggregationQueryBase struct {
session *InMemoryDocumentSessionOperations
query *IndexQuery
startTime time.Time
// from AggregationDocumentQuery
source *abstractDocumentQuery
err error
}
func newAggregationQueryBase(source *DocumentQuery) *aggregationQueryBase {
res := &aggregationQueryBase{
session: source.getSession(),
source: source.abstractDocumentQuery,
}
res.err = source.err
return res
}
func newAggregationDocumentQuery(source *DocumentQuery) *AggregationDocumentQuery {
return newAggregationQueryBase(source)
}
func (q *aggregationQueryBase) Execute() (map[string]*FacetResult, error) {
if q.err != nil {
return nil, q.err
}
command, err := q.GetCommand()
if err != nil {
return nil, err
}
q.startTime = time.Now()
if err = q.session.incrementRequestCount(); err != nil {
return nil, err
}
if err = q.session.GetRequestExecutor().ExecuteCommand(command, nil); err != nil {
return nil, err
}
return q.processResults(command.Result, q.session.GetConventions())
}
// arg to onEval is map[string]*FacetResult
// results is map[string]*FacetResult
func (q *aggregationQueryBase) ExecuteLazy() (*Lazy, error) {
if q.err != nil {
return nil, q.err
}
var err error
q.query, err = q.GetIndexQuery()
if err != nil {
return nil, err
}
afterFn := func(result *QueryResult) {
q.invokeAfterQueryExecuted(result)
}
processResultFn := func(queryResult *QueryResult, conventions *DocumentConventions) (map[string]*FacetResult, error) {
results := map[string]*FacetResult{}
res, err := q.processResults(queryResult, conventions)
if err != nil {
return nil, err
}
// processResult returns its own map, have to copy it to map provided by
// caller as a result
for k, v := range res {
results[k] = v
}
return res, nil
}
op := newLazyAggregationQueryOperation(q.session.Conventions, q.query, afterFn, processResultFn)
return q.session.session.addLazyOperation(op, nil, nil), nil
}
/*
// abstract
func (q *AggregationQueryBase) getIndexQuery() *IndexQuery {
return nil
}
// abstract
func (q *AggregationQueryBase) invokeAfterQueryExecuted(result *QueryResult) {
}
*/
func (q *aggregationQueryBase) processResults(queryResult *QueryResult, conventions *DocumentConventions) (map[string]*FacetResult, error) {
q.invokeAfterQueryExecuted(queryResult)
results := map[string]*FacetResult{}
for _, result := range queryResult.Results {
res, err := convertValue(result, reflect.TypeOf(&FacetResult{}))
if err != nil {
return nil, err
}
facetResult := res.(*FacetResult)
results[facetResult.Name] = facetResult
}
err := queryOperationEnsureIsAcceptable(queryResult, q.query.waitForNonStaleResults, q.startTime, q.session)
if err != nil {
return nil, err
}
return results, nil
}
func (q *aggregationQueryBase) GetCommand() (*QueryCommand, error) {
if q.err != nil {
return nil, q.err
}
var err error
q.query, err = q.GetIndexQuery()
if err != nil {
return nil, err
}
return NewQueryCommand(q.session.GetConventions(), q.query, false, false)
}
func (q *aggregationQueryBase) string() (string, error) {
if q.err != nil {
return "", q.err
}
iq, err := q.GetIndexQuery()
if err != nil {
return "", err
}
return iq.String(), nil
}
// from AggregationDocumentQuery
func (q *AggregationDocumentQuery) AndAggregateByFacet(facet FacetBase) *AggregationDocumentQuery {
if q.err != nil {
return q
}
q.err = q.source.aggregateBy(facet)
return q
}
func (q *AggregationDocumentQuery) GetIndexQuery() (*IndexQuery, error) {
return q.source.GetIndexQuery()
}
func (q *AggregationDocumentQuery) invokeAfterQueryExecuted(result *QueryResult) {
q.source.invokeAfterQueryExecuted(result)
}