engine/sqlite/src/vdbefifo.cpp
author Sebastian Brannstrom <sebastianb@symbian.org>
Fri, 22 Oct 2010 14:28:27 +0100
branchsymbian1
changeset 266 ff6cd24e77e2
parent 2 29cda98b007e
permissions -rw-r--r--
Slightly tweaked icons
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     1
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     2
** 2005 June 16
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     3
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     4
** The author disclaims copyright to this source code.  In place of
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     5
** a legal notice, here is a blessing:
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     6
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     7
**    May you do good and not evil.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     8
**    May you find forgiveness for yourself and forgive others.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
     9
**    May you share freely, never taking more than you give.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    10
**
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    11
*************************************************************************
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    12
** This file implements a FIFO queue of rowids used for processing
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    13
** UPDATE and DELETE statements.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    14
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    15
#include "sqliteInt.h"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    16
#include "vdbeInt.h"
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    17
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    18
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    19
** Allocate a new FifoPage and return a pointer to it.  Return NULL if
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    20
** we run out of memory.  Leave space on the page for nEntry entries.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    21
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    22
static FifoPage *allocateFifoPage(int nEntry){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    23
  FifoPage *pPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    24
  if( nEntry>32767 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    25
    nEntry = 32767;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    26
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    27
  pPage = (FifoPage*)sqlite3_malloc( sizeof(FifoPage) + sizeof(i64)*(nEntry-1) );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    28
  if( pPage ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    29
    pPage->nSlot = nEntry;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    30
    pPage->iWrite = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    31
    pPage->iRead = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    32
    pPage->pNext = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    33
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    34
  return pPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    35
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    36
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    37
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    38
** Initialize a Fifo structure.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    39
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    40
void sqlite3VdbeFifoInit(Fifo *pFifo){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    41
  memset(pFifo, 0, sizeof(*pFifo));
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    42
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    43
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    44
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    45
** Push a single 64-bit integer value into the Fifo.  Return SQLITE_OK
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    46
** normally.   SQLITE_NOMEM is returned if we are unable to allocate
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    47
** memory.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    48
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    49
int sqlite3VdbeFifoPush(Fifo *pFifo, i64 val){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    50
  FifoPage *pPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    51
  pPage = pFifo->pLast;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    52
  if( pPage==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    53
    pPage = pFifo->pLast = pFifo->pFirst = allocateFifoPage(20);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    54
    if( pPage==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    55
      return SQLITE_NOMEM;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    56
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    57
  }else if( pPage->iWrite>=pPage->nSlot ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    58
    pPage->pNext = allocateFifoPage(pFifo->nEntry);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    59
    if( pPage->pNext==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    60
      return SQLITE_NOMEM;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    61
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    62
    pPage = pFifo->pLast = pPage->pNext;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    63
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    64
  pPage->aSlot[pPage->iWrite++] = val;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    65
  pFifo->nEntry++;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    66
  return SQLITE_OK;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    67
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    68
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    69
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    70
** Extract a single 64-bit integer value from the Fifo.  The integer
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    71
** extracted is the one least recently inserted.  If the Fifo is empty
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    72
** return SQLITE_DONE.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    73
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    74
int sqlite3VdbeFifoPop(Fifo *pFifo, i64 *pVal){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    75
  FifoPage *pPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    76
  if( pFifo->nEntry==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    77
    return SQLITE_DONE;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    78
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    79
  assert( pFifo->nEntry>0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    80
  pPage = pFifo->pFirst;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    81
  assert( pPage!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    82
  assert( pPage->iWrite>pPage->iRead );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    83
  assert( pPage->iWrite<=pPage->nSlot );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    84
  assert( pPage->iRead<pPage->nSlot );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    85
  assert( pPage->iRead>=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    86
  *pVal = pPage->aSlot[pPage->iRead++];
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    87
  pFifo->nEntry--;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    88
  if( pPage->iRead>=pPage->iWrite ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    89
    pFifo->pFirst = pPage->pNext;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    90
    sqlite3_free(pPage);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    91
    if( pFifo->nEntry==0 ){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    92
      assert( pFifo->pLast==pPage );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    93
      pFifo->pLast = 0;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    94
    }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    95
      assert( pFifo->pFirst!=0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    96
    }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    97
  }else{
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    98
    assert( pFifo->nEntry>0 );
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
    99
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   100
  return SQLITE_OK;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   101
}
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   102
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   103
/*
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   104
** Delete all information from a Fifo object.   Free all memory held
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   105
** by the Fifo.
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   106
*/
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   107
void sqlite3VdbeFifoClear(Fifo *pFifo){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   108
  FifoPage *pPage, *pNextPage;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   109
  for(pPage=pFifo->pFirst; pPage; pPage=pNextPage){
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   110
    pNextPage = pPage->pNext;
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   111
    sqlite3_free(pPage);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   112
  }
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   113
  sqlite3VdbeFifoInit(pFifo);
29cda98b007e Initial import of Podcatcher from the Bergamot project
skip
parents:
diff changeset
   114
}