SQLite

Check-in [6183f1bd86]
Login

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

Overview
Comment:Add the sqlite3_io_methods.xMremap() method to the VFS interface. Also "PRAGMA mmap_size".
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | experimental-mmap
Files: files | file ages | folders
SHA1: 6183f1bd86ceed76d22d9762f3d7eb33262c62d1
User & Date: dan 2013-03-19 19:28:06.473
Context
2013-03-20
10:07
Add test file mmap1.test. (check-in: aee1f53a74 user: dan tags: experimental-mmap)
2013-03-19
19:28
Add the sqlite3_io_methods.xMremap() method to the VFS interface. Also "PRAGMA mmap_size". (check-in: 6183f1bd86 user: dan tags: experimental-mmap)
2013-03-16
20:19
When possible, use memcpy() to and from the mapped region instead of xWrite() and xRead(). (check-in: f8ca5622d9 user: dan tags: experimental-mmap)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/btree.c.
2125
2126
2127
2128
2129
2130
2131













2132
2133
2134
2135
2136
2137
2138
  BtShared *pBt = p->pBt;
  assert( sqlite3_mutex_held(p->db->mutex) );
  sqlite3BtreeEnter(p);
  sqlite3PagerSetCachesize(pBt->pPager, mxPage);
  sqlite3BtreeLeave(p);
  return SQLITE_OK;
}














/*
** Change the way data is synced to disk in order to increase or decrease
** how well the database resists damage due to OS crashes and power
** failures.  Level 1 is the same as asynchronous (no syncs() occur and
** there is a high probability of damage)  Level 2 is the default.  There
** is a very low but non-zero probability of damage.  Level 3 reduces the







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







2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
  BtShared *pBt = p->pBt;
  assert( sqlite3_mutex_held(p->db->mutex) );
  sqlite3BtreeEnter(p);
  sqlite3PagerSetCachesize(pBt->pPager, mxPage);
  sqlite3BtreeLeave(p);
  return SQLITE_OK;
}

/*
** Change the limit on the amount of the database file that may be
** memory mapped.
*/
int sqlite3BtreeSetMmapSize(Btree *p, int nMap){
  BtShared *pBt = p->pBt;
  assert( sqlite3_mutex_held(p->db->mutex) );
  sqlite3BtreeEnter(p);
  sqlite3PagerSetMmapsize(pBt->pPager, nMap);
  sqlite3BtreeLeave(p);
  return SQLITE_OK;
}

/*
** Change the way data is synced to disk in order to increase or decrease
** how well the database resists damage due to OS crashes and power
** failures.  Level 1 is the same as asynchronous (no syncs() occur and
** there is a high probability of damage)  Level 2 is the default.  There
** is a very low but non-zero probability of damage.  Level 3 reduces the
Changes to src/btree.h.
59
60
61
62
63
64
65

66
67
68
69
70
71
72
#define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
#define BTREE_MEMORY        2  /* This is an in-memory DB */
#define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
#define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */

int sqlite3BtreeClose(Btree*);
int sqlite3BtreeSetCacheSize(Btree*,int);

int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
int sqlite3BtreeSyncDisabled(Btree*);
int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
int sqlite3BtreeGetPageSize(Btree*);
int sqlite3BtreeMaxPageCount(Btree*,int);
u32 sqlite3BtreeLastPage(Btree*);
int sqlite3BtreeSecureDelete(Btree*,int);







>







59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#define BTREE_OMIT_JOURNAL  1  /* Do not create or use a rollback journal */
#define BTREE_MEMORY        2  /* This is an in-memory DB */
#define BTREE_SINGLE        4  /* The file contains at most 1 b-tree */
#define BTREE_UNORDERED     8  /* Use of a hash implementation is OK */

int sqlite3BtreeClose(Btree*);
int sqlite3BtreeSetCacheSize(Btree*,int);
int sqlite3BtreeSetMmapSize(Btree*, int);
int sqlite3BtreeSetSafetyLevel(Btree*,int,int,int);
int sqlite3BtreeSyncDisabled(Btree*);
int sqlite3BtreeSetPageSize(Btree *p, int nPagesize, int nReserve, int eFix);
int sqlite3BtreeGetPageSize(Btree*);
int sqlite3BtreeMaxPageCount(Btree*,int);
u32 sqlite3BtreeLastPage(Btree*);
int sqlite3BtreeSecureDelete(Btree*,int);
Changes to src/os.c.
135
136
137
138
139
140
141



142
143
144
145
146
147
148
  int iPage,
  int pgsz,
  int bExtend,                    /* True to extend file if necessary */
  void volatile **pp              /* OUT: Pointer to mapping */
){
  DO_OS_MALLOC_TEST(id);
  return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);



}

/*
** The next group of routines are convenience wrappers around the
** VFS methods.
*/
int sqlite3OsOpen(







>
>
>







135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
  int iPage,
  int pgsz,
  int bExtend,                    /* True to extend file if necessary */
  void volatile **pp              /* OUT: Pointer to mapping */
){
  DO_OS_MALLOC_TEST(id);
  return id->pMethods->xShmMap(id, iPage, pgsz, bExtend, pp);
}
int sqlite3OsMremap(sqlite3_file *id, i64 iOff, i64 nOld, i64 nNew, void **pp){
  return id->pMethods->xMremap(id, iOff, nOld, nNew, pp);
}

/*
** The next group of routines are convenience wrappers around the
** VFS methods.
*/
int sqlite3OsOpen(
Changes to src/os.h.
255
256
257
258
259
260
261

262
263
264
265
266
267
268
#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
int sqlite3OsSectorSize(sqlite3_file *id);
int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
void sqlite3OsShmBarrier(sqlite3_file *id);
int sqlite3OsShmUnmap(sqlite3_file *id, int);



/* 
** Functions for accessing sqlite3_vfs methods 
*/
int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
int sqlite3OsDelete(sqlite3_vfs *, const char *, int);







>







255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#define SQLITE_FCNTL_DB_UNCHANGED 0xca093fa0
int sqlite3OsSectorSize(sqlite3_file *id);
int sqlite3OsDeviceCharacteristics(sqlite3_file *id);
int sqlite3OsShmMap(sqlite3_file *,int,int,int,void volatile **);
int sqlite3OsShmLock(sqlite3_file *id, int, int, int);
void sqlite3OsShmBarrier(sqlite3_file *id);
int sqlite3OsShmUnmap(sqlite3_file *id, int);
int sqlite3OsMremap(sqlite3_file *id, i64, i64, i64, void **);


/* 
** Functions for accessing sqlite3_vfs methods 
*/
int sqlite3OsOpen(sqlite3_vfs *, const char *, sqlite3_file*, int, int *);
int sqlite3OsDelete(sqlite3_vfs *, const char *, int);
Changes to src/os_unix.c.
4424
4425
4426
4427
4428
4429
4430




































4431
4432
4433
4434
4435
4436
4437

#else
# define unixShmMap     0
# define unixShmLock    0
# define unixShmBarrier 0
# define unixShmUnmap   0
#endif /* #ifndef SQLITE_OMIT_WAL */





































/*
** Here ends the implementation of all sqlite3_file methods.
**
********************** End sqlite3_file Methods *******************************
******************************************************************************/








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







4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473

#else
# define unixShmMap     0
# define unixShmLock    0
# define unixShmBarrier 0
# define unixShmUnmap   0
#endif /* #ifndef SQLITE_OMIT_WAL */

/*
** Map, remap or unmap part of the database file.
*/
static int unixMremap(
  sqlite3_file *fd,               /* Main database file */
  sqlite3_int64 iOff,             /* Offset to start mapping at */
  sqlite3_int64 nOld,             /* Size of old mapping, or zero */
  sqlite3_int64 nNew,             /* Size of new mapping, or zero */
  void **ppMap                    /* IN/OUT: Old/new mappings */
){
  unixFile *p = (unixFile *)fd;   /* The underlying database file */
  int rc = SQLITE_OK;             /* Return code */
  void *pNew = 0;                 /* New mapping */

  assert( iOff==0 );

  if( nOld!=0 ){
    void *pOld = *ppMap;
    munmap(pOld, nOld);
  }

  if( nNew>0 ){
    int flags = PROT_READ;
    if( (p->ctrlFlags & UNIXFILE_RDONLY)==0 ) flags |= PROT_WRITE;
    nNew = (nNew+4095) & ~(i64)((1 << 12)-1);
    pNew = mmap(0, nNew, flags, MAP_SHARED, p->h, iOff);
    if( pNew==MAP_FAILED ){
      pNew = 0;
      rc = SQLITE_IOERR;
    }
  }

  *ppMap = pNew;
  return rc;
}

/*
** Here ends the implementation of all sqlite3_file methods.
**
********************** End sqlite3_file Methods *******************************
******************************************************************************/

4483
4484
4485
4486
4487
4488
4489
4490

4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
   CKLOCK,                     /* xCheckReservedLock */                      \
   unixFileControl,            /* xFileControl */                            \
   unixSectorSize,             /* xSectorSize */                             \
   unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
   unixShmMap,                 /* xShmMap */                                 \
   unixShmLock,                /* xShmLock */                                \
   unixShmBarrier,             /* xShmBarrier */                             \
   unixShmUnmap                /* xShmUnmap */                               \

};                                                                           \
static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
  UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
  return &METHOD;                                                            \
}                                                                            \
static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
    = FINDER##Impl;

/*
** Here are all of the sqlite3_io_methods objects for each of the
** locking strategies.  Functions that return pointers to these methods
** are also created.
*/
IOMETHODS(
  posixIoFinder,            /* Finder function name */
  posixIoMethods,           /* sqlite3_io_methods object name */
  2,                        /* shared memory is enabled */
  unixClose,                /* xClose method */
  unixLock,                 /* xLock method */
  unixUnlock,               /* xUnlock method */
  unixCheckReservedLock     /* xCheckReservedLock method */
)
IOMETHODS(
  nolockIoFinder,           /* Finder function name */







|
>
















|







4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
   CKLOCK,                     /* xCheckReservedLock */                      \
   unixFileControl,            /* xFileControl */                            \
   unixSectorSize,             /* xSectorSize */                             \
   unixDeviceCharacteristics,  /* xDeviceCapabilities */                     \
   unixShmMap,                 /* xShmMap */                                 \
   unixShmLock,                /* xShmLock */                                \
   unixShmBarrier,             /* xShmBarrier */                             \
   unixShmUnmap,               /* xShmUnmap */                               \
   unixMremap,                 /* xMremap */                                 \
};                                                                           \
static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){   \
  UNUSED_PARAMETER(z); UNUSED_PARAMETER(p);                                  \
  return &METHOD;                                                            \
}                                                                            \
static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p)    \
    = FINDER##Impl;

/*
** Here are all of the sqlite3_io_methods objects for each of the
** locking strategies.  Functions that return pointers to these methods
** are also created.
*/
IOMETHODS(
  posixIoFinder,            /* Finder function name */
  posixIoMethods,           /* sqlite3_io_methods object name */
  3,                        /* shared memory and mmap are enabled */
  unixClose,                /* xClose method */
  unixLock,                 /* xLock method */
  unixUnlock,               /* xUnlock method */
  unixCheckReservedLock     /* xCheckReservedLock method */
)
IOMETHODS(
  nolockIoFinder,           /* Finder function name */
Changes to src/pager.c.
655
656
657
658
659
660
661


662
663

664
665
666
667
668
669
670
  PagerSavepoint *aSavepoint; /* Array of active savepoints */
  int nSavepoint;             /* Number of elements in aSavepoint[] */
  char dbFileVers[16];        /* Changes whenever database file changes */

  void *pMap;                 /* Memory mapped prefix of database file */
  i64 nMap;                   /* Size of mapping at pMap in bytes */ 
  i64 nMapValid;              /* Bytes at pMap known to be valid */


  int nMmapOut;               /* Number of mmap pages currently outstanding */
  PgHdr *pFree;               /* List of free mmap page headers (pDirty) */

  /*
  ** End of the routinely-changing class members
  ***************************************************************************/

  u16 nExtra;                 /* Add this many bytes to each in-memory page */
  i16 nReserve;               /* Number of unused bytes at end of each page */
  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */







>
>


>







655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
  PagerSavepoint *aSavepoint; /* Array of active savepoints */
  int nSavepoint;             /* Number of elements in aSavepoint[] */
  char dbFileVers[16];        /* Changes whenever database file changes */

  void *pMap;                 /* Memory mapped prefix of database file */
  i64 nMap;                   /* Size of mapping at pMap in bytes */ 
  i64 nMapValid;              /* Bytes at pMap known to be valid */
  i64 nMapLimit;              /* Maximum permitted mapping size */
  int nMapCfgLimit;           /* Configured limit value */
  int nMmapOut;               /* Number of mmap pages currently outstanding */
  PgHdr *pFree;               /* List of free mmap page headers (pDirty) */
  int bMapResize;             /* Check if the mapping should be resized */
  /*
  ** End of the routinely-changing class members
  ***************************************************************************/

  u16 nExtra;                 /* Add this many bytes to each in-memory page */
  i16 nReserve;               /* Number of unused bytes at end of each page */
  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
3351
3352
3353
3354
3355
3356
3357





























3358
3359
3360
3361
3362
3363
3364

/*
** Change the maximum number of in-memory pages that are allowed.
*/
void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
  sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
}






























/*
** Free as much memory as possible from the pager.
*/
void sqlite3PagerShrink(Pager *pPager){
  sqlite3PcacheShrink(pPager->pPCache);
}







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







3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396

/*
** Change the maximum number of in-memory pages that are allowed.
*/
void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
  sqlite3PcacheSetCachesize(pPager->pPCache, mxPage);
}

/*
** Set Pager.nMapLimit, the maximum permitted mapping size, based on the
** current values of Pager.nMapCfgLimit and Pager.pageSize.
**
** If this connection should not use mmap at all, set nMapLimit to zero.
*/
static void pagerFixMaplimit(Pager *pPager){
  if( isOpen(pPager->fd)==0 
   || pPager->fd->pMethods->iVersion<3 
   || pPager->fd->pMethods->xMremap==0 
   || pPager->tempFile 
   || pPager->pWal 
  ){
    pPager->nMapLimit = 0;
  }else if( pPager->nMapCfgLimit<0 ){
    pPager->nMapLimit = (i64)pPager->nMapCfgLimit * -1024;
  }else{
    pPager->nMapLimit = (i64)pPager->nMapCfgLimit * pPager->pageSize;
  }
}

/*
** Change the maximum size of any memory mapping made of the database file.
*/
void sqlite3PagerSetMmapsize(Pager *pPager, int nMap){
  pPager->nMapCfgLimit = nMap;
  pagerFixMaplimit(pPager);
}

/*
** Free as much memory as possible from the pager.
*/
void sqlite3PagerShrink(Pager *pPager){
  sqlite3PcacheShrink(pPager->pPCache);
}
3587
3588
3589
3590
3591
3592
3593

3594
3595
3596
3597
3598
3599
3600

  *pPageSize = pPager->pageSize;
  if( rc==SQLITE_OK ){
    if( nReserve<0 ) nReserve = pPager->nReserve;
    assert( nReserve>=0 && nReserve<1000 );
    pPager->nReserve = (i16)nReserve;
    pagerReportSize(pPager);

  }
  return rc;
}

/*
** Return a pointer to the "temporary page" buffer held internally
** by the pager.  This is a buffer that is big enough to hold the







>







3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633

  *pPageSize = pPager->pageSize;
  if( rc==SQLITE_OK ){
    if( nReserve<0 ) nReserve = pPager->nReserve;
    assert( nReserve>=0 && nReserve<1000 );
    pPager->nReserve = (i16)nReserve;
    pagerReportSize(pPager);
    pagerFixMaplimit(pPager);
  }
  return rc;
}

/*
** Return a pointer to the "temporary page" buffer held internally
** by the pager.  This is a buffer that is big enough to hold the
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833



3834
3835

3836
3837

3838



3839


3840
3841

3842
3843
3844
3845
3846
3847

3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871



3872
3873
3874




3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897

3898

3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920




3921
3922
3923
3924
3925
3926
3927



3928
3929
3930
3931
3932
3933
3934
  }
  if( rc==SQLITE_OK ){
    rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
  }
  return rc;
}

#include <sys/mman.h>

/*
** Unmap any mapping of the database file.
*/
static int pagerUnmap(Pager *pPager){
  if( pPager->pMap ){
    munmap(pPager->pMap, pPager->nMap);
    pPager->pMap = 0;
    pPager->nMap = 0;
    pPager->nMapValid = 0;
  }
  return SQLITE_OK;
}




static int pagerMap(Pager *pPager){
  int rc;

  i64 sz = 0;


  assert( pPager->pMap==0 && pPager->nMap==0 );






  rc = sqlite3OsFileSize(pPager->fd, &sz);
  sz = sz & ~(4096-1);


  if( rc==SQLITE_OK && sz>0 ){
    int fd;
    rc = sqlite3OsFileControl(pPager->fd, SQLITE_FCNTL_GETFD, (void *)&fd);
    if( rc==SQLITE_OK ){
      void *pMap = mmap(0, sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);

      if( pMap==MAP_FAILED ){
        return SQLITE_IOERR;
      }
      pPager->pMap = pMap;
      pPager->nMapValid = pPager->nMap = sz;
    }
  }

  return rc;
}

static int pagerRemap(Pager *pPager, Pgno nPage){
  i64 sz = (i64)nPage * pPager->pageSize;
  sz = sz & ~(4096-1);

  if( pPager->nMap!=sz ){
    void *pMap = mremap(pPager->pMap, pPager->nMap, sz, MREMAP_MAYMOVE);    
    if( pMap==MAP_FAILED ){
      return SQLITE_IOERR;
    }
    pPager->pMap = pMap;
    pPager->nMapValid = pPager->nMap = sz;
  }




  return SQLITE_OK;
}





static int pagerAcquireMapPage(Pager *pPager, Pgno pgno, PgHdr **ppPage){
  int rc;
  *ppPage = 0;

  assert( pPager->pWal==0 );

  if( MEMDB==0 && pPager->tempFile==0 ){
    if( pPager->pMap==0 ){
      rc = pagerMap(pPager);
      if( rc!=SQLITE_OK ) return rc;
    }

    if( pgno!=1 && pPager->pMap 
     && pPager->nMapValid>=((i64)pgno*pPager->pageSize) 
    ){
      PgHdr *p;
      if( pPager->pFree ){
        p = pPager->pFree;
        pPager->pFree = p->pDirty;
        p->pDirty = 0;
        memset(p->pExtra, 0, pPager->nExtra);
      }else{
        p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);

        if( p==0 ) return SQLITE_NOMEM;

        p->pExtra = (void *)&p[1];
        p->flags = PGHDR_MMAP;
        p->nRef = 1;
        p->pPager = pPager;
      }

      assert( p->pExtra==(void *)&p[1] );
      assert( p->pPage==0 );
      assert( p->flags==PGHDR_MMAP );
      assert( p->pPager==pPager );
      assert( p->nRef==1 );

      p->pData = &((u8 *)pPager->pMap)[(i64)(pgno-1) * pPager->pageSize];
      p->pgno = pgno;
      pPager->nMmapOut++;
      *ppPage = p;
    }
  }

  return SQLITE_OK;
}





static void pagerReleaseMapPage(PgHdr *pPg){
  Pager *pPager = pPg->pPager;
  pPager->nMmapOut--;
  pPg->pDirty = pPager->pFree;
  pPager->pFree = pPg;
}




static void pagerFreeMapHdrs(Pager *pPager){
  PgHdr *p;
  PgHdr *pNext;
  for(p=pPager->pFree; p; p=pNext){
    pNext = p->pDirty;
    sqlite3_free(p);
  }







<
<

|



|
<






>
>
>

|
>
|

>
|
>
>
>

>
>
|
|
>

|
<
|

<
>
|
<
|
|
|

<
|
<
|
|
<
<
<

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

<
<
|
<

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

|
|
|
|
|

|
|
|
<
|
<
<



>
>
>
>







>
>
>







3845
3846
3847
3848
3849
3850
3851


3852
3853
3854
3855
3856
3857

3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884

3885
3886

3887
3888

3889
3890
3891
3892

3893

3894
3895



3896



3897
3898


3899
3900
3901
3902
3903
3904

3905
3906
3907
3908
3909
3910


3911

3912










3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937

3938


3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
  }
  if( rc==SQLITE_OK ){
    rc = sqlite3OsFileSize(pPager->jfd, &pPager->journalHdr);
  }
  return rc;
}



/*
** Unmap any memory mapping of the database file.
*/
static int pagerUnmap(Pager *pPager){
  if( pPager->pMap ){
    sqlite3OsMremap(pPager->fd, 0, pPager->nMap, 0, &pPager->pMap);

    pPager->nMap = 0;
    pPager->nMapValid = 0;
  }
  return SQLITE_OK;
}

/*
** Create, or recreate, the memory mapping of the database file.
*/
static int pagerMap(Pager *pPager){
  int rc = SQLITE_OK;             /* Return code */
  Pgno nPg;                       /* Size of mapping to request in pages */
  i64 sz;                         /* Size of mapping to request in bytes */

  assert( pPager->pWal==0 && isOpen(pPager->fd) && pPager->tempFile==0 );
  assert( pPager->pMap==0 || pPager->nMap>0 );
  assert( pPager->eState>=1 );
  assert( pPager->nMmapOut==0 );
  assert( pPager->nMapLimit>0 );

  /* Figure out how large a mapping to request. Set variable sz to this 
  ** value in bytes. */
  nPg = (pPager->eState==1) ? pPager->dbSize : pPager->dbFileSize;
  sz = (i64)nPg * pPager->pageSize;
  if( sz>pPager->nMapLimit ) sz = pPager->nMapLimit;

  if( sz!=pPager->nMapValid ){

    rc = sqlite3OsMremap(pPager->fd, 0, pPager->nMap, sz, &pPager->pMap);
    if( rc==SQLITE_OK ){

      assert( pPager->pMap!=0 );
      pPager->nMap = sz;

    }else{
      assert( pPager->pMap==0 );
      pPager->nMap = 0;
    }

    pPager->nMapValid = pPager->nMap;

  }
  pPager->bMapResize = 0;







  return rc;
}



/*
** Obtain a reference to a memory mapped page object for page number pgno. 
** The caller must ensure that page pgno lies within the currently mapped 
** region. If successful, set *ppPage to point to the new page reference
** and return SQLITE_OK. Otherwise, return an SQLite error code and set

** *ppPage to zero.
**
** Page references obtained by calling this function should be released
** by calling pagerReleaseMapPage().
*/
static int pagerAcquireMapPage(Pager *pPager, Pgno pgno, PgHdr **ppPage){


  PgHdr *p;                       /* Memory mapped page to return */












  if( pPager->pFree ){
    *ppPage = p = pPager->pFree;
    pPager->pFree = p->pDirty;
    p->pDirty = 0;
    memset(p->pExtra, 0, pPager->nExtra);
  }else{
    *ppPage = p = (PgHdr *)sqlite3MallocZero(sizeof(PgHdr) + pPager->nExtra);
    if( p==0 ){
      return SQLITE_NOMEM;
    }
    p->pExtra = (void *)&p[1];
    p->flags = PGHDR_MMAP;
    p->nRef = 1;
    p->pPager = pPager;
  }

  assert( p->pExtra==(void *)&p[1] );
  assert( p->pPage==0 );
  assert( p->flags==PGHDR_MMAP );
  assert( p->pPager==pPager );
  assert( p->nRef==1 );

  p->pData = &((u8 *)pPager->pMap)[(i64)(pgno-1) * pPager->pageSize];
  p->pgno = pgno;
  pPager->nMmapOut++;




  return SQLITE_OK;
}

/*
** Release a reference to page pPg. pPg must have been returned by an 
** earlier call to pagerAcquireMapPage().
*/
static void pagerReleaseMapPage(PgHdr *pPg){
  Pager *pPager = pPg->pPager;
  pPager->nMmapOut--;
  pPg->pDirty = pPager->pFree;
  pPager->pFree = pPg;
}

/*
** Free all PgHdr objects stored in the Pager.pFree list.
*/
static void pagerFreeMapHdrs(Pager *pPager){
  PgHdr *p;
  PgHdr *pNext;
  for(p=pPager->pFree; p; p=pNext){
    pNext = p->pDirty;
    sqlite3_free(p);
  }
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
        ** may have truncated the database file and then extended it back
        ** to its original size while this process was not holding a lock.
        ** In this case there may exist a Pager.pMap mapping that appears
        ** to be the right size but is not actually valid. Avoid this
        ** possibility by unmapping the db here. */
        pagerUnmap(pPager);
      }else if( pPager->pMap ){
        pagerRemap(pPager, nPage);
      }
    }

    /* If there is a WAL file in the file-system, open this database in WAL
    ** mode. Otherwise, the following function call is a no-op.
    */
    rc = pagerOpenWalIfPresent(pPager);







|







5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
        ** may have truncated the database file and then extended it back
        ** to its original size while this process was not holding a lock.
        ** In this case there may exist a Pager.pMap mapping that appears
        ** to be the right size but is not actually valid. Avoid this
        ** possibility by unmapping the db here. */
        pagerUnmap(pPager);
      }else if( pPager->pMap ){
        pPager->bMapResize = 1;
      }
    }

    /* If there is a WAL file in the file-system, open this database in WAL
    ** mode. Otherwise, the following function call is a no-op.
    */
    rc = pagerOpenWalIfPresent(pPager);
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
  PgHdr *pPg = 0;
  const int noContent = (flags & PAGER_ACQUIRE_NOCONTENT);

  /* It is acceptable to use a read-only (mmap) page for any page except
  ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
  ** flag was specified by the caller. And so long as the db is not a 
  ** temporary or in-memory database.  */
  const int bMmapOk = (
      (pgno!=1 && pPager->pWal==0 && !pPager->tempFile && !MEMDB)
   && (pPager->eState==PAGER_READER || (flags & PAGER_ACQUIRE_READONLY))
  );

  assert( pPager->eState>=PAGER_READER );
  assert( assert_pager_state(pPager) );

  if( pgno==0 ){
    return SQLITE_CORRUPT_BKPT;
  }

  /* If the pager is in the error state, return an error immediately. 
  ** Otherwise, request the page from the PCache layer. */
  if( pPager->errCode!=SQLITE_OK ){
    rc = pPager->errCode;
  }else{

    if( bMmapOk ){
      if( pPager->pMap==0 ){
        rc = pagerMap(pPager);
      }
      if( rc==SQLITE_OK && pPager->nMap>=((i64)pgno * pPager->pageSize) ){
        if( pPager->eState>PAGER_READER ){
          (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
        }
        if( pPg==0 ){







|
<

















|







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
5275
5276
5277
5278
5279
5280
5281
5282
  PgHdr *pPg = 0;
  const int noContent = (flags & PAGER_ACQUIRE_NOCONTENT);

  /* It is acceptable to use a read-only (mmap) page for any page except
  ** page 1 if there is no write-transaction open or the ACQUIRE_READONLY
  ** flag was specified by the caller. And so long as the db is not a 
  ** temporary or in-memory database.  */
  const int bMmapOk = (pPager->nMapLimit>0 && pgno!=1

   && (pPager->eState==PAGER_READER || (flags & PAGER_ACQUIRE_READONLY))
  );

  assert( pPager->eState>=PAGER_READER );
  assert( assert_pager_state(pPager) );

  if( pgno==0 ){
    return SQLITE_CORRUPT_BKPT;
  }

  /* If the pager is in the error state, return an error immediately. 
  ** Otherwise, request the page from the PCache layer. */
  if( pPager->errCode!=SQLITE_OK ){
    rc = pPager->errCode;
  }else{

    if( bMmapOk ){
      if( pPager->pMap==0 || (pPager->bMapResize && pPager->nMmapOut==0) ){
        rc = pagerMap(pPager);
      }
      if( rc==SQLITE_OK && pPager->nMap>=((i64)pgno * pPager->pageSize) ){
        if( pPager->eState>PAGER_READER ){
          (void)sqlite3PcacheFetch(pPager->pPCache, pgno, 0, &pPg);
        }
        if( pPg==0 ){
7010
7011
7012
7013
7014
7015
7016

7017
7018
7019
7020
7021
7022
7023
  */
  if( rc==SQLITE_OK ){
    rc = sqlite3WalOpen(pPager->pVfs, 
        pPager->fd, pPager->zWal, pPager->exclusiveMode,
        pPager->journalSizeLimit, &pPager->pWal
    );
  }


  return rc;
}


/*
** The caller must be holding a SHARED lock on the database file to call







>







7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
  */
  if( rc==SQLITE_OK ){
    rc = sqlite3WalOpen(pPager->pVfs, 
        pPager->fd, pPager->zWal, pPager->exclusiveMode,
        pPager->journalSizeLimit, &pPager->pWal
    );
  }
  pagerFixMaplimit(pPager);

  return rc;
}


/*
** The caller must be holding a SHARED lock on the database file to call
7100
7101
7102
7103
7104
7105
7106

7107
7108
7109
7110
7111
7112
7113
  */
  if( rc==SQLITE_OK && pPager->pWal ){
    rc = pagerExclusiveLock(pPager);
    if( rc==SQLITE_OK ){
      rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
                           pPager->pageSize, (u8*)pPager->pTmpSpace);
      pPager->pWal = 0;

    }
  }
  return rc;
}

#endif /* !SQLITE_OMIT_WAL */








>







7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
  */
  if( rc==SQLITE_OK && pPager->pWal ){
    rc = pagerExclusiveLock(pPager);
    if( rc==SQLITE_OK ){
      rc = sqlite3WalClose(pPager->pWal, pPager->ckptSyncFlags,
                           pPager->pageSize, (u8*)pPager->pTmpSpace);
      pPager->pWal = 0;
      pagerFixMaplimit(pPager);
    }
  }
  return rc;
}

#endif /* !SQLITE_OMIT_WAL */

Changes to src/pragma.c.
740
741
742
743
744
745
746
























747
748
749
750
751
752
753
    }else{
      int size = sqlite3Atoi(zRight);
      pDb->pSchema->cache_size = size;
      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
    }
  }else

























  /*
  **   PRAGMA temp_store
  **   PRAGMA temp_store = "default"|"memory"|"file"
  **
  ** Return or set the local value of the temp_store flag.  Changing
  ** the local value does not make changes to the disk file and the default
  ** value will be restored the next time the database is opened.







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







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
    }else{
      int size = sqlite3Atoi(zRight);
      pDb->pSchema->cache_size = size;
      sqlite3BtreeSetCacheSize(pDb->pBt, pDb->pSchema->cache_size);
    }
  }else

  /*
  **  PRAGMA [database.]mmap_size
  **  PRAGMA [database.]mmap_size=N
  **
  ** Used to set or query the mapping size limit. The mapping size limit is
  ** used to limit the aggregate size of all memory mapped regions of the
  ** database file. If this parameter is set to zero, then memory mapping
  ** is not used at all. If it is set to a positive value, then it is
  ** interpreted as a maximum size in pages. If set to less than zero, then
  ** the absolute value is interpreted as a size limit in KB.
  **
  ** The default value is zero (do not use memory mapped IO).
  */
  if( sqlite3StrICmp(zLeft,"mmap_size")==0 ){
    assert( sqlite3SchemaMutexHeld(db, iDb, 0) );
    if( !zRight ){
      returnSingleInt(pParse, "mmap_size", pDb->pSchema->mmap_size);
    }else{
      int size = sqlite3Atoi(zRight);
      pDb->pSchema->mmap_size = size;
      sqlite3BtreeSetMmapSize(pDb->pBt, pDb->pSchema->mmap_size);
    }
  }else

  /*
  **   PRAGMA temp_store
  **   PRAGMA temp_store = "default"|"memory"|"file"
  **
  ** Return or set the local value of the temp_store flag.  Changing
  ** the local value does not make changes to the disk file and the default
  ** value will be restored the next time the database is opened.
Changes to src/sqlite.h.in.
702
703
704
705
706
707
708


















709
710
711
712
713
714
715
** to xWrite().
**
** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
** in the unread portions of the buffer with zeros.  A VFS that
** fails to zero-fill short reads might seem to work.  However,
** failure to zero-fill short reads will eventually lead to
** database corruption.


















*/
typedef struct sqlite3_io_methods sqlite3_io_methods;
struct sqlite3_io_methods {
  int iVersion;
  int (*xClose)(sqlite3_file*);
  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);







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







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
** to xWrite().
**
** If xRead() returns SQLITE_IOERR_SHORT_READ it must also fill
** in the unread portions of the buffer with zeros.  A VFS that
** fails to zero-fill short reads might seem to work.  However,
** failure to zero-fill short reads will eventually lead to
** database corruption.
**
** Assuming parameter nNew is non-zero, the xMremap method should attempt
** to memory map a region nNew bytes in size starting at offset iOffset
** of the file.  If successful, it should set *ppMap to point to the
** mapping and return SQLITE_OK. If the file is opened for read-write
** access, then the mapping should also be read-write.
**
** If nOld is non-zero, then the initial value of *ppMap points to a
** mapping returned by a previous call to xMremap. The existing mapping
** is nOld bytes in size and starts at offset iOffset of the file. In
** this case the xMremap method is expected to unmap the existing mapping
** and overwrite *ppMap with the pointer to the new mapping. If nOld is
** zero, then the initial value of *ppMap is undefined.
**
** If nNew is zero, then no new mapping should be created. Any old
** mapping must still be unmapped if nOld is non-zero. If the nOld
** parameter is non-zero, then the existing mapping is always unmapped -
** even if an error occurs.
*/
typedef struct sqlite3_io_methods sqlite3_io_methods;
struct sqlite3_io_methods {
  int iVersion;
  int (*xClose)(sqlite3_file*);
  int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst);
  int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst);
724
725
726
727
728
729
730



731
732
733
734
735
736
737
  int (*xDeviceCharacteristics)(sqlite3_file*);
  /* Methods above are valid for version 1 */
  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
  void (*xShmBarrier)(sqlite3_file*);
  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
  /* Methods above are valid for version 2 */



  /* Additional methods may be added in future releases */
};

/*
** CAPI3REF: Standard File Control Opcodes
**
** These integer constants are opcodes for the xFileControl method







>
>
>







742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
  int (*xDeviceCharacteristics)(sqlite3_file*);
  /* Methods above are valid for version 1 */
  int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**);
  int (*xShmLock)(sqlite3_file*, int offset, int n, int flags);
  void (*xShmBarrier)(sqlite3_file*);
  int (*xShmUnmap)(sqlite3_file*, int deleteFlag);
  /* Methods above are valid for version 2 */
  int (*xMremap)(sqlite3_file *fd, 
      sqlite3_int64 iOff, sqlite3_int64 nOld, sqlite3_int64 nNew, void **ppMap);
  /* Methods above are valid for version 3 */
  /* Additional methods may be added in future releases */
};

/*
** CAPI3REF: Standard File Control Opcodes
**
** These integer constants are opcodes for the xFileControl method
Changes to src/sqliteInt.h.
742
743
744
745
746
747
748

749
750
751
752
753
754
755
  Hash trigHash;       /* All triggers indexed by name */
  Hash fkeyHash;       /* All foreign keys by referenced table name */
  Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
  u8 file_format;      /* Schema format version for this file */
  u8 enc;              /* Text encoding used by this database */
  u16 flags;           /* Flags associated with this schema */
  int cache_size;      /* Number of pages to use in the cache */

};

/*
** These macros can be used to test, set, or clear bits in the 
** Db.pSchema->flags field.
*/
#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))







>







742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
  Hash trigHash;       /* All triggers indexed by name */
  Hash fkeyHash;       /* All foreign keys by referenced table name */
  Table *pSeqTab;      /* The sqlite_sequence table used by AUTOINCREMENT */
  u8 file_format;      /* Schema format version for this file */
  u8 enc;              /* Text encoding used by this database */
  u16 flags;           /* Flags associated with this schema */
  int cache_size;      /* Number of pages to use in the cache */
  int mmap_size;       /* Number of pages to memory map */
};

/*
** These macros can be used to test, set, or clear bits in the 
** Db.pSchema->flags field.
*/
#define DbHasProperty(D,I,P)     (((D)->aDb[I].pSchema->flags&(P))==(P))
Changes to src/test1.c.
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
){
  char buf[1024];
  struct rusage r;
  memset(&r, 0, sizeof(r));
  getrusage(RUSAGE_SELF, &r);

  sprintf(buf, "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d", 
    r.ru_utime.tv_sec, r.ru_utime.tv_usec, 
    r.ru_stime.tv_sec, r.ru_stime.tv_usec, 
    r.ru_minflt, r.ru_majflt
  );
  Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
  return TCL_OK;
}

#if SQLITE_OS_WIN
/*







|
|
|







5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
){
  char buf[1024];
  struct rusage r;
  memset(&r, 0, sizeof(r));
  getrusage(RUSAGE_SELF, &r);

  sprintf(buf, "ru_utime=%d.%06d ru_stime=%d.%06d ru_minflt=%d ru_majflt=%d", 
    (int)r.ru_utime.tv_sec, (int)r.ru_utime.tv_usec, 
    (int)r.ru_stime.tv_sec, (int)r.ru_stime.tv_usec, 
    (int)r.ru_minflt, (int)r.ru_majflt
  );
  Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
  return TCL_OK;
}

#if SQLITE_OS_WIN
/*
Changes to test/dbstatus2.test.
36
37
38
39
40
41
42

43
44
45
46
47
48
49
proc db_write {db {reset 0}} {
  sqlite3_db_status $db CACHE_WRITE $reset
}

do_test 1.1 {
  db close
  sqlite3 db test.db

  expr {[file size test.db] / 1024}
} 6

do_test 1.2 {
  execsql { SELECT b FROM t1 WHERE a=2 }
  db_hit_miss db
} {{0 2 0} {0 4 0}}







>







36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
proc db_write {db {reset 0}} {
  sqlite3_db_status $db CACHE_WRITE $reset
}

do_test 1.1 {
  db close
  sqlite3 db test.db
  execsql { PRAGMA mmap_size = 0 }
  expr {[file size test.db] / 1024}
} 6

do_test 1.2 {
  execsql { SELECT b FROM t1 WHERE a=2 }
  db_hit_miss db
} {{0 2 0} {0 4 0}}
Changes to test/exclusive2.test.
20
21
22
23
24
25
26








27
28
29
30
31
32
33
#
do_not_use_codec

ifcapable {!pager_pragmas} {
  finish_test
  return
}









# This module does not work right if the cache spills at unexpected
# moments.  So disable the soft-heap-limit.
#
sqlite3_soft_heap_limit 0

proc pagerChangeCounter {filename new {fd ""}} {







>
>
>
>
>
>
>
>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#
do_not_use_codec

ifcapable {!pager_pragmas} {
  finish_test
  return
}

# Tests in this file verify that locking_mode=exclusive causes SQLite to
# use cached pages even if the database is changed on disk. This doesn't
# work with mmap.
if {[permutation]=="mmap"} {
  finish_test
  return
}

# This module does not work right if the cache spills at unexpected
# moments.  So disable the soft-heap-limit.
#
sqlite3_soft_heap_limit 0

proc pagerChangeCounter {filename new {fd ""}} {
Changes to test/func.test.
1269
1270
1271
1272
1273
1274
1275

1276
1277
1278
1279
1280

1281
1282
1283
1284
1285
1286
1287
} {1}
do_test func-29.3 {
  db close
  sqlite3 db test.db
  sqlite3_db_status db CACHE_MISS 1
  db eval {SELECT typeof(+x) FROM t29 ORDER BY id}
} {integer null real blob text}

do_test func-29.4 {
  set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
  if {$x>100} {set x many}
  set x
} {many}

do_test func-29.5 {
  db close
  sqlite3 db test.db
  sqlite3_db_status db CACHE_MISS 1
  db eval {SELECT sum(length(x)) FROM t29}
} {1000009}
do_test func-29.6 {







>
|
|
|
|
|
>







1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
} {1}
do_test func-29.3 {
  db close
  sqlite3 db test.db
  sqlite3_db_status db CACHE_MISS 1
  db eval {SELECT typeof(+x) FROM t29 ORDER BY id}
} {integer null real blob text}
if {[permutation] != "mmap"} {
  do_test func-29.4 {
    set x [lindex [sqlite3_db_status db CACHE_MISS 1] 1]
    if {$x>100} {set x many}
    set x
  } {many}
}
do_test func-29.5 {
  db close
  sqlite3 db test.db
  sqlite3_db_status db CACHE_MISS 1
  db eval {SELECT sum(length(x)) FROM t29}
} {1000009}
do_test func-29.6 {
Changes to test/incrblob.test.
119
120
121
122
123
124
125

126
127
128
129
130
131
132
    }
  }

  db close
  forcedelete test.db test.db-journal

  sqlite3 db test.db

  execsql "PRAGMA auto_vacuum = $AutoVacuumMode"

  do_test incrblob-2.$AutoVacuumMode.1 {
    set ::str [string repeat abcdefghij 2900]
    execsql {
      BEGIN;
      CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER);







>







119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
    }
  }

  db close
  forcedelete test.db test.db-journal

  sqlite3 db test.db
  execsql "PRAGMA mmap_size = 0"
  execsql "PRAGMA auto_vacuum = $AutoVacuumMode"

  do_test incrblob-2.$AutoVacuumMode.1 {
    set ::str [string repeat abcdefghij 2900]
    execsql {
      BEGIN;
      CREATE TABLE blobs(k PRIMARY KEY, v BLOB, i INTEGER);
145
146
147
148
149
150
151

152
153
154
155
156
157
158
    } $AutoVacuumMode
  }

  do_test incrblob-2.$AutoVacuumMode.3 {
    # Open and close the db to make sure the page cache is empty.
    db close
    sqlite3 db test.db

  
    # Read the last 20 bytes of the blob via a blob handle.
    set ::blob [db incrblob blobs v 1]
    seek $::blob -20 end
    set ::fragment [read $::blob]
    close $::blob
  







>







146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
    } $AutoVacuumMode
  }

  do_test incrblob-2.$AutoVacuumMode.3 {
    # Open and close the db to make sure the page cache is empty.
    db close
    sqlite3 db test.db
    execsql "PRAGMA mmap_size = 0"
  
    # Read the last 20 bytes of the blob via a blob handle.
    set ::blob [db incrblob blobs v 1]
    seek $::blob -20 end
    set ::fragment [read $::blob]
    close $::blob
  
167
168
169
170
171
172
173

174
175
176
177
178
179
180
    string range [db one {SELECT v FROM blobs}] end-19 end
  } $::fragment

  do_test incrblob-2.$AutoVacuumMode.5 {
    # Open and close the db to make sure the page cache is empty.
    db close
    sqlite3 db test.db

  
    # Write the second-to-last 20 bytes of the blob via a blob handle.
    #
    set ::blob [db incrblob blobs v 1]
    seek $::blob -40 end
    puts -nonewline $::blob "1234567890abcdefghij"
    flush $::blob







>







169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    string range [db one {SELECT v FROM blobs}] end-19 end
  } $::fragment

  do_test incrblob-2.$AutoVacuumMode.5 {
    # Open and close the db to make sure the page cache is empty.
    db close
    sqlite3 db test.db
    execsql "PRAGMA mmap_size = 0"
  
    # Write the second-to-last 20 bytes of the blob via a blob handle.
    #
    set ::blob [db incrblob blobs v 1]
    seek $::blob -40 end
    puts -nonewline $::blob "1234567890abcdefghij"
    flush $::blob
196
197
198
199
200
201
202

203
204
205
206
207
208
209
    string range [db one {SELECT v FROM blobs}] end-39 end-20
  } "1234567890abcdefghij"

  do_test incrblob-2.$AutoVacuumMode.8 {
    # Open and close the db to make sure the page cache is empty.
    db close
    sqlite3 db test.db


    execsql { SELECT i FROM blobs } 
  } {45}

  do_test incrblob-2.$AutoVacuumMode.9 {
    nRead db
  } [expr $AutoVacuumMode ? 4 : 30]







>







199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
    string range [db one {SELECT v FROM blobs}] end-39 end-20
  } "1234567890abcdefghij"

  do_test incrblob-2.$AutoVacuumMode.8 {
    # Open and close the db to make sure the page cache is empty.
    db close
    sqlite3 db test.db
    execsql { PRAGMA mmap_size = 0 }

    execsql { SELECT i FROM blobs } 
  } {45}

  do_test incrblob-2.$AutoVacuumMode.9 {
    nRead db
  } [expr $AutoVacuumMode ? 4 : 30]
Changes to test/pageropt.test.
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# must refill.
#
do_test pageropt-1.5 {
  db2 eval {CREATE TABLE t2(y)}
  pagercount_sql {
    SELECT hex(x) FROM t1
  }
} [list 6 0 0 $blobcontent]
do_test pageropt-1.6 {
  pagercount_sql {
    SELECT hex(x) FROM t1
  }
} [list 0 0 0 $blobcontent]

# Verify that the last page of an overflow chain is not read from







|







88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# must refill.
#
do_test pageropt-1.5 {
  db2 eval {CREATE TABLE t2(y)}
  pagercount_sql {
    SELECT hex(x) FROM t1
  }
} [list [expr {[permutation]=="mmap" ? 1 : 6}] 0 0 $blobcontent]
do_test pageropt-1.6 {
  pagercount_sql {
    SELECT hex(x) FROM t1
  }
} [list 0 0 0 $blobcontent]

# Verify that the last page of an overflow chain is not read from
Changes to test/permutations.test.
130
131
132
133
134
135
136








137
138
139
140
141
142
143
#
lappend ::testsuitelist xxx

test_suite "veryquick" -prefix "" -description {
  "Very" quick test suite. Runs in less than 5 minutes on a workstation. 
  This test suite is the same as the "quick" tests, except that some files
  that test malloc and IO errors are omitted.








} -files [
  test_set $allquicktests -exclude *malloc* *ioerr* *fault* \
  multiplex* server1.test shared2.test shared6.test
]

test_suite "valgrind" -prefix "" -description {
  Run the "veryquick" test suite with a couple of multi-process tests (that







>
>
>
>
>
>
>
>







130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#
lappend ::testsuitelist xxx

test_suite "veryquick" -prefix "" -description {
  "Very" quick test suite. Runs in less than 5 minutes on a workstation. 
  This test suite is the same as the "quick" tests, except that some files
  that test malloc and IO errors are omitted.
} -files [
  test_set $allquicktests -exclude *malloc* *ioerr* *fault*
]

test_suite "mmap" -prefix "mm-" -description {
  Similar to veryquick. Except with memory mapping enabled.
} -presql {
  pragma mmap_size = -65536;
} -files [
  test_set $allquicktests -exclude *malloc* *ioerr* *fault* \
  multiplex* server1.test shared2.test shared6.test
]

test_suite "valgrind" -prefix "" -description {
  Run the "veryquick" test suite with a couple of multi-process tests (that