SQLite4
Check-in [a2630e5d90]
Not logged in

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

Overview
Comment:Add the sqlite4_kvfactory typedef to the interface and use that typedef throughout the implementation. This check-in also includes some unrelated cleanup of the sqlite4.h file.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: a2630e5d900181d2588a09d9098aea0ee0141387
User & Date: drh 2013-01-01 19:02:48
Context
2013-01-01
19:55
Remove obsolete memory pool allocators from LSM. check-in: 69b21857ca user: drh tags: trunk
19:02
Add the sqlite4_kvfactory typedef to the interface and use that typedef throughout the implementation. This check-in also includes some unrelated cleanup of the sqlite4.h file. check-in: a2630e5d90 user: drh tags: trunk
18:41
Fix a memory leak in fts5.c. check-in: 7bc0e58875 user: dan tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/kv.c.

86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  unsigned flags           /* Option flags */
){
  KVStore *pNew = 0;
  int rc;
  sqlite4_env *pEnv = &sqlite4DefaultEnv;  /* OR db->pEnv */
  const char *zStorageName;
  KVFactory *pMkr;
  int (*xFactory)(sqlite4_env*,sqlite4_kvstore**,const char*,unsigned);

  if( (flags & SQLITE4_KVOPEN_TEMPORARY)!=0 || zUri==0 || zUri[0]==0 ){
    zStorageName = "temp";
  }else{
    zStorageName = sqlite4_uri_parameter(zName, "kv");
    if( zStorageName==0 ){
      if( memcmp(":memory:", zUri, 8)==0 ){







|







86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  unsigned flags           /* Option flags */
){
  KVStore *pNew = 0;
  int rc;
  sqlite4_env *pEnv = &sqlite4DefaultEnv;  /* OR db->pEnv */
  const char *zStorageName;
  KVFactory *pMkr;
  sqlite4_kvfactory xFactory;

  if( (flags & SQLITE4_KVOPEN_TEMPORARY)!=0 || zUri==0 || zUri[0]==0 ){
    zStorageName = "temp";
  }else{
    zStorageName = sqlite4_uri_parameter(zName, "kv");
    if( zStorageName==0 ){
      if( memcmp(":memory:", zUri, 8)==0 ){

Changes to src/main.c.

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
      typedef void(*LOGFUNC_t)(void*,int,const char*);
      pEnv->xLog = va_arg(ap, LOGFUNC_t);
      pEnv->pLogArg = va_arg(ap, void*);
      break;
    }

    /*
    ** sqlite4_env_config(pEnv, SQLITE4_ENVCONFIG_KVSTORE_PUSH, zName, xFactory);
    **
    ** Push a new KVStore factory onto the factory stack.  The new factory
    ** takes priority over prior factories.
    */
    case SQLITE4_ENVCONFIG_KVSTORE_PUSH: {
      typedef int(*PFactory)(sqlite4_env*, KVStore **, const char *, unsigned int);
      const char *zName = va_arg(ap, const char*);
      int nName = sqlite4Strlen30(zName);
      KVFactory *pMkr = sqlite4_malloc(pEnv, sizeof(*pMkr)+nName+1);
      char *z;
      if( pMkr==0 ) return SQLITE4_NOMEM;
      z = (char*)&pMkr[1];
      memcpy(z, zName, nName+1);
      memset(pMkr, 0, sizeof(*pMkr));
      pMkr->zName = z;
      pMkr->xFactory = va_arg(ap, PFactory);
      sqlite4_mutex_enter(pEnv->pFactoryMutex);
      pMkr->pNext = pEnv->pFactory;
      pEnv->pFactory = pMkr;
      sqlite4_mutex_leave(pEnv->pFactoryMutex);
      break;
    }








|





<









|







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
      typedef void(*LOGFUNC_t)(void*,int,const char*);
      pEnv->xLog = va_arg(ap, LOGFUNC_t);
      pEnv->pLogArg = va_arg(ap, void*);
      break;
    }

    /*
    ** sqlite4_env_config(pEnv, SQLITE4_ENVCONFIG_KVSTORE_PUSH, zName,xFactory);
    **
    ** Push a new KVStore factory onto the factory stack.  The new factory
    ** takes priority over prior factories.
    */
    case SQLITE4_ENVCONFIG_KVSTORE_PUSH: {

      const char *zName = va_arg(ap, const char*);
      int nName = sqlite4Strlen30(zName);
      KVFactory *pMkr = sqlite4_malloc(pEnv, sizeof(*pMkr)+nName+1);
      char *z;
      if( pMkr==0 ) return SQLITE4_NOMEM;
      z = (char*)&pMkr[1];
      memcpy(z, zName, nName+1);
      memset(pMkr, 0, sizeof(*pMkr));
      pMkr->zName = z;
      pMkr->xFactory = va_arg(ap, sqlite4_kvfactory);
      sqlite4_mutex_enter(pEnv->pFactoryMutex);
      pMkr->pNext = pEnv->pFactory;
      pEnv->pFactory = pMkr;
      sqlite4_mutex_leave(pEnv->pFactoryMutex);
      break;
    }

Changes to src/sqlite.h.in.

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
...
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
...
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
...
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
....
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332















4333
4334
4335
4336
4337
4338
4339
#define SQLITE4_ENVCONFIG_MEMSTATUS     9   /* boolean */
#define SQLITE4_ENVCONFIG_LOOKASIDE    10   /* size, count */
#define SQLITE4_ENVCONFIG_LOG          11   /* xLog, pArg */
#define SQLITE4_ENVCONFIG_KVSTORE_PUSH 12   /* name, factory */
#define SQLITE4_ENVCONFIG_KVSTORE_POP  13   /* name */
#define SQLITE4_ENVCONFIG_KVSTORE_GET  14   /* name, *factor */


/*
** CAPIREF: Compile-Time Library Version Numbers
**
** ^(The [SQLITE4_VERSION] C preprocessor macro in the sqlite4.h header
** evaluates to a string literal that is the SQLite version in the
** format "X.Y.Z" where X is the major version number (always 3 for
** SQLite3) and Y is the minor version number and Z is the release number.)^
................................................................................
**
** New error codes may be added in future versions of SQLite.
**
** See also: [SQLITE4_IOERR_READ | extended result codes],
** [sqlite4_vtab_on_conflict()] [SQLITE4_ROLLBACK | result codes].
*/
#define SQLITE4_OK           0   /* Successful result */
/* beginning-of-error-codes */
#define SQLITE4_ERROR        1   /* SQL error or missing database */
#define SQLITE4_INTERNAL     2   /* Internal logic error in SQLite */
#define SQLITE4_PERM         3   /* Access permission denied */
#define SQLITE4_ABORT        4   /* Callback routine requested an abort */
#define SQLITE4_BUSY         5   /* The database file is locked */
#define SQLITE4_LOCKED       6   /* A table in the database is locked */
#define SQLITE4_NOMEM        7   /* A malloc() failed */
#define SQLITE4_READONLY     8   /* Attempt to write a readonly database */
#define SQLITE4_INTERRUPT    9   /* Operation terminated by sqlite4_interrupt()*/
#define SQLITE4_IOERR       10   /* Some kind of disk I/O error occurred */
#define SQLITE4_CORRUPT     11   /* The database disk image is malformed */
#define SQLITE4_NOTFOUND    12   /* Unknown opcode in sqlite4_file_control() */
#define SQLITE4_FULL        13   /* Insertion failed because database is full */
#define SQLITE4_CANTOPEN    14   /* Unable to open the database file */
#define SQLITE4_PROTOCOL    15   /* Database lock protocol error */
#define SQLITE4_EMPTY       16   /* Database is empty */
................................................................................
#define SQLITE4_TOOBIG      18   /* String or BLOB exceeds size limit */
#define SQLITE4_CONSTRAINT  19   /* Abort due to constraint violation */
#define SQLITE4_MISMATCH    20   /* Data type mismatch */
#define SQLITE4_MISUSE      21   /* Library used incorrectly */
#define SQLITE4_NOLFS       22   /* Uses OS features not supported on host */
#define SQLITE4_AUTH        23   /* Authorization denied */
#define SQLITE4_FORMAT      24   /* Auxiliary database format error */
#define SQLITE4_RANGE       25   /* 2nd parameter to sqlite4_bind out of range */
#define SQLITE4_NOTADB      26   /* File opened that is not a database file */
#define SQLITE4_ROW         100  /* sqlite4_step() has another row ready */
#define SQLITE4_DONE        101  /* sqlite4_step() has finished executing */
#define SQLITE4_INEXACT     102  /* xSeek method of storage finds nearby ans */
/* end-of-error-codes */

/*
** CAPIREF: Extended Result Codes
** KEYWORDS: {extended error code} {extended error codes}
** KEYWORDS: {extended result code} {extended result codes}
**
** In its default configuration, SQLite API routines return one of 26 integer
................................................................................
typedef struct sqlite4_mutex sqlite4_mutex;
struct sqlite4_mutex {
  struct sqlite4_mutex_methods *pMutexMethods;
  /* Subclasses will typically add additional fields */
};

/*
** CAPIREF: Initialize The SQLite Library
**
** ^The sqlite4_initialize(A) routine initializes an sqlite4_env object A.
** ^The sqlite4_shutdown(A) routine
** deallocates any resources that were allocated by sqlite4_initialize(A).
** A parameter value of NULL means to use the default environment.
** All environment objects are required to be initialized before use
** (even the default environment).
................................................................................
  int (*xControl)(sqlite4_kvstore*, int, void*);
};
typedef struct sqlite4_kv_methods sqlite4_kv_methods;

/*
** CAPI4REF: Key-value storage engine open flags
**
** Allowed values to the flags parameter of an sqlite4_kvstore object
** factory.
**
** The flags parameter to the sqlite4_kvstore factory (the fourth parameter)
** is an OR-ed combination of these values and the
** [SQLITE4_OPEN_READONLY | SQLITE4_OPEN_xxxxx] flags that appear as 
** arguments to [sqlite4_open()].
*/
#define SQLITE4_KVOPEN_TEMPORARY       0x00010000  /* A temporary database */
#define SQLITE4_KVOPEN_NO_TRANSACTIONS 0x00020000  /* No transactions needed */

















/*
** CAPI4REF: Representation Of Numbers
**
** Every number in SQLite is represented in memory by an instance of
** the following object.
*/







<







 







<








|







 







|




<







 







|







 







|
<

|







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







121
122
123
124
125
126
127

128
129
130
131
132
133
134
...
406
407
408
409
410
411
412

413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
...
430
431
432
433
434
435
436
437
438
439
440
441

442
443
444
445
446
447
448
...
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
....
4312
4313
4314
4315
4316
4317
4318
4319

4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
#define SQLITE4_ENVCONFIG_MEMSTATUS     9   /* boolean */
#define SQLITE4_ENVCONFIG_LOOKASIDE    10   /* size, count */
#define SQLITE4_ENVCONFIG_LOG          11   /* xLog, pArg */
#define SQLITE4_ENVCONFIG_KVSTORE_PUSH 12   /* name, factory */
#define SQLITE4_ENVCONFIG_KVSTORE_POP  13   /* name */
#define SQLITE4_ENVCONFIG_KVSTORE_GET  14   /* name, *factor */


/*
** CAPIREF: Compile-Time Library Version Numbers
**
** ^(The [SQLITE4_VERSION] C preprocessor macro in the sqlite4.h header
** evaluates to a string literal that is the SQLite version in the
** format "X.Y.Z" where X is the major version number (always 3 for
** SQLite3) and Y is the minor version number and Z is the release number.)^
................................................................................
**
** New error codes may be added in future versions of SQLite.
**
** See also: [SQLITE4_IOERR_READ | extended result codes],
** [sqlite4_vtab_on_conflict()] [SQLITE4_ROLLBACK | result codes].
*/
#define SQLITE4_OK           0   /* Successful result */

#define SQLITE4_ERROR        1   /* SQL error or missing database */
#define SQLITE4_INTERNAL     2   /* Internal logic error in SQLite */
#define SQLITE4_PERM         3   /* Access permission denied */
#define SQLITE4_ABORT        4   /* Callback routine requested an abort */
#define SQLITE4_BUSY         5   /* The database file is locked */
#define SQLITE4_LOCKED       6   /* A table in the database is locked */
#define SQLITE4_NOMEM        7   /* A malloc() failed */
#define SQLITE4_READONLY     8   /* Attempt to write a readonly database */
#define SQLITE4_INTERRUPT    9   /* Stopped terminated by sqlite4_interrupt()*/
#define SQLITE4_IOERR       10   /* Some kind of disk I/O error occurred */
#define SQLITE4_CORRUPT     11   /* The database disk image is malformed */
#define SQLITE4_NOTFOUND    12   /* Unknown opcode in sqlite4_file_control() */
#define SQLITE4_FULL        13   /* Insertion failed because database is full */
#define SQLITE4_CANTOPEN    14   /* Unable to open the database file */
#define SQLITE4_PROTOCOL    15   /* Database lock protocol error */
#define SQLITE4_EMPTY       16   /* Database is empty */
................................................................................
#define SQLITE4_TOOBIG      18   /* String or BLOB exceeds size limit */
#define SQLITE4_CONSTRAINT  19   /* Abort due to constraint violation */
#define SQLITE4_MISMATCH    20   /* Data type mismatch */
#define SQLITE4_MISUSE      21   /* Library used incorrectly */
#define SQLITE4_NOLFS       22   /* Uses OS features not supported on host */
#define SQLITE4_AUTH        23   /* Authorization denied */
#define SQLITE4_FORMAT      24   /* Auxiliary database format error */
#define SQLITE4_RANGE       25   /* 2nd param to sqlite4_bind out of range */
#define SQLITE4_NOTADB      26   /* File opened that is not a database file */
#define SQLITE4_ROW         100  /* sqlite4_step() has another row ready */
#define SQLITE4_DONE        101  /* sqlite4_step() has finished executing */
#define SQLITE4_INEXACT     102  /* xSeek method of storage finds nearby ans */


/*
** CAPIREF: Extended Result Codes
** KEYWORDS: {extended error code} {extended error codes}
** KEYWORDS: {extended result code} {extended result codes}
**
** In its default configuration, SQLite API routines return one of 26 integer
................................................................................
typedef struct sqlite4_mutex sqlite4_mutex;
struct sqlite4_mutex {
  struct sqlite4_mutex_methods *pMutexMethods;
  /* Subclasses will typically add additional fields */
};

/*
** CAPIREF: Initialize An SQLite Environment
**
** ^The sqlite4_initialize(A) routine initializes an sqlite4_env object A.
** ^The sqlite4_shutdown(A) routine
** deallocates any resources that were allocated by sqlite4_initialize(A).
** A parameter value of NULL means to use the default environment.
** All environment objects are required to be initialized before use
** (even the default environment).
................................................................................
  int (*xControl)(sqlite4_kvstore*, int, void*);
};
typedef struct sqlite4_kv_methods sqlite4_kv_methods;

/*
** CAPI4REF: Key-value storage engine open flags
**
** Allowed values to the flags parameter of an [sqlite4_kvfactory] object.

**
** The flags parameter to the sqlite4_kvfactory fuction (the fourth parameter)
** is an OR-ed combination of these values and the
** [SQLITE4_OPEN_READONLY | SQLITE4_OPEN_xxxxx] flags that appear as 
** arguments to [sqlite4_open()].
*/
#define SQLITE4_KVOPEN_TEMPORARY       0x00010000  /* A temporary database */
#define SQLITE4_KVOPEN_NO_TRANSACTIONS 0x00020000  /* No transactions needed */

/*
** CAPIREF: Key-value storage object factory
**
** New key/value storage engines can be added to SQLite4 at run-time.
** In order to create a new KV storage engine, the application must 
** supply a "factory" function that creates an instance of the
** sqlite4_kvstore object.  This is typedef defines the signature
** of that factory function.
*/
typedef int (*sqlite4_kvfactory)(
  sqlite4_env *pEnv,             /* The environment to use */
  sqlite4_kvstore **ppKVStore,   /* OUT: New KV store returned here */
  const char *zFilename,         /* Name of database file to open */
  unsigned flags                 /* Bit flags */
);

/*
** CAPI4REF: Representation Of Numbers
**
** Every number in SQLite is represented in memory by an instance of
** the following object.
*/

Changes to src/sqliteInt.h.

2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
typedef struct {
  sqlite4 *db;        /* The database being initialized */
  int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
  char **pzErrMsg;    /* Error message stored here */
  int rc;             /* Result code stored here */
} InitData;

/*
** FIXME: missing docs
*/
typedef int (*KVFactory_factory_f)(sqlite4_env*,sqlite4_kvstore**,const char*,unsigned);

/*
** A pluggable storage engine
*/
typedef struct KVFactory {
  struct KVFactory *pNext;          /* Next in list of them all */
  const char *zName;                /* Name of this factory */
  KVFactory_factory_f xFactory;
  int isPerm;                       /* True if a built-in.  Cannot be popped */
} KVFactory;

/*
** An instance of this structure defines the run-time environment.
*/
struct sqlite4_env {
  int nByte;                        /* Size of this object in bytes */







<
<
<
<
<




|
|
|
|







2413
2414
2415
2416
2417
2418
2419





2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
typedef struct {
  sqlite4 *db;        /* The database being initialized */
  int iDb;            /* 0 for main database.  1 for TEMP, 2.. for ATTACHed */
  char **pzErrMsg;    /* Error message stored here */
  int rc;             /* Result code stored here */
} InitData;






/*
** A pluggable storage engine
*/
typedef struct KVFactory {
  struct KVFactory *pNext;       /* Next in list of all storage engines */
  const char *zName;             /* Name of this factory */
  sqlite4_kvfactory xFactory;    /* Function to make an sqlite4_kvstore obj */
  int isPerm;                    /* True if a built-in.  Cannot be popped */
} KVFactory;

/*
** An instance of this structure defines the run-time environment.
*/
struct sqlite4_env {
  int nByte;                        /* Size of this object in bytes */

Changes to test/test_kv2.c.

12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
**
*/

#include "sqliteInt.h"


static struct KVWrapGlobal {
  int (*xFactory)(sqlite4_env*, KVStore **, const char *, unsigned int);
  int nStep;                      /* Total number of successful next/prev */
  int nSeek;                      /* Total number of calls to xSeek */
} kvwg = {0};

typedef struct KVWrap KVWrap;
typedef struct KVWrapCsr KVWrapCsr;








|







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
**
*/

#include "sqliteInt.h"


static struct KVWrapGlobal {
  sqlite4_kvfactory xFactory;
  int nStep;                      /* Total number of successful next/prev */
  int nSeek;                      /* Total number of calls to xSeek */
} kvwg = {0};

typedef struct KVWrap KVWrap;
typedef struct KVWrapCsr KVWrapCsr;