Fix harmless compiler warnings seen with MSVC for lsm1.
[sqlite.git] / ext / lsm1 / lsm_vtab.c
blob08556211604b6926cf61deef3429c126a18f1301
1 /*
2 ** 2015-11-16
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 *************************************************************************
13 ** This file implements a simple virtual table wrapper around the LSM
14 ** storage engine from SQLite4.
16 #include "sqlite3ext.h"
17 SQLITE_EXTENSION_INIT1
18 #include "lsm.h"
19 #include <assert.h>
20 #include <string.h>
22 /* Forward declaration of subclasses of virtual table objects */
23 typedef struct lsm1_vtab lsm1_vtab;
24 typedef struct lsm1_cursor lsm1_cursor;
26 /* Primitive types */
27 typedef unsigned char u8;
29 /* An open connection to an LSM table */
30 struct lsm1_vtab {
31 sqlite3_vtab base; /* Base class - must be first */
32 lsm_db *pDb; /* Open connection to the LSM table */
36 /* lsm1_cursor is a subclass of sqlite3_vtab_cursor which will
37 ** serve as the underlying representation of a cursor that scans
38 ** over rows of the result
40 struct lsm1_cursor {
41 sqlite3_vtab_cursor base; /* Base class - must be first */
42 lsm_cursor *pLsmCur; /* The LSM cursor */
43 u8 isDesc; /* 0: scan forward. 1: scan reverse */
44 u8 atEof; /* True if the scan is complete */
45 u8 bUnique; /* True if no more than one row of output */
49 ** The lsm1Connect() method is invoked to create a new
50 ** lsm1_vtab that describes the virtual table.
52 static int lsm1Connect(
53 sqlite3 *db,
54 void *pAux,
55 int argc, const char *const*argv,
56 sqlite3_vtab **ppVtab,
57 char **pzErr
59 lsm1_vtab *pNew;
60 int rc;
62 if( argc!=4 || argv[3]==0 || argv[3][0]==0 ){
63 *pzErr = sqlite3_mprintf("filename argument missing");
64 return SQLITE_ERROR;
66 *ppVtab = sqlite3_malloc( sizeof(*pNew) );
67 pNew = (lsm1_vtab*)*ppVtab;
68 if( pNew==0 ){
69 return SQLITE_NOMEM;
71 memset(pNew, 0, sizeof(*pNew));
72 rc = lsm_new(0, &pNew->pDb);
73 if( rc ){
74 *pzErr = sqlite3_mprintf("lsm_new failed with error code %d", rc);
75 rc = SQLITE_ERROR;
76 goto connect_failed;
78 rc = lsm_open(pNew->pDb, argv[3]);
79 if( rc ){
80 *pzErr = sqlite3_mprintf("lsm_open failed with %d", rc);
81 rc = SQLITE_ERROR;
82 goto connect_failed;
85 /* Column numbers */
86 #define LSM1_COLUMN_KEY 0
87 #define LSM1_COLUMN_BLOBKEY 1
88 #define LSM1_COLUMN_VALUE 2
89 #define LSM1_COLUMN_BLOBVALUE 3
90 #define LSM1_COLUMN_COMMAND 4
92 rc = sqlite3_declare_vtab(db,
93 "CREATE TABLE x("
94 " key," /* The primary key. Any non-NULL */
95 " blobkey," /* Pure BLOB primary key */
96 " value," /* The value associated with key. Any non-NULL */
97 " blobvalue," /* Pure BLOB value */
98 " command hidden" /* Insert here for control operations */
99 ");"
101 connect_failed:
102 if( rc!=SQLITE_OK ){
103 if( pNew ){
104 if( pNew->pDb ) lsm_close(pNew->pDb);
105 sqlite3_free(pNew);
107 *ppVtab = 0;
109 return rc;
113 ** This method is the destructor for lsm1_cursor objects.
115 static int lsm1Disconnect(sqlite3_vtab *pVtab){
116 lsm1_vtab *p = (lsm1_vtab*)pVtab;
117 lsm_close(p->pDb);
118 sqlite3_free(p);
119 return SQLITE_OK;
123 ** Constructor for a new lsm1_cursor object.
125 static int lsm1Open(sqlite3_vtab *pVtab, sqlite3_vtab_cursor **ppCursor){
126 lsm1_vtab *p = (lsm1_vtab*)pVtab;
127 lsm1_cursor *pCur;
128 int rc;
129 pCur = sqlite3_malloc( sizeof(*pCur) );
130 if( pCur==0 ) return SQLITE_NOMEM;
131 memset(pCur, 0, sizeof(*pCur));
132 *ppCursor = &pCur->base;
133 rc = lsm_csr_open(p->pDb, &pCur->pLsmCur);
134 if( rc==LSM_OK ){
135 rc = SQLITE_OK;
136 }else{
137 sqlite3_free(pCur);
138 *ppCursor = 0;
139 rc = SQLITE_ERROR;
141 return rc;
145 ** Destructor for a lsm1_cursor.
147 static int lsm1Close(sqlite3_vtab_cursor *cur){
148 lsm1_cursor *pCur = (lsm1_cursor*)cur;
149 lsm_csr_close(pCur->pLsmCur);
150 sqlite3_free(pCur);
151 return SQLITE_OK;
156 ** Advance a lsm1_cursor to its next row of output.
158 static int lsm1Next(sqlite3_vtab_cursor *cur){
159 lsm1_cursor *pCur = (lsm1_cursor*)cur;
160 int rc = LSM_OK;
161 if( pCur->bUnique ){
162 pCur->atEof = 1;
163 }else{
164 if( pCur->isDesc ){
165 rc = lsm_csr_prev(pCur->pLsmCur);
166 }else{
167 rc = lsm_csr_next(pCur->pLsmCur);
169 if( rc==LSM_OK && lsm_csr_valid(pCur->pLsmCur)==0 ){
170 pCur->atEof = 1;
173 return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
177 ** Return TRUE if the cursor has been moved off of the last
178 ** row of output.
180 static int lsm1Eof(sqlite3_vtab_cursor *cur){
181 lsm1_cursor *pCur = (lsm1_cursor*)cur;
182 return pCur->atEof;
186 ** Rowids are not supported by the underlying virtual table. So always
187 ** return 0 for the rowid.
189 static int lsm1Rowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
190 *pRowid = 0;
191 return SQLITE_OK;
195 ** Type prefixes on LSM keys
197 #define LSM1_TYPE_NEGATIVE 0
198 #define LSM1_TYPE_POSITIVE 1
199 #define LSM1_TYPE_TEXT 2
200 #define LSM1_TYPE_BLOB 3
203 ** Write a 32-bit unsigned integer as 4 big-endian bytes.
205 static void varintWrite32(unsigned char *z, unsigned int y){
206 z[0] = (unsigned char)(y>>24);
207 z[1] = (unsigned char)(y>>16);
208 z[2] = (unsigned char)(y>>8);
209 z[3] = (unsigned char)(y);
213 ** Write a varint into z[]. The buffer z[] must be at least 9 characters
214 ** long to accommodate the largest possible varint. Return the number of
215 ** bytes of z[] used.
217 static int lsm1PutVarint64(unsigned char *z, sqlite3_uint64 x){
218 unsigned int w, y;
219 if( x<=240 ){
220 z[0] = (unsigned char)x;
221 return 1;
223 if( x<=2287 ){
224 y = (unsigned int)(x - 240);
225 z[0] = (unsigned char)(y/256 + 241);
226 z[1] = (unsigned char)(y%256);
227 return 2;
229 if( x<=67823 ){
230 y = (unsigned int)(x - 2288);
231 z[0] = 249;
232 z[1] = (unsigned char)(y/256);
233 z[2] = (unsigned char)(y%256);
234 return 3;
236 y = (unsigned int)x;
237 w = (unsigned int)(x>>32);
238 if( w==0 ){
239 if( y<=16777215 ){
240 z[0] = 250;
241 z[1] = (unsigned char)(y>>16);
242 z[2] = (unsigned char)(y>>8);
243 z[3] = (unsigned char)(y);
244 return 4;
246 z[0] = 251;
247 varintWrite32(z+1, y);
248 return 5;
250 if( w<=255 ){
251 z[0] = 252;
252 z[1] = (unsigned char)w;
253 varintWrite32(z+2, y);
254 return 6;
256 if( w<=65535 ){
257 z[0] = 253;
258 z[1] = (unsigned char)(w>>8);
259 z[2] = (unsigned char)w;
260 varintWrite32(z+3, y);
261 return 7;
263 if( w<=16777215 ){
264 z[0] = 254;
265 z[1] = (unsigned char)(w>>16);
266 z[2] = (unsigned char)(w>>8);
267 z[3] = (unsigned char)w;
268 varintWrite32(z+4, y);
269 return 8;
271 z[0] = 255;
272 varintWrite32(z+1, w);
273 varintWrite32(z+5, y);
274 return 9;
278 ** Decode the varint in the first n bytes z[]. Write the integer value
279 ** into *pResult and return the number of bytes in the varint.
281 ** If the decode fails because there are not enough bytes in z[] then
282 ** return 0;
284 static int lsm1GetVarint64(
285 const unsigned char *z,
286 int n,
287 sqlite3_uint64 *pResult
289 unsigned int x;
290 if( n<1 ) return 0;
291 if( z[0]<=240 ){
292 *pResult = z[0];
293 return 1;
295 if( z[0]<=248 ){
296 if( n<2 ) return 0;
297 *pResult = (z[0]-241)*256 + z[1] + 240;
298 return 2;
300 if( n<z[0]-246 ) return 0;
301 if( z[0]==249 ){
302 *pResult = 2288 + 256*z[1] + z[2];
303 return 3;
305 if( z[0]==250 ){
306 *pResult = (z[1]<<16) + (z[2]<<8) + z[3];
307 return 4;
309 x = (z[1]<<24) + (z[2]<<16) + (z[3]<<8) + z[4];
310 if( z[0]==251 ){
311 *pResult = x;
312 return 5;
314 if( z[0]==252 ){
315 *pResult = (((sqlite3_uint64)x)<<8) + z[5];
316 return 6;
318 if( z[0]==253 ){
319 *pResult = (((sqlite3_uint64)x)<<16) + (z[5]<<8) + z[6];
320 return 7;
322 if( z[0]==254 ){
323 *pResult = (((sqlite3_uint64)x)<<24) + (z[5]<<16) + (z[6]<<8) + z[7];
324 return 8;
326 *pResult = (((sqlite3_uint64)x)<<32) +
327 (0xffffffff & ((z[5]<<24) + (z[6]<<16) + (z[7]<<8) + z[8]));
328 return 9;
332 ** Generate a key encoding for pValue such that all keys compare in
333 ** lexicographical order. Return an SQLite error code or SQLITE_OK.
335 ** The key encoding is *pnKey bytes in length written into *ppKey.
336 ** Space to hold the key is taken from pSpace if sufficient, or else
337 ** from sqlite3_malloc(). The caller is responsible for freeing malloced
338 ** space.
340 static int lsm1EncodeKey(
341 sqlite3_value *pValue, /* Value to be encoded */
342 unsigned char **ppKey, /* Write the encoding here */
343 int *pnKey, /* Write the size of the encoding here */
344 unsigned char *pSpace, /* Use this space if it is large enough */
345 int nSpace /* Size of pSpace[] */
347 int eType = sqlite3_value_type(pValue);
348 *ppKey = 0;
349 *pnKey = 0;
350 assert( nSpace>=32 );
351 switch( eType ){
352 default: {
353 return SQLITE_ERROR; /* We cannot handle NULL keys */
355 case SQLITE_BLOB:
356 case SQLITE_TEXT: {
357 int nVal = sqlite3_value_bytes(pValue);
358 const void *pVal;
359 if( eType==SQLITE_BLOB ){
360 eType = LSM1_TYPE_BLOB;
361 pVal = sqlite3_value_blob(pValue);
362 }else{
363 eType = LSM1_TYPE_TEXT;
364 pVal = (const void*)sqlite3_value_text(pValue);
365 if( pVal==0 ) return SQLITE_NOMEM;
367 if( nVal+1>nSpace ){
368 pSpace = sqlite3_malloc( nVal+1 );
369 if( pSpace==0 ) return SQLITE_NOMEM;
371 pSpace[0] = (unsigned char)eType;
372 memcpy(&pSpace[1], pVal, nVal);
373 *ppKey = pSpace;
374 *pnKey = nVal+1;
375 break;
377 case SQLITE_INTEGER: {
378 sqlite3_int64 iVal = sqlite3_value_int64(pValue);
379 sqlite3_uint64 uVal;
380 if( iVal<0 ){
381 if( iVal==0xffffffffffffffffLL ) return SQLITE_ERROR;
382 uVal = *(sqlite3_uint64*)&iVal;
383 eType = LSM1_TYPE_NEGATIVE;
384 }else{
385 uVal = iVal;
386 eType = LSM1_TYPE_POSITIVE;
388 pSpace[0] = (unsigned char)eType;
389 *ppKey = pSpace;
390 *pnKey = 1 + lsm1PutVarint64(&pSpace[1], uVal);
393 return SQLITE_OK;
397 ** Return values of columns for the row at which the lsm1_cursor
398 ** is currently pointing.
400 static int lsm1Column(
401 sqlite3_vtab_cursor *cur, /* The cursor */
402 sqlite3_context *ctx, /* First argument to sqlite3_result_...() */
403 int i /* Which column to return */
405 lsm1_cursor *pCur = (lsm1_cursor*)cur;
406 switch( i ){
407 case LSM1_COLUMN_BLOBKEY: {
408 const void *pVal;
409 int nVal;
410 if( lsm_csr_key(pCur->pLsmCur, &pVal, &nVal)==LSM_OK ){
411 sqlite3_result_blob(ctx, pVal, nVal, SQLITE_TRANSIENT);
413 break;
415 case LSM1_COLUMN_KEY: {
416 const unsigned char *pVal;
417 int nVal;
418 if( lsm_csr_key(pCur->pLsmCur, (const void**)&pVal, &nVal)==LSM_OK
419 && nVal>=1
421 if( pVal[0]==LSM1_TYPE_BLOB ){
422 sqlite3_result_blob(ctx, (const void*)&pVal[1],nVal-1,
423 SQLITE_TRANSIENT);
424 }else if( pVal[0]==LSM1_TYPE_TEXT ){
425 sqlite3_result_text(ctx, (const char*)&pVal[1],nVal-1,
426 SQLITE_TRANSIENT);
427 }else if( nVal>=2 && nVal<=10 &&
428 (pVal[0]==LSM1_TYPE_POSITIVE || pVal[0]==LSM1_TYPE_NEGATIVE)
430 sqlite3_int64 iVal;
431 lsm1GetVarint64(pVal+1, nVal-1, (sqlite3_uint64*)&iVal);
432 sqlite3_result_int64(ctx, iVal);
435 break;
437 case LSM1_COLUMN_BLOBVALUE: {
438 const void *pVal;
439 int nVal;
440 if( lsm_csr_value(pCur->pLsmCur, (const void**)&pVal, &nVal)==LSM_OK ){
441 sqlite3_result_blob(ctx, pVal, nVal, SQLITE_TRANSIENT);
443 break;
445 case LSM1_COLUMN_VALUE: {
446 const unsigned char *aVal;
447 int nVal;
448 if( lsm_csr_value(pCur->pLsmCur, (const void**)&aVal, &nVal)==LSM_OK
449 && nVal>=1
451 switch( aVal[0] ){
452 case SQLITE_FLOAT:
453 case SQLITE_INTEGER: {
454 sqlite3_uint64 x = 0;
455 int j;
456 for(j=1; j<nVal; j++){
457 x = (x<<8) | aVal[j];
459 if( aVal[0]==SQLITE_INTEGER ){
460 sqlite3_result_int64(ctx, *(sqlite3_int64*)&x);
461 }else{
462 double r;
463 assert( sizeof(r)==sizeof(x) );
464 memcpy(&r, &x, sizeof(r));
465 sqlite3_result_double(ctx, r);
467 break;
469 case SQLITE_TEXT: {
470 sqlite3_result_text(ctx, (char*)&aVal[1], nVal-1, SQLITE_TRANSIENT);
471 break;
473 case SQLITE_BLOB: {
474 sqlite3_result_blob(ctx, &aVal[1], nVal-1, SQLITE_TRANSIENT);
475 break;
479 break;
481 default: {
482 break;
485 return SQLITE_OK;
488 /* Move to the first row to return.
490 static int lsm1Filter(
491 sqlite3_vtab_cursor *pVtabCursor,
492 int idxNum, const char *idxStr,
493 int argc, sqlite3_value **argv
495 lsm1_cursor *pCur = (lsm1_cursor *)pVtabCursor;
496 int rc = LSM_OK;
497 pCur->atEof = 1;
498 if( idxNum==1 ){
499 assert( argc==1 );
500 pCur->isDesc = 0;
501 pCur->bUnique = 1;
502 if( sqlite3_value_type(argv[0])==SQLITE_BLOB ){
503 const void *pVal = sqlite3_value_blob(argv[0]);
504 int nVal = sqlite3_value_bytes(argv[0]);
505 rc = lsm_csr_seek(pCur->pLsmCur, pVal, nVal, LSM_SEEK_EQ);
507 }else{
508 rc = lsm_csr_first(pCur->pLsmCur);
509 pCur->isDesc = 0;
510 pCur->bUnique = 0;
512 if( rc==LSM_OK && lsm_csr_valid(pCur->pLsmCur)!=0 ){
513 pCur->atEof = 0;
515 return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
519 ** Only comparisons against the key are allowed. The idxNum defines
520 ** which comparisons are available:
522 ** 0 Full table scan only
523 ** bit 1 key==?1 single argument for ?1
524 ** bit 2 key>?1
525 ** bit 3 key>=?1
526 ** bit 4 key<?N (N==1 if bits 2,3 clear, or 2 if bits2,3 set)
527 ** bit 5 key<=?N (N==1 if bits 2,3 clear, or 2 if bits2,3 set)
528 ** bit 6 Use blobkey instead of key
530 ** To put it another way:
532 ** 0 Full table scan.
533 ** 1 key==?1
534 ** 2 key>?1
535 ** 4 key>=?1
536 ** 8 key<?1
537 ** 10 key>?1 AND key<?2
538 ** 12 key>=?1 AND key<?2
539 ** 16 key<=?1
540 ** 18 key>?1 AND key<=?2
541 ** 20 key>=?1 AND key<=?2
542 ** 33..52 Use blobkey in place of key...
544 static int lsm1BestIndex(
545 sqlite3_vtab *tab,
546 sqlite3_index_info *pIdxInfo
548 int i; /* Loop over constraints */
549 int idxNum = 0; /* The query plan bitmask */
550 int nArg = 0; /* Number of arguments to xFilter */
551 int eqIdx = -1; /* Index of the key== constraint, or -1 if none */
553 const struct sqlite3_index_constraint *pConstraint;
554 pConstraint = pIdxInfo->aConstraint;
555 for(i=0; i<pIdxInfo->nConstraint && idxNum<16; i++, pConstraint++){
556 if( pConstraint->usable==0 ) continue;
557 if( pConstraint->iColumn!=LSM1_COLUMN_KEY ) continue;
558 if( pConstraint->op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
559 switch( pConstraint->op ){
560 case SQLITE_INDEX_CONSTRAINT_EQ: {
561 eqIdx = i;
562 idxNum = 1;
563 break;
567 if( eqIdx>=0 ){
568 pIdxInfo->aConstraintUsage[eqIdx].argvIndex = ++nArg;
569 pIdxInfo->aConstraintUsage[eqIdx].omit = 1;
571 if( idxNum==1 ){
572 pIdxInfo->estimatedCost = (double)1;
573 pIdxInfo->estimatedRows = 1;
574 pIdxInfo->orderByConsumed = 1;
575 }else{
576 /* Full table scan */
577 pIdxInfo->estimatedCost = (double)2147483647;
578 pIdxInfo->estimatedRows = 2147483647;
580 pIdxInfo->idxNum = idxNum;
581 return SQLITE_OK;
585 ** The xUpdate method is normally used for INSERT, REPLACE, UPDATE, and
586 ** DELETE. But this virtual table only supports INSERT and REPLACE.
587 ** DELETE is accomplished by inserting a record with a value of NULL.
588 ** UPDATE is achieved by using REPLACE.
590 int lsm1Update(
591 sqlite3_vtab *pVTab,
592 int argc,
593 sqlite3_value **argv,
594 sqlite_int64 *pRowid
596 lsm1_vtab *p = (lsm1_vtab*)pVTab;
597 const void *pKey;
598 int nKey;
599 int eType;
600 int rc = LSM_OK;
601 sqlite3_value *pValue;
602 const unsigned char *pVal;
603 unsigned char *pData;
604 int nVal;
605 unsigned char pSpace[100];
607 if( argc==1 ){
608 pVTab->zErrMsg = sqlite3_mprintf("cannot DELETE");
609 return SQLITE_ERROR;
611 if( sqlite3_value_type(argv[0])!=SQLITE_NULL ){
612 pVTab->zErrMsg = sqlite3_mprintf("cannot UPDATE");
613 return SQLITE_ERROR;
616 /* "INSERT INTO tab(command) VALUES('....')" is used to implement
617 ** special commands.
619 if( sqlite3_value_type(argv[2+LSM1_COLUMN_COMMAND])!=SQLITE_NULL ){
620 return SQLITE_OK;
622 if( sqlite3_value_type(argv[2+LSM1_COLUMN_BLOBKEY])==SQLITE_BLOB ){
623 /* Use the blob key exactly as supplied */
624 pKey = sqlite3_value_blob(argv[2+LSM1_COLUMN_BLOBKEY]);
625 nKey = sqlite3_value_bytes(argv[2+LSM1_COLUMN_BLOBKEY]);
626 }else{
627 /* Use a key encoding that sorts in lexicographical order */
628 rc = lsm1EncodeKey(argv[2+LSM1_COLUMN_KEY],
629 (unsigned char**)&pKey,&nKey,
630 pSpace,sizeof(pSpace));
631 if( rc ) return rc;
633 if( sqlite3_value_type(argv[2+LSM1_COLUMN_BLOBVALUE])==SQLITE_BLOB ){
634 pVal = sqlite3_value_blob(argv[2+LSM1_COLUMN_BLOBVALUE]);
635 nVal = sqlite3_value_bytes(argv[2+LSM1_COLUMN_BLOBVALUE]);
636 rc = lsm_insert(p->pDb, pKey, nKey, pVal, nVal);
637 }else{
638 pValue = argv[2+LSM1_COLUMN_VALUE];
639 eType = sqlite3_value_type(pValue);
640 switch( eType ){
641 case SQLITE_NULL: {
642 rc = lsm_delete(p->pDb, pKey, nKey);
643 break;
645 case SQLITE_BLOB:
646 case SQLITE_TEXT: {
647 if( eType==SQLITE_TEXT ){
648 pVal = sqlite3_value_text(pValue);
649 }else{
650 pVal = (unsigned char*)sqlite3_value_blob(pValue);
652 nVal = sqlite3_value_bytes(pValue);
653 pData = sqlite3_malloc( nVal+1 );
654 if( pData==0 ){
655 rc = SQLITE_NOMEM;
656 }else{
657 pData[0] = (unsigned char)eType;
658 memcpy(&pData[1], pVal, nVal);
659 rc = lsm_insert(p->pDb, pKey, nKey, pData, nVal+1);
660 sqlite3_free(pData);
662 break;
664 case SQLITE_INTEGER:
665 case SQLITE_FLOAT: {
666 sqlite3_uint64 x;
667 unsigned char aVal[9];
668 int i;
669 if( eType==SQLITE_INTEGER ){
670 *(sqlite3_int64*)&x = sqlite3_value_int64(pValue);
671 }else{
672 double r = sqlite3_value_double(pValue);
673 assert( sizeof(r)==sizeof(x) );
674 memcpy(&x, &r, sizeof(r));
676 for(i=8; x>0 && i>=1; i--){
677 aVal[i] = x & 0xff;
678 x >>= 8;
680 aVal[i] = (unsigned char)eType;
681 rc = lsm_insert(p->pDb, pKey, nKey, &aVal[i], 9-i);
682 break;
686 if( pKey!=(const void*)pSpace ) sqlite3_free((void*)pKey);
687 return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
690 /* Begin a transaction
692 static int lsm1Begin(sqlite3_vtab *pVtab){
693 lsm1_vtab *p = (lsm1_vtab*)pVtab;
694 int rc = lsm_begin(p->pDb, 1);
695 return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
698 /* Phase 1 of a transaction commit.
700 static int lsm1Sync(sqlite3_vtab *pVtab){
701 return SQLITE_OK;
704 /* Commit a transaction
706 static int lsm1Commit(sqlite3_vtab *pVtab){
707 lsm1_vtab *p = (lsm1_vtab*)pVtab;
708 int rc = lsm_commit(p->pDb, 0);
709 return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
712 /* Rollback a transaction
714 static int lsm1Rollback(sqlite3_vtab *pVtab){
715 lsm1_vtab *p = (lsm1_vtab*)pVtab;
716 int rc = lsm_rollback(p->pDb, 0);
717 return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
721 ** This following structure defines all the methods for the
722 ** generate_lsm1 virtual table.
724 static sqlite3_module lsm1Module = {
725 0, /* iVersion */
726 lsm1Connect, /* xCreate */
727 lsm1Connect, /* xConnect */
728 lsm1BestIndex, /* xBestIndex */
729 lsm1Disconnect, /* xDisconnect */
730 lsm1Disconnect, /* xDestroy */
731 lsm1Open, /* xOpen - open a cursor */
732 lsm1Close, /* xClose - close a cursor */
733 lsm1Filter, /* xFilter - configure scan constraints */
734 lsm1Next, /* xNext - advance a cursor */
735 lsm1Eof, /* xEof - check for end of scan */
736 lsm1Column, /* xColumn - read data */
737 lsm1Rowid, /* xRowid - read data */
738 lsm1Update, /* xUpdate */
739 lsm1Begin, /* xBegin */
740 lsm1Sync, /* xSync */
741 lsm1Commit, /* xCommit */
742 lsm1Rollback, /* xRollback */
743 0, /* xFindMethod */
744 0, /* xRename */
748 #ifdef _WIN32
749 __declspec(dllexport)
750 #endif
751 int sqlite3_lsm_init(
752 sqlite3 *db,
753 char **pzErrMsg,
754 const sqlite3_api_routines *pApi
756 int rc = SQLITE_OK;
757 SQLITE_EXTENSION_INIT2(pApi);
758 rc = sqlite3_create_module(db, "lsm1", &lsm1Module, 0);
759 return rc;