| 1 |
|
| #ifndef SG_ABI_IMPL_H |
| 2 |
|
| #define SG_ABI_IMPL_H |
| 3 |
|
| |
| 4 |
|
| /* Search::Trigram-side implementation of the shared C ABI (sg_abi.h). |
| 5 |
|
| * Included by Trigram.xs AFTER strigram.h and STRIGRAM_FROM_SV are in scope. |
| 6 |
|
| * Everything here is private to this translation unit; consumers reach it |
| 7 |
|
| * only through the SG_ABI table returned by Search::Trigram::_abi_ptr. */ |
| 8 |
|
| |
| 9 |
|
| #include "sg_abi.h" |
| 10 |
|
| |
| 11 |
|
| /* Like STRIGRAM_FROM_SV but returns NULL where that would dereference |
| 12 |
|
| * whatever it was handed: a consumer probing whether an SV is a usable index |
| 13 |
|
| * must be able to ask without an eval. */ |
| 14 |
16 |
| static void *sg_abi_index_of(pTHX_ SV *obj_sv) |
| 15 |
|
| { |
| 16 |
|
| SV *rv; |
| 17 |
16 |
▶ | if (!obj_sv || !SvROK(obj_sv)) return NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!obj_sv || !SvROK(obj_sv)) return NULL; |
16 |
0 |
16 |
| if (!obj_sv || !SvROK(obj_sv)) return NULL; |
1 |
15 |
16 |
|
| 18 |
15 |
▶ | if (!sv_derived_from(obj_sv, "Search::Trigram")) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_derived_from(obj_sv, "Search::Trigram")) return NULL; |
2 |
13 |
15 |
|
| 19 |
13 |
| rv = SvRV(obj_sv); |
| 20 |
13 |
▶ | if (!SvIOK(rv)) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIOK(rv)) return NULL; |
0 |
13 |
13 |
|
| 21 |
13 |
| return INT2PTR(void *, SvIV(rv)); |
| 22 |
|
| } |
| 23 |
|
| |
| 24 |
6 |
| static uint32_t sg_abi_add(void *idx, const char *text, uint32_t len) |
| 25 |
|
| { |
| 26 |
6 |
▶ | if (!idx || !text) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!idx || !text) return 0; |
6 |
0 |
6 |
| if (!idx || !text) return 0; |
0 |
6 |
6 |
|
| 27 |
6 |
| return strigram_add((strigram_t *)idx, text, len); |
| 28 |
|
| } |
| 29 |
|
| |
| 30 |
6 |
| static void sg_abi_optimize(void *idx) |
| 31 |
|
| { |
| 32 |
6 |
▶ | if (idx) strigram_optimize((strigram_t *)idx); |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx) strigram_optimize((strigram_t *)idx); |
6 |
0 |
6 |
|
| 33 |
6 |
| } |
| 34 |
|
| |
| 35 |
1 |
| static uint32_t sg_abi_doc_count(const void *idx) |
| 36 |
|
| { |
| 37 |
1 |
▶ | if (!idx) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!idx) return 0; |
0 |
1 |
1 |
|
| 38 |
1 |
| return strigram_doc_count((const strigram_t *)idx); |
| 39 |
|
| } |
| 40 |
|
| |
| 41 |
|
| /* strigram_search mallocs its result array and pairs with |
| 42 |
|
| * strigram_results_free. The ABI does not expose that pairing - see the note |
| 43 |
|
| * in sg_abi.h - so this copies into the caller's array and frees the |
| 44 |
|
| * provider-side allocation before returning, keeping both halves of the |
| 45 |
|
| * malloc/free on this side of the boundary. |
| 46 |
|
| * |
| 47 |
|
| * The `text` pointers copied out are borrowed from the index itself, not from |
| 48 |
|
| * the result array, so they stay valid after the free. */ |
| 49 |
6 |
| static uint32_t sg_abi_search(void *idx, const char *q, uint32_t qlen, |
| 50 |
|
| uint32_t limit, sg_abi_hit *hits, |
| 51 |
|
| uint32_t max_hits) |
| 52 |
|
| { |
| 53 |
|
| strigram_result_t *results; |
| 54 |
6 |
| uint32_t rcount = 0, n, i; |
| 55 |
|
| |
| 56 |
6 |
▶ | if (!idx || !q || !hits || max_hits == 0) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!idx || !q || !hits || max_hits == 0) return 0; |
6 |
0 |
6 |
| if (!idx || !q || !hits || max_hits == 0) return 0; |
6 |
0 |
6 |
| if (!idx || !q || !hits || max_hits == 0) return 0; |
6 |
0 |
6 |
| if (!idx || !q || !hits || max_hits == 0) return 0; |
0 |
6 |
6 |
|
| 57 |
|
| |
| 58 |
6 |
| results = strigram_search((strigram_t *)idx, q, qlen, limit, &rcount); |
| 59 |
6 |
▶ | if (!results) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!results) return 0; |
1 |
5 |
6 |
|
| 60 |
|
| |
| 61 |
5 |
| n = rcount < max_hits ? rcount : max_hits; |
| 62 |
15 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
10 |
5 |
15 |
|
| 63 |
10 |
| hits[i].doc_id = results[i].doc_id; |
| 64 |
10 |
| hits[i].score = results[i].score; |
| 65 |
10 |
| hits[i].text = results[i].text; |
| 66 |
10 |
| hits[i].text_len = results[i].text_len; |
| 67 |
|
| } |
| 68 |
5 |
| strigram_results_free(results); |
| 69 |
5 |
| return n; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
|
| static const sg_abi SG_ABI = { |
| 73 |
|
| SG_ABI_VERSION, |
| 74 |
|
| sg_abi_index_of, |
| 75 |
|
| sg_abi_add, |
| 76 |
|
| sg_abi_optimize, |
| 77 |
|
| sg_abi_doc_count, |
| 78 |
|
| sg_abi_search, |
| 79 |
|
| }; |
| 80 |
|
| |
| 81 |
|
| #endif /* SG_ABI_IMPL_H */ |