/ Check-in [98da8253]
Login

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

Overview
Comment::-) (CVS 180)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:98da825312fd4bb8a20ff33293131c02beb3ae63
User & Date: drh 2001-01-22 00:31:53
Context
2001-01-25
01:45
:-) (CVS 1712) check-in: edb01b12 user: drh tags: trunk
2001-01-22
00:31
:-) (CVS 180) check-in: 98da8253 user: drh tags: trunk
2001-01-21
22:03
:-) (CVS 1711) check-in: 0529c979 user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added doc/report1.txt.

































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
The SQL database used for ACD contains 113 tables and indices implemented
in GDBM.  The following are statistics on the sizes of keys and data
within these tables and indices.

Entries:      962080
Size:         45573853
Avg Size:     48
Key Size:     11045299
Avg Key Size: 12
Max Key Size: 99

    0..8            266    0%
    9..12          5485    0%
   13..16         73633    8%
   17..24        180918   27%
   25..32        209823   48%
   33..40        148995   64%
   41..48         76304   72%
   49..56         14346   73%
   57..64         15725   75%
   65..80         44916   80%
   81..96        127815   93%
   97..112        34769   96%
  113..128        13314   98%
  129..144         8098   99%
  145..160         3355   99%
  161..176         1159   99%
  177..192          629   99%
  193..208          221   99%
  209..224          210   99%
  225..240          129   99%
  241..256           57   99%
  257..288          496   99%
  289..320           60   99%
  321..352           37   99%
  353..384           46   99%
  385..416           22   99%
  417..448           24   99%
  449..480           26   99%
  481..512           27   99%
  513..1024         471   99%
 1025..2048         389   99%
 2049..4096         182   99%
 4097..8192          74   99%
 8193..16384         34   99%
16385..32768         17   99%
32769..65536          5   99%
65537..131073         3  100%

Changes to src/TODO.

18
19
20
21
22
23
24

25
26
27
28
29
30
31


32
      - Able to delete without disturbing scan order
      - Now keeps a count of number of table entries
         + Special processing for count(*)
         + Better selection of indices on a select
      - Transactions
  *  Modify sqlite_master to store the table number.
  *  Add a cache in DbCursor to speed up the sqliteDbReadOvfl() routine.


Longer term:
  *  Document all the changes and release Sqlite 2.0.
  *  Techniques for optimizing querys by grouping data with similar
     indices.
  *  "OPTIMIZE select" statement to automatically create and/or tune
     indices.


  *  Parse and use constraints.







>







>
>

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
      - Able to delete without disturbing scan order
      - Now keeps a count of number of table entries
         + Special processing for count(*)
         + Better selection of indices on a select
      - Transactions
  *  Modify sqlite_master to store the table number.
  *  Add a cache in DbCursor to speed up the sqliteDbReadOvfl() routine.
  *  Add cache information to speed up sqliteDbCursorMoveTo().

Longer term:
  *  Document all the changes and release Sqlite 2.0.
  *  Techniques for optimizing querys by grouping data with similar
     indices.
  *  "OPTIMIZE select" statement to automatically create and/or tune
     indices.
  *  "CREATE INDEX FOR select" to automatically generate needed indices.
  *  "VACUUM table USING index".
  *  Parse and use constraints.

Changes to src/db.c.

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
..
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
...
137
138
139
140
141
142
143
































































































































































































































































144
145
146
147
148
149
150
...
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
...
250
251
252
253
254
255
256
257

258
259
260
261
262
263
264
265
266
267
268

269
270
271
272
273
274
275
...
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323


324
325
326
327
328
329
330
** Boston, MA  02111-1307, USA.
**
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** $Id: db.c,v 1.2 2001/01/21 00:58:08 drh Exp $
*/
#include "sqliteInt.h"
#include "pg.h"

/*
** Everything we need to know about an open database
*/
................................................................................
** Leaf blocks:
**
**     0.   BLOCK_MAGIC | BLOCK_LEAF 
**     1.   number of table entries  (only used if a table root block)
**     entries....
**         0.  size of this entry (measured in u32's)
**         1.  hash
**         2.  keysize  (in bytes. bit 31 set if uses overflow)
**         3.  datasize (in bytes. bit 31 set if uses overflow pages)
**         4.  key
**         5+. data
**
** Overflow block:

**

**     0.   BLOCK_MAGIC | BLOCK_OVERFLOW
**     1.   address of next block in overflow buffer
**     data...
**
** Index block:
**
**     0.   BLOCK_MAGIC | BLOCK_INDEX
**     1.   number of table entries  (only used if a table root block)
**     2.   entries in this index block
**     entries...
**         0.  largest hash value for pgno
**         1.  pgno of subblock
**
** Contents block:  (The first page in the file)
**     0.   BLOCK_MAGIC | BLOCK_CONTENTS
**     1.   overflow page list
**     2.   freelist
**     3.   number of tables
**     table root page numbers...
*/




/*
** Byte swapping code.
*/
#ifdef BIG_ENDIAN
# SWB(x) (x)
#else
# SWB(x) sqliteDbSwapBytes(x)
................................................................................
  d[1] = s[2];
  d[2] = s[1];
  d[3] = s[0];
  return r;
}

#endif

































































































































































































































































/*
** Allocate space for the content table in the given Db structure.
** return SQLITE_OK on success and SQLITE_NOMEM if it fails.
*/
static int sqliteDbExpandContent(Db *pDb, int newSize){
  if( pDb->nAlloc>=newSize ) return SQLITE_OK;
................................................................................
  aPage[1] = pDb->aContent[0];
  memset(&aPage[2], 0, SQLITE_PAGE_SIZE - 2*sizeof(u32));
  pDb->aContent[0] = SWB(pgno);
  sqlitePgTouch(aPage);
  sqlitePgUnref(aPage);
}

/*
** Write data into overflow pages.  The first overflow page is
** provided in the second argument.  If additional pages are
** needed, they must be allocated.
*/
static int sqliteDbWriteOvfl(Db *pDb, u32 *aPage, int nData, const void *pData){
  while( nData>0 ){
    int toWrite, rc;
    u32 *nxPage, nxPgno;
    if( nData > SQLITE_PAGE_SIZE - 2*sizeof(u32) ){
      toWrite = SQLITE_PAGE_SIZE - 2*sizeof(u32);
    }else{
      toWrite = nData;
    }
    memcpy(&aPage[2], pData, toWrite);
    nData -= toWrite;
    pData = &((char*)pData)[toWrite];
    if( nData<=0 ) break;
    rc = sqliteDbAllocPage(pDb, &nxPgno, &nxPage);
    if( rc!=SQLITE_OK ) return rc;  /* Be smarter here */
    aPage[1] = SWB(nxPgno);
    nxPage[0] = SWB(BLOCK_MAGIC|BLOCK_OVERFLOW);
    nxPage[1] = 0;
    sqlitePgTouch(aPage);
    sqlitePgUnref(aPage);
    aPage = nxPage;
  }
  return SQLITE_OK;
}

/*
** Open a database.
*/
int sqliteDbOpen(const char *filename, Db **ppDb){
  Db *pDb = 0;
  Pgr *pPgr = 0;
  u32 *aPage1;
................................................................................
  pDb->pCursor = 0;
  pDb->inTransaction = 0;
  sqlitePgCount(pDb->pPgr, &nPage);
  rc = sqlitePgGet(pDb->pPgr, 1, &aPage1);
  if( rc!=0 ) goto open_err;
  if( nPage==0 ){
    sqlitePgBeginTransaction(pDb->pPgr);
    aPage1[0] = SWB(BLOCK_MAGIC|BLOCK_CONTENT);

    sqlitePgTouch(aPage1);
    sqlitePgCommit(pDb->pPgr);
  }
  pDb->nContent = SWB(aPage1[3]) + 2;
  pDb->nAlloc = 0;
  rc = sqliteDbExpandContent(pDb, pDb->nContent);
  if( rc!=SQLITE_OK ) goto open_err;
  rc = sqliteDbReadOvfl(pDb, 1, aPage1, 0, pDb->nContent*sizeof(u32),
                        pDb->aContent);
  if( rc!=SQLITE_OK ) goto open_err;
  sqlitePgUnref(aPage1);

  *ppDb = pDb;
  return SQLITE_OK;

open_err:
  *ppDb = 0;
  if( pPgr ) sqlitePgClose(pPgr);
  if( pDb && pDb->aContent ) sqliteFree(pDb->aContent);
................................................................................
  return SQLITE_OK;
}

/*
** Commit changes to the database
*/ 
int sqliteDbCommit(Db *pDb){
  u32 *aPage;
  int rc;
  if( !pDb->inTransaction ){
    return SQLITE_OK;
  }
  rc = sqlitePgGet(pDb->pPgr, 1, &aPage);
  if( rc!=SQLITE_OK ) return rc;
  sqliteDbWriteOvfl(pDb, aPage, pDb->nContent*sizeof(u32), pDb->aContent);


  rc = sqlitePgCommit(pDb->pPgr);
  if( rc!=SQLITE_OK ) return rc;
  pDb->inTransaction = 0;
  return SQLITE_OK;
}

/*







|







 







|
|
|
<

<
>

>
|
|
|












|
|
|
|


>
>
>







 







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







 







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







 







|
>



|



<
|
<

>







 







|




|

|
>
>







17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
..
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
...
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
...
454
455
456
457
458
459
460






























461
462
463
464
465
466
467
...
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

495

496
497
498
499
500
501
502
503
504
...
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
** Boston, MA  02111-1307, USA.
**
** Author contact information:
**   drh@hwaci.com
**   http://www.hwaci.com/drh/
**
*************************************************************************
** $Id: db.c,v 1.3 2001/01/22 00:31:53 drh Exp $
*/
#include "sqliteInt.h"
#include "pg.h"

/*
** Everything we need to know about an open database
*/
................................................................................
** Leaf blocks:
**
**     0.   BLOCK_MAGIC | BLOCK_LEAF 
**     1.   number of table entries  (only used if a table root block)
**     entries....
**         0.  size of this entry (measured in u32's)
**         1.  hash
**         2.  keysize  (in bytes)
**         3.  datasize (in bytes)
**         4.  payload

**

** Payload area:
**
**     *   up to LOCAL_PAYLOAD bytes of data
**     *   10 page number of direct blocks
**     *   1 indirect block
**     *   1 double-indirect block
**
** Index block:
**
**     0.   BLOCK_MAGIC | BLOCK_INDEX
**     1.   number of table entries  (only used if a table root block)
**     2.   entries in this index block
**     entries...
**         0.  largest hash value for pgno
**         1.  pgno of subblock
**
** Contents block:  (The first page in the file)
**     0.   BLOCK_MAGIC | BLOCK_CONTENTS
**     1.   zero
**     2.   number of bytes of payload
**     3.   freelist
**     4... root pages numbers of tables
*/

#define U32_PER_PAGE  (SQLITE_PAGE_SIZE/sizeof(u32))
#deifne LOCAL_PAYLOAD  (SQLITE_PAGE_SIZE - 18*sizeof(u32))

/*
** Byte swapping code.
*/
#ifdef BIG_ENDIAN
# SWB(x) (x)
#else
# SWB(x) sqliteDbSwapBytes(x)
................................................................................
  d[1] = s[2];
  d[2] = s[1];
  d[3] = s[0];
  return r;
}

#endif

/*
** Return the number of bytes of payload storage required on the leaf
** node to hold the key and data given.  Overflow pages do not count.
** The argument is the total size of the payload.
*/
static int payloadLocalSize(int nTotal){
  int nLocal, i;
  if( nTotal<0 ) nTotal = 0;
  if( nTotal <= LOCAL_PAYLOAD ){
    /* All the data fits on the leaf page */
    return (nTotal + 3)/4;
  }
  nLocal = LOCAL_PAYLOAD;
  nTotal -= LOCAL_PAYLOAD;
  if( nTotal < 10*SQLITE_PAGE_SIZE ){
    return nLocal + ((nTotal+SQLITE_PAGE_SIZE-1)/SQLITE_PAGE_SIZE)*sizeof(u32);
  }
  nLocal += 10*sizeof(u32);
  nTotal -= 10*SQLITE_PAGE_SIZE;
  if( nTotal < U32_PER_PAGE*SQLITE_PAGE_SIZE ){
    return nLocal + sizeof(u32);
  }
  nLocal += sizeof(u32);
  nTotal -= U32_PER_PAGE*SQLITE_PAGE_SIZE;
  if( nTotal < U32_PER_PAGE*U32_PER_PAGE*SQLITE_PAGE_SIZE ){
    return nLocal + sizeof(u32);
  }
  return -1;  /* This payload will not fit. */
}

/*
** Read data from the payload area.
**
** aPage points directly at the beginning of the payload.  No bounds 
** checking is done on offset or amt -- it is assumed that the payload
** area is big enough to accomodate.
*/
static int payloadRead(Db *pDb, u32 *aPage, int offset, int amt, void *pBuf){
  int rc;
  int toread, more;

  assert( offset>=0 && amt>=0 );
  if( offset < LOCAL_PAYLOAD ){
    /* Data stored directly in the leaf block of the BTree */
    if( amt+offset>LOCAL_PAYLOAD ){
      toread = LOCAL_PAYLOAD - offset;
      more = 1;
    }else{
      toread = amt;
      more = 0;
    }
    memcpy(pBuf, &((char*)aPage)[offset], toread);
    if( !more ) return SQLITE_OK;
    pBuf = &((char*)pBuf)[toread];
    offset += toread;
    amt -= toread;
  }
  offset -= LOCAL_PAYLOAD;
  aPage += LOCAL_PAYLOAD/sizeof(aPage[0]);
  while( offset < 10*SQLITE_PAGE_SIZE ){
    /* Data stored in one of 10 direct pages */
    int iDir;
    char *aData;
    iDir = offset/SQLITE_PAGE_SIZE;
    base = offset - iDir*SQLITE_PAGE_SIZE;
    rc = sqlitePgGet(pDb->pPgr, aPage[iDir], &aData);
    if( rc!=SQLITE_OK ) return rc;
    if( amt+base > SQLITE_PAGE_SIZE ){
      toread = SQLITE_PAGE_SIZE - base;
      more = 1;
    }else{
      toread = amt;
      more = 0;
    }
    memcpy(pBuf, &aData[base], toread);
    sqlitePgUnref(aData);
    if( !more ) return SQLITE_OK;
    pBuf = &((char*)pBuf)[toread];
    amt -= toread;
    offset += toread;
  }
  offset -= 10*SQLITE_PAGE_SIZE;
  aPage += 10;
  if( offset < U32_PER_PAGE*SQLITE_PAGE_SIZE ){
    /* Data stored in an indirect page */ 
    u32 *indirPage;
    rc = sqlitePgGet(pDb->pPgr, aPage[0], &indirPage);
    if( rc!=SQLITE_OK ) return rc;
    while( amt>0 && offset < U32_PER_PAGE*SQLITE_PAGE_SIZE ){
      int idx, base;
      char *aData;
      idx = offset/SQLITE_PAGE_SIZE;
      base = offset - idx*SQLITE_PAGE_SIZE;
      rc = sqlitePgGet(pDb->pPgr, indirPage[idx], &aData);
      if( rc!=SQLITE_OK ) break;
      if( amt+base > SQLITE_PAGE_SIZE ){
        toread = SQLITE_PAGE_SIZE - base;
      }else{
        toread = amt;
      }
      memcpy(pBuf, &aData[base], toread);
      sqlitePgUnref(aData);
      pBuf = &((char*)pBuf)[toread];
      amt -= toread;
      offset += toread;
    }
    sqlitePgUnref(indirPage);
    if( rc!=SQLITE_OK ) return rc;
  }
  offset -= U32_PER_PAGE*SQLITE_PAGE_SIZE;
  aPage++;
  if( offset < U32_PER_PAGE*U32_PER_PAGE*SQLITE_PAGE_SIZE ){
    /* Data stored in a double-indirect page */
    u32 *dblIndirPage;
    rc = sqlitePgGet(pDb->pPgr, aPage[0], &dblIndirPage);
    if( rc!=SQLITE_OK ) return rc;
    while( amt>0 && offset < U32_PER_PAGE*U32_PER_PAGE*SQLITE_PAGE_SIZE ){
      int dblidx;
      u32 *indirPage;
      int basis;
      dblidx = offset/(U32_PER_PAGE*SQLITE_PAGE_SIZE);
      rc = sqlitePgGet(pDb->pPgr, dblIndirPage[dblidx], &indirPage);
      if( rc!=SQLITE_OK ) break;
      basis = dblidx*U32_PER_PAGE*SQLITE_PAGE_SIZE;
      while( amt>0 && offset < basis + U32_PER_PAGE*SQLITE_PAGE_SIZE ){
        int idx, base;
        char *aData;
        idx = (offset - basis)/SQLITE_PAGE_SIZE;
        base = (offset - basis) - idx*SQLITE_PAGE_SIZE;
        rc = sqlitePgGet(pDb->pPgr, indirPage[idx], &aData);
        if( rc!=SQLITE_OK ) break;
        if( amt+base > SQLITE_PAGE_SIZE ){
          toread = SQLITE_PAGE_SIZE - base;
        }else{
          toread = amt;
        }
        memcpy(pBuf, &aData[base], toread);
        sqlitePgUnref(aData);
        pBuf = &((char*)pBuf)[toread];
        amt -= toread;
        offset += toread;
      }
      sqlitePgUnref(indirPage);
      if( rc!=SQLITE_OK ) break;
    }
    sqlitePgUnref(dblIndirPage);
    return rc;
  }
  memset(pBuf, 0, amt);
  return SQLITE_OK;
}

/*
** Write data into the payload area.
**
** If pages have already been allocated for the payload, they are
** simply overwritten.  New pages are allocated as necessary to
** fill in gaps.  sqlitePgTouch() is called on all overflow pages,
** but the calling function must invoke sqlitePgTouch() for aPage
** itself.
*/
static int payloadWrite(Db *pDb, u32 *aPage, int offset, int amt, void *pBuf){
  assert( offset>=0 && amt>=0 );
  if( offset < LOCAL_PAYLOAD ){
    if( amt+offset>LOCAL_PAYLOAD ){
      towrite = LOCAL_PAYLOAD - offset;
      more = 1;
    }else{
      towrite = amt;
      more = 0;
    }
    memcpy(&((char*)aPage)[offset], pBuf, towrite);
    if( !more ) return SQLITE_OK;
    pBuf = &((char*)pBuf)[towrite];
    offset += toread;
    amt -= toread;
  }
  offset -= LOCAL_PAYLOAD;
  aPage += LOCAL_PAYLOAD/sizeof(aPage[0]);
  while( offset < 10*SQLITE_PAGE_SIZE ){
    int iDir;
    char *aData;
    iDir = offset/SQLITE_PAGE_SIZE;
    base = offset - iDir*SQLITE_PAGE_SIZE;
    if( aPage[iDir] ){
      rc = sqliteGet(pDb->pPgr, aPage[iDir], &aData);
    }else{
      rc = sqliteDbAllocPage(pDb, &aPage[iDir], &aData);
    }
    if( rc!=SQLITE_OK ) return rc;
    if( amt+base > SQLITE_PAGE_SIZE ){
      towrite = SQLITE_PAGE_SIZE - base;
      more = 1;
    }else{
      towrite = amt;
      more = 0;
    }
    memcpy(&aData[base], pBuf, towrite);
    sqlitePgUnref(aData);
    if( !more ) return SQLITE_OK;
    pBuf = &((char*)pBuf)[towrite];
    amt -= towrite;
    offset += towrite;
  }
  /* TBD.... */
}

/*
** Release any and all overflow pages associated with data starting
** with byte "newSize" up to but not including "oldSize".
*/
static int payloadFree(Db *pDb, u32 *aPage, int newSize, int oldSize){
  int i;

  if( newSize>=oldSize ) return;
  oldSize -= LOCAL_PAYLOAD;
  if( oldSize<=0 ) return SQLITE_OK;
  newSize -= LOCAL_PAYLOAD;
  if( newSize<0 ) newSize = 0;
  aPage += LOCAL_PAYLOAD/sizeof(u32);
*************
  for(i=0; i<10; i++){
    sqliteDbFreePage(pDb, aPage[0], 0);
    amt -= SQLITE_PAGE_SIZE;
    if( amt<=0 ) return SQLITE_OK;
    aPage++;
  }
  rc = sqlitePgGet(pDb->pPgr, aPage[0], &indirPage);
  if( rc!=SQLITE_OK ) return rc;
  for(i=0; i<U32_PER_PAGE; i++){
    if( indirPage[i]==0 ) continue;
    sqliteDbFreePage(pDb, indirPage[i], 0);
  }
  sqliteDbFreePage(pDb, aPage[0], indirPage);
  sqlitePgUnref(indirPage);
  amt -= U32_PER_PAGE*SQLITE_PAGE_SIZE;
  if( amt<=0 ) return SQLITE_OK;
  aPage++;
  rc = sqlitePgGet(pDb->pPgr, aPage[0], &dblIndirPage);
  if( rc!=SQLITE_OK ) return rc;
  for(i=0; i<U32_PER_PAGE; i++){
    if( dblIndirPage[i]==0 ) continue;
    rc = sqlitePgGet(pDb->pPgr, dblIndirPage[i], &indirPage);
    if( rc!=SQLITE_OK ) break;
    for(j=0; j<U32_PER_PAGE; j++){
      if( indirPage[j]==0 ) continue;
      sqliteDbFreePage(pDb, dblIndirPage[i], 0);
    }
    sqliteDbFreePage(pDb, dblIndirPage[i], indirPage);
    sqlitePgUnder(indirPage);
  }
  sqliteDbFreePage(pDb, aPage[0], dblIndirPage);
  sqlitePgUnref(dblIndirPage);
  return SQLITE_OK;    
}

/*
** Allocate space for the content table in the given Db structure.
** return SQLITE_OK on success and SQLITE_NOMEM if it fails.
*/
static int sqliteDbExpandContent(Db *pDb, int newSize){
  if( pDb->nAlloc>=newSize ) return SQLITE_OK;
................................................................................
  aPage[1] = pDb->aContent[0];
  memset(&aPage[2], 0, SQLITE_PAGE_SIZE - 2*sizeof(u32));
  pDb->aContent[0] = SWB(pgno);
  sqlitePgTouch(aPage);
  sqlitePgUnref(aPage);
}































/*
** Open a database.
*/
int sqliteDbOpen(const char *filename, Db **ppDb){
  Db *pDb = 0;
  Pgr *pPgr = 0;
  u32 *aPage1;
................................................................................
  pDb->pCursor = 0;
  pDb->inTransaction = 0;
  sqlitePgCount(pDb->pPgr, &nPage);
  rc = sqlitePgGet(pDb->pPgr, 1, &aPage1);
  if( rc!=0 ) goto open_err;
  if( nPage==0 ){
    sqlitePgBeginTransaction(pDb->pPgr);
    aPage1[0] = BLOCK_MAGIC|BLOCK_CONTENT;
    aPage1[2] = sizeof(u32)*10;
    sqlitePgTouch(aPage1);
    sqlitePgCommit(pDb->pPgr);
  }
  pDb->nContent = aPage1[2]/sizeof(u32);
  pDb->nAlloc = 0;
  rc = sqliteDbExpandContent(pDb, pDb->nContent);
  if( rc!=SQLITE_OK ) goto open_err;

  rc = payloadRead(pDb, &aPage1[3], 0, aPage[2], pDb->aContent);

  sqlitePgUnref(aPage1);
  if( rc!=SQLITE_OK ) goto open_err;
  *ppDb = pDb;
  return SQLITE_OK;

open_err:
  *ppDb = 0;
  if( pPgr ) sqlitePgClose(pPgr);
  if( pDb && pDb->aContent ) sqliteFree(pDb->aContent);
................................................................................
  return SQLITE_OK;
}

/*
** Commit changes to the database
*/ 
int sqliteDbCommit(Db *pDb){
  u32 *aPage1;
  int rc;
  if( !pDb->inTransaction ){
    return SQLITE_OK;
  }
  rc = sqlitePgGet(pDb->pPgr, 1, &aPage1);
  if( rc!=SQLITE_OK ) return rc;
  aPage1[2] = pDb->nContent*sizeof(u32);
  payloadWrite(pDb, 0, aPage1[2], pDb->aContent);
  sqlitePgUnref(aPage1);
  rc = sqlitePgCommit(pDb->pPgr);
  if( rc!=SQLITE_OK ) return rc;
  pDb->inTransaction = 0;
  return SQLITE_OK;
}

/*

Changes to test/tester.tcl.

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...
135
136
137
138
139
140
141

142
143
144
145
146
147
148
#   drh@hwaci.com
#   http://www.hwaci.com/drh/
#
#***********************************************************************
# This file implements some common TCL routines used for regression
# testing the SQLite library
#
# $Id: tester.tcl,v 1.8 2000/12/10 18:23:52 drh Exp $

# Create a test database
#
if {![info exists dbprefix]} {
  if {[info exists env(SQLITE_PREFIX)]} {
    set dbprefix $env(SQLITE_PREFIX):
  } else {
................................................................................
  puts "$nErr errors out of $nTest tests"
  exit $nErr
}

# A procedure to execute SQL
#
proc execsql {sql} {

  return [db eval $sql]
}

# Another procedure to execute SQL.  This one includes the field
# names in the returned list.
#
proc execsql2 {sql} {







|







 







>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
...
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#   drh@hwaci.com
#   http://www.hwaci.com/drh/
#
#***********************************************************************
# This file implements some common TCL routines used for regression
# testing the SQLite library
#
# $Id: tester.tcl,v 1.9 2001/01/22 00:31:53 drh Exp $

# Create a test database
#
if {![info exists dbprefix]} {
  if {[info exists env(SQLITE_PREFIX)]} {
    set dbprefix $env(SQLITE_PREFIX):
  } else {
................................................................................
  puts "$nErr errors out of $nTest tests"
  exit $nErr
}

# A procedure to execute SQL
#
proc execsql {sql} {
  # puts "SQL = $sql"
  return [db eval $sql]
}

# Another procedure to execute SQL.  This one includes the field
# names in the returned list.
#
proc execsql2 {sql} {