1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
-
+
|
/*
** 2004 April 6
**
** The author disclaims copyright to this source code. In place of
** a legal notice, here is a blessing:
**
** May you do good and not evil.
** May you find forgiveness for yourself and forgive others.
** May you share freely, never taking more than you give.
**
*************************************************************************
** $Id: btree.c,v 1.341 2007/03/19 17:44:27 danielk1977 Exp $
** $Id: btree.c,v 1.342 2007/03/26 22:05:01 drh Exp $
**
** This file implements a external (disk-based) database using BTrees.
** For a detailed discussion of BTrees, refer to
**
** Donald E. Knuth, THE ART OF COMPUTER PROGRAMMING, Volume 3:
** "Sorting And Searching", pages 473-480. Addison-Wesley
** Publishing Company, Reading, Massachusetts.
|
︙ | | |
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
|
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
|
-
+
|
rc = ptrmapPut(pBt, iFreePage, eType, iPtrPage);
}
}
return rc;
}
/* Forward declaration required by autoVacuumCommit(). */
static int allocatePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8);
/*
** This routine is called prior to sqlite3PagerCommit when a transaction
** is commited for an auto-vacuum database.
*/
static int autoVacuumCommit(BtShared *pBt, Pgno *nTrunc){
Pager *pPager = pBt->pPager;
|
︙ | | |
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
|
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
|
-
+
-
+
|
continue;
}
rc = getPage(pBt, iDbPage, &pDbMemPage, 0);
if( rc!=SQLITE_OK ) goto autovacuum_out;
/* Find the next page in the free-list that is not already at the end
** of the file. A page can be pulled off the free list using the
** allocatePage() routine.
** allocateBtreePage() routine.
*/
do{
if( pFreeMemPage ){
releasePage(pFreeMemPage);
pFreeMemPage = 0;
}
rc = allocatePage(pBt, &pFreeMemPage, &iFreePage, 0, 0);
rc = allocateBtreePage(pBt, &pFreeMemPage, &iFreePage, 0, 0);
if( rc!=SQLITE_OK ){
releasePage(pDbMemPage);
goto autovacuum_out;
}
assert( iFreePage<=origSize );
}while( iFreePage>finSize );
releasePage(pFreeMemPage);
|
︙ | | |
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
|
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
|
-
+
|
** attempt to keep related pages close to each other in the database file,
** which in turn can make database access faster.
**
** If the "exact" parameter is not 0, and the page-number nearby exists
** anywhere on the free-list, then it is guarenteed to be returned. This
** is only used by auto-vacuum databases when allocating a new table.
*/
static int allocatePage(
static int allocateBtreePage(
BtShared *pBt,
MemPage **ppPage,
Pgno *pPgno,
Pgno nearby,
u8 exact
){
MemPage *pPage1;
|
︙ | | |
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
|
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
|
-
+
|
pPrior = &pCell[info.iOverflow];
while( nPayload>0 ){
if( spaceLeft==0 ){
#ifndef SQLITE_OMIT_AUTOVACUUM
Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */
#endif
rc = allocatePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0);
#ifndef SQLITE_OMIT_AUTOVACUUM
/* If the database supports auto-vacuum, and the second or subsequent
** overflow page is being allocated, add an entry to the pointer-map
** for that page now. The entry for the first overflow page will be
** added later, by the insertCell() routine.
*/
if( pBt->autoVacuum && pgnoPtrmap!=0 && rc==SQLITE_OK ){
|
︙ | | |
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
|
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
|
-
+
|
int parentIdx = pParent->nCell; /* pParent new divider cell index */
int parentSize; /* Size of new divider cell */
u8 parentCell[64]; /* Space for the new divider cell */
/* Allocate a new page. Insert the overflow cell from pPage
** into it. Then remove the overflow cell from pPage.
*/
rc = allocatePage(pBt, &pNew, &pgnoNew, 0, 0);
rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0);
if( rc!=SQLITE_OK ){
return rc;
}
pCell = pPage->aOvfl[0].pCell;
szCell = cellSizePtr(pPage, pCell);
zeroPage(pNew, pPage->aData[0]);
assemblePage(pNew, 1, &pCell, &szCell);
|
︙ | | |
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
|
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
|
-
+
|
pNew = apNew[i] = apOld[i];
pgnoNew[i] = pgnoOld[i];
apOld[i] = 0;
rc = sqlite3PagerWrite(pNew->pDbPage);
if( rc ) goto balance_cleanup;
}else{
assert( i>0 );
rc = allocatePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
rc = allocateBtreePage(pBt, &pNew, &pgnoNew[i], pgnoNew[i-1], 0);
if( rc ) goto balance_cleanup;
apNew[i] = pNew;
}
nNew++;
zeroPage(pNew, pageFlags);
}
|
︙ | | |
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
|
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
|
-
+
|
u8 *cdata; /* Content of the child page */
int hdr; /* Offset to page header in parent */
int brk; /* Offset to content of first cell in parent */
assert( pPage->pParent==0 );
assert( pPage->nOverflow>0 );
pBt = pPage->pBt;
rc = allocatePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
rc = allocateBtreePage(pBt, &pChild, &pgnoChild, pPage->pgno, 0);
if( rc ) return rc;
assert( sqlite3PagerIswriteable(pChild->pDbPage) );
usableSize = pBt->usableSize;
data = pPage->aData;
hdr = pPage->hdrOffset;
brk = get2byte(&data[hdr+5]);
cdata = pChild->aData;
|
︙ | | |
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
|
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
|
-
+
|
** If an open cursor was using the page a problem would occur.
*/
if( pBt->pCursor ){
return SQLITE_LOCKED;
}
#ifdef SQLITE_OMIT_AUTOVACUUM
rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
if( rc ) return rc;
#else
if( pBt->autoVacuum ){
Pgno pgnoMove; /* Move a page here to make room for the root-page */
MemPage *pPageMove; /* The page to move to. */
/* Read the value of meta[3] from the database to determine where the
|
︙ | | |
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
|
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
|
-
+
|
}
assert( pgnoRoot>=3 );
/* Allocate a page. The page that currently resides at pgnoRoot will
** be moved to the allocated page (unless the allocated page happens
** to reside at pgnoRoot).
*/
rc = allocatePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, 1);
if( rc!=SQLITE_OK ){
return rc;
}
if( pgnoMove!=pgnoRoot ){
u8 eType;
Pgno iPtrPage;
|
︙ | | |
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
|
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
|
-
+
|
rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot);
if( rc ){
releasePage(pRoot);
return rc;
}
}else{
rc = allocatePage(pBt, &pRoot, &pgnoRoot, 1, 0);
rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0);
if( rc ) return rc;
}
#endif
assert( sqlite3PagerIswriteable(pRoot->pDbPage) );
zeroPage(pRoot, flags | PTF_LEAF);
sqlite3PagerUnref(pRoot->pDbPage);
*piTable = (int)pgnoRoot;
|
︙ | | |
︙ | | |
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
|
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
|
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
|
unsigned int sqlite3_pending_byte = 0x40000000;
#endif
int sqlite3_os_trace = 0;
#ifdef SQLITE_DEBUG
static int last_page = 0;
#define SEEK(X) last_page=(X)
#define TRACE1(X) if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
#define TRACE2(X,Y) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
#define TRACE3(X,Y,Z) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
#define TRACE4(X,Y,Z,A) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
#define TRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
#define TRACE6(X,Y,Z,A,B,C) if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
#define TRACE7(X,Y,Z,A,B,C,D) \
#define OSTRACE1(X) if( sqlite3_os_trace ) sqlite3DebugPrintf(X)
#define OSTRACE2(X,Y) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y)
#define OSTRACE3(X,Y,Z) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z)
#define OSTRACE4(X,Y,Z,A) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A)
#define OSTRACE5(X,Y,Z,A,B) if( sqlite3_os_trace ) sqlite3DebugPrintf(X,Y,Z,A,B)
#define OSTRACE6(X,Y,Z,A,B,C) \
if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C)
#define OSTRACE7(X,Y,Z,A,B,C,D) \
if(sqlite3_os_trace) sqlite3DebugPrintf(X,Y,Z,A,B,C,D)
#else
#define SEEK(X)
#define TRACE1(X)
#define TRACE2(X,Y)
#define TRACE3(X,Y,Z)
#define TRACE4(X,Y,Z,A)
#define TRACE5(X,Y,Z,A,B)
#define TRACE6(X,Y,Z,A,B,C)
#define TRACE7(X,Y,Z,A,B,C,D)
#define OSTRACE1(X)
#define OSTRACE2(X,Y)
#define OSTRACE3(X,Y,Z)
#define OSTRACE4(X,Y,Z,A)
#define OSTRACE5(X,Y,Z,A,B)
#define OSTRACE6(X,Y,Z,A,B,C)
#define OSTRACE7(X,Y,Z,A,B,C,D)
#endif
/*
** Macros for performance tracing. Normally turned off. Only works
** on i486 hardware.
*/
#ifdef SQLITE_PERFORMANCE_TRACE
|
︙ | | |
193
194
195
196
197
198
199
200
|
194
195
196
197
198
199
200
|
-
|
/*
** The default size of a disk sector
*/
#ifndef PAGER_SECTOR_SIZE
# define PAGER_SECTOR_SIZE 512
#endif
|
︙ | | |
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
-
+
|
/*
** Delete the named file
*/
int sqlite3Os2Delete( const char *zFilename ){
APIRET rc = NO_ERROR;
rc = DosDelete( (PSZ)zFilename );
TRACE2( "DELETE \"%s\"\n", zFilename );
OSTRACE2( "DELETE \"%s\"\n", zFilename );
return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
}
/*
** Return TRUE if the named file exists.
*/
int sqlite3Os2FileExists( const char *zFilename ){
|
︙ | | |
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
-
+
|
*pReadonly = 0;
}
f.h = hf;
f.locktype = NO_LOCK;
f.delOnClose = 0;
f.pathToDel = NULL;
OpenCounter(+1);
TRACE3( "OPEN R/W %d \"%s\"\n", hf, zFilename );
OSTRACE3( "OPEN R/W %d \"%s\"\n", hf, zFilename );
return allocateOs2File( &f, pld );
}
/*
** Attempt to open a new file for exclusive access by this process.
** The file will be opened for both reading and writing. To avoid
|
︙ | | |
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
-
+
|
f.h = hf;
f.locktype = NO_LOCK;
f.delOnClose = delFlag ? 1 : 0;
f.pathToDel = delFlag ? sqlite3OsFullPathname( zFilename ) : NULL;
OpenCounter( +1 );
if( delFlag ) DosForceDelete( sqlite3OsFullPathname( zFilename ) );
TRACE3( "OPEN EX %d \"%s\"\n", hf, sqlite3OsFullPathname ( zFilename ) );
OSTRACE3( "OPEN EX %d \"%s\"\n", hf, sqlite3OsFullPathname ( zFilename ) );
return allocateOs2File( &f, pld );
}
/*
** Attempt to open a new file for read-only access.
**
** On success, write the file handle into *id and return SQLITE_OK.
|
︙ | | |
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
|
-
+
|
return SQLITE_CANTOPEN;
}
f.h = hf;
f.locktype = NO_LOCK;
f.delOnClose = 0;
f.pathToDel = NULL;
OpenCounter( +1 );
TRACE3( "OPEN RO %d \"%s\"\n", hf, zFilename );
OSTRACE3( "OPEN RO %d \"%s\"\n", hf, zFilename );
return allocateOs2File( &f, pld );
}
/*
** Attempt to open a file descriptor for the directory that contains a
** file. This file descriptor can be used to fsync() the directory
** in order to make sure the creation of a new file is actually written
|
︙ | | |
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
|
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
|
-
+
-
+
|
sqlite3Randomness( 15, &zBuf[j] );
for( i = 0; i < 15; i++, j++ ){
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
}
zBuf[j] = 0;
if( !sqlite3OsFileExists( zBuf ) ) break;
}
TRACE2( "TEMP FILENAME: %s\n", zBuf );
OSTRACE2( "TEMP FILENAME: %s\n", zBuf );
return SQLITE_OK;
}
/*
** Close a file.
*/
int os2Close( OsFile **pld ){
os2File *pFile;
APIRET rc = NO_ERROR;
if( pld && (pFile = (os2File*)*pld) != 0 ){
TRACE2( "CLOSE %d\n", pFile->h );
OSTRACE2( "CLOSE %d\n", pFile->h );
rc = DosClose( pFile->h );
pFile->locktype = NO_LOCK;
if( pFile->delOnClose != 0 ){
rc = DosForceDelete( pFile->pathToDel );
}
*pld = 0;
OpenCounter( -1 );
|
︙ | | |
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
-
+
|
** bytes were read successfully and SQLITE_IOERR if anything goes
** wrong.
*/
int os2Read( OsFile *id, void *pBuf, int amt ){
ULONG got;
assert( id!=0 );
SimulateIOError( return SQLITE_IOERR );
TRACE3( "READ %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
OSTRACE3( "READ %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
DosRead( ((os2File*)id)->h, pBuf, amt, &got );
if (got == (ULONG)amt)
return SQLITE_OK;
else if (got < 0)
return SQLITE_IOERR_READ;
else {
memset(&((char*)pBuf)[got], 0, amt-got);
|
︙ | | |
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
|
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
|
-
+
-
+
-
+
-
+
|
*/
int os2Write( OsFile *id, const void *pBuf, int amt ){
APIRET rc = NO_ERROR;
ULONG wrote;
assert( id!=0 );
SimulateIOError( return SQLITE_IOERR );
SimulateDiskfullError( return SQLITE_FULL );
TRACE3( "WRITE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
OSTRACE3( "WRITE %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
while( amt > 0 &&
(rc = DosWrite( ((os2File*)id)->h, (PVOID)pBuf, amt, &wrote )) && wrote > 0 ){
amt -= wrote;
pBuf = &((char*)pBuf)[wrote];
}
return ( rc != NO_ERROR || amt > (int)wrote ) ? SQLITE_FULL : SQLITE_OK;
}
/*
** Move the read/write pointer in a file.
*/
int os2Seek( OsFile *id, i64 offset ){
APIRET rc = NO_ERROR;
ULONG filePointer = 0L;
assert( id!=0 );
rc = DosSetFilePtr( ((os2File*)id)->h, offset, FILE_BEGIN, &filePointer );
TRACE3( "SEEK %d %lld\n", ((os2File*)id)->h, offset );
OSTRACE3( "SEEK %d %lld\n", ((os2File*)id)->h, offset );
return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
}
/*
** Make sure all writes to a particular file are committed to disk.
*/
int os2Sync( OsFile *id, int dataOnly ){
assert( id!=0 );
TRACE3( "SYNC %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
OSTRACE3( "SYNC %d lock=%d\n", ((os2File*)id)->h, ((os2File*)id)->locktype );
return DosResetBuffer( ((os2File*)id)->h ) == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
}
/*
** Sync the directory zDirname. This is a no-op on operating systems other
** than UNIX.
*/
int sqlite3Os2SyncDirectory( const char *zDirname ){
SimulateIOError( return SQLITE_IOERR );
return SQLITE_OK;
}
/*
** Truncate an open file to a specified size
*/
int os2Truncate( OsFile *id, i64 nByte ){
APIRET rc = NO_ERROR;
ULONG upperBits = nByte>>32;
assert( id!=0 );
TRACE3( "TRUNCATE %d %lld\n", ((os2File*)id)->h, nByte );
OSTRACE3( "TRUNCATE %d %lld\n", ((os2File*)id)->h, nByte );
SimulateIOError( return SQLITE_IOERR );
rc = DosSetFilePtr( ((os2File*)id)->h, nByte, FILE_BEGIN, &upperBits );
if( rc != NO_ERROR ){
return SQLITE_IOERR;
}
rc = DosSetFilePtr( ((os2File*)id)->h, 0L, FILE_END, &upperBits );
return rc == NO_ERROR ? SQLITE_OK : SQLITE_IOERR;
|
︙ | | |
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
|
-
+
|
int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
FILELOCK LockArea,
UnlockArea;
os2File *pFile = (os2File*)id;
memset(&LockArea, 0, sizeof(LockArea));
memset(&UnlockArea, 0, sizeof(UnlockArea));
assert( pFile!=0 );
TRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
OSTRACE4( "LOCK %d %d was %d\n", pFile->h, locktype, pFile->locktype );
/* If there is already a lock of this type or more restrictive on the
** OsFile, do nothing. Don't use the end_lock: exit path, as
** sqlite3OsEnterMutex() hasn't been called yet.
*/
if( pFile->locktype>=locktype ){
return SQLITE_OK;
|
︙ | | |
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
|
-
+
|
UnlockArea.lOffset = 0L;
UnlockArea.lRange = 0L;
while( cnt-->0 && (res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L) )!=NO_ERROR ){
/* Try 3 times to get the pending lock. The pending lock might be
** held by another reader process who will release it momentarily.
*/
TRACE2( "could not get a PENDING lock. cnt=%d\n", cnt );
OSTRACE2( "could not get a PENDING lock. cnt=%d\n", cnt );
DosSleep(1);
}
gotPendingLock = res;
}
/* Acquire a shared lock
*/
|
︙ | | |
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
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
|
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
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
|
-
+
-
+
-
+
-
+
-
+
|
}
/* Acquire an EXCLUSIVE lock
*/
if( locktype==EXCLUSIVE_LOCK && res ){
assert( pFile->locktype>=SHARED_LOCK );
res = unlockReadLock(pFile);
TRACE2( "unreadlock = %d\n", res );
OSTRACE2( "unreadlock = %d\n", res );
LockArea.lOffset = SHARED_FIRST;
LockArea.lRange = SHARED_SIZE;
UnlockArea.lOffset = 0L;
UnlockArea.lRange = 0L;
res = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
if( res == NO_ERROR ){
newLocktype = EXCLUSIVE_LOCK;
}else{
TRACE2( "error-code = %d\n", res );
OSTRACE2( "error-code = %d\n", res );
}
}
/* If we are holding a PENDING lock that ought to be released, then
** release it now.
*/
if( gotPendingLock && locktype==SHARED_LOCK ){
LockArea.lOffset = 0L;
LockArea.lRange = 0L;
UnlockArea.lOffset = PENDING_BYTE;
UnlockArea.lRange = 1L;
DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
}
/* Update the state of the lock has held in the file descriptor then
** return the appropriate result code.
*/
if( res == NO_ERROR ){
rc = SQLITE_OK;
}else{
TRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
OSTRACE4( "LOCK FAILED %d trying for %d but got %d\n", pFile->h,
locktype, newLocktype );
rc = SQLITE_BUSY;
}
pFile->locktype = newLocktype;
return rc;
}
/*
** This routine checks if there is a RESERVED lock held on the specified
** file by this or any other process. If such a lock is held, return
** non-zero, otherwise zero.
*/
int os2CheckReservedLock( OsFile *id ){
APIRET rc = NO_ERROR;
os2File *pFile = (os2File*)id;
assert( pFile!=0 );
if( pFile->locktype>=RESERVED_LOCK ){
rc = 1;
TRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, rc );
OSTRACE3( "TEST WR-LOCK %d %d (local)\n", pFile->h, rc );
}else{
FILELOCK LockArea,
UnlockArea;
memset(&LockArea, 0, sizeof(LockArea));
memset(&UnlockArea, 0, sizeof(UnlockArea));
LockArea.lOffset = RESERVED_BYTE;
LockArea.lRange = 1L;
UnlockArea.lOffset = 0L;
UnlockArea.lRange = 0L;
rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
if( rc == NO_ERROR ){
LockArea.lOffset = 0L;
LockArea.lRange = 0L;
UnlockArea.lOffset = RESERVED_BYTE;
UnlockArea.lRange = 1L;
rc = DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
}
TRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, rc );
OSTRACE3( "TEST WR-LOCK %d %d (remote)\n", pFile->h, rc );
}
return rc;
}
/*
** Lower the locking level on file descriptor id to locktype. locktype
** must be either NO_LOCK or SHARED_LOCK.
|
︙ | | |
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
|
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
|
-
+
|
os2File *pFile = (os2File*)id;
FILELOCK LockArea,
UnlockArea;
memset(&LockArea, 0, sizeof(LockArea));
memset(&UnlockArea, 0, sizeof(UnlockArea));
assert( pFile!=0 );
assert( locktype<=SHARED_LOCK );
TRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
OSTRACE4( "UNLOCK %d to %d was %d\n", pFile->h, locktype, pFile->locktype );
type = pFile->locktype;
if( type>=EXCLUSIVE_LOCK ){
LockArea.lOffset = 0L;
LockArea.lRange = 0L;
UnlockArea.lOffset = SHARED_FIRST;
UnlockArea.lRange = SHARED_SIZE;
DosSetFileLocks( pFile->h, &UnlockArea, &LockArea, 2000L, 1L );
|
︙ | | |
︙ | | |
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
|
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
|
-
+
-
+
+
-
+
|
if( threadsOverrideEachOthersLocks ){
/* Ownership transfers not needed on this system */
return SQLITE_OK;
}
hSelf = pthread_self();
if( pthread_equal(pFile->tid, hSelf) ){
/* We are still in the same thread */
TRACE1("No-transfer, same thread\n");
OSTRACE1("No-transfer, same thread\n");
return SQLITE_OK;
}
if( pFile->locktype!=NO_LOCK ){
/* We cannot change ownership while we are holding a lock! */
return SQLITE_MISUSE;
}
TRACE4("Transfer ownership of %d from %d to %d\n", pFile->h,pFile->tid,hSelf);
OSTRACE4("Transfer ownership of %d from %d to %d\n",
pFile->h, pFile->tid, hSelf);
pFile->tid = hSelf;
if (pFile->pLock != NULL) {
releaseLockInfo(pFile->pLock);
rc = findLockInfo(pFile->h, &pFile->pLock, 0);
TRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
OSTRACE5("LOCK %d is now %s(%s,%d)\n", pFile->h,
locktypeName(pFile->locktype),
locktypeName(pFile->pLock->locktype), pFile->pLock->cnt);
return rc;
} else {
return SQLITE_OK;
}
}
|
︙ | | |
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
|
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
|
-
+
|
}
SET_THREADID(pFile);
assert( pFile->dirfd<0 );
pFile->dirfd = open(zDirname, O_RDONLY|O_BINARY, 0);
if( pFile->dirfd<0 ){
return SQLITE_CANTOPEN;
}
TRACE3("OPENDIR %-3d %s\n", pFile->dirfd, zDirname);
OSTRACE3("OPENDIR %-3d %s\n", pFile->dirfd, zDirname);
return SQLITE_OK;
}
/*
** If the following global variable points to a string which is the
** name of a directory, then that directory will be used to store
** temporary files.
|
︙ | | |
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
|
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
|
-
+
|
newOffset = lseek(id->h, id->offset, SEEK_SET);
if( newOffset!=id->offset ){
return -1;
}
got = read(id->h, pBuf, cnt);
#endif
TIMER_END;
TRACE5("READ %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
OSTRACE5("READ %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
if( got>0 ){
id->offset += got;
}
return got;
}
/*
|
︙ | | |
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
|
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
|
-
+
|
newOffset = lseek(id->h, id->offset, SEEK_SET);
if( newOffset!=id->offset ){
return -1;
}
got = write(id->h, pBuf, cnt);
#endif
TIMER_END;
TRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
OSTRACE5("WRITE %-3d %5d %7lld %d\n", id->h, got, id->offset, TIMER_ELAPSED);
if( got>0 ){
id->offset += got;
}
return got;
}
|
︙ | | |
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
|
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
|
-
+
-
+
|
** the directory entry for the journal was never created) and the transaction
** will not roll back - possibly leading to database corruption.
*/
static int unixSync(OsFile *id, int dataOnly){
int rc;
unixFile *pFile = (unixFile*)id;
assert( pFile );
TRACE2("SYNC %-3d\n", pFile->h);
OSTRACE2("SYNC %-3d\n", pFile->h);
rc = full_fsync(pFile->h, pFile->fullSync, dataOnly);
SimulateIOError( rc=1 );
if( rc ){
return SQLITE_IOERR_FSYNC;
}
if( pFile->dirfd>=0 ){
TRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
OSTRACE4("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd,
HAVE_FULLFSYNC, pFile->fullSync);
#ifndef SQLITE_DISABLE_DIRSYNC
/* The directory sync is only attempted if full_fsync is
** turned off or unavailable. If a full_fsync occurred above,
** then the directory sync is superfluous.
*/
if( (!HAVE_FULLFSYNC || !pFile->fullSync) && full_fsync(pFile->dirfd,0,0) ){
|
︙ | | |
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
|
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
|
-
+
|
int sqlite3UnixSyncDirectory(const char *zDirname){
#ifdef SQLITE_DISABLE_DIRSYNC
return SQLITE_OK;
#else
int fd;
int r;
fd = open(zDirname, O_RDONLY|O_BINARY, 0);
TRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
OSTRACE3("DIRSYNC %-3d (%s)\n", fd, zDirname);
if( fd<0 ){
return SQLITE_CANTOPEN;
}
r = fsync(fd);
close(fd);
SimulateIOError( r=1 );
if( r ){
|
︙ | | |
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
|
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
|
-
+
|
fcntl(pFile->h, F_GETLK, &lock);
if( lock.l_type!=F_UNLCK ){
r = 1;
}
}
sqlite3OsLeaveMutex();
TRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
return r;
}
/*
** Lock the file with the lock specified by parameter locktype - one
** of the following:
|
︙ | | |
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
|
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
|
-
+
-
+
|
int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id;
struct lockInfo *pLock = pFile->pLock;
struct flock lock;
int s;
assert( pFile );
TRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
OSTRACE7("LOCK %d %s was %s(%s,%d) pid=%d\n", pFile->h,
locktypeName(locktype), locktypeName(pFile->locktype),
locktypeName(pLock->locktype), pLock->cnt , getpid());
/* If there is already a lock of this type or more restrictive on the
** OsFile, do nothing. Don't use the end_lock: exit path, as
** sqlite3OsEnterMutex() hasn't been called yet.
*/
if( pFile->locktype>=locktype ){
TRACE3("LOCK %d %s ok (already held)\n", pFile->h,
OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
locktypeName(locktype));
return SQLITE_OK;
}
/* Make sure the locking sequence is correct
*/
assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
|
︙ | | |
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
|
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
|
-
+
-
+
|
}else if( locktype==EXCLUSIVE_LOCK ){
pFile->locktype = PENDING_LOCK;
pLock->locktype = PENDING_LOCK;
}
end_lock:
sqlite3OsLeaveMutex();
TRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
rc==SQLITE_OK ? "ok" : "failed");
return rc;
}
/*
** Lower the locking level on file descriptor pFile to locktype. locktype
** must be either NO_LOCK or SHARED_LOCK.
**
** If the locking level of the file descriptor is already at or below
** the requested locking level, this routine is a no-op.
*/
static int unixUnlock(OsFile *id, int locktype){
struct lockInfo *pLock;
struct flock lock;
int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id;
assert( pFile );
TRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
OSTRACE7("UNLOCK %d %d was %d(%d,%d) pid=%d\n", pFile->h, locktype,
pFile->locktype, pFile->pLock->locktype, pFile->pLock->cnt, getpid());
assert( locktype<=SHARED_LOCK );
if( pFile->locktype<=locktype ){
return SQLITE_OK;
}
if( CHECK_THREADID(pFile) ){
|
︙ | | |
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
|
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
|
-
+
|
close(id->h);
}
releaseLockInfo(id->pLock);
releaseOpenCnt(id->pOpen);
sqlite3OsLeaveMutex();
id->isOpen = 0;
TRACE2("CLOSE %-3d\n", id->h);
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
|
︙ | | |
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
|
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
|
-
+
-
+
|
int err;
pb.unLockFlag = setLockFlag ? 0 : 1;
pb.startEndFlag = 0;
pb.offset = offset;
pb.length = length;
pb.fd = fd;
TRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
OSTRACE5("AFPLOCK setting lock %s for %d in range %llx:%llx\n",
(setLockFlag?"ON":"OFF"), fd, offset, length);
err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0);
if ( err==-1 ) {
TRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
OSTRACE4("AFPLOCK failed to fsctl() '%s' %d %s\n", path, errno,
strerror(errno));
return 1; // error
} else {
return 0;
}
}
|
︙ | | |
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
|
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
|
-
+
-
+
-
+
|
r = 1;
} else {
/* if we succeeded in taking the reserved lock, unlock it to restore
** the original state */
_AFPFSSetLock(context->filePath, pFile->h, RESERVED_BYTE, 1, 0);
}
}
TRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
OSTRACE3("TEST WR-LOCK %d %d\n", pFile->h, r);
return r;
}
/* AFP-style locking following the behavior of unixLock, see the unixLock
** function comments for details of lock management. */
static int afpUnixLock(OsFile *id, int locktype)
{
int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id;
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
int gotPendingLock = 0;
assert( pFile );
TRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
OSTRACE5("LOCK %d %s was %s pid=%d\n", pFile->h,
locktypeName(locktype), locktypeName(pFile->locktype), getpid());
/* If there is already a lock of this type or more restrictive on the
** OsFile, do nothing. Don't use the afp_end_lock: exit path, as
** sqlite3OsEnterMutex() hasn't been called yet.
*/
if( pFile->locktype>=locktype ){
TRACE3("LOCK %d %s ok (already held)\n", pFile->h,
OSTRACE3("LOCK %d %s ok (already held)\n", pFile->h,
locktypeName(locktype));
return SQLITE_OK;
}
/* Make sure the locking sequence is correct
*/
assert( pFile->locktype!=NO_LOCK || locktype==SHARED_LOCK );
|
︙ | | |
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
|
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
|
-
+
-
+
|
pFile->locktype = locktype;
}else if( locktype==EXCLUSIVE_LOCK ){
pFile->locktype = PENDING_LOCK;
}
afp_end_lock:
sqlite3OsLeaveMutex();
TRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
OSTRACE4("LOCK %d %s %s\n", pFile->h, locktypeName(locktype),
rc==SQLITE_OK ? "ok" : "failed");
return rc;
}
/*
** Lower the locking level on file descriptor pFile to locktype. locktype
** must be either NO_LOCK or SHARED_LOCK.
**
** If the locking level of the file descriptor is already at or below
** the requested locking level, this routine is a no-op.
*/
static int afpUnixUnlock(OsFile *id, int locktype) {
struct flock lock;
int rc = SQLITE_OK;
unixFile *pFile = (unixFile*)id;
afpLockingContext *context = (afpLockingContext *) pFile->lockingContext;
assert( pFile );
TRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
OSTRACE5("UNLOCK %d %d was %d pid=%d\n", pFile->h, locktype,
pFile->locktype, getpid());
assert( locktype<=SHARED_LOCK );
if( pFile->locktype<=locktype ){
return SQLITE_OK;
}
if( CHECK_THREADID(pFile) ){
|
︙ | | |
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
|
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
|
-
+
|
sqlite3ThreadSafeFree(id->lockingContext);
}
if( id->dirfd>=0 ) close(id->dirfd);
id->dirfd = -1;
close(id->h);
id->isOpen = 0;
TRACE2("CLOSE %-3d\n", id->h);
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
|
︙ | | |
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
|
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
|
-
+
|
if( id->dirfd>=0 ) close(id->dirfd);
id->dirfd = -1;
sqlite3OsEnterMutex();
close(id->h);
sqlite3OsLeaveMutex();
id->isOpen = 0;
TRACE2("CLOSE %-3d\n", id->h);
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
#pragma mark Old-School .lock file based locking
|
︙ | | |
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
|
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
|
-
+
|
id->dirfd = -1;
sqlite3OsEnterMutex();
close(id->h);
sqlite3OsLeaveMutex();
id->isOpen = 0;
TRACE2("CLOSE %-3d\n", id->h);
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
|
︙ | | |
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
|
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
|
-
+
|
id->dirfd = -1;
sqlite3OsEnterMutex();
close(id->h);
sqlite3OsLeaveMutex();
id->isOpen = 0;
TRACE2("CLOSE %-3d\n", id->h);
OSTRACE2("CLOSE %-3d\n", id->h);
OpenCounter(-1);
sqlite3ThreadSafeFree(id);
*pId = 0;
return SQLITE_OK;
}
#endif /* SQLITE_ENABLE_LOCKING_STYLE */
|
︙ | | |
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
|
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
|
-
+
|
if( delFlag ){
unlink(zFilename);
}
if( rc ){
close(h);
return SQLITE_NOMEM;
}
TRACE3("OPEN %-3d %s\n", h, zFilename);
OSTRACE3("OPEN %-3d %s\n", h, zFilename);
f.dirfd = -1;
f.fullSync = 0;
f.locktype = 0;
f.offset = 0;
f.h = h;
SET_THREADID(&f);
pNew = sqlite3ThreadSafeMalloc( sizeof(unixFile) );
|
︙ | | |
︙ | | |
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
|
-
+
|
do{
rc = DeleteFileA(zConverted);
}while( rc==0 && GetFileAttributesA(zConverted)!=0xffffffff
&& cnt++ < MX_DELETION_ATTEMPTS && (Sleep(100), 1) );
#endif
}
sqliteFree(zConverted);
TRACE2("DELETE \"%s\"\n", zFilename);
OSTRACE2("DELETE \"%s\"\n", zFilename);
return rc!=0 ? SQLITE_OK : SQLITE_IOERR;
}
/*
** Return TRUE if the named file exists.
*/
int sqlite3WinFileExists(const char *zFilename){
|
︙ | | |
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
-
+
|
sqliteFree(zConverted);
f.h = h;
#if OS_WINCE
f.zDeleteOnClose = 0;
#endif
TRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
OSTRACE3("OPEN R/W %d \"%s\"\n", h, zFilename);
return allocateWinFile(&f, pId);
}
/*
** Attempt to open a new file for exclusive access by this process.
** The file will be opened for both reading and writing. To avoid
|
︙ | | |
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
|
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
|
-
+
|
f.hMutex = NULL;
#endif
sqliteFree(zConverted);
if( h==INVALID_HANDLE_VALUE ){
return SQLITE_CANTOPEN;
}
f.h = h;
TRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
OSTRACE3("OPEN EX %d \"%s\"\n", h, zFilename);
return allocateWinFile(&f, pId);
}
/*
** Attempt to open a new file for read-only access.
**
** On success, write the file handle into *id and return SQLITE_OK.
|
︙ | | |
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
|
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
|
-
+
|
return SQLITE_CANTOPEN;
}
f.h = h;
#if OS_WINCE
f.zDeleteOnClose = 0;
f.hMutex = NULL;
#endif
TRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
OSTRACE3("OPEN RO %d \"%s\"\n", h, zFilename);
return allocateWinFile(&f, pId);
}
/*
** Attempt to open a file descriptor for the directory that contains a
** file. This file descriptor can be used to fsync() the directory
** in order to make sure the creation of a new file is actually written
|
︙ | | |
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
|
-
+
-
+
|
sqlite3Randomness(15, &zBuf[j]);
for(i=0; i<15; i++, j++){
zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ];
}
zBuf[j] = 0;
if( !sqlite3OsFileExists(zBuf) ) break;
}
TRACE2("TEMP FILENAME: %s\n", zBuf);
OSTRACE2("TEMP FILENAME: %s\n", zBuf);
return SQLITE_OK;
}
/*
** Close a file.
**
** It is reported that an attempt to close a handle might sometimes
** fail. This is a very unreasonable result, but windows is notorious
** for being unreasonable so I do not doubt that it might happen. If
** the close fails, we pause for 100 milliseconds and try again. As
** many as MX_CLOSE_ATTEMPT attempts to close the handle are made before
** giving up and returning an error.
*/
#define MX_CLOSE_ATTEMPT 3
static int winClose(OsFile **pId){
winFile *pFile;
int rc = 1;
if( pId && (pFile = (winFile*)*pId)!=0 ){
int rc, cnt = 0;
TRACE2("CLOSE %d\n", pFile->h);
OSTRACE2("CLOSE %d\n", pFile->h);
do{
rc = CloseHandle(pFile->h);
}while( rc==0 && cnt++ < MX_CLOSE_ATTEMPT && (Sleep(100), 1) );
#if OS_WINCE
winceDestroyLock(pFile);
if( pFile->zDeleteOnClose ){
DeleteFileW(pFile->zDeleteOnClose);
|
︙ | | |
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
|
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
|
-
+
|
** bytes were read successfully and SQLITE_IOERR if anything goes
** wrong.
*/
static int winRead(OsFile *id, void *pBuf, int amt){
DWORD got;
assert( id!=0 );
SimulateIOError(return SQLITE_IOERR_READ);
TRACE3("READ %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
OSTRACE3("READ %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
if( !ReadFile(((winFile*)id)->h, pBuf, amt, &got, 0) ){
return SQLITE_IOERR_READ;
}
if( got==(DWORD)amt ){
return SQLITE_OK;
}else{
memset(&((char*)pBuf)[got], 0, amt-got);
|
︙ | | |
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
|
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
|
-
+
|
*/
static int winWrite(OsFile *id, const void *pBuf, int amt){
int rc = 0;
DWORD wrote;
assert( id!=0 );
SimulateIOError(return SQLITE_IOERR_READ);
SimulateDiskfullError(return SQLITE_FULL);
TRACE3("WRITE %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
OSTRACE3("WRITE %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
assert( amt>0 );
while( amt>0 && (rc = WriteFile(((winFile*)id)->h, pBuf, amt, &wrote, 0))!=0
&& wrote>0 ){
amt -= wrote;
pBuf = &((char*)pBuf)[wrote];
}
if( !rc || amt>(int)wrote ){
|
︙ | | |
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
|
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
|
-
+
-
+
|
DWORD rc;
assert( id!=0 );
#ifdef SQLITE_TEST
if( offset ) SimulateDiskfullError(return SQLITE_FULL);
#endif
SEEK(offset/1024 + 1);
rc = SetFilePointer(((winFile*)id)->h, lowerBits, &upperBits, FILE_BEGIN);
TRACE3("SEEK %d %lld\n", ((winFile*)id)->h, offset);
OSTRACE3("SEEK %d %lld\n", ((winFile*)id)->h, offset);
if( rc==INVALID_SET_FILE_POINTER && GetLastError()!=NO_ERROR ){
return SQLITE_FULL;
}
return SQLITE_OK;
}
/*
** Make sure all writes to a particular file are committed to disk.
*/
static int winSync(OsFile *id, int dataOnly){
assert( id!=0 );
TRACE3("SYNC %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
OSTRACE3("SYNC %d lock=%d\n", ((winFile*)id)->h, ((winFile*)id)->locktype);
if( FlushFileBuffers(((winFile*)id)->h) ){
return SQLITE_OK;
}else{
return SQLITE_IOERR;
}
}
|
︙ | | |
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
|
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
|
-
+
|
/*
** Truncate an open file to a specified size
*/
static int winTruncate(OsFile *id, i64 nByte){
LONG upperBits = nByte>>32;
assert( id!=0 );
TRACE3("TRUNCATE %d %lld\n", ((winFile*)id)->h, nByte);
OSTRACE3("TRUNCATE %d %lld\n", ((winFile*)id)->h, nByte);
SimulateIOError(return SQLITE_IOERR_TRUNCATE);
SetFilePointer(((winFile*)id)->h, nByte, &upperBits, FILE_BEGIN);
SetEndOfFile(((winFile*)id)->h);
return SQLITE_OK;
}
/*
|
︙ | | |
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
|
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
|
-
+
|
int rc = SQLITE_OK; /* Return code from subroutines */
int res = 1; /* Result of a windows lock call */
int newLocktype; /* Set id->locktype to this value before exiting */
int gotPendingLock = 0;/* True if we acquired a PENDING lock this time */
winFile *pFile = (winFile*)id;
assert( pFile!=0 );
TRACE5("LOCK %d %d was %d(%d)\n",
OSTRACE5("LOCK %d %d was %d(%d)\n",
pFile->h, locktype, pFile->locktype, pFile->sharedLockByte);
/* If there is already a lock of this type or more restrictive on the
** OsFile, do nothing. Don't use the end_lock: exit path, as
** sqlite3OsEnterMutex() hasn't been called yet.
*/
if( pFile->locktype>=locktype ){
|
︙ | | |
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
|
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
|
-
+
|
|| (locktype==EXCLUSIVE_LOCK && pFile->locktype==RESERVED_LOCK)
){
int cnt = 3;
while( cnt-->0 && (res = LockFile(pFile->h, PENDING_BYTE, 0, 1, 0))==0 ){
/* Try 3 times to get the pending lock. The pending lock might be
** held by another reader process who will release it momentarily.
*/
TRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
OSTRACE2("could not get a PENDING lock. cnt=%d\n", cnt);
Sleep(1);
}
gotPendingLock = res;
}
/* Acquire a shared lock
*/
|
︙ | | |
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
|
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
|
-
+
-
+
-
+
-
+
-
+
|
}
/* Acquire an EXCLUSIVE lock
*/
if( locktype==EXCLUSIVE_LOCK && res ){
assert( pFile->locktype>=SHARED_LOCK );
res = unlockReadLock(pFile);
TRACE2("unreadlock = %d\n", res);
OSTRACE2("unreadlock = %d\n", res);
res = LockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
if( res ){
newLocktype = EXCLUSIVE_LOCK;
}else{
TRACE2("error-code = %d\n", GetLastError());
OSTRACE2("error-code = %d\n", GetLastError());
}
}
/* If we are holding a PENDING lock that ought to be released, then
** release it now.
*/
if( gotPendingLock && locktype==SHARED_LOCK ){
UnlockFile(pFile->h, PENDING_BYTE, 0, 1, 0);
}
/* Update the state of the lock has held in the file descriptor then
** return the appropriate result code.
*/
if( res ){
rc = SQLITE_OK;
}else{
TRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
OSTRACE4("LOCK FAILED %d trying for %d but got %d\n", pFile->h,
locktype, newLocktype);
rc = SQLITE_BUSY;
}
pFile->locktype = newLocktype;
return rc;
}
/*
** This routine checks if there is a RESERVED lock held on the specified
** file by this or any other process. If such a lock is held, return
** non-zero, otherwise zero.
*/
static int winCheckReservedLock(OsFile *id){
int rc;
winFile *pFile = (winFile*)id;
assert( pFile!=0 );
if( pFile->locktype>=RESERVED_LOCK ){
rc = 1;
TRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
OSTRACE3("TEST WR-LOCK %d %d (local)\n", pFile->h, rc);
}else{
rc = LockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
if( rc ){
UnlockFile(pFile->h, RESERVED_BYTE, 0, 1, 0);
}
rc = !rc;
TRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
OSTRACE3("TEST WR-LOCK %d %d (remote)\n", pFile->h, rc);
}
return rc;
}
/*
** Lower the locking level on file descriptor id to locktype. locktype
** must be either NO_LOCK or SHARED_LOCK.
|
︙ | | |
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
|
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
|
-
+
|
*/
static int winUnlock(OsFile *id, int locktype){
int type;
int rc = SQLITE_OK;
winFile *pFile = (winFile*)id;
assert( pFile!=0 );
assert( locktype<=SHARED_LOCK );
TRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
OSTRACE5("UNLOCK %d to %d was %d(%d)\n", pFile->h, locktype,
pFile->locktype, pFile->sharedLockByte);
type = pFile->locktype;
if( type>=EXCLUSIVE_LOCK ){
UnlockFile(pFile->h, SHARED_FIRST, 0, SHARED_SIZE, 0);
if( locktype==SHARED_LOCK && !getReadLock(pFile) ){
/* This should never happen. We should always be able to
** reacquire the read lock */
|
︙ | | |
︙ | | |
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
|
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
|
-
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
+
|
** The pager is used to access a database disk file. It implements
** atomic commit and rollback through the use of a journal file that
** is separate from the database file. The pager also implements file
** locking to prevent two processes from writing the same database
** file simultaneously, or one process from reading the database while
** another is writing.
**
** @(#) $Id: pager.c,v 1.299 2007/03/26 15:46:01 danielk1977 Exp $
** @(#) $Id: pager.c,v 1.300 2007/03/26 22:05:02 drh Exp $
*/
#ifndef SQLITE_OMIT_DISKIO
#include "sqliteInt.h"
#include "os.h"
#include "pager.h"
#include <assert.h>
#include <string.h>
/*
** Macros for troubleshooting. Normally turned off
*/
#if 0
#define sqlite3DebugPrintf printf
#define TRACE1(X) sqlite3DebugPrintf(X)
#define TRACE2(X,Y) sqlite3DebugPrintf(X,Y)
#define TRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
#define TRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
#define TRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
#else
#define TRACE1(X)
#define TRACE2(X,Y)
#define TRACE3(X,Y,Z)
#define TRACE4(X,Y,Z,W)
#define TRACE5(X,Y,Z,W,V)
#define PAGERTRACE1(X)
#define PAGERTRACE2(X,Y)
#define PAGERTRACE3(X,Y,Z)
#define PAGERTRACE4(X,Y,Z,W)
#define PAGERTRACE5(X,Y,Z,W,V)
#endif
/*
** The following two macros are used within the TRACEX() macros above
** The following two macros are used within the PAGERTRACEX() macros above
** to print out file-descriptors.
**
** PAGERID() takes a pointer to a Pager struct as it's argument. The
** associated file-descriptor is returned. FILEHANDLEID() takes an OsFile
** struct as it's argument.
*/
#define PAGERID(p) ((int)(p->fd))
|
︙ | | |
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
|
-
+
|
** content. Two conditions must be met before writing to the database
** files. (1) the database must be locked. (2) we know that the original
** page content is in the main journal either because the page is not in
** cache or else it is marked as needSync==0.
*/
pPg = pager_lookup(pPager, pgno);
assert( pPager->state>=PAGER_EXCLUSIVE || pPg!=0 );
TRACE3("PLAYBACK %d page %d\n", PAGERID(pPager), pgno);
PAGERTRACE3("PLAYBACK %d page %d\n", PAGERID(pPager), pgno);
if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
if( rc==SQLITE_OK ){
rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize);
}
if( pPg ){
makeClean(pPg);
|
︙ | | |
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
|
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
|
-
+
|
char *zBuf = pPager->pTmpSpace; /* Temp storage for one page */
if( !pPg->dirty ) continue;
if( (int)pPg->pgno <= pPager->origDbSize ){
rc = sqlite3OsSeek(pPager->fd, pPager->pageSize*(i64)(pPg->pgno-1));
if( rc==SQLITE_OK ){
rc = sqlite3OsRead(pPager->fd, zBuf, pPager->pageSize);
}
TRACE3("REFETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
PAGERTRACE3("REFETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
if( rc ) break;
CODEC1(pPager, zBuf, pPg->pgno, 2);
}else{
memset(zBuf, 0, pPager->pageSize);
}
if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), pPager->pageSize) ){
memcpy(PGHDR_TO_DATA(pPg), zBuf, pPager->pageSize);
|
︙ | | |
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
|
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
|
-
+
|
if( !pPager || !zFullPathname || !pPager->pTmpSpace || rc!=SQLITE_OK ){
sqlite3OsClose(&fd);
sqliteFree(zFullPathname);
sqliteFree(pPager);
return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
}
TRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname);
PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(fd), zFullPathname);
IOTRACE(("OPEN %p %s\n", pPager, zFullPathname))
pPager->zFilename = (char*)&pPager[1];
pPager->zDirectory = &pPager->zFilename[nameLen+1];
pPager->zJournal = &pPager->zDirectory[nameLen+1];
strcpy(pPager->zFilename, zFullPathname);
strcpy(pPager->zDirectory, zFullPathname);
|
︙ | | |
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
|
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
|
-
+
|
** and returned.
*/
int sqlite3PagerSetPagesize(Pager *pPager, int pageSize){
assert( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE );
if( !pPager->memDb && pPager->nRef==0 ){
pager_reset(pPager);
pPager->pageSize = pageSize;
sqlite3ReallocOrFree((void **)&pPager->pTmpSpace, pageSize);
sqlite3ReallocOrFree(&pPager->pTmpSpace, pageSize);
}
return pPager->pageSize;
}
/*
** The following set of routines are used to disable the simulated
** I/O error mechanism. These routines are used to avoid simulated
|
︙ | | |
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
|
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
|
-
+
|
disable_simulated_io_errors();
pPager->errCode = 0;
pPager->exclusiveMode = 0;
pager_reset(pPager);
pagerUnlockAndRollback(pPager);
enable_simulated_io_errors();
TRACE2("CLOSE %d\n", PAGERID(pPager));
PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
IOTRACE(("CLOSE %p\n", pPager))
assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
if( pPager->journalOpen ){
sqlite3OsClose(&pPager->jfd);
}
sqliteFree(pPager->aInJournal);
if( pPager->stmtOpen ){
|
︙ | | |
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
|
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
|
-
+
-
+
|
{
/* Write the nRec value into the journal file header. If in
** full-synchronous mode, sync the journal first. This ensures that
** all data has really hit the disk before nRec is updated to mark
** it as a candidate for rollback.
*/
if( pPager->fullSync ){
TRACE2("SYNC journal of %d\n", PAGERID(pPager));
PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
IOTRACE(("JSYNC %p\n", pPager))
rc = sqlite3OsSync(pPager->jfd, 0);
if( rc!=0 ) return rc;
}
rc = sqlite3OsSeek(pPager->jfd,
pPager->journalHdr + sizeof(aJournalMagic));
if( rc ) return rc;
IOTRACE(("JHDR %p %lld %d\n", pPager,
pPager->journalHdr + sizeof(aJournalMagic), 4))
rc = write32bits(pPager->jfd, pPager->nRec);
if( rc ) return rc;
rc = sqlite3OsSeek(pPager->jfd, pPager->journalOff);
if( rc ) return rc;
}
TRACE2("SYNC journal of %d\n", PAGERID(pPager));
PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
IOTRACE(("JSYNC %d\n", pPager))
rc = sqlite3OsSync(pPager->jfd, pPager->full_fsync);
if( rc!=0 ) return rc;
pPager->journalStarted = 1;
}
pPager->needSync = 0;
|
︙ | | |
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
|
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
|
-
+
-
+
|
/* If there are dirty pages in the page cache with page numbers greater
** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
** make the file smaller (presumably by auto-vacuum code). Do not write
** any such pages to the file.
*/
if( pList->pgno<=pPager->dbSize ){
char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
TRACE3("STORE %d page %d\n", PAGERID(pPager), pList->pgno);
PAGERTRACE3("STORE %d page %d\n", PAGERID(pPager), pList->pgno);
IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno))
rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize);
TEST_INCR(pPager->nWrite);
}
#ifndef NDEBUG
else{
TRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
}
#endif
if( rc ) return rc;
pList->dirty = 0;
#ifdef SQLITE_CHECK_PAGES
pList->pageHash = pager_pagehash(pList);
#endif
|
︙ | | |
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
|
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
|
-
+
|
assert( MEMDB==0 );
rc = sqlite3OsSeek(pPager->fd, (pgno-1)*(i64)pPager->pageSize);
if( rc==SQLITE_OK ){
rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg),
pPager->pageSize);
}
IOTRACE(("PGIN %p %d\n", pPager, pgno))
TRACE3("FETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
PAGERTRACE3("FETCH %d page %d\n", PAGERID(pPager), pPg->pgno);
CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
pPg->pgno = 0;
sqlite3PagerUnref(pPg);
return rc;
}else{
TEST_INCR(pPager->nRead);
|
︙ | | |
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
|
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
|
-
+
|
rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
}
}
if( rc!=SQLITE_OK ){
return rc;
}
pPager->dirtyCache = 0;
TRACE2("TRANSACTION %d\n", PAGERID(pPager));
PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
if( pPager->useJournal && !pPager->tempFile ){
rc = pager_open_journal(pPager);
}
}
}else if( pPager->journalOpen && pPager->journalOff==0 ){
/* This happens when the pager was in exclusive-access mode last
** time a (read or write) transaction was successfully concluded
|
︙ | | |
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
|
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
|
-
+
|
** the transaction journal if it is not there already.
*/
if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
if( (int)pPg->pgno <= pPager->origDbSize ){
int szPg;
if( MEMDB ){
PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
TRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
assert( pHist->pOrig==0 );
pHist->pOrig = sqliteMallocRaw( pPager->pageSize );
if( pHist->pOrig ){
memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
}
}else{
u32 cksum, saved;
|
︙ | | |
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
|
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
|
-
+
|
put32bits(pEnd, cksum);
szPg = pPager->pageSize+8;
put32bits(pData2, pPg->pgno);
rc = sqlite3OsWrite(pPager->jfd, pData2, szPg);
IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
pPager->journalOff, szPg))
pPager->journalOff += szPg;
TRACE4("JOURNAL %d page %d needSync=%d\n",
PAGERTRACE4("JOURNAL %d page %d needSync=%d\n",
PAGERID(pPager), pPg->pgno, pPg->needSync);
*(u32*)pEnd = saved;
/* An error has occured writing to the journal file. The
** transaction will be rolled back by the layer above.
*/
if( rc!=SQLITE_OK ){
|
︙ | | |
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
|
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
|
-
+
|
if( pPager->stmtInUse ){
pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
page_add_to_stmt_list(pPg);
}
}
}else{
pPg->needSync = !pPager->journalStarted && !pPager->noSync;
TRACE4("APPEND %d page %d needSync=%d\n",
PAGERTRACE4("APPEND %d page %d needSync=%d\n",
PAGERID(pPager), pPg->pgno, pPg->needSync);
}
if( pPg->needSync ){
pPager->needSync = 1;
}
pPg->inJournal = 1;
}
|
︙ | | |
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
|
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
|
-
+
-
+
|
if( MEMDB ){
PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
assert( pHist->pStmt==0 );
pHist->pStmt = sqliteMallocRaw( pPager->pageSize );
if( pHist->pStmt ){
memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
}
TRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
}else{
char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
put32bits(pData2, pPg->pgno);
rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4);
TRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
if( rc!=SQLITE_OK ){
return rc;
}
pPager->stmtNRec++;
assert( pPager->aInStmt!=0 );
pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
}
|
︙ | | |
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
|
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
|
-
+
|
** When the database file grows, we must make sure that the last page
** gets written at least once so that the disk file will be the correct
** size. If you do not write this page and the size of the file
** on the disk ends up being too small, that can lead to database
** corruption during the next transaction.
*/
}else{
TRACE3("DONT_WRITE page %d of %d\n", pgno, PAGERID(pPager));
PAGERTRACE3("DONT_WRITE page %d of %d\n", pgno, PAGERID(pPager));
IOTRACE(("CLEAN %p %d\n", pPager, pgno))
makeClean(pPg);
#ifdef SQLITE_CHECK_PAGES
pPg->pageHash = pager_pagehash(pPg);
#endif
}
}
|
︙ | | |
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
|
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
|
-
+
|
assert( pPager->aInJournal!=0 );
pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
pPg->inJournal = 1;
if( pPager->stmtInUse ){
pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
page_add_to_stmt_list(pPg);
}
TRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
}
if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
assert( pPager->aInStmt!=0 );
pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
page_add_to_stmt_list(pPg);
|
︙ | | |
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
|
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
|
-
+
|
if( pPager->errCode ){
return pPager->errCode;
}
if( pPager->state<PAGER_RESERVED ){
return SQLITE_ERROR;
}
TRACE2("COMMIT %d\n", PAGERID(pPager));
PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
if( MEMDB ){
pPg = pager_get_all_dirty_pages(pPager);
while( pPg ){
clearHistory(PGHDR_TO_HIST(pPg, pPager));
pPg->dirty = 0;
pPg->inJournal = 0;
pPg->inStmt = 0;
|
︙ | | |
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
|
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
|
-
+
-
+
-
+
|
** process is writing trash into the journal file (SQLITE_CORRUPT) or
** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
** codes are returned for all these occasions. Otherwise,
** SQLITE_OK is returned.
*/
int sqlite3PagerRollback(Pager *pPager){
int rc;
TRACE2("ROLLBACK %d\n", PAGERID(pPager));
PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
if( MEMDB ){
PgHdr *p;
for(p=pPager->pAll; p; p=p->pNextAll){
PgHistory *pHist;
assert( !p->alwaysRollback );
if( !p->dirty ){
assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
continue;
}
pHist = PGHDR_TO_HIST(p, pPager);
if( pHist->pOrig ){
memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
TRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
}else{
TRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
}
clearHistory(pHist);
p->dirty = 0;
p->inJournal = 0;
p->inStmt = 0;
p->pPrevStmt = p->pNextStmt = 0;
if( pPager->xReiniter ){
|
︙ | | |
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
|
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
|
-
+
|
** changes of a single SQL command within a larger transaction.
*/
int sqlite3PagerStmtBegin(Pager *pPager){
int rc;
assert( !pPager->stmtInUse );
assert( pPager->state>=PAGER_SHARED );
assert( pPager->dbSize>=0 );
TRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
if( MEMDB ){
pPager->stmtInUse = 1;
pPager->stmtSize = pPager->dbSize;
return SQLITE_OK;
}
if( !pPager->journalOpen ){
pPager->stmtAutoopen = 1;
|
︙ | | |
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
|
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
|
-
+
|
/*
** Commit a statement.
*/
int sqlite3PagerStmtCommit(Pager *pPager){
if( pPager->stmtInUse ){
PgHdr *pPg, *pNext;
TRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
if( !MEMDB ){
sqlite3OsSeek(pPager->stfd, 0);
/* sqlite3OsTruncate(pPager->stfd, 0); */
sqliteFree( pPager->aInStmt );
pPager->aInStmt = 0;
}
for(pPg=pPager->pStmt; pPg; pPg=pNext){
|
︙ | | |
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
|
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
|
-
+
|
/*
** Rollback a statement.
*/
int sqlite3PagerStmtRollback(Pager *pPager){
int rc;
if( pPager->stmtInUse ){
TRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
if( MEMDB ){
PgHdr *pPg;
for(pPg=pPager->pStmt; pPg; pPg=pPg->pNextStmt){
PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
if( pHist->pStmt ){
memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
sqliteFree(pHist->pStmt);
|
︙ | | |
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
|
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
|
-
+
|
**
** If parameter nTrunc is non-zero, then the pager file is truncated to
** nTrunc pages (this is used by auto-vacuum databases).
*/
int sqlite3PagerSync(Pager *pPager, const char *zMaster, Pgno nTrunc){
int rc = SQLITE_OK;
TRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
pPager->zFilename, zMaster, nTrunc);
/* If this is an in-memory db, or no pages have been written to, or this
** function has already been called, it is a no-op.
*/
if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
PgHdr *pPg;
|
︙ | | |
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
|
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
|
-
+
|
int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
PgHdr *pPgOld;
int h;
Pgno needSyncPgno = 0;
assert( pPg->nRef>0 );
TRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
if( pPg->needSync ){
needSyncPgno = pPg->pgno;
assert( pPg->inJournal );
assert( pPg->dirty );
|
︙ | | |
︙ | | |
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
-
+
|
** May you share freely, never taking more than you give.
**
*************************************************************************
** Code for testing all sorts of SQLite interfaces. This code
** is not included in the SQLite library. It is used for automated
** testing of the SQLite library.
**
** $Id: test1.c,v 1.229 2007/02/24 13:53:05 drh Exp $
** $Id: test1.c,v 1.230 2007/03/26 22:05:02 drh Exp $
*/
#include "sqliteInt.h"
#include "tcl.h"
#include "os.h"
#include <stdlib.h>
#include <string.h>
|
︙ | | |
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
|
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
|
-
+
-
+
|
case SQLITE_ROW: zName = "SQLITE_ROW"; break;
case SQLITE_DONE: zName = "SQLITE_DONE"; break;
case SQLITE_NOTADB: zName = "SQLITE_NOTADB"; break;
default: zName = "SQLITE_Unknown"; break;
}
return zName;
}
#define errorName sqlite3TestErrorName
#define t1ErrorName sqlite3TestErrorName
/*
** Convert an sqlite3_stmt* into an sqlite3*. This depends on the
** fact that the sqlite3* is the first field in the Vdbe structure.
*/
#define StmtToDb(X) sqlite3_db_handle(X)
/*
** Check a return value to make sure it agrees with the results
** from sqlite3_errcode.
*/
int sqlite3TestErrCode(Tcl_Interp *interp, sqlite3 *db, int rc){
if( rc!=SQLITE_MISUSE && rc!=SQLITE_OK && sqlite3_errcode(db)!=rc ){
char zBuf[200];
int r2 = sqlite3_errcode(db);
sprintf(zBuf, "error code %s (%d) does not match sqlite3_errcode %s (%d)",
errorName(rc), rc, errorName(r2), r2);
t1ErrorName(rc), rc, t1ErrorName(r2), r2);
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, zBuf, 0);
return 1;
}
return 0;
}
|
︙ | | |
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
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
|
-
+
+
-
+
+
+
+
|
if( argc!=2 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" FILENAME\"", 0);
return TCL_ERROR;
}
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
rc = sqlite3_close(db);
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_OK;
}
/*
** Implementation of the x_coalesce() function.
** Return the first argument non-NULL argument.
*/
static void t1_ifnullFunc(
static void ifnullFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
int i;
for(i=0; i<argc; i++){
if( SQLITE_NULL!=sqlite3_value_type(argv[i]) ){
sqlite3_result_text(context, (char*)sqlite3_value_text(argv[i]),
sqlite3_value_bytes(argv[i]), SQLITE_TRANSIENT);
break;
}
|
︙ | | |
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
|
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
|
-
+
|
if( argc!=2 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DB\"", 0);
return TCL_ERROR;
}
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
rc = sqlite3_create_function(db, "x_coalesce", -1, SQLITE_ANY, 0,
ifnullFunc, 0, 0);
t1_ifnullFunc, 0, 0);
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "hex8", 1, SQLITE_ANY, 0,
hex8Func, 0, 0);
}
if( rc==SQLITE_OK ){
rc = sqlite3_create_function(db, "hex16", 1, SQLITE_ANY, 0,
hex16Func, 0, 0);
|
︙ | | |
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
|
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
|
-
+
-
-
+
+
+
-
-
+
+
+
+
+
-
-
+
+
|
sqlite3ValueText(pVal, SQLITE_UTF16NATIVE),
1, SQLITE_UTF16, db, sqlite3ExecFunc, 0, 0);
sqlite3ValueFree(pVal);
}
#endif
if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
Tcl_SetResult(interp, (char *)errorName(rc), 0);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
return TCL_OK;
}
/*
** Routines to implement the x_count() aggregate function.
**
** x_count() counts the number of non-null arguments. But there are
** some twists for testing purposes.
**
** If the argument to x_count() is 40 then a UTF-8 error is reported
** on the step function. If x_count(41) is seen, then a UTF-16 error
** is reported on the step function. If the total count is 42, then
** a UTF-8 error is reported on the finalize function.
*/
typedef struct CountCtx CountCtx;
struct CountCtx {
typedef struct t1CountCtx t1CountCtx;
struct t1CountCtx {
int n;
};
static void t1CountStep(
static void countStep(sqlite3_context *context, int argc, sqlite3_value **argv){
CountCtx *p;
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
t1CountCtx *p;
p = sqlite3_aggregate_context(context, sizeof(*p));
if( (argc==0 || SQLITE_NULL!=sqlite3_value_type(argv[0]) ) && p ){
p->n++;
}
if( argc>0 ){
int v = sqlite3_value_int(argv[0]);
if( v==40 ){
sqlite3_result_error(context, "value of 40 handed to x_count", -1);
#ifndef SQLITE_OMIT_UTF16
}else if( v==41 ){
const char zUtf16ErrMsg[] = { 0, 0x61, 0, 0x62, 0, 0x63, 0, 0, 0};
sqlite3_result_error16(context, &zUtf16ErrMsg[1-SQLITE_BIGENDIAN], -1);
#endif
}
}
}
static void countFinalize(sqlite3_context *context){
CountCtx *p;
static void t1CountFinalize(sqlite3_context *context){
t1CountCtx *p;
p = sqlite3_aggregate_context(context, sizeof(*p));
if( p ){
if( p->n==42 ){
sqlite3_result_error(context, "x_count totals to 42", -1);
}else{
sqlite3_result_int(context, p ? p->n : 0);
}
|
︙ | | |
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
|
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
|
-
+
-
+
|
if( argc!=2 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" FILENAME\"", 0);
return TCL_ERROR;
}
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
rc = sqlite3_create_function(db, "x_count", 0, SQLITE_UTF8, 0, 0,
countStep,countFinalize);
t1CountStep,t1CountFinalize);
if( rc==SQLITE_OK ){
sqlite3_create_function(db, "x_count", 1, SQLITE_UTF8, 0, 0,
countStep,countFinalize);
t1CountStep,t1CountFinalize);
}
if( sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
return TCL_OK;
}
/*
|
︙ | | |
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
|
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
|
-
+
|
if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
if( pStmt ){
db = StmtToDb(pStmt);
}
rc = sqlite3_finalize(pStmt);
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
if( db && sqlite3TestErrCode(interp, db, rc) ) return TCL_ERROR;
return TCL_OK;
}
/*
** Usage: sqlite3_reset STMT
**
|
︙ | | |
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
|
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
|
-
+
|
if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
rc = sqlite3_reset(pStmt);
if( pStmt && sqlite3TestErrCode(interp, StmtToDb(pStmt), rc) ){
return TCL_ERROR;
}
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
/*
if( rc ){
return TCL_ERROR;
}
*/
return TCL_OK;
}
|
︙ | | |
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
|
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
|
-
+
|
int i;
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "<error code>");
}
zCode = Tcl_GetString(objv[1]);
for(i=0; i<200; i++){
if( 0==strcmp(errorName(i), zCode) ) break;
if( 0==strcmp(t1ErrorName(i), zCode) ) break;
}
Tcl_SetResult(interp, (char *)sqlite3ErrStr(i), 0);
return TCL_OK;
}
/*
** Usage: breakpoint
|
︙ | | |
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
|
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
|
-
+
|
if( getDbPointer(interp, Tcl_GetString(objv[1]), &db) ) return TCL_ERROR;
rc = sqlite3_errcode(db);
if( (rc&0xff)==rc ){
zBuf[0] = 0;
}else{
sprintf(zBuf,"+%d", rc>>8);
}
Tcl_AppendResult(interp, (char *)errorName(rc), zBuf, 0);
Tcl_AppendResult(interp, (char *)t1ErrorName(rc), zBuf, 0);
return TCL_OK;
}
/*
** Usage: test_errmsg DB
**
** Returns the UTF-8 representation of the error message string for the
|
︙ | | |
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
|
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
|
-
+
|
return TCL_ERROR;
}
if( getStmtPointer(interp, Tcl_GetString(objv[1]), &pStmt) ) return TCL_ERROR;
rc = sqlite3_step(pStmt);
/* if( rc!=SQLITE_DONE && rc!=SQLITE_ROW ) return TCL_ERROR; */
Tcl_SetResult(interp, (char *)errorName(rc), 0);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), 0);
return TCL_OK;
}
/*
** Usage: sqlite3_column_count STMT
**
** Return the number of columns returned by the sql statement STMT.
|
︙ | | |
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
|
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
|
-
+
|
#ifndef SQLITE_OMIT_GLOBALRECOVER
int rc;
if( objc!=1 ){
Tcl_WrongNumArgs(interp, 1, objv, "");
return TCL_ERROR;
}
rc = sqlite3_global_recover();
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
#endif
return TCL_OK;
}
/*
** Usage: sqlite3_column_text STMT column
**
|
︙ | | |
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
|
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
|
-
+
|
Tcl_AppendResult(interp, "wrong # args: should be \"",
Tcl_GetString(objv[0]), " filename", 0);
return TCL_ERROR;
}
rc = sqlite3OsOpenReadWrite(Tcl_GetString(objv[1]), &pFile, &dummy);
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_ERROR;
}
sqlite3TestMakePointerStr(interp, zBuf, pFile);
Tcl_SetResult(interp, zBuf, 0);
return TCL_ERROR;
}
|
︙ | | |
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
|
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
|
-
+
|
}
if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
return TCL_ERROR;
}
rc = sqlite3OsClose(&pFile);
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: sqlite3OsLock <file handle> <locktype>
|
︙ | | |
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
|
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
|
-
+
|
Tcl_AppendResult(interp, "wrong # args: should be \"",
Tcl_GetString(objv[0]),
" filehandle (SHARED|RESERVED|PENDING|EXCLUSIVE)", 0);
return TCL_ERROR;
}
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: sqlite3OsUnlock <file handle>
|
︙ | | |
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
|
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
|
-
+
-
+
|
}
if( getFilePointer(interp, Tcl_GetString(objv[1]), &pFile) ){
return TCL_ERROR;
}
rc = sqlite3OsUnlock(pFile, NO_LOCK);
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_ERROR;
}
return TCL_OK;
}
/*
** Usage: sqlite3OsTempFileName
*/
static int test_sqlite3OsTempFileName(
void * clientData,
Tcl_Interp *interp,
int objc,
Tcl_Obj *CONST objv[]
){
char zFile[SQLITE_TEMPNAME_SIZE];
int rc;
rc = sqlite3OsTempFileName(zFile);
if( rc!=SQLITE_OK ){
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_ERROR;
}
Tcl_AppendResult(interp, zFile, 0);
return TCL_OK;
}
#endif
|
︙ | | |
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
|
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
|
-
+
|
if( argc!=3 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DB function-name", 0);
return TCL_ERROR;
}
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
rc = sqlite3_create_function(db, argv[2], -1, SQLITE_UTF8, 0, 0, 0, 0);
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_OK;
}
/*
** Usage: sqlite_delete_collation DB collation-name
**
** Delete the collation sequence 'collation-name' from database handle
|
︙ | | |
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
|
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
|
-
+
|
if( argc!=3 ){
Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
" DB function-name", 0);
return TCL_ERROR;
}
if( getDbPointer(interp, argv[1], &db) ) return TCL_ERROR;
rc = sqlite3_create_collation(db, argv[2], SQLITE_UTF8, 0, 0);
Tcl_SetResult(interp, (char *)errorName(rc), TCL_STATIC);
Tcl_SetResult(interp, (char *)t1ErrorName(rc), TCL_STATIC);
return TCL_OK;
}
/*
** Usage: sqlite3_get_autocommit DB
**
** Return true if the database DB is currently in auto-commit mode.
|
︙ | | |
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
|
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
|
-
+
|
static int bitmask_size = sizeof(Bitmask)*8;
int i;
extern int sqlite3_os_trace;
extern int sqlite3_where_trace;
extern int sqlite3_sync_count, sqlite3_fullsync_count;
extern int sqlite3_opentemp_count;
extern int sqlite3_memUsed;
extern int sqlite3_malloc_id;
extern char *sqlite3_malloc_id;
extern int sqlite3_memMax;
extern int sqlite3_like_count;
extern int sqlite3_tsd_count;
extern int sqlite3_xferopt_count;
#if OS_UNIX && defined(SQLITE_TEST) && defined(THREADSAFE) && THREADSAFE
extern int threadsOverrideEachOthersLocks;
#endif
|
︙ | | |
︙ | | |
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
-
+
|
/* Forward references */
typedef struct AsyncWrite AsyncWrite;
typedef struct AsyncFile AsyncFile;
/* Enable for debugging */
static int sqlite3async_trace = 0;
# define TRACE(X) if( sqlite3async_trace ) asyncTrace X
# define ASYNC_TRACE(X) if( sqlite3async_trace ) asyncTrace X
static void asyncTrace(const char *zFormat, ...){
char *z;
va_list ap;
va_start(ap, zFormat);
z = sqlite3_vmprintf(zFormat, ap);
va_end(ap);
fprintf(stderr, "[%d] %s", (int)pthread_self(), z);
|
︙ | | |
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
-
+
|
if( async.pQueueLast ){
assert( async.pQueueFirst );
async.pQueueLast->pNext = pWrite;
}else{
async.pQueueFirst = pWrite;
}
async.pQueueLast = pWrite;
TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op],
ASYNC_TRACE(("PUSH %p (%s %s %d)\n", pWrite, azOpcodeName[pWrite->op],
pWrite->pFile ? pWrite->pFile->zName : "-", pWrite->iOffset));
if( pWrite->op==ASYNC_CLOSE ){
async.nFile--;
if( async.nFile==0 ){
async.ioError = SQLITE_OK;
}
|
︙ | | |
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
-
+
-
+
|
rc = sqlite3OsSeek(pBase, pFile->iOffset);
if( rc!=SQLITE_OK ){
goto asyncread_out;
}
nRead = MIN(filesize - pFile->iOffset, amt);
if( nRead>0 ){
rc = sqlite3OsRead(pBase, obuf, nRead);
TRACE(("READ %s %d bytes at %d\n", pFile->zName, nRead, pFile->iOffset));
ASYNC_TRACE(("READ %s %d bytes at %d\n", pFile->zName, nRead, pFile->iOffset));
}
}
if( rc==SQLITE_OK ){
AsyncWrite *p;
i64 iOffset = pFile->iOffset; /* Current seek offset */
for(p=async.pQueueFirst; p; p = p->pNext){
if( p->pFile==pFile && p->op==ASYNC_WRITE ){
int iBeginOut = (p->iOffset - iOffset);
int iBeginIn = -iBeginOut;
int nCopy;
if( iBeginIn<0 ) iBeginIn = 0;
if( iBeginOut<0 ) iBeginOut = 0;
nCopy = MIN(p->nByte-iBeginIn, amt-iBeginOut);
if( nCopy>0 ){
memcpy(&((char *)obuf)[iBeginOut], &p->zBuf[iBeginIn], nCopy);
TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset));
ASYNC_TRACE(("OVERREAD %d bytes at %d\n", nCopy, iBeginOut+iOffset));
}
}
}
pFile->iOffset += (i64)amt;
}
|
︙ | | |
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
655
656
|
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
655
656
|
-
+
-
+
|
** as with standard (unmodified) SQLite as long as all connections
** come from this one process. Connections from external processes
** cannot see our internal hash table (obviously) and will thus not
** honor our locks.
*/
static int asyncLock(OsFile *id, int lockType){
AsyncFile *pFile = (AsyncFile*)id;
TRACE(("LOCK %d (%s)\n", lockType, pFile->zName));
ASYNC_TRACE(("LOCK %d (%s)\n", lockType, pFile->zName));
pthread_mutex_lock(&async.lockMutex);
sqlite3HashInsert(&async.aLock, pFile->zName, pFile->nName, (void*)lockType);
pthread_mutex_unlock(&async.lockMutex);
return SQLITE_OK;
}
static int asyncUnlock(OsFile *id, int lockType){
return asyncLock(id, lockType);
}
/*
** This function is called when the pager layer first opens a database file
** and is checking for a hot-journal.
*/
static int asyncCheckReservedLock(OsFile *id){
AsyncFile *pFile = (AsyncFile*)id;
int rc;
pthread_mutex_lock(&async.lockMutex);
rc = (int)sqlite3HashFind(&async.aLock, pFile->zName, pFile->nName);
pthread_mutex_unlock(&async.lockMutex);
TRACE(("CHECK-LOCK %d (%s)\n", rc, pFile->zName));
ASYNC_TRACE(("CHECK-LOCK %d (%s)\n", rc, pFile->zName));
return rc>SHARED_LOCK;
}
static int asyncSectorSize(OsFile *id){
/* TODO: This is tricky to implement, as this backend might not have
** an open file handle at this point.
*/
|
︙ | | |
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
-
+
|
if( p->op==ASYNC_DELETE && 0==strcmp(p->zBuf, z) ){
ret = 0;
}else if( p->op==ASYNC_OPENEXCLUSIVE && 0==strcmp(p->zBuf, z) ){
ret = 1;
}
}
TRACE(("EXISTS: %s = %d\n", z, ret));
ASYNC_TRACE(("EXISTS: %s = %d\n", z, ret));
pthread_mutex_unlock(&async.queueMutex);
return ret;
}
/*
** Call this routine to enable or disable the
** asynchronous IO features implemented in this file.
|
︙ | | |
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
|
-
+
-
+
|
}
while( (p = async.pQueueFirst)==0 ){
pthread_cond_broadcast(&async.emptySignal);
if( async.writerHaltWhenIdle ){
pthread_mutex_unlock(&async.queueMutex);
break;
}else{
TRACE(("IDLE\n"));
ASYNC_TRACE(("IDLE\n"));
pthread_cond_wait(&async.queueSignal, &async.queueMutex);
TRACE(("WAKEUP\n"));
ASYNC_TRACE(("WAKEUP\n"));
}
}
if( p==0 ) break;
holdingMutex = 1;
/* Right now this thread is holding the mutex on the write-op queue.
** Variable 'p' points to the first entry in the write-op queue. In
|
︙ | | |
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
|
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
switch( p->op ){
case ASYNC_NOOP:
break;
case ASYNC_WRITE:
assert( pBase );
TRACE(("WRITE %s %d bytes at %d\n",
ASYNC_TRACE(("WRITE %s %d bytes at %d\n",
p->pFile->zName, p->nByte, p->iOffset));
rc = sqlite3OsSeek(pBase, p->iOffset);
if( rc==SQLITE_OK ){
rc = sqlite3OsWrite(pBase, (const void *)(p->zBuf), p->nByte);
}
break;
case ASYNC_SYNC:
assert( pBase );
TRACE(("SYNC %s\n", p->pFile->zName));
ASYNC_TRACE(("SYNC %s\n", p->pFile->zName));
rc = sqlite3OsSync(pBase, p->nByte);
break;
case ASYNC_TRUNCATE:
assert( pBase );
TRACE(("TRUNCATE %s to %d bytes\n", p->pFile->zName, p->iOffset));
ASYNC_TRACE(("TRUNCATE %s to %d bytes\n", p->pFile->zName, p->iOffset));
rc = sqlite3OsTruncate(pBase, p->iOffset);
break;
case ASYNC_CLOSE:
TRACE(("CLOSE %s\n", p->pFile->zName));
ASYNC_TRACE(("CLOSE %s\n", p->pFile->zName));
sqlite3OsClose(&p->pFile->pBaseWrite);
sqlite3OsClose(&p->pFile->pBaseRead);
sqlite3OsFree(p->pFile);
break;
case ASYNC_OPENDIRECTORY:
assert( pBase );
TRACE(("OPENDIR %s\n", p->zBuf));
ASYNC_TRACE(("OPENDIR %s\n", p->zBuf));
sqlite3OsOpenDirectory(pBase, p->zBuf);
break;
case ASYNC_SETFULLSYNC:
assert( pBase );
TRACE(("SETFULLSYNC %s %d\n", p->pFile->zName, p->nByte));
ASYNC_TRACE(("SETFULLSYNC %s %d\n", p->pFile->zName, p->nByte));
sqlite3OsSetFullSync(pBase, p->nByte);
break;
case ASYNC_DELETE:
TRACE(("DELETE %s\n", p->zBuf));
ASYNC_TRACE(("DELETE %s\n", p->zBuf));
rc = xOrigDelete(p->zBuf);
break;
case ASYNC_SYNCDIRECTORY:
TRACE(("SYNCDIR %s\n", p->zBuf));
ASYNC_TRACE(("SYNCDIR %s\n", p->zBuf));
rc = xOrigSyncDirectory(p->zBuf);
break;
case ASYNC_OPENEXCLUSIVE: {
AsyncFile *pFile = p->pFile;
int delFlag = ((p->iOffset)?1:0);
OsFile *pBase = 0;
TRACE(("OPEN %s delFlag=%d\n", p->zBuf, delFlag));
ASYNC_TRACE(("OPEN %s delFlag=%d\n", p->zBuf, delFlag));
assert(pFile->pBaseRead==0 && pFile->pBaseWrite==0);
rc = xOrigOpenExclusive(p->zBuf, &pBase, delFlag);
assert( holdingMutex==0 );
pthread_mutex_lock(&async.queueMutex);
holdingMutex = 1;
if( rc==SQLITE_OK ){
pFile->pBaseRead = pBase;
|
︙ | | |
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
|
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
|
-
+
|
** so that the AsyncWrite structure can be safely removed from the
** global write-op queue.
*/
if( !holdingMutex ){
pthread_mutex_lock(&async.queueMutex);
holdingMutex = 1;
}
/* TRACE(("UNLINK %p\n", p)); */
/* ASYNC_TRACE(("UNLINK %p\n", p)); */
if( p==async.pQueueLast ){
async.pQueueLast = 0;
}
async.pQueueFirst = p->pNext;
sqlite3OsFree(p);
assert( holdingMutex );
|
︙ | | |
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
|
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
|
-
+
-
+
|
}
while( cnt-- && !pthread_mutex_trylock(&async.writerMutex) ){
pthread_mutex_unlock(&async.writerMutex);
sched_yield();
}
if( cnt>=0 ){
TRACE(("WAIT\n"));
ASYNC_TRACE(("WAIT\n"));
pthread_mutex_lock(&async.queueMutex);
pthread_cond_broadcast(&async.queueSignal);
pthread_mutex_unlock(&async.queueMutex);
pthread_mutex_lock(&async.writerMutex);
pthread_mutex_unlock(&async.writerMutex);
}else{
TRACE(("NO-WAIT\n"));
ASYNC_TRACE(("NO-WAIT\n"));
}
return TCL_OK;
}
#endif /* OS_UNIX and THREADSAFE and defined(SQLITE_ENABLE_REDEF_IO) */
|
︙ | | |
︙ | | |
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
|
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
|
-
+
-
+
-
+
|
** This module contains C code that generates VDBE code used to process
** the WHERE clause of SQL statements. This module is reponsible for
** generating the code that loops through a table looking for applicable
** rows. Indices are selected and used to speed the search when doing
** so is applicable. Because this module is responsible for selecting
** indices, you might also think of this module as the "query optimizer".
**
** $Id: where.c,v 1.239 2007/03/02 08:12:22 danielk1977 Exp $
** $Id: where.c,v 1.240 2007/03/26 22:05:02 drh Exp $
*/
#include "sqliteInt.h"
/*
** The number of bits in a Bitmask. "BMS" means "BitMask Size".
*/
#define BMS (sizeof(Bitmask)*8)
/*
** Determine the number of elements in an array.
*/
#define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0]))
/*
** Trace output macros
*/
#if defined(SQLITE_TEST) || defined(SQLITE_DEBUG)
int sqlite3_where_trace = 0;
# define TRACE(X) if(sqlite3_where_trace) sqlite3DebugPrintf X
# define WHERETRACE(X) if(sqlite3_where_trace) sqlite3DebugPrintf X
#else
# define TRACE(X)
# define WHERETRACE(X)
#endif
/* Forward reference
*/
typedef struct WhereClause WhereClause;
typedef struct ExprMaskSet ExprMaskSet;
|
︙ | | |
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
|
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
|
-
+
|
** allocated and initialized for this virtual table, then allocate
** and initialize it now
*/
pIdxInfo = *ppIdxInfo;
if( pIdxInfo==0 ){
WhereTerm *pTerm;
int nTerm;
TRACE(("Recomputing index info for %s...\n", pTab->zName));
WHERETRACE(("Recomputing index info for %s...\n", pTab->zName));
/* Count the number of possible WHERE clause constraints referring
** to this virtual table */
for(i=nTerm=0, pTerm=pWC->a; i<pWC->nTerm; i++, pTerm++){
if( pTerm->leftCursor != pSrc->iCursor ) continue;
if( pTerm->eOperator==WO_IN ) continue;
nTerm++;
|
︙ | | |
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
|
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
|
-
+
|
pIdxInfo->estimatedCost = SQLITE_BIG_DBL / 2.0;
nOrderBy = pIdxInfo->nOrderBy;
if( pIdxInfo->nOrderBy && !orderByUsable ){
*(int*)&pIdxInfo->nOrderBy = 0;
}
sqlite3SafetyOff(pParse->db);
TRACE(("xBestIndex for %s\n", pTab->zName));
WHERETRACE(("xBestIndex for %s\n", pTab->zName));
TRACE_IDX_INPUTS(pIdxInfo);
rc = pTab->pVtab->pModule->xBestIndex(pTab->pVtab, pIdxInfo);
TRACE_IDX_OUTPUTS(pIdxInfo);
if( rc!=SQLITE_OK ){
if( rc==SQLITE_NOMEM ){
sqlite3FailedMalloc();
}else {
|
︙ | | |
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
|
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
|
-
+
|
Index *pProbe; /* An index we are evaluating */
int rev; /* True to scan in reverse order */
int flags; /* Flags associated with pProbe */
int nEq; /* Number of == or IN constraints */
int eqTermMask; /* Mask of valid equality operators */
double cost; /* Cost of using pProbe */
TRACE(("bestIndex: tbl=%s notReady=%x\n", pSrc->pTab->zName, notReady));
WHERETRACE(("bestIndex: tbl=%s notReady=%x\n", pSrc->pTab->zName, notReady));
lowestCost = SQLITE_BIG_DBL;
pProbe = pSrc->pTab->pIndex;
/* If the table has no indices and there are no terms in the where
** clause that refer to the ROWID, then we will never be able to do
** anything other than a full table scan on this table. We might as
** well put it first in the join order. That way, perhaps it can be
|
︙ | | |
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
|
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
|
-
+
-
+
-
+
-
+
-
+
|
*ppIndex = 0;
bestFlags = WHERE_ROWID_EQ;
if( pTerm->eOperator & WO_EQ ){
/* Rowid== is always the best pick. Look no further. Because only
** a single row is generated, output is always in sorted order */
*pFlags = WHERE_ROWID_EQ | WHERE_UNIQUE;
*pnEq = 1;
TRACE(("... best is rowid\n"));
WHERETRACE(("... best is rowid\n"));
return 0.0;
}else if( (pExpr = pTerm->pExpr)->pList!=0 ){
/* Rowid IN (LIST): cost is NlogN where N is the number of list
** elements. */
lowestCost = pExpr->pList->nExpr;
lowestCost *= estLog(lowestCost);
}else{
/* Rowid IN (SELECT): cost is NlogN where N is the number of rows
** in the result of the inner select. We have no way to estimate
** that value so make a wild guess. */
lowestCost = 200;
}
TRACE(("... rowid IN cost: %.9g\n", lowestCost));
WHERETRACE(("... rowid IN cost: %.9g\n", lowestCost));
}
/* Estimate the cost of a table scan. If we do not know how many
** entries are in the table, use 1 million as a guess.
*/
cost = pProbe ? pProbe->aiRowEst[0] : 1000000;
TRACE(("... table scan base cost: %.9g\n", cost));
WHERETRACE(("... table scan base cost: %.9g\n", cost));
flags = WHERE_ROWID_RANGE;
/* Check for constraints on a range of rowids in a table scan.
*/
pTerm = findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE|WO_GT|WO_GE, 0);
if( pTerm ){
if( findTerm(pWC, iCur, -1, notReady, WO_LT|WO_LE, 0) ){
flags |= WHERE_TOP_LIMIT;
cost /= 3; /* Guess that rowid<EXPR eliminates two-thirds or rows */
}
if( findTerm(pWC, iCur, -1, notReady, WO_GT|WO_GE, 0) ){
flags |= WHERE_BTM_LIMIT;
cost /= 3; /* Guess that rowid>EXPR eliminates two-thirds of rows */
}
TRACE(("... rowid range reduces cost to %.9g\n", cost));
WHERETRACE(("... rowid range reduces cost to %.9g\n", cost));
}else{
flags = 0;
}
/* If the table scan does not satisfy the ORDER BY clause, increase
** the cost by NlogN to cover the expense of sorting. */
if( pOrderBy ){
if( sortableByRowid(iCur, pOrderBy, pWC->pMaskSet, &rev) ){
flags |= WHERE_ORDERBY|WHERE_ROWID_RANGE;
if( rev ){
flags |= WHERE_REVERSE;
}
}else{
cost += cost*estLog(cost);
TRACE(("... sorting increases cost to %.9g\n", cost));
WHERETRACE(("... sorting increases cost to %.9g\n", cost));
}
}
if( cost<lowestCost ){
lowestCost = cost;
bestFlags = flags;
}
|
︙ | | |
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
|
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
|
-
+
|
/* Look at each index.
*/
for(; pProbe; pProbe=pProbe->pNext){
int i; /* Loop counter */
double inMultiplier = 1;
TRACE(("... index %s:\n", pProbe->zName));
WHERETRACE(("... index %s:\n", pProbe->zName));
/* Count the number of columns in the index that are satisfied
** by x=EXPR constraints or x IN (...) constraints.
*/
flags = 0;
for(i=0; i<pProbe->nColumn; i++){
int j = pProbe->aiColumn[i];
|
︙ | | |
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
|
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
|
-
+
-
+
-
+
-
+
-
+
|
}
cost = pProbe->aiRowEst[i] * inMultiplier * estLog(inMultiplier);
nEq = i;
if( pProbe->onError!=OE_None && (flags & WHERE_COLUMN_IN)==0
&& nEq==pProbe->nColumn ){
flags |= WHERE_UNIQUE;
}
TRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n", nEq, inMultiplier, cost));
WHERETRACE(("...... nEq=%d inMult=%.9g cost=%.9g\n", nEq, inMultiplier, cost));
/* Look for range constraints
*/
if( nEq<pProbe->nColumn ){
int j = pProbe->aiColumn[nEq];
pTerm = findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE|WO_GT|WO_GE, pProbe);
if( pTerm ){
flags |= WHERE_COLUMN_RANGE;
if( findTerm(pWC, iCur, j, notReady, WO_LT|WO_LE, pProbe) ){
flags |= WHERE_TOP_LIMIT;
cost /= 3;
}
if( findTerm(pWC, iCur, j, notReady, WO_GT|WO_GE, pProbe) ){
flags |= WHERE_BTM_LIMIT;
cost /= 3;
}
TRACE(("...... range reduces cost to %.9g\n", cost));
WHERETRACE(("...... range reduces cost to %.9g\n", cost));
}
}
/* Add the additional cost of sorting if that is a factor.
*/
if( pOrderBy ){
if( (flags & WHERE_COLUMN_IN)==0 &&
isSortingIndex(pParse,pWC->pMaskSet,pProbe,iCur,pOrderBy,nEq,&rev) ){
if( flags==0 ){
flags = WHERE_COLUMN_RANGE;
}
flags |= WHERE_ORDERBY;
if( rev ){
flags |= WHERE_REVERSE;
}
}else{
cost += cost*estLog(cost);
TRACE(("...... orderby increases cost to %.9g\n", cost));
WHERETRACE(("...... orderby increases cost to %.9g\n", cost));
}
}
/* Check to see if we can get away with using just the index without
** ever reading the table. If that is the case, then halve the
** cost of this index.
*/
if( flags && pSrc->colUsed < (((Bitmask)1)<<(BMS-1)) ){
Bitmask m = pSrc->colUsed;
int j;
for(j=0; j<pProbe->nColumn; j++){
int x = pProbe->aiColumn[j];
if( x<BMS-1 ){
m &= ~(((Bitmask)1)<<x);
}
}
if( m==0 ){
flags |= WHERE_IDX_ONLY;
cost /= 2;
TRACE(("...... idx-only reduces cost to %.9g\n", cost));
WHERETRACE(("...... idx-only reduces cost to %.9g\n", cost));
}
}
/* If this index has achieved the lowest cost so far, then use it.
*/
if( cost < lowestCost ){
bestIdx = pProbe;
lowestCost = cost;
assert( flags!=0 );
bestFlags = flags;
bestNEq = nEq;
}
}
/* Report the best result
*/
*ppIndex = bestIdx;
TRACE(("best index is %s, cost=%.9g, flags=%x, nEq=%d\n",
WHERETRACE(("best index is %s, cost=%.9g, flags=%x, nEq=%d\n",
bestIdx ? bestIdx->zName : "(none)", lowestCost, bestFlags, bestNEq));
*pFlags = bestFlags | eqTermMask;
*pnEq = bestNEq;
return lowestCost;
}
|
︙ | | |
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
|
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
|
-
-
+
+
|
assert( pX->op==TK_IN );
sqlite3CodeSubselect(pParse, pX);
iTab = pX->iTable;
sqlite3VdbeAddOp(v, OP_Rewind, iTab, 0);
VdbeComment((v, "# %.*s", pX->span.n, pX->span.z));
pLevel->nIn++;
sqliteReallocOrFree((void**)&pLevel->aInLoop,
sizeof(pLevel->aInLoop[0])*2*pLevel->nIn);
sqliteReallocOrFree(&pLevel->aInLoop,
sizeof(pLevel->aInLoop[0])*2*pLevel->nIn);
aIn = pLevel->aInLoop;
if( aIn ){
aIn += pLevel->nIn*2 - 2;
aIn[0] = iTab;
aIn[1] = sqlite3VdbeAddOp(v, OP_Column, iTab, 0);
}else{
pLevel->nIn = 0;
|
︙ | | |
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
|
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
|
-
+
|
** This loop also figures out the nesting order of tables in the FROM
** clause.
*/
notReady = ~(Bitmask)0;
pTabItem = pTabList->a;
pLevel = pWInfo->a;
andFlags = ~0;
TRACE(("*** Optimizer Start ***\n"));
WHERETRACE(("*** Optimizer Start ***\n"));
for(i=iFrom=0, pLevel=pWInfo->a; i<pTabList->nSrc; i++, pLevel++){
Index *pIdx; /* Index for FROM table at pTabItem */
int flags; /* Flags asssociated with pIdx */
int nEq; /* Number of == or IN constraints */
double cost; /* The cost for pIdx */
int j; /* For looping over FROM tables */
Index *pBest = 0; /* The best index seen so far */
|
︙ | | |
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
|
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
|
-
+
-
+
|
bestFlags = flags;
bestNEq = nEq;
bestJ = j;
pLevel->pBestIdx = pIndex;
}
if( doNotReorder ) break;
}
TRACE(("*** Optimizer choose table %d for loop %d\n", bestJ,
WHERETRACE(("*** Optimizer choose table %d for loop %d\n", bestJ,
pLevel-pWInfo->a));
if( (bestFlags & WHERE_ORDERBY)!=0 ){
*ppOrderBy = 0;
}
andFlags &= bestFlags;
pLevel->flags = bestFlags;
pLevel->pIdx = pBest;
pLevel->nEq = bestNEq;
pLevel->aInLoop = 0;
pLevel->nIn = 0;
if( pBest ){
pLevel->iIdxCur = pParse->nTab++;
}else{
pLevel->iIdxCur = -1;
}
notReady &= ~getMask(&maskSet, pTabList->a[bestJ].iCursor);
pLevel->iFrom = bestJ;
}
TRACE(("*** Optimizer Finished ***\n"));
WHERETRACE(("*** Optimizer Finished ***\n"));
/* If the total query only selects a single row, then the ORDER BY
** clause is irrelevant.
*/
if( (andFlags & WHERE_UNIQUE)!=0 && ppOrderBy ){
*ppOrderBy = 0;
}
|
︙ | | |