-
Notifications
You must be signed in to change notification settings - Fork 509
/
result_processor.h
268 lines (219 loc) · 8.75 KB
/
result_processor.h
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright Redis Ltd. 2016 - present
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
* the Server Side Public License v1 (SSPLv1).
*/
#pragma once
#include "redisearch.h"
#include "sortable.h"
#include "value.h"
#include "concurrent_ctx.h"
#include "search_ctx.h"
#include "index_iterator.h"
#include "search_options.h"
#include "rlookup.h"
#include "extension.h"
#include "score_explain.h"
#ifdef __cplusplus
extern "C" {
#endif
/********************************************************************************
* Result Processor Chain
*
* We use a chain of result processors to sort, score, filter and page the results coming from the
* index.
*
* The index iterator tree is responsible for extracting results from the index, and the processor
* chain is responsible for processing those and preparing them for the users.
* The processors are exposing an iterator interface, adding values to SearchResult objects.
*
* SearchResult objects contain all the data needed for a search result - from docId and score, to
* the actual fields loaded from redis.
*
* Processors can add more fields, rewrite them, change the score, etc.
* The query plan builds the chain based on the request, and then the chain just processes the
* results.
*
********************************************************************************/
typedef enum {
RP_INDEX,
RP_LOADER,
RP_SAFE_LOADER,
RP_SCORER,
RP_SORTER,
RP_COUNTER,
RP_PAGER_LIMITER,
RP_HIGHLIGHTER,
RP_GROUP,
RP_PROJECTOR,
RP_FILTER,
RP_PROFILE,
RP_NETWORK,
RP_METRICS,
RP_MAX,
} ResultProcessorType;
struct ResultProcessor;
struct RLookup;
typedef struct {
// First processor
struct ResultProcessor *rootProc;
// Last processor
struct ResultProcessor *endProc;
// Concurrent search context for thread switching
ConcurrentSearchCtx *conc;
// Contains our spec
RedisSearchCtx *sctx;
// the minimal score applicable for a result. It can be used to optimize the scorers
double minScore;
// the total results found in the query, incremented by the root processors and decremented by
// others who might disqualify results
uint32_t totalResults;
// the number of results we requested to return at the current chunk. This value may be used by
// processors to optimize their work and to signal RP in the upstream their limit.
uint32_t resultLimit;
// Object which contains the error
QueryError *err;
RSTimeoutPolicy timeoutPolicy;
} QueryIterator, QueryProcessingCtx;
IndexIterator *QITR_GetRootFilter(QueryIterator *it);
void QITR_PushRP(QueryIterator *it, struct ResultProcessor *rp);
void QITR_FreeChain(QueryIterator *qitr);
/*
* SearchResult - the object all the processing chain is working on.
* It has the indexResult which is what the index scan brought - scores, vectors, flags, etc,
* and a list of fields loaded by the chain
*/
typedef struct {
t_docId docId;
// not all results have score - TBD
double score;
RSScoreExplain *scoreExplain;
const RSDocumentMetadata *dmd;
// index result should cover what you need for highlighting,
// but we will add a method to duplicate index results to make
// them thread safe
RSIndexResult *indexResult;
// Row data. Use RLookup_* functions to access
RLookupRow rowdata;
} SearchResult;
/* Result processor return codes */
/** Possible return values from Next() */
typedef enum {
// Result is filled with valid data
RS_RESULT_OK = 0,
// Result is empty, and the last result has already been returned.
RS_RESULT_EOF,
// Execution paused due to rate limiting (or manual pause from ext. thread??)
RS_RESULT_PAUSED,
// Execution halted because of timeout
RS_RESULT_TIMEDOUT,
// Aborted because of error. The QueryState (parent->status) should have
// more information.
RS_RESULT_ERROR,
// Not a return code per se, but a marker signifying the end of the 'public'
// return codes. Implementations can use this for extensions.
RS_RESULT_MAX
} RPStatus;
/**
* Result processor structure. This should be "Subclassed" by the actual
* implementations
*/
typedef struct ResultProcessor {
// Reference to the parent structure
QueryIterator *parent;
// Previous result processor in the chain
struct ResultProcessor *upstream;
// Type of result processor
ResultProcessorType type;
/**
* Populates the result pointed to by `res`. The existing data of `res` is
* not read, so it is the responsibility of the caller to ensure that there
* are no refcount leaks in the structure.
*
* Users can use SearchResult_Clear() to reset the structure without freeing
* it.
*
* The populated structure (if RS_RESULT_OK is returned) does contain references
* to document data. Callers *MUST* ensure they are eventually freed.
*/
int (*Next)(struct ResultProcessor *self, SearchResult *res);
/** Frees the processor and any internal data related to it. */
void (*Free)(struct ResultProcessor *self);
} ResultProcessor;
/**
* This function allocates a new SearchResult, copies the data from `src` to it,
* and returns it.
*/
SearchResult *SearchResult_Copy(SearchResult *r);
/**
* This function resets the search result, so that it may be reused again.
* Internal caches are reset but not freed
*/
void SearchResult_Clear(SearchResult *r);
/**
* This function clears the search result, also freeing its internals. Internal
* caches are freed. Use this function if `r` will not be used again.
*/
void SearchResult_Destroy(SearchResult *r);
ResultProcessor *RPIndexIterator_New(IndexIterator *itr);
ResultProcessor *RPScorer_New(const ExtScoringFunctionCtx *funcs,
const ScoringFunctionArgs *fnargs);
ResultProcessor *RPMetricsLoader_New();
/** Functions abstracting the sortmap. Hides the bitwise logic */
#define SORTASCMAP_INIT 0xFFFFFFFFFFFFFFFF
#define SORTASCMAP_MAXFIELDS 8
#define SORTASCMAP_SETASC(mm, pos) ((mm) |= (1LLU << (pos)))
#define SORTASCMAP_SETDESC(mm, pos) ((mm) &= ~(1LLU << (pos)))
#define SORTASCMAP_GETASC(mm, pos) ((mm) & (1LLU << (pos)))
void SortAscMap_Dump(uint64_t v, size_t n);
/**
* Creates a sorter result processor.
* @param keys is an array of RLookupkeys to sort by them,
* @param nkeys is the number of keys.
* keys will be freed by the arrange step dtor.
*/
ResultProcessor *RPSorter_NewByFields(size_t maxresults, const RLookupKey **keys, size_t nkeys, uint64_t ascendingMap);
ResultProcessor *RPSorter_NewByScore(size_t maxresults);
ResultProcessor *RPPager_New(size_t offset, size_t limit);
/*******************************************************************************************************************
* Loading Processor
*
* This processor simply takes the search results, and based on the request parameters, loads the
* relevant fields for the results that need to be displayed to the user, from redis.
*
* It fills the result objects' field map with values corresponding to the requested return fields
*
* On thread safe mode, the loader will buffer results, in an internal phase will lock redis and load the requested
* fields and then unlock redis, and then will yield the results to the next processor in the chain.
* On non thread safe mode (running the query from the main thread), the loader will load the requested fields
* for each result, one result at a time, and yield it to the next processor in the chain.
*
*******************************************************************************************************************/
struct AREQ;
ResultProcessor *RPLoader_New(struct AREQ *r, RLookup *lk, const RLookupKey **keys, size_t nkeys);
/** Creates a new Highlight processor */
ResultProcessor *RPHighlighter_New(const RSSearchOptions *searchopts, const FieldList *fields,
const RLookup *lookup);
void RP_DumpChain(const ResultProcessor *rp);
/*******************************************************************************************************************
* Profiling Processor
*
* This processor collects time and count info about the performance of its upstream RP.
*
*******************************************************************************************************************/
ResultProcessor *RPProfile_New(ResultProcessor *rp, QueryIterator *qiter);
/*******************************************************************************************************************
* Counter Processor
*
* This processor counts the search results.
*
*******************************************************************************************************************/
ResultProcessor *RPCounter_New();
clock_t RPProfile_GetClock(ResultProcessor *rp);
uint64_t RPProfile_GetCount(ResultProcessor *rp);
void Profile_AddRPs(QueryIterator *qiter);
// Return string for RPType
const char *RPTypeToString(ResultProcessorType type);
#ifdef __cplusplus
}
#endif