Fix the locking protocol. (CVS 280)

FossilOrigin-Name: 484b82d8a1c84f3d9725a509de93276b9fa9b294
This commit is contained in:
drh 2001-10-09 04:19:46 +00:00
parent f57b339988
commit ad75e9874b
13 changed files with 413 additions and 134 deletions

View File

@ -1,5 +1,5 @@
C Support\sfor\stemporary\stables\sadded.\s\sStill\sneed\smore\stesting.\s(CVS\s279)
D 2001-10-08T13:22:32
C Fix\sthe\slocking\sprotocol.\s(CVS\s280)
D 2001-10-09T04:19:47
F Makefile.in 98d4627cb364537e4c3a29ee806171f3abf5211a
F Makefile.template 1e54087c0390c4ce0bb5be43e14ba028283751e6
F README 93d2977cc5c6595c448de16bdefc312b9d401533
@ -21,35 +21,35 @@ F publish.sh 502b907fa9e0214309406fa5f520b3d3c14f9c1d
F src/TODO af7f3cab0228e34149cf98e073aa83d45878e7e6
F src/btree.c 7e9c33a714ed1630562f89ad19847f5f28bd6d4d
F src/btree.h 57d653ef5137b91f2a068aaf71a2905468dd2cb7
F src/build.c 55ca22cd7af59b7f8895c601ed9cf006adf50f05
F src/build.c 707f6ef58dcdd50ead9ead914d673b08e7121bc5
F src/delete.c 93c9d5e160395020a25d59371625db74c97c7c4d
F src/expr.c 2f68829d983ec3f92eeb8b89ce4b9e5704169a80
F src/hash.c bf36fb4cba114015123b0050f137d2c4553778a1
F src/hash.h 5f6e7c04c46ed015ab4e01797c2049b4af5b006d
F src/hash.h a5f5b3ce2d086a172c5879b0b06a27a82eac9fac
F src/insert.c a48ba850461b203fb8dbc7add83fc6b6a9cf47f3
F src/main.c 87b2fca50cbe8b400e1443b2c73693e18d9911cb
F src/md5.c 52f677bfc590e09f71d07d7e327bd59da738d07c
F src/os.c d3f435d89241e06d4230b6f79a4e9d49102eb0a4
F src/os.h 0f478e2fef5ec1612f94b59b163d4807d4c77d6d
F src/os.c 64d0015bfc9edcb6380ce6fc75b31c73490f4c7b
F src/os.h bed702c9e3b768bc3cb1b12c90b83d099c1546be
F src/pager.c 3445bd7c18cbfdffd8d6d1077f0b2bdf788da4fe
F src/pager.h a0d4c5ae271914aa07b62aee0707997d6932b6ca
F src/parse.y e88f1efe096a1a01c9076099fe1d81deedfa11de
F src/printf.c b1e22a47be8cdf707815647239991e08e8cb69f9
F src/random.c 708a23f69f40d6f2ae5ce1a04e6a4055d4a6ecec
F src/random.c 2a9cc2c9716d14815fd4c2accf89d87a1143e46b
F src/select.c 0ef8ca1b7de2467fe082bcb35a5ab3b5be56153c
F src/shell.c cb8c41f1b2173efd212dab3f35f1fc6bf32ead76
F src/shell.tcl 27ecbd63dd88396ad16d81ab44f73e6c0ea9d20e
F src/sqlite.h.in b95c161abf1d58bceb05290fa3f657d8f388fc11
F src/sqliteInt.h d75506e003b508d8e2501217648f045496813f2c
F src/sqliteInt.h 603566f58dff0e0295e57792e9313fe5d253f1a2
F src/table.c abd0adbe0fee39d995287b3bcccd908d174dfcac
F src/tclsqlite.c 765599686c19ed777ac379928d732c8bfc63ebac
F src/test1.c e4b31f62ea71963cbae44338acf477a04fc8fc49
F src/test2.c 0168b39225b768cfdadd534406f9dec58c27879e
F src/test3.c 4a0d7b882fdae731dbb759f512ad867122452f96
F src/tokenize.c 5bd2dd048d77f4c683f0551a73d2fa5e964b53f0
F src/tokenize.c 15d349b68d9dc5722956bd7549752ace62034787
F src/update.c 49a1edb1a3e44dfff3f799e00f2a3319f2393cd8
F src/util.c 4da3be37d0fd3c640d2d3033503768afdc8e5387
F src/vdbe.c 469c36ce2ef72a10447796dc5b5d61317e47fff2
F src/vdbe.c 594050d9a8dc51b97320c52d4665de313b842c27
F src/vdbe.h 7eb7e9e6c58fe9430efab35e168f96cb4bd6cb45
F src/where.c b676765ad0360769173b09f46265ddec8d48367a
F test/all.test a2320eb40b462f25bd3e33115b1cabf3791450dd
@ -64,7 +64,7 @@ F test/in.test 9323681388be301dc73f370b4cd62c5a33f79d1e
F test/index.test 6076f29d09a4f26a2efa38b03b8cc338b8662f0e
F test/insert.test a5c122aa726f1cef6f07d6767e8fd6f220994c11
F test/insert2.test 252d7130d8cc20f649b31a4f503cd87e660abda8
F test/lock.test a9641cdc282214563a2fb0233735b09cc2fdd8f2
F test/lock.test 19593689260c419efe7ced55b1418653a4b7bcd1
F test/main.test 085ece17913a487caacbc0a392638c958c83a75d
F test/malloc.test f1400a8d002eb96f1ca0a34abe56d2ab3e324740
F test/misc1.test 50a5ca3481fc1f3cd6b978bcd6ed04c06f26a1e6
@ -83,7 +83,7 @@ F test/subselect.test 335d3dad8d585726c447dfee8d9c4f7383c76b78
F test/table.test 3ef4254d62ece31a3872ab11cdaec846f6fa8fd1
F test/tableapi.test 162840153191a91a7dce6395f2334f9aef713b37
F test/tclsqlite.test a57bb478d7e9f0b2c927f92e161f391e2896631a
F test/temptable.test 99611832cdef52a30e62b091eaf941dbc934f303
F test/temptable.test 9576d30a6809a3233310163fee9ae8d4b9d27f54
F test/tester.tcl c7ddeebc14cc841abb37134cd5d40c1e3ad367c1
F test/trans.test 855337b8a178c73c433fcf8ee88e4b2f5efff0d9
F test/unique.test ef1f67607a7109e9c0842cd8557550fb121d7ec6
@ -101,7 +101,7 @@ F www/arch.fig d5f9752a4dbf242e9cfffffd3f5762b6c63b3bcf
F www/arch.png 82ef36db1143828a7abc88b1e308a5f55d4336f4
F www/arch.tcl 03b521d252575f93b9c52f7c8b0007011512fcfb
F www/c_interface.tcl 8e8d9e66e8467c5751116c3427296bde77f474a6
F www/changes.tcl b42f68ebc6a590ab3dd4f16e389faad2a7f2d541
F www/changes.tcl 7530ecb46af5e2ecb196a90f0ccd46feb3321169
F www/crosscompile.tcl c99efacb3aefaa550c6e80d91b240f55eb9fd33e
F www/download.tcl 3e51c9ff1326b0a182846134987301310dff7d60
F www/dynload.tcl 02eb8273aa78cfa9070dd4501dca937fb22b466c
@ -113,7 +113,7 @@ F www/speed.tcl ab7d6d3bc898472bd94320a5d3c63de928d4804b
F www/sqlite.tcl 6a21242a272e9c0939a04419a51c3d50cae33e3e
F www/tclsqlite.tcl 13d50723f583888fc80ae1a38247c0ab415066fa
F www/vdbe.tcl bb7d620995f0a987293e9d4fb6185a3b077e9b44
P b63b3f3684a3d584ef99f54cde76b6c483bbfef7
R 9a90e6e0cf1a1b0f9063fbb657761a98
P 9368c62e4097aae3081a325962c1dec167fd253d
R 08d1f89d62d87ee167ed839388fa6d57
U drh
Z 4e9556749f24d361b6f8c2df79fd7c4c
Z 67f2491d5f9216116d67e90ffb5f4570

View File

@ -1 +1 @@
9368c62e4097aae3081a325962c1dec167fd253d
484b82d8a1c84f3d9725a509de93276b9fa9b294

View File

@ -25,7 +25,7 @@
** ROLLBACK
** PRAGMA
**
** $Id: build.c,v 1.45 2001/10/08 13:22:32 drh Exp $
** $Id: build.c,v 1.46 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@ -573,7 +573,7 @@ void sqliteEndTable(Parse *pParse, Token *pEnd){
/* Add the table to the in-memory representation of the database.
*/
assert( pParse->nameClash==0 || pParse->initFlag==0 );
assert( pParse->nameClash==0 || pParse->initFlag==1 );
if( pParse->explain==0 && pParse->nameClash==0 ){
sqliteHashInsert(&db->tblHash, p->zName, strlen(p->zName)+1, p);
pParse->pNewTable = 0;

View File

@ -12,7 +12,7 @@
** This is the header file for the generic hash-table implemenation
** used in SQLite.
**
** $Id: hash.h,v 1.1 2001/09/22 18:12:10 drh Exp $
** $Id: hash.h,v 1.2 2001/10/09 04:19:47 drh Exp $
*/
#ifndef _SQLITE_HASH_H_
#define _SQLITE_HASH_H_
@ -30,7 +30,7 @@ typedef struct HashElem HashElem;
** this structure opaque.
*/
struct Hash {
char keyClass; /* SQLITE_HASH_INT, ..._STRING, or _BINARY */
char keyClass; /* SQLITE_HASH_INT, _POINTER, _STRING, _BINARY */
char copyKey; /* True if copy of key made on insert */
int count; /* Number of entries in this table */
HashElem *first; /* The first element of the array */

220
src/os.c
View File

@ -37,6 +37,73 @@
# include <winbase.h>
#endif
#if OS_UNIX
/*
** An instance of the following structure serves as the key used
** to locate a particular lockInfo structure given its inode.
*/
struct inodeKey {
dev_t dev; /* Device number */
ino_t ino; /* Inode number */
};
/*
** An instance of the following structure is allocated for each inode.
** A single inode can have multiple file descriptors, so each OsFile structure
** contains a pointer to an instance of this object.
*/
struct lockInfo {
struct inodeKey key; /* The lookup key */
int cnt; /* 0: unlocked. -1: write lock. >=1: read lock */
int nRef; /* Number of pointers to this structure */
};
/*
** This hash table maps inodes (in the form of inodeKey structures) into
** pointers to lockInfo structures.
*/
static Hash lockHash = { SQLITE_HASH_BINARY, 0, 0, 0, 0, 0 };
/*
** Given a file descriptor, locate a lockInfo structure that describes
** that file descriptor. Create a new one if necessary.
*/
static struct lockInfo *findLockInfo(int fd){
int rc;
struct inodeKey key;
struct stat statbuf;
struct lockInfo *pInfo;
rc = fstat(fd, &statbuf);
if( rc!=0 ) return 0;
key.dev = statbuf.st_dev;
key.ino = statbuf.st_ino;
pInfo = (struct lockInfo*)sqliteHashFind(&lockHash, &key, sizeof(key));
if( pInfo==0 ){
pInfo = sqliteMalloc( sizeof(*pInfo) );
pInfo->key = key;
pInfo->nRef = 1;
pInfo->cnt = 0;
sqliteHashInsert(&lockHash, &pInfo->key, sizeof(key), pInfo);
}else{
pInfo->nRef++;
}
return pInfo;
}
/*
** Release a lockInfo structure previously allocated by findLockInfo().
*/
static void releaseLockInfo(struct lockInfo *pInfo){
pInfo->nRef--;
if( pInfo->nRef==0 ){
sqliteHashInsert(&lockHash, &pInfo->key, sizeof(pInfo->key), 0);
sqliteFree(pInfo);
}
}
#endif
/*
** Delete the named file
*/
@ -95,17 +162,25 @@ int sqliteOsOpenReadWrite(
int *pReadonly
){
#if OS_UNIX
int fd = open(zFilename, O_RDWR|O_CREAT, 0644);
if( fd<0 ){
fd = open(zFilename, O_RDONLY);
if( fd<0 ){
OsFile s;
s.fd = open(zFilename, O_RDWR|O_CREAT, 0644);
if( s.fd<0 ){
s.fd = open(zFilename, O_RDONLY);
if( s.fd<0 ){
return SQLITE_CANTOPEN;
}
*pReadonly = 1;
}else{
*pReadonly = 0;
}
*pResult = fd;
sqliteOsEnterMutex();
s.pLock = findLockInfo(s.fd);
sqliteOsLeaveMutex();
if( s.pLock==0 ){
close(s.fd);
return SQLITE_NOMEM;
}
*pResult = s;
return SQLITE_OK;
#endif
#if OS_WIN
@ -152,18 +227,25 @@ int sqliteOsOpenReadWrite(
*/
int sqliteOsOpenExclusive(const char *zFilename, OsFile *pResult){
#if OS_UNIX
int fd;
OsFile s;
if( access(zFilename, 0)==0 ){
return SQLITE_CANTOPEN;
}
#ifndef O_NOFOLLOW
# define O_NOFOLLOW 0
#endif
fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600);
if( fd<0 ){
s.fd = open(zFilename, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0600);
if( s.fd<0 ){
return SQLITE_CANTOPEN;
}
*pResult = fd;
sqliteOsEnterMutex();
s.pLock = findLockInfo(s.fd);
sqliteOsLeaveMutex();
if( s.pLock==0 ){
close(s.fd);
return SQLITE_NOMEM;
}
*pResult = s;
return SQLITE_OK;
#endif
#if OS_WIN
@ -192,11 +274,19 @@ int sqliteOsOpenExclusive(const char *zFilename, OsFile *pResult){
*/
int sqliteOsOpenReadOnly(const char *zFilename, OsFile *pResult){
#if OS_UNIX
int fd = open(zFilename, O_RDONLY);
if( fd<0 ){
OsFile s;
s.fd = open(zFilename, O_RDONLY);
if( s.fd<0 ){
return SQLITE_CANTOPEN;
}
*pResult = fd;
sqliteOsEnterMutex();
s.pLock = findLockInfo(s.fd);
sqliteOsLeaveMutex();
if( s.pLock==0 ){
close(s.fd);
return SQLITE_NOMEM;
}
*pResult = s;
return SQLITE_OK;
#endif
#if OS_WIN
@ -246,7 +336,7 @@ int sqliteOsTempFileName(char *zBuf){
sprintf(zBuf, "%s/sqlite_", zDir);
j = strlen(zBuf);
for(i=0; i<15; i++){
int n = rand() % sizeof(zChars);
int n = sqliteRandomByte() % (sizeof(zChars)-1);
zBuf[j++] = zChars[n];
}
zBuf[j] = 0;
@ -264,7 +354,7 @@ int sqliteOsTempFileName(char *zBuf){
sprintf(zBuf, "%s/sqlite_", zTempPath);
j = strlen(zBuf);
for(i=0; i<15; i++){
int n = rand() % sizeof(zChars);
int n = sqliteRandomByte() % sizeof(zChars);
zBuf[j++] = zChars[n];
}
zBuf[j] = 0;
@ -279,7 +369,10 @@ int sqliteOsTempFileName(char *zBuf){
*/
int sqliteOsClose(OsFile id){
#if OS_UNIX
close(id);
close(id.fd);
sqliteOsEnterMutex();
releaseLockInfo(id.pLock);
sqliteOsLeaveMutex();
return SQLITE_OK;
#endif
#if OS_WIN
@ -295,7 +388,7 @@ int sqliteOsClose(OsFile id){
int sqliteOsRead(OsFile id, void *pBuf, int amt){
#if OS_UNIX
int got;
got = read(id, pBuf, amt);
got = read(id.fd, pBuf, amt);
if( got<0 ) got = 0;
return got==amt ? SQLITE_OK : SQLITE_IOERR;
#endif
@ -315,7 +408,7 @@ int sqliteOsRead(OsFile id, void *pBuf, int amt){
int sqliteOsWrite(OsFile id, const void *pBuf, int amt){
#if OS_UNIX
int wrote;
wrote = write(id, pBuf, amt);
wrote = write(id.fd, pBuf, amt);
if( wrote<amt ) return SQLITE_FULL;
return SQLITE_OK;
#endif
@ -333,7 +426,7 @@ int sqliteOsWrite(OsFile id, const void *pBuf, int amt){
*/
int sqliteOsSeek(OsFile id, int offset){
#if OS_UNIX
lseek(id, offset, SEEK_SET);
lseek(id.fd, offset, SEEK_SET);
return SQLITE_OK;
#endif
#if OS_WIN
@ -347,7 +440,7 @@ int sqliteOsSeek(OsFile id, int offset){
*/
int sqliteOsSync(OsFile id){
#if OS_UNIX
return fsync(id)==0 ? SQLITE_OK : SQLITE_IOERR;
return fsync(id.fd)==0 ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
return FlushFileBuffers(id) ? SQLITE_OK : SQLITE_IOERR;
@ -359,7 +452,7 @@ int sqliteOsSync(OsFile id){
*/
int sqliteOsTruncate(OsFile id, int nByte){
#if OS_UNIX
return ftruncate(id, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
return ftruncate(id.fd, nByte)==0 ? SQLITE_OK : SQLITE_IOERR;
#endif
#if OS_WIN
SetFilePointer(id, nByte, 0, FILE_BEGIN);
@ -374,7 +467,7 @@ int sqliteOsTruncate(OsFile id, int nByte){
int sqliteOsFileSize(OsFile id, int *pSize){
#if OS_UNIX
struct stat buf;
if( fstat(id, &buf)!=0 ){
if( fstat(id.fd, &buf)!=0 ){
return SQLITE_IOERR;
}
*pSize = buf.st_size;
@ -386,21 +479,41 @@ int sqliteOsFileSize(OsFile id, int *pSize){
#endif
}
/*
** Get a read or write lock on a file.
*/
int sqliteOsLock(OsFile id, int wrlock){
#if OS_UNIX
int rc;
struct flock lock;
lock.l_type = wrlock ? F_WRLCK : F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
rc = fcntl(id, F_SETLK, &lock);
if( rc ){
fcntl(id, F_GETLK, &lock); /* For debugging */
int needSysLock;
sqliteOsEnterMutex();
if( wrlock ){
if( id.pLock->cnt!=0 ){
rc = SQLITE_BUSY;
}else{
rc = SQLITE_OK;
id.pLock->cnt = -1;
needSysLock = 1;
}
}else{
if( id.pLock<0 ){
rc = SQLITE_BUSY;
}else{
rc = SQLITE_OK;
needSysLock = id.pLock->cnt==0;
id.pLock->cnt++;
}
}
return rc==0 ? SQLITE_OK : SQLITE_BUSY;
sqliteOsLeaveMutex();
if( rc==SQLITE_OK && needSysLock ){
struct flock lock;
lock.l_type = wrlock ? F_WRLCK : F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
rc = fcntl(id.fd, F_SETLK, &lock)==0 ? SQLITE_OK : SQLITE_BUSY;
}
return rc;
#endif
#if OS_WIN
if( !LockFile(id, 0, 0, 1024, 0) ){
@ -416,12 +529,28 @@ int sqliteOsLock(OsFile id, int wrlock){
int sqliteOsUnlock(OsFile id){
#if OS_UNIX
int rc;
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
rc = fcntl(id, F_SETLK, &lock);
return rc==0 ? SQLITE_OK : SQLITE_IOERR;
int needSysUnlock;
sqliteOsEnterMutex();
if( id.pLock->cnt<0 ){
needSysUnlock = 1;
id.pLock->cnt = 0;
}else if( id.pLock->cnt>0 ){
id.pLock->cnt--;
needSysUnlock = id.pLock->cnt==0;
}else{
rc = SQLITE_OK;
needSysUnlock = 0;
}
sqliteOsLeaveMutex();
if( needSysUnlock ){
struct flock lock;
lock.l_type = F_UNLCK;
lock.l_whence = SEEK_SET;
lock.l_start = lock.l_len = 0L;
rc = fcntl(id.fd, F_SETLK, &lock)==0 ? SQLITE_OK : SQLITE_IOERR;
}
return rc;
#endif
#if OS_WIN
return UnlockFile(id, 0, 0, 1024, 0) ? SQLITE_OK : SQLITE_IOERR;
@ -469,3 +598,24 @@ int sqliteOsSleep(int ms){
return ms;
#endif
}
/*
** The following pair of routine implement mutual exclusion for
** multi-threaded processes. Only a single thread is allowed to
** executed code that is surrounded by EnterMutex() and LeaveMutex().
**
** SQLite uses only a single Mutex. There is not much critical
** code and what little there is executes quickly and without blocking.
**
****** TBD: The mutex is currently unimplemented. Until it is
****** implemented, SQLite is not threadsafe.
*/
static int inMutex = 0;
void sqliteOsEnterMutex(){
assert( !inMutex );
inMutex = 1;
}
void sqliteOsLeaveMutex(){
assert( inMutex );
inMutex = 0;
}

View File

@ -21,7 +21,11 @@
** A handle for an open file is stored in an OsFile object.
*/
#if OS_UNIX
typedef int OsFile;
typedef struct OsFile OsFile;
struct OsFile {
struct lockInfo *pLock; /* Information about locks on this inode */
int fd; /* The file descriptor */
};
# define SQLITE_TEMPNAME_SIZE 200
# if defined(HAVE_USLEEP) && HAVE_USLEEP
# define SQLITE_MIN_SLEEP_MS 1
@ -55,6 +59,8 @@ int sqliteOsLock(OsFile, int wrlock);
int sqliteOsUnlock(OsFile);
int sqliteOsRandomSeed(char*);
int sqliteOsSleep(int ms);
void sqliteOsEnterMutex();
void sqliteOsLeaveMutex();

View File

@ -15,17 +15,27 @@
** Random numbers are used by some of the database backends in order
** to generate random integer keys for tables or random filenames.
**
** $Id: random.c,v 1.7 2001/09/23 19:46:52 drh Exp $
** $Id: random.c,v 1.8 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
/*
** Get a single 8-bit random value from the RC4 PRNG.
** Get a single 8-bit random value from the RC4 PRNG. The Mutex
** must be held while executing this routine.
*/
int sqliteRandomByte(sqlite *db){
static int randomByte(){
int t;
/* All threads share a single random number generator.
** This structure is the current state of the generator.
*/
static struct {
int isInit; /* True if initialized */
int i, j; /* State variables */
int s[256]; /* State variables */
} prng;
/* Initialize the state of the random number generator once,
** the first time this routine is called. The seed value does
** not need to contain a lot of randomness since we are not
@ -35,46 +45,59 @@ int sqliteRandomByte(sqlite *db){
** encryption. The RC4 algorithm is being used as a PRNG (pseudo-random
** number generator) not as an encryption device.
*/
if( !db->prng.isInit ){
if( !prng.isInit ){
int i;
char k[256];
db->prng.j = 0;
db->prng.i = 0;
prng.j = 0;
prng.i = 0;
sqliteOsRandomSeed(k);
for(i=0; i<256; i++){
db->prng.s[i] = i;
prng.s[i] = i;
}
for(i=0; i<256; i++){
int t;
db->prng.j = (db->prng.j + db->prng.s[i] + k[i]) & 0xff;
t = db->prng.s[db->prng.j];
db->prng.s[db->prng.j] = db->prng.s[i];
db->prng.s[i] = t;
prng.j = (prng.j + prng.s[i] + k[i]) & 0xff;
t = prng.s[prng.j];
prng.s[prng.j] = prng.s[i];
prng.s[i] = t;
}
db->prng.isInit = 1;
prng.isInit = 1;
}
/* Generate and return single random byte
*/
db->prng.i = (db->prng.i + 1) & 0xff;
db->prng.j = (db->prng.j + db->prng.s[db->prng.i]) & 0xff;
t = db->prng.s[db->prng.i];
db->prng.s[db->prng.i] = db->prng.s[db->prng.j];
db->prng.s[db->prng.j] = t;
t = db->prng.s[db->prng.i] + db->prng.s[db->prng.j];
return db->prng.s[t & 0xff];
prng.i = (prng.i + 1) & 0xff;
prng.j = (prng.j + prng.s[prng.i]) & 0xff;
t = prng.s[prng.i];
prng.s[prng.i] = prng.s[prng.j];
prng.s[prng.j] = t;
t = prng.s[prng.i] + prng.s[prng.j];
return prng.s[t & 0xff];
}
/*
** Return an random 8-bit integer.
*/
int sqliteRandomByte(){
int r;
sqliteOsEnterMutex();
r = randomByte();
sqliteOsLeaveMutex();
return r;
}
/*
** Return a random 32-bit integer. The integer is generated by making
** 4 calls to sqliteRandomByte().
*/
int sqliteRandomInteger(sqlite *db){
int sqliteRandomInteger(){
int r;
int i;
r = sqliteRandomByte(db);
sqliteOsEnterMutex();
r = randomByte();
for(i=1; i<4; i++){
r = (r<<8) + sqliteRandomByte(db);
r = (r<<8) + randomByte();
}
sqliteOsLeaveMutex();
return r;
}

View File

@ -11,7 +11,7 @@
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.58 2001/10/08 13:22:33 drh Exp $
** @(#) $Id: sqliteInt.h,v 1.59 2001/10/09 04:19:47 drh Exp $
*/
#include "sqlite.h"
#include "hash.h"
@ -149,11 +149,6 @@ struct sqlite {
int (*xBusyCallback)(void *,const char*,int); /* The busy callback */
Hash tblHash; /* All tables indexed by name */
Hash idxHash; /* All (named) indices indexed by name */
struct { /* State of the RC4 random number generator */
int isInit; /* True if initialized */
int i, j; /* State variables */
int s[256]; /* State variables */
} prng;
int nextRowid; /* Next generated rowID */
};
@ -466,8 +461,8 @@ void sqliteExprResolveInSelect(Parse*, Expr*);
int sqliteExprAnalyzeAggregates(Parse*, Expr*);
void sqliteParseInfoReset(Parse*);
Vdbe *sqliteGetVdbe(Parse*);
int sqliteRandomByte(sqlite*);
int sqliteRandomInteger(sqlite*);
int sqliteRandomByte();
int sqliteRandomInteger();
void sqliteBeginTransaction(Parse*);
void sqliteCommitTransaction(Parse*);
void sqliteRollbackTransaction(Parse*);

View File

@ -15,9 +15,10 @@
** individual tokens and sends those tokens one-by-one over to the
** parser for analysis.
**
** $Id: tokenize.c,v 1.25 2001/10/08 13:22:33 drh Exp $
** $Id: tokenize.c,v 1.26 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include "os.h"
#include <ctype.h>
#include <stdlib.h>
@ -113,16 +114,20 @@ static int sqliteKeywordCode(const char *z, int n){
Keyword *p;
if( aKeywordTable[0].len==0 ){
/* Initialize the keyword hash table */
int i;
int n;
n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
for(i=0; i<n; i++){
aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
h %= KEY_HASH_SIZE;
aKeywordTable[i].pNext = apHashTable[h];
apHashTable[h] = &aKeywordTable[i];
sqliteOsEnterMutex();
if( aKeywordTable[0].len==0 ){
int i;
int n;
n = sizeof(aKeywordTable)/sizeof(aKeywordTable[0]);
for(i=0; i<n; i++){
aKeywordTable[i].len = strlen(aKeywordTable[i].zName);
h = sqliteHashNoCase(aKeywordTable[i].zName, aKeywordTable[i].len);
h %= KEY_HASH_SIZE;
aKeywordTable[i].pNext = apHashTable[h];
apHashTable[h] = &aKeywordTable[i];
}
}
sqliteOsLeaveMutex();
}
h = sqliteHashNoCase(z, n) % KEY_HASH_SIZE;
for(p=apHashTable[h]; p; p=p->pNext){

View File

@ -30,7 +30,7 @@
** But other routines are also provided to help in building up
** a program instruction by instruction.
**
** $Id: vdbe.c,v 1.81 2001/10/08 13:22:33 drh Exp $
** $Id: vdbe.c,v 1.82 2001/10/09 04:19:47 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@ -2368,9 +2368,9 @@ case OP_NewRecno: {
v = db->nextRowid;
do{
if( cnt>5 ){
v = sqliteRandomInteger(db);
v = sqliteRandomInteger();
}else{
v += sqliteRandomByte(db) + 1;
v += sqliteRandomByte() + 1;
}
if( v==0 ) continue;
x = bigEndian(v);

View File

@ -11,7 +11,7 @@
# This file implements regression tests for SQLite library. The
# focus of this script is database locks.
#
# $Id: lock.test,v 1.12 2001/09/23 19:46:52 drh Exp $
# $Id: lock.test,v 1.13 2001/10/09 04:19:47 drh Exp $
set testdir [file dirname $argv0]
@ -33,16 +33,14 @@ do_test lock-1.3 {
execsql {SELECT name FROM sqlite_master WHERE type='table' ORDER BY name}
} {t1}
do_test lock-1.4 {
set r [catch {execsql {
SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
} db2} msg]
lappend r $msg
catchsql {
SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
} db2
} {1 {database schema has changed}}
do_test lock-1.5 {
set r [catch {execsql {
catchsql {
SELECT name FROM sqlite_master WHERE type='table' ORDER BY name
} db2} msg]
lappend r $msg
} db2
} {0 t1}
do_test lock-1.6 {
@ -59,19 +57,16 @@ do_test lock-1.8 {
do_test lock-1.9 {
execsql {SELECT * FROM t1}
} {2 1}
do_test lock-1.10 {
execsql {BEGIN TRANSACTION}
execsql {SELECT * FROM t1}
} {2 1}
do_test lock-1.11 {
set r [catch {execsql {SELECT * FROM t1} db2} msg]
lappend r $msg
catchsql {SELECT * FROM t1} db2
} {1 {database is locked}}
do_test lock-1.12 {
execsql {ROLLBACK}
set r [catch {execsql {SELECT * FROM t1} db2} msg]
lappend r $msg
catchsql {SELECT * FROM t1}
} {0 {2 1}}
do_test lock-1.13 {
@ -80,12 +75,10 @@ do_test lock-1.13 {
execsql {SELECT * FROM t2}
} {8 9}
do_test lock-1.14 {
set r [catch {execsql {SELECT * FROM t1} db2} msg]
lappend r $msg
catchsql {SELECT * FROM t1} db2
} {1 {database schema has changed}}
do_test lock-1.15 {
set r [catch {execsql {SELECT * FROM t2} db2} msg]
lappend r $msg
catchsql {SELECT * FROM t2} db2
} {0 {8 9}}
do_test lock-1.16 {

View File

@ -12,7 +12,7 @@
#
# This file implements tests for temporary tables and indices.
#
# $Id: temptable.test,v 1.1 2001/10/08 13:22:33 drh Exp $
# $Id: temptable.test,v 1.2 2001/10/09 04:19:47 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -34,50 +34,50 @@ do_test temptable-1.2 {
catch {db2 eval {SELECT * FROM sqlite_master}}
db2 eval {SELECT * FROM t1}
} {1 2 3}
do_test testtable-1.3 {
do_test temptable-1.3 {
execsql {SELECT name FROM sqlite_master}
} {t1}
do_test testtable-1.4 {
do_test temptable-1.4 {
db2 eval {SELECT name FROM sqlite_master}
} {t1}
# Create a temporary table. Verify that only one of the two
# processes can see it.
#
do_test testtable-1.5 {
do_test temptable-1.5 {
db2 eval {
CREATE TEMP TABLE t2(x,y,z);
INSERT INTO t2 VALUES(4,5,6);
}
db2 eval {SELECT * FROM t2}
} {4 5 6}
do_test testtable-1.6 {
do_test temptable-1.6 {
catch {execsql {SELECT * FROM sqlite_master}}
catchsql {SELECT * FROM t2}
} {1 {no such table: t2}}
do_test testtable-1.7 {
do_test temptable-1.7 {
catchsql {INSERT INTO t2 VALUES(8,9,0);}
} {1 {no such table: t2}}
do_test testtable-1.8 {
do_test temptable-1.8 {
db2 eval {INSERT INTO t2 VALUES(8,9,0);}
db2 eval {SELECT * FROM t2 ORDER BY x}
} {4 5 6 8 9 0}
do_test testtable-1.9 {
do_test temptable-1.9 {
db2 eval {DELETE FROM t2 WHERE x==8}
db2 eval {SELECT * FROM t2 ORDER BY x}
} {4 5 6}
do_test testtable-1.10 {
do_test temptable-1.10 {
db2 eval {DELETE FROM t2}
db2 eval {SELECT * FROM t2}
} {}
do_test testtable-1.11 {
do_test temptable-1.11 {
db2 eval {
INSERT INTO t2 VALUES(7,6,5);
INSERT INTO t2 VALUES(4,3,2);
SELECT * FROM t2 ORDER BY x;
}
} {4 3 2 7 6 5}
do_test testtable-1.12 {
do_test temptable-1.12 {
db2 eval {DROP TABLE t2;}
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
@ -85,7 +85,7 @@ do_test testtable-1.12 {
# Make sure temporary tables work with transactions
#
do_test testtable-2.1 {
do_test temptable-2.1 {
execsql {
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t2(x,y);
@ -93,11 +93,11 @@ do_test testtable-2.1 {
SELECT * FROM t2;
}
} {1 2}
do_test testtable-2.2 {
do_test temptable-2.2 {
execsql {ROLLBACK}
catchsql {SELECT * FROM t2}
} {1 {no such table: t2}}
do_test testtable-2.3 {
do_test temptable-2.3 {
execsql {
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE t2(x,y);
@ -105,20 +105,124 @@ do_test testtable-2.3 {
SELECT * FROM t2;
}
} {1 2}
do_test testtable-2.4 {
do_test temptable-2.4 {
execsql {COMMIT}
catchsql {SELECT * FROM t2}
} {0 {1 2}}
do_test testtable-2.5 {
do_test temptable-2.5 {
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {1 {no such table: t2}}
# Make sure indices on temporary tables are also temporary.
#
do_test temptable-3.1 {
execsql {
CREATE INDEX i2 ON t2(x);
SELECT name FROM sqlite_master WHERE type='index';
}
} {}
do_test temptable-3.2 {
execsql {
SELECT y FROM t2 WHERE x=1;
}
} {2}
do_test temptable-3.3 {
execsql {
DROP INDEX i2;
SELECT y FROM t2 WHERE x=1;
}
} {2}
do_test temptable-3.4 {
execsql {
CREATE INDEX i2 ON t2(x);
DROP TABLE t2;
}
catchsql {DROP INDEX i2}
} {1 {no such index: i2}}
# Check for correct name collision processing. A name collision can
# occur when process A creates a temporary table T then process B
# creates a permanent table also named T. The temp table in process A
# hides the existance of the permanent table.
#
do_test temptable-4.1 {
db2 eval {
CREATE TEMP TABLE t2(x,y);
INSERT INTO t2 VALUES(10,20);
SELECT * FROM t2;
}
} {10 20}
do_test temptable-4.2 {
execsql {
CREATE TABLE t2(x,y,z);
INSERT INTO t2 VALUES(9,8,7);
SELECT * FROM t2;
}
} {9 8 7}
do_test temptable-4.3 {
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {1 {database schema has changed}}
do_test temptable-4.4 {
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {0 {10 20}}
do_test temptable-4.5 {
db2 eval {DROP TABLE t2}
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {1 {no such table: t2}}
do_test temptable-4.6 {
db2 close
sqlite db2 ./test.db
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {0 {9 8 7}}
# Now create a temporary table in db2 and a permanent index in db. The
# temporary table in db2 should mask the name of the permanent index,
# but the permanent index should still be accessible and should still
# be updated when its correspnding table changes.
#
do_test temptable-5.1 {
db2 eval {CREATE TEMP TABLE mask(a,b,c)}
execsql {
CREATE INDEX mask ON t2(x);
SELECT * FROM t2;
}
} {9 8 7}
do_test temptable-5.2 {
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {1 {database schema has changed}}
do_test temptable-5.3 {
set r [catch {db2 eval {SELECT * FROM t2}} msg]
lappend r $msg
} {0 {9 8 7}}
do_test temptable-5.4 {
execsql {SELECT y FROM t2 WHERE x=9}
} {8}
do_test temptable-5.5 {
db2 eval {SELECT y FROM t2 WHERE x=9}
} {8}
do_test temptable-5.6 {
db2 eval {
INSERT INTO t2 VALUES(1,2,3);
SELECT y FROM t2 WHERE x=1;
}
} {2}
do_test temptable-5.7 {
db2 eval {SELECT y FROM t2 WHERE x=9}
} {8}
do_test temptable-5.8 {
execsql {
SELECT y FROM t2 WHERE x=1;
}
} {2}
do_test temptable-5.9 {
execsql {SELECT y FROM t2 WHERE x=9}
} {8}
finish_test

View File

@ -17,12 +17,15 @@ proc chng {date desc} {
puts "<DD><P><UL>$desc</UL></P></DD>"
}
chng {2001 Oct ? (2.0.2)} {
chng {2001 Oct 8 (2.0.2)} {
<li>Fix a bugs in the locking protocol.</li>
<li>Removed some unused "#include <unistd.h>" that were causing problems
for VC++.</li>
<li>Fixed <b>sqlite.h</b> so that it is usable from C++</li>
<li>Added the FULL_COLUMN_NAMES pragma. When set to "ON", the names of
columns are reported back as TABLE.COLUMN instead of just COLUMN.</li>
<li>Added the TABLE_INFO() and INDEX_INFO() pragmas to help support the
ODBC interface.</li>
<li>Added support for TEMPORARY tables and indices.</li>
}