-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindexer.py.c
More file actions
297 lines (252 loc) · 7.51 KB
/
indexer.py.c
File metadata and controls
297 lines (252 loc) · 7.51 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include <string.h>
#include "txt-seg/txt-seg.h"
#include "txt-seg/lex.h"
#include "indices-v3/indices.h"
#include "head.h"
PyObject *index_open(PyObject *self, PyObject *args, PyObject* kwargs)
{
const char *path, *option = NULL, *seg_dict = NULL;
int highlight = true;
static char *kwlist[] = {"path", "option", "segment_dict", "highlight", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|ssp", kwlist,
&path, &option, &seg_dict, &highlight)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTupleAndKeywords error");
return NULL;
}
int failed;
struct indices *indices = malloc(sizeof *indices);
if (NULL == option || NULL != strstr(option, "w")) {
failed = indices_open(indices, path, INDICES_OPEN_RW);
} else {
failed = indices_open(indices, path, INDICES_OPEN_RD);
}
if (failed) {
free(indices);
Py_INCREF(Py_None); // return a new reference of Py_None
return Py_None;
}
int content_field = seg_dict ? FIELD__INDEX_MIX : FIELD__INDEX_ENG;
if (highlight) content_field = content_field | FIELD__INDEX_HIGHLIGHT;
struct indices_field fields[] = {
{"url", FIELD__STORE_COMPRESSED, FIELD__INDEX_NO},
{"content", FIELD__STORE_COMPRESSED, content_field},
{"extern_id", FIELD__STORE_PLAIN, FIELD__INDEX_NO}
};
(void)indices_schema_add_field(indices, fields,
sizeof(fields) / sizeof(fields[0]));
if (seg_dict)
text_segment_init(seg_dict);
return PyLong_FromVoidPtr(indices);
}
PyObject *index_close(PyObject *self, PyObject *args)
{
PyObject *pyindices;
if (!PyArg_ParseTuple(args, "O", &pyindices)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTuple error");
return NULL;
}
struct indices *indices = PyLong_AsVoidPtr(pyindices);
indices_close(indices);
free(indices);
text_segment_free();
Py_RETURN_NONE;
}
PyObject *index_memcache(PyObject *self, PyObject *args, PyObject* kwargs)
{
PyObject *pyindices;
int term_cache = 0, math_cache = 0; /* in MiB */
static char *kwlist[] = {"index", "term_cache", "math_cache", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ii", kwlist,
&pyindices, &term_cache, &math_cache)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTupleAndKeywords error");
return NULL;
}
struct indices *indices = PyLong_AsVoidPtr(pyindices);
indices->ti_cache_limit = term_cache MB;
indices->mi_cache_limit = math_cache MB;
indices_cache(indices);
Py_RETURN_NONE;
}
PyObject *index_print_summary(PyObject *self, PyObject *args)
{
PyObject *pyindices;
if (!PyArg_ParseTuple(args, "O", &pyindices)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTuple error");
return NULL;
}
struct indices *indices = PyLong_AsVoidPtr(pyindices);
indices_print_summary(indices);
Py_RETURN_NONE;
}
PyObject *index_lookup_doc(PyObject *self, PyObject *args)
{
unsigned int key;
PyObject *pyindices;
if (!PyArg_ParseTuple(args, "OI", &pyindices, &key)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTuple error");
return NULL;
}
if (key == 0) {
PyErr_Format(PyExc_RuntimeError,
"key#0 never exists");
return NULL;
}
struct indices *indices = PyLong_AsVoidPtr(pyindices);
int n_field = indices->n_field;
struct indices_field *fields = indices->fields;
PyObject *result = PyDict_New();
for (int i = 0; i < n_field; i++) {
if (!fields[i].store)
continue;
char *field_name = fields[i].name;
bool compress = (fields[i].store == FIELD__STORE_COMPRESSED);
size_t len;
char *s = get_blob_txt(indices->bi[i], key, compress, &len);
if (s) {
// dict setter does NOT steal the reference
PyObject* py_s = PyUnicode_FromString(s);
PyDict_SetItemString(result, field_name, py_s);
Py_DECREF(py_s);
free(s);
} else {
// dict setter does NOT steal the reference
PyObject* py_s = PyUnicode_FromString("");
PyDict_SetItemString(result, field_name, py_s);
Py_DECREF(py_s);
}
}
return result;
}
PyObject *index_lookup_df(PyObject *self, PyObject *args)
{
const char *term = NULL;
PyObject *pyindices;
if (!PyArg_ParseTuple(args, "Os", &pyindices, &term)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTuple error");
return NULL;
}
const char prefix[] = "content:";
size_t prefix_len = strlen(prefix), term_len = strlen(term);
char prefix_term[prefix_len + term_len + 1];
sprintf(prefix_term, "%s%s", prefix, term);
struct indices *indices = PyLong_AsVoidPtr(pyindices);
term_id_t term_id = term_lookup(indices->ti, prefix_term);
if (term_id > 0) {
uint32_t df = term_index_get_df(indices->ti, term_id);
return PyLong_FromUnsignedLong(df);
} else {
Py_RETURN_NONE;
}
}
static int
parser_exception(struct indexer *indexer, const char *tex, char *msg)
{
fprintf(stderr, "Parser error: %s in `%s'\n", msg, tex);
return 0;
}
PyObject *indexer_new(PyObject *self, PyObject *args)
{
PyObject *pyindices;
if (!PyArg_ParseTuple(args, "O", &pyindices)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTuple error");
return NULL;
}
struct indices *indices = PyLong_AsVoidPtr(pyindices);
struct indexer *indexer;
indexer = indexer_alloc(indices, parser_exception);
return PyLong_FromVoidPtr(indexer);
}
PyObject *indexer_del(PyObject *self, PyObject *args)
{
PyObject *pyindexer;
if (!PyArg_ParseTuple(args, "O", &pyindexer)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTuple error");
return NULL;
}
struct indexer *indexer = PyLong_AsVoidPtr(pyindexer);
indexer_free(indexer);
Py_RETURN_NONE;
}
PyObject *do_maintain(PyObject *self, PyObject *args, PyObject* kwargs)
{
PyObject *pyindexer;
int force = 0;
static char *kwlist[] = {"writer", "force", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|p", kwlist,
&pyindexer, &force)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTupleAndKeywords error");
return NULL;
}
struct indexer *indexer = PyLong_AsVoidPtr(pyindexer);
if (indexer_should_maintain(indexer) || force) {
indexer_maintain(indexer);
return PyBool_FromLong(1);
} else {
return PyBool_FromLong(0);
}
}
PyObject *do_flush(PyObject *self, PyObject *args)
{
PyObject *pyindexer;
if (!PyArg_ParseTuple(args, "O", &pyindexer)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTuple error");
return NULL;
}
struct indexer *indexer = PyLong_AsVoidPtr(pyindexer);
indexer_flush(indexer);
Py_RETURN_NONE;
}
PyObject *add_document(PyObject *self, PyObject *args, PyObject* kwargs)
{
PyObject *pyindexer;
const char *content, *url = NULL, *extern_id_str = NULL;
static char *kwlist[] = {"writer", "content", "url", "extern_id", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Os|ss", kwlist,
&pyindexer, &content, &url, &extern_id_str)) {
PyErr_Format(PyExc_RuntimeError,
"PyArg_ParseTupleAndKeywords error");
return NULL;
}
struct indexer *indexer = PyLong_AsVoidPtr(pyindexer);
struct indices *indices = indexer->indices;
uint32_t docID = indices->n_doc + 1;
bool anyfield_written = false;
if (extern_id_str) {
uint32_t extern_id;
if (1 == sscanf(extern_id_str, "%lu", &extern_id)) {
char docIDstr[1024];
snprintf(docIDstr, 1024, "%d", docID);
(void)indexer_write_field(indexer, extern_id, "extern_id", docIDstr);
anyfield_written = true;
}
}
if (content) {
/* for all the other fields */
(void)indexer_write_field(indexer, 0, "content", content);
anyfield_written = true;
} else {
return PyLong_FromUnsignedLong(0);
}
if (url) {
/* for all the other fields */
(void)indexer_write_field(indexer, 0, "url", url);
anyfield_written = true;
}
if (anyfield_written) {
/* maintain and prepare for the next indexing document */
docID = indexer_next_doc(indexer);
} else {
docID = 0;
}
return PyLong_FromUnsignedLong(docID);
}