Trigram.xs

filter: Statementstmt 50.0% 108 / 216 Branchbran 26.4% 36 / 136 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 40.9% 144 / 352 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/arch/Search/Trigram/Install/Files.pm ↑ Summary include/sg_abi_impl.h »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "strigram.h"
5 #include "strigram_compat.h"
6  
7 #define STRIGRAM_FROM_SV(sv) INT2PTR(strigram_t *, SvIV(SvRV(sv)))
8  
9 /* The shared C ABI. Must come after strigram.h and STRIGRAM_FROM_SV. */
10 #include "sg_abi_impl.h"
11  
12 /* ======================================================
13 Custom op descriptors
14 ====================================================== */
15  
16 static XOP xop_strigram_add;
17 static XOP xop_strigram_search;
18 static XOP xop_strigram_remove;
19 static XOP xop_strigram_clear;
20 static XOP xop_strigram_optimize;
21 static XOP xop_strigram_doc_count;
22 static XOP xop_strigram_trigram_count;
23  
24 /* ======================================================
25 pp_ implementations
26 ====================================================== */
27  
28 /* BINOP: self (TOPs), text (POPs) -> UV doc_id replaces self */
29 static OP *
30 0 pp_strigram_add(pTHX) {
31 0 dSP;
32 0 SV *text_sv = POPs;
33 0 SV *self = TOPs;
34 STRLEN len;
35 0 const char *str = SvPVutf8(text_sv, len);
36 0 SETs(sv_2mortal(newSVuv(
37 (UV)strigram_add(STRIGRAM_FROM_SV(self), str, (uint32_t)len))));
38 0 RETURN;
39 }
40  
41 /* BINOP: self (TOPs), query (POPs) -> AV ref replaces self.
42 limit from op_targ (0 = use default 10; non-zero = that value). */
43 static OP *
44 0 pp_strigram_search(pTHX) {
45 0 dSP;
46 0 SV *query_sv = POPs;
47 0 SV *self = TOPs;
48 0 ▶ uint32_t limit = PL_op->op_targ ? (uint32_t)PL_op->op_targ : 10;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t limit = PL_op->op_targ ? (uint32_t)PL_op->op_targ : 10; 0 0 0
49 STRLEN qlen;
50 const char *qstr;
51 0 uint32_t rcount = 0;
52 strigram_result_t *results;
53 AV *av;
54 uint32_t i;
55  
56 0 qstr = SvPVutf8(query_sv, qlen);
57 0 results = strigram_search(STRIGRAM_FROM_SV(self), qstr, (uint32_t)qlen,
58 limit, &rcount);
59 0 av = newAV();
60 0 ▶ if (results) {
Branch: 1 branch
BranchTrueFalseTotal
if (results) { 0 0 0
61 0 ▶ for (i = 0; i < rcount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rcount; i++) { 0 0 0
62 0 HV *hv = newHV();
63 0 hv_stores(hv, "doc_id", newSVuv(results[i].doc_id));
64 0 hv_stores(hv, "score", newSVnv(results[i].score));
65 0 hv_stores(hv, "text",
66 newSVpvn_flags(results[i].text, results[i].text_len, SVf_UTF8));
67 0 av_push(av, newRV_noinc((SV *)hv));
68 }
69 0 strigram_results_free(results);
70 }
71 0 SETs(sv_2mortal(newRV_noinc((SV *)av)));
72 0 RETURN;
73 }
74  
75 /* BINOP: self (POPs), doc_id (POPs) -> void */
76 static OP *
77 0 pp_strigram_remove(pTHX) {
78 0 dSP;
79 0 SV *id_sv = POPs;
80 0 SV *self = POPs;
81 0 strigram_remove(STRIGRAM_FROM_SV(self), (uint32_t)SvUV(id_sv));
82 0 RETURN;
83 }
84  
85 /* UNOP: self (POPs) -> void */
86 static OP *
87 0 pp_strigram_clear(pTHX) {
88 0 dSP;
89 0 SV *self = POPs;
90 0 strigram_clear(STRIGRAM_FROM_SV(self));
91 0 RETURN;
92 }
93  
94 /* UNOP: self (POPs) -> void */
95 static OP *
96 0 pp_strigram_optimize(pTHX) {
97 0 dSP;
98 0 SV *self = POPs;
99 0 strigram_optimize(STRIGRAM_FROM_SV(self));
100 0 RETURN;
101 }
102  
103 /* UNOP: self (TOPs) -> UV replaces self */
104 static OP *
105 0 pp_strigram_doc_count(pTHX) {
106 0 dSP;
107 0 SV *self = TOPs;
108 0 SETs(sv_2mortal(newSVuv(
109 (UV)strigram_doc_count(STRIGRAM_FROM_SV(self)))));
110 0 RETURN;
111 }
112  
113 /* UNOP: self (TOPs) -> UV replaces self */
114 static OP *
115 0 pp_strigram_trigram_count(pTHX) {
116 0 dSP;
117 0 SV *self = TOPs;
118 0 SETs(sv_2mortal(newSVuv(
119 (UV)strigram_trigram_count(STRIGRAM_FROM_SV(self)))));
120 0 RETURN;
121 }
122  
123 /* ======================================================
124 Call checkers
125 ====================================================== */
126  
127 static int
128 0 strigram_simple_op(OP *op) {
129 0 ▶ if (!op) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!op) return 0; 0 0 0
130 0 ▶ switch (op->op_type) {
Branch: 1 branch
BranchTrueFalseTotal
switch (op->op_type) { 0 0 0
131 0 case OP_PADSV:
132 case OP_CONST:
133 case OP_GV:
134 case OP_GVSV:
135 case OP_AELEMFAST:
136 #if defined(OP_AELEMFAST_LEX) && OP_AELEMFAST_LEX != OP_AELEMFAST
137 case OP_AELEMFAST_LEX:
138 #endif
139 case OP_NULL:
140 0 return 1;
141 0 default:
142 0 return 0;
143 }
144 }
145  
146 /* Walk entersub op tree into its components. */
147 static void
148 0 strigram_walk_args(OP *entersubop,
149 OP **pushop_out, OP **selfop_out,
150 OP **argop_out, OP **cvop_out)
151 {
152 OP *pushop, *selfop, *argop, *cvop;
153 0 pushop = cUNOPx(entersubop)->op_first;
154 0 ▶ if (!OpHAS_SIBLING(pushop))
Branch: 1 branch
BranchTrueFalseTotal
if (!OpHAS_SIBLING(pushop)) 0 0 0
155 0 pushop = cUNOPx(pushop)->op_first;
156 0 ▶ selfop = OpSIBLING(pushop);
Branch: 1 branch
BranchTrueFalseTotal
selfop = OpSIBLING(pushop); 0 0 0
157 0 cvop = argop = selfop;
158 0 ▶ while (OpHAS_SIBLING(cvop)) {
Branch: 1 branch
BranchTrueFalseTotal
while (OpHAS_SIBLING(cvop)) { 0 0 0
159 0 argop = cvop;
160 0 ▶ cvop = OpSIBLING(cvop);
Branch: 1 branch
BranchTrueFalseTotal
cvop = OpSIBLING(cvop); 0 0 0
161 }
162 0 *pushop_out = pushop;
163 0 *selfop_out = selfop;
164 0 *argop_out = argop;
165 0 *cvop_out = cvop;
166 0 }
167  
168 /* Stamp out per-method call checkers with macros to avoid storing
169 function pointers in ckobj (non-portable cast on some platforms). */
170  
171 #define STRIGRAM_CK_UNOP(ck_name, pp_func) \
172 static OP * \
173 ck_name(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) \
174 { \
175 OP *pushop, *selfop, *argop, *cvop, *newop; \
176 PERL_UNUSED_ARG(namegv); \
177 PERL_UNUSED_ARG(ckobj); \
178 strigram_walk_args(entersubop, &pushop, &selfop, &argop, &cvop); \
179 if (argop != selfop) return entersubop; \
180 if (!strigram_simple_op(selfop)) return entersubop; \
181 OpMORESIB_set(pushop, cvop); \
182 OpLASTSIB_set(selfop, NULL); \
183 newop = newUNOP(OP_NULL, 0, selfop); \
184 newop->op_type = OP_CUSTOM; \
185 newop->op_ppaddr = pp_func; \
186 op_free(entersubop); \
187 return newop; \
188 }
189  
190 #define STRIGRAM_CK_BINOP(ck_name, pp_func) \
191 static OP * \
192 ck_name(pTHX_ OP *entersubop, GV *namegv, SV *ckobj) \
193 { \
194 OP *pushop, *selfop, *argop, *cvop, *newop; \
195 PERL_UNUSED_ARG(namegv); \
196 PERL_UNUSED_ARG(ckobj); \
197 strigram_walk_args(entersubop, &pushop, &selfop, &argop, &cvop); \
198 if (argop == selfop) return entersubop; \
199 if (OpSIBLING(selfop) != argop) return entersubop; \
200 if (!strigram_simple_op(selfop) || !strigram_simple_op(argop)) \
201 return entersubop; \
202 OpMORESIB_set(pushop, cvop); \
203 OpLASTSIB_set(argop, NULL); \
204 OpLASTSIB_set(selfop, NULL); \
205 newop = newBINOP(OP_NULL, 0, selfop, argop); \
206 newop->op_type = OP_CUSTOM; \
207 newop->op_ppaddr = pp_func; \
208 op_free(entersubop); \
209 return newop; \
210 }
211  
212 0 ▶STRIGRAM_CK_UNOP(ck_strigram_clear, pp_strigram_clear)
Branch: 2 branches
BranchTrueFalseTotal
STRIGRAM_CK_UNOP(ck_strigram_clear, pp_strigram_clear) 0 0 0
STRIGRAM_CK_UNOP(ck_strigram_clear, pp_strigram_clear) 0 0 0
213 0 ▶STRIGRAM_CK_UNOP(ck_strigram_optimize, pp_strigram_optimize)
Branch: 2 branches
BranchTrueFalseTotal
STRIGRAM_CK_UNOP(ck_strigram_optimize, pp_strigram_optimize) 0 0 0
STRIGRAM_CK_UNOP(ck_strigram_optimize, pp_strigram_optimize) 0 0 0
214 0 ▶STRIGRAM_CK_UNOP(ck_strigram_doc_count, pp_strigram_doc_count)
Branch: 2 branches
BranchTrueFalseTotal
STRIGRAM_CK_UNOP(ck_strigram_doc_count, pp_strigram_doc_count) 0 0 0
STRIGRAM_CK_UNOP(ck_strigram_doc_count, pp_strigram_doc_count) 0 0 0
215 0 ▶STRIGRAM_CK_UNOP(ck_strigram_trigram_count, pp_strigram_trigram_count)
Branch: 2 branches
BranchTrueFalseTotal
STRIGRAM_CK_UNOP(ck_strigram_trigram_count, pp_strigram_trigram_count) 0 0 0
STRIGRAM_CK_UNOP(ck_strigram_trigram_count, pp_strigram_trigram_count) 0 0 0
216 0 ▶STRIGRAM_CK_BINOP(ck_strigram_add, pp_strigram_add)
Branch: 5 branches
BranchTrueFalseTotal
STRIGRAM_CK_BINOP(ck_strigram_add, pp_strigram_add) 0 0 0
STRIGRAM_CK_BINOP(ck_strigram_add, pp_strigram_add) 0 0 0
STRIGRAM_CK_BINOP(ck_strigram_add, pp_strigram_add) 0 0 0
STRIGRAM_CK_BINOP(ck_strigram_add, pp_strigram_add) 0 0 0
STRIGRAM_CK_BINOP(ck_strigram_add, pp_strigram_add) 0 0 0
217 0 ▶STRIGRAM_CK_BINOP(ck_strigram_remove, pp_strigram_remove)
Branch: 5 branches
BranchTrueFalseTotal
STRIGRAM_CK_BINOP(ck_strigram_remove, pp_strigram_remove) 0 0 0
STRIGRAM_CK_BINOP(ck_strigram_remove, pp_strigram_remove) 0 0 0
STRIGRAM_CK_BINOP(ck_strigram_remove, pp_strigram_remove) 0 0 0
STRIGRAM_CK_BINOP(ck_strigram_remove, pp_strigram_remove) 0 0 0
STRIGRAM_CK_BINOP(ck_strigram_remove, pp_strigram_remove) 0 0 0
218  
219 /* search: BINOP(self, query) with optional compile-time constant limit
220 embedded in op_targ. Falls back to XS when limit is a runtime variable. */
221 static OP *
222 0 ck_strigram_search(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)
223 {
224 OP *pushop, *selfop, *argop, *cvop;
225 OP *queryop, *limitop, *newop;
226 PADOFFSET limit_val;
227 PERL_UNUSED_ARG(namegv);
228 PERL_UNUSED_ARG(ckobj);
229  
230 0 strigram_walk_args(entersubop, &pushop, &selfop, &argop, &cvop);
231  
232 0 ▶ if (argop == selfop) return entersubop; /* no query arg */
Branch: 1 branch
BranchTrueFalseTotal
if (argop == selfop) return entersubop; /* no query arg */ 0 0 0
233  
234 0 ▶ queryop = OpSIBLING(selfop);
Branch: 1 branch
BranchTrueFalseTotal
queryop = OpSIBLING(selfop); 0 0 0
235 0 limit_val = 0; /* 0 means pp_ uses default 10 */
236  
237 0 ▶ if (queryop == argop) {
Branch: 1 branch
BranchTrueFalseTotal
if (queryop == argop) { 0 0 0
238 /* $idx->search($query) — no limit supplied */
239 0 ▶ if (!strigram_simple_op(selfop) || !strigram_simple_op(queryop))
Branch: 2 branches
BranchTrueFalseTotal
if (!strigram_simple_op(selfop) || !strigram_simple_op(queryop)) 0 0 0
if (!strigram_simple_op(selfop) || !strigram_simple_op(queryop)) 0 0 0
240 0 return entersubop;
241 0 OpMORESIB_set(pushop, cvop);
242 0 OpLASTSIB_set(queryop, NULL);
243 0 OpLASTSIB_set(selfop, NULL);
244 } else {
245 /* $idx->search($query, $limit) */
246 0 ▶ limitop = OpSIBLING(queryop);
Branch: 1 branch
BranchTrueFalseTotal
limitop = OpSIBLING(queryop); 0 0 0
247 0 ▶ if (limitop != argop) return entersubop; /* > 2 extra args */
Branch: 1 branch
BranchTrueFalseTotal
if (limitop != argop) return entersubop; /* > 2 extra args */ 0 0 0
248 /* Only inline when limit is a compile-time constant */
249 0 ▶ if (limitop->op_type != OP_CONST) return entersubop;
Branch: 1 branch
BranchTrueFalseTotal
if (limitop->op_type != OP_CONST) return entersubop; 0 0 0
250 0 ▶ if (!strigram_simple_op(selfop) || !strigram_simple_op(queryop))
Branch: 2 branches
BranchTrueFalseTotal
if (!strigram_simple_op(selfop) || !strigram_simple_op(queryop)) 0 0 0
if (!strigram_simple_op(selfop) || !strigram_simple_op(queryop)) 0 0 0
251 0 return entersubop;
252 0 limit_val = (PADOFFSET)SvUV(cSVOPx(limitop)->op_sv);
253 0 ▶ if (limit_val == 0) limit_val = 10;
Branch: 1 branch
BranchTrueFalseTotal
if (limit_val == 0) limit_val = 10; 0 0 0
254 0 OpMORESIB_set(pushop, cvop);
255 0 OpLASTSIB_set(queryop, NULL);
256 0 OpLASTSIB_set(selfop, NULL);
257 0 OpLASTSIB_set(limitop, NULL); /* clear before freeing */
258 0 op_free(limitop);
259 }
260  
261 0 newop = newBINOP(OP_NULL, 0, selfop, queryop);
262 0 newop->op_type = OP_CUSTOM;
263 0 newop->op_ppaddr = pp_strigram_search;
264 0 newop->op_targ = limit_val;
265  
266 0 op_free(entersubop);
267 0 return newop;
268 }
269  
270 /* ======================================================
271 XS module (fallbacks for non-optimisable call sites)
272 ====================================================== */
273  
274 MODULE = Search::Trigram PACKAGE = Search::Trigram
275  
276 SV *
277 new(class)
278 const char *class
279 CODE:
280 30 strigram_t *idx = strigram_new();
281 30 SV *obj = newSV(0);
282 30 sv_setref_pv(obj, class, (void *)idx);
283 30 RETVAL = obj;
284 OUTPUT:
285 RETVAL
286  
287 UV
288 add(self, text)
289 SV *self
290 SV *text
291 CODE:
292 10040 strigram_t *idx = STRIGRAM_FROM_SV(self);
293 STRLEN len;
294 10040 const char *str = SvPVutf8(text, len);
295 10040 ▶ RETVAL = (UV)strigram_add(idx, str, (uint32_t)len);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)strigram_add(idx, str, (uint32_t)len); 10003 37 10040
296 OUTPUT:
297 RETVAL
298  
299 SV *
300 search(self, query, ...)
301 SV *self
302 SV *query
303 CODE:
304 20 strigram_t *idx = STRIGRAM_FROM_SV(self);
305 20 ▶ uint32_t limit = (items > 2) ? (uint32_t)SvUV(ST(2)) : 10;
Branch: 1 branch
BranchTrueFalseTotal
uint32_t limit = (items > 2) ? (uint32_t)SvUV(ST(2)) : 10; 2 18 20
306 STRLEN qlen;
307 20 const char *qstr = SvPVutf8(query, qlen);
308 20 uint32_t rcount = 0;
309 strigram_result_t *results =
310 20 strigram_search(idx, qstr, (uint32_t)qlen, limit, &rcount);
311 20 AV *av = newAV();
312 20 ▶ if (results) {
Branch: 1 branch
BranchTrueFalseTotal
if (results) { 15 5 20
313 uint32_t i;
314 38 ▶ for (i = 0; i < rcount; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < rcount; i++) { 23 15 38
315 23 HV *hv = newHV();
316 23 hv_stores(hv, "doc_id", newSVuv(results[i].doc_id));
317 23 hv_stores(hv, "score", newSVnv(results[i].score));
318 23 hv_stores(hv, "text",
319 newSVpvn_flags(results[i].text, results[i].text_len,
320 SVf_UTF8));
321 23 av_push(av, newRV_noinc((SV *)hv));
322 }
323 15 strigram_results_free(results);
324 }
325 20 RETVAL = newRV_noinc((SV *)av);
326 OUTPUT:
327 RETVAL
328  
329 void
330 remove(self, doc_id)
331 SV *self
332 UV doc_id
333 CODE:
334 5 strigram_remove(STRIGRAM_FROM_SV(self), (uint32_t)doc_id);
335  
336 void
337 optimize(self)
338 SV *self
339 CODE:
340 4 strigram_optimize(STRIGRAM_FROM_SV(self));
341  
342 void
343 clear(self)
344 SV *self
345 CODE:
346 1 strigram_clear(STRIGRAM_FROM_SV(self));
347  
348 UV
349 doc_count(self)
350 SV *self
351 CODE:
352 9 ▶ RETVAL = (UV)strigram_doc_count(STRIGRAM_FROM_SV(self));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)strigram_doc_count(STRIGRAM_FROM_SV(self)); 0 9 9
353 OUTPUT:
354 RETVAL
355  
356 UV
357 trigram_count(self)
358 SV *self
359 CODE:
360 5 ▶ RETVAL = (UV)strigram_trigram_count(STRIGRAM_FROM_SV(self));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)strigram_trigram_count(STRIGRAM_FROM_SV(self)); 0 5 5
361 OUTPUT:
362 RETVAL
363  
364 void
365 DESTROY(self)
366 SV *self
367 CODE:
368 30 strigram_free(STRIGRAM_FROM_SV(self));
369  
370 # ---- shared C ABI ---------------------------------------------------------
371  
372 # Address of Search::Trigram's own C ABI table (sg_abi.h). A consumer XS
373 # module fetches this once at boot, INT2PTRs it to a `const sg_abi *`, and
374 # checks ->abi_version before using it. Not part of the public Perl API.
375 IV
376 _abi_ptr()
377 CODE:
378 11 ▶ RETVAL = PTR2IV(&SG_ABI);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2IV(&SG_ABI); 0 11 11
379 OUTPUT:
380 RETVAL
381  
382 # The ABI version this build compiled against, for a consumer's diagnostics.
383 IV
384 _abi_version()
385 CODE:
386 1 ▶ RETVAL = SG_ABI_VERSION;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = SG_ABI_VERSION; 0 1 1
387 OUTPUT:
388 RETVAL
389  
390 # Exercise the whole sg_abi table the way a C consumer would: resolve it from
391 # the IV _abi_ptr hands back, gate on abi_version, then drive index_of ->
392 # search through the function pointers rather than calling the C directly.
393 # This is how the ABI gets tested without a second distribution.
394 #
395 # Returns a list of { doc_id, score, text } hashrefs; an empty list when the
396 # gate rejects the table or the invocant is not an index, which is where a
397 # consumer would fall back to the search method.
398 void
399 _abi_selftest(self, query, limit = 10)
400 SV *self
401 SV *query
402 UV limit
403 PPCODE:
404 {
405 9 const sg_abi *abi = NULL;
406 void *idx;
407 sg_abi_hit hits[64];
408 uint32_t n, i;
409 STRLEN qlen;
410 const char *q;
411 {
412 9 dSP;
413 9 IV ptr = 0;
414 int count;
415 9 ENTER; SAVETMPS;
416 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
417 9 PUTBACK;
418 9 count = call_pv("Search::Trigram::_abi_ptr", G_SCALAR | G_EVAL);
419 9 SPAGAIN;
420 9 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 9 0 9
421 /* Pop into a local first: before 5.30 SvIV was a macro that
422 * evaluated its argument twice, so SvIV(POPs) popped twice
423 * and read the IV off whatever sat one slot lower. */
424 9 SV *rv = POPs;
425 9 ▶ if (!SvTRUE(ERRSV)) ptr = SvIV(rv);
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) ptr = SvIV(rv); 9 0 9
if (!SvTRUE(ERRSV)) ptr = SvIV(rv); 9 0 9
426 }
427 9 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 9 0 9
428 9 ▶ if (ptr) abi = INT2PTR(const sg_abi *, ptr);
Branch: 1 branch
BranchTrueFalseTotal
if (ptr) abi = INT2PTR(const sg_abi *, ptr); 9 0 9
429 }
430 9 ▶ if (!abi || abi->abi_version < SG_ABI_VERSION) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!abi || abi->abi_version < SG_ABI_VERSION) XSRETURN_EMPTY; 9 0 9
if (!abi || abi->abi_version < SG_ABI_VERSION) XSRETURN_EMPTY; 0 9 9
431  
432 9 idx = abi->index_of(aTHX_ self);
433 9 ▶ if (!idx) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!idx) XSRETURN_EMPTY; 3 6 9
434  
435 6 q = SvPVutf8(query, qlen);
436 6 n = abi->search(idx, q, (uint32_t)qlen, (uint32_t)limit,
437 hits, (uint32_t)(sizeof hits / sizeof hits[0]));
438  
439 6 ▶ EXTEND(SP, (SSize_t)n);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, (SSize_t)n); 0 6 6
440 16 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 10 6 16
441 10 HV *h = newHV();
442 10 (void)hv_stores(h, "doc_id", newSVuv(hits[i].doc_id));
443 10 (void)hv_stores(h, "score", newSVnv(hits[i].score));
444 10 (void)hv_stores(h, "text",
445 newSVpvn_flags(hits[i].text, hits[i].text_len, SVf_UTF8));
446 10 mPUSHs(newRV_noinc((SV *)h));
447 }
448 6 XSRETURN((I32)n);
449 }
450  
451 # add / optimize / doc_count driven through the table, so a mis-ordered entry
452 # shows up as a wrong answer rather than passing unnoticed.
453 UV
454 _abi_selftest_add(self, text)
455 SV *self
456 SV *text
457 CODE:
458 {
459 6 const sg_abi *abi = INT2PTR(const sg_abi *, PTR2IV(&SG_ABI));
460 6 void *idx = abi->index_of(aTHX_ self);
461 STRLEN len;
462 6 const char *s = SvPVutf8(text, len);
463 6 ▶ RETVAL = idx ? (UV)abi->add(idx, s, (uint32_t)len) : 0;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = idx ? (UV)abi->add(idx, s, (uint32_t)len) : 0; 6 0 6
464 6 ▶ if (idx) abi->optimize(idx);
Branch: 1 branch
BranchTrueFalseTotal
if (idx) abi->optimize(idx); 6 0 6
465 }
466 OUTPUT:
467 RETVAL
468  
469 UV
470 _abi_selftest_doc_count(self)
471 SV *self
472 CODE:
473 {
474 1 const sg_abi *abi = INT2PTR(const sg_abi *, PTR2IV(&SG_ABI));
475 1 void *idx = abi->index_of(aTHX_ self);
476 1 ▶ RETVAL = idx ? (UV)abi->doc_count(idx) : 0;
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = idx ? (UV)abi->doc_count(idx) : 0; 1 0 1
RETVAL = idx ? (UV)abi->doc_count(idx) : 0; 0 1 1
477 }
478 OUTPUT:
479 RETVAL
480  
481 BOOT:
482 {
483 7 SV *empty = newSViv(0);
484 CV *cv;
485  
486 7 XopENTRY_set(&xop_strigram_add, xop_name, "strigram_add");
487 7 XopENTRY_set(&xop_strigram_add, xop_desc, "trigram index: add document");
488 7 XopENTRY_set(&xop_strigram_add, xop_class, OA_BINOP);
489 7 Perl_custom_op_register(aTHX_ pp_strigram_add, &xop_strigram_add);
490  
491 7 XopENTRY_set(&xop_strigram_search, xop_name, "strigram_search");
492 7 XopENTRY_set(&xop_strigram_search, xop_desc, "trigram index: search");
493 7 XopENTRY_set(&xop_strigram_search, xop_class, OA_BINOP);
494 7 Perl_custom_op_register(aTHX_ pp_strigram_search, &xop_strigram_search);
495  
496 7 XopENTRY_set(&xop_strigram_remove, xop_name, "strigram_remove");
497 7 XopENTRY_set(&xop_strigram_remove, xop_desc, "trigram index: remove document");
498 7 XopENTRY_set(&xop_strigram_remove, xop_class, OA_BINOP);
499 7 Perl_custom_op_register(aTHX_ pp_strigram_remove, &xop_strigram_remove);
500  
501 7 XopENTRY_set(&xop_strigram_clear, xop_name, "strigram_clear");
502 7 XopENTRY_set(&xop_strigram_clear, xop_desc, "trigram index: clear");
503 7 XopENTRY_set(&xop_strigram_clear, xop_class, OA_UNOP);
504 7 Perl_custom_op_register(aTHX_ pp_strigram_clear, &xop_strigram_clear);
505  
506 7 XopENTRY_set(&xop_strigram_optimize, xop_name, "strigram_optimize");
507 7 XopENTRY_set(&xop_strigram_optimize, xop_desc, "trigram index: optimize");
508 7 XopENTRY_set(&xop_strigram_optimize, xop_class, OA_UNOP);
509 7 Perl_custom_op_register(aTHX_ pp_strigram_optimize, &xop_strigram_optimize);
510  
511 7 XopENTRY_set(&xop_strigram_doc_count, xop_name, "strigram_doc_count");
512 7 XopENTRY_set(&xop_strigram_doc_count, xop_desc, "trigram index: doc_count");
513 7 XopENTRY_set(&xop_strigram_doc_count, xop_class, OA_UNOP);
514 7 Perl_custom_op_register(aTHX_ pp_strigram_doc_count, &xop_strigram_doc_count);
515  
516 7 XopENTRY_set(&xop_strigram_trigram_count, xop_name, "strigram_trigram_count");
517 7 XopENTRY_set(&xop_strigram_trigram_count, xop_desc, "trigram index: trigram_count");
518 7 XopENTRY_set(&xop_strigram_trigram_count, xop_class, OA_UNOP);
519 7 Perl_custom_op_register(aTHX_ pp_strigram_trigram_count, &xop_strigram_trigram_count);
520  
521 7 cv = get_cv("Search::Trigram::add", 0);
522 7 ▶ if (cv) cv_set_call_checker(cv, ck_strigram_add, empty);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) cv_set_call_checker(cv, ck_strigram_add, empty); 7 0 7
523  
524 7 cv = get_cv("Search::Trigram::search", 0);
525 7 ▶ if (cv) cv_set_call_checker(cv, ck_strigram_search, empty);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) cv_set_call_checker(cv, ck_strigram_search, empty); 7 0 7
526  
527 7 cv = get_cv("Search::Trigram::remove", 0);
528 7 ▶ if (cv) cv_set_call_checker(cv, ck_strigram_remove, empty);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) cv_set_call_checker(cv, ck_strigram_remove, empty); 7 0 7
529  
530 7 cv = get_cv("Search::Trigram::clear", 0);
531 7 ▶ if (cv) cv_set_call_checker(cv, ck_strigram_clear, empty);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) cv_set_call_checker(cv, ck_strigram_clear, empty); 7 0 7
532  
533 7 cv = get_cv("Search::Trigram::optimize", 0);
534 7 ▶ if (cv) cv_set_call_checker(cv, ck_strigram_optimize, empty);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) cv_set_call_checker(cv, ck_strigram_optimize, empty); 7 0 7
535  
536 7 cv = get_cv("Search::Trigram::doc_count", 0);
537 7 ▶ if (cv) cv_set_call_checker(cv, ck_strigram_doc_count, empty);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) cv_set_call_checker(cv, ck_strigram_doc_count, empty); 7 0 7
538  
539 7 cv = get_cv("Search::Trigram::trigram_count", 0);
540 7 ▶ if (cv) cv_set_call_checker(cv, ck_strigram_trigram_count, empty);
Branch: 1 branch
BranchTrueFalseTotal
if (cv) cv_set_call_checker(cv, ck_strigram_trigram_count, empty); 7 0 7
541  
542 7 SvREFCNT_dec(empty);
543 }
« blib/arch/Search/Trigram/Install/Files.pm ↑ Summary include/sg_abi_impl.h »