github.com/altipla-consulting/ravendb-go-client@v0.1.3/lazy_aggregation_query_operation.go (about)

     1  package ravendb
     2  
     3  var _ ILazyOperation = &LazyAggregationQueryOperation{}
     4  
     5  // LazyAggregationQueryOperation represents lazy aggregation query operation
     6  type LazyAggregationQueryOperation struct {
     7  	conventions              *DocumentConventions
     8  	indexQuery               *IndexQuery
     9  	invokeAfterQueryExecuted func(*QueryResult)
    10  	processResults           func(*QueryResult, *DocumentConventions) (map[string]*FacetResult, error)
    11  
    12  	result        map[string]*FacetResult
    13  	queryResult   *QueryResult
    14  	requiresRetry bool
    15  }
    16  
    17  func newLazyAggregationQueryOperation(conventions *DocumentConventions, indexQuery *IndexQuery, invokeAfterQueryExecuted func(*QueryResult),
    18  	processResults func(*QueryResult, *DocumentConventions) (map[string]*FacetResult, error)) *LazyAggregationQueryOperation {
    19  	return &LazyAggregationQueryOperation{
    20  		conventions:              conventions,
    21  		indexQuery:               indexQuery,
    22  		invokeAfterQueryExecuted: invokeAfterQueryExecuted,
    23  		processResults:           processResults,
    24  	}
    25  }
    26  
    27  // needed for ILazyOperation
    28  func (o *LazyAggregationQueryOperation) createRequest() *getRequest {
    29  	request := &getRequest{
    30  		url:     "/queries",
    31  		method:  "POST",
    32  		query:   "?queryHash=" + o.indexQuery.GetQueryHash(),
    33  		content: NewIndexQueryContent(o.conventions, o.indexQuery),
    34  	}
    35  	return request
    36  }
    37  
    38  // needed for ILazyOperation
    39  func (o *LazyAggregationQueryOperation) getResult(results interface{}) error {
    40  	return setInterfaceToValue(results, o.result)
    41  }
    42  
    43  // needed for ILazyOperation
    44  func (o *LazyAggregationQueryOperation) getQueryResult() *QueryResult {
    45  	return o.queryResult
    46  }
    47  
    48  // needed for ILazyOperation
    49  func (o *LazyAggregationQueryOperation) isRequiresRetry() bool {
    50  	return o.requiresRetry
    51  }
    52  
    53  // needed for ILazyOperation
    54  func (o *LazyAggregationQueryOperation) handleResponse(response *GetResponse) error {
    55  	if response.IsForceRetry {
    56  		o.result = nil
    57  		o.requiresRetry = true
    58  		return nil
    59  	}
    60  
    61  	var queryResult *QueryResult
    62  	err := jsonUnmarshal(response.Result, &queryResult)
    63  	if err != nil {
    64  		return err
    65  	}
    66  	return o.handleResponse2(queryResult)
    67  }
    68  
    69  func (o *LazyAggregationQueryOperation) handleResponse2(queryResult *QueryResult) error {
    70  	var err error
    71  	o.invokeAfterQueryExecuted(queryResult)
    72  	o.result, err = o.processResults(queryResult, o.conventions)
    73  	o.queryResult = queryResult
    74  	return err
    75  }