/ Check-in [44d8d1e9]
Login

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

Overview
Comment:Base the name of the statement journal on the original database filename. Remember the statement journal name for the lifetime of the Pager so that the name pointer passed to xOpen persists as long as the file exists. (CVS 4410)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:44d8d1e92d93a5bb28bd10281b4d87e89586ef58
User & Date: drh 2007-09-06 23:28:24
Context
2007-09-06
23:39
Throw an error on an attempt to open a database where the page size is larger than SQLITE_MAX_PAGE_SIZE. Ticket #2628. (CVS 4411) check-in: 4881f7cb user: drh tags: trunk
23:28
Base the name of the statement journal on the original database filename. Remember the statement journal name for the lifetime of the Pager so that the name pointer passed to xOpen persists as long as the file exists. (CVS 4410) check-in: 44d8d1e9 user: drh tags: trunk
22:19
Allocate page cache headers and page cache data buffers separately. The data buffer will be a power of two in size and this gives some malloc implementation additional optimization opportunitites. (CVS 4409) check-in: 2b755def user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/pager.c.

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
...
366
367
368
369
370
371
372

373
374
375
376
377
378
379
....
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
....
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101

2102
2103
2104
2105
2106
2107
2108
....
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181


2182
2183
2184
2185
2186
2187
2188
....
4722
4723
4724
4725
4726
4727
4728
4729
4730

4731

4732
4733
4734
4735
4736
4737
4738
** The pager is used to access a database disk file.  It implements
** atomic commit and rollback through the use of a journal file that
** is separate from the database file.  The pager also implements file
** locking to prevent two processes from writing the same database
** file simultaneously, or one process from reading the database while
** another is writing.
**
** @(#) $Id: pager.c,v 1.386 2007/09/06 22:19:15 drh Exp $
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"
#include <assert.h>
#include <string.h>

/*
................................................................................
  int mxPage;                 /* Maximum number of pages to hold in cache */
  Pgno mxPgno;                /* Maximum allowed size of the database */
  u8 *aInJournal;             /* One bit for each page in the database file */
  u8 *aInStmt;                /* One bit for each page in the database */
  char *zFilename;            /* Name of the database file */
  char *zJournal;             /* Name of the journal file */
  char *zDirectory;           /* Directory hold database and journal files */

  sqlite3_file *fd, *jfd;     /* File descriptors for database and journal */
  sqlite3_file *stfd;         /* File descriptor for the statement subjournal*/
  BusyHandler *pBusyHandler;  /* Pointer to sqlite.busyHandler */
  PagerLruList lru;           /* LRU list of free pages */
  PgHdr *pAll;                /* List of all pages */
  PgHdr *pStmt;               /* List of pages in the statement subjournal */
  PgHdr *pDirty;              /* List of all dirty pages */
................................................................................

/*
** Open a temporary file. 
**
** Write the file descriptor into *fd.  Return SQLITE_OK on success or some
** other error code if we fail. The OS will automatically delete the temporary
** file when it is closed.
**
** If zFilename is 0, then an appropriate temporary filename is
** generated automatically.
**
** The vfsFlags value should be SQLITE_OPEN_SUBJOURNAL or SQLITE_OPEN
*/
static int sqlite3PagerOpentemp(
  sqlite3_vfs *pVfs,    /* The virtual file system layer */
  sqlite3_file *pFile,  /* Write the file descriptor here */
  char *zFilename,      /* Name of the file.  Might be NULL */
  int vfsFlags          /* Flags passed through to the VFS */
){
  int rc;

  char *zFree = 0;
  if( zFilename==0 ){
    zFree = (char *)sqlite3_malloc(pVfs->mxPathname);
    if( !zFree ){
      return SQLITE_NOMEM;
    }
    zFilename = zFree;
    rc = sqlite3OsGetTempName(pVfs, zFilename);
    if( rc!=SQLITE_OK ){
      sqlite3_free(zFree);
      return rc;
    }
  }

#ifdef SQLITE_TEST
  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
#endif

  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
  rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
  assert( rc!=SQLITE_OK || pFile->pMethods );
  sqlite3_free(zFree);
  return rc;
}

/*
** Create a new page cache and put a pointer to the page cache in *ppPager.
** The file to be cached need not exist.  The file is not locked until
** the first call to sqlite3PagerGet() and is only held open until the
................................................................................
  nPathname = strlen(zPathname);

  /* Allocate memory for the pager structure */
  pPager = sqlite3MallocZero(
    sizeof(*pPager) +           /* Pager structure */
    journalFileSize +           /* The journal file structure */ 
    pVfs->szOsFile * 2 +        /* The db and stmt journal files */ 
    nPathname * 3 + 30          /* zFilename, zDirectory, zJournal */
  );
  if( !pPager ){
    sqlite3_free(zPathname);
    return SQLITE_NOMEM;
  }
  pPtr = (u8 *)&pPager[1];
  pPager->vfsFlags = vfsFlags;
  pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
  pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
  pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
  pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
  pPager->zDirectory = &pPager->zFilename[nPathname+1];
  pPager->zJournal = &pPager->zDirectory[nPathname+1];

  pPager->pVfs = pVfs;
  memcpy(pPager->zFilename, zPathname, nPathname+1);
  sqlite3_free(zPathname);

  /* Open the pager file.
  */
  if( zFilename && zFilename[0] && !memDb ){
................................................................................
  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))

  /* Fill in Pager.zDirectory[] */
  memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
  for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
  if( i>0 ) pPager->zDirectory[i-1] = 0;

  /* Fill in Pager.zJournal[] */
  memcpy(pPager->zJournal, pPager->zFilename, nPathname);
  memcpy(&pPager->zJournal[nPathname], "-journal", 9);



  /* pPager->journalOpen = 0; */
  pPager->useJournal = useJournal && !memDb;
  pPager->noReadlock = noReadlock && readOnly;
  /* pPager->stmtOpen = 0; */
  /* pPager->stmtInUse = 0; */
  /* pPager->nRef = 0; */
................................................................................
  assert( pPager->stmtJSize == pPager->journalOff );
#endif
  pPager->stmtJSize = pPager->journalOff;
  pPager->stmtSize = pPager->dbSize;
  pPager->stmtHdrOff = 0;
  pPager->stmtCksum = pPager->cksumInit;
  if( !pPager->stmtOpen ){
    rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, 0,
                              SQLITE_OPEN_SUBJOURNAL);

    if( rc ) goto stmt_begin_failed;

    pPager->stmtOpen = 1;
    pPager->stmtNRec = 0;
  }
  pPager->stmtInUse = 1;
  return SQLITE_OK;
 
stmt_begin_failed:







|







 







>







 







<
<
<
<
<








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









<







 







|













>







 







|


>
>







 







|

>
|
>







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
....
1974
1975
1976
1977
1978
1979
1980





1981
1982
1983
1984
1985
1986
1987
1988


1989











1990
1991
1992
1993
1994
1995
1996
1997
1998

1999
2000
2001
2002
2003
2004
2005
....
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
....
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
....
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
** The pager is used to access a database disk file.  It implements
** atomic commit and rollback through the use of a journal file that
** is separate from the database file.  The pager also implements file
** locking to prevent two processes from writing the same database
** file simultaneously, or one process from reading the database while
** another is writing.
**
** @(#) $Id: pager.c,v 1.387 2007/09/06 23:28:24 drh Exp $
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"
#include <assert.h>
#include <string.h>

/*
................................................................................
  int mxPage;                 /* Maximum number of pages to hold in cache */
  Pgno mxPgno;                /* Maximum allowed size of the database */
  u8 *aInJournal;             /* One bit for each page in the database file */
  u8 *aInStmt;                /* One bit for each page in the database */
  char *zFilename;            /* Name of the database file */
  char *zJournal;             /* Name of the journal file */
  char *zDirectory;           /* Directory hold database and journal files */
  char *zStmtJrnl;            /* Name of the statement journal file */
  sqlite3_file *fd, *jfd;     /* File descriptors for database and journal */
  sqlite3_file *stfd;         /* File descriptor for the statement subjournal*/
  BusyHandler *pBusyHandler;  /* Pointer to sqlite.busyHandler */
  PagerLruList lru;           /* LRU list of free pages */
  PgHdr *pAll;                /* List of all pages */
  PgHdr *pStmt;               /* List of pages in the statement subjournal */
  PgHdr *pDirty;              /* List of all dirty pages */
................................................................................

/*
** Open a temporary file. 
**
** Write the file descriptor into *fd.  Return SQLITE_OK on success or some
** other error code if we fail. The OS will automatically delete the temporary
** file when it is closed.





*/
static int sqlite3PagerOpentemp(
  sqlite3_vfs *pVfs,    /* The virtual file system layer */
  sqlite3_file *pFile,  /* Write the file descriptor here */
  char *zFilename,      /* Name of the file.  Might be NULL */
  int vfsFlags          /* Flags passed through to the VFS */
){
  int rc;


  assert( zFilename!=0 );












#ifdef SQLITE_TEST
  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
#endif

  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
  rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
  assert( rc!=SQLITE_OK || pFile->pMethods );

  return rc;
}

/*
** Create a new page cache and put a pointer to the page cache in *ppPager.
** The file to be cached need not exist.  The file is not locked until
** the first call to sqlite3PagerGet() and is only held open until the
................................................................................
  nPathname = strlen(zPathname);

  /* Allocate memory for the pager structure */
  pPager = sqlite3MallocZero(
    sizeof(*pPager) +           /* Pager structure */
    journalFileSize +           /* The journal file structure */ 
    pVfs->szOsFile * 2 +        /* The db and stmt journal files */ 
    4*nPathname + 40            /* zFilename, zDirectory, zJournal, zStmtJrnl */
  );
  if( !pPager ){
    sqlite3_free(zPathname);
    return SQLITE_NOMEM;
  }
  pPtr = (u8 *)&pPager[1];
  pPager->vfsFlags = vfsFlags;
  pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
  pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
  pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
  pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
  pPager->zDirectory = &pPager->zFilename[nPathname+1];
  pPager->zJournal = &pPager->zDirectory[nPathname+1];
  pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
  pPager->pVfs = pVfs;
  memcpy(pPager->zFilename, zPathname, nPathname+1);
  sqlite3_free(zPathname);

  /* Open the pager file.
  */
  if( zFilename && zFilename[0] && !memDb ){
................................................................................
  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))

  /* Fill in Pager.zDirectory[] */
  memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
  for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
  if( i>0 ) pPager->zDirectory[i-1] = 0;

  /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */
  memcpy(pPager->zJournal, pPager->zFilename, nPathname);
  memcpy(&pPager->zJournal[nPathname], "-journal", 9);
  memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname);
  memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10);

  /* pPager->journalOpen = 0; */
  pPager->useJournal = useJournal && !memDb;
  pPager->noReadlock = noReadlock && readOnly;
  /* pPager->stmtOpen = 0; */
  /* pPager->stmtInUse = 0; */
  /* pPager->nRef = 0; */
................................................................................
  assert( pPager->stmtJSize == pPager->journalOff );
#endif
  pPager->stmtJSize = pPager->journalOff;
  pPager->stmtSize = pPager->dbSize;
  pPager->stmtHdrOff = 0;
  pPager->stmtCksum = pPager->cksumInit;
  if( !pPager->stmtOpen ){
    rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
                              SQLITE_OPEN_SUBJOURNAL);
    if( rc ){
      goto stmt_begin_failed;
    }
    pPager->stmtOpen = 1;
    pPager->stmtNRec = 0;
  }
  pPager->stmtInUse = 1;
  return SQLITE_OK;
 
stmt_begin_failed:

Changes to test/lock4.test.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#    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 SQLite library.  The
# focus of this script is database locks.
#
# $Id: lock4.test,v 1.5 2007/09/05 13:56:32 danielk1977 Exp $


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

# Initialize the test.db database so that it is non-empty
#
................................................................................
    INSERT INTO t2 VALUES(1)
  } db2
} {1 {database is locked}}
do_test lock4-1.3 {
  db eval {
     COMMIT;
  }
breakpoint
  while {[file exists test2.db-journal]} {
    after 10
  }
  db2 eval {
     SELECT * FROM t2
  }
} {2}







|







 







<







7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
..
79
80
81
82
83
84
85

86
87
88
89
90
91
92
#    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 SQLite library.  The
# focus of this script is database locks.
#
# $Id: lock4.test,v 1.6 2007/09/06 23:28:25 drh Exp $


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

# Initialize the test.db database so that it is non-empty
#
................................................................................
    INSERT INTO t2 VALUES(1)
  } db2
} {1 {database is locked}}
do_test lock4-1.3 {
  db eval {
     COMMIT;
  }

  while {[file exists test2.db-journal]} {
    after 10
  }
  db2 eval {
     SELECT * FROM t2
  }
} {2}