-
Notifications
You must be signed in to change notification settings - Fork 9
/
build_wrapper.py
421 lines (341 loc) · 10.6 KB
/
build_wrapper.py
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import platform
import cffi
aduana_src_root = 'lib/src/'
aduana_lib_root = 'lib/lib/'
aduana_src = [
aduana_lib_root + x for x in [
'smaz.c',
'xxhash.c',
'lmdb/mdb.c',
'lmdb/midl.c',
]] + [
aduana_src_root + x for x in [
'mmap_array.c',
'page_db.c',
'hits.c',
'page_rank.c',
'scheduler.c',
'bf_scheduler.c',
'util.c',
'page_rank_scorer.c',
'hits_scorer.c',
'txn_manager.c',
'domain_temp.c',
'freq_scheduler.c',
'freq_algo.c'
]]
if platform.system() == 'Windows':
aduana_src.append(aduana_lib_root + 'mman.c')
aduana_include = [
aduana_lib_root,
aduana_lib_root + 'lmdb',
aduana_src_root
]
aduana_define = [
('MDB_MAXKEYSIZE', 500)
]
aduana_compile_args = [
'-std=c99',
'-m64',
'-msse2',
'-pthread'
]
aduana_libraries = ['m']
ffi = cffi.FFI()
ffi.set_source(
'_aduana',
'''
#include "bf_scheduler.h"
#include "domain_temp.h"
#include "hits.h"
#include "hits_scorer.h"
#include "link_stream.h"
#include "mmap_array.h"
#include "page_db.h"
#include "page_rank.h"
#include "page_rank_scorer.h"
#include "scheduler.h"
#include "txn_manager.h"
#include "util.h"
#include "freq_scheduler.h"
#include "freq_algo.h"
''',
sources = aduana_src,
include_dirs = aduana_include,
define_macros = aduana_define,
extra_compile_args = aduana_compile_args,
libraries = aduana_libraries
)
ffi.cdef(
"""
const char *
error_message(const void *error);
int
error_code(const void *error);
"""
)
ffi.cdef(
"""
typedef struct {
char *url;
uint64_t linked_from;
uint64_t depth;
double first_crawl;
double last_crawl;
uint64_t n_changes;
uint64_t n_crawls;
float score;
uint64_t content_hash_length;
char *content_hash;
} PageInfo;
float
page_info_rate(const PageInfo *pi);
int
page_info_is_seed(const PageInfo *pi);
void
page_info_delete(PageInfo *pi);
typedef struct {
char *url; /**< ASCII, null terminated string for the page URL*/
float score; /**< An estimated value of the link score */
} LinkInfo;
typedef struct {
LinkInfo *link_info; /**< Array of LinkInfo */
size_t n_links; /**< Number of items inside link_info */
size_t m_links; /**< Maximum number of items that can be stored inside link_info */
} PageLinks;
typedef struct {
char *url; /**< ASCII, null terminated string for the page URL*/
PageLinks *links; /**< List of links inside this page */
double time; /**< Number of seconds since epoch */
float score; /**< A number giving an idea of the page content's value */
char *content_hash; /**< A hash to detect content change since last crawl.
Arbitrary byte sequence */
size_t content_hash_length; /**< Number of byes of the content_hash */
} CrawledPage;
CrawledPage *
crawled_page_new(const char *url);
void
crawled_page_delete(CrawledPage *cp);
int
crawled_page_add_link(CrawledPage *cp, const char *url, float score);
size_t
crawled_page_n_links(const CrawledPage *cp);
int
crawled_page_set_hash64(CrawledPage *cp, uint64_t hash);
const LinkInfo *
crawled_page_get_link(const CrawledPage *cp, size_t i);
typedef enum {
page_db_error_ok = 0, /**< No error */
page_db_error_memory, /**< Error allocating memory */
page_db_error_invalid_path, /**< File system error */
page_db_error_internal, /**< Unexpected error */
page_db_error_no_page /**< A page was requested but could not be found */
} PageDBError;
typedef struct {
char *path;
void* txn_manager;
void *domain_temp;
void *error;
int persist;
} PageDB;
uint64_t
page_db_hash(const char *url);
PageDBError
page_db_new(PageDB **db, const char *path);
PageDBError
page_db_get_info(PageDB *db, uint64_t hash, PageInfo **pi);
PageDBError
page_db_add(PageDB *db, const CrawledPage *page, void **page_info_list);
PageDBError
page_db_delete(PageDB *db);
void
page_db_set_persist(PageDB *db, int value);
typedef enum {
stream_state_init,
stream_state_next,
stream_state_end,
stream_state_error
} StreamState;
typedef struct {
PageDB *db;
void *cur;
StreamState state;
} HashInfoStream;
PageDBError
hashinfo_stream_new(HashInfoStream **st, PageDB *db);
StreamState
hashinfo_stream_next(HashInfoStream *st, uint64_t *hash, PageInfo **pi);
void
hashinfo_stream_delete(HashInfoStream *st);
"""
)
ffi.cdef(
"""
typedef enum {
page_rank_scorer_error_ok = 0, /**< No error */
page_rank_scorer_error_memory, /**< Error allocating memory */
page_rank_scorer_error_internal, /**< Unexpected error */
page_rank_scorer_error_precision /**< Could not achieve precision in maximum number of loops */
} PageRankScorerError;
typedef struct {
void *page_rank;
PageDB *page_db;
void *error;
int persist;
int use_content_scores;
} PageRankScorer;
PageRankScorerError
page_rank_scorer_new(PageRankScorer **prs, PageDB *db);
PageRankScorerError
page_rank_scorer_delete(PageRankScorer *prs);
void
page_rank_scorer_setup(PageRankScorer *prs, void *scorer);
void
page_rank_scorer_set_persist(PageRankScorer *prs, int value);
void
page_rank_scorer_set_use_content_scores(PageRankScorer *prs, int value);
void
page_rank_scorer_set_damping(PageRankScorer *prs, float value);
"""
)
ffi.cdef(
"""
typedef enum {
hits_scorer_error_ok = 0, /**< No error */
hits_scorer_error_memory, /**< Error allocating memory */
hits_scorer_error_internal, /**< Unexpected error */
hits_scorer_error_precision /**< Could not achieve precision in maximum number of loops */
} HitsScorerError;
typedef struct {
void *hits;
PageDB *page_db;
void *error;
int persist;
int use_content_scores;
} HitsScorer;
HitsScorerError
hits_scorer_new(HitsScorer **hs, PageDB *db);
HitsScorerError
hits_scorer_delete(HitsScorer *hs);
void
hits_scorer_setup(HitsScorer *hs, void *scorer);
void
hits_scorer_set_persist(HitsScorer *hs, int value);
void
hits_scorer_set_use_content_scores(HitsScorer *hs, int value);
"""
)
ffi.cdef(
"""
typedef struct {
char **urls;
size_t n_urls;
} PageRequest;
PageRequest*
page_request_new(size_t n_urls);
void
page_request_delete(PageRequest *req);
int
page_request_add_url(PageRequest *req, const char *url);
"""
)
ffi.cdef(
"""
typedef enum {
bf_scheduler_error_ok = 0, /**< No error */
bf_scheduler_error_memory, /**< Error allocating memory */
bf_scheduler_error_invalid_path, /**< File system error */
bf_scheduler_error_internal, /**< Unexpected error */
bf_scheduler_error_thread /**< Error inside the threading library */
} BFSchedulerError;
typedef struct {
PageDB *page_db;
void *scorer;
void *txn_manager;
char *path;
void *update_thread;
void *error;
int persist;
float max_soft_domain_crawl_rate;
float max_hard_domain_crawl_rate;
uint64_t max_crawl_depth;
} BFScheduler;
BFSchedulerError
bf_scheduler_new(BFScheduler **sch, PageDB *db, const char *path);
BFSchedulerError
bf_scheduler_add(BFScheduler *sch, const CrawledPage *page);
BFSchedulerError
bf_scheduler_request(BFScheduler *sch, size_t n_pages, PageRequest **request);
void
bf_scheduler_delete(BFScheduler *sch);
BFSchedulerError
bf_scheduler_update_start(BFScheduler *sch);
BFSchedulerError
bf_scheduler_update_stop(BFScheduler *sch);
void
bf_scheduler_set_persist(BFScheduler *sch, int value);
BFSchedulerError
bf_scheduler_set_max_domain_crawl_rate(BFScheduler *sch,
float max_soft_crawl_rate,
float max_hard_crawl_rate);
void
bf_scheduler_set_max_crawl_depth(BFScheduler *sch, uint64_t value);
typedef int... time_t;
void
bf_scheduler_set_update_interval(BFScheduler *sch, time_t value);
"""
)
ffi.cdef(
"""
typedef enum {
freq_scheduler_error_ok = 0, /**< No error */
freq_scheduler_error_memory, /**< Error allocating memory */
freq_scheduler_error_invalid_path, /**< File system error */
freq_scheduler_error_internal /**< Unexpected error */
} FreqSchedulerError;
typedef struct {
char *path;
PageDB *page_db;
void *txn_manager;
void *error;
int persist;
float margin;
size_t max_n_crawls;
} FreqScheduler;
FreqSchedulerError
freq_scheduler_new(FreqScheduler **sch, PageDB *db, const char *path);
FreqSchedulerError
freq_scheduler_load_simple(FreqScheduler *sch,
float freq_default,
float freq_scale);
FreqSchedulerError
freq_scheduler_load_mmap(FreqScheduler *sch, void *freqs);
FreqSchedulerError
freq_scheduler_request(FreqScheduler *sch,
size_t max_requests,
PageRequest **request);
FreqSchedulerError
freq_scheduler_add(FreqScheduler *sch, const CrawledPage *page);
void
freq_scheduler_delete(FreqScheduler *sch);
FreqSchedulerError
freq_scheduler_cursor_open(FreqScheduler *sch, void **cursor);
FreqSchedulerError
freq_scheduler_cursor_commit(FreqScheduler *sch, void *cursor);
void
freq_scheduler_cursor_abort(FreqScheduler *sch, void *cursor);
FreqSchedulerError
freq_scheduler_cursor_write(FreqScheduler *sch,
void *cursor,
uint64_t hash,
float freq);
"""
)
ffi.cdef(
"""
int
freq_algo_simple(PageDB *db, void **freqs, const char *path, char **error_msg);
"""
)
if __name__ == '__main__':
ffi.compile()