/ Check-in [0509eff0]
Login

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

Overview
Comment:Add the memjournal.c file that should have been with the previous checkin. (CVS 5831)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:0509eff0e6b5cdeeb52c12c0574ea63e9715bcb5
User & Date: danielk1977 2008-10-17 19:13:05
Context
2008-10-19
21:03
When a name in double quotes falls back to being a string literal, make sure it has no affinity. Ticket #3442. (CVS 5832) check-in: 81cfee5c user: drh tags: trunk
2008-10-17
19:13
Add the memjournal.c file that should have been with the previous checkin. (CVS 5831) check-in: 0509eff0 user: danielk1977 tags: trunk
18:51
Add "pragma journal_mode=memory". Change the way rollback works for in-memory databases so that it reuses the journal_mode=memory code. (CVS 5830) check-in: 39ebf01a user: danielk1977 tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Added src/memjournal.c.

            1  +/*
            2  +** 2007 August 22
            3  +**
            4  +** The author disclaims copyright to this source code.  In place of
            5  +** a legal notice, here is a blessing:
            6  +**
            7  +**    May you do good and not evil.
            8  +**    May you find forgiveness for yourself and forgive others.
            9  +**    May you share freely, never taking more than you give.
           10  +**
           11  +*************************************************************************
           12  +**
           13  +** @(#) $Id: memjournal.c,v 1.1 2008/10/17 19:13:05 danielk1977 Exp $
           14  +*/
           15  +
           16  +
           17  +#include "sqliteInt.h"
           18  +
           19  +typedef struct MemJournal MemJournal;
           20  +typedef struct FilePoint FilePoint;
           21  +typedef struct FileChunk FileChunk;
           22  +
           23  +#define JOURNAL_CHUNKSIZE 1024
           24  +
           25  +#define MIN(x,y) ((x)<(y)?(x):(y))
           26  +
           27  +struct FileChunk {
           28  +  FileChunk *pNext;
           29  +  u8 zChunk[JOURNAL_CHUNKSIZE];
           30  +};
           31  +
           32  +struct FilePoint {
           33  +  sqlite3_int64 iOffset;
           34  +  FileChunk *pChunk;
           35  +};
           36  +
           37  +struct MemJournal {
           38  +  sqlite3_io_methods *pMethod;    /* I/O methods on journal files */
           39  +  FileChunk *pFirst;              /* Head of in-memory chunk-list */
           40  +  FilePoint endpoint;             /* Pointer to the end of the file */
           41  +  FilePoint readpoint;            /* Pointer to the end of the last xRead() */
           42  +};
           43  +
           44  +/*
           45  +** Read data from the file.
           46  +*/
           47  +static int memjrnlRead(
           48  +  sqlite3_file *pJfd,    /* The journal file from which to read */
           49  +  void *zBuf,            /* Put the results here */
           50  +  int iAmt,              /* Number of bytes to read */
           51  +  sqlite_int64 iOfst     /* Begin reading at this offset */
           52  +){
           53  +  MemJournal *p = (MemJournal *)pJfd;
           54  +  u8 *zOut = zBuf;
           55  +  int nRead = iAmt;
           56  +  int iChunkOffset;
           57  +  FileChunk *pChunk;
           58  +
           59  +  assert( iOfst+iAmt<=p->endpoint.iOffset );
           60  +
           61  +  if( p->readpoint.iOffset!=iOfst || iOfst==0 ){
           62  +    sqlite3_int64 iOff = 0;
           63  +    for(pChunk=p->pFirst; 
           64  +        pChunk && (iOff+JOURNAL_CHUNKSIZE)<=iOfst;
           65  +        pChunk=pChunk->pNext
           66  +    ){
           67  +      iOff += JOURNAL_CHUNKSIZE;
           68  +    }
           69  +  }else{
           70  +    pChunk = p->readpoint.pChunk;
           71  +  }
           72  +
           73  +  iChunkOffset = (iOfst%JOURNAL_CHUNKSIZE);
           74  +  do {
           75  +    int iSpace = JOURNAL_CHUNKSIZE - iChunkOffset;
           76  +    int nCopy = MIN(nRead, (JOURNAL_CHUNKSIZE - iChunkOffset));
           77  +    memcpy(zOut, &pChunk->zChunk[iChunkOffset], nCopy);
           78  +    zOut += nCopy;
           79  +    nRead -= iSpace;
           80  +    iChunkOffset = 0;
           81  +  } while( nRead>=0 && (pChunk=pChunk->pNext) && nRead>0 );
           82  +  p->readpoint.iOffset = iOfst+iAmt;
           83  +  p->readpoint.pChunk = pChunk;
           84  +
           85  +  return SQLITE_OK;
           86  +}
           87  +
           88  +/*
           89  +** Write data to the file.
           90  +*/
           91  +static int memjrnlWrite(
           92  +  sqlite3_file *pJfd,    /* The journal file into which to write */
           93  +  const void *zBuf,      /* Take data to be written from here */
           94  +  int iAmt,              /* Number of bytes to write */
           95  +  sqlite_int64 iOfst     /* Begin writing at this offset into the file */
           96  +){
           97  +  MemJournal *p = (MemJournal *)pJfd;
           98  +  int nWrite = iAmt;
           99  +  u8 *zWrite = (u8 *)zBuf;
          100  +
          101  +  /* An in-memory journal file should only ever be appended to. Random
          102  +  ** access writes are not required by sqlite.
          103  +  */
          104  +  assert(iOfst==p->endpoint.iOffset);
          105  +
          106  +  while( nWrite>0 ){
          107  +    FileChunk *pChunk = p->endpoint.pChunk;
          108  +    int iChunkOffset = p->endpoint.iOffset%JOURNAL_CHUNKSIZE;
          109  +    int iSpace = MIN(nWrite, JOURNAL_CHUNKSIZE - iChunkOffset);
          110  +
          111  +    if( iChunkOffset==0 ){
          112  +      /* New chunk is required to extend the file. */
          113  +      FileChunk *pNew = sqlite3_malloc(sizeof(FileChunk));
          114  +      if( !pNew ){
          115  +        return SQLITE_IOERR_NOMEM;
          116  +      }
          117  +      pNew->pNext = 0;
          118  +      if( pChunk ){
          119  +        assert( p->pFirst );
          120  +        pChunk->pNext = pNew;
          121  +      }else{
          122  +        assert( !p->pFirst );
          123  +        p->pFirst = pNew;
          124  +      }
          125  +      p->endpoint.pChunk = pNew;
          126  +    }
          127  +
          128  +    memcpy(&p->endpoint.pChunk->zChunk[iChunkOffset], zWrite, iSpace);
          129  +    zWrite += iSpace;
          130  +    nWrite -= iSpace;
          131  +    p->endpoint.iOffset += iSpace;
          132  +  }
          133  +
          134  +  return SQLITE_OK;
          135  +}
          136  +
          137  +/*
          138  +** Truncate the file.
          139  +*/
          140  +static int memjrnlTruncate(sqlite3_file *pJfd, sqlite_int64 size){
          141  +  MemJournal *p = (MemJournal *)pJfd;
          142  +  FileChunk *pChunk;
          143  +  assert(size==0);
          144  +  pChunk = p->pFirst;
          145  +  while( pChunk ){
          146  +    FileChunk *pTmp = pChunk;
          147  +    pChunk = pChunk->pNext;
          148  +    sqlite3_free(pTmp);
          149  +  }
          150  +  sqlite3MemJournalOpen(pJfd);
          151  +  return SQLITE_OK;
          152  +}
          153  +
          154  +/*
          155  +** Close the file.
          156  +*/
          157  +static int memjrnlClose(sqlite3_file *pJfd){
          158  +  memjrnlTruncate(pJfd, 0);
          159  +  return SQLITE_OK;
          160  +}
          161  +
          162  +
          163  +/*
          164  +** Sync the file.
          165  +*/
          166  +static int memjrnlSync(sqlite3_file *pJfd, int flags){
          167  +  return SQLITE_OK;
          168  +}
          169  +
          170  +/*
          171  +** Query the size of the file in bytes.
          172  +*/
          173  +static int memjrnlFileSize(sqlite3_file *pJfd, sqlite_int64 *pSize){
          174  +  MemJournal *p = (MemJournal *)pJfd;
          175  +  *pSize = (sqlite_int64) p->endpoint.iOffset;
          176  +  return SQLITE_OK;
          177  +}
          178  +
          179  +/*
          180  +** Table of methods for MemJournal sqlite3_file object.
          181  +*/
          182  +static struct sqlite3_io_methods MemJournalMethods = {
          183  +  1,                /* iVersion */
          184  +  memjrnlClose,     /* xClose */
          185  +  memjrnlRead,      /* xRead */
          186  +  memjrnlWrite,     /* xWrite */
          187  +  memjrnlTruncate,  /* xTruncate */
          188  +  memjrnlSync,      /* xSync */
          189  +  memjrnlFileSize,  /* xFileSize */
          190  +  0,                /* xLock */
          191  +  0,                /* xUnlock */
          192  +  0,                /* xCheckReservedLock */
          193  +  0,                /* xFileControl */
          194  +  0,                /* xSectorSize */
          195  +  0                 /* xDeviceCharacteristics */
          196  +};
          197  +
          198  +/* 
          199  +** Open a journal file.
          200  +*/
          201  +void sqlite3MemJournalOpen(sqlite3_file *pJfd){
          202  +  MemJournal *p = (MemJournal *)pJfd;
          203  +  memset(p, 0, sqlite3MemJournalSize());
          204  +  p->pMethod = &MemJournalMethods;
          205  +}
          206  +
          207  +/*
          208  +** Return true if the file-handle passed as an argument is 
          209  +** an in-memory journal 
          210  +*/
          211  +int sqlite3IsMemJournal(sqlite3_file *pJfd){
          212  +  return pJfd->pMethods==&MemJournalMethods;
          213  +}
          214  +
          215  +/* 
          216  +** Return the number of bytes required to store a MemJournal that uses vfs
          217  +** pVfs to create the underlying on-disk files.
          218  +*/
          219  +int sqlite3MemJournalSize(){
          220  +  return sizeof(MemJournal);
          221  +}
          222  +
          223  +