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

Overview
Comment:Progress on reading and writing frames from and to the log file.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5ba2ff07017597fb490536ae1a0f879fafd41f79
User & Date: dan 2013-10-23 19:31:03.551
Context
2013-10-23
20:28
Add connect/disconnect locking. And locking primitives for single-process mode. check-in: f7fc6aeec8 user: dan tags: trunk
19:31
Progress on reading and writing frames from and to the log file. check-in: 5ba2ff0701 user: dan tags: trunk
2013-10-22
19:04
Add "PRAGMA bt_page_dump" to access a specific bt xControl() command. This is probably a temporary solution. check-in: d00d7c08d1 user: dan tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/bt_lock.c.
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    sqlite4_mutex_enter(pShared->pClientMutex);
    p->pNext = pShared->pLock;
    pShared->pLock = p;
    sqlite4_mutex_leave(pShared->pClientMutex);
    p->pShared = pShared;
  }

  if( rc==SQLITE4_OK ){
    rc = xRecover(p);
  }

  return rc;
}

/*







|







131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    sqlite4_mutex_enter(pShared->pClientMutex);
    p->pNext = pShared->pLock;
    pShared->pLock = p;
    sqlite4_mutex_leave(pShared->pClientMutex);
    p->pShared = pShared;
  }

  if( rc==SQLITE4_OK && pShared->nRef==1 ){
    rc = xRecover(p);
  }

  return rc;
}

/*
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
  for(pp=&p->pShared->pLock; *pp!=p; pp=&(*pp)->pNext);
  *pp = (*pp)->pNext;
  sqlite4_mutex_leave(pShared->pClientMutex);

  btLockMutexEnter();
  pShared->nRef--;
  if( pShared->nRef==0 ){

    BtShared **ppS;
    for(ppS=&gShared.pDatabase; *ppS!=pShared; ppS=&(*ppS)->pNext);
    *ppS = (*ppS)->pNext;

    sqlite4_mutex_free(pShared->pClientMutex);




    sqlite4_free(p->pEnv, pShared);
  }
  btLockMutexLeave();
  return rc;
}

int sqlite4BtLockCheckpoint(BtLock *p, int (*xCkpt)(BtLock*)){
  return xCkpt(p);
}

int sqlite4BtLockBegin(BtLock *p, int eLock){
  return SQLITE4_OK;
}

int sqlite4BtLockEnd(BtLock *p, int eLock){
  return SQLITE4_OK;
}


/* Obtain a READER lock. 
**
** Argument aLog points to an array of 6 frame addresses. These are the 
** first and last frames in each of log regions A, B and C. Argument 
** aLock points to the array of read-lock slots in shared memory.
*/
int sqlite4BtLockReader(BtLock *pLock, u32 *aLog, u32 *aLock){








  return SQLITE4_OK;
}

int sqlite4BtLockShmMap(BtLock *pLock, int iChunk, int nByte, u8 **ppOut){
  int rc = SQLITE4_OK;
  BtShared *pShared = pLock->pShared;
  u8 *pOut = 0;







>





>
>
>
>


















>
|






>
>
>
>
>
>
>
>







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
  for(pp=&p->pShared->pLock; *pp!=p; pp=&(*pp)->pNext);
  *pp = (*pp)->pNext;
  sqlite4_mutex_leave(pShared->pClientMutex);

  btLockMutexEnter();
  pShared->nRef--;
  if( pShared->nRef==0 ){
    int i;
    BtShared **ppS;
    for(ppS=&gShared.pDatabase; *ppS!=pShared; ppS=&(*ppS)->pNext);
    *ppS = (*ppS)->pNext;

    sqlite4_mutex_free(pShared->pClientMutex);
    for(i=0; i<pShared->nShmChunk; i++){
      sqlite4_free(p->pEnv, pShared->apShmChunk[i]);
    }
    sqlite4_free(p->pEnv, pShared->apShmChunk);
    sqlite4_free(p->pEnv, pShared);
  }
  btLockMutexLeave();
  return rc;
}

int sqlite4BtLockCheckpoint(BtLock *p, int (*xCkpt)(BtLock*)){
  return xCkpt(p);
}

int sqlite4BtLockBegin(BtLock *p, int eLock){
  return SQLITE4_OK;
}

int sqlite4BtLockEnd(BtLock *p, int eLock){
  return SQLITE4_OK;
}

/* 
** Obtain a READER lock. 
**
** Argument aLog points to an array of 6 frame addresses. These are the 
** first and last frames in each of log regions A, B and C. Argument 
** aLock points to the array of read-lock slots in shared memory.
*/
int sqlite4BtLockReader(BtLock *pLock, u32 *aLog, u32 *aLock){
  /* todo... */
  return SQLITE4_OK;
}

/*
** Release the READER lock currently held by connection pLock.
*/
int sqlite4BtLockReaderUnlock(BtLock *pLock){
  return SQLITE4_OK;
}

int sqlite4BtLockShmMap(BtLock *pLock, int iChunk, int nByte, u8 **ppOut){
  int rc = SQLITE4_OK;
  BtShared *pShared = pLock->pShared;
  u8 *pOut = 0;
232
233
234
235
236
237
238


239
240
241
242
243
244
245
246
247
248
249
250
251
252
      pShared->nShmChunk = nNew;
      pShared->apShmChunk = apNew;
    }
  }

  if( rc==SQLITE4_OK ){
    if( pShared->apShmChunk[iChunk]==0 ){


      pShared->apShmChunk[iChunk] = (u8*)sqlite4_malloc(pLock->pEnv, nByte);
    }
    pOut = pShared->apShmChunk[iChunk];
    if( pOut==0 ){
      rc = btErrorBkpt(SQLITE4_NOMEM);
    }
  }
  sqlite4_mutex_leave(pShared->pClientMutex);
  
  *ppOut = pOut;
  return rc;
}









>
>
|












<
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267

      pShared->nShmChunk = nNew;
      pShared->apShmChunk = apNew;
    }
  }

  if( rc==SQLITE4_OK ){
    if( pShared->apShmChunk[iChunk]==0 ){
      u8 *p = (u8*)sqlite4_malloc(pLock->pEnv, nByte);
      if( p ) memset(p, 0, nByte);
      pShared->apShmChunk[iChunk] = p;
    }
    pOut = pShared->apShmChunk[iChunk];
    if( pOut==0 ){
      rc = btErrorBkpt(SQLITE4_NOMEM);
    }
  }
  sqlite4_mutex_leave(pShared->pClientMutex);
  
  *ppOut = pOut;
  return rc;
}


Changes to src/bt_log.c.
60
61
62
63
64
65
66

67
68
69
70
71
72
73
** Shared memory header. Shared memory begins with two copies of
** this structure. All fields are stored in machine byte-order.
*/
struct BtShmHdr {
  u32 aLog[6];                    /* First/last frames for each log region */
  int nSector;                    /* Sector size assumed for WAL file */
  u32 aFrameCksum[2];             /* Checksum of previous frame */

  u32 aCksum[2];                  /* Object checksum */
};

/*
** A single instance of this structure follows the two BtShmHdr structures 
** in shared memory.
*/







>







60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
** Shared memory header. Shared memory begins with two copies of
** this structure. All fields are stored in machine byte-order.
*/
struct BtShmHdr {
  u32 aLog[6];                    /* First/last frames for each log region */
  int nSector;                    /* Sector size assumed for WAL file */
  u32 aFrameCksum[2];             /* Checksum of previous frame */
  int padding;
  u32 aCksum[2];                  /* Object checksum */
};

/*
** A single instance of this structure follows the two BtShmHdr structures 
** in shared memory.
*/
180
181
182
183
184
185
186























































187
188
189
190
191
192
193
      aData += 2;
    }while( aData<aEnd );
  }

  aOut[0] = s1;
  aOut[1] = s2;
}
























































/*
** Open the log file for pager pPager. If successful, return the BtLog* 
** handle via output variable *ppLog. If parameter bRecover is true, then
** also run database recovery before returning. In this case, the caller
** has already obtained the required locks.
*/







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







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
      aData += 2;
    }while( aData<aEnd );
  }

  aOut[0] = s1;
  aOut[1] = s2;
}

/*
** Ensure that shared-memory chunk iChunk is mapped and available in
** the BtLog.apShm[] array. If an error occurs, return an SQLite4 error
** code. Otherwise, SQLITE4_OK.
*/
static int btLogMapShm(BtLog *pLog, int iChunk){
  int rc = SQLITE4_OK;

  if( pLog->nShm<=iChunk ){
    sqlite4_env *pEnv = pLog->pLock->pEnv;
    u8 **apNew;
    int nNew = iChunk+1;

    apNew = (u8**)sqlite4_realloc(pEnv, pLog->apShm, sizeof(u8*)*nNew);
    if( apNew==0 ) return btErrorBkpt(SQLITE4_NOMEM);
    memset(&apNew[pLog->nShm], 0, (nNew-pLog->nShm) * sizeof(u8*));
    pLog->nShm = nNew;
    pLog->apShm = apNew;
  }

  if( pLog->apShm[iChunk]==0 ){
    u8 **pp = &pLog->apShm[iChunk];
    rc = sqlite4BtLockShmMap(pLog->pLock, iChunk, BT_SHM_CHUNK_SIZE, pp);
  }

  return rc;
}

static BtShm *btLogShm(BtLog *pLog){
  return (BtShm*)(pLog->apShm[0]);
}

static int btLogUpdateSharedHdr(BtLog *pLog){
  bt_env *pVfs = pLog->pLock->pVfs;
  BtShmHdr *p = &pLog->snapshot;
  BtShm *pShm = btLogShm(pLog);

  /* Calculate a checksum for the private snapshot object. */
  btLogChecksum(1, (u8*)p, offsetof(BtShmHdr, aCksum), 0, p->aCksum);

  /* Update the shared object. */
  pVfs->xShmBarrier(pLog->pFd);
  memcpy(&pShm->hdr1, p, sizeof(BtShmHdr));
  pVfs->xShmBarrier(pLog->pFd);
  memcpy(&pShm->hdr2, p, sizeof(BtShmHdr));

  return SQLITE4_OK;
}

static void btLogZeroSnapshot(BtLog *pLog){
  bt_env *pVfs = pLog->pLock->pVfs;
  memset(&pLog->snapshot, 0, sizeof(BtShmHdr));
  pLog->snapshot.nSector = pVfs->xSectorSize(pLog->pFd);
}

/*
** Open the log file for pager pPager. If successful, return the BtLog* 
** handle via output variable *ppLog. If parameter bRecover is true, then
** also run database recovery before returning. In this case, the caller
** has already obtained the required locks.
*/
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
  memset(pLog, 0, sizeof(BtLog));
  pLog->pLock = (BtLock*)pPager;

  zWal = sqlite4BtPagerFilename(pPager, BT_PAGERFILE_LOG);
  rc = pVfs->xOpen(pEnv, pVfs, zWal, 0, &pLog->pFd);

  if( rc==SQLITE4_OK && bRecover ){
    /* TODO: Run recovery... */






  }

 open_out:
  if( rc!=SQLITE4_OK ){
    sqlite4_free(pEnv, pLog);
    pLog = 0;
  }
  *ppLog = pLog;
  return rc;
}

/*
** Close the log file handle BtLog*. 
*/
int sqlite4BtLogClose(BtLog *pLog, int bCleanup){
  int rc = SQLITE4_OK;

  sqlite4_env *pEnv = pLog->pLock->pEnv;
  bt_env *pVfs = pLog->pLock->pVfs;

  pVfs->xClose(pLog->pFd);
  sqlite4_free(pEnv, pLog);

  if( bCleanup ){
    BtPager *pPager = (BtPager*)pLog->pLock;
    const char *zWal = sqlite4BtPagerFilename(pPager, BT_PAGERFILE_LOG);
    rc = pVfs->xUnlink(pEnv, pVfs, zWal);




  }

  return rc;
}

/*
** Attempt to read data for page pgno from the log file. If successful,
** the data is written into buffer aData[] (which must be at least as
** large as a database page). In this case SQLITE4_OK is returned.
**
** If the log does not contain any version of page pgno, SQLITE4_NOTFOUND
** is returned and the contents of buffer aData[] are not modified.
**
** If any other error occurs, an SQLite4 error code is returned. The final
** state of buffer aData[] is undefined in this case.
*/
int sqlite4BtLogRead(BtLog *pLog, u32 pgno, u8 *aData){
  return SQLITE4_OK;
}

/*
** Return true if log is completely empty (as it is if a file zero bytes
** in size has been opened or created).
*/
static int btLogIsEmpty(BtLog *pLog){
  return (pLog->snapshot.aLog[4]==0);
}







|
>
>
>
>
>
>
















>
|
|

|
<
<
|
|
|
|
>
>
>
>





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







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
  memset(pLog, 0, sizeof(BtLog));
  pLog->pLock = (BtLock*)pPager;

  zWal = sqlite4BtPagerFilename(pPager, BT_PAGERFILE_LOG);
  rc = pVfs->xOpen(pEnv, pVfs, zWal, 0, &pLog->pFd);

  if( rc==SQLITE4_OK && bRecover ){
    /* TODO: Run real recovery... */

    rc = btLogMapShm(pLog, 0);
    if( rc==SQLITE4_OK ){
      btLogZeroSnapshot(pLog);
      rc = btLogUpdateSharedHdr(pLog);
    }
  }

 open_out:
  if( rc!=SQLITE4_OK ){
    sqlite4_free(pEnv, pLog);
    pLog = 0;
  }
  *ppLog = pLog;
  return rc;
}

/*
** Close the log file handle BtLog*. 
*/
int sqlite4BtLogClose(BtLog *pLog, int bCleanup){
  int rc = SQLITE4_OK;
  if( pLog ){
    sqlite4_env *pEnv = pLog->pLock->pEnv;
    bt_env *pVfs = pLog->pLock->pVfs;

    pVfs->xClose(pLog->pFd);


    if( bCleanup ){
      BtPager *pPager = (BtPager*)pLog->pLock;
      const char *zWal = sqlite4BtPagerFilename(pPager, BT_PAGERFILE_LOG);
      rc = pVfs->xUnlink(pEnv, pVfs, zWal);
    }

    sqlite4_free(pEnv, pLog->apShm);
    sqlite4_free(pEnv, pLog);
  }

  return rc;
}
















/*
** Return true if log is completely empty (as it is if a file zero bytes
** in size has been opened or created).
*/
static int btLogIsEmpty(BtLog *pLog){
  return (pLog->snapshot.aLog[4]==0);
}
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
  /* Calculate a checksum for the header */
  btLogChecksum(1, (u8*)pHdr, offsetof(BtWalHdr, aCksum), 0, pHdr->aCksum);

  /* Write the object to disk */
  iOff = iHdr * pLog->snapshot.nSector;
  rc = btLogWriteData(pLog, iOff, (u8*)pHdr, sizeof(BtWalHdr));

  return rc;
}

/*
** Ensure that shared-memory chunk iChunk is mapped and available in
** the BtLog.apShm[] array. If an error occurs, return an SQLite4 error
** code. Otherwise, SQLITE4_OK.
*/
static int btLogMapShm(BtLog *pLog, int iChunk){
  int rc = SQLITE4_OK;

  if( pLog->nShm<=iChunk ){
    sqlite4_env *pEnv = pLog->pLock->pEnv;
    u8 **apNew;
    int nNew = iChunk+1;

    apNew = (u8**)sqlite4_malloc(pEnv, sizeof(u8*)*nNew);
    if( apNew==0 ) return btErrorBkpt(SQLITE4_NOMEM);
    memcpy(apNew, pLog->apShm, sizeof(u8*)*pLog->nShm);
    memset(&apNew[pLog->nShm], 0, (nNew-pLog->nShm) * sizeof(u8*));
    pLog->nShm = nNew;
    pLog->apShm = apNew;
  }

  if( pLog->apShm[iChunk]==0 ){
    u8 **pp = &pLog->apShm[iChunk];
    rc = sqlite4BtLockShmMap(pLog->pLock, iChunk, BT_SHM_CHUNK_SIZE, pp);
  }

  return rc;
}

/*
** Locate the iHash'th hash table in shared memory. Return it.
*/
static int btLogFindHash(







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







329
330
331
332
333
334
335





























336
337
338
339
340
341
342
  /* Calculate a checksum for the header */
  btLogChecksum(1, (u8*)pHdr, offsetof(BtWalHdr, aCksum), 0, pHdr->aCksum);

  /* Write the object to disk */
  iOff = iHdr * pLog->snapshot.nSector;
  rc = btLogWriteData(pLog, iOff, (u8*)pHdr, sizeof(BtWalHdr));






























  return rc;
}

/*
** Locate the iHash'th hash table in shared memory. Return it.
*/
static int btLogFindHash(
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
    *paPgno = aPgno;
    *piZero = iZero;
  }

  return rc;
}

static BtShm *btLogShm(BtLog *pLog){
  return (BtShm*)(pLog->apShm[0]);
}

/*
** Return the index of the hash table that contains the entry for frame
** iFrame. 
*/
static int btLogFrameHash(BtLog *pLog, u32 iFrame){
  if( iFrame<=HASHTABLE_NFRAME_ONE ) return 0;
  return 1 + ((iFrame - HASHTABLE_NFRAME_ONE - 1) / HASHTABLE_NFRAME);
}

/*
** Return a hash key for page number pgno.
*/
static int btLogHashKey(BtLog *pLog, u32 pgno){
  assert( pgno>=1 );
  return ((pgno * HASHTABLE_KEY_MUL) % HASHTABLE_NSLOT);
}

static int btLogHashNext(BtLog *pLog, int iSlot){
  return ((iSlot + 1) % HASHTABLE_NSLOT);
}














































































/*
** Add an entry mapping database page pgno to log frame iFrame to the
** the shared hash table. Return SQLITE4_OK if successful, or an SQLite4
** error code if an error occurs.
*/
static int btLogHashInsert(BtLog *pLog, u32 pgno, u32 iFrame){







<
<
<
<
















>



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







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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
    *paPgno = aPgno;
    *piZero = iZero;
  }

  return rc;
}





/*
** Return the index of the hash table that contains the entry for frame
** iFrame. 
*/
static int btLogFrameHash(BtLog *pLog, u32 iFrame){
  if( iFrame<=HASHTABLE_NFRAME_ONE ) return 0;
  return 1 + ((iFrame - HASHTABLE_NFRAME_ONE - 1) / HASHTABLE_NFRAME);
}

/*
** Return a hash key for page number pgno.
*/
static int btLogHashKey(BtLog *pLog, u32 pgno){
  assert( pgno>=1 );
  return ((pgno * HASHTABLE_KEY_MUL) % HASHTABLE_NSLOT);
}

static int btLogHashNext(BtLog *pLog, int iSlot){
  return ((iSlot + 1) % HASHTABLE_NSLOT);
}

static int btLogHashSearch(BtLog *pLog, int iHash, u32 pgno, u32 *piFrame){
  ht_slot *aHash;
  u32 *aPgno;
  u32 iZero;
  int rc;

  rc = btLogFindHash(pLog, iHash, &aHash, &aPgno, &iZero);
  if( rc==SQLITE4_OK ){
    int nCollide = HASHTABLE_NSLOT*2;
    int iSlot;
    u32 iFrame = 0;
    
    iSlot=btLogHashKey(pLog, pgno); 
    for( ; aHash[iSlot]; iSlot=btLogHashNext(pLog, iSlot)){
      if( aPgno[aHash[iSlot]-1]==pgno ){
        iFrame = iZero + aHash[iSlot] - 1;
      }
      if( (nCollide--)==0 ) return btErrorBkpt(SQLITE4_CORRUPT);
    }

    *piFrame = iFrame;
    if( iFrame==0 ){
      rc = SQLITE4_NOTFOUND;
    }
  }

  return rc;
}


/*
** Return the offset of frame iFrame within the log file.
*/
static i64 btLogFrameOffset(BtLog *pLog, int pgsz, u32 iFrame){
  return 
      (i64)pLog->snapshot.nSector*2 
    + (i64)(iFrame-1) * (i64)(pgsz + sizeof(BtFrameHdr));
}

/*
** Attempt to read data for page pgno from the log file. If successful,
** the data is written into buffer aData[] (which must be at least as
** large as a database page). In this case SQLITE4_OK is returned.
**
** If the log does not contain any version of page pgno, SQLITE4_NOTFOUND
** is returned and the contents of buffer aData[] are not modified.
**
** If any other error occurs, an SQLite4 error code is returned. The final
** state of buffer aData[] is undefined in this case.
*/
int sqlite4BtLogRead(BtLog *pLog, u32 pgno, u8 *aData){
  const int pgsz = sqlite4BtPagerPagesize((BtPager*)(pLog->pLock));
  int iHash;
  int rc = SQLITE4_NOTFOUND;
  u32 iFrame = 0;

  iHash = btLogFrameHash(pLog, pLog->snapshot.aLog[5]);
  for( ; rc==SQLITE4_NOTFOUND && iHash>=0 && iFrame==0; iHash--){
    rc = btLogHashSearch(pLog, iHash, pgno, &iFrame);
  }

  if( rc==SQLITE4_OK && iFrame!=0 ){
    bt_env *pVfs = pLog->pLock->pVfs;
    i64 iOff;
    assert( rc==SQLITE4_OK );
    iOff = btLogFrameOffset(pLog, pgsz, iFrame);
    rc = pVfs->xRead(pLog->pFd, iOff + sizeof(BtFrameHdr), aData, pgsz);

#if 0
    fprintf(stderr, "read page %d from offset %d\n", (int)pgno, (int)iOff);
    fflush(stderr);
#endif
  }

  return rc;
}

/*
** Add an entry mapping database page pgno to log frame iFrame to the
** the shared hash table. Return SQLITE4_OK if successful, or an SQLite4
** error code if an error occurs.
*/
static int btLogHashInsert(BtLog *pLog, u32 pgno, u32 iFrame){
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
      if( (nCollide--)==0 ) return btErrorBkpt(SQLITE4_CORRUPT);
    }
  }

  return rc;
}

static int btLogUpdateSharedHdr(BtLog *pLog){
  bt_env *pVfs = pLog->pLock->pVfs;
  BtShmHdr *p = &pLog->snapshot;
  BtShm *pShm = btLogShm(pLog);

  /* Calculate a checksum for the private snapshot object. */
  btLogChecksum(1, (u8*)p, offsetof(BtShmHdr, aCksum), 0, p->aCksum);

  /* Update the shared object. */
  pVfs->xShmBarrier(pLog->pFd);
  memcpy(&pShm->hdr1, p, sizeof(BtShmHdr));
  pVfs->xShmBarrier(pLog->pFd);
  memcpy(&pShm->hdr2, p, sizeof(BtShmHdr));

  return SQLITE4_OK;
}

/*
** Return the offset of frame iFrame within the log file.
*/
static i64 btLogFrameOffset(BtLog *pLog, int pgsz, u32 iFrame){
  return (i64)pLog->snapshot.nSector*2 + (i64)(iFrame-1) * (i64)pgsz;
}

/*
** Write a frame to the log file.
*/
int sqlite4BtLogWrite(BtLog *pLog, u32 pgno, u8 *aData, int bCommit){
  const int pgsz = sqlite4BtPagerPagesize((BtPager*)(pLog->pLock));
  int rc = SQLITE4_OK;
  u32 iFrame;                     /* Write this frame (numbered from 1) */







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







500
501
502
503
504
505
506
























507
508
509
510
511
512
513
      if( (nCollide--)==0 ) return btErrorBkpt(SQLITE4_CORRUPT);
    }
  }

  return rc;
}

























/*
** Write a frame to the log file.
*/
int sqlite4BtLogWrite(BtLog *pLog, u32 pgno, u8 *aData, int bCommit){
  const int pgsz = sqlite4BtPagerPagesize((BtPager*)(pLog->pLock));
  int rc = SQLITE4_OK;
  u32 iFrame;                     /* Write this frame (numbered from 1) */
468
469
470
471
472
473
474






475
476
477
478
479
480
481
  /* Populate the frame header object. */
  memset(&frame, 0, sizeof(frame));
  frame.pgno = pgno;
  frame.ctrl = (bCommit ? 0x80000000 : 0x00000000);
  a = pLog->snapshot.aFrameCksum;
  btLogChecksum(1, (u8*)&frame, offsetof(BtFrameHdr, aCksum), a, frame.aCksum);
  btLogChecksum(1, aData, pgsz, frame.aCksum, frame.aCksum);







  /* Write the header and page record to the log file. */
  rc = btLogWriteData(pLog, iOff, (u8*)&frame, sizeof(frame));
  if( rc==SQLITE4_OK ){
    rc = btLogWriteData(pLog, iOff+sizeof(frame), aData, pgsz);
  }








>
>
>
>
>
>







539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
  /* Populate the frame header object. */
  memset(&frame, 0, sizeof(frame));
  frame.pgno = pgno;
  frame.ctrl = (bCommit ? 0x80000000 : 0x00000000);
  a = pLog->snapshot.aFrameCksum;
  btLogChecksum(1, (u8*)&frame, offsetof(BtFrameHdr, aCksum), a, frame.aCksum);
  btLogChecksum(1, aData, pgsz, frame.aCksum, frame.aCksum);

#if 0
fprintf(stderr, "writing page %d at log offset %d (frame %d)\n", (int)pgno, (int)iOff, (int)iFrame);
fflush(stderr);
#endif


  /* Write the header and page record to the log file. */
  rc = btLogWriteData(pLog, iOff, (u8*)&frame, sizeof(frame));
  if( rc==SQLITE4_OK ){
    rc = btLogWriteData(pLog, iOff+sizeof(frame), aData, pgsz);
  }

497
498
499
500
501
502
503
504































505
























506
507
508
509

510
511
512
513
514
515
516
517

518
519
520
  /* If this is a COMMIT, also update the shared shm-header. */
  if( bCommit ){
    rc = btLogUpdateSharedHdr(pLog);
  }

  return rc;
}
































int sqlite4BtLogSnapshotOpen(BtLog *pLog){
























  return SQLITE4_OK;
}

int sqlite4BtLogSnapshotClose(BtLog *pLog){

  return SQLITE4_OK;
}

int sqlite4BtLogSnapshotWritable(BtLog *pLog){
  return 1;
}

int sqlite4BtLogCheckpoint(BtLog *pLog){

}










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

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|



>








>



574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
  /* If this is a COMMIT, also update the shared shm-header. */
  if( bCommit ){
    rc = btLogUpdateSharedHdr(pLog);
  }

  return rc;
}

/*
** Return true if the checksum in BtShmHdr.aCksum[] matches the rest
** of the object.
*/
static int btLogChecksumOk(BtShmHdr *pHdr){
  u32 aCksum[2];
  btLogChecksum(1, (u8*)pHdr, offsetof(BtShmHdr, aCksum), 0, aCksum);
  return (aCksum[0]==pHdr->aCksum[0] && aCksum[1]==pHdr->aCksum[1]);
}

static int btLogSnapshot(BtLog *pLog, BtShmHdr *pHdr){
  int rc;

  rc = btLogMapShm(pLog, 0);
  if( rc==SQLITE4_OK ){
    BtShm *pShm = btLogShm(pLog);
    int nAttempt = 500;

    while( (nAttempt--)>0 ){
      memcpy(pHdr, &pShm->hdr1, sizeof(BtShmHdr));
      if( btLogChecksumOk(pHdr) ) break;
      memcpy(pHdr, &pShm->hdr2, sizeof(BtShmHdr));
      if( btLogChecksumOk(pHdr) ) break;
    }

    if( nAttempt==0 ) rc = SQLITE4_PROTOCOL;
  }

  return rc;
}

int sqlite4BtLogSnapshotOpen(BtLog *pLog){
  int rc = SQLITE4_NOTFOUND;
  BtShmHdr shmhdr;

  while( rc==SQLITE4_NOTFOUND ){
    /* Attempt to read a copy of the BtShmHdr from shared-memory. */
    rc = btLogSnapshot(pLog, &pLog->snapshot);

    /* Take a read lock on the database */
    if( rc==SQLITE4_OK ){
      u32 *aReadlock = btLogShm(pLog)->aReadlock;
      rc = sqlite4BtLockReader(pLog->pLock, pLog->snapshot.aLog, aReadlock);
    }

    /* Check that the BtShmHdr in shared-memory has not changed. If it has,
    ** drop the read-lock and re-attempt the entire operation. */
    if( rc==SQLITE4_OK ){
      rc = btLogSnapshot(pLog, &shmhdr);
    }
    if( rc==SQLITE4_OK && memcmp(&shmhdr, &pLog->snapshot, sizeof(BtShmHdr)) ){
      sqlite4BtLockReaderUnlock(pLog->pLock);
      rc = SQLITE4_NOTFOUND;
    }
  }

  return rc;
}

int sqlite4BtLogSnapshotClose(BtLog *pLog){
  sqlite4BtLockReaderUnlock(pLog->pLock);
  return SQLITE4_OK;
}

int sqlite4BtLogSnapshotWritable(BtLog *pLog){
  return 1;
}

int sqlite4BtLogCheckpoint(BtLog *pLog){
  return SQLITE4_OK;
}


Changes to src/bt_main.c.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef MIN
# define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#ifndef MAX
# define MAX(a,b) (((a)>(b))?(a):(b))
#endif

#define BT_STDERR_DEBUG 1

struct bt_db {
  sqlite4_env *pEnv;              /* SQLite environment */
  BtPager *pPager;                /* Underlying page-based database */
  bt_cursor *pAllCsr;             /* List of all open cursors */
};








|







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef MIN
# define MIN(a,b) (((a)<(b))?(a):(b))
#endif
#ifndef MAX
# define MAX(a,b) (((a)>(b))?(a):(b))
#endif

/* #define BT_STDERR_DEBUG 1 */

struct bt_db {
  sqlite4_env *pEnv;              /* SQLite environment */
  BtPager *pPager;                /* Underlying page-based database */
  bt_cursor *pAllCsr;             /* List of all open cursors */
};

Changes to src/bt_pager.c.
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
static void btFreePage(BtPager *p, BtPage *pPg){
  if( pPg ){
    sqlite4_free(p->btl.pEnv, pPg->aData);
    sqlite4_free(p->btl.pEnv, pPg);
  }
}

/*
** Close a pager database handle.
*/
int sqlite4BtPagerClose(BtPager *p){
  int i;

  if( p->btl.pFd ){
    p->btl.pVfs->xClose(p->btl.pFd);
  }

  for(i=0; i<p->hash.nHash; i++){
    BtPage *pPg;
    BtPage *pNext;
    for(pPg=p->hash.aHash[i]; pPg; pPg=pNext){
      pNext = pPg->pNextHash;
      btFreePage(p, pPg);
    }
  }


  btHashClear(p);



























  sqlite4_free(p->btl.pEnv, p->zFile);
  sqlite4_free(p->btl.pEnv, p);
  return SQLITE4_OK;
}

/*
** Return a pointer to the nExtra bytes of space allocated by PagerNew().
*/
void *sqlite4BtPagerExtra(BtPager *p){
  return (void*)&p[1];
}

/*
** Open the logging module and run recovery on the database. This is 
** called during connection by the bt_lock module.
*/
static int btRecover(BtLock *pLock){
  BtPager *p = (BtPager*)pLock;
  int rc;
  rc = sqlite4BtLogOpen(p, 1, &p->pLog);
  return rc;
}

static int btCheckpoint(BtLock *pLock){
  BtPager *p = (BtPager*)pLock;
  return sqlite4BtLogCheckpoint(p->pLog);
}

static int btCleanup(BtLock *pLock){
  BtPager *p = (BtPager*)pLock;
  int rc = sqlite4BtLogClose(p->pLog, 1);
  return rc;
}

/*
** Attach a database file to a pager object.
**
** This function may only be called once for each BtPager object. If it
** fails, the BtPager is rendered unusable (and must be closed by the
** caller using BtPagerClose()).
**







<
<
<
|

>
|
|
<









>
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


|




















<
<
<
<
<
<
<
<
<
<
<







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
static void btFreePage(BtPager *p, BtPage *pPg){
  if( pPg ){
    sqlite4_free(p->btl.pEnv, pPg->aData);
    sqlite4_free(p->btl.pEnv, pPg);
  }
}




static void btPurgeCache(BtPager *p){
  int i;
  assert( p->iTransactionLevel==0 );
  assert( p->pDirty==0 );
  assert( p->nTotalRef==0 );


  for(i=0; i<p->hash.nHash; i++){
    BtPage *pPg;
    BtPage *pNext;
    for(pPg=p->hash.aHash[i]; pPg; pPg=pNext){
      pNext = pPg->pNextHash;
      btFreePage(p, pPg);
    }
  }
  btHashClear(p);
}

static int btCheckpoint(BtLock *pLock){
  BtPager *p = (BtPager*)pLock;
  return sqlite4BtLogCheckpoint(p->pLog);
}

static int btCleanup(BtLock *pLock){
  BtPager *p = (BtPager*)pLock;
  int rc = sqlite4BtLogClose(p->pLog, 1);
  p->pLog = 0;
  return rc;
}

/*
** Close a pager database handle.
*/
int sqlite4BtPagerClose(BtPager *p){
  int i;
  int rc;

  rc = sqlite4BtLockDisconnect(p, btCheckpoint, btCleanup);

  if( p->btl.pFd ){
    p->btl.pVfs->xClose(p->btl.pFd);
  }

  btPurgeCache(p);
  sqlite4BtLogClose(p->pLog, 0);
  sqlite4_free(p->btl.pEnv, p->zFile);
  sqlite4_free(p->btl.pEnv, p);
  return rc;
}

/*
** Return a pointer to the nExtra bytes of space allocated by PagerNew().
*/
void *sqlite4BtPagerExtra(BtPager *p){
  return (void*)&p[1];
}

/*
** Open the logging module and run recovery on the database. This is 
** called during connection by the bt_lock module.
*/
static int btRecover(BtLock *pLock){
  BtPager *p = (BtPager*)pLock;
  int rc;
  rc = sqlite4BtLogOpen(p, 1, &p->pLog);
  return rc;
}












/*
** Attach a database file to a pager object.
**
** This function may only be called once for each BtPager object. If it
** fails, the BtPager is rendered unusable (and must be closed by the
** caller using BtPagerClose()).
**
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
static int btOpenReadTransaction(BtPager *p){
  int rc;
  i64 nByte;                      /* Size of db file in bytes */

  assert( p->iTransactionLevel==0 );
  assert( p->btl.pFd );


  rc = p->btl.pVfs->xSize(p->btl.pFd, &nByte);
  if( rc==SQLITE4_OK && nByte>0 ){
    rc = p->btl.pVfs->xRead(p->btl.pFd, 0, &p->dbhdr, sizeof(p->dbhdr));
  }else{
    memset(&p->dbhdr, 0, sizeof(p->dbhdr));
    p->dbhdr.pgsz = BT_DEFAULT_PGSZ;
    p->dbhdr.nPg = 2;
  }





  if( rc==SQLITE4_OK ){
    /* If the read transaction was successfully opened, the transaction 
    ** level is now 1.  */
    p->iTransactionLevel = 1;
  }












  return rc;
}

/*
** Commit the current write transaction to disk.
*/
static int btCommitTransaction(BtPager *p){
  int rc = SQLITE4_OK;
  BtPage *pPg;
  BtPage *pNext;
  assert( p->iTransactionLevel>=2 );

  for(pPg=p->pDirty; pPg; pPg=pNext){
    pNext = pPg->pNextDirty;
    pPg->flags &= ~(BT_PAGE_DIRTY);
    pPg->pNextDirty = 0;
    if( rc==SQLITE4_OK ){
      i64 iOff = (i64)p->dbhdr.pgsz * (i64)(pPg->pgno-1);
      rc = p->btl.pVfs->xWrite(p->btl.pFd, iOff, pPg->aData, p->dbhdr.pgsz);
    }
  }
  p->pDirty = 0;

  if( rc==SQLITE4_OK ){
    rc = p->btl.pVfs->xWrite(p->btl.pFd, 0, (void*)&p->dbhdr, sizeof(BtDbhdr));
  }

  return rc;
}

static int btLoadPageData(BtPager *p, BtPage *pPg){










  i64 iOff = (i64)p->dbhdr.pgsz * (i64)(pPg->pgno-1);
  int rc = p->btl.pVfs->xRead(p->btl.pFd, iOff, pPg->aData, p->dbhdr.pgsz);


  return rc;
}

static int btAllocatePage(BtPager *p, BtPage **ppPg){
  int rc;                         /* Return code */
  BtPage *pRet;
  u8 *aData;







>








>
>
>
>






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

















|
|












>
>
>
>
>
>
>
>
>
>
|
|
>
>







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
static int btOpenReadTransaction(BtPager *p){
  int rc;
  i64 nByte;                      /* Size of db file in bytes */

  assert( p->iTransactionLevel==0 );
  assert( p->btl.pFd );

  /* TODO: This should be subsumed into log recovery etc. */
  rc = p->btl.pVfs->xSize(p->btl.pFd, &nByte);
  if( rc==SQLITE4_OK && nByte>0 ){
    rc = p->btl.pVfs->xRead(p->btl.pFd, 0, &p->dbhdr, sizeof(p->dbhdr));
  }else{
    memset(&p->dbhdr, 0, sizeof(p->dbhdr));
    p->dbhdr.pgsz = BT_DEFAULT_PGSZ;
    p->dbhdr.nPg = 2;
  }

  if( rc==SQLITE4_OK ){
    rc = sqlite4BtLogSnapshotOpen(p->pLog);
  }

  if( rc==SQLITE4_OK ){
    /* If the read transaction was successfully opened, the transaction 
    ** level is now 1.  */
    p->iTransactionLevel = 1;
  }
  return rc;
}

static int btCloseReadTransaction(BtPager *p){
  int rc;
  assert( p->iTransactionLevel==0 );
  rc = sqlite4BtLogSnapshotClose(p->pLog);

  /* Purge the page cache. */
  assert( p->pDirty==0 );
  btPurgeCache(p);

  return rc;
}

/*
** Commit the current write transaction to disk.
*/
static int btCommitTransaction(BtPager *p){
  int rc = SQLITE4_OK;
  BtPage *pPg;
  BtPage *pNext;
  assert( p->iTransactionLevel>=2 );

  for(pPg=p->pDirty; pPg; pPg=pNext){
    pNext = pPg->pNextDirty;
    pPg->flags &= ~(BT_PAGE_DIRTY);
    pPg->pNextDirty = 0;
    if( rc==SQLITE4_OK ){
      int bCommit = (pNext==0);
      rc = sqlite4BtLogWrite(p->pLog, pPg->pgno, pPg->aData, bCommit);
    }
  }
  p->pDirty = 0;

  if( rc==SQLITE4_OK ){
    rc = p->btl.pVfs->xWrite(p->btl.pFd, 0, (void*)&p->dbhdr, sizeof(BtDbhdr));
  }

  return rc;
}

static int btLoadPageData(BtPager *p, BtPage *pPg){
  int rc;                         /* Return code */

  /* Try to load data from the logging module. If SQLITE4_OK is returned,
  ** data was loaded successfully. If SQLITE4_NOTFOUND, the required page
  ** is not present in the log and should be loaded from the database
  ** file. Any other error code is returned to the caller.  */
  rc = sqlite4BtLogRead(p->pLog, pPg->pgno, pPg->aData);

  /* If necessary, load data from the database file. */
  if( rc==SQLITE4_NOTFOUND ){
    i64 iOff = (i64)p->dbhdr.pgsz * (i64)(pPg->pgno-1);
    rc = p->btl.pVfs->xRead(p->btl.pFd, iOff, pPg->aData, p->dbhdr.pgsz);
  }

  return rc;
}

static int btAllocatePage(BtPager *p, BtPage **ppPg){
  int rc;                         /* Return code */
  BtPage *pRet;
  u8 *aData;
440
441
442
443
444
445
446



447
448
449
450
451
452
453
454
455
456
457
458
459
460



461
462
463
464
465
466
467
  assert( p->btl.pFd );
  if( p->iTransactionLevel>=iLevel ){
    if( p->iTransactionLevel>=2 && iLevel<2 ){
      /* Commit the main write transaction. */
      rc = btCommitTransaction(p);
    }
    p->iTransactionLevel = iLevel;



  }
  return rc;
}

int sqlite4BtPagerRollback(BtPager *p, int iLevel){
  int rc = SQLITE4_OK;

  assert( p->btl.pFd );
  if( p->iTransactionLevel>=iLevel ){
    assert( iLevel<=1 );          /* TODO: Fix this! */
    if( p->iTransactionLevel>=2 ){
      rc = btRollbackTransaction(p);
    }
    p->iTransactionLevel = iLevel;



  }

  return rc;
}

int sqlite4BtPagerRevert(BtPager *p, int iLevel){
  int rc;







>
>
>














>
>
>







483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
  assert( p->btl.pFd );
  if( p->iTransactionLevel>=iLevel ){
    if( p->iTransactionLevel>=2 && iLevel<2 ){
      /* Commit the main write transaction. */
      rc = btCommitTransaction(p);
    }
    p->iTransactionLevel = iLevel;
    if( iLevel==0 ){
      rc = btCloseReadTransaction(p);
    }
  }
  return rc;
}

int sqlite4BtPagerRollback(BtPager *p, int iLevel){
  int rc = SQLITE4_OK;

  assert( p->btl.pFd );
  if( p->iTransactionLevel>=iLevel ){
    assert( iLevel<=1 );          /* TODO: Fix this! */
    if( p->iTransactionLevel>=2 ){
      rc = btRollbackTransaction(p);
    }
    p->iTransactionLevel = iLevel;
    if( iLevel==0 ){
      rc = btCloseReadTransaction(p);
    }
  }

  return rc;
}

int sqlite4BtPagerRevert(BtPager *p, int iLevel){
  int rc;
Changes to src/bt_unix.c.
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    }else{
      assert( zCwd==zTmp );
      nCwd = strlen(zCwd);
    }
  }

  if( rc==SQLITE4_OK ){
    int nReq = nCwd + 1 + strlen(zName) + 1;
    zOut = sqlite4_malloc(pSqlEnv, nReq);
    if( zOut ){
      int nName = strlen(zName);
      if( nCwd ){
        memcpy(zOut, zCwd, nCwd);
        zOut[nCwd] = '/';
        memcpy(&zOut[nCwd+1], zName, nName+1);







|







229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
    }else{
      assert( zCwd==zTmp );
      nCwd = strlen(zCwd);
    }
  }

  if( rc==SQLITE4_OK ){
    int nReq = nCwd + 1 + strlen(zName) + 1 + 4;
    zOut = sqlite4_malloc(pSqlEnv, nReq);
    if( zOut ){
      int nName = strlen(zName);
      if( nCwd ){
        memcpy(zOut, zCwd, nCwd);
        zOut[nCwd] = '/';
        memcpy(&zOut[nCwd+1], zName, nName+1);
Changes to test/simple3.test.
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
forcedelete test.db

proc bigstr {n} {
  set nRep [expr 1+($n/20)]
  string range [string repeat "abcdefghijklmnopqrstuvwxyz" $nRep] 0 [expr $n-1]
}

if 0 {
sqlite4 db file:test.db?kv=bt
foreach {tn nStr} {
  1 3000
  2 30000
  3 300000
  4 3000000
  5 30000000
} {
  set big [bigstr $nStr]
  do_execsql_test 6.$tn.1 {
    DROP TABLE IF EXISTS t6;
    CREATE TABLE t6(a PRIMARY KEY, b VALUE);
    INSERT INTO t6 VALUES($big, '123');
  }

  do_execsql_test 6.$tn.2 {
    SELECT length(a) FROM t6;
  } $nStr

  do_execsql_test 6.$tn.3 {
    SELECT a FROM t6;
  } [list $big]
}

finish_test
}


do_test 1.0 {
  sqlite4 db file:test.db?kv=bt
  db close
} {}

do_test 1.1 { sqlite4 db file:test.db?kv=bt } {}

do_execsql_test 1.2 { 
  CREATE TABLE t1(a, b) 
} 

do_execsql_test 1.3 {
  SELECT * FROM sqlite_master;
} {table t1 t1 2 {CREATE TABLE t1(a, b)}}








do_execsql_test 1.4 {
  INSERT INTO t1 VALUES('abc', 'def');
  INSERT INTO t1 VALUES('ghi', 'jkl');
} {}

do_execsql_test 1.5 {







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












|


>
>
>
>
>
>
>







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
forcedelete test.db

proc bigstr {n} {
  set nRep [expr 1+($n/20)]
  string range [string repeat "abcdefghijklmnopqrstuvwxyz" $nRep] 0 [expr $n-1]
}






























do_test 1.0 {
  sqlite4 db file:test.db?kv=bt
  db close
} {}

do_test 1.1 { sqlite4 db file:test.db?kv=bt } {}

do_execsql_test 1.2 { 
  CREATE TABLE t1(a, b) 
} 

do_execsql_test 1.3.1 {
  SELECT * FROM sqlite_master;
} {table t1 t1 2 {CREATE TABLE t1(a, b)}}

do_test 1.3.2 {
  sqlite4 db2 file:test.db?kv=bt
  breakpoint
  execsql { SELECT * FROM sqlite_master } db2
} {table t1 t1 2 {CREATE TABLE t1(a, b)}}
db2 close

do_execsql_test 1.4 {
  INSERT INTO t1 VALUES('abc', 'def');
  INSERT INTO t1 VALUES('ghi', 'jkl');
} {}

do_execsql_test 1.5 {
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
  INSERT INTO t1 VALUES(6, $val);
}

do_execsql_test 2.5 { 
  SELECT a, length(b) FROM t1 
} {1 200  3 200  4 200  5 200  6 200}


#-------------------------------------------------------------------------

proc lshuffle {list} {
  set nVal [llength $list]
  for {set i 0} {$i < $nVal} {incr i} {
    set i2 [expr int(rand()*$nVal)]
    set tmp [lindex $list $i]







<







100
101
102
103
104
105
106

107
108
109
110
111
112
113
  INSERT INTO t1 VALUES(6, $val);
}

do_execsql_test 2.5 { 
  SELECT a, length(b) FROM t1 
} {1 200  3 200  4 200  5 200  6 200}


#-------------------------------------------------------------------------

proc lshuffle {list} {
  set nVal [llength $list]
  for {set i 0} {$i < $nVal} {incr i} {
    set i2 [expr int(rand()*$nVal)]
    set tmp [lindex $list $i]
148
149
150
151
152
153
154

155
156
157
158
159
160
161
  }
  return $ret
}

do_test 3.0 {
  catch { db close }
  forcedelete test.db

  sqlite4 db file:test.db?kv=bt
} {}

do_execsql_test 3.1 {
  CREATE TABLE t1(a PRIMARY KEY, b);
}








>







126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  }
  return $ret
}

do_test 3.0 {
  catch { db close }
  forcedelete test.db
  forcedelete test.db-wal
  sqlite4 db file:test.db?kv=bt
} {}

do_execsql_test 3.1 {
  CREATE TABLE t1(a PRIMARY KEY, b);
}