|
| 1 | +#ifndef SRC_REDISEARCH_API_H_ |
| 2 | +#define SRC_REDISEARCH_API_H_ |
| 3 | + |
| 4 | +#include "redismodule.h" |
| 5 | +#include <limits.h> |
| 6 | + |
| 7 | +#ifdef __cplusplus |
| 8 | +extern "C" { |
| 9 | +#endif |
| 10 | + |
| 11 | +#define REDISEARCH_CAPI_VERSION 1 |
| 12 | + |
| 13 | +#ifdef REDISEARCH_API_EXTERN |
| 14 | +#define MODULE_API_FUNC(T, N) extern T(*N) |
| 15 | +#else |
| 16 | +#define MODULE_API_FUNC(T, N) T N |
| 17 | +#endif |
| 18 | + |
| 19 | +typedef struct IndexSpec RSIndex; |
| 20 | +typedef size_t RSFieldID; |
| 21 | +#define RSFIELD_INVALID SIZE_MAX |
| 22 | + |
| 23 | +typedef struct Document RSDoc; |
| 24 | +typedef struct RSQueryNode RSQNode; |
| 25 | +typedef struct RS_ApiIter RSResultsIterator; |
| 26 | +typedef struct RSIdxOptions RSIndexOptions; |
| 27 | + |
| 28 | +#define RSVALTYPE_NOTFOUND 0 |
| 29 | +#define RSVALTYPE_STRING 1 |
| 30 | +#define RSVALTYPE_DOUBLE 2 |
| 31 | + |
| 32 | +#define RSRANGE_INF (1.0 / 0.0) |
| 33 | +#define RSRANGE_NEG_INF (-1.0 / 0.0) |
| 34 | + |
| 35 | +#define RSLECRANGE_INF NULL |
| 36 | +#define RSLEXRANGE_NEG_INF NULL |
| 37 | + |
| 38 | +#define RSQNTYPE_INTERSECT 1 |
| 39 | +#define RSQNTYPE_UNION 2 |
| 40 | +#define RSQNTYPE_TOKEN 3 |
| 41 | +#define RSQNTYPE_NUMERIC 4 |
| 42 | +#define RSQNTYPE_NOT 5 |
| 43 | +#define RSQNTYPE_OPTIONAL 6 |
| 44 | +#define RSQNTYPE_GEO 7 |
| 45 | +#define RSQNTYPE_PREFX 8 |
| 46 | +#define RSQNTYPE_TAG 11 |
| 47 | +#define RSQNTYPE_FUZZY 12 |
| 48 | +#define RSQNTYPE_LEXRANGE 13 |
| 49 | + |
| 50 | +#define RSFLDTYPE_DEFAULT 0x00 |
| 51 | +#define RSFLDTYPE_FULLTEXT 0x01 |
| 52 | +#define RSFLDTYPE_NUMERIC 0x02 |
| 53 | +#define RSFLDTYPE_GEO 0x04 |
| 54 | +#define RSFLDTYPE_TAG 0x08 |
| 55 | + |
| 56 | +#define RSFLDOPT_NONE 0x00 |
| 57 | +#define RSFLDOPT_SORTABLE 0x01 |
| 58 | +#define RSFLDOPT_NOINDEX 0x02 |
| 59 | +#define RSFLDOPT_TXTNOSTEM 0x04 |
| 60 | +#define RSFLDOPT_TXTPHONETIC 0x08 |
| 61 | + |
| 62 | +typedef int (*RSGetValueCallback)(void* ctx, const char* fieldName, const void* id, char** strVal, |
| 63 | + double* doubleVal); |
| 64 | + |
| 65 | +MODULE_API_FUNC(int, RediSearch_GetCApiVersion)(); |
| 66 | + |
| 67 | +#define RSIDXOPT_DOCTBLSIZE_UNLIMITED 0x01 |
| 68 | + |
| 69 | +#define GC_POLICY_NONE -1 |
| 70 | +#define GC_POLICY_FORK 0 |
| 71 | + |
| 72 | +struct RSIdxOptions { |
| 73 | + RSGetValueCallback gvcb; |
| 74 | + void* gvcbData; |
| 75 | + uint32_t flags; |
| 76 | + int gcPolicy; |
| 77 | +}; |
| 78 | + |
| 79 | +/** |
| 80 | + * Allocate an index options struct. This structure can be used to set global |
| 81 | + * options on the index prior to it being created. |
| 82 | + */ |
| 83 | +MODULE_API_FUNC(RSIndexOptions*, RediSearch_CreateIndexOptions)(void); |
| 84 | + |
| 85 | +/** |
| 86 | + * Frees the index options previously allocated. The options are _not_ freed |
| 87 | + * in a call to CreateIndex() |
| 88 | + */ |
| 89 | +MODULE_API_FUNC(void, RediSearch_FreeIndexOptions)(RSIndexOptions*); |
| 90 | +MODULE_API_FUNC(void, RediSearch_IndexOptionsSetGetValueCallback) |
| 91 | +(RSIndexOptions* opts, RSGetValueCallback cb, void* ctx); |
| 92 | + |
| 93 | +/** Set flags modifying index creation. */ |
| 94 | +MODULE_API_FUNC(void, RediSearch_IndexOptionsSetFlags)(RSIndexOptions* opts, uint32_t flags); |
| 95 | + |
| 96 | +MODULE_API_FUNC(RSIndex*, RediSearch_CreateIndex) |
| 97 | +(const char* name, const RSIndexOptions* options); |
| 98 | + |
| 99 | +MODULE_API_FUNC(void, RediSearch_DropIndex)(RSIndex*); |
| 100 | + |
| 101 | +/** |
| 102 | + * Create a new field in the index |
| 103 | + * @param idx the index |
| 104 | + * @param name the name of the field |
| 105 | + * @param ftype a mask of RSFieldType that should be supported for indexing. |
| 106 | + * This also indicates the default indexing settings if not otherwise specified |
| 107 | + * @param fopt a mask of RSFieldOptions |
| 108 | + */ |
| 109 | +MODULE_API_FUNC(RSFieldID, RediSearch_CreateField) |
| 110 | +(RSIndex* idx, const char* name, unsigned ftype, unsigned fopt); |
| 111 | + |
| 112 | +#define RediSearch_CreateNumericField(idx, name) \ |
| 113 | + RediSearch_CreateField(idx, name, RSFLDTYPE_NUMERIC, RSFLDOPT_NONE) |
| 114 | +#define RediSearch_CreateTextField(idx, name) \ |
| 115 | + RediSearch_CreateField(idx, name, RSFLDTYPE_FULLTEXT, RSFLDOPT_NONE) |
| 116 | +#define RediSearch_CreateTagField(idx, name) \ |
| 117 | + RediSearch_CreateField(idx, name, RSFLDTYPE_TAG, RSFLDOPT_NONE) |
| 118 | +#define RediSearch_CreateGeoField(idx, name) \ |
| 119 | + RediSearch_CreateField(idx, name, RSFLDTYPE_GEO, RSFLDOPT_NONE) |
| 120 | + |
| 121 | +MODULE_API_FUNC(void, RediSearch_TextFieldSetWeight)(RSIndex* sp, RSFieldID fs, double w); |
| 122 | +MODULE_API_FUNC(void, RediSearch_TagFieldSetSeparator)(RSIndex* sp, RSFieldID fs, char sep); |
| 123 | +MODULE_API_FUNC(void, RediSearch_TagFieldSetCaseSensitive)(RSIndex* sp, RSFieldID fs, int enable); |
| 124 | + |
| 125 | +MODULE_API_FUNC(RSDoc*, RediSearch_CreateDocument) |
| 126 | +(const void* docKey, size_t len, double score, const char* lang); |
| 127 | +MODULE_API_FUNC(void, RediSearch_FreeDocument)(RSDoc* doc); |
| 128 | +#define RediSearch_CreateDocumentSimple(s) RediSearch_CreateDocument(s, strlen(s), 1.0, NULL) |
| 129 | + |
| 130 | +MODULE_API_FUNC(int, RediSearch_DeleteDocument)(RSIndex* sp, const void* docKey, size_t len); |
| 131 | +#define RediSearch_DropDocument RediSearch_DeleteDocument |
| 132 | +/** |
| 133 | + * Add a field (with value) to the document |
| 134 | + * @param d the document |
| 135 | + * @param fieldName the name of the field |
| 136 | + * @param s the contents of the field to be added (if numeric, the string representation) |
| 137 | + * @param indexAsTypes the types the field should be indexed as. Should be a |
| 138 | + * bitmask of RSFieldType. |
| 139 | + */ |
| 140 | +MODULE_API_FUNC(void, RediSearch_DocumentAddField) |
| 141 | +(RSDoc* d, const char* fieldName, RedisModuleString* s, RedisModuleCtx* ctx, unsigned indexAsTypes); |
| 142 | + |
| 143 | +MODULE_API_FUNC(void, RediSearch_DocumentAddFieldString) |
| 144 | +(RSDoc* d, const char* fieldName, const char* s, size_t n, unsigned indexAsTypes); |
| 145 | +#define RediSearch_DocumentAddFieldCString(doc, fieldname, s, indexAs) \ |
| 146 | + RediSearch_DocumentAddFieldString(doc, fieldname, s, strlen(s), indexAs) |
| 147 | +MODULE_API_FUNC(void, RediSearch_DocumentAddFieldNumber) |
| 148 | +(RSDoc* d, const char* fieldName, double n, unsigned indexAsTypes); |
| 149 | + |
| 150 | +/** |
| 151 | + * Replace document if it already exists |
| 152 | + */ |
| 153 | +#define REDISEARCH_ADD_REPLACE 0x01 |
| 154 | + |
| 155 | +MODULE_API_FUNC(int, RediSearch_IndexAddDocument)(RSIndex* sp, RSDoc* d, int flags, char**); |
| 156 | +#define RediSearch_SpecAddDocument(sp, d) \ |
| 157 | + RediSearch_IndexAddDocument(sp, d, REDISEARCH_ADD_REPLACE, NULL) |
| 158 | + |
| 159 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreateTokenNode) |
| 160 | +(RSIndex* sp, const char* fieldName, const char* token); |
| 161 | + |
| 162 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreateNumericNode) |
| 163 | +(RSIndex* sp, const char* field, double max, double min, int includeMax, int includeMin); |
| 164 | + |
| 165 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreatePrefixNode) |
| 166 | +(RSIndex* sp, const char* fieldName, const char* s); |
| 167 | + |
| 168 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreateLexRangeNode) |
| 169 | +(RSIndex* sp, const char* fieldName, const char* begin, const char* end, int includeBegin, |
| 170 | + int includeEnd); |
| 171 | + |
| 172 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreateTagNode)(RSIndex* sp, const char* field); |
| 173 | + |
| 174 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreateIntersectNode)(RSIndex* sp, int exact); |
| 175 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreateUnionNode)(RSIndex* sp); |
| 176 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreateEmptyNode)(RSIndex* sp); |
| 177 | +MODULE_API_FUNC(RSQNode*, RediSearch_CreateNotNode)(RSIndex* sp); |
| 178 | +MODULE_API_FUNC(void, RediSearch_QueryNodeFree)(RSQNode* qn); |
| 179 | + |
| 180 | +MODULE_API_FUNC(void, RediSearch_QueryNodeAddChild)(RSQNode*, RSQNode*); |
| 181 | +MODULE_API_FUNC(void, RediSearch_QueryNodeClearChildren)(RSQNode*); |
| 182 | +MODULE_API_FUNC(RSQNode*, RediSearch_QueryNodeGetChild)(const RSQNode*, size_t); |
| 183 | +MODULE_API_FUNC(size_t, RediSearch_QueryNodeNumChildren)(const RSQNode*); |
| 184 | + |
| 185 | +MODULE_API_FUNC(int, RediSearch_QueryNodeType)(RSQNode* qn); |
| 186 | +MODULE_API_FUNC(int, RediSearch_QueryNodeGetFieldMask)(RSQNode* qn); |
| 187 | + |
| 188 | +MODULE_API_FUNC(RSResultsIterator*, RediSearch_GetResultsIterator)(RSQNode* qn, RSIndex* sp); |
| 189 | + |
| 190 | +MODULE_API_FUNC(void, RediSearch_SetCriteriaTesterThreshold)(size_t num); |
| 191 | + |
| 192 | +/** |
| 193 | + * Return an iterator over the results of the specified query string |
| 194 | + * @param sp the index |
| 195 | + * @param s the query string |
| 196 | + * @param n the length of the steing |
| 197 | + * @param[out] if not-NULL, will be set to the error message, if there is a |
| 198 | + * problem parsing the query |
| 199 | + * @return an iterator over the results, or NULL if no iterator can be had |
| 200 | + * (see err, or no results). |
| 201 | + */ |
| 202 | +MODULE_API_FUNC(RSResultsIterator*, RediSearch_IterateQuery) |
| 203 | +(RSIndex* sp, const char* s, size_t n, char** err); |
| 204 | + |
| 205 | +MODULE_API_FUNC(int, RediSearch_DocumentExists) |
| 206 | +(RSIndex* sp, const void* docKey, size_t len); |
| 207 | + |
| 208 | +MODULE_API_FUNC(const void*, RediSearch_ResultsIteratorNext) |
| 209 | +(RSResultsIterator* iter, RSIndex* sp, size_t* len); |
| 210 | + |
| 211 | +MODULE_API_FUNC(void, RediSearch_ResultsIteratorFree)(RSResultsIterator* iter); |
| 212 | + |
| 213 | +MODULE_API_FUNC(void, RediSearch_ResultsIteratorReset)(RSResultsIterator* iter); |
| 214 | + |
| 215 | +MODULE_API_FUNC(double, RediSearch_ResultsIteratorGetScore)(const RSResultsIterator* it); |
| 216 | + |
| 217 | +MODULE_API_FUNC(void, RediSearch_IndexOptionsSetGCPolicy)(RSIndexOptions* options, int policy); |
| 218 | + |
| 219 | +#define RS_XAPIFUNC(X) \ |
| 220 | + X(GetCApiVersion) \ |
| 221 | + X(CreateIndexOptions) \ |
| 222 | + X(IndexOptionsSetGetValueCallback) \ |
| 223 | + X(IndexOptionsSetFlags) \ |
| 224 | + X(FreeIndexOptions) \ |
| 225 | + X(CreateIndex) \ |
| 226 | + X(DropIndex) \ |
| 227 | + X(CreateField) \ |
| 228 | + X(TextFieldSetWeight) \ |
| 229 | + X(TagSetSeparator) \ |
| 230 | + X(CreateDocument) \ |
| 231 | + X(DeleteDocument) \ |
| 232 | + X(DocumentAddField) \ |
| 233 | + X(DocumentAddFieldNumber) \ |
| 234 | + X(DocumentAddFieldString) \ |
| 235 | + X(IndexAddDocument) \ |
| 236 | + X(CreateTokenNode) \ |
| 237 | + X(CreateNumericNode) \ |
| 238 | + X(CreatePrefixNode) \ |
| 239 | + X(CreateLexRangeNode) \ |
| 240 | + X(CreateTagNode) \ |
| 241 | + X(CreateIntersectNode) \ |
| 242 | + X(CreateUnionNode) \ |
| 243 | + X(CreateNotNode) \ |
| 244 | + X(QueryNodeAddChild) \ |
| 245 | + X(QueryNodeClearChildren) \ |
| 246 | + X(QueryNodeGetChild) \ |
| 247 | + X(QueryNodeNumChildren) \ |
| 248 | + X(QueryNodeFree) \ |
| 249 | + X(QueryNodeType) \ |
| 250 | + X(QueryNodeGetFieldMask) \ |
| 251 | + X(GetResultsIterator) \ |
| 252 | + X(ResultsIteratorNext) \ |
| 253 | + X(ResultsIteratorFree) \ |
| 254 | + X(ResultsIteratorReset) \ |
| 255 | + X(IterateQuery) \ |
| 256 | + X(ResultsIteratorGetScore) \ |
| 257 | + X(IndexOptionsSetGCPolicy) \ |
| 258 | + X(SetCriteriaTesterThreshold) |
| 259 | + |
| 260 | +#define REDISEARCH_MODULE_INIT_FUNCTION(name) \ |
| 261 | + if (RedisModule_GetApi("RediSearch_" #name, ((void**)&RediSearch_##name))) { \ |
| 262 | + printf("could not initialize RediSearch_" #name "\r\n"); \ |
| 263 | + rv__ = REDISMODULE_ERR; \ |
| 264 | + goto rsfunc_init_end__; \ |
| 265 | + } |
| 266 | + |
| 267 | +#ifdef REDISEARCH_API_EXTERN |
| 268 | +/** |
| 269 | + * This is implemented as a macro rather than a function so that the inclusion of this |
| 270 | + * header file does not automatically require the symbols to be defined above. |
| 271 | + * |
| 272 | + * We are making use of special GCC statement-expressions `({...})`. This is also |
| 273 | + * supported by clang. |
| 274 | + * |
| 275 | + * This function should not be used if RediSearch is compiled as a |
| 276 | + * static library. In this case, the functions are actually properly |
| 277 | + * linked. |
| 278 | + */ |
| 279 | +#define RediSearch_Initialize() \ |
| 280 | + ({ \ |
| 281 | + int rv__ = REDISMODULE_OK; \ |
| 282 | + RS_XAPIFUNC(REDISEARCH_MODULE_INIT_FUNCTION); \ |
| 283 | + if (RediSearch_GetCApiVersion() > REDISEARCH_CAPI_VERSION) { \ |
| 284 | + rv__ = REDISMODULE_ERR; \ |
| 285 | + } \ |
| 286 | + rsfunc_init_end__:; \ |
| 287 | + rv__; \ |
| 288 | + }) |
| 289 | + |
| 290 | +#define REDISEARCH__API_INIT_NULL(s) __typeof__(RediSearch_##s) RediSearch_##s = NULL; |
| 291 | +#define REDISEARCH_API_INIT_SYMBOLS() RS_XAPIFUNC(REDISEARCH__API_INIT_NULL) |
| 292 | +#else |
| 293 | +#define REDISEARCH_API_INIT_SYMBOLS() |
| 294 | +#define RediSearch_Initialize() |
| 295 | +#endif |
| 296 | + |
| 297 | +/** |
| 298 | + * Export the C API to be dynamically discoverable by other modules. |
| 299 | + * This is an internal function |
| 300 | + */ |
| 301 | +int RediSearch_ExportCapi(RedisModuleCtx* ctx); |
| 302 | + |
| 303 | +#define REDISEARCH_INIT_MODULE 0x01 |
| 304 | +#define REDISEARCH_INIT_LIBRARY 0x02 |
| 305 | +int RediSearch_Init(RedisModuleCtx* ctx, int mode); |
| 306 | +#ifdef __cplusplus |
| 307 | +} |
| 308 | +#endif |
| 309 | +#endif /* SRC_REDISEARCH_API_H_ */ |
0 commit comments