SQLite

Check-in [95d53c4432]
Login

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

Overview
Comment:Merge the latest changes of trunk into the session branch.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | sessions
Files: files | file ages | folders
SHA1: 95d53c44320b9639f2623aa9cc88d0d3e1a3be8f
User & Date: drh 2011-04-04 13:19:36.696
Context
2011-04-05
13:27
Pull the latest trunk changes (and hence the schema-parse-refactor changes) into the sessions branch. (check-in: 03ca83422f user: drh tags: sessions)
2011-04-04
13:19
Merge the latest changes of trunk into the session branch. (check-in: 95d53c4432 user: drh tags: sessions)
12:29
Move the expired-statement test for OP_Function until after all memory has been freed. The test is still commented out, however. (check-in: 425e3edb14 user: drh tags: trunk)
2011-04-01
15:43
Merge latest trunk changes. (check-in: d184cf0057 user: dan tags: sessions)
Changes
Unified Diff Ignore Whitespace Patch
Changes to Makefile.in.
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
  $(TOP)/src/test_async.c \
  $(TOP)/src/test_backup.c \
  $(TOP)/src/test_btree.c \
  $(TOP)/src/test_config.c \
  $(TOP)/src/test_demovfs.c \
  $(TOP)/src/test_devsym.c \
  $(TOP)/src/test_func.c \

  $(TOP)/src/test_hexio.c \
  $(TOP)/src/test_init.c \
  $(TOP)/src/test_intarray.c \
  $(TOP)/src/test_journal.c \
  $(TOP)/src/test_malloc.c \
  $(TOP)/src/test_multiplex.c \
  $(TOP)/src/test_mutex.c \
  $(TOP)/src/test_onefile.c \
  $(TOP)/src/test_osinst.c \
  $(TOP)/src/test_pcache.c \
  $(TOP)/src/test_quota.c \
  $(TOP)/src/test_rtree.c \
  $(TOP)/src/test_schema.c \
  $(TOP)/src/test_server.c \
  $(TOP)/src/test_superlock.c \

  $(TOP)/src/test_stat.c \
  $(TOP)/src/test_tclvar.c \
  $(TOP)/src/test_thread.c \
  $(TOP)/src/test_vfs.c \

  $(TOP)/src/test_wsd.c

# Source code to the library files needed by the test fixture
#
TESTSRC2 = \
  $(TOP)/src/attach.c \
  $(TOP)/src/backup.c \







>















>




>







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
  $(TOP)/src/test_async.c \
  $(TOP)/src/test_backup.c \
  $(TOP)/src/test_btree.c \
  $(TOP)/src/test_config.c \
  $(TOP)/src/test_demovfs.c \
  $(TOP)/src/test_devsym.c \
  $(TOP)/src/test_func.c \
  $(TOP)/src/test_fuzzer.c \
  $(TOP)/src/test_hexio.c \
  $(TOP)/src/test_init.c \
  $(TOP)/src/test_intarray.c \
  $(TOP)/src/test_journal.c \
  $(TOP)/src/test_malloc.c \
  $(TOP)/src/test_multiplex.c \
  $(TOP)/src/test_mutex.c \
  $(TOP)/src/test_onefile.c \
  $(TOP)/src/test_osinst.c \
  $(TOP)/src/test_pcache.c \
  $(TOP)/src/test_quota.c \
  $(TOP)/src/test_rtree.c \
  $(TOP)/src/test_schema.c \
  $(TOP)/src/test_server.c \
  $(TOP)/src/test_superlock.c \
  $(TOP)/src/test_syscall.c \
  $(TOP)/src/test_stat.c \
  $(TOP)/src/test_tclvar.c \
  $(TOP)/src/test_thread.c \
  $(TOP)/src/test_vfs.c \
  $(TOP)/src/test_wholenumber.c \
  $(TOP)/src/test_wsd.c

# Source code to the library files needed by the test fixture
#
TESTSRC2 = \
  $(TOP)/src/attach.c \
  $(TOP)/src/backup.c \
Changes to main.mk.
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
  $(TOP)/src/test_async.c \
  $(TOP)/src/test_backup.c \
  $(TOP)/src/test_btree.c \
  $(TOP)/src/test_config.c \
  $(TOP)/src/test_demovfs.c \
  $(TOP)/src/test_devsym.c \
  $(TOP)/src/test_func.c \

  $(TOP)/src/test_hexio.c \
  $(TOP)/src/test_init.c \
  $(TOP)/src/test_intarray.c \
  $(TOP)/src/test_journal.c \
  $(TOP)/src/test_malloc.c \
  $(TOP)/src/test_multiplex.c \
  $(TOP)/src/test_mutex.c \
  $(TOP)/src/test_onefile.c \
  $(TOP)/src/test_osinst.c \
  $(TOP)/src/test_pcache.c \
  $(TOP)/src/test_quota.c \
  $(TOP)/src/test_rtree.c \
  $(TOP)/src/test_schema.c \
  $(TOP)/src/test_server.c \
  $(TOP)/src/test_stat.c \
  $(TOP)/src/test_superlock.c \
  $(TOP)/src/test_syscall.c \
  $(TOP)/src/test_tclvar.c \
  $(TOP)/src/test_thread.c \
  $(TOP)/src/test_vfs.c \

  $(TOP)/src/test_wsd.c 

#TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c
#TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c

TESTSRC2 = \
  $(TOP)/src/attach.c \
  $(TOP)/src/backup.c \







>




















>
|







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
  $(TOP)/src/test_async.c \
  $(TOP)/src/test_backup.c \
  $(TOP)/src/test_btree.c \
  $(TOP)/src/test_config.c \
  $(TOP)/src/test_demovfs.c \
  $(TOP)/src/test_devsym.c \
  $(TOP)/src/test_func.c \
  $(TOP)/src/test_fuzzer.c \
  $(TOP)/src/test_hexio.c \
  $(TOP)/src/test_init.c \
  $(TOP)/src/test_intarray.c \
  $(TOP)/src/test_journal.c \
  $(TOP)/src/test_malloc.c \
  $(TOP)/src/test_multiplex.c \
  $(TOP)/src/test_mutex.c \
  $(TOP)/src/test_onefile.c \
  $(TOP)/src/test_osinst.c \
  $(TOP)/src/test_pcache.c \
  $(TOP)/src/test_quota.c \
  $(TOP)/src/test_rtree.c \
  $(TOP)/src/test_schema.c \
  $(TOP)/src/test_server.c \
  $(TOP)/src/test_stat.c \
  $(TOP)/src/test_superlock.c \
  $(TOP)/src/test_syscall.c \
  $(TOP)/src/test_tclvar.c \
  $(TOP)/src/test_thread.c \
  $(TOP)/src/test_vfs.c \
  $(TOP)/src/test_wholenumber.c \
  $(TOP)/src/test_wsd.c

#TESTSRC += $(TOP)/ext/fts2/fts2_tokenizer.c
#TESTSRC += $(TOP)/ext/fts3/fts3_tokenizer.c

TESTSRC2 = \
  $(TOP)/src/attach.c \
  $(TOP)/src/backup.c \
Changes to src/btmutex.c.
35
36
37
38
39
40
41

42
43
44
45
46

47
48
49

















50
51
52
53
54
55
56
}

/*
** Release the BtShared mutex associated with B-Tree handle p and
** clear the p->locked boolean.
*/
static void unlockBtreeMutex(Btree *p){

  assert( p->locked==1 );
  assert( sqlite3_mutex_held(p->pBt->mutex) );
  assert( sqlite3_mutex_held(p->db->mutex) );
  assert( p->db==p->pBt->db );


  sqlite3_mutex_leave(p->pBt->mutex);
  p->locked = 0;
}


















/*
** Enter a mutex on the given BTree object.
**
** If the object is not sharable, then no mutex is ever required
** and this routine is a no-op.  The underlying mutex is non-recursive.
** But we keep a reference count in Btree.wantToLock so the behavior







>

|

|

>
|


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







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
}

/*
** Release the BtShared mutex associated with B-Tree handle p and
** clear the p->locked boolean.
*/
static void unlockBtreeMutex(Btree *p){
  BtShared *pBt = p->pBt;
  assert( p->locked==1 );
  assert( sqlite3_mutex_held(pBt->mutex) );
  assert( sqlite3_mutex_held(p->db->mutex) );
  assert( p->db==pBt->db );

  pBt->iMutexCounter++;
  sqlite3_mutex_leave(pBt->mutex);
  p->locked = 0;
}

#ifdef SQLITE_DEBUG
/*
** Return the number of times that the mutex has been exited for
** the given btree.
**
** This is a small circular counter that wraps around to zero on
** overflow.  It is used only for sanity checking - to verify that
** mutexes are held continously by asserting that the value of
** this counter at the beginning of a region is the same as at
** the end.
*/
u32 sqlite3BtreeMutexCounter(Btree *p){
  assert( p->locked==1 || p->sharable==0 );
  return p->pBt->iMutexCounter;
}
#endif

/*
** Enter a mutex on the given BTree object.
**
** If the object is not sharable, then no mutex is ever required
** and this routine is a no-op.  The underlying mutex is non-recursive.
** But we keep a reference count in Btree.wantToLock so the behavior
87
88
89
90
91
92
93


















94
95
96
97
98
99
100
  /* Unless the database is sharable and unlocked, then BtShared.db
  ** should already be set correctly. */
  assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );

  if( !p->sharable ) return;
  p->wantToLock++;
  if( p->locked ) return;



















  /* In most cases, we should be able to acquire the lock we
  ** want without having to go throught the ascending lock
  ** procedure that follows.  Just be sure not to block.
  */
  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
    p->pBt->db = p->db;







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







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
  /* Unless the database is sharable and unlocked, then BtShared.db
  ** should already be set correctly. */
  assert( (p->locked==0 && p->sharable) || p->pBt->db==p->db );

  if( !p->sharable ) return;
  p->wantToLock++;
  if( p->locked ) return;

  /* Increment the mutex counter on all locked btrees in the same
  ** database connection.  This simulates the unlocking that would
  ** occur on a worst-case mutex dead-lock avoidance scenario.
  */
#ifdef SQLITE_DEBUG
  {
    int ii;
    sqlite3 *db = p->db;
    Btree *pOther;
    for(ii=0; ii<db->nDb; ii++){
      if( ii==1 ) continue;
      pOther = db->aDb[ii].pBt;
      if( pOther==0 || pOther->sharable==0 || pOther->locked==0 ) continue;
      pOther->pBt->iMutexCounter++;
    }
  }
#endif

  /* In most cases, we should be able to acquire the lock we
  ** want without having to go throught the ascending lock
  ** procedure that follows.  Just be sure not to block.
  */
  if( sqlite3_mutex_try(p->pBt->mutex)==SQLITE_OK ){
    p->pBt->db = p->db;
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    p = db->aDb[i].pBt;
    assert( !p || (p->locked==0 && p->sharable) || p->pBt->db==p->db );
    if( p && p->sharable ){
      p->wantToLock++;
      if( !p->locked ){
        assert( p->wantToLock==1 );
        while( p->pPrev ) p = p->pPrev;
        /* Reason for ALWAYS:  There must be at least on unlocked Btree in
        ** the chain.  Otherwise the !p->locked test above would have failed */
        while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
        for(pLater = p->pNext; pLater; pLater=pLater->pNext){
          if( pLater->locked ){
            unlockBtreeMutex(pLater);
          }
        }







|







228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
    p = db->aDb[i].pBt;
    assert( !p || (p->locked==0 && p->sharable) || p->pBt->db==p->db );
    if( p && p->sharable ){
      p->wantToLock++;
      if( !p->locked ){
        assert( p->wantToLock==1 );
        while( p->pPrev ) p = p->pPrev;
        /* Reason for ALWAYS:  There must be at least one unlocked Btree in
        ** the chain.  Otherwise the !p->locked test above would have failed */
        while( p->locked && ALWAYS(p->pNext) ) p = p->pNext;
        for(pLater = p->pNext; pLater; pLater=pLater->pNext){
          if( pLater->locked ){
            unlockBtreeMutex(pLater);
          }
        }
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
      return 0;
    }
  }
  return 1;
}
#endif /* NDEBUG */

/*
** Add a new Btree pointer to a BtreeMutexArray. 
** if the pointer can possibly be shared with
** another database connection.
**
** The pointers are kept in sorted order by pBtree->pBt.  That
** way when we go to enter all the mutexes, we can enter them
** in order without every having to backup and retry and without
** worrying about deadlock.
**
** The number of shared btrees will always be small (usually 0 or 1)
** so an insertion sort is an adequate algorithm here.
*/
void sqlite3BtreeMutexArrayInsert(BtreeMutexArray *pArray, Btree *pBtree){
  int i, j;
  BtShared *pBt;
  if( pBtree==0 || pBtree->sharable==0 ) return;
#ifndef NDEBUG
  {
    for(i=0; i<pArray->nMutex; i++){
      assert( pArray->aBtree[i]!=pBtree );
    }
  }
#endif
  assert( pArray->nMutex>=0 );
  assert( pArray->nMutex<ArraySize(pArray->aBtree)-1 );
  pBt = pBtree->pBt;
  for(i=0; i<pArray->nMutex; i++){
    assert( pArray->aBtree[i]!=pBtree );
    if( pArray->aBtree[i]->pBt>pBt ){
      for(j=pArray->nMutex; j>i; j--){
        pArray->aBtree[j] = pArray->aBtree[j-1];
      }
      pArray->aBtree[i] = pBtree;
      pArray->nMutex++;
      return;
    }
  }
  pArray->aBtree[pArray->nMutex++] = pBtree;
}

/*
** Enter the mutex of every btree in the array.  This routine is
** called at the beginning of sqlite3VdbeExec().  The mutexes are
** exited at the end of the same function.
*/
void sqlite3BtreeMutexArrayEnter(BtreeMutexArray *pArray){
  int i;
  for(i=0; i<pArray->nMutex; i++){
    Btree *p = pArray->aBtree[i];
    /* Some basic sanity checking */
    assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
    assert( !p->locked || p->wantToLock>0 );

    /* We should already hold a lock on the database connection */
    assert( sqlite3_mutex_held(p->db->mutex) );

    /* The Btree is sharable because only sharable Btrees are entered
    ** into the array in the first place. */
    assert( p->sharable );

    p->wantToLock++;
    if( !p->locked ){
      lockBtreeMutex(p);
    }
  }
}

/*
** Leave the mutex of every btree in the group.

*/
void sqlite3BtreeMutexArrayLeave(BtreeMutexArray *pArray){
  int i;
  for(i=0; i<pArray->nMutex; i++){
    Btree *p = pArray->aBtree[i];
    /* Some basic sanity checking */
    assert( i==0 || pArray->aBtree[i-1]->pBt<p->pBt );
    assert( p->locked );
    assert( p->wantToLock>0 );

    /* We should already hold a lock on the database connection */
    assert( sqlite3_mutex_held(p->db->mutex) );

    p->wantToLock--;
    if( p->wantToLock==0 ){
      unlockBtreeMutex(p);
    }
  }
}

#else
void sqlite3BtreeEnter(Btree *p){
  p->pBt->db = p->db;
}
void sqlite3BtreeEnterAll(sqlite3 *db){
  int i;
  for(i=0; i<db->nDb; i++){
    Btree *p = db->aDb[i].pBt;







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|

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

<
<
<
<
<
<
<
<

<
<
<
<
<
<
<
<
<
<
<







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
      return 0;
    }
  }
  return 1;
}
#endif /* NDEBUG */









































#else /* SQLITE_THREADSAFE>0 above.  SQLITE_THREADSAFE==0 below */
/*











** The following are special cases for mutex enter routines for use


** in single threaded applications that use shared cache.  Except for



** these two routines, all mutex operations are no-ops in that case and






** are null #defines in btree.h.
**
** If shared cache is disabled, then all btree mutex routines, including
** the ones below, are no-ops and are null #defines in btree.h.
*/




















void sqlite3BtreeEnter(Btree *p){
  p->pBt->db = p->db;
}
void sqlite3BtreeEnterAll(sqlite3 *db){
  int i;
  for(i=0; i<db->nDb; i++){
    Btree *p = db->aDb[i].pBt;
Changes to src/btree.c.
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
**
** If the nBytes parameter is 0 and the blob of memory has not yet been
** allocated, a null pointer is returned. If the blob has already been
** allocated, it is returned as normal.
**
** Just before the shared-btree is closed, the function passed as the 
** xFree argument when the memory allocation was made is invoked on the 
** blob of allocated memory. This function should not call sqlite3_free()
** on the memory, the btree layer does that.
*/
void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
  BtShared *pBt = p->pBt;
  sqlite3BtreeEnter(p);
  if( !pBt->pSchema && nBytes ){
    pBt->pSchema = sqlite3DbMallocZero(0, nBytes);







|







7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
**
** If the nBytes parameter is 0 and the blob of memory has not yet been
** allocated, a null pointer is returned. If the blob has already been
** allocated, it is returned as normal.
**
** Just before the shared-btree is closed, the function passed as the 
** xFree argument when the memory allocation was made is invoked on the 
** blob of allocated memory. The xFree function should not call sqlite3_free()
** on the memory, the btree layer does that.
*/
void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){
  BtShared *pBt = p->pBt;
  sqlite3BtreeEnter(p);
  if( !pBt->pSchema && nBytes ){
    pBt->pSchema = sqlite3DbMallocZero(0, nBytes);
Changes to src/btree.h.
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

/*
** Forward declarations of structure
*/
typedef struct Btree Btree;
typedef struct BtCursor BtCursor;
typedef struct BtShared BtShared;
typedef struct BtreeMutexArray BtreeMutexArray;

/*
** This structure records all of the Btrees that need to hold
** a mutex before we enter sqlite3VdbeExec().  The Btrees are
** are placed in aBtree[] in order of aBtree[]->pBt.  That way,
** we can always lock and unlock them all quickly.
*/
struct BtreeMutexArray {
  int nMutex;
  Btree *aBtree[SQLITE_MAX_ATTACHED+1];
};


int sqlite3BtreeOpen(
  const char *zFilename,   /* Name of database file to open */
  sqlite3 *db,             /* Associated database connection */
  Btree **ppBtree,         /* Return open Btree* here */
  int flags,               /* Flags */







<
<
<
<
<
<
<
<
<
<
<
<







35
36
37
38
39
40
41












42
43
44
45
46
47
48

/*
** Forward declarations of structure
*/
typedef struct Btree Btree;
typedef struct BtCursor BtCursor;
typedef struct BtShared BtShared;














int sqlite3BtreeOpen(
  const char *zFilename,   /* Name of database file to open */
  sqlite3 *db,             /* Associated database connection */
  Btree **ppBtree,         /* Return open Btree* here */
  int flags,               /* Flags */
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
#endif

#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
  void sqlite3BtreeLeave(Btree*);
  void sqlite3BtreeEnterCursor(BtCursor*);
  void sqlite3BtreeLeaveCursor(BtCursor*);
  void sqlite3BtreeLeaveAll(sqlite3*);
  void sqlite3BtreeMutexArrayEnter(BtreeMutexArray*);
  void sqlite3BtreeMutexArrayLeave(BtreeMutexArray*);
  void sqlite3BtreeMutexArrayInsert(BtreeMutexArray*, Btree*);
#ifndef NDEBUG
  /* These routines are used inside assert() statements only. */
  int sqlite3BtreeHoldsMutex(Btree*);
  int sqlite3BtreeHoldsAllMutexes(sqlite3*);

#endif
#else

# define sqlite3BtreeLeave(X)

# define sqlite3BtreeEnterCursor(X)
# define sqlite3BtreeLeaveCursor(X)
# define sqlite3BtreeLeaveAll(X)
# define sqlite3BtreeMutexArrayEnter(X)
# define sqlite3BtreeMutexArrayLeave(X)
# define sqlite3BtreeMutexArrayInsert(X,Y)

# define sqlite3BtreeHoldsMutex(X) 1
# define sqlite3BtreeHoldsAllMutexes(X) 1
#endif


#endif /* _BTREE_H_ */







<
<
<




>




>



<
<
<







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
#endif

#if !defined(SQLITE_OMIT_SHARED_CACHE) && SQLITE_THREADSAFE
  void sqlite3BtreeLeave(Btree*);
  void sqlite3BtreeEnterCursor(BtCursor*);
  void sqlite3BtreeLeaveCursor(BtCursor*);
  void sqlite3BtreeLeaveAll(sqlite3*);



#ifndef NDEBUG
  /* These routines are used inside assert() statements only. */
  int sqlite3BtreeHoldsMutex(Btree*);
  int sqlite3BtreeHoldsAllMutexes(sqlite3*);
  u32 sqlite3BtreeMutexCounter(Btree*);
#endif
#else

# define sqlite3BtreeLeave(X)
# define sqlite3BtreeMutexCounter(X) 0
# define sqlite3BtreeEnterCursor(X)
# define sqlite3BtreeLeaveCursor(X)
# define sqlite3BtreeLeaveAll(X)




# define sqlite3BtreeHoldsMutex(X) 1
# define sqlite3BtreeHoldsAllMutexes(X) 1
#endif


#endif /* _BTREE_H_ */
Changes to src/btreeInt.h.
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
** points to the same BtShared object.  The database cache and the
** schema associated with the database file are all contained within
** the BtShared object.
**
** All fields in this structure are accessed under sqlite3.mutex.
** The pBt pointer itself may not be changed while there exists cursors 
** in the referenced BtShared that point back to this Btree since those
** cursors have to do go through this Btree to find their BtShared and
** they often do so without holding sqlite3.mutex.
*/
struct Btree {
  sqlite3 *db;       /* The database connection holding this btree */
  BtShared *pBt;     /* Sharable content of this btree */
  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
  u8 sharable;       /* True if we can share pBt with another db */







|







332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
** points to the same BtShared object.  The database cache and the
** schema associated with the database file are all contained within
** the BtShared object.
**
** All fields in this structure are accessed under sqlite3.mutex.
** The pBt pointer itself may not be changed while there exists cursors 
** in the referenced BtShared that point back to this Btree since those
** cursors have to go through this Btree to find their BtShared and
** they often do so without holding sqlite3.mutex.
*/
struct Btree {
  sqlite3 *db;       /* The database connection holding this btree */
  BtShared *pBt;     /* Sharable content of this btree */
  u8 inTrans;        /* TRANS_NONE, TRANS_READ or TRANS_WRITE */
  u8 sharable;       /* True if we can share pBt with another db */
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437

438
439
440
441
442
443
444
  u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
  u32 pageSize;         /* Total number of bytes on a page */
  u32 usableSize;       /* Number of usable bytes on each page */
  int nTransaction;     /* Number of open transactions (read + write) */
  u32 nPage;            /* Number of pages in the database */
  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this struct */
  Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
#ifndef SQLITE_OMIT_SHARED_CACHE
  int nRef;             /* Number of references to this structure */
  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
  BtLock *pLock;        /* List of locks held on this shared-btree struct */
  Btree *pWriter;       /* Btree with currently open write transaction */
  u8 isExclusive;       /* True if pWriter has an EXCLUSIVE lock on the db */
  u8 isPending;         /* If waiting for read-locks to clear */

#endif
  u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
};

/*
** An instance of the following structure is used to hold information
** about a cell.  The parseCellPtr() function fills in this structure







|








>







422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  u16 minLeaf;          /* Minimum local payload in a LEAFDATA table */
  u32 pageSize;         /* Total number of bytes on a page */
  u32 usableSize;       /* Number of usable bytes on each page */
  int nTransaction;     /* Number of open transactions (read + write) */
  u32 nPage;            /* Number of pages in the database */
  void *pSchema;        /* Pointer to space allocated by sqlite3BtreeSchema() */
  void (*xFreeSchema)(void*);  /* Destructor for BtShared.pSchema */
  sqlite3_mutex *mutex; /* Non-recursive mutex required to access this object */
  Bitvec *pHasContent;  /* Set of pages moved to free-list this transaction */
#ifndef SQLITE_OMIT_SHARED_CACHE
  int nRef;             /* Number of references to this structure */
  BtShared *pNext;      /* Next on a list of sharable BtShared structs */
  BtLock *pLock;        /* List of locks held on this shared-btree struct */
  Btree *pWriter;       /* Btree with currently open write transaction */
  u8 isExclusive;       /* True if pWriter has an EXCLUSIVE lock on the db */
  u8 isPending;         /* If waiting for read-locks to clear */
  u16 iMutexCounter;    /* The number of mutex_leave(mutex) calls */
#endif
  u8 *pTmpSpace;        /* BtShared.pageSize bytes of space for tmp use */
};

/*
** An instance of the following structure is used to hold information
** about a cell.  The parseCellPtr() function fills in this structure
Changes to src/build.c.
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    /* The cookie mask contains one bit for each database file open.
    ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
    ** set for each database that is used.  Generate code to start a
    ** transaction on each used database and to verify the schema cookie
    ** on each used database.
    */
    if( pParse->cookieGoto>0 ){
      tAttachMask mask;
      int iDb;
      sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
      for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
        if( (mask & pParse->cookieMask)==0 ) continue;
        sqlite3VdbeUsesBtree(v, iDb);
        sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
        if( db->init.busy==0 ){







|







144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    /* The cookie mask contains one bit for each database file open.
    ** (Bit 0 is for main, bit 1 is for temp, and so forth.)  Bits are
    ** set for each database that is used.  Generate code to start a
    ** transaction on each used database and to verify the schema cookie
    ** on each used database.
    */
    if( pParse->cookieGoto>0 ){
      yDbMask mask;
      int iDb;
      sqlite3VdbeJumpHere(v, pParse->cookieGoto-1);
      for(iDb=0, mask=1; iDb<db->nDb; mask<<=1, iDb++){
        if( (mask & pParse->cookieMask)==0 ) continue;
        sqlite3VdbeUsesBtree(v, iDb);
        sqlite3VdbeAddOp2(v,OP_Transaction, iDb, (mask & pParse->writeMask)!=0);
        if( db->init.busy==0 ){
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
  if( pToplevel->cookieGoto==0 ){
    Vdbe *v = sqlite3GetVdbe(pToplevel);
    if( v==0 ) return;  /* This only happens if there was a prior error */
    pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
  }
  if( iDb>=0 ){
    sqlite3 *db = pToplevel->db;
    tAttachMask mask;

    assert( iDb<db->nDb );
    assert( db->aDb[iDb].pBt!=0 || iDb==1 );
    assert( iDb<SQLITE_MAX_ATTACHED+2 );
    mask = ((tAttachMask)1)<<iDb;
    if( (pToplevel->cookieMask & mask)==0 ){
      pToplevel->cookieMask |= mask;
      pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
      if( !OMIT_TEMPDB && iDb==1 ){
        sqlite3OpenTempDatabase(pToplevel);
      }
    }







|




|







3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
  if( pToplevel->cookieGoto==0 ){
    Vdbe *v = sqlite3GetVdbe(pToplevel);
    if( v==0 ) return;  /* This only happens if there was a prior error */
    pToplevel->cookieGoto = sqlite3VdbeAddOp2(v, OP_Goto, 0, 0)+1;
  }
  if( iDb>=0 ){
    sqlite3 *db = pToplevel->db;
    yDbMask mask;

    assert( iDb<db->nDb );
    assert( db->aDb[iDb].pBt!=0 || iDb==1 );
    assert( iDb<SQLITE_MAX_ATTACHED+2 );
    mask = ((yDbMask)1)<<iDb;
    if( (pToplevel->cookieMask & mask)==0 ){
      pToplevel->cookieMask |= mask;
      pToplevel->cookieValue[iDb] = db->aDb[iDb].pSchema->schema_cookie;
      if( !OMIT_TEMPDB && iDb==1 ){
        sqlite3OpenTempDatabase(pToplevel);
      }
    }
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
** rollback the whole transaction.  For operations where all constraints
** can be checked before any changes are made to the database, it is never
** necessary to undo a write and the checkpoint should not be set.
*/
void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
  Parse *pToplevel = sqlite3ParseToplevel(pParse);
  sqlite3CodeVerifySchema(pParse, iDb);
  pToplevel->writeMask |= ((tAttachMask)1)<<iDb;
  pToplevel->isMultiWrite |= setStatement;
}

/*
** Indicate that the statement currently under construction might write
** more than one entry (example: deleting one row then inserting another,
** inserting multiple rows in a table, or inserting a row and index entries.)







|







3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
** rollback the whole transaction.  For operations where all constraints
** can be checked before any changes are made to the database, it is never
** necessary to undo a write and the checkpoint should not be set.
*/
void sqlite3BeginWriteOperation(Parse *pParse, int setStatement, int iDb){
  Parse *pToplevel = sqlite3ParseToplevel(pParse);
  sqlite3CodeVerifySchema(pParse, iDb);
  pToplevel->writeMask |= ((yDbMask)1)<<iDb;
  pToplevel->isMultiWrite |= setStatement;
}

/*
** Indicate that the statement currently under construction might write
** more than one entry (example: deleting one row then inserting another,
** inserting multiple rows in a table, or inserting a row and index entries.)
Changes to src/os_unix.c.
390
391
392
393
394
395
396

397
398
399
400
401
402
403
404
405
406
407
  int rc = SQLITE_NOTFOUND;

  UNUSED_PARAMETER(pNotUsed);
  if( zName==0 ){
    /* If no zName is given, restore all system calls to their default
    ** settings and return NULL
    */

    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
      if( aSyscall[i].pDefault ){
        aSyscall[i].pCurrent = aSyscall[i].pDefault;
        rc = SQLITE_OK;
      }
    }
  }else{
    /* If zName is specified, operate on only the one system call
    ** specified.
    */
    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){







>



<







390
391
392
393
394
395
396
397
398
399
400

401
402
403
404
405
406
407
  int rc = SQLITE_NOTFOUND;

  UNUSED_PARAMETER(pNotUsed);
  if( zName==0 ){
    /* If no zName is given, restore all system calls to their default
    ** settings and return NULL
    */
    rc = SQLITE_OK;
    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
      if( aSyscall[i].pDefault ){
        aSyscall[i].pCurrent = aSyscall[i].pDefault;

      }
    }
  }else{
    /* If zName is specified, operate on only the one system call
    ** specified.
    */
    for(i=0; i<sizeof(aSyscall)/sizeof(aSyscall[0]); i++){
Changes to src/sqliteInt.h.
2129
2130
2131
2132
2133
2134
2135

2136

2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
  Trigger *pTrigger;      /* Trigger this program was coded from */
  int orconf;             /* Default ON CONFLICT policy */
  SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
  u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
  TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
};


/* Datatype for the bitmask of all attached databases */

#if SQLITE_MAX_ATTACHED>30
  typedef sqlite3_uint64 tAttachMask;
#else
  typedef unsigned int tAttachMask;
#endif

/*
** An SQL parser context.  A copy of this structure is passed through
** the parser and down into all the parser action routine in order to
** carry around information that is global to the entire parse.
**







>
|
>

|

|







2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
  Trigger *pTrigger;      /* Trigger this program was coded from */
  int orconf;             /* Default ON CONFLICT policy */
  SubProgram *pProgram;   /* Program implementing pTrigger/orconf */
  u32 aColmask[2];        /* Masks of old.*, new.* columns accessed */
  TriggerPrg *pNext;      /* Next entry in Parse.pTriggerPrg list */
};

/*
** The yDbMask datatype for the bitmask of all attached databases.
*/
#if SQLITE_MAX_ATTACHED>30
  typedef sqlite3_uint64 yDbMask;
#else
  typedef unsigned int yDbMask;
#endif

/*
** An SQL parser context.  A copy of this structure is passed through
** the parser and down into all the parser action routine in order to
** carry around information that is global to the entire parse.
**
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
    int iTable;           /* Table cursor number */
    int iColumn;          /* Table column number */
    u8 tempReg;           /* iReg is a temp register that needs to be freed */
    int iLevel;           /* Nesting level */
    int iReg;             /* Reg with value of this column. 0 means none. */
    int lru;              /* Least recently used entry has the smallest value */
  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
  tAttachMask writeMask;  /* Start a write transaction on these databases */
  tAttachMask cookieMask; /* Bitmask of schema verified databases */
  u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
  u8 mayAbort;         /* True if statement may throw an ABORT exception */
  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
  int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
#ifndef SQLITE_OMIT_SHARED_CACHE
  int nTableLock;        /* Number of locks in aTableLock */
  TableLock *aTableLock; /* Required table locks for shared-cache mode */







|
|







2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
    int iTable;           /* Table cursor number */
    int iColumn;          /* Table column number */
    u8 tempReg;           /* iReg is a temp register that needs to be freed */
    int iLevel;           /* Nesting level */
    int iReg;             /* Reg with value of this column. 0 means none. */
    int lru;              /* Least recently used entry has the smallest value */
  } aColCache[SQLITE_N_COLCACHE];  /* One for each column cache entry */
  yDbMask writeMask;   /* Start a write transaction on these databases */
  yDbMask cookieMask;  /* Bitmask of schema verified databases */
  u8 isMultiWrite;     /* True if statement may affect/insert multiple rows */
  u8 mayAbort;         /* True if statement may throw an ABORT exception */
  int cookieGoto;      /* Address of OP_Goto to cookie verifier subroutine */
  int cookieValue[SQLITE_MAX_ATTACHED+2];  /* Values of cookies to verify */
#ifndef SQLITE_OMIT_SHARED_CACHE
  int nTableLock;        /* Number of locks in aTableLock */
  TableLock *aTableLock; /* Required table locks for shared-cache mode */
Changes to src/tclsqlite.c.
3724
3725
3726
3727
3728
3729
3730


3731
3732
3733
3734
3735
3736
3737
    extern int Sqlitetestvfs_Init(Tcl_Interp *);
    extern int SqlitetestStat_Init(Tcl_Interp*);
    extern int Sqlitetestrtree_Init(Tcl_Interp*);
    extern int Sqlitequota_Init(Tcl_Interp*);
    extern int Sqlitemultiplex_Init(Tcl_Interp*);
    extern int SqliteSuperlock_Init(Tcl_Interp*);
    extern int SqlitetestSyscall_Init(Tcl_Interp*);


#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
    extern int TestSession_Init(Tcl_Interp*);
#endif
#ifdef SQLITE_ENABLE_ZIPVFS
    extern int Zipvfs_Init(Tcl_Interp*);
    Zipvfs_Init(interp);
#endif







>
>







3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
    extern int Sqlitetestvfs_Init(Tcl_Interp *);
    extern int SqlitetestStat_Init(Tcl_Interp*);
    extern int Sqlitetestrtree_Init(Tcl_Interp*);
    extern int Sqlitequota_Init(Tcl_Interp*);
    extern int Sqlitemultiplex_Init(Tcl_Interp*);
    extern int SqliteSuperlock_Init(Tcl_Interp*);
    extern int SqlitetestSyscall_Init(Tcl_Interp*);
    extern int Sqlitetestfuzzer_Init(Tcl_Interp*);
    extern int Sqlitetestwholenumber_Init(Tcl_Interp*);
#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
    extern int TestSession_Init(Tcl_Interp*);
#endif
#ifdef SQLITE_ENABLE_ZIPVFS
    extern int Zipvfs_Init(Tcl_Interp*);
    Zipvfs_Init(interp);
#endif
3764
3765
3766
3767
3768
3769
3770


3771
3772
3773
3774
3775
3776
3777
    Sqlitetestvfs_Init(interp);
    SqlitetestStat_Init(interp);
    Sqlitetestrtree_Init(interp);
    Sqlitequota_Init(interp);
    Sqlitemultiplex_Init(interp);
    SqliteSuperlock_Init(interp);
    SqlitetestSyscall_Init(interp);


#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
    TestSession_Init(interp);
#endif

    Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0);

#ifdef SQLITE_SSE







>
>







3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
    Sqlitetestvfs_Init(interp);
    SqlitetestStat_Init(interp);
    Sqlitetestrtree_Init(interp);
    Sqlitequota_Init(interp);
    Sqlitemultiplex_Init(interp);
    SqliteSuperlock_Init(interp);
    SqlitetestSyscall_Init(interp);
    Sqlitetestfuzzer_Init(interp);
    Sqlitetestwholenumber_Init(interp);
#if defined(SQLITE_ENABLE_SESSION) && defined(SQLITE_ENABLE_PREUPDATE_HOOK)
    TestSession_Init(interp);
#endif

    Tcl_CreateObjCommand(interp,"load_testfixture_extensions",init_all_cmd,0,0);

#ifdef SQLITE_SSE
Added src/test_fuzzer.c.
































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
/*
** 2011 March 24
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** Code for demonstartion virtual table that generates variations
** on an input word at increasing edit distances from the original.
**
** A fuzzer virtual table is created like this:
**
**     CREATE VIRTUAL TABLE temp.f USING fuzzer;
**
** The name of the new virtual table in the example above is "f".
** Note that all fuzzer virtual tables must be TEMP tables.  The
** "temp." prefix in front of the table name is required when the
** table is being created.  The "temp." prefix can be omitted when
** using the table as long as the name is unambiguous.
**
** Before being used, the fuzzer needs to be programmed by giving it
** character transformations and a cost associated with each transformation.
** Examples:
**
**    INSERT INTO f(cFrom,cTo,Cost) VALUES('','a',100);
**
** The above statement says that the cost of inserting a letter 'a' is
** 100.  (All costs are integers.  We recommend that costs be scaled so
** that the average cost is around 100.)
**
**    INSERT INTO f(cFrom,cTo,Cost) VALUES('b','',87);
**
** The above statement says that the cost of deleting a single letter
** 'b' is 87.
**
**    INSERT INTO f(cFrom,cTo,Cost) VALUES('o','oe',38);
**    INSERT INTO f(cFrom,cTo,Cost) VALUES('oe','o',40);
**
** This third example says that the cost of transforming the single
** letter "o" into the two-letter sequence "oe" is 38 and that the
** cost of transforming "oe" back into "o" is 40.
**
** After all the transformation costs have been set, the fuzzer table
** can be queried as follows:
**
**    SELECT word, distance FROM f
**     WHERE word MATCH 'abcdefg'
**       AND distance<200;
**
** This first query outputs the string "abcdefg" and all strings that
** can be derived from that string by appling the specified transformations.
** The strings are output together with their total transformation cost
** (called "distance") and appear in order of increasing cost.  No string
** is output more than once.  If there are multiple ways to transform the
** target string into the output string then the lowest cost transform is
** the one that is returned.  In the example, the search is limited to 
** strings with a total distance of less than 200.
**
** It is important to put some kind of a limit on the fuzzer output.  This
** can be either in the form of a LIMIT clause at the end of the query,
** or better, a "distance<NNN" constraint where NNN is some number.  The
** running time and memory requirement is exponential in the value of NNN 
** so you want to make sure that NNN is not too big.  A value of NNN that
** is about twice the average transformation cost seems to give good results.
**
** The fuzzer table can be useful for tasks such as spelling correction.
** Suppose there is a second table vocabulary(w) where the w column contains
** all correctly spelled words.   Let $word be a word you want to look up.
**
**   SELECT vocabulary.w FROM f, vocabulary
**    WHERE f.word MATCH $word
**      AND f.distance<=200
**      AND f.word=vocabulary.w
**    LIMIT 20
**
** The query above gives the 20 closest words to the $word being tested.
** (Note that for good performance, the vocubulary.w column should be
** indexed.)
**
** A similar query can be used to find all words in the dictionary that
** begin with some prefix $prefix:
**
**   SELECT vocabulary.w FROM f, vocabulary
**    WHERE f.word MATCH $prefix
**      AND f.distance<=200
**      AND vocabulary.w BETWEEN f.word AND (f.word || x'F7BFBFBF')
**    LIMIT 50
**
** This last query will show up to 50 words out of the vocabulary that
** match or nearly match the $prefix.
*/
#include "sqlite3.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>

#ifndef SQLITE_OMIT_VIRTUALTABLE

/*
** Forward declaration of objects used by this implementation
*/
typedef struct fuzzer_vtab fuzzer_vtab;
typedef struct fuzzer_cursor fuzzer_cursor;
typedef struct fuzzer_rule fuzzer_rule;
typedef struct fuzzer_seen fuzzer_seen;
typedef struct fuzzer_stem fuzzer_stem;

/*
** Type of the "cost" of an edit operation.  Might be changed to
** "float" or "double" or "sqlite3_int64" in the future.
*/
typedef int fuzzer_cost;


/*
** Each transformation rule is stored as an instance of this object.
** All rules are kept on a linked list sorted by rCost.
*/
struct fuzzer_rule {
  fuzzer_rule *pNext;        /* Next rule in order of increasing rCost */
  fuzzer_cost rCost;         /* Cost of this transformation */
  int nFrom, nTo;            /* Length of the zFrom and zTo strings */
  char *zFrom;               /* Transform from */
  char zTo[4];               /* Transform to (extra space appended) */
};

/*
** A stem object is used to generate variants.  It is also used to record
** previously generated outputs.
**
** Every stem is added to a hash table as it is output.  Generation of
** duplicate stems is suppressed.
**
** Active stems (those that might generate new outputs) are kepts on a linked
** list sorted by increasing cost.  The cost is the sum of rBaseCost and
** pRule->rCost.
*/
struct fuzzer_stem {
  char *zBasis;              /* Word being fuzzed */
  int nBasis;                /* Length of the zBasis string */
  const fuzzer_rule *pRule;  /* Current rule to apply */
  int n;                     /* Apply pRule at this character offset */
  fuzzer_cost rBaseCost;     /* Base cost of getting to zBasis */
  fuzzer_cost rCostX;        /* Precomputed rBaseCost + pRule->rCost */
  fuzzer_stem *pNext;        /* Next stem in rCost order */
  fuzzer_stem *pHash;        /* Next stem with same hash on zBasis */
};

/* 
** A fuzzer virtual-table object 
*/
struct fuzzer_vtab {
  sqlite3_vtab base;         /* Base class - must be first */
  char *zClassName;          /* Name of this class.  Default: "fuzzer" */
  fuzzer_rule *pRule;        /* All active rules in this fuzzer */
  fuzzer_rule *pNewRule;     /* New rules to add when last cursor expires */
  int nCursor;               /* Number of active cursors */
};

#define FUZZER_HASH  4001    /* Hash table size */
#define FUZZER_NQUEUE  20    /* Number of slots on the stem queue */

/* A fuzzer cursor object */
struct fuzzer_cursor {
  sqlite3_vtab_cursor base;  /* Base class - must be first */
  sqlite3_int64 iRowid;      /* The rowid of the current word */
  fuzzer_vtab *pVtab;        /* The virtual table this cursor belongs to */
  fuzzer_cost rLimit;        /* Maximum cost of any term */
  fuzzer_stem *pStem;        /* Stem with smallest rCostX */
  fuzzer_stem *pDone;        /* Stems already processed to completion */
  fuzzer_stem *aQueue[FUZZER_NQUEUE];  /* Queue of stems with higher rCostX */
  int mxQueue;               /* Largest used index in aQueue[] */
  char *zBuf;                /* Temporary use buffer */
  int nBuf;                  /* Bytes allocated for zBuf */
  int nStem;                 /* Number of stems allocated */
  fuzzer_rule nullRule;      /* Null rule used first */
  fuzzer_stem *apHash[FUZZER_HASH]; /* Hash of previously generated terms */
};

/* Methods for the fuzzer module */
static int fuzzerConnect(
  sqlite3 *db,
  void *pAux,
  int argc, const char *const*argv,
  sqlite3_vtab **ppVtab,
  char **pzErr
){
  fuzzer_vtab *pNew;
  int n;
  if( strcmp(argv[1],"temp")!=0 ){
    *pzErr = sqlite3_mprintf("%s virtual tables must be TEMP", argv[0]);
    return SQLITE_ERROR;
  }
  n = strlen(argv[0]) + 1;
  pNew = sqlite3_malloc( sizeof(*pNew) + n );
  if( pNew==0 ) return SQLITE_NOMEM;
  pNew->zClassName = (char*)&pNew[1];
  memcpy(pNew->zClassName, argv[0], n);
  sqlite3_declare_vtab(db, "CREATE TABLE x(word,distance,cFrom,cTo,cost)");
  memset(pNew, 0, sizeof(*pNew));
  *ppVtab = &pNew->base;
  return SQLITE_OK;
}
/* Note that for this virtual table, the xCreate and xConnect
** methods are identical. */

static int fuzzerDisconnect(sqlite3_vtab *pVtab){
  fuzzer_vtab *p = (fuzzer_vtab*)pVtab;
  assert( p->nCursor==0 );
  do{
    while( p->pRule ){
      fuzzer_rule *pRule = p->pRule;
      p->pRule = pRule->pNext;
      sqlite3_free(pRule);
    }
    p->pRule = p->pNewRule;
    p->pNewRule = 0;
  }while( p->pRule );
  sqlite3_free(p);
  return SQLITE_OK;
}
/* The xDisconnect and xDestroy methods are also the same */

/*
** The two input rule lists are both sorted in order of increasing
** cost.  Merge them together into a single list, sorted by cost, and
** return a pointer to the head of that list.
*/
static fuzzer_rule *fuzzerMergeRules(fuzzer_rule *pA, fuzzer_rule *pB){
  fuzzer_rule head;
  fuzzer_rule *pTail;

  pTail =  &head;
  while( pA && pB ){
    if( pA->rCost<=pB->rCost ){
      pTail->pNext = pA;
      pTail = pA;
      pA = pA->pNext;
    }else{
      pTail->pNext = pB;
      pTail = pB;
      pB = pB->pNext;
    }
  }
  if( pA==0 ){
    pTail->pNext = pB;
  }else{
    pTail->pNext = pA;
  }
  return head.pNext;
}


/*
** Open a new fuzzer cursor.
*/
static int fuzzerOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
  fuzzer_vtab *p = (fuzzer_vtab*)pVTab;
  fuzzer_cursor *pCur;
  pCur = sqlite3_malloc( sizeof(*pCur) );
  if( pCur==0 ) return SQLITE_NOMEM;
  memset(pCur, 0, sizeof(*pCur));
  pCur->pVtab = p;
  *ppCursor = &pCur->base;
  if( p->nCursor==0 && p->pNewRule ){
    unsigned int i;
    fuzzer_rule *pX;
    fuzzer_rule *a[15];
    for(i=0; i<sizeof(a)/sizeof(a[0]); i++) a[i] = 0;
    while( (pX = p->pNewRule)!=0 ){
      p->pNewRule = pX->pNext;
      pX->pNext = 0;
      for(i=0; a[i] && i<sizeof(a)/sizeof(a[0])-1; i++){
        pX = fuzzerMergeRules(a[i], pX);
        a[i] = 0;
      }
      a[i] = fuzzerMergeRules(a[i], pX);
    }
    for(pX=a[0], i=1; i<sizeof(a)/sizeof(a[0]); i++){
      pX = fuzzerMergeRules(a[i], pX);
    }
    p->pRule = fuzzerMergeRules(p->pRule, pX);
  }
  p->nCursor++;
  return SQLITE_OK;
}

/*
** Free all stems in a list.
*/
static void fuzzerClearStemList(fuzzer_stem *pStem){
  while( pStem ){
    fuzzer_stem *pNext = pStem->pNext;
    sqlite3_free(pStem);
    pStem = pNext;
  }
}

/*
** Free up all the memory allocated by a cursor.  Set it rLimit to 0
** to indicate that it is at EOF.
*/
static void fuzzerClearCursor(fuzzer_cursor *pCur, int clearHash){
  int i;
  fuzzerClearStemList(pCur->pStem);
  fuzzerClearStemList(pCur->pDone);
  for(i=0; i<FUZZER_NQUEUE; i++) fuzzerClearStemList(pCur->aQueue[i]);
  pCur->rLimit = (fuzzer_cost)0;
  if( clearHash && pCur->nStem ){
    pCur->mxQueue = 0;
    pCur->pStem = 0;
    pCur->pDone = 0;
    memset(pCur->aQueue, 0, sizeof(pCur->aQueue));
    memset(pCur->apHash, 0, sizeof(pCur->apHash));
  }
  pCur->nStem = 0;
}

/*
** Close a fuzzer cursor.
*/
static int fuzzerClose(sqlite3_vtab_cursor *cur){
  fuzzer_cursor *pCur = (fuzzer_cursor *)cur;
  fuzzerClearCursor(pCur, 0);
  sqlite3_free(pCur->zBuf);
  pCur->pVtab->nCursor--;
  sqlite3_free(pCur);
  return SQLITE_OK;
}

/*
** Compute the current output term for a fuzzer_stem.
*/
static int fuzzerRender(
  fuzzer_stem *pStem,   /* The stem to be rendered */
  char **pzBuf,         /* Write results into this buffer.  realloc if needed */
  int *pnBuf            /* Size of the buffer */
){
  const fuzzer_rule *pRule = pStem->pRule;
  int n;
  char *z;

  n = pStem->nBasis + pRule->nTo - pRule->nFrom;
  if( (*pnBuf)<n+1 ){
    (*pzBuf) = sqlite3_realloc((*pzBuf), n+100);
    if( (*pzBuf)==0 ) return SQLITE_NOMEM;
    (*pnBuf) = n+100;
  }
  n = pStem->n;
  z = *pzBuf;
  if( n<0 ){
    memcpy(z, pStem->zBasis, pStem->nBasis+1);
  }else{
    memcpy(z, pStem->zBasis, n);
    memcpy(&z[n], pRule->zTo, pRule->nTo);
    memcpy(&z[n+pRule->nTo], &pStem->zBasis[n+pRule->nFrom], 
           pStem->nBasis-n-pRule->nFrom+1);
  }
  return SQLITE_OK;
}

/*
** Compute a hash on zBasis.
*/
static unsigned int fuzzerHash(const char *z){
  unsigned int h = 0;
  while( *z ){ h = (h<<3) ^ (h>>29) ^ *(z++); }
  return h % FUZZER_HASH;
}

/*
** Current cost of a stem
*/
static fuzzer_cost fuzzerCost(fuzzer_stem *pStem){
  return pStem->rCostX = pStem->rBaseCost + pStem->pRule->rCost;
}

#if 0
/*
** Print a description of a fuzzer_stem on stderr.
*/
static void fuzzerStemPrint(
  const char *zPrefix,
  fuzzer_stem *pStem,
  const char *zSuffix
){
  if( pStem->n<0 ){
    fprintf(stderr, "%s[%s](%d)-->self%s",
       zPrefix,
       pStem->zBasis, pStem->rBaseCost,
       zSuffix
    );
  }else{
    char *zBuf = 0;
    int nBuf = 0;
    if( fuzzerRender(pStem, &zBuf, &nBuf)!=SQLITE_OK ) return;
    fprintf(stderr, "%s[%s](%d)-->{%s}(%d)%s",
      zPrefix,
      pStem->zBasis, pStem->rBaseCost, zBuf, pStem->,
      zSuffix
    );
    sqlite3_free(zBuf);
  }
}
#endif

/*
** Return 1 if the string to which the cursor is point has already
** been emitted.  Return 0 if not.  Return -1 on a memory allocation
** failures.
*/
static int fuzzerSeen(fuzzer_cursor *pCur, fuzzer_stem *pStem){
  unsigned int h;
  fuzzer_stem *pLookup;

  if( fuzzerRender(pStem, &pCur->zBuf, &pCur->nBuf)==SQLITE_NOMEM ){
    return -1;
  }
  h = fuzzerHash(pCur->zBuf);
  pLookup = pCur->apHash[h];
    while( pLookup && strcmp(pLookup->zBasis, pCur->zBuf)!=0 ){
    pLookup = pLookup->pHash;
  }
  return pLookup!=0;
}

/*
** Advance a fuzzer_stem to its next value.   Return 0 if there are
** no more values that can be generated by this fuzzer_stem.  Return
** -1 on a memory allocation failure.
*/
static int fuzzerAdvance(fuzzer_cursor *pCur, fuzzer_stem *pStem){
  const fuzzer_rule *pRule;
  while( (pRule = pStem->pRule)!=0 ){
    while( pStem->n < pStem->nBasis - pRule->nFrom ){
      pStem->n++;
      if( pRule->nFrom==0
       || memcmp(&pStem->zBasis[pStem->n], pRule->zFrom, pRule->nFrom)==0
      ){
        /* Found a rewrite case.  Make sure it is not a duplicate */
        int rc = fuzzerSeen(pCur, pStem);
        if( rc<0 ) return -1;
        if( rc==0 ){
          fuzzerCost(pStem);
          return 1;
        }
      }
    }
    pStem->n = -1;
    pStem->pRule = pRule->pNext;
    if( pStem->pRule && fuzzerCost(pStem)>pCur->rLimit ) pStem->pRule = 0;
  }
  return 0;
}

/*
** The two input stem lists are both sorted in order of increasing
** rCostX.  Merge them together into a single list, sorted by rCostX, and
** return a pointer to the head of that new list.
*/
static fuzzer_stem *fuzzerMergeStems(fuzzer_stem *pA, fuzzer_stem *pB){
  fuzzer_stem head;
  fuzzer_stem *pTail;

  pTail =  &head;
  while( pA && pB ){
    if( pA->rCostX<=pB->rCostX ){
      pTail->pNext = pA;
      pTail = pA;
      pA = pA->pNext;
    }else{
      pTail->pNext = pB;
      pTail = pB;
      pB = pB->pNext;
    }
  }
  if( pA==0 ){
    pTail->pNext = pB;
  }else{
    pTail->pNext = pA;
  }
  return head.pNext;
}

/*
** Load pCur->pStem with the lowest-cost stem.  Return a pointer
** to the lowest-cost stem.
*/
static fuzzer_stem *fuzzerLowestCostStem(fuzzer_cursor *pCur){
  fuzzer_stem *pBest, *pX;
  int iBest;
  int i;

  if( pCur->pStem==0 ){
    iBest = -1;
    pBest = 0;
    for(i=0; i<=pCur->mxQueue; i++){
      pX = pCur->aQueue[i];
      if( pX==0 ) continue;
      if( pBest==0 || pBest->rCostX>pX->rCostX ){
        pBest = pX;
        iBest = i;
      }
    } 
    if( pBest ){
      pCur->aQueue[iBest] = pBest->pNext;
      pBest->pNext = 0;
      pCur->pStem = pBest;
    }
  }
  return pCur->pStem;
}

/*
** Insert pNew into queue of pending stems.  Then find the stem
** with the lowest rCostX and move it into pCur->pStem.
** list.  The insert is done such the pNew is in the correct order
** according to fuzzer_stem.zBaseCost+fuzzer_stem.pRule->rCost.
*/
static fuzzer_stem *fuzzerInsert(fuzzer_cursor *pCur, fuzzer_stem *pNew){
  fuzzer_stem *pX;
  int i;

  /* If pCur->pStem exists and is greater than pNew, then make pNew
  ** the new pCur->pStem and insert the old pCur->pStem instead.
  */
  if( (pX = pCur->pStem)!=0 && pX->rCostX>pNew->rCostX ){
    pNew->pNext = 0;
    pCur->pStem = pNew;
    pNew = pX;
  }

  /* Insert the new value */
  pNew->pNext = 0;
  pX = pNew;
  for(i=0; i<=pCur->mxQueue; i++){
    if( pCur->aQueue[i] ){
      pX = fuzzerMergeStems(pX, pCur->aQueue[i]);
      pCur->aQueue[i] = 0;
    }else{
      pCur->aQueue[i] = pX;
      break;
    }
  }
  if( i>pCur->mxQueue ){
    if( i<FUZZER_NQUEUE ){
      pCur->mxQueue = i;
      pCur->aQueue[i] = pX;
    }else{
      assert( pCur->mxQueue==FUZZER_NQUEUE-1 );
      pX = fuzzerMergeStems(pX, pCur->aQueue[FUZZER_NQUEUE-1]);
      pCur->aQueue[FUZZER_NQUEUE-1] = pX;
    }
  }

  return fuzzerLowestCostStem(pCur);
}

/*
** Allocate a new fuzzer_stem.  Add it to the hash table but do not
** link it into either the pCur->pStem or pCur->pDone lists.
*/
static fuzzer_stem *fuzzerNewStem(
  fuzzer_cursor *pCur,
  const char *zWord,
  fuzzer_cost rBaseCost
){
  fuzzer_stem *pNew;
  unsigned int h;

  pNew = sqlite3_malloc( sizeof(*pNew) + strlen(zWord) + 1 );
  if( pNew==0 ) return 0;
  memset(pNew, 0, sizeof(*pNew));
  pNew->zBasis = (char*)&pNew[1];
  pNew->nBasis = strlen(zWord);
  memcpy(pNew->zBasis, zWord, pNew->nBasis+1);
  pNew->pRule = pCur->pVtab->pRule;
  pNew->n = -1;
  pNew->rBaseCost = pNew->rCostX = rBaseCost;
  h = fuzzerHash(pNew->zBasis);
  pNew->pHash = pCur->apHash[h];
  pCur->apHash[h] = pNew;
  pCur->nStem++;
  return pNew;
}


/*
** Advance a cursor to its next row of output
*/
static int fuzzerNext(sqlite3_vtab_cursor *cur){
  fuzzer_cursor *pCur = (fuzzer_cursor*)cur;
  int rc;
  fuzzer_stem *pStem, *pNew;

  pCur->iRowid++;

  /* Use the element the cursor is currently point to to create
  ** a new stem and insert the new stem into the priority queue.
  */
  pStem = pCur->pStem;
  if( pStem->rCostX>0 ){
    rc = fuzzerRender(pStem, &pCur->zBuf, &pCur->nBuf);
    if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
    pNew = fuzzerNewStem(pCur, pCur->zBuf, pStem->rCostX);
    if( pNew ){
      if( fuzzerAdvance(pCur, pNew)==0 ){
        pNew->pNext = pCur->pDone;
        pCur->pDone = pNew;
      }else{
        if( fuzzerInsert(pCur, pNew)==pNew ){
          return SQLITE_OK;
        }
      }
    }else{
      return SQLITE_NOMEM;
    }
  }

  /* Adjust the priority queue so that the first element of the
  ** stem list is the next lowest cost word.
  */
  while( (pStem = pCur->pStem)!=0 ){
    if( fuzzerAdvance(pCur, pStem) ){
      pCur->pStem = 0;
      pStem = fuzzerInsert(pCur, pStem);
      if( (rc = fuzzerSeen(pCur, pStem))!=0 ){
        if( rc<0 ) return SQLITE_NOMEM;
        continue;
      }
      return SQLITE_OK;  /* New word found */
    }
    pCur->pStem = 0;
    pStem->pNext = pCur->pDone;
    pCur->pDone = pStem;
    if( fuzzerLowestCostStem(pCur) ){
      rc = fuzzerSeen(pCur, pCur->pStem);
      if( rc<0 ) return SQLITE_NOMEM;
      if( rc==0 ){
        return SQLITE_OK;
      }
    }
  }

  /* Reach this point only if queue has been exhausted and there is
  ** nothing left to be output. */
  pCur->rLimit = (fuzzer_cost)0;
  return SQLITE_OK;
}

/*
** Called to "rewind" a cursor back to the beginning so that
** it starts its output over again.  Always called at least once
** prior to any fuzzerColumn, fuzzerRowid, or fuzzerEof call.
*/
static int fuzzerFilter(
  sqlite3_vtab_cursor *pVtabCursor, 
  int idxNum, const char *idxStr,
  int argc, sqlite3_value **argv
){
  fuzzer_cursor *pCur = (fuzzer_cursor *)pVtabCursor;
  const char *zWord = 0;
  fuzzer_stem *pStem;

  fuzzerClearCursor(pCur, 1);
  pCur->rLimit = 2147483647;
  if( idxNum==1 ){
    zWord = (const char*)sqlite3_value_text(argv[0]);
  }else if( idxNum==2 ){
    pCur->rLimit = (fuzzer_cost)sqlite3_value_int(argv[0]);
  }else if( idxNum==3 ){
    zWord = (const char*)sqlite3_value_text(argv[0]);
    pCur->rLimit = (fuzzer_cost)sqlite3_value_int(argv[1]);
  }
  if( zWord==0 ) zWord = "";
  pCur->pStem = pStem = fuzzerNewStem(pCur, zWord, (fuzzer_cost)0);
  if( pStem==0 ) return SQLITE_NOMEM;
  pCur->nullRule.pNext = pCur->pVtab->pRule;
  pCur->nullRule.rCost = 0;
  pCur->nullRule.nFrom = 0;
  pCur->nullRule.nTo = 0;
  pCur->nullRule.zFrom = "";
  pStem->pRule = &pCur->nullRule;
  pStem->n = pStem->nBasis;
  pCur->iRowid = 1;
  return SQLITE_OK;
}

/*
** Only the word and distance columns have values.  All other columns
** return NULL
*/
static int fuzzerColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
  fuzzer_cursor *pCur = (fuzzer_cursor*)cur;
  if( i==0 ){
    /* the "word" column */
    if( fuzzerRender(pCur->pStem, &pCur->zBuf, &pCur->nBuf)==SQLITE_NOMEM ){
      return SQLITE_NOMEM;
    }
    sqlite3_result_text(ctx, pCur->zBuf, -1, SQLITE_TRANSIENT);
  }else if( i==1 ){
    /* the "distance" column */
    sqlite3_result_int(ctx, pCur->pStem->rCostX);
  }else{
    /* All other columns are NULL */
    sqlite3_result_null(ctx);
  }
  return SQLITE_OK;
}

/*
** The rowid.
*/
static int fuzzerRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  fuzzer_cursor *pCur = (fuzzer_cursor*)cur;
  *pRowid = pCur->iRowid;
  return SQLITE_OK;
}

/*
** When the fuzzer_cursor.rLimit value is 0 or less, that is a signal
** that the cursor has nothing more to output.
*/
static int fuzzerEof(sqlite3_vtab_cursor *cur){
  fuzzer_cursor *pCur = (fuzzer_cursor*)cur;
  return pCur->rLimit<=(fuzzer_cost)0;
}

/*
** Search for terms of these forms:
**
**       word MATCH $str
**       distance < $value
**       distance <= $value
**
** The distance< and distance<= are both treated as distance<=.
** The query plan number is as follows:
**
**   0:    None of the terms above are found
**   1:    There is a "word MATCH" term with $str in filter.argv[0].
**   2:    There is a "distance<" term with $value in filter.argv[0].
**   3:    Both "word MATCH" and "distance<" with $str in argv[0] and
**         $value in argv[1].
*/
static int fuzzerBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
  int iPlan = 0;
  int iDistTerm = -1;
  int i;
  const struct sqlite3_index_constraint *pConstraint;
  pConstraint = pIdxInfo->aConstraint;
  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
    if( pConstraint->usable==0 ) continue;
    if( (iPlan & 1)==0 
     && pConstraint->iColumn==0
     && pConstraint->op==SQLITE_INDEX_CONSTRAINT_MATCH
    ){
      iPlan |= 1;
      pIdxInfo->aConstraintUsage[i].argvIndex = 1;
      pIdxInfo->aConstraintUsage[i].omit = 1;
    }
    if( (iPlan & 2)==0
     && pConstraint->iColumn==1
     && (pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT
           || pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE)
    ){
      iPlan |= 2;
      iDistTerm = i;
    }
  }
  if( iPlan==2 ){
    pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = 1;
  }else if( iPlan==3 ){
    pIdxInfo->aConstraintUsage[iDistTerm].argvIndex = 2;
  }
  pIdxInfo->idxNum = iPlan;
  if( pIdxInfo->nOrderBy==1
   && pIdxInfo->aOrderBy[0].iColumn==1
   && pIdxInfo->aOrderBy[0].desc==0
  ){
    pIdxInfo->orderByConsumed = 1;
  }
  pIdxInfo->estimatedCost = (double)10000;
   
  return SQLITE_OK;
}

/*
** Disallow all attempts to DELETE or UPDATE.  Only INSERTs are allowed.
**
** On an insert, the cFrom, cTo, and cost columns are used to construct
** a new rule.   All other columns are ignored.  The rule is ignored
** if cFrom and cTo are identical.  A NULL value for cFrom or cTo is
** interpreted as an empty string.  The cost must be positive.
*/
static int fuzzerUpdate(
  sqlite3_vtab *pVTab,
  int argc,
  sqlite3_value **argv,
  sqlite_int64 *pRowid
){
  fuzzer_vtab *p = (fuzzer_vtab*)pVTab;
  fuzzer_rule *pRule;
  const char *zFrom;
  int nFrom;
  const char *zTo;
  int nTo;
  fuzzer_cost rCost;
  if( argc!=7 ){
    sqlite3_free(pVTab->zErrMsg);
    pVTab->zErrMsg = sqlite3_mprintf("cannot delete from a %s virtual table",
                                     p->zClassName);
    return SQLITE_CONSTRAINT;
  }
  if( sqlite3_value_type(argv[0])!=SQLITE_NULL ){
    sqlite3_free(pVTab->zErrMsg);
    pVTab->zErrMsg = sqlite3_mprintf("cannot update a %s virtual table",
                                     p->zClassName);
    return SQLITE_CONSTRAINT;
  }
  zFrom = (char*)sqlite3_value_text(argv[4]);
  if( zFrom==0 ) zFrom = "";
  zTo = (char*)sqlite3_value_text(argv[5]);
  if( zTo==0 ) zTo = "";
  if( strcmp(zFrom,zTo)==0 ){
    /* Silently ignore null transformations */
    return SQLITE_OK;
  }
  rCost = sqlite3_value_int(argv[6]);
  if( rCost<=0 ){
    sqlite3_free(pVTab->zErrMsg);
    pVTab->zErrMsg = sqlite3_mprintf("cost must be positive");
    return SQLITE_CONSTRAINT;    
  }
  nFrom = strlen(zFrom);
  nTo = strlen(zTo);
  pRule = sqlite3_malloc( sizeof(*pRule) + nFrom + nTo );
  if( pRule==0 ){
    return SQLITE_NOMEM;
  }
  pRule->zFrom = &pRule->zTo[nTo+1];
  pRule->nFrom = nFrom;
  memcpy(pRule->zFrom, zFrom, nFrom+1);
  memcpy(pRule->zTo, zTo, nTo+1);
  pRule->nTo = nTo;
  pRule->rCost = rCost;
  pRule->pNext = p->pNewRule;
  p->pNewRule = pRule;
  return SQLITE_OK;
}

/*
** A virtual table module that provides read-only access to a
** Tcl global variable namespace.
*/
static sqlite3_module fuzzerModule = {
  0,                           /* iVersion */
  fuzzerConnect,
  fuzzerConnect,
  fuzzerBestIndex,
  fuzzerDisconnect, 
  fuzzerDisconnect,
  fuzzerOpen,                  /* xOpen - open a cursor */
  fuzzerClose,                 /* xClose - close a cursor */
  fuzzerFilter,                /* xFilter - configure scan constraints */
  fuzzerNext,                  /* xNext - advance a cursor */
  fuzzerEof,                   /* xEof - check for end of scan */
  fuzzerColumn,                /* xColumn - read data */
  fuzzerRowid,                 /* xRowid - read data */
  fuzzerUpdate,                /* xUpdate - INSERT */
  0,                           /* xBegin */
  0,                           /* xSync */
  0,                           /* xCommit */
  0,                           /* xRollback */
  0,                           /* xFindMethod */
  0,                           /* xRename */
};

#endif /* SQLITE_OMIT_VIRTUALTABLE */


/*
** Register the fuzzer virtual table
*/
int fuzzer_register(sqlite3 *db){
  int rc = SQLITE_OK;
#ifndef SQLITE_OMIT_VIRTUALTABLE
  rc = sqlite3_create_module(db, "fuzzer", &fuzzerModule, 0);
#endif
  return rc;
}

#ifdef SQLITE_TEST
#include <tcl.h>
/*
** Decode a pointer to an sqlite3 object.
*/
extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);

/*
** Register the echo virtual table module.
*/
static int register_fuzzer_module(
  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int objc,              /* Number of arguments */
  Tcl_Obj *CONST objv[]  /* Command arguments */
){
  sqlite3 *db;
  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "DB");
    return TCL_ERROR;
  }
  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  fuzzer_register(db);
  return TCL_OK;
}


/*
** Register commands with the TCL interpreter.
*/
int Sqlitetestfuzzer_Init(Tcl_Interp *interp){
  static struct {
     char *zName;
     Tcl_ObjCmdProc *xProc;
     void *clientData;
  } aObjCmd[] = {
     { "register_fuzzer_module",   register_fuzzer_module, 0 },
  };
  int i;
  for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 
        aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
  }
  return TCL_OK;
}

#endif /* SQLITE_TEST */
Changes to src/test_multiplex.c.
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
** "chunks" such that the total DB file size may exceed the maximum
** file size of the underlying file system.
**
*/
#include "sqlite3.h"
#include <string.h>
#include <assert.h>





#include "sqliteInt.h"











/*
** For a build without mutexes, no-op the mutex calls.
*/
#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
#define sqlite3_mutex_free(X)
#define sqlite3_mutex_enter(X)
#define sqlite3_mutex_try(X)      SQLITE_OK
#define sqlite3_mutex_leave(X)
#define sqlite3_mutex_held(X)     ((void)(X),1)
#define sqlite3_mutex_notheld(X)  ((void)(X),1)
#endif /* SQLITE_THREADSAFE==0 */


/************************ Shim Definitions ******************************/



/* This is the limit on the chunk size.  It may be changed by calling
** the sqlite3_multiplex_set() interface.

*/
#define SQLITE_MULTIPLEX_CHUNK_SIZE 0x40000000

/* Default limit on number of chunks.  Care should be taken
** so that values for chunks numbers fit in the SQLITE_MULTIPLEX_EXT_FMT
** format specifier. It may be changed by calling
** the sqlite3_multiplex_set() interface.
*/
#define SQLITE_MULTIPLEX_MAX_CHUNKS 32

/* If SQLITE_MULTIPLEX_EXT_OVWR is defined, the 
** last SQLITE_MULTIPLEX_EXT_SZ characters of the 
** filename will be overwritten, otherwise, the 
** multiplex extension is simply appended to the filename.







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

















>
>

|
>

|
>



|







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
** "chunks" such that the total DB file size may exceed the maximum
** file size of the underlying file system.
**
*/
#include "sqlite3.h"
#include <string.h>
#include <assert.h>
#include "test_multiplex.h"

#ifndef SQLITE_CORE
  #define SQLITE_CORE 1  /* Disable the API redefinition in sqlite3ext.h */
#endif
#include "sqlite3ext.h"

/* 
** These should be defined to be the same as the values in 
** sqliteInt.h.  They are defined seperately here so that
** the multiplex VFS shim can be built as a loadable 
** module.
*/
#define UNUSED_PARAMETER(x) (void)(x)
#define MAX_PAGE_SIZE       0x10000
#define DEFAULT_SECTOR_SIZE 0x1000

/*
** For a build without mutexes, no-op the mutex calls.
*/
#if defined(SQLITE_THREADSAFE) && SQLITE_THREADSAFE==0
#define sqlite3_mutex_alloc(X)    ((sqlite3_mutex*)8)
#define sqlite3_mutex_free(X)
#define sqlite3_mutex_enter(X)
#define sqlite3_mutex_try(X)      SQLITE_OK
#define sqlite3_mutex_leave(X)
#define sqlite3_mutex_held(X)     ((void)(X),1)
#define sqlite3_mutex_notheld(X)  ((void)(X),1)
#endif /* SQLITE_THREADSAFE==0 */


/************************ Shim Definitions ******************************/

#define SQLITE_MULTIPLEX_VFS_NAME "multiplex"

/* This is the limit on the chunk size.  It may be changed by calling
** the xFileControl() interface.  It will be rounded up to a 
** multiple of MAX_PAGE_SIZE.  We default it here to 1GB.
*/
#define SQLITE_MULTIPLEX_CHUNK_SIZE (MAX_PAGE_SIZE*16384)

/* Default limit on number of chunks.  Care should be taken
** so that values for chunks numbers fit in the SQLITE_MULTIPLEX_EXT_FMT
** format specifier. It may be changed by calling
** the xFileControl() interface.
*/
#define SQLITE_MULTIPLEX_MAX_CHUNKS 32

/* If SQLITE_MULTIPLEX_EXT_OVWR is defined, the 
** last SQLITE_MULTIPLEX_EXT_SZ characters of the 
** filename will be overwritten, otherwise, the 
** multiplex extension is simply appended to the filename.
78
79
80
81
82
83
84



85
86
87
88
89
90
91
*/
struct multiplexGroup {
  sqlite3_file **pReal;            /* Handles to each chunk */
  char *bOpen;                     /* array of bools - 0 if chunk not opened */
  char *zName;                     /* Base filename of this group */
  int nName;                       /* Length of base filename */
  int flags;                       /* Flags used for original opening */



  multiplexGroup *pNext, *pPrev;   /* Doubly linked list of all group objects */
};

/*
** An instance of the following object represents each open connection
** to a file that is multiplex'ed.  This object is a 
** subclass of sqlite3_file.  The sqlite3_file object for the underlying







>
>
>







97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
*/
struct multiplexGroup {
  sqlite3_file **pReal;            /* Handles to each chunk */
  char *bOpen;                     /* array of bools - 0 if chunk not opened */
  char *zName;                     /* Base filename of this group */
  int nName;                       /* Length of base filename */
  int flags;                       /* Flags used for original opening */
  int nChunkSize;                  /* Chunk size used for this group */
  int nMaxChunks;                  /* Max number of chunks for this group */
  int bEnabled;                    /* TRUE to use Multiplex VFS for this file */
  multiplexGroup *pNext, *pPrev;   /* Doubly linked list of all group objects */
};

/*
** An instance of the following object represents each open connection
** to a file that is multiplex'ed.  This object is a 
** subclass of sqlite3_file.  The sqlite3_file object for the underlying
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
  */
  sqlite3_mutex *pMutex;

  /* List of multiplexGroup objects.
  */
  multiplexGroup *pGroups;

  /* Chunk params.
  */
  int nChunkSize;
  int nMaxChunks;

  /* Storage for temp file names.  Allocated during 
  ** initialization to the max pathname of the underlying VFS.
  */
  char *zName;

} gMultiplex;

/************************* Utility Routines *********************************/
/*
** Acquire and release the mutex used to serialize access to the
** list of multiplexGroups.
*/
static void multiplexEnter(void){ sqlite3_mutex_enter(gMultiplex.pMutex); }
static void multiplexLeave(void){ sqlite3_mutex_leave(gMultiplex.pMutex); }
















/* Translate an sqlite3_file* that is really a multiplexGroup* into
** the sqlite3_file* for the underlying original VFS.
*/
static sqlite3_file *multiplexSubOpen(multiplexConn *pConn, int iChunk, int *rc, int *pOutFlags){
  multiplexGroup *pGroup = pConn->pGroup;
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;        /* Real VFS */
  if( iChunk<gMultiplex.nMaxChunks ){
    sqlite3_file *pSubOpen = pGroup->pReal[iChunk];    /* Real file descriptor */
    if( !pGroup->bOpen[iChunk] ){
      memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1);
      if( iChunk ){
#ifdef SQLITE_MULTIPLEX_EXT_OVWR
        sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+pGroup->nName-SQLITE_MULTIPLEX_EXT_SZ, SQLITE_MULTIPLEX_EXT_FMT, iChunk);
#else







<
<
<
<
<














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







|







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
  */
  sqlite3_mutex *pMutex;

  /* List of multiplexGroup objects.
  */
  multiplexGroup *pGroups;






  /* Storage for temp file names.  Allocated during 
  ** initialization to the max pathname of the underlying VFS.
  */
  char *zName;

} gMultiplex;

/************************* Utility Routines *********************************/
/*
** Acquire and release the mutex used to serialize access to the
** list of multiplexGroups.
*/
static void multiplexEnter(void){ sqlite3_mutex_enter(gMultiplex.pMutex); }
static void multiplexLeave(void){ sqlite3_mutex_leave(gMultiplex.pMutex); }

/*
** Compute a string length that is limited to what can be stored in
** lower 30 bits of a 32-bit signed integer.
**
** The value returned will never be negative.  Nor will it ever be greater
** than the actual length of the string.  For very long strings (greater
** than 1GiB) the value returned might be less than the true string length.
*/
int multiplexStrlen30(const char *z){
  const char *z2 = z;
  if( z==0 ) return 0;
  while( *z2 ){ z2++; }
  return 0x3fffffff & (int)(z2 - z);
}

/* Translate an sqlite3_file* that is really a multiplexGroup* into
** the sqlite3_file* for the underlying original VFS.
*/
static sqlite3_file *multiplexSubOpen(multiplexConn *pConn, int iChunk, int *rc, int *pOutFlags){
  multiplexGroup *pGroup = pConn->pGroup;
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;        /* Real VFS */
  if( iChunk<pGroup->nMaxChunks ){
    sqlite3_file *pSubOpen = pGroup->pReal[iChunk];    /* Real file descriptor */
    if( !pGroup->bOpen[iChunk] ){
      memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1);
      if( iChunk ){
#ifdef SQLITE_MULTIPLEX_EXT_OVWR
        sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+pGroup->nName-SQLITE_MULTIPLEX_EXT_SZ, SQLITE_MULTIPLEX_EXT_FMT, iChunk);
#else
186
187
188
189
190
191
192
























































193
194
195
196
197
198
199
    }
    *rc = SQLITE_OK;
    return pSubOpen;
  }
  *rc = SQLITE_FULL;
  return NULL;
}

























































/************************* VFS Method Wrappers *****************************/

/*
** This is the xOpen method used for the "multiplex" VFS.
**
** Most of the work is done by the underlying original VFS.  This method







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







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
    }
    *rc = SQLITE_OK;
    return pSubOpen;
  }
  *rc = SQLITE_FULL;
  return NULL;
}

/*
** This is the implementation of the multiplex_control() SQL function.
*/
static void multiplexControlFunc(
  sqlite3_context *context,
  int argc,
  sqlite3_value **argv
){
  int rc = SQLITE_OK;
  sqlite3 *db = sqlite3_context_db_handle(context);
  int op;
  int iVal;

  if( !db || argc!=2 ){ 
    rc = SQLITE_ERROR; 
  }else{
    /* extract params */
    op = sqlite3_value_int(argv[0]);
    iVal = sqlite3_value_int(argv[1]);
    /* map function op to file_control op */
    switch( op ){
      case 1: 
        op = MULTIPLEX_CTRL_ENABLE; 
        break;
      case 2: 
        op = MULTIPLEX_CTRL_SET_CHUNK_SIZE; 
        break;
      case 3: 
        op = MULTIPLEX_CTRL_SET_MAX_CHUNKS; 
        break;
      default:
        rc = SQLITE_NOTFOUND;
        break;
    }
  }
  if( rc==SQLITE_OK ){
    rc = sqlite3_file_control(db, 0, op, &iVal);
  }
  sqlite3_result_error_code(context, rc);
}

/*
** This is the entry point to register the auto-extension for the 
** multiplex_control() function.
*/
static int multiplexFuncInit(
  sqlite3 *db, 
  char **pzErrMsg, 
  const sqlite3_api_routines *pApi
){
  int rc;
  rc = sqlite3_create_function(db, "multiplex_control", 2, SQLITE_ANY, 
      0, multiplexControlFunc, 0, 0);
  return rc;
}

/************************* VFS Method Wrappers *****************************/

/*
** This is the xOpen method used for the "multiplex" VFS.
**
** Most of the work is done by the underlying original VFS.  This method
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
  int *pOutFlags             /* Flags showing results of opening */
){
  int rc;                                        /* Result code */
  multiplexConn *pMultiplexOpen;                 /* The new multiplex file descriptor */
  multiplexGroup *pGroup;                        /* Corresponding multiplexGroup object */
  sqlite3_file *pSubOpen;                        /* Real file descriptor */
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
  int nName = sqlite3Strlen30(zName);
  int i;
  int sz;

  UNUSED_PARAMETER(pVfs);

  /* We need to create a group structure and manage
  ** access to this group of files.
  */
  multiplexEnter();
  pMultiplexOpen = (multiplexConn*)pConn;
  /* allocate space for group */
  sz = sizeof(multiplexGroup)                         /* multiplexGroup */
     + (sizeof(sqlite3_file *)*gMultiplex.nMaxChunks) /* pReal[] */
     + (pOrigVfs->szOsFile*gMultiplex.nMaxChunks)     /* *pReal */
     + gMultiplex.nMaxChunks                          /* bOpen[] */
     + nName + 1;                                     /* zName */
#ifndef SQLITE_MULTIPLEX_EXT_OVWR
  sz += SQLITE_MULTIPLEX_EXT_SZ;
  assert(nName+SQLITE_MULTIPLEX_EXT_SZ < pOrigVfs->mxPathname);
#else
  assert(nName >= SQLITE_MULTIPLEX_EXT_SZ);
  assert(nName < pOrigVfs->mxPathname);
#endif
  pGroup = sqlite3_malloc( sz );
  if( pGroup==0 ){
    rc=SQLITE_NOMEM;
  }else{
    /* assign pointers to extra space allocated */
    char *p = (char *)&pGroup[1];
    pMultiplexOpen->pGroup = pGroup;
    memset(pGroup, 0, sz);



    pGroup->pReal = (sqlite3_file **)p;
    p += (sizeof(sqlite3_file *)*gMultiplex.nMaxChunks);
    for(i=0; i<gMultiplex.nMaxChunks; i++){
      pGroup->pReal[i] = (sqlite3_file *)p;
      p += pOrigVfs->szOsFile;
    }

    pGroup->bOpen = p;
    p += gMultiplex.nMaxChunks;
    pGroup->zName = p;
    /* save off base filename, name length, and original open flags  */
    memcpy(pGroup->zName, zName, nName+1);
    pGroup->nName = nName;
    pGroup->flags = flags;
    pSubOpen = multiplexSubOpen(pMultiplexOpen, 0, &rc, pOutFlags);
    if( pSubOpen ){








      if( pSubOpen->pMethods->iVersion==1 ){
        pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV1;
      }else{
        pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV2;
      }
      /* place this group at the head of our list */
      pGroup->pNext = gMultiplex.pGroups;







|











|
|
|
|
|















>
>
>

|
|



>

|







>
>
>
>
>
>
>
>







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
  int *pOutFlags             /* Flags showing results of opening */
){
  int rc;                                        /* Result code */
  multiplexConn *pMultiplexOpen;                 /* The new multiplex file descriptor */
  multiplexGroup *pGroup;                        /* Corresponding multiplexGroup object */
  sqlite3_file *pSubOpen;                        /* Real file descriptor */
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
  int nName = multiplexStrlen30(zName);
  int i;
  int sz;

  UNUSED_PARAMETER(pVfs);

  /* We need to create a group structure and manage
  ** access to this group of files.
  */
  multiplexEnter();
  pMultiplexOpen = (multiplexConn*)pConn;
  /* allocate space for group */
  sz = sizeof(multiplexGroup)                                /* multiplexGroup */
     + (sizeof(sqlite3_file *)*SQLITE_MULTIPLEX_MAX_CHUNKS)  /* pReal[] */
     + (pOrigVfs->szOsFile*SQLITE_MULTIPLEX_MAX_CHUNKS)      /* *pReal */
     + SQLITE_MULTIPLEX_MAX_CHUNKS                           /* bOpen[] */
     + nName + 1;                                            /* zName */
#ifndef SQLITE_MULTIPLEX_EXT_OVWR
  sz += SQLITE_MULTIPLEX_EXT_SZ;
  assert(nName+SQLITE_MULTIPLEX_EXT_SZ < pOrigVfs->mxPathname);
#else
  assert(nName >= SQLITE_MULTIPLEX_EXT_SZ);
  assert(nName < pOrigVfs->mxPathname);
#endif
  pGroup = sqlite3_malloc( sz );
  if( pGroup==0 ){
    rc=SQLITE_NOMEM;
  }else{
    /* assign pointers to extra space allocated */
    char *p = (char *)&pGroup[1];
    pMultiplexOpen->pGroup = pGroup;
    memset(pGroup, 0, sz);
    pGroup->bEnabled = -1;
    pGroup->nChunkSize = SQLITE_MULTIPLEX_CHUNK_SIZE;
    pGroup->nMaxChunks = SQLITE_MULTIPLEX_MAX_CHUNKS;
    pGroup->pReal = (sqlite3_file **)p;
    p += (sizeof(sqlite3_file *)*pGroup->nMaxChunks);
    for(i=0; i<pGroup->nMaxChunks; i++){
      pGroup->pReal[i] = (sqlite3_file *)p;
      p += pOrigVfs->szOsFile;
    }
    /* bOpen[] vals should all be zero from memset above */
    pGroup->bOpen = p;
    p += pGroup->nMaxChunks;
    pGroup->zName = p;
    /* save off base filename, name length, and original open flags  */
    memcpy(pGroup->zName, zName, nName+1);
    pGroup->nName = nName;
    pGroup->flags = flags;
    pSubOpen = multiplexSubOpen(pMultiplexOpen, 0, &rc, pOutFlags);
    if( pSubOpen ){
      /* if this file is already larger than chunk size, disable 
      ** the multiplex feature.
      */
      sqlite3_int64 sz;
      int rc2 = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
      if( (rc2==SQLITE_OK) && (sz>pGroup->nChunkSize) ){
        pGroup->bEnabled = 0;
      }
      if( pSubOpen->pMethods->iVersion==1 ){
        pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV1;
      }else{
        pMultiplexOpen->base.pMethods = &gMultiplex.sIoMethodsV2;
      }
      /* place this group at the head of our list */
      pGroup->pNext = gMultiplex.pGroups;
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
static int multiplexDelete(
  sqlite3_vfs *pVfs,         /* The multiplex VFS */
  const char *zName,         /* Name of file to delete */
  int syncDir
){
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
  int rc = SQLITE_OK;
  int nName = sqlite3Strlen30(zName);
  int i;

  UNUSED_PARAMETER(pVfs);

  multiplexEnter();
  memcpy(gMultiplex.zName, zName, nName+1);
  for(i=0; i<gMultiplex.nMaxChunks; i++){
    int rc2;
    int exists = 0;
    if( i ){
#ifdef SQLITE_MULTIPLEX_EXT_OVWR
        sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+nName-SQLITE_MULTIPLEX_EXT_SZ, SQLITE_MULTIPLEX_EXT_FMT, i);


#else
        sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+nName, SQLITE_MULTIPLEX_EXT_FMT, i);


#endif
    }
    rc2 = pOrigVfs->xAccess(pOrigVfs, gMultiplex.zName, SQLITE_ACCESS_EXISTS, &exists);

    if( rc2==SQLITE_OK && exists){
      /* if it exists, delete it */
      rc2 = pOrigVfs->xDelete(pOrigVfs, gMultiplex.zName, syncDir);
      if( rc2!=SQLITE_OK ) rc = rc2;
    }else{
      /* stop at first "gap" */
      break;







|






|




|
>
>

|
>
>


|
>







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
static int multiplexDelete(
  sqlite3_vfs *pVfs,         /* The multiplex VFS */
  const char *zName,         /* Name of file to delete */
  int syncDir
){
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
  int rc = SQLITE_OK;
  int nName = multiplexStrlen30(zName);
  int i;

  UNUSED_PARAMETER(pVfs);

  multiplexEnter();
  memcpy(gMultiplex.zName, zName, nName+1);
  for(i=0; i<SQLITE_MULTIPLEX_MAX_CHUNKS; i++){
    int rc2;
    int exists = 0;
    if( i ){
#ifdef SQLITE_MULTIPLEX_EXT_OVWR
        sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, 
            gMultiplex.zName+nName-SQLITE_MULTIPLEX_EXT_SZ, 
            SQLITE_MULTIPLEX_EXT_FMT, i);
#else
        sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, 
            gMultiplex.zName+nName, 
            SQLITE_MULTIPLEX_EXT_FMT, i);
#endif
    }
    rc2 = pOrigVfs->xAccess(pOrigVfs, gMultiplex.zName, 
        SQLITE_ACCESS_EXISTS, &exists);
    if( rc2==SQLITE_OK && exists){
      /* if it exists, delete it */
      rc2 = pOrigVfs->xDelete(pOrigVfs, gMultiplex.zName, syncDir);
      if( rc2!=SQLITE_OK ) rc = rc2;
    }else{
      /* stop at first "gap" */
      break;
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
static int multiplexClose(sqlite3_file *pConn){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  int i;
  multiplexEnter();
  /* close any open handles */
  for(i=0; i<gMultiplex.nMaxChunks; i++){
    if( pGroup->bOpen[i] ){
      sqlite3_file *pSubOpen = pGroup->pReal[i];
      int rc2 = pSubOpen->pMethods->xClose(pSubOpen);
      if( rc2!=SQLITE_OK ) rc = rc2;
      pGroup->bOpen[i] = 0;
    }
  }







|







468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
static int multiplexClose(sqlite3_file *pConn){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  int i;
  multiplexEnter();
  /* close any open handles */
  for(i=0; i<pGroup->nMaxChunks; i++){
    if( pGroup->bOpen[i] ){
      sqlite3_file *pSubOpen = pGroup->pReal[i];
      int rc2 = pSubOpen->pMethods->xClose(pSubOpen);
      if( rc2!=SQLITE_OK ) rc = rc2;
      pGroup->bOpen[i] = 0;
    }
  }
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
static int multiplexRead(
  sqlite3_file *pConn,
  void *pBuf,
  int iAmt,
  sqlite3_int64 iOfst
){
  multiplexConn *p = (multiplexConn*)pConn;

  int rc = SQLITE_OK;
  multiplexEnter();




  while( iAmt > 0 ){
    int i = (int)(iOfst/gMultiplex.nChunkSize);
    sqlite3_file *pSubOpen = multiplexSubOpen(p, i, &rc, NULL);
    if( pSubOpen ){
      int extra = ((int)(iOfst % gMultiplex.nChunkSize) + iAmt) - gMultiplex.nChunkSize;
      if( extra<0 ) extra = 0;
      iAmt -= extra;
      rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst%gMultiplex.nChunkSize);
      if( rc!=SQLITE_OK ) break;
      pBuf = (char *)pBuf + iAmt;
      iOfst += iAmt;
      iAmt = extra;
    }else{
      rc = SQLITE_IOERR_READ;
      break;

    }
  }
  multiplexLeave();
  return rc;
}

/* Pass xWrite requests thru to the original VFS after
** determining the correct chunk to operate on.
** Break up writes across chunk boundaries.
*/
static int multiplexWrite(
  sqlite3_file *pConn,
  const void *pBuf,
  int iAmt,
  sqlite3_int64 iOfst
){
  multiplexConn *p = (multiplexConn*)pConn;

  int rc = SQLITE_OK;
  multiplexEnter();




  while( iAmt > 0 ){
    int i = (int)(iOfst/gMultiplex.nChunkSize);
    sqlite3_file *pSubOpen = multiplexSubOpen(p, i, &rc, NULL);
    if( pSubOpen ){
      int extra = ((int)(iOfst % gMultiplex.nChunkSize) + iAmt) - gMultiplex.nChunkSize;
      if( extra<0 ) extra = 0;
      iAmt -= extra;
      rc = pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst%gMultiplex.nChunkSize);
      if( rc!=SQLITE_OK ) break;
      pBuf = (char *)pBuf + iAmt;
      iOfst += iAmt;
      iAmt = extra;
    }else{
      rc = SQLITE_IOERR_WRITE;
      break;

    }
  }
  multiplexLeave();
  return rc;
}

/* Pass xTruncate requests thru to the original VFS after
** determining the correct chunk to operate on.  Delete any
** chunks above the truncate mark.
*/
static int multiplexTruncate(sqlite3_file *pConn, sqlite3_int64 size){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;





  int rc2;
  int i;
  sqlite3_file *pSubOpen;
  sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
  multiplexEnter();
  memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1);
  /* delete the chunks above the truncate limit */
  for(i=(int)(size/gMultiplex.nChunkSize)+1; i<gMultiplex.nMaxChunks; i++){
    /* close any open chunks before deleting them */
    if( pGroup->bOpen[i] ){
      pSubOpen = pGroup->pReal[i];
      rc2 = pSubOpen->pMethods->xClose(pSubOpen);
      if( rc2!=SQLITE_OK ) rc = SQLITE_IOERR_TRUNCATE;
      pGroup->bOpen[i] = 0;
    }
#ifdef SQLITE_MULTIPLEX_EXT_OVWR
    sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+pGroup->nName-SQLITE_MULTIPLEX_EXT_SZ, SQLITE_MULTIPLEX_EXT_FMT, i);


#else
    sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+pGroup->nName, SQLITE_MULTIPLEX_EXT_FMT, i);


#endif
    rc2 = pOrigVfs->xDelete(pOrigVfs, gMultiplex.zName, 0);
    if( rc2!=SQLITE_OK ) rc = SQLITE_IOERR_TRUNCATE;
  }
  pSubOpen = multiplexSubOpen(p, (int)(size/gMultiplex.nChunkSize), &rc2, NULL);
  if( pSubOpen ){
    rc2 = pSubOpen->pMethods->xTruncate(pSubOpen, size%gMultiplex.nChunkSize);
    if( rc2!=SQLITE_OK ) rc = rc2;
  }else{
    rc = SQLITE_IOERR_TRUNCATE;

  }
  multiplexLeave();
  return rc;
}

/* Pass xSync requests through to the original VFS without change
*/
static int multiplexSync(sqlite3_file *pConn, int flags){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  int i;
  multiplexEnter();
  for(i=0; i<gMultiplex.nMaxChunks; i++){
    /* if we don't have it open, we don't need to sync it */
    if( pGroup->bOpen[i] ){
      sqlite3_file *pSubOpen = pGroup->pReal[i];
      int rc2 = pSubOpen->pMethods->xSync(pSubOpen, flags);
      if( rc2!=SQLITE_OK ) rc = rc2;
    }
  }







>


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

















>


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














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

|
>
>

|
>
>

|
|
|
|
|
|
|
|
|
>













|







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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
static int multiplexRead(
  sqlite3_file *pConn,
  void *pBuf,
  int iAmt,
  sqlite3_int64 iOfst
){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  multiplexEnter();
  if( !pGroup->bEnabled ){
    sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
    rc = ( !pSubOpen ) ? SQLITE_IOERR_READ : pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
  }else{
    while( iAmt > 0 ){
      int i = (int)(iOfst / pGroup->nChunkSize);
      sqlite3_file *pSubOpen = multiplexSubOpen(p, i, &rc, NULL);
      if( pSubOpen ){
        int extra = ((int)(iOfst % pGroup->nChunkSize) + iAmt) - pGroup->nChunkSize;
        if( extra<0 ) extra = 0;
        iAmt -= extra;
        rc = pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst % pGroup->nChunkSize);
        if( rc!=SQLITE_OK ) break;
        pBuf = (char *)pBuf + iAmt;
        iOfst += iAmt;
        iAmt = extra;
      }else{
        rc = SQLITE_IOERR_READ;
        break;
      }
    }
  }
  multiplexLeave();
  return rc;
}

/* Pass xWrite requests thru to the original VFS after
** determining the correct chunk to operate on.
** Break up writes across chunk boundaries.
*/
static int multiplexWrite(
  sqlite3_file *pConn,
  const void *pBuf,
  int iAmt,
  sqlite3_int64 iOfst
){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  multiplexEnter();
  if( !pGroup->bEnabled ){
    sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
    rc = ( !pSubOpen ) ? SQLITE_IOERR_WRITE : pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
  }else{
    while( iAmt > 0 ){
      int i = (int)(iOfst / pGroup->nChunkSize);
      sqlite3_file *pSubOpen = multiplexSubOpen(p, i, &rc, NULL);
      if( pSubOpen ){
        int extra = ((int)(iOfst % pGroup->nChunkSize) + iAmt) - pGroup->nChunkSize;
        if( extra<0 ) extra = 0;
        iAmt -= extra;
        rc = pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst % pGroup->nChunkSize);
        if( rc!=SQLITE_OK ) break;
        pBuf = (char *)pBuf + iAmt;
        iOfst += iAmt;
        iAmt = extra;
      }else{
        rc = SQLITE_IOERR_WRITE;
        break;
      }
    }
  }
  multiplexLeave();
  return rc;
}

/* Pass xTruncate requests thru to the original VFS after
** determining the correct chunk to operate on.  Delete any
** chunks above the truncate mark.
*/
static int multiplexTruncate(sqlite3_file *pConn, sqlite3_int64 size){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  multiplexEnter();
  if( !pGroup->bEnabled ){
    sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
    rc = ( !pSubOpen ) ? SQLITE_IOERR_TRUNCATE : pSubOpen->pMethods->xTruncate(pSubOpen, size);
  }else{
    int rc2;
    int i;
    sqlite3_file *pSubOpen;
    sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */

    memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1);
    /* delete the chunks above the truncate limit */
    for(i=(int)(size / pGroup->nChunkSize)+1; i<pGroup->nMaxChunks; i++){
      /* close any open chunks before deleting them */
      if( pGroup->bOpen[i] ){
        pSubOpen = pGroup->pReal[i];
        rc2 = pSubOpen->pMethods->xClose(pSubOpen);
        if( rc2!=SQLITE_OK ) rc = SQLITE_IOERR_TRUNCATE;
        pGroup->bOpen[i] = 0;
      }
#ifdef SQLITE_MULTIPLEX_EXT_OVWR
      sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, 
          gMultiplex.zName+pGroup->nName-SQLITE_MULTIPLEX_EXT_SZ, 
          SQLITE_MULTIPLEX_EXT_FMT, i);
#else
      sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, 
          gMultiplex.zName+pGroup->nName, 
          SQLITE_MULTIPLEX_EXT_FMT, i);
#endif
      rc2 = pOrigVfs->xDelete(pOrigVfs, gMultiplex.zName, 0);
      if( rc2!=SQLITE_OK ) rc = SQLITE_IOERR_TRUNCATE;
    }
    pSubOpen = multiplexSubOpen(p, (int)(size / pGroup->nChunkSize), &rc2, NULL);
    if( pSubOpen ){
      rc2 = pSubOpen->pMethods->xTruncate(pSubOpen, size % pGroup->nChunkSize);
      if( rc2!=SQLITE_OK ) rc = rc2;
    }else{
      rc = SQLITE_IOERR_TRUNCATE;
    }
  }
  multiplexLeave();
  return rc;
}

/* Pass xSync requests through to the original VFS without change
*/
static int multiplexSync(sqlite3_file *pConn, int flags){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  int i;
  multiplexEnter();
  for(i=0; i<pGroup->nMaxChunks; i++){
    /* if we don't have it open, we don't need to sync it */
    if( pGroup->bOpen[i] ){
      sqlite3_file *pSubOpen = pGroup->pReal[i];
      int rc2 = pSubOpen->pMethods->xSync(pSubOpen, flags);
      if( rc2!=SQLITE_OK ) rc = rc2;
    }
  }
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
static int multiplexFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  int rc2;
  int i;
  multiplexEnter();




  *pSize = 0;
  for(i=0; i<gMultiplex.nMaxChunks; i++){
    sqlite3_file *pSubOpen = NULL;
    /* if not opened already, check to see if the chunk exists */
    if( pGroup->bOpen[i] ){
      pSubOpen = pGroup->pReal[i];
    }else{
      sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
      int exists = 0;
      memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1);
      if( i ){
#ifdef SQLITE_MULTIPLEX_EXT_OVWR
        sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+pGroup->nName-SQLITE_MULTIPLEX_EXT_SZ, SQLITE_MULTIPLEX_EXT_FMT, i);


#else
        sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, gMultiplex.zName+pGroup->nName, SQLITE_MULTIPLEX_EXT_FMT, i);


#endif
      }
      rc2 = pOrigVfs->xAccess(pOrigVfs, gMultiplex.zName, SQLITE_ACCESS_EXISTS, &exists);

      if( rc2==SQLITE_OK && exists){
        /* if it exists, open it */
        pSubOpen = multiplexSubOpen(p, i, &rc, NULL);
      }else{
        /* stop at first "gap" */
        break;
      }
    }
    if( pSubOpen ){
      sqlite3_int64 sz;
      rc2 = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
      if( rc2!=SQLITE_OK ){
        rc = rc2;
      }else{
        if( sz>gMultiplex.nChunkSize ){
          rc = SQLITE_IOERR_FSTAT;
        }
        *pSize += sz;
      }
    }else{
      break;

    }
  }
  multiplexLeave();
  return rc;
}

/* Pass xLock requests through to the original VFS unchanged.







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

|
>
>

|
>
>

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







649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
static int multiplexFileSize(sqlite3_file *pConn, sqlite3_int64 *pSize){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_OK;
  int rc2;
  int i;
  multiplexEnter();
  if( !pGroup->bEnabled ){
    sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
    rc = ( !pSubOpen ) ? SQLITE_IOERR_FSTAT : pSubOpen->pMethods->xFileSize(pSubOpen, pSize);
  }else{
    *pSize = 0;
    for(i=0; i<pGroup->nMaxChunks; i++){
      sqlite3_file *pSubOpen = NULL;
      /* if not opened already, check to see if the chunk exists */
      if( pGroup->bOpen[i] ){
        pSubOpen = pGroup->pReal[i];
      }else{
        sqlite3_vfs *pOrigVfs = gMultiplex.pOrigVfs;   /* Real VFS */
        int exists = 0;
        memcpy(gMultiplex.zName, pGroup->zName, pGroup->nName+1);
        if( i ){
#ifdef SQLITE_MULTIPLEX_EXT_OVWR
          sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, 
              gMultiplex.zName+pGroup->nName-SQLITE_MULTIPLEX_EXT_SZ, 
              SQLITE_MULTIPLEX_EXT_FMT, i);
#else
          sqlite3_snprintf(SQLITE_MULTIPLEX_EXT_SZ+1, 
              gMultiplex.zName+pGroup->nName, 
              SQLITE_MULTIPLEX_EXT_FMT, i);
#endif
        }
        rc2 = pOrigVfs->xAccess(pOrigVfs, gMultiplex.zName, 
            SQLITE_ACCESS_EXISTS, &exists);
        if( rc2==SQLITE_OK && exists){
          /* if it exists, open it */
          pSubOpen = multiplexSubOpen(p, i, &rc, NULL);
        }else{
          /* stop at first "gap" */
          break;
        }
      }
      if( pSubOpen ){
        sqlite3_int64 sz;
        rc2 = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
        if( rc2!=SQLITE_OK ){
          rc = rc2;
        }else{
          if( sz>pGroup->nChunkSize ){
            rc = SQLITE_IOERR_FSTAT;
          }
          *pSize += sz;
        }
      }else{
        break;
      }
    }
  }
  multiplexLeave();
  return rc;
}

/* Pass xLock requests through to the original VFS unchanged.
604
605
606
607
608
609
610
611

612
613
614

615
616




































617




618
619
620
621


622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
  sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
  if( pSubOpen ){
    return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
  }
  return SQLITE_IOERR_CHECKRESERVEDLOCK;
}

/* Pass xFileControl requests through to the original VFS unchanged.

*/
static int multiplexFileControl(sqlite3_file *pConn, int op, void *pArg){
  multiplexConn *p = (multiplexConn*)pConn;

  int rc;
  sqlite3_file *pSubOpen;




































  if ( op==SQLITE_FCNTL_SIZE_HINT || op==SQLITE_FCNTL_CHUNK_SIZE ) return SQLITE_OK;




  pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
  if( pSubOpen ){
    return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
  }


  return SQLITE_ERROR;
}

/* Pass xSectorSize requests through to the original VFS unchanged.
*/
static int multiplexSectorSize(sqlite3_file *pConn){
  multiplexConn *p = (multiplexConn*)pConn;
  int rc;
  sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
  if( pSubOpen ){
    return pSubOpen->pMethods->xSectorSize(pSubOpen);
  }
  return SQLITE_DEFAULT_SECTOR_SIZE;
}

/* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
*/
static int multiplexDeviceCharacteristics(sqlite3_file *pConn){
  multiplexConn *p = (multiplexConn*)pConn;
  int rc;







|
>



>
|

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











|







740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
  sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
  if( pSubOpen ){
    return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
  }
  return SQLITE_IOERR_CHECKRESERVEDLOCK;
}

/* Pass xFileControl requests through to the original VFS unchanged,
** except for any MULTIPLEX_CTRL_* requests here.
*/
static int multiplexFileControl(sqlite3_file *pConn, int op, void *pArg){
  multiplexConn *p = (multiplexConn*)pConn;
  multiplexGroup *pGroup = p->pGroup;
  int rc = SQLITE_ERROR;
  sqlite3_file *pSubOpen;

  if( !gMultiplex.isInitialized ) return SQLITE_MISUSE;
  switch( op ){
    case MULTIPLEX_CTRL_ENABLE:
      if( pArg ) {
        int bEnabled = *(int *)pArg;
        pGroup->bEnabled = bEnabled;
        rc = SQLITE_OK;
      }
      break;
    case MULTIPLEX_CTRL_SET_CHUNK_SIZE:
      if( pArg ) {
        int nChunkSize = *(int *)pArg;
        if( nChunkSize<1 ){
          rc = SQLITE_MISUSE;
        }else{
          /* Round up to nearest multiple of MAX_PAGE_SIZE. */
          nChunkSize = (nChunkSize + (MAX_PAGE_SIZE-1));
          nChunkSize &= ~(MAX_PAGE_SIZE-1);
          pGroup->nChunkSize = nChunkSize;
          rc = SQLITE_OK;
        }
      }
      break;
    case MULTIPLEX_CTRL_SET_MAX_CHUNKS:
      if( pArg ) {
        int nMaxChunks = *(int *)pArg;
        if(( nMaxChunks<1 ) || ( nMaxChunks>SQLITE_MULTIPLEX_MAX_CHUNKS )){
          rc = SQLITE_MISUSE;
        }else{
          pGroup->nMaxChunks = nMaxChunks;
          rc = SQLITE_OK;
        }
      }
      break;
    case SQLITE_FCNTL_SIZE_HINT:
    case SQLITE_FCNTL_CHUNK_SIZE:
      /* no-op these */
      rc = SQLITE_OK;
      break;
    default:
      pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
      if( pSubOpen ){
        rc = pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
      }
      break;
  }
  return rc;
}

/* Pass xSectorSize requests through to the original VFS unchanged.
*/
static int multiplexSectorSize(sqlite3_file *pConn){
  multiplexConn *p = (multiplexConn*)pConn;
  int rc;
  sqlite3_file *pSubOpen = multiplexSubOpen(p, 0, &rc, NULL);
  if( pSubOpen ){
    return pSubOpen->pMethods->xSectorSize(pSubOpen);
  }
  return DEFAULT_SECTOR_SIZE;
}

/* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
*/
static int multiplexDeviceCharacteristics(sqlite3_file *pConn){
  multiplexConn *p = (multiplexConn*)pConn;
  int rc;
702
703
704
705
706
707
708
709

710
711
712
713
714
715
716
717
718
    return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
  }
  return SQLITE_OK;
}

/************************** Public Interfaces *****************************/
/*
** Initialize the multiplex VFS shim.  Use the VFS named zOrigVfsName

** as the VFS that does the actual work.  Use the default if
** zOrigVfsName==NULL.  
**
** The multiplex VFS shim is named "multiplex".  It will become the default
** VFS if makeDefault is non-zero.
**
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
** during start-up.
*/







|
>
|
|







882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
    return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
  }
  return SQLITE_OK;
}

/************************** Public Interfaces *****************************/
/*
** CAPI: Initialize the multiplex VFS shim - sqlite3_multiplex_initialize()
**
** Use the VFS named zOrigVfsName as the VFS that does the actual work.  
** Use the default if zOrigVfsName==NULL.  
**
** The multiplex VFS shim is named "multiplex".  It will become the default
** VFS if makeDefault is non-zero.
**
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
** during start-up.
*/
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
    return SQLITE_NOMEM;
  }
  gMultiplex.zName = sqlite3_malloc(pOrigVfs->mxPathname);
  if( !gMultiplex.zName ){
    sqlite3_mutex_free(gMultiplex.pMutex);
    return SQLITE_NOMEM;
  }
  gMultiplex.nChunkSize = SQLITE_MULTIPLEX_CHUNK_SIZE;
  gMultiplex.nMaxChunks = SQLITE_MULTIPLEX_MAX_CHUNKS;
  gMultiplex.pGroups = NULL;
  gMultiplex.isInitialized = 1;
  gMultiplex.pOrigVfs = pOrigVfs;
  gMultiplex.sThisVfs = *pOrigVfs;
  gMultiplex.sThisVfs.szOsFile += sizeof(multiplexConn);
  gMultiplex.sThisVfs.zName = "multiplex";
  gMultiplex.sThisVfs.xOpen = multiplexOpen;
  gMultiplex.sThisVfs.xDelete = multiplexDelete;
  gMultiplex.sThisVfs.xAccess = multiplexAccess;
  gMultiplex.sThisVfs.xFullPathname = multiplexFullPathname;
  gMultiplex.sThisVfs.xDlOpen = multiplexDlOpen;
  gMultiplex.sThisVfs.xDlError = multiplexDlError;
  gMultiplex.sThisVfs.xDlSym = multiplexDlSym;







<
<





|







908
909
910
911
912
913
914


915
916
917
918
919
920
921
922
923
924
925
926
927
    return SQLITE_NOMEM;
  }
  gMultiplex.zName = sqlite3_malloc(pOrigVfs->mxPathname);
  if( !gMultiplex.zName ){
    sqlite3_mutex_free(gMultiplex.pMutex);
    return SQLITE_NOMEM;
  }


  gMultiplex.pGroups = NULL;
  gMultiplex.isInitialized = 1;
  gMultiplex.pOrigVfs = pOrigVfs;
  gMultiplex.sThisVfs = *pOrigVfs;
  gMultiplex.sThisVfs.szOsFile += sizeof(multiplexConn);
  gMultiplex.sThisVfs.zName = SQLITE_MULTIPLEX_VFS_NAME;
  gMultiplex.sThisVfs.xOpen = multiplexOpen;
  gMultiplex.sThisVfs.xDelete = multiplexDelete;
  gMultiplex.sThisVfs.xAccess = multiplexAccess;
  gMultiplex.sThisVfs.xFullPathname = multiplexFullPathname;
  gMultiplex.sThisVfs.xDlOpen = multiplexDlOpen;
  gMultiplex.sThisVfs.xDlError = multiplexDlError;
  gMultiplex.sThisVfs.xDlSym = multiplexDlSym;
769
770
771
772
773
774
775



776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
  gMultiplex.sIoMethodsV2 = gMultiplex.sIoMethodsV1;
  gMultiplex.sIoMethodsV2.iVersion = 2;
  gMultiplex.sIoMethodsV2.xShmMap = multiplexShmMap;
  gMultiplex.sIoMethodsV2.xShmLock = multiplexShmLock;
  gMultiplex.sIoMethodsV2.xShmBarrier = multiplexShmBarrier;
  gMultiplex.sIoMethodsV2.xShmUnmap = multiplexShmUnmap;
  sqlite3_vfs_register(&gMultiplex.sThisVfs, makeDefault);



  return SQLITE_OK;
}

/*
** Shutdown the multiplex system.
**
** All SQLite database connections must be closed before calling this
** routine.
**
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
** shutting down in order to free all remaining multiplex groups.
*/
int sqlite3_multiplex_shutdown(void){
  if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE;
  if( gMultiplex.pGroups ) return SQLITE_MISUSE;
  gMultiplex.isInitialized = 0;
  sqlite3_free(gMultiplex.zName);
  sqlite3_mutex_free(gMultiplex.pMutex);
  sqlite3_vfs_unregister(&gMultiplex.sThisVfs);
  memset(&gMultiplex, 0, sizeof(gMultiplex));
  return SQLITE_OK;
}

/*
** Adjust chunking params.  VFS should be initialized first.
** No files should be open.  Re-intializing will reset these
** to the default.
*/
int sqlite3_multiplex_set(
  int nChunkSize,                 /* Max chunk size */
  int nMaxChunks                  /* Max number of chunks */
){
  if( !gMultiplex.isInitialized ) return SQLITE_MISUSE;
  if( gMultiplex.pGroups ) return SQLITE_MISUSE;
  if( nChunkSize<32 ) return SQLITE_MISUSE;
  if( nMaxChunks<1 ) return SQLITE_MISUSE;
  if( nMaxChunks>99 ) return SQLITE_MISUSE;
  multiplexEnter();
  gMultiplex.nChunkSize = nChunkSize;
  gMultiplex.nMaxChunks = nMaxChunks;
  multiplexLeave();
  return SQLITE_OK;
}

/***************************** Test Code ***********************************/
#ifdef SQLITE_TEST
#include <tcl.h>

extern const char *sqlite3TestErrorName(int);


/*
** tclcmd: sqlite3_multiplex_initialize NAME MAKEDEFAULT
*/
static int test_multiplex_initialize(







>
>
>




|


















<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



<







948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980





















981
982
983

984
985
986
987
988
989
990
  gMultiplex.sIoMethodsV2 = gMultiplex.sIoMethodsV1;
  gMultiplex.sIoMethodsV2.iVersion = 2;
  gMultiplex.sIoMethodsV2.xShmMap = multiplexShmMap;
  gMultiplex.sIoMethodsV2.xShmLock = multiplexShmLock;
  gMultiplex.sIoMethodsV2.xShmBarrier = multiplexShmBarrier;
  gMultiplex.sIoMethodsV2.xShmUnmap = multiplexShmUnmap;
  sqlite3_vfs_register(&gMultiplex.sThisVfs, makeDefault);

  sqlite3_auto_extension((void*)multiplexFuncInit);

  return SQLITE_OK;
}

/*
** CAPI: Shutdown the multiplex system - sqlite3_multiplex_shutdown()
**
** All SQLite database connections must be closed before calling this
** routine.
**
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
** shutting down in order to free all remaining multiplex groups.
*/
int sqlite3_multiplex_shutdown(void){
  if( gMultiplex.isInitialized==0 ) return SQLITE_MISUSE;
  if( gMultiplex.pGroups ) return SQLITE_MISUSE;
  gMultiplex.isInitialized = 0;
  sqlite3_free(gMultiplex.zName);
  sqlite3_mutex_free(gMultiplex.pMutex);
  sqlite3_vfs_unregister(&gMultiplex.sThisVfs);
  memset(&gMultiplex, 0, sizeof(gMultiplex));
  return SQLITE_OK;
}






















/***************************** Test Code ***********************************/
#ifdef SQLITE_TEST
#include <tcl.h>

extern const char *sqlite3TestErrorName(int);


/*
** tclcmd: sqlite3_multiplex_initialize NAME MAKEDEFAULT
*/
static int test_multiplex_initialize(
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
  /* Call sqlite3_multiplex_shutdown() */
  rc = sqlite3_multiplex_shutdown();
  Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);

  return TCL_OK;
}

/*
** tclcmd: sqlite3_multiplex_set CHUNK_SIZE MAX_CHUNKS
*/
static int test_multiplex_set(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  int nChunkSize;                 /* Max chunk size */
  int nMaxChunks;                 /* Max number of chunks */
  int rc;                         /* Value returned by sqlite3_multiplex_set() */

  UNUSED_PARAMETER(clientData);

  /* Process arguments */
  if( objc!=3 ){
    Tcl_WrongNumArgs(interp, 1, objv, "CHUNK_SIZE MAX_CHUNKS");
    return TCL_ERROR;
  }
  if( Tcl_GetIntFromObj(interp, objv[1], &nChunkSize) ) return TCL_ERROR;
  if( Tcl_GetIntFromObj(interp, objv[2], &nMaxChunks) ) return TCL_ERROR;

  /* Invoke sqlite3_multiplex_set() */
  rc = sqlite3_multiplex_set(nChunkSize, nMaxChunks);

  Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
  return TCL_OK;
}

/*
** tclcmd:  sqlite3_multiplex_dump
*/
static int test_multiplex_dump(
  void * clientData,
  Tcl_Interp *interp,
  int objc,







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







1036
1037
1038
1039
1040
1041
1042






























1043
1044
1045
1046
1047
1048
1049
  /* Call sqlite3_multiplex_shutdown() */
  rc = sqlite3_multiplex_shutdown();
  Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);

  return TCL_OK;
}































/*
** tclcmd:  sqlite3_multiplex_dump
*/
static int test_multiplex_dump(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962






























































963
964
965
966
967
968
969
970
971
972
973
974
975
976
977

978
979
980
981
982
983
984
985
986
987
          Tcl_NewStringObj(pGroup->zName, -1));
    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(pGroup->nName));
    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(pGroup->flags));

    /* count number of chunks with open handles */
    for(i=0; i<gMultiplex.nMaxChunks; i++){
      if( pGroup->bOpen[i] ) nChunks++;
    }
    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(nChunks));

    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(gMultiplex.nChunkSize));
    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(gMultiplex.nMaxChunks));

    Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
  }
  multiplexLeave();
  Tcl_SetObjResult(interp, pResult);
  return TCL_OK;
}































































/*
** This routine registers the custom TCL commands defined in this
** module.  This should be the only procedure visible from outside
** of this module.
*/
int Sqlitemultiplex_Init(Tcl_Interp *interp){
  static struct {
     char *zName;
     Tcl_ObjCmdProc *xProc;
  } aCmd[] = {
    { "sqlite3_multiplex_initialize", test_multiplex_initialize },
    { "sqlite3_multiplex_shutdown", test_multiplex_shutdown },
    { "sqlite3_multiplex_set", test_multiplex_set },
    { "sqlite3_multiplex_dump", test_multiplex_dump },

  };
  int i;

  for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
    Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
  }

  return TCL_OK;
}
#endif







|






|

|







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













<

>










1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167

1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
          Tcl_NewStringObj(pGroup->zName, -1));
    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(pGroup->nName));
    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(pGroup->flags));

    /* count number of chunks with open handles */
    for(i=0; i<pGroup->nMaxChunks; i++){
      if( pGroup->bOpen[i] ) nChunks++;
    }
    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(nChunks));

    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(pGroup->nChunkSize));
    Tcl_ListObjAppendElement(interp, pGroupTerm,
          Tcl_NewIntObj(pGroup->nMaxChunks));

    Tcl_ListObjAppendElement(interp, pResult, pGroupTerm);
  }
  multiplexLeave();
  Tcl_SetObjResult(interp, pResult);
  return TCL_OK;
}

/*
** Tclcmd: test_multiplex_control HANDLE DBNAME SUB-COMMAND ?INT-VALUE?
*/
static int test_multiplex_control(
  ClientData cd,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  int rc;                         /* Return code from file_control() */
  int idx;                        /* Index in aSub[] */
  Tcl_CmdInfo cmdInfo;            /* Command info structure for HANDLE */
  sqlite3 *db;                    /* Underlying db handle for HANDLE */
  int iValue = 0;
  void *pArg = 0;

  struct SubCommand {
    const char *zName;
    int op;
    int argtype;
  } aSub[] = {
    { "enable",       MULTIPLEX_CTRL_ENABLE,           1 },
    { "chunk_size",   MULTIPLEX_CTRL_SET_CHUNK_SIZE,   1 },
    { "max_chunks",   MULTIPLEX_CTRL_SET_MAX_CHUNKS,   1 },
    { 0, 0, 0 }
  };

  if( objc!=5 ){
    Tcl_WrongNumArgs(interp, 1, objv, "HANDLE DBNAME SUB-COMMAND INT-VALUE");
    return TCL_ERROR;
  }

  if( 0==Tcl_GetCommandInfo(interp, Tcl_GetString(objv[1]), &cmdInfo) ){
    Tcl_AppendResult(interp, "expected database handle, got \"", 0);
    Tcl_AppendResult(interp, Tcl_GetString(objv[1]), "\"", 0);
    return TCL_ERROR;
  }else{
    db = *(sqlite3 **)cmdInfo.objClientData;
  }

  rc = Tcl_GetIndexFromObjStruct(
      interp, objv[3], aSub, sizeof(aSub[0]), "sub-command", 0, &idx
  );
  if( rc!=TCL_OK ) return rc;

  switch( aSub[idx].argtype ){
    case 1:
      if( Tcl_GetIntFromObj(interp, objv[4], &iValue) ){
        return TCL_ERROR;
      }
      pArg = (void *)&iValue;
      break;
    default:
      Tcl_WrongNumArgs(interp, 4, objv, "SUB-COMMAND");
      return TCL_ERROR;
  }

  rc = sqlite3_file_control(db, Tcl_GetString(objv[2]), aSub[idx].op, pArg);
  Tcl_SetResult(interp, (char *)sqlite3TestErrorName(rc), TCL_STATIC);
  return (rc==SQLITE_OK) ? TCL_OK : TCL_ERROR;
}

/*
** This routine registers the custom TCL commands defined in this
** module.  This should be the only procedure visible from outside
** of this module.
*/
int Sqlitemultiplex_Init(Tcl_Interp *interp){
  static struct {
     char *zName;
     Tcl_ObjCmdProc *xProc;
  } aCmd[] = {
    { "sqlite3_multiplex_initialize", test_multiplex_initialize },
    { "sqlite3_multiplex_shutdown", test_multiplex_shutdown },

    { "sqlite3_multiplex_dump", test_multiplex_dump },
    { "sqlite3_multiplex_control", test_multiplex_control },
  };
  int i;

  for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){
    Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0);
  }

  return TCL_OK;
}
#endif
Added src/test_multiplex.h.






















































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
** 2011 March 18
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file contains a VFS "shim" - a layer that sits in between the
** pager and the real VFS.
**
** This particular shim enforces a multiplex system on DB files.  
** This shim shards/partitions a single DB file into smaller 
** "chunks" such that the total DB file size may exceed the maximum
** file size of the underlying file system.
**
*/

#ifndef _TEST_MULTIPLEX_H
#define _TEST_MULTIPLEX_H

/*
** CAPI: File-control Operations Supported by Multiplex VFS
**
** Values interpreted by the xFileControl method of a Multiplex VFS db file-handle.
**
** MULTIPLEX_CTRL_ENABLE:
**   This file control is used to enable or disable the multiplex
**   shim.
**
** MULTIPLEX_CTRL_SET_CHUNK_SIZE:
**   This file control is used to set the maximum allowed chunk 
**   size for a multiplex file set.  The chunk size should be 
**   a multiple of SQLITE_MAX_PAGE_SIZE, and will be rounded up
**   if not.
**
** MULTIPLEX_CTRL_SET_MAX_CHUNKS:
**   This file control is used to set the maximum number of chunks
**   allowed to be used for a mutliplex file set.
*/
#define MULTIPLEX_CTRL_ENABLE          214014
#define MULTIPLEX_CTRL_SET_CHUNK_SIZE  214015
#define MULTIPLEX_CTRL_SET_MAX_CHUNKS  214016

/*
** CAPI: Initialize the multiplex VFS shim - sqlite3_multiplex_initialize()
**
** Use the VFS named zOrigVfsName as the VFS that does the actual work.  
** Use the default if zOrigVfsName==NULL.  
**
** The multiplex VFS shim is named "multiplex".  It will become the default
** VFS if makeDefault is non-zero.
**
** An auto-extension is registered which will make the function 
** multiplex_control() available to database connections.  This
** function gives access to the xFileControl interface of the 
** multiplex VFS shim.
**
** SELECT multiplex_control(<op>,<val>);
** 
**   <op>=1 MULTIPLEX_CTRL_ENABLE
**   <val>=0 disable
**   <val>=1 enable
** 
**   <op>=2 MULTIPLEX_CTRL_SET_CHUNK_SIZE
**   <val> int, chunk size
** 
**   <op>=3 MULTIPLEX_CTRL_SET_MAX_CHUNKS
**   <val> int, max chunks
**
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once
** during start-up.
*/
extern int sqlite3_multiplex_initialize(const char *zOrigVfsName, int makeDefault);

/*
** CAPI: Shutdown the multiplex system - sqlite3_multiplex_shutdown()
**
** All SQLite database connections must be closed before calling this
** routine.
**
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly once while
** shutting down in order to free all remaining multiplex groups.
*/
extern int sqlite3_multiplex_shutdown(void);

#endif
Changes to src/test_syscall.c.
210
211
212
213
214
215
216






217
218
219
220
221
222
223
}

/*
** A wrapper around close().
*/
static int ts_close(int fd){
  if( tsIsFail() ){






    return -1;
  }
  return orig_close(fd);
}

/*
** A wrapper around access().







>
>
>
>
>
>







210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
}

/*
** A wrapper around close().
*/
static int ts_close(int fd){
  if( tsIsFail() ){
    /* Even if simulating an error, close the original file-descriptor. 
    ** This is to stop the test process from running out of file-descriptors
    ** when running a long test. If a call to close() appears to fail, SQLite
    ** never attempts to use the file-descriptor afterwards (or even to close
    ** it a second time).  */
    orig_close(fd);
    return -1;
  }
  return orig_close(fd);
}

/*
** A wrapper around access().
588
589
590
591
592
593
594


















595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612

613
614
615
616
617
618
619
    Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1));
  }

  Tcl_SetObjResult(interp, pList);
  Tcl_DecrRefCount(pList);
  return TCL_OK;
}



















static int test_syscall(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  struct SyscallCmd {
    const char *zName;
    Tcl_ObjCmdProc *xCmd;
  } aCmd[] = {
    { "fault",     test_syscall_fault },
    { "install",   test_syscall_install },
    { "uninstall", test_syscall_uninstall },
    { "reset",     test_syscall_reset },
    { "errno",     test_syscall_errno },
    { "exists",    test_syscall_exists },
    { "list",      test_syscall_list },

    { 0, 0 }
  };
  int iCmd;
  int rc;

  if( objc<2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ...");







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











|
|
|
|
|
|
|
>







594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    Tcl_ListObjAppendElement(interp, pList, Tcl_NewStringObj(zSys, -1));
  }

  Tcl_SetObjResult(interp, pList);
  Tcl_DecrRefCount(pList);
  return TCL_OK;
}

static int test_syscall_defaultvfs(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  sqlite3_vfs *pVfs; 

  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 2, objv, "");
    return TCL_ERROR;
  }

  pVfs = sqlite3_vfs_find(0);
  Tcl_SetObjResult(interp, Tcl_NewStringObj(pVfs->zName, -1));
  return TCL_OK;
}

static int test_syscall(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  struct SyscallCmd {
    const char *zName;
    Tcl_ObjCmdProc *xCmd;
  } aCmd[] = {
    { "fault",      test_syscall_fault },
    { "install",    test_syscall_install },
    { "uninstall",  test_syscall_uninstall },
    { "reset",      test_syscall_reset },
    { "errno",      test_syscall_errno },
    { "exists",     test_syscall_exists },
    { "list",       test_syscall_list },
    { "defaultvfs", test_syscall_defaultvfs },
    { 0, 0 }
  };
  int iCmd;
  int rc;

  if( objc<2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "SUB-COMMAND ...");
Added src/test_wholenumber.c.














































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
** 2011 April 02
**
** The author disclaims copyright to this source code.  In place of
** a legal notice, here is a blessing:
**
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
**
** This file implements a virtual table that returns the whole numbers
** between 1 and 4294967295, inclusive.
**
** Example:
**
**     CREATE VIRTUAL TABLE nums USING wholenumber;
**     SELECT value FROM nums WHERE value<10;
**
** Results in:
**
**     1 2 3 4 5 6 7 8 9
*/
#include "sqlite3.h"
#include <assert.h>
#include <string.h>

#ifndef SQLITE_OMIT_VIRTUALTABLE


/* A wholenumber cursor object */
typedef struct wholenumber_cursor wholenumber_cursor;
struct wholenumber_cursor {
  sqlite3_vtab_cursor base;  /* Base class - must be first */
  unsigned iValue;           /* Current value */
  unsigned mxValue;          /* Maximum value */
};

/* Methods for the wholenumber module */
static int wholenumberConnect(
  sqlite3 *db,
  void *pAux,
  int argc, const char *const*argv,
  sqlite3_vtab **ppVtab,
  char **pzErr
){
  sqlite3_vtab *pNew;
  pNew = *ppVtab = sqlite3_malloc( sizeof(*pNew) );
  if( pNew==0 ) return SQLITE_NOMEM;
  sqlite3_declare_vtab(db, "CREATE TABLE x(value)");
  memset(pNew, 0, sizeof(*pNew));
  return SQLITE_OK;
}
/* Note that for this virtual table, the xCreate and xConnect
** methods are identical. */

static int wholenumberDisconnect(sqlite3_vtab *pVtab){
  sqlite3_free(pVtab);
  return SQLITE_OK;
}
/* The xDisconnect and xDestroy methods are also the same */


/*
** Open a new wholenumber cursor.
*/
static int wholenumberOpen(sqlite3_vtab *p, sqlite3_vtab_cursor **ppCursor){
  wholenumber_cursor *pCur;
  pCur = sqlite3_malloc( sizeof(*pCur) );
  if( pCur==0 ) return SQLITE_NOMEM;
  memset(pCur, 0, sizeof(*pCur));
  *ppCursor = &pCur->base;
  return SQLITE_OK;
}

/*
** Close a wholenumber cursor.
*/
static int wholenumberClose(sqlite3_vtab_cursor *cur){
  sqlite3_free(cur);
  return SQLITE_OK;
}


/*
** Advance a cursor to its next row of output
*/
static int wholenumberNext(sqlite3_vtab_cursor *cur){
  wholenumber_cursor *pCur = (wholenumber_cursor*)cur;
  pCur->iValue++;
  return SQLITE_OK;
}

/*
** Return the value associated with a wholenumber.
*/
static int wholenumberColumn(
  sqlite3_vtab_cursor *cur,
  sqlite3_context *ctx,
  int i
){
  wholenumber_cursor *pCur = (wholenumber_cursor*)cur;
  sqlite3_result_int64(ctx, pCur->iValue);
  return SQLITE_OK;
}

/*
** The rowid.
*/
static int wholenumberRowid(sqlite3_vtab_cursor *cur, sqlite_int64 *pRowid){
  wholenumber_cursor *pCur = (wholenumber_cursor*)cur;
  *pRowid = pCur->iValue;
  return SQLITE_OK;
}

/*
** When the wholenumber_cursor.rLimit value is 0 or less, that is a signal
** that the cursor has nothing more to output.
*/
static int wholenumberEof(sqlite3_vtab_cursor *cur){
  wholenumber_cursor *pCur = (wholenumber_cursor*)cur;
  return pCur->iValue>pCur->mxValue || pCur->iValue==0;
}

/*
** Called to "rewind" a cursor back to the beginning so that
** it starts its output over again.  Always called at least once
** prior to any wholenumberColumn, wholenumberRowid, or wholenumberEof call.
**
**    idxNum   Constraints
**    ------   ---------------------
**      0      (none)
**      1      value > $argv0
**      2      value >= $argv0
**      4      value < $argv0
**      8      value <= $argv0
**
**      5      value > $argv0 AND value < $argv1
**      6      value >= $argv0 AND value < $argv1
**      9      value > $argv0 AND value <= $argv1
**     10      value >= $argv0 AND value <= $argv1
*/
static int wholenumberFilter(
  sqlite3_vtab_cursor *pVtabCursor, 
  int idxNum, const char *idxStr,
  int argc, sqlite3_value **argv
){
  wholenumber_cursor *pCur = (wholenumber_cursor *)pVtabCursor;
  sqlite3_int64 v;
  int i = 0;
  pCur->iValue = 1;
  pCur->mxValue = 0xffffffff;  /* 4294967295 */
  if( idxNum & 3 ){
    v = sqlite3_value_int64(argv[0]) + (idxNum&1);
    if( v>pCur->iValue && v<=pCur->mxValue ) pCur->iValue = v;
    i++;
  }
  if( idxNum & 12 ){
    v = sqlite3_value_int64(argv[i]) - ((idxNum>>2)&1);
    if( v>=pCur->iValue && v<pCur->mxValue ) pCur->mxValue = v;
  }
  return SQLITE_OK;
}

/*
** Search for terms of these forms:
**
**  (1)  value > $value
**  (2)  value >= $value
**  (4)  value < $value
**  (8)  value <= $value
**
** idxNum is an ORed combination of 1 or 2 with 4 or 8.
*/
static int wholenumberBestIndex(
  sqlite3_vtab *tab,
  sqlite3_index_info *pIdxInfo
){
  int i;
  int idxNum = 0;
  int argvIdx = 1;
  int ltIdx = -1;
  int gtIdx = -1;
  const struct sqlite3_index_constraint *pConstraint;
  pConstraint = pIdxInfo->aConstraint;
  for(i=0; i<pIdxInfo->nConstraint; i++, pConstraint++){
    if( pConstraint->usable==0 ) continue;
    if( (idxNum & 3)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_GT ){
      idxNum |= 1;
      ltIdx = i;
    }
    if( (idxNum & 3)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_GE ){
      idxNum |= 2;
      ltIdx = i;
    }
    if( (idxNum & 12)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_LT ){
      idxNum |= 4;
      gtIdx = i;
    }
    if( (idxNum & 12)==0 && pConstraint->op==SQLITE_INDEX_CONSTRAINT_LE ){
      idxNum |= 8;
      gtIdx = i;
    }
  }
  pIdxInfo->idxNum = idxNum;
  if( ltIdx>=0 ){
    pIdxInfo->aConstraintUsage[ltIdx].argvIndex = argvIdx++;
    pIdxInfo->aConstraintUsage[ltIdx].omit = 1;
  }
  if( gtIdx>=0 ){
    pIdxInfo->aConstraintUsage[gtIdx].argvIndex = argvIdx;
    pIdxInfo->aConstraintUsage[gtIdx].omit = 1;
  }
  if( pIdxInfo->nOrderBy==1
   && pIdxInfo->aOrderBy[0].desc==0
  ){
    pIdxInfo->orderByConsumed = 1;
  }
  pIdxInfo->estimatedCost = (double)1;
  return SQLITE_OK;
}

/*
** A virtual table module that provides read-only access to a
** Tcl global variable namespace.
*/
static sqlite3_module wholenumberModule = {
  0,                         /* iVersion */
  wholenumberConnect,
  wholenumberConnect,
  wholenumberBestIndex,
  wholenumberDisconnect, 
  wholenumberDisconnect,
  wholenumberOpen,           /* xOpen - open a cursor */
  wholenumberClose,          /* xClose - close a cursor */
  wholenumberFilter,         /* xFilter - configure scan constraints */
  wholenumberNext,           /* xNext - advance a cursor */
  wholenumberEof,            /* xEof - check for end of scan */
  wholenumberColumn,         /* xColumn - read data */
  wholenumberRowid,          /* xRowid - read data */
  0,                         /* xUpdate */
  0,                         /* xBegin */
  0,                         /* xSync */
  0,                         /* xCommit */
  0,                         /* xRollback */
  0,                         /* xFindMethod */
  0,                         /* xRename */
};

#endif /* SQLITE_OMIT_VIRTUALTABLE */


/*
** Register the wholenumber virtual table
*/
int wholenumber_register(sqlite3 *db){
  int rc = SQLITE_OK;
#ifndef SQLITE_OMIT_VIRTUALTABLE
  rc = sqlite3_create_module(db, "wholenumber", &wholenumberModule, 0);
#endif
  return rc;
}

#ifdef SQLITE_TEST
#include <tcl.h>
/*
** Decode a pointer to an sqlite3 object.
*/
extern int getDbPointer(Tcl_Interp *interp, const char *zA, sqlite3 **ppDb);

/*
** Register the echo virtual table module.
*/
static int register_wholenumber_module(
  ClientData clientData, /* Pointer to sqlite3_enable_XXX function */
  Tcl_Interp *interp,    /* The TCL interpreter that invoked this command */
  int objc,              /* Number of arguments */
  Tcl_Obj *CONST objv[]  /* Command arguments */
){
  sqlite3 *db;
  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "DB");
    return TCL_ERROR;
  }
  if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
  wholenumber_register(db);
  return TCL_OK;
}


/*
** Register commands with the TCL interpreter.
*/
int Sqlitetestwholenumber_Init(Tcl_Interp *interp){
  static struct {
     char *zName;
     Tcl_ObjCmdProc *xProc;
     void *clientData;
  } aObjCmd[] = {
     { "register_wholenumber_module",   register_wholenumber_module, 0 },
  };
  int i;
  for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){
    Tcl_CreateObjCommand(interp, aObjCmd[i].zName, 
        aObjCmd[i].xProc, aObjCmd[i].clientData, 0);
  }
  return TCL_OK;
}

#endif /* SQLITE_TEST */
Changes to src/vdbe.c.
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#ifdef VDBE_PROFILE
  u64 start;                 /* CPU clock count at start of opcode */
  int origPc;                /* Program counter at start of opcode */
#endif
  /*** INSERT STACK UNION HERE ***/

  assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
  sqlite3VdbeMutexArrayEnter(p);
  if( p->rc==SQLITE_NOMEM ){
    /* This happens if a malloc() inside a call to sqlite3_column_text() or
    ** sqlite3_column_text16() failed.  */
    goto no_mem;
  }
  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
  p->rc = SQLITE_OK;







|







577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
#ifdef VDBE_PROFILE
  u64 start;                 /* CPU clock count at start of opcode */
  int origPc;                /* Program counter at start of opcode */
#endif
  /*** INSERT STACK UNION HERE ***/

  assert( p->magic==VDBE_MAGIC_RUN );  /* sqlite3_step() verifies this */
  sqlite3VdbeEnter(p);
  if( p->rc==SQLITE_NOMEM ){
    /* This happens if a malloc() inside a call to sqlite3_column_text() or
    ** sqlite3_column_text16() failed.  */
    goto no_mem;
  }
  assert( p->rc==SQLITE_OK || p->rc==SQLITE_BUSY );
  p->rc = SQLITE_OK;
1400
1401
1402
1403
1404
1405
1406

1407
1408
1409
1410
1411
1412
1413
  if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
    assert( pOp>aOp );
    assert( pOp[-1].p4type==P4_COLLSEQ );
    assert( pOp[-1].opcode==OP_CollSeq );
    ctx.pColl = pOp[-1].p4.pColl;
  }
  (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */

  if( db->mallocFailed ){
    /* Even though a malloc() has failed, the implementation of the
    ** user function may have called an sqlite3_result_XXX() function
    ** to return a value. The following call releases any resources
    ** associated with such a value.
    */
    sqlite3VdbeMemRelease(&ctx.s);







>







1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
  if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
    assert( pOp>aOp );
    assert( pOp[-1].p4type==P4_COLLSEQ );
    assert( pOp[-1].opcode==OP_CollSeq );
    ctx.pColl = pOp[-1].p4.pColl;
  }
  (*ctx.pFunc->xFunc)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  sqlite3VdbeMutexResync(p);
  if( db->mallocFailed ){
    /* Even though a malloc() has failed, the implementation of the
    ** user function may have called an sqlite3_result_XXX() function
    ** to return a value. The following call releases any resources
    ** associated with such a value.
    */
    sqlite3VdbeMemRelease(&ctx.s);
1431
1432
1433
1434
1435
1436
1437









1438
1439
1440
1441
1442
1443
1444

  /* Copy the result of the function into register P3 */
  sqlite3VdbeChangeEncoding(&ctx.s, encoding);
  sqlite3VdbeMemMove(pOut, &ctx.s);
  if( sqlite3VdbeMemTooBig(pOut) ){
    goto too_big;
  }









  REGISTER_TRACE(pOp->p3, pOut);
  UPDATE_MAX_BLOBSIZE(pOut);
  break;
}

/* Opcode: BitAnd P1 P2 P3 * *
**







>
>
>
>
>
>
>
>
>







1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454

  /* Copy the result of the function into register P3 */
  sqlite3VdbeChangeEncoding(&ctx.s, encoding);
  sqlite3VdbeMemMove(pOut, &ctx.s);
  if( sqlite3VdbeMemTooBig(pOut) ){
    goto too_big;
  }

#if 0
  /* The app-defined function has done something that as caused this
  ** statement to expire.  (Perhaps the function called sqlite3_exec()
  ** with a CREATE TABLE statement.)
  */
  if( p->expired ) rc = SQLITE_ABORT;
#endif

  REGISTER_TRACE(pOp->p3, pOut);
  UPDATE_MAX_BLOBSIZE(pOut);
  break;
}

/* Opcode: BitAnd P1 P2 P3 * *
**
2656
2657
2658
2659
2660
2661
2662

2663
2664
2665
2666
2667
2668
2669
          if( rc!=SQLITE_OK ){
            goto abort_due_to_error;
          }
        }
        if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
          sqlite3ExpirePreparedStatements(db);
          sqlite3ResetInternalSchema(db, 0);

          db->flags = (db->flags | SQLITE_InternChanges);
        }
      }
  
      /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all 
      ** savepoints nested inside of the savepoint being operated on. */
      while( db->pSavepoint!=pSavepoint ){







>







2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
          if( rc!=SQLITE_OK ){
            goto abort_due_to_error;
          }
        }
        if( p1==SAVEPOINT_ROLLBACK && (db->flags&SQLITE_InternChanges)!=0 ){
          sqlite3ExpirePreparedStatements(db);
          sqlite3ResetInternalSchema(db, 0);
          sqlite3VdbeMutexResync(p);
          db->flags = (db->flags | SQLITE_InternChanges);
        }
      }
  
      /* Regardless of whether this is a RELEASE or ROLLBACK, destroy all 
      ** savepoints nested inside of the savepoint being operated on. */
      while( db->pSavepoint!=pSavepoint ){
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
**
** If P2 is zero, then a read-lock is obtained on the database file.
*/
case OP_Transaction: {
  Btree *pBt;

  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
  pBt = db->aDb[pOp->p1].pBt;

  if( pBt ){
    rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
    if( rc==SQLITE_BUSY ){
      p->pc = pc;
      p->rc = rc = SQLITE_BUSY;







|







2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
**
** If P2 is zero, then a read-lock is obtained on the database file.
*/
case OP_Transaction: {
  Btree *pBt;

  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  pBt = db->aDb[pOp->p1].pBt;

  if( pBt ){
    rc = sqlite3BtreeBeginTrans(pBt, pOp->p2);
    if( rc==SQLITE_BUSY ){
      p->pc = pc;
      p->rc = rc = SQLITE_BUSY;
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
  int iCookie;

  iDb = pOp->p1;
  iCookie = pOp->p3;
  assert( pOp->p3<SQLITE_N_BTREE_META );
  assert( iDb>=0 && iDb<db->nDb );
  assert( db->aDb[iDb].pBt!=0 );
  assert( (p->btreeMask & (1<<iDb))!=0 );

  sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
  pOut->u.i = iMeta;
  break;
}

/* Opcode: SetCookie P1 P2 P3 * *
**
** Write the content of register P3 (interpreted as an integer)
** into cookie number P2 of database P1.  P2==1 is the schema version.  
** P2==2 is the database format. P2==3 is the recommended pager cache 
** size, and so forth.  P1==0 is the main database file and P1==1 is the 
** database file used to store temporary tables.
**
** A transaction must be started before executing this opcode.
*/
case OP_SetCookie: {       /* in3 */
  Db *pDb;
  assert( pOp->p2<SQLITE_N_BTREE_META );
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
  pDb = &db->aDb[pOp->p1];
  assert( pDb->pBt!=0 );
  pIn3 = &aMem[pOp->p3];
  sqlite3VdbeMemIntegerify(pIn3);
  /* See note about index shifting on OP_ReadCookie */
  rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
  if( pOp->p2==BTREE_SCHEMA_VERSION ){







|




















|







2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
  int iCookie;

  iDb = pOp->p1;
  iCookie = pOp->p3;
  assert( pOp->p3<SQLITE_N_BTREE_META );
  assert( iDb>=0 && iDb<db->nDb );
  assert( db->aDb[iDb].pBt!=0 );
  assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );

  sqlite3BtreeGetMeta(db->aDb[iDb].pBt, iCookie, (u32 *)&iMeta);
  pOut->u.i = iMeta;
  break;
}

/* Opcode: SetCookie P1 P2 P3 * *
**
** Write the content of register P3 (interpreted as an integer)
** into cookie number P2 of database P1.  P2==1 is the schema version.  
** P2==2 is the database format. P2==3 is the recommended pager cache 
** size, and so forth.  P1==0 is the main database file and P1==1 is the 
** database file used to store temporary tables.
**
** A transaction must be started before executing this opcode.
*/
case OP_SetCookie: {       /* in3 */
  Db *pDb;
  assert( pOp->p2<SQLITE_N_BTREE_META );
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  pDb = &db->aDb[pOp->p1];
  assert( pDb->pBt!=0 );
  pIn3 = &aMem[pOp->p3];
  sqlite3VdbeMemIntegerify(pIn3);
  /* See note about index shifting on OP_ReadCookie */
  rc = sqlite3BtreeUpdateMeta(pDb->pBt, pOp->p2, (int)pIn3->u.i);
  if( pOp->p2==BTREE_SCHEMA_VERSION ){
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
*/
case OP_VerifyCookie: {
  int iMeta;
  int iGen;
  Btree *pBt;

  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
  pBt = db->aDb[pOp->p1].pBt;
  if( pBt ){
    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
    iGen = db->aDb[pOp->p1].pSchema->iGeneration;
  }else{
    iMeta = 0;
  }







|







2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
*/
case OP_VerifyCookie: {
  int iMeta;
  int iGen;
  Btree *pBt;

  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  pBt = db->aDb[pOp->p1].pBt;
  if( pBt ){
    sqlite3BtreeGetMeta(pBt, BTREE_SCHEMA_VERSION, (u32 *)&iMeta);
    iGen = db->aDb[pOp->p1].pSchema->iGeneration;
  }else{
    iMeta = 0;
  }
2946
2947
2948
2949
2950
2951
2952

2953
2954
2955
2956
2957
2958
2959
    ** discard the database schema, as the user code implementing the
    ** v-table would have to be ready for the sqlite3_vtab structure itself
    ** to be invalidated whenever sqlite3_step() is called from within 
    ** a v-table method.
    */
    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
      sqlite3ResetInternalSchema(db, pOp->p1);

    }

    p->expired = 1;
    rc = SQLITE_SCHEMA;
  }
  break;
}







>







2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
    ** discard the database schema, as the user code implementing the
    ** v-table would have to be ready for the sqlite3_vtab structure itself
    ** to be invalidated whenever sqlite3_step() is called from within 
    ** a v-table method.
    */
    if( db->aDb[pOp->p1].pSchema->schema_cookie!=iMeta ){
      sqlite3ResetInternalSchema(db, pOp->p1);
      sqlite3VdbeMutexResync(p);
    }

    p->expired = 1;
    rc = SQLITE_SCHEMA;
  }
  break;
}
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
  }

  nField = 0;
  pKeyInfo = 0;
  p2 = pOp->p2;
  iDb = pOp->p3;
  assert( iDb>=0 && iDb<db->nDb );
  assert( (p->btreeMask & (1<<iDb))!=0 );
  pDb = &db->aDb[iDb];
  pX = pDb->pBt;
  assert( pX!=0 );
  if( pOp->opcode==OP_OpenWrite ){
    wrFlag = 1;
    if( pDb->pSchema->file_format < p->minWriteFileFormat ){
      p->minWriteFileFormat = pDb->pSchema->file_format;







|







3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
  }

  nField = 0;
  pKeyInfo = 0;
  p2 = pOp->p2;
  iDb = pOp->p3;
  assert( iDb>=0 && iDb<db->nDb );
  assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
  pDb = &db->aDb[iDb];
  pX = pDb->pBt;
  assert( pX!=0 );
  if( pOp->opcode==OP_OpenWrite ){
    wrFlag = 1;
    if( pDb->pSchema->file_format < p->minWriteFileFormat ){
      p->minWriteFileFormat = pDb->pSchema->file_format;
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
  pOut->flags = MEM_Null;
  if( iCnt>1 ){
    rc = SQLITE_LOCKED;
    p->errorAction = OE_Abort;
  }else{
    iDb = pOp->p3;
    assert( iCnt==1 );
    assert( (p->btreeMask & (1<<iDb))!=0 );
    rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
    pOut->flags = MEM_Int;
    pOut->u.i = iMoved;
#ifndef SQLITE_OMIT_AUTOVACUUM
    if( rc==SQLITE_OK && iMoved!=0 ){
      sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
      resetSchemaOnFault = 1;







|







4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
  pOut->flags = MEM_Null;
  if( iCnt>1 ){
    rc = SQLITE_LOCKED;
    p->errorAction = OE_Abort;
  }else{
    iDb = pOp->p3;
    assert( iCnt==1 );
    assert( (p->btreeMask & (((yDbMask)1)<<iDb))!=0 );
    rc = sqlite3BtreeDropTable(db->aDb[iDb].pBt, pOp->p1, &iMoved);
    pOut->flags = MEM_Int;
    pOut->u.i = iMoved;
#ifndef SQLITE_OMIT_AUTOVACUUM
    if( rc==SQLITE_OK && iMoved!=0 ){
      sqlite3RootPageMoved(&db->aDb[iDb], iMoved, pOp->p1);
      resetSchemaOnFault = 1;
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
**
** See also: Destroy
*/
case OP_Clear: {
  int nChange;
 
  nChange = 0;
  assert( (p->btreeMask & (1<<pOp->p2))!=0 );
  rc = sqlite3BtreeClearTable(
      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
  );
  if( pOp->p3 ){
    p->nChange += nChange;
    if( pOp->p3>0 ){
      assert( memIsValid(&aMem[pOp->p3]) );







|







4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
**
** See also: Destroy
*/
case OP_Clear: {
  int nChange;
 
  nChange = 0;
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p2))!=0 );
  rc = sqlite3BtreeClearTable(
      db->aDb[pOp->p2].pBt, pOp->p1, (pOp->p3 ? &nChange : 0)
  );
  if( pOp->p3 ){
    p->nChange += nChange;
    if( pOp->p3>0 ){
      assert( memIsValid(&aMem[pOp->p3]) );
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
case OP_CreateTable: {          /* out2-prerelease */
  int pgno;
  int flags;
  Db *pDb;

  pgno = 0;
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
  pDb = &db->aDb[pOp->p1];
  assert( pDb->pBt!=0 );
  if( pOp->opcode==OP_CreateTable ){
    /* flags = BTREE_INTKEY; */
    flags = BTREE_INTKEY;
  }else{
    flags = BTREE_BLOBKEY;







|







4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
case OP_CreateTable: {          /* out2-prerelease */
  int pgno;
  int flags;
  Db *pDb;

  pgno = 0;
  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  pDb = &db->aDb[pOp->p1];
  assert( pDb->pBt!=0 );
  if( pOp->opcode==OP_CreateTable ){
    /* flags = BTREE_INTKEY; */
    flags = BTREE_INTKEY;
  }else{
    flags = BTREE_BLOBKEY;
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693


4694



4695



4696

4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
** then runs the new virtual machine.  It is thus a re-entrant opcode.
*/
case OP_ParseSchema: {
  int iDb;
  const char *zMaster;
  char *zSql;
  InitData initData;

  iDb = pOp->p1;
  assert( iDb>=0 && iDb<db->nDb );

  /* Although the mutex on the BtShared object that corresponds to
  ** database iDb (the database containing the sqlite_master table
  ** read by this instruction) is currently held, it is necessary to
  ** obtain the mutexes on all attached databases before checking if
  ** the schema of iDb is loaded. This is because, at the start of
  ** the sqlite3_exec() call below, SQLite will invoke 
  ** sqlite3BtreeEnterAll(). If all mutexes are not already held, the
  ** iDb mutex may be temporarily released to avoid deadlock. If 
  ** this happens, then some other thread may delete the in-memory 
  ** schema of database iDb before the SQL statement runs. The schema
  ** will not be reloaded becuase the db->init.busy flag is set. This
  ** can result in a "no such table: sqlite_master" or "malformed
  ** database schema" error being returned to the user.
  */


  assert( sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );



  sqlite3BtreeEnterAll(db);



  if( ALWAYS(DbHasProperty(db, iDb, DB_SchemaLoaded)) ){

    zMaster = SCHEMA_TABLE(iDb);
    initData.db = db;
    initData.iDb = pOp->p1;
    initData.pzErrMsg = &p->zErrMsg;
    zSql = sqlite3MPrintf(db,
       "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
       db->aDb[iDb].zName, zMaster, pOp->p4.z);
    if( zSql==0 ){
      rc = SQLITE_NOMEM;
    }else{
      assert( db->init.busy==0 );
      db->init.busy = 1;
      initData.rc = SQLITE_OK;
      assert( !db->mallocFailed );
      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
      if( rc==SQLITE_OK ) rc = initData.rc;
      sqlite3DbFree(db, zSql);
      db->init.busy = 0;
    }
  }
  sqlite3BtreeLeaveAll(db);
  if( rc==SQLITE_NOMEM ){
    goto no_mem;
  }
  break;  
}

#if !defined(SQLITE_OMIT_ANALYZE)








<
<
|
<
<
<
<
|
<
|
<
<
<
<
<
<

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




















<







4681
4682
4683
4684
4685
4686
4687
4688


4689




4690

4691






4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724

4725
4726
4727
4728
4729
4730
4731
** then runs the new virtual machine.  It is thus a re-entrant opcode.
*/
case OP_ParseSchema: {
  int iDb;
  const char *zMaster;
  char *zSql;
  InitData initData;



  /* Any prepared statement that invokes this opcode will hold mutexes




  ** on every btree.  This is a prerequisite for invoking 

  ** sqlite3InitCallback().






  */
#ifdef SQLITE_DEBUG
  for(iDb=0; iDb<db->nDb; iDb++){
    assert( iDb==1 || sqlite3BtreeHoldsMutex(db->aDb[iDb].pBt) );
  }
#endif
  assert( p->btreeMask == ~(yDbMask)0 );


  iDb = pOp->p1;
  assert( iDb>=0 && iDb<db->nDb );
  assert( DbHasProperty(db, iDb, DB_SchemaLoaded) );
  /* Used to be a conditional */ {
    zMaster = SCHEMA_TABLE(iDb);
    initData.db = db;
    initData.iDb = pOp->p1;
    initData.pzErrMsg = &p->zErrMsg;
    zSql = sqlite3MPrintf(db,
       "SELECT name, rootpage, sql FROM '%q'.%s WHERE %s ORDER BY rowid",
       db->aDb[iDb].zName, zMaster, pOp->p4.z);
    if( zSql==0 ){
      rc = SQLITE_NOMEM;
    }else{
      assert( db->init.busy==0 );
      db->init.busy = 1;
      initData.rc = SQLITE_OK;
      assert( !db->mallocFailed );
      rc = sqlite3_exec(db, zSql, sqlite3InitCallback, &initData, 0);
      if( rc==SQLITE_OK ) rc = initData.rc;
      sqlite3DbFree(db, zSql);
      db->init.busy = 0;
    }
  }

  if( rc==SQLITE_NOMEM ){
    goto no_mem;
  }
  break;  
}

#if !defined(SQLITE_OMIT_ANALYZE)
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
  assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
  pIn1 = &aMem[pOp->p1];
  for(j=0; j<nRoot; j++){
    aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
  }
  aRoot[j] = 0;
  assert( pOp->p5<db->nDb );
  assert( (p->btreeMask & (1<<pOp->p5))!=0 );
  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
                                 (int)pnErr->u.i, &nErr);
  sqlite3DbFree(db, aRoot);
  pnErr->u.i -= nErr;
  sqlite3VdbeMemSetNull(pIn1);
  if( nErr==0 ){
    assert( z==0 );







|







4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
  assert( (pnErr->flags & (MEM_Str|MEM_Blob))==0 );
  pIn1 = &aMem[pOp->p1];
  for(j=0; j<nRoot; j++){
    aRoot[j] = (int)sqlite3VdbeIntValue(&pIn1[j]);
  }
  aRoot[j] = 0;
  assert( pOp->p5<db->nDb );
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p5))!=0 );
  z = sqlite3BtreeIntegrityCheck(db->aDb[pOp->p5].pBt, aRoot, nRoot,
                                 (int)pnErr->u.i, &nErr);
  sqlite3DbFree(db, aRoot);
  pnErr->u.i -= nErr;
  sqlite3VdbeMemSetNull(pIn1);
  if( nErr==0 ){
    assert( z==0 );
5235
5236
5237
5238
5239
5240
5241

5242
5243
5244
5245












5246

5247
5248
5249
5250
5251
5252
5253
  if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
    assert( pOp>p->aOp );
    assert( pOp[-1].p4type==P4_COLLSEQ );
    assert( pOp[-1].opcode==OP_CollSeq );
    ctx.pColl = pOp[-1].p4.pColl;
  }
  (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */

  if( ctx.isError ){
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
    rc = ctx.isError;
  }












  sqlite3VdbeMemRelease(&ctx.s);

  break;
}

/* Opcode: AggFinal P1 P2 * P4 *
**
** Execute the finalizer function for an aggregate.  P1 is
** the memory location that is the accumulator for the aggregate.







>




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

>







5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
  if( ctx.pFunc->flags & SQLITE_FUNC_NEEDCOLL ){
    assert( pOp>p->aOp );
    assert( pOp[-1].p4type==P4_COLLSEQ );
    assert( pOp[-1].opcode==OP_CollSeq );
    ctx.pColl = pOp[-1].p4.pColl;
  }
  (ctx.pFunc->xStep)(&ctx, n, apVal); /* IMP: R-24505-23230 */
  sqlite3VdbeMutexResync(p);
  if( ctx.isError ){
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(&ctx.s));
    rc = ctx.isError;
  }

  /* The app-defined function has done something that as caused this
  ** statement to expire.  (Perhaps the function called sqlite3_exec()
  ** with a CREATE TABLE statement.)
  */
#if 0
  if( p->expired ){
    rc = SQLITE_ABORT;
    break;
  }
#endif

  sqlite3VdbeMemRelease(&ctx.s);

  break;
}

/* Opcode: AggFinal P1 P2 * P4 *
**
** Execute the finalizer function for an aggregate.  P1 is
** the memory location that is the accumulator for the aggregate.
5261
5262
5263
5264
5265
5266
5267

5268
5269


5270
5271
5272
5273
5274
5275
5276
*/
case OP_AggFinal: {
  Mem *pMem;
  assert( pOp->p1>0 && pOp->p1<=p->nMem );
  pMem = &aMem[pOp->p1];
  assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
  rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);

  if( rc ){
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));


  }
  sqlite3VdbeChangeEncoding(pMem, encoding);
  UPDATE_MAX_BLOBSIZE(pMem);
  if( sqlite3VdbeMemTooBig(pMem) ){
    goto too_big;
  }
  break;







>


>
>







5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
*/
case OP_AggFinal: {
  Mem *pMem;
  assert( pOp->p1>0 && pOp->p1<=p->nMem );
  pMem = &aMem[pOp->p1];
  assert( (pMem->flags & ~(MEM_Null|MEM_Agg))==0 );
  rc = sqlite3VdbeMemFinalize(pMem, pOp->p4.pFunc);
  sqlite3VdbeMutexResync(p);
  if( rc ){
    sqlite3SetString(&p->zErrMsg, db, "%s", sqlite3_value_text(pMem));
  }else if( p->expired ){
    rc = SQLITE_ABORT;
  }
  sqlite3VdbeChangeEncoding(pMem, encoding);
  UPDATE_MAX_BLOBSIZE(pMem);
  if( sqlite3VdbeMemTooBig(pMem) ){
    goto too_big;
  }
  break;
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
       || eNew==PAGER_JOURNALMODE_WAL
       || eNew==PAGER_JOURNALMODE_QUERY
  );
  assert( pOp->p1>=0 && pOp->p1<db->nDb );

  /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
  ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
  ** when the statment is prepared and so p->aMutex.nMutex>0.  All mutexes
  ** are already acquired.  But when used in ATTACH, sqlite3VdbeUsesBtree()
  ** is not called when the statement is prepared because it requires the
  ** iDb index of the database as a parameter, and the database has not
  ** yet been attached so that index is unavailable.  We have to wait
  ** until runtime (now) to get the mutex on the newly attached database.
  ** No other mutexes are required by the ATTACH command so this is safe
  ** to do.
  */
  assert( (p->btreeMask & (1<<pOp->p1))!=0 || p->aMutex.nMutex==0 );
  if( p->aMutex.nMutex==0 ){
    /* This occurs right after ATTACH.  Get a mutex on the newly ATTACHed
    ** database. */
    sqlite3VdbeUsesBtree(p, pOp->p1);
    sqlite3VdbeMutexArrayEnter(p);
  }

  pBt = db->aDb[pOp->p1].pBt;
  pPager = sqlite3BtreePager(pBt);
  eOld = sqlite3PagerGetJournalMode(pPager);
  if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
  if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;







|








<
|



|







5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378

5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
       || eNew==PAGER_JOURNALMODE_WAL
       || eNew==PAGER_JOURNALMODE_QUERY
  );
  assert( pOp->p1>=0 && pOp->p1<db->nDb );

  /* This opcode is used in two places: PRAGMA journal_mode and ATTACH.
  ** In PRAGMA journal_mode, the sqlite3VdbeUsesBtree() routine is called
  ** when the statement is prepared and so p->btreeMask!=0.  All mutexes
  ** are already acquired.  But when used in ATTACH, sqlite3VdbeUsesBtree()
  ** is not called when the statement is prepared because it requires the
  ** iDb index of the database as a parameter, and the database has not
  ** yet been attached so that index is unavailable.  We have to wait
  ** until runtime (now) to get the mutex on the newly attached database.
  ** No other mutexes are required by the ATTACH command so this is safe
  ** to do.
  */

  if( p->btreeMask==0 ){
    /* This occurs right after ATTACH.  Get a mutex on the newly ATTACHed
    ** database. */
    sqlite3VdbeUsesBtree(p, pOp->p1);
    sqlite3VdbeEnter(p);
  }

  pBt = db->aDb[pOp->p1].pBt;
  pPager = sqlite3BtreePager(pBt);
  eOld = sqlite3PagerGetJournalMode(pPager);
  if( eNew==PAGER_JOURNALMODE_QUERY ) eNew = eOld;
  if( !sqlite3PagerOkToChangeJournalMode(pPager) ) eNew = eOld;
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
** the P1 database. If the vacuum has finished, jump to instruction
** P2. Otherwise, fall through to the next instruction.
*/
case OP_IncrVacuum: {        /* jump */
  Btree *pBt;

  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (1<<pOp->p1))!=0 );
  pBt = db->aDb[pOp->p1].pBt;
  rc = sqlite3BtreeIncrVacuum(pBt);
  if( rc==SQLITE_DONE ){
    pc = pOp->p2 - 1;
    rc = SQLITE_OK;
  }
  break;







|







5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
** the P1 database. If the vacuum has finished, jump to instruction
** P2. Otherwise, fall through to the next instruction.
*/
case OP_IncrVacuum: {        /* jump */
  Btree *pBt;

  assert( pOp->p1>=0 && pOp->p1<db->nDb );
  assert( (p->btreeMask & (((yDbMask)1)<<pOp->p1))!=0 );
  pBt = db->aDb[pOp->p1].pBt;
  rc = sqlite3BtreeIncrVacuum(pBt);
  if( rc==SQLITE_DONE ){
    pc = pOp->p2 - 1;
    rc = SQLITE_OK;
  }
  break;
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
** used to generate an error message if the lock cannot be obtained.
*/
case OP_TableLock: {
  u8 isWriteLock = (u8)pOp->p3;
  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
    int p1 = pOp->p1; 
    assert( p1>=0 && p1<db->nDb );
    assert( (p->btreeMask & (1<<p1))!=0 );
    assert( isWriteLock==0 || isWriteLock==1 );
    rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
    if( (rc&0xFF)==SQLITE_LOCKED ){
      const char *z = pOp->p4.z;
      sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
    }
  }







|







5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
** used to generate an error message if the lock cannot be obtained.
*/
case OP_TableLock: {
  u8 isWriteLock = (u8)pOp->p3;
  if( isWriteLock || 0==(db->flags&SQLITE_ReadUncommitted) ){
    int p1 = pOp->p1; 
    assert( p1>=0 && p1<db->nDb );
    assert( (p->btreeMask & (((yDbMask)1)<<p1))!=0 );
    assert( isWriteLock==0 || isWriteLock==1 );
    rc = sqlite3BtreeLockTable(db->aDb[p1].pBt, pOp->p2, isWriteLock);
    if( (rc&0xFF)==SQLITE_LOCKED ){
      const char *z = pOp->p4.z;
      sqlite3SetString(&p->zErrMsg, db, "database table is locked: %s", z);
    }
  }
5991
5992
5993
5994
5995
5996
5997
5998



5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
  p->rc = rc;
  testcase( sqlite3GlobalConfig.xLog!=0 );
  sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
                   pc, p->zSql, p->zErrMsg);
  sqlite3VdbeHalt(p);
  if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
  rc = SQLITE_ERROR;
  if( resetSchemaOnFault ) sqlite3ResetInternalSchema(db, 0);




  /* This is the only way out of this procedure.  We have to
  ** release the mutexes on btrees that were acquired at the
  ** top. */
vdbe_return:
  sqlite3BtreeMutexArrayLeave(&p->aMutex);
  return rc;

  /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
  ** is encountered.
  */
too_big:
  sqlite3SetString(&p->zErrMsg, db, "string or blob too big");







|
>
>
>





|







6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
  p->rc = rc;
  testcase( sqlite3GlobalConfig.xLog!=0 );
  sqlite3_log(rc, "statement aborts at %d: [%s] %s", 
                   pc, p->zSql, p->zErrMsg);
  sqlite3VdbeHalt(p);
  if( rc==SQLITE_IOERR_NOMEM ) db->mallocFailed = 1;
  rc = SQLITE_ERROR;
  if( resetSchemaOnFault ){
    sqlite3ResetInternalSchema(db, 0);
    sqlite3VdbeMutexResync(p);
  }

  /* This is the only way out of this procedure.  We have to
  ** release the mutexes on btrees that were acquired at the
  ** top. */
vdbe_return:
  sqlite3VdbeLeave(p);
  return rc;

  /* Jump to here if a string or blob larger than SQLITE_MAX_LENGTH
  ** is encountered.
  */
too_big:
  sqlite3SetString(&p->zErrMsg, db, "string or blob too big");
Changes to src/vdbeInt.h.
298
299
300
301
302
303
304
305

306
307
308
309
310
311
312
313
314
315
  u8 runOnlyOnce;         /* Automatically expire on reset */
  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
  u8 inVtabMethod;        /* See comments above */
  u8 usesStmtJournal;     /* True if uses a statement journal */
  u8 readOnly;            /* True for read-only statements */
  u8 isPrepareV2;         /* True if prepared with prepare_v2() */
  int nChange;            /* Number of db changes made since last reset */
  tAttachMask btreeMask;  /* Bitmask of db->aDb[] entries referenced */

  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
  int aCounter[3];        /* Counters used by sqlite3_stmt_status() */
  BtreeMutexArray aMutex; /* An array of Btree used here and needing locks */
#ifndef SQLITE_OMIT_TRACE
  i64 startTime;          /* Time when query started - used for profiling */
#endif
  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
  char *zSql;             /* Text of the SQL statement that generated this */
  void *pFree;            /* Free this when deleting the vdbe */







|
>


<







298
299
300
301
302
303
304
305
306
307
308

309
310
311
312
313
314
315
  u8 runOnlyOnce;         /* Automatically expire on reset */
  u8 minWriteFileFormat;  /* Minimum file format for writable database files */
  u8 inVtabMethod;        /* See comments above */
  u8 usesStmtJournal;     /* True if uses a statement journal */
  u8 readOnly;            /* True for read-only statements */
  u8 isPrepareV2;         /* True if prepared with prepare_v2() */
  int nChange;            /* Number of db changes made since last reset */
  yDbMask btreeMask;      /* Bitmask of db->aDb[] entries referenced */
  u32 iMutexCounter;      /* Mutex counter upon sqlite3VdbeEnter() */
  int iStatement;         /* Statement number (or 0 if has not opened stmt) */
  int aCounter[3];        /* Counters used by sqlite3_stmt_status() */

#ifndef SQLITE_OMIT_TRACE
  i64 startTime;          /* Time when query started - used for profiling */
#endif
  i64 nFkConstraint;      /* Number of imm. FK constraints this VM */
  i64 nStmtDefCons;       /* Number of def. constraints when stmt started */
  char *zSql;             /* Text of the SQL statement that generated this */
  void *pFree;            /* Free this when deleting the vdbe */
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
int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
int sqlite3VdbeCloseStatement(Vdbe *, int);
void sqlite3VdbeFrameDelete(VdbeFrame*);
int sqlite3VdbeFrameRestore(VdbeFrame *);
void sqlite3VdbeMemStoreType(Mem *pMem);
void sqlite3VdbePreUpdateHook(
    Vdbe *, VdbeCursor *, int, const char*, Table *, i64, int);




#ifdef SQLITE_DEBUG
void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
#endif

#ifndef SQLITE_OMIT_FOREIGN_KEY
int sqlite3VdbeCheckFk(Vdbe *, int);
#else
# define sqlite3VdbeCheckFk(p,i) 0
#endif

#ifndef SQLITE_OMIT_SHARED_CACHE
void sqlite3VdbeMutexArrayEnter(Vdbe *p);
#else
# define sqlite3VdbeMutexArrayEnter(p)
#endif

int sqlite3VdbeMemTranslate(Mem*, u8);
#ifdef SQLITE_DEBUG
  void sqlite3VdbePrintSql(Vdbe*);
  void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
#endif
int sqlite3VdbeMemHandleBom(Mem *pMem);








>
>
>











<
<
<
<
<
<







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
int sqlite3VdbeMemGrow(Mem *pMem, int n, int preserve);
int sqlite3VdbeCloseStatement(Vdbe *, int);
void sqlite3VdbeFrameDelete(VdbeFrame*);
int sqlite3VdbeFrameRestore(VdbeFrame *);
void sqlite3VdbeMemStoreType(Mem *pMem);
void sqlite3VdbePreUpdateHook(
    Vdbe *, VdbeCursor *, int, const char*, Table *, i64, int);
void sqlite3VdbeEnter(Vdbe*);
void sqlite3VdbeLeave(Vdbe*);
void sqlite3VdbeMutexResync(Vdbe*);

#ifdef SQLITE_DEBUG
void sqlite3VdbeMemPrepareToChange(Vdbe*,Mem*);
#endif

#ifndef SQLITE_OMIT_FOREIGN_KEY
int sqlite3VdbeCheckFk(Vdbe *, int);
#else
# define sqlite3VdbeCheckFk(p,i) 0
#endif







int sqlite3VdbeMemTranslate(Mem*, u8);
#ifdef SQLITE_DEBUG
  void sqlite3VdbePrintSql(Vdbe*);
  void sqlite3VdbeMemPrettyPrint(Mem *pMem, char *zBuf);
#endif
int sqlite3VdbeMemHandleBom(Mem *pMem);

Changes to src/vdbeaux.c.
154
155
156
157
158
159
160





161
162
163
164
165
166
167
  pOp->p5 = 0;
  pOp->p1 = p1;
  pOp->p2 = p2;
  pOp->p3 = p3;
  pOp->p4.p = 0;
  pOp->p4type = P4_NOTUSED;
  p->expired = 0;





#ifdef SQLITE_DEBUG
  pOp->zComment = 0;
  if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
#endif
#ifdef VDBE_PROFILE
  pOp->cycles = 0;
  pOp->cnt = 0;







>
>
>
>
>







154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  pOp->p5 = 0;
  pOp->p1 = p1;
  pOp->p2 = p2;
  pOp->p3 = p3;
  pOp->p4.p = 0;
  pOp->p4type = P4_NOTUSED;
  p->expired = 0;
  if( op==OP_ParseSchema ){
    /* Any program that uses the OP_ParseSchema opcode needs to lock
    ** all btrees. */
    p->btreeMask = ~(yDbMask)0;
  }
#ifdef SQLITE_DEBUG
  pOp->zComment = 0;
  if( sqlite3VdbeAddopTrace ) sqlite3VdbePrintOp(0, i, &p->aOp[i]);
#endif
#ifdef VDBE_PROFILE
  pOp->cycles = 0;
  pOp->cnt = 0;
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
** returned program.
*/
VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
  VdbeOp *aOp = p->aOp;
  assert( aOp && !p->db->mallocFailed );

  /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
  assert( p->aMutex.nMutex==0 );

  resolveP2Values(p, pnMaxArg);
  *pnOp = p->nOp;
  p->aOp = 0;
  return aOp;
}








|







459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
** returned program.
*/
VdbeOp *sqlite3VdbeTakeOpArray(Vdbe *p, int *pnOp, int *pnMaxArg){
  VdbeOp *aOp = p->aOp;
  assert( aOp && !p->db->mallocFailed );

  /* Check that sqlite3VdbeUsesBtree() was not called on this VM */
  assert( p->btreeMask==0 );

  resolveP2Values(p, pnMaxArg);
  *pnOp = p->nOp;
  p->aOp = 0;
  return aOp;
}

942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960

961

















962




963

964


























































































965
966
967
968
969
970
971
  return zP4;
}
#endif

/*
** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
**
** The prepared statement has to know in advance which Btree objects
** will be used so that it can acquire mutexes on them all in sorted
** order (via sqlite3VdbeMutexArrayEnter().  Mutexes are acquired
** in order (and released in reverse order) to avoid deadlocks.
*/
void sqlite3VdbeUsesBtree(Vdbe *p, int i){
  tAttachMask mask;
  assert( i>=0 && i<p->db->nDb && i<sizeof(tAttachMask)*8 );
  assert( i<(int)sizeof(p->btreeMask)*8 );
  mask = ((u32)1)<<i;
  if( (p->btreeMask & mask)==0 ){
    p->btreeMask |= mask;

    sqlite3BtreeMutexArrayInsert(&p->aMutex, p->db->aDb[i].pBt);

















  }




}





























































































#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
/*
** Print a single opcode.  This routine is used for debugging only.
*/
void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
  char *zP4;







|
|
|
<


<
|

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

>
>
>
>

>

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







947
948
949
950
951
952
953
954
955
956

957
958

959
960


961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
  return zP4;
}
#endif

/*
** Declare to the Vdbe that the BTree object at db->aDb[i] is used.
**
** The prepared statements need to know in advance the complete set of
** attached databases that they will be using.  A mask of these databases
** is maintained in p->btreeMask and is used for locking and other purposes.

*/
void sqlite3VdbeUsesBtree(Vdbe *p, int i){

  assert( i>=0 && i<p->db->nDb && i<sizeof(yDbMask)*8 );
  assert( i<(int)sizeof(p->btreeMask)*8 );


  p->btreeMask |= ((yDbMask)1)<<i;
}

/*
** Compute the sum of all mutex counters for all btrees in the
** given prepared statement.
*/
#ifndef SQLITE_OMIT_SHARED_CACHE
static u32 mutexCounterSum(Vdbe *p){
  u32 cntSum = 0;
#ifdef SQLITE_DEBUG
  int i;
  yDbMask mask;
  sqlite3 *db = p->db;
  Db *aDb = db->aDb;
  int nDb = db->nDb;
  for(i=0, mask=1; i<nDb; i++, mask += mask){
    if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
      cntSum += sqlite3BtreeMutexCounter(aDb[i].pBt);
    }
  }
#else
  UNUSED_PARAMETER(p);
#endif
  return cntSum;
}
#endif

/*
** If SQLite is compiled to support shared-cache mode and to be threadsafe,
** this routine obtains the mutex associated with each BtShared structure
** that may be accessed by the VM passed as an argument. In doing so it also
** sets the BtShared.db member of each of the BtShared structures, ensuring
** that the correct busy-handler callback is invoked if required.
**
** If SQLite is not threadsafe but does support shared-cache mode, then
** sqlite3BtreeEnter() is invoked to set the BtShared.db variables
** of all of BtShared structures accessible via the database handle 
** associated with the VM.
**
** If SQLite is not threadsafe and does not support shared-cache mode, this
** function is a no-op.
**
** The p->btreeMask field is a bitmask of all btrees that the prepared 
** statement p will ever use.  Let N be the number of bits in p->btreeMask
** corresponding to btrees that use shared cache.  Then the runtime of
** this routine is N*N.  But as N is rarely more than 1, this should not
** be a problem.
*/
void sqlite3VdbeEnter(Vdbe *p){
#ifndef SQLITE_OMIT_SHARED_CACHE
  int i;
  yDbMask mask;
  sqlite3 *db = p->db;
  Db *aDb = db->aDb;
  int nDb = db->nDb;
  for(i=0, mask=1; i<nDb; i++, mask += mask){
    if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
      sqlite3BtreeEnter(aDb[i].pBt);
    }
  }
  p->iMutexCounter = mutexCounterSum(p);
#else
  UNUSED_PARAMETER(p);
#endif
}

/*
** Unlock all of the btrees previously locked by a call to sqlite3VdbeEnter().
*/
void sqlite3VdbeLeave(Vdbe *p){
#ifndef SQLITE_OMIT_SHARED_CACHE
  int i;
  yDbMask mask;
  sqlite3 *db = p->db;
  Db *aDb = db->aDb;
  int nDb = db->nDb;

  /* Assert that the all mutexes have been held continously since
  ** the most recent sqlite3VdbeEnter() or sqlite3VdbeMutexResync().
  */
  assert( mutexCounterSum(p) == p->iMutexCounter );

  for(i=0, mask=1; i<nDb; i++, mask += mask){
    if( i!=1 && (mask & p->btreeMask)!=0 && ALWAYS(aDb[i].pBt!=0) ){
      sqlite3BtreeLeave(aDb[i].pBt);
    }
  }
#else
  UNUSED_PARAMETER(p);
#endif
}

/*
** Recompute the sum of the mutex counters on all btrees used by the
** prepared statement p.
**
** Call this routine while holding a sqlite3VdbeEnter() after doing something
** that might cause one or more of the individual mutexes held by the
** prepared statement to be released.  Calling sqlite3BtreeEnter() on 
** any BtShared mutex which is not used by the prepared statement is one
** way to cause one or more of the mutexes in the prepared statement
** to be temporarily released.  The anti-deadlocking logic in
** sqlite3BtreeEnter() can cause mutexes to be released temporarily then
** reacquired.
**
** Calling this routine is an acknowledgement that some of the individual
** mutexes in the prepared statement might have been released and reacquired.
** So checks to verify that mutex-protected content did not change
** unexpectedly should accompany any call to this routine.
*/
void sqlite3VdbeMutexResync(Vdbe *p){
#if !defined(SQLITE_OMIT_SHARED_CACHE) && defined(SQLITE_DEBUG)
  p->iMutexCounter = mutexCounterSum(p);
#else
  UNUSED_PARAMETER(p);
#endif
}

#if defined(VDBE_PROFILE) || defined(SQLITE_DEBUG)
/*
** Print a single opcode.  This routine is used for debugging only.
*/
void sqlite3VdbePrintOp(FILE *pOut, int pc, Op *pOp){
  char *zP4;
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
    if( eOp==SAVEPOINT_ROLLBACK ){
      db->nDeferredCons = p->nStmtDefCons;
    }
  }
  return rc;
}

/*
** If SQLite is compiled to support shared-cache mode and to be threadsafe,
** this routine obtains the mutex associated with each BtShared structure
** that may be accessed by the VM passed as an argument. In doing so it
** sets the BtShared.db member of each of the BtShared structures, ensuring
** that the correct busy-handler callback is invoked if required.
**
** If SQLite is not threadsafe but does support shared-cache mode, then
** sqlite3BtreeEnterAll() is invoked to set the BtShared.db variables
** of all of BtShared structures accessible via the database handle 
** associated with the VM. Of course only a subset of these structures
** will be accessed by the VM, and we could use Vdbe.btreeMask to figure
** that subset out, but there is no advantage to doing so.
**
** If SQLite is not threadsafe and does not support shared-cache mode, this
** function is a no-op.
*/
#ifndef SQLITE_OMIT_SHARED_CACHE
void sqlite3VdbeMutexArrayEnter(Vdbe *p){
#if SQLITE_THREADSAFE
  sqlite3BtreeMutexArrayEnter(&p->aMutex);
#else
  sqlite3BtreeEnterAll(p->db);
#endif
}
#endif

/*
** This function is called when a transaction opened by the database 
** handle associated with the VM passed as an argument is about to be 
** committed. If there are outstanding deferred foreign key constraint
** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
**
** If there are outstanding FK violations and this function returns 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







2070
2071
2072
2073
2074
2075
2076



























2077
2078
2079
2080
2081
2082
2083
    if( eOp==SAVEPOINT_ROLLBACK ){
      db->nDeferredCons = p->nStmtDefCons;
    }
  }
  return rc;
}




























/*
** This function is called when a transaction opened by the database 
** handle associated with the VM passed as an argument is about to be 
** committed. If there are outstanding deferred foreign key constraint
** violations, return SQLITE_ERROR. Otherwise, SQLITE_OK.
**
** If there are outstanding FK violations and this function returns 
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
  /* No commit or rollback needed if the program never started */
  if( p->pc>=0 ){
    int mrc;   /* Primary error code from p->rc */
    int eStatementOp = 0;
    int isSpecialError;            /* Set to true if a 'special' error */

    /* Lock all btrees used by the statement */
    sqlite3VdbeMutexArrayEnter(p);

    /* Check for one of the special errors */
    mrc = p->rc & 0xff;
    assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
    isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
                     || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
    if( isSpecialError ){







|







2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
  /* No commit or rollback needed if the program never started */
  if( p->pc>=0 ){
    int mrc;   /* Primary error code from p->rc */
    int eStatementOp = 0;
    int isSpecialError;            /* Set to true if a 'special' error */

    /* Lock all btrees used by the statement */
    sqlite3VdbeEnter(p);

    /* Check for one of the special errors */
    mrc = p->rc & 0xff;
    assert( p->rc!=SQLITE_IOERR_BLOCKED );  /* This error no longer exists */
    isSpecialError = mrc==SQLITE_NOMEM || mrc==SQLITE_IOERR
                     || mrc==SQLITE_INTERRUPT || mrc==SQLITE_FULL;
    if( isSpecialError ){
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
     && db->autoCommit 
     && db->writeVdbeCnt==(p->readOnly==0) 
    ){
      if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
        rc = sqlite3VdbeCheckFk(p, 1);
        if( rc!=SQLITE_OK ){
          if( NEVER(p->readOnly) ){
            sqlite3BtreeMutexArrayLeave(&p->aMutex);
            return SQLITE_ERROR;
          }
          rc = SQLITE_CONSTRAINT;
        }else{ 
          /* The auto-commit flag is true, the vdbe program was successful 
          ** or hit an 'OR FAIL' constraint and there are no deferred foreign
          ** key constraints to hold up the transaction. This means a commit 
          ** is required. */
          rc = vdbeCommit(db, p);
        }
        if( rc==SQLITE_BUSY && p->readOnly ){
          sqlite3BtreeMutexArrayLeave(&p->aMutex);
          return SQLITE_BUSY;
        }else if( rc!=SQLITE_OK ){
          p->rc = rc;
          sqlite3RollbackAll(db);
        }else{
          db->nDeferredCons = 0;
          sqlite3CommitInternalChanges(db);







|











|







2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
     && db->autoCommit 
     && db->writeVdbeCnt==(p->readOnly==0) 
    ){
      if( p->rc==SQLITE_OK || (p->errorAction==OE_Fail && !isSpecialError) ){
        rc = sqlite3VdbeCheckFk(p, 1);
        if( rc!=SQLITE_OK ){
          if( NEVER(p->readOnly) ){
            sqlite3VdbeLeave(p);
            return SQLITE_ERROR;
          }
          rc = SQLITE_CONSTRAINT;
        }else{ 
          /* The auto-commit flag is true, the vdbe program was successful 
          ** or hit an 'OR FAIL' constraint and there are no deferred foreign
          ** key constraints to hold up the transaction. This means a commit 
          ** is required. */
          rc = vdbeCommit(db, p);
        }
        if( rc==SQLITE_BUSY && p->readOnly ){
          sqlite3VdbeLeave(p);
          return SQLITE_BUSY;
        }else if( rc!=SQLITE_OK ){
          p->rc = rc;
          sqlite3RollbackAll(db);
        }else{
          db->nDeferredCons = 0;
          sqlite3CommitInternalChanges(db);
2193
2194
2195
2196
2197
2198
2199

2200
2201
2202
2203
2204
2205
2206
2207
    /* Rollback or commit any schema changes that occurred. */
    if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
      sqlite3ResetInternalSchema(db, 0);
      db->flags = (db->flags | SQLITE_InternChanges);
    }

    /* Release the locks */

    sqlite3BtreeMutexArrayLeave(&p->aMutex);
  }

  /* We have successfully halted and closed the VM.  Record this fact. */
  if( p->pc>=0 ){
    db->activeVdbeCnt--;
    if( !p->readOnly ){
      db->writeVdbeCnt--;







>
|







2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
    /* Rollback or commit any schema changes that occurred. */
    if( p->rc!=SQLITE_OK && db->flags&SQLITE_InternChanges ){
      sqlite3ResetInternalSchema(db, 0);
      db->flags = (db->flags | SQLITE_InternChanges);
    }

    /* Release the locks */
    sqlite3VdbeMutexResync(p);
    sqlite3VdbeLeave(p);
  }

  /* We have successfully halted and closed the VM.  Record this fact. */
  if( p->pc>=0 ){
    db->activeVdbeCnt--;
    if( !p->readOnly ){
      db->writeVdbeCnt--;
Changes to test/analyze7.test.
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
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# There is nothing to test if ANALYZE is disable for this build.
#
ifcapable {!analyze} {
  finish_test
  return
}

# Generate some test data
#
do_test analyze7-1.0 {

  execsql {
    CREATE TABLE sequence(x INTEGER PRIMARY KEY);
    INSERT INTO sequence VALUES(1);
    INSERT INTO sequence VALUES(2);
    INSERT INTO sequence SELECT x+2 FROM sequence;
    INSERT INTO sequence SELECT x+4 FROM sequence;
    INSERT INTO sequence SELECT x+8 FROM sequence;
    INSERT INTO sequence SELECT x+16 FROM sequence;
    INSERT INTO sequence SELECT x+32 FROM sequence;
    INSERT INTO sequence SELECT x+64 FROM sequence;
    INSERT INTO sequence SELECT x+128 FROM sequence;
    INSERT INTO sequence SELECT x+256 FROM sequence;
    CREATE TABLE t1(a,b,c,d);
    CREATE INDEX t1a ON t1(a);
    CREATE INDEX t1b ON t1(b);
    CREATE INDEX t1cd ON t1(c,d);

    INSERT INTO t1 SELECT x, x, x/100, x FROM sequence;

    EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;
  }
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~10 rows)}}
do_test analyze7-1.1 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}}
do_test analyze7-1.2 {







|







>

<
<
<
<
<
<
<
<
<
<
<




>
|
>







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
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

# There is nothing to test if ANALYZE is disable for this build.
#
ifcapable {!analyze||!vtab} {
  finish_test
  return
}

# Generate some test data
#
do_test analyze7-1.0 {
  register_wholenumber_module db
  execsql {











    CREATE TABLE t1(a,b,c,d);
    CREATE INDEX t1a ON t1(a);
    CREATE INDEX t1b ON t1(b);
    CREATE INDEX t1cd ON t1(c,d);
    CREATE VIRTUAL TABLE nums USING wholenumber;
    INSERT INTO t1 SELECT value, value, value/100, value FROM nums
                    WHERE value BETWEEN 1 AND 256;
    EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;
  }
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~10 rows)}}
do_test analyze7-1.1 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}}
do_test analyze7-1.2 {
83
84
85
86
87
88
89
90






91
92







93
94
95
96
97
98
99
  execsql {ANALYZE t1cd;}
  db cache flush;
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
do_test analyze7-3.1 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}}
do_test analyze7-3.2 {






  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~102 rows)}}







do_test analyze7-3.3 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
do_test analyze7-3.4 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
do_test analyze7-3.5 {







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







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
  execsql {ANALYZE t1cd;}
  db cache flush;
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123;}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
do_test analyze7-3.1 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE b=123;}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~10 rows)}}
do_test analyze7-3.2.1 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=?;}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~86 rows)}}
ifcapable stat2 {
  # If ENABLE_STAT2 is defined, SQLite comes up with a different estimated
  # row count for (c=2) than it does for (c=?).
  do_test analyze7-3.2.2 {
    execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~102 rows)}}
} else {
  # If ENABLE_STAT2 is not defined, the expected row count for (c=2) is the
  # same as that for (c=?).
  do_test analyze7-3.2.3 {
    execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=2;}
  } {0 0 0 {SEARCH TABLE t1 USING INDEX t1cd (c=?) (~86 rows)}}
}
do_test analyze7-3.3 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE a=123 AND b=123}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1a (a=?) (~1 rows)}}
do_test analyze7-3.4 {
  execsql {EXPLAIN QUERY PLAN SELECT * FROM t1 WHERE c=123 AND b=123}
} {0 0 0 {SEARCH TABLE t1 USING INDEX t1b (b=?) (~2 rows)}}
do_test analyze7-3.5 {
Added test/fuzzer1.test.












































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
# 2011 March 25
#
# The author disclaims copyright to this source code.  In place of
# a legal notice, here is a blessing:
#
#    May you do good and not evil.
#    May you find forgiveness for yourself and forgive others.
#    May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for TCL interface to the
# SQLite library. 
#
# The focus of the tests is the word-fuzzer virtual table.
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl

ifcapable !vtab {
  finish_test
  return
}

register_fuzzer_module db
do_test fuzzer1-1.0 {
  catchsql {CREATE VIRTUAL TABLE fault1 USING fuzzer;}
} {1 {fuzzer virtual tables must be TEMP}}
do_test fuzzer1-1.1 {
  db eval {CREATE VIRTUAL TABLE temp.f1 USING fuzzer;}
} {}
do_test fuzzer1-1.2 {
  db eval {
    INSERT INTO f1(cfrom, cto, cost) VALUES('e','a',1);
    INSERT INTO f1(cfrom, cto, cost) VALUES('a','e',10);
    INSERT INTO f1(cfrom, cto, cost) VALUES('e','o',100);
  }
} {}

do_test fuzzer1-1.3 {
  db eval {
    SELECT word, distance FROM f1 WHERE word MATCH 'abcde'
  }
} {abcde 0 abcda 1 ebcde 10 ebcda 11 abcdo 100 ebcdo 110 obcde 110 obcda 111 obcdo 210}

do_test fuzzer1-2.0 {
  execsql {
    CREATE VIRTUAL TABLE temp.f2 USING fuzzer;
    -- costs based on English letter frequencies
    INSERT INTO f2(cFrom,cTo,cost) VALUES('a','e',24);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('a','o',47);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('a','u',50);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('e','a',23);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('e','i',33);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('e','o',37);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('i','e',33);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('i','y',33);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('o','a',41);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('o','e',46);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('o','u',57);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('u','o',58);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('y','i',33);

    INSERT INTO f2(cFrom,cTo,cost) VALUES('t','th',70);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('th','t',66);

 
    INSERT INTO f2(cFrom,cTo,cost) VALUES('a','',84);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','b',106);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('b','',106);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','c',94);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('c','',94);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','d',89);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('d','',89);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','e',83);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('e','',83);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','f',97);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('f','',97);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','g',99);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('g','',99);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','h',86);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('h','',86);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','i',85);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('i','',85);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','j',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('j','',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','k',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('k','',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','l',89);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('l','',89);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','m',96);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('m','',96);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','n',85);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('n','',85);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','o',85);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('o','',85);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','p',100);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('p','',100);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','q',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('q','',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','r',86);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('r','',86);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','s',86);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('s','',86);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','t',84);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('t','',84);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','u',94);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('u','',94);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','v',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('v','',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','w',96);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('w','',96);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','x',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('x','',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','y',100);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('y','',100);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('','z',120);
    INSERT INTO f2(cFrom,cTo,cost) VALUES('z','',120);

    -- Street names for the 28269 ZIPCODE.
    --
    CREATE TEMP TABLE streetname(n TEXT UNIQUE);
    INSERT INTO streetname VALUES('abbotsinch');
    INSERT INTO streetname VALUES('abbottsgate');
    INSERT INTO streetname VALUES('abbywood');
    INSERT INTO streetname VALUES('abner');
    INSERT INTO streetname VALUES('acacia ridge');
    INSERT INTO streetname VALUES('acorn creek');
    INSERT INTO streetname VALUES('acorn forest');
    INSERT INTO streetname VALUES('adel');
    INSERT INTO streetname VALUES('ainslie');
    INSERT INTO streetname VALUES('airways');
    INSERT INTO streetname VALUES('alabaster');
    INSERT INTO streetname VALUES('alba');
    INSERT INTO streetname VALUES('albertine');
    INSERT INTO streetname VALUES('alden glen');
    INSERT INTO streetname VALUES('alderson');
    INSERT INTO streetname VALUES('allen');
    INSERT INTO streetname VALUES('allen a brown');
    INSERT INTO streetname VALUES('allness glen');
    INSERT INTO streetname VALUES('aloysia');
    INSERT INTO streetname VALUES('alpine');
    INSERT INTO streetname VALUES('alwyn');
    INSERT INTO streetname VALUES('amaranthus');
    INSERT INTO streetname VALUES('amber glen');
    INSERT INTO streetname VALUES('amber leigh way');
    INSERT INTO streetname VALUES('amber meadows');
    INSERT INTO streetname VALUES('amberway');
    INSERT INTO streetname VALUES('ame');
    INSERT INTO streetname VALUES('amesbury hill');
    INSERT INTO streetname VALUES('anderson');
    INSERT INTO streetname VALUES('andrew thomas');
    INSERT INTO streetname VALUES('anduin falls');
    INSERT INTO streetname VALUES('ankeny');
    INSERT INTO streetname VALUES('annandale');
    INSERT INTO streetname VALUES('annbick');
    INSERT INTO streetname VALUES('antelope');
    INSERT INTO streetname VALUES('anzack');
    INSERT INTO streetname VALUES('apple glen');
    INSERT INTO streetname VALUES('applevalley');
    INSERT INTO streetname VALUES('appley mead');
    INSERT INTO streetname VALUES('aragorn');
    INSERT INTO streetname VALUES('arbor creek');
    INSERT INTO streetname VALUES('arbor day');
    INSERT INTO streetname VALUES('arbor meadows');
    INSERT INTO streetname VALUES('arbor spring');
    INSERT INTO streetname VALUES('arborview');
    INSERT INTO streetname VALUES('arklow');
    INSERT INTO streetname VALUES('armitage');
    INSERT INTO streetname VALUES('arvin');
    INSERT INTO streetname VALUES('ash cove');
    INSERT INTO streetname VALUES('ashford leigh');
    INSERT INTO streetname VALUES('ashmont');
    INSERT INTO streetname VALUES('atlas');
    INSERT INTO streetname VALUES('atwater');
    INSERT INTO streetname VALUES('auburn hill');
    INSERT INTO streetname VALUES('aulton link');
    INSERT INTO streetname VALUES('austin dekota');
    INSERT INTO streetname VALUES('austin knoll');
    INSERT INTO streetname VALUES('auten');
    INSERT INTO streetname VALUES('autumn harvest');
    INSERT INTO streetname VALUES('autumn oak');
    INSERT INTO streetname VALUES('autumn ridge');
    INSERT INTO streetname VALUES('avalon forest');
    INSERT INTO streetname VALUES('avalon loop');
    INSERT INTO streetname VALUES('avon farm');
    INSERT INTO streetname VALUES('avonhurst');
    INSERT INTO streetname VALUES('avonlea');
    INSERT INTO streetname VALUES('aynrand');
    INSERT INTO streetname VALUES('azure valley');
    INSERT INTO streetname VALUES('baberton');
    INSERT INTO streetname VALUES('baffin');
    INSERT INTO streetname VALUES('baggins');
    INSERT INTO streetname VALUES('balata');
    INSERT INTO streetname VALUES('ballantray');
    INSERT INTO streetname VALUES('ballston');
    INSERT INTO streetname VALUES('balsam tree');
    INSERT INTO streetname VALUES('bambi');
    INSERT INTO streetname VALUES('banwell');
    INSERT INTO streetname VALUES('barbee');
    INSERT INTO streetname VALUES('barefoot forest');
    INSERT INTO streetname VALUES('barnview');
    INSERT INTO streetname VALUES('baroda');
    INSERT INTO streetname VALUES('barson');
    INSERT INTO streetname VALUES('baskerville');
    INSERT INTO streetname VALUES('battle creek');
    INSERT INTO streetname VALUES('baucom');
    INSERT INTO streetname VALUES('bay pines');
    INSERT INTO streetname VALUES('beaker');
    INSERT INTO streetname VALUES('beard');
    INSERT INTO streetname VALUES('beardsley');
    INSERT INTO streetname VALUES('bearoak');
    INSERT INTO streetname VALUES('beauvista');
    INSERT INTO streetname VALUES('beaver creek');
    INSERT INTO streetname VALUES('beaver hollow');
    INSERT INTO streetname VALUES('bedlington');
    INSERT INTO streetname VALUES('beech cove');
    INSERT INTO streetname VALUES('beech crest');
    INSERT INTO streetname VALUES('beith');
    INSERT INTO streetname VALUES('bell glen');
    INSERT INTO streetname VALUES('bellmore');
    INSERT INTO streetname VALUES('bells mill');
    INSERT INTO streetname VALUES('bellville');
    INSERT INTO streetname VALUES('belmar place');
    INSERT INTO streetname VALUES('bembridge');
    INSERT INTO streetname VALUES('bennett neely');
    INSERT INTO streetname VALUES('bentgrass run');
    INSERT INTO streetname VALUES('benthaven');
    INSERT INTO streetname VALUES('bernardy');
    INSERT INTO streetname VALUES('bernbrook shadow');
    INSERT INTO streetname VALUES('berrybrook');
    INSERT INTO streetname VALUES('berrybush');
    INSERT INTO streetname VALUES('berwick');
    INSERT INTO streetname VALUES('betterton');
    INSERT INTO streetname VALUES('bickham');
    INSERT INTO streetname VALUES('billingham');
    INSERT INTO streetname VALUES('birchcroft');
    INSERT INTO streetname VALUES('birchstone');
    INSERT INTO streetname VALUES('birdwell');
    INSERT INTO streetname VALUES('bisaner');
    INSERT INTO streetname VALUES('bitterbush');
    INSERT INTO streetname VALUES('bitterroot');
    INSERT INTO streetname VALUES('black fox');
    INSERT INTO streetname VALUES('black maple');
    INSERT INTO streetname VALUES('black trail');
    INSERT INTO streetname VALUES('blackbird');
    INSERT INTO streetname VALUES('blake a dare');
    INSERT INTO streetname VALUES('blasdell');
    INSERT INTO streetname VALUES('blue aster');
    INSERT INTO streetname VALUES('blue finch');
    INSERT INTO streetname VALUES('blue lilac');
    INSERT INTO streetname VALUES('blue sky');
    INSERT INTO streetname VALUES('blue tick');
    INSERT INTO streetname VALUES('bob beatty');
    INSERT INTO streetname VALUES('bobcat');
    INSERT INTO streetname VALUES('bolton');
    INSERT INTO streetname VALUES('boomerang');
    INSERT INTO streetname VALUES('boulder');
    INSERT INTO streetname VALUES('boxer');
    INSERT INTO streetname VALUES('boxmeer');
    INSERT INTO streetname VALUES('brachnell view');
    INSERT INTO streetname VALUES('bradford lake');
    INSERT INTO streetname VALUES('bradwell');
    INSERT INTO streetname VALUES('brady');
    INSERT INTO streetname VALUES('braids bend');
    INSERT INTO streetname VALUES('bralers');
    INSERT INTO streetname VALUES('brandie glen');
    INSERT INTO streetname VALUES('brandy ridge');
    INSERT INTO streetname VALUES('brandybuck');
    INSERT INTO streetname VALUES('branthurst');
    INSERT INTO streetname VALUES('brassy creek');
    INSERT INTO streetname VALUES('brathay');
    INSERT INTO streetname VALUES('brawer farm');
    INSERT INTO streetname VALUES('breezy morn');
    INSERT INTO streetname VALUES('brenda');
    INSERT INTO streetname VALUES('brenly');
    INSERT INTO streetname VALUES('brenock');
    INSERT INTO streetname VALUES('brianwood');
    INSERT INTO streetname VALUES('briar rose');
    INSERT INTO streetname VALUES('briarcrest');
    INSERT INTO streetname VALUES('briarthorne');
    INSERT INTO streetname VALUES('brick dust');
    INSERT INTO streetname VALUES('bridgepath');
    INSERT INTO streetname VALUES('bridle ridge');
    INSERT INTO streetname VALUES('briggs');
    INSERT INTO streetname VALUES('brightleaf');
    INSERT INTO streetname VALUES('brigstock');
    INSERT INTO streetname VALUES('broad ridge');
    INSERT INTO streetname VALUES('brock');
    INSERT INTO streetname VALUES('brockhampton');
    INSERT INTO streetname VALUES('broken pine');
    INSERT INTO streetname VALUES('brompton');
    INSERT INTO streetname VALUES('brook falls');
    INSERT INTO streetname VALUES('brookings');
    INSERT INTO streetname VALUES('browne');
    INSERT INTO streetname VALUES('brownes creek');
    INSERT INTO streetname VALUES('brownes ferry');
    INSERT INTO streetname VALUES('brownestone view');
    INSERT INTO streetname VALUES('brumit');
    INSERT INTO streetname VALUES('bryn athyn');
    INSERT INTO streetname VALUES('buck');
    INSERT INTO streetname VALUES('bucklebury');
    INSERT INTO streetname VALUES('buckminister');
    INSERT INTO streetname VALUES('buckspring');
    INSERT INTO streetname VALUES('burch');
    INSERT INTO streetname VALUES('burch shire');
    INSERT INTO streetname VALUES('burkston');
    INSERT INTO streetname VALUES('burmith');
    INSERT INTO streetname VALUES('burnaby');
    INSERT INTO streetname VALUES('butterfly');
    INSERT INTO streetname VALUES('cabin creek');
    INSERT INTO streetname VALUES('cairns mill');
    INSERT INTO streetname VALUES('callender');
    INSERT INTO streetname VALUES('cambellton');
    INSERT INTO streetname VALUES('cambridge bay');
    INSERT INTO streetname VALUES('canary');
    INSERT INTO streetname VALUES('canbury');
    INSERT INTO streetname VALUES('candle leaf');
    INSERT INTO streetname VALUES('canipe');
    INSERT INTO streetname VALUES('canipe farm');
    INSERT INTO streetname VALUES('cannon');
    INSERT INTO streetname VALUES('canopy');
    INSERT INTO streetname VALUES('canso');
    INSERT INTO streetname VALUES('canterbrook');
    INSERT INTO streetname VALUES('cardinal glen');
    INSERT INTO streetname VALUES('cardinal point');
    INSERT INTO streetname VALUES('cardinals nest');
    INSERT INTO streetname VALUES('carlota');
    INSERT INTO streetname VALUES('carmathen');
    INSERT INTO streetname VALUES('carver');
    INSERT INTO streetname VALUES('carver pond');
    INSERT INTO streetname VALUES('casa loma');
    INSERT INTO streetname VALUES('caselton');
    INSERT INTO streetname VALUES('castello');
    INSERT INTO streetname VALUES('castle ridge');
    INSERT INTO streetname VALUES('castleglen');
    INSERT INTO streetname VALUES('castlemaine');
    INSERT INTO streetname VALUES('cavett');
    INSERT INTO streetname VALUES('caymus');
    INSERT INTO streetname VALUES('cedardale ridge');
    INSERT INTO streetname VALUES('cedarhurst');
    INSERT INTO streetname VALUES('cemkey way');
    INSERT INTO streetname VALUES('cerise');
    INSERT INTO streetname VALUES('chaceview');
    INSERT INTO streetname VALUES('chadsworth');
    INSERT INTO streetname VALUES('chadwell');
    INSERT INTO streetname VALUES('champions crest');
    INSERT INTO streetname VALUES('chandler haven');
    INSERT INTO streetname VALUES('chapel crossing');
    INSERT INTO streetname VALUES('chapel ridge');
    INSERT INTO streetname VALUES('charles crawford');
    INSERT INTO streetname VALUES('charminster');
    INSERT INTO streetname VALUES('chasewind');
    INSERT INTO streetname VALUES('chavel');
    INSERT INTO streetname VALUES('chelsea jade');
    INSERT INTO streetname VALUES('chestnut knoll');
    INSERT INTO streetname VALUES('cheviot');
    INSERT INTO streetname VALUES('chickadee');
    INSERT INTO streetname VALUES('chidley');
    INSERT INTO streetname VALUES('chimney ridge');
    INSERT INTO streetname VALUES('chimney springs');
    INSERT INTO streetname VALUES('chinaberry');
    INSERT INTO streetname VALUES('chinemist');
    INSERT INTO streetname VALUES('chinquapin');
    INSERT INTO streetname VALUES('chiswell');
    INSERT INTO streetname VALUES('christenbury');
    INSERT INTO streetname VALUES('christenbury hills');
    INSERT INTO streetname VALUES('churchill');
    INSERT INTO streetname VALUES('cindy');
    INSERT INTO streetname VALUES('cinnamon teal');
    INSERT INTO streetname VALUES('citadel');
    INSERT INTO streetname VALUES('clare olivia');
    INSERT INTO streetname VALUES('clarke creek');
    INSERT INTO streetname VALUES('clarke ridge');
    INSERT INTO streetname VALUES('clear day');
    INSERT INTO streetname VALUES('clear stream');
    INSERT INTO streetname VALUES('cleve brown');
    INSERT INTO streetname VALUES('cliff cameron');
    INSERT INTO streetname VALUES('cliffvale');
    INSERT INTO streetname VALUES('cloverside');
    INSERT INTO streetname VALUES('clymer');
    INSERT INTO streetname VALUES('coatbridge');
    INSERT INTO streetname VALUES('cobble glen');
    INSERT INTO streetname VALUES('cochran farm');
    INSERT INTO streetname VALUES('cochrane');
    INSERT INTO streetname VALUES('coleridge');
    INSERT INTO streetname VALUES('coleshire');
    INSERT INTO streetname VALUES('collins');
    INSERT INTO streetname VALUES('colvard');
    INSERT INTO streetname VALUES('colvard park');
    INSERT INTO streetname VALUES('condor');
    INSERT INTO streetname VALUES('conner ridge');
    INSERT INTO streetname VALUES('connery');
    INSERT INTO streetname VALUES('cooper run');
    INSERT INTO streetname VALUES('coopers ridge');
    INSERT INTO streetname VALUES('copper hill');
    INSERT INTO streetname VALUES('coppermine');
    INSERT INTO streetname VALUES('cornelia');
    INSERT INTO streetname VALUES('corner');
    INSERT INTO streetname VALUES('cornerstone');
    INSERT INTO streetname VALUES('cottage oaks');
    INSERT INTO streetname VALUES('cougar');
    INSERT INTO streetname VALUES('coves end');
    INSERT INTO streetname VALUES('cragland');
    INSERT INTO streetname VALUES('crail');
    INSERT INTO streetname VALUES('cranberry nook');
    INSERT INTO streetname VALUES('crawford brook');
    INSERT INTO streetname VALUES('crayton');
    INSERT INTO streetname VALUES('creek breeze');
    INSERT INTO streetname VALUES('crescent ridge');
    INSERT INTO streetname VALUES('crescent view');
    INSERT INTO streetname VALUES('cresta');
    INSERT INTO streetname VALUES('crestfield');
    INSERT INTO streetname VALUES('crestland');
    INSERT INTO streetname VALUES('crestwick');
    INSERT INTO streetname VALUES('crisfield');
    INSERT INTO streetname VALUES('crisp wood');
    INSERT INTO streetname VALUES('croft haven');
    INSERT INTO streetname VALUES('crofton springs');
    INSERT INTO streetname VALUES('cross');
    INSERT INTO streetname VALUES('crosspoint center');
    INSERT INTO streetname VALUES('crownvista');
    INSERT INTO streetname VALUES('crystal arms');
    INSERT INTO streetname VALUES('crystal crest');
    INSERT INTO streetname VALUES('crystal leaf');
    INSERT INTO streetname VALUES('cunningham park');
    INSERT INTO streetname VALUES('cypress pond');
    INSERT INTO streetname VALUES('daffodil');
    INSERT INTO streetname VALUES('daisyfield');
    INSERT INTO streetname VALUES('dalecrest');
    INSERT INTO streetname VALUES('dannelly park');
    INSERT INTO streetname VALUES('daphne');
    INSERT INTO streetname VALUES('daria');
    INSERT INTO streetname VALUES('dartmouth');
    INSERT INTO streetname VALUES('datha');
    INSERT INTO streetname VALUES('david cox');
    INSERT INTO streetname VALUES('davis');
    INSERT INTO streetname VALUES('davis crossing');
    INSERT INTO streetname VALUES('davis lake');
    INSERT INTO streetname VALUES('davis ridge');
    INSERT INTO streetname VALUES('dawnmist');
    INSERT INTO streetname VALUES('daybreak');
    INSERT INTO streetname VALUES('dearmon');
    INSERT INTO streetname VALUES('dearview');
    INSERT INTO streetname VALUES('deaton hill');
    INSERT INTO streetname VALUES('deer cross');
    INSERT INTO streetname VALUES('deerton');
    INSERT INTO streetname VALUES('degrasse');
    INSERT INTO streetname VALUES('delamere');
    INSERT INTO streetname VALUES('dellfield');
    INSERT INTO streetname VALUES('dellinger');
    INSERT INTO streetname VALUES('demington');
    INSERT INTO streetname VALUES('denmeade');
    INSERT INTO streetname VALUES('derita');
    INSERT INTO streetname VALUES('derita woods');
    INSERT INTO streetname VALUES('deruyter');
    INSERT INTO streetname VALUES('dervish');
    INSERT INTO streetname VALUES('devas');
    INSERT INTO streetname VALUES('devon croft');
    INSERT INTO streetname VALUES('devonbridge');
    INSERT INTO streetname VALUES('devongate');
    INSERT INTO streetname VALUES('devonhill');
    INSERT INTO streetname VALUES('dewmorn');
    INSERT INTO streetname VALUES('distribution center');
    INSERT INTO streetname VALUES('dominion crest');
    INSERT INTO streetname VALUES('dominion green');
    INSERT INTO streetname VALUES('dominion village');
    INSERT INTO streetname VALUES('dorshire');
    INSERT INTO streetname VALUES('double creek crossing');
    INSERT INTO streetname VALUES('dow');
    INSERT INTO streetname VALUES('downfield wood');
    INSERT INTO streetname VALUES('downing creek');
    INSERT INTO streetname VALUES('driscol');
    INSERT INTO streetname VALUES('driwood');
    INSERT INTO streetname VALUES('dry brook');
    INSERT INTO streetname VALUES('dumont');
    INSERT INTO streetname VALUES('dunblane');
    INSERT INTO streetname VALUES('dunfield');
    INSERT INTO streetname VALUES('dunoon');
    INSERT INTO streetname VALUES('dunslow');
    INSERT INTO streetname VALUES('dunstaff');
    INSERT INTO streetname VALUES('durham');
    INSERT INTO streetname VALUES('durston');
    INSERT INTO streetname VALUES('dusty cedar');
    INSERT INTO streetname VALUES('dusty trail');
    INSERT INTO streetname VALUES('dutchess');
    INSERT INTO streetname VALUES('duxford');
    INSERT INTO streetname VALUES('eagle creek');
    INSERT INTO streetname VALUES('eagles field');
    INSERT INTO streetname VALUES('eargle');
    INSERT INTO streetname VALUES('earlswood');
    INSERT INTO streetname VALUES('early mist');
    INSERT INTO streetname VALUES('earthenware');
    INSERT INTO streetname VALUES('eastfield park');
    INSERT INTO streetname VALUES('eastfield village');
    INSERT INTO streetname VALUES('easy');
    INSERT INTO streetname VALUES('eben');
    INSERT INTO streetname VALUES('edgepine');
    INSERT INTO streetname VALUES('edgewier');
    INSERT INTO streetname VALUES('edinburgh');
    INSERT INTO streetname VALUES('edinmeadow');
    INSERT INTO streetname VALUES('edmonton');
    INSERT INTO streetname VALUES('edwin jones');
    INSERT INTO streetname VALUES('elberon');
    INSERT INTO streetname VALUES('elderslie');
    INSERT INTO streetname VALUES('elementary view');
    INSERT INTO streetname VALUES('elendil');
    INSERT INTO streetname VALUES('elizabeth');
    INSERT INTO streetname VALUES('elm cove');
    INSERT INTO streetname VALUES('elrond');
    INSERT INTO streetname VALUES('elsenham');
    INSERT INTO streetname VALUES('elven');
    INSERT INTO streetname VALUES('emma lynn');
    INSERT INTO streetname VALUES('english setter');
    INSERT INTO streetname VALUES('enoch');
    INSERT INTO streetname VALUES('equipment');
    INSERT INTO streetname VALUES('ernest russell');
    INSERT INTO streetname VALUES('ernie');
    INSERT INTO streetname VALUES('esmeralda');
    INSERT INTO streetname VALUES('evergreen hollow');
    INSERT INTO streetname VALUES('eversfield');
    INSERT INTO streetname VALUES('ewen');
    INSERT INTO streetname VALUES('ewert cut');
    INSERT INTO streetname VALUES('exbury');
    INSERT INTO streetname VALUES('fair grounds park');
    INSERT INTO streetname VALUES('fairbourne');
    INSERT INTO streetname VALUES('fairchase');
    INSERT INTO streetname VALUES('faircreek');
    INSERT INTO streetname VALUES('fairglen');
    INSERT INTO streetname VALUES('fairlea');
    INSERT INTO streetname VALUES('fairmead');
    INSERT INTO streetname VALUES('fairmeadows');
    INSERT INTO streetname VALUES('fairstone');
    INSERT INTO streetname VALUES('fairvista');
    INSERT INTO streetname VALUES('fairway point');
    INSERT INTO streetname VALUES('falconcrest');
    INSERT INTO streetname VALUES('falls ridge');
    INSERT INTO streetname VALUES('falmouth');
    INSERT INTO streetname VALUES('far west');
    INSERT INTO streetname VALUES('farlow');
    INSERT INTO streetname VALUES('farris wheel');
    INSERT INTO streetname VALUES('fawndale');
    INSERT INTO streetname VALUES('feather bend');
    INSERT INTO streetname VALUES('fernledge');
    INSERT INTO streetname VALUES('fernmoss');
    INSERT INTO streetname VALUES('ferrell commons');
    INSERT INTO streetname VALUES('fieldstone');
    INSERT INTO streetname VALUES('fillian');
    INSERT INTO streetname VALUES('fincher');
    INSERT INTO streetname VALUES('foggy meadow');
    INSERT INTO streetname VALUES('fordyce');
    INSERT INTO streetname VALUES('forest grove');
    INSERT INTO streetname VALUES('forest path');
    INSERT INTO streetname VALUES('forestridge commons');
    INSERT INTO streetname VALUES('forestrock');
    INSERT INTO streetname VALUES('fortunes ridge');
    INSERT INTO streetname VALUES('founders club');
    INSERT INTO streetname VALUES('fountaingrass');
    INSERT INTO streetname VALUES('fox chase');
    INSERT INTO streetname VALUES('fox glen');
    INSERT INTO streetname VALUES('fox hill');
    INSERT INTO streetname VALUES('fox point');
    INSERT INTO streetname VALUES('fox trot');
    INSERT INTO streetname VALUES('foxbriar');
    INSERT INTO streetname VALUES('frank little');
    INSERT INTO streetname VALUES('franzia');
    INSERT INTO streetname VALUES('french woods');
    INSERT INTO streetname VALUES('frostmoor');
    INSERT INTO streetname VALUES('frye');
    INSERT INTO streetname VALUES('furlong');
    INSERT INTO streetname VALUES('galena view');
    INSERT INTO streetname VALUES('gallery pointe');
    INSERT INTO streetname VALUES('gammon');
    INSERT INTO streetname VALUES('garden grove');
    INSERT INTO streetname VALUES('gardendale');
    INSERT INTO streetname VALUES('garganey');
    INSERT INTO streetname VALUES('garnet field');
    INSERT INTO streetname VALUES('garrison');
    INSERT INTO streetname VALUES('garvin');
    INSERT INTO streetname VALUES('garvis');
    INSERT INTO streetname VALUES('gaskill');
    INSERT INTO streetname VALUES('gemstone');
    INSERT INTO streetname VALUES('gibbon');
    INSERT INTO streetname VALUES('gibbon terrace');
    INSERT INTO streetname VALUES('gibbons link');
    INSERT INTO streetname VALUES('gillman');
    INSERT INTO streetname VALUES('gladwood');
    INSERT INTO streetname VALUES('gladwyne');
    INSERT INTO streetname VALUES('glamorgan');
    INSERT INTO streetname VALUES('glaze');
    INSERT INTO streetname VALUES('glen brook');
    INSERT INTO streetname VALUES('glen cove');
    INSERT INTO streetname VALUES('glen hope');
    INSERT INTO streetname VALUES('glen manor');
    INSERT INTO streetname VALUES('glen olden');
    INSERT INTO streetname VALUES('glencairn');
    INSERT INTO streetname VALUES('glendock');
    INSERT INTO streetname VALUES('glenolden');
    INSERT INTO streetname VALUES('glenover');
    INSERT INTO streetname VALUES('glenshire');
    INSERT INTO streetname VALUES('glenstone');
    INSERT INTO streetname VALUES('gold dust');
    INSERT INTO streetname VALUES('golden pond');
    INSERT INTO streetname VALUES('goldenblush');
    INSERT INTO streetname VALUES('goldenfield');
    INSERT INTO streetname VALUES('goose landing');
    INSERT INTO streetname VALUES('gorham gate');
    INSERT INTO streetname VALUES('grabill');
    INSERT INTO streetname VALUES('graburns ford');
    INSERT INTO streetname VALUES('graham');
    INSERT INTO streetname VALUES('grahamson');
    INSERT INTO streetname VALUES('granard');
    INSERT INTO streetname VALUES('grand teton');
    INSERT INTO streetname VALUES('grande heights');
    INSERT INTO streetname VALUES('grandeur');
    INSERT INTO streetname VALUES('granite creek');
    INSERT INTO streetname VALUES('grasset');
    INSERT INTO streetname VALUES('graypark');
    INSERT INTO streetname VALUES('grays ridge');
    INSERT INTO streetname VALUES('great bear');
    INSERT INTO streetname VALUES('green clover');
    INSERT INTO streetname VALUES('green hedge');
    INSERT INTO streetname VALUES('green meadow');
    INSERT INTO streetname VALUES('green pasture');
    INSERT INTO streetname VALUES('greene');
    INSERT INTO streetname VALUES('greenloch');
    INSERT INTO streetname VALUES('greenock ridge');
    INSERT INTO streetname VALUES('greenware');
    INSERT INTO streetname VALUES('greenway village');
    INSERT INTO streetname VALUES('grenelefe village');
    INSERT INTO streetname VALUES('grey dogwood');
    INSERT INTO streetname VALUES('greyhound');
    INSERT INTO streetname VALUES('greylock ridge');
    INSERT INTO streetname VALUES('grosbeak');
    INSERT INTO streetname VALUES('grove');
    INSERT INTO streetname VALUES('groveton');
    INSERT INTO streetname VALUES('groveview');
    INSERT INTO streetname VALUES('hackberry creek');
    INSERT INTO streetname VALUES('hackberry grove');
    INSERT INTO streetname VALUES('hackett');
    INSERT INTO streetname VALUES('haddington');
    INSERT INTO streetname VALUES('hagler');
    INSERT INTO streetname VALUES('halcott');
    INSERT INTO streetname VALUES('half dome');
    INSERT INTO streetname VALUES('hallam');
    INSERT INTO streetname VALUES('hamilton russell');
    INSERT INTO streetname VALUES('hampton place');
    INSERT INTO streetname VALUES('hankins');
    INSERT INTO streetname VALUES('harburn forest');
    INSERT INTO streetname VALUES('harringham');
    INSERT INTO streetname VALUES('harrington woods');
    INSERT INTO streetname VALUES('harris corners');
    INSERT INTO streetname VALUES('harris cove');
    INSERT INTO streetname VALUES('harris glen');
    INSERT INTO streetname VALUES('harris hill');
    INSERT INTO streetname VALUES('harris oak');
    INSERT INTO streetname VALUES('harris pointe');
    INSERT INTO streetname VALUES('harris pond');
    INSERT INTO streetname VALUES('harris ridge');
    INSERT INTO streetname VALUES('harris technology');
    INSERT INTO streetname VALUES('harris woods');
    INSERT INTO streetname VALUES('hartfield downs');
    INSERT INTO streetname VALUES('hattie little');
    INSERT INTO streetname VALUES('hatwynn');
    INSERT INTO streetname VALUES('hawkins');
    INSERT INTO streetname VALUES('hawksnest');
    INSERT INTO streetname VALUES('haybridge');
    INSERT INTO streetname VALUES('hayden');
    INSERT INTO streetname VALUES('hazelcroft');
    INSERT INTO streetname VALUES('hazlitt');
    INSERT INTO streetname VALUES('hazy valley');
    INSERT INTO streetname VALUES('hearst');
    INSERT INTO streetname VALUES('heathcrest');
    INSERT INTO streetname VALUES('heathcroft');
    INSERT INTO streetname VALUES('hedge maple');
    INSERT INTO streetname VALUES('hedgecrest');
    INSERT INTO streetname VALUES('hedingham');
    INSERT INTO streetname VALUES('heman');
    INSERT INTO streetname VALUES('henderson');
    INSERT INTO streetname VALUES('henderson oaks');
    INSERT INTO streetname VALUES('henderson valley');
    INSERT INTO streetname VALUES('hendry');
    INSERT INTO streetname VALUES('heritage hills');
    INSERT INTO streetname VALUES('heritage woods');
    INSERT INTO streetname VALUES('heron cove');
    INSERT INTO streetname VALUES('heron glen');
    INSERT INTO streetname VALUES('hewitt');
    INSERT INTO streetname VALUES('hey rock');
    INSERT INTO streetname VALUES('heysham');
    INSERT INTO streetname VALUES('hickory cove');
    INSERT INTO streetname VALUES('hidden meadow');
    INSERT INTO streetname VALUES('high glen');
    INSERT INTO streetname VALUES('high laurel');
    INSERT INTO streetname VALUES('high valley');
    INSERT INTO streetname VALUES('highcroft');
    INSERT INTO streetname VALUES('highland');
    INSERT INTO streetname VALUES('highland commons');
    INSERT INTO streetname VALUES('highland creek');
    INSERT INTO streetname VALUES('highland glen');
    INSERT INTO streetname VALUES('highland park');
    INSERT INTO streetname VALUES('highlander');
    INSERT INTO streetname VALUES('highstream');
    INSERT INTO streetname VALUES('hilltop');
    INSERT INTO streetname VALUES('hobbitshire');
    INSERT INTO streetname VALUES('hoffman');
    INSERT INTO streetname VALUES('hogans way');
    INSERT INTO streetname VALUES('holbert');
    INSERT INTO streetname VALUES('hollow ridge');
    INSERT INTO streetname VALUES('holly vista');
    INSERT INTO streetname VALUES('hollywood');
    INSERT INTO streetname VALUES('hoover');
    INSERT INTO streetname VALUES('hopkins');
    INSERT INTO streetname VALUES('horace mann');
    INSERT INTO streetname VALUES('hornbeam');
    INSERT INTO streetname VALUES('horse pasture');
    INSERT INTO streetname VALUES('hosta');
    INSERT INTO streetname VALUES('howard');
    INSERT INTO streetname VALUES('hubbard');
    INSERT INTO streetname VALUES('hubbard falls');
    INSERT INTO streetname VALUES('hubbard woods');
    INSERT INTO streetname VALUES('hucks');
    INSERT INTO streetname VALUES('hunters creek');
    INSERT INTO streetname VALUES('hunters pointe');
    INSERT INTO streetname VALUES('hunters spring');
    INSERT INTO streetname VALUES('hunters whip');
    INSERT INTO streetname VALUES('huntmeadow');
    INSERT INTO streetname VALUES('hutchison mcdonald');
    INSERT INTO streetname VALUES('ingleton');
    INSERT INTO streetname VALUES('insdale');
    INSERT INTO streetname VALUES('interstate 85 service');
    INSERT INTO streetname VALUES('iola');
    INSERT INTO streetname VALUES('iredell');
    INSERT INTO streetname VALUES('iron brigade');
    INSERT INTO streetname VALUES('irwin valley');
    INSERT INTO streetname VALUES('irwin wood');
    INSERT INTO streetname VALUES('ivy brook');
    INSERT INTO streetname VALUES('ivy ridge');
    INSERT INTO streetname VALUES('jack russell');
    INSERT INTO streetname VALUES('jackson');
    INSERT INTO streetname VALUES('jacob martin');
    INSERT INTO streetname VALUES('jamison');
    INSERT INTO streetname VALUES('jane');
    INSERT INTO streetname VALUES('jaspar crest');
    INSERT INTO streetname VALUES('jessica');
    INSERT INTO streetname VALUES('jimmy oehler');
    INSERT INTO streetname VALUES('jocelyn');
    INSERT INTO streetname VALUES('johnston mill');
    INSERT INTO streetname VALUES('johnston oehler');
    INSERT INTO streetname VALUES('judal');
    INSERT INTO streetname VALUES('junipeous');
    INSERT INTO streetname VALUES('juniper');
    INSERT INTO streetname VALUES('juniperus');
    INSERT INTO streetname VALUES('kalispell');
    INSERT INTO streetname VALUES('karylsturn');
    INSERT INTO streetname VALUES('katelyn');
    INSERT INTO streetname VALUES('kayron');
    INSERT INTO streetname VALUES('keaton');
    INSERT INTO streetname VALUES('keble');
    INSERT INTO streetname VALUES('keels');
    INSERT INTO streetname VALUES('keith');
    INSERT INTO streetname VALUES('keithwood');
    INSERT INTO streetname VALUES('kelden walker');
    INSERT INTO streetname VALUES('kelsey emma');
    INSERT INTO streetname VALUES('kendrick');
    INSERT INTO streetname VALUES('kenmont');
    INSERT INTO streetname VALUES('kennerly cove');
    INSERT INTO streetname VALUES('kenninghall');
    INSERT INTO streetname VALUES('kent village');
    INSERT INTO streetname VALUES('kestral ridge');
    INSERT INTO streetname VALUES('kestrel');
    INSERT INTO streetname VALUES('kilmartin');
    INSERT INTO streetname VALUES('kilty');
    INSERT INTO streetname VALUES('kinglet');
    INSERT INTO streetname VALUES('kingsland');
    INSERT INTO streetname VALUES('kingsnorth');
    INSERT INTO streetname VALUES('kinsmore');
    INSERT INTO streetname VALUES('kirkgard');
    INSERT INTO streetname VALUES('kirkmont');
    INSERT INTO streetname VALUES('knightsgate');
    INSERT INTO streetname VALUES('kobuk');
    INSERT INTO streetname VALUES('kotlik');
    INSERT INTO streetname VALUES('kotz');
    INSERT INTO streetname VALUES('kyndall walk');
    INSERT INTO streetname VALUES('laborde');
    INSERT INTO streetname VALUES('lady bank');
    INSERT INTO streetname VALUES('lagrande');
    INSERT INTO streetname VALUES('lake');
    INSERT INTO streetname VALUES('lakeridge commons');
    INSERT INTO streetname VALUES('lakeview');
    INSERT INTO streetname VALUES('lakewood edge');
    INSERT INTO streetname VALUES('lakota');
    INSERT INTO streetname VALUES('lambrook');
    INSERT INTO streetname VALUES('lampkin');
    INSERT INTO streetname VALUES('lampkin park');
    INSERT INTO streetname VALUES('langham');
    INSERT INTO streetname VALUES('lanzerac manor');
    INSERT INTO streetname VALUES('larkmead forest');
    INSERT INTO streetname VALUES('lattice');
    INSERT INTO streetname VALUES('laurel crest');
    INSERT INTO streetname VALUES('laurel ridge');
    INSERT INTO streetname VALUES('laurel run');
    INSERT INTO streetname VALUES('laurenfield');
    INSERT INTO streetname VALUES('laveta');
    INSERT INTO streetname VALUES('lazy day');
    INSERT INTO streetname VALUES('leawood run');
    INSERT INTO streetname VALUES('lee marie');
    INSERT INTO streetname VALUES('legacy lake');
    INSERT INTO streetname VALUES('legacy park');
    INSERT INTO streetname VALUES('legato');
    INSERT INTO streetname VALUES('legolas');
    INSERT INTO streetname VALUES('leigh glen');
    INSERT INTO streetname VALUES('lence');
    INSERT INTO streetname VALUES('lenox hill');
    INSERT INTO streetname VALUES('leonine');
    INSERT INTO streetname VALUES('leslie');
    INSERT INTO streetname VALUES('lester hill');
    INSERT INTO streetname VALUES('levisey');
    INSERT INTO streetname VALUES('liberty bell');
    INSERT INTO streetname VALUES('linden berry');
    INSERT INTO streetname VALUES('lisbon');
    INSERT INTO streetname VALUES('little stoney');
    INSERT INTO streetname VALUES('livengood');
    INSERT INTO streetname VALUES('lochway');
    INSERT INTO streetname VALUES('lockman');
    INSERT INTO streetname VALUES('loganville');
    INSERT INTO streetname VALUES('lone tree');
    INSERT INTO streetname VALUES('long creek park');
    INSERT INTO streetname VALUES('long forest');
    INSERT INTO streetname VALUES('looking glass');
    INSERT INTO streetname VALUES('lookout point');
    INSERT INTO streetname VALUES('lowen');
    INSERT INTO streetname VALUES('lusby');
    INSERT INTO streetname VALUES('lyleton');
    INSERT INTO streetname VALUES('lynn lee');
    INSERT INTO streetname VALUES('lynnewood glen');
    INSERT INTO streetname VALUES('machrie');
    INSERT INTO streetname VALUES('mackinac');
    INSERT INTO streetname VALUES('maddox');
    INSERT INTO streetname VALUES('madison park');
    INSERT INTO streetname VALUES('mallard');
    INSERT INTO streetname VALUES('mallard cove');
    INSERT INTO streetname VALUES('mallard forest');
    INSERT INTO streetname VALUES('mallard grove');
    INSERT INTO streetname VALUES('mallard hill');
    INSERT INTO streetname VALUES('mallard park');
    INSERT INTO streetname VALUES('mallard ridge');
    INSERT INTO streetname VALUES('mallard view');
    INSERT INTO streetname VALUES('manbey');
    INSERT INTO streetname VALUES('manning');
    INSERT INTO streetname VALUES('mantario');
    INSERT INTO streetname VALUES('maple');
    INSERT INTO streetname VALUES('maple cove');
    INSERT INTO streetname VALUES('maple park');
    INSERT INTO streetname VALUES('marathon hill');
    INSERT INTO streetname VALUES('marbury');
    INSERT INTO streetname VALUES('marett');
    INSERT INTO streetname VALUES('marigold');
    INSERT INTO streetname VALUES('marionwood');
    INSERT INTO streetname VALUES('marshbank');
    INSERT INTO streetname VALUES('mason');
    INSERT INTO streetname VALUES('mayapple');
    INSERT INTO streetname VALUES('maylandia');
    INSERT INTO streetname VALUES('mayspring');
    INSERT INTO streetname VALUES('mcadam');
    INSERT INTO streetname VALUES('mcchesney');
    INSERT INTO streetname VALUES('mccurdy');
    INSERT INTO streetname VALUES('mcgrath');
    INSERT INTO streetname VALUES('mckendree');
    INSERT INTO streetname VALUES('mclaughlin');
    INSERT INTO streetname VALUES('mctaggart');
    INSERT INTO streetname VALUES('meadow green');
    INSERT INTO streetname VALUES('meadow knoll');
    INSERT INTO streetname VALUES('meadow post');
    INSERT INTO streetname VALUES('meadowmont');
    INSERT INTO streetname VALUES('meadowmont view');
    INSERT INTO streetname VALUES('meadowview hills');
    INSERT INTO streetname VALUES('melshire');
    INSERT INTO streetname VALUES('melstrand');
    INSERT INTO streetname VALUES('mentone');
    INSERT INTO streetname VALUES('meridale crossing');
    INSERT INTO streetname VALUES('merion hills');
    INSERT INTO streetname VALUES('merlot');
    INSERT INTO streetname VALUES('mersham');
    INSERT INTO streetname VALUES('metromont');
    INSERT INTO streetname VALUES('metromont industrial');
    INSERT INTO streetname VALUES('michaw');
    INSERT INTO streetname VALUES('milhaven');
    INSERT INTO streetname VALUES('milhof');
    INSERT INTO streetname VALUES('millstream ridge');
    INSERT INTO streetname VALUES('mineral ridge');
    INSERT INTO streetname VALUES('mint thistle');
    INSERT INTO streetname VALUES('mintleaf');
    INSERT INTO streetname VALUES('mintvale');
    INSERT INTO streetname VALUES('misty');
    INSERT INTO streetname VALUES('misty arbor');
    INSERT INTO streetname VALUES('misty creek');
    INSERT INTO streetname VALUES('misty oaks');
    INSERT INTO streetname VALUES('misty wood');
    INSERT INTO streetname VALUES('mitzi deborah');
    INSERT INTO streetname VALUES('mobile');
    INSERT INTO streetname VALUES('molly elizabeth');
    INSERT INTO streetname VALUES('monmouth');
    INSERT INTO streetname VALUES('montrose');
    INSERT INTO streetname VALUES('moonlight');
    INSERT INTO streetname VALUES('moose');
    INSERT INTO streetname VALUES('morning dew');
    INSERT INTO streetname VALUES('morningsong');
    INSERT INTO streetname VALUES('morningview');
    INSERT INTO streetname VALUES('morsey');
    INSERT INTO streetname VALUES('moss glen');
    INSERT INTO streetname VALUES('mossy bank');
    INSERT INTO streetname VALUES('motor sport');
    INSERT INTO streetname VALUES('mountain laurel');
    INSERT INTO streetname VALUES('mourning dove');
    INSERT INTO streetname VALUES('mozart');
    INSERT INTO streetname VALUES('munsing');
    INSERT INTO streetname VALUES('murray');
    INSERT INTO streetname VALUES('nathan');
    INSERT INTO streetname VALUES('netherhall');
    INSERT INTO streetname VALUES('netherton');
    INSERT INTO streetname VALUES('neuhoff');
    INSERT INTO streetname VALUES('nevin');
    INSERT INTO streetname VALUES('nevin brook');
    INSERT INTO streetname VALUES('nevin glen');
    INSERT INTO streetname VALUES('nevin place');
    INSERT INTO streetname VALUES('new england');
    INSERT INTO streetname VALUES('new house');
    INSERT INTO streetname VALUES('newbary');
    INSERT INTO streetname VALUES('newchurch');
    INSERT INTO streetname VALUES('newfane');
    INSERT INTO streetname VALUES('newgard');
    INSERT INTO streetname VALUES('nicholas');
    INSERT INTO streetname VALUES('nicole');
    INSERT INTO streetname VALUES('nobility');
    INSERT INTO streetname VALUES('norcroft');
    INSERT INTO streetname VALUES('northridge');
    INSERT INTO streetname VALUES('northside');
    INSERT INTO streetname VALUES('northwoods business');
    INSERT INTO streetname VALUES('norway');
    INSERT INTO streetname VALUES('nottinghill');
    INSERT INTO streetname VALUES('numenore');
    INSERT INTO streetname VALUES('nyewood');
    INSERT INTO streetname VALUES('oak');
    INSERT INTO streetname VALUES('oak cove');
    INSERT INTO streetname VALUES('oak pasture');
    INSERT INTO streetname VALUES('oakburn');
    INSERT INTO streetname VALUES('oakwinds');
    INSERT INTO streetname VALUES('oakwood');
    INSERT INTO streetname VALUES('obrien');
    INSERT INTO streetname VALUES('ocala');
    INSERT INTO streetname VALUES('old bridge');
    INSERT INTO streetname VALUES('old fox');
    INSERT INTO streetname VALUES('old potters');
    INSERT INTO streetname VALUES('old statesville');
    INSERT INTO streetname VALUES('old steine');
    INSERT INTO streetname VALUES('old stoney creek');
    INSERT INTO streetname VALUES('old sugar creek');
    INSERT INTO streetname VALUES('old timber');
    INSERT INTO streetname VALUES('old wagon');
    INSERT INTO streetname VALUES('old willow');
    INSERT INTO streetname VALUES('oldenway');
    INSERT INTO streetname VALUES('oneida');
    INSERT INTO streetname VALUES('ontario');
    INSERT INTO streetname VALUES('oriole');
    INSERT INTO streetname VALUES('orofino');
    INSERT INTO streetname VALUES('orr');
    INSERT INTO streetname VALUES('osage');
    INSERT INTO streetname VALUES('osceola');
    INSERT INTO streetname VALUES('osprey knoll');
    INSERT INTO streetname VALUES('oxford hill');
    INSERT INTO streetname VALUES('painted fern');
    INSERT INTO streetname VALUES('painted pony');
    INSERT INTO streetname VALUES('paisley');
    INSERT INTO streetname VALUES('pale moss');
    INSERT INTO streetname VALUES('palladium');
    INSERT INTO streetname VALUES('palmutum');
    INSERT INTO streetname VALUES('palustris');
    INSERT INTO streetname VALUES('panglemont');
    INSERT INTO streetname VALUES('panther');
    INSERT INTO streetname VALUES('panthersville');
    INSERT INTO streetname VALUES('paper whites');
    INSERT INTO streetname VALUES('park');
    INSERT INTO streetname VALUES('parker green');
    INSERT INTO streetname VALUES('parkhouse');
    INSERT INTO streetname VALUES('passour ridge');
    INSERT INTO streetname VALUES('pasture view');
    INSERT INTO streetname VALUES('patricia ann');
    INSERT INTO streetname VALUES('patton');
    INSERT INTO streetname VALUES('patton ridge');
    INSERT INTO streetname VALUES('pawpaw');
    INSERT INTO streetname VALUES('peach');
    INSERT INTO streetname VALUES('peakwood');
    INSERT INTO streetname VALUES('pebble creek');
    INSERT INTO streetname VALUES('pecan cove');
    INSERT INTO streetname VALUES('pedigree');
    INSERT INTO streetname VALUES('pelorus');
    INSERT INTO streetname VALUES('penmore');
    INSERT INTO streetname VALUES('pensfold');
    INSERT INTO streetname VALUES('pepperstone');
    INSERT INTO streetname VALUES('peregrine');
    INSERT INTO streetname VALUES('periwinkle');
    INSERT INTO streetname VALUES('perkins');
    INSERT INTO streetname VALUES('pete brown');
    INSERT INTO streetname VALUES('phillips');
    INSERT INTO streetname VALUES('pickway');
    INSERT INTO streetname VALUES('piercy woods');
    INSERT INTO streetname VALUES('pierpoint');
    INSERT INTO streetname VALUES('pine');
    INSERT INTO streetname VALUES('pine branch');
    INSERT INTO streetname VALUES('pine meadow');
    INSERT INTO streetname VALUES('pineleaf');
    INSERT INTO streetname VALUES('pinewood');
    INSERT INTO streetname VALUES('pintail');
    INSERT INTO streetname VALUES('pipestone');
    INSERT INTO streetname VALUES('placer maple');
    INSERT INTO streetname VALUES('plover');
    INSERT INTO streetname VALUES('plum');
    INSERT INTO streetname VALUES('po box');
    INSERT INTO streetname VALUES('pochard');
    INSERT INTO streetname VALUES('pointview');
    INSERT INTO streetname VALUES('polk and white');
    INSERT INTO streetname VALUES('pond valley');
    INSERT INTO streetname VALUES('pondridge');
    INSERT INTO streetname VALUES('pope farm');
    INSERT INTO streetname VALUES('poplar grove');
    INSERT INTO streetname VALUES('poplar springs');
    INSERT INTO streetname VALUES('portola');
    INSERT INTO streetname VALUES('potters glen');
    INSERT INTO streetname VALUES('powatan');
    INSERT INTO streetname VALUES('prairie valley');
    INSERT INTO streetname VALUES('prescott');
    INSERT INTO streetname VALUES('presmann');
    INSERT INTO streetname VALUES('prestigious');
    INSERT INTO streetname VALUES('princess');
    INSERT INTO streetname VALUES('prosperity');
    INSERT INTO streetname VALUES('prosperity church');
    INSERT INTO streetname VALUES('prosperity commons');
    INSERT INTO streetname VALUES('prosperity park');
    INSERT INTO streetname VALUES('prosperity point');
    INSERT INTO streetname VALUES('prosperity ridge');
    INSERT INTO streetname VALUES('prosperity view');
    INSERT INTO streetname VALUES('purple finch');
    INSERT INTO streetname VALUES('quail');
    INSERT INTO streetname VALUES('queensbury');
    INSERT INTO streetname VALUES('quinn');
    INSERT INTO streetname VALUES('racine');
    INSERT INTO streetname VALUES('radbourne');
    INSERT INTO streetname VALUES('raddington');
    INSERT INTO streetname VALUES('raku');
    INSERT INTO streetname VALUES('rancliffe');
    INSERT INTO streetname VALUES('ravencrest');
    INSERT INTO streetname VALUES('reames');
    INSERT INTO streetname VALUES('rebecca run');
    INSERT INTO streetname VALUES('red bluff');
    INSERT INTO streetname VALUES('red clay');
    INSERT INTO streetname VALUES('red clover');
    INSERT INTO streetname VALUES('red rose');
    INSERT INTO streetname VALUES('red shed');
    INSERT INTO streetname VALUES('red tail');
    INSERT INTO streetname VALUES('redbridge');
    INSERT INTO streetname VALUES('redstart');
    INSERT INTO streetname VALUES('redstone view');
    INSERT INTO streetname VALUES('reedmont');
    INSERT INTO streetname VALUES('reeves');
    INSERT INTO streetname VALUES('regal');
    INSERT INTO streetname VALUES('reinbeck');
    INSERT INTO streetname VALUES('retriever');
    INSERT INTO streetname VALUES('ribbonwalk');
    INSERT INTO streetname VALUES('richardson park');
    INSERT INTO streetname VALUES('richfield');
    INSERT INTO streetname VALUES('riddings');
    INSERT INTO streetname VALUES('ridge');
    INSERT INTO streetname VALUES('ridge cliff');
    INSERT INTO streetname VALUES('ridge path');
    INSERT INTO streetname VALUES('ridge peak');
    INSERT INTO streetname VALUES('ridgefield');
    INSERT INTO streetname VALUES('ridgeline');
    INSERT INTO streetname VALUES('ridgeview commons');
    INSERT INTO streetname VALUES('riley');
    INSERT INTO streetname VALUES('riley woods');
    INSERT INTO streetname VALUES('rillet');
    INSERT INTO streetname VALUES('rindle');
    INSERT INTO streetname VALUES('rivendell');
    INSERT INTO streetname VALUES('robin');
    INSERT INTO streetname VALUES('robins nest');
    INSERT INTO streetname VALUES('robur');
    INSERT INTO streetname VALUES('robyns glen');
    INSERT INTO streetname VALUES('rock stream');
    INSERT INTO streetname VALUES('rockwell');
    INSERT INTO streetname VALUES('rockwell church');
    INSERT INTO streetname VALUES('rocky brook');
    INSERT INTO streetname VALUES('rocky ford club');
    INSERT INTO streetname VALUES('rotary');
    INSERT INTO streetname VALUES('rouda');
    INSERT INTO streetname VALUES('royal bluff');
    INSERT INTO streetname VALUES('royal celadon');
    INSERT INTO streetname VALUES('rubin lura');
    INSERT INTO streetname VALUES('runswyck');
    INSERT INTO streetname VALUES('ruth ferrell');
    INSERT INTO streetname VALUES('ruth polk');
    INSERT INTO streetname VALUES('ryan jay');
    INSERT INTO streetname VALUES('sackett');
    INSERT INTO streetname VALUES('saddle pace');
    INSERT INTO streetname VALUES('saddle run');
    INSERT INTO streetname VALUES('saddle trail');
    INSERT INTO streetname VALUES('saguaro');
    INSERT INTO streetname VALUES('saint audrey');
    INSERT INTO streetname VALUES('saint bernard');
    INSERT INTO streetname VALUES('saint frances');
    INSERT INTO streetname VALUES('sam roper');
    INSERT INTO streetname VALUES('samara');
    INSERT INTO streetname VALUES('sanders creek');
    INSERT INTO streetname VALUES('saquache');
    INSERT INTO streetname VALUES('sarnia');
    INSERT INTO streetname VALUES('savannah springs');
    INSERT INTO streetname VALUES('sawgrass ridge');
    INSERT INTO streetname VALUES('saxonbury');
    INSERT INTO streetname VALUES('scotch moss');
    INSERT INTO streetname VALUES('seasons');
    INSERT INTO streetname VALUES('serenity');
    INSERT INTO streetname VALUES('seths');
    INSERT INTO streetname VALUES('shadow lawn');
    INSERT INTO streetname VALUES('shadow oaks');
    INSERT INTO streetname VALUES('shadow pine');
    INSERT INTO streetname VALUES('shadyside');
    INSERT INTO streetname VALUES('shallow oak');
    INSERT INTO streetname VALUES('shelley');
    INSERT INTO streetname VALUES('shining oak');
    INSERT INTO streetname VALUES('ship');
    INSERT INTO streetname VALUES('shore haven');
    INSERT INTO streetname VALUES('shuman');
    INSERT INTO streetname VALUES('sidney');
    INSERT INTO streetname VALUES('silver birch');
    INSERT INTO streetname VALUES('silvermere');
    INSERT INTO streetname VALUES('simonton');
    INSERT INTO streetname VALUES('singing hills');
    INSERT INTO streetname VALUES('singing oak');
    INSERT INTO streetname VALUES('sipes');
    INSERT INTO streetname VALUES('six point');
    INSERT INTO streetname VALUES('skycrest');
    INSERT INTO streetname VALUES('skyline');
    INSERT INTO streetname VALUES('small');
    INSERT INTO streetname VALUES('smith corners');
    INSERT INTO streetname VALUES('smithwood');
    INSERT INTO streetname VALUES('snow hill');
    INSERT INTO streetname VALUES('soapstone');
    INSERT INTO streetname VALUES('sobeck');
    INSERT INTO streetname VALUES('socata');
    INSERT INTO streetname VALUES('solace');
    INSERT INTO streetname VALUES('solway');
    INSERT INTO streetname VALUES('song sparrow');
    INSERT INTO streetname VALUES('sorrento');
    INSERT INTO streetname VALUES('spector');
    INSERT INTO streetname VALUES('spin drift');
    INSERT INTO streetname VALUES('spring crest');
    INSERT INTO streetname VALUES('spring lee');
    INSERT INTO streetname VALUES('spring park');
    INSERT INTO streetname VALUES('spring terrace');
    INSERT INTO streetname VALUES('spring trace');
    INSERT INTO streetname VALUES('springhaven');
    INSERT INTO streetname VALUES('squirrel trail');
    INSERT INTO streetname VALUES('stardust');
    INSERT INTO streetname VALUES('stargaze');
    INSERT INTO streetname VALUES('starita');
    INSERT INTO streetname VALUES('starmount');
    INSERT INTO streetname VALUES('statesville');
    INSERT INTO streetname VALUES('steed');
    INSERT INTO streetname VALUES('steelewood');
    INSERT INTO streetname VALUES('steepleglen');
    INSERT INTO streetname VALUES('stephens farm');
    INSERT INTO streetname VALUES('stewarton');
    INSERT INTO streetname VALUES('stone park');
    INSERT INTO streetname VALUES('stonebrook');
    INSERT INTO streetname VALUES('stonefield');
    INSERT INTO streetname VALUES('stoneglen');
    INSERT INTO streetname VALUES('stonemarsh');
    INSERT INTO streetname VALUES('stoney garden');
    INSERT INTO streetname VALUES('stoney run');
    INSERT INTO streetname VALUES('stoney valley');
    INSERT INTO streetname VALUES('stoneykirk');
    INSERT INTO streetname VALUES('stream bank');
    INSERT INTO streetname VALUES('stream ridge');
    INSERT INTO streetname VALUES('suburban');
    INSERT INTO streetname VALUES('suffield');
    INSERT INTO streetname VALUES('sugar creek');
    INSERT INTO streetname VALUES('sugarberry');
    INSERT INTO streetname VALUES('sugarstone');
    INSERT INTO streetname VALUES('summer creek');
    INSERT INTO streetname VALUES('summer valley');
    INSERT INTO streetname VALUES('summercrest');
    INSERT INTO streetname VALUES('summercroft');
    INSERT INTO streetname VALUES('summerford');
    INSERT INTO streetname VALUES('summergold');
    INSERT INTO streetname VALUES('sunbeam');
    INSERT INTO streetname VALUES('sunbridge');
    INSERT INTO streetname VALUES('sunpath');
    INSERT INTO streetname VALUES('sunset');
    INSERT INTO streetname VALUES('sunset ridge');
    INSERT INTO streetname VALUES('sunstone');
    INSERT INTO streetname VALUES('suntrace');
    INSERT INTO streetname VALUES('sunwalk');
    INSERT INTO streetname VALUES('sutters hill');
    INSERT INTO streetname VALUES('suttonview');
    INSERT INTO streetname VALUES('swallow tail');
    INSERT INTO streetname VALUES('swanston');
    INSERT INTO streetname VALUES('sweet grove');
    INSERT INTO streetname VALUES('sweet rose');
    INSERT INTO streetname VALUES('sweetbriar ridge');
    INSERT INTO streetname VALUES('sweetfield');
    INSERT INTO streetname VALUES('sydney overlook');
    INSERT INTO streetname VALUES('sylvan');
    INSERT INTO streetname VALUES('symphony woods');
    INSERT INTO streetname VALUES('tallia');
    INSERT INTO streetname VALUES('tallu');
    INSERT INTO streetname VALUES('talwyn');
    INSERT INTO streetname VALUES('tanager');
    INSERT INTO streetname VALUES('tanager park');
    INSERT INTO streetname VALUES('tangley');
    INSERT INTO streetname VALUES('taranasay');
    INSERT INTO streetname VALUES('tarby');
    INSERT INTO streetname VALUES('tarland');
    INSERT INTO streetname VALUES('tarpway');
    INSERT INTO streetname VALUES('tauten');
    INSERT INTO streetname VALUES('taymouth');
    INSERT INTO streetname VALUES('ten trees');
    INSERT INTO streetname VALUES('terrace view');
    INSERT INTO streetname VALUES('terrier');
    INSERT INTO streetname VALUES('tesh');
    INSERT INTO streetname VALUES('teton');
    INSERT INTO streetname VALUES('tewkesbury');
    INSERT INTO streetname VALUES('thelema');
    INSERT INTO streetname VALUES('thistle bloom');
    INSERT INTO streetname VALUES('thistledown');
    INSERT INTO streetname VALUES('thomas ridge');
    INSERT INTO streetname VALUES('thornbrook');
    INSERT INTO streetname VALUES('tifton grass');
    INSERT INTO streetname VALUES('tigerton');
    INSERT INTO streetname VALUES('tomsie efird');
    INSERT INTO streetname VALUES('tor');
    INSERT INTO streetname VALUES('torphin');
    INSERT INTO streetname VALUES('torrence');
    INSERT INTO streetname VALUES('towering pine');
    INSERT INTO streetname VALUES('towhee');
    INSERT INTO streetname VALUES('toxaway');
    INSERT INTO streetname VALUES('tracy glenn');
    INSERT INTO streetname VALUES('tradition view');
    INSERT INTO streetname VALUES('trailer');
    INSERT INTO streetname VALUES('transport');
    INSERT INTO streetname VALUES('trehurst');
    INSERT INTO streetname VALUES('trexler');
    INSERT INTO streetname VALUES('trillium fields');
    INSERT INTO streetname VALUES('trimbach');
    INSERT INTO streetname VALUES('tucker');
    INSERT INTO streetname VALUES('tullamore');
    INSERT INTO streetname VALUES('tullock creek');
    INSERT INTO streetname VALUES('tunston');
    INSERT INTO streetname VALUES('tupelo');
    INSERT INTO streetname VALUES('turnabout');
    INSERT INTO streetname VALUES('turney');
    INSERT INTO streetname VALUES('turtle cross');
    INSERT INTO streetname VALUES('turtleback');
    INSERT INTO streetname VALUES('twelvestone');
    INSERT INTO streetname VALUES('twin');
    INSERT INTO streetname VALUES('twin brook');
    INSERT INTO streetname VALUES('twin lakes');
    INSERT INTO streetname VALUES('twisted pine');
    INSERT INTO streetname VALUES('tyler finley');
    INSERT INTO streetname VALUES('university station');
    INSERT INTO streetname VALUES('uphill');
    INSERT INTO streetname VALUES('valeview');
    INSERT INTO streetname VALUES('valhalla');
    INSERT INTO streetname VALUES('van');
    INSERT INTO streetname VALUES('vance davis');
    INSERT INTO streetname VALUES('vanhoy');
    INSERT INTO streetname VALUES('veckman');
    INSERT INTO streetname VALUES('victoria');
    INSERT INTO streetname VALUES('victory');
    INSERT INTO streetname VALUES('village glen');
    INSERT INTO streetname VALUES('vireo');
    INSERT INTO streetname VALUES('viscount');
    INSERT INTO streetname VALUES('voeltz');
    INSERT INTO streetname VALUES('wade e morgan');
    INSERT INTO streetname VALUES('wake');
    INSERT INTO streetname VALUES('wales');
    INSERT INTO streetname VALUES('wallace ridge');
    INSERT INTO streetname VALUES('waltham');
    INSERT INTO streetname VALUES('wanamassa');
    INSERT INTO streetname VALUES('warbler wood');
    INSERT INTO streetname VALUES('washington');
    INSERT INTO streetname VALUES('water');
    INSERT INTO streetname VALUES('waterelm');
    INSERT INTO streetname VALUES('waterford hills');
    INSERT INTO streetname VALUES('waterford valley');
    INSERT INTO streetname VALUES('waterloo');
    INSERT INTO streetname VALUES('waterton leas');
    INSERT INTO streetname VALUES('waverly lynn');
    INSERT INTO streetname VALUES('waverlyglen');
    INSERT INTO streetname VALUES('wayside');
    INSERT INTO streetname VALUES('westbury lake');
    INSERT INTO streetname VALUES('westray');
    INSERT INTO streetname VALUES('whistlers chase');
    INSERT INTO streetname VALUES('whistley green');
    INSERT INTO streetname VALUES('whistling oak');
    INSERT INTO streetname VALUES('whitcomb');
    INSERT INTO streetname VALUES('white aspen');
    INSERT INTO streetname VALUES('white cascade');
    INSERT INTO streetname VALUES('white mist');
    INSERT INTO streetname VALUES('white rock');
    INSERT INTO streetname VALUES('white stag');
    INSERT INTO streetname VALUES('whitegate');
    INSERT INTO streetname VALUES('whitehill');
    INSERT INTO streetname VALUES('whitetail');
    INSERT INTO streetname VALUES('whitewood');
    INSERT INTO streetname VALUES('wilburn park');
    INSERT INTO streetname VALUES('wild garden');
    INSERT INTO streetname VALUES('wild rose');
    INSERT INTO streetname VALUES('wilkins terrace');
    INSERT INTO streetname VALUES('william ficklen');
    INSERT INTO streetname VALUES('wiltshire ridge');
    INSERT INTO streetname VALUES('windchase');
    INSERT INTO streetname VALUES('winding jordan');
    INSERT INTO streetname VALUES('windy meadow');
    INSERT INTO streetname VALUES('winghaven');
    INSERT INTO streetname VALUES('wingmont');
    INSERT INTO streetname VALUES('winslow');
    INSERT INTO streetname VALUES('winter pine');
    INSERT INTO streetname VALUES('winter view');
    INSERT INTO streetname VALUES('wolf creek');
    INSERT INTO streetname VALUES('wondering oak');
    INSERT INTO streetname VALUES('woodard');
    INSERT INTO streetname VALUES('woodfire');
    INSERT INTO streetname VALUES('woodland commons');
    INSERT INTO streetname VALUES('woodland hills');
    INSERT INTO streetname VALUES('woodnotch');
    INSERT INTO streetname VALUES('woodstone');
    INSERT INTO streetname VALUES('worsley');
    INSERT INTO streetname VALUES('wren creek');
    INSERT INTO streetname VALUES('wrens nest');
    INSERT INTO streetname VALUES('wrexham');
    INSERT INTO streetname VALUES('wt harris');
    INSERT INTO streetname VALUES('wylie meadow');
    INSERT INTO streetname VALUES('wynborough');
    INSERT INTO streetname VALUES('wynbrook');
    INSERT INTO streetname VALUES('wyndham hill');
    INSERT INTO streetname VALUES('yandem');
    INSERT INTO streetname VALUES('yellow rose');
    INSERT INTO streetname VALUES('yellow spaniel');
    INSERT INTO streetname VALUES('yorkford');
    INSERT INTO streetname VALUES('ziegler');
    INSERT INTO streetname VALUES('zion renaissance');

    SELECT count(*) FROM streetname;
  }
} {1228}

do_test fuzzer1-2.1 {
  execsql {
    SELECT n, distance FROM f2, streetname
     WHERE f2.word MATCH 'wersley'
       AND f2.distance<=150
       AND f2.word=streetname.n
  }
} {worsley 37}
do_test fuzzer1-2.2 {
  execsql {
    SELECT n, distance FROM f2, streetname
     WHERE f2.word MATCH 'testledown'
       AND f2.distance<=150
       AND f2.word=streetname.n
  }
} {thistledown 103}
do_test fuzzer1-2.3 {
  execsql {
    SELECT DISTINCT streetname.n FROM f2, streetname
     WHERE f2.word MATCH 'tayle'
       AND f2.distance<=200
       AND streetname.n>=f2.word AND streetname.n<=(f2.word || x'F7BFBFBF')
  }
} {steelewood tallia tallu talwyn taymouth thelema trailer {tyler finley}}


finish_test
Changes to test/memdb.test.
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
do_test memdb-6.15 {
  execsql {
    DELETE FROM t5 WHERE x>0;
    SELECT * FROM t5;
  }
} {}

ifcapable subquery {
  do_test memdb-7.1 {

    execsql {
      CREATE TABLE t6(x);
      INSERT INTO t6 VALUES(1);
      INSERT INTO t6 SELECT x+1 FROM t6;
      INSERT INTO t6 SELECT x+2 FROM t6;
      INSERT INTO t6 SELECT x+4 FROM t6;
      INSERT INTO t6 SELECT x+8 FROM t6;
      INSERT INTO t6 SELECT x+16 FROM t6;
      INSERT INTO t6 SELECT x+32 FROM t6;
      INSERT INTO t6 SELECT x+64 FROM t6;
      INSERT INTO t6 SELECT x+128 FROM t6;
      SELECT count(*) FROM (SELECT DISTINCT x FROM t6);
    }
  } {256}
  for {set i 1} {$i<=256} {incr i} {
    do_test memdb-7.2.$i {
       execsql "DELETE FROM t6 WHERE x=\
                (SELECT x FROM t6 ORDER BY random() LIMIT 1)"







|

>


<
|
|
<
<
<
<
<
<







359
360
361
362
363
364
365
366
367
368
369
370

371
372






373
374
375
376
377
378
379
do_test memdb-6.15 {
  execsql {
    DELETE FROM t5 WHERE x>0;
    SELECT * FROM t5;
  }
} {}

ifcapable subquery&&vtab {
  do_test memdb-7.1 {
    register_wholenumber_module db
    execsql {
      CREATE TABLE t6(x);

      CREATE VIRTUAL TABLE nums USING wholenumber;
      INSERT INTO t6 SELECT value FROM nums WHERE value BETWEEN 1 AND 256;






      SELECT count(*) FROM (SELECT DISTINCT x FROM t6);
    }
  } {256}
  for {set i 1} {$i<=256} {incr i} {
    do_test memdb-7.2.$i {
       execsql "DELETE FROM t6 WHERE x=\
                (SELECT x FROM t6 ORDER BY random() LIMIT 1)"
Changes to test/multiplex.test.
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
#***********************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl

set g_chunk_size 2147483648
set g_max_chunks 32

# This handles appending the chunk number
# to the end of the filename.  if 
# SQLITE_MULTIPLEX_EXT_OVWR is defined, then
# it overwrites the last 2 bytes of the 
# file name with the chunk number.
proc multiplex_name {name chunk} {
  if {$chunk==0} { return $name }
  set num [format "%02d" $chunk]
  ifcapable {multiplex_ext_overwrite} {
    set name [string range $name 0 [expr [string length $name]-2-1]]
  }
  return $name$num
}

# This saves off the parameters and calls the 
# underlying sqlite3_multiplex_set() API.
proc multiplex_set {chunk_size max_chunks} {
  global g_chunk_size
  global g_max_chunks
  set g_chunk_size $chunk_size
  set g_max_chunks $max_chunks
  sqlite3_multiplex_set $chunk_size $max_chunks




}

# This attempts to delete the base file and 
# and files with the chunk extension.
proc multiplex_delete {name} {
  global g_max_chunks
  for {set i 0} {$i<$g_max_chunks} {incr i} {







|

















|
|


|

|
>
>
>
>







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
#***********************************************************************
#

set testdir [file dirname $argv0]
source $testdir/tester.tcl
source $testdir/malloc_common.tcl

set g_chunk_size [ expr ($::SQLITE_MAX_PAGE_SIZE*16384) ]
set g_max_chunks 32

# This handles appending the chunk number
# to the end of the filename.  if 
# SQLITE_MULTIPLEX_EXT_OVWR is defined, then
# it overwrites the last 2 bytes of the 
# file name with the chunk number.
proc multiplex_name {name chunk} {
  if {$chunk==0} { return $name }
  set num [format "%02d" $chunk]
  ifcapable {multiplex_ext_overwrite} {
    set name [string range $name 0 [expr [string length $name]-2-1]]
  }
  return $name$num
}

# This saves off the parameters and calls the 
# underlying sqlite3_multiplex_control() API.
proc multiplex_set {db name chunk_size max_chunks} {
  global g_chunk_size
  global g_max_chunks
  set g_chunk_size [ expr (($chunk_size+($::SQLITE_MAX_PAGE_SIZE-1)) & ~($::SQLITE_MAX_PAGE_SIZE-1)) ]
  set g_max_chunks $max_chunks
  set rc [catch {sqlite3_multiplex_control $db $name chunk_size $chunk_size} msg]
  if { $rc==0 } { 
    set rc [catch {sqlite3_multiplex_control $db $name max_chunks $max_chunks} msg]
  }
  list $msg
}

# This attempts to delete the base file and 
# and files with the chunk extension.
proc multiplex_delete {name} {
  global g_max_chunks
  for {set i 0} {$i<$g_max_chunks} {incr i} {
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
do_test multiplex-1.4 { sqlite3_multiplex_shutdown }               {SQLITE_OK}

do_test multiplex-1.5 { sqlite3_multiplex_initialize "" 0 }        {SQLITE_OK}
do_test multiplex-1.6 { sqlite3_multiplex_shutdown }               {SQLITE_OK}
do_test multiplex-1.7 { sqlite3_multiplex_initialize "" 1 }        {SQLITE_OK}
do_test multiplex-1.8 { sqlite3_multiplex_shutdown }               {SQLITE_OK}


do_test multiplex-1.9  { sqlite3_multiplex_initialize "" 1 }       {SQLITE_OK}
do_test multiplex-1.10.1 { multiplex_set 32768 16 }                {SQLITE_OK}








do_test multiplex-1.10.2 { multiplex_set 32768 -1 }                {SQLITE_MISUSE}


do_test multiplex-1.10.3 { multiplex_set -1 16 }                   {SQLITE_MISUSE}







do_test multiplex-1.10.4 { multiplex_set 31 16 }                   {SQLITE_MISUSE}







do_test multiplex-1.10.5 { multiplex_set 32768 100 }               {SQLITE_MISUSE}






do_test multiplex-1.11 { sqlite3_multiplex_shutdown }              {SQLITE_OK}








#-------------------------------------------------------------------------
# Some simple warm-body tests with a single database file in rollback 
# mode:
#
#   multiplex-2.1.*: Test simple writing to a multiplex file.
#
#   multiplex-2.2.*: More writing.
#
#   multiplex-2.3.*: Open and close a second db.
#
#   multiplex-2.4.*: Try to shutdown the multiplex system before closing the db
#                file. Check that this fails and the multiplex system still works
#                afterwards. Then close the database and successfully shut
#                down the multiplex system.
#
#   multiplex-2.5.*: More reading/writing.
#
#   multiplex-2.6.*: More reading/writing with varying small chunk sizes, as
#                well as varying journal mode.




sqlite3_multiplex_initialize "" 1
multiplex_set 32768 16

do_test multiplex-2.1.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size=1024;
    PRAGMA auto_vacuum=OFF;
    PRAGMA journal_mode=DELETE;







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

>
>
>
>
>
>




















|
>
>
>

|







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
do_test multiplex-1.4 { sqlite3_multiplex_shutdown }               {SQLITE_OK}

do_test multiplex-1.5 { sqlite3_multiplex_initialize "" 0 }        {SQLITE_OK}
do_test multiplex-1.6 { sqlite3_multiplex_shutdown }               {SQLITE_OK}
do_test multiplex-1.7 { sqlite3_multiplex_initialize "" 1 }        {SQLITE_OK}
do_test multiplex-1.8 { sqlite3_multiplex_shutdown }               {SQLITE_OK}


do_test multiplex-1.9.1  { sqlite3_multiplex_initialize "" 1 }     {SQLITE_OK}
do_test multiplex-1.9.2  { sqlite3 db test.db }                    {}
do_test multiplex-1.9.3  { multiplex_set db main 32768 16 }        {SQLITE_OK}
do_test multiplex-1.9.4  { multiplex_set db main 32768 -1 }        {SQLITE_MISUSE}
do_test multiplex-1.9.5  { multiplex_set db main -1 16 }           {SQLITE_MISUSE}
do_test multiplex-1.9.6  { multiplex_set db main 31 16 }           {SQLITE_OK}
do_test multiplex-1.9.7  { multiplex_set db main 32768 100 }       {SQLITE_MISUSE}
do_test multiplex-1.9.8  { multiplex_set db main 1073741824 1 }    {SQLITE_OK}
do_test multiplex-1.9.9  { db close }                              {}
do_test multiplex-1.9.10 { sqlite3_multiplex_shutdown }            {SQLITE_OK}

do_test multiplex-1.10.1  { sqlite3_multiplex_initialize "" 1 }                                  {SQLITE_OK}
do_test multiplex-1.10.2  { sqlite3 db test.db }                                                 {}
do_test multiplex-1.10.3  { lindex [ catchsql { SELECT multiplex_control(2, 32768); } ] 0 }      {0}
do_test multiplex-1.10.4  { lindex [ catchsql { SELECT multiplex_control(3, -1); } ] 0 }         {1}
do_test multiplex-1.10.5  { lindex [ catchsql { SELECT multiplex_control(2, -1); } ] 0 }         {1}
do_test multiplex-1.10.6  { lindex [ catchsql { SELECT multiplex_control(2, 31); } ] 0 }         {0}
do_test multiplex-1.10.7  { lindex [ catchsql { SELECT multiplex_control(3, 100); } ] 0 }        {1}
do_test multiplex-1.10.8  { lindex [ catchsql { SELECT multiplex_control(2, 1073741824); } ] 0 } {0}
do_test multiplex-1.10.9  { db close }                                                           {}
do_test multiplex-1.10.10 { sqlite3_multiplex_shutdown }                                         {SQLITE_OK}

do_test multiplex-1.11.1  { sqlite3_multiplex_initialize "" 1 }               {SQLITE_OK}
do_test multiplex-1.11.2  { sqlite3 db test.db }                              {}
do_test multiplex-1.11.3  { sqlite3_multiplex_control db main enable 0  }     {SQLITE_OK}
do_test multiplex-1.11.4  { sqlite3_multiplex_control db main enable 1  }     {SQLITE_OK}
do_test multiplex-1.11.5  { sqlite3_multiplex_control db main enable -1 }     {SQLITE_OK}
do_test multiplex-1.11.6  { db close }                                        {}
do_test multiplex-1.11.7  { sqlite3_multiplex_shutdown }                      {SQLITE_OK}

do_test multiplex-1.12.1  { sqlite3_multiplex_initialize "" 1 }                           {SQLITE_OK}
do_test multiplex-1.12.2  { sqlite3 db test.db }                                          {}
do_test multiplex-1.12.3  { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 }   {0}
do_test multiplex-1.12.4  { lindex [ catchsql { SELECT multiplex_control(1, 1); } ] 0 }   {0}
do_test multiplex-1.12.5  { lindex [ catchsql { SELECT multiplex_control(1, -1); } ] 0 }  {0}
do_test multiplex-1.12.6  { db close }                                                    {}
do_test multiplex-1.12.7  { sqlite3_multiplex_shutdown }                                  {SQLITE_OK}

do_test multiplex-1.13.1  { sqlite3_multiplex_initialize "" 1 }                           {SQLITE_OK}
do_test multiplex-1.13.2  { sqlite3 db test.db }                                          {}
do_test multiplex-1.13.3  { lindex [ catchsql { SELECT multiplex_control(-1, 0); } ] 0 }  {1}
do_test multiplex-1.13.4  { lindex [ catchsql { SELECT multiplex_control(4, 1); } ] 0 }   {1}
do_test multiplex-1.13.6  { db close }                                                    {}
do_test multiplex-1.13.7  { sqlite3_multiplex_shutdown }                                  {SQLITE_OK}

#-------------------------------------------------------------------------
# Some simple warm-body tests with a single database file in rollback 
# mode:
#
#   multiplex-2.1.*: Test simple writing to a multiplex file.
#
#   multiplex-2.2.*: More writing.
#
#   multiplex-2.3.*: Open and close a second db.
#
#   multiplex-2.4.*: Try to shutdown the multiplex system before closing the db
#                file. Check that this fails and the multiplex system still works
#                afterwards. Then close the database and successfully shut
#                down the multiplex system.
#
#   multiplex-2.5.*: More reading/writing.
#
#   multiplex-2.6.*: More reading/writing with varying small chunk sizes, as
#                well as varying journal mode.
#
#   multiplex-2.7.*: Disable/enable tests.
#

sqlite3_multiplex_initialize "" 1
multiplex_set db main 32768 16

do_test multiplex-2.1.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size=1024;
    PRAGMA auto_vacuum=OFF;
    PRAGMA journal_mode=DELETE;
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
} {}
do_test multiplex-2.2.3 { file size [multiplex_name test.db 0] } {6144}

do_test multiplex-2.3.1 {
  sqlite3 db2 test2.db
  db2 close
} {}


do_test multiplex-2.4.1 {
  sqlite3_multiplex_shutdown
} {SQLITE_MISUSE}
do_test multiplex-2.4.2 {
  execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
} {}
do_test multiplex-2.4.4 { file size [multiplex_name test.db 0] } {7168}
do_test multiplex-2.4.99 {
  db close
  sqlite3_multiplex_shutdown
} {SQLITE_OK}


do_test multiplex-2.5.1 {
  multiplex_delete test.db
  sqlite3_multiplex_initialize "" 1

  multiplex_set 4096 16
} {SQLITE_OK}

do_test multiplex-2.5.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = delete;
    PRAGMA auto_vacuum = off;
    CREATE TABLE t1(a PRIMARY KEY, b);
  }
} {delete}

do_test multiplex-2.5.3 { 
  execsql { 
    INSERT INTO t1 VALUES(1, 'one');
    INSERT INTO t1 VALUES(2, randomblob(4000));
    INSERT INTO t1 VALUES(3, 'three');
    INSERT INTO t1 VALUES(4, randomblob(4000));
    INSERT INTO t1 VALUES(5, 'five') 


  }
} {}

do_test multiplex-2.5.4 {
  db eval {SELECT * FROM t1 WHERE a=1}
} {1 one}








>

















>
|



<














|
>
>







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
} {}
do_test multiplex-2.2.3 { file size [multiplex_name test.db 0] } {6144}

do_test multiplex-2.3.1 {
  sqlite3 db2 test2.db
  db2 close
} {}


do_test multiplex-2.4.1 {
  sqlite3_multiplex_shutdown
} {SQLITE_MISUSE}
do_test multiplex-2.4.2 {
  execsql { INSERT INTO t1 VALUES(3, randomblob(1100)) }
} {}
do_test multiplex-2.4.4 { file size [multiplex_name test.db 0] } {7168}
do_test multiplex-2.4.99 {
  db close
  sqlite3_multiplex_shutdown
} {SQLITE_OK}


do_test multiplex-2.5.1 {
  multiplex_delete test.db
  sqlite3_multiplex_initialize "" 1
  sqlite3 db test.db
  multiplex_set db main 4096 16
} {SQLITE_OK}

do_test multiplex-2.5.2 {

  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = delete;
    PRAGMA auto_vacuum = off;
    CREATE TABLE t1(a PRIMARY KEY, b);
  }
} {delete}

do_test multiplex-2.5.3 { 
  execsql { 
    INSERT INTO t1 VALUES(1, 'one');
    INSERT INTO t1 VALUES(2, randomblob(4000));
    INSERT INTO t1 VALUES(3, 'three');
    INSERT INTO t1 VALUES(4, randomblob(4000));
    INSERT INTO t1 VALUES(5, 'five');
    INSERT INTO t1 VALUES(6, randomblob($g_chunk_size));
    INSERT INTO t1 VALUES(7, randomblob($g_chunk_size));
  }
} {}

do_test multiplex-2.5.4 {
  db eval {SELECT * FROM t1 WHERE a=1}
} {1 one}

201
202
203
204
205
206
207

208
209
210
211
212
213
214
215
216
217
218
219
set all_journal_modes {delete persist truncate memory off}
foreach jmode $all_journal_modes {
  for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {

    do_test multiplex-2.6.1.$sz.$jmode {
      multiplex_delete test.db
      sqlite3_multiplex_initialize "" 1

      multiplex_set $sz 32
    } {SQLITE_OK}

    do_test multiplex-2.6.2.$sz.$jmode {
      sqlite3 db test.db
      db eval {
        PRAGMA page_size = 1024;
        PRAGMA auto_vacuum = off;
      }
      db eval "PRAGMA journal_mode = $jmode;"
    } $jmode








>
|



<







248
249
250
251
252
253
254
255
256
257
258
259

260
261
262
263
264
265
266
set all_journal_modes {delete persist truncate memory off}
foreach jmode $all_journal_modes {
  for {set sz 151} {$sz<8000} {set sz [expr $sz+419]} {

    do_test multiplex-2.6.1.$sz.$jmode {
      multiplex_delete test.db
      sqlite3_multiplex_initialize "" 1
      sqlite3 db test.db
      multiplex_set db main $sz 32
    } {SQLITE_OK}

    do_test multiplex-2.6.2.$sz.$jmode {

      db eval {
        PRAGMA page_size = 1024;
        PRAGMA auto_vacuum = off;
      }
      db eval "PRAGMA journal_mode = $jmode;"
    } $jmode

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
      db close
      sqlite3_multiplex_shutdown
    } {SQLITE_OK}

  }
}


























#-------------------------------------------------------------------------
# Try some tests with more than one connection to a database file. Still
# in rollback mode.
#
#   multiplex-3.1.*: Two connections to a single database file.
#
#   multiplex-3.2.*: Two connections to each of several database files (that
#                are in the same multiplex group).
#
do_test multiplex-3.1.1 {
  multiplex_delete test.db
  sqlite3_multiplex_initialize "" 1

  multiplex_set 32768 16
} {SQLITE_OK}
do_test multiplex-3.1.2 {
  sqlite3 db test.db
  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = delete;
    PRAGMA auto_vacuum = off;
    CREATE TABLE t1(a PRIMARY KEY, b);
    INSERT INTO t1 VALUES(1, 'one');
  }







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












>
|


<







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
      db close
      sqlite3_multiplex_shutdown
    } {SQLITE_OK}

  }
}

do_test multiplex-2.7.1  { multiplex_delete test.db }                                       {}
do_test multiplex-2.7.2  { sqlite3_multiplex_initialize "" 1 }                              {SQLITE_OK}
do_test multiplex-2.7.3  { sqlite3 db test.db }                                             {}
do_test multiplex-2.7.4  { lindex [ catchsql { SELECT multiplex_control(2, 65536); } ] 0 }  {0}
do_test multiplex-2.7.5  { lindex [ catchsql { SELECT multiplex_control(1, 0); } ] 0 }      {0}
do_test multiplex-2.7.6 { 
  execsql { 
    CREATE TABLE t1(a PRIMARY KEY, b);
    INSERT INTO t1 VALUES(1, randomblob(1000));
  }
} {}
# verify only one file, and file size is less than chunks size
do_test multiplex-2.7.7  { expr ([file size [multiplex_name test.db 0]] < 65536) } {1}
do_test multiplex-2.7.8  { file exists [multiplex_name test.db 1] }                {0}
do_test multiplex-2.7.9 { 
  execsql { 
    INSERT INTO t1 VALUES(2, randomblob(65536));
  }
} {}
# verify only one file, and file size exceeds chunks size
do_test multiplex-2.7.10 { expr ([file size [multiplex_name test.db 0]] > 65536) } {1}
do_test multiplex-2.7.11 { file exists [multiplex_name test.db 1] }                {0}
do_test multiplex-2.7.12 { db close }                                              {}
do_test multiplex-2.7.13 { sqlite3_multiplex_shutdown }                            {SQLITE_OK}

#-------------------------------------------------------------------------
# Try some tests with more than one connection to a database file. Still
# in rollback mode.
#
#   multiplex-3.1.*: Two connections to a single database file.
#
#   multiplex-3.2.*: Two connections to each of several database files (that
#                are in the same multiplex group).
#
do_test multiplex-3.1.1 {
  multiplex_delete test.db
  sqlite3_multiplex_initialize "" 1
  sqlite3 db test.db
  multiplex_set db main 32768 16
} {SQLITE_OK}
do_test multiplex-3.1.2 {

  execsql {
    PRAGMA page_size = 1024;
    PRAGMA journal_mode = delete;
    PRAGMA auto_vacuum = off;
    CREATE TABLE t1(a PRIMARY KEY, b);
    INSERT INTO t1 VALUES(1, 'one');
  }
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
  foreach db {db1a db2a db2b db1b} { catch { $db close } }
} {}

#-------------------------------------------------------------------------
#

sqlite3_multiplex_initialize "" 1
multiplex_set 32768 16

# Return a list of all currently defined multiplexs.
proc multiplex_list {} {
  set allq {}
  foreach q [sqlite3_multiplex_dump] {
    lappend allq [lindex $q 0]
  }







|







409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
  foreach db {db1a db2a db2b db1b} { catch { $db close } }
} {}

#-------------------------------------------------------------------------
#

sqlite3_multiplex_initialize "" 1
multiplex_set db main 32768 16

# Return a list of all currently defined multiplexs.
proc multiplex_list {} {
  set allq {}
  foreach q [sqlite3_multiplex_dump] {
    lappend allq [lindex $q 0]
  }
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

#-------------------------------------------------------------------------
# The following tests test that the multiplex VFS handles malloc and IO 
# errors.
#

sqlite3_multiplex_initialize "" 1
multiplex_set 32768 16

do_faultsim_test multiplex-5.1 -prep {
  catch {db close}
} -body {
  sqlite3 db test2.db
}
do_faultsim_test multiplex-5.2 -prep {







|







471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

#-------------------------------------------------------------------------
# The following tests test that the multiplex VFS handles malloc and IO 
# errors.
#

sqlite3_multiplex_initialize "" 1
multiplex_set db main 32768 16

do_faultsim_test multiplex-5.1 -prep {
  catch {db close}
} -body {
  sqlite3 db test2.db
}
do_faultsim_test multiplex-5.2 -prep {
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
} {1 {unable to open database file}}
catch { file delete test.db }

do_faultsim_test multiplex-5.5 -prep {
  catch { sqlite3_multiplex_shutdown }
} -body {
  sqlite3_multiplex_initialize "" 1
  multiplex_set 32768 16
}

# test that mismatch filesize is detected
#
# Do not run this test if $::G(perm:presql) is set. If it is set, then the
# expected IO error will occur within the Tcl [sqlite3] wrapper, not within
# the first SQL statement executed below. This breaks the test case.







|







516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
} {1 {unable to open database file}}
catch { file delete test.db }

do_faultsim_test multiplex-5.5 -prep {
  catch { sqlite3_multiplex_shutdown }
} -body {
  sqlite3_multiplex_initialize "" 1
  multiplex_set db main 32768 16
}

# test that mismatch filesize is detected
#
# Do not run this test if $::G(perm:presql) is set. If it is set, then the
# expected IO error will occur within the Tcl [sqlite3] wrapper, not within
# the first SQL statement executed below. This breaks the test case.
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
        PRAGMA auto_vacuum = off;
      }
      db eval "PRAGMA journal_mode = $jmode;"
    } $jmode
    do_test multiplex-5.6.2.$jmode {
      execsql {
        CREATE TABLE t1(a, b);
        INSERT INTO t1 VALUES(1, randomblob(1100));
        INSERT INTO t1 VALUES(2, randomblob(1100));
        INSERT INTO t1 VALUES(3, randomblob(1100));
        INSERT INTO t1 VALUES(4, randomblob(1100));
        INSERT INTO t1 VALUES(5, randomblob(1100));
      }
      db close
      sqlite3_multiplex_initialize "" 1
      multiplex_set 4096 16
      sqlite3 db test.db

    } {}
    do_test multiplex-5.6.3.$jmode {
      catchsql {
        INSERT INTO t1 VALUES(6, randomblob(1100));
      }
    } {1 {disk I/O error}}
    do_test multiplex-5.6.4.$jmode {
      db close
    } {}
  }
}

catch { sqlite3_multiplex_shutdown }
finish_test







|
|
|
|
|



<

>
|


|










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
        PRAGMA auto_vacuum = off;
      }
      db eval "PRAGMA journal_mode = $jmode;"
    } $jmode
    do_test multiplex-5.6.2.$jmode {
      execsql {
        CREATE TABLE t1(a, b);
        INSERT INTO t1 VALUES(1, randomblob(15000));
        INSERT INTO t1 VALUES(2, randomblob(15000));
        INSERT INTO t1 VALUES(3, randomblob(15000));
        INSERT INTO t1 VALUES(4, randomblob(15000));
        INSERT INTO t1 VALUES(5, randomblob(15000));
      }
      db close
      sqlite3_multiplex_initialize "" 1

      sqlite3 db test.db
      multiplex_set db main 4096 16
    } {SQLITE_OK}
    do_test multiplex-5.6.3.$jmode {
      catchsql {
        INSERT INTO t1 VALUES(6, randomblob(15000));
      }
    } {1 {disk I/O error}}
    do_test multiplex-5.6.4.$jmode {
      db close
    } {}
  }
}

catch { sqlite3_multiplex_shutdown }
finish_test
Changes to test/oserror.test.
96
97
98
99
100
101
102

103
104

105
106


107
108
109
110
111
112
113

114
115
116
117
#--------------------------------------------------------------------------
# Tests oserror-1.* test failures in the unlink() system call.
#
do_test 2.1.1 {
  set ::log [list]
  file mkdir test.db-wal
  forcedelete test.db

  sqlite3 dbh test.db
  catchsql { SELECT * FROM sqlite_master } dbh

} {1 {disk I/O error}}



do_re_test 2.1.2 { lindex $::log 0 } {^os_unix.c:\d+: \(\d+\) unlink\(.*test.db-wal\) - }
do_test 2.1.3 { 
  dbh close
  forcedelete test.db-wal
} {}
  


sqlite3_shutdown
test_sqlite3_log 
sqlite3_initialize
finish_test







>
|
|
>


>
>
|

|




>




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
#--------------------------------------------------------------------------
# Tests oserror-1.* test failures in the unlink() system call.
#
do_test 2.1.1 {
  set ::log [list]
  file mkdir test.db-wal
  forcedelete test.db
  list [catch {
    sqlite3 dbh test.db
    execsql { SELECT * FROM sqlite_master } dbh
  } msg] $msg
} {1 {disk I/O error}}

do_re_test 2.1.2 { 
  lindex $::log 0 
} {^os_unix.c:\d+: \(\d+\) unlink\(.*test.db-wal\) - }
do_test 2.1.3 { 
  catch { dbh close }
  forcedelete test.db-wal
} {}
  

test_syscall reset
sqlite3_shutdown
test_sqlite3_log 
sqlite3_initialize
finish_test
Changes to test/pagerfault3.test.
22
23
24
25
26
27
28

29
30
31
32
33
34
35

# Create a database with page-size 2048 bytes that uses 2 pages. Populate
# it so that if the page-size is changed to 1024 bytes and the db vacuumed, 
# the new db size is 3 pages.
#
do_test pagerfault3-pre1 {
  execsql {

    PRAGMA page_size = 2048;
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(randomblob(1200));
    PRAGMA page_count;
  }
} {2}
do_test pagerfault3-pre2 {







>







22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

# Create a database with page-size 2048 bytes that uses 2 pages. Populate
# it so that if the page-size is changed to 1024 bytes and the db vacuumed, 
# the new db size is 3 pages.
#
do_test pagerfault3-pre1 {
  execsql {
    PRAGMA auto_vacuum = 0;
    PRAGMA page_size = 2048;
    CREATE TABLE t1(x);
    INSERT INTO t1 VALUES(randomblob(1200));
    PRAGMA page_count;
  }
} {2}
do_test pagerfault3-pre2 {
Changes to test/syscall.test.
15
16
17
18
19
20
21
22



23

24
25
26
27
28
29
30
source $testdir/lock_common.tcl
source $testdir/malloc_common.tcl

if {[llength [info commands test_syscall]]==0} {
  finish_test
  return
} 
set testprefix syscall






#-------------------------------------------------------------------------
# Tests for the xSetSystemCall method.
#
do_test 1.1.1 {
  list [catch { test_syscall reset open } msg] $msg
} {0 {}}







|
>
>
>
|
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
source $testdir/lock_common.tcl
source $testdir/malloc_common.tcl

if {[llength [info commands test_syscall]]==0} {
  finish_test
  return
} 

if {[test_syscall defaultvfs] != "unix"} {
  finish_test
  return
}
set testprefix syscall

#-------------------------------------------------------------------------
# Tests for the xSetSystemCall method.
#
do_test 1.1.1 {
  list [catch { test_syscall reset open } msg] $msg
} {0 {}}
48
49
50
51
52
53
54
55
56
57
58


59

60
61
62
63
64
65
66
67
#
do_test 2.1.1 { test_syscall exists open } 1
do_test 2.1.2 { test_syscall exists nosuchcall } 0

#-------------------------------------------------------------------------
# Tests for the xNextSystemCall method.
#
set syscall_list [list                                \
    open close access getcwd stat fstat ftruncate     \
    fcntl read pread write pwrite fchmod              \
]


if {[test_syscall exists fallocate]} {lappend syscall_list fallocate}

do_test 3.1 { test_syscall list } $syscall_list

#-------------------------------------------------------------------------
# This test verifies that if a call to open() fails and errno is set to
# EINTR, the call is retried. If it succeeds, execution continues as if
# nothing happened. 
#
test_syscall reset







|
|
|
<
>
>
|
>
|







52
53
54
55
56
57
58
59
60
61

62
63
64
65
66
67
68
69
70
71
72
73
#
do_test 2.1.1 { test_syscall exists open } 1
do_test 2.1.2 { test_syscall exists nosuchcall } 0

#-------------------------------------------------------------------------
# Tests for the xNextSystemCall method.
#
foreach s {
    open close access getcwd stat fstat ftruncate
    fcntl read pread write pwrite fchmod fallocate

    pread64 pwrite64
} {
  if {[test_syscall exists $s]} {lappend syscall_list $s}
}
do_test 3.1 { lsort [test_syscall list] } [lsort $syscall_list]

#-------------------------------------------------------------------------
# This test verifies that if a call to open() fails and errno is set to
# EINTR, the call is retried. If it succeeds, execution continues as if
# nothing happened. 
#
test_syscall reset
211
212
213
214
215
216
217

218
219

220
221
222
223
224
225
226
foreach {nByte res} {
  1      {0 {}}
  2      {1 {file is encrypted or is not a database}}
  3      {1 {file is encrypted or is not a database}}
} {
  do_test 7.$nByte {
    create_db_file $nByte

    sqlite3 db test.db
    catchsql { CREATE TABLE t1(a, b) }

  } $res
  catch { db close }
}

#-------------------------------------------------------------------------
# 
catch { db close }







>
|
|
>







217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
foreach {nByte res} {
  1      {0 {}}
  2      {1 {file is encrypted or is not a database}}
  3      {1 {file is encrypted or is not a database}}
} {
  do_test 7.$nByte {
    create_db_file $nByte
    list [catch {
      sqlite3 db test.db
      execsql { CREATE TABLE t1(a, b) }
    } msg] $msg
  } $res
  catch { db close }
}

#-------------------------------------------------------------------------
# 
catch { db close }
241
242
243
244
245
246
247
248
249
250
} {
  do_test 8.2.$tn {
    file_control_sizehint_test db main $hint
    file size test.db
  } $size
}



finish_test







|
<

249
250
251
252
253
254
255
256

257
} {
  do_test 8.2.$tn {
    file_control_sizehint_test db main $hint
    file size test.db
  } $size
}

test_syscall reset

finish_test
Changes to test/wal.test.
459
460
461
462
463
464
465

466
467
468
469
470
471
472
#
do_multiclient_test tn {

  # Initialize the database schema and contents.
  #
  do_test wal-10.$tn.1 {
    execsql {

      PRAGMA journal_mode = wal;
      CREATE TABLE t1(a, b);
      INSERT INTO t1 VALUES(1, 2);
      SELECT * FROM t1;
    }
  } {wal 1 2}








>







459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#
do_multiclient_test tn {

  # Initialize the database schema and contents.
  #
  do_test wal-10.$tn.1 {
    execsql {
      PRAGMA auto_vacuum = 0;
      PRAGMA journal_mode = wal;
      CREATE TABLE t1(a, b);
      INSERT INTO t1 VALUES(1, 2);
      SELECT * FROM t1;
    }
  } {wal 1 2}

Changes to test/wal2.test.
339
340
341
342
343
344
345

346
347
348
349
350
351
352
#-------------------------------------------------------------------------
# Test that a database connection using a VFS that does not support the
# xShmXXX interfaces cannot open a WAL database.
#
do_test wal2-4.1 {
  sqlite3 db test.db
  execsql {

    PRAGMA journal_mode = WAL;
    CREATE TABLE data(x);
    INSERT INTO data VALUES('need xShmOpen to see this');
    PRAGMA wal_checkpoint;
  }
} {wal 0 5 5}
do_test wal2-4.2 {







>







339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#-------------------------------------------------------------------------
# Test that a database connection using a VFS that does not support the
# xShmXXX interfaces cannot open a WAL database.
#
do_test wal2-4.1 {
  sqlite3 db test.db
  execsql {
    PRAGMA auto_vacuum = 0;
    PRAGMA journal_mode = WAL;
    CREATE TABLE data(x);
    INSERT INTO data VALUES('need xShmOpen to see this');
    PRAGMA wal_checkpoint;
  }
} {wal 0 5 5}
do_test wal2-4.2 {
618
619
620
621
622
623
624

625
626
627
628
629
630
631
  {4 1 lock shared} 
    {0 1 lock exclusive} {0 1 unlock exclusive} 
  {4 1 unlock shared}
}

foreach {tn sql res expected_locks} {
  2 {

    PRAGMA journal_mode = WAL;
    BEGIN;
      CREATE TABLE t1(x);
      INSERT INTO t1 VALUES('Leonard');
      INSERT INTO t1 VALUES('Arthur');
    COMMIT;
  } {wal} {







>







619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
  {4 1 lock shared} 
    {0 1 lock exclusive} {0 1 unlock exclusive} 
  {4 1 unlock shared}
}

foreach {tn sql res expected_locks} {
  2 {
    PRAGMA auto_vacuum = 0;
    PRAGMA journal_mode = WAL;
    BEGIN;
      CREATE TABLE t1(x);
      INSERT INTO t1 VALUES('Leonard');
      INSERT INTO t1 VALUES('Arthur');
    COMMIT;
  } {wal} {
703
704
705
706
707
708
709

710
711
712
713
714
715
716

db close
tvfs delete

do_test wal2-6.5.1 {
  sqlite3 db test.db
  execsql {

    PRAGMA journal_mode = wal;
    PRAGMA locking_mode = exclusive;
    CREATE TABLE t2(a, b);
    PRAGMA wal_checkpoint;
    INSERT INTO t2 VALUES('I', 'II');
    PRAGMA journal_mode;
  }







>







705
706
707
708
709
710
711
712
713
714
715
716
717
718
719

db close
tvfs delete

do_test wal2-6.5.1 {
  sqlite3 db test.db
  execsql {
    PRAGMA auto_vacuum = 0;
    PRAGMA journal_mode = wal;
    PRAGMA locking_mode = exclusive;
    CREATE TABLE t2(a, b);
    PRAGMA wal_checkpoint;
    INSERT INTO t2 VALUES('I', 'II');
    PRAGMA journal_mode;
  }
1162
1163
1164
1165
1166
1167
1168

1169
1170
1171
1172
1173
1174
1175
foreach {tn sql reslist} {
  1 { }                                 {8 0 3 0 5 0}
  2 { PRAGMA checkpoint_fullfsync = 1 } {8 4 3 2 5 2}
  3 { PRAGMA checkpoint_fullfsync = 0 } {8 0 3 0 5 0}
} {
  faultsim_delete_and_reopen


  execsql $sql
  do_execsql_test wal2-14.$tn.1 { PRAGMA journal_mode = WAL } {wal}

  set sqlite_sync_count 0
  set sqlite_fullsync_count 0

  do_execsql_test wal2-14.$tn.2 {







>







1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
foreach {tn sql reslist} {
  1 { }                                 {8 0 3 0 5 0}
  2 { PRAGMA checkpoint_fullfsync = 1 } {8 4 3 2 5 2}
  3 { PRAGMA checkpoint_fullfsync = 0 } {8 0 3 0 5 0}
} {
  faultsim_delete_and_reopen

  execsql {PRAGMA auto_vacuum = 0}
  execsql $sql
  do_execsql_test wal2-14.$tn.1 { PRAGMA journal_mode = WAL } {wal}

  set sqlite_sync_count 0
  set sqlite_fullsync_count 0

  do_execsql_test wal2-14.$tn.2 {
Changes to test/wal3.test.
409
410
411
412
413
414
415

416
417
418
419
420
421
422
#     obtain a different read-lock.
#
catch {db close}
testvfs T -default 1
do_test wal3-6.1.1 {
  file delete -force test.db test.db-journal test.db wal
  sqlite3 db test.db

  execsql { PRAGMA journal_mode = WAL }
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES('o', 't');
    INSERT INTO t1 VALUES('t', 'f');
  }
} {}







>







409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#     obtain a different read-lock.
#
catch {db close}
testvfs T -default 1
do_test wal3-6.1.1 {
  file delete -force test.db test.db-journal test.db wal
  sqlite3 db test.db
  execsql { PRAGMA auto_vacuum = off }
  execsql { PRAGMA journal_mode = WAL }
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES('o', 't');
    INSERT INTO t1 VALUES('t', 'f');
  }
} {}
490
491
492
493
494
495
496

497
498
499
500
501
502
503
db2 close
db close

do_test wal3-6.2.1 {
  file delete -force test.db test.db-journal test.db wal
  sqlite3 db test.db
  sqlite3 db2 test.db

  execsql { PRAGMA journal_mode = WAL }
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES('h', 'h');
    INSERT INTO t1 VALUES('l', 'b');
  }
} {}







>







491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
db2 close
db close

do_test wal3-6.2.1 {
  file delete -force test.db test.db-journal test.db wal
  sqlite3 db test.db
  sqlite3 db2 test.db
  execsql { PRAGMA auto_vacuum = off }
  execsql { PRAGMA journal_mode = WAL }
  execsql {
    CREATE TABLE t1(a, b);
    INSERT INTO t1 VALUES('h', 'h');
    INSERT INTO t1 VALUES('l', 'b');
  }
} {}
613
614
615
616
617
618
619

620
621
622
623
624
625
626
#-------------------------------------------------------------------------
# 
do_test wal3-8.1 {
  file delete -force test.db test.db-journal test.db wal
  sqlite3 db test.db
  sqlite3 db2 test.db
  execsql {

    PRAGMA journal_mode = WAL;
    CREATE TABLE b(c);
    INSERT INTO b VALUES('Tehran');
    INSERT INTO b VALUES('Qom');
    INSERT INTO b VALUES('Markazi');
    PRAGMA wal_checkpoint;
  }







>







615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#-------------------------------------------------------------------------
# 
do_test wal3-8.1 {
  file delete -force test.db test.db-journal test.db wal
  sqlite3 db test.db
  sqlite3 db2 test.db
  execsql {
    PRAGMA auto_vacuum = off;
    PRAGMA journal_mode = WAL;
    CREATE TABLE b(c);
    INSERT INTO b VALUES('Tehran');
    INSERT INTO b VALUES('Qom');
    INSERT INTO b VALUES('Markazi');
    PRAGMA wal_checkpoint;
  }
Changes to test/wal5.test.
165
166
167
168
169
170
171


172
173
174
175
176
177
178
  # database file.
  #
  proc setup_and_attach_aux {} {
    sql1 { ATTACH 'test.db2' AS aux }
    sql2 { ATTACH 'test.db2' AS aux }
    sql3 { ATTACH 'test.db2' AS aux }
    sql1 {


      PRAGMA main.page_size=1024; PRAGMA main.journal_mode=WAL;
      PRAGMA aux.page_size=1024;  PRAGMA aux.journal_mode=WAL;
    }
  }

  proc file_page_counts {} {
    list [db_page_count  test.db ] \







>
>







165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
  # database file.
  #
  proc setup_and_attach_aux {} {
    sql1 { ATTACH 'test.db2' AS aux }
    sql2 { ATTACH 'test.db2' AS aux }
    sql3 { ATTACH 'test.db2' AS aux }
    sql1 {
      PRAGMA aux.auto_vacuum = 0;
      PRAGMA main.auto_vacuum = 0;
      PRAGMA main.page_size=1024; PRAGMA main.journal_mode=WAL;
      PRAGMA aux.page_size=1024;  PRAGMA aux.journal_mode=WAL;
    }
  }

  proc file_page_counts {} {
    list [db_page_count  test.db ] \
305
306
307
308
309
310
311

312
313
314
315
316
317
318

    code1 $do_wal_checkpoint
    code2 $do_wal_checkpoint
    code3 $do_wal_checkpoint
    
    do_test 3.$tn.1 {
      sql1 {

        PRAGMA journal_mode = WAL;
        PRAGMA synchronous = normal;
        CREATE TABLE t1(x, y);
      }

      sql2 { PRAGMA journal_mode }
      sql3 { PRAGMA journal_mode }







>







307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

    code1 $do_wal_checkpoint
    code2 $do_wal_checkpoint
    code3 $do_wal_checkpoint
    
    do_test 3.$tn.1 {
      sql1 {
        PRAGMA auto_vacuum = 0;
        PRAGMA journal_mode = WAL;
        PRAGMA synchronous = normal;
        CREATE TABLE t1(x, y);
      }

      sql2 { PRAGMA journal_mode }
      sql3 { PRAGMA journal_mode }
Changes to test/walfault.test.
129
130
131
132
133
134
135

136
137
138
139
140
141
142
if {[permutation] != "inmemory_journal"} {
  faultsim_delete_and_reopen
  faultsim_save_and_close
  do_faultsim_test walfault-4 -prep {
    faultsim_restore_and_reopen
  } -body {
    execsql {

      PRAGMA journal_mode = WAL;
      CREATE TABLE t1(a PRIMARY KEY, b);
      INSERT INTO t1 VALUES('a', 'b');
      PRAGMA wal_checkpoint;
      SELECT * FROM t1;
    }
  } -test {







>







129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
if {[permutation] != "inmemory_journal"} {
  faultsim_delete_and_reopen
  faultsim_save_and_close
  do_faultsim_test walfault-4 -prep {
    faultsim_restore_and_reopen
  } -body {
    execsql {
      PRAGMA auto_vacuum = 0;
      PRAGMA journal_mode = WAL;
      CREATE TABLE t1(a PRIMARY KEY, b);
      INSERT INTO t1 VALUES('a', 'b');
      PRAGMA wal_checkpoint;
      SELECT * FROM t1;
    }
  } -test {
519
520
521
522
523
524
525

526
527
528
529
530
531
532

#-------------------------------------------------------------------------
# Test fault-handling when wrapping around to the start of a WAL file.
#
do_test walfault-14-pre {
  faultsim_delete_and_reopen
  execsql {

    PRAGMA journal_mode = WAL;
    BEGIN;
      CREATE TABLE abc(a PRIMARY KEY);
      INSERT INTO abc VALUES(randomblob(1500));
      INSERT INTO abc VALUES(randomblob(1500));
    COMMIT;
  }







>







520
521
522
523
524
525
526
527
528
529
530
531
532
533
534

#-------------------------------------------------------------------------
# Test fault-handling when wrapping around to the start of a WAL file.
#
do_test walfault-14-pre {
  faultsim_delete_and_reopen
  execsql {
    PRAGMA auto_vacuum = 0;
    PRAGMA journal_mode = WAL;
    BEGIN;
      CREATE TABLE abc(a PRIMARY KEY);
      INSERT INTO abc VALUES(randomblob(1500));
      INSERT INTO abc VALUES(randomblob(1500));
    COMMIT;
  }
tool/mkopts.tcl became a regular file.
Changes to tool/mksqlite3c.tcl.
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#
set out [open sqlite3.c w]
set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1]
puts $out [subst \
{/******************************************************************************
** This file is an amalgamation of many separate C source files from SQLite
** version $VERSION.  By combining all the individual C code files into this 
** single large file, the entire code can be compiled as a one translation
** unit.  This allows many compilers to do optimizations that would not be
** possible if the files were compiled separately.  Performance improvements
** of 5% or more are commonly seen when SQLite is compiled as a single
** translation unit.
**
** This file is all you need to compile SQLite.  To use SQLite in other
** programs, you need this file and the "sqlite3.h" header file that defines







|







47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#
set out [open sqlite3.c w]
set today [clock format [clock seconds] -format "%Y-%m-%d %H:%M:%S UTC" -gmt 1]
puts $out [subst \
{/******************************************************************************
** This file is an amalgamation of many separate C source files from SQLite
** version $VERSION.  By combining all the individual C code files into this 
** single large file, the entire code can be compiled as a single translation
** unit.  This allows many compilers to do optimizations that would not be
** possible if the files were compiled separately.  Performance improvements
** of 5% or more are commonly seen when SQLite is compiled as a single
** translation unit.
**
** This file is all you need to compile SQLite.  To use SQLite in other
** programs, you need this file and the "sqlite3.h" header file that defines
Added tool/split-sqlite3c.tcl.




































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#!/usr/bin/tclsh
#
# This script splits the sqlite3.c amalgamated source code files into
# several smaller files such that no single files is more than a fixed
# number of lines in length (32k or 64k).  Each of the split out files
# is #include-ed by the master file.
#
# Splitting files up this way allows them to be used with older compilers
# that cannot handle really long source files.
#
set MAX 32768    ;# Maximum number of lines per file.

set BEGIN {^/\*+ Begin file ([a-zA-Z0-9_.]+) \*+/}
set END   {^/\*+ End of %s \*+/}

set in [open sqlite3.c]
set out1 [open sqlite3-all.c w]

# Copy the header from sqlite3.c into sqlite3-all.c
#
while {[gets $in line]} {
  if {[regexp $BEGIN $line]} break
  puts $out1 $line
}

# Gather the complete content of a file into memory.  Store the
# content in $bufout.  Store the number of lines is $nout
#
proc gather_one_file {firstline bufout nout} {
  regexp $::BEGIN $firstline all filename
  set end [format $::END $filename]
  upvar $bufout buf $nout n
  set buf $firstline\n
  global in
  set n 0
  while {[gets $in line]>=0} {
    incr n
    append buf $line\n
    if {[regexp $end $line]} break
  }
}

# Write a big chunk of text in to an auxiliary file "sqlite3-NNN.c".
# Also add an appropriate #include to sqlite3-all.c
#
set filecnt 0
proc write_one_file {content} {
  global filecnt
  incr filecnt
  set out [open sqlite3-$filecnt.c w]
  puts -nonewline $out $content
  close $out
  puts $::out1 "#include \"sqlite3-$filecnt.c\""
}

# Continue reading input.  Store chunks in separate files and add
# the #includes to the main sqlite3-all.c file as necessary to reference
# the extra chunks.
#
set all {}
set N 0
while {[regexp $BEGIN $line]} {
  set buf {}
  set n 0
  gather_one_file $line buf n
  if {$n+$N>=$MAX} {
    write_one_file $all
    set all {}
    set N 0
  }
  append all $buf
  incr N $n
  while {[gets $in line]>=0} {
    if {[regexp $BEGIN $line]} break
    puts $out1 $line
  }
}
if {$N>0} {
  write_one_file $all
}
close $out1
close $in