Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Begin adding BTree code (CVS 213) |
---|---|
Downloads: | Tarball | ZIP archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
bdb1c425f577d455155982ee2cd8cb68 |
User & Date: | drh 2001-04-17 20:09:11.000 |
Context
2001-04-17
| ||
20:09 | :-) (CVS 1719) (check-in: 8e0476f900 user: drh tags: trunk) | |
20:09 | Begin adding BTree code (CVS 213) (check-in: bdb1c425f5 user: drh tags: trunk) | |
2001-04-15
| ||
02:30 | Version 1.0.31 (CVS 472) (check-in: a7bfcbb413 user: drh tags: trunk) | |
Changes
Added src/btree.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 | /* ** Copyright (c) 2001 D. Richard Hipp ** ** This program is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public ** License as published by the Free Software Foundation; either ** version 2 of the License, or (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ** General Public License for more details. ** ** You should have received a copy of the GNU General Public ** License along with this library; if not, write to the ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, ** Boston, MA 02111-1307, USA. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ************************************************************************* ** $Id: btree.c,v 1.1 2001/04/17 20:09:11 drh Exp $ */ #include "sqliteInt.h" #include "pager.h" #include "btree.h" #include <assert.h> typedef unsigned int u32; /* ** Everything we need to know about an open database */ struct Btree { Pager *pPager; /* The page cache */ BtCursor *pCursor; /* All open cursors */ u32 *page1; /* First page of the database */ int inTrans; /* True if a transaction is current */ }; typedef Btree Bt; /* ** The maximum depth of a cursor */ #define MX_LEVEL 20 /* ** Within a cursor, each level off the search tree is an instance of ** this structure. */ typedef struct BtIdxpt BtIdxpt; struct BtIdxpt { Pgno pgno; /* The page number */ u32 *aPage; /* The page data */ int idx; /* Index into pPage[] */ }; /* ** Everything we need to know about a cursor */ struct BtCursor { Btree *pBt; /* The whole database */ BtCursor *pPrev, *pNext; /* Linked list of all cursors */ int valid; /* True if the cursor points to something */ int nLevel; /* Number of levels of indexing used */ BtIdxpt aLevel[MX_LEVEL]; /* The index levels */ }; /* ** The first page contains the following additional information: ** ** MAGIC-1 ** MAGIC-2 ** First free block */ #define EXTRA_PAGE_1_CELLS 3 #define MAGIC_1 0x7264dc61 #define MAGIC_2 0x54e55d9e /* ** Open a new database */ int sqliteBtreeOpen(const char *zFilename, int mode, Btree **ppBtree){ Btree *pBt; pBt = sqliteMalloc( sizeof(*pBt) ); if( pBt==0 ){ **ppBtree = 0; return SQLITE_NOMEM; } rc = sqlitepager_open(&pBt->pPager, zFilename, 100); if( rc!=SQLITE_OK ){ if( pBt->pPager ) sqlitepager_close(pBt->pPager); sqliteFree(pBt); *ppBtree = 0; return rc; } pBt->pCursor = 0; pBt->page1 = 0; *ppBtree = pBt; return SQLITE_OK; } /* ** Close an open database and invalidate all cursors. */ int sqliteBtreeClose(Btree *pBt){ while( pBt->pCursor ){ sqliteBtreeCloseCursor(pBt->pCursor); } sqlitepager_close(pBt->pPager); sqliteFree(pBt); return SQLITE_OK; } /* ** Start a new transaction */ int sqliteBtreeBeginTrans(Btree *pBt){ int rc; if( pBt->inTrans ) return SQLITE_ERROR; if( pBt->page1==0 ){ rc = sqlitepager_get(pBt->pPager, 1, &pBt->page1); if( rc!=SQLITE_OK ) return rc; } rc = sqlitepager_write(pBt->page1); if( rc==SQLITE_OK ){ pBt->inTrans = 1; } return rc; } /* ** Get a reference to page1 of the database file. This will ** also acquire a readlock on that file. */ static int lockBtree(Btree *pBt){ int rc; if( pBt->page1 ) return SQLITE_OK; rc = sqlitepager_get(pBt->pPager, 1, &pBt->page1); if( rc!=SQLITE_OK ) return rc; /* Sanity checking on the database file format */ return rc; } /* ** Remove the last reference to the database file. This will ** remove the read lock. */ static void unlockBtree(Btree *pBt){ if( pBt->pCursor==0 && pBt->page1!=0 ){ sqlitepager_unref(pBt->page1); pBt->page1 = 0; pBt->inTrans = 0; } } /* ** Commit the transaction currently in progress. All cursors ** must be closed before this routine is called. */ int sqliteBtreeCommit(Btree *pBt){ int rc; assert( pBt->pCursor==0 ); rc = sqlitepager_commit(pBt->pPager); unlockBtree(pBt); return rc; } /* ** Rollback the transaction in progress. All cursors must be ** closed before this routine is called. */ int sqliteBtreeRollback(Btree *pBt){ int rc; assert( pBt->pCursor==0 ); rc = sqlitepager_rollback(pBt->pPager); unlockBtree(pBt); return rc; } /* ** Create a new cursor. The act of acquiring a cursor ** gets a read lock on the database file. */ int sqliteBtreeCursor(Btree *pBt, BtCursor **ppCur){ int rc; BtCursor *pCur; if( pBt->page1==0 ){ rc = lockBtree(pBt); if( rc!=SQLITE_OK ){ *ppCur = 0; return rc; } } pCur = sqliteMalloc( sizeof(*pCur) ); if( pCur==0 ){ *ppCur = 0; unlockBtree(pBt); return SQLITE_NOMEM; } pCur->pPrev = 0; pCur->pNext = pBt->pCursor; if( pCur->pNext ){ pCur->pNext->pPrev = pCur; } pBt->pCursor = pCur; pCur->pBt = pBt; pCur->nLevel = 1; pCur->aLevel[0].pgno = 1; pCur->aLevel[0].aPage = pBt->page1; pCur->aLevel[0].idx = 0; } /* ** Close a cursor. */ int sqliteBtreeCloseCursor(BtCursor *pCur){ Btree *pBt = pCur->pBt; int i; if( pCur->pPrev ){ pCur->pPrev->pNext = pCur->pNext; }else{ pBt->pCursor = pCur->pNext; } if( pCur->pNext ){ pCur->pNext->pPrev = pCur->pPrev; } for(i=pCur->nLevel-1; i>0; i--){ sqlitepager_unref(pCur->aLevel[i].aPage); } if( pBt->pCursor==0 && pBt->inTrans==0 ){ unlockBtree(pBt); } sqliteFree(pCur); } int sqliteBtreeKeySize(BtCursor *pCur){ int nEntry; u32 *aPage; BtIdxpt *pIdx; int offset; if( !pCur->valid ) return 0; pIdx = &pCur->aLevel[pCur->nLevel-1]; aPage = pIdx->aPage; offset = (pIdx->pgno==1)*EXTRA_PAGE_1_CELLS; nEntry = aPage[offset]; if( pIdx->idx<nEntry ){ } int sqliteBtreeKey(BtCursor*, int offset, int amt, char *zBuf); int sqliteBtreeDataSize(BtCursor*); int sqliteBtreeData(BtCursor*, int offset, int amt, char *zBuf); /* Move the cursor so that it points to an entry near pKey. ** Return 0 if the cursor is left pointing exactly at pKey. ** Return -1 if the cursor points to the largest entry less than pKey. ** Return 1 if the cursor points to the smallest entry greater than pKey. */ int sqliteBtreeMoveto(BtCursor*, void *pKey, int nKey); int sqliteBtreeDelete(BtCursor*); int sqliteBtreeInsert(BtCursor*, void *pKey, int nKey, void *pData, int nData); int sqliteBtreeNext(BtCursor*); |
Added src/btree.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 | /* ** Copyright (c) 2001 D. Richard Hipp ** ** This program is free software; you can redistribute it and/or ** modify it under the terms of the GNU General Public ** License as published by the Free Software Foundation; either ** version 2 of the License, or (at your option) any later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ** General Public License for more details. ** ** You should have received a copy of the GNU General Public ** License along with this library; if not, write to the ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, ** Boston, MA 02111-1307, USA. ** ** Author contact information: ** drh@hwaci.com ** http://www.hwaci.com/drh/ ** ************************************************************************* ** This header file defines the interface that the sqlite B-Tree file ** subsystem. ** ** @(#) $Id: btree.h,v 1.1 2001/04/17 20:09:11 drh Exp $ */ typedef struct Btree Btree; typedef struct BtCursor BtCursor; int sqliteBtreeOpen(const char *zFilename, int mode, Btree **ppBtree); int sqliteBtreeClose(Btree*); int sqliteBtreeBeginTrans(Btree*); int sqliteBtreeCommit(Btree*); int sqliteBtreeRollback(Btree*); int sqliteBtreeCursor(Btree*, BtCursor **ppCur); /* Move the cursor so that it points to an entry near pKey. ** Return 0 if the cursor is left pointing exactly at pKey. ** Return -1 if the cursor points to the largest entry less than pKey. ** Return 1 if the cursor points to the smallest entry greater than pKey. */ int sqliteBtreeMoveto(BtCursor*, void *pKey, int nKey); int sqliteBtreeDelete(BtCursor*); int sqliteBtreeInsert(BtCursor*, void *pKey, int nKey, void *pData, int nData); int sqliteBtreeNext(BtCursor*); int sqliteBtreeKeySize(BtCursor*); int sqliteBtreeKey(BtCursor*, int offset, int amt, char *zBuf); int sqliteBtreeDataSize(BtCursor*); int sqliteBtreeData(BtCursor*, int offset, int amt, char *zBuf); int sqliteBtreeCloseCursor(BtCursor*); |
Changes to test/pager.test.
︙ | ︙ | |||
19 20 21 22 23 24 25 | # drh@hwaci.com # http://www.hwaci.com/drh/ # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this script is page cache subsystem. # | | | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | # drh@hwaci.com # http://www.hwaci.com/drh/ # #*********************************************************************** # This file implements regression tests for SQLite library. The # focus of this script is page cache subsystem. # # $Id: pager.test,v 1.3 2001/04/17 20:09:11 drh Exp $ set testdir [file dirname $argv0] source $testdir/tester.tcl if {$dbprefix!="mem:" && [info commands pager_open]!=""} { |
︙ | ︙ | |||
167 168 169 170 171 172 173 | } {Page-One} do_test pager-2.99 { pager_close $::p1 } {} do_test pager-3.1 { set v [catch { | | | 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | } {Page-One} do_test pager-2.99 { pager_close $::p1 } {} do_test pager-3.1 { set v [catch { set ::p1 [pager_open ptf1.db 15] } msg] if {$v} {lappend v $msg} set v } {0} do_test pager-3.2 { pager_pagecount $::p1 } {1} |
︙ | ︙ | |||
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | do_test pager-3.6.[expr {$i-1}] [subst { set gx \[page_get $::p1 $i\] set v \[page_read \$gx\] page_unref \$gx set v }] "Page-$i" } do_test pager-3.99 { pager_close $::p1 } {} } ;# end if( not mem: and has pager_open command ); finish_test | > > > > > > > > > > > > > > > > > > > > > > > | 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 | do_test pager-3.6.[expr {$i-1}] [subst { set gx \[page_get $::p1 $i\] set v \[page_read \$gx\] page_unref \$gx set v }] "Page-$i" } for {set i 1} {$i<=20} {incr i} { regsub -all CNT { set ::g1 [page_get $::p1 CNT] set ::g2 [page_get $::p1 CNT] set ::vx [page_read $::g2] expr {$::g1==$::g2} } $i body; do_test pager-3.7.$i.1 $body {1} regsub -all CNT { page_unref $::g2 set vy [page_read $::g1] expr {$vy==$::vx} } $i body; do_test pager-3.7.$i.2 $body {1} regsub -all CNT { page_unref $::g1 set gx [page_get $::p1 CNT] set vy [page_read $gx] page_unref $gx expr {$vy==$::vx} } $i body; do_test pager-3.7.$i.3 $body {1} } do_test pager-3.99 { pager_close $::p1 } {} } ;# end if( not mem: and has pager_open command ); finish_test |