-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathsqlite-lines.c
More file actions
596 lines (543 loc) · 18.5 KB
/
sqlite-lines.c
File metadata and controls
596 lines (543 loc) · 18.5 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
#include "sqlite3ext.h"
SQLITE_EXTENSION_INIT1
#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#pragma region sqlite - lines meta scalar functions
// TODO is this deterministic?
static void linesVersionFunc(sqlite3_context *context, int argc,
sqlite3_value **argv) {
sqlite3_result_text(context, sqlite3_user_data(context), -1, SQLITE_STATIC);
}
// TODO is this deterministic?
static void linesDebugFunc(sqlite3_context *context, int argc,
sqlite3_value **arg) {
sqlite3_result_text(context, sqlite3_user_data(context), -1, SQLITE_STATIC);
}
#pragma endregion
#pragma region lines() and lines_read() table functions
typedef struct lines_cursor lines_cursor;
struct lines_cursor {
sqlite3_vtab_cursor base; /* Base class - must be first */
// File pointer of the file being "read" (or in memory file for lines())
FILE *fp;
// length of current line
size_t curLineLength;
char *curLineContents;
size_t curLineLen;
char delim;
int idxNum;
int rowid_eq_yielded;
// either the path to the file being read (lines_read()),
// or the contents of the "document" (lines())
char *in;
sqlite3_int64 iRowid; /* The rowid */
};
#define LINES_READ_COLUMN_ROWID -1
#define LINES_READ_COLUMN_LINE 0
#define LINES_READ_COLUMN_PATH 1
#define LINES_READ_COLUMN_DELIM 2
#define LINES_IDXNUM_FULL 1
#define LINES_IDXNUM_ROWID_EQ 2
#define LINES_IDXSTR_PATH 'P'
#define LINES_IDXSTR_DELIMITER 'D'
#define LINES_IDXSTR_ROWID 'R'
#define LINES_IDXSTR_LENGTH 3
/*
** The linesReadConnect() method is invoked to create a new
** lines_vtab that describes the lines_read virtual table.
**
** Think of this routine as the constructor for lines_vtab objects.
**
** All this routine needs to do is:
**
** (1) Allocate the lines_vtab object and initialize all fields.
**
** (2) Tell SQLite (via the sqlite3_declare_vtab() interface) what the
** result set of queries against lines_read will look like.
*/
static int linesConnect(sqlite3 *db, void *pUnused, int argcUnused,
const char *const *argvUnused, sqlite3_vtab **ppVtab,
char **pzErrUnused) {
sqlite3_vtab *pNew;
int rc;
(void)pUnused;
(void)argcUnused;
(void)argvUnused;
(void)pzErrUnused;
rc = sqlite3_declare_vtab(db, "CREATE TABLE x(line text,"
"document hidden, delimiter hidden)");
if (rc == SQLITE_OK) {
pNew = *ppVtab = sqlite3_malloc(sizeof(*pNew));
if (pNew == 0)
return SQLITE_NOMEM;
memset(pNew, 0, sizeof(*pNew));
sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
}
return rc;
}
/*
** This method is the destructor for lines_cursor objects.
*/
static int linesDisconnect(sqlite3_vtab *pVtab) {
sqlite3_free(pVtab);
return SQLITE_OK;
}
/*
** Constructor for a new lines_cursor object.
*/
static int linesOpen(sqlite3_vtab *pUnused, sqlite3_vtab_cursor **ppCursor) {
lines_cursor *pCur;
(void)pUnused;
pCur = sqlite3_malloc(sizeof(*pCur));
if (pCur == 0)
return SQLITE_NOMEM;
memset(pCur, 0, sizeof(*pCur));
*ppCursor = &pCur->base;
return SQLITE_OK;
}
/*
** Destructor for a lines_cursor.
*/
static int linesClose(sqlite3_vtab_cursor *cur) {
lines_cursor *pCur = (lines_cursor *)cur;
if (pCur->curLineContents != NULL)
free(pCur->curLineContents);
if (pCur->fp != NULL)
fclose(pCur->fp);
sqlite3_free(cur);
return SQLITE_OK;
}
/*
** Advance a lines_cursor to its next row of output.
*/
static int linesNext(sqlite3_vtab_cursor *cur) {
lines_cursor *pCur = (lines_cursor *)cur;
pCur->iRowid++;
pCur->curLineLength = getdelim(&pCur->curLineContents, &pCur->curLineLen,
pCur->delim, pCur->fp);
return SQLITE_OK;
}
/*
** Return TRUE if the cursor has been moved off of the last
** row of output.
*/
static int linesEof(sqlite3_vtab_cursor *cur) {
lines_cursor *pCur = (lines_cursor *)cur;
if (pCur->idxNum == LINES_IDXNUM_ROWID_EQ) {
if (pCur->rowid_eq_yielded)
return 1;
pCur->rowid_eq_yielded = 1;
return 0;
}
return pCur->curLineLength == -1;
}
/*
** Return values of columns for the row at which the lines_cursor
** is currently pointing.
*/
static int
linesColumn(sqlite3_vtab_cursor *cur, /* The cursor */
sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
int i /* Which column to return */
) {
lines_cursor *pCur = (lines_cursor *)cur;
sqlite3_int64 x = 0;
switch (i) {
case LINES_READ_COLUMN_LINE: {
// If the line ends in the delimiter character, then shave it off.
// If the delimter is '\n' and the line ends with '\r\n', then also
// shave off that '\r', to support CRLF files.
int trim = 0;
if (pCur->curLineLength > 0 &&
pCur->curLineContents[pCur->curLineLength - 1] == pCur->delim) {
if (pCur->curLineLength > 1 &&
pCur->curLineContents[pCur->curLineLength - 1] == '\n' &&
pCur->curLineContents[pCur->curLineLength - 2] == '\r')
trim = 2;
else
trim = 1;
}
sqlite3 *db = sqlite3_context_db_handle(ctx);
int mxBlob = sqlite3_limit(db, SQLITE_LIMIT_LENGTH, -1);
if (pCur->curLineLength > mxBlob) {
sqlite3_result_error_code(ctx, SQLITE_TOOBIG);
sqlite3_result_error(
ctx,
sqlite3_mprintf(
"line %d has a size of %d bytes, but SQLITE_LIMIT_LENGTH is %d",
pCur->iRowid, pCur->curLineLength, mxBlob),
-1);
return SQLITE_ERROR;
}
sqlite3_result_text(ctx, pCur->curLineContents, pCur->curLineLength - trim,
SQLITE_TRANSIENT);
break;
}
case LINES_READ_COLUMN_DELIM: {
sqlite3_result_text(ctx, &pCur->delim, 1, SQLITE_TRANSIENT);
break;
}
case LINES_READ_COLUMN_PATH: {
sqlite3_result_text(ctx, pCur->in, -1, SQLITE_TRANSIENT);
break;
}
}
return SQLITE_OK;
}
/*
** Return the rowid for the current row. In this implementation, the
** first row returned is assigned rowid value 1, and each subsequent
** row a value 1 more than that of the previous.
*/
static int linesRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid) {
lines_cursor *pCur = (lines_cursor *)cur;
*pRowid = pCur->iRowid;
return SQLITE_OK;
}
/*
** SQLite will invoke this method one or more times while planning a query
** that uses the lines_read virtual table. This routine needs to create
** a query plan for each invocation and compute an estimated cost for that
** plan.
*/
/*
Every query plan for lines() or lines_read() will use idxNum and idxStr.
idxNum options:
LINES_IDXNUM_FULL: "do a full scan", ie read all lines from file/document
LINES_IDXNUM_ROWID_EQ: Only read a single line, defined by a "rowid = :x"
constraint
idxStr is a 3-character string that denotes which argv option cooresponds
to which column constraint. The i-th character in the string cooresponds
to the i-th argv option in the xFilter functions.
idxStr character options:
LINES_IDXSTR_PATH: argv[i] is text to the path of the file or the document
itself LINES_IDXSTR_DELIMITER: argv[i] will be text of delimiter to use
LINES_IDXSTR_ROWID: argv[i] is integer of rowid to filter to, with
LINES_IDXNUM_ROWID_EQ
*/
static int linesBestIndex(sqlite3_vtab *pVTab, sqlite3_index_info *pIdxInfo) {
int hasPath = 0;
int hasDelim = 0;
int hasRowidEq = 0;
int argv = 1;
pIdxInfo->idxStr = sqlite3_mprintf("000");
if (pIdxInfo->idxStr == NULL) {
pVTab->zErrMsg = sqlite3_mprintf("unable to allocate memory for idxStr");
return SQLITE_NOMEM;
}
for (int i = 0; i < pIdxInfo->nConstraint; i++) {
const struct sqlite3_index_constraint *pCons = &pIdxInfo->aConstraint[i];
#ifdef SQLITE_LINES_DEBUG
printf("i=%d iColumn=%d, op=%d, usable=%d\n", i, pCons->iColumn, pCons->op,
pCons->usable);
#endif
switch (pCons->iColumn) {
case LINES_READ_COLUMN_ROWID: {
if (pCons->op == SQLITE_INDEX_CONSTRAINT_EQ && pCons->usable) {
hasRowidEq = 1;
pIdxInfo->aConstraintUsage[i].argvIndex = argv;
pIdxInfo->aConstraintUsage[i].omit = 1;
pIdxInfo->idxStr[argv - 1] = LINES_IDXSTR_ROWID;
argv++;
}
break;
}
case LINES_READ_COLUMN_PATH: {
if (!hasPath && !pCons->usable || pCons->op != SQLITE_INDEX_CONSTRAINT_EQ)
return SQLITE_CONSTRAINT;
hasPath = 1;
pIdxInfo->aConstraintUsage[i].argvIndex = argv;
pIdxInfo->aConstraintUsage[i].omit = 1;
pIdxInfo->idxStr[argv - 1] = LINES_IDXSTR_PATH;
argv++;
break;
}
case LINES_READ_COLUMN_DELIM: {
if (!pCons->usable || pCons->op != SQLITE_INDEX_CONSTRAINT_EQ)
return SQLITE_CONSTRAINT;
hasDelim = 1;
pIdxInfo->aConstraintUsage[i].argvIndex = argv;
pIdxInfo->aConstraintUsage[i].omit = 1;
pIdxInfo->idxStr[argv - 1] = LINES_IDXSTR_DELIMITER;
argv++;
break;
}
}
}
if (!hasPath) {
pVTab->zErrMsg = sqlite3_mprintf("path argument is required");
return SQLITE_ERROR;
}
if (hasRowidEq) {
pIdxInfo->idxNum = LINES_IDXNUM_ROWID_EQ;
pIdxInfo->estimatedCost = (double)1;
pIdxInfo->estimatedRows = 1;
// pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
return SQLITE_OK;
}
pIdxInfo->idxNum = LINES_IDXNUM_FULL;
pIdxInfo->needToFreeIdxStr = 1;
pIdxInfo->estimatedCost = (double)100000;
pIdxInfo->estimatedRows = 100000;
return SQLITE_OK;
}
/*
** This method is called to "rewind" the lines_cursor object back
** to the first row of output. This method is always called at least
** once prior to any call to xColumn() or xRowid() or xEof().
**
** This routine should initialize the cursor and position it so that it
** is pointing at the first row, or pointing off the end of the table
** (so that xEof() will return true) if the table is empty.
*/
static int linesFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum,
const char *idxStr, int argc, sqlite3_value **argv) {
int targetRowid;
char delim = '\n';
lines_cursor *pCur = (lines_cursor *)pVtabCursor;
if (pCur->fp != NULL) {
fclose(pCur->fp);
}
if (pCur->curLineContents != NULL)
free(pCur->curLineContents);
for (int i = 0; i < LINES_IDXSTR_LENGTH; i++) {
switch (idxStr[i]) {
case LINES_IDXSTR_ROWID: {
targetRowid = sqlite3_value_int64(argv[i]);
break;
}
case LINES_IDXSTR_PATH: {
int nByte = sqlite3_value_bytes(argv[i]);
void *pData = (void *)sqlite3_value_blob(argv[i]);
int errnum;
pCur->fp = fmemopen(pData, nByte, "r");
if (pCur->fp == NULL) {
int errnum;
errnum = errno;
pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf(
"Error reading document, size=%d: %s", nByte, strerror(errnum));
return SQLITE_ERROR;
}
break;
}
case LINES_IDXSTR_DELIMITER: {
int nByte = sqlite3_value_bytes(argv[i]);
if (nByte != 1) {
pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf(
"Delimiter must be 1 character long, got %d characters", nByte);
return SQLITE_ERROR;
}
const char *s = (const char *)sqlite3_value_text(argv[i]);
delim = s[0];
break;
}
}
}
pCur->curLineContents = 0;
pCur->curLineLength =
getdelim(&pCur->curLineContents, &pCur->curLineLen, delim, pCur->fp);
pCur->iRowid = 1;
pCur->delim = delim;
pCur->idxNum = idxNum;
pCur->in = "";
if (pCur->idxNum == LINES_IDXNUM_ROWID_EQ) {
pCur->rowid_eq_yielded = 0;
while (pCur->iRowid < targetRowid && pCur->curLineLength >= 0) {
pCur->curLineLength =
getdelim(&pCur->curLineContents, &pCur->curLineLen, delim, pCur->fp);
pCur->iRowid++;
}
}
return SQLITE_OK;
}
static int linesReadFilter(sqlite3_vtab_cursor *pVtabCursor, int idxNum,
const char *idxStr, int argc, sqlite3_value **argv) {
int targetRowid;
char delim = '\n';
lines_cursor *pCur = (lines_cursor *)pVtabCursor;
if (pCur->fp != NULL) {
fclose(pCur->fp);
}
if (pCur->curLineContents != NULL)
free(pCur->curLineContents);
for (int i = 0; i < LINES_IDXSTR_LENGTH; i++) {
switch (idxStr[i]) {
case LINES_IDXSTR_ROWID: {
targetRowid = sqlite3_value_int64(argv[i]);
break;
}
case LINES_IDXSTR_PATH: {
if (sqlite3_value_type(argv[i]) == SQLITE_NULL) {
pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf("path is null");
return SQLITE_ERROR;
}
char *path = (char *)sqlite3_value_text(argv[i]);
// TODO should we free this later?
pCur->in = (char *)path;
int errnum;
pCur->fp = fopen(path, "r");
if (pCur->fp == NULL) {
int errnum;
errnum = errno;
pVtabCursor->pVtab->zErrMsg =
sqlite3_mprintf("Error reading %s: %s", path, strerror(errnum));
return SQLITE_ERROR;
}
break;
}
case LINES_IDXSTR_DELIMITER: {
int nByte = sqlite3_value_bytes(argv[i]);
if (nByte != 1) {
pVtabCursor->pVtab->zErrMsg = sqlite3_mprintf(
"Delimiter must be 1 character long, got %d characters", nByte);
return SQLITE_ERROR;
}
const char *s = (const char *)sqlite3_value_text(argv[i]);
delim = s[0];
break;
}
}
}
pCur->curLineContents = 0;
pCur->curLineLength =
getdelim(&pCur->curLineContents, &pCur->curLineLen, delim, pCur->fp);
pCur->iRowid = 1;
pCur->delim = delim;
pCur->idxNum = idxNum;
if (pCur->idxNum == LINES_IDXNUM_ROWID_EQ) {
pCur->rowid_eq_yielded = 0;
while (pCur->iRowid < targetRowid && pCur->curLineLength >= 0) {
pCur->curLineLength =
getdelim(&pCur->curLineContents, &pCur->curLineLen, delim, pCur->fp);
pCur->iRowid++;
}
}
return SQLITE_OK;
}
static int linesReadConnect(sqlite3 *db, void *pUnused, int argcUnused,
const char *const *argvUnused,
sqlite3_vtab **ppVtab, char **pzErrUnused) {
sqlite3_vtab *pNew;
int rc;
(void)pUnused;
(void)argcUnused;
(void)argvUnused;
(void)pzErrUnused;
// only difference is schema, uses "path" instead of "document"
rc = sqlite3_declare_vtab(db, "CREATE TABLE x(line text,"
"path hidden, delimiter hidden)");
if (rc == SQLITE_OK) {
pNew = *ppVtab = sqlite3_malloc(sizeof(*pNew));
if (pNew == 0)
return SQLITE_NOMEM;
memset(pNew, 0, sizeof(*pNew));
sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
}
return rc;
}
static sqlite3_module linesModule = {
0, /* iVersion */
0, /* xCreate */
linesConnect, /* xConnect */
linesBestIndex, /* xBestIndex */
linesDisconnect, /* xDisconnect */
0, /* xDestroy */
linesOpen, /* xOpen - open a cursor */
linesClose, /* xClose - close a cursor */
linesFilter, /* xFilter - configure scan constraints */
linesNext, /* xNext - advance a cursor */
linesEof, /* xEof - check for end of scan */
linesColumn, /* xColumn - read data */
linesRowid, /* xRowid - read data */
0, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
0 /* xShadowName */
};
static sqlite3_module linesReadModule = {
0, /* iVersion */
0, /* xCreate */
linesReadConnect, /* xConnect */
linesBestIndex, /* xBestIndex */
linesDisconnect, /* xDisconnect */
0, /* xDestroy */
linesOpen, /* xOpen - open a cursor */
linesClose, /* xClose - close a cursor */
linesReadFilter, /* xFilter - configure scan constraints */
linesNext, /* xNext - advance a cursor */
linesEof, /* xEof - check for end of scan */
linesColumn, /* xColumn - read data */
linesRowid, /* xRowid - read data */
0, /* xUpdate */
0, /* xBegin */
0, /* xSync */
0, /* xCommit */
0, /* xRollback */
0, /* xFindMethod */
0, /* xRename */
0, /* xSavepoint */
0, /* xRelease */
0, /* xRollbackTo */
0 /* xShadowName */
};
#pragma endregion
#pragma region entry points
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_lines_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi) {
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
int flags = SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC;
const char *debug = sqlite3_mprintf(
"Version: %s\nDate: %s\nSource: %s", SQLITE_LINES_VERSION,
SQLITE_LINES_DATE, SQLITE_LINES_SOURCE);
if (rc == SQLITE_OK)
rc = sqlite3_create_function_v2(db, "lines_version", 0, flags,
(void *)SQLITE_LINES_VERSION,
linesVersionFunc, 0, 0, 0);
if (rc == SQLITE_OK)
rc = sqlite3_create_function_v2(db, "lines_debug", 0, flags, (void *)debug,
linesDebugFunc, 0, 0, sqlite3_free);
if (rc == SQLITE_OK)
rc = sqlite3_create_module(db, "lines", &linesModule, 0);
if (rc == SQLITE_OK)
rc = sqlite3_create_module(db, "lines_read", &linesReadModule, 0);
return rc;
}
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_lines_no_read_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi) {
int rc = SQLITE_OK;
SQLITE_EXTENSION_INIT2(pApi);
(void)pzErrMsg; /* Unused parameter */
int flags = SQLITE_UTF8 | SQLITE_INNOCUOUS | SQLITE_DETERMINISTIC;
const char *debug = sqlite3_mprintf(
"Version: %s\nDate: %s\nSource: %s\nNO FILESYSTEM", SQLITE_LINES_VERSION,
SQLITE_LINES_DATE, SQLITE_LINES_SOURCE);
if (rc == SQLITE_OK)
rc = sqlite3_create_function_v2(db, "lines_version", 0, flags,
(void *)SQLITE_LINES_VERSION,
linesVersionFunc, 0, 0, 0);
if (rc == SQLITE_OK)
rc = sqlite3_create_function_v2(db, "lines_debug", 0, flags, (void *)debug,
linesDebugFunc, 0, 0, sqlite3_free);
if (rc == SQLITE_OK)
rc = sqlite3_create_module(db, "lines", &linesModule, 0);
return rc;
}
#pragma endregion