SQLite

Check-in [5c79f53131]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Work toward more flexible typing for keys and values.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | lsm-vtab
Files: files | file ages | folders
SHA1: 5c79f53131a7ab3c83f49e35a5021a6cdb2518fc
User & Date: drh 2015-11-19 19:27:07.806
Context
2015-11-19
19:31
Merge the latest enhancements from trunk. (check-in: 8aede091c4 user: drh tags: lsm-vtab)
19:27
Work toward more flexible typing for keys and values. (check-in: 5c79f53131 user: drh tags: lsm-vtab)
2015-11-17
02:23
Basic functionality is now working. (check-in: aa129c51ec user: drh tags: lsm-vtab)
Changes
Unified Diff Ignore Whitespace Patch
Changes to ext/lsm1/lsm_vtab.c.
79
80
81
82
83
84
85
86
87
88


89
90
91







92
93
94
95
96
97
98
  if( rc ){
    *pzErr = sqlite3_mprintf("lsm_open failed with %d", rc);
    rc = SQLITE_ERROR;
    goto connect_failed;
  }

/* Column numbers */
#define LSM1_COLUMN_KEY      0
#define LSM1_COLUMN_VALUE    1
#define LSM1_COLUMN_COMMAND  2



  rc = sqlite3_declare_vtab(db,
     "CREATE TABLE x(key,value,command hidden)");







connect_failed:
  if( rc!=SQLITE_OK ){
    if( pNew ){
      if( pNew->pDb ) lsm_close(pNew->pDb);
      sqlite3_free(pNew);
    }
    *ppVtab = 0;







|
|
|
>
>


|
>
>
>
>
>
>
>







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
  if( rc ){
    *pzErr = sqlite3_mprintf("lsm_open failed with %d", rc);
    rc = SQLITE_ERROR;
    goto connect_failed;
  }

/* Column numbers */
#define LSM1_COLUMN_KEY         0
#define LSM1_COLUMN_BLOBKEY     1
#define LSM1_COLUMN_VALUE       2
#define LSM1_COLUMN_BLOBVALUE   3
#define LSM1_COLUMN_COMMAND     4

  rc = sqlite3_declare_vtab(db,
     "CREATE TABLE x("
     "  key,"              /* The primary key.  Any non-NULL */
     "  blobkey,"          /* Pure BLOB primary key */
     "  value,"            /* The value associated with key.  Any non-NULL */
     "  blobvalue,"        /* Pure BLOB value */
     "  command hidden"    /* Insert here for control operations */
     ");"
  );
connect_failed:
  if( rc!=SQLITE_OK ){
    if( pNew ){
      if( pNew->pDb ) lsm_close(pNew->pDb);
      sqlite3_free(pNew);
    }
    *ppVtab = 0;
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
** Return TRUE if the cursor has been moved off of the last
** row of output.
*/
static int lsm1Eof(sqlite3_vtab_cursor *cur){
  lsm1_cursor *pCur = (lsm1_cursor*)cur;
  return pCur->atEof;
}



































































/*
** Return values of columns for the row at which the lsm1_cursor
** is currently pointing.
*/
static int lsm1Column(
  sqlite3_vtab_cursor *cur,   /* The cursor */
  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
  int i                       /* Which column to return */
){
  lsm1_cursor *pCur = (lsm1_cursor*)cur;
  switch( i ){
    case LSM1_COLUMN_KEY: {
      const void *pVal;
      int nVal;








      if( lsm_csr_key(pCur->pLsmCur, (const void**)&pVal, &nVal)==LSM_OK ){
















        sqlite3_result_blob(ctx, pVal, nVal, SQLITE_TRANSIENT);
      }
      break;
    }
    case LSM1_COLUMN_VALUE: {
      const unsigned char *aVal;
      int nVal;
      if( lsm_csr_value(pCur->pLsmCur, (const void**)&aVal, &nVal)==LSM_OK
          && nVal>=1
      ){
        switch( aVal[0] ){
          case SQLITE_FLOAT:
          case SQLITE_INTEGER: {
            sqlite3_uint64 x = 0;
            int j;
            for(j=1; j<=8; j++){
              x = (x<<8) | aVal[j];
            }
            if( aVal[0]==SQLITE_INTEGER ){
              sqlite3_result_int64(ctx, *(sqlite3_int64*)&x);
            }else{
              sqlite3_result_double(ctx, *(double*)&x);
            }







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>












|


>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>















|







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
** Return TRUE if the cursor has been moved off of the last
** row of output.
*/
static int lsm1Eof(sqlite3_vtab_cursor *cur){
  lsm1_cursor *pCur = (lsm1_cursor*)cur;
  return pCur->atEof;
}

/*
** Rowids are not supported by the underlying virtual table.  So always
** return 0 for the rowid.
*/
static int lsm1Rowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  *pRowid = 0;
  return SQLITE_OK;
}

/*
** Type prefixes on LSM keys
*/
#define LSM1_TYPE_NEGATIVE   0
#define LSM1_TYPE_POSITIVE   1
#define LSM1_TYPE_TEXT       2
#define LSM1_TYPE_BLOB       3

/*
** Generate a key encoding for pValue such that all keys compare in
** lexicographical order.  Return an SQLite error code or SQLITE_OK.
**
** The key encoding is *pnKey bytes in length written into *ppKey.
** Space to hold the key is taken from pSpace if sufficient, or else
** from sqlite3_malloc().  The caller is responsible for freeing malloced
** space.
*/
static int lsm1EncodeKey(
  sqlite3_value *pValue,     /* Value to be encoded */
  unsigned char **ppKey,     /* Write the encoding here */
  int *pnKey,                /* Write the size of the encoding here */
  unsigned char *pSpace,     /* Use this space if it is large enough */
  int nSpace                 /* Size of pSpace[] */
){
  int eType = sqlite3_value_type(pValue);
  *ppKey = 0;
  *pnKey = 0;
  switch( eType ){
    default: {
      return SQLITE_ERROR;  /* We cannot handle NULL keys */
    }
    case SQLITE_BLOB:
    case SQLITE_TEXT: {
      int nVal = sqlite3_value_bytes(pValue);
      const void *pVal;
      if( eType==SQLITE_BLOB ){
        eType = LSM1_TYPE_BLOB;
        pVal = sqlite3_value_blob(pValue);
      }else{
        eType = LSM1_TYPE_TEXT;
        pVal = (const void*)sqlite3_value_text(pValue);
        if( pVal==0 ) return SQLITE_NOMEM;
      }
      if( nVal+1>nSpace ){
        pSpace = sqlite3_malloc( nVal+1 );
        if( pSpace==0 ) return SQLITE_NOMEM;
      }
      pSpace[0] = eType;
      memcpy(&pSpace[1], pVal, nVal);
      *ppKey = pSpace;
      *pnKey = nVal+1;
      break;
    }
  }
  return SQLITE_OK;
}

/*
** Return values of columns for the row at which the lsm1_cursor
** is currently pointing.
*/
static int lsm1Column(
  sqlite3_vtab_cursor *cur,   /* The cursor */
  sqlite3_context *ctx,       /* First argument to sqlite3_result_...() */
  int i                       /* Which column to return */
){
  lsm1_cursor *pCur = (lsm1_cursor*)cur;
  switch( i ){
    case LSM1_COLUMN_BLOBKEY: {
      const void *pVal;
      int nVal;
      if( lsm_csr_key(pCur->pLsmCur, &pVal, &nVal)==LSM_OK ){
        sqlite3_result_blob(ctx, pVal, nVal, SQLITE_TRANSIENT);
      }
      break;
    }
    case LSM1_COLUMN_KEY: {
      const unsigned char *pVal;
      int nVal;
      if( lsm_csr_key(pCur->pLsmCur, (const void**)&pVal, &nVal)==LSM_OK
       && nVal>=1
      ){
        if( pVal[0]==LSM1_TYPE_BLOB ){
          sqlite3_result_blob(ctx, (const void*)&pVal[1],nVal-1,
                              SQLITE_TRANSIENT);
        }else if( pVal[0]==LSM1_TYPE_TEXT ){
          sqlite3_result_text(ctx, (const char*)&pVal[1],nVal-1,
                              SQLITE_TRANSIENT);
        }
      }
      break;
    }
    case LSM1_COLUMN_BLOBVALUE: {
      const void *pVal;
      int nVal;
      if( lsm_csr_value(pCur->pLsmCur, (const void**)&pVal, &nVal)==LSM_OK ){
        sqlite3_result_blob(ctx, pVal, nVal, SQLITE_TRANSIENT);
      }
      break;
    }
    case LSM1_COLUMN_VALUE: {
      const unsigned char *aVal;
      int nVal;
      if( lsm_csr_value(pCur->pLsmCur, (const void**)&aVal, &nVal)==LSM_OK
          && nVal>=1
      ){
        switch( aVal[0] ){
          case SQLITE_FLOAT:
          case SQLITE_INTEGER: {
            sqlite3_uint64 x = 0;
            int j;
            for(j=1; j<nVal; j++){
              x = (x<<8) | aVal[j];
            }
            if( aVal[0]==SQLITE_INTEGER ){
              sqlite3_result_int64(ctx, *(sqlite3_int64*)&x);
            }else{
              sqlite3_result_double(ctx, *(double*)&x);
            }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
      }
      break;
    }
    default: {
      break;
    }
  }
  return SQLITE_OK;
}

/*
** Rowids are not supported by the underlying virtual table.  So always
** return 0 for the rowid.
*/
static int lsm1Rowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  *pRowid = 0;
  return SQLITE_OK;
}

/* Move to the first row to return.
*/
static int lsm1Filter(
  sqlite3_vtab_cursor *pVtabCursor, 







<
<
<
<
<
<
<
<
<







324
325
326
327
328
329
330









331
332
333
334
335
336
337
      }
      break;
    }
    default: {
      break;
    }
  }









  return SQLITE_OK;
}

/* Move to the first row to return.
*/
static int lsm1Filter(
  sqlite3_vtab_cursor *pVtabCursor, 
271
272
273
274
275
276
277
278
279





280













281
282
283
284
285
286
287
  return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
}

/*
** Only comparisons against the key are allowed.  The idxNum defines
** which comparisons are available:
**
**     0      Full table scan only
**     1      key==?  single argument for ?





**    













*/
static int lsm1BestIndex(
  sqlite3_vtab *tab,
  sqlite3_index_info *pIdxInfo
){
  int i;                 /* Loop over constraints */
  int idxNum = 0;        /* The query plan bitmask */







|
|
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>







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
  return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
}

/*
** Only comparisons against the key are allowed.  The idxNum defines
** which comparisons are available:
**
**     0        Full table scan only
**   bit 1      key==?1  single argument for ?1
**   bit 2      key>?1
**   bit 3      key>=?1
**   bit 4      key<?N   (N==1 if bits 2,3 clear, or 2 if bits2,3 set)
**   bit 5      key<=?N  (N==1 if bits 2,3 clear, or 2 if bits2,3 set)
**   bit 6      Use blobkey instead of key
**
** To put it another way:
**
**     0        Full table scan.
**     1        key==?1
**     2        key>?1
**     4        key>=?1
**     8        key<?1
**     10       key>?1 AND key<?2
**     12       key>=?1 AND key<?2
**     16       key<=?1
**     18       key>?1 AND key<=?2
**     20       key>=?1 AND key<=?2
**     33..52   Use blobkey in place of key...
*/
static int lsm1BestIndex(
  sqlite3_vtab *tab,
  sqlite3_index_info *pIdxInfo
){
  int i;                 /* Loop over constraints */
  int idxNum = 0;        /* The query plan bitmask */
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
){
  lsm1_vtab *p = (lsm1_vtab*)pVTab;
  const void *pKey;
  int nKey;
  int eType;
  int rc;
  sqlite3_value *pValue;





  if( argc==1 ){
    pVTab->zErrMsg = sqlite3_mprintf("cannot DELETE");
    return SQLITE_ERROR;
  }
  if( sqlite3_value_type(argv[0])!=SQLITE_NULL ){
    pVTab->zErrMsg = sqlite3_mprintf("cannot UPDATE");
    return SQLITE_ERROR;
  }

  /* "INSERT INTO tab(command) VALUES('....')" is used to implement
  ** special commands.
  */
  if( sqlite3_value_type(argv[2+LSM1_COLUMN_COMMAND])!=SQLITE_NULL ){
    return SQLITE_OK;
  }
  if( sqlite3_value_type(argv[2+LSM1_COLUMN_KEY])!=SQLITE_BLOB ){

    pVTab->zErrMsg = sqlite3_mprintf("BLOB keys only");






    return SQLITE_ERROR;
  }

  pKey = sqlite3_value_blob(argv[2+LSM1_COLUMN_KEY]);
  nKey = sqlite3_value_bytes(argv[2+LSM1_COLUMN_KEY]);


  pValue = argv[2+LSM1_COLUMN_VALUE];
  eType = sqlite3_value_type(pValue);
  switch( eType ){
    case SQLITE_NULL: {
      rc = lsm_delete(p->pDb, pKey, nKey);
      break;
    }
    case SQLITE_BLOB:
    case SQLITE_TEXT: {
      const unsigned char *pVal;
      unsigned char *pData;
      int nVal;
      if( eType==SQLITE_TEXT ){
        pVal = sqlite3_value_text(pValue);
      }else{
        pVal = (unsigned char*)sqlite3_value_blob(pValue);
      }
      nVal = sqlite3_value_bytes(pValue);
      pData = sqlite3_malloc( nVal+1 );
      if( pData==0 ){
        rc = SQLITE_NOMEM;
      }else{
        pData[0] = eType;
        memcpy(&pData[1], pVal, nVal);
        rc = lsm_insert(p->pDb, pKey, nKey, pData, nVal+1);
        sqlite3_free(pData);
      }
      break;
    }
    case SQLITE_INTEGER:
    case SQLITE_FLOAT: {
      sqlite3_uint64 x;
      unsigned char aVal[9];
      int i;
      if( eType==SQLITE_INTEGER ){
        *(sqlite3_int64*)&x = sqlite3_value_int64(pValue);
      }else{
        *(double*)&x = sqlite3_value_double(pValue);
      }
      for(i=8; i>=1; i--){
        aVal[i] = x & 0xff;
        x >>= 8;
      }
      aVal[0] = eType;
      rc = lsm_insert(p->pDb, pKey, nKey, aVal, 9);
      break;
    }
  }


  return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
}      

/* Begin a transaction
*/
static int lsm1Begin(sqlite3_vtab *pVtab){
  lsm1_vtab *p = (lsm1_vtab*)pVtab;







>
>
>
>
>















|
>
|
>
>
>
>
>
>
|

>
|
|
>
>
|
|
|
|
|
|
|
|
|
<
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>
>







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
){
  lsm1_vtab *p = (lsm1_vtab*)pVTab;
  const void *pKey;
  int nKey;
  int eType;
  int rc;
  sqlite3_value *pValue;
  const unsigned char *pVal;
  unsigned char *pData;
  int nVal;
  unsigned char pSpace[100];

  if( argc==1 ){
    pVTab->zErrMsg = sqlite3_mprintf("cannot DELETE");
    return SQLITE_ERROR;
  }
  if( sqlite3_value_type(argv[0])!=SQLITE_NULL ){
    pVTab->zErrMsg = sqlite3_mprintf("cannot UPDATE");
    return SQLITE_ERROR;
  }

  /* "INSERT INTO tab(command) VALUES('....')" is used to implement
  ** special commands.
  */
  if( sqlite3_value_type(argv[2+LSM1_COLUMN_COMMAND])!=SQLITE_NULL ){
    return SQLITE_OK;
  }
  if( sqlite3_value_type(argv[2+LSM1_COLUMN_BLOBKEY])==SQLITE_BLOB ){
    /* Use the blob key exactly as supplied */
    pKey = sqlite3_value_blob(argv[2+LSM1_COLUMN_BLOBKEY]);
    nKey = sqlite3_value_bytes(argv[2+LSM1_COLUMN_BLOBKEY]);
  }else{
    /* Use a key encoding that sorts in lexicographical order */
    rc = lsm1EncodeKey(argv[2+LSM1_COLUMN_KEY],
                       (unsigned char**)&pKey,&nKey,
                       pSpace,sizeof(pSpace));
    if( rc ) return rc;
  }
  if( sqlite3_value_type(argv[2+LSM1_COLUMN_BLOBVALUE])==SQLITE_BLOB ){
    pVal = sqlite3_value_blob(argv[2+LSM1_COLUMN_BLOBVALUE]);
    nVal = sqlite3_value_bytes(argv[2+LSM1_COLUMN_BLOBVALUE]);
    rc = lsm_insert(p->pDb, pKey, nKey, pVal, nVal);
  }else{
    pValue = argv[2+LSM1_COLUMN_VALUE];
    eType = sqlite3_value_type(pValue);
    switch( eType ){
      case SQLITE_NULL: {
        rc = lsm_delete(p->pDb, pKey, nKey);
        break;
      }
      case SQLITE_BLOB:
      case SQLITE_TEXT: {



        if( eType==SQLITE_TEXT ){
          pVal = sqlite3_value_text(pValue);
        }else{
          pVal = (unsigned char*)sqlite3_value_blob(pValue);
        }
        nVal = sqlite3_value_bytes(pValue);
        pData = sqlite3_malloc( nVal+1 );
        if( pData==0 ){
          rc = SQLITE_NOMEM;
        }else{
          pData[0] = eType;
          memcpy(&pData[1], pVal, nVal);
          rc = lsm_insert(p->pDb, pKey, nKey, pData, nVal+1);
          sqlite3_free(pData);
        }
        break;
      }
      case SQLITE_INTEGER:
      case SQLITE_FLOAT: {
        sqlite3_uint64 x;
        unsigned char aVal[9];
        int i;
        if( eType==SQLITE_INTEGER ){
          *(sqlite3_int64*)&x = sqlite3_value_int64(pValue);
        }else{
          *(double*)&x = sqlite3_value_double(pValue);
        }
        for(i=8; x>0 && i>=1; i--){
          aVal[i] = x & 0xff;
          x >>= 8;
        }
        aVal[i] = eType;
        rc = lsm_insert(p->pDb, pKey, nKey, &aVal[i], 9-i);
        break;
      }
    }
  }
  if( pKey!=(const void*)pSpace ) sqlite3_free((void*)pKey);
  return rc==LSM_OK ? SQLITE_OK : SQLITE_ERROR;
}      

/* Begin a transaction
*/
static int lsm1Begin(sqlite3_vtab *pVtab){
  lsm1_vtab *p = (lsm1_vtab*)pVtab;