dbdimp_tokenizer.inc

filter: Statementstmt 0.0% 0 / 118 Branchbran 0.0% 0 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 162 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« dbdimp.h ↑ Summary dbdimp_virtual_table.inc »
1 typedef struct perl_tokenizer {
2 sqlite3_tokenizer base;
3 SV *coderef; /* the perl tokenizer is a coderef that takes
4 a string and returns a cursor coderef */
5 } perl_tokenizer;
6  
7 typedef struct perl_tokenizer_cursor {
8 sqlite3_tokenizer_cursor base;
9 SV *coderef; /* ref to the closure that returns terms */
10 char *pToken; /* storage for a copy of the last token */
11 int nTokenAllocated; /* space allocated to pToken buffer */
12  
13 /* members below are only used if the input string is in utf8 */
14 const char *pInput; /* input we are tokenizing */
15 const char *lastByteOffset; /* offset into pInput */
16 int lastCharOffset; /* char offset corresponding to lastByteOffset */
17 } perl_tokenizer_cursor;
18  
19 /*
20 ** Create a new tokenizer instance.
21 ** Will be called whenever a FTS3 table is created with
22 ** CREATE .. USING fts3( ... , tokenize=perl qualified::function::name)
23 ** where qualified::function::name is a fully qualified perl function
24 */
25 0 static int perl_tokenizer_Create(
26 int argc, const char * const *argv,
27 sqlite3_tokenizer **ppTokenizer
28 ){
29 dTHX;
30 0 dSP;
31 int n_retval;
32 SV *retval;
33 perl_tokenizer *t;
34  
35 0 ▶ if (!argc) {
Branch: 1 branch
BranchTrueFalseTotal
if (!argc) { 0 0 0
36 0 return SQLITE_ERROR;
37 }
38  
39 0 t = (perl_tokenizer *) sqlite3_malloc(sizeof(*t));
40 0 ▶ if( t==NULL ) return SQLITE_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if( t==NULL ) return SQLITE_NOMEM; 0 0 0
41 0 memset(t, 0, sizeof(*t));
42  
43 0 ENTER;
44 0 SAVETMPS;
45  
46 /* call the qualified::function::name */
47 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
48 0 PUTBACK;
49 0 n_retval = call_pv(argv[0], G_SCALAR);
50 0 SPAGAIN;
51  
52 /* store a copy of the returned coderef into the tokenizer structure */
53 0 ▶ if (n_retval != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_retval != 1) { 0 0 0
54 0 warn("tokenizer_Create returned %d arguments", n_retval);
55 }
56 0 retval = POPs;
57 0 t->coderef = newSVsv(retval);
58 0 *ppTokenizer = &t->base;
59  
60 0 PUTBACK;
61 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
62 0 LEAVE;
63  
64 0 return SQLITE_OK;
65 }
66  
67 /*
68 ** Destroy a tokenizer
69 */
70 0 static int perl_tokenizer_Destroy(sqlite3_tokenizer *pTokenizer){
71 dTHX;
72 0 perl_tokenizer *t = (perl_tokenizer *) pTokenizer;
73 0 sv_free(t->coderef);
74 0 sqlite3_free(t);
75 0 return SQLITE_OK;
76 }
77  
78 /*
79 ** Prepare to begin tokenizing a particular string. The input
80 ** string to be tokenized is supposed to be pInput[0..nBytes-1] ..
81 ** except that nBytes passed by fts3 is -1 (don't know why) !
82 ** This is passed to the tokenizer instance, which then returns a
83 ** closure implementing the cursor (so the cursor is again a coderef).
84 */
85 0 static int perl_tokenizer_Open(
86 sqlite3_tokenizer *pTokenizer, /* Tokenizer object */
87 const char *pInput, int nBytes, /* Input buffer */
88 sqlite3_tokenizer_cursor **ppCursor /* OUT: Created tokenizer cursor */
89 ){
90 dTHX;
91 0 dSP;
92 dMY_CXT;
93 U32 flags;
94 SV *perl_string;
95 int n_retval;
96  
97 0 perl_tokenizer *t = (perl_tokenizer *)pTokenizer;
98  
99 /* allocate and initialize the cursor struct */
100 perl_tokenizer_cursor *c;
101 0 c = (perl_tokenizer_cursor *) sqlite3_malloc(sizeof(*c));
102 0 memset(c, 0, sizeof(*c));
103 0 *ppCursor = &c->base;
104  
105 /* flags for creating the Perl SV containing the input string */
106 0 flags = SVs_TEMP; /* will call sv_2mortal */
107  
108 /* special handling if working with utf8 strings */
109 0 ▶ if (MY_CXT.last_dbh_is_unicode) {
Branch: 1 branch
BranchTrueFalseTotal
if (MY_CXT.last_dbh_is_unicode) { 0 0 0
110  
111 /* data to keep track of byte offsets */
112 0 c->lastByteOffset = c->pInput = pInput;
113 0 c->lastCharOffset = 0;
114  
115 /* string passed to Perl needs to be flagged as utf8 */
116 0 flags |= SVf_UTF8;
117 }
118  
119 0 ENTER;
120 0 SAVETMPS;
121  
122 /* build a Perl copy of the input string */
123 0 ▶ if (nBytes < 0) { /* we get -1 from fts3. Don't know why ! */
Branch: 1 branch
BranchTrueFalseTotal
if (nBytes < 0) { /* we get -1 from fts3. Don't know why ! */ 0 0 0
124 0 nBytes = strlen(pInput);
125 }
126 0 perl_string = newSVpvn_flags(pInput, nBytes, flags);
127  
128 /* call the tokenizer coderef */
129 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
130 0 ▶ XPUSHs(perl_string);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(perl_string); 0 0 0
131 0 PUTBACK;
132 0 n_retval = call_sv(t->coderef, G_SCALAR);
133 0 SPAGAIN;
134  
135 /* store the cursor coderef returned by the tokenizer */
136 0 ▶ if (n_retval != 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_retval != 1) { 0 0 0
137 0 warn("tokenizer returned %d arguments", n_retval);
138 }
139 0 c->coderef = newSVsv(POPs);
140  
141 0 PUTBACK;
142 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
143 0 LEAVE;
144 0 return SQLITE_OK;
145 }
146  
147 /*
148 ** Close a tokenization cursor previously opened by a call to
149 ** perl_tokenizer_Open() above.
150 */
151 0 static int perl_tokenizer_Close(sqlite3_tokenizer_cursor *pCursor){
152 0 perl_tokenizer_cursor *c = (perl_tokenizer_cursor *) pCursor;
153  
154 dTHX;
155 0 sv_free(c->coderef);
156 0 ▶ if (c->pToken) sqlite3_free(c->pToken);
Branch: 1 branch
BranchTrueFalseTotal
if (c->pToken) sqlite3_free(c->pToken); 0 0 0
157 0 sqlite3_free(c);
158 0 return SQLITE_OK;
159 }
160  
161 /*
162 ** Extract the next token from a tokenization cursor. The cursor must
163 ** have been opened by a prior call to perl_tokenizer_Open().
164 */
165 0 static int perl_tokenizer_Next(
166 sqlite3_tokenizer_cursor *pCursor, /* Cursor returned by perl_tokenizer_Open */
167 const char **ppToken, /* OUT: *ppToken is the token text */
168 int *pnBytes, /* OUT: Number of bytes in token */
169 int *piStartOffset, /* OUT: Starting offset of token */
170 int *piEndOffset, /* OUT: Ending offset of token */
171 int *piPosition /* OUT: Position integer of token */
172 ){
173 0 perl_tokenizer_cursor *c = (perl_tokenizer_cursor *) pCursor;
174 int result;
175 int n_retval;
176 char *token;
177 char *byteOffset;
178 STRLEN n_a; /* this is required for older perls < 5.8.8 */
179 I32 hop;
180  
181 dTHX;
182 0 dSP;
183  
184 0 ENTER;
185 0 SAVETMPS;
186  
187 /* call the cursor */
188 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
189 0 PUTBACK;
190 0 n_retval = call_sv(c->coderef, G_ARRAY);
191 0 SPAGAIN;
192  
193 /* if we get back an empty list, there is no more token */
194 0 ▶ if (n_retval == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_retval == 0) { 0 0 0
195 0 result = SQLITE_DONE;
196 }
197 /* otherwise, get token details from the return list */
198 else {
199 0 ▶ if (n_retval != 5) {
Branch: 1 branch
BranchTrueFalseTotal
if (n_retval != 5) { 0 0 0
200 0 warn("tokenizer cursor returned %d arguments", n_retval);
201 }
202 0 *piPosition = POPi;
203 0 *piEndOffset = POPi;
204 0 *piStartOffset = POPi;
205 0 *pnBytes = POPi;
206 0 token = POPpx;
207  
208 0 ▶ if (c->pInput) { /* if working with utf8 data */
Branch: 1 branch
BranchTrueFalseTotal
if (c->pInput) { /* if working with utf8 data */ 0 0 0
209  
210 /* recompute *pnBytes in bytes, not in chars */
211 0 *pnBytes = strlen(token);
212  
213 /* recompute start/end offsets in bytes, not in chars */
214 0 hop = *piStartOffset - c->lastCharOffset;
215 0 byteOffset = (char*)utf8_hop((U8*)c->lastByteOffset, hop);
216 0 hop = *piEndOffset - *piStartOffset;
217 0 *piStartOffset = byteOffset - c->pInput;
218 0 byteOffset = (char*)utf8_hop((U8*)byteOffset, hop);
219 0 *piEndOffset = byteOffset - c->pInput;
220  
221 /* remember where we are for next round */
222 0 c->lastCharOffset = *piEndOffset,
223 0 c->lastByteOffset = byteOffset;
224 }
225  
226 /* make sure we have enough storage for copying the token */
227 0 ▶ if (*pnBytes > c->nTokenAllocated ){
Branch: 1 branch
BranchTrueFalseTotal
if (*pnBytes > c->nTokenAllocated ){ 0 0 0
228 char *pNew;
229 0 c->nTokenAllocated = *pnBytes + 20;
230 0 pNew = sqlite3_realloc(c->pToken, c->nTokenAllocated);
231 0 ▶ if( !pNew ) return SQLITE_NOMEM;
Branch: 1 branch
BranchTrueFalseTotal
if( !pNew ) return SQLITE_NOMEM; 0 0 0
232 0 c->pToken = pNew;
233 }
234  
235 /* need to copy the token into the C cursor before perl frees that
236 memory */
237 0 memcpy(c->pToken, token, *pnBytes);
238 0 *ppToken = c->pToken;
239  
240 0 result = SQLITE_OK;
241 }
242  
243 0 PUTBACK;
244 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
245 0 LEAVE;
246  
247 0 return result;
248 }
249  
250 /*
251 ** The set of routines that implement the perl tokenizer
252 */
253 sqlite3_tokenizer_module perl_tokenizer_Module = {
254 0,
255 perl_tokenizer_Create,
256 perl_tokenizer_Destroy,
257 perl_tokenizer_Open,
258 perl_tokenizer_Close,
259 perl_tokenizer_Next
260 };
261  
262 /*
263 ** Register the perl tokenizer with FTS3
264 */
265 0 int sqlite_db_register_fts3_perl_tokenizer(pTHX_ SV *dbh)
266 {
267 0 D_imp_dbh(dbh);
268  
269 int rc;
270 sqlite3_stmt *pStmt;
271 0 const char zSql[] = "SELECT fts3_tokenizer(?, ?)";
272 0 sqlite3_tokenizer_module *p = &perl_tokenizer_Module;
273  
274 0 ▶ if (!DBIc_ACTIVE(imp_dbh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!DBIc_ACTIVE(imp_dbh)) { 0 0 0
275 0 sqlite_error(dbh, -2, "attempt to register fts3 tokenizer on inactive database handle");
276 0 return FALSE;
277 }
278  
279 #if SQLITE_VERSION_NUMBER >= 3012000
280 0 rc = sqlite3_db_config(imp_dbh->db, SQLITE_DBCONFIG_ENABLE_FTS3_TOKENIZER, 1, 0);
281 0 ▶ if( rc!=SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc!=SQLITE_OK ){ 0 0 0
282 0 return rc;
283 }
284 #endif
285  
286 0 rc = sqlite3_prepare_v2(imp_dbh->db, zSql, -1, &pStmt, 0);
287 0 ▶ if( rc!=SQLITE_OK ){
Branch: 1 branch
BranchTrueFalseTotal
if( rc!=SQLITE_OK ){ 0 0 0
288 0 return rc;
289 }
290  
291 0 sqlite3_bind_text(pStmt, 1, "perl", -1, SQLITE_STATIC);
292 0 sqlite3_bind_blob(pStmt, 2, &p, sizeof(p), SQLITE_STATIC);
293 0 sqlite3_step(pStmt);
294  
295 0 return sqlite3_finalize(pStmt);
296 }
« dbdimp.h ↑ Summary dbdimp_virtual_table.inc »