| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (results) { |
0 |
0 |
0 |
|
| 61 |
0 |
▶ | for (i = 0; i < rcount; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (!op) return 0; |
0 |
0 |
0 |
|
| 130 |
0 |
▶ | switch (op->op_type) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (!OpHAS_SIBLING(pushop)) |
0 |
0 |
0 |
|
| 155 |
0 |
| pushop = cUNOPx(pushop)->op_first; |
| 156 |
0 |
▶ | selfop = OpSIBLING(pushop); |
Branch: 1 branch
| Branch | True | False | Total |
| selfop = OpSIBLING(pushop); |
0 |
0 |
0 |
|
| 157 |
0 |
| cvop = argop = selfop; |
| 158 |
0 |
▶ | while (OpHAS_SIBLING(cvop)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (OpHAS_SIBLING(cvop)) { |
0 |
0 |
0 |
|
| 159 |
0 |
| argop = cvop; |
| 160 |
0 |
▶ | cvop = OpSIBLING(cvop); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (argop == selfop) return entersubop; /* no query arg */ |
0 |
0 |
0 |
|
| 233 |
|
| |
| 234 |
0 |
▶ | queryop = OpSIBLING(selfop); |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| limitop = OpSIBLING(queryop); |
0 |
0 |
0 |
|
| 247 |
0 |
▶ | if (limitop != argop) return entersubop; /* > 2 extra args */ |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| if (results) { |
15 |
5 |
20 |
|
| 313 |
|
| uint32_t i; |
| 314 |
38 |
▶ | for (i = 0; i < rcount; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
|
| # |
| 376 |
|
| # Unsigned, not IV: where the loader maps this object decides the sign bit. |
| 377 |
|
| # A 32-bit perl above 0x7fffffff, or illumos putting shared objects up at |
| 378 |
|
| # 0xfffffd7f..., would hand back a negative number from PTR2IV. |
| 379 |
|
| UV |
| 380 |
|
| _abi_ptr() |
| 381 |
|
| CODE: |
| 382 |
11 |
▶ | RETVAL = PTR2UV(&SG_ABI); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2UV(&SG_ABI); |
0 |
11 |
11 |
|
| 383 |
|
| OUTPUT: |
| 384 |
|
| RETVAL |
| 385 |
|
| |
| 386 |
|
| # The ABI version this build compiled against, for a consumer's diagnostics. |
| 387 |
|
| IV |
| 388 |
|
| _abi_version() |
| 389 |
|
| CODE: |
| 390 |
1 |
▶ | RETVAL = SG_ABI_VERSION; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = SG_ABI_VERSION; |
0 |
1 |
1 |
|
| 391 |
|
| OUTPUT: |
| 392 |
|
| RETVAL |
| 393 |
|
| |
| 394 |
|
| # Exercise the whole sg_abi table the way a C consumer would: resolve it from |
| 395 |
|
| # the UV _abi_ptr hands back, gate on abi_version, then drive index_of -> |
| 396 |
|
| # search through the function pointers rather than calling the C directly. |
| 397 |
|
| # This is how the ABI gets tested without a second distribution. |
| 398 |
|
| # |
| 399 |
|
| # Returns a list of { doc_id, score, text } hashrefs; an empty list when the |
| 400 |
|
| # gate rejects the table or the invocant is not an index, which is where a |
| 401 |
|
| # consumer would fall back to the search method. |
| 402 |
|
| void |
| 403 |
|
| _abi_selftest(self, query, limit = 10) |
| 404 |
|
| SV *self |
| 405 |
|
| SV *query |
| 406 |
|
| UV limit |
| 407 |
|
| PPCODE: |
| 408 |
|
| { |
| 409 |
9 |
| const sg_abi *abi = NULL; |
| 410 |
|
| void *idx; |
| 411 |
|
| sg_abi_hit hits[64]; |
| 412 |
|
| uint32_t n, i; |
| 413 |
|
| STRLEN qlen; |
| 414 |
|
| const char *q; |
| 415 |
|
| { |
| 416 |
9 |
| dSP; |
| 417 |
9 |
| UV ptr = 0; |
| 418 |
|
| int count; |
| 419 |
9 |
| ENTER; SAVETMPS; |
| 420 |
9 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
9 |
9 |
|
| 421 |
9 |
| PUTBACK; |
| 422 |
9 |
| count = call_pv("Search::Trigram::_abi_ptr", G_SCALAR | G_EVAL); |
| 423 |
9 |
| SPAGAIN; |
| 424 |
9 |
▶ | if (count > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 0) { |
9 |
0 |
9 |
|
| 425 |
|
| /* Pop into a local first: before 5.30 SvUV was a macro that |
| 426 |
|
| * evaluated its argument twice, so SvUV(POPs) popped twice |
| 427 |
|
| * and read the value off whatever sat one slot lower. |
| 428 |
|
| * |
| 429 |
|
| * SvUV, not SvIV, to match the UV _abi_ptr returns: an |
| 430 |
|
| * address with the top bit set is above IV_MAX, and SvIV |
| 431 |
|
| * would clamp it rather than hand back the address. */ |
| 432 |
9 |
| SV *rv = POPs; |
| 433 |
9 |
▶ | if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
9 |
0 |
9 |
| if (!SvTRUE(ERRSV)) ptr = SvUV(rv); |
9 |
0 |
9 |
|
| 434 |
|
| } |
| 435 |
9 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
9 |
0 |
9 |
|
| 436 |
9 |
▶ | if (ptr) abi = INT2PTR(const sg_abi *, ptr); |
Branch: 1 branch
| Branch | True | False | Total |
| if (ptr) abi = INT2PTR(const sg_abi *, ptr); |
9 |
0 |
9 |
|
| 437 |
|
| } |
| 438 |
9 |
▶ | if (!abi || abi->abi_version < SG_ABI_VERSION) XSRETURN_EMPTY; |
Branch: 2 branches
| Branch | True | False | Total |
| 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 |
|
| 439 |
|
| |
| 440 |
9 |
| idx = abi->index_of(aTHX_ self); |
| 441 |
9 |
▶ | if (!idx) XSRETURN_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!idx) XSRETURN_EMPTY; |
3 |
6 |
9 |
|
| 442 |
|
| |
| 443 |
6 |
| q = SvPVutf8(query, qlen); |
| 444 |
6 |
| n = abi->search(idx, q, (uint32_t)qlen, (uint32_t)limit, |
| 445 |
|
| hits, (uint32_t)(sizeof hits / sizeof hits[0])); |
| 446 |
|
| |
| 447 |
6 |
▶ | EXTEND(SP, (SSize_t)n); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, (SSize_t)n); |
0 |
6 |
6 |
|
| 448 |
16 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
10 |
6 |
16 |
|
| 449 |
10 |
| HV *h = newHV(); |
| 450 |
10 |
| (void)hv_stores(h, "doc_id", newSVuv(hits[i].doc_id)); |
| 451 |
10 |
| (void)hv_stores(h, "score", newSVnv(hits[i].score)); |
| 452 |
10 |
| (void)hv_stores(h, "text", |
| 453 |
|
| newSVpvn_flags(hits[i].text, hits[i].text_len, SVf_UTF8)); |
| 454 |
10 |
| mPUSHs(newRV_noinc((SV *)h)); |
| 455 |
|
| } |
| 456 |
6 |
| XSRETURN((I32)n); |
| 457 |
|
| } |
| 458 |
|
| |
| 459 |
|
| # add / optimize / doc_count driven through the table, so a mis-ordered entry |
| 460 |
|
| # shows up as a wrong answer rather than passing unnoticed. |
| 461 |
|
| UV |
| 462 |
|
| _abi_selftest_add(self, text) |
| 463 |
|
| SV *self |
| 464 |
|
| SV *text |
| 465 |
|
| CODE: |
| 466 |
|
| { |
| 467 |
6 |
| const sg_abi *abi = INT2PTR(const sg_abi *, PTR2IV(&SG_ABI)); |
| 468 |
6 |
| void *idx = abi->index_of(aTHX_ self); |
| 469 |
|
| STRLEN len; |
| 470 |
6 |
| const char *s = SvPVutf8(text, len); |
| 471 |
6 |
▶ | RETVAL = idx ? (UV)abi->add(idx, s, (uint32_t)len) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = idx ? (UV)abi->add(idx, s, (uint32_t)len) : 0; |
6 |
0 |
6 |
|
| 472 |
6 |
▶ | if (idx) abi->optimize(idx); |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx) abi->optimize(idx); |
6 |
0 |
6 |
|
| 473 |
|
| } |
| 474 |
|
| OUTPUT: |
| 475 |
|
| RETVAL |
| 476 |
|
| |
| 477 |
|
| UV |
| 478 |
|
| _abi_selftest_doc_count(self) |
| 479 |
|
| SV *self |
| 480 |
|
| CODE: |
| 481 |
|
| { |
| 482 |
1 |
| const sg_abi *abi = INT2PTR(const sg_abi *, PTR2IV(&SG_ABI)); |
| 483 |
1 |
| void *idx = abi->index_of(aTHX_ self); |
| 484 |
1 |
▶ | RETVAL = idx ? (UV)abi->doc_count(idx) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = idx ? (UV)abi->doc_count(idx) : 0; |
1 |
0 |
1 |
| RETVAL = idx ? (UV)abi->doc_count(idx) : 0; |
0 |
1 |
1 |
|
| 485 |
|
| } |
| 486 |
|
| OUTPUT: |
| 487 |
|
| RETVAL |
| 488 |
|
| |
| 489 |
|
| BOOT: |
| 490 |
|
| { |
| 491 |
7 |
| SV *empty = newSViv(0); |
| 492 |
|
| CV *cv; |
| 493 |
|
| |
| 494 |
7 |
| XopENTRY_set(&xop_strigram_add, xop_name, "strigram_add"); |
| 495 |
7 |
| XopENTRY_set(&xop_strigram_add, xop_desc, "trigram index: add document"); |
| 496 |
7 |
| XopENTRY_set(&xop_strigram_add, xop_class, OA_BINOP); |
| 497 |
7 |
| Perl_custom_op_register(aTHX_ pp_strigram_add, &xop_strigram_add); |
| 498 |
|
| |
| 499 |
7 |
| XopENTRY_set(&xop_strigram_search, xop_name, "strigram_search"); |
| 500 |
7 |
| XopENTRY_set(&xop_strigram_search, xop_desc, "trigram index: search"); |
| 501 |
7 |
| XopENTRY_set(&xop_strigram_search, xop_class, OA_BINOP); |
| 502 |
7 |
| Perl_custom_op_register(aTHX_ pp_strigram_search, &xop_strigram_search); |
| 503 |
|
| |
| 504 |
7 |
| XopENTRY_set(&xop_strigram_remove, xop_name, "strigram_remove"); |
| 505 |
7 |
| XopENTRY_set(&xop_strigram_remove, xop_desc, "trigram index: remove document"); |
| 506 |
7 |
| XopENTRY_set(&xop_strigram_remove, xop_class, OA_BINOP); |
| 507 |
7 |
| Perl_custom_op_register(aTHX_ pp_strigram_remove, &xop_strigram_remove); |
| 508 |
|
| |
| 509 |
7 |
| XopENTRY_set(&xop_strigram_clear, xop_name, "strigram_clear"); |
| 510 |
7 |
| XopENTRY_set(&xop_strigram_clear, xop_desc, "trigram index: clear"); |
| 511 |
7 |
| XopENTRY_set(&xop_strigram_clear, xop_class, OA_UNOP); |
| 512 |
7 |
| Perl_custom_op_register(aTHX_ pp_strigram_clear, &xop_strigram_clear); |
| 513 |
|
| |
| 514 |
7 |
| XopENTRY_set(&xop_strigram_optimize, xop_name, "strigram_optimize"); |
| 515 |
7 |
| XopENTRY_set(&xop_strigram_optimize, xop_desc, "trigram index: optimize"); |
| 516 |
7 |
| XopENTRY_set(&xop_strigram_optimize, xop_class, OA_UNOP); |
| 517 |
7 |
| Perl_custom_op_register(aTHX_ pp_strigram_optimize, &xop_strigram_optimize); |
| 518 |
|
| |
| 519 |
7 |
| XopENTRY_set(&xop_strigram_doc_count, xop_name, "strigram_doc_count"); |
| 520 |
7 |
| XopENTRY_set(&xop_strigram_doc_count, xop_desc, "trigram index: doc_count"); |
| 521 |
7 |
| XopENTRY_set(&xop_strigram_doc_count, xop_class, OA_UNOP); |
| 522 |
7 |
| Perl_custom_op_register(aTHX_ pp_strigram_doc_count, &xop_strigram_doc_count); |
| 523 |
|
| |
| 524 |
7 |
| XopENTRY_set(&xop_strigram_trigram_count, xop_name, "strigram_trigram_count"); |
| 525 |
7 |
| XopENTRY_set(&xop_strigram_trigram_count, xop_desc, "trigram index: trigram_count"); |
| 526 |
7 |
| XopENTRY_set(&xop_strigram_trigram_count, xop_class, OA_UNOP); |
| 527 |
7 |
| Perl_custom_op_register(aTHX_ pp_strigram_trigram_count, &xop_strigram_trigram_count); |
| 528 |
|
| |
| 529 |
7 |
| cv = get_cv("Search::Trigram::add", 0); |
| 530 |
7 |
▶ | if (cv) cv_set_call_checker(cv, ck_strigram_add, empty); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) cv_set_call_checker(cv, ck_strigram_add, empty); |
7 |
0 |
7 |
|
| 531 |
|
| |
| 532 |
7 |
| cv = get_cv("Search::Trigram::search", 0); |
| 533 |
7 |
▶ | if (cv) cv_set_call_checker(cv, ck_strigram_search, empty); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) cv_set_call_checker(cv, ck_strigram_search, empty); |
7 |
0 |
7 |
|
| 534 |
|
| |
| 535 |
7 |
| cv = get_cv("Search::Trigram::remove", 0); |
| 536 |
7 |
▶ | if (cv) cv_set_call_checker(cv, ck_strigram_remove, empty); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) cv_set_call_checker(cv, ck_strigram_remove, empty); |
7 |
0 |
7 |
|
| 537 |
|
| |
| 538 |
7 |
| cv = get_cv("Search::Trigram::clear", 0); |
| 539 |
7 |
▶ | if (cv) cv_set_call_checker(cv, ck_strigram_clear, empty); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) cv_set_call_checker(cv, ck_strigram_clear, empty); |
7 |
0 |
7 |
|
| 540 |
|
| |
| 541 |
7 |
| cv = get_cv("Search::Trigram::optimize", 0); |
| 542 |
7 |
▶ | if (cv) cv_set_call_checker(cv, ck_strigram_optimize, empty); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) cv_set_call_checker(cv, ck_strigram_optimize, empty); |
7 |
0 |
7 |
|
| 543 |
|
| |
| 544 |
7 |
| cv = get_cv("Search::Trigram::doc_count", 0); |
| 545 |
7 |
▶ | if (cv) cv_set_call_checker(cv, ck_strigram_doc_count, empty); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) cv_set_call_checker(cv, ck_strigram_doc_count, empty); |
7 |
0 |
7 |
|
| 546 |
|
| |
| 547 |
7 |
| cv = get_cv("Search::Trigram::trigram_count", 0); |
| 548 |
7 |
▶ | if (cv) cv_set_call_checker(cv, ck_strigram_trigram_count, empty); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cv) cv_set_call_checker(cv, ck_strigram_trigram_count, empty); |
7 |
0 |
7 |
|
| 549 |
|
| |
| 550 |
7 |
| SvREFCNT_dec(empty); |
| 551 |
|
| } |