Fossil SCM

Incorporate recent features/fixes.

andybradford 2014-06-01 04:17 autosync-tries merge
Commit 715a36c8ec72736209f3404797f57a61ae64c96e
+12 -1
--- src/checkin.c
+++ src/checkin.c
@@ -1239,10 +1239,11 @@
12391239
int fBinary; /* does the blob content appear to be binary? */
12401240
int lookFlags; /* output flags from looks_like_utf8/utf16() */
12411241
int fHasAnyCr; /* the blob contains one or more CR chars */
12421242
int fHasLoneCrOnly; /* all detected line endings are CR only */
12431243
int fHasCrLfOnly; /* all detected line endings are CR/LF pairs */
1244
+ int fHasInvalidUtf8 = 0;/* contains byte-sequence which is invalid for UTF-8 */
12441245
char *zMsg; /* Warning message */
12451246
Blob fname; /* Relative pathname of the file */
12461247
static int allOk = 0; /* Set to true to disable this routine */
12471248
12481249
if( allOk ) return 0;
@@ -1249,16 +1250,19 @@
12491250
fUnicode = could_be_utf16(p, &bReverse);
12501251
if( fUnicode ){
12511252
lookFlags = looks_like_utf16(p, bReverse, LOOK_NUL);
12521253
}else{
12531254
lookFlags = looks_like_utf8(p, LOOK_NUL);
1255
+ if( !(lookFlags & LOOK_BINARY) && invalid_utf8(p) ){
1256
+ fHasInvalidUtf8 = 1;
1257
+ }
12541258
}
12551259
fHasAnyCr = (lookFlags & LOOK_CR);
12561260
fBinary = (lookFlags & LOOK_BINARY);
12571261
fHasLoneCrOnly = ((lookFlags & LOOK_EOL) == LOOK_LONE_CR);
12581262
fHasCrLfOnly = ((lookFlags & LOOK_EOL) == LOOK_CRLF);
1259
- if( fUnicode || fHasAnyCr || fBinary ){
1263
+ if( fUnicode || fHasAnyCr || fBinary || fHasInvalidUtf8){
12601264
const char *zWarning;
12611265
const char *zDisable;
12621266
const char *zConvert = "c=convert/";
12631267
Blob ans;
12641268
char cReply;
@@ -1287,10 +1291,17 @@
12871291
zWarning = "CR/NL line endings and Unicode";
12881292
}else{
12891293
zWarning = "mixed line endings and Unicode";
12901294
}
12911295
zDisable = "\"crnl-glob\" and \"encoding-glob\" settings";
1296
+ }else if( fHasInvalidUtf8 ){
1297
+ if( encodingOk ){
1298
+ return 0; /* We don't want encoding warnings for this file. */
1299
+ }
1300
+ zWarning = "invalid UTF-8";
1301
+ zConvert = ""; /* Possible conversion to UTF-8 not yet implemented. */
1302
+ zDisable = "\"encoding-glob\" setting";
12921303
}else if( fHasAnyCr ){
12931304
if( crnlOk ){
12941305
return 0; /* We don't want CR/NL warnings for this file. */
12951306
}
12961307
if( fHasLoneCrOnly ){
12971308
--- src/checkin.c
+++ src/checkin.c
@@ -1239,10 +1239,11 @@
1239 int fBinary; /* does the blob content appear to be binary? */
1240 int lookFlags; /* output flags from looks_like_utf8/utf16() */
1241 int fHasAnyCr; /* the blob contains one or more CR chars */
1242 int fHasLoneCrOnly; /* all detected line endings are CR only */
1243 int fHasCrLfOnly; /* all detected line endings are CR/LF pairs */
 
1244 char *zMsg; /* Warning message */
1245 Blob fname; /* Relative pathname of the file */
1246 static int allOk = 0; /* Set to true to disable this routine */
1247
1248 if( allOk ) return 0;
@@ -1249,16 +1250,19 @@
1249 fUnicode = could_be_utf16(p, &bReverse);
1250 if( fUnicode ){
1251 lookFlags = looks_like_utf16(p, bReverse, LOOK_NUL);
1252 }else{
1253 lookFlags = looks_like_utf8(p, LOOK_NUL);
 
 
 
1254 }
1255 fHasAnyCr = (lookFlags & LOOK_CR);
1256 fBinary = (lookFlags & LOOK_BINARY);
1257 fHasLoneCrOnly = ((lookFlags & LOOK_EOL) == LOOK_LONE_CR);
1258 fHasCrLfOnly = ((lookFlags & LOOK_EOL) == LOOK_CRLF);
1259 if( fUnicode || fHasAnyCr || fBinary ){
1260 const char *zWarning;
1261 const char *zDisable;
1262 const char *zConvert = "c=convert/";
1263 Blob ans;
1264 char cReply;
@@ -1287,10 +1291,17 @@
1287 zWarning = "CR/NL line endings and Unicode";
1288 }else{
1289 zWarning = "mixed line endings and Unicode";
1290 }
1291 zDisable = "\"crnl-glob\" and \"encoding-glob\" settings";
 
 
 
 
 
 
 
1292 }else if( fHasAnyCr ){
1293 if( crnlOk ){
1294 return 0; /* We don't want CR/NL warnings for this file. */
1295 }
1296 if( fHasLoneCrOnly ){
1297
--- src/checkin.c
+++ src/checkin.c
@@ -1239,10 +1239,11 @@
1239 int fBinary; /* does the blob content appear to be binary? */
1240 int lookFlags; /* output flags from looks_like_utf8/utf16() */
1241 int fHasAnyCr; /* the blob contains one or more CR chars */
1242 int fHasLoneCrOnly; /* all detected line endings are CR only */
1243 int fHasCrLfOnly; /* all detected line endings are CR/LF pairs */
1244 int fHasInvalidUtf8 = 0;/* contains byte-sequence which is invalid for UTF-8 */
1245 char *zMsg; /* Warning message */
1246 Blob fname; /* Relative pathname of the file */
1247 static int allOk = 0; /* Set to true to disable this routine */
1248
1249 if( allOk ) return 0;
@@ -1249,16 +1250,19 @@
1250 fUnicode = could_be_utf16(p, &bReverse);
1251 if( fUnicode ){
1252 lookFlags = looks_like_utf16(p, bReverse, LOOK_NUL);
1253 }else{
1254 lookFlags = looks_like_utf8(p, LOOK_NUL);
1255 if( !(lookFlags & LOOK_BINARY) && invalid_utf8(p) ){
1256 fHasInvalidUtf8 = 1;
1257 }
1258 }
1259 fHasAnyCr = (lookFlags & LOOK_CR);
1260 fBinary = (lookFlags & LOOK_BINARY);
1261 fHasLoneCrOnly = ((lookFlags & LOOK_EOL) == LOOK_LONE_CR);
1262 fHasCrLfOnly = ((lookFlags & LOOK_EOL) == LOOK_CRLF);
1263 if( fUnicode || fHasAnyCr || fBinary || fHasInvalidUtf8){
1264 const char *zWarning;
1265 const char *zDisable;
1266 const char *zConvert = "c=convert/";
1267 Blob ans;
1268 char cReply;
@@ -1287,10 +1291,17 @@
1291 zWarning = "CR/NL line endings and Unicode";
1292 }else{
1293 zWarning = "mixed line endings and Unicode";
1294 }
1295 zDisable = "\"crnl-glob\" and \"encoding-glob\" settings";
1296 }else if( fHasInvalidUtf8 ){
1297 if( encodingOk ){
1298 return 0; /* We don't want encoding warnings for this file. */
1299 }
1300 zWarning = "invalid UTF-8";
1301 zConvert = ""; /* Possible conversion to UTF-8 not yet implemented. */
1302 zDisable = "\"encoding-glob\" setting";
1303 }else if( fHasAnyCr ){
1304 if( crnlOk ){
1305 return 0; /* We don't want CR/NL warnings for this file. */
1306 }
1307 if( fHasLoneCrOnly ){
1308
+12 -1
--- src/checkin.c
+++ src/checkin.c
@@ -1239,10 +1239,11 @@
12391239
int fBinary; /* does the blob content appear to be binary? */
12401240
int lookFlags; /* output flags from looks_like_utf8/utf16() */
12411241
int fHasAnyCr; /* the blob contains one or more CR chars */
12421242
int fHasLoneCrOnly; /* all detected line endings are CR only */
12431243
int fHasCrLfOnly; /* all detected line endings are CR/LF pairs */
1244
+ int fHasInvalidUtf8 = 0;/* contains byte-sequence which is invalid for UTF-8 */
12441245
char *zMsg; /* Warning message */
12451246
Blob fname; /* Relative pathname of the file */
12461247
static int allOk = 0; /* Set to true to disable this routine */
12471248
12481249
if( allOk ) return 0;
@@ -1249,16 +1250,19 @@
12491250
fUnicode = could_be_utf16(p, &bReverse);
12501251
if( fUnicode ){
12511252
lookFlags = looks_like_utf16(p, bReverse, LOOK_NUL);
12521253
}else{
12531254
lookFlags = looks_like_utf8(p, LOOK_NUL);
1255
+ if( !(lookFlags & LOOK_BINARY) && invalid_utf8(p) ){
1256
+ fHasInvalidUtf8 = 1;
1257
+ }
12541258
}
12551259
fHasAnyCr = (lookFlags & LOOK_CR);
12561260
fBinary = (lookFlags & LOOK_BINARY);
12571261
fHasLoneCrOnly = ((lookFlags & LOOK_EOL) == LOOK_LONE_CR);
12581262
fHasCrLfOnly = ((lookFlags & LOOK_EOL) == LOOK_CRLF);
1259
- if( fUnicode || fHasAnyCr || fBinary ){
1263
+ if( fUnicode || fHasAnyCr || fBinary || fHasInvalidUtf8){
12601264
const char *zWarning;
12611265
const char *zDisable;
12621266
const char *zConvert = "c=convert/";
12631267
Blob ans;
12641268
char cReply;
@@ -1287,10 +1291,17 @@
12871291
zWarning = "CR/NL line endings and Unicode";
12881292
}else{
12891293
zWarning = "mixed line endings and Unicode";
12901294
}
12911295
zDisable = "\"crnl-glob\" and \"encoding-glob\" settings";
1296
+ }else if( fHasInvalidUtf8 ){
1297
+ if( encodingOk ){
1298
+ return 0; /* We don't want encoding warnings for this file. */
1299
+ }
1300
+ zWarning = "invalid UTF-8";
1301
+ zConvert = ""; /* Possible conversion to UTF-8 not yet implemented. */
1302
+ zDisable = "\"encoding-glob\" setting";
12921303
}else if( fHasAnyCr ){
12931304
if( crnlOk ){
12941305
return 0; /* We don't want CR/NL warnings for this file. */
12951306
}
12961307
if( fHasLoneCrOnly ){
12971308
--- src/checkin.c
+++ src/checkin.c
@@ -1239,10 +1239,11 @@
1239 int fBinary; /* does the blob content appear to be binary? */
1240 int lookFlags; /* output flags from looks_like_utf8/utf16() */
1241 int fHasAnyCr; /* the blob contains one or more CR chars */
1242 int fHasLoneCrOnly; /* all detected line endings are CR only */
1243 int fHasCrLfOnly; /* all detected line endings are CR/LF pairs */
 
1244 char *zMsg; /* Warning message */
1245 Blob fname; /* Relative pathname of the file */
1246 static int allOk = 0; /* Set to true to disable this routine */
1247
1248 if( allOk ) return 0;
@@ -1249,16 +1250,19 @@
1249 fUnicode = could_be_utf16(p, &bReverse);
1250 if( fUnicode ){
1251 lookFlags = looks_like_utf16(p, bReverse, LOOK_NUL);
1252 }else{
1253 lookFlags = looks_like_utf8(p, LOOK_NUL);
 
 
 
1254 }
1255 fHasAnyCr = (lookFlags & LOOK_CR);
1256 fBinary = (lookFlags & LOOK_BINARY);
1257 fHasLoneCrOnly = ((lookFlags & LOOK_EOL) == LOOK_LONE_CR);
1258 fHasCrLfOnly = ((lookFlags & LOOK_EOL) == LOOK_CRLF);
1259 if( fUnicode || fHasAnyCr || fBinary ){
1260 const char *zWarning;
1261 const char *zDisable;
1262 const char *zConvert = "c=convert/";
1263 Blob ans;
1264 char cReply;
@@ -1287,10 +1291,17 @@
1287 zWarning = "CR/NL line endings and Unicode";
1288 }else{
1289 zWarning = "mixed line endings and Unicode";
1290 }
1291 zDisable = "\"crnl-glob\" and \"encoding-glob\" settings";
 
 
 
 
 
 
 
1292 }else if( fHasAnyCr ){
1293 if( crnlOk ){
1294 return 0; /* We don't want CR/NL warnings for this file. */
1295 }
1296 if( fHasLoneCrOnly ){
1297
--- src/checkin.c
+++ src/checkin.c
@@ -1239,10 +1239,11 @@
1239 int fBinary; /* does the blob content appear to be binary? */
1240 int lookFlags; /* output flags from looks_like_utf8/utf16() */
1241 int fHasAnyCr; /* the blob contains one or more CR chars */
1242 int fHasLoneCrOnly; /* all detected line endings are CR only */
1243 int fHasCrLfOnly; /* all detected line endings are CR/LF pairs */
1244 int fHasInvalidUtf8 = 0;/* contains byte-sequence which is invalid for UTF-8 */
1245 char *zMsg; /* Warning message */
1246 Blob fname; /* Relative pathname of the file */
1247 static int allOk = 0; /* Set to true to disable this routine */
1248
1249 if( allOk ) return 0;
@@ -1249,16 +1250,19 @@
1250 fUnicode = could_be_utf16(p, &bReverse);
1251 if( fUnicode ){
1252 lookFlags = looks_like_utf16(p, bReverse, LOOK_NUL);
1253 }else{
1254 lookFlags = looks_like_utf8(p, LOOK_NUL);
1255 if( !(lookFlags & LOOK_BINARY) && invalid_utf8(p) ){
1256 fHasInvalidUtf8 = 1;
1257 }
1258 }
1259 fHasAnyCr = (lookFlags & LOOK_CR);
1260 fBinary = (lookFlags & LOOK_BINARY);
1261 fHasLoneCrOnly = ((lookFlags & LOOK_EOL) == LOOK_LONE_CR);
1262 fHasCrLfOnly = ((lookFlags & LOOK_EOL) == LOOK_CRLF);
1263 if( fUnicode || fHasAnyCr || fBinary || fHasInvalidUtf8){
1264 const char *zWarning;
1265 const char *zDisable;
1266 const char *zConvert = "c=convert/";
1267 Blob ans;
1268 char cReply;
@@ -1287,10 +1291,17 @@
1291 zWarning = "CR/NL line endings and Unicode";
1292 }else{
1293 zWarning = "mixed line endings and Unicode";
1294 }
1295 zDisable = "\"crnl-glob\" and \"encoding-glob\" settings";
1296 }else if( fHasInvalidUtf8 ){
1297 if( encodingOk ){
1298 return 0; /* We don't want encoding warnings for this file. */
1299 }
1300 zWarning = "invalid UTF-8";
1301 zConvert = ""; /* Possible conversion to UTF-8 not yet implemented. */
1302 zDisable = "\"encoding-glob\" setting";
1303 }else if( fHasAnyCr ){
1304 if( crnlOk ){
1305 return 0; /* We don't want CR/NL warnings for this file. */
1306 }
1307 if( fHasLoneCrOnly ){
1308
+6
--- src/db.c
+++ src/db.c
@@ -1442,22 +1442,28 @@
14421442
** Options:
14431443
** --template FILE copy settings from repository file
14441444
** --admin-user|-A USERNAME select given USERNAME as admin user
14451445
** --date-override DATETIME use DATETIME as time of the initial checkin
14461446
** (default: don't create initial checkin)
1447
+** --empty Do not create an initial empty checkin.
14471448
**
14481449
** See also: clone
14491450
*/
14501451
void create_repository_cmd(void){
14511452
char *zPassword;
14521453
const char *zTemplate; /* Repository from which to copy settings */
14531454
const char *zDate; /* Date of the initial check-in */
14541455
const char *zDefaultUser; /* Optional name of the default user */
1456
+ char const *zCreateEmpty; /* --empty flag set? */
14551457
14561458
zTemplate = find_option("template",0,1);
14571459
zDate = find_option("date-override",0,1);
14581460
zDefaultUser = find_option("admin-user","A",1);
1461
+ zCreateEmpty = find_option("empty", 0, 0);
1462
+ if(!zDate && !zCreateEmpty){
1463
+ zDate = "now";
1464
+ }
14591465
if( g.argc!=3 ){
14601466
usage("REPOSITORY-NAME");
14611467
}
14621468
db_create_repository(g.argv[2]);
14631469
db_open_repository(g.argv[2]);
14641470
--- src/db.c
+++ src/db.c
@@ -1442,22 +1442,28 @@
1442 ** Options:
1443 ** --template FILE copy settings from repository file
1444 ** --admin-user|-A USERNAME select given USERNAME as admin user
1445 ** --date-override DATETIME use DATETIME as time of the initial checkin
1446 ** (default: don't create initial checkin)
 
1447 **
1448 ** See also: clone
1449 */
1450 void create_repository_cmd(void){
1451 char *zPassword;
1452 const char *zTemplate; /* Repository from which to copy settings */
1453 const char *zDate; /* Date of the initial check-in */
1454 const char *zDefaultUser; /* Optional name of the default user */
 
1455
1456 zTemplate = find_option("template",0,1);
1457 zDate = find_option("date-override",0,1);
1458 zDefaultUser = find_option("admin-user","A",1);
 
 
 
 
1459 if( g.argc!=3 ){
1460 usage("REPOSITORY-NAME");
1461 }
1462 db_create_repository(g.argv[2]);
1463 db_open_repository(g.argv[2]);
1464
--- src/db.c
+++ src/db.c
@@ -1442,22 +1442,28 @@
1442 ** Options:
1443 ** --template FILE copy settings from repository file
1444 ** --admin-user|-A USERNAME select given USERNAME as admin user
1445 ** --date-override DATETIME use DATETIME as time of the initial checkin
1446 ** (default: don't create initial checkin)
1447 ** --empty Do not create an initial empty checkin.
1448 **
1449 ** See also: clone
1450 */
1451 void create_repository_cmd(void){
1452 char *zPassword;
1453 const char *zTemplate; /* Repository from which to copy settings */
1454 const char *zDate; /* Date of the initial check-in */
1455 const char *zDefaultUser; /* Optional name of the default user */
1456 char const *zCreateEmpty; /* --empty flag set? */
1457
1458 zTemplate = find_option("template",0,1);
1459 zDate = find_option("date-override",0,1);
1460 zDefaultUser = find_option("admin-user","A",1);
1461 zCreateEmpty = find_option("empty", 0, 0);
1462 if(!zDate && !zCreateEmpty){
1463 zDate = "now";
1464 }
1465 if( g.argc!=3 ){
1466 usage("REPOSITORY-NAME");
1467 }
1468 db_create_repository(g.argv[2]);
1469 db_open_repository(g.argv[2]);
1470
+6
--- src/db.c
+++ src/db.c
@@ -1442,22 +1442,28 @@
14421442
** Options:
14431443
** --template FILE copy settings from repository file
14441444
** --admin-user|-A USERNAME select given USERNAME as admin user
14451445
** --date-override DATETIME use DATETIME as time of the initial checkin
14461446
** (default: don't create initial checkin)
1447
+** --empty Do not create an initial empty checkin.
14471448
**
14481449
** See also: clone
14491450
*/
14501451
void create_repository_cmd(void){
14511452
char *zPassword;
14521453
const char *zTemplate; /* Repository from which to copy settings */
14531454
const char *zDate; /* Date of the initial check-in */
14541455
const char *zDefaultUser; /* Optional name of the default user */
1456
+ char const *zCreateEmpty; /* --empty flag set? */
14551457
14561458
zTemplate = find_option("template",0,1);
14571459
zDate = find_option("date-override",0,1);
14581460
zDefaultUser = find_option("admin-user","A",1);
1461
+ zCreateEmpty = find_option("empty", 0, 0);
1462
+ if(!zDate && !zCreateEmpty){
1463
+ zDate = "now";
1464
+ }
14591465
if( g.argc!=3 ){
14601466
usage("REPOSITORY-NAME");
14611467
}
14621468
db_create_repository(g.argv[2]);
14631469
db_open_repository(g.argv[2]);
14641470
--- src/db.c
+++ src/db.c
@@ -1442,22 +1442,28 @@
1442 ** Options:
1443 ** --template FILE copy settings from repository file
1444 ** --admin-user|-A USERNAME select given USERNAME as admin user
1445 ** --date-override DATETIME use DATETIME as time of the initial checkin
1446 ** (default: don't create initial checkin)
 
1447 **
1448 ** See also: clone
1449 */
1450 void create_repository_cmd(void){
1451 char *zPassword;
1452 const char *zTemplate; /* Repository from which to copy settings */
1453 const char *zDate; /* Date of the initial check-in */
1454 const char *zDefaultUser; /* Optional name of the default user */
 
1455
1456 zTemplate = find_option("template",0,1);
1457 zDate = find_option("date-override",0,1);
1458 zDefaultUser = find_option("admin-user","A",1);
 
 
 
 
1459 if( g.argc!=3 ){
1460 usage("REPOSITORY-NAME");
1461 }
1462 db_create_repository(g.argv[2]);
1463 db_open_repository(g.argv[2]);
1464
--- src/db.c
+++ src/db.c
@@ -1442,22 +1442,28 @@
1442 ** Options:
1443 ** --template FILE copy settings from repository file
1444 ** --admin-user|-A USERNAME select given USERNAME as admin user
1445 ** --date-override DATETIME use DATETIME as time of the initial checkin
1446 ** (default: don't create initial checkin)
1447 ** --empty Do not create an initial empty checkin.
1448 **
1449 ** See also: clone
1450 */
1451 void create_repository_cmd(void){
1452 char *zPassword;
1453 const char *zTemplate; /* Repository from which to copy settings */
1454 const char *zDate; /* Date of the initial check-in */
1455 const char *zDefaultUser; /* Optional name of the default user */
1456 char const *zCreateEmpty; /* --empty flag set? */
1457
1458 zTemplate = find_option("template",0,1);
1459 zDate = find_option("date-override",0,1);
1460 zDefaultUser = find_option("admin-user","A",1);
1461 zCreateEmpty = find_option("empty", 0, 0);
1462 if(!zDate && !zCreateEmpty){
1463 zDate = "now";
1464 }
1465 if( g.argc!=3 ){
1466 usage("REPOSITORY-NAME");
1467 }
1468 db_create_repository(g.argv[2]);
1469 db_open_repository(g.argv[2]);
1470
+17 -1
--- src/export.c
+++ src/export.c
@@ -53,25 +53,41 @@
5353
zName[j] = 0;
5454
printf(" %s <%s>", zName, zUser);
5555
free(zName);
5656
return;
5757
}
58
+ /*
59
+ ** We have contact information.
60
+ ** It may or may not contain an email address.
61
+ */
5862
zContact = db_column_text(&q, 0);
5963
for(i=0; zContact[i] && zContact[i]!='>' && zContact[i]!='<'; i++){}
6064
if( zContact[i]==0 ){
65
+ /* No email address found. Take as user info if not empty */
6166
printf(" %s <%s>", zContact[0] ? zContact : zUser, zUser);
6267
db_reset(&q);
6368
return;
6469
}
6570
if( zContact[i]=='<' ){
71
+ /*
72
+ ** Found beginning of email address. Look for the end and extract
73
+ ** the part.
74
+ */
6675
zEmail = mprintf("%s", &zContact[i]);
6776
for(i=0; zEmail[i] && zEmail[i]!='>'; i++){}
6877
if( zEmail[i]=='>' ) zEmail[i+1] = 0;
6978
}else{
79
+ /*
80
+ ** Found an end marker for email, but nothing else.
81
+ */
7082
zEmail = mprintf("<%s>", zUser);
7183
}
72
- zName = mprintf("%.*s", i, zContact);
84
+ /*
85
+ ** Here zContact[i] either '<' or '>'. Extract the string _before_
86
+ ** either as user name.
87
+ */
88
+ zName = mprintf("%.*s", i-1, zContact);
7389
for(i=j=0; zName[i]; i++){
7490
if( zName[i]!='"' ) zName[j++] = zName[i];
7591
}
7692
zName[j] = 0;
7793
printf(" %s %s", zName, zEmail);
7894
--- src/export.c
+++ src/export.c
@@ -53,25 +53,41 @@
53 zName[j] = 0;
54 printf(" %s <%s>", zName, zUser);
55 free(zName);
56 return;
57 }
 
 
 
 
58 zContact = db_column_text(&q, 0);
59 for(i=0; zContact[i] && zContact[i]!='>' && zContact[i]!='<'; i++){}
60 if( zContact[i]==0 ){
 
61 printf(" %s <%s>", zContact[0] ? zContact : zUser, zUser);
62 db_reset(&q);
63 return;
64 }
65 if( zContact[i]=='<' ){
 
 
 
 
66 zEmail = mprintf("%s", &zContact[i]);
67 for(i=0; zEmail[i] && zEmail[i]!='>'; i++){}
68 if( zEmail[i]=='>' ) zEmail[i+1] = 0;
69 }else{
 
 
 
70 zEmail = mprintf("<%s>", zUser);
71 }
72 zName = mprintf("%.*s", i, zContact);
 
 
 
 
73 for(i=j=0; zName[i]; i++){
74 if( zName[i]!='"' ) zName[j++] = zName[i];
75 }
76 zName[j] = 0;
77 printf(" %s %s", zName, zEmail);
78
--- src/export.c
+++ src/export.c
@@ -53,25 +53,41 @@
53 zName[j] = 0;
54 printf(" %s <%s>", zName, zUser);
55 free(zName);
56 return;
57 }
58 /*
59 ** We have contact information.
60 ** It may or may not contain an email address.
61 */
62 zContact = db_column_text(&q, 0);
63 for(i=0; zContact[i] && zContact[i]!='>' && zContact[i]!='<'; i++){}
64 if( zContact[i]==0 ){
65 /* No email address found. Take as user info if not empty */
66 printf(" %s <%s>", zContact[0] ? zContact : zUser, zUser);
67 db_reset(&q);
68 return;
69 }
70 if( zContact[i]=='<' ){
71 /*
72 ** Found beginning of email address. Look for the end and extract
73 ** the part.
74 */
75 zEmail = mprintf("%s", &zContact[i]);
76 for(i=0; zEmail[i] && zEmail[i]!='>'; i++){}
77 if( zEmail[i]=='>' ) zEmail[i+1] = 0;
78 }else{
79 /*
80 ** Found an end marker for email, but nothing else.
81 */
82 zEmail = mprintf("<%s>", zUser);
83 }
84 /*
85 ** Here zContact[i] either '<' or '>'. Extract the string _before_
86 ** either as user name.
87 */
88 zName = mprintf("%.*s", i-1, zContact);
89 for(i=j=0; zName[i]; i++){
90 if( zName[i]!='"' ) zName[j++] = zName[i];
91 }
92 zName[j] = 0;
93 printf(" %s %s", zName, zEmail);
94
--- src/json_artifact.c
+++ src/json_artifact.c
@@ -245,29 +245,29 @@
245245
** If the "format" (CLI: -f) flag is set function returns the same as
246246
** json_wiki_get_content_format_flag(), else it returns true (non-0)
247247
** if either the includeContent (HTTP) or -content|-c boolean flags
248248
** (CLI) are set.
249249
*/
250
-static char json_artifact_get_content_format_flag(){
250
+static int json_artifact_get_content_format_flag(){
251251
enum { MagicValue = -9 };
252
- char contentFormat = json_wiki_get_content_format_flag(MagicValue);
252
+ int contentFormat = json_wiki_get_content_format_flag(MagicValue);
253253
if(MagicValue == contentFormat){
254254
contentFormat = json_find_option_bool("includeContent","content","c",0) /* deprecated */ ? -1 : 0;
255255
}
256256
return contentFormat;
257257
}
258258
259
-extern char json_wiki_get_content_format_flag( char defaultValue ) /* json_wiki.c */;
259
+extern int json_wiki_get_content_format_flag( int defaultValue ) /* json_wiki.c */;
260260
261261
cson_value * json_artifact_wiki(cson_object * zParent, int rid){
262262
if( ! g.perm.RdWiki ){
263263
json_set_err(FSL_JSON_E_DENIED,
264264
"Requires 'j' privileges.");
265265
return NULL;
266266
}else{
267267
enum { MagicValue = -9 };
268
- char const contentFormat = json_artifact_get_content_format_flag();
268
+ int const contentFormat = json_artifact_get_content_format_flag();
269269
return json_get_wiki_page_by_rid(rid, contentFormat);
270270
}
271271
}
272272
273273
/*
@@ -289,11 +289,11 @@
289289
290290
cson_value * json_artifact_file(cson_object * zParent, int rid){
291291
cson_object * pay = NULL;
292292
Stmt q = empty_Stmt;
293293
cson_array * checkin_arr = NULL;
294
- char contentFormat;
294
+ int contentFormat;
295295
i64 contentSize = -1;
296296
char * parentUuid;
297297
if( ! g.perm.Read ){
298298
json_set_err(FSL_JSON_E_DENIED,
299299
"Requires 'o' privileges.");
300300
--- src/json_artifact.c
+++ src/json_artifact.c
@@ -245,29 +245,29 @@
245 ** If the "format" (CLI: -f) flag is set function returns the same as
246 ** json_wiki_get_content_format_flag(), else it returns true (non-0)
247 ** if either the includeContent (HTTP) or -content|-c boolean flags
248 ** (CLI) are set.
249 */
250 static char json_artifact_get_content_format_flag(){
251 enum { MagicValue = -9 };
252 char contentFormat = json_wiki_get_content_format_flag(MagicValue);
253 if(MagicValue == contentFormat){
254 contentFormat = json_find_option_bool("includeContent","content","c",0) /* deprecated */ ? -1 : 0;
255 }
256 return contentFormat;
257 }
258
259 extern char json_wiki_get_content_format_flag( char defaultValue ) /* json_wiki.c */;
260
261 cson_value * json_artifact_wiki(cson_object * zParent, int rid){
262 if( ! g.perm.RdWiki ){
263 json_set_err(FSL_JSON_E_DENIED,
264 "Requires 'j' privileges.");
265 return NULL;
266 }else{
267 enum { MagicValue = -9 };
268 char const contentFormat = json_artifact_get_content_format_flag();
269 return json_get_wiki_page_by_rid(rid, contentFormat);
270 }
271 }
272
273 /*
@@ -289,11 +289,11 @@
289
290 cson_value * json_artifact_file(cson_object * zParent, int rid){
291 cson_object * pay = NULL;
292 Stmt q = empty_Stmt;
293 cson_array * checkin_arr = NULL;
294 char contentFormat;
295 i64 contentSize = -1;
296 char * parentUuid;
297 if( ! g.perm.Read ){
298 json_set_err(FSL_JSON_E_DENIED,
299 "Requires 'o' privileges.");
300
--- src/json_artifact.c
+++ src/json_artifact.c
@@ -245,29 +245,29 @@
245 ** If the "format" (CLI: -f) flag is set function returns the same as
246 ** json_wiki_get_content_format_flag(), else it returns true (non-0)
247 ** if either the includeContent (HTTP) or -content|-c boolean flags
248 ** (CLI) are set.
249 */
250 static int json_artifact_get_content_format_flag(){
251 enum { MagicValue = -9 };
252 int contentFormat = json_wiki_get_content_format_flag(MagicValue);
253 if(MagicValue == contentFormat){
254 contentFormat = json_find_option_bool("includeContent","content","c",0) /* deprecated */ ? -1 : 0;
255 }
256 return contentFormat;
257 }
258
259 extern int json_wiki_get_content_format_flag( int defaultValue ) /* json_wiki.c */;
260
261 cson_value * json_artifact_wiki(cson_object * zParent, int rid){
262 if( ! g.perm.RdWiki ){
263 json_set_err(FSL_JSON_E_DENIED,
264 "Requires 'j' privileges.");
265 return NULL;
266 }else{
267 enum { MagicValue = -9 };
268 int const contentFormat = json_artifact_get_content_format_flag();
269 return json_get_wiki_page_by_rid(rid, contentFormat);
270 }
271 }
272
273 /*
@@ -289,11 +289,11 @@
289
290 cson_value * json_artifact_file(cson_object * zParent, int rid){
291 cson_object * pay = NULL;
292 Stmt q = empty_Stmt;
293 cson_array * checkin_arr = NULL;
294 int contentFormat;
295 i64 contentSize = -1;
296 char * parentUuid;
297 if( ! g.perm.Read ){
298 json_set_err(FSL_JSON_E_DENIED,
299 "Requires 'o' privileges.");
300
+11 -7
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -80,11 +80,11 @@
8080
** the page.
8181
**
8282
** The returned value, if not NULL, is-a JSON Object owned by the
8383
** caller. If it returns NULL then it may set g.json's error state.
8484
*/
85
-cson_value * json_get_wiki_page_by_rid(int rid, char contentFormat){
85
+cson_value * json_get_wiki_page_by_rid(int rid, int contentFormat){
8686
Manifest * pWiki = NULL;
8787
if( NULL == (pWiki = manifest_get(rid, CFTYPE_WIKI, 0)) ){
8888
json_set_err( FSL_JSON_E_UNKNOWN,
8989
"Error reading wiki page from manifest (rid=%d).",
9090
rid );
@@ -145,11 +145,11 @@
145145
/*
146146
** Searches for the latest version of a wiki page with the given
147147
** name. If found it behaves like json_get_wiki_page_by_rid(theRid,
148148
** contentFormat), else it returns NULL.
149149
*/
150
-cson_value * json_get_wiki_page_by_name(char const * zPageName, char contentFormat){
150
+cson_value * json_get_wiki_page_by_name(char const * zPageName, int contentFormat){
151151
int rid;
152152
rid = db_int(0,
153153
"SELECT x.rid FROM tag t, tagxref x, blob b"
154154
" WHERE x.tagid=t.tagid AND t.tagname='wiki-%q' "
155155
" AND b.rid=x.rid"
@@ -175,12 +175,12 @@
175175
** [r]aw = -1
176176
**
177177
** The return value is intended for use with
178178
** json_get_wiki_page_by_rid() and friends.
179179
*/
180
-char json_wiki_get_content_format_flag( char defaultValue ){
181
- char contentFormat = defaultValue;
180
+int json_wiki_get_content_format_flag( int defaultValue ){
181
+ int contentFormat = defaultValue;
182182
char const * zFormat = json_find_option_cstr("format",NULL,"f");
183183
if( !zFormat || !*zFormat ){
184184
return contentFormat;
185185
}
186186
else if('r'==*zFormat){
@@ -203,11 +203,11 @@
203203
** json_get_wiki_page_by_name() will be returned (owned by the
204204
** caller). On error g.json's error state is set and NULL is returned.
205205
*/
206206
static cson_value * json_wiki_get_by_name_or_symname(char const * zPageName,
207207
char const * zSymname,
208
- char contentFormat ){
208
+ int contentFormat ){
209209
if(!zSymname || !*zSymname){
210210
return json_get_wiki_page_by_name(zPageName, contentFormat);
211211
}else{
212212
int rid = symbolic_name_to_rid( zSymname ? zSymname : zPageName, "w" );
213213
if(rid<0){
@@ -229,11 +229,11 @@
229229
**
230230
*/
231231
static cson_value * json_wiki_get(){
232232
char const * zPageName;
233233
char const * zSymName = NULL;
234
- char contentFormat = -1;
234
+ int contentFormat = -1;
235235
if( !g.perm.RdWiki && !g.perm.Read ){
236236
json_set_err(FSL_JSON_E_DENIED,
237237
"Requires 'o' or 'j' access.");
238238
return NULL;
239239
}
@@ -308,10 +308,11 @@
308308
char const * zPageName; /* cstr form of page name */
309309
cson_value * contentV; /* passed-in content */
310310
cson_value * emptyContent = NULL; /* placeholder for empty content. */
311311
cson_value * payV = NULL; /* payload/return value */
312312
cson_string const * jstr = NULL; /* temp for cson_value-to-cson_string conversions. */
313
+ char const * zMimeType = 0;
313314
unsigned int contentLen = 0;
314315
int rid;
315316
if( (createMode && !g.perm.NewWiki)
316317
|| (!createMode && !g.perm.WrWiki)){
317318
json_set_err(FSL_JSON_E_DENIED,
@@ -371,11 +372,14 @@
371372
jstr = cson_value_get_string(contentV);
372373
contentLen = (int)cson_string_length_bytes(jstr);
373374
if(contentLen){
374375
blob_append(&content, cson_string_cstr(jstr),contentLen);
375376
}
376
- wiki_cmd_commit(zPageName, 0==rid, &content);
377
+
378
+ zMimeType = json_find_option_cstr("mimetype","mimetype","M");
379
+
380
+ wiki_cmd_commit(zPageName, 0==rid, &content, zMimeType);
377381
blob_reset(&content);
378382
/*
379383
Our return value here has a race condition: if this operation
380384
is called concurrently for the same wiki page via two requests,
381385
payV could reflect the results of the other save operation.
382386
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -80,11 +80,11 @@
80 ** the page.
81 **
82 ** The returned value, if not NULL, is-a JSON Object owned by the
83 ** caller. If it returns NULL then it may set g.json's error state.
84 */
85 cson_value * json_get_wiki_page_by_rid(int rid, char contentFormat){
86 Manifest * pWiki = NULL;
87 if( NULL == (pWiki = manifest_get(rid, CFTYPE_WIKI, 0)) ){
88 json_set_err( FSL_JSON_E_UNKNOWN,
89 "Error reading wiki page from manifest (rid=%d).",
90 rid );
@@ -145,11 +145,11 @@
145 /*
146 ** Searches for the latest version of a wiki page with the given
147 ** name. If found it behaves like json_get_wiki_page_by_rid(theRid,
148 ** contentFormat), else it returns NULL.
149 */
150 cson_value * json_get_wiki_page_by_name(char const * zPageName, char contentFormat){
151 int rid;
152 rid = db_int(0,
153 "SELECT x.rid FROM tag t, tagxref x, blob b"
154 " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q' "
155 " AND b.rid=x.rid"
@@ -175,12 +175,12 @@
175 ** [r]aw = -1
176 **
177 ** The return value is intended for use with
178 ** json_get_wiki_page_by_rid() and friends.
179 */
180 char json_wiki_get_content_format_flag( char defaultValue ){
181 char contentFormat = defaultValue;
182 char const * zFormat = json_find_option_cstr("format",NULL,"f");
183 if( !zFormat || !*zFormat ){
184 return contentFormat;
185 }
186 else if('r'==*zFormat){
@@ -203,11 +203,11 @@
203 ** json_get_wiki_page_by_name() will be returned (owned by the
204 ** caller). On error g.json's error state is set and NULL is returned.
205 */
206 static cson_value * json_wiki_get_by_name_or_symname(char const * zPageName,
207 char const * zSymname,
208 char contentFormat ){
209 if(!zSymname || !*zSymname){
210 return json_get_wiki_page_by_name(zPageName, contentFormat);
211 }else{
212 int rid = symbolic_name_to_rid( zSymname ? zSymname : zPageName, "w" );
213 if(rid<0){
@@ -229,11 +229,11 @@
229 **
230 */
231 static cson_value * json_wiki_get(){
232 char const * zPageName;
233 char const * zSymName = NULL;
234 char contentFormat = -1;
235 if( !g.perm.RdWiki && !g.perm.Read ){
236 json_set_err(FSL_JSON_E_DENIED,
237 "Requires 'o' or 'j' access.");
238 return NULL;
239 }
@@ -308,10 +308,11 @@
308 char const * zPageName; /* cstr form of page name */
309 cson_value * contentV; /* passed-in content */
310 cson_value * emptyContent = NULL; /* placeholder for empty content. */
311 cson_value * payV = NULL; /* payload/return value */
312 cson_string const * jstr = NULL; /* temp for cson_value-to-cson_string conversions. */
 
313 unsigned int contentLen = 0;
314 int rid;
315 if( (createMode && !g.perm.NewWiki)
316 || (!createMode && !g.perm.WrWiki)){
317 json_set_err(FSL_JSON_E_DENIED,
@@ -371,11 +372,14 @@
371 jstr = cson_value_get_string(contentV);
372 contentLen = (int)cson_string_length_bytes(jstr);
373 if(contentLen){
374 blob_append(&content, cson_string_cstr(jstr),contentLen);
375 }
376 wiki_cmd_commit(zPageName, 0==rid, &content);
 
 
 
377 blob_reset(&content);
378 /*
379 Our return value here has a race condition: if this operation
380 is called concurrently for the same wiki page via two requests,
381 payV could reflect the results of the other save operation.
382
--- src/json_wiki.c
+++ src/json_wiki.c
@@ -80,11 +80,11 @@
80 ** the page.
81 **
82 ** The returned value, if not NULL, is-a JSON Object owned by the
83 ** caller. If it returns NULL then it may set g.json's error state.
84 */
85 cson_value * json_get_wiki_page_by_rid(int rid, int contentFormat){
86 Manifest * pWiki = NULL;
87 if( NULL == (pWiki = manifest_get(rid, CFTYPE_WIKI, 0)) ){
88 json_set_err( FSL_JSON_E_UNKNOWN,
89 "Error reading wiki page from manifest (rid=%d).",
90 rid );
@@ -145,11 +145,11 @@
145 /*
146 ** Searches for the latest version of a wiki page with the given
147 ** name. If found it behaves like json_get_wiki_page_by_rid(theRid,
148 ** contentFormat), else it returns NULL.
149 */
150 cson_value * json_get_wiki_page_by_name(char const * zPageName, int contentFormat){
151 int rid;
152 rid = db_int(0,
153 "SELECT x.rid FROM tag t, tagxref x, blob b"
154 " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q' "
155 " AND b.rid=x.rid"
@@ -175,12 +175,12 @@
175 ** [r]aw = -1
176 **
177 ** The return value is intended for use with
178 ** json_get_wiki_page_by_rid() and friends.
179 */
180 int json_wiki_get_content_format_flag( int defaultValue ){
181 int contentFormat = defaultValue;
182 char const * zFormat = json_find_option_cstr("format",NULL,"f");
183 if( !zFormat || !*zFormat ){
184 return contentFormat;
185 }
186 else if('r'==*zFormat){
@@ -203,11 +203,11 @@
203 ** json_get_wiki_page_by_name() will be returned (owned by the
204 ** caller). On error g.json's error state is set and NULL is returned.
205 */
206 static cson_value * json_wiki_get_by_name_or_symname(char const * zPageName,
207 char const * zSymname,
208 int contentFormat ){
209 if(!zSymname || !*zSymname){
210 return json_get_wiki_page_by_name(zPageName, contentFormat);
211 }else{
212 int rid = symbolic_name_to_rid( zSymname ? zSymname : zPageName, "w" );
213 if(rid<0){
@@ -229,11 +229,11 @@
229 **
230 */
231 static cson_value * json_wiki_get(){
232 char const * zPageName;
233 char const * zSymName = NULL;
234 int contentFormat = -1;
235 if( !g.perm.RdWiki && !g.perm.Read ){
236 json_set_err(FSL_JSON_E_DENIED,
237 "Requires 'o' or 'j' access.");
238 return NULL;
239 }
@@ -308,10 +308,11 @@
308 char const * zPageName; /* cstr form of page name */
309 cson_value * contentV; /* passed-in content */
310 cson_value * emptyContent = NULL; /* placeholder for empty content. */
311 cson_value * payV = NULL; /* payload/return value */
312 cson_string const * jstr = NULL; /* temp for cson_value-to-cson_string conversions. */
313 char const * zMimeType = 0;
314 unsigned int contentLen = 0;
315 int rid;
316 if( (createMode && !g.perm.NewWiki)
317 || (!createMode && !g.perm.WrWiki)){
318 json_set_err(FSL_JSON_E_DENIED,
@@ -371,11 +372,14 @@
372 jstr = cson_value_get_string(contentV);
373 contentLen = (int)cson_string_length_bytes(jstr);
374 if(contentLen){
375 blob_append(&content, cson_string_cstr(jstr),contentLen);
376 }
377
378 zMimeType = json_find_option_cstr("mimetype","mimetype","M");
379
380 wiki_cmd_commit(zPageName, 0==rid, &content, zMimeType);
381 blob_reset(&content);
382 /*
383 Our return value here has a race condition: if this operation
384 is called concurrently for the same wiki page via two requests,
385 payV could reflect the results of the other save operation.
386
+21 -2
--- src/shell.c
+++ src/shell.c
@@ -695,11 +695,12 @@
695695
/*
696696
** This routine runs when the user presses Ctrl-C
697697
*/
698698
static void interrupt_handler(int NotUsed){
699699
UNUSED_PARAMETER(NotUsed);
700
- seenInterrupt = 1;
700
+ seenInterrupt++;
701
+ if( seenInterrupt>2 ) exit(1);
701702
if( db ) sqlite3_interrupt(db);
702703
}
703704
#endif
704705
705706
/*
@@ -1577,11 +1578,11 @@
15771578
" LIKE pattern TABLE.\n"
15781579
".echo ON|OFF Turn command echo on or off\n"
15791580
".exit Exit this program\n"
15801581
".explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.\n"
15811582
" With no args, it turns EXPLAIN on.\n"
1582
- ".header(s) ON|OFF Turn display of headers on or off\n"
1583
+ ".headers ON|OFF Turn display of headers on or off\n"
15831584
".help Show this message\n"
15841585
".import FILE TABLE Import data from FILE into TABLE\n"
15851586
".indices ?TABLE? Show names of all indices\n"
15861587
" If TABLE specified, only show indices for tables\n"
15871588
" matching LIKE pattern TABLE.\n"
@@ -1613,19 +1614,22 @@
16131614
".save FILE Write in-memory database into FILE\n"
16141615
".schema ?TABLE? Show the CREATE statements\n"
16151616
" If TABLE specified, only show tables matching\n"
16161617
" LIKE pattern TABLE.\n"
16171618
".separator STRING Change separator used by output mode and .import\n"
1619
+ ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
16181620
".show Show the current values for various settings\n"
16191621
".stats ON|OFF Turn stats on or off\n"
1622
+ ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
16201623
".tables ?TABLE? List names of tables\n"
16211624
" If TABLE specified, only list tables matching\n"
16221625
" LIKE pattern TABLE.\n"
16231626
".timeout MS Try opening locked tables for MS milliseconds\n"
16241627
".trace FILE|off Output each SQL statement as it is run\n"
16251628
".vfsname ?AUX? Print the name of the VFS stack\n"
16261629
".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1630
+ " Negative values right-justify\n"
16271631
;
16281632
16291633
static char zTimerHelp[] =
16301634
".timer ON|OFF Turn the CPU timer measurement on or off\n"
16311635
;
@@ -2428,10 +2432,11 @@
24282432
xCloser(sCsv.in);
24292433
return 1;
24302434
}
24312435
nByte = strlen30(zSql);
24322436
rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2437
+ csv_append_char(&sCsv, 0); /* To ensure sCsv.z is allocated */
24332438
if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(db))==0 ){
24342439
char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
24352440
char cSep = '(';
24362441
while( csv_read_one_field(&sCsv) ){
24372442
zCreate = sqlite3_mprintf("%z%c\n \"%s\" TEXT", zCreate, cSep, sCsv.z);
@@ -2895,10 +2900,24 @@
28952900
28962901
if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
28972902
sqlite3_snprintf(sizeof(p->separator), p->separator,
28982903
"%.*s", (int)sizeof(p->separator)-1, azArg[1]);
28992904
}else
2905
+
2906
+ if( c=='s'
2907
+ && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
2908
+ && nArg>=2
2909
+ ){
2910
+ char *zCmd;
2911
+ int i;
2912
+ zCmd = sqlite3_mprintf("\"%s\"", azArg[1]);
2913
+ for(i=2; i<nArg; i++){
2914
+ zCmd = sqlite3_mprintf("%z \"%s\"", zCmd, azArg[i]);
2915
+ }
2916
+ system(zCmd);
2917
+ sqlite3_free(zCmd);
2918
+ }else
29002919
29012920
if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
29022921
int i;
29032922
fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
29042923
fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
29052924
--- src/shell.c
+++ src/shell.c
@@ -695,11 +695,12 @@
695 /*
696 ** This routine runs when the user presses Ctrl-C
697 */
698 static void interrupt_handler(int NotUsed){
699 UNUSED_PARAMETER(NotUsed);
700 seenInterrupt = 1;
 
701 if( db ) sqlite3_interrupt(db);
702 }
703 #endif
704
705 /*
@@ -1577,11 +1578,11 @@
1577 " LIKE pattern TABLE.\n"
1578 ".echo ON|OFF Turn command echo on or off\n"
1579 ".exit Exit this program\n"
1580 ".explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.\n"
1581 " With no args, it turns EXPLAIN on.\n"
1582 ".header(s) ON|OFF Turn display of headers on or off\n"
1583 ".help Show this message\n"
1584 ".import FILE TABLE Import data from FILE into TABLE\n"
1585 ".indices ?TABLE? Show names of all indices\n"
1586 " If TABLE specified, only show indices for tables\n"
1587 " matching LIKE pattern TABLE.\n"
@@ -1613,19 +1614,22 @@
1613 ".save FILE Write in-memory database into FILE\n"
1614 ".schema ?TABLE? Show the CREATE statements\n"
1615 " If TABLE specified, only show tables matching\n"
1616 " LIKE pattern TABLE.\n"
1617 ".separator STRING Change separator used by output mode and .import\n"
 
1618 ".show Show the current values for various settings\n"
1619 ".stats ON|OFF Turn stats on or off\n"
 
1620 ".tables ?TABLE? List names of tables\n"
1621 " If TABLE specified, only list tables matching\n"
1622 " LIKE pattern TABLE.\n"
1623 ".timeout MS Try opening locked tables for MS milliseconds\n"
1624 ".trace FILE|off Output each SQL statement as it is run\n"
1625 ".vfsname ?AUX? Print the name of the VFS stack\n"
1626 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
 
1627 ;
1628
1629 static char zTimerHelp[] =
1630 ".timer ON|OFF Turn the CPU timer measurement on or off\n"
1631 ;
@@ -2428,10 +2432,11 @@
2428 xCloser(sCsv.in);
2429 return 1;
2430 }
2431 nByte = strlen30(zSql);
2432 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
 
2433 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(db))==0 ){
2434 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
2435 char cSep = '(';
2436 while( csv_read_one_field(&sCsv) ){
2437 zCreate = sqlite3_mprintf("%z%c\n \"%s\" TEXT", zCreate, cSep, sCsv.z);
@@ -2895,10 +2900,24 @@
2895
2896 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
2897 sqlite3_snprintf(sizeof(p->separator), p->separator,
2898 "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
2899 }else
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2900
2901 if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
2902 int i;
2903 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
2904 fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
2905
--- src/shell.c
+++ src/shell.c
@@ -695,11 +695,12 @@
695 /*
696 ** This routine runs when the user presses Ctrl-C
697 */
698 static void interrupt_handler(int NotUsed){
699 UNUSED_PARAMETER(NotUsed);
700 seenInterrupt++;
701 if( seenInterrupt>2 ) exit(1);
702 if( db ) sqlite3_interrupt(db);
703 }
704 #endif
705
706 /*
@@ -1577,11 +1578,11 @@
1578 " LIKE pattern TABLE.\n"
1579 ".echo ON|OFF Turn command echo on or off\n"
1580 ".exit Exit this program\n"
1581 ".explain ?ON|OFF? Turn output mode suitable for EXPLAIN on or off.\n"
1582 " With no args, it turns EXPLAIN on.\n"
1583 ".headers ON|OFF Turn display of headers on or off\n"
1584 ".help Show this message\n"
1585 ".import FILE TABLE Import data from FILE into TABLE\n"
1586 ".indices ?TABLE? Show names of all indices\n"
1587 " If TABLE specified, only show indices for tables\n"
1588 " matching LIKE pattern TABLE.\n"
@@ -1613,19 +1614,22 @@
1614 ".save FILE Write in-memory database into FILE\n"
1615 ".schema ?TABLE? Show the CREATE statements\n"
1616 " If TABLE specified, only show tables matching\n"
1617 " LIKE pattern TABLE.\n"
1618 ".separator STRING Change separator used by output mode and .import\n"
1619 ".shell CMD ARGS... Run CMD ARGS... in a system shell\n"
1620 ".show Show the current values for various settings\n"
1621 ".stats ON|OFF Turn stats on or off\n"
1622 ".system CMD ARGS... Run CMD ARGS... in a system shell\n"
1623 ".tables ?TABLE? List names of tables\n"
1624 " If TABLE specified, only list tables matching\n"
1625 " LIKE pattern TABLE.\n"
1626 ".timeout MS Try opening locked tables for MS milliseconds\n"
1627 ".trace FILE|off Output each SQL statement as it is run\n"
1628 ".vfsname ?AUX? Print the name of the VFS stack\n"
1629 ".width NUM1 NUM2 ... Set column widths for \"column\" mode\n"
1630 " Negative values right-justify\n"
1631 ;
1632
1633 static char zTimerHelp[] =
1634 ".timer ON|OFF Turn the CPU timer measurement on or off\n"
1635 ;
@@ -2428,10 +2432,11 @@
2432 xCloser(sCsv.in);
2433 return 1;
2434 }
2435 nByte = strlen30(zSql);
2436 rc = sqlite3_prepare_v2(p->db, zSql, -1, &pStmt, 0);
2437 csv_append_char(&sCsv, 0); /* To ensure sCsv.z is allocated */
2438 if( rc && sqlite3_strglob("no such table: *", sqlite3_errmsg(db))==0 ){
2439 char *zCreate = sqlite3_mprintf("CREATE TABLE %s", zTable);
2440 char cSep = '(';
2441 while( csv_read_one_field(&sCsv) ){
2442 zCreate = sqlite3_mprintf("%z%c\n \"%s\" TEXT", zCreate, cSep, sCsv.z);
@@ -2895,10 +2900,24 @@
2900
2901 if( c=='s' && strncmp(azArg[0], "separator", n)==0 && nArg==2 ){
2902 sqlite3_snprintf(sizeof(p->separator), p->separator,
2903 "%.*s", (int)sizeof(p->separator)-1, azArg[1]);
2904 }else
2905
2906 if( c=='s'
2907 && (strncmp(azArg[0], "shell", n)==0 || strncmp(azArg[0],"system",n)==0)
2908 && nArg>=2
2909 ){
2910 char *zCmd;
2911 int i;
2912 zCmd = sqlite3_mprintf("\"%s\"", azArg[1]);
2913 for(i=2; i<nArg; i++){
2914 zCmd = sqlite3_mprintf("%z \"%s\"", zCmd, azArg[i]);
2915 }
2916 system(zCmd);
2917 sqlite3_free(zCmd);
2918 }else
2919
2920 if( c=='s' && strncmp(azArg[0], "show", n)==0 && nArg==1 ){
2921 int i;
2922 fprintf(p->out,"%9.9s: %s\n","echo", p->echoOn ? "on" : "off");
2923 fprintf(p->out,"%9.9s: %s\n","eqp", p->autoEQP ? "on" : "off");
2924
+2137 -1329
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -222,11 +222,11 @@
222222
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
223223
** [sqlite_version()] and [sqlite_source_id()].
224224
*/
225225
#define SQLITE_VERSION "3.8.5"
226226
#define SQLITE_VERSION_NUMBER 3008005
227
-#define SQLITE_SOURCE_ID "2014-04-18 22:20:31 9a5d38c79d2482a23bcfbc3ff35ca4fa269c768d"
227
+#define SQLITE_SOURCE_ID "2014-05-28 20:22:28 d018a34a05cec6adda61ed225d084c587343f2a6"
228228
229229
/*
230230
** CAPI3REF: Run-Time Library Version Numbers
231231
** KEYWORDS: sqlite3_version, sqlite3_sourceid
232232
**
@@ -673,11 +673,14 @@
673673
** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
674674
** after reboot following a crash or power loss, the only bytes in a
675675
** file that were written at the application level might have changed
676676
** and that adjacent bytes, even bytes within the same sector are
677677
** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
678
-** flag indicate that a file cannot be deleted when open.
678
+** flag indicate that a file cannot be deleted when open. The
679
+** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
680
+** read-only media and cannot be changed even by processes with
681
+** elevated privileges.
679682
*/
680683
#define SQLITE_IOCAP_ATOMIC 0x00000001
681684
#define SQLITE_IOCAP_ATOMIC512 0x00000002
682685
#define SQLITE_IOCAP_ATOMIC1K 0x00000004
683686
#define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -688,10 +691,11 @@
688691
#define SQLITE_IOCAP_ATOMIC64K 0x00000100
689692
#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
690693
#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
691694
#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
692695
#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
696
+#define SQLITE_IOCAP_IMMUTABLE 0x00002000
693697
694698
/*
695699
** CAPI3REF: File Locking Levels
696700
**
697701
** SQLite uses one of these integer values as the second
@@ -2892,10 +2896,34 @@
28922896
** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
28932897
** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
28942898
** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
28952899
** a URI filename, its value overrides any behavior requested by setting
28962900
** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2901
+**
2902
+** <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
2903
+** "1") or "false" (or "off" or "no" or "0") to indicate that the
2904
+** [powersafe overwrite] property does or does not apply to the
2905
+** storage media on which the database file resides. ^The psow query
2906
+** parameter only works for the built-in unix and Windows VFSes.
2907
+**
2908
+** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
2909
+** which if set disables file locking in rollback journal modes. This
2910
+** is useful for accessing a database on a filesystem that does not
2911
+** support locking. Caution: Database corruption might result if two
2912
+** or more processes write to the same database and any one of those
2913
+** processes uses nolock=1.
2914
+**
2915
+** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
2916
+** parameter that indicates that the database file is stored on
2917
+** read-only media. ^When immutable is set, SQLite assumes that the
2918
+** database file cannot be changed, even by a process with higher
2919
+** privilege, and so the database is opened read-only and all locking
2920
+** and change detection is disabled. Caution: Setting the immutable
2921
+** property on a database file that does in fact change can result
2922
+** in incorrect query results and/or [SQLITE_CORRUPT] errors.
2923
+** See also: [SQLITE_IOCAP_IMMUTABLE].
2924
+**
28972925
** </ul>
28982926
**
28992927
** ^Specifying an unknown parameter in the query component of a URI is not an
29002928
** error. Future versions of SQLite might understand additional query
29012929
** parameters. See "[query parameters with special meaning to SQLite]" for
@@ -2921,12 +2949,13 @@
29212949
** in URI filenames.
29222950
** <tr><td> file:data.db?mode=ro&cache=private <td>
29232951
** Open file "data.db" in the current directory for read-only access.
29242952
** Regardless of whether or not shared-cache mode is enabled by
29252953
** default, use a private cache.
2926
-** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
2927
-** Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
2954
+** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
2955
+** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
2956
+** that uses dot-files in place of posix advisory locking.
29282957
** <tr><td> file:data.db?mode=readonly <td>
29292958
** An error. "readonly" is not a valid option for the "mode" parameter.
29302959
** </table>
29312960
**
29322961
** ^URI hexadecimal escape sequences (%HH) are supported within the path and
@@ -7460,10 +7489,20 @@
74607489
#if 0
74617490
extern "C" {
74627491
#endif
74637492
74647493
typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7494
+typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
7495
+
7496
+/* The double-precision datatype used by RTree depends on the
7497
+** SQLITE_RTREE_INT_ONLY compile-time option.
7498
+*/
7499
+#ifdef SQLITE_RTREE_INT_ONLY
7500
+ typedef sqlite3_int64 sqlite3_rtree_dbl;
7501
+#else
7502
+ typedef double sqlite3_rtree_dbl;
7503
+#endif
74657504
74667505
/*
74677506
** Register a geometry callback named zGeom that can be used as part of an
74687507
** R-Tree geometry query as follows:
74697508
**
@@ -7470,15 +7509,11 @@
74707509
** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
74717510
*/
74727511
SQLITE_API int sqlite3_rtree_geometry_callback(
74737512
sqlite3 *db,
74747513
const char *zGeom,
7475
-#ifdef SQLITE_RTREE_INT_ONLY
7476
- int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
7477
-#else
7478
- int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
7479
-#endif
7514
+ int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
74807515
void *pContext
74817516
);
74827517
74837518
74847519
/*
@@ -7486,15 +7521,64 @@
74867521
** argument to callbacks registered using rtree_geometry_callback().
74877522
*/
74887523
struct sqlite3_rtree_geometry {
74897524
void *pContext; /* Copy of pContext passed to s_r_g_c() */
74907525
int nParam; /* Size of array aParam[] */
7491
- double *aParam; /* Parameters passed to SQL geom function */
7526
+ sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
74927527
void *pUser; /* Callback implementation user data */
74937528
void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
74947529
};
74957530
7531
+/*
7532
+** Register a 2nd-generation geometry callback named zScore that can be
7533
+** used as part of an R-Tree geometry query as follows:
7534
+**
7535
+** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
7536
+*/
7537
+SQLITE_API int sqlite3_rtree_query_callback(
7538
+ sqlite3 *db,
7539
+ const char *zQueryFunc,
7540
+ int (*xQueryFunc)(sqlite3_rtree_query_info*),
7541
+ void *pContext,
7542
+ void (*xDestructor)(void*)
7543
+);
7544
+
7545
+
7546
+/*
7547
+** A pointer to a structure of the following type is passed as the
7548
+** argument to scored geometry callback registered using
7549
+** sqlite3_rtree_query_callback().
7550
+**
7551
+** Note that the first 5 fields of this structure are identical to
7552
+** sqlite3_rtree_geometry. This structure is a subclass of
7553
+** sqlite3_rtree_geometry.
7554
+*/
7555
+struct sqlite3_rtree_query_info {
7556
+ void *pContext; /* pContext from when function registered */
7557
+ int nParam; /* Number of function parameters */
7558
+ sqlite3_rtree_dbl *aParam; /* value of function parameters */
7559
+ void *pUser; /* callback can use this, if desired */
7560
+ void (*xDelUser)(void*); /* function to free pUser */
7561
+ sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
7562
+ unsigned int *anQueue; /* Number of pending entries in the queue */
7563
+ int nCoord; /* Number of coordinates */
7564
+ int iLevel; /* Level of current node or entry */
7565
+ int mxLevel; /* The largest iLevel value in the tree */
7566
+ sqlite3_int64 iRowid; /* Rowid for current entry */
7567
+ sqlite3_rtree_dbl rParentScore; /* Score of parent node */
7568
+ int eParentWithin; /* Visibility of parent node */
7569
+ int eWithin; /* OUT: Visiblity */
7570
+ sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
7571
+};
7572
+
7573
+/*
7574
+** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
7575
+*/
7576
+#define NOT_WITHIN 0 /* Object completely outside of query region */
7577
+#define PARTLY_WITHIN 1 /* Object partially overlaps query region */
7578
+#define FULLY_WITHIN 2 /* Object fully contained within query region */
7579
+
74967580
74977581
#if 0
74987582
} /* end of the 'extern "C"' block */
74997583
#endif
75007584
@@ -8417,14 +8501,14 @@
84178501
** Estimated quantities used for query planning are stored as 16-bit
84188502
** logarithms. For quantity X, the value stored is 10*log2(X). This
84198503
** gives a possible range of values of approximately 1.0e986 to 1e-986.
84208504
** But the allowed values are "grainy". Not every value is representable.
84218505
** For example, quantities 16 and 17 are both represented by a LogEst
8422
-** of 40. However, since LogEst quantatites are suppose to be estimates,
8506
+** of 40. However, since LogEst quantaties are suppose to be estimates,
84238507
** not exact values, this imprecision is not a problem.
84248508
**
8425
-** "LogEst" is short for "Logarithimic Estimate".
8509
+** "LogEst" is short for "Logarithmic Estimate".
84268510
**
84278511
** Examples:
84288512
** 1 -> 0 20 -> 43 10000 -> 132
84298513
** 2 -> 10 25 -> 46 25000 -> 146
84308514
** 3 -> 16 100 -> 66 1000000 -> 199
@@ -8916,10 +9000,11 @@
89169000
SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
89179001
SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
89189002
SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
89199003
SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
89209004
SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
9005
+SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
89219006
89229007
#ifndef NDEBUG
89239008
SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
89249009
#endif
89259010
@@ -9870,87 +9955,75 @@
98709955
*/
98719956
#ifndef _SQLITE_OS_H_
98729957
#define _SQLITE_OS_H_
98739958
98749959
/*
9875
-** Figure out if we are dealing with Unix, Windows, or some other
9876
-** operating system. After the following block of preprocess macros,
9877
-** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
9878
-** will defined to either 1 or 0. One of the four will be 1. The other
9879
-** three will be 0.
9960
+** Attempt to automatically detect the operating system and setup the
9961
+** necessary pre-processor macros for it.
9962
+*/
9963
+/************** Include os_setup.h in the middle of os.h *********************/
9964
+/************** Begin file os_setup.h ****************************************/
9965
+/*
9966
+** 2013 November 25
9967
+**
9968
+** The author disclaims copyright to this source code. In place of
9969
+** a legal notice, here is a blessing:
9970
+**
9971
+** May you do good and not evil.
9972
+** May you find forgiveness for yourself and forgive others.
9973
+** May you share freely, never taking more than you give.
9974
+**
9975
+******************************************************************************
9976
+**
9977
+** This file contains pre-processor directives related to operating system
9978
+** detection and/or setup.
9979
+*/
9980
+#ifndef _OS_SETUP_H_
9981
+#define _OS_SETUP_H_
9982
+
9983
+/*
9984
+** Figure out if we are dealing with Unix, Windows, or some other operating
9985
+** system.
9986
+**
9987
+** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
9988
+** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
9989
+** the three will be 1. The other two will be 0.
98809990
*/
98819991
#if defined(SQLITE_OS_OTHER)
9882
-# if SQLITE_OS_OTHER==1
9883
-# undef SQLITE_OS_UNIX
9884
-# define SQLITE_OS_UNIX 0
9885
-# undef SQLITE_OS_WIN
9886
-# define SQLITE_OS_WIN 0
9887
-# else
9888
-# undef SQLITE_OS_OTHER
9889
-# endif
9992
+# if SQLITE_OS_OTHER==1
9993
+# undef SQLITE_OS_UNIX
9994
+# define SQLITE_OS_UNIX 0
9995
+# undef SQLITE_OS_WIN
9996
+# define SQLITE_OS_WIN 0
9997
+# else
9998
+# undef SQLITE_OS_OTHER
9999
+# endif
989010000
#endif
989110001
#if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
9892
-# define SQLITE_OS_OTHER 0
9893
-# ifndef SQLITE_OS_WIN
9894
-# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
9895
-# define SQLITE_OS_WIN 1
9896
-# define SQLITE_OS_UNIX 0
9897
-# else
9898
-# define SQLITE_OS_WIN 0
9899
-# define SQLITE_OS_UNIX 1
10002
+# define SQLITE_OS_OTHER 0
10003
+# ifndef SQLITE_OS_WIN
10004
+# if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
10005
+ defined(__MINGW32__) || defined(__BORLANDC__)
10006
+# define SQLITE_OS_WIN 1
10007
+# define SQLITE_OS_UNIX 0
10008
+# else
10009
+# define SQLITE_OS_WIN 0
10010
+# define SQLITE_OS_UNIX 1
10011
+# endif
10012
+# else
10013
+# define SQLITE_OS_UNIX 0
10014
+# endif
10015
+#else
10016
+# ifndef SQLITE_OS_WIN
10017
+# define SQLITE_OS_WIN 0
990010018
# endif
9901
-# else
9902
-# define SQLITE_OS_UNIX 0
9903
-# endif
9904
-#else
9905
-# ifndef SQLITE_OS_WIN
9906
-# define SQLITE_OS_WIN 0
9907
-# endif
9908
-#endif
9909
-
9910
-#if SQLITE_OS_WIN
9911
-# include <windows.h>
9912
-#endif
9913
-
9914
-/*
9915
-** Determine if we are dealing with Windows NT.
9916
-**
9917
-** We ought to be able to determine if we are compiling for win98 or winNT
9918
-** using the _WIN32_WINNT macro as follows:
9919
-**
9920
-** #if defined(_WIN32_WINNT)
9921
-** # define SQLITE_OS_WINNT 1
9922
-** #else
9923
-** # define SQLITE_OS_WINNT 0
9924
-** #endif
9925
-**
9926
-** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
9927
-** so the above test does not work. We'll just assume that everything is
9928
-** winNT unless the programmer explicitly says otherwise by setting
9929
-** SQLITE_OS_WINNT to 0.
9930
-*/
9931
-#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
9932
-# define SQLITE_OS_WINNT 1
9933
-#endif
9934
-
9935
-/*
9936
-** Determine if we are dealing with WindowsCE - which has a much
9937
-** reduced API.
9938
-*/
9939
-#if defined(_WIN32_WCE)
9940
-# define SQLITE_OS_WINCE 1
9941
-#else
9942
-# define SQLITE_OS_WINCE 0
9943
-#endif
9944
-
9945
-/*
9946
-** Determine if we are dealing with WinRT, which provides only a subset of
9947
-** the full Win32 API.
9948
-*/
9949
-#if !defined(SQLITE_OS_WINRT)
9950
-# define SQLITE_OS_WINRT 0
9951
-#endif
10019
+#endif
10020
+
10021
+#endif /* _OS_SETUP_H_ */
10022
+
10023
+/************** End of os_setup.h ********************************************/
10024
+/************** Continuing where we left off in os.h *************************/
995210025
995310026
/* If the SET_FULLSYNC macro is not defined above, then make it
995410027
** a no-op
995510028
*/
995610029
#ifndef SET_FULLSYNC
@@ -10845,11 +10918,11 @@
1084510918
FKey *pFKey; /* Linked list of all foreign keys in this table */
1084610919
char *zColAff; /* String defining the affinity of each column */
1084710920
#ifndef SQLITE_OMIT_CHECK
1084810921
ExprList *pCheck; /* All CHECK constraints */
1084910922
#endif
10850
- tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
10923
+ LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
1085110924
int tnum; /* Root BTree node for this table (see note above) */
1085210925
i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
1085310926
i16 nCol; /* Number of columns in this table */
1085410927
u16 nRef; /* Number of pointers to this Table */
1085510928
LogEst szTabRow; /* Estimated size of each table row in bytes */
@@ -11054,11 +11127,11 @@
1105411127
** element.
1105511128
*/
1105611129
struct Index {
1105711130
char *zName; /* Name of this index */
1105811131
i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
11059
- tRowcnt *aiRowEst; /* From ANALYZE: Est. rows selected by each column */
11132
+ LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
1106011133
Table *pTable; /* The SQL table being indexed */
1106111134
char *zColAff; /* String defining the affinity of each column */
1106211135
Index *pNext; /* The next index associated with the same table */
1106311136
Schema *pSchema; /* Schema containing this index */
1106411137
u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
@@ -11068,11 +11141,11 @@
1106811141
int tnum; /* DB Page containing root of this index */
1106911142
LogEst szIdxRow; /* Estimated average row size in bytes */
1107011143
u16 nKeyCol; /* Number of columns forming the key */
1107111144
u16 nColumn; /* Number of columns stored in the index */
1107211145
u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
11073
- unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
11146
+ unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
1107411147
unsigned bUnordered:1; /* Use this index for == or IN queries only */
1107511148
unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
1107611149
unsigned isResized:1; /* True if resizeIndexObject() has been called */
1107711150
unsigned isCovering:1; /* True if this is a covering index */
1107811151
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
@@ -11081,10 +11154,20 @@
1108111154
tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
1108211155
IndexSample *aSample; /* Samples of the left-most key */
1108311156
#endif
1108411157
};
1108511158
11159
+/*
11160
+** Allowed values for Index.idxType
11161
+*/
11162
+#define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
11163
+#define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
11164
+#define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
11165
+
11166
+/* Return true if index X is a PRIMARY KEY index */
11167
+#define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
11168
+
1108611169
/*
1108711170
** Each sample stored in the sqlite_stat3 table is represented in memory
1108811171
** using a structure of this type. See documentation at the top of the
1108911172
** analyze.c source file for additional information.
1109011173
*/
@@ -11499,10 +11582,11 @@
1149911582
#define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */
1150011583
#define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
1150111584
#define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
1150211585
#define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
1150311586
#define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
11587
+#define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */
1150411588
1150511589
/* Allowed return values from sqlite3WhereIsDistinct()
1150611590
*/
1150711591
#define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
1150811592
#define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
@@ -12082,15 +12166,14 @@
1208212166
int isInit; /* True after initialization has finished */
1208312167
int inProgress; /* True while initialization in progress */
1208412168
int isMutexInit; /* True after mutexes are initialized */
1208512169
int isMallocInit; /* True after malloc is initialized */
1208612170
int isPCacheInit; /* True after malloc is initialized */
12087
- sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
1208812171
int nRefInitMutex; /* Number of users of pInitMutex */
12172
+ sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
1208912173
void (*xLog)(void*,int,const char*); /* Function for logging */
1209012174
void *pLogArg; /* First argument to xLog() */
12091
- int bLocaltimeFault; /* True to fail localtime() calls */
1209212175
#ifdef SQLITE_ENABLE_SQLLOG
1209312176
void(*xSqllog)(void*,sqlite3*,const char*, int);
1209412177
void *pSqllogArg;
1209512178
#endif
1209612179
#ifdef SQLITE_VDBE_COVERAGE
@@ -12098,10 +12181,14 @@
1209812181
** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
1209912182
*/
1210012183
void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx); /* Callback */
1210112184
void *pVdbeBranchArg; /* 1st argument */
1210212185
#endif
12186
+#ifndef SQLITE_OMIT_BUILTIN_TEST
12187
+ int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
12188
+#endif
12189
+ int bLocaltimeFault; /* True to fail localtime() calls */
1210312190
};
1210412191
1210512192
/*
1210612193
** This macro is used inside of assert() statements to indicate that
1210712194
** the assert is only valid on a well-formed database. Instead of:
@@ -12398,10 +12485,16 @@
1239812485
SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
1239912486
SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
1240012487
sqlite3_vfs**,char**,char **);
1240112488
SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
1240212489
SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
12490
+
12491
+#ifdef SQLITE_OMIT_BUILTIN_TEST
12492
+# define sqlite3FaultSim(X) SQLITE_OK
12493
+#else
12494
+SQLITE_PRIVATE int sqlite3FaultSim(int);
12495
+#endif
1240312496
1240412497
SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
1240512498
SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
1240612499
SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
1240712500
SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
@@ -12466,10 +12559,11 @@
1246612559
SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
1246712560
SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
1246812561
SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
1246912562
SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
1247012563
SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
12564
+SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
1247112565
SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
1247212566
SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
1247312567
SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
1247412568
SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
1247512569
SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
@@ -13203,19 +13297,26 @@
1320313297
0, /* isInit */
1320413298
0, /* inProgress */
1320513299
0, /* isMutexInit */
1320613300
0, /* isMallocInit */
1320713301
0, /* isPCacheInit */
13208
- 0, /* pInitMutex */
1320913302
0, /* nRefInitMutex */
13303
+ 0, /* pInitMutex */
1321013304
0, /* xLog */
1321113305
0, /* pLogArg */
13212
- 0, /* bLocaltimeFault */
1321313306
#ifdef SQLITE_ENABLE_SQLLOG
1321413307
0, /* xSqllog */
13215
- 0 /* pSqllogArg */
13308
+ 0, /* pSqllogArg */
1321613309
#endif
13310
+#ifdef SQLITE_VDBE_COVERAGE
13311
+ 0, /* xVdbeBranch */
13312
+ 0, /* pVbeBranchArg */
13313
+#endif
13314
+#ifndef SQLITE_OMIT_BUILTIN_TEST
13315
+ 0, /* xTestCallback */
13316
+#endif
13317
+ 0 /* bLocaltimeFault */
1321713318
};
1321813319
1321913320
/*
1322013321
** Hash table for global functions - functions common to all
1322113322
** database connections. After initialization, this table is
@@ -18934,10 +19035,88 @@
1893419035
**
1893519036
*************************************************************************
1893619037
** This file contains the C functions that implement mutexes for win32
1893719038
*/
1893819039
19040
+#if SQLITE_OS_WIN
19041
+/*
19042
+** Include the header file for the Windows VFS.
19043
+*/
19044
+/************** Include os_win.h in the middle of mutex_w32.c ****************/
19045
+/************** Begin file os_win.h ******************************************/
19046
+/*
19047
+** 2013 November 25
19048
+**
19049
+** The author disclaims copyright to this source code. In place of
19050
+** a legal notice, here is a blessing:
19051
+**
19052
+** May you do good and not evil.
19053
+** May you find forgiveness for yourself and forgive others.
19054
+** May you share freely, never taking more than you give.
19055
+**
19056
+******************************************************************************
19057
+**
19058
+** This file contains code that is specific to Windows.
19059
+*/
19060
+#ifndef _OS_WIN_H_
19061
+#define _OS_WIN_H_
19062
+
19063
+/*
19064
+** Include the primary Windows SDK header file.
19065
+*/
19066
+#include "windows.h"
19067
+
19068
+#ifdef __CYGWIN__
19069
+# include <sys/cygwin.h>
19070
+# include <errno.h> /* amalgamator: dontcache */
19071
+#endif
19072
+
19073
+/*
19074
+** Determine if we are dealing with Windows NT.
19075
+**
19076
+** We ought to be able to determine if we are compiling for Windows 9x or
19077
+** Windows NT using the _WIN32_WINNT macro as follows:
19078
+**
19079
+** #if defined(_WIN32_WINNT)
19080
+** # define SQLITE_OS_WINNT 1
19081
+** #else
19082
+** # define SQLITE_OS_WINNT 0
19083
+** #endif
19084
+**
19085
+** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
19086
+** it ought to, so the above test does not work. We'll just assume that
19087
+** everything is Windows NT unless the programmer explicitly says otherwise
19088
+** by setting SQLITE_OS_WINNT to 0.
19089
+*/
19090
+#if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
19091
+# define SQLITE_OS_WINNT 1
19092
+#endif
19093
+
19094
+/*
19095
+** Determine if we are dealing with Windows CE - which has a much reduced
19096
+** API.
19097
+*/
19098
+#if defined(_WIN32_WCE)
19099
+# define SQLITE_OS_WINCE 1
19100
+#else
19101
+# define SQLITE_OS_WINCE 0
19102
+#endif
19103
+
19104
+/*
19105
+** Determine if we are dealing with WinRT, which provides only a subset of
19106
+** the full Win32 API.
19107
+*/
19108
+#if !defined(SQLITE_OS_WINRT)
19109
+# define SQLITE_OS_WINRT 0
19110
+#endif
19111
+
19112
+#endif /* _OS_WIN_H_ */
19113
+
19114
+/************** End of os_win.h **********************************************/
19115
+/************** Continuing where we left off in mutex_w32.c ******************/
19116
+#endif
19117
+
1893919118
/*
1894019119
** The code in this file is only used if we are compiling multithreaded
1894119120
** on a win32 system.
1894219121
*/
1894319122
#ifdef SQLITE_MUTEX_W32
@@ -21789,10 +21968,28 @@
2178921968
static unsigned dummy = 0;
2179021969
dummy += (unsigned)x;
2179121970
}
2179221971
#endif
2179321972
21973
+/*
21974
+** Give a callback to the test harness that can be used to simulate faults
21975
+** in places where it is difficult or expensive to do so purely by means
21976
+** of inputs.
21977
+**
21978
+** The intent of the integer argument is to let the fault simulator know
21979
+** which of multiple sqlite3FaultSim() calls has been hit.
21980
+**
21981
+** Return whatever integer value the test callback returns, or return
21982
+** SQLITE_OK if no test callback is installed.
21983
+*/
21984
+#ifndef SQLITE_OMIT_BUILTIN_TEST
21985
+SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
21986
+ int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
21987
+ return xCallback ? xCallback(iTest) : SQLITE_OK;
21988
+}
21989
+#endif
21990
+
2179421991
#ifndef SQLITE_OMIT_FLOATING_POINT
2179521992
/*
2179621993
** Return true if the floating point value is Not a Number (NaN).
2179721994
**
2179821995
** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
@@ -23004,12 +23201,12 @@
2300423201
return b+x[b-a];
2300523202
}
2300623203
}
2300723204
2300823205
/*
23009
-** Convert an integer into a LogEst. In other words, compute a
23010
-** good approximatation for 10*log2(x).
23206
+** Convert an integer into a LogEst. In other words, compute an
23207
+** approximation for 10*log2(x).
2301123208
*/
2301223209
SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
2301323210
static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
2301423211
LogEst y = 40;
2301523212
if( x<8 ){
@@ -31226,15 +31423,10 @@
3122631423
**
3122731424
** This file contains code that is specific to Windows.
3122831425
*/
3122931426
#if SQLITE_OS_WIN /* This file is used for Windows only */
3123031427
31231
-#ifdef __CYGWIN__
31232
-# include <sys/cygwin.h>
31233
-# include <errno.h> /* amalgamator: keep */
31234
-#endif
31235
-
3123631428
/*
3123731429
** Include code that is common to all os_*.c files
3123831430
*/
3123931431
/************** Include os_common.h in the middle of os_win.c ****************/
3124031432
/************** Begin file os_common.h ***************************************/
@@ -31443,10 +31635,14 @@
3144331635
3144431636
#endif /* !defined(_OS_COMMON_H_) */
3144531637
3144631638
/************** End of os_common.h *******************************************/
3144731639
/************** Continuing where we left off in os_win.c *********************/
31640
+
31641
+/*
31642
+** Include the header file for the Windows VFS.
31643
+*/
3144831644
3144931645
/*
3145031646
** Compiling and using WAL mode requires several APIs that are only
3145131647
** available in Windows platforms based on the NT kernel.
3145231648
*/
@@ -33255,10 +33451,36 @@
3325533451
# define SQLITE_WIN32_IOERR_RETRY_DELAY 25
3325633452
#endif
3325733453
static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
3325833454
static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
3325933455
33456
+/*
33457
+** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
33458
+** error code obtained via GetLastError() is eligible to be retried. It
33459
+** must accept the error code DWORD as its only argument and should return
33460
+** non-zero if the error code is transient in nature and the operation
33461
+** responsible for generating the original error might succeed upon being
33462
+** retried. The argument to this macro should be a variable.
33463
+**
33464
+** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
33465
+** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
33466
+** returns zero. The "winIoerrCanRetry2" macro is completely optional and
33467
+** may be used to include additional error codes in the set that should
33468
+** result in the failing I/O operation being retried by the caller. If
33469
+** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
33470
+** identical to those of the "winIoerrCanRetry1" macro.
33471
+*/
33472
+#if !defined(winIoerrCanRetry1)
33473
+#define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
33474
+ ((a)==ERROR_SHARING_VIOLATION) || \
33475
+ ((a)==ERROR_LOCK_VIOLATION) || \
33476
+ ((a)==ERROR_DEV_NOT_EXIST) || \
33477
+ ((a)==ERROR_NETNAME_DELETED) || \
33478
+ ((a)==ERROR_SEM_TIMEOUT) || \
33479
+ ((a)==ERROR_NETWORK_UNREACHABLE))
33480
+#endif
33481
+
3326033482
/*
3326133483
** If a ReadFile() or WriteFile() error occurs, invoke this routine
3326233484
** to see if it should be retried. Return TRUE to retry. Return FALSE
3326333485
** to give up with an error.
3326433486
*/
@@ -33268,17 +33490,22 @@
3326833490
if( pError ){
3326933491
*pError = e;
3327033492
}
3327133493
return 0;
3327233494
}
33273
- if( e==ERROR_ACCESS_DENIED ||
33274
- e==ERROR_LOCK_VIOLATION ||
33275
- e==ERROR_SHARING_VIOLATION ){
33495
+ if( winIoerrCanRetry1(e) ){
33496
+ sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
33497
+ ++*pnRetry;
33498
+ return 1;
33499
+ }
33500
+#if defined(winIoerrCanRetry2)
33501
+ else if( winIoerrCanRetry2(e) ){
3327633502
sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
3327733503
++*pnRetry;
3327833504
return 1;
3327933505
}
33506
+#endif
3328033507
if( pError ){
3328133508
*pError = e;
3328233509
}
3328333510
return 0;
3328433511
}
@@ -34213,11 +34440,11 @@
3421334440
#endif
3421434441
if( res == 0 ){
3421534442
pFile->lastErrno = osGetLastError();
3421634443
/* No need to log a failure to lock */
3421734444
}
34218
- OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
34445
+ OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
3421934446
return res;
3422034447
}
3422134448
3422234449
/*
3422334450
** Undo a readlock
@@ -34237,11 +34464,11 @@
3423734464
if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
3423834465
pFile->lastErrno = lastErrno;
3423934466
winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
3424034467
"winUnlockReadLock", pFile->zPath);
3424134468
}
34242
- OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
34469
+ OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
3424334470
return res;
3424434471
}
3424534472
3424634473
/*
3424734474
** Lock the file with the lock specified by parameter locktype - one
@@ -34312,12 +34539,12 @@
3431234539
** around problems caused by indexing and/or anti-virus software on
3431334540
** Windows systems.
3431434541
** If you are using this code as a model for alternative VFSes, do not
3431534542
** copy this retry logic. It is a hack intended for Windows only.
3431634543
*/
34317
- OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
34318
- pFile->h, cnt, sqlite3ErrName(res)));
34544
+ OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
34545
+ pFile->h, cnt, res));
3431934546
if( cnt ) sqlite3_win32_sleep(1);
3432034547
}
3432134548
gotPendingLock = res;
3432234549
if( !res ){
3432334550
lastErrno = osGetLastError();
@@ -34398,29 +34625,29 @@
3439834625
** This routine checks if there is a RESERVED lock held on the specified
3439934626
** file by this or any other process. If such a lock is held, return
3440034627
** non-zero, otherwise zero.
3440134628
*/
3440234629
static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
34403
- int rc;
34630
+ int res;
3440434631
winFile *pFile = (winFile*)id;
3440534632
3440634633
SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
3440734634
OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
3440834635
3440934636
assert( id!=0 );
3441034637
if( pFile->locktype>=RESERVED_LOCK ){
34411
- rc = 1;
34412
- OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
34638
+ res = 1;
34639
+ OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
3441334640
}else{
34414
- rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
34415
- if( rc ){
34641
+ res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
34642
+ if( res ){
3441634643
winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
3441734644
}
34418
- rc = !rc;
34419
- OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
34645
+ res = !res;
34646
+ OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
3442034647
}
34421
- *pResOut = rc;
34648
+ *pResOut = res;
3442234649
OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
3442334650
pFile->h, pResOut, *pResOut));
3442434651
return SQLITE_OK;
3442534652
}
3442634653
@@ -40231,11 +40458,12 @@
4023140458
u8 noSync; /* Do not sync the journal if true */
4023240459
u8 fullSync; /* Do extra syncs of the journal for robustness */
4023340460
u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
4023440461
u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
4023540462
u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
40236
- u8 tempFile; /* zFilename is a temporary file */
40463
+ u8 tempFile; /* zFilename is a temporary or immutable file */
40464
+ u8 noLock; /* Do not lock (except in WAL mode) */
4023740465
u8 readOnly; /* True for a read-only database */
4023840466
u8 memDb; /* True to inhibit all file I/O */
4023940467
4024040468
/**************************************************************************
4024140469
** The following block contains those class members that change during
@@ -40696,11 +40924,11 @@
4069640924
assert( !pPager->exclusiveMode || pPager->eLock==eLock );
4069740925
assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
4069840926
assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
4069940927
if( isOpen(pPager->fd) ){
4070040928
assert( pPager->eLock>=eLock );
40701
- rc = sqlite3OsUnlock(pPager->fd, eLock);
40929
+ rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
4070240930
if( pPager->eLock!=UNKNOWN_LOCK ){
4070340931
pPager->eLock = (u8)eLock;
4070440932
}
4070540933
IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
4070640934
}
@@ -40720,11 +40948,11 @@
4072040948
static int pagerLockDb(Pager *pPager, int eLock){
4072140949
int rc = SQLITE_OK;
4072240950
4072340951
assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
4072440952
if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
40725
- rc = sqlite3OsLock(pPager->fd, eLock);
40953
+ rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
4072640954
if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
4072740955
pPager->eLock = (u8)eLock;
4072840956
IOTRACE(("LOCK %p %d\n", pPager, eLock))
4072940957
}
4073040958
}
@@ -44279,47 +44507,59 @@
4427944507
**
4428044508
** + SQLITE_DEFAULT_PAGE_SIZE,
4428144509
** + The value returned by sqlite3OsSectorSize()
4428244510
** + The largest page size that can be written atomically.
4428344511
*/
44284
- if( rc==SQLITE_OK && !readOnly ){
44285
- setSectorSize(pPager);
44286
- assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
44287
- if( szPageDflt<pPager->sectorSize ){
44288
- if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
44289
- szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
44290
- }else{
44291
- szPageDflt = (u32)pPager->sectorSize;
44292
- }
44293
- }
44512
+ if( rc==SQLITE_OK ){
44513
+ int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
44514
+ if( !readOnly ){
44515
+ setSectorSize(pPager);
44516
+ assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
44517
+ if( szPageDflt<pPager->sectorSize ){
44518
+ if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
44519
+ szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
44520
+ }else{
44521
+ szPageDflt = (u32)pPager->sectorSize;
44522
+ }
44523
+ }
4429444524
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
44295
- {
44296
- int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
44297
- int ii;
44298
- assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
44299
- assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
44300
- assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
44301
- for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
44302
- if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
44303
- szPageDflt = ii;
44525
+ {
44526
+ int ii;
44527
+ assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
44528
+ assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
44529
+ assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
44530
+ for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
44531
+ if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
44532
+ szPageDflt = ii;
44533
+ }
4430444534
}
4430544535
}
44536
+#endif
4430644537
}
44307
-#endif
44538
+ pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
44539
+ if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
44540
+ || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
44541
+ vfsFlags |= SQLITE_OPEN_READONLY;
44542
+ goto act_like_temp_file;
44543
+ }
4430844544
}
4430944545
}else{
4431044546
/* If a temporary file is requested, it is not opened immediately.
4431144547
** In this case we accept the default page size and delay actually
4431244548
** opening the file until the first call to OsWrite().
4431344549
**
4431444550
** This branch is also run for an in-memory database. An in-memory
4431544551
** database is the same as a temp-file that is never written out to
4431644552
** disk and uses an in-memory rollback journal.
44553
+ **
44554
+ ** This branch also runs for files marked as immutable.
4431744555
*/
44556
+act_like_temp_file:
4431844557
tempFile = 1;
44319
- pPager->eState = PAGER_READER;
44320
- pPager->eLock = EXCLUSIVE_LOCK;
44558
+ pPager->eState = PAGER_READER; /* Pretend we already have a lock */
44559
+ pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE locking mode */
44560
+ pPager->noLock = 1; /* Do no locking */
4432144561
readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
4432244562
}
4432344563
4432444564
/* The following call to PagerSetPagesize() serves to set the value of
4432544565
** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
@@ -44356,13 +44596,10 @@
4435644596
/* pPager->stmtSize = 0; */
4435744597
/* pPager->stmtJSize = 0; */
4435844598
/* pPager->nPage = 0; */
4435944599
pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
4436044600
/* pPager->state = PAGER_UNLOCK; */
44361
-#if 0
44362
- assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
44363
-#endif
4436444601
/* pPager->errMask = 0; */
4436544602
pPager->tempFile = (u8)tempFile;
4436644603
assert( tempFile==PAGER_LOCKINGMODE_NORMAL
4436744604
|| tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
4436844605
assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
@@ -55106,16 +55343,10 @@
5510655343
assert( pCur->eState==CURSOR_VALID );
5510755344
assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
5510855345
assert( cursorHoldsMutex(pCur) );
5510955346
assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
5511055347
assert( pCur->info.nSize>0 );
55111
-#if 0
55112
- if( pCur->info.nSize==0 ){
55113
- btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
55114
- &pCur->info);
55115
- }
55116
-#endif
5511755348
*pAmt = pCur->info.nLocal;
5511855349
return (void*)(pCur->info.pCell + pCur->info.nHeader);
5511955350
}
5512055351
5512155352
@@ -59414,10 +59645,17 @@
5941459645
*/
5941559646
SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
5941659647
assert( mask==BTREE_BULKLOAD || mask==0 );
5941759648
pCsr->hints = mask;
5941859649
}
59650
+
59651
+/*
59652
+** Return true if the given Btree is read-only.
59653
+*/
59654
+SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
59655
+ return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
59656
+}
5941959657
5942059658
/************** End of btree.c ***********************************************/
5942159659
/************** Begin file backup.c ******************************************/
5942259660
/*
5942359661
** 2009 January 28
@@ -70686,11 +70924,11 @@
7068670924
**
7068770925
** Reposition cursor P1 so that it points to the smallest entry that
7068870926
** is greater than or equal to the key value. If there are no records
7068970927
** greater than or equal to the key and P2 is not zero, then jump to P2.
7069070928
**
70691
-** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
70929
+** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
7069270930
*/
7069370931
/* Opcode: SeekGt P1 P2 P3 P4 *
7069470932
** Synopsis: key=r[P3@P4]
7069570933
**
7069670934
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70700,11 +70938,11 @@
7070070938
**
7070170939
** Reposition cursor P1 so that it points to the smallest entry that
7070270940
** is greater than the key value. If there are no records greater than
7070370941
** the key and P2 is not zero, then jump to P2.
7070470942
**
70705
-** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
70943
+** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
7070670944
*/
7070770945
/* Opcode: SeekLt P1 P2 P3 P4 *
7070870946
** Synopsis: key=r[P3@P4]
7070970947
**
7071070948
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70714,11 +70952,11 @@
7071470952
**
7071570953
** Reposition cursor P1 so that it points to the largest entry that
7071670954
** is less than the key value. If there are no records less than
7071770955
** the key and P2 is not zero, then jump to P2.
7071870956
**
70719
-** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
70957
+** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
7072070958
*/
7072170959
/* Opcode: SeekLe P1 P2 P3 P4 *
7072270960
** Synopsis: key=r[P3@P4]
7072370961
**
7072470962
** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70728,11 +70966,11 @@
7072870966
**
7072970967
** Reposition cursor P1 so that it points to the largest entry that
7073070968
** is less than or equal to the key value. If there are no records
7073170969
** less than or equal to the key and P2 is not zero, then jump to P2.
7073270970
**
70733
-** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
70971
+** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
7073470972
*/
7073570973
case OP_SeekLT: /* jump, in3 */
7073670974
case OP_SeekLE: /* jump, in3 */
7073770975
case OP_SeekGE: /* jump, in3 */
7073870976
case OP_SeekGT: { /* jump, in3 */
@@ -71454,10 +71692,11 @@
7145471692
7145571693
pOut = &aMem[pOp->p2];
7145671694
pC = p->apCsr[pOp->p1];
7145771695
assert( isSorter(pC) );
7145871696
rc = sqlite3VdbeSorterRowkey(pC, pOut);
71697
+ assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
7145971698
break;
7146071699
}
7146171700
7146271701
/* Opcode: RowData P1 P2 * * *
7146371702
** Synopsis: r[P2]=data
@@ -73502,11 +73741,11 @@
7350273741
#ifdef SQLITE_USE_FCNTL_TRACE
7350373742
zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
7350473743
if( zTrace ){
7350573744
int i;
7350673745
for(i=0; i<db->nDb; i++){
73507
- if( MASKBIT(i) & p->btreeMask)==0 ) continue;
73746
+ if( (MASKBIT(i) & p->btreeMask)==0 ) continue;
7350873747
sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
7350973748
}
7351073749
}
7351173750
#endif /* SQLITE_USE_FCNTL_TRACE */
7351273751
#ifdef SQLITE_DEBUG
@@ -73545,12 +73784,12 @@
7354573784
*****************************************************************************/
7354673785
}
7354773786
7354873787
#ifdef VDBE_PROFILE
7354973788
{
73550
- u64 elapsed = sqlite3Hwtime() - start;
73551
- pOp->cycles += elapsed;
73789
+ u64 endTime = sqlite3Hwtime();
73790
+ if( endTime>start ) pOp->cycles += endTime - start;
7355273791
pOp->cnt++;
7355373792
}
7355473793
#endif
7355573794
7355673795
/* The following code adds nothing to the actual functionality
@@ -74457,11 +74696,10 @@
7445774696
nRead = (int)(pSorter->iWriteOff - iStart);
7445874697
}
7445974698
rc = sqlite3OsRead(
7446074699
pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
7446174700
);
74462
- assert( rc!=SQLITE_IOERR_SHORT_READ );
7446374701
}
7446474702
7446574703
if( rc==SQLITE_OK ){
7446674704
u64 nByte; /* Size of PMA in bytes */
7446774705
pIter->iEof = pSorter->iWriteOff;
@@ -83420,11 +83658,11 @@
8342083658
nCol = pIdx->nKeyCol;
8342183659
aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
8342283660
if( aGotoChng==0 ) continue;
8342383661
8342483662
/* Populate the register containing the index name. */
83425
- if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
83663
+ if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
8342683664
zIdxName = pTab->zName;
8342783665
}else{
8342883666
zIdxName = pIdx->zName;
8342983667
}
8343083668
sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
@@ -83789,10 +84027,11 @@
8378984027
*/
8379084028
static void decodeIntArray(
8379184029
char *zIntArray, /* String containing int array to decode */
8379284030
int nOut, /* Number of slots in aOut[] */
8379384031
tRowcnt *aOut, /* Store integers here */
84032
+ LogEst *aLog, /* Or, if aOut==0, here */
8379484033
Index *pIndex /* Handle extra flags for this index, if not NULL */
8379584034
){
8379684035
char *z = zIntArray;
8379784036
int c;
8379884037
int i;
@@ -83807,11 +84046,21 @@
8380784046
v = 0;
8380884047
while( (c=z[0])>='0' && c<='9' ){
8380984048
v = v*10 + c - '0';
8381084049
z++;
8381184050
}
83812
- aOut[i] = v;
84051
+#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
84052
+ if( aOut ){
84053
+ aOut[i] = v;
84054
+ }else
84055
+#else
84056
+ assert( aOut==0 );
84057
+ UNUSED_PARAMETER(aOut);
84058
+#endif
84059
+ {
84060
+ aLog[i] = sqlite3LogEst(v);
84061
+ }
8381384062
if( *z==' ' ) z++;
8381484063
}
8381584064
#ifndef SQLITE_ENABLE_STAT3_OR_STAT4
8381684065
assert( pIndex!=0 );
8381784066
#else
@@ -83863,16 +84112,16 @@
8386384112
pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
8386484113
}
8386584114
z = argv[2];
8386684115
8386784116
if( pIndex ){
83868
- decodeIntArray((char*)z, pIndex->nKeyCol+1, pIndex->aiRowEst, pIndex);
83869
- if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
84117
+ decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex);
84118
+ if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
8387084119
}else{
8387184120
Index fakeIdx;
8387284121
fakeIdx.szIdxRow = pTable->szTabRow;
83873
- decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
84122
+ decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
8387484123
pTable->szTabRow = fakeIdx.szIdxRow;
8387584124
}
8387684125
8387784126
return 0;
8387884127
}
@@ -84060,13 +84309,13 @@
8406084309
if( pIdx!=pPrevIdx ){
8406184310
initAvgEq(pPrevIdx);
8406284311
pPrevIdx = pIdx;
8406384312
}
8406484313
pSample = &pIdx->aSample[pIdx->nSample];
84065
- decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
84066
- decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
84067
- decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
84314
+ decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
84315
+ decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
84316
+ decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
8406884317
8406984318
/* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
8407084319
** This is in case the sample record is corrupted. In that case, the
8407184320
** sqlite3VdbeRecordCompare() may read up to two varints past the
8407284321
** end of the allocated buffer before it realizes it is dealing with
@@ -85776,11 +86025,11 @@
8577686025
/*
8577786026
** Return the PRIMARY KEY index of a table
8577886027
*/
8577986028
SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
8578086029
Index *p;
85781
- for(p=pTab->pIndex; p && p->autoIndex!=2; p=p->pNext){}
86030
+ for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
8578286031
return p;
8578386032
}
8578486033
8578586034
/*
8578686035
** Return the column of index pIdx that corresponds to table
@@ -85924,11 +86173,11 @@
8592486173
}
8592586174
pTable->zName = zName;
8592686175
pTable->iPKey = -1;
8592786176
pTable->pSchema = db->aDb[iDb].pSchema;
8592886177
pTable->nRef = 1;
85929
- pTable->nRowEst = 1048576;
86178
+ pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
8593086179
assert( pParse->pNewTable==0 );
8593186180
pParse->pNewTable = pTable;
8593286181
8593386182
/* If this is the magic sqlite_sequence table used by autoincrement,
8593486183
** then record a pointer to this table in the main database structure
@@ -86305,11 +86554,11 @@
8630586554
Index *p;
8630686555
if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
8630786556
p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
8630886557
0, sortOrder, 0);
8630986558
if( p ){
86310
- p->autoIndex = 2;
86559
+ p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
8631186560
if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
8631286561
}
8631386562
pList = 0;
8631486563
}
8631586564
@@ -86325,11 +86574,14 @@
8632586574
Parse *pParse, /* Parsing context */
8632686575
Expr *pCheckExpr /* The check expression */
8632786576
){
8632886577
#ifndef SQLITE_OMIT_CHECK
8632986578
Table *pTab = pParse->pNewTable;
86330
- if( pTab && !IN_DECLARE_VTAB ){
86579
+ sqlite3 *db = pParse->db;
86580
+ if( pTab && !IN_DECLARE_VTAB
86581
+ && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
86582
+ ){
8633186583
pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
8633286584
if( pParse->constraintName.n ){
8633386585
sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
8633486586
}
8633586587
}else
@@ -86677,11 +86929,11 @@
8667786929
pTab->aCol[pTab->iPKey].zName);
8667886930
pList->a[0].sortOrder = pParse->iPkSortOrder;
8667986931
assert( pParse->pNewTable==pTab );
8668086932
pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
8668186933
if( pPk==0 ) return;
86682
- pPk->autoIndex = 2;
86934
+ pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
8668386935
pTab->iPKey = -1;
8668486936
}else{
8668586937
pPk = sqlite3PrimaryKeyIndex(pTab);
8668686938
}
8668786939
pPk->isCovering = 1;
@@ -86700,11 +86952,11 @@
8670086952
/* Update the in-memory representation of all UNIQUE indices by converting
8670186953
** the final rowid column into one or more columns of the PRIMARY KEY.
8670286954
*/
8670386955
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
8670486956
int n;
86705
- if( pIdx->autoIndex==2 ) continue;
86957
+ if( IsPrimaryKeyIndex(pIdx) ) continue;
8670686958
for(i=n=0; i<nPk; i++){
8670786959
if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
8670886960
}
8670986961
if( n==0 ){
8671086962
/* This index is a superset of the primary key */
@@ -87749,19 +88001,19 @@
8774988001
Index *p; /* Allocated index object */
8775088002
int nByte; /* Bytes of space for Index object + arrays */
8775188003
8775288004
nByte = ROUND8(sizeof(Index)) + /* Index structure */
8775388005
ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
87754
- ROUND8(sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
88006
+ ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
8775588007
sizeof(i16)*nCol + /* Index.aiColumn */
8775688008
sizeof(u8)*nCol); /* Index.aSortOrder */
8775788009
p = sqlite3DbMallocZero(db, nByte + nExtra);
8775888010
if( p ){
8775988011
char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
87760
- p->azColl = (char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
87761
- p->aiRowEst = (tRowcnt*)pExtra; pExtra += sizeof(tRowcnt)*(nCol+1);
87762
- p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
88012
+ p->azColl = (char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
88013
+ p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
88014
+ p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
8776388015
p->aSortOrder = (u8*)pExtra;
8776488016
p->nColumn = nCol;
8776588017
p->nKeyCol = nCol - 1;
8776688018
*ppExtra = ((char*)p) + nByte;
8776788019
}
@@ -87780,11 +88032,11 @@
8778088032
** is a primary key or unique-constraint on the most recent column added
8778188033
** to the table currently under construction.
8778288034
**
8778388035
** If the index is created successfully, return a pointer to the new Index
8778488036
** structure. This is used by sqlite3AddPrimaryKey() to mark the index
87785
-** as the tables primary key (Index.autoIndex==2).
88037
+** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY)
8778688038
*/
8778788039
SQLITE_PRIVATE Index *sqlite3CreateIndex(
8778888040
Parse *pParse, /* All information about this parse */
8778988041
Token *pName1, /* First part of index name. May be NULL */
8779088042
Token *pName2, /* Second part of index name. May be NULL */
@@ -87987,19 +88239,19 @@
8798788239
pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
8798888240
nName + nExtra + 1, &zExtra);
8798988241
if( db->mallocFailed ){
8799088242
goto exit_create_index;
8799188243
}
87992
- assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
88244
+ assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
8799388245
assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
8799488246
pIndex->zName = zExtra;
8799588247
zExtra += nName + 1;
8799688248
memcpy(pIndex->zName, zName, nName+1);
8799788249
pIndex->pTable = pTab;
8799888250
pIndex->onError = (u8)onError;
8799988251
pIndex->uniqNotNull = onError!=OE_None;
88000
- pIndex->autoIndex = (u8)(pName==0);
88252
+ pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE;
8800188253
pIndex->pSchema = db->aDb[iDb].pSchema;
8800288254
pIndex->nKeyCol = pList->nExpr;
8800388255
if( pPIWhere ){
8800488256
sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
8800588257
pIndex->pPartIdxWhere = pPIWhere;
@@ -88107,11 +88359,11 @@
8810788359
*/
8810888360
Index *pIdx;
8810988361
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
8811088362
int k;
8811188363
assert( pIdx->onError!=OE_None );
88112
- assert( pIdx->autoIndex );
88364
+ assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
8811388365
assert( pIndex->onError!=OE_None );
8811488366
8811588367
if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
8811688368
for(k=0; k<pIdx->nKeyCol; k++){
8811788369
const char *z1;
@@ -88268,11 +88520,11 @@
8826888520
**
8826988521
** aiRowEst[0] is suppose to contain the number of elements in the index.
8827088522
** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
8827188523
** number of rows in the table that match any particular value of the
8827288524
** first column of the index. aiRowEst[2] is an estimate of the number
88273
-** of rows that match any particular combiniation of the first 2 columns
88525
+** of rows that match any particular combination of the first 2 columns
8827488526
** of the index. And so forth. It must always be the case that
8827588527
*
8827688528
** aiRowEst[N]<=aiRowEst[N-1]
8827788529
** aiRowEst[N]>=1
8827888530
**
@@ -88279,24 +88531,31 @@
8827988531
** Apart from that, we have little to go on besides intuition as to
8828088532
** how aiRowEst[] should be initialized. The numbers generated here
8828188533
** are based on typical values found in actual indices.
8828288534
*/
8828388535
SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
88284
- tRowcnt *a = pIdx->aiRowEst;
88536
+ /* 10, 9, 8, 7, 6 */
88537
+ LogEst aVal[] = { 33, 32, 30, 28, 26 };
88538
+ LogEst *a = pIdx->aiRowLogEst;
88539
+ int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
8828588540
int i;
88286
- tRowcnt n;
88287
- assert( a!=0 );
88288
- a[0] = pIdx->pTable->nRowEst;
88289
- if( a[0]<10 ) a[0] = 10;
88290
- n = 10;
88291
- for(i=1; i<=pIdx->nKeyCol; i++){
88292
- a[i] = n;
88293
- if( n>5 ) n--;
88294
- }
88295
- if( pIdx->onError!=OE_None ){
88296
- a[pIdx->nKeyCol] = 1;
88297
- }
88541
+
88542
+ /* Set the first entry (number of rows in the index) to the estimated
88543
+ ** number of rows in the table. Or 10, if the estimated number of rows
88544
+ ** in the table is less than that. */
88545
+ a[0] = pIdx->pTable->nRowLogEst;
88546
+ if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) );
88547
+
88548
+ /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
88549
+ ** 6 and each subsequent value (if any) is 5. */
88550
+ memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
88551
+ for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
88552
+ a[i] = 23; assert( 23==sqlite3LogEst(5) );
88553
+ }
88554
+
88555
+ assert( 0==sqlite3LogEst(1) );
88556
+ if( pIdx->onError!=OE_None ) a[pIdx->nKeyCol] = 0;
8829888557
}
8829988558
8830088559
/*
8830188560
** This routine will drop an existing named index. This routine
8830288561
** implements the DROP INDEX statement.
@@ -88323,11 +88582,11 @@
8832388582
sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
8832488583
}
8832588584
pParse->checkSchema = 1;
8832688585
goto exit_drop_index;
8832788586
}
88328
- if( pIndex->autoIndex ){
88587
+ if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
8832988588
sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
8833088589
"or PRIMARY KEY constraint cannot be dropped", 0);
8833188590
goto exit_drop_index;
8833288591
}
8833388592
iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
@@ -88982,11 +89241,12 @@
8898289241
sqlite3StrAccumAppend(&errMsg, ".", 1);
8898389242
sqlite3StrAccumAppendAll(&errMsg, zCol);
8898489243
}
8898589244
zErr = sqlite3StrAccumFinish(&errMsg);
8898689245
sqlite3HaltConstraint(pParse,
88987
- (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
89246
+ IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
89247
+ : SQLITE_CONSTRAINT_UNIQUE,
8898889248
onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
8898989249
}
8899089250
8899189251
8899289252
/*
@@ -92116,11 +92376,11 @@
9211692376
}
9211792377
if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
9211892378
}
9211992379
zVal = (char*)sqlite3_value_text(argv[0]);
9212092380
nVal = sqlite3_value_bytes(argv[0]);
92121
- if( nVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
92381
+ if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
9212292382
}
9212392383
}
9212492384
static void groupConcatFinalize(sqlite3_context *context){
9212592385
StrAccum *pAccum;
9212692386
pAccum = sqlite3_aggregate_context(context, 0);
@@ -92560,12 +92820,12 @@
9256092820
** column of pFKey, then this index is a winner. */
9256192821
9256292822
if( zKey==0 ){
9256392823
/* If zKey is NULL, then this foreign key is implicitly mapped to
9256492824
** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
92565
- ** identified by the test (Index.autoIndex==2). */
92566
- if( pIdx->autoIndex==2 ){
92825
+ ** identified by the test. */
92826
+ if( IsPrimaryKeyIndex(pIdx) ){
9256792827
if( aiCol ){
9256892828
int i;
9256992829
for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
9257092830
}
9257192831
break;
@@ -94306,10 +94566,11 @@
9430694566
}
9430794567
}
9430894568
if( j>=pTab->nCol ){
9430994569
if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
9431094570
ipkColumn = i;
94571
+ bIdListInOrder = 0;
9431194572
}else{
9431294573
sqlite3ErrorMsg(pParse, "table %S has no column named %s",
9431394574
pTabList, 0, pColumn->a[i].zName);
9431494575
pParse->checkSchema = 1;
9431594576
goto insert_cleanup;
@@ -95154,11 +95415,11 @@
9515495415
** For a UNIQUE index, only conflict if the PRIMARY KEY values
9515595416
** of the matched index row are different from the original PRIMARY
9515695417
** KEY values of this row before the update. */
9515795418
int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
9515895419
int op = OP_Ne;
95159
- int regCmp = (pIdx->autoIndex==2 ? regIdx : regR);
95420
+ int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
9516095421
9516195422
for(i=0; i<pPk->nKeyCol; i++){
9516295423
char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
9516395424
x = pPk->aiColumn[i];
9516495425
if( i==(pPk->nKeyCol-1) ){
@@ -95255,11 +95516,11 @@
9525595516
VdbeCoverage(v);
9525695517
}
9525795518
sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
9525895519
pik_flags = 0;
9525995520
if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
95260
- if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
95521
+ if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
9526195522
assert( pParse->nested==0 );
9526295523
pik_flags |= OPFLAG_NCHANGE;
9526395524
}
9526495525
if( pik_flags ) sqlite3VdbeChangeP5(v, pik_flags);
9526595526
}
@@ -95341,11 +95602,11 @@
9534195602
}
9534295603
if( piIdxCur ) *piIdxCur = iBase;
9534395604
for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
9534495605
int iIdxCur = iBase++;
9534595606
assert( pIdx->pSchema==pTab->pSchema );
95346
- if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
95607
+ if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){
9534795608
*piDataCur = iIdxCur;
9534895609
}
9534995610
if( aToOpen==0 || aToOpen[i+1] ){
9535095611
sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
9535195612
sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
@@ -95557,18 +95818,27 @@
9555795818
}
9555895819
if( pDest->iPKey!=pSrc->iPKey ){
9555995820
return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
9556095821
}
9556195822
for(i=0; i<pDest->nCol; i++){
95562
- if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
95823
+ Column *pDestCol = &pDest->aCol[i];
95824
+ Column *pSrcCol = &pSrc->aCol[i];
95825
+ if( pDestCol->affinity!=pSrcCol->affinity ){
9556395826
return 0; /* Affinity must be the same on all columns */
9556495827
}
95565
- if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
95828
+ if( !xferCompatibleCollation(pDestCol->zColl, pSrcCol->zColl) ){
9556695829
return 0; /* Collating sequence must be the same on all columns */
9556795830
}
95568
- if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
95831
+ if( pDestCol->notNull && !pSrcCol->notNull ){
9556995832
return 0; /* tab2 must be NOT NULL if tab1 is */
95833
+ }
95834
+ /* Default values for second and subsequent columns need to match. */
95835
+ if( i>0
95836
+ && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0)
95837
+ || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0))
95838
+ ){
95839
+ return 0; /* Default values must be the same for all columns */
9557095840
}
9557195841
}
9557295842
for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
9557395843
if( pDestIdx->onError!=OE_None ){
9557495844
destHasUniqueIdx = 1;
@@ -98583,17 +98853,19 @@
9858398853
Table *pTab = sqliteHashData(i);
9858498854
sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
9858598855
sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
9858698856
sqlite3VdbeAddOp2(v, OP_Integer,
9858798857
(int)sqlite3LogEstToInt(pTab->szTabRow), 3);
98588
- sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
98858
+ sqlite3VdbeAddOp2(v, OP_Integer,
98859
+ (int)sqlite3LogEstToInt(pTab->nRowLogEst), 4);
9858998860
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
9859098861
for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
9859198862
sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
9859298863
sqlite3VdbeAddOp2(v, OP_Integer,
9859398864
(int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
98594
- sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
98865
+ sqlite3VdbeAddOp2(v, OP_Integer,
98866
+ (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]), 4);
9859598867
sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
9859698868
}
9859798869
}
9859898870
}
9859998871
break;
@@ -100760,19 +101032,21 @@
100760101032
Select *pSelect, /* The whole SELECT statement */
100761101033
int regData /* Register holding data to be sorted */
100762101034
){
100763101035
Vdbe *v = pParse->pVdbe;
100764101036
int nExpr = pSort->pOrderBy->nExpr;
100765
- int regBase = sqlite3GetTempRange(pParse, nExpr+2);
100766
- int regRecord = sqlite3GetTempReg(pParse);
101037
+ int regRecord = ++pParse->nMem;
101038
+ int regBase = pParse->nMem+1;
100767101039
int nOBSat = pSort->nOBSat;
100768101040
int op;
101041
+
101042
+ pParse->nMem += nExpr+2; /* nExpr+2 registers allocated at regBase */
100769101043
sqlite3ExprCacheClear(pParse);
100770101044
sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, 0);
100771101045
sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
100772101046
sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
100773
- sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nExpr+2-nOBSat, regRecord);
101047
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nExpr+2-nOBSat,regRecord);
100774101048
if( nOBSat>0 ){
100775101049
int regPrevKey; /* The first nOBSat columns of the previous row */
100776101050
int addrFirst; /* Address of the OP_IfNot opcode */
100777101051
int addrJmp; /* Address of the OP_Jump opcode */
100778101052
VdbeOp *pOp; /* Opcode that opens the sorter */
@@ -100805,14 +101079,10 @@
100805101079
op = OP_SorterInsert;
100806101080
}else{
100807101081
op = OP_IdxInsert;
100808101082
}
100809101083
sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord);
100810
- if( nOBSat==0 ){
100811
- sqlite3ReleaseTempReg(pParse, regRecord);
100812
- sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
100813
- }
100814101084
if( pSelect->iLimit ){
100815101085
int addr1, addr2;
100816101086
int iLimit;
100817101087
if( pSelect->iOffset ){
100818101088
iLimit = pSelect->iOffset+1;
@@ -101984,11 +102254,11 @@
101984102254
/* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
101985102255
** is disabled */
101986102256
assert( db->lookaside.bEnabled==0 );
101987102257
pTab->nRef = 1;
101988102258
pTab->zName = 0;
101989
- pTab->nRowEst = 1048576;
102259
+ pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
101990102260
selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
101991102261
selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
101992102262
pTab->iPKey = -1;
101993102263
if( db->mallocFailed ){
101994102264
sqlite3DeleteTable(db, pTab);
@@ -104123,11 +104393,11 @@
104123104393
pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
104124104394
if( pTab==0 ) return WRC_Abort;
104125104395
pTab->nRef = 1;
104126104396
pTab->zName = sqlite3DbStrDup(db, pCte->zName);
104127104397
pTab->iPKey = -1;
104128
- pTab->nRowEst = 1048576;
104398
+ pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
104129104399
pTab->tabFlags |= TF_Ephemeral;
104130104400
pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
104131104401
if( db->mallocFailed ) return SQLITE_NOMEM;
104132104402
assert( pFrom->pSelect );
104133104403
@@ -104299,11 +104569,11 @@
104299104569
pTab->nRef = 1;
104300104570
pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
104301104571
while( pSel->pPrior ){ pSel = pSel->pPrior; }
104302104572
selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
104303104573
pTab->iPKey = -1;
104304
- pTab->nRowEst = 1048576;
104574
+ pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
104305104575
pTab->tabFlags |= TF_Ephemeral;
104306104576
#endif
104307104577
}else{
104308104578
/* An ordinary table or view name in the FROM clause */
104309104579
assert( pFrom->pTab==0 );
@@ -104794,14 +105064,15 @@
104794105064
Parse *pParse, /* Parse context */
104795105065
Table *pTab, /* Table being queried */
104796105066
Index *pIdx /* Index used to optimize scan, or NULL */
104797105067
){
104798105068
if( pParse->explain==2 ){
105069
+ int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
104799105070
char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
104800
- pTab->zName,
104801
- pIdx ? " USING COVERING INDEX " : "",
104802
- pIdx ? pIdx->zName : ""
105071
+ pTab->zName,
105072
+ bCover ? " USING COVERING INDEX " : "",
105073
+ bCover ? pIdx->zName : ""
104803105074
);
104804105075
sqlite3VdbeAddOp4(
104805105076
pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
104806105077
);
104807105078
}
@@ -104949,11 +105220,11 @@
104949105220
VdbeComment((v, "%s", pItem->pTab->zName));
104950105221
pItem->addrFillSub = addrTop;
104951105222
sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
104952105223
explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104953105224
sqlite3Select(pParse, pSub, &dest);
104954
- pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
105225
+ pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
104955105226
pItem->viaCoroutine = 1;
104956105227
pItem->regResult = dest.iSdst;
104957105228
sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
104958105229
sqlite3VdbeJumpHere(v, addrTop-1);
104959105230
sqlite3ClearTempRegCache(pParse);
@@ -104980,11 +105251,11 @@
104980105251
VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
104981105252
}
104982105253
sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
104983105254
explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104984105255
sqlite3Select(pParse, pSub, &dest);
104985
- pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
105256
+ pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
104986105257
if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
104987105258
retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
104988105259
VdbeComment((v, "end %s", pItem->pTab->zName));
104989105260
sqlite3VdbeChangeP1(v, topAddr, retAddr);
104990105261
sqlite3ClearTempRegCache(pParse);
@@ -105013,22 +105284,10 @@
105013105284
explainSetInteger(pParse->iSelectId, iRestoreSelectId);
105014105285
return rc;
105015105286
}
105016105287
#endif
105017105288
105018
- /* If there is both a GROUP BY and an ORDER BY clause and they are
105019
- ** identical, then disable the ORDER BY clause since the GROUP BY
105020
- ** will cause elements to come out in the correct order. This is
105021
- ** an optimization - the correct answer should result regardless.
105022
- ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
105023
- ** to disable this optimization for testing purposes.
105024
- */
105025
- if( sqlite3ExprListCompare(p->pGroupBy, sSort.pOrderBy, -1)==0
105026
- && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
105027
- sSort.pOrderBy = 0;
105028
- }
105029
-
105030105289
/* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
105031105290
** if the select-list is the same as the ORDER BY list, then this query
105032105291
** can be rewritten as a GROUP BY. In other words, this:
105033105292
**
105034105293
** SELECT DISTINCT xyz FROM ... ORDER BY xyz
@@ -105153,10 +105412,11 @@
105153105412
int iAbortFlag; /* Mem address which causes query abort if positive */
105154105413
int groupBySort; /* Rows come from source in GROUP BY order */
105155105414
int addrEnd; /* End of processing for this SELECT */
105156105415
int sortPTab = 0; /* Pseudotable used to decode sorting results */
105157105416
int sortOut = 0; /* Output register from the sorter */
105417
+ int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
105158105418
105159105419
/* Remove any and all aliases between the result set and the
105160105420
** GROUP BY clause.
105161105421
*/
105162105422
if( pGroupBy ){
@@ -105172,10 +105432,22 @@
105172105432
if( p->nSelectRow>100 ) p->nSelectRow = 100;
105173105433
}else{
105174105434
p->nSelectRow = 1;
105175105435
}
105176105436
105437
+
105438
+ /* If there is both a GROUP BY and an ORDER BY clause and they are
105439
+ ** identical, then it may be possible to disable the ORDER BY clause
105440
+ ** on the grounds that the GROUP BY will cause elements to come out
105441
+ ** in the correct order. It also may not - the GROUP BY may use a
105442
+ ** database index that causes rows to be grouped together as required
105443
+ ** but not actually sorted. Either way, record the fact that the
105444
+ ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
105445
+ ** variable. */
105446
+ if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
105447
+ orderByGrp = 1;
105448
+ }
105177105449
105178105450
/* Create a label to jump to when we want to abort the query */
105179105451
addrEnd = sqlite3VdbeMakeLabel(v);
105180105452
105181105453
/* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
@@ -105252,11 +105524,12 @@
105252105524
** it might be a single loop that uses an index to extract information
105253105525
** in the right order to begin with.
105254105526
*/
105255105527
sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
105256105528
pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
105257
- WHERE_GROUPBY, 0);
105529
+ WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
105530
+ );
105258105531
if( pWInfo==0 ) goto select_end;
105259105532
if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
105260105533
/* The optimizer is able to deliver rows in group by order so
105261105534
** we do not have to sort. The OP_OpenEphemeral table will be
105262105535
** cancelled later because we still need to use the pKeyInfo
@@ -105317,10 +105590,25 @@
105317105590
sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
105318105591
sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
105319105592
VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
105320105593
sAggInfo.useSortingIdx = 1;
105321105594
sqlite3ExprCacheClear(pParse);
105595
+
105596
+ }
105597
+
105598
+ /* If the index or temporary table used by the GROUP BY sort
105599
+ ** will naturally deliver rows in the order required by the ORDER BY
105600
+ ** clause, cancel the ephemeral table open coded earlier.
105601
+ **
105602
+ ** This is an optimization - the correct answer should result regardless.
105603
+ ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
105604
+ ** disable this optimization for testing purposes. */
105605
+ if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
105606
+ && (groupBySort || sqlite3WhereIsSorted(pWInfo))
105607
+ ){
105608
+ sSort.pOrderBy = 0;
105609
+ sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
105322105610
}
105323105611
105324105612
/* Evaluate the current GROUP BY terms and store in b0, b1, b2...
105325105613
** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
105326105614
** Then compare the current GROUP BY terms against the GROUP BY terms
@@ -107204,11 +107492,11 @@
107204107492
*/
107205107493
pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
107206107494
iIdxCur = iDataCur+1;
107207107495
pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
107208107496
for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
107209
- if( pIdx->autoIndex==2 && pPk!=0 ){
107497
+ if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
107210107498
iDataCur = pParse->nTab;
107211107499
pTabList->a[0].iCursor = iDataCur;
107212107500
}
107213107501
pParse->nTab++;
107214107502
}
@@ -109680,10 +109968,11 @@
109680109968
WhereLoop *pLoops; /* List of all WhereLoop objects */
109681109969
Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
109682109970
LogEst nRowOut; /* Estimated number of output rows */
109683109971
u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
109684109972
i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
109973
+ u8 sorted; /* True if really sorted (not just grouped) */
109685109974
u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
109686109975
u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
109687109976
u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
109688109977
u8 nLevel; /* Number of nested loop */
109689109978
int iTop; /* The very beginning of the WHERE loop */
@@ -109739,10 +110028,11 @@
109739110028
#define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
109740110029
#define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
109741110030
#define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
109742110031
#define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
109743110032
#define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
110033
+#define WHERE_LIKELIHOOD 0x00020000 /* A likelihood() is affecting nOut */
109744110034
109745110035
/************** End of whereInt.h ********************************************/
109746110036
/************** Continuing where we left off in where.c **********************/
109747110037
109748110038
/*
@@ -109951,11 +110241,11 @@
109951110241
}
109952110242
pTerm = &pWC->a[idx = pWC->nTerm++];
109953110243
if( p && ExprHasProperty(p, EP_Unlikely) ){
109954110244
pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
109955110245
}else{
109956
- pTerm->truthProb = -1;
110246
+ pTerm->truthProb = 1;
109957110247
}
109958110248
pTerm->pExpr = sqlite3ExprSkipCollate(p);
109959110249
pTerm->wtFlags = wtFlags;
109960110250
pTerm->pWC = pWC;
109961110251
pTerm->iParent = -1;
@@ -111680,11 +111970,12 @@
111680111970
tRowcnt iLower, iUpper, iGap;
111681111971
if( i==0 ){
111682111972
iLower = 0;
111683111973
iUpper = aSample[0].anLt[iCol];
111684111974
}else{
111685
- iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
111975
+ i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
111976
+ iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
111686111977
iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
111687111978
}
111688111979
aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
111689111980
if( iLower>=iUpper ){
111690111981
iGap = 0;
@@ -111698,10 +111989,33 @@
111698111989
}
111699111990
aStat[0] = iLower + iGap;
111700111991
}
111701111992
}
111702111993
#endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
111994
+
111995
+/*
111996
+** If it is not NULL, pTerm is a term that provides an upper or lower
111997
+** bound on a range scan. Without considering pTerm, it is estimated
111998
+** that the scan will visit nNew rows. This function returns the number
111999
+** estimated to be visited after taking pTerm into account.
112000
+**
112001
+** If the user explicitly specified a likelihood() value for this term,
112002
+** then the return value is the likelihood multiplied by the number of
112003
+** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
112004
+** has a likelihood of 0.50, and any other term a likelihood of 0.25.
112005
+*/
112006
+static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
112007
+ LogEst nRet = nNew;
112008
+ if( pTerm ){
112009
+ if( pTerm->truthProb<=0 ){
112010
+ nRet += pTerm->truthProb;
112011
+ }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
112012
+ nRet -= 20; assert( 20==sqlite3LogEst(4) );
112013
+ }
112014
+ }
112015
+ return nRet;
112016
+}
111703112017
111704112018
/*
111705112019
** This function is used to estimate the number of rows that will be visited
111706112020
** by scanning an index for a range of values. The range may have an upper
111707112021
** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -111791,11 +112105,11 @@
111791112105
aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
111792112106
}
111793112107
/* Determine iLower and iUpper using ($P) only. */
111794112108
if( nEq==0 ){
111795112109
iLower = 0;
111796
- iUpper = p->aiRowEst[0];
112110
+ iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
111797112111
}else{
111798112112
/* Note: this call could be optimized away - since the same values must
111799112113
** have been requested when testing key $P in whereEqualScanEst(). */
111800112114
whereKeyStats(pParse, p, pRec, 0, a);
111801112115
iLower = a[0];
@@ -111851,21 +112165,22 @@
111851112165
#else
111852112166
UNUSED_PARAMETER(pParse);
111853112167
UNUSED_PARAMETER(pBuilder);
111854112168
#endif
111855112169
assert( pLower || pUpper );
111856
- /* TUNING: Each inequality constraint reduces the search space 4-fold.
111857
- ** A BETWEEN operator, therefore, reduces the search space 16-fold */
111858
- nNew = nOut;
111859
- if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
111860
- nNew -= 20; assert( 20==sqlite3LogEst(4) );
111861
- nOut--;
111862
- }
111863
- if( pUpper ){
111864
- nNew -= 20; assert( 20==sqlite3LogEst(4) );
111865
- nOut--;
111866
- }
112170
+ assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
112171
+ nNew = whereRangeAdjust(pLower, nOut);
112172
+ nNew = whereRangeAdjust(pUpper, nNew);
112173
+
112174
+ /* TUNING: If there is both an upper and lower limit, assume the range is
112175
+ ** reduced by an additional 75%. This means that, by default, an open-ended
112176
+ ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
112177
+ ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
112178
+ ** match 1/64 of the index. */
112179
+ if( pLower && pUpper ) nNew -= 20;
112180
+
112181
+ nOut -= (pLower!=0) + (pUpper!=0);
111867112182
if( nNew<10 ) nNew = 10;
111868112183
if( nNew<nOut ) nOut = nNew;
111869112184
pLoop->nOut = (LogEst)nOut;
111870112185
return rc;
111871112186
}
@@ -111958,26 +112273,27 @@
111958112273
WhereLoopBuilder *pBuilder,
111959112274
ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
111960112275
tRowcnt *pnRow /* Write the revised row estimate here */
111961112276
){
111962112277
Index *p = pBuilder->pNew->u.btree.pIndex;
112278
+ i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
111963112279
int nRecValid = pBuilder->nRecValid;
111964112280
int rc = SQLITE_OK; /* Subfunction return code */
111965112281
tRowcnt nEst; /* Number of rows for a single term */
111966112282
tRowcnt nRowEst = 0; /* New estimate of the number of rows */
111967112283
int i; /* Loop counter */
111968112284
111969112285
assert( p->aSample!=0 );
111970112286
for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
111971
- nEst = p->aiRowEst[0];
112287
+ nEst = nRow0;
111972112288
rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
111973112289
nRowEst += nEst;
111974112290
pBuilder->nRecValid = nRecValid;
111975112291
}
111976112292
111977112293
if( rc==SQLITE_OK ){
111978
- if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
112294
+ if( nRowEst > nRow0 ) nRowEst = nRow0;
111979112295
*pnRow = nRowEst;
111980112296
WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst));
111981112297
}
111982112298
assert( pBuilder->nRecValid==nRecValid );
111983112299
return rc;
@@ -112416,17 +112732,24 @@
112416112732
zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
112417112733
}
112418112734
if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
112419112735
&& ALWAYS(pLoop->u.btree.pIndex!=0)
112420112736
){
112737
+ const char *zFmt;
112738
+ Index *pIdx = pLoop->u.btree.pIndex;
112421112739
char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
112422
- zMsg = sqlite3MAppendf(db, zMsg,
112423
- ((flags & WHERE_AUTO_INDEX) ?
112424
- "%s USING AUTOMATIC %sINDEX%.0s%s" :
112425
- "%s USING %sINDEX %s%s"),
112426
- zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""),
112427
- pLoop->u.btree.pIndex->zName, zWhere);
112740
+ assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
112741
+ if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
112742
+ zFmt = zWhere ? "%s USING PRIMARY KEY%.0s%s" : "%s%.0s%s";
112743
+ }else if( flags & WHERE_AUTO_INDEX ){
112744
+ zFmt = "%s USING AUTOMATIC COVERING INDEX%.0s%s";
112745
+ }else if( flags & WHERE_IDX_ONLY ){
112746
+ zFmt = "%s USING COVERING INDEX %s%s";
112747
+ }else{
112748
+ zFmt = "%s USING INDEX %s%s";
112749
+ }
112750
+ zMsg = sqlite3MAppendf(db, zMsg, zFmt, zMsg, pIdx->zName, zWhere);
112428112751
sqlite3DbFree(db, zWhere);
112429112752
}else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
112430112753
zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
112431112754
112432112755
if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
@@ -112914,11 +113237,11 @@
112914113237
}else if( HasRowid(pIdx->pTable) ){
112915113238
iRowidReg = ++pParse->nMem;
112916113239
sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
112917113240
sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
112918113241
sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
112919
- }else{
113242
+ }else if( iCur!=iIdxCur ){
112920113243
Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
112921113244
iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
112922113245
for(j=0; j<pPk->nKeyCol; j++){
112923113246
k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
112924113247
sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
@@ -112984,10 +113307,14 @@
112984113307
**
112985113308
** Return 2 # Jump back to the Gosub
112986113309
**
112987113310
** B: <after the loop>
112988113311
**
113312
+ ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
113313
+ ** use an ephermeral index instead of a RowSet to record the primary
113314
+ ** keys of the rows we have already seen.
113315
+ **
112989113316
*/
112990113317
WhereClause *pOrWc; /* The OR-clause broken out into subterms */
112991113318
SrcList *pOrTab; /* Shortened table list or OR-clause generation */
112992113319
Index *pCov = 0; /* Potential covering index (or NULL) */
112993113320
int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
@@ -112998,10 +113325,11 @@
112998113325
int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
112999113326
int iRetInit; /* Address of regReturn init */
113000113327
int untestedTerms = 0; /* Some terms not completely tested */
113001113328
int ii; /* Loop counter */
113002113329
Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
113330
+ Table *pTab = pTabItem->pTab;
113003113331
113004113332
pTerm = pLoop->aLTerm[0];
113005113333
assert( pTerm!=0 );
113006113334
assert( pTerm->eOperator & WO_OR );
113007113335
assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
@@ -113030,11 +113358,12 @@
113030113358
}else{
113031113359
pOrTab = pWInfo->pTabList;
113032113360
}
113033113361
113034113362
/* Initialize the rowset register to contain NULL. An SQL NULL is
113035
- ** equivalent to an empty rowset.
113363
+ ** equivalent to an empty rowset. Or, create an ephermeral index
113364
+ ** capable of holding primary keys in the case of a WITHOUT ROWID.
113036113365
**
113037113366
** Also initialize regReturn to contain the address of the instruction
113038113367
** immediately following the OP_Return at the bottom of the loop. This
113039113368
** is required in a few obscure LEFT JOIN cases where control jumps
113040113369
** over the top of the loop into the body of it. In this case the
@@ -113041,13 +113370,20 @@
113041113370
** correct response for the end-of-loop code (the OP_Return) is to
113042113371
** fall through to the next instruction, just as an OP_Next does if
113043113372
** called on an uninitialized cursor.
113044113373
*/
113045113374
if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
113046
- regRowset = ++pParse->nMem;
113375
+ if( HasRowid(pTab) ){
113376
+ regRowset = ++pParse->nMem;
113377
+ sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
113378
+ }else{
113379
+ Index *pPk = sqlite3PrimaryKeyIndex(pTab);
113380
+ regRowset = pParse->nTab++;
113381
+ sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
113382
+ sqlite3VdbeSetP4KeyInfo(pParse, pPk);
113383
+ }
113047113384
regRowid = ++pParse->nMem;
113048
- sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
113049113385
}
113050113386
iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
113051113387
113052113388
/* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
113053113389
** Then for every term xN, evaluate as the subexpression: xN AND z
@@ -113079,15 +113415,20 @@
113079113415
if( pAndExpr ){
113080113416
pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
113081113417
}
113082113418
}
113083113419
113420
+ /* Run a separate WHERE clause for each term of the OR clause. After
113421
+ ** eliminating duplicates from other WHERE clauses, the action for each
113422
+ ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
113423
+ */
113084113424
for(ii=0; ii<pOrWc->nTerm; ii++){
113085113425
WhereTerm *pOrTerm = &pOrWc->a[ii];
113086113426
if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
113087
- WhereInfo *pSubWInfo; /* Info for single OR-term scan */
113088
- Expr *pOrExpr = pOrTerm->pExpr;
113427
+ WhereInfo *pSubWInfo; /* Info for single OR-term scan */
113428
+ Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
113429
+ int j1 = 0; /* Address of jump operation */
113089113430
if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
113090113431
pAndExpr->pLeft = pOrExpr;
113091113432
pOrExpr = pAndExpr;
113092113433
}
113093113434
/* Loop through table entries that match term pOrTerm. */
@@ -113098,20 +113439,66 @@
113098113439
if( pSubWInfo ){
113099113440
WhereLoop *pSubLoop;
113100113441
explainOneScan(
113101113442
pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
113102113443
);
113444
+ /* This is the sub-WHERE clause body. First skip over
113445
+ ** duplicate rows from prior sub-WHERE clauses, and record the
113446
+ ** rowid (or PRIMARY KEY) for the current row so that the same
113447
+ ** row will be skipped in subsequent sub-WHERE clauses.
113448
+ */
113103113449
if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
113104
- int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
113105113450
int r;
113106
- r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
113107
- regRowid, 0);
113108
- sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
113109
- sqlite3VdbeCurrentAddr(v)+2, r, iSet);
113110
- VdbeCoverage(v);
113451
+ int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
113452
+ if( HasRowid(pTab) ){
113453
+ r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
113454
+ j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
113455
+ VdbeCoverage(v);
113456
+ }else{
113457
+ Index *pPk = sqlite3PrimaryKeyIndex(pTab);
113458
+ int nPk = pPk->nKeyCol;
113459
+ int iPk;
113460
+
113461
+ /* Read the PK into an array of temp registers. */
113462
+ r = sqlite3GetTempRange(pParse, nPk);
113463
+ for(iPk=0; iPk<nPk; iPk++){
113464
+ int iCol = pPk->aiColumn[iPk];
113465
+ sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
113466
+ }
113467
+
113468
+ /* Check if the temp table already contains this key. If so,
113469
+ ** the row has already been included in the result set and
113470
+ ** can be ignored (by jumping past the Gosub below). Otherwise,
113471
+ ** insert the key into the temp table and proceed with processing
113472
+ ** the row.
113473
+ **
113474
+ ** Use some of the same optimizations as OP_RowSetTest: If iSet
113475
+ ** is zero, assume that the key cannot already be present in
113476
+ ** the temp table. And if iSet is -1, assume that there is no
113477
+ ** need to insert the key into the temp table, as it will never
113478
+ ** be tested for. */
113479
+ if( iSet ){
113480
+ j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
113481
+ VdbeCoverage(v);
113482
+ }
113483
+ if( iSet>=0 ){
113484
+ sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
113485
+ sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
113486
+ if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
113487
+ }
113488
+
113489
+ /* Release the array of temp registers */
113490
+ sqlite3ReleaseTempRange(pParse, r, nPk);
113491
+ }
113111113492
}
113493
+
113494
+ /* Invoke the main loop body as a subroutine */
113112113495
sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
113496
+
113497
+ /* Jump here (skipping the main loop body subroutine) if the
113498
+ ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
113499
+ if( j1 ) sqlite3VdbeJumpHere(v, j1);
113113113500
113114113501
/* The pSubWInfo->untestedTerms flag means that this OR term
113115113502
** contained one or more AND term from a notReady table. The
113116113503
** terms from the notReady table could not be tested and will
113117113504
** need to be tested later.
@@ -113132,10 +113519,11 @@
113132113519
*/
113133113520
pSubLoop = pSubWInfo->a[0].pWLoop;
113134113521
assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
113135113522
if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
113136113523
&& (ii==0 || pSubLoop->u.btree.pIndex==pCov)
113524
+ && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
113137113525
){
113138113526
assert( pSubWInfo->a[0].iIdxCur==iCovCur );
113139113527
pCov = pSubLoop->u.btree.pIndex;
113140113528
}else{
113141113529
pCov = 0;
@@ -113459,11 +113847,11 @@
113459113847
if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */
113460113848
if( pX->rRun >= pY->rRun ){
113461113849
if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
113462113850
if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
113463113851
}
113464
- for(j=0, i=pX->nLTerm-1; i>=0; i--){
113852
+ for(i=pX->nLTerm-1; i>=0; i--){
113465113853
for(j=pY->nLTerm-1; j>=0; j--){
113466113854
if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
113467113855
}
113468113856
if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
113469113857
}
@@ -113481,16 +113869,29 @@
113481113869
** is a proper subset.
113482113870
**
113483113871
** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
113484113872
** WHERE clause terms than Y and that every WHERE clause term used by X is
113485113873
** also used by Y.
113874
+**
113875
+** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the
113876
+** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE
113877
+** clause terms covered, since some of the first nLTerm entries in aLTerm[]
113878
+** will be NULL (because they are skipped). That makes it more difficult
113879
+** to compare the loops. We could add extra code to do the comparison, and
113880
+** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this
113881
+** adjustment is sufficient minor, that it is very difficult to construct
113882
+** a test case where the extra code would improve the query plan. Better
113883
+** to avoid the added complexity and just omit cost adjustments to SKIPSCAN
113884
+** loops.
113486113885
*/
113487113886
static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
113488113887
if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
113888
+ if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return;
113489113889
for(; p; p=p->pNextLoop){
113490113890
if( p->iTab!=pTemplate->iTab ) continue;
113491113891
if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
113892
+ if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue;
113492113893
if( whereLoopCheaperProperSubset(p, pTemplate) ){
113493113894
/* Adjust pTemplate cost downward so that it is cheaper than its
113494113895
** subset p */
113495113896
pTemplate->rRun = p->rRun;
113496113897
pTemplate->nOut = p->nOut - 1;
@@ -113711,17 +114112,24 @@
113711114112
pX = pLoop->aLTerm[j];
113712114113
if( pX==0 ) continue;
113713114114
if( pX==pTerm ) break;
113714114115
if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
113715114116
}
113716
- if( j<0 ) pLoop->nOut += pTerm->truthProb;
114117
+ if( j<0 ){
114118
+ pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1);
114119
+ }
113717114120
}
113718114121
}
113719114122
113720114123
/*
113721
-** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
113722
-** Try to match one more.
114124
+** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
114125
+** index pIndex. Try to match one more.
114126
+**
114127
+** When this function is called, pBuilder->pNew->nOut contains the
114128
+** number of rows expected to be visited by filtering using the nEq
114129
+** terms only. If it is modified, this value is restored before this
114130
+** function returns.
113723114131
**
113724114132
** If pProbe->tnum==0, that means pIndex is a fake index used for the
113725114133
** INTEGER PRIMARY KEY.
113726114134
*/
113727114135
static int whereLoopAddBtreeIndex(
@@ -113743,11 +114151,10 @@
113743114151
u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */
113744114152
u32 saved_wsFlags; /* Original value of pNew->wsFlags */
113745114153
LogEst saved_nOut; /* Original value of pNew->nOut */
113746114154
int iCol; /* Index of the column in the table */
113747114155
int rc = SQLITE_OK; /* Return code */
113748
- LogEst nRowEst; /* Estimated index selectivity */
113749114156
LogEst rLogSize; /* Logarithm of table size */
113750114157
WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
113751114158
113752114159
pNew = pBuilder->pNew;
113753114160
if( db->mallocFailed ) return SQLITE_NOMEM;
@@ -113764,15 +114171,12 @@
113764114171
if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
113765114172
113766114173
assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
113767114174
if( pNew->u.btree.nEq < pProbe->nKeyCol ){
113768114175
iCol = pProbe->aiColumn[pNew->u.btree.nEq];
113769
- nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
113770
- if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
113771114176
}else{
113772114177
iCol = -1;
113773
- nRowEst = 0;
113774114178
}
113775114179
pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
113776114180
opMask, pProbe);
113777114181
saved_nEq = pNew->u.btree.nEq;
113778114182
saved_nSkip = pNew->u.btree.nSkip;
@@ -113779,57 +114183,68 @@
113779114183
saved_nLTerm = pNew->nLTerm;
113780114184
saved_wsFlags = pNew->wsFlags;
113781114185
saved_prereq = pNew->prereq;
113782114186
saved_nOut = pNew->nOut;
113783114187
pNew->rSetup = 0;
113784
- rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
114188
+ rLogSize = estLog(pProbe->aiRowLogEst[0]);
113785114189
113786114190
/* Consider using a skip-scan if there are no WHERE clause constraints
113787114191
** available for the left-most terms of the index, and if the average
113788
- ** number of repeats in the left-most terms is at least 18. The magic
113789
- ** number 18 was found by experimentation to be the payoff point where
113790
- ** skip-scan become faster than a full-scan.
113791
- */
114192
+ ** number of repeats in the left-most terms is at least 18.
114193
+ **
114194
+ ** The magic number 18 is selected on the basis that scanning 17 rows
114195
+ ** is almost always quicker than an index seek (even though if the index
114196
+ ** contains fewer than 2^17 rows we assume otherwise in other parts of
114197
+ ** the code). And, even if it is not, it should not be too much slower.
114198
+ ** On the other hand, the extra seeks could end up being significantly
114199
+ ** more expensive. */
114200
+ assert( 42==sqlite3LogEst(18) );
113792114201
if( pTerm==0
113793114202
&& saved_nEq==saved_nSkip
113794114203
&& saved_nEq+1<pProbe->nKeyCol
113795
- && pProbe->aiRowEst[saved_nEq+1]>=18 /* TUNING: Minimum for skip-scan */
114204
+ && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
113796114205
&& (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
113797114206
){
113798114207
LogEst nIter;
113799114208
pNew->u.btree.nEq++;
113800114209
pNew->u.btree.nSkip++;
113801114210
pNew->aLTerm[pNew->nLTerm++] = 0;
113802114211
pNew->wsFlags |= WHERE_SKIPSCAN;
113803
- nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]);
113804
- pNew->rRun = rLogSize + nIter;
113805
- pNew->nOut += nIter;
113806
- whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter);
114212
+ nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
114213
+ pNew->nOut -= nIter;
114214
+ whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
113807114215
pNew->nOut = saved_nOut;
113808114216
}
113809114217
for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
114218
+ u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
114219
+ LogEst rCostIdx;
114220
+ LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
113810114221
int nIn = 0;
113811114222
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113812114223
int nRecValid = pBuilder->nRecValid;
113813114224
#endif
113814
- if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
114225
+ if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
113815114226
&& (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
113816114227
){
113817114228
continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
113818114229
}
113819114230
if( pTerm->prereqRight & pNew->maskSelf ) continue;
113820114231
113821
- assert( pNew->nOut==saved_nOut );
113822
-
113823114232
pNew->wsFlags = saved_wsFlags;
113824114233
pNew->u.btree.nEq = saved_nEq;
113825114234
pNew->nLTerm = saved_nLTerm;
113826114235
if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
113827114236
pNew->aLTerm[pNew->nLTerm++] = pTerm;
113828114237
pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
113829
- pNew->rRun = rLogSize; /* Baseline cost is log2(N). Adjustments below */
113830
- if( pTerm->eOperator & WO_IN ){
114238
+
114239
+ assert( nInMul==0
114240
+ || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
114241
+ || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
114242
+ || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
114243
+ );
114244
+
114245
+ if( eOp & WO_IN ){
113831114246
Expr *pExpr = pTerm->pExpr;
113832114247
pNew->wsFlags |= WHERE_COLUMN_IN;
113833114248
if( ExprHasProperty(pExpr, EP_xIsSelect) ){
113834114249
/* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
113835114250
nIn = 46; assert( 46==sqlite3LogEst(25) );
@@ -113837,87 +114252,122 @@
113837114252
/* "x IN (value, value, ...)" */
113838114253
nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
113839114254
}
113840114255
assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
113841114256
** changes "x IN (?)" into "x=?". */
113842
- pNew->rRun += nIn;
113843
- pNew->u.btree.nEq++;
113844
- pNew->nOut = nRowEst + nInMul + nIn;
113845
- }else if( pTerm->eOperator & (WO_EQ) ){
113846
- assert(
113847
- (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0
113848
- || nInMul==0
113849
- );
114257
+
114258
+ }else if( eOp & (WO_EQ) ){
113850114259
pNew->wsFlags |= WHERE_COLUMN_EQ;
113851
- if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1)){
113852
- assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
114260
+ if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
113853114261
if( iCol>=0 && pProbe->onError==OE_None ){
113854114262
pNew->wsFlags |= WHERE_UNQ_WANTED;
113855114263
}else{
113856114264
pNew->wsFlags |= WHERE_ONEROW;
113857114265
}
113858114266
}
113859
- pNew->u.btree.nEq++;
113860
- pNew->nOut = nRowEst + nInMul;
113861
- }else if( pTerm->eOperator & (WO_ISNULL) ){
114267
+ }else if( eOp & WO_ISNULL ){
113862114268
pNew->wsFlags |= WHERE_COLUMN_NULL;
113863
- pNew->u.btree.nEq++;
113864
- /* TUNING: IS NULL selects 2 rows */
113865
- nIn = 10; assert( 10==sqlite3LogEst(2) );
113866
- pNew->nOut = nRowEst + nInMul + nIn;
113867
- }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
113868
- testcase( pTerm->eOperator & WO_GT );
113869
- testcase( pTerm->eOperator & WO_GE );
114269
+ }else if( eOp & (WO_GT|WO_GE) ){
114270
+ testcase( eOp & WO_GT );
114271
+ testcase( eOp & WO_GE );
113870114272
pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
113871114273
pBtm = pTerm;
113872114274
pTop = 0;
113873114275
}else{
113874
- assert( pTerm->eOperator & (WO_LT|WO_LE) );
113875
- testcase( pTerm->eOperator & WO_LT );
113876
- testcase( pTerm->eOperator & WO_LE );
114276
+ assert( eOp & (WO_LT|WO_LE) );
114277
+ testcase( eOp & WO_LT );
114278
+ testcase( eOp & WO_LE );
113877114279
pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
113878114280
pTop = pTerm;
113879114281
pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
113880114282
pNew->aLTerm[pNew->nLTerm-2] : 0;
113881114283
}
114284
+
114285
+ /* At this point pNew->nOut is set to the number of rows expected to
114286
+ ** be visited by the index scan before considering term pTerm, or the
114287
+ ** values of nIn and nInMul. In other words, assuming that all
114288
+ ** "x IN(...)" terms are replaced with "x = ?". This block updates
114289
+ ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
114290
+ assert( pNew->nOut==saved_nOut );
113882114291
if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
113883
- /* Adjust nOut and rRun for STAT3 range values */
113884
- assert( pNew->nOut==saved_nOut );
114292
+ /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
114293
+ ** data, using some other estimate. */
113885114294
whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
113886
- }
114295
+ }else{
114296
+ int nEq = ++pNew->u.btree.nEq;
114297
+ assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) );
114298
+
114299
+ assert( pNew->nOut==saved_nOut );
114300
+ if( pTerm->truthProb<=0 && iCol>=0 ){
114301
+ assert( (eOp & WO_IN) || nIn==0 );
114302
+ testcase( eOp & WO_IN );
114303
+ pNew->nOut += pTerm->truthProb;
114304
+ pNew->nOut -= nIn;
114305
+ pNew->wsFlags |= WHERE_LIKELIHOOD;
114306
+ }else{
113887114307
#ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113888
- if( nInMul==0
113889
- && pProbe->nSample
113890
- && pNew->u.btree.nEq<=pProbe->nSampleCol
113891
- && OptimizationEnabled(db, SQLITE_Stat3)
113892
- ){
113893
- Expr *pExpr = pTerm->pExpr;
113894
- tRowcnt nOut = 0;
113895
- if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
113896
- testcase( pTerm->eOperator & WO_EQ );
113897
- testcase( pTerm->eOperator & WO_ISNULL );
113898
- rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
113899
- }else if( (pTerm->eOperator & WO_IN)
113900
- && !ExprHasProperty(pExpr, EP_xIsSelect) ){
113901
- rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
113902
- }
113903
- assert( nOut==0 || rc==SQLITE_OK );
113904
- if( nOut ){
113905
- pNew->nOut = sqlite3LogEst(nOut);
113906
- if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
113907
- }
113908
- }
113909
-#endif
114308
+ tRowcnt nOut = 0;
114309
+ if( nInMul==0
114310
+ && pProbe->nSample
114311
+ && pNew->u.btree.nEq<=pProbe->nSampleCol
114312
+ && OptimizationEnabled(db, SQLITE_Stat3)
114313
+ && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
114314
+ && (pNew->wsFlags & WHERE_LIKELIHOOD)==0
114315
+ ){
114316
+ Expr *pExpr = pTerm->pExpr;
114317
+ if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
114318
+ testcase( eOp & WO_EQ );
114319
+ testcase( eOp & WO_ISNULL );
114320
+ rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
114321
+ }else{
114322
+ rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
114323
+ }
114324
+ assert( rc!=SQLITE_OK || nOut>0 );
114325
+ if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
114326
+ if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
114327
+ if( nOut ){
114328
+ pNew->nOut = sqlite3LogEst(nOut);
114329
+ if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
114330
+ pNew->nOut -= nIn;
114331
+ }
114332
+ }
114333
+ if( nOut==0 )
114334
+#endif
114335
+ {
114336
+ pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
114337
+ if( eOp & WO_ISNULL ){
114338
+ /* TUNING: If there is no likelihood() value, assume that a
114339
+ ** "col IS NULL" expression matches twice as many rows
114340
+ ** as (col=?). */
114341
+ pNew->nOut += 10;
114342
+ }
114343
+ }
114344
+ }
114345
+ }
114346
+
114347
+ /* Set rCostIdx to the cost of visiting selected rows in index. Add
114348
+ ** it to pNew->rRun, which is currently set to the cost of the index
114349
+ ** seek only. Then, if this is a non-covering index, add the cost of
114350
+ ** visiting the rows in the main table. */
114351
+ rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
114352
+ pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
113910114353
if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
113911
- /* Each row involves a step of the index, then a binary search of
113912
- ** the main table */
113913
- pNew->rRun = sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
114354
+ pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
113914114355
}
113915
- /* Step cost for each output row */
113916
- pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
114356
+
114357
+ nOutUnadjusted = pNew->nOut;
114358
+ pNew->rRun += nInMul + nIn;
114359
+ pNew->nOut += nInMul + nIn;
113917114360
whereLoopOutputAdjust(pBuilder->pWC, pNew);
113918114361
rc = whereLoopInsert(pBuilder, pNew);
114362
+
114363
+ if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
114364
+ pNew->nOut = saved_nOut;
114365
+ }else{
114366
+ pNew->nOut = nOutUnadjusted;
114367
+ }
114368
+
113919114369
if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
113920114370
&& pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
113921114371
){
113922114372
whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
113923114373
}
@@ -113997,19 +114447,42 @@
113997114447
113998114448
/*
113999114449
** Add all WhereLoop objects for a single table of the join where the table
114000114450
** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be
114001114451
** a b-tree table, not a virtual table.
114452
+**
114453
+** The costs (WhereLoop.rRun) of the b-tree loops added by this function
114454
+** are calculated as follows:
114455
+**
114456
+** For a full scan, assuming the table (or index) contains nRow rows:
114457
+**
114458
+** cost = nRow * 3.0 // full-table scan
114459
+** cost = nRow * K // scan of covering index
114460
+** cost = nRow * (K+3.0) // scan of non-covering index
114461
+**
114462
+** where K is a value between 1.1 and 3.0 set based on the relative
114463
+** estimated average size of the index and table records.
114464
+**
114465
+** For an index scan, where nVisit is the number of index rows visited
114466
+** by the scan, and nSeek is the number of seek operations required on
114467
+** the index b-tree:
114468
+**
114469
+** cost = nSeek * (log(nRow) + K * nVisit) // covering index
114470
+** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
114471
+**
114472
+** Normally, nSeek is 1. nSeek values greater than 1 come about if the
114473
+** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
114474
+** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
114002114475
*/
114003114476
static int whereLoopAddBtree(
114004114477
WhereLoopBuilder *pBuilder, /* WHERE clause information */
114005114478
Bitmask mExtra /* Extra prerequesites for using this table */
114006114479
){
114007114480
WhereInfo *pWInfo; /* WHERE analysis context */
114008114481
Index *pProbe; /* An index we are evaluating */
114009114482
Index sPk; /* A fake index object for the primary key */
114010
- tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
114483
+ LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
114011114484
i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
114012114485
SrcList *pTabList; /* The FROM clause */
114013114486
struct SrcList_item *pSrc; /* The FROM clause btree term to add */
114014114487
WhereLoop *pNew; /* Template WhereLoop object */
114015114488
int rc = SQLITE_OK; /* Return code */
@@ -114040,24 +114513,25 @@
114040114513
** indices to follow */
114041114514
Index *pFirst; /* First of real indices on the table */
114042114515
memset(&sPk, 0, sizeof(Index));
114043114516
sPk.nKeyCol = 1;
114044114517
sPk.aiColumn = &aiColumnPk;
114045
- sPk.aiRowEst = aiRowEstPk;
114518
+ sPk.aiRowLogEst = aiRowEstPk;
114046114519
sPk.onError = OE_Replace;
114047114520
sPk.pTable = pTab;
114048
- aiRowEstPk[0] = pTab->nRowEst;
114049
- aiRowEstPk[1] = 1;
114521
+ sPk.szIdxRow = pTab->szTabRow;
114522
+ aiRowEstPk[0] = pTab->nRowLogEst;
114523
+ aiRowEstPk[1] = 0;
114050114524
pFirst = pSrc->pTab->pIndex;
114051114525
if( pSrc->notIndexed==0 ){
114052114526
/* The real indices of the table are only considered if the
114053114527
** NOT INDEXED qualifier is omitted from the FROM clause */
114054114528
sPk.pNext = pFirst;
114055114529
}
114056114530
pProbe = &sPk;
114057114531
}
114058
- rSize = sqlite3LogEst(pTab->nRowEst);
114532
+ rSize = pTab->nRowLogEst;
114059114533
rLogSize = estLog(rSize);
114060114534
114061114535
#ifndef SQLITE_OMIT_AUTOMATIC_INDEX
114062114536
/* Automatic indexes */
114063114537
if( !pBuilder->pOrSet
@@ -114103,10 +114577,11 @@
114103114577
for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
114104114578
if( pProbe->pPartIdxWhere!=0
114105114579
&& !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
114106114580
continue; /* Partial index inappropriate for this query */
114107114581
}
114582
+ rSize = pProbe->aiRowLogEst[0];
114108114583
pNew->u.btree.nEq = 0;
114109114584
pNew->u.btree.nSkip = 0;
114110114585
pNew->nLTerm = 0;
114111114586
pNew->iSortIdx = 0;
114112114587
pNew->rSetup = 0;
@@ -114120,14 +114595,12 @@
114120114595
/* Integer primary key index */
114121114596
pNew->wsFlags = WHERE_IPK;
114122114597
114123114598
/* Full table scan */
114124114599
pNew->iSortIdx = b ? iSortIdx : 0;
114125
- /* TUNING: Cost of full table scan is 3*(N + log2(N)).
114126
- ** + The extra 3 factor is to encourage the use of indexed lookups
114127
- ** over full scans. FIXME */
114128
- pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
114600
+ /* TUNING: Cost of full table scan is (N*3.0). */
114601
+ pNew->rRun = rSize + 16;
114129114602
whereLoopOutputAdjust(pWC, pNew);
114130114603
rc = whereLoopInsert(pBuilder, pNew);
114131114604
pNew->nOut = rSize;
114132114605
if( rc ) break;
114133114606
}else{
@@ -114150,39 +114623,20 @@
114150114623
&& sqlite3GlobalConfig.bUseCis
114151114624
&& OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
114152114625
)
114153114626
){
114154114627
pNew->iSortIdx = b ? iSortIdx : 0;
114155
- /* TUNING: The base cost of an index scan is N + log2(N).
114156
- ** The log2(N) is for the initial seek to the beginning and the N
114157
- ** is for the scan itself. */
114158
- pNew->rRun = sqlite3LogEstAdd(rSize, rLogSize);
114159
- if( m==0 ){
114160
- /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
114161
- ** + The extra factor K of between 1.1 and 3.0 that depends
114162
- ** on the relative sizes of the table and the index. K
114163
- ** is smaller for smaller indices, thus favoring them.
114164
- ** The upper bound on K (3.0) matches the penalty factor
114165
- ** on a full table scan that tries to encourage the use of
114166
- ** indexed lookups over full scans.
114167
- */
114168
- pNew->rRun += 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
114169
- }else{
114170
- /* TUNING: The cost of scanning a non-covering index is multiplied
114171
- ** by log2(N) to account for the binary search of the main table
114172
- ** that must happen for each row of the index.
114173
- ** TODO: Should there be a multiplier here, analogous to the 3x
114174
- ** multiplier for a fulltable scan or covering index scan, to
114175
- ** further discourage the use of an index scan? Or is the log2(N)
114176
- ** term sufficient discouragement?
114177
- ** TODO: What if some or all of the WHERE clause terms can be
114178
- ** computed without reference to the original table. Then the
114179
- ** penality should reduce to logK where K is the number of output
114180
- ** rows.
114181
- */
114182
- pNew->rRun += rLogSize;
114183
- }
114628
+
114629
+ /* The cost of visiting the index rows is N*K, where K is
114630
+ ** between 1.1 and 3.0, depending on the relative sizes of the
114631
+ ** index and table rows. If this is a non-covering index scan,
114632
+ ** also add the cost of visiting table rows (N*3.0). */
114633
+ pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
114634
+ if( m!=0 ){
114635
+ pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
114636
+ }
114637
+
114184114638
whereLoopOutputAdjust(pWC, pNew);
114185114639
rc = whereLoopInsert(pBuilder, pNew);
114186114640
pNew->nOut = rSize;
114187114641
if( rc ) break;
114188114642
}
@@ -114382,20 +114836,19 @@
114382114836
WhereTerm *pTerm, *pWCEnd;
114383114837
int rc = SQLITE_OK;
114384114838
int iCur;
114385114839
WhereClause tempWC;
114386114840
WhereLoopBuilder sSubBuild;
114387
- WhereOrSet sSum, sCur, sPrev;
114841
+ WhereOrSet sSum, sCur;
114388114842
struct SrcList_item *pItem;
114389114843
114390114844
pWC = pBuilder->pWC;
114391114845
if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
114392114846
pWCEnd = pWC->a + pWC->nTerm;
114393114847
pNew = pBuilder->pNew;
114394114848
memset(&sSum, 0, sizeof(sSum));
114395114849
pItem = pWInfo->pTabList->a + pNew->iTab;
114396
- if( !HasRowid(pItem->pTab) ) return SQLITE_OK;
114397114850
iCur = pItem->iCursor;
114398114851
114399114852
for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
114400114853
if( (pTerm->eOperator & WO_OR)!=0
114401114854
&& (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
@@ -114438,10 +114891,11 @@
114438114891
break;
114439114892
}else if( once ){
114440114893
whereOrMove(&sSum, &sCur);
114441114894
once = 0;
114442114895
}else{
114896
+ WhereOrSet sPrev;
114443114897
whereOrMove(&sPrev, &sSum);
114444114898
sSum.n = 0;
114445114899
for(i=0; i<sPrev.n; i++){
114446114900
for(j=0; j<sCur.n; j++){
114447114901
whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
@@ -114456,12 +114910,23 @@
114456114910
pNew->wsFlags = WHERE_MULTI_OR;
114457114911
pNew->rSetup = 0;
114458114912
pNew->iSortIdx = 0;
114459114913
memset(&pNew->u, 0, sizeof(pNew->u));
114460114914
for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
114461
- /* TUNING: Multiple by 3.5 for the secondary table lookup */
114462
- pNew->rRun = sSum.a[i].rRun + 18;
114915
+ /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
114916
+ ** of all sub-scans required by the OR-scan. However, due to rounding
114917
+ ** errors, it may be that the cost of the OR-scan is equal to its
114918
+ ** most expensive sub-scan. Add the smallest possible penalty
114919
+ ** (equivalent to multiplying the cost by 1.07) to ensure that
114920
+ ** this does not happen. Otherwise, for WHERE clauses such as the
114921
+ ** following where there is an index on "y":
114922
+ **
114923
+ ** WHERE likelihood(x=?, 0.99) OR y=?
114924
+ **
114925
+ ** the planner may elect to "OR" together a full-table scan and an
114926
+ ** index lookup. And other similarly odd results. */
114927
+ pNew->rRun = sSum.a[i].rRun + 1;
114463114928
pNew->nOut = sSum.a[i].nOut;
114464114929
pNew->prereq = sSum.a[i].prereq;
114465114930
rc = whereLoopInsert(pBuilder, pNew);
114466114931
}
114467114932
}
@@ -114520,11 +114985,11 @@
114520114985
** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
114521114986
**
114522114987
** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
114523114988
** strict. With GROUP BY and DISTINCT the only requirement is that
114524114989
** equivalent rows appear immediately adjacent to one another. GROUP BY
114525
-** and DISTINT do not require rows to appear in any particular order as long
114990
+** and DISTINCT do not require rows to appear in any particular order as long
114526114991
** as equivelent rows are grouped together. Thus for GROUP BY and DISTINCT
114527114992
** the pOrderBy terms can be matched in any order. With ORDER BY, the
114528114993
** pOrderBy terms must be matched in strict left-to-right order.
114529114994
*/
114530114995
static i8 wherePathSatisfiesOrderBy(
@@ -114581,18 +115046,10 @@
114581115046
** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
114582115047
** automatically order-distinct.
114583115048
*/
114584115049
114585115050
assert( pOrderBy!=0 );
114586
-
114587
- /* Sortability of virtual tables is determined by the xBestIndex method
114588
- ** of the virtual table itself */
114589
- if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
114590
- testcase( nLoop>0 ); /* True when outer loops are one-row and match
114591
- ** no ORDER BY terms */
114592
- return pLast->u.vtab.isOrdered;
114593
- }
114594115051
if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
114595115052
114596115053
nOrderBy = pOrderBy->nExpr;
114597115054
testcase( nOrderBy==BMS-1 );
114598115055
if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
@@ -114601,11 +115058,14 @@
114601115058
orderDistinctMask = 0;
114602115059
ready = 0;
114603115060
for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
114604115061
if( iLoop>0 ) ready |= pLoop->maskSelf;
114605115062
pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
114606
- assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
115063
+ if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
115064
+ if( pLoop->u.vtab.isOrdered ) obSat = obDone;
115065
+ break;
115066
+ }
114607115067
iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
114608115068
114609115069
/* Mark off any ORDER BY term X that is a column in the table of
114610115070
** the current loop for which there is term in the WHERE
114611115071
** clause of the form X IS NULL or X=? that reference only outer
@@ -114689,11 +115149,11 @@
114689115149
){
114690115150
isOrderDistinct = 0;
114691115151
}
114692115152
114693115153
/* Find the ORDER BY term that corresponds to the j-th column
114694
- ** of the index and and mark that ORDER BY term off
115154
+ ** of the index and mark that ORDER BY term off
114695115155
*/
114696115156
bOnce = 1;
114697115157
isMatch = 0;
114698115158
for(i=0; bOnce && i<nOrderBy; i++){
114699115159
if( MASKBIT(i) & obSat ) continue;
@@ -114769,10 +115229,40 @@
114769115229
return 0;
114770115230
}
114771115231
return -1;
114772115232
}
114773115233
115234
+
115235
+/*
115236
+** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
115237
+** the planner assumes that the specified pOrderBy list is actually a GROUP
115238
+** BY clause - and so any order that groups rows as required satisfies the
115239
+** request.
115240
+**
115241
+** Normally, in this case it is not possible for the caller to determine
115242
+** whether or not the rows are really being delivered in sorted order, or
115243
+** just in some other order that provides the required grouping. However,
115244
+** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
115245
+** this function may be called on the returned WhereInfo object. It returns
115246
+** true if the rows really will be sorted in the specified order, or false
115247
+** otherwise.
115248
+**
115249
+** For example, assuming:
115250
+**
115251
+** CREATE INDEX i1 ON t1(x, Y);
115252
+**
115253
+** then
115254
+**
115255
+** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
115256
+** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
115257
+*/
115258
+SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
115259
+ assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
115260
+ assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
115261
+ return pWInfo->sorted;
115262
+}
115263
+
114774115264
#ifdef WHERETRACE_ENABLED
114775115265
/* For debugging use only: */
114776115266
static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
114777115267
static char zName[65];
114778115268
int i;
@@ -114780,11 +115270,10 @@
114780115270
if( pLast ) zName[i++] = pLast->cId;
114781115271
zName[i] = 0;
114782115272
return zName;
114783115273
}
114784115274
#endif
114785
-
114786115275
114787115276
/*
114788115277
** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
114789115278
** attempts to find the lowest cost path that visits each WhereLoop
114790115279
** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
@@ -114879,26 +115368,31 @@
114879115368
if( isOrdered<0 ){
114880115369
isOrdered = wherePathSatisfiesOrderBy(pWInfo,
114881115370
pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
114882115371
iLoop, pWLoop, &revMask);
114883115372
if( isOrdered>=0 && isOrdered<nOrderBy ){
114884
- /* TUNING: Estimated cost of sorting is N*log(N).
114885
- ** If the order-by clause has X terms but only the last Y terms
114886
- ** are out of order, then block-sorting will reduce the sorting
114887
- ** cost to N*log(N)*log(Y/X). The log(Y/X) term is computed
114888
- ** by rScale.
114889
- ** TODO: Should the sorting cost get a small multiplier to help
114890
- ** discourage the use of sorting and encourage the use of index
114891
- ** scans instead?
114892
- */
115373
+ /* TUNING: Estimated cost of a full external sort, where N is
115374
+ ** the number of rows to sort is:
115375
+ **
115376
+ ** cost = (3.0 * N * log(N)).
115377
+ **
115378
+ ** Or, if the order-by clause has X terms but only the last Y
115379
+ ** terms are out of order, then block-sorting will reduce the
115380
+ ** sorting cost to:
115381
+ **
115382
+ ** cost = (3.0 * N * log(N)) * (Y/X)
115383
+ **
115384
+ ** The (Y/X) term is implemented using stack variable rScale
115385
+ ** below. */
114893115386
LogEst rScale, rSortCost;
114894
- assert( nOrderBy>0 );
115387
+ assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
114895115388
rScale = sqlite3LogEst((nOrderBy-isOrdered)*100/nOrderBy) - 66;
114896
- rSortCost = nRowEst + estLog(nRowEst) + rScale;
115389
+ rSortCost = nRowEst + estLog(nRowEst) + rScale + 16;
115390
+
114897115391
/* TUNING: The cost of implementing DISTINCT using a B-TREE is
114898
- ** also N*log(N) but it has a larger constant of proportionality.
114899
- ** Multiply by 3.0. */
115392
+ ** similar but with a larger constant of proportionality.
115393
+ ** Multiply by an additional factor of 3.0. */
114900115394
if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
114901115395
rSortCost += 16;
114902115396
}
114903115397
WHERETRACE(0x002,
114904115398
("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
@@ -115062,11 +115556,23 @@
115062115556
}else{
115063115557
pWInfo->nOBSat = pFrom->isOrdered;
115064115558
if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
115065115559
pWInfo->revMask = pFrom->revLoop;
115066115560
}
115561
+ if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
115562
+ && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr
115563
+ ){
115564
+ Bitmask notUsed = 0;
115565
+ int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
115566
+ pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed
115567
+ );
115568
+ assert( pWInfo->sorted==0 );
115569
+ pWInfo->sorted = (nOrder==pWInfo->pOrderBy->nExpr);
115570
+ }
115067115571
}
115572
+
115573
+
115068115574
pWInfo->nRowOut = pFrom->nRow;
115069115575
115070115576
/* Free temporary memory and return success */
115071115577
sqlite3DbFree(db, pSpace);
115072115578
return SQLITE_OK;
@@ -115587,11 +116093,18 @@
115587116093
Index *pIx = pLoop->u.btree.pIndex;
115588116094
int iIndexCur;
115589116095
int op = OP_OpenRead;
115590116096
/* iIdxCur is always set if to a positive value if ONEPASS is possible */
115591116097
assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
115592
- if( pWInfo->okOnePass ){
116098
+ if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
116099
+ && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0
116100
+ ){
116101
+ /* This is one term of an OR-optimization using the PRIMARY KEY of a
116102
+ ** WITHOUT ROWID table. No need for a separate index */
116103
+ iIndexCur = pLevel->iTabCur;
116104
+ op = 0;
116105
+ }else if( pWInfo->okOnePass ){
115593116106
Index *pJ = pTabItem->pTab->pIndex;
115594116107
iIndexCur = iIdxCur;
115595116108
assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
115596116109
while( ALWAYS(pJ) && pJ!=pIx ){
115597116110
iIndexCur++;
@@ -115605,13 +116118,15 @@
115605116118
iIndexCur = pParse->nTab++;
115606116119
}
115607116120
pLevel->iIdxCur = iIndexCur;
115608116121
assert( pIx->pSchema==pTab->pSchema );
115609116122
assert( iIndexCur>=0 );
115610
- sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
115611
- sqlite3VdbeSetP4KeyInfo(pParse, pIx);
115612
- VdbeComment((v, "%s", pIx->zName));
116123
+ if( op ){
116124
+ sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
116125
+ sqlite3VdbeSetP4KeyInfo(pParse, pIx);
116126
+ VdbeComment((v, "%s", pIx->zName));
116127
+ }
115613116128
}
115614116129
if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
115615116130
notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
115616116131
}
115617116132
pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
@@ -123654,10 +124169,32 @@
123654124169
int sz = va_arg(ap, int);
123655124170
int *aProg = va_arg(ap, int*);
123656124171
rc = sqlite3BitvecBuiltinTest(sz, aProg);
123657124172
break;
123658124173
}
124174
+
124175
+ /*
124176
+ ** sqlite3_test_control(FAULT_INSTALL, xCallback)
124177
+ **
124178
+ ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
124179
+ ** if xCallback is not NULL.
124180
+ **
124181
+ ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
124182
+ ** is called immediately after installing the new callback and the return
124183
+ ** value from sqlite3FaultSim(0) becomes the return from
124184
+ ** sqlite3_test_control().
124185
+ */
124186
+ case SQLITE_TESTCTRL_FAULT_INSTALL: {
124187
+ /* MSVC is picky about pulling func ptrs from va lists.
124188
+ ** http://support.microsoft.com/kb/47961
124189
+ ** sqlite3Config.xTestCallback = va_arg(ap, int(*)(int));
124190
+ */
124191
+ typedef int(*TESTCALLBACKFUNC_t)(int);
124192
+ sqlite3Config.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
124193
+ rc = sqlite3FaultSim(0);
124194
+ break;
124195
+ }
123659124196
123660124197
/*
123661124198
** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
123662124199
**
123663124200
** Register hooks to call to indicate which malloc() failures
@@ -123964,11 +124501,11 @@
123964124501
** Return 1 if database is read-only or 0 if read/write. Return -1 if
123965124502
** no such database exists.
123966124503
*/
123967124504
SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
123968124505
Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
123969
- return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1;
124506
+ return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
123970124507
}
123971124508
123972124509
/************** End of main.c ************************************************/
123973124510
/************** Begin file notify.c ******************************************/
123974124511
/*
@@ -125084,17 +125621,17 @@
125084125621
char **azColumn; /* column names. malloced */
125085125622
u8 *abNotindexed; /* True for 'notindexed' columns */
125086125623
sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
125087125624
char *zContentTbl; /* content=xxx option, or NULL */
125088125625
char *zLanguageid; /* languageid=xxx option, or NULL */
125089
- u8 bAutoincrmerge; /* True if automerge=1 */
125626
+ int nAutoincrmerge; /* Value configured by 'automerge' */
125090125627
u32 nLeafAdd; /* Number of leaf blocks added this trans */
125091125628
125092125629
/* Precompiled statements used by the implementation. Each of these
125093125630
** statements is run and reset within a single virtual table API call.
125094125631
*/
125095
- sqlite3_stmt *aStmt[37];
125632
+ sqlite3_stmt *aStmt[40];
125096125633
125097125634
char *zReadExprlist;
125098125635
char *zWriteExprlist;
125099125636
125100125637
int nNodeSize; /* Soft limit for node size */
@@ -126512,11 +127049,11 @@
126512127049
p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
126513127050
p->bHasDocsize = (isFts4 && bNoDocsize==0);
126514127051
p->bHasStat = isFts4;
126515127052
p->bFts4 = isFts4;
126516127053
p->bDescIdx = bDescIdx;
126517
- p->bAutoincrmerge = 0xff; /* 0xff means setting unknown */
127054
+ p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
126518127055
p->zContentTbl = zContent;
126519127056
p->zLanguageid = zLanguageid;
126520127057
zContent = 0;
126521127058
zLanguageid = 0;
126522127059
TESTONLY( p->inTransaction = -1 );
@@ -126555,11 +127092,13 @@
126555127092
/* Fill in the abNotindexed array */
126556127093
for(iCol=0; iCol<nCol; iCol++){
126557127094
int n = (int)strlen(p->azColumn[iCol]);
126558127095
for(i=0; i<nNotindexed; i++){
126559127096
char *zNot = azNotindexed[i];
126560
- if( zNot && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n) ){
127097
+ if( zNot && n==(int)strlen(zNot)
127098
+ && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
127099
+ ){
126561127100
p->abNotindexed[iCol] = 1;
126562127101
sqlite3_free(zNot);
126563127102
azNotindexed[i] = 0;
126564127103
}
126565127104
}
@@ -128481,19 +129020,22 @@
128481129020
const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
128482129021
128483129022
Fts3Table *p = (Fts3Table*)pVtab;
128484129023
int rc = sqlite3Fts3PendingTermsFlush(p);
128485129024
128486
- if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
129025
+ if( rc==SQLITE_OK
129026
+ && p->nLeafAdd>(nMinMerge/16)
129027
+ && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
129028
+ ){
128487129029
int mxLevel = 0; /* Maximum relative level value in db */
128488129030
int A; /* Incr-merge parameter A */
128489129031
128490129032
rc = sqlite3Fts3MaxLevel(p, &mxLevel);
128491129033
assert( rc==SQLITE_OK || mxLevel==0 );
128492129034
A = p->nLeafAdd * mxLevel;
128493129035
A += (A/2);
128494
- if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
129036
+ if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
128495129037
}
128496129038
sqlite3Fts3SegmentsClose(p);
128497129039
return rc;
128498129040
}
128499129041
@@ -131712,44 +132254,27 @@
131712132254
sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
131713132255
sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
131714132256
int rc;
131715132257
sqlite3_tokenizer_cursor *pCursor;
131716132258
Fts3Expr *pRet = 0;
131717
- int nConsumed = 0;
132259
+ int i = 0;
131718132260
131719
- rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
132261
+ /* Set variable i to the maximum number of bytes of input to tokenize. */
132262
+ for(i=0; i<n; i++){
132263
+ if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
132264
+ if( z[i]=='*' || z[i]=='"' ) break;
132265
+ }
132266
+
132267
+ *pnConsumed = i;
132268
+ rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
131720132269
if( rc==SQLITE_OK ){
131721132270
const char *zToken;
131722132271
int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
131723132272
int nByte; /* total space to allocate */
131724132273
131725132274
rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
131726
-
131727
- if( (rc==SQLITE_OK || rc==SQLITE_DONE) && sqlite3_fts3_enable_parentheses ){
131728
- int i;
131729
- if( rc==SQLITE_DONE ) iStart = n;
131730
- for(i=0; i<iStart; i++){
131731
- if( z[i]=='(' ){
131732
- pParse->nNest++;
131733
- rc = fts3ExprParse(pParse, &z[i+1], n-i-1, &pRet, &nConsumed);
131734
- if( rc==SQLITE_OK && !pRet ){
131735
- rc = SQLITE_DONE;
131736
- }
131737
- nConsumed = (int)(i + 1 + nConsumed);
131738
- break;
131739
- }
131740
-
131741
- if( z[i]==')' ){
131742
- rc = SQLITE_DONE;
131743
- pParse->nNest--;
131744
- nConsumed = i+1;
131745
- break;
131746
- }
131747
- }
131748
- }
131749
-
131750
- if( nConsumed==0 && rc==SQLITE_OK ){
132275
+ if( rc==SQLITE_OK ){
131751132276
nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
131752132277
pRet = (Fts3Expr *)fts3MallocZero(nByte);
131753132278
if( !pRet ){
131754132279
rc = SQLITE_NOMEM;
131755132280
}else{
@@ -131779,17 +132304,18 @@
131779132304
break;
131780132305
}
131781132306
}
131782132307
131783132308
}
131784
- nConsumed = iEnd;
132309
+ *pnConsumed = iEnd;
132310
+ }else if( i && rc==SQLITE_DONE ){
132311
+ rc = SQLITE_OK;
131785132312
}
131786132313
131787132314
pModule->xClose(pCursor);
131788132315
}
131789132316
131790
- *pnConsumed = nConsumed;
131791132317
*ppExpr = pRet;
131792132318
return rc;
131793132319
}
131794132320
131795132321
@@ -132035,10 +132561,25 @@
132035132561
return SQLITE_ERROR;
132036132562
}
132037132563
return getNextString(pParse, &zInput[1], ii-1, ppExpr);
132038132564
}
132039132565
132566
+ if( sqlite3_fts3_enable_parentheses ){
132567
+ if( *zInput=='(' ){
132568
+ int nConsumed = 0;
132569
+ pParse->nNest++;
132570
+ rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
132571
+ if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; }
132572
+ *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
132573
+ return rc;
132574
+ }else if( *zInput==')' ){
132575
+ pParse->nNest--;
132576
+ *pnConsumed = (int)((zInput - z) + 1);
132577
+ *ppExpr = 0;
132578
+ return SQLITE_DONE;
132579
+ }
132580
+ }
132040132581
132041132582
/* If control flows to this point, this must be a regular token, or
132042132583
** the end of the input. Read a regular token using the sqlite3_tokenizer
132043132584
** interface. Before doing so, figure out if there is an explicit
132044132585
** column specifier for the token.
@@ -132153,100 +132694,104 @@
132153132694
int isRequirePhrase = 1;
132154132695
132155132696
while( rc==SQLITE_OK ){
132156132697
Fts3Expr *p = 0;
132157132698
int nByte = 0;
132158
- rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
132159
- if( rc==SQLITE_OK ){
132160
- int isPhrase;
132161
-
132162
- if( !sqlite3_fts3_enable_parentheses
132163
- && p->eType==FTSQUERY_PHRASE && pParse->isNot
132164
- ){
132165
- /* Create an implicit NOT operator. */
132166
- Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
132167
- if( !pNot ){
132168
- sqlite3Fts3ExprFree(p);
132169
- rc = SQLITE_NOMEM;
132170
- goto exprparse_out;
132171
- }
132172
- pNot->eType = FTSQUERY_NOT;
132173
- pNot->pRight = p;
132174
- p->pParent = pNot;
132175
- if( pNotBranch ){
132176
- pNot->pLeft = pNotBranch;
132177
- pNotBranch->pParent = pNot;
132178
- }
132179
- pNotBranch = pNot;
132180
- p = pPrev;
132181
- }else{
132182
- int eType = p->eType;
132183
- isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
132184
-
132185
- /* The isRequirePhrase variable is set to true if a phrase or
132186
- ** an expression contained in parenthesis is required. If a
132187
- ** binary operator (AND, OR, NOT or NEAR) is encounted when
132188
- ** isRequirePhrase is set, this is a syntax error.
132189
- */
132190
- if( !isPhrase && isRequirePhrase ){
132191
- sqlite3Fts3ExprFree(p);
132192
- rc = SQLITE_ERROR;
132193
- goto exprparse_out;
132194
- }
132195
-
132196
- if( isPhrase && !isRequirePhrase ){
132197
- /* Insert an implicit AND operator. */
132198
- Fts3Expr *pAnd;
132199
- assert( pRet && pPrev );
132200
- pAnd = fts3MallocZero(sizeof(Fts3Expr));
132201
- if( !pAnd ){
132202
- sqlite3Fts3ExprFree(p);
132203
- rc = SQLITE_NOMEM;
132204
- goto exprparse_out;
132205
- }
132206
- pAnd->eType = FTSQUERY_AND;
132207
- insertBinaryOperator(&pRet, pPrev, pAnd);
132208
- pPrev = pAnd;
132209
- }
132210
-
132211
- /* This test catches attempts to make either operand of a NEAR
132212
- ** operator something other than a phrase. For example, either of
132213
- ** the following:
132214
- **
132215
- ** (bracketed expression) NEAR phrase
132216
- ** phrase NEAR (bracketed expression)
132217
- **
132218
- ** Return an error in either case.
132219
- */
132220
- if( pPrev && (
132221
- (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
132222
- || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
132223
- )){
132224
- sqlite3Fts3ExprFree(p);
132225
- rc = SQLITE_ERROR;
132226
- goto exprparse_out;
132227
- }
132228
-
132229
- if( isPhrase ){
132230
- if( pRet ){
132231
- assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
132232
- pPrev->pRight = p;
132233
- p->pParent = pPrev;
132234
- }else{
132235
- pRet = p;
132236
- }
132237
- }else{
132238
- insertBinaryOperator(&pRet, pPrev, p);
132239
- }
132240
- isRequirePhrase = !isPhrase;
132699
+
132700
+ rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
132701
+ assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
132702
+ if( rc==SQLITE_OK ){
132703
+ if( p ){
132704
+ int isPhrase;
132705
+
132706
+ if( !sqlite3_fts3_enable_parentheses
132707
+ && p->eType==FTSQUERY_PHRASE && pParse->isNot
132708
+ ){
132709
+ /* Create an implicit NOT operator. */
132710
+ Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
132711
+ if( !pNot ){
132712
+ sqlite3Fts3ExprFree(p);
132713
+ rc = SQLITE_NOMEM;
132714
+ goto exprparse_out;
132715
+ }
132716
+ pNot->eType = FTSQUERY_NOT;
132717
+ pNot->pRight = p;
132718
+ p->pParent = pNot;
132719
+ if( pNotBranch ){
132720
+ pNot->pLeft = pNotBranch;
132721
+ pNotBranch->pParent = pNot;
132722
+ }
132723
+ pNotBranch = pNot;
132724
+ p = pPrev;
132725
+ }else{
132726
+ int eType = p->eType;
132727
+ isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
132728
+
132729
+ /* The isRequirePhrase variable is set to true if a phrase or
132730
+ ** an expression contained in parenthesis is required. If a
132731
+ ** binary operator (AND, OR, NOT or NEAR) is encounted when
132732
+ ** isRequirePhrase is set, this is a syntax error.
132733
+ */
132734
+ if( !isPhrase && isRequirePhrase ){
132735
+ sqlite3Fts3ExprFree(p);
132736
+ rc = SQLITE_ERROR;
132737
+ goto exprparse_out;
132738
+ }
132739
+
132740
+ if( isPhrase && !isRequirePhrase ){
132741
+ /* Insert an implicit AND operator. */
132742
+ Fts3Expr *pAnd;
132743
+ assert( pRet && pPrev );
132744
+ pAnd = fts3MallocZero(sizeof(Fts3Expr));
132745
+ if( !pAnd ){
132746
+ sqlite3Fts3ExprFree(p);
132747
+ rc = SQLITE_NOMEM;
132748
+ goto exprparse_out;
132749
+ }
132750
+ pAnd->eType = FTSQUERY_AND;
132751
+ insertBinaryOperator(&pRet, pPrev, pAnd);
132752
+ pPrev = pAnd;
132753
+ }
132754
+
132755
+ /* This test catches attempts to make either operand of a NEAR
132756
+ ** operator something other than a phrase. For example, either of
132757
+ ** the following:
132758
+ **
132759
+ ** (bracketed expression) NEAR phrase
132760
+ ** phrase NEAR (bracketed expression)
132761
+ **
132762
+ ** Return an error in either case.
132763
+ */
132764
+ if( pPrev && (
132765
+ (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
132766
+ || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
132767
+ )){
132768
+ sqlite3Fts3ExprFree(p);
132769
+ rc = SQLITE_ERROR;
132770
+ goto exprparse_out;
132771
+ }
132772
+
132773
+ if( isPhrase ){
132774
+ if( pRet ){
132775
+ assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
132776
+ pPrev->pRight = p;
132777
+ p->pParent = pPrev;
132778
+ }else{
132779
+ pRet = p;
132780
+ }
132781
+ }else{
132782
+ insertBinaryOperator(&pRet, pPrev, p);
132783
+ }
132784
+ isRequirePhrase = !isPhrase;
132785
+ }
132786
+ pPrev = p;
132241132787
}
132242132788
assert( nByte>0 );
132243132789
}
132244132790
assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
132245132791
nIn -= nByte;
132246132792
zIn += nByte;
132247
- pPrev = p;
132248132793
}
132249132794
132250132795
if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
132251132796
rc = SQLITE_ERROR;
132252132797
}
@@ -135230,10 +135775,11 @@
135230135775
int nMalloc; /* Size of malloc'd buffer at zMalloc */
135231135776
char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
135232135777
int nSize; /* Size of allocation at aData */
135233135778
int nData; /* Bytes of data in aData */
135234135779
char *aData; /* Pointer to block from malloc() */
135780
+ i64 nLeafData; /* Number of bytes of leaf data written */
135235135781
};
135236135782
135237135783
/*
135238135784
** Type SegmentNode is used by the following three functions to create
135239135785
** the interior part of the segment b+-tree structures (everything except
@@ -135304,10 +135850,14 @@
135304135850
#define SQL_SELECT_SEGDIR 32
135305135851
#define SQL_CHOMP_SEGDIR 33
135306135852
#define SQL_SEGMENT_IS_APPENDABLE 34
135307135853
#define SQL_SELECT_INDEXES 35
135308135854
#define SQL_SELECT_MXLEVEL 36
135855
+
135856
+#define SQL_SELECT_LEVEL_RANGE2 37
135857
+#define SQL_UPDATE_LEVEL_IDX 38
135858
+#define SQL_UPDATE_LEVEL 39
135309135859
135310135860
/*
135311135861
** This function is used to obtain an SQLite prepared statement handle
135312135862
** for the statement identified by the second argument. If successful,
135313135863
** *pp is set to the requested statement handle and SQLITE_OK returned.
@@ -135406,11 +135956,22 @@
135406135956
** Return the list of valid segment indexes for absolute level ? */
135407135957
/* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
135408135958
135409135959
/* SQL_SELECT_MXLEVEL
135410135960
** Return the largest relative level in the FTS index or indexes. */
135411
-/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
135961
+/* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
135962
+
135963
+ /* Return segments in order from oldest to newest.*/
135964
+/* 37 */ "SELECT level, idx, end_block "
135965
+ "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
135966
+ "ORDER BY level DESC, idx ASC",
135967
+
135968
+ /* Update statements used while promoting segments */
135969
+/* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
135970
+ "WHERE level=? AND idx=?",
135971
+/* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
135972
+
135412135973
};
135413135974
int rc = SQLITE_OK;
135414135975
sqlite3_stmt *pStmt;
135415135976
135416135977
assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
@@ -136947,10 +137508,11 @@
136947137508
sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
136948137509
int iIdx, /* Value for "idx" field */
136949137510
sqlite3_int64 iStartBlock, /* Value for "start_block" field */
136950137511
sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
136951137512
sqlite3_int64 iEndBlock, /* Value for "end_block" field */
137513
+ sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
136952137514
char *zRoot, /* Blob value for "root" field */
136953137515
int nRoot /* Number of bytes in buffer zRoot */
136954137516
){
136955137517
sqlite3_stmt *pStmt;
136956137518
int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
@@ -136957,11 +137519,17 @@
136957137519
if( rc==SQLITE_OK ){
136958137520
sqlite3_bind_int64(pStmt, 1, iLevel);
136959137521
sqlite3_bind_int(pStmt, 2, iIdx);
136960137522
sqlite3_bind_int64(pStmt, 3, iStartBlock);
136961137523
sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
136962
- sqlite3_bind_int64(pStmt, 5, iEndBlock);
137524
+ if( nLeafData==0 ){
137525
+ sqlite3_bind_int64(pStmt, 5, iEndBlock);
137526
+ }else{
137527
+ char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
137528
+ if( !zEnd ) return SQLITE_NOMEM;
137529
+ sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
137530
+ }
136963137531
sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
136964137532
sqlite3_step(pStmt);
136965137533
rc = sqlite3_reset(pStmt);
136966137534
}
136967137535
return rc;
@@ -137282,10 +137850,13 @@
137282137850
sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
137283137851
nTerm + /* Term suffix */
137284137852
sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
137285137853
nDoclist; /* Doclist data */
137286137854
}
137855
+
137856
+ /* Increase the total number of bytes written to account for the new entry. */
137857
+ pWriter->nLeafData += nReq;
137287137858
137288137859
/* If the buffer currently allocated is too small for this entry, realloc
137289137860
** the buffer to make it large enough.
137290137861
*/
137291137862
if( nReq>pWriter->nSize ){
@@ -137354,17 +137925,17 @@
137354137925
if( rc==SQLITE_OK ){
137355137926
rc = fts3NodeWrite(p, pWriter->pTree, 1,
137356137927
pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
137357137928
}
137358137929
if( rc==SQLITE_OK ){
137359
- rc = fts3WriteSegdir(
137360
- p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
137930
+ rc = fts3WriteSegdir(p, iLevel, iIdx,
137931
+ pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
137361137932
}
137362137933
}else{
137363137934
/* The entire tree fits on the root node. Write it to the segdir table. */
137364
- rc = fts3WriteSegdir(
137365
- p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
137935
+ rc = fts3WriteSegdir(p, iLevel, iIdx,
137936
+ 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
137366137937
}
137367137938
p->nLeafAdd++;
137368137939
return rc;
137369137940
}
137370137941
@@ -137443,10 +138014,41 @@
137443138014
if( SQLITE_ROW==sqlite3_step(pStmt) ){
137444138015
*pnMax = sqlite3_column_int64(pStmt, 0);
137445138016
}
137446138017
return sqlite3_reset(pStmt);
137447138018
}
138019
+
138020
+/*
138021
+** iAbsLevel is an absolute level that may be assumed to exist within
138022
+** the database. This function checks if it is the largest level number
138023
+** within its index. Assuming no error occurs, *pbMax is set to 1 if
138024
+** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
138025
+** is returned. If an error occurs, an error code is returned and the
138026
+** final value of *pbMax is undefined.
138027
+*/
138028
+static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
138029
+
138030
+ /* Set pStmt to the compiled version of:
138031
+ **
138032
+ ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
138033
+ **
138034
+ ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
138035
+ */
138036
+ sqlite3_stmt *pStmt;
138037
+ int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
138038
+ if( rc!=SQLITE_OK ) return rc;
138039
+ sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
138040
+ sqlite3_bind_int64(pStmt, 2,
138041
+ ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
138042
+ );
138043
+
138044
+ *pbMax = 0;
138045
+ if( SQLITE_ROW==sqlite3_step(pStmt) ){
138046
+ *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
138047
+ }
138048
+ return sqlite3_reset(pStmt);
138049
+}
137448138050
137449138051
/*
137450138052
** Delete all entries in the %_segments table associated with the segment
137451138053
** opened with seg-reader pSeg. This function does not affect the contents
137452138054
** of the %_segdir table.
@@ -137978,10 +138580,144 @@
137978138580
pCsr->nSegment = 0;
137979138581
pCsr->apSegment = 0;
137980138582
pCsr->aBuffer = 0;
137981138583
}
137982138584
}
138585
+
138586
+/*
138587
+** Decode the "end_block" field, selected by column iCol of the SELECT
138588
+** statement passed as the first argument.
138589
+**
138590
+** The "end_block" field may contain either an integer, or a text field
138591
+** containing the text representation of two non-negative integers separated
138592
+** by one or more space (0x20) characters. In the first case, set *piEndBlock
138593
+** to the integer value and *pnByte to zero before returning. In the second,
138594
+** set *piEndBlock to the first value and *pnByte to the second.
138595
+*/
138596
+static void fts3ReadEndBlockField(
138597
+ sqlite3_stmt *pStmt,
138598
+ int iCol,
138599
+ i64 *piEndBlock,
138600
+ i64 *pnByte
138601
+){
138602
+ const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
138603
+ if( zText ){
138604
+ int i;
138605
+ int iMul = 1;
138606
+ i64 iVal = 0;
138607
+ for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
138608
+ iVal = iVal*10 + (zText[i] - '0');
138609
+ }
138610
+ *piEndBlock = iVal;
138611
+ while( zText[i]==' ' ) i++;
138612
+ iVal = 0;
138613
+ if( zText[i]=='-' ){
138614
+ i++;
138615
+ iMul = -1;
138616
+ }
138617
+ for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
138618
+ iVal = iVal*10 + (zText[i] - '0');
138619
+ }
138620
+ *pnByte = (iVal * (i64)iMul);
138621
+ }
138622
+}
138623
+
138624
+
138625
+/*
138626
+** A segment of size nByte bytes has just been written to absolute level
138627
+** iAbsLevel. Promote any segments that should be promoted as a result.
138628
+*/
138629
+static int fts3PromoteSegments(
138630
+ Fts3Table *p, /* FTS table handle */
138631
+ sqlite3_int64 iAbsLevel, /* Absolute level just updated */
138632
+ sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
138633
+){
138634
+ int rc = SQLITE_OK;
138635
+ sqlite3_stmt *pRange;
138636
+
138637
+ rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
138638
+
138639
+ if( rc==SQLITE_OK ){
138640
+ int bOk = 0;
138641
+ i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
138642
+ i64 nLimit = (nByte*3)/2;
138643
+
138644
+ /* Loop through all entries in the %_segdir table corresponding to
138645
+ ** segments in this index on levels greater than iAbsLevel. If there is
138646
+ ** at least one such segment, and it is possible to determine that all
138647
+ ** such segments are smaller than nLimit bytes in size, they will be
138648
+ ** promoted to level iAbsLevel. */
138649
+ sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
138650
+ sqlite3_bind_int64(pRange, 2, iLast);
138651
+ while( SQLITE_ROW==sqlite3_step(pRange) ){
138652
+ i64 nSize = 0, dummy;
138653
+ fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
138654
+ if( nSize<=0 || nSize>nLimit ){
138655
+ /* If nSize==0, then the %_segdir.end_block field does not not
138656
+ ** contain a size value. This happens if it was written by an
138657
+ ** old version of FTS. In this case it is not possible to determine
138658
+ ** the size of the segment, and so segment promotion does not
138659
+ ** take place. */
138660
+ bOk = 0;
138661
+ break;
138662
+ }
138663
+ bOk = 1;
138664
+ }
138665
+ rc = sqlite3_reset(pRange);
138666
+
138667
+ if( bOk ){
138668
+ int iIdx = 0;
138669
+ sqlite3_stmt *pUpdate1;
138670
+ sqlite3_stmt *pUpdate2;
138671
+
138672
+ if( rc==SQLITE_OK ){
138673
+ rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
138674
+ }
138675
+ if( rc==SQLITE_OK ){
138676
+ rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
138677
+ }
138678
+
138679
+ if( rc==SQLITE_OK ){
138680
+
138681
+ /* Loop through all %_segdir entries for segments in this index with
138682
+ ** levels equal to or greater than iAbsLevel. As each entry is visited,
138683
+ ** updated it to set (level = -1) and (idx = N), where N is 0 for the
138684
+ ** oldest segment in the range, 1 for the next oldest, and so on.
138685
+ **
138686
+ ** In other words, move all segments being promoted to level -1,
138687
+ ** setting the "idx" fields as appropriate to keep them in the same
138688
+ ** order. The contents of level -1 (which is never used, except
138689
+ ** transiently here), will be moved back to level iAbsLevel below. */
138690
+ sqlite3_bind_int64(pRange, 1, iAbsLevel);
138691
+ while( SQLITE_ROW==sqlite3_step(pRange) ){
138692
+ sqlite3_bind_int(pUpdate1, 1, iIdx++);
138693
+ sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
138694
+ sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
138695
+ sqlite3_step(pUpdate1);
138696
+ rc = sqlite3_reset(pUpdate1);
138697
+ if( rc!=SQLITE_OK ){
138698
+ sqlite3_reset(pRange);
138699
+ break;
138700
+ }
138701
+ }
138702
+ }
138703
+ if( rc==SQLITE_OK ){
138704
+ rc = sqlite3_reset(pRange);
138705
+ }
138706
+
138707
+ /* Move level -1 to level iAbsLevel */
138708
+ if( rc==SQLITE_OK ){
138709
+ sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
138710
+ sqlite3_step(pUpdate2);
138711
+ rc = sqlite3_reset(pUpdate2);
138712
+ }
138713
+ }
138714
+ }
138715
+
138716
+
138717
+ return rc;
138718
+}
137983138719
137984138720
/*
137985138721
** Merge all level iLevel segments in the database into a single
137986138722
** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
137987138723
** single segment with a level equal to the numerically largest level
@@ -138003,10 +138739,11 @@
138003138739
sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
138004138740
SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
138005138741
Fts3SegFilter filter; /* Segment term filter condition */
138006138742
Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
138007138743
int bIgnoreEmpty = 0; /* True to ignore empty segments */
138744
+ i64 iMaxLevel = 0; /* Max level number for this index/langid */
138008138745
138009138746
assert( iLevel==FTS3_SEGCURSOR_ALL
138010138747
|| iLevel==FTS3_SEGCURSOR_PENDING
138011138748
|| iLevel>=0
138012138749
);
@@ -138013,10 +138750,15 @@
138013138750
assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
138014138751
assert( iIndex>=0 && iIndex<p->nIndex );
138015138752
138016138753
rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
138017138754
if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
138755
+
138756
+ if( iLevel!=FTS3_SEGCURSOR_PENDING ){
138757
+ rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
138758
+ if( rc!=SQLITE_OK ) goto finished;
138759
+ }
138018138760
138019138761
if( iLevel==FTS3_SEGCURSOR_ALL ){
138020138762
/* This call is to merge all segments in the database to a single
138021138763
** segment. The level of the new segment is equal to the numerically
138022138764
** greatest segment level currently present in the database for this
@@ -138023,25 +138765,25 @@
138023138765
** index. The idx of the new segment is always 0. */
138024138766
if( csr.nSegment==1 ){
138025138767
rc = SQLITE_DONE;
138026138768
goto finished;
138027138769
}
138028
- rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iNewLevel);
138770
+ iNewLevel = iMaxLevel;
138029138771
bIgnoreEmpty = 1;
138030138772
138031
- }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
138032
- iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, 0);
138033
- rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, 0, &iIdx);
138034138773
}else{
138035138774
/* This call is to merge all segments at level iLevel. find the next
138036138775
** available segment index at level iLevel+1. The call to
138037138776
** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
138038138777
** a single iLevel+2 segment if necessary. */
138039
- rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
138778
+ assert( FTS3_SEGCURSOR_PENDING==-1 );
138040138779
iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
138780
+ rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
138781
+ bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
138041138782
}
138042138783
if( rc!=SQLITE_OK ) goto finished;
138784
+
138043138785
assert( csr.nSegment>0 );
138044138786
assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
138045138787
assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
138046138788
138047138789
memset(&filter, 0, sizeof(Fts3SegFilter));
@@ -138054,29 +138796,36 @@
138054138796
if( rc!=SQLITE_ROW ) break;
138055138797
rc = fts3SegWriterAdd(p, &pWriter, 1,
138056138798
csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
138057138799
}
138058138800
if( rc!=SQLITE_OK ) goto finished;
138059
- assert( pWriter );
138801
+ assert( pWriter || bIgnoreEmpty );
138060138802
138061138803
if( iLevel!=FTS3_SEGCURSOR_PENDING ){
138062138804
rc = fts3DeleteSegdir(
138063138805
p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
138064138806
);
138065138807
if( rc!=SQLITE_OK ) goto finished;
138066138808
}
138067
- rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
138809
+ if( pWriter ){
138810
+ rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
138811
+ if( rc==SQLITE_OK ){
138812
+ if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
138813
+ rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
138814
+ }
138815
+ }
138816
+ }
138068138817
138069138818
finished:
138070138819
fts3SegWriterFree(pWriter);
138071138820
sqlite3Fts3SegReaderFinish(&csr);
138072138821
return rc;
138073138822
}
138074138823
138075138824
138076138825
/*
138077
-** Flush the contents of pendingTerms to level 0 segments.
138826
+** Flush the contents of pendingTerms to level 0 segments.
138078138827
*/
138079138828
SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
138080138829
int rc = SQLITE_OK;
138081138830
int i;
138082138831
@@ -138088,18 +138837,23 @@
138088138837
138089138838
/* Determine the auto-incr-merge setting if unknown. If enabled,
138090138839
** estimate the number of leaf blocks of content to be written
138091138840
*/
138092138841
if( rc==SQLITE_OK && p->bHasStat
138093
- && p->bAutoincrmerge==0xff && p->nLeafAdd>0
138842
+ && p->nAutoincrmerge==0xff && p->nLeafAdd>0
138094138843
){
138095138844
sqlite3_stmt *pStmt = 0;
138096138845
rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
138097138846
if( rc==SQLITE_OK ){
138098138847
sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
138099138848
rc = sqlite3_step(pStmt);
138100
- p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
138849
+ if( rc==SQLITE_ROW ){
138850
+ p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
138851
+ if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
138852
+ }else if( rc==SQLITE_DONE ){
138853
+ p->nAutoincrmerge = 0;
138854
+ }
138101138855
rc = sqlite3_reset(pStmt);
138102138856
}
138103138857
}
138104138858
return rc;
138105138859
}
@@ -138463,10 +139217,12 @@
138463139217
int nWork; /* Number of leaf pages flushed */
138464139218
sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
138465139219
int iIdx; /* Index of *output* segment in iAbsLevel+1 */
138466139220
sqlite3_int64 iStart; /* Block number of first allocated block */
138467139221
sqlite3_int64 iEnd; /* Block number of last allocated block */
139222
+ sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
139223
+ u8 bNoLeafData; /* If true, store 0 for segment size */
138468139224
NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
138469139225
};
138470139226
138471139227
/*
138472139228
** An object of the following type is used to read data from a single
@@ -138801,12 +139557,12 @@
138801139557
nSpace = 1;
138802139558
nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
138803139559
nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
138804139560
}
138805139561
139562
+ pWriter->nLeafData += nSpace;
138806139563
blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
138807
-
138808139564
if( rc==SQLITE_OK ){
138809139565
if( pLeaf->block.n==0 ){
138810139566
pLeaf->block.n = 1;
138811139567
pLeaf->block.a[0] = '\0';
138812139568
}
@@ -138901,10 +139657,11 @@
138901139657
pWriter->iAbsLevel+1, /* level */
138902139658
pWriter->iIdx, /* idx */
138903139659
pWriter->iStart, /* start_block */
138904139660
pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
138905139661
pWriter->iEnd, /* end_block */
139662
+ (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
138906139663
pRoot->block.a, pRoot->block.n /* root */
138907139664
);
138908139665
}
138909139666
sqlite3_free(pRoot->block.a);
138910139667
sqlite3_free(pRoot->key.a);
@@ -139002,11 +139759,15 @@
139002139759
sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
139003139760
sqlite3_bind_int(pSelect, 2, iIdx);
139004139761
if( sqlite3_step(pSelect)==SQLITE_ROW ){
139005139762
iStart = sqlite3_column_int64(pSelect, 1);
139006139763
iLeafEnd = sqlite3_column_int64(pSelect, 2);
139007
- iEnd = sqlite3_column_int64(pSelect, 3);
139764
+ fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
139765
+ if( pWriter->nLeafData<0 ){
139766
+ pWriter->nLeafData = pWriter->nLeafData * -1;
139767
+ }
139768
+ pWriter->bNoLeafData = (pWriter->nLeafData==0);
139008139769
nRoot = sqlite3_column_bytes(pSelect, 4);
139009139770
aRoot = sqlite3_column_blob(pSelect, 4);
139010139771
}else{
139011139772
return sqlite3_reset(pSelect);
139012139773
}
@@ -139603,15 +140364,15 @@
139603140364
139604140365
139605140366
/*
139606140367
** Attempt an incremental merge that writes nMerge leaf blocks.
139607140368
**
139608
-** Incremental merges happen nMin segments at a time. The two
139609
-** segments to be merged are the nMin oldest segments (the ones with
139610
-** the smallest indexes) in the highest level that contains at least
139611
-** nMin segments. Multiple merges might occur in an attempt to write the
139612
-** quota of nMerge leaf blocks.
140369
+** Incremental merges happen nMin segments at a time. The segments
140370
+** to be merged are the nMin oldest segments (the ones with the smallest
140371
+** values for the _segdir.idx field) in the highest level that contains
140372
+** at least nMin segments. Multiple merges might occur in an attempt to
140373
+** write the quota of nMerge leaf blocks.
139613140374
*/
139614140375
SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
139615140376
int rc; /* Return code */
139616140377
int nRem = nMerge; /* Number of leaf pages yet to be written */
139617140378
Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
@@ -139632,10 +140393,11 @@
139632140393
rc = fts3IncrmergeHintLoad(p, &hint);
139633140394
while( rc==SQLITE_OK && nRem>0 ){
139634140395
const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
139635140396
sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
139636140397
int bUseHint = 0; /* True if attempting to append */
140398
+ int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
139637140399
139638140400
/* Search the %_segdir table for the absolute level with the smallest
139639140401
** relative level number that contains at least nMin segments, if any.
139640140402
** If one is found, set iAbsLevel to the absolute level number and
139641140403
** nSeg to nMin. If no level with at least nMin segments can be found,
@@ -139685,27 +140447,36 @@
139685140447
** segments available in level iAbsLevel. In this case, no work is
139686140448
** done on iAbsLevel - fall through to the next iteration of the loop
139687140449
** to start work on some other level. */
139688140450
memset(pWriter, 0, nAlloc);
139689140451
pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
140452
+
140453
+ if( rc==SQLITE_OK ){
140454
+ rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
140455
+ assert( bUseHint==1 || bUseHint==0 );
140456
+ if( iIdx==0 || (bUseHint && iIdx==1) ){
140457
+ int bIgnore = 0;
140458
+ rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
140459
+ if( bIgnore ){
140460
+ pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
140461
+ }
140462
+ }
140463
+ }
140464
+
139690140465
if( rc==SQLITE_OK ){
139691140466
rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
139692140467
}
139693140468
if( SQLITE_OK==rc && pCsr->nSegment==nSeg
139694140469
&& SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
139695140470
&& SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
139696140471
){
139697
- int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
139698
- rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
139699
- if( rc==SQLITE_OK ){
139700
- if( bUseHint && iIdx>0 ){
139701
- const char *zKey = pCsr->zTerm;
139702
- int nKey = pCsr->nTerm;
139703
- rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
139704
- }else{
139705
- rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
139706
- }
140472
+ if( bUseHint && iIdx>0 ){
140473
+ const char *zKey = pCsr->zTerm;
140474
+ int nKey = pCsr->nTerm;
140475
+ rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
140476
+ }else{
140477
+ rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
139707140478
}
139708140479
139709140480
if( rc==SQLITE_OK && pWriter->nLeafEst ){
139710140481
fts3LogMerge(nSeg, iAbsLevel);
139711140482
do {
@@ -139723,11 +140494,17 @@
139723140494
fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
139724140495
}
139725140496
}
139726140497
}
139727140498
140499
+ if( nSeg!=0 ){
140500
+ pWriter->nLeafData = pWriter->nLeafData * -1;
140501
+ }
139728140502
fts3IncrmergeRelease(p, pWriter, &rc);
140503
+ if( nSeg==0 && pWriter->bNoLeafData==0 ){
140504
+ fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
140505
+ }
139729140506
}
139730140507
139731140508
sqlite3Fts3SegReaderFinish(pCsr);
139732140509
}
139733140510
@@ -139810,20 +140587,23 @@
139810140587
Fts3Table *p, /* FTS3 table handle */
139811140588
const char *zParam /* Nul-terminated string containing boolean */
139812140589
){
139813140590
int rc = SQLITE_OK;
139814140591
sqlite3_stmt *pStmt = 0;
139815
- p->bAutoincrmerge = fts3Getint(&zParam)!=0;
140592
+ p->nAutoincrmerge = fts3Getint(&zParam);
140593
+ if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
140594
+ p->nAutoincrmerge = 8;
140595
+ }
139816140596
if( !p->bHasStat ){
139817140597
assert( p->bFts4==0 );
139818140598
sqlite3Fts3CreateStatTable(&rc, p);
139819140599
if( rc ) return rc;
139820140600
}
139821140601
rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
139822140602
if( rc ) return rc;
139823140603
sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
139824
- sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
140604
+ sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
139825140605
sqlite3_step(pStmt);
139826140606
rc = sqlite3_reset(pStmt);
139827140607
return rc;
139828140608
}
139829140609
@@ -142802,64 +143582,24 @@
142802143582
** child page.
142803143583
*/
142804143584
142805143585
#if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
142806143586
142807
-/*
142808
-** This file contains an implementation of a couple of different variants
142809
-** of the r-tree algorithm. See the README file for further details. The
142810
-** same data-structure is used for all, but the algorithms for insert and
142811
-** delete operations vary. The variants used are selected at compile time
142812
-** by defining the following symbols:
142813
-*/
142814
-
142815
-/* Either, both or none of the following may be set to activate
142816
-** r*tree variant algorithms.
142817
-*/
142818
-#define VARIANT_RSTARTREE_CHOOSESUBTREE 0
142819
-#define VARIANT_RSTARTREE_REINSERT 1
142820
-
142821
-/*
142822
-** Exactly one of the following must be set to 1.
142823
-*/
142824
-#define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
142825
-#define VARIANT_GUTTMAN_LINEAR_SPLIT 0
142826
-#define VARIANT_RSTARTREE_SPLIT 1
142827
-
142828
-#define VARIANT_GUTTMAN_SPLIT \
142829
- (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
142830
-
142831
-#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
142832
- #define PickNext QuadraticPickNext
142833
- #define PickSeeds QuadraticPickSeeds
142834
- #define AssignCells splitNodeGuttman
142835
-#endif
142836
-#if VARIANT_GUTTMAN_LINEAR_SPLIT
142837
- #define PickNext LinearPickNext
142838
- #define PickSeeds LinearPickSeeds
142839
- #define AssignCells splitNodeGuttman
142840
-#endif
142841
-#if VARIANT_RSTARTREE_SPLIT
142842
- #define AssignCells splitNodeStartree
142843
-#endif
142844
-
142845
-#if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
142846
-# define NDEBUG 1
142847
-#endif
142848
-
142849143587
#ifndef SQLITE_CORE
142850143588
SQLITE_EXTENSION_INIT1
142851143589
#else
142852143590
#endif
142853143591
142854143592
/* #include <string.h> */
142855143593
/* #include <assert.h> */
143594
+/* #include <stdio.h> */
142856143595
142857143596
#ifndef SQLITE_AMALGAMATION
142858143597
#include "sqlite3rtree.h"
142859143598
typedef sqlite3_int64 i64;
142860143599
typedef unsigned char u8;
143600
+typedef unsigned short u16;
142861143601
typedef unsigned int u32;
142862143602
#endif
142863143603
142864143604
/* The following macro is used to suppress compiler warnings.
142865143605
*/
@@ -142873,19 +143613,20 @@
142873143613
typedef struct RtreeCell RtreeCell;
142874143614
typedef struct RtreeConstraint RtreeConstraint;
142875143615
typedef struct RtreeMatchArg RtreeMatchArg;
142876143616
typedef struct RtreeGeomCallback RtreeGeomCallback;
142877143617
typedef union RtreeCoord RtreeCoord;
143618
+typedef struct RtreeSearchPoint RtreeSearchPoint;
142878143619
142879143620
/* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
142880143621
#define RTREE_MAX_DIMENSIONS 5
142881143622
142882143623
/* Size of hash table Rtree.aHash. This hash table is not expected to
142883143624
** ever contain very many entries, so a fixed number of buckets is
142884143625
** used.
142885143626
*/
142886
-#define HASHSIZE 128
143627
+#define HASHSIZE 97
142887143628
142888143629
/* The xBestIndex method of this virtual table requires an estimate of
142889143630
** the number of rows in the virtual table to calculate the costs of
142890143631
** various strategies. If possible, this estimate is loaded from the
142891143632
** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
@@ -142897,19 +143638,19 @@
142897143638
142898143639
/*
142899143640
** An rtree virtual-table object.
142900143641
*/
142901143642
struct Rtree {
142902
- sqlite3_vtab base;
143643
+ sqlite3_vtab base; /* Base class. Must be first */
142903143644
sqlite3 *db; /* Host database connection */
142904143645
int iNodeSize; /* Size in bytes of each node in the node table */
142905
- int nDim; /* Number of dimensions */
142906
- int nBytesPerCell; /* Bytes consumed per cell */
143646
+ u8 nDim; /* Number of dimensions */
143647
+ u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
143648
+ u8 nBytesPerCell; /* Bytes consumed per cell */
142907143649
int iDepth; /* Current depth of the r-tree structure */
142908143650
char *zDb; /* Name of database containing r-tree table */
142909143651
char *zName; /* Name of r-tree table */
142910
- RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
142911143652
int nBusy; /* Current number of users of this structure */
142912143653
i64 nRowEst; /* Estimated number of rows in this table */
142913143654
142914143655
/* List of nodes removed during a CondenseTree operation. List is
142915143656
** linked together via the pointer normally used for hash chains -
@@ -142932,14 +143673,14 @@
142932143673
/* Statements to read/write/delete a record from xxx_parent */
142933143674
sqlite3_stmt *pReadParent;
142934143675
sqlite3_stmt *pWriteParent;
142935143676
sqlite3_stmt *pDeleteParent;
142936143677
142937
- int eCoordType;
143678
+ RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
142938143679
};
142939143680
142940
-/* Possible values for eCoordType: */
143681
+/* Possible values for Rtree.eCoordType: */
142941143682
#define RTREE_COORD_REAL32 0
142942143683
#define RTREE_COORD_INT32 1
142943143684
142944143685
/*
142945143686
** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
@@ -142947,14 +143688,33 @@
142947143688
** will be done.
142948143689
*/
142949143690
#ifdef SQLITE_RTREE_INT_ONLY
142950143691
typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
142951143692
typedef int RtreeValue; /* Low accuracy coordinate */
143693
+# define RTREE_ZERO 0
142952143694
#else
142953143695
typedef double RtreeDValue; /* High accuracy coordinate */
142954143696
typedef float RtreeValue; /* Low accuracy coordinate */
143697
+# define RTREE_ZERO 0.0
142955143698
#endif
143699
+
143700
+/*
143701
+** When doing a search of an r-tree, instances of the following structure
143702
+** record intermediate results from the tree walk.
143703
+**
143704
+** The id is always a node-id. For iLevel>=1 the id is the node-id of
143705
+** the node that the RtreeSearchPoint represents. When iLevel==0, however,
143706
+** the id is of the parent node and the cell that RtreeSearchPoint
143707
+** represents is the iCell-th entry in the parent node.
143708
+*/
143709
+struct RtreeSearchPoint {
143710
+ RtreeDValue rScore; /* The score for this node. Smallest goes first. */
143711
+ sqlite3_int64 id; /* Node ID */
143712
+ u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
143713
+ u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
143714
+ u8 iCell; /* Cell index within the node */
143715
+};
142956143716
142957143717
/*
142958143718
** The minimum number of cells allowed for a node is a third of the
142959143719
** maximum. In Gutman's notation:
142960143720
**
@@ -142974,25 +143734,48 @@
142974143734
** 2^40 is greater than 2^64, an r-tree structure always has a depth of
142975143735
** 40 or less.
142976143736
*/
142977143737
#define RTREE_MAX_DEPTH 40
142978143738
143739
+
143740
+/*
143741
+** Number of entries in the cursor RtreeNode cache. The first entry is
143742
+** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
143743
+** entries cache the RtreeNode for the first elements of the priority queue.
143744
+*/
143745
+#define RTREE_CACHE_SZ 5
143746
+
142979143747
/*
142980143748
** An rtree cursor object.
142981143749
*/
142982143750
struct RtreeCursor {
142983
- sqlite3_vtab_cursor base;
142984
- RtreeNode *pNode; /* Node cursor is currently pointing at */
142985
- int iCell; /* Index of current cell in pNode */
143751
+ sqlite3_vtab_cursor base; /* Base class. Must be first */
143752
+ u8 atEOF; /* True if at end of search */
143753
+ u8 bPoint; /* True if sPoint is valid */
142986143754
int iStrategy; /* Copy of idxNum search parameter */
142987143755
int nConstraint; /* Number of entries in aConstraint */
142988143756
RtreeConstraint *aConstraint; /* Search constraints. */
143757
+ int nPointAlloc; /* Number of slots allocated for aPoint[] */
143758
+ int nPoint; /* Number of slots used in aPoint[] */
143759
+ int mxLevel; /* iLevel value for root of the tree */
143760
+ RtreeSearchPoint *aPoint; /* Priority queue for search points */
143761
+ RtreeSearchPoint sPoint; /* Cached next search point */
143762
+ RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
143763
+ u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
142989143764
};
142990143765
143766
+/* Return the Rtree of a RtreeCursor */
143767
+#define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
143768
+
143769
+/*
143770
+** A coordinate can be either a floating point number or a integer. All
143771
+** coordinates within a single R-Tree are always of the same time.
143772
+*/
142991143773
union RtreeCoord {
142992
- RtreeValue f;
142993
- int i;
143774
+ RtreeValue f; /* Floating point value */
143775
+ int i; /* Integer value */
143776
+ u32 u; /* Unsigned for byte-order conversions */
142994143777
};
142995143778
142996143779
/*
142997143780
** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
142998143781
** formatted as a RtreeDValue (double or int64). This macro assumes that local
@@ -143013,42 +143796,71 @@
143013143796
** A search constraint.
143014143797
*/
143015143798
struct RtreeConstraint {
143016143799
int iCoord; /* Index of constrained coordinate */
143017143800
int op; /* Constraining operation */
143018
- RtreeDValue rValue; /* Constraint value. */
143019
- int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
143020
- sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */
143801
+ union {
143802
+ RtreeDValue rValue; /* Constraint value. */
143803
+ int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
143804
+ int (*xQueryFunc)(sqlite3_rtree_query_info*);
143805
+ } u;
143806
+ sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
143021143807
};
143022143808
143023143809
/* Possible values for RtreeConstraint.op */
143024
-#define RTREE_EQ 0x41
143025
-#define RTREE_LE 0x42
143026
-#define RTREE_LT 0x43
143027
-#define RTREE_GE 0x44
143028
-#define RTREE_GT 0x45
143029
-#define RTREE_MATCH 0x46
143810
+#define RTREE_EQ 0x41 /* A */
143811
+#define RTREE_LE 0x42 /* B */
143812
+#define RTREE_LT 0x43 /* C */
143813
+#define RTREE_GE 0x44 /* D */
143814
+#define RTREE_GT 0x45 /* E */
143815
+#define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
143816
+#define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
143817
+
143030143818
143031143819
/*
143032143820
** An rtree structure node.
143033143821
*/
143034143822
struct RtreeNode {
143035
- RtreeNode *pParent; /* Parent node */
143036
- i64 iNode;
143037
- int nRef;
143038
- int isDirty;
143039
- u8 *zData;
143040
- RtreeNode *pNext; /* Next node in this hash chain */
143823
+ RtreeNode *pParent; /* Parent node */
143824
+ i64 iNode; /* The node number */
143825
+ int nRef; /* Number of references to this node */
143826
+ int isDirty; /* True if the node needs to be written to disk */
143827
+ u8 *zData; /* Content of the node, as should be on disk */
143828
+ RtreeNode *pNext; /* Next node in this hash collision chain */
143041143829
};
143830
+
143831
+/* Return the number of cells in a node */
143042143832
#define NCELL(pNode) readInt16(&(pNode)->zData[2])
143043143833
143044143834
/*
143045
-** Structure to store a deserialized rtree record.
143835
+** A single cell from a node, deserialized
143046143836
*/
143047143837
struct RtreeCell {
143048
- i64 iRowid;
143049
- RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
143838
+ i64 iRowid; /* Node or entry ID */
143839
+ RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
143840
+};
143841
+
143842
+
143843
+/*
143844
+** This object becomes the sqlite3_user_data() for the SQL functions
143845
+** that are created by sqlite3_rtree_geometry_callback() and
143846
+** sqlite3_rtree_query_callback() and which appear on the right of MATCH
143847
+** operators in order to constrain a search.
143848
+**
143849
+** xGeom and xQueryFunc are the callback functions. Exactly one of
143850
+** xGeom and xQueryFunc fields is non-NULL, depending on whether the
143851
+** SQL function was created using sqlite3_rtree_geometry_callback() or
143852
+** sqlite3_rtree_query_callback().
143853
+**
143854
+** This object is deleted automatically by the destructor mechanism in
143855
+** sqlite3_create_function_v2().
143856
+*/
143857
+struct RtreeGeomCallback {
143858
+ int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
143859
+ int (*xQueryFunc)(sqlite3_rtree_query_info*);
143860
+ void (*xDestructor)(void*);
143861
+ void *pContext;
143050143862
};
143051143863
143052143864
143053143865
/*
143054143866
** Value for the first field of every RtreeMatchArg object. The MATCH
@@ -143056,33 +143868,20 @@
143056143868
** value to avoid operating on invalid blobs (which could cause a segfault).
143057143869
*/
143058143870
#define RTREE_GEOMETRY_MAGIC 0x891245AB
143059143871
143060143872
/*
143061
-** An instance of this structure must be supplied as a blob argument to
143062
-** the right-hand-side of an SQL MATCH operator used to constrain an
143063
-** r-tree query.
143873
+** An instance of this structure (in the form of a BLOB) is returned by
143874
+** the SQL functions that sqlite3_rtree_geometry_callback() and
143875
+** sqlite3_rtree_query_callback() create, and is read as the right-hand
143876
+** operand to the MATCH operator of an R-Tree.
143064143877
*/
143065143878
struct RtreeMatchArg {
143066
- u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
143067
- int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
143068
- void *pContext;
143069
- int nParam;
143070
- RtreeDValue aParam[1];
143071
-};
143072
-
143073
-/*
143074
-** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
143075
-** a single instance of the following structure is allocated. It is used
143076
-** as the context for the user-function created by by s_r_g_c(). The object
143077
-** is eventually deleted by the destructor mechanism provided by
143078
-** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
143079
-** the geometry callback function).
143080
-*/
143081
-struct RtreeGeomCallback {
143082
- int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
143083
- void *pContext;
143879
+ u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
143880
+ RtreeGeomCallback cb; /* Info about the callback functions */
143881
+ int nParam; /* Number of parameters to the SQL function */
143882
+ RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
143084143883
};
143085143884
143086143885
#ifndef MAX
143087143886
# define MAX(x,y) ((x) < (y) ? (y) : (x))
143088143887
#endif
@@ -143172,14 +143971,11 @@
143172143971
/*
143173143972
** Given a node number iNode, return the corresponding key to use
143174143973
** in the Rtree.aHash table.
143175143974
*/
143176143975
static int nodeHash(i64 iNode){
143177
- return (
143178
- (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^
143179
- (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
143180
- ) % HASHSIZE;
143976
+ return iNode % HASHSIZE;
143181143977
}
143182143978
143183143979
/*
143184143980
** Search the node hash table for node iNode. If found, return a pointer
143185143981
** to it. Otherwise, return 0.
@@ -143235,12 +144031,11 @@
143235144031
}
143236144032
143237144033
/*
143238144034
** Obtain a reference to an r-tree node.
143239144035
*/
143240
-static int
143241
-nodeAcquire(
144036
+static int nodeAcquire(
143242144037
Rtree *pRtree, /* R-tree structure */
143243144038
i64 iNode, /* Node number to load */
143244144039
RtreeNode *pParent, /* Either the parent node or NULL */
143245144040
RtreeNode **ppNode /* OUT: Acquired node */
143246144041
){
@@ -143325,14 +144120,14 @@
143325144120
143326144121
/*
143327144122
** Overwrite cell iCell of node pNode with the contents of pCell.
143328144123
*/
143329144124
static void nodeOverwriteCell(
143330
- Rtree *pRtree,
143331
- RtreeNode *pNode,
143332
- RtreeCell *pCell,
143333
- int iCell
144125
+ Rtree *pRtree, /* The overall R-Tree */
144126
+ RtreeNode *pNode, /* The node into which the cell is to be written */
144127
+ RtreeCell *pCell, /* The cell to write */
144128
+ int iCell /* Index into pNode into which pCell is written */
143334144129
){
143335144130
int ii;
143336144131
u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
143337144132
p += writeInt64(p, pCell->iRowid);
143338144133
for(ii=0; ii<(pRtree->nDim*2); ii++){
@@ -143340,11 +144135,11 @@
143340144135
}
143341144136
pNode->isDirty = 1;
143342144137
}
143343144138
143344144139
/*
143345
-** Remove cell the cell with index iCell from node pNode.
144140
+** Remove the cell with index iCell from node pNode.
143346144141
*/
143347144142
static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
143348144143
u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
143349144144
u8 *pSrc = &pDst[pRtree->nBytesPerCell];
143350144145
int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
@@ -143357,15 +144152,14 @@
143357144152
** Insert the contents of cell pCell into node pNode. If the insert
143358144153
** is successful, return SQLITE_OK.
143359144154
**
143360144155
** If there is not enough free space in pNode, return SQLITE_FULL.
143361144156
*/
143362
-static int
143363
-nodeInsertCell(
143364
- Rtree *pRtree,
143365
- RtreeNode *pNode,
143366
- RtreeCell *pCell
144157
+static int nodeInsertCell(
144158
+ Rtree *pRtree, /* The overall R-Tree */
144159
+ RtreeNode *pNode, /* Write new cell into this node */
144160
+ RtreeCell *pCell /* The cell to be inserted */
143367144161
){
143368144162
int nCell; /* Current number of cells in pNode */
143369144163
int nMaxCell; /* Maximum number of cells for pNode */
143370144164
143371144165
nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
@@ -143382,12 +144176,11 @@
143382144176
}
143383144177
143384144178
/*
143385144179
** If the node is dirty, write it out to the database.
143386144180
*/
143387
-static int
143388
-nodeWrite(Rtree *pRtree, RtreeNode *pNode){
144181
+static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
143389144182
int rc = SQLITE_OK;
143390144183
if( pNode->isDirty ){
143391144184
sqlite3_stmt *p = pRtree->pWriteNode;
143392144185
if( pNode->iNode ){
143393144186
sqlite3_bind_int64(p, 1, pNode->iNode);
@@ -143408,12 +144201,11 @@
143408144201
143409144202
/*
143410144203
** Release a reference to a node. If the node is dirty and the reference
143411144204
** count drops to zero, the node data is written to the database.
143412144205
*/
143413
-static int
143414
-nodeRelease(Rtree *pRtree, RtreeNode *pNode){
144206
+static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
143415144207
int rc = SQLITE_OK;
143416144208
if( pNode ){
143417144209
assert( pNode->nRef>0 );
143418144210
pNode->nRef--;
143419144211
if( pNode->nRef==0 ){
@@ -143437,45 +144229,50 @@
143437144229
** Return the 64-bit integer value associated with cell iCell of
143438144230
** node pNode. If pNode is a leaf node, this is a rowid. If it is
143439144231
** an internal node, then the 64-bit integer is a child page number.
143440144232
*/
143441144233
static i64 nodeGetRowid(
143442
- Rtree *pRtree,
143443
- RtreeNode *pNode,
143444
- int iCell
144234
+ Rtree *pRtree, /* The overall R-Tree */
144235
+ RtreeNode *pNode, /* The node from which to extract the ID */
144236
+ int iCell /* The cell index from which to extract the ID */
143445144237
){
143446144238
assert( iCell<NCELL(pNode) );
143447144239
return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
143448144240
}
143449144241
143450144242
/*
143451144243
** Return coordinate iCoord from cell iCell in node pNode.
143452144244
*/
143453144245
static void nodeGetCoord(
143454
- Rtree *pRtree,
143455
- RtreeNode *pNode,
143456
- int iCell,
143457
- int iCoord,
143458
- RtreeCoord *pCoord /* Space to write result to */
144246
+ Rtree *pRtree, /* The overall R-Tree */
144247
+ RtreeNode *pNode, /* The node from which to extract a coordinate */
144248
+ int iCell, /* The index of the cell within the node */
144249
+ int iCoord, /* Which coordinate to extract */
144250
+ RtreeCoord *pCoord /* OUT: Space to write result to */
143459144251
){
143460144252
readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
143461144253
}
143462144254
143463144255
/*
143464144256
** Deserialize cell iCell of node pNode. Populate the structure pointed
143465144257
** to by pCell with the results.
143466144258
*/
143467144259
static void nodeGetCell(
143468
- Rtree *pRtree,
143469
- RtreeNode *pNode,
143470
- int iCell,
143471
- RtreeCell *pCell
144260
+ Rtree *pRtree, /* The overall R-Tree */
144261
+ RtreeNode *pNode, /* The node containing the cell to be read */
144262
+ int iCell, /* Index of the cell within the node */
144263
+ RtreeCell *pCell /* OUT: Write the cell contents here */
143472144264
){
143473
- int ii;
144265
+ u8 *pData;
144266
+ u8 *pEnd;
144267
+ RtreeCoord *pCoord;
143474144268
pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
143475
- for(ii=0; ii<pRtree->nDim*2; ii++){
143476
- nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
144269
+ pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
144270
+ pEnd = pData + pRtree->nDim*8;
144271
+ pCoord = pCell->aCoord;
144272
+ for(; pData<pEnd; pData+=4, pCoord++){
144273
+ readCoord(pData, pCoord);
143477144274
}
143478144275
}
143479144276
143480144277
143481144278
/* Forward declaration for the function that does the work of
@@ -143597,14 +144394,14 @@
143597144394
*/
143598144395
static void freeCursorConstraints(RtreeCursor *pCsr){
143599144396
if( pCsr->aConstraint ){
143600144397
int i; /* Used to iterate through constraint array */
143601144398
for(i=0; i<pCsr->nConstraint; i++){
143602
- sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
143603
- if( pGeom ){
143604
- if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
143605
- sqlite3_free(pGeom);
144399
+ sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
144400
+ if( pInfo ){
144401
+ if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
144402
+ sqlite3_free(pInfo);
143606144403
}
143607144404
}
143608144405
sqlite3_free(pCsr->aConstraint);
143609144406
pCsr->aConstraint = 0;
143610144407
}
@@ -143613,16 +144410,17 @@
143613144410
/*
143614144411
** Rtree virtual table module xClose method.
143615144412
*/
143616144413
static int rtreeClose(sqlite3_vtab_cursor *cur){
143617144414
Rtree *pRtree = (Rtree *)(cur->pVtab);
143618
- int rc;
144415
+ int ii;
143619144416
RtreeCursor *pCsr = (RtreeCursor *)cur;
143620144417
freeCursorConstraints(pCsr);
143621
- rc = nodeRelease(pRtree, pCsr->pNode);
144418
+ sqlite3_free(pCsr->aPoint);
144419
+ for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
143622144420
sqlite3_free(pCsr);
143623
- return rc;
144421
+ return SQLITE_OK;
143624144422
}
143625144423
143626144424
/*
143627144425
** Rtree virtual table module xEof method.
143628144426
**
@@ -143629,198 +144427,168 @@
143629144427
** Return non-zero if the cursor does not currently point to a valid
143630144428
** record (i.e if the scan has finished), or zero otherwise.
143631144429
*/
143632144430
static int rtreeEof(sqlite3_vtab_cursor *cur){
143633144431
RtreeCursor *pCsr = (RtreeCursor *)cur;
143634
- return (pCsr->pNode==0);
143635
-}
143636
-
143637
-/*
143638
-** The r-tree constraint passed as the second argument to this function is
143639
-** guaranteed to be a MATCH constraint.
143640
-*/
143641
-static int testRtreeGeom(
143642
- Rtree *pRtree, /* R-Tree object */
143643
- RtreeConstraint *pConstraint, /* MATCH constraint to test */
143644
- RtreeCell *pCell, /* Cell to test */
143645
- int *pbRes /* OUT: Test result */
143646
-){
143647
- int i;
143648
- RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
143649
- int nCoord = pRtree->nDim*2;
143650
-
143651
- assert( pConstraint->op==RTREE_MATCH );
143652
- assert( pConstraint->pGeom );
143653
-
143654
- for(i=0; i<nCoord; i++){
143655
- aCoord[i] = DCOORD(pCell->aCoord[i]);
143656
- }
143657
- return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
143658
-}
143659
-
143660
-/*
143661
-** Cursor pCursor currently points to a cell in a non-leaf page.
143662
-** Set *pbEof to true if the sub-tree headed by the cell is filtered
143663
-** (excluded) by the constraints in the pCursor->aConstraint[]
143664
-** array, or false otherwise.
143665
-**
143666
-** Return SQLITE_OK if successful or an SQLite error code if an error
143667
-** occurs within a geometry callback.
143668
-*/
143669
-static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
143670
- RtreeCell cell;
143671
- int ii;
143672
- int bRes = 0;
143673
- int rc = SQLITE_OK;
143674
-
143675
- nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
143676
- for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
143677
- RtreeConstraint *p = &pCursor->aConstraint[ii];
143678
- RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
143679
- RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
143680
-
143681
- assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
143682
- || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
143683
- );
143684
-
143685
- switch( p->op ){
143686
- case RTREE_LE: case RTREE_LT:
143687
- bRes = p->rValue<cell_min;
143688
- break;
143689
-
143690
- case RTREE_GE: case RTREE_GT:
143691
- bRes = p->rValue>cell_max;
143692
- break;
143693
-
143694
- case RTREE_EQ:
143695
- bRes = (p->rValue>cell_max || p->rValue<cell_min);
143696
- break;
143697
-
143698
- default: {
143699
- assert( p->op==RTREE_MATCH );
143700
- rc = testRtreeGeom(pRtree, p, &cell, &bRes);
143701
- bRes = !bRes;
143702
- break;
143703
- }
143704
- }
143705
- }
143706
-
143707
- *pbEof = bRes;
143708
- return rc;
143709
-}
143710
-
143711
-/*
143712
-** Test if the cell that cursor pCursor currently points to
143713
-** would be filtered (excluded) by the constraints in the
143714
-** pCursor->aConstraint[] array. If so, set *pbEof to true before
143715
-** returning. If the cell is not filtered (excluded) by the constraints,
143716
-** set pbEof to zero.
143717
-**
143718
-** Return SQLITE_OK if successful or an SQLite error code if an error
143719
-** occurs within a geometry callback.
143720
-**
143721
-** This function assumes that the cell is part of a leaf node.
143722
-*/
143723
-static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
143724
- RtreeCell cell;
143725
- int ii;
143726
- *pbEof = 0;
143727
-
143728
- nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
143729
- for(ii=0; ii<pCursor->nConstraint; ii++){
143730
- RtreeConstraint *p = &pCursor->aConstraint[ii];
143731
- RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
143732
- int res;
143733
- assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
143734
- || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
143735
- );
143736
- switch( p->op ){
143737
- case RTREE_LE: res = (coord<=p->rValue); break;
143738
- case RTREE_LT: res = (coord<p->rValue); break;
143739
- case RTREE_GE: res = (coord>=p->rValue); break;
143740
- case RTREE_GT: res = (coord>p->rValue); break;
143741
- case RTREE_EQ: res = (coord==p->rValue); break;
143742
- default: {
143743
- int rc;
143744
- assert( p->op==RTREE_MATCH );
143745
- rc = testRtreeGeom(pRtree, p, &cell, &res);
143746
- if( rc!=SQLITE_OK ){
143747
- return rc;
143748
- }
143749
- break;
143750
- }
143751
- }
143752
-
143753
- if( !res ){
143754
- *pbEof = 1;
143755
- return SQLITE_OK;
143756
- }
143757
- }
143758
-
143759
- return SQLITE_OK;
143760
-}
143761
-
143762
-/*
143763
-** Cursor pCursor currently points at a node that heads a sub-tree of
143764
-** height iHeight (if iHeight==0, then the node is a leaf). Descend
143765
-** to point to the left-most cell of the sub-tree that matches the
143766
-** configured constraints.
143767
-*/
143768
-static int descendToCell(
143769
- Rtree *pRtree,
143770
- RtreeCursor *pCursor,
143771
- int iHeight,
143772
- int *pEof /* OUT: Set to true if cannot descend */
143773
-){
143774
- int isEof;
143775
- int rc;
143776
- int ii;
143777
- RtreeNode *pChild;
143778
- sqlite3_int64 iRowid;
143779
-
143780
- RtreeNode *pSavedNode = pCursor->pNode;
143781
- int iSavedCell = pCursor->iCell;
143782
-
143783
- assert( iHeight>=0 );
143784
-
143785
- if( iHeight==0 ){
143786
- rc = testRtreeEntry(pRtree, pCursor, &isEof);
143787
- }else{
143788
- rc = testRtreeCell(pRtree, pCursor, &isEof);
143789
- }
143790
- if( rc!=SQLITE_OK || isEof || iHeight==0 ){
143791
- goto descend_to_cell_out;
143792
- }
143793
-
143794
- iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
143795
- rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
143796
- if( rc!=SQLITE_OK ){
143797
- goto descend_to_cell_out;
143798
- }
143799
-
143800
- nodeRelease(pRtree, pCursor->pNode);
143801
- pCursor->pNode = pChild;
143802
- isEof = 1;
143803
- for(ii=0; isEof && ii<NCELL(pChild); ii++){
143804
- pCursor->iCell = ii;
143805
- rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
143806
- if( rc!=SQLITE_OK ){
143807
- goto descend_to_cell_out;
143808
- }
143809
- }
143810
-
143811
- if( isEof ){
143812
- assert( pCursor->pNode==pChild );
143813
- nodeReference(pSavedNode);
143814
- nodeRelease(pRtree, pChild);
143815
- pCursor->pNode = pSavedNode;
143816
- pCursor->iCell = iSavedCell;
143817
- }
143818
-
143819
-descend_to_cell_out:
143820
- *pEof = isEof;
143821
- return rc;
144432
+ return pCsr->atEOF;
144433
+}
144434
+
144435
+/*
144436
+** Convert raw bits from the on-disk RTree record into a coordinate value.
144437
+** The on-disk format is big-endian and needs to be converted for little-
144438
+** endian platforms. The on-disk record stores integer coordinates if
144439
+** eInt is true and it stores 32-bit floating point records if eInt is
144440
+** false. a[] is the four bytes of the on-disk record to be decoded.
144441
+** Store the results in "r".
144442
+**
144443
+** There are three versions of this macro, one each for little-endian and
144444
+** big-endian processors and a third generic implementation. The endian-
144445
+** specific implementations are much faster and are preferred if the
144446
+** processor endianness is known at compile-time. The SQLITE_BYTEORDER
144447
+** macro is part of sqliteInt.h and hence the endian-specific
144448
+** implementation will only be used if this module is compiled as part
144449
+** of the amalgamation.
144450
+*/
144451
+#if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
144452
+#define RTREE_DECODE_COORD(eInt, a, r) { \
144453
+ RtreeCoord c; /* Coordinate decoded */ \
144454
+ memcpy(&c.u,a,4); \
144455
+ c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
144456
+ ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
144457
+ r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
144458
+}
144459
+#elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
144460
+#define RTREE_DECODE_COORD(eInt, a, r) { \
144461
+ RtreeCoord c; /* Coordinate decoded */ \
144462
+ memcpy(&c.u,a,4); \
144463
+ r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
144464
+}
144465
+#else
144466
+#define RTREE_DECODE_COORD(eInt, a, r) { \
144467
+ RtreeCoord c; /* Coordinate decoded */ \
144468
+ c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
144469
+ +((u32)a[2]<<8) + a[3]; \
144470
+ r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
144471
+}
144472
+#endif
144473
+
144474
+/*
144475
+** Check the RTree node or entry given by pCellData and p against the MATCH
144476
+** constraint pConstraint.
144477
+*/
144478
+static int rtreeCallbackConstraint(
144479
+ RtreeConstraint *pConstraint, /* The constraint to test */
144480
+ int eInt, /* True if RTree holding integer coordinates */
144481
+ u8 *pCellData, /* Raw cell content */
144482
+ RtreeSearchPoint *pSearch, /* Container of this cell */
144483
+ sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
144484
+ int *peWithin /* OUT: visibility of the cell */
144485
+){
144486
+ int i; /* Loop counter */
144487
+ sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
144488
+ int nCoord = pInfo->nCoord; /* No. of coordinates */
144489
+ int rc; /* Callback return code */
144490
+ sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
144491
+
144492
+ assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
144493
+ assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
144494
+
144495
+ if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
144496
+ pInfo->iRowid = readInt64(pCellData);
144497
+ }
144498
+ pCellData += 8;
144499
+ for(i=0; i<nCoord; i++, pCellData += 4){
144500
+ RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
144501
+ }
144502
+ if( pConstraint->op==RTREE_MATCH ){
144503
+ rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
144504
+ nCoord, aCoord, &i);
144505
+ if( i==0 ) *peWithin = NOT_WITHIN;
144506
+ *prScore = RTREE_ZERO;
144507
+ }else{
144508
+ pInfo->aCoord = aCoord;
144509
+ pInfo->iLevel = pSearch->iLevel - 1;
144510
+ pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
144511
+ pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
144512
+ rc = pConstraint->u.xQueryFunc(pInfo);
144513
+ if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
144514
+ if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
144515
+ *prScore = pInfo->rScore;
144516
+ }
144517
+ }
144518
+ return rc;
144519
+}
144520
+
144521
+/*
144522
+** Check the internal RTree node given by pCellData against constraint p.
144523
+** If this constraint cannot be satisfied by any child within the node,
144524
+** set *peWithin to NOT_WITHIN.
144525
+*/
144526
+static void rtreeNonleafConstraint(
144527
+ RtreeConstraint *p, /* The constraint to test */
144528
+ int eInt, /* True if RTree holds integer coordinates */
144529
+ u8 *pCellData, /* Raw cell content as appears on disk */
144530
+ int *peWithin /* Adjust downward, as appropriate */
144531
+){
144532
+ sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
144533
+
144534
+ /* p->iCoord might point to either a lower or upper bound coordinate
144535
+ ** in a coordinate pair. But make pCellData point to the lower bound.
144536
+ */
144537
+ pCellData += 8 + 4*(p->iCoord&0xfe);
144538
+
144539
+ assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
144540
+ || p->op==RTREE_GT || p->op==RTREE_EQ );
144541
+ switch( p->op ){
144542
+ case RTREE_LE:
144543
+ case RTREE_LT:
144544
+ case RTREE_EQ:
144545
+ RTREE_DECODE_COORD(eInt, pCellData, val);
144546
+ /* val now holds the lower bound of the coordinate pair */
144547
+ if( p->u.rValue>=val ) return;
144548
+ if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */
144549
+ /* Fall through for the RTREE_EQ case */
144550
+
144551
+ default: /* RTREE_GT or RTREE_GE, or fallthrough of RTREE_EQ */
144552
+ pCellData += 4;
144553
+ RTREE_DECODE_COORD(eInt, pCellData, val);
144554
+ /* val now holds the upper bound of the coordinate pair */
144555
+ if( p->u.rValue<=val ) return;
144556
+ }
144557
+ *peWithin = NOT_WITHIN;
144558
+}
144559
+
144560
+/*
144561
+** Check the leaf RTree cell given by pCellData against constraint p.
144562
+** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
144563
+** If the constraint is satisfied, leave *peWithin unchanged.
144564
+**
144565
+** The constraint is of the form: xN op $val
144566
+**
144567
+** The op is given by p->op. The xN is p->iCoord-th coordinate in
144568
+** pCellData. $val is given by p->u.rValue.
144569
+*/
144570
+static void rtreeLeafConstraint(
144571
+ RtreeConstraint *p, /* The constraint to test */
144572
+ int eInt, /* True if RTree holds integer coordinates */
144573
+ u8 *pCellData, /* Raw cell content as appears on disk */
144574
+ int *peWithin /* Adjust downward, as appropriate */
144575
+){
144576
+ RtreeDValue xN; /* Coordinate value converted to a double */
144577
+
144578
+ assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
144579
+ || p->op==RTREE_GT || p->op==RTREE_EQ );
144580
+ pCellData += 8 + p->iCoord*4;
144581
+ RTREE_DECODE_COORD(eInt, pCellData, xN);
144582
+ switch( p->op ){
144583
+ case RTREE_LE: if( xN <= p->u.rValue ) return; break;
144584
+ case RTREE_LT: if( xN < p->u.rValue ) return; break;
144585
+ case RTREE_GE: if( xN >= p->u.rValue ) return; break;
144586
+ case RTREE_GT: if( xN > p->u.rValue ) return; break;
144587
+ default: if( xN == p->u.rValue ) return; break;
144588
+ }
144589
+ *peWithin = NOT_WITHIN;
143822144590
}
143823144591
143824144592
/*
143825144593
** One of the cells in node pNode is guaranteed to have a 64-bit
143826144594
** integer value equal to iRowid. Return the index of this cell.
@@ -143831,10 +144599,11 @@
143831144599
i64 iRowid,
143832144600
int *piIndex
143833144601
){
143834144602
int ii;
143835144603
int nCell = NCELL(pNode);
144604
+ assert( nCell<200 );
143836144605
for(ii=0; ii<nCell; ii++){
143837144606
if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
143838144607
*piIndex = ii;
143839144608
return SQLITE_OK;
143840144609
}
@@ -143852,82 +144621,342 @@
143852144621
return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
143853144622
}
143854144623
*piIndex = -1;
143855144624
return SQLITE_OK;
143856144625
}
144626
+
144627
+/*
144628
+** Compare two search points. Return negative, zero, or positive if the first
144629
+** is less than, equal to, or greater than the second.
144630
+**
144631
+** The rScore is the primary key. Smaller rScore values come first.
144632
+** If the rScore is a tie, then use iLevel as the tie breaker with smaller
144633
+** iLevel values coming first. In this way, if rScore is the same for all
144634
+** SearchPoints, then iLevel becomes the deciding factor and the result
144635
+** is a depth-first search, which is the desired default behavior.
144636
+*/
144637
+static int rtreeSearchPointCompare(
144638
+ const RtreeSearchPoint *pA,
144639
+ const RtreeSearchPoint *pB
144640
+){
144641
+ if( pA->rScore<pB->rScore ) return -1;
144642
+ if( pA->rScore>pB->rScore ) return +1;
144643
+ if( pA->iLevel<pB->iLevel ) return -1;
144644
+ if( pA->iLevel>pB->iLevel ) return +1;
144645
+ return 0;
144646
+}
144647
+
144648
+/*
144649
+** Interchange to search points in a cursor.
144650
+*/
144651
+static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
144652
+ RtreeSearchPoint t = p->aPoint[i];
144653
+ assert( i<j );
144654
+ p->aPoint[i] = p->aPoint[j];
144655
+ p->aPoint[j] = t;
144656
+ i++; j++;
144657
+ if( i<RTREE_CACHE_SZ ){
144658
+ if( j>=RTREE_CACHE_SZ ){
144659
+ nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
144660
+ p->aNode[i] = 0;
144661
+ }else{
144662
+ RtreeNode *pTemp = p->aNode[i];
144663
+ p->aNode[i] = p->aNode[j];
144664
+ p->aNode[j] = pTemp;
144665
+ }
144666
+ }
144667
+}
144668
+
144669
+/*
144670
+** Return the search point with the lowest current score.
144671
+*/
144672
+static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
144673
+ return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
144674
+}
144675
+
144676
+/*
144677
+** Get the RtreeNode for the search point with the lowest score.
144678
+*/
144679
+static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
144680
+ sqlite3_int64 id;
144681
+ int ii = 1 - pCur->bPoint;
144682
+ assert( ii==0 || ii==1 );
144683
+ assert( pCur->bPoint || pCur->nPoint );
144684
+ if( pCur->aNode[ii]==0 ){
144685
+ assert( pRC!=0 );
144686
+ id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
144687
+ *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
144688
+ }
144689
+ return pCur->aNode[ii];
144690
+}
144691
+
144692
+/*
144693
+** Push a new element onto the priority queue
144694
+*/
144695
+static RtreeSearchPoint *rtreeEnqueue(
144696
+ RtreeCursor *pCur, /* The cursor */
144697
+ RtreeDValue rScore, /* Score for the new search point */
144698
+ u8 iLevel /* Level for the new search point */
144699
+){
144700
+ int i, j;
144701
+ RtreeSearchPoint *pNew;
144702
+ if( pCur->nPoint>=pCur->nPointAlloc ){
144703
+ int nNew = pCur->nPointAlloc*2 + 8;
144704
+ pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
144705
+ if( pNew==0 ) return 0;
144706
+ pCur->aPoint = pNew;
144707
+ pCur->nPointAlloc = nNew;
144708
+ }
144709
+ i = pCur->nPoint++;
144710
+ pNew = pCur->aPoint + i;
144711
+ pNew->rScore = rScore;
144712
+ pNew->iLevel = iLevel;
144713
+ assert( iLevel>=0 && iLevel<=RTREE_MAX_DEPTH );
144714
+ while( i>0 ){
144715
+ RtreeSearchPoint *pParent;
144716
+ j = (i-1)/2;
144717
+ pParent = pCur->aPoint + j;
144718
+ if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
144719
+ rtreeSearchPointSwap(pCur, j, i);
144720
+ i = j;
144721
+ pNew = pParent;
144722
+ }
144723
+ return pNew;
144724
+}
144725
+
144726
+/*
144727
+** Allocate a new RtreeSearchPoint and return a pointer to it. Return
144728
+** NULL if malloc fails.
144729
+*/
144730
+static RtreeSearchPoint *rtreeSearchPointNew(
144731
+ RtreeCursor *pCur, /* The cursor */
144732
+ RtreeDValue rScore, /* Score for the new search point */
144733
+ u8 iLevel /* Level for the new search point */
144734
+){
144735
+ RtreeSearchPoint *pNew, *pFirst;
144736
+ pFirst = rtreeSearchPointFirst(pCur);
144737
+ pCur->anQueue[iLevel]++;
144738
+ if( pFirst==0
144739
+ || pFirst->rScore>rScore
144740
+ || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
144741
+ ){
144742
+ if( pCur->bPoint ){
144743
+ int ii;
144744
+ pNew = rtreeEnqueue(pCur, rScore, iLevel);
144745
+ if( pNew==0 ) return 0;
144746
+ ii = (int)(pNew - pCur->aPoint) + 1;
144747
+ if( ii<RTREE_CACHE_SZ ){
144748
+ assert( pCur->aNode[ii]==0 );
144749
+ pCur->aNode[ii] = pCur->aNode[0];
144750
+ }else{
144751
+ nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
144752
+ }
144753
+ pCur->aNode[0] = 0;
144754
+ *pNew = pCur->sPoint;
144755
+ }
144756
+ pCur->sPoint.rScore = rScore;
144757
+ pCur->sPoint.iLevel = iLevel;
144758
+ pCur->bPoint = 1;
144759
+ return &pCur->sPoint;
144760
+ }else{
144761
+ return rtreeEnqueue(pCur, rScore, iLevel);
144762
+ }
144763
+}
144764
+
144765
+#if 0
144766
+/* Tracing routines for the RtreeSearchPoint queue */
144767
+static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
144768
+ if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
144769
+ printf(" %d.%05lld.%02d %g %d",
144770
+ p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
144771
+ );
144772
+ idx++;
144773
+ if( idx<RTREE_CACHE_SZ ){
144774
+ printf(" %p\n", pCur->aNode[idx]);
144775
+ }else{
144776
+ printf("\n");
144777
+ }
144778
+}
144779
+static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
144780
+ int ii;
144781
+ printf("=== %9s ", zPrefix);
144782
+ if( pCur->bPoint ){
144783
+ tracePoint(&pCur->sPoint, -1, pCur);
144784
+ }
144785
+ for(ii=0; ii<pCur->nPoint; ii++){
144786
+ if( ii>0 || pCur->bPoint ) printf(" ");
144787
+ tracePoint(&pCur->aPoint[ii], ii, pCur);
144788
+ }
144789
+}
144790
+# define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
144791
+#else
144792
+# define RTREE_QUEUE_TRACE(A,B) /* no-op */
144793
+#endif
144794
+
144795
+/* Remove the search point with the lowest current score.
144796
+*/
144797
+static void rtreeSearchPointPop(RtreeCursor *p){
144798
+ int i, j, k, n;
144799
+ i = 1 - p->bPoint;
144800
+ assert( i==0 || i==1 );
144801
+ if( p->aNode[i] ){
144802
+ nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
144803
+ p->aNode[i] = 0;
144804
+ }
144805
+ if( p->bPoint ){
144806
+ p->anQueue[p->sPoint.iLevel]--;
144807
+ p->bPoint = 0;
144808
+ }else if( p->nPoint ){
144809
+ p->anQueue[p->aPoint[0].iLevel]--;
144810
+ n = --p->nPoint;
144811
+ p->aPoint[0] = p->aPoint[n];
144812
+ if( n<RTREE_CACHE_SZ-1 ){
144813
+ p->aNode[1] = p->aNode[n+1];
144814
+ p->aNode[n+1] = 0;
144815
+ }
144816
+ i = 0;
144817
+ while( (j = i*2+1)<n ){
144818
+ k = j+1;
144819
+ if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
144820
+ if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
144821
+ rtreeSearchPointSwap(p, i, k);
144822
+ i = k;
144823
+ }else{
144824
+ break;
144825
+ }
144826
+ }else{
144827
+ if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
144828
+ rtreeSearchPointSwap(p, i, j);
144829
+ i = j;
144830
+ }else{
144831
+ break;
144832
+ }
144833
+ }
144834
+ }
144835
+ }
144836
+}
144837
+
144838
+
144839
+/*
144840
+** Continue the search on cursor pCur until the front of the queue
144841
+** contains an entry suitable for returning as a result-set row,
144842
+** or until the RtreeSearchPoint queue is empty, indicating that the
144843
+** query has completed.
144844
+*/
144845
+static int rtreeStepToLeaf(RtreeCursor *pCur){
144846
+ RtreeSearchPoint *p;
144847
+ Rtree *pRtree = RTREE_OF_CURSOR(pCur);
144848
+ RtreeNode *pNode;
144849
+ int eWithin;
144850
+ int rc = SQLITE_OK;
144851
+ int nCell;
144852
+ int nConstraint = pCur->nConstraint;
144853
+ int ii;
144854
+ int eInt;
144855
+ RtreeSearchPoint x;
144856
+
144857
+ eInt = pRtree->eCoordType==RTREE_COORD_INT32;
144858
+ while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
144859
+ pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
144860
+ if( rc ) return rc;
144861
+ nCell = NCELL(pNode);
144862
+ assert( nCell<200 );
144863
+ while( p->iCell<nCell ){
144864
+ sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
144865
+ u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
144866
+ eWithin = FULLY_WITHIN;
144867
+ for(ii=0; ii<nConstraint; ii++){
144868
+ RtreeConstraint *pConstraint = pCur->aConstraint + ii;
144869
+ if( pConstraint->op>=RTREE_MATCH ){
144870
+ rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
144871
+ &rScore, &eWithin);
144872
+ if( rc ) return rc;
144873
+ }else if( p->iLevel==1 ){
144874
+ rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
144875
+ }else{
144876
+ rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
144877
+ }
144878
+ if( eWithin==NOT_WITHIN ) break;
144879
+ }
144880
+ p->iCell++;
144881
+ if( eWithin==NOT_WITHIN ) continue;
144882
+ x.iLevel = p->iLevel - 1;
144883
+ if( x.iLevel ){
144884
+ x.id = readInt64(pCellData);
144885
+ x.iCell = 0;
144886
+ }else{
144887
+ x.id = p->id;
144888
+ x.iCell = p->iCell - 1;
144889
+ }
144890
+ if( p->iCell>=nCell ){
144891
+ RTREE_QUEUE_TRACE(pCur, "POP-S:");
144892
+ rtreeSearchPointPop(pCur);
144893
+ }
144894
+ if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
144895
+ p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
144896
+ if( p==0 ) return SQLITE_NOMEM;
144897
+ p->eWithin = eWithin;
144898
+ p->id = x.id;
144899
+ p->iCell = x.iCell;
144900
+ RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
144901
+ break;
144902
+ }
144903
+ if( p->iCell>=nCell ){
144904
+ RTREE_QUEUE_TRACE(pCur, "POP-Se:");
144905
+ rtreeSearchPointPop(pCur);
144906
+ }
144907
+ }
144908
+ pCur->atEOF = p==0;
144909
+ return SQLITE_OK;
144910
+}
143857144911
143858144912
/*
143859144913
** Rtree virtual table module xNext method.
143860144914
*/
143861144915
static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
143862
- Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
143863144916
RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
143864144917
int rc = SQLITE_OK;
143865144918
143866
- /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
143867
- ** already at EOF. It is against the rules to call the xNext() method of
143868
- ** a cursor that has already reached EOF.
143869
- */
143870
- assert( pCsr->pNode );
143871
-
143872
- if( pCsr->iStrategy==1 ){
143873
- /* This "scan" is a direct lookup by rowid. There is no next entry. */
143874
- nodeRelease(pRtree, pCsr->pNode);
143875
- pCsr->pNode = 0;
143876
- }else{
143877
- /* Move to the next entry that matches the configured constraints. */
143878
- int iHeight = 0;
143879
- while( pCsr->pNode ){
143880
- RtreeNode *pNode = pCsr->pNode;
143881
- int nCell = NCELL(pNode);
143882
- for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
143883
- int isEof;
143884
- rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
143885
- if( rc!=SQLITE_OK || !isEof ){
143886
- return rc;
143887
- }
143888
- }
143889
- pCsr->pNode = pNode->pParent;
143890
- rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
143891
- if( rc!=SQLITE_OK ){
143892
- return rc;
143893
- }
143894
- nodeReference(pCsr->pNode);
143895
- nodeRelease(pRtree, pNode);
143896
- iHeight++;
143897
- }
143898
- }
143899
-
144919
+ /* Move to the next entry that matches the configured constraints. */
144920
+ RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
144921
+ rtreeSearchPointPop(pCsr);
144922
+ rc = rtreeStepToLeaf(pCsr);
143900144923
return rc;
143901144924
}
143902144925
143903144926
/*
143904144927
** Rtree virtual table module xRowid method.
143905144928
*/
143906144929
static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
143907
- Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
143908144930
RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
143909
-
143910
- assert(pCsr->pNode);
143911
- *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
143912
-
143913
- return SQLITE_OK;
144931
+ RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
144932
+ int rc = SQLITE_OK;
144933
+ RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
144934
+ if( rc==SQLITE_OK && p ){
144935
+ *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
144936
+ }
144937
+ return rc;
143914144938
}
143915144939
143916144940
/*
143917144941
** Rtree virtual table module xColumn method.
143918144942
*/
143919144943
static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
143920144944
Rtree *pRtree = (Rtree *)cur->pVtab;
143921144945
RtreeCursor *pCsr = (RtreeCursor *)cur;
144946
+ RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
144947
+ RtreeCoord c;
144948
+ int rc = SQLITE_OK;
144949
+ RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
143922144950
144951
+ if( rc ) return rc;
144952
+ if( p==0 ) return SQLITE_OK;
143923144953
if( i==0 ){
143924
- i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
143925
- sqlite3_result_int64(ctx, iRowid);
144954
+ sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
143926144955
}else{
143927
- RtreeCoord c;
143928
- nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
144956
+ if( rc ) return rc;
144957
+ nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
143929144958
#ifndef SQLITE_RTREE_INT_ONLY
143930144959
if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
143931144960
sqlite3_result_double(ctx, c.f);
143932144961
}else
143933144962
#endif
@@ -143934,11 +144963,10 @@
143934144963
{
143935144964
assert( pRtree->eCoordType==RTREE_COORD_INT32 );
143936144965
sqlite3_result_int(ctx, c.i);
143937144966
}
143938144967
}
143939
-
143940144968
return SQLITE_OK;
143941144969
}
143942144970
143943144971
/*
143944144972
** Use nodeAcquire() to obtain the leaf node containing the record with
@@ -143945,16 +144973,22 @@
143945144973
** rowid iRowid. If successful, set *ppLeaf to point to the node and
143946144974
** return SQLITE_OK. If there is no such record in the table, set
143947144975
** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
143948144976
** to zero and return an SQLite error code.
143949144977
*/
143950
-static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
144978
+static int findLeafNode(
144979
+ Rtree *pRtree, /* RTree to search */
144980
+ i64 iRowid, /* The rowid searching for */
144981
+ RtreeNode **ppLeaf, /* Write the node here */
144982
+ sqlite3_int64 *piNode /* Write the node-id here */
144983
+){
143951144984
int rc;
143952144985
*ppLeaf = 0;
143953144986
sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
143954144987
if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
143955144988
i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
144989
+ if( piNode ) *piNode = iNode;
143956144990
rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
143957144991
sqlite3_reset(pRtree->pReadRowid);
143958144992
}else{
143959144993
rc = sqlite3_reset(pRtree->pReadRowid);
143960144994
}
@@ -143966,13 +145000,14 @@
143966145000
** as the second argument for a MATCH constraint. The value passed as the
143967145001
** first argument to this function is the right-hand operand to the MATCH
143968145002
** operator.
143969145003
*/
143970145004
static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
143971
- RtreeMatchArg *p;
143972
- sqlite3_rtree_geometry *pGeom;
143973
- int nBlob;
145005
+ RtreeMatchArg *pBlob; /* BLOB returned by geometry function */
145006
+ sqlite3_rtree_query_info *pInfo; /* Callback information */
145007
+ int nBlob; /* Size of the geometry function blob */
145008
+ int nExpected; /* Expected size of the BLOB */
143974145009
143975145010
/* Check that value is actually a blob. */
143976145011
if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
143977145012
143978145013
/* Check that the blob is roughly the right size. */
@@ -143981,31 +145016,33 @@
143981145016
|| ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
143982145017
){
143983145018
return SQLITE_ERROR;
143984145019
}
143985145020
143986
- pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
143987
- sizeof(sqlite3_rtree_geometry) + nBlob
143988
- );
143989
- if( !pGeom ) return SQLITE_NOMEM;
143990
- memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
143991
- p = (RtreeMatchArg *)&pGeom[1];
143992
-
143993
- memcpy(p, sqlite3_value_blob(pValue), nBlob);
143994
- if( p->magic!=RTREE_GEOMETRY_MAGIC
143995
- || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
143996
- ){
143997
- sqlite3_free(pGeom);
145021
+ pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
145022
+ if( !pInfo ) return SQLITE_NOMEM;
145023
+ memset(pInfo, 0, sizeof(*pInfo));
145024
+ pBlob = (RtreeMatchArg*)&pInfo[1];
145025
+
145026
+ memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
145027
+ nExpected = (int)(sizeof(RtreeMatchArg) +
145028
+ (pBlob->nParam-1)*sizeof(RtreeDValue));
145029
+ if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
145030
+ sqlite3_free(pInfo);
143998145031
return SQLITE_ERROR;
143999145032
}
145033
+ pInfo->pContext = pBlob->cb.pContext;
145034
+ pInfo->nParam = pBlob->nParam;
145035
+ pInfo->aParam = pBlob->aParam;
144000145036
144001
- pGeom->pContext = p->pContext;
144002
- pGeom->nParam = p->nParam;
144003
- pGeom->aParam = p->aParam;
144004
-
144005
- pCons->xGeom = p->xGeom;
144006
- pCons->pGeom = pGeom;
145037
+ if( pBlob->cb.xGeom ){
145038
+ pCons->u.xGeom = pBlob->cb.xGeom;
145039
+ }else{
145040
+ pCons->op = RTREE_QUERY;
145041
+ pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
145042
+ }
145043
+ pCons->pInfo = pInfo;
144007145044
return SQLITE_OK;
144008145045
}
144009145046
144010145047
/*
144011145048
** Rtree virtual table module xFilter method.
@@ -144015,91 +145052,96 @@
144015145052
int idxNum, const char *idxStr,
144016145053
int argc, sqlite3_value **argv
144017145054
){
144018145055
Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
144019145056
RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
144020
-
144021145057
RtreeNode *pRoot = 0;
144022145058
int ii;
144023145059
int rc = SQLITE_OK;
145060
+ int iCell = 0;
144024145061
144025145062
rtreeReference(pRtree);
144026145063
144027145064
freeCursorConstraints(pCsr);
144028145065
pCsr->iStrategy = idxNum;
144029145066
144030145067
if( idxNum==1 ){
144031145068
/* Special case - lookup by rowid. */
144032145069
RtreeNode *pLeaf; /* Leaf on which the required cell resides */
145070
+ RtreeSearchPoint *p; /* Search point for the the leaf */
144033145071
i64 iRowid = sqlite3_value_int64(argv[0]);
144034
- rc = findLeafNode(pRtree, iRowid, &pLeaf);
144035
- pCsr->pNode = pLeaf;
144036
- if( pLeaf ){
144037
- assert( rc==SQLITE_OK );
144038
- rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
145072
+ i64 iNode = 0;
145073
+ rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
145074
+ if( rc==SQLITE_OK && pLeaf!=0 ){
145075
+ p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
145076
+ assert( p!=0 ); /* Always returns pCsr->sPoint */
145077
+ pCsr->aNode[0] = pLeaf;
145078
+ p->id = iNode;
145079
+ p->eWithin = PARTLY_WITHIN;
145080
+ rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
145081
+ p->iCell = iCell;
145082
+ RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
145083
+ }else{
145084
+ pCsr->atEOF = 1;
144039145085
}
144040145086
}else{
144041145087
/* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
144042145088
** with the configured constraints.
144043145089
*/
144044
- if( argc>0 ){
145090
+ rc = nodeAcquire(pRtree, 1, 0, &pRoot);
145091
+ if( rc==SQLITE_OK && argc>0 ){
144045145092
pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
144046145093
pCsr->nConstraint = argc;
144047145094
if( !pCsr->aConstraint ){
144048145095
rc = SQLITE_NOMEM;
144049145096
}else{
144050145097
memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
145098
+ memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
144051145099
assert( (idxStr==0 && argc==0)
144052145100
|| (idxStr && (int)strlen(idxStr)==argc*2) );
144053145101
for(ii=0; ii<argc; ii++){
144054145102
RtreeConstraint *p = &pCsr->aConstraint[ii];
144055145103
p->op = idxStr[ii*2];
144056
- p->iCoord = idxStr[ii*2+1]-'a';
144057
- if( p->op==RTREE_MATCH ){
145104
+ p->iCoord = idxStr[ii*2+1]-'0';
145105
+ if( p->op>=RTREE_MATCH ){
144058145106
/* A MATCH operator. The right-hand-side must be a blob that
144059145107
** can be cast into an RtreeMatchArg object. One created using
144060145108
** an sqlite3_rtree_geometry_callback() SQL user function.
144061145109
*/
144062145110
rc = deserializeGeometry(argv[ii], p);
144063145111
if( rc!=SQLITE_OK ){
144064145112
break;
144065145113
}
145114
+ p->pInfo->nCoord = pRtree->nDim*2;
145115
+ p->pInfo->anQueue = pCsr->anQueue;
145116
+ p->pInfo->mxLevel = pRtree->iDepth + 1;
144066145117
}else{
144067145118
#ifdef SQLITE_RTREE_INT_ONLY
144068
- p->rValue = sqlite3_value_int64(argv[ii]);
145119
+ p->u.rValue = sqlite3_value_int64(argv[ii]);
144069145120
#else
144070
- p->rValue = sqlite3_value_double(argv[ii]);
145121
+ p->u.rValue = sqlite3_value_double(argv[ii]);
144071145122
#endif
144072145123
}
144073145124
}
144074145125
}
144075145126
}
144076
-
144077
- if( rc==SQLITE_OK ){
144078
- pCsr->pNode = 0;
144079
- rc = nodeAcquire(pRtree, 1, 0, &pRoot);
144080
- }
144081
- if( rc==SQLITE_OK ){
144082
- int isEof = 1;
144083
- int nCell = NCELL(pRoot);
144084
- pCsr->pNode = pRoot;
144085
- for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
144086
- assert( pCsr->pNode==pRoot );
144087
- rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
144088
- if( !isEof ){
144089
- break;
144090
- }
144091
- }
144092
- if( rc==SQLITE_OK && isEof ){
144093
- assert( pCsr->pNode==pRoot );
144094
- nodeRelease(pRtree, pRoot);
144095
- pCsr->pNode = 0;
144096
- }
144097
- assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
144098
- }
144099
- }
144100
-
145127
+ if( rc==SQLITE_OK ){
145128
+ RtreeSearchPoint *pNew;
145129
+ pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
145130
+ if( pNew==0 ) return SQLITE_NOMEM;
145131
+ pNew->id = 1;
145132
+ pNew->iCell = 0;
145133
+ pNew->eWithin = PARTLY_WITHIN;
145134
+ assert( pCsr->bPoint==1 );
145135
+ pCsr->aNode[0] = pRoot;
145136
+ pRoot = 0;
145137
+ RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
145138
+ rc = rtreeStepToLeaf(pCsr);
145139
+ }
145140
+ }
145141
+
145142
+ nodeRelease(pRtree, pRoot);
144101145143
rtreeRelease(pRtree);
144102145144
return rc;
144103145145
}
144104145146
144105145147
/*
@@ -144197,11 +145239,11 @@
144197145239
assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
144198145240
op = RTREE_MATCH;
144199145241
break;
144200145242
}
144201145243
zIdxStr[iIdx++] = op;
144202
- zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
145244
+ zIdxStr[iIdx++] = p->iColumn - 1 + '0';
144203145245
pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
144204145246
pIdxInfo->aConstraintUsage[ii].omit = 1;
144205145247
}
144206145248
}
144207145249
@@ -144290,66 +145332,36 @@
144290145332
area = cellArea(pRtree, &cell);
144291145333
cellUnion(pRtree, &cell, pCell);
144292145334
return (cellArea(pRtree, &cell)-area);
144293145335
}
144294145336
144295
-#if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
144296145337
static RtreeDValue cellOverlap(
144297145338
Rtree *pRtree,
144298145339
RtreeCell *p,
144299145340
RtreeCell *aCell,
144300
- int nCell,
144301
- int iExclude
145341
+ int nCell
144302145342
){
144303145343
int ii;
144304
- RtreeDValue overlap = 0.0;
145344
+ RtreeDValue overlap = RTREE_ZERO;
144305145345
for(ii=0; ii<nCell; ii++){
144306
-#if VARIANT_RSTARTREE_CHOOSESUBTREE
144307
- if( ii!=iExclude )
144308
-#else
144309
- assert( iExclude==-1 );
144310
- UNUSED_PARAMETER(iExclude);
144311
-#endif
144312
- {
144313
- int jj;
144314
- RtreeDValue o = (RtreeDValue)1;
144315
- for(jj=0; jj<(pRtree->nDim*2); jj+=2){
144316
- RtreeDValue x1, x2;
144317
-
144318
- x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
144319
- x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
144320
-
144321
- if( x2<x1 ){
144322
- o = 0.0;
144323
- break;
144324
- }else{
144325
- o = o * (x2-x1);
144326
- }
144327
- }
144328
- overlap += o;
144329
- }
145346
+ int jj;
145347
+ RtreeDValue o = (RtreeDValue)1;
145348
+ for(jj=0; jj<(pRtree->nDim*2); jj+=2){
145349
+ RtreeDValue x1, x2;
145350
+ x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
145351
+ x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
145352
+ if( x2<x1 ){
145353
+ o = (RtreeDValue)0;
145354
+ break;
145355
+ }else{
145356
+ o = o * (x2-x1);
145357
+ }
145358
+ }
145359
+ overlap += o;
144330145360
}
144331145361
return overlap;
144332145362
}
144333
-#endif
144334
-
144335
-#if VARIANT_RSTARTREE_CHOOSESUBTREE
144336
-static RtreeDValue cellOverlapEnlargement(
144337
- Rtree *pRtree,
144338
- RtreeCell *p,
144339
- RtreeCell *pInsert,
144340
- RtreeCell *aCell,
144341
- int nCell,
144342
- int iExclude
144343
-){
144344
- RtreeDValue before, after;
144345
- before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
144346
- cellUnion(pRtree, p, pInsert);
144347
- after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
144348
- return (after-before);
144349
-}
144350
-#endif
144351145363
144352145364
144353145365
/*
144354145366
** This function implements the ChooseLeaf algorithm from Gutman[84].
144355145367
** ChooseSubTree in r*tree terminology.
@@ -144367,39 +145379,19 @@
144367145379
144368145380
for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
144369145381
int iCell;
144370145382
sqlite3_int64 iBest = 0;
144371145383
144372
- RtreeDValue fMinGrowth = 0.0;
144373
- RtreeDValue fMinArea = 0.0;
144374
-#if VARIANT_RSTARTREE_CHOOSESUBTREE
144375
- RtreeDValue fMinOverlap = 0.0;
144376
- RtreeDValue overlap;
144377
-#endif
145384
+ RtreeDValue fMinGrowth = RTREE_ZERO;
145385
+ RtreeDValue fMinArea = RTREE_ZERO;
144378145386
144379145387
int nCell = NCELL(pNode);
144380145388
RtreeCell cell;
144381145389
RtreeNode *pChild;
144382145390
144383145391
RtreeCell *aCell = 0;
144384145392
144385
-#if VARIANT_RSTARTREE_CHOOSESUBTREE
144386
- if( ii==(pRtree->iDepth-1) ){
144387
- int jj;
144388
- aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
144389
- if( !aCell ){
144390
- rc = SQLITE_NOMEM;
144391
- nodeRelease(pRtree, pNode);
144392
- pNode = 0;
144393
- continue;
144394
- }
144395
- for(jj=0; jj<nCell; jj++){
144396
- nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
144397
- }
144398
- }
144399
-#endif
144400
-
144401145393
/* Select the child node which will be enlarged the least if pCell
144402145394
** is inserted into it. Resolve ties by choosing the entry with
144403145395
** the smallest area.
144404145396
*/
144405145397
for(iCell=0; iCell<nCell; iCell++){
@@ -144407,30 +145399,13 @@
144407145399
RtreeDValue growth;
144408145400
RtreeDValue area;
144409145401
nodeGetCell(pRtree, pNode, iCell, &cell);
144410145402
growth = cellGrowth(pRtree, &cell, pCell);
144411145403
area = cellArea(pRtree, &cell);
144412
-
144413
-#if VARIANT_RSTARTREE_CHOOSESUBTREE
144414
- if( ii==(pRtree->iDepth-1) ){
144415
- overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
144416
- }else{
144417
- overlap = 0.0;
144418
- }
144419
- if( (iCell==0)
144420
- || (overlap<fMinOverlap)
144421
- || (overlap==fMinOverlap && growth<fMinGrowth)
144422
- || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
144423
- ){
144424
- bBest = 1;
144425
- fMinOverlap = overlap;
144426
- }
144427
-#else
144428145404
if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
144429145405
bBest = 1;
144430145406
}
144431
-#endif
144432145407
if( bBest ){
144433145408
fMinGrowth = growth;
144434145409
fMinArea = area;
144435145410
iBest = cell.iRowid;
144436145411
}
@@ -144497,159 +145472,10 @@
144497145472
return sqlite3_reset(pRtree->pWriteParent);
144498145473
}
144499145474
144500145475
static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
144501145476
144502
-#if VARIANT_GUTTMAN_LINEAR_SPLIT
144503
-/*
144504
-** Implementation of the linear variant of the PickNext() function from
144505
-** Guttman[84].
144506
-*/
144507
-static RtreeCell *LinearPickNext(
144508
- Rtree *pRtree,
144509
- RtreeCell *aCell,
144510
- int nCell,
144511
- RtreeCell *pLeftBox,
144512
- RtreeCell *pRightBox,
144513
- int *aiUsed
144514
-){
144515
- int ii;
144516
- for(ii=0; aiUsed[ii]; ii++);
144517
- aiUsed[ii] = 1;
144518
- return &aCell[ii];
144519
-}
144520
-
144521
-/*
144522
-** Implementation of the linear variant of the PickSeeds() function from
144523
-** Guttman[84].
144524
-*/
144525
-static void LinearPickSeeds(
144526
- Rtree *pRtree,
144527
- RtreeCell *aCell,
144528
- int nCell,
144529
- int *piLeftSeed,
144530
- int *piRightSeed
144531
-){
144532
- int i;
144533
- int iLeftSeed = 0;
144534
- int iRightSeed = 1;
144535
- RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
144536
-
144537
- /* Pick two "seed" cells from the array of cells. The algorithm used
144538
- ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
144539
- ** indices of the two seed cells in the array are stored in local
144540
- ** variables iLeftSeek and iRightSeed.
144541
- */
144542
- for(i=0; i<pRtree->nDim; i++){
144543
- RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
144544
- RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
144545
- RtreeDValue x3 = x1;
144546
- RtreeDValue x4 = x2;
144547
- int jj;
144548
-
144549
- int iCellLeft = 0;
144550
- int iCellRight = 0;
144551
-
144552
- for(jj=1; jj<nCell; jj++){
144553
- RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
144554
- RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
144555
-
144556
- if( left<x1 ) x1 = left;
144557
- if( right>x4 ) x4 = right;
144558
- if( left>x3 ){
144559
- x3 = left;
144560
- iCellRight = jj;
144561
- }
144562
- if( right<x2 ){
144563
- x2 = right;
144564
- iCellLeft = jj;
144565
- }
144566
- }
144567
-
144568
- if( x4!=x1 ){
144569
- RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
144570
- if( normalwidth>maxNormalInnerWidth ){
144571
- iLeftSeed = iCellLeft;
144572
- iRightSeed = iCellRight;
144573
- }
144574
- }
144575
- }
144576
-
144577
- *piLeftSeed = iLeftSeed;
144578
- *piRightSeed = iRightSeed;
144579
-}
144580
-#endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
144581
-
144582
-#if VARIANT_GUTTMAN_QUADRATIC_SPLIT
144583
-/*
144584
-** Implementation of the quadratic variant of the PickNext() function from
144585
-** Guttman[84].
144586
-*/
144587
-static RtreeCell *QuadraticPickNext(
144588
- Rtree *pRtree,
144589
- RtreeCell *aCell,
144590
- int nCell,
144591
- RtreeCell *pLeftBox,
144592
- RtreeCell *pRightBox,
144593
- int *aiUsed
144594
-){
144595
- #define FABS(a) ((a)<0.0?-1.0*(a):(a))
144596
-
144597
- int iSelect = -1;
144598
- RtreeDValue fDiff;
144599
- int ii;
144600
- for(ii=0; ii<nCell; ii++){
144601
- if( aiUsed[ii]==0 ){
144602
- RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
144603
- RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
144604
- RtreeDValue diff = FABS(right-left);
144605
- if( iSelect<0 || diff>fDiff ){
144606
- fDiff = diff;
144607
- iSelect = ii;
144608
- }
144609
- }
144610
- }
144611
- aiUsed[iSelect] = 1;
144612
- return &aCell[iSelect];
144613
-}
144614
-
144615
-/*
144616
-** Implementation of the quadratic variant of the PickSeeds() function from
144617
-** Guttman[84].
144618
-*/
144619
-static void QuadraticPickSeeds(
144620
- Rtree *pRtree,
144621
- RtreeCell *aCell,
144622
- int nCell,
144623
- int *piLeftSeed,
144624
- int *piRightSeed
144625
-){
144626
- int ii;
144627
- int jj;
144628
-
144629
- int iLeftSeed = 0;
144630
- int iRightSeed = 1;
144631
- RtreeDValue fWaste = 0.0;
144632
-
144633
- for(ii=0; ii<nCell; ii++){
144634
- for(jj=ii+1; jj<nCell; jj++){
144635
- RtreeDValue right = cellArea(pRtree, &aCell[jj]);
144636
- RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
144637
- RtreeDValue waste = growth - right;
144638
-
144639
- if( waste>fWaste ){
144640
- iLeftSeed = ii;
144641
- iRightSeed = jj;
144642
- fWaste = waste;
144643
- }
144644
- }
144645
- }
144646
-
144647
- *piLeftSeed = iLeftSeed;
144648
- *piRightSeed = iRightSeed;
144649
-}
144650
-#endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
144651145477
144652145478
/*
144653145479
** Arguments aIdx, aDistance and aSpare all point to arrays of size
144654145480
** nIdx. The aIdx array contains the set of integers from 0 to
144655145481
** (nIdx-1) in no particular order. This function sorts the values
@@ -144786,11 +145612,10 @@
144786145612
}
144787145613
#endif
144788145614
}
144789145615
}
144790145616
144791
-#if VARIANT_RSTARTREE_SPLIT
144792145617
/*
144793145618
** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
144794145619
*/
144795145620
static int splitNodeStartree(
144796145621
Rtree *pRtree,
@@ -144805,11 +145630,11 @@
144805145630
int *aSpare;
144806145631
int ii;
144807145632
144808145633
int iBestDim = 0;
144809145634
int iBestSplit = 0;
144810
- RtreeDValue fBestMargin = 0.0;
145635
+ RtreeDValue fBestMargin = RTREE_ZERO;
144811145636
144812145637
int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
144813145638
144814145639
aaSorted = (int **)sqlite3_malloc(nByte);
144815145640
if( !aaSorted ){
@@ -144826,13 +145651,13 @@
144826145651
}
144827145652
SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
144828145653
}
144829145654
144830145655
for(ii=0; ii<pRtree->nDim; ii++){
144831
- RtreeDValue margin = 0.0;
144832
- RtreeDValue fBestOverlap = 0.0;
144833
- RtreeDValue fBestArea = 0.0;
145656
+ RtreeDValue margin = RTREE_ZERO;
145657
+ RtreeDValue fBestOverlap = RTREE_ZERO;
145658
+ RtreeDValue fBestArea = RTREE_ZERO;
144834145659
int iBestLeft = 0;
144835145660
int nLeft;
144836145661
144837145662
for(
144838145663
nLeft=RTREE_MINCELLS(pRtree);
@@ -144854,11 +145679,11 @@
144854145679
cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
144855145680
}
144856145681
}
144857145682
margin += cellMargin(pRtree, &left);
144858145683
margin += cellMargin(pRtree, &right);
144859
- overlap = cellOverlap(pRtree, &left, &right, 1, -1);
145684
+ overlap = cellOverlap(pRtree, &left, &right, 1);
144860145685
area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
144861145686
if( (nLeft==RTREE_MINCELLS(pRtree))
144862145687
|| (overlap<fBestOverlap)
144863145688
|| (overlap==fBestOverlap && area<fBestArea)
144864145689
){
@@ -144886,67 +145711,11 @@
144886145711
}
144887145712
144888145713
sqlite3_free(aaSorted);
144889145714
return SQLITE_OK;
144890145715
}
144891
-#endif
144892
-
144893
-#if VARIANT_GUTTMAN_SPLIT
144894
-/*
144895
-** Implementation of the regular R-tree SplitNode from Guttman[1984].
144896
-*/
144897
-static int splitNodeGuttman(
144898
- Rtree *pRtree,
144899
- RtreeCell *aCell,
144900
- int nCell,
144901
- RtreeNode *pLeft,
144902
- RtreeNode *pRight,
144903
- RtreeCell *pBboxLeft,
144904
- RtreeCell *pBboxRight
144905
-){
144906
- int iLeftSeed = 0;
144907
- int iRightSeed = 1;
144908
- int *aiUsed;
144909
- int i;
144910
-
144911
- aiUsed = sqlite3_malloc(sizeof(int)*nCell);
144912
- if( !aiUsed ){
144913
- return SQLITE_NOMEM;
144914
- }
144915
- memset(aiUsed, 0, sizeof(int)*nCell);
144916
-
144917
- PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
144918
-
144919
- memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
144920
- memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
144921
- nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
144922
- nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
144923
- aiUsed[iLeftSeed] = 1;
144924
- aiUsed[iRightSeed] = 1;
144925
-
144926
- for(i=nCell-2; i>0; i--){
144927
- RtreeCell *pNext;
144928
- pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
144929
- RtreeDValue diff =
144930
- cellGrowth(pRtree, pBboxLeft, pNext) -
144931
- cellGrowth(pRtree, pBboxRight, pNext)
144932
- ;
144933
- if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
144934
- || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
144935
- ){
144936
- nodeInsertCell(pRtree, pRight, pNext);
144937
- cellUnion(pRtree, pBboxRight, pNext);
144938
- }else{
144939
- nodeInsertCell(pRtree, pLeft, pNext);
144940
- cellUnion(pRtree, pBboxLeft, pNext);
144941
- }
144942
- }
144943
-
144944
- sqlite3_free(aiUsed);
144945
- return SQLITE_OK;
144946
-}
144947
-#endif
145716
+
144948145717
144949145718
static int updateMapping(
144950145719
Rtree *pRtree,
144951145720
i64 iRowid,
144952145721
RtreeNode *pNode,
@@ -145020,11 +145789,12 @@
145020145789
}
145021145790
145022145791
memset(pLeft->zData, 0, pRtree->iNodeSize);
145023145792
memset(pRight->zData, 0, pRtree->iNodeSize);
145024145793
145025
- rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
145794
+ rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
145795
+ &leftbbox, &rightbbox);
145026145796
if( rc!=SQLITE_OK ){
145027145797
goto splitnode_out;
145028145798
}
145029145799
145030145800
/* Ensure both child nodes have node numbers assigned to them by calling
@@ -145303,11 +146073,11 @@
145303146073
for(iDim=0; iDim<pRtree->nDim; iDim++){
145304146074
aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
145305146075
}
145306146076
145307146077
for(ii=0; ii<nCell; ii++){
145308
- aDistance[ii] = 0.0;
146078
+ aDistance[ii] = RTREE_ZERO;
145309146079
for(iDim=0; iDim<pRtree->nDim; iDim++){
145310146080
RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
145311146081
DCOORD(aCell[ii].aCoord[iDim*2]));
145312146082
aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
145313146083
}
@@ -145369,20 +146139,16 @@
145369146139
nodeReference(pNode);
145370146140
pChild->pParent = pNode;
145371146141
}
145372146142
}
145373146143
if( nodeInsertCell(pRtree, pNode, pCell) ){
145374
-#if VARIANT_RSTARTREE_REINSERT
145375146144
if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
145376146145
rc = SplitNode(pRtree, pNode, pCell, iHeight);
145377146146
}else{
145378146147
pRtree->iReinsertHeight = iHeight;
145379146148
rc = Reinsert(pRtree, pNode, pCell, iHeight);
145380146149
}
145381
-#else
145382
- rc = SplitNode(pRtree, pNode, pCell, iHeight);
145383
-#endif
145384146150
}else{
145385146151
rc = AdjustTree(pRtree, pNode, pCell);
145386146152
if( rc==SQLITE_OK ){
145387146153
if( iHeight==0 ){
145388146154
rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
@@ -145448,11 +146214,11 @@
145448146214
145449146215
/* Obtain a reference to the leaf node that contains the entry
145450146216
** about to be deleted.
145451146217
*/
145452146218
if( rc==SQLITE_OK ){
145453
- rc = findLeafNode(pRtree, iDelete, &pLeaf);
146219
+ rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
145454146220
}
145455146221
145456146222
/* Delete the cell in question from the leaf node. */
145457146223
if( rc==SQLITE_OK ){
145458146224
int rc2;
@@ -145785,11 +146551,12 @@
145785146551
145786146552
if( isCreate ){
145787146553
char *zCreate = sqlite3_mprintf(
145788146554
"CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
145789146555
"CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
145790
-"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
146556
+"CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,"
146557
+ " parentnode INTEGER);"
145791146558
"INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
145792146559
zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
145793146560
);
145794146561
if( !zCreate ){
145795146562
return SQLITE_NOMEM;
@@ -145999,14 +146766,14 @@
145999146766
146000146767
/*
146001146768
** Implementation of a scalar function that decodes r-tree nodes to
146002146769
** human readable strings. This can be used for debugging and analysis.
146003146770
**
146004
-** The scalar function takes two arguments, a blob of data containing
146005
-** an r-tree node, and the number of dimensions the r-tree indexes.
146006
-** For a two-dimensional r-tree structure called "rt", to deserialize
146007
-** all nodes, a statement like:
146771
+** The scalar function takes two arguments: (1) the number of dimensions
146772
+** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
146773
+** an r-tree node. For a two-dimensional r-tree structure called "rt", to
146774
+** deserialize all nodes, a statement like:
146008146775
**
146009146776
** SELECT rtreenode(2, data) FROM rt_node;
146010146777
**
146011146778
** The human readable string takes the form of a Tcl list with one
146012146779
** entry for each cell in the r-tree node. Each entry is itself a
@@ -146035,11 +146802,11 @@
146035146802
nodeGetCell(&tree, &node, ii, &cell);
146036146803
sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
146037146804
nCell = (int)strlen(zCell);
146038146805
for(jj=0; jj<tree.nDim*2; jj++){
146039146806
#ifndef SQLITE_RTREE_INT_ONLY
146040
- sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
146807
+ sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
146041146808
(double)cell.aCoord[jj].f);
146042146809
#else
146043146810
sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
146044146811
cell.aCoord[jj].i);
146045146812
#endif
@@ -146056,10 +146823,19 @@
146056146823
}
146057146824
146058146825
sqlite3_result_text(ctx, zText, -1, sqlite3_free);
146059146826
}
146060146827
146828
+/* This routine implements an SQL function that returns the "depth" parameter
146829
+** from the front of a blob that is an r-tree node. For example:
146830
+**
146831
+** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
146832
+**
146833
+** The depth value is 0 for all nodes other than the root node, and the root
146834
+** node always has nodeno=1, so the example above is the primary use for this
146835
+** routine. This routine is intended for testing and analysis only.
146836
+*/
146061146837
static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
146062146838
UNUSED_PARAMETER(nArg);
146063146839
if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
146064146840
|| sqlite3_value_bytes(apArg[0])<2
146065146841
){
@@ -146098,26 +146874,35 @@
146098146874
146099146875
return rc;
146100146876
}
146101146877
146102146878
/*
146103
-** A version of sqlite3_free() that can be used as a callback. This is used
146104
-** in two places - as the destructor for the blob value returned by the
146105
-** invocation of a geometry function, and as the destructor for the geometry
146106
-** functions themselves.
146879
+** This routine deletes the RtreeGeomCallback object that was attached
146880
+** one of the SQL functions create by sqlite3_rtree_geometry_callback()
146881
+** or sqlite3_rtree_query_callback(). In other words, this routine is the
146882
+** destructor for an RtreeGeomCallback objecct. This routine is called when
146883
+** the corresponding SQL function is deleted.
146107146884
*/
146108
-static void doSqlite3Free(void *p){
146885
+static void rtreeFreeCallback(void *p){
146886
+ RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
146887
+ if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
146109146888
sqlite3_free(p);
146110146889
}
146111146890
146112146891
/*
146113
-** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
146114
-** scalar user function. This C function is the callback used for all such
146115
-** registered SQL functions.
146892
+** Each call to sqlite3_rtree_geometry_callback() or
146893
+** sqlite3_rtree_query_callback() creates an ordinary SQLite
146894
+** scalar function that is implemented by this routine.
146895
+**
146896
+** All this function does is construct an RtreeMatchArg object that
146897
+** contains the geometry-checking callback routines and a list of
146898
+** parameters to this function, then return that RtreeMatchArg object
146899
+** as a BLOB.
146116146900
**
146117
-** The scalar user functions return a blob that is interpreted by r-tree
146118
-** table MATCH operators.
146901
+** The R-Tree MATCH operator will read the returned BLOB, deserialize
146902
+** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
146903
+** out which elements of the R-Tree should be returned by the query.
146119146904
*/
146120146905
static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
146121146906
RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
146122146907
RtreeMatchArg *pBlob;
146123146908
int nBlob;
@@ -146127,45 +146912,68 @@
146127146912
if( !pBlob ){
146128146913
sqlite3_result_error_nomem(ctx);
146129146914
}else{
146130146915
int i;
146131146916
pBlob->magic = RTREE_GEOMETRY_MAGIC;
146132
- pBlob->xGeom = pGeomCtx->xGeom;
146133
- pBlob->pContext = pGeomCtx->pContext;
146917
+ pBlob->cb = pGeomCtx[0];
146134146918
pBlob->nParam = nArg;
146135146919
for(i=0; i<nArg; i++){
146136146920
#ifdef SQLITE_RTREE_INT_ONLY
146137146921
pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
146138146922
#else
146139146923
pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
146140146924
#endif
146141146925
}
146142
- sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
146926
+ sqlite3_result_blob(ctx, pBlob, nBlob, sqlite3_free);
146143146927
}
146144146928
}
146145146929
146146146930
/*
146147146931
** Register a new geometry function for use with the r-tree MATCH operator.
146148146932
*/
146149146933
SQLITE_API int sqlite3_rtree_geometry_callback(
146150
- sqlite3 *db,
146151
- const char *zGeom,
146152
- int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
146153
- void *pContext
146934
+ sqlite3 *db, /* Register SQL function on this connection */
146935
+ const char *zGeom, /* Name of the new SQL function */
146936
+ int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
146937
+ void *pContext /* Extra data associated with the callback */
146154146938
){
146155146939
RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
146156146940
146157146941
/* Allocate and populate the context object. */
146158146942
pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
146159146943
if( !pGeomCtx ) return SQLITE_NOMEM;
146160146944
pGeomCtx->xGeom = xGeom;
146945
+ pGeomCtx->xQueryFunc = 0;
146946
+ pGeomCtx->xDestructor = 0;
146161146947
pGeomCtx->pContext = pContext;
146162
-
146163
- /* Create the new user-function. Register a destructor function to delete
146164
- ** the context object when it is no longer required. */
146165146948
return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
146166
- (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
146949
+ (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
146950
+ );
146951
+}
146952
+
146953
+/*
146954
+** Register a new 2nd-generation geometry function for use with the
146955
+** r-tree MATCH operator.
146956
+*/
146957
+SQLITE_API int sqlite3_rtree_query_callback(
146958
+ sqlite3 *db, /* Register SQL function on this connection */
146959
+ const char *zQueryFunc, /* Name of new SQL function */
146960
+ int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
146961
+ void *pContext, /* Extra data passed into the callback */
146962
+ void (*xDestructor)(void*) /* Destructor for the extra data */
146963
+){
146964
+ RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
146965
+
146966
+ /* Allocate and populate the context object. */
146967
+ pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
146968
+ if( !pGeomCtx ) return SQLITE_NOMEM;
146969
+ pGeomCtx->xGeom = 0;
146970
+ pGeomCtx->xQueryFunc = xQueryFunc;
146971
+ pGeomCtx->xDestructor = xDestructor;
146972
+ pGeomCtx->pContext = pContext;
146973
+ return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
146974
+ (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
146167146975
);
146168146976
}
146169146977
146170146978
#if !SQLITE_CORE
146171146979
#ifdef _WIN32
146172146980
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -222,11 +222,11 @@
222 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
223 ** [sqlite_version()] and [sqlite_source_id()].
224 */
225 #define SQLITE_VERSION "3.8.5"
226 #define SQLITE_VERSION_NUMBER 3008005
227 #define SQLITE_SOURCE_ID "2014-04-18 22:20:31 9a5d38c79d2482a23bcfbc3ff35ca4fa269c768d"
228
229 /*
230 ** CAPI3REF: Run-Time Library Version Numbers
231 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
232 **
@@ -673,11 +673,14 @@
673 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
674 ** after reboot following a crash or power loss, the only bytes in a
675 ** file that were written at the application level might have changed
676 ** and that adjacent bytes, even bytes within the same sector are
677 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
678 ** flag indicate that a file cannot be deleted when open.
 
 
 
679 */
680 #define SQLITE_IOCAP_ATOMIC 0x00000001
681 #define SQLITE_IOCAP_ATOMIC512 0x00000002
682 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
683 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -688,10 +691,11 @@
688 #define SQLITE_IOCAP_ATOMIC64K 0x00000100
689 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
690 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
691 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
692 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
 
693
694 /*
695 ** CAPI3REF: File Locking Levels
696 **
697 ** SQLite uses one of these integer values as the second
@@ -2892,10 +2896,34 @@
2892 ** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
2893 ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2894 ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2895 ** a URI filename, its value overrides any behavior requested by setting
2896 ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2897 ** </ul>
2898 **
2899 ** ^Specifying an unknown parameter in the query component of a URI is not an
2900 ** error. Future versions of SQLite might understand additional query
2901 ** parameters. See "[query parameters with special meaning to SQLite]" for
@@ -2921,12 +2949,13 @@
2921 ** in URI filenames.
2922 ** <tr><td> file:data.db?mode=ro&cache=private <td>
2923 ** Open file "data.db" in the current directory for read-only access.
2924 ** Regardless of whether or not shared-cache mode is enabled by
2925 ** default, use a private cache.
2926 ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
2927 ** Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
 
2928 ** <tr><td> file:data.db?mode=readonly <td>
2929 ** An error. "readonly" is not a valid option for the "mode" parameter.
2930 ** </table>
2931 **
2932 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
@@ -7460,10 +7489,20 @@
7460 #if 0
7461 extern "C" {
7462 #endif
7463
7464 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
 
 
 
 
 
 
 
 
 
 
7465
7466 /*
7467 ** Register a geometry callback named zGeom that can be used as part of an
7468 ** R-Tree geometry query as follows:
7469 **
@@ -7470,15 +7509,11 @@
7470 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7471 */
7472 SQLITE_API int sqlite3_rtree_geometry_callback(
7473 sqlite3 *db,
7474 const char *zGeom,
7475 #ifdef SQLITE_RTREE_INT_ONLY
7476 int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
7477 #else
7478 int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
7479 #endif
7480 void *pContext
7481 );
7482
7483
7484 /*
@@ -7486,15 +7521,64 @@
7486 ** argument to callbacks registered using rtree_geometry_callback().
7487 */
7488 struct sqlite3_rtree_geometry {
7489 void *pContext; /* Copy of pContext passed to s_r_g_c() */
7490 int nParam; /* Size of array aParam[] */
7491 double *aParam; /* Parameters passed to SQL geom function */
7492 void *pUser; /* Callback implementation user data */
7493 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
7494 };
7495
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7496
7497 #if 0
7498 } /* end of the 'extern "C"' block */
7499 #endif
7500
@@ -8417,14 +8501,14 @@
8417 ** Estimated quantities used for query planning are stored as 16-bit
8418 ** logarithms. For quantity X, the value stored is 10*log2(X). This
8419 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
8420 ** But the allowed values are "grainy". Not every value is representable.
8421 ** For example, quantities 16 and 17 are both represented by a LogEst
8422 ** of 40. However, since LogEst quantatites are suppose to be estimates,
8423 ** not exact values, this imprecision is not a problem.
8424 **
8425 ** "LogEst" is short for "Logarithimic Estimate".
8426 **
8427 ** Examples:
8428 ** 1 -> 0 20 -> 43 10000 -> 132
8429 ** 2 -> 10 25 -> 46 25000 -> 146
8430 ** 3 -> 16 100 -> 66 1000000 -> 199
@@ -8916,10 +9000,11 @@
8916 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
8917 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
8918 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
8919 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
8920 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
 
8921
8922 #ifndef NDEBUG
8923 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
8924 #endif
8925
@@ -9870,87 +9955,75 @@
9870 */
9871 #ifndef _SQLITE_OS_H_
9872 #define _SQLITE_OS_H_
9873
9874 /*
9875 ** Figure out if we are dealing with Unix, Windows, or some other
9876 ** operating system. After the following block of preprocess macros,
9877 ** all of SQLITE_OS_UNIX, SQLITE_OS_WIN, and SQLITE_OS_OTHER
9878 ** will defined to either 1 or 0. One of the four will be 1. The other
9879 ** three will be 0.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9880 */
9881 #if defined(SQLITE_OS_OTHER)
9882 # if SQLITE_OS_OTHER==1
9883 # undef SQLITE_OS_UNIX
9884 # define SQLITE_OS_UNIX 0
9885 # undef SQLITE_OS_WIN
9886 # define SQLITE_OS_WIN 0
9887 # else
9888 # undef SQLITE_OS_OTHER
9889 # endif
9890 #endif
9891 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
9892 # define SQLITE_OS_OTHER 0
9893 # ifndef SQLITE_OS_WIN
9894 # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__)
9895 # define SQLITE_OS_WIN 1
9896 # define SQLITE_OS_UNIX 0
9897 # else
9898 # define SQLITE_OS_WIN 0
9899 # define SQLITE_OS_UNIX 1
 
 
 
 
 
 
 
 
9900 # endif
9901 # else
9902 # define SQLITE_OS_UNIX 0
9903 # endif
9904 #else
9905 # ifndef SQLITE_OS_WIN
9906 # define SQLITE_OS_WIN 0
9907 # endif
9908 #endif
9909
9910 #if SQLITE_OS_WIN
9911 # include <windows.h>
9912 #endif
9913
9914 /*
9915 ** Determine if we are dealing with Windows NT.
9916 **
9917 ** We ought to be able to determine if we are compiling for win98 or winNT
9918 ** using the _WIN32_WINNT macro as follows:
9919 **
9920 ** #if defined(_WIN32_WINNT)
9921 ** # define SQLITE_OS_WINNT 1
9922 ** #else
9923 ** # define SQLITE_OS_WINNT 0
9924 ** #endif
9925 **
9926 ** However, vs2005 does not set _WIN32_WINNT by default, as it ought to,
9927 ** so the above test does not work. We'll just assume that everything is
9928 ** winNT unless the programmer explicitly says otherwise by setting
9929 ** SQLITE_OS_WINNT to 0.
9930 */
9931 #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
9932 # define SQLITE_OS_WINNT 1
9933 #endif
9934
9935 /*
9936 ** Determine if we are dealing with WindowsCE - which has a much
9937 ** reduced API.
9938 */
9939 #if defined(_WIN32_WCE)
9940 # define SQLITE_OS_WINCE 1
9941 #else
9942 # define SQLITE_OS_WINCE 0
9943 #endif
9944
9945 /*
9946 ** Determine if we are dealing with WinRT, which provides only a subset of
9947 ** the full Win32 API.
9948 */
9949 #if !defined(SQLITE_OS_WINRT)
9950 # define SQLITE_OS_WINRT 0
9951 #endif
9952
9953 /* If the SET_FULLSYNC macro is not defined above, then make it
9954 ** a no-op
9955 */
9956 #ifndef SET_FULLSYNC
@@ -10845,11 +10918,11 @@
10845 FKey *pFKey; /* Linked list of all foreign keys in this table */
10846 char *zColAff; /* String defining the affinity of each column */
10847 #ifndef SQLITE_OMIT_CHECK
10848 ExprList *pCheck; /* All CHECK constraints */
10849 #endif
10850 tRowcnt nRowEst; /* Estimated rows in table - from sqlite_stat1 table */
10851 int tnum; /* Root BTree node for this table (see note above) */
10852 i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
10853 i16 nCol; /* Number of columns in this table */
10854 u16 nRef; /* Number of pointers to this Table */
10855 LogEst szTabRow; /* Estimated size of each table row in bytes */
@@ -11054,11 +11127,11 @@
11054 ** element.
11055 */
11056 struct Index {
11057 char *zName; /* Name of this index */
11058 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
11059 tRowcnt *aiRowEst; /* From ANALYZE: Est. rows selected by each column */
11060 Table *pTable; /* The SQL table being indexed */
11061 char *zColAff; /* String defining the affinity of each column */
11062 Index *pNext; /* The next index associated with the same table */
11063 Schema *pSchema; /* Schema containing this index */
11064 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
@@ -11068,11 +11141,11 @@
11068 int tnum; /* DB Page containing root of this index */
11069 LogEst szIdxRow; /* Estimated average row size in bytes */
11070 u16 nKeyCol; /* Number of columns forming the key */
11071 u16 nColumn; /* Number of columns stored in the index */
11072 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
11073 unsigned autoIndex:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
11074 unsigned bUnordered:1; /* Use this index for == or IN queries only */
11075 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
11076 unsigned isResized:1; /* True if resizeIndexObject() has been called */
11077 unsigned isCovering:1; /* True if this is a covering index */
11078 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
@@ -11081,10 +11154,20 @@
11081 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
11082 IndexSample *aSample; /* Samples of the left-most key */
11083 #endif
11084 };
11085
 
 
 
 
 
 
 
 
 
 
11086 /*
11087 ** Each sample stored in the sqlite_stat3 table is represented in memory
11088 ** using a structure of this type. See documentation at the top of the
11089 ** analyze.c source file for additional information.
11090 */
@@ -11499,10 +11582,11 @@
11499 #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */
11500 #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
11501 #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
11502 #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
11503 #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
 
11504
11505 /* Allowed return values from sqlite3WhereIsDistinct()
11506 */
11507 #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
11508 #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
@@ -12082,15 +12166,14 @@
12082 int isInit; /* True after initialization has finished */
12083 int inProgress; /* True while initialization in progress */
12084 int isMutexInit; /* True after mutexes are initialized */
12085 int isMallocInit; /* True after malloc is initialized */
12086 int isPCacheInit; /* True after malloc is initialized */
12087 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
12088 int nRefInitMutex; /* Number of users of pInitMutex */
 
12089 void (*xLog)(void*,int,const char*); /* Function for logging */
12090 void *pLogArg; /* First argument to xLog() */
12091 int bLocaltimeFault; /* True to fail localtime() calls */
12092 #ifdef SQLITE_ENABLE_SQLLOG
12093 void(*xSqllog)(void*,sqlite3*,const char*, int);
12094 void *pSqllogArg;
12095 #endif
12096 #ifdef SQLITE_VDBE_COVERAGE
@@ -12098,10 +12181,14 @@
12098 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
12099 */
12100 void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx); /* Callback */
12101 void *pVdbeBranchArg; /* 1st argument */
12102 #endif
 
 
 
 
12103 };
12104
12105 /*
12106 ** This macro is used inside of assert() statements to indicate that
12107 ** the assert is only valid on a well-formed database. Instead of:
@@ -12398,10 +12485,16 @@
12398 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
12399 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
12400 sqlite3_vfs**,char**,char **);
12401 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
12402 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
 
 
 
 
 
 
12403
12404 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
12405 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
12406 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
12407 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
@@ -12466,10 +12559,11 @@
12466 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
12467 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
12468 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
12469 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
12470 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
 
12471 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
12472 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
12473 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
12474 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
12475 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
@@ -13203,19 +13297,26 @@
13203 0, /* isInit */
13204 0, /* inProgress */
13205 0, /* isMutexInit */
13206 0, /* isMallocInit */
13207 0, /* isPCacheInit */
13208 0, /* pInitMutex */
13209 0, /* nRefInitMutex */
 
13210 0, /* xLog */
13211 0, /* pLogArg */
13212 0, /* bLocaltimeFault */
13213 #ifdef SQLITE_ENABLE_SQLLOG
13214 0, /* xSqllog */
13215 0 /* pSqllogArg */
13216 #endif
 
 
 
 
 
 
 
 
13217 };
13218
13219 /*
13220 ** Hash table for global functions - functions common to all
13221 ** database connections. After initialization, this table is
@@ -18934,10 +19035,88 @@
18934 **
18935 *************************************************************************
18936 ** This file contains the C functions that implement mutexes for win32
18937 */
18938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18939 /*
18940 ** The code in this file is only used if we are compiling multithreaded
18941 ** on a win32 system.
18942 */
18943 #ifdef SQLITE_MUTEX_W32
@@ -21789,10 +21968,28 @@
21789 static unsigned dummy = 0;
21790 dummy += (unsigned)x;
21791 }
21792 #endif
21793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21794 #ifndef SQLITE_OMIT_FLOATING_POINT
21795 /*
21796 ** Return true if the floating point value is Not a Number (NaN).
21797 **
21798 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
@@ -23004,12 +23201,12 @@
23004 return b+x[b-a];
23005 }
23006 }
23007
23008 /*
23009 ** Convert an integer into a LogEst. In other words, compute a
23010 ** good approximatation for 10*log2(x).
23011 */
23012 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
23013 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
23014 LogEst y = 40;
23015 if( x<8 ){
@@ -31226,15 +31423,10 @@
31226 **
31227 ** This file contains code that is specific to Windows.
31228 */
31229 #if SQLITE_OS_WIN /* This file is used for Windows only */
31230
31231 #ifdef __CYGWIN__
31232 # include <sys/cygwin.h>
31233 # include <errno.h> /* amalgamator: keep */
31234 #endif
31235
31236 /*
31237 ** Include code that is common to all os_*.c files
31238 */
31239 /************** Include os_common.h in the middle of os_win.c ****************/
31240 /************** Begin file os_common.h ***************************************/
@@ -31443,10 +31635,14 @@
31443
31444 #endif /* !defined(_OS_COMMON_H_) */
31445
31446 /************** End of os_common.h *******************************************/
31447 /************** Continuing where we left off in os_win.c *********************/
 
 
 
 
31448
31449 /*
31450 ** Compiling and using WAL mode requires several APIs that are only
31451 ** available in Windows platforms based on the NT kernel.
31452 */
@@ -33255,10 +33451,36 @@
33255 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
33256 #endif
33257 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
33258 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
33259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33260 /*
33261 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
33262 ** to see if it should be retried. Return TRUE to retry. Return FALSE
33263 ** to give up with an error.
33264 */
@@ -33268,17 +33490,22 @@
33268 if( pError ){
33269 *pError = e;
33270 }
33271 return 0;
33272 }
33273 if( e==ERROR_ACCESS_DENIED ||
33274 e==ERROR_LOCK_VIOLATION ||
33275 e==ERROR_SHARING_VIOLATION ){
 
 
 
 
33276 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
33277 ++*pnRetry;
33278 return 1;
33279 }
 
33280 if( pError ){
33281 *pError = e;
33282 }
33283 return 0;
33284 }
@@ -34213,11 +34440,11 @@
34213 #endif
34214 if( res == 0 ){
34215 pFile->lastErrno = osGetLastError();
34216 /* No need to log a failure to lock */
34217 }
34218 OSTRACE(("READ-LOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
34219 return res;
34220 }
34221
34222 /*
34223 ** Undo a readlock
@@ -34237,11 +34464,11 @@
34237 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
34238 pFile->lastErrno = lastErrno;
34239 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
34240 "winUnlockReadLock", pFile->zPath);
34241 }
34242 OSTRACE(("READ-UNLOCK file=%p, rc=%s\n", pFile->h, sqlite3ErrName(res)));
34243 return res;
34244 }
34245
34246 /*
34247 ** Lock the file with the lock specified by parameter locktype - one
@@ -34312,12 +34539,12 @@
34312 ** around problems caused by indexing and/or anti-virus software on
34313 ** Windows systems.
34314 ** If you are using this code as a model for alternative VFSes, do not
34315 ** copy this retry logic. It is a hack intended for Windows only.
34316 */
34317 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, rc=%s\n",
34318 pFile->h, cnt, sqlite3ErrName(res)));
34319 if( cnt ) sqlite3_win32_sleep(1);
34320 }
34321 gotPendingLock = res;
34322 if( !res ){
34323 lastErrno = osGetLastError();
@@ -34398,29 +34625,29 @@
34398 ** This routine checks if there is a RESERVED lock held on the specified
34399 ** file by this or any other process. If such a lock is held, return
34400 ** non-zero, otherwise zero.
34401 */
34402 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
34403 int rc;
34404 winFile *pFile = (winFile*)id;
34405
34406 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34407 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
34408
34409 assert( id!=0 );
34410 if( pFile->locktype>=RESERVED_LOCK ){
34411 rc = 1;
34412 OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (local)\n", pFile->h, rc));
34413 }else{
34414 rc = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
34415 if( rc ){
34416 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
34417 }
34418 rc = !rc;
34419 OSTRACE(("TEST-WR-LOCK file=%p, rc=%d (remote)\n", pFile->h, rc));
34420 }
34421 *pResOut = rc;
34422 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
34423 pFile->h, pResOut, *pResOut));
34424 return SQLITE_OK;
34425 }
34426
@@ -40231,11 +40458,12 @@
40231 u8 noSync; /* Do not sync the journal if true */
40232 u8 fullSync; /* Do extra syncs of the journal for robustness */
40233 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
40234 u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
40235 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
40236 u8 tempFile; /* zFilename is a temporary file */
 
40237 u8 readOnly; /* True for a read-only database */
40238 u8 memDb; /* True to inhibit all file I/O */
40239
40240 /**************************************************************************
40241 ** The following block contains those class members that change during
@@ -40696,11 +40924,11 @@
40696 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
40697 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
40698 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
40699 if( isOpen(pPager->fd) ){
40700 assert( pPager->eLock>=eLock );
40701 rc = sqlite3OsUnlock(pPager->fd, eLock);
40702 if( pPager->eLock!=UNKNOWN_LOCK ){
40703 pPager->eLock = (u8)eLock;
40704 }
40705 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
40706 }
@@ -40720,11 +40948,11 @@
40720 static int pagerLockDb(Pager *pPager, int eLock){
40721 int rc = SQLITE_OK;
40722
40723 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
40724 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
40725 rc = sqlite3OsLock(pPager->fd, eLock);
40726 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
40727 pPager->eLock = (u8)eLock;
40728 IOTRACE(("LOCK %p %d\n", pPager, eLock))
40729 }
40730 }
@@ -44279,47 +44507,59 @@
44279 **
44280 ** + SQLITE_DEFAULT_PAGE_SIZE,
44281 ** + The value returned by sqlite3OsSectorSize()
44282 ** + The largest page size that can be written atomically.
44283 */
44284 if( rc==SQLITE_OK && !readOnly ){
44285 setSectorSize(pPager);
44286 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
44287 if( szPageDflt<pPager->sectorSize ){
44288 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
44289 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
44290 }else{
44291 szPageDflt = (u32)pPager->sectorSize;
44292 }
44293 }
 
 
44294 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
44295 {
44296 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
44297 int ii;
44298 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
44299 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
44300 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
44301 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
44302 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
44303 szPageDflt = ii;
44304 }
44305 }
 
44306 }
44307 #endif
 
 
 
 
 
44308 }
44309 }else{
44310 /* If a temporary file is requested, it is not opened immediately.
44311 ** In this case we accept the default page size and delay actually
44312 ** opening the file until the first call to OsWrite().
44313 **
44314 ** This branch is also run for an in-memory database. An in-memory
44315 ** database is the same as a temp-file that is never written out to
44316 ** disk and uses an in-memory rollback journal.
 
 
44317 */
 
44318 tempFile = 1;
44319 pPager->eState = PAGER_READER;
44320 pPager->eLock = EXCLUSIVE_LOCK;
 
44321 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
44322 }
44323
44324 /* The following call to PagerSetPagesize() serves to set the value of
44325 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
@@ -44356,13 +44596,10 @@
44356 /* pPager->stmtSize = 0; */
44357 /* pPager->stmtJSize = 0; */
44358 /* pPager->nPage = 0; */
44359 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
44360 /* pPager->state = PAGER_UNLOCK; */
44361 #if 0
44362 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
44363 #endif
44364 /* pPager->errMask = 0; */
44365 pPager->tempFile = (u8)tempFile;
44366 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
44367 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
44368 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
@@ -55106,16 +55343,10 @@
55106 assert( pCur->eState==CURSOR_VALID );
55107 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55108 assert( cursorHoldsMutex(pCur) );
55109 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
55110 assert( pCur->info.nSize>0 );
55111 #if 0
55112 if( pCur->info.nSize==0 ){
55113 btreeParseCell(pCur->apPage[pCur->iPage], pCur->aiIdx[pCur->iPage],
55114 &pCur->info);
55115 }
55116 #endif
55117 *pAmt = pCur->info.nLocal;
55118 return (void*)(pCur->info.pCell + pCur->info.nHeader);
55119 }
55120
55121
@@ -59414,10 +59645,17 @@
59414 */
59415 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
59416 assert( mask==BTREE_BULKLOAD || mask==0 );
59417 pCsr->hints = mask;
59418 }
 
 
 
 
 
 
 
59419
59420 /************** End of btree.c ***********************************************/
59421 /************** Begin file backup.c ******************************************/
59422 /*
59423 ** 2009 January 28
@@ -70686,11 +70924,11 @@
70686 **
70687 ** Reposition cursor P1 so that it points to the smallest entry that
70688 ** is greater than or equal to the key value. If there are no records
70689 ** greater than or equal to the key and P2 is not zero, then jump to P2.
70690 **
70691 ** See also: Found, NotFound, Distinct, SeekLt, SeekGt, SeekLe
70692 */
70693 /* Opcode: SeekGt P1 P2 P3 P4 *
70694 ** Synopsis: key=r[P3@P4]
70695 **
70696 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70700,11 +70938,11 @@
70700 **
70701 ** Reposition cursor P1 so that it points to the smallest entry that
70702 ** is greater than the key value. If there are no records greater than
70703 ** the key and P2 is not zero, then jump to P2.
70704 **
70705 ** See also: Found, NotFound, Distinct, SeekLt, SeekGe, SeekLe
70706 */
70707 /* Opcode: SeekLt P1 P2 P3 P4 *
70708 ** Synopsis: key=r[P3@P4]
70709 **
70710 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70714,11 +70952,11 @@
70714 **
70715 ** Reposition cursor P1 so that it points to the largest entry that
70716 ** is less than the key value. If there are no records less than
70717 ** the key and P2 is not zero, then jump to P2.
70718 **
70719 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLe
70720 */
70721 /* Opcode: SeekLe P1 P2 P3 P4 *
70722 ** Synopsis: key=r[P3@P4]
70723 **
70724 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70728,11 +70966,11 @@
70728 **
70729 ** Reposition cursor P1 so that it points to the largest entry that
70730 ** is less than or equal to the key value. If there are no records
70731 ** less than or equal to the key and P2 is not zero, then jump to P2.
70732 **
70733 ** See also: Found, NotFound, Distinct, SeekGt, SeekGe, SeekLt
70734 */
70735 case OP_SeekLT: /* jump, in3 */
70736 case OP_SeekLE: /* jump, in3 */
70737 case OP_SeekGE: /* jump, in3 */
70738 case OP_SeekGT: { /* jump, in3 */
@@ -71454,10 +71692,11 @@
71454
71455 pOut = &aMem[pOp->p2];
71456 pC = p->apCsr[pOp->p1];
71457 assert( isSorter(pC) );
71458 rc = sqlite3VdbeSorterRowkey(pC, pOut);
 
71459 break;
71460 }
71461
71462 /* Opcode: RowData P1 P2 * * *
71463 ** Synopsis: r[P2]=data
@@ -73502,11 +73741,11 @@
73502 #ifdef SQLITE_USE_FCNTL_TRACE
73503 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
73504 if( zTrace ){
73505 int i;
73506 for(i=0; i<db->nDb; i++){
73507 if( MASKBIT(i) & p->btreeMask)==0 ) continue;
73508 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
73509 }
73510 }
73511 #endif /* SQLITE_USE_FCNTL_TRACE */
73512 #ifdef SQLITE_DEBUG
@@ -73545,12 +73784,12 @@
73545 *****************************************************************************/
73546 }
73547
73548 #ifdef VDBE_PROFILE
73549 {
73550 u64 elapsed = sqlite3Hwtime() - start;
73551 pOp->cycles += elapsed;
73552 pOp->cnt++;
73553 }
73554 #endif
73555
73556 /* The following code adds nothing to the actual functionality
@@ -74457,11 +74696,10 @@
74457 nRead = (int)(pSorter->iWriteOff - iStart);
74458 }
74459 rc = sqlite3OsRead(
74460 pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
74461 );
74462 assert( rc!=SQLITE_IOERR_SHORT_READ );
74463 }
74464
74465 if( rc==SQLITE_OK ){
74466 u64 nByte; /* Size of PMA in bytes */
74467 pIter->iEof = pSorter->iWriteOff;
@@ -83420,11 +83658,11 @@
83420 nCol = pIdx->nKeyCol;
83421 aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
83422 if( aGotoChng==0 ) continue;
83423
83424 /* Populate the register containing the index name. */
83425 if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
83426 zIdxName = pTab->zName;
83427 }else{
83428 zIdxName = pIdx->zName;
83429 }
83430 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
@@ -83789,10 +84027,11 @@
83789 */
83790 static void decodeIntArray(
83791 char *zIntArray, /* String containing int array to decode */
83792 int nOut, /* Number of slots in aOut[] */
83793 tRowcnt *aOut, /* Store integers here */
 
83794 Index *pIndex /* Handle extra flags for this index, if not NULL */
83795 ){
83796 char *z = zIntArray;
83797 int c;
83798 int i;
@@ -83807,11 +84046,21 @@
83807 v = 0;
83808 while( (c=z[0])>='0' && c<='9' ){
83809 v = v*10 + c - '0';
83810 z++;
83811 }
83812 aOut[i] = v;
 
 
 
 
 
 
 
 
 
 
83813 if( *z==' ' ) z++;
83814 }
83815 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
83816 assert( pIndex!=0 );
83817 #else
@@ -83863,16 +84112,16 @@
83863 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
83864 }
83865 z = argv[2];
83866
83867 if( pIndex ){
83868 decodeIntArray((char*)z, pIndex->nKeyCol+1, pIndex->aiRowEst, pIndex);
83869 if( pIndex->pPartIdxWhere==0 ) pTable->nRowEst = pIndex->aiRowEst[0];
83870 }else{
83871 Index fakeIdx;
83872 fakeIdx.szIdxRow = pTable->szTabRow;
83873 decodeIntArray((char*)z, 1, &pTable->nRowEst, &fakeIdx);
83874 pTable->szTabRow = fakeIdx.szIdxRow;
83875 }
83876
83877 return 0;
83878 }
@@ -84060,13 +84309,13 @@
84060 if( pIdx!=pPrevIdx ){
84061 initAvgEq(pPrevIdx);
84062 pPrevIdx = pIdx;
84063 }
84064 pSample = &pIdx->aSample[pIdx->nSample];
84065 decodeIntArray((char*)sqlite3_column_text(pStmt,1), nCol, pSample->anEq, 0);
84066 decodeIntArray((char*)sqlite3_column_text(pStmt,2), nCol, pSample->anLt, 0);
84067 decodeIntArray((char*)sqlite3_column_text(pStmt,3), nCol, pSample->anDLt,0);
84068
84069 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
84070 ** This is in case the sample record is corrupted. In that case, the
84071 ** sqlite3VdbeRecordCompare() may read up to two varints past the
84072 ** end of the allocated buffer before it realizes it is dealing with
@@ -85776,11 +86025,11 @@
85776 /*
85777 ** Return the PRIMARY KEY index of a table
85778 */
85779 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
85780 Index *p;
85781 for(p=pTab->pIndex; p && p->autoIndex!=2; p=p->pNext){}
85782 return p;
85783 }
85784
85785 /*
85786 ** Return the column of index pIdx that corresponds to table
@@ -85924,11 +86173,11 @@
85924 }
85925 pTable->zName = zName;
85926 pTable->iPKey = -1;
85927 pTable->pSchema = db->aDb[iDb].pSchema;
85928 pTable->nRef = 1;
85929 pTable->nRowEst = 1048576;
85930 assert( pParse->pNewTable==0 );
85931 pParse->pNewTable = pTable;
85932
85933 /* If this is the magic sqlite_sequence table used by autoincrement,
85934 ** then record a pointer to this table in the main database structure
@@ -86305,11 +86554,11 @@
86305 Index *p;
86306 if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
86307 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
86308 0, sortOrder, 0);
86309 if( p ){
86310 p->autoIndex = 2;
86311 if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
86312 }
86313 pList = 0;
86314 }
86315
@@ -86325,11 +86574,14 @@
86325 Parse *pParse, /* Parsing context */
86326 Expr *pCheckExpr /* The check expression */
86327 ){
86328 #ifndef SQLITE_OMIT_CHECK
86329 Table *pTab = pParse->pNewTable;
86330 if( pTab && !IN_DECLARE_VTAB ){
 
 
 
86331 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
86332 if( pParse->constraintName.n ){
86333 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
86334 }
86335 }else
@@ -86677,11 +86929,11 @@
86677 pTab->aCol[pTab->iPKey].zName);
86678 pList->a[0].sortOrder = pParse->iPkSortOrder;
86679 assert( pParse->pNewTable==pTab );
86680 pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
86681 if( pPk==0 ) return;
86682 pPk->autoIndex = 2;
86683 pTab->iPKey = -1;
86684 }else{
86685 pPk = sqlite3PrimaryKeyIndex(pTab);
86686 }
86687 pPk->isCovering = 1;
@@ -86700,11 +86952,11 @@
86700 /* Update the in-memory representation of all UNIQUE indices by converting
86701 ** the final rowid column into one or more columns of the PRIMARY KEY.
86702 */
86703 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
86704 int n;
86705 if( pIdx->autoIndex==2 ) continue;
86706 for(i=n=0; i<nPk; i++){
86707 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
86708 }
86709 if( n==0 ){
86710 /* This index is a superset of the primary key */
@@ -87749,19 +88001,19 @@
87749 Index *p; /* Allocated index object */
87750 int nByte; /* Bytes of space for Index object + arrays */
87751
87752 nByte = ROUND8(sizeof(Index)) + /* Index structure */
87753 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
87754 ROUND8(sizeof(tRowcnt)*(nCol+1) + /* Index.aiRowEst */
87755 sizeof(i16)*nCol + /* Index.aiColumn */
87756 sizeof(u8)*nCol); /* Index.aSortOrder */
87757 p = sqlite3DbMallocZero(db, nByte + nExtra);
87758 if( p ){
87759 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
87760 p->azColl = (char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
87761 p->aiRowEst = (tRowcnt*)pExtra; pExtra += sizeof(tRowcnt)*(nCol+1);
87762 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
87763 p->aSortOrder = (u8*)pExtra;
87764 p->nColumn = nCol;
87765 p->nKeyCol = nCol - 1;
87766 *ppExtra = ((char*)p) + nByte;
87767 }
@@ -87780,11 +88032,11 @@
87780 ** is a primary key or unique-constraint on the most recent column added
87781 ** to the table currently under construction.
87782 **
87783 ** If the index is created successfully, return a pointer to the new Index
87784 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
87785 ** as the tables primary key (Index.autoIndex==2).
87786 */
87787 SQLITE_PRIVATE Index *sqlite3CreateIndex(
87788 Parse *pParse, /* All information about this parse */
87789 Token *pName1, /* First part of index name. May be NULL */
87790 Token *pName2, /* Second part of index name. May be NULL */
@@ -87987,19 +88239,19 @@
87987 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
87988 nName + nExtra + 1, &zExtra);
87989 if( db->mallocFailed ){
87990 goto exit_create_index;
87991 }
87992 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowEst) );
87993 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
87994 pIndex->zName = zExtra;
87995 zExtra += nName + 1;
87996 memcpy(pIndex->zName, zName, nName+1);
87997 pIndex->pTable = pTab;
87998 pIndex->onError = (u8)onError;
87999 pIndex->uniqNotNull = onError!=OE_None;
88000 pIndex->autoIndex = (u8)(pName==0);
88001 pIndex->pSchema = db->aDb[iDb].pSchema;
88002 pIndex->nKeyCol = pList->nExpr;
88003 if( pPIWhere ){
88004 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
88005 pIndex->pPartIdxWhere = pPIWhere;
@@ -88107,11 +88359,11 @@
88107 */
88108 Index *pIdx;
88109 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
88110 int k;
88111 assert( pIdx->onError!=OE_None );
88112 assert( pIdx->autoIndex );
88113 assert( pIndex->onError!=OE_None );
88114
88115 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
88116 for(k=0; k<pIdx->nKeyCol; k++){
88117 const char *z1;
@@ -88268,11 +88520,11 @@
88268 **
88269 ** aiRowEst[0] is suppose to contain the number of elements in the index.
88270 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
88271 ** number of rows in the table that match any particular value of the
88272 ** first column of the index. aiRowEst[2] is an estimate of the number
88273 ** of rows that match any particular combiniation of the first 2 columns
88274 ** of the index. And so forth. It must always be the case that
88275 *
88276 ** aiRowEst[N]<=aiRowEst[N-1]
88277 ** aiRowEst[N]>=1
88278 **
@@ -88279,24 +88531,31 @@
88279 ** Apart from that, we have little to go on besides intuition as to
88280 ** how aiRowEst[] should be initialized. The numbers generated here
88281 ** are based on typical values found in actual indices.
88282 */
88283 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
88284 tRowcnt *a = pIdx->aiRowEst;
 
 
 
88285 int i;
88286 tRowcnt n;
88287 assert( a!=0 );
88288 a[0] = pIdx->pTable->nRowEst;
88289 if( a[0]<10 ) a[0] = 10;
88290 n = 10;
88291 for(i=1; i<=pIdx->nKeyCol; i++){
88292 a[i] = n;
88293 if( n>5 ) n--;
88294 }
88295 if( pIdx->onError!=OE_None ){
88296 a[pIdx->nKeyCol] = 1;
88297 }
 
 
 
 
88298 }
88299
88300 /*
88301 ** This routine will drop an existing named index. This routine
88302 ** implements the DROP INDEX statement.
@@ -88323,11 +88582,11 @@
88323 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
88324 }
88325 pParse->checkSchema = 1;
88326 goto exit_drop_index;
88327 }
88328 if( pIndex->autoIndex ){
88329 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
88330 "or PRIMARY KEY constraint cannot be dropped", 0);
88331 goto exit_drop_index;
88332 }
88333 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
@@ -88982,11 +89241,12 @@
88982 sqlite3StrAccumAppend(&errMsg, ".", 1);
88983 sqlite3StrAccumAppendAll(&errMsg, zCol);
88984 }
88985 zErr = sqlite3StrAccumFinish(&errMsg);
88986 sqlite3HaltConstraint(pParse,
88987 (pIdx->autoIndex==2)?SQLITE_CONSTRAINT_PRIMARYKEY:SQLITE_CONSTRAINT_UNIQUE,
 
88988 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
88989 }
88990
88991
88992 /*
@@ -92116,11 +92376,11 @@
92116 }
92117 if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
92118 }
92119 zVal = (char*)sqlite3_value_text(argv[0]);
92120 nVal = sqlite3_value_bytes(argv[0]);
92121 if( nVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
92122 }
92123 }
92124 static void groupConcatFinalize(sqlite3_context *context){
92125 StrAccum *pAccum;
92126 pAccum = sqlite3_aggregate_context(context, 0);
@@ -92560,12 +92820,12 @@
92560 ** column of pFKey, then this index is a winner. */
92561
92562 if( zKey==0 ){
92563 /* If zKey is NULL, then this foreign key is implicitly mapped to
92564 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
92565 ** identified by the test (Index.autoIndex==2). */
92566 if( pIdx->autoIndex==2 ){
92567 if( aiCol ){
92568 int i;
92569 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
92570 }
92571 break;
@@ -94306,10 +94566,11 @@
94306 }
94307 }
94308 if( j>=pTab->nCol ){
94309 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
94310 ipkColumn = i;
 
94311 }else{
94312 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
94313 pTabList, 0, pColumn->a[i].zName);
94314 pParse->checkSchema = 1;
94315 goto insert_cleanup;
@@ -95154,11 +95415,11 @@
95154 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
95155 ** of the matched index row are different from the original PRIMARY
95156 ** KEY values of this row before the update. */
95157 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
95158 int op = OP_Ne;
95159 int regCmp = (pIdx->autoIndex==2 ? regIdx : regR);
95160
95161 for(i=0; i<pPk->nKeyCol; i++){
95162 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
95163 x = pPk->aiColumn[i];
95164 if( i==(pPk->nKeyCol-1) ){
@@ -95255,11 +95516,11 @@
95255 VdbeCoverage(v);
95256 }
95257 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
95258 pik_flags = 0;
95259 if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
95260 if( pIdx->autoIndex==2 && !HasRowid(pTab) ){
95261 assert( pParse->nested==0 );
95262 pik_flags |= OPFLAG_NCHANGE;
95263 }
95264 if( pik_flags ) sqlite3VdbeChangeP5(v, pik_flags);
95265 }
@@ -95341,11 +95602,11 @@
95341 }
95342 if( piIdxCur ) *piIdxCur = iBase;
95343 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
95344 int iIdxCur = iBase++;
95345 assert( pIdx->pSchema==pTab->pSchema );
95346 if( pIdx->autoIndex==2 && !HasRowid(pTab) && piDataCur ){
95347 *piDataCur = iIdxCur;
95348 }
95349 if( aToOpen==0 || aToOpen[i+1] ){
95350 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
95351 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
@@ -95557,18 +95818,27 @@
95557 }
95558 if( pDest->iPKey!=pSrc->iPKey ){
95559 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
95560 }
95561 for(i=0; i<pDest->nCol; i++){
95562 if( pDest->aCol[i].affinity!=pSrc->aCol[i].affinity ){
 
 
95563 return 0; /* Affinity must be the same on all columns */
95564 }
95565 if( !xferCompatibleCollation(pDest->aCol[i].zColl, pSrc->aCol[i].zColl) ){
95566 return 0; /* Collating sequence must be the same on all columns */
95567 }
95568 if( pDest->aCol[i].notNull && !pSrc->aCol[i].notNull ){
95569 return 0; /* tab2 must be NOT NULL if tab1 is */
 
 
 
 
 
 
 
95570 }
95571 }
95572 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
95573 if( pDestIdx->onError!=OE_None ){
95574 destHasUniqueIdx = 1;
@@ -98583,17 +98853,19 @@
98583 Table *pTab = sqliteHashData(i);
98584 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
98585 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
98586 sqlite3VdbeAddOp2(v, OP_Integer,
98587 (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
98588 sqlite3VdbeAddOp2(v, OP_Integer, (int)pTab->nRowEst, 4);
 
98589 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
98590 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
98591 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
98592 sqlite3VdbeAddOp2(v, OP_Integer,
98593 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
98594 sqlite3VdbeAddOp2(v, OP_Integer, (int)pIdx->aiRowEst[0], 4);
 
98595 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
98596 }
98597 }
98598 }
98599 break;
@@ -100760,19 +101032,21 @@
100760 Select *pSelect, /* The whole SELECT statement */
100761 int regData /* Register holding data to be sorted */
100762 ){
100763 Vdbe *v = pParse->pVdbe;
100764 int nExpr = pSort->pOrderBy->nExpr;
100765 int regBase = sqlite3GetTempRange(pParse, nExpr+2);
100766 int regRecord = sqlite3GetTempReg(pParse);
100767 int nOBSat = pSort->nOBSat;
100768 int op;
 
 
100769 sqlite3ExprCacheClear(pParse);
100770 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, 0);
100771 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
100772 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
100773 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nExpr+2-nOBSat, regRecord);
100774 if( nOBSat>0 ){
100775 int regPrevKey; /* The first nOBSat columns of the previous row */
100776 int addrFirst; /* Address of the OP_IfNot opcode */
100777 int addrJmp; /* Address of the OP_Jump opcode */
100778 VdbeOp *pOp; /* Opcode that opens the sorter */
@@ -100805,14 +101079,10 @@
100805 op = OP_SorterInsert;
100806 }else{
100807 op = OP_IdxInsert;
100808 }
100809 sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord);
100810 if( nOBSat==0 ){
100811 sqlite3ReleaseTempReg(pParse, regRecord);
100812 sqlite3ReleaseTempRange(pParse, regBase, nExpr+2);
100813 }
100814 if( pSelect->iLimit ){
100815 int addr1, addr2;
100816 int iLimit;
100817 if( pSelect->iOffset ){
100818 iLimit = pSelect->iOffset+1;
@@ -101984,11 +102254,11 @@
101984 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
101985 ** is disabled */
101986 assert( db->lookaside.bEnabled==0 );
101987 pTab->nRef = 1;
101988 pTab->zName = 0;
101989 pTab->nRowEst = 1048576;
101990 selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
101991 selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
101992 pTab->iPKey = -1;
101993 if( db->mallocFailed ){
101994 sqlite3DeleteTable(db, pTab);
@@ -104123,11 +104393,11 @@
104123 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
104124 if( pTab==0 ) return WRC_Abort;
104125 pTab->nRef = 1;
104126 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
104127 pTab->iPKey = -1;
104128 pTab->nRowEst = 1048576;
104129 pTab->tabFlags |= TF_Ephemeral;
104130 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
104131 if( db->mallocFailed ) return SQLITE_NOMEM;
104132 assert( pFrom->pSelect );
104133
@@ -104299,11 +104569,11 @@
104299 pTab->nRef = 1;
104300 pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
104301 while( pSel->pPrior ){ pSel = pSel->pPrior; }
104302 selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
104303 pTab->iPKey = -1;
104304 pTab->nRowEst = 1048576;
104305 pTab->tabFlags |= TF_Ephemeral;
104306 #endif
104307 }else{
104308 /* An ordinary table or view name in the FROM clause */
104309 assert( pFrom->pTab==0 );
@@ -104794,14 +105064,15 @@
104794 Parse *pParse, /* Parse context */
104795 Table *pTab, /* Table being queried */
104796 Index *pIdx /* Index used to optimize scan, or NULL */
104797 ){
104798 if( pParse->explain==2 ){
 
104799 char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
104800 pTab->zName,
104801 pIdx ? " USING COVERING INDEX " : "",
104802 pIdx ? pIdx->zName : ""
104803 );
104804 sqlite3VdbeAddOp4(
104805 pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
104806 );
104807 }
@@ -104949,11 +105220,11 @@
104949 VdbeComment((v, "%s", pItem->pTab->zName));
104950 pItem->addrFillSub = addrTop;
104951 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
104952 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104953 sqlite3Select(pParse, pSub, &dest);
104954 pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
104955 pItem->viaCoroutine = 1;
104956 pItem->regResult = dest.iSdst;
104957 sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
104958 sqlite3VdbeJumpHere(v, addrTop-1);
104959 sqlite3ClearTempRegCache(pParse);
@@ -104980,11 +105251,11 @@
104980 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
104981 }
104982 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
104983 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
104984 sqlite3Select(pParse, pSub, &dest);
104985 pItem->pTab->nRowEst = (unsigned)pSub->nSelectRow;
104986 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
104987 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
104988 VdbeComment((v, "end %s", pItem->pTab->zName));
104989 sqlite3VdbeChangeP1(v, topAddr, retAddr);
104990 sqlite3ClearTempRegCache(pParse);
@@ -105013,22 +105284,10 @@
105013 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
105014 return rc;
105015 }
105016 #endif
105017
105018 /* If there is both a GROUP BY and an ORDER BY clause and they are
105019 ** identical, then disable the ORDER BY clause since the GROUP BY
105020 ** will cause elements to come out in the correct order. This is
105021 ** an optimization - the correct answer should result regardless.
105022 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER
105023 ** to disable this optimization for testing purposes.
105024 */
105025 if( sqlite3ExprListCompare(p->pGroupBy, sSort.pOrderBy, -1)==0
105026 && OptimizationEnabled(db, SQLITE_GroupByOrder) ){
105027 sSort.pOrderBy = 0;
105028 }
105029
105030 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
105031 ** if the select-list is the same as the ORDER BY list, then this query
105032 ** can be rewritten as a GROUP BY. In other words, this:
105033 **
105034 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
@@ -105153,10 +105412,11 @@
105153 int iAbortFlag; /* Mem address which causes query abort if positive */
105154 int groupBySort; /* Rows come from source in GROUP BY order */
105155 int addrEnd; /* End of processing for this SELECT */
105156 int sortPTab = 0; /* Pseudotable used to decode sorting results */
105157 int sortOut = 0; /* Output register from the sorter */
 
105158
105159 /* Remove any and all aliases between the result set and the
105160 ** GROUP BY clause.
105161 */
105162 if( pGroupBy ){
@@ -105172,10 +105432,22 @@
105172 if( p->nSelectRow>100 ) p->nSelectRow = 100;
105173 }else{
105174 p->nSelectRow = 1;
105175 }
105176
 
 
 
 
 
 
 
 
 
 
 
 
105177
105178 /* Create a label to jump to when we want to abort the query */
105179 addrEnd = sqlite3VdbeMakeLabel(v);
105180
105181 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
@@ -105252,11 +105524,12 @@
105252 ** it might be a single loop that uses an index to extract information
105253 ** in the right order to begin with.
105254 */
105255 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
105256 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
105257 WHERE_GROUPBY, 0);
 
105258 if( pWInfo==0 ) goto select_end;
105259 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
105260 /* The optimizer is able to deliver rows in group by order so
105261 ** we do not have to sort. The OP_OpenEphemeral table will be
105262 ** cancelled later because we still need to use the pKeyInfo
@@ -105317,10 +105590,25 @@
105317 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
105318 sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
105319 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
105320 sAggInfo.useSortingIdx = 1;
105321 sqlite3ExprCacheClear(pParse);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105322 }
105323
105324 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
105325 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
105326 ** Then compare the current GROUP BY terms against the GROUP BY terms
@@ -107204,11 +107492,11 @@
107204 */
107205 pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
107206 iIdxCur = iDataCur+1;
107207 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
107208 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
107209 if( pIdx->autoIndex==2 && pPk!=0 ){
107210 iDataCur = pParse->nTab;
107211 pTabList->a[0].iCursor = iDataCur;
107212 }
107213 pParse->nTab++;
107214 }
@@ -109680,10 +109968,11 @@
109680 WhereLoop *pLoops; /* List of all WhereLoop objects */
109681 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
109682 LogEst nRowOut; /* Estimated number of output rows */
109683 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
109684 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
 
109685 u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
109686 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
109687 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
109688 u8 nLevel; /* Number of nested loop */
109689 int iTop; /* The very beginning of the WHERE loop */
@@ -109739,10 +110028,11 @@
109739 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
109740 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
109741 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
109742 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
109743 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
 
109744
109745 /************** End of whereInt.h ********************************************/
109746 /************** Continuing where we left off in where.c **********************/
109747
109748 /*
@@ -109951,11 +110241,11 @@
109951 }
109952 pTerm = &pWC->a[idx = pWC->nTerm++];
109953 if( p && ExprHasProperty(p, EP_Unlikely) ){
109954 pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
109955 }else{
109956 pTerm->truthProb = -1;
109957 }
109958 pTerm->pExpr = sqlite3ExprSkipCollate(p);
109959 pTerm->wtFlags = wtFlags;
109960 pTerm->pWC = pWC;
109961 pTerm->iParent = -1;
@@ -111680,11 +111970,12 @@
111680 tRowcnt iLower, iUpper, iGap;
111681 if( i==0 ){
111682 iLower = 0;
111683 iUpper = aSample[0].anLt[iCol];
111684 }else{
111685 iUpper = i>=pIdx->nSample ? pIdx->aiRowEst[0] : aSample[i].anLt[iCol];
 
111686 iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
111687 }
111688 aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
111689 if( iLower>=iUpper ){
111690 iGap = 0;
@@ -111698,10 +111989,33 @@
111698 }
111699 aStat[0] = iLower + iGap;
111700 }
111701 }
111702 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111703
111704 /*
111705 ** This function is used to estimate the number of rows that will be visited
111706 ** by scanning an index for a range of values. The range may have an upper
111707 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -111791,11 +112105,11 @@
111791 aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
111792 }
111793 /* Determine iLower and iUpper using ($P) only. */
111794 if( nEq==0 ){
111795 iLower = 0;
111796 iUpper = p->aiRowEst[0];
111797 }else{
111798 /* Note: this call could be optimized away - since the same values must
111799 ** have been requested when testing key $P in whereEqualScanEst(). */
111800 whereKeyStats(pParse, p, pRec, 0, a);
111801 iLower = a[0];
@@ -111851,21 +112165,22 @@
111851 #else
111852 UNUSED_PARAMETER(pParse);
111853 UNUSED_PARAMETER(pBuilder);
111854 #endif
111855 assert( pLower || pUpper );
111856 /* TUNING: Each inequality constraint reduces the search space 4-fold.
111857 ** A BETWEEN operator, therefore, reduces the search space 16-fold */
111858 nNew = nOut;
111859 if( pLower && (pLower->wtFlags & TERM_VNULL)==0 ){
111860 nNew -= 20; assert( 20==sqlite3LogEst(4) );
111861 nOut--;
111862 }
111863 if( pUpper ){
111864 nNew -= 20; assert( 20==sqlite3LogEst(4) );
111865 nOut--;
111866 }
 
111867 if( nNew<10 ) nNew = 10;
111868 if( nNew<nOut ) nOut = nNew;
111869 pLoop->nOut = (LogEst)nOut;
111870 return rc;
111871 }
@@ -111958,26 +112273,27 @@
111958 WhereLoopBuilder *pBuilder,
111959 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
111960 tRowcnt *pnRow /* Write the revised row estimate here */
111961 ){
111962 Index *p = pBuilder->pNew->u.btree.pIndex;
 
111963 int nRecValid = pBuilder->nRecValid;
111964 int rc = SQLITE_OK; /* Subfunction return code */
111965 tRowcnt nEst; /* Number of rows for a single term */
111966 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
111967 int i; /* Loop counter */
111968
111969 assert( p->aSample!=0 );
111970 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
111971 nEst = p->aiRowEst[0];
111972 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
111973 nRowEst += nEst;
111974 pBuilder->nRecValid = nRecValid;
111975 }
111976
111977 if( rc==SQLITE_OK ){
111978 if( nRowEst > p->aiRowEst[0] ) nRowEst = p->aiRowEst[0];
111979 *pnRow = nRowEst;
111980 WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst));
111981 }
111982 assert( pBuilder->nRecValid==nRecValid );
111983 return rc;
@@ -112416,17 +112732,24 @@
112416 zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
112417 }
112418 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
112419 && ALWAYS(pLoop->u.btree.pIndex!=0)
112420 ){
 
 
112421 char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
112422 zMsg = sqlite3MAppendf(db, zMsg,
112423 ((flags & WHERE_AUTO_INDEX) ?
112424 "%s USING AUTOMATIC %sINDEX%.0s%s" :
112425 "%s USING %sINDEX %s%s"),
112426 zMsg, ((flags & WHERE_IDX_ONLY) ? "COVERING " : ""),
112427 pLoop->u.btree.pIndex->zName, zWhere);
 
 
 
 
 
112428 sqlite3DbFree(db, zWhere);
112429 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
112430 zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
112431
112432 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
@@ -112914,11 +113237,11 @@
112914 }else if( HasRowid(pIdx->pTable) ){
112915 iRowidReg = ++pParse->nMem;
112916 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
112917 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
112918 sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
112919 }else{
112920 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
112921 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
112922 for(j=0; j<pPk->nKeyCol; j++){
112923 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
112924 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
@@ -112984,10 +113307,14 @@
112984 **
112985 ** Return 2 # Jump back to the Gosub
112986 **
112987 ** B: <after the loop>
112988 **
 
 
 
 
112989 */
112990 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
112991 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
112992 Index *pCov = 0; /* Potential covering index (or NULL) */
112993 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
@@ -112998,10 +113325,11 @@
112998 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
112999 int iRetInit; /* Address of regReturn init */
113000 int untestedTerms = 0; /* Some terms not completely tested */
113001 int ii; /* Loop counter */
113002 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
 
113003
113004 pTerm = pLoop->aLTerm[0];
113005 assert( pTerm!=0 );
113006 assert( pTerm->eOperator & WO_OR );
113007 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
@@ -113030,11 +113358,12 @@
113030 }else{
113031 pOrTab = pWInfo->pTabList;
113032 }
113033
113034 /* Initialize the rowset register to contain NULL. An SQL NULL is
113035 ** equivalent to an empty rowset.
 
113036 **
113037 ** Also initialize regReturn to contain the address of the instruction
113038 ** immediately following the OP_Return at the bottom of the loop. This
113039 ** is required in a few obscure LEFT JOIN cases where control jumps
113040 ** over the top of the loop into the body of it. In this case the
@@ -113041,13 +113370,20 @@
113041 ** correct response for the end-of-loop code (the OP_Return) is to
113042 ** fall through to the next instruction, just as an OP_Next does if
113043 ** called on an uninitialized cursor.
113044 */
113045 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
113046 regRowset = ++pParse->nMem;
 
 
 
 
 
 
 
 
113047 regRowid = ++pParse->nMem;
113048 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
113049 }
113050 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
113051
113052 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
113053 ** Then for every term xN, evaluate as the subexpression: xN AND z
@@ -113079,15 +113415,20 @@
113079 if( pAndExpr ){
113080 pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
113081 }
113082 }
113083
 
 
 
 
113084 for(ii=0; ii<pOrWc->nTerm; ii++){
113085 WhereTerm *pOrTerm = &pOrWc->a[ii];
113086 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
113087 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
113088 Expr *pOrExpr = pOrTerm->pExpr;
 
113089 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
113090 pAndExpr->pLeft = pOrExpr;
113091 pOrExpr = pAndExpr;
113092 }
113093 /* Loop through table entries that match term pOrTerm. */
@@ -113098,20 +113439,66 @@
113098 if( pSubWInfo ){
113099 WhereLoop *pSubLoop;
113100 explainOneScan(
113101 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
113102 );
 
 
 
 
 
113103 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
113104 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
113105 int r;
113106 r = sqlite3ExprCodeGetColumn(pParse, pTabItem->pTab, -1, iCur,
113107 regRowid, 0);
113108 sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset,
113109 sqlite3VdbeCurrentAddr(v)+2, r, iSet);
113110 VdbeCoverage(v);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113111 }
 
 
113112 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
 
 
 
 
113113
113114 /* The pSubWInfo->untestedTerms flag means that this OR term
113115 ** contained one or more AND term from a notReady table. The
113116 ** terms from the notReady table could not be tested and will
113117 ** need to be tested later.
@@ -113132,10 +113519,11 @@
113132 */
113133 pSubLoop = pSubWInfo->a[0].pWLoop;
113134 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
113135 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
113136 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
 
113137 ){
113138 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
113139 pCov = pSubLoop->u.btree.pIndex;
113140 }else{
113141 pCov = 0;
@@ -113459,11 +113847,11 @@
113459 if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */
113460 if( pX->rRun >= pY->rRun ){
113461 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
113462 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
113463 }
113464 for(j=0, i=pX->nLTerm-1; i>=0; i--){
113465 for(j=pY->nLTerm-1; j>=0; j--){
113466 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
113467 }
113468 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
113469 }
@@ -113481,16 +113869,29 @@
113481 ** is a proper subset.
113482 **
113483 ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
113484 ** WHERE clause terms than Y and that every WHERE clause term used by X is
113485 ** also used by Y.
 
 
 
 
 
 
 
 
 
 
 
113486 */
113487 static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
113488 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
 
113489 for(; p; p=p->pNextLoop){
113490 if( p->iTab!=pTemplate->iTab ) continue;
113491 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
 
113492 if( whereLoopCheaperProperSubset(p, pTemplate) ){
113493 /* Adjust pTemplate cost downward so that it is cheaper than its
113494 ** subset p */
113495 pTemplate->rRun = p->rRun;
113496 pTemplate->nOut = p->nOut - 1;
@@ -113711,17 +114112,24 @@
113711 pX = pLoop->aLTerm[j];
113712 if( pX==0 ) continue;
113713 if( pX==pTerm ) break;
113714 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
113715 }
113716 if( j<0 ) pLoop->nOut += pTerm->truthProb;
 
 
113717 }
113718 }
113719
113720 /*
113721 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the index pIndex.
113722 ** Try to match one more.
 
 
 
 
 
113723 **
113724 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
113725 ** INTEGER PRIMARY KEY.
113726 */
113727 static int whereLoopAddBtreeIndex(
@@ -113743,11 +114151,10 @@
113743 u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */
113744 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
113745 LogEst saved_nOut; /* Original value of pNew->nOut */
113746 int iCol; /* Index of the column in the table */
113747 int rc = SQLITE_OK; /* Return code */
113748 LogEst nRowEst; /* Estimated index selectivity */
113749 LogEst rLogSize; /* Logarithm of table size */
113750 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
113751
113752 pNew = pBuilder->pNew;
113753 if( db->mallocFailed ) return SQLITE_NOMEM;
@@ -113764,15 +114171,12 @@
113764 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
113765
113766 assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
113767 if( pNew->u.btree.nEq < pProbe->nKeyCol ){
113768 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
113769 nRowEst = sqlite3LogEst(pProbe->aiRowEst[pNew->u.btree.nEq+1]);
113770 if( nRowEst==0 && pProbe->onError==OE_None ) nRowEst = 1;
113771 }else{
113772 iCol = -1;
113773 nRowEst = 0;
113774 }
113775 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
113776 opMask, pProbe);
113777 saved_nEq = pNew->u.btree.nEq;
113778 saved_nSkip = pNew->u.btree.nSkip;
@@ -113779,57 +114183,68 @@
113779 saved_nLTerm = pNew->nLTerm;
113780 saved_wsFlags = pNew->wsFlags;
113781 saved_prereq = pNew->prereq;
113782 saved_nOut = pNew->nOut;
113783 pNew->rSetup = 0;
113784 rLogSize = estLog(sqlite3LogEst(pProbe->aiRowEst[0]));
113785
113786 /* Consider using a skip-scan if there are no WHERE clause constraints
113787 ** available for the left-most terms of the index, and if the average
113788 ** number of repeats in the left-most terms is at least 18. The magic
113789 ** number 18 was found by experimentation to be the payoff point where
113790 ** skip-scan become faster than a full-scan.
113791 */
 
 
 
 
 
113792 if( pTerm==0
113793 && saved_nEq==saved_nSkip
113794 && saved_nEq+1<pProbe->nKeyCol
113795 && pProbe->aiRowEst[saved_nEq+1]>=18 /* TUNING: Minimum for skip-scan */
113796 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
113797 ){
113798 LogEst nIter;
113799 pNew->u.btree.nEq++;
113800 pNew->u.btree.nSkip++;
113801 pNew->aLTerm[pNew->nLTerm++] = 0;
113802 pNew->wsFlags |= WHERE_SKIPSCAN;
113803 nIter = sqlite3LogEst(pProbe->aiRowEst[0]/pProbe->aiRowEst[saved_nEq+1]);
113804 pNew->rRun = rLogSize + nIter;
113805 pNew->nOut += nIter;
113806 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter);
113807 pNew->nOut = saved_nOut;
113808 }
113809 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
 
 
 
113810 int nIn = 0;
113811 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113812 int nRecValid = pBuilder->nRecValid;
113813 #endif
113814 if( (pTerm->eOperator==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
113815 && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
113816 ){
113817 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
113818 }
113819 if( pTerm->prereqRight & pNew->maskSelf ) continue;
113820
113821 assert( pNew->nOut==saved_nOut );
113822
113823 pNew->wsFlags = saved_wsFlags;
113824 pNew->u.btree.nEq = saved_nEq;
113825 pNew->nLTerm = saved_nLTerm;
113826 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
113827 pNew->aLTerm[pNew->nLTerm++] = pTerm;
113828 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
113829 pNew->rRun = rLogSize; /* Baseline cost is log2(N). Adjustments below */
113830 if( pTerm->eOperator & WO_IN ){
 
 
 
 
 
 
113831 Expr *pExpr = pTerm->pExpr;
113832 pNew->wsFlags |= WHERE_COLUMN_IN;
113833 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
113834 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
113835 nIn = 46; assert( 46==sqlite3LogEst(25) );
@@ -113837,87 +114252,122 @@
113837 /* "x IN (value, value, ...)" */
113838 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
113839 }
113840 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
113841 ** changes "x IN (?)" into "x=?". */
113842 pNew->rRun += nIn;
113843 pNew->u.btree.nEq++;
113844 pNew->nOut = nRowEst + nInMul + nIn;
113845 }else if( pTerm->eOperator & (WO_EQ) ){
113846 assert(
113847 (pNew->wsFlags & (WHERE_COLUMN_NULL|WHERE_COLUMN_IN|WHERE_SKIPSCAN))!=0
113848 || nInMul==0
113849 );
113850 pNew->wsFlags |= WHERE_COLUMN_EQ;
113851 if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1)){
113852 assert( (pNew->wsFlags & WHERE_COLUMN_IN)==0 || iCol<0 );
113853 if( iCol>=0 && pProbe->onError==OE_None ){
113854 pNew->wsFlags |= WHERE_UNQ_WANTED;
113855 }else{
113856 pNew->wsFlags |= WHERE_ONEROW;
113857 }
113858 }
113859 pNew->u.btree.nEq++;
113860 pNew->nOut = nRowEst + nInMul;
113861 }else if( pTerm->eOperator & (WO_ISNULL) ){
113862 pNew->wsFlags |= WHERE_COLUMN_NULL;
113863 pNew->u.btree.nEq++;
113864 /* TUNING: IS NULL selects 2 rows */
113865 nIn = 10; assert( 10==sqlite3LogEst(2) );
113866 pNew->nOut = nRowEst + nInMul + nIn;
113867 }else if( pTerm->eOperator & (WO_GT|WO_GE) ){
113868 testcase( pTerm->eOperator & WO_GT );
113869 testcase( pTerm->eOperator & WO_GE );
113870 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
113871 pBtm = pTerm;
113872 pTop = 0;
113873 }else{
113874 assert( pTerm->eOperator & (WO_LT|WO_LE) );
113875 testcase( pTerm->eOperator & WO_LT );
113876 testcase( pTerm->eOperator & WO_LE );
113877 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
113878 pTop = pTerm;
113879 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
113880 pNew->aLTerm[pNew->nLTerm-2] : 0;
113881 }
 
 
 
 
 
 
 
113882 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
113883 /* Adjust nOut and rRun for STAT3 range values */
113884 assert( pNew->nOut==saved_nOut );
113885 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
113886 }
 
 
 
 
 
 
 
 
 
 
 
113887 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
113888 if( nInMul==0
113889 && pProbe->nSample
113890 && pNew->u.btree.nEq<=pProbe->nSampleCol
113891 && OptimizationEnabled(db, SQLITE_Stat3)
113892 ){
113893 Expr *pExpr = pTerm->pExpr;
113894 tRowcnt nOut = 0;
113895 if( (pTerm->eOperator & (WO_EQ|WO_ISNULL))!=0 ){
113896 testcase( pTerm->eOperator & WO_EQ );
113897 testcase( pTerm->eOperator & WO_ISNULL );
113898 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
113899 }else if( (pTerm->eOperator & WO_IN)
113900 && !ExprHasProperty(pExpr, EP_xIsSelect) ){
113901 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
113902 }
113903 assert( nOut==0 || rc==SQLITE_OK );
113904 if( nOut ){
113905 pNew->nOut = sqlite3LogEst(nOut);
113906 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
113907 }
113908 }
113909 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
113910 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
113911 /* Each row involves a step of the index, then a binary search of
113912 ** the main table */
113913 pNew->rRun = sqlite3LogEstAdd(pNew->rRun,rLogSize>27 ? rLogSize-17 : 10);
113914 }
113915 /* Step cost for each output row */
113916 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut);
 
 
113917 whereLoopOutputAdjust(pBuilder->pWC, pNew);
113918 rc = whereLoopInsert(pBuilder, pNew);
 
 
 
 
 
 
 
113919 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
113920 && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
113921 ){
113922 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
113923 }
@@ -113997,19 +114447,42 @@
113997
113998 /*
113999 ** Add all WhereLoop objects for a single table of the join where the table
114000 ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be
114001 ** a b-tree table, not a virtual table.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114002 */
114003 static int whereLoopAddBtree(
114004 WhereLoopBuilder *pBuilder, /* WHERE clause information */
114005 Bitmask mExtra /* Extra prerequesites for using this table */
114006 ){
114007 WhereInfo *pWInfo; /* WHERE analysis context */
114008 Index *pProbe; /* An index we are evaluating */
114009 Index sPk; /* A fake index object for the primary key */
114010 tRowcnt aiRowEstPk[2]; /* The aiRowEst[] value for the sPk index */
114011 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
114012 SrcList *pTabList; /* The FROM clause */
114013 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
114014 WhereLoop *pNew; /* Template WhereLoop object */
114015 int rc = SQLITE_OK; /* Return code */
@@ -114040,24 +114513,25 @@
114040 ** indices to follow */
114041 Index *pFirst; /* First of real indices on the table */
114042 memset(&sPk, 0, sizeof(Index));
114043 sPk.nKeyCol = 1;
114044 sPk.aiColumn = &aiColumnPk;
114045 sPk.aiRowEst = aiRowEstPk;
114046 sPk.onError = OE_Replace;
114047 sPk.pTable = pTab;
114048 aiRowEstPk[0] = pTab->nRowEst;
114049 aiRowEstPk[1] = 1;
 
114050 pFirst = pSrc->pTab->pIndex;
114051 if( pSrc->notIndexed==0 ){
114052 /* The real indices of the table are only considered if the
114053 ** NOT INDEXED qualifier is omitted from the FROM clause */
114054 sPk.pNext = pFirst;
114055 }
114056 pProbe = &sPk;
114057 }
114058 rSize = sqlite3LogEst(pTab->nRowEst);
114059 rLogSize = estLog(rSize);
114060
114061 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
114062 /* Automatic indexes */
114063 if( !pBuilder->pOrSet
@@ -114103,10 +114577,11 @@
114103 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
114104 if( pProbe->pPartIdxWhere!=0
114105 && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
114106 continue; /* Partial index inappropriate for this query */
114107 }
 
114108 pNew->u.btree.nEq = 0;
114109 pNew->u.btree.nSkip = 0;
114110 pNew->nLTerm = 0;
114111 pNew->iSortIdx = 0;
114112 pNew->rSetup = 0;
@@ -114120,14 +114595,12 @@
114120 /* Integer primary key index */
114121 pNew->wsFlags = WHERE_IPK;
114122
114123 /* Full table scan */
114124 pNew->iSortIdx = b ? iSortIdx : 0;
114125 /* TUNING: Cost of full table scan is 3*(N + log2(N)).
114126 ** + The extra 3 factor is to encourage the use of indexed lookups
114127 ** over full scans. FIXME */
114128 pNew->rRun = sqlite3LogEstAdd(rSize,rLogSize) + 16;
114129 whereLoopOutputAdjust(pWC, pNew);
114130 rc = whereLoopInsert(pBuilder, pNew);
114131 pNew->nOut = rSize;
114132 if( rc ) break;
114133 }else{
@@ -114150,39 +114623,20 @@
114150 && sqlite3GlobalConfig.bUseCis
114151 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
114152 )
114153 ){
114154 pNew->iSortIdx = b ? iSortIdx : 0;
114155 /* TUNING: The base cost of an index scan is N + log2(N).
114156 ** The log2(N) is for the initial seek to the beginning and the N
114157 ** is for the scan itself. */
114158 pNew->rRun = sqlite3LogEstAdd(rSize, rLogSize);
114159 if( m==0 ){
114160 /* TUNING: Cost of a covering index scan is K*(N + log2(N)).
114161 ** + The extra factor K of between 1.1 and 3.0 that depends
114162 ** on the relative sizes of the table and the index. K
114163 ** is smaller for smaller indices, thus favoring them.
114164 ** The upper bound on K (3.0) matches the penalty factor
114165 ** on a full table scan that tries to encourage the use of
114166 ** indexed lookups over full scans.
114167 */
114168 pNew->rRun += 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
114169 }else{
114170 /* TUNING: The cost of scanning a non-covering index is multiplied
114171 ** by log2(N) to account for the binary search of the main table
114172 ** that must happen for each row of the index.
114173 ** TODO: Should there be a multiplier here, analogous to the 3x
114174 ** multiplier for a fulltable scan or covering index scan, to
114175 ** further discourage the use of an index scan? Or is the log2(N)
114176 ** term sufficient discouragement?
114177 ** TODO: What if some or all of the WHERE clause terms can be
114178 ** computed without reference to the original table. Then the
114179 ** penality should reduce to logK where K is the number of output
114180 ** rows.
114181 */
114182 pNew->rRun += rLogSize;
114183 }
114184 whereLoopOutputAdjust(pWC, pNew);
114185 rc = whereLoopInsert(pBuilder, pNew);
114186 pNew->nOut = rSize;
114187 if( rc ) break;
114188 }
@@ -114382,20 +114836,19 @@
114382 WhereTerm *pTerm, *pWCEnd;
114383 int rc = SQLITE_OK;
114384 int iCur;
114385 WhereClause tempWC;
114386 WhereLoopBuilder sSubBuild;
114387 WhereOrSet sSum, sCur, sPrev;
114388 struct SrcList_item *pItem;
114389
114390 pWC = pBuilder->pWC;
114391 if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
114392 pWCEnd = pWC->a + pWC->nTerm;
114393 pNew = pBuilder->pNew;
114394 memset(&sSum, 0, sizeof(sSum));
114395 pItem = pWInfo->pTabList->a + pNew->iTab;
114396 if( !HasRowid(pItem->pTab) ) return SQLITE_OK;
114397 iCur = pItem->iCursor;
114398
114399 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
114400 if( (pTerm->eOperator & WO_OR)!=0
114401 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
@@ -114438,10 +114891,11 @@
114438 break;
114439 }else if( once ){
114440 whereOrMove(&sSum, &sCur);
114441 once = 0;
114442 }else{
 
114443 whereOrMove(&sPrev, &sSum);
114444 sSum.n = 0;
114445 for(i=0; i<sPrev.n; i++){
114446 for(j=0; j<sCur.n; j++){
114447 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
@@ -114456,12 +114910,23 @@
114456 pNew->wsFlags = WHERE_MULTI_OR;
114457 pNew->rSetup = 0;
114458 pNew->iSortIdx = 0;
114459 memset(&pNew->u, 0, sizeof(pNew->u));
114460 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
114461 /* TUNING: Multiple by 3.5 for the secondary table lookup */
114462 pNew->rRun = sSum.a[i].rRun + 18;
 
 
 
 
 
 
 
 
 
 
 
114463 pNew->nOut = sSum.a[i].nOut;
114464 pNew->prereq = sSum.a[i].prereq;
114465 rc = whereLoopInsert(pBuilder, pNew);
114466 }
114467 }
@@ -114520,11 +114985,11 @@
114520 ** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
114521 **
114522 ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
114523 ** strict. With GROUP BY and DISTINCT the only requirement is that
114524 ** equivalent rows appear immediately adjacent to one another. GROUP BY
114525 ** and DISTINT do not require rows to appear in any particular order as long
114526 ** as equivelent rows are grouped together. Thus for GROUP BY and DISTINCT
114527 ** the pOrderBy terms can be matched in any order. With ORDER BY, the
114528 ** pOrderBy terms must be matched in strict left-to-right order.
114529 */
114530 static i8 wherePathSatisfiesOrderBy(
@@ -114581,18 +115046,10 @@
114581 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
114582 ** automatically order-distinct.
114583 */
114584
114585 assert( pOrderBy!=0 );
114586
114587 /* Sortability of virtual tables is determined by the xBestIndex method
114588 ** of the virtual table itself */
114589 if( pLast->wsFlags & WHERE_VIRTUALTABLE ){
114590 testcase( nLoop>0 ); /* True when outer loops are one-row and match
114591 ** no ORDER BY terms */
114592 return pLast->u.vtab.isOrdered;
114593 }
114594 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
114595
114596 nOrderBy = pOrderBy->nExpr;
114597 testcase( nOrderBy==BMS-1 );
114598 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
@@ -114601,11 +115058,14 @@
114601 orderDistinctMask = 0;
114602 ready = 0;
114603 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
114604 if( iLoop>0 ) ready |= pLoop->maskSelf;
114605 pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
114606 assert( (pLoop->wsFlags & WHERE_VIRTUALTABLE)==0 );
 
 
 
114607 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
114608
114609 /* Mark off any ORDER BY term X that is a column in the table of
114610 ** the current loop for which there is term in the WHERE
114611 ** clause of the form X IS NULL or X=? that reference only outer
@@ -114689,11 +115149,11 @@
114689 ){
114690 isOrderDistinct = 0;
114691 }
114692
114693 /* Find the ORDER BY term that corresponds to the j-th column
114694 ** of the index and and mark that ORDER BY term off
114695 */
114696 bOnce = 1;
114697 isMatch = 0;
114698 for(i=0; bOnce && i<nOrderBy; i++){
114699 if( MASKBIT(i) & obSat ) continue;
@@ -114769,10 +115229,40 @@
114769 return 0;
114770 }
114771 return -1;
114772 }
114773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114774 #ifdef WHERETRACE_ENABLED
114775 /* For debugging use only: */
114776 static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
114777 static char zName[65];
114778 int i;
@@ -114780,11 +115270,10 @@
114780 if( pLast ) zName[i++] = pLast->cId;
114781 zName[i] = 0;
114782 return zName;
114783 }
114784 #endif
114785
114786
114787 /*
114788 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
114789 ** attempts to find the lowest cost path that visits each WhereLoop
114790 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
@@ -114879,26 +115368,31 @@
114879 if( isOrdered<0 ){
114880 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
114881 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
114882 iLoop, pWLoop, &revMask);
114883 if( isOrdered>=0 && isOrdered<nOrderBy ){
114884 /* TUNING: Estimated cost of sorting is N*log(N).
114885 ** If the order-by clause has X terms but only the last Y terms
114886 ** are out of order, then block-sorting will reduce the sorting
114887 ** cost to N*log(N)*log(Y/X). The log(Y/X) term is computed
114888 ** by rScale.
114889 ** TODO: Should the sorting cost get a small multiplier to help
114890 ** discourage the use of sorting and encourage the use of index
114891 ** scans instead?
114892 */
 
 
 
 
114893 LogEst rScale, rSortCost;
114894 assert( nOrderBy>0 );
114895 rScale = sqlite3LogEst((nOrderBy-isOrdered)*100/nOrderBy) - 66;
114896 rSortCost = nRowEst + estLog(nRowEst) + rScale;
 
114897 /* TUNING: The cost of implementing DISTINCT using a B-TREE is
114898 ** also N*log(N) but it has a larger constant of proportionality.
114899 ** Multiply by 3.0. */
114900 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
114901 rSortCost += 16;
114902 }
114903 WHERETRACE(0x002,
114904 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
@@ -115062,11 +115556,23 @@
115062 }else{
115063 pWInfo->nOBSat = pFrom->isOrdered;
115064 if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
115065 pWInfo->revMask = pFrom->revLoop;
115066 }
 
 
 
 
 
 
 
 
 
 
115067 }
 
 
115068 pWInfo->nRowOut = pFrom->nRow;
115069
115070 /* Free temporary memory and return success */
115071 sqlite3DbFree(db, pSpace);
115072 return SQLITE_OK;
@@ -115587,11 +116093,18 @@
115587 Index *pIx = pLoop->u.btree.pIndex;
115588 int iIndexCur;
115589 int op = OP_OpenRead;
115590 /* iIdxCur is always set if to a positive value if ONEPASS is possible */
115591 assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
115592 if( pWInfo->okOnePass ){
 
 
 
 
 
 
 
115593 Index *pJ = pTabItem->pTab->pIndex;
115594 iIndexCur = iIdxCur;
115595 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
115596 while( ALWAYS(pJ) && pJ!=pIx ){
115597 iIndexCur++;
@@ -115605,13 +116118,15 @@
115605 iIndexCur = pParse->nTab++;
115606 }
115607 pLevel->iIdxCur = iIndexCur;
115608 assert( pIx->pSchema==pTab->pSchema );
115609 assert( iIndexCur>=0 );
115610 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
115611 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
115612 VdbeComment((v, "%s", pIx->zName));
 
 
115613 }
115614 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
115615 notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
115616 }
115617 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
@@ -123654,10 +124169,32 @@
123654 int sz = va_arg(ap, int);
123655 int *aProg = va_arg(ap, int*);
123656 rc = sqlite3BitvecBuiltinTest(sz, aProg);
123657 break;
123658 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
123659
123660 /*
123661 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
123662 **
123663 ** Register hooks to call to indicate which malloc() failures
@@ -123964,11 +124501,11 @@
123964 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
123965 ** no such database exists.
123966 */
123967 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
123968 Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
123969 return pBt ? sqlite3PagerIsreadonly(sqlite3BtreePager(pBt)) : -1;
123970 }
123971
123972 /************** End of main.c ************************************************/
123973 /************** Begin file notify.c ******************************************/
123974 /*
@@ -125084,17 +125621,17 @@
125084 char **azColumn; /* column names. malloced */
125085 u8 *abNotindexed; /* True for 'notindexed' columns */
125086 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
125087 char *zContentTbl; /* content=xxx option, or NULL */
125088 char *zLanguageid; /* languageid=xxx option, or NULL */
125089 u8 bAutoincrmerge; /* True if automerge=1 */
125090 u32 nLeafAdd; /* Number of leaf blocks added this trans */
125091
125092 /* Precompiled statements used by the implementation. Each of these
125093 ** statements is run and reset within a single virtual table API call.
125094 */
125095 sqlite3_stmt *aStmt[37];
125096
125097 char *zReadExprlist;
125098 char *zWriteExprlist;
125099
125100 int nNodeSize; /* Soft limit for node size */
@@ -126512,11 +127049,11 @@
126512 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
126513 p->bHasDocsize = (isFts4 && bNoDocsize==0);
126514 p->bHasStat = isFts4;
126515 p->bFts4 = isFts4;
126516 p->bDescIdx = bDescIdx;
126517 p->bAutoincrmerge = 0xff; /* 0xff means setting unknown */
126518 p->zContentTbl = zContent;
126519 p->zLanguageid = zLanguageid;
126520 zContent = 0;
126521 zLanguageid = 0;
126522 TESTONLY( p->inTransaction = -1 );
@@ -126555,11 +127092,13 @@
126555 /* Fill in the abNotindexed array */
126556 for(iCol=0; iCol<nCol; iCol++){
126557 int n = (int)strlen(p->azColumn[iCol]);
126558 for(i=0; i<nNotindexed; i++){
126559 char *zNot = azNotindexed[i];
126560 if( zNot && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n) ){
 
 
126561 p->abNotindexed[iCol] = 1;
126562 sqlite3_free(zNot);
126563 azNotindexed[i] = 0;
126564 }
126565 }
@@ -128481,19 +129020,22 @@
128481 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
128482
128483 Fts3Table *p = (Fts3Table*)pVtab;
128484 int rc = sqlite3Fts3PendingTermsFlush(p);
128485
128486 if( rc==SQLITE_OK && p->bAutoincrmerge==1 && p->nLeafAdd>(nMinMerge/16) ){
 
 
 
128487 int mxLevel = 0; /* Maximum relative level value in db */
128488 int A; /* Incr-merge parameter A */
128489
128490 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
128491 assert( rc==SQLITE_OK || mxLevel==0 );
128492 A = p->nLeafAdd * mxLevel;
128493 A += (A/2);
128494 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, 8);
128495 }
128496 sqlite3Fts3SegmentsClose(p);
128497 return rc;
128498 }
128499
@@ -131712,44 +132254,27 @@
131712 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
131713 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
131714 int rc;
131715 sqlite3_tokenizer_cursor *pCursor;
131716 Fts3Expr *pRet = 0;
131717 int nConsumed = 0;
131718
131719 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, n, &pCursor);
 
 
 
 
 
 
 
131720 if( rc==SQLITE_OK ){
131721 const char *zToken;
131722 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
131723 int nByte; /* total space to allocate */
131724
131725 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
131726
131727 if( (rc==SQLITE_OK || rc==SQLITE_DONE) && sqlite3_fts3_enable_parentheses ){
131728 int i;
131729 if( rc==SQLITE_DONE ) iStart = n;
131730 for(i=0; i<iStart; i++){
131731 if( z[i]=='(' ){
131732 pParse->nNest++;
131733 rc = fts3ExprParse(pParse, &z[i+1], n-i-1, &pRet, &nConsumed);
131734 if( rc==SQLITE_OK && !pRet ){
131735 rc = SQLITE_DONE;
131736 }
131737 nConsumed = (int)(i + 1 + nConsumed);
131738 break;
131739 }
131740
131741 if( z[i]==')' ){
131742 rc = SQLITE_DONE;
131743 pParse->nNest--;
131744 nConsumed = i+1;
131745 break;
131746 }
131747 }
131748 }
131749
131750 if( nConsumed==0 && rc==SQLITE_OK ){
131751 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
131752 pRet = (Fts3Expr *)fts3MallocZero(nByte);
131753 if( !pRet ){
131754 rc = SQLITE_NOMEM;
131755 }else{
@@ -131779,17 +132304,18 @@
131779 break;
131780 }
131781 }
131782
131783 }
131784 nConsumed = iEnd;
 
 
131785 }
131786
131787 pModule->xClose(pCursor);
131788 }
131789
131790 *pnConsumed = nConsumed;
131791 *ppExpr = pRet;
131792 return rc;
131793 }
131794
131795
@@ -132035,10 +132561,25 @@
132035 return SQLITE_ERROR;
132036 }
132037 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
132038 }
132039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132040
132041 /* If control flows to this point, this must be a regular token, or
132042 ** the end of the input. Read a regular token using the sqlite3_tokenizer
132043 ** interface. Before doing so, figure out if there is an explicit
132044 ** column specifier for the token.
@@ -132153,100 +132694,104 @@
132153 int isRequirePhrase = 1;
132154
132155 while( rc==SQLITE_OK ){
132156 Fts3Expr *p = 0;
132157 int nByte = 0;
132158 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
132159 if( rc==SQLITE_OK ){
132160 int isPhrase;
132161
132162 if( !sqlite3_fts3_enable_parentheses
132163 && p->eType==FTSQUERY_PHRASE && pParse->isNot
132164 ){
132165 /* Create an implicit NOT operator. */
132166 Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
132167 if( !pNot ){
132168 sqlite3Fts3ExprFree(p);
132169 rc = SQLITE_NOMEM;
132170 goto exprparse_out;
132171 }
132172 pNot->eType = FTSQUERY_NOT;
132173 pNot->pRight = p;
132174 p->pParent = pNot;
132175 if( pNotBranch ){
132176 pNot->pLeft = pNotBranch;
132177 pNotBranch->pParent = pNot;
132178 }
132179 pNotBranch = pNot;
132180 p = pPrev;
132181 }else{
132182 int eType = p->eType;
132183 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
132184
132185 /* The isRequirePhrase variable is set to true if a phrase or
132186 ** an expression contained in parenthesis is required. If a
132187 ** binary operator (AND, OR, NOT or NEAR) is encounted when
132188 ** isRequirePhrase is set, this is a syntax error.
132189 */
132190 if( !isPhrase && isRequirePhrase ){
132191 sqlite3Fts3ExprFree(p);
132192 rc = SQLITE_ERROR;
132193 goto exprparse_out;
132194 }
132195
132196 if( isPhrase && !isRequirePhrase ){
132197 /* Insert an implicit AND operator. */
132198 Fts3Expr *pAnd;
132199 assert( pRet && pPrev );
132200 pAnd = fts3MallocZero(sizeof(Fts3Expr));
132201 if( !pAnd ){
132202 sqlite3Fts3ExprFree(p);
132203 rc = SQLITE_NOMEM;
132204 goto exprparse_out;
132205 }
132206 pAnd->eType = FTSQUERY_AND;
132207 insertBinaryOperator(&pRet, pPrev, pAnd);
132208 pPrev = pAnd;
132209 }
132210
132211 /* This test catches attempts to make either operand of a NEAR
132212 ** operator something other than a phrase. For example, either of
132213 ** the following:
132214 **
132215 ** (bracketed expression) NEAR phrase
132216 ** phrase NEAR (bracketed expression)
132217 **
132218 ** Return an error in either case.
132219 */
132220 if( pPrev && (
132221 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
132222 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
132223 )){
132224 sqlite3Fts3ExprFree(p);
132225 rc = SQLITE_ERROR;
132226 goto exprparse_out;
132227 }
132228
132229 if( isPhrase ){
132230 if( pRet ){
132231 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
132232 pPrev->pRight = p;
132233 p->pParent = pPrev;
132234 }else{
132235 pRet = p;
132236 }
132237 }else{
132238 insertBinaryOperator(&pRet, pPrev, p);
132239 }
132240 isRequirePhrase = !isPhrase;
 
 
 
 
 
132241 }
132242 assert( nByte>0 );
132243 }
132244 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
132245 nIn -= nByte;
132246 zIn += nByte;
132247 pPrev = p;
132248 }
132249
132250 if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
132251 rc = SQLITE_ERROR;
132252 }
@@ -135230,10 +135775,11 @@
135230 int nMalloc; /* Size of malloc'd buffer at zMalloc */
135231 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
135232 int nSize; /* Size of allocation at aData */
135233 int nData; /* Bytes of data in aData */
135234 char *aData; /* Pointer to block from malloc() */
 
135235 };
135236
135237 /*
135238 ** Type SegmentNode is used by the following three functions to create
135239 ** the interior part of the segment b+-tree structures (everything except
@@ -135304,10 +135850,14 @@
135304 #define SQL_SELECT_SEGDIR 32
135305 #define SQL_CHOMP_SEGDIR 33
135306 #define SQL_SEGMENT_IS_APPENDABLE 34
135307 #define SQL_SELECT_INDEXES 35
135308 #define SQL_SELECT_MXLEVEL 36
 
 
 
 
135309
135310 /*
135311 ** This function is used to obtain an SQLite prepared statement handle
135312 ** for the statement identified by the second argument. If successful,
135313 ** *pp is set to the requested statement handle and SQLITE_OK returned.
@@ -135406,11 +135956,22 @@
135406 ** Return the list of valid segment indexes for absolute level ? */
135407 /* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
135408
135409 /* SQL_SELECT_MXLEVEL
135410 ** Return the largest relative level in the FTS index or indexes. */
135411 /* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'"
 
 
 
 
 
 
 
 
 
 
 
135412 };
135413 int rc = SQLITE_OK;
135414 sqlite3_stmt *pStmt;
135415
135416 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
@@ -136947,10 +137508,11 @@
136947 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
136948 int iIdx, /* Value for "idx" field */
136949 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
136950 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
136951 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
 
136952 char *zRoot, /* Blob value for "root" field */
136953 int nRoot /* Number of bytes in buffer zRoot */
136954 ){
136955 sqlite3_stmt *pStmt;
136956 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
@@ -136957,11 +137519,17 @@
136957 if( rc==SQLITE_OK ){
136958 sqlite3_bind_int64(pStmt, 1, iLevel);
136959 sqlite3_bind_int(pStmt, 2, iIdx);
136960 sqlite3_bind_int64(pStmt, 3, iStartBlock);
136961 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
136962 sqlite3_bind_int64(pStmt, 5, iEndBlock);
 
 
 
 
 
 
136963 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
136964 sqlite3_step(pStmt);
136965 rc = sqlite3_reset(pStmt);
136966 }
136967 return rc;
@@ -137282,10 +137850,13 @@
137282 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
137283 nTerm + /* Term suffix */
137284 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
137285 nDoclist; /* Doclist data */
137286 }
 
 
 
137287
137288 /* If the buffer currently allocated is too small for this entry, realloc
137289 ** the buffer to make it large enough.
137290 */
137291 if( nReq>pWriter->nSize ){
@@ -137354,17 +137925,17 @@
137354 if( rc==SQLITE_OK ){
137355 rc = fts3NodeWrite(p, pWriter->pTree, 1,
137356 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
137357 }
137358 if( rc==SQLITE_OK ){
137359 rc = fts3WriteSegdir(
137360 p, iLevel, iIdx, pWriter->iFirst, iLastLeaf, iLast, zRoot, nRoot);
137361 }
137362 }else{
137363 /* The entire tree fits on the root node. Write it to the segdir table. */
137364 rc = fts3WriteSegdir(
137365 p, iLevel, iIdx, 0, 0, 0, pWriter->aData, pWriter->nData);
137366 }
137367 p->nLeafAdd++;
137368 return rc;
137369 }
137370
@@ -137443,10 +138014,41 @@
137443 if( SQLITE_ROW==sqlite3_step(pStmt) ){
137444 *pnMax = sqlite3_column_int64(pStmt, 0);
137445 }
137446 return sqlite3_reset(pStmt);
137447 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137448
137449 /*
137450 ** Delete all entries in the %_segments table associated with the segment
137451 ** opened with seg-reader pSeg. This function does not affect the contents
137452 ** of the %_segdir table.
@@ -137978,10 +138580,144 @@
137978 pCsr->nSegment = 0;
137979 pCsr->apSegment = 0;
137980 pCsr->aBuffer = 0;
137981 }
137982 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
137983
137984 /*
137985 ** Merge all level iLevel segments in the database into a single
137986 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
137987 ** single segment with a level equal to the numerically largest level
@@ -138003,10 +138739,11 @@
138003 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
138004 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
138005 Fts3SegFilter filter; /* Segment term filter condition */
138006 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
138007 int bIgnoreEmpty = 0; /* True to ignore empty segments */
 
138008
138009 assert( iLevel==FTS3_SEGCURSOR_ALL
138010 || iLevel==FTS3_SEGCURSOR_PENDING
138011 || iLevel>=0
138012 );
@@ -138013,10 +138750,15 @@
138013 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
138014 assert( iIndex>=0 && iIndex<p->nIndex );
138015
138016 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
138017 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
 
 
 
 
 
138018
138019 if( iLevel==FTS3_SEGCURSOR_ALL ){
138020 /* This call is to merge all segments in the database to a single
138021 ** segment. The level of the new segment is equal to the numerically
138022 ** greatest segment level currently present in the database for this
@@ -138023,25 +138765,25 @@
138023 ** index. The idx of the new segment is always 0. */
138024 if( csr.nSegment==1 ){
138025 rc = SQLITE_DONE;
138026 goto finished;
138027 }
138028 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iNewLevel);
138029 bIgnoreEmpty = 1;
138030
138031 }else if( iLevel==FTS3_SEGCURSOR_PENDING ){
138032 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, 0);
138033 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, 0, &iIdx);
138034 }else{
138035 /* This call is to merge all segments at level iLevel. find the next
138036 ** available segment index at level iLevel+1. The call to
138037 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
138038 ** a single iLevel+2 segment if necessary. */
138039 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
138040 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
 
 
138041 }
138042 if( rc!=SQLITE_OK ) goto finished;
 
138043 assert( csr.nSegment>0 );
138044 assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
138045 assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
138046
138047 memset(&filter, 0, sizeof(Fts3SegFilter));
@@ -138054,29 +138796,36 @@
138054 if( rc!=SQLITE_ROW ) break;
138055 rc = fts3SegWriterAdd(p, &pWriter, 1,
138056 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
138057 }
138058 if( rc!=SQLITE_OK ) goto finished;
138059 assert( pWriter );
138060
138061 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
138062 rc = fts3DeleteSegdir(
138063 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
138064 );
138065 if( rc!=SQLITE_OK ) goto finished;
138066 }
138067 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
 
 
 
 
 
 
 
138068
138069 finished:
138070 fts3SegWriterFree(pWriter);
138071 sqlite3Fts3SegReaderFinish(&csr);
138072 return rc;
138073 }
138074
138075
138076 /*
138077 ** Flush the contents of pendingTerms to level 0 segments.
138078 */
138079 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
138080 int rc = SQLITE_OK;
138081 int i;
138082
@@ -138088,18 +138837,23 @@
138088
138089 /* Determine the auto-incr-merge setting if unknown. If enabled,
138090 ** estimate the number of leaf blocks of content to be written
138091 */
138092 if( rc==SQLITE_OK && p->bHasStat
138093 && p->bAutoincrmerge==0xff && p->nLeafAdd>0
138094 ){
138095 sqlite3_stmt *pStmt = 0;
138096 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
138097 if( rc==SQLITE_OK ){
138098 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
138099 rc = sqlite3_step(pStmt);
138100 p->bAutoincrmerge = (rc==SQLITE_ROW && sqlite3_column_int(pStmt, 0));
 
 
 
 
 
138101 rc = sqlite3_reset(pStmt);
138102 }
138103 }
138104 return rc;
138105 }
@@ -138463,10 +139217,12 @@
138463 int nWork; /* Number of leaf pages flushed */
138464 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
138465 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
138466 sqlite3_int64 iStart; /* Block number of first allocated block */
138467 sqlite3_int64 iEnd; /* Block number of last allocated block */
 
 
138468 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
138469 };
138470
138471 /*
138472 ** An object of the following type is used to read data from a single
@@ -138801,12 +139557,12 @@
138801 nSpace = 1;
138802 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
138803 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
138804 }
138805
 
138806 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
138807
138808 if( rc==SQLITE_OK ){
138809 if( pLeaf->block.n==0 ){
138810 pLeaf->block.n = 1;
138811 pLeaf->block.a[0] = '\0';
138812 }
@@ -138901,10 +139657,11 @@
138901 pWriter->iAbsLevel+1, /* level */
138902 pWriter->iIdx, /* idx */
138903 pWriter->iStart, /* start_block */
138904 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
138905 pWriter->iEnd, /* end_block */
 
138906 pRoot->block.a, pRoot->block.n /* root */
138907 );
138908 }
138909 sqlite3_free(pRoot->block.a);
138910 sqlite3_free(pRoot->key.a);
@@ -139002,11 +139759,15 @@
139002 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
139003 sqlite3_bind_int(pSelect, 2, iIdx);
139004 if( sqlite3_step(pSelect)==SQLITE_ROW ){
139005 iStart = sqlite3_column_int64(pSelect, 1);
139006 iLeafEnd = sqlite3_column_int64(pSelect, 2);
139007 iEnd = sqlite3_column_int64(pSelect, 3);
 
 
 
 
139008 nRoot = sqlite3_column_bytes(pSelect, 4);
139009 aRoot = sqlite3_column_blob(pSelect, 4);
139010 }else{
139011 return sqlite3_reset(pSelect);
139012 }
@@ -139603,15 +140364,15 @@
139603
139604
139605 /*
139606 ** Attempt an incremental merge that writes nMerge leaf blocks.
139607 **
139608 ** Incremental merges happen nMin segments at a time. The two
139609 ** segments to be merged are the nMin oldest segments (the ones with
139610 ** the smallest indexes) in the highest level that contains at least
139611 ** nMin segments. Multiple merges might occur in an attempt to write the
139612 ** quota of nMerge leaf blocks.
139613 */
139614 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
139615 int rc; /* Return code */
139616 int nRem = nMerge; /* Number of leaf pages yet to be written */
139617 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
@@ -139632,10 +140393,11 @@
139632 rc = fts3IncrmergeHintLoad(p, &hint);
139633 while( rc==SQLITE_OK && nRem>0 ){
139634 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
139635 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
139636 int bUseHint = 0; /* True if attempting to append */
 
139637
139638 /* Search the %_segdir table for the absolute level with the smallest
139639 ** relative level number that contains at least nMin segments, if any.
139640 ** If one is found, set iAbsLevel to the absolute level number and
139641 ** nSeg to nMin. If no level with at least nMin segments can be found,
@@ -139685,27 +140447,36 @@
139685 ** segments available in level iAbsLevel. In this case, no work is
139686 ** done on iAbsLevel - fall through to the next iteration of the loop
139687 ** to start work on some other level. */
139688 memset(pWriter, 0, nAlloc);
139689 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
 
 
 
 
 
 
 
 
 
 
 
 
 
139690 if( rc==SQLITE_OK ){
139691 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
139692 }
139693 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
139694 && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
139695 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
139696 ){
139697 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
139698 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
139699 if( rc==SQLITE_OK ){
139700 if( bUseHint && iIdx>0 ){
139701 const char *zKey = pCsr->zTerm;
139702 int nKey = pCsr->nTerm;
139703 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
139704 }else{
139705 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
139706 }
139707 }
139708
139709 if( rc==SQLITE_OK && pWriter->nLeafEst ){
139710 fts3LogMerge(nSeg, iAbsLevel);
139711 do {
@@ -139723,11 +140494,17 @@
139723 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
139724 }
139725 }
139726 }
139727
 
 
 
139728 fts3IncrmergeRelease(p, pWriter, &rc);
 
 
 
139729 }
139730
139731 sqlite3Fts3SegReaderFinish(pCsr);
139732 }
139733
@@ -139810,20 +140587,23 @@
139810 Fts3Table *p, /* FTS3 table handle */
139811 const char *zParam /* Nul-terminated string containing boolean */
139812 ){
139813 int rc = SQLITE_OK;
139814 sqlite3_stmt *pStmt = 0;
139815 p->bAutoincrmerge = fts3Getint(&zParam)!=0;
 
 
 
139816 if( !p->bHasStat ){
139817 assert( p->bFts4==0 );
139818 sqlite3Fts3CreateStatTable(&rc, p);
139819 if( rc ) return rc;
139820 }
139821 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
139822 if( rc ) return rc;
139823 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
139824 sqlite3_bind_int(pStmt, 2, p->bAutoincrmerge);
139825 sqlite3_step(pStmt);
139826 rc = sqlite3_reset(pStmt);
139827 return rc;
139828 }
139829
@@ -142802,64 +143582,24 @@
142802 ** child page.
142803 */
142804
142805 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
142806
142807 /*
142808 ** This file contains an implementation of a couple of different variants
142809 ** of the r-tree algorithm. See the README file for further details. The
142810 ** same data-structure is used for all, but the algorithms for insert and
142811 ** delete operations vary. The variants used are selected at compile time
142812 ** by defining the following symbols:
142813 */
142814
142815 /* Either, both or none of the following may be set to activate
142816 ** r*tree variant algorithms.
142817 */
142818 #define VARIANT_RSTARTREE_CHOOSESUBTREE 0
142819 #define VARIANT_RSTARTREE_REINSERT 1
142820
142821 /*
142822 ** Exactly one of the following must be set to 1.
142823 */
142824 #define VARIANT_GUTTMAN_QUADRATIC_SPLIT 0
142825 #define VARIANT_GUTTMAN_LINEAR_SPLIT 0
142826 #define VARIANT_RSTARTREE_SPLIT 1
142827
142828 #define VARIANT_GUTTMAN_SPLIT \
142829 (VARIANT_GUTTMAN_LINEAR_SPLIT||VARIANT_GUTTMAN_QUADRATIC_SPLIT)
142830
142831 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
142832 #define PickNext QuadraticPickNext
142833 #define PickSeeds QuadraticPickSeeds
142834 #define AssignCells splitNodeGuttman
142835 #endif
142836 #if VARIANT_GUTTMAN_LINEAR_SPLIT
142837 #define PickNext LinearPickNext
142838 #define PickSeeds LinearPickSeeds
142839 #define AssignCells splitNodeGuttman
142840 #endif
142841 #if VARIANT_RSTARTREE_SPLIT
142842 #define AssignCells splitNodeStartree
142843 #endif
142844
142845 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
142846 # define NDEBUG 1
142847 #endif
142848
142849 #ifndef SQLITE_CORE
142850 SQLITE_EXTENSION_INIT1
142851 #else
142852 #endif
142853
142854 /* #include <string.h> */
142855 /* #include <assert.h> */
 
142856
142857 #ifndef SQLITE_AMALGAMATION
142858 #include "sqlite3rtree.h"
142859 typedef sqlite3_int64 i64;
142860 typedef unsigned char u8;
 
142861 typedef unsigned int u32;
142862 #endif
142863
142864 /* The following macro is used to suppress compiler warnings.
142865 */
@@ -142873,19 +143613,20 @@
142873 typedef struct RtreeCell RtreeCell;
142874 typedef struct RtreeConstraint RtreeConstraint;
142875 typedef struct RtreeMatchArg RtreeMatchArg;
142876 typedef struct RtreeGeomCallback RtreeGeomCallback;
142877 typedef union RtreeCoord RtreeCoord;
 
142878
142879 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
142880 #define RTREE_MAX_DIMENSIONS 5
142881
142882 /* Size of hash table Rtree.aHash. This hash table is not expected to
142883 ** ever contain very many entries, so a fixed number of buckets is
142884 ** used.
142885 */
142886 #define HASHSIZE 128
142887
142888 /* The xBestIndex method of this virtual table requires an estimate of
142889 ** the number of rows in the virtual table to calculate the costs of
142890 ** various strategies. If possible, this estimate is loaded from the
142891 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
@@ -142897,19 +143638,19 @@
142897
142898 /*
142899 ** An rtree virtual-table object.
142900 */
142901 struct Rtree {
142902 sqlite3_vtab base;
142903 sqlite3 *db; /* Host database connection */
142904 int iNodeSize; /* Size in bytes of each node in the node table */
142905 int nDim; /* Number of dimensions */
142906 int nBytesPerCell; /* Bytes consumed per cell */
 
142907 int iDepth; /* Current depth of the r-tree structure */
142908 char *zDb; /* Name of database containing r-tree table */
142909 char *zName; /* Name of r-tree table */
142910 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
142911 int nBusy; /* Current number of users of this structure */
142912 i64 nRowEst; /* Estimated number of rows in this table */
142913
142914 /* List of nodes removed during a CondenseTree operation. List is
142915 ** linked together via the pointer normally used for hash chains -
@@ -142932,14 +143673,14 @@
142932 /* Statements to read/write/delete a record from xxx_parent */
142933 sqlite3_stmt *pReadParent;
142934 sqlite3_stmt *pWriteParent;
142935 sqlite3_stmt *pDeleteParent;
142936
142937 int eCoordType;
142938 };
142939
142940 /* Possible values for eCoordType: */
142941 #define RTREE_COORD_REAL32 0
142942 #define RTREE_COORD_INT32 1
142943
142944 /*
142945 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
@@ -142947,14 +143688,33 @@
142947 ** will be done.
142948 */
142949 #ifdef SQLITE_RTREE_INT_ONLY
142950 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
142951 typedef int RtreeValue; /* Low accuracy coordinate */
 
142952 #else
142953 typedef double RtreeDValue; /* High accuracy coordinate */
142954 typedef float RtreeValue; /* Low accuracy coordinate */
 
142955 #endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142956
142957 /*
142958 ** The minimum number of cells allowed for a node is a third of the
142959 ** maximum. In Gutman's notation:
142960 **
@@ -142974,25 +143734,48 @@
142974 ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
142975 ** 40 or less.
142976 */
142977 #define RTREE_MAX_DEPTH 40
142978
 
 
 
 
 
 
 
 
142979 /*
142980 ** An rtree cursor object.
142981 */
142982 struct RtreeCursor {
142983 sqlite3_vtab_cursor base;
142984 RtreeNode *pNode; /* Node cursor is currently pointing at */
142985 int iCell; /* Index of current cell in pNode */
142986 int iStrategy; /* Copy of idxNum search parameter */
142987 int nConstraint; /* Number of entries in aConstraint */
142988 RtreeConstraint *aConstraint; /* Search constraints. */
 
 
 
 
 
 
 
142989 };
142990
 
 
 
 
 
 
 
142991 union RtreeCoord {
142992 RtreeValue f;
142993 int i;
 
142994 };
142995
142996 /*
142997 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
142998 ** formatted as a RtreeDValue (double or int64). This macro assumes that local
@@ -143013,42 +143796,71 @@
143013 ** A search constraint.
143014 */
143015 struct RtreeConstraint {
143016 int iCoord; /* Index of constrained coordinate */
143017 int op; /* Constraining operation */
143018 RtreeDValue rValue; /* Constraint value. */
143019 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
143020 sqlite3_rtree_geometry *pGeom; /* Constraint callback argument for a MATCH */
 
 
 
143021 };
143022
143023 /* Possible values for RtreeConstraint.op */
143024 #define RTREE_EQ 0x41
143025 #define RTREE_LE 0x42
143026 #define RTREE_LT 0x43
143027 #define RTREE_GE 0x44
143028 #define RTREE_GT 0x45
143029 #define RTREE_MATCH 0x46
 
 
143030
143031 /*
143032 ** An rtree structure node.
143033 */
143034 struct RtreeNode {
143035 RtreeNode *pParent; /* Parent node */
143036 i64 iNode;
143037 int nRef;
143038 int isDirty;
143039 u8 *zData;
143040 RtreeNode *pNext; /* Next node in this hash chain */
143041 };
 
 
143042 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
143043
143044 /*
143045 ** Structure to store a deserialized rtree record.
143046 */
143047 struct RtreeCell {
143048 i64 iRowid;
143049 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2];
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143050 };
143051
143052
143053 /*
143054 ** Value for the first field of every RtreeMatchArg object. The MATCH
@@ -143056,33 +143868,20 @@
143056 ** value to avoid operating on invalid blobs (which could cause a segfault).
143057 */
143058 #define RTREE_GEOMETRY_MAGIC 0x891245AB
143059
143060 /*
143061 ** An instance of this structure must be supplied as a blob argument to
143062 ** the right-hand-side of an SQL MATCH operator used to constrain an
143063 ** r-tree query.
 
143064 */
143065 struct RtreeMatchArg {
143066 u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
143067 int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue*, int *);
143068 void *pContext;
143069 int nParam;
143070 RtreeDValue aParam[1];
143071 };
143072
143073 /*
143074 ** When a geometry callback is created (see sqlite3_rtree_geometry_callback),
143075 ** a single instance of the following structure is allocated. It is used
143076 ** as the context for the user-function created by by s_r_g_c(). The object
143077 ** is eventually deleted by the destructor mechanism provided by
143078 ** sqlite3_create_function_v2() (which is called by s_r_g_c() to create
143079 ** the geometry callback function).
143080 */
143081 struct RtreeGeomCallback {
143082 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
143083 void *pContext;
143084 };
143085
143086 #ifndef MAX
143087 # define MAX(x,y) ((x) < (y) ? (y) : (x))
143088 #endif
@@ -143172,14 +143971,11 @@
143172 /*
143173 ** Given a node number iNode, return the corresponding key to use
143174 ** in the Rtree.aHash table.
143175 */
143176 static int nodeHash(i64 iNode){
143177 return (
143178 (iNode>>56) ^ (iNode>>48) ^ (iNode>>40) ^ (iNode>>32) ^
143179 (iNode>>24) ^ (iNode>>16) ^ (iNode>> 8) ^ (iNode>> 0)
143180 ) % HASHSIZE;
143181 }
143182
143183 /*
143184 ** Search the node hash table for node iNode. If found, return a pointer
143185 ** to it. Otherwise, return 0.
@@ -143235,12 +144031,11 @@
143235 }
143236
143237 /*
143238 ** Obtain a reference to an r-tree node.
143239 */
143240 static int
143241 nodeAcquire(
143242 Rtree *pRtree, /* R-tree structure */
143243 i64 iNode, /* Node number to load */
143244 RtreeNode *pParent, /* Either the parent node or NULL */
143245 RtreeNode **ppNode /* OUT: Acquired node */
143246 ){
@@ -143325,14 +144120,14 @@
143325
143326 /*
143327 ** Overwrite cell iCell of node pNode with the contents of pCell.
143328 */
143329 static void nodeOverwriteCell(
143330 Rtree *pRtree,
143331 RtreeNode *pNode,
143332 RtreeCell *pCell,
143333 int iCell
143334 ){
143335 int ii;
143336 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
143337 p += writeInt64(p, pCell->iRowid);
143338 for(ii=0; ii<(pRtree->nDim*2); ii++){
@@ -143340,11 +144135,11 @@
143340 }
143341 pNode->isDirty = 1;
143342 }
143343
143344 /*
143345 ** Remove cell the cell with index iCell from node pNode.
143346 */
143347 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
143348 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
143349 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
143350 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
@@ -143357,15 +144152,14 @@
143357 ** Insert the contents of cell pCell into node pNode. If the insert
143358 ** is successful, return SQLITE_OK.
143359 **
143360 ** If there is not enough free space in pNode, return SQLITE_FULL.
143361 */
143362 static int
143363 nodeInsertCell(
143364 Rtree *pRtree,
143365 RtreeNode *pNode,
143366 RtreeCell *pCell
143367 ){
143368 int nCell; /* Current number of cells in pNode */
143369 int nMaxCell; /* Maximum number of cells for pNode */
143370
143371 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
@@ -143382,12 +144176,11 @@
143382 }
143383
143384 /*
143385 ** If the node is dirty, write it out to the database.
143386 */
143387 static int
143388 nodeWrite(Rtree *pRtree, RtreeNode *pNode){
143389 int rc = SQLITE_OK;
143390 if( pNode->isDirty ){
143391 sqlite3_stmt *p = pRtree->pWriteNode;
143392 if( pNode->iNode ){
143393 sqlite3_bind_int64(p, 1, pNode->iNode);
@@ -143408,12 +144201,11 @@
143408
143409 /*
143410 ** Release a reference to a node. If the node is dirty and the reference
143411 ** count drops to zero, the node data is written to the database.
143412 */
143413 static int
143414 nodeRelease(Rtree *pRtree, RtreeNode *pNode){
143415 int rc = SQLITE_OK;
143416 if( pNode ){
143417 assert( pNode->nRef>0 );
143418 pNode->nRef--;
143419 if( pNode->nRef==0 ){
@@ -143437,45 +144229,50 @@
143437 ** Return the 64-bit integer value associated with cell iCell of
143438 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
143439 ** an internal node, then the 64-bit integer is a child page number.
143440 */
143441 static i64 nodeGetRowid(
143442 Rtree *pRtree,
143443 RtreeNode *pNode,
143444 int iCell
143445 ){
143446 assert( iCell<NCELL(pNode) );
143447 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
143448 }
143449
143450 /*
143451 ** Return coordinate iCoord from cell iCell in node pNode.
143452 */
143453 static void nodeGetCoord(
143454 Rtree *pRtree,
143455 RtreeNode *pNode,
143456 int iCell,
143457 int iCoord,
143458 RtreeCoord *pCoord /* Space to write result to */
143459 ){
143460 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
143461 }
143462
143463 /*
143464 ** Deserialize cell iCell of node pNode. Populate the structure pointed
143465 ** to by pCell with the results.
143466 */
143467 static void nodeGetCell(
143468 Rtree *pRtree,
143469 RtreeNode *pNode,
143470 int iCell,
143471 RtreeCell *pCell
143472 ){
143473 int ii;
 
 
143474 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
143475 for(ii=0; ii<pRtree->nDim*2; ii++){
143476 nodeGetCoord(pRtree, pNode, iCell, ii, &pCell->aCoord[ii]);
 
 
 
143477 }
143478 }
143479
143480
143481 /* Forward declaration for the function that does the work of
@@ -143597,14 +144394,14 @@
143597 */
143598 static void freeCursorConstraints(RtreeCursor *pCsr){
143599 if( pCsr->aConstraint ){
143600 int i; /* Used to iterate through constraint array */
143601 for(i=0; i<pCsr->nConstraint; i++){
143602 sqlite3_rtree_geometry *pGeom = pCsr->aConstraint[i].pGeom;
143603 if( pGeom ){
143604 if( pGeom->xDelUser ) pGeom->xDelUser(pGeom->pUser);
143605 sqlite3_free(pGeom);
143606 }
143607 }
143608 sqlite3_free(pCsr->aConstraint);
143609 pCsr->aConstraint = 0;
143610 }
@@ -143613,16 +144410,17 @@
143613 /*
143614 ** Rtree virtual table module xClose method.
143615 */
143616 static int rtreeClose(sqlite3_vtab_cursor *cur){
143617 Rtree *pRtree = (Rtree *)(cur->pVtab);
143618 int rc;
143619 RtreeCursor *pCsr = (RtreeCursor *)cur;
143620 freeCursorConstraints(pCsr);
143621 rc = nodeRelease(pRtree, pCsr->pNode);
 
143622 sqlite3_free(pCsr);
143623 return rc;
143624 }
143625
143626 /*
143627 ** Rtree virtual table module xEof method.
143628 **
@@ -143629,198 +144427,168 @@
143629 ** Return non-zero if the cursor does not currently point to a valid
143630 ** record (i.e if the scan has finished), or zero otherwise.
143631 */
143632 static int rtreeEof(sqlite3_vtab_cursor *cur){
143633 RtreeCursor *pCsr = (RtreeCursor *)cur;
143634 return (pCsr->pNode==0);
143635 }
143636
143637 /*
143638 ** The r-tree constraint passed as the second argument to this function is
143639 ** guaranteed to be a MATCH constraint.
143640 */
143641 static int testRtreeGeom(
143642 Rtree *pRtree, /* R-Tree object */
143643 RtreeConstraint *pConstraint, /* MATCH constraint to test */
143644 RtreeCell *pCell, /* Cell to test */
143645 int *pbRes /* OUT: Test result */
143646 ){
143647 int i;
143648 RtreeDValue aCoord[RTREE_MAX_DIMENSIONS*2];
143649 int nCoord = pRtree->nDim*2;
143650
143651 assert( pConstraint->op==RTREE_MATCH );
143652 assert( pConstraint->pGeom );
143653
143654 for(i=0; i<nCoord; i++){
143655 aCoord[i] = DCOORD(pCell->aCoord[i]);
143656 }
143657 return pConstraint->xGeom(pConstraint->pGeom, nCoord, aCoord, pbRes);
143658 }
143659
143660 /*
143661 ** Cursor pCursor currently points to a cell in a non-leaf page.
143662 ** Set *pbEof to true if the sub-tree headed by the cell is filtered
143663 ** (excluded) by the constraints in the pCursor->aConstraint[]
143664 ** array, or false otherwise.
143665 **
143666 ** Return SQLITE_OK if successful or an SQLite error code if an error
143667 ** occurs within a geometry callback.
143668 */
143669 static int testRtreeCell(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
143670 RtreeCell cell;
143671 int ii;
143672 int bRes = 0;
143673 int rc = SQLITE_OK;
143674
143675 nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
143676 for(ii=0; bRes==0 && ii<pCursor->nConstraint; ii++){
143677 RtreeConstraint *p = &pCursor->aConstraint[ii];
143678 RtreeDValue cell_min = DCOORD(cell.aCoord[(p->iCoord>>1)*2]);
143679 RtreeDValue cell_max = DCOORD(cell.aCoord[(p->iCoord>>1)*2+1]);
143680
143681 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
143682 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
143683 );
143684
143685 switch( p->op ){
143686 case RTREE_LE: case RTREE_LT:
143687 bRes = p->rValue<cell_min;
143688 break;
143689
143690 case RTREE_GE: case RTREE_GT:
143691 bRes = p->rValue>cell_max;
143692 break;
143693
143694 case RTREE_EQ:
143695 bRes = (p->rValue>cell_max || p->rValue<cell_min);
143696 break;
143697
143698 default: {
143699 assert( p->op==RTREE_MATCH );
143700 rc = testRtreeGeom(pRtree, p, &cell, &bRes);
143701 bRes = !bRes;
143702 break;
143703 }
143704 }
143705 }
143706
143707 *pbEof = bRes;
143708 return rc;
143709 }
143710
143711 /*
143712 ** Test if the cell that cursor pCursor currently points to
143713 ** would be filtered (excluded) by the constraints in the
143714 ** pCursor->aConstraint[] array. If so, set *pbEof to true before
143715 ** returning. If the cell is not filtered (excluded) by the constraints,
143716 ** set pbEof to zero.
143717 **
143718 ** Return SQLITE_OK if successful or an SQLite error code if an error
143719 ** occurs within a geometry callback.
143720 **
143721 ** This function assumes that the cell is part of a leaf node.
143722 */
143723 static int testRtreeEntry(Rtree *pRtree, RtreeCursor *pCursor, int *pbEof){
143724 RtreeCell cell;
143725 int ii;
143726 *pbEof = 0;
143727
143728 nodeGetCell(pRtree, pCursor->pNode, pCursor->iCell, &cell);
143729 for(ii=0; ii<pCursor->nConstraint; ii++){
143730 RtreeConstraint *p = &pCursor->aConstraint[ii];
143731 RtreeDValue coord = DCOORD(cell.aCoord[p->iCoord]);
143732 int res;
143733 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
143734 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_MATCH
143735 );
143736 switch( p->op ){
143737 case RTREE_LE: res = (coord<=p->rValue); break;
143738 case RTREE_LT: res = (coord<p->rValue); break;
143739 case RTREE_GE: res = (coord>=p->rValue); break;
143740 case RTREE_GT: res = (coord>p->rValue); break;
143741 case RTREE_EQ: res = (coord==p->rValue); break;
143742 default: {
143743 int rc;
143744 assert( p->op==RTREE_MATCH );
143745 rc = testRtreeGeom(pRtree, p, &cell, &res);
143746 if( rc!=SQLITE_OK ){
143747 return rc;
143748 }
143749 break;
143750 }
143751 }
143752
143753 if( !res ){
143754 *pbEof = 1;
143755 return SQLITE_OK;
143756 }
143757 }
143758
143759 return SQLITE_OK;
143760 }
143761
143762 /*
143763 ** Cursor pCursor currently points at a node that heads a sub-tree of
143764 ** height iHeight (if iHeight==0, then the node is a leaf). Descend
143765 ** to point to the left-most cell of the sub-tree that matches the
143766 ** configured constraints.
143767 */
143768 static int descendToCell(
143769 Rtree *pRtree,
143770 RtreeCursor *pCursor,
143771 int iHeight,
143772 int *pEof /* OUT: Set to true if cannot descend */
143773 ){
143774 int isEof;
143775 int rc;
143776 int ii;
143777 RtreeNode *pChild;
143778 sqlite3_int64 iRowid;
143779
143780 RtreeNode *pSavedNode = pCursor->pNode;
143781 int iSavedCell = pCursor->iCell;
143782
143783 assert( iHeight>=0 );
143784
143785 if( iHeight==0 ){
143786 rc = testRtreeEntry(pRtree, pCursor, &isEof);
143787 }else{
143788 rc = testRtreeCell(pRtree, pCursor, &isEof);
143789 }
143790 if( rc!=SQLITE_OK || isEof || iHeight==0 ){
143791 goto descend_to_cell_out;
143792 }
143793
143794 iRowid = nodeGetRowid(pRtree, pCursor->pNode, pCursor->iCell);
143795 rc = nodeAcquire(pRtree, iRowid, pCursor->pNode, &pChild);
143796 if( rc!=SQLITE_OK ){
143797 goto descend_to_cell_out;
143798 }
143799
143800 nodeRelease(pRtree, pCursor->pNode);
143801 pCursor->pNode = pChild;
143802 isEof = 1;
143803 for(ii=0; isEof && ii<NCELL(pChild); ii++){
143804 pCursor->iCell = ii;
143805 rc = descendToCell(pRtree, pCursor, iHeight-1, &isEof);
143806 if( rc!=SQLITE_OK ){
143807 goto descend_to_cell_out;
143808 }
143809 }
143810
143811 if( isEof ){
143812 assert( pCursor->pNode==pChild );
143813 nodeReference(pSavedNode);
143814 nodeRelease(pRtree, pChild);
143815 pCursor->pNode = pSavedNode;
143816 pCursor->iCell = iSavedCell;
143817 }
143818
143819 descend_to_cell_out:
143820 *pEof = isEof;
143821 return rc;
143822 }
143823
143824 /*
143825 ** One of the cells in node pNode is guaranteed to have a 64-bit
143826 ** integer value equal to iRowid. Return the index of this cell.
@@ -143831,10 +144599,11 @@
143831 i64 iRowid,
143832 int *piIndex
143833 ){
143834 int ii;
143835 int nCell = NCELL(pNode);
 
143836 for(ii=0; ii<nCell; ii++){
143837 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
143838 *piIndex = ii;
143839 return SQLITE_OK;
143840 }
@@ -143852,82 +144621,342 @@
143852 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
143853 }
143854 *piIndex = -1;
143855 return SQLITE_OK;
143856 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143857
143858 /*
143859 ** Rtree virtual table module xNext method.
143860 */
143861 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
143862 Rtree *pRtree = (Rtree *)(pVtabCursor->pVtab);
143863 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
143864 int rc = SQLITE_OK;
143865
143866 /* RtreeCursor.pNode must not be NULL. If is is NULL, then this cursor is
143867 ** already at EOF. It is against the rules to call the xNext() method of
143868 ** a cursor that has already reached EOF.
143869 */
143870 assert( pCsr->pNode );
143871
143872 if( pCsr->iStrategy==1 ){
143873 /* This "scan" is a direct lookup by rowid. There is no next entry. */
143874 nodeRelease(pRtree, pCsr->pNode);
143875 pCsr->pNode = 0;
143876 }else{
143877 /* Move to the next entry that matches the configured constraints. */
143878 int iHeight = 0;
143879 while( pCsr->pNode ){
143880 RtreeNode *pNode = pCsr->pNode;
143881 int nCell = NCELL(pNode);
143882 for(pCsr->iCell++; pCsr->iCell<nCell; pCsr->iCell++){
143883 int isEof;
143884 rc = descendToCell(pRtree, pCsr, iHeight, &isEof);
143885 if( rc!=SQLITE_OK || !isEof ){
143886 return rc;
143887 }
143888 }
143889 pCsr->pNode = pNode->pParent;
143890 rc = nodeParentIndex(pRtree, pNode, &pCsr->iCell);
143891 if( rc!=SQLITE_OK ){
143892 return rc;
143893 }
143894 nodeReference(pCsr->pNode);
143895 nodeRelease(pRtree, pNode);
143896 iHeight++;
143897 }
143898 }
143899
143900 return rc;
143901 }
143902
143903 /*
143904 ** Rtree virtual table module xRowid method.
143905 */
143906 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
143907 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
143908 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
143909
143910 assert(pCsr->pNode);
143911 *pRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
143912
143913 return SQLITE_OK;
 
 
143914 }
143915
143916 /*
143917 ** Rtree virtual table module xColumn method.
143918 */
143919 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
143920 Rtree *pRtree = (Rtree *)cur->pVtab;
143921 RtreeCursor *pCsr = (RtreeCursor *)cur;
 
 
 
 
143922
 
 
143923 if( i==0 ){
143924 i64 iRowid = nodeGetRowid(pRtree, pCsr->pNode, pCsr->iCell);
143925 sqlite3_result_int64(ctx, iRowid);
143926 }else{
143927 RtreeCoord c;
143928 nodeGetCoord(pRtree, pCsr->pNode, pCsr->iCell, i-1, &c);
143929 #ifndef SQLITE_RTREE_INT_ONLY
143930 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
143931 sqlite3_result_double(ctx, c.f);
143932 }else
143933 #endif
@@ -143934,11 +144963,10 @@
143934 {
143935 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
143936 sqlite3_result_int(ctx, c.i);
143937 }
143938 }
143939
143940 return SQLITE_OK;
143941 }
143942
143943 /*
143944 ** Use nodeAcquire() to obtain the leaf node containing the record with
@@ -143945,16 +144973,22 @@
143945 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
143946 ** return SQLITE_OK. If there is no such record in the table, set
143947 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
143948 ** to zero and return an SQLite error code.
143949 */
143950 static int findLeafNode(Rtree *pRtree, i64 iRowid, RtreeNode **ppLeaf){
 
 
 
 
 
143951 int rc;
143952 *ppLeaf = 0;
143953 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
143954 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
143955 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
 
143956 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
143957 sqlite3_reset(pRtree->pReadRowid);
143958 }else{
143959 rc = sqlite3_reset(pRtree->pReadRowid);
143960 }
@@ -143966,13 +145000,14 @@
143966 ** as the second argument for a MATCH constraint. The value passed as the
143967 ** first argument to this function is the right-hand operand to the MATCH
143968 ** operator.
143969 */
143970 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
143971 RtreeMatchArg *p;
143972 sqlite3_rtree_geometry *pGeom;
143973 int nBlob;
 
143974
143975 /* Check that value is actually a blob. */
143976 if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
143977
143978 /* Check that the blob is roughly the right size. */
@@ -143981,31 +145016,33 @@
143981 || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
143982 ){
143983 return SQLITE_ERROR;
143984 }
143985
143986 pGeom = (sqlite3_rtree_geometry *)sqlite3_malloc(
143987 sizeof(sqlite3_rtree_geometry) + nBlob
143988 );
143989 if( !pGeom ) return SQLITE_NOMEM;
143990 memset(pGeom, 0, sizeof(sqlite3_rtree_geometry));
143991 p = (RtreeMatchArg *)&pGeom[1];
143992
143993 memcpy(p, sqlite3_value_blob(pValue), nBlob);
143994 if( p->magic!=RTREE_GEOMETRY_MAGIC
143995 || nBlob!=(int)(sizeof(RtreeMatchArg) + (p->nParam-1)*sizeof(RtreeDValue))
143996 ){
143997 sqlite3_free(pGeom);
143998 return SQLITE_ERROR;
143999 }
 
 
 
144000
144001 pGeom->pContext = p->pContext;
144002 pGeom->nParam = p->nParam;
144003 pGeom->aParam = p->aParam;
144004
144005 pCons->xGeom = p->xGeom;
144006 pCons->pGeom = pGeom;
 
144007 return SQLITE_OK;
144008 }
144009
144010 /*
144011 ** Rtree virtual table module xFilter method.
@@ -144015,91 +145052,96 @@
144015 int idxNum, const char *idxStr,
144016 int argc, sqlite3_value **argv
144017 ){
144018 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
144019 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
144020
144021 RtreeNode *pRoot = 0;
144022 int ii;
144023 int rc = SQLITE_OK;
 
144024
144025 rtreeReference(pRtree);
144026
144027 freeCursorConstraints(pCsr);
144028 pCsr->iStrategy = idxNum;
144029
144030 if( idxNum==1 ){
144031 /* Special case - lookup by rowid. */
144032 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
 
144033 i64 iRowid = sqlite3_value_int64(argv[0]);
144034 rc = findLeafNode(pRtree, iRowid, &pLeaf);
144035 pCsr->pNode = pLeaf;
144036 if( pLeaf ){
144037 assert( rc==SQLITE_OK );
144038 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &pCsr->iCell);
 
 
 
 
 
 
 
 
144039 }
144040 }else{
144041 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
144042 ** with the configured constraints.
144043 */
144044 if( argc>0 ){
 
144045 pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
144046 pCsr->nConstraint = argc;
144047 if( !pCsr->aConstraint ){
144048 rc = SQLITE_NOMEM;
144049 }else{
144050 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
 
144051 assert( (idxStr==0 && argc==0)
144052 || (idxStr && (int)strlen(idxStr)==argc*2) );
144053 for(ii=0; ii<argc; ii++){
144054 RtreeConstraint *p = &pCsr->aConstraint[ii];
144055 p->op = idxStr[ii*2];
144056 p->iCoord = idxStr[ii*2+1]-'a';
144057 if( p->op==RTREE_MATCH ){
144058 /* A MATCH operator. The right-hand-side must be a blob that
144059 ** can be cast into an RtreeMatchArg object. One created using
144060 ** an sqlite3_rtree_geometry_callback() SQL user function.
144061 */
144062 rc = deserializeGeometry(argv[ii], p);
144063 if( rc!=SQLITE_OK ){
144064 break;
144065 }
 
 
 
144066 }else{
144067 #ifdef SQLITE_RTREE_INT_ONLY
144068 p->rValue = sqlite3_value_int64(argv[ii]);
144069 #else
144070 p->rValue = sqlite3_value_double(argv[ii]);
144071 #endif
144072 }
144073 }
144074 }
144075 }
144076
144077 if( rc==SQLITE_OK ){
144078 pCsr->pNode = 0;
144079 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
144080 }
144081 if( rc==SQLITE_OK ){
144082 int isEof = 1;
144083 int nCell = NCELL(pRoot);
144084 pCsr->pNode = pRoot;
144085 for(pCsr->iCell=0; rc==SQLITE_OK && pCsr->iCell<nCell; pCsr->iCell++){
144086 assert( pCsr->pNode==pRoot );
144087 rc = descendToCell(pRtree, pCsr, pRtree->iDepth, &isEof);
144088 if( !isEof ){
144089 break;
144090 }
144091 }
144092 if( rc==SQLITE_OK && isEof ){
144093 assert( pCsr->pNode==pRoot );
144094 nodeRelease(pRtree, pRoot);
144095 pCsr->pNode = 0;
144096 }
144097 assert( rc!=SQLITE_OK || !pCsr->pNode || pCsr->iCell<NCELL(pCsr->pNode) );
144098 }
144099 }
144100
144101 rtreeRelease(pRtree);
144102 return rc;
144103 }
144104
144105 /*
@@ -144197,11 +145239,11 @@
144197 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
144198 op = RTREE_MATCH;
144199 break;
144200 }
144201 zIdxStr[iIdx++] = op;
144202 zIdxStr[iIdx++] = p->iColumn - 1 + 'a';
144203 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
144204 pIdxInfo->aConstraintUsage[ii].omit = 1;
144205 }
144206 }
144207
@@ -144290,66 +145332,36 @@
144290 area = cellArea(pRtree, &cell);
144291 cellUnion(pRtree, &cell, pCell);
144292 return (cellArea(pRtree, &cell)-area);
144293 }
144294
144295 #if VARIANT_RSTARTREE_CHOOSESUBTREE || VARIANT_RSTARTREE_SPLIT
144296 static RtreeDValue cellOverlap(
144297 Rtree *pRtree,
144298 RtreeCell *p,
144299 RtreeCell *aCell,
144300 int nCell,
144301 int iExclude
144302 ){
144303 int ii;
144304 RtreeDValue overlap = 0.0;
144305 for(ii=0; ii<nCell; ii++){
144306 #if VARIANT_RSTARTREE_CHOOSESUBTREE
144307 if( ii!=iExclude )
144308 #else
144309 assert( iExclude==-1 );
144310 UNUSED_PARAMETER(iExclude);
144311 #endif
144312 {
144313 int jj;
144314 RtreeDValue o = (RtreeDValue)1;
144315 for(jj=0; jj<(pRtree->nDim*2); jj+=2){
144316 RtreeDValue x1, x2;
144317
144318 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
144319 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
144320
144321 if( x2<x1 ){
144322 o = 0.0;
144323 break;
144324 }else{
144325 o = o * (x2-x1);
144326 }
144327 }
144328 overlap += o;
144329 }
144330 }
144331 return overlap;
144332 }
144333 #endif
144334
144335 #if VARIANT_RSTARTREE_CHOOSESUBTREE
144336 static RtreeDValue cellOverlapEnlargement(
144337 Rtree *pRtree,
144338 RtreeCell *p,
144339 RtreeCell *pInsert,
144340 RtreeCell *aCell,
144341 int nCell,
144342 int iExclude
144343 ){
144344 RtreeDValue before, after;
144345 before = cellOverlap(pRtree, p, aCell, nCell, iExclude);
144346 cellUnion(pRtree, p, pInsert);
144347 after = cellOverlap(pRtree, p, aCell, nCell, iExclude);
144348 return (after-before);
144349 }
144350 #endif
144351
144352
144353 /*
144354 ** This function implements the ChooseLeaf algorithm from Gutman[84].
144355 ** ChooseSubTree in r*tree terminology.
@@ -144367,39 +145379,19 @@
144367
144368 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
144369 int iCell;
144370 sqlite3_int64 iBest = 0;
144371
144372 RtreeDValue fMinGrowth = 0.0;
144373 RtreeDValue fMinArea = 0.0;
144374 #if VARIANT_RSTARTREE_CHOOSESUBTREE
144375 RtreeDValue fMinOverlap = 0.0;
144376 RtreeDValue overlap;
144377 #endif
144378
144379 int nCell = NCELL(pNode);
144380 RtreeCell cell;
144381 RtreeNode *pChild;
144382
144383 RtreeCell *aCell = 0;
144384
144385 #if VARIANT_RSTARTREE_CHOOSESUBTREE
144386 if( ii==(pRtree->iDepth-1) ){
144387 int jj;
144388 aCell = sqlite3_malloc(sizeof(RtreeCell)*nCell);
144389 if( !aCell ){
144390 rc = SQLITE_NOMEM;
144391 nodeRelease(pRtree, pNode);
144392 pNode = 0;
144393 continue;
144394 }
144395 for(jj=0; jj<nCell; jj++){
144396 nodeGetCell(pRtree, pNode, jj, &aCell[jj]);
144397 }
144398 }
144399 #endif
144400
144401 /* Select the child node which will be enlarged the least if pCell
144402 ** is inserted into it. Resolve ties by choosing the entry with
144403 ** the smallest area.
144404 */
144405 for(iCell=0; iCell<nCell; iCell++){
@@ -144407,30 +145399,13 @@
144407 RtreeDValue growth;
144408 RtreeDValue area;
144409 nodeGetCell(pRtree, pNode, iCell, &cell);
144410 growth = cellGrowth(pRtree, &cell, pCell);
144411 area = cellArea(pRtree, &cell);
144412
144413 #if VARIANT_RSTARTREE_CHOOSESUBTREE
144414 if( ii==(pRtree->iDepth-1) ){
144415 overlap = cellOverlapEnlargement(pRtree,&cell,pCell,aCell,nCell,iCell);
144416 }else{
144417 overlap = 0.0;
144418 }
144419 if( (iCell==0)
144420 || (overlap<fMinOverlap)
144421 || (overlap==fMinOverlap && growth<fMinGrowth)
144422 || (overlap==fMinOverlap && growth==fMinGrowth && area<fMinArea)
144423 ){
144424 bBest = 1;
144425 fMinOverlap = overlap;
144426 }
144427 #else
144428 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
144429 bBest = 1;
144430 }
144431 #endif
144432 if( bBest ){
144433 fMinGrowth = growth;
144434 fMinArea = area;
144435 iBest = cell.iRowid;
144436 }
@@ -144497,159 +145472,10 @@
144497 return sqlite3_reset(pRtree->pWriteParent);
144498 }
144499
144500 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
144501
144502 #if VARIANT_GUTTMAN_LINEAR_SPLIT
144503 /*
144504 ** Implementation of the linear variant of the PickNext() function from
144505 ** Guttman[84].
144506 */
144507 static RtreeCell *LinearPickNext(
144508 Rtree *pRtree,
144509 RtreeCell *aCell,
144510 int nCell,
144511 RtreeCell *pLeftBox,
144512 RtreeCell *pRightBox,
144513 int *aiUsed
144514 ){
144515 int ii;
144516 for(ii=0; aiUsed[ii]; ii++);
144517 aiUsed[ii] = 1;
144518 return &aCell[ii];
144519 }
144520
144521 /*
144522 ** Implementation of the linear variant of the PickSeeds() function from
144523 ** Guttman[84].
144524 */
144525 static void LinearPickSeeds(
144526 Rtree *pRtree,
144527 RtreeCell *aCell,
144528 int nCell,
144529 int *piLeftSeed,
144530 int *piRightSeed
144531 ){
144532 int i;
144533 int iLeftSeed = 0;
144534 int iRightSeed = 1;
144535 RtreeDValue maxNormalInnerWidth = (RtreeDValue)0;
144536
144537 /* Pick two "seed" cells from the array of cells. The algorithm used
144538 ** here is the LinearPickSeeds algorithm from Gutman[1984]. The
144539 ** indices of the two seed cells in the array are stored in local
144540 ** variables iLeftSeek and iRightSeed.
144541 */
144542 for(i=0; i<pRtree->nDim; i++){
144543 RtreeDValue x1 = DCOORD(aCell[0].aCoord[i*2]);
144544 RtreeDValue x2 = DCOORD(aCell[0].aCoord[i*2+1]);
144545 RtreeDValue x3 = x1;
144546 RtreeDValue x4 = x2;
144547 int jj;
144548
144549 int iCellLeft = 0;
144550 int iCellRight = 0;
144551
144552 for(jj=1; jj<nCell; jj++){
144553 RtreeDValue left = DCOORD(aCell[jj].aCoord[i*2]);
144554 RtreeDValue right = DCOORD(aCell[jj].aCoord[i*2+1]);
144555
144556 if( left<x1 ) x1 = left;
144557 if( right>x4 ) x4 = right;
144558 if( left>x3 ){
144559 x3 = left;
144560 iCellRight = jj;
144561 }
144562 if( right<x2 ){
144563 x2 = right;
144564 iCellLeft = jj;
144565 }
144566 }
144567
144568 if( x4!=x1 ){
144569 RtreeDValue normalwidth = (x3 - x2) / (x4 - x1);
144570 if( normalwidth>maxNormalInnerWidth ){
144571 iLeftSeed = iCellLeft;
144572 iRightSeed = iCellRight;
144573 }
144574 }
144575 }
144576
144577 *piLeftSeed = iLeftSeed;
144578 *piRightSeed = iRightSeed;
144579 }
144580 #endif /* VARIANT_GUTTMAN_LINEAR_SPLIT */
144581
144582 #if VARIANT_GUTTMAN_QUADRATIC_SPLIT
144583 /*
144584 ** Implementation of the quadratic variant of the PickNext() function from
144585 ** Guttman[84].
144586 */
144587 static RtreeCell *QuadraticPickNext(
144588 Rtree *pRtree,
144589 RtreeCell *aCell,
144590 int nCell,
144591 RtreeCell *pLeftBox,
144592 RtreeCell *pRightBox,
144593 int *aiUsed
144594 ){
144595 #define FABS(a) ((a)<0.0?-1.0*(a):(a))
144596
144597 int iSelect = -1;
144598 RtreeDValue fDiff;
144599 int ii;
144600 for(ii=0; ii<nCell; ii++){
144601 if( aiUsed[ii]==0 ){
144602 RtreeDValue left = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
144603 RtreeDValue right = cellGrowth(pRtree, pLeftBox, &aCell[ii]);
144604 RtreeDValue diff = FABS(right-left);
144605 if( iSelect<0 || diff>fDiff ){
144606 fDiff = diff;
144607 iSelect = ii;
144608 }
144609 }
144610 }
144611 aiUsed[iSelect] = 1;
144612 return &aCell[iSelect];
144613 }
144614
144615 /*
144616 ** Implementation of the quadratic variant of the PickSeeds() function from
144617 ** Guttman[84].
144618 */
144619 static void QuadraticPickSeeds(
144620 Rtree *pRtree,
144621 RtreeCell *aCell,
144622 int nCell,
144623 int *piLeftSeed,
144624 int *piRightSeed
144625 ){
144626 int ii;
144627 int jj;
144628
144629 int iLeftSeed = 0;
144630 int iRightSeed = 1;
144631 RtreeDValue fWaste = 0.0;
144632
144633 for(ii=0; ii<nCell; ii++){
144634 for(jj=ii+1; jj<nCell; jj++){
144635 RtreeDValue right = cellArea(pRtree, &aCell[jj]);
144636 RtreeDValue growth = cellGrowth(pRtree, &aCell[ii], &aCell[jj]);
144637 RtreeDValue waste = growth - right;
144638
144639 if( waste>fWaste ){
144640 iLeftSeed = ii;
144641 iRightSeed = jj;
144642 fWaste = waste;
144643 }
144644 }
144645 }
144646
144647 *piLeftSeed = iLeftSeed;
144648 *piRightSeed = iRightSeed;
144649 }
144650 #endif /* VARIANT_GUTTMAN_QUADRATIC_SPLIT */
144651
144652 /*
144653 ** Arguments aIdx, aDistance and aSpare all point to arrays of size
144654 ** nIdx. The aIdx array contains the set of integers from 0 to
144655 ** (nIdx-1) in no particular order. This function sorts the values
@@ -144786,11 +145612,10 @@
144786 }
144787 #endif
144788 }
144789 }
144790
144791 #if VARIANT_RSTARTREE_SPLIT
144792 /*
144793 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
144794 */
144795 static int splitNodeStartree(
144796 Rtree *pRtree,
@@ -144805,11 +145630,11 @@
144805 int *aSpare;
144806 int ii;
144807
144808 int iBestDim = 0;
144809 int iBestSplit = 0;
144810 RtreeDValue fBestMargin = 0.0;
144811
144812 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
144813
144814 aaSorted = (int **)sqlite3_malloc(nByte);
144815 if( !aaSorted ){
@@ -144826,13 +145651,13 @@
144826 }
144827 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
144828 }
144829
144830 for(ii=0; ii<pRtree->nDim; ii++){
144831 RtreeDValue margin = 0.0;
144832 RtreeDValue fBestOverlap = 0.0;
144833 RtreeDValue fBestArea = 0.0;
144834 int iBestLeft = 0;
144835 int nLeft;
144836
144837 for(
144838 nLeft=RTREE_MINCELLS(pRtree);
@@ -144854,11 +145679,11 @@
144854 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
144855 }
144856 }
144857 margin += cellMargin(pRtree, &left);
144858 margin += cellMargin(pRtree, &right);
144859 overlap = cellOverlap(pRtree, &left, &right, 1, -1);
144860 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
144861 if( (nLeft==RTREE_MINCELLS(pRtree))
144862 || (overlap<fBestOverlap)
144863 || (overlap==fBestOverlap && area<fBestArea)
144864 ){
@@ -144886,67 +145711,11 @@
144886 }
144887
144888 sqlite3_free(aaSorted);
144889 return SQLITE_OK;
144890 }
144891 #endif
144892
144893 #if VARIANT_GUTTMAN_SPLIT
144894 /*
144895 ** Implementation of the regular R-tree SplitNode from Guttman[1984].
144896 */
144897 static int splitNodeGuttman(
144898 Rtree *pRtree,
144899 RtreeCell *aCell,
144900 int nCell,
144901 RtreeNode *pLeft,
144902 RtreeNode *pRight,
144903 RtreeCell *pBboxLeft,
144904 RtreeCell *pBboxRight
144905 ){
144906 int iLeftSeed = 0;
144907 int iRightSeed = 1;
144908 int *aiUsed;
144909 int i;
144910
144911 aiUsed = sqlite3_malloc(sizeof(int)*nCell);
144912 if( !aiUsed ){
144913 return SQLITE_NOMEM;
144914 }
144915 memset(aiUsed, 0, sizeof(int)*nCell);
144916
144917 PickSeeds(pRtree, aCell, nCell, &iLeftSeed, &iRightSeed);
144918
144919 memcpy(pBboxLeft, &aCell[iLeftSeed], sizeof(RtreeCell));
144920 memcpy(pBboxRight, &aCell[iRightSeed], sizeof(RtreeCell));
144921 nodeInsertCell(pRtree, pLeft, &aCell[iLeftSeed]);
144922 nodeInsertCell(pRtree, pRight, &aCell[iRightSeed]);
144923 aiUsed[iLeftSeed] = 1;
144924 aiUsed[iRightSeed] = 1;
144925
144926 for(i=nCell-2; i>0; i--){
144927 RtreeCell *pNext;
144928 pNext = PickNext(pRtree, aCell, nCell, pBboxLeft, pBboxRight, aiUsed);
144929 RtreeDValue diff =
144930 cellGrowth(pRtree, pBboxLeft, pNext) -
144931 cellGrowth(pRtree, pBboxRight, pNext)
144932 ;
144933 if( (RTREE_MINCELLS(pRtree)-NCELL(pRight)==i)
144934 || (diff>0.0 && (RTREE_MINCELLS(pRtree)-NCELL(pLeft)!=i))
144935 ){
144936 nodeInsertCell(pRtree, pRight, pNext);
144937 cellUnion(pRtree, pBboxRight, pNext);
144938 }else{
144939 nodeInsertCell(pRtree, pLeft, pNext);
144940 cellUnion(pRtree, pBboxLeft, pNext);
144941 }
144942 }
144943
144944 sqlite3_free(aiUsed);
144945 return SQLITE_OK;
144946 }
144947 #endif
144948
144949 static int updateMapping(
144950 Rtree *pRtree,
144951 i64 iRowid,
144952 RtreeNode *pNode,
@@ -145020,11 +145789,12 @@
145020 }
145021
145022 memset(pLeft->zData, 0, pRtree->iNodeSize);
145023 memset(pRight->zData, 0, pRtree->iNodeSize);
145024
145025 rc = AssignCells(pRtree, aCell, nCell, pLeft, pRight, &leftbbox, &rightbbox);
 
145026 if( rc!=SQLITE_OK ){
145027 goto splitnode_out;
145028 }
145029
145030 /* Ensure both child nodes have node numbers assigned to them by calling
@@ -145303,11 +146073,11 @@
145303 for(iDim=0; iDim<pRtree->nDim; iDim++){
145304 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
145305 }
145306
145307 for(ii=0; ii<nCell; ii++){
145308 aDistance[ii] = 0.0;
145309 for(iDim=0; iDim<pRtree->nDim; iDim++){
145310 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
145311 DCOORD(aCell[ii].aCoord[iDim*2]));
145312 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
145313 }
@@ -145369,20 +146139,16 @@
145369 nodeReference(pNode);
145370 pChild->pParent = pNode;
145371 }
145372 }
145373 if( nodeInsertCell(pRtree, pNode, pCell) ){
145374 #if VARIANT_RSTARTREE_REINSERT
145375 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
145376 rc = SplitNode(pRtree, pNode, pCell, iHeight);
145377 }else{
145378 pRtree->iReinsertHeight = iHeight;
145379 rc = Reinsert(pRtree, pNode, pCell, iHeight);
145380 }
145381 #else
145382 rc = SplitNode(pRtree, pNode, pCell, iHeight);
145383 #endif
145384 }else{
145385 rc = AdjustTree(pRtree, pNode, pCell);
145386 if( rc==SQLITE_OK ){
145387 if( iHeight==0 ){
145388 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
@@ -145448,11 +146214,11 @@
145448
145449 /* Obtain a reference to the leaf node that contains the entry
145450 ** about to be deleted.
145451 */
145452 if( rc==SQLITE_OK ){
145453 rc = findLeafNode(pRtree, iDelete, &pLeaf);
145454 }
145455
145456 /* Delete the cell in question from the leaf node. */
145457 if( rc==SQLITE_OK ){
145458 int rc2;
@@ -145785,11 +146551,12 @@
145785
145786 if( isCreate ){
145787 char *zCreate = sqlite3_mprintf(
145788 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
145789 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
145790 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY, parentnode INTEGER);"
 
145791 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
145792 zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
145793 );
145794 if( !zCreate ){
145795 return SQLITE_NOMEM;
@@ -145999,14 +146766,14 @@
145999
146000 /*
146001 ** Implementation of a scalar function that decodes r-tree nodes to
146002 ** human readable strings. This can be used for debugging and analysis.
146003 **
146004 ** The scalar function takes two arguments, a blob of data containing
146005 ** an r-tree node, and the number of dimensions the r-tree indexes.
146006 ** For a two-dimensional r-tree structure called "rt", to deserialize
146007 ** all nodes, a statement like:
146008 **
146009 ** SELECT rtreenode(2, data) FROM rt_node;
146010 **
146011 ** The human readable string takes the form of a Tcl list with one
146012 ** entry for each cell in the r-tree node. Each entry is itself a
@@ -146035,11 +146802,11 @@
146035 nodeGetCell(&tree, &node, ii, &cell);
146036 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
146037 nCell = (int)strlen(zCell);
146038 for(jj=0; jj<tree.nDim*2; jj++){
146039 #ifndef SQLITE_RTREE_INT_ONLY
146040 sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
146041 (double)cell.aCoord[jj].f);
146042 #else
146043 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
146044 cell.aCoord[jj].i);
146045 #endif
@@ -146056,10 +146823,19 @@
146056 }
146057
146058 sqlite3_result_text(ctx, zText, -1, sqlite3_free);
146059 }
146060
 
 
 
 
 
 
 
 
 
146061 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
146062 UNUSED_PARAMETER(nArg);
146063 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
146064 || sqlite3_value_bytes(apArg[0])<2
146065 ){
@@ -146098,26 +146874,35 @@
146098
146099 return rc;
146100 }
146101
146102 /*
146103 ** A version of sqlite3_free() that can be used as a callback. This is used
146104 ** in two places - as the destructor for the blob value returned by the
146105 ** invocation of a geometry function, and as the destructor for the geometry
146106 ** functions themselves.
 
146107 */
146108 static void doSqlite3Free(void *p){
 
 
146109 sqlite3_free(p);
146110 }
146111
146112 /*
146113 ** Each call to sqlite3_rtree_geometry_callback() creates an ordinary SQLite
146114 ** scalar user function. This C function is the callback used for all such
146115 ** registered SQL functions.
 
 
 
 
 
146116 **
146117 ** The scalar user functions return a blob that is interpreted by r-tree
146118 ** table MATCH operators.
 
146119 */
146120 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
146121 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
146122 RtreeMatchArg *pBlob;
146123 int nBlob;
@@ -146127,45 +146912,68 @@
146127 if( !pBlob ){
146128 sqlite3_result_error_nomem(ctx);
146129 }else{
146130 int i;
146131 pBlob->magic = RTREE_GEOMETRY_MAGIC;
146132 pBlob->xGeom = pGeomCtx->xGeom;
146133 pBlob->pContext = pGeomCtx->pContext;
146134 pBlob->nParam = nArg;
146135 for(i=0; i<nArg; i++){
146136 #ifdef SQLITE_RTREE_INT_ONLY
146137 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
146138 #else
146139 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
146140 #endif
146141 }
146142 sqlite3_result_blob(ctx, pBlob, nBlob, doSqlite3Free);
146143 }
146144 }
146145
146146 /*
146147 ** Register a new geometry function for use with the r-tree MATCH operator.
146148 */
146149 SQLITE_API int sqlite3_rtree_geometry_callback(
146150 sqlite3 *db,
146151 const char *zGeom,
146152 int (*xGeom)(sqlite3_rtree_geometry *, int, RtreeDValue *, int *),
146153 void *pContext
146154 ){
146155 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
146156
146157 /* Allocate and populate the context object. */
146158 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
146159 if( !pGeomCtx ) return SQLITE_NOMEM;
146160 pGeomCtx->xGeom = xGeom;
 
 
146161 pGeomCtx->pContext = pContext;
146162
146163 /* Create the new user-function. Register a destructor function to delete
146164 ** the context object when it is no longer required. */
146165 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
146166 (void *)pGeomCtx, geomCallback, 0, 0, doSqlite3Free
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146167 );
146168 }
146169
146170 #if !SQLITE_CORE
146171 #ifdef _WIN32
146172
--- src/sqlite3.c
+++ src/sqlite3.c
@@ -222,11 +222,11 @@
222 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
223 ** [sqlite_version()] and [sqlite_source_id()].
224 */
225 #define SQLITE_VERSION "3.8.5"
226 #define SQLITE_VERSION_NUMBER 3008005
227 #define SQLITE_SOURCE_ID "2014-05-28 20:22:28 d018a34a05cec6adda61ed225d084c587343f2a6"
228
229 /*
230 ** CAPI3REF: Run-Time Library Version Numbers
231 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
232 **
@@ -673,11 +673,14 @@
673 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
674 ** after reboot following a crash or power loss, the only bytes in a
675 ** file that were written at the application level might have changed
676 ** and that adjacent bytes, even bytes within the same sector are
677 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
678 ** flag indicate that a file cannot be deleted when open. The
679 ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
680 ** read-only media and cannot be changed even by processes with
681 ** elevated privileges.
682 */
683 #define SQLITE_IOCAP_ATOMIC 0x00000001
684 #define SQLITE_IOCAP_ATOMIC512 0x00000002
685 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
686 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -688,10 +691,11 @@
691 #define SQLITE_IOCAP_ATOMIC64K 0x00000100
692 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
693 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
694 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
695 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
696 #define SQLITE_IOCAP_IMMUTABLE 0x00002000
697
698 /*
699 ** CAPI3REF: File Locking Levels
700 **
701 ** SQLite uses one of these integer values as the second
@@ -2892,10 +2896,34 @@
2896 ** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
2897 ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2898 ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2899 ** a URI filename, its value overrides any behavior requested by setting
2900 ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2901 **
2902 ** <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
2903 ** "1") or "false" (or "off" or "no" or "0") to indicate that the
2904 ** [powersafe overwrite] property does or does not apply to the
2905 ** storage media on which the database file resides. ^The psow query
2906 ** parameter only works for the built-in unix and Windows VFSes.
2907 **
2908 ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
2909 ** which if set disables file locking in rollback journal modes. This
2910 ** is useful for accessing a database on a filesystem that does not
2911 ** support locking. Caution: Database corruption might result if two
2912 ** or more processes write to the same database and any one of those
2913 ** processes uses nolock=1.
2914 **
2915 ** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
2916 ** parameter that indicates that the database file is stored on
2917 ** read-only media. ^When immutable is set, SQLite assumes that the
2918 ** database file cannot be changed, even by a process with higher
2919 ** privilege, and so the database is opened read-only and all locking
2920 ** and change detection is disabled. Caution: Setting the immutable
2921 ** property on a database file that does in fact change can result
2922 ** in incorrect query results and/or [SQLITE_CORRUPT] errors.
2923 ** See also: [SQLITE_IOCAP_IMMUTABLE].
2924 **
2925 ** </ul>
2926 **
2927 ** ^Specifying an unknown parameter in the query component of a URI is not an
2928 ** error. Future versions of SQLite might understand additional query
2929 ** parameters. See "[query parameters with special meaning to SQLite]" for
@@ -2921,12 +2949,13 @@
2949 ** in URI filenames.
2950 ** <tr><td> file:data.db?mode=ro&cache=private <td>
2951 ** Open file "data.db" in the current directory for read-only access.
2952 ** Regardless of whether or not shared-cache mode is enabled by
2953 ** default, use a private cache.
2954 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
2955 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
2956 ** that uses dot-files in place of posix advisory locking.
2957 ** <tr><td> file:data.db?mode=readonly <td>
2958 ** An error. "readonly" is not a valid option for the "mode" parameter.
2959 ** </table>
2960 **
2961 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
@@ -7460,10 +7489,20 @@
7489 #if 0
7490 extern "C" {
7491 #endif
7492
7493 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7494 typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
7495
7496 /* The double-precision datatype used by RTree depends on the
7497 ** SQLITE_RTREE_INT_ONLY compile-time option.
7498 */
7499 #ifdef SQLITE_RTREE_INT_ONLY
7500 typedef sqlite3_int64 sqlite3_rtree_dbl;
7501 #else
7502 typedef double sqlite3_rtree_dbl;
7503 #endif
7504
7505 /*
7506 ** Register a geometry callback named zGeom that can be used as part of an
7507 ** R-Tree geometry query as follows:
7508 **
@@ -7470,15 +7509,11 @@
7509 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7510 */
7511 SQLITE_API int sqlite3_rtree_geometry_callback(
7512 sqlite3 *db,
7513 const char *zGeom,
7514 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
 
 
 
 
7515 void *pContext
7516 );
7517
7518
7519 /*
@@ -7486,15 +7521,64 @@
7521 ** argument to callbacks registered using rtree_geometry_callback().
7522 */
7523 struct sqlite3_rtree_geometry {
7524 void *pContext; /* Copy of pContext passed to s_r_g_c() */
7525 int nParam; /* Size of array aParam[] */
7526 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
7527 void *pUser; /* Callback implementation user data */
7528 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
7529 };
7530
7531 /*
7532 ** Register a 2nd-generation geometry callback named zScore that can be
7533 ** used as part of an R-Tree geometry query as follows:
7534 **
7535 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
7536 */
7537 SQLITE_API int sqlite3_rtree_query_callback(
7538 sqlite3 *db,
7539 const char *zQueryFunc,
7540 int (*xQueryFunc)(sqlite3_rtree_query_info*),
7541 void *pContext,
7542 void (*xDestructor)(void*)
7543 );
7544
7545
7546 /*
7547 ** A pointer to a structure of the following type is passed as the
7548 ** argument to scored geometry callback registered using
7549 ** sqlite3_rtree_query_callback().
7550 **
7551 ** Note that the first 5 fields of this structure are identical to
7552 ** sqlite3_rtree_geometry. This structure is a subclass of
7553 ** sqlite3_rtree_geometry.
7554 */
7555 struct sqlite3_rtree_query_info {
7556 void *pContext; /* pContext from when function registered */
7557 int nParam; /* Number of function parameters */
7558 sqlite3_rtree_dbl *aParam; /* value of function parameters */
7559 void *pUser; /* callback can use this, if desired */
7560 void (*xDelUser)(void*); /* function to free pUser */
7561 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
7562 unsigned int *anQueue; /* Number of pending entries in the queue */
7563 int nCoord; /* Number of coordinates */
7564 int iLevel; /* Level of current node or entry */
7565 int mxLevel; /* The largest iLevel value in the tree */
7566 sqlite3_int64 iRowid; /* Rowid for current entry */
7567 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
7568 int eParentWithin; /* Visibility of parent node */
7569 int eWithin; /* OUT: Visiblity */
7570 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
7571 };
7572
7573 /*
7574 ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
7575 */
7576 #define NOT_WITHIN 0 /* Object completely outside of query region */
7577 #define PARTLY_WITHIN 1 /* Object partially overlaps query region */
7578 #define FULLY_WITHIN 2 /* Object fully contained within query region */
7579
7580
7581 #if 0
7582 } /* end of the 'extern "C"' block */
7583 #endif
7584
@@ -8417,14 +8501,14 @@
8501 ** Estimated quantities used for query planning are stored as 16-bit
8502 ** logarithms. For quantity X, the value stored is 10*log2(X). This
8503 ** gives a possible range of values of approximately 1.0e986 to 1e-986.
8504 ** But the allowed values are "grainy". Not every value is representable.
8505 ** For example, quantities 16 and 17 are both represented by a LogEst
8506 ** of 40. However, since LogEst quantaties are suppose to be estimates,
8507 ** not exact values, this imprecision is not a problem.
8508 **
8509 ** "LogEst" is short for "Logarithmic Estimate".
8510 **
8511 ** Examples:
8512 ** 1 -> 0 20 -> 43 10000 -> 132
8513 ** 2 -> 10 25 -> 46 25000 -> 146
8514 ** 3 -> 16 100 -> 66 1000000 -> 199
@@ -8916,10 +9000,11 @@
9000 SQLITE_PRIVATE int sqlite3BtreePutData(BtCursor*, u32 offset, u32 amt, void*);
9001 SQLITE_PRIVATE void sqlite3BtreeIncrblobCursor(BtCursor *);
9002 SQLITE_PRIVATE void sqlite3BtreeClearCursor(BtCursor *);
9003 SQLITE_PRIVATE int sqlite3BtreeSetVersion(Btree *pBt, int iVersion);
9004 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *, unsigned int mask);
9005 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *pBt);
9006
9007 #ifndef NDEBUG
9008 SQLITE_PRIVATE int sqlite3BtreeCursorIsValid(BtCursor*);
9009 #endif
9010
@@ -9870,87 +9955,75 @@
9955 */
9956 #ifndef _SQLITE_OS_H_
9957 #define _SQLITE_OS_H_
9958
9959 /*
9960 ** Attempt to automatically detect the operating system and setup the
9961 ** necessary pre-processor macros for it.
9962 */
9963 /************** Include os_setup.h in the middle of os.h *********************/
9964 /************** Begin file os_setup.h ****************************************/
9965 /*
9966 ** 2013 November 25
9967 **
9968 ** The author disclaims copyright to this source code. In place of
9969 ** a legal notice, here is a blessing:
9970 **
9971 ** May you do good and not evil.
9972 ** May you find forgiveness for yourself and forgive others.
9973 ** May you share freely, never taking more than you give.
9974 **
9975 ******************************************************************************
9976 **
9977 ** This file contains pre-processor directives related to operating system
9978 ** detection and/or setup.
9979 */
9980 #ifndef _OS_SETUP_H_
9981 #define _OS_SETUP_H_
9982
9983 /*
9984 ** Figure out if we are dealing with Unix, Windows, or some other operating
9985 ** system.
9986 **
9987 ** After the following block of preprocess macros, all of SQLITE_OS_UNIX,
9988 ** SQLITE_OS_WIN, and SQLITE_OS_OTHER will defined to either 1 or 0. One of
9989 ** the three will be 1. The other two will be 0.
9990 */
9991 #if defined(SQLITE_OS_OTHER)
9992 # if SQLITE_OS_OTHER==1
9993 # undef SQLITE_OS_UNIX
9994 # define SQLITE_OS_UNIX 0
9995 # undef SQLITE_OS_WIN
9996 # define SQLITE_OS_WIN 0
9997 # else
9998 # undef SQLITE_OS_OTHER
9999 # endif
10000 #endif
10001 #if !defined(SQLITE_OS_UNIX) && !defined(SQLITE_OS_OTHER)
10002 # define SQLITE_OS_OTHER 0
10003 # ifndef SQLITE_OS_WIN
10004 # if defined(_WIN32) || defined(WIN32) || defined(__CYGWIN__) || \
10005 defined(__MINGW32__) || defined(__BORLANDC__)
10006 # define SQLITE_OS_WIN 1
10007 # define SQLITE_OS_UNIX 0
10008 # else
10009 # define SQLITE_OS_WIN 0
10010 # define SQLITE_OS_UNIX 1
10011 # endif
10012 # else
10013 # define SQLITE_OS_UNIX 0
10014 # endif
10015 #else
10016 # ifndef SQLITE_OS_WIN
10017 # define SQLITE_OS_WIN 0
10018 # endif
10019 #endif
10020
10021 #endif /* _OS_SETUP_H_ */
10022
10023 /************** End of os_setup.h ********************************************/
10024 /************** Continuing where we left off in os.h *************************/
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10025
10026 /* If the SET_FULLSYNC macro is not defined above, then make it
10027 ** a no-op
10028 */
10029 #ifndef SET_FULLSYNC
@@ -10845,11 +10918,11 @@
10918 FKey *pFKey; /* Linked list of all foreign keys in this table */
10919 char *zColAff; /* String defining the affinity of each column */
10920 #ifndef SQLITE_OMIT_CHECK
10921 ExprList *pCheck; /* All CHECK constraints */
10922 #endif
10923 LogEst nRowLogEst; /* Estimated rows in table - from sqlite_stat1 table */
10924 int tnum; /* Root BTree node for this table (see note above) */
10925 i16 iPKey; /* If not negative, use aCol[iPKey] as the primary key */
10926 i16 nCol; /* Number of columns in this table */
10927 u16 nRef; /* Number of pointers to this Table */
10928 LogEst szTabRow; /* Estimated size of each table row in bytes */
@@ -11054,11 +11127,11 @@
11127 ** element.
11128 */
11129 struct Index {
11130 char *zName; /* Name of this index */
11131 i16 *aiColumn; /* Which columns are used by this index. 1st is 0 */
11132 LogEst *aiRowLogEst; /* From ANALYZE: Est. rows selected by each column */
11133 Table *pTable; /* The SQL table being indexed */
11134 char *zColAff; /* String defining the affinity of each column */
11135 Index *pNext; /* The next index associated with the same table */
11136 Schema *pSchema; /* Schema containing this index */
11137 u8 *aSortOrder; /* for each column: True==DESC, False==ASC */
@@ -11068,11 +11141,11 @@
11141 int tnum; /* DB Page containing root of this index */
11142 LogEst szIdxRow; /* Estimated average row size in bytes */
11143 u16 nKeyCol; /* Number of columns forming the key */
11144 u16 nColumn; /* Number of columns stored in the index */
11145 u8 onError; /* OE_Abort, OE_Ignore, OE_Replace, or OE_None */
11146 unsigned idxType:2; /* 1==UNIQUE, 2==PRIMARY KEY, 0==CREATE INDEX */
11147 unsigned bUnordered:1; /* Use this index for == or IN queries only */
11148 unsigned uniqNotNull:1; /* True if UNIQUE and NOT NULL for all columns */
11149 unsigned isResized:1; /* True if resizeIndexObject() has been called */
11150 unsigned isCovering:1; /* True if this is a covering index */
11151 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
@@ -11081,10 +11154,20 @@
11154 tRowcnt *aAvgEq; /* Average nEq values for keys not in aSample */
11155 IndexSample *aSample; /* Samples of the left-most key */
11156 #endif
11157 };
11158
11159 /*
11160 ** Allowed values for Index.idxType
11161 */
11162 #define SQLITE_IDXTYPE_APPDEF 0 /* Created using CREATE INDEX */
11163 #define SQLITE_IDXTYPE_UNIQUE 1 /* Implements a UNIQUE constraint */
11164 #define SQLITE_IDXTYPE_PRIMARYKEY 2 /* Is the PRIMARY KEY for the table */
11165
11166 /* Return true if index X is a PRIMARY KEY index */
11167 #define IsPrimaryKeyIndex(X) ((X)->idxType==SQLITE_IDXTYPE_PRIMARYKEY)
11168
11169 /*
11170 ** Each sample stored in the sqlite_stat3 table is represented in memory
11171 ** using a structure of this type. See documentation at the top of the
11172 ** analyze.c source file for additional information.
11173 */
@@ -11499,10 +11582,11 @@
11582 #define WHERE_ONETABLE_ONLY 0x0040 /* Only code the 1st table in pTabList */
11583 #define WHERE_AND_ONLY 0x0080 /* Don't use indices for OR terms */
11584 #define WHERE_GROUPBY 0x0100 /* pOrderBy is really a GROUP BY */
11585 #define WHERE_DISTINCTBY 0x0200 /* pOrderby is really a DISTINCT clause */
11586 #define WHERE_WANT_DISTINCT 0x0400 /* All output needs to be distinct */
11587 #define WHERE_SORTBYGROUP 0x0800 /* Support sqlite3WhereIsSorted() */
11588
11589 /* Allowed return values from sqlite3WhereIsDistinct()
11590 */
11591 #define WHERE_DISTINCT_NOOP 0 /* DISTINCT keyword not used */
11592 #define WHERE_DISTINCT_UNIQUE 1 /* No duplicates */
@@ -12082,15 +12166,14 @@
12166 int isInit; /* True after initialization has finished */
12167 int inProgress; /* True while initialization in progress */
12168 int isMutexInit; /* True after mutexes are initialized */
12169 int isMallocInit; /* True after malloc is initialized */
12170 int isPCacheInit; /* True after malloc is initialized */
 
12171 int nRefInitMutex; /* Number of users of pInitMutex */
12172 sqlite3_mutex *pInitMutex; /* Mutex used by sqlite3_initialize() */
12173 void (*xLog)(void*,int,const char*); /* Function for logging */
12174 void *pLogArg; /* First argument to xLog() */
 
12175 #ifdef SQLITE_ENABLE_SQLLOG
12176 void(*xSqllog)(void*,sqlite3*,const char*, int);
12177 void *pSqllogArg;
12178 #endif
12179 #ifdef SQLITE_VDBE_COVERAGE
@@ -12098,10 +12181,14 @@
12181 ** operation. Set the callback using SQLITE_TESTCTRL_VDBE_COVERAGE.
12182 */
12183 void (*xVdbeBranch)(void*,int iSrcLine,u8 eThis,u8 eMx); /* Callback */
12184 void *pVdbeBranchArg; /* 1st argument */
12185 #endif
12186 #ifndef SQLITE_OMIT_BUILTIN_TEST
12187 int (*xTestCallback)(int); /* Invoked by sqlite3FaultSim() */
12188 #endif
12189 int bLocaltimeFault; /* True to fail localtime() calls */
12190 };
12191
12192 /*
12193 ** This macro is used inside of assert() statements to indicate that
12194 ** the assert is only valid on a well-formed database. Instead of:
@@ -12398,10 +12485,16 @@
12485 SQLITE_PRIVATE void sqlite3EndTable(Parse*,Token*,Token*,u8,Select*);
12486 SQLITE_PRIVATE int sqlite3ParseUri(const char*,const char*,unsigned int*,
12487 sqlite3_vfs**,char**,char **);
12488 SQLITE_PRIVATE Btree *sqlite3DbNameToBtree(sqlite3*,const char*);
12489 SQLITE_PRIVATE int sqlite3CodeOnce(Parse *);
12490
12491 #ifdef SQLITE_OMIT_BUILTIN_TEST
12492 # define sqlite3FaultSim(X) SQLITE_OK
12493 #else
12494 SQLITE_PRIVATE int sqlite3FaultSim(int);
12495 #endif
12496
12497 SQLITE_PRIVATE Bitvec *sqlite3BitvecCreate(u32);
12498 SQLITE_PRIVATE int sqlite3BitvecTest(Bitvec*, u32);
12499 SQLITE_PRIVATE int sqlite3BitvecSet(Bitvec*, u32);
12500 SQLITE_PRIVATE void sqlite3BitvecClear(Bitvec*, u32, void*);
@@ -12466,10 +12559,11 @@
12559 SQLITE_PRIVATE WhereInfo *sqlite3WhereBegin(Parse*,SrcList*,Expr*,ExprList*,ExprList*,u16,int);
12560 SQLITE_PRIVATE void sqlite3WhereEnd(WhereInfo*);
12561 SQLITE_PRIVATE u64 sqlite3WhereOutputRowCount(WhereInfo*);
12562 SQLITE_PRIVATE int sqlite3WhereIsDistinct(WhereInfo*);
12563 SQLITE_PRIVATE int sqlite3WhereIsOrdered(WhereInfo*);
12564 SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo*);
12565 SQLITE_PRIVATE int sqlite3WhereContinueLabel(WhereInfo*);
12566 SQLITE_PRIVATE int sqlite3WhereBreakLabel(WhereInfo*);
12567 SQLITE_PRIVATE int sqlite3WhereOkOnePass(WhereInfo*, int*);
12568 SQLITE_PRIVATE int sqlite3ExprCodeGetColumn(Parse*, Table*, int, int, int, u8);
12569 SQLITE_PRIVATE void sqlite3ExprCodeGetColumnOfTable(Vdbe*, Table*, int, int, int);
@@ -13203,19 +13297,26 @@
13297 0, /* isInit */
13298 0, /* inProgress */
13299 0, /* isMutexInit */
13300 0, /* isMallocInit */
13301 0, /* isPCacheInit */
 
13302 0, /* nRefInitMutex */
13303 0, /* pInitMutex */
13304 0, /* xLog */
13305 0, /* pLogArg */
 
13306 #ifdef SQLITE_ENABLE_SQLLOG
13307 0, /* xSqllog */
13308 0, /* pSqllogArg */
13309 #endif
13310 #ifdef SQLITE_VDBE_COVERAGE
13311 0, /* xVdbeBranch */
13312 0, /* pVbeBranchArg */
13313 #endif
13314 #ifndef SQLITE_OMIT_BUILTIN_TEST
13315 0, /* xTestCallback */
13316 #endif
13317 0 /* bLocaltimeFault */
13318 };
13319
13320 /*
13321 ** Hash table for global functions - functions common to all
13322 ** database connections. After initialization, this table is
@@ -18934,10 +19035,88 @@
19035 **
19036 *************************************************************************
19037 ** This file contains the C functions that implement mutexes for win32
19038 */
19039
19040 #if SQLITE_OS_WIN
19041 /*
19042 ** Include the header file for the Windows VFS.
19043 */
19044 /************** Include os_win.h in the middle of mutex_w32.c ****************/
19045 /************** Begin file os_win.h ******************************************/
19046 /*
19047 ** 2013 November 25
19048 **
19049 ** The author disclaims copyright to this source code. In place of
19050 ** a legal notice, here is a blessing:
19051 **
19052 ** May you do good and not evil.
19053 ** May you find forgiveness for yourself and forgive others.
19054 ** May you share freely, never taking more than you give.
19055 **
19056 ******************************************************************************
19057 **
19058 ** This file contains code that is specific to Windows.
19059 */
19060 #ifndef _OS_WIN_H_
19061 #define _OS_WIN_H_
19062
19063 /*
19064 ** Include the primary Windows SDK header file.
19065 */
19066 #include "windows.h"
19067
19068 #ifdef __CYGWIN__
19069 # include <sys/cygwin.h>
19070 # include <errno.h> /* amalgamator: dontcache */
19071 #endif
19072
19073 /*
19074 ** Determine if we are dealing with Windows NT.
19075 **
19076 ** We ought to be able to determine if we are compiling for Windows 9x or
19077 ** Windows NT using the _WIN32_WINNT macro as follows:
19078 **
19079 ** #if defined(_WIN32_WINNT)
19080 ** # define SQLITE_OS_WINNT 1
19081 ** #else
19082 ** # define SQLITE_OS_WINNT 0
19083 ** #endif
19084 **
19085 ** However, Visual Studio 2005 does not set _WIN32_WINNT by default, as
19086 ** it ought to, so the above test does not work. We'll just assume that
19087 ** everything is Windows NT unless the programmer explicitly says otherwise
19088 ** by setting SQLITE_OS_WINNT to 0.
19089 */
19090 #if SQLITE_OS_WIN && !defined(SQLITE_OS_WINNT)
19091 # define SQLITE_OS_WINNT 1
19092 #endif
19093
19094 /*
19095 ** Determine if we are dealing with Windows CE - which has a much reduced
19096 ** API.
19097 */
19098 #if defined(_WIN32_WCE)
19099 # define SQLITE_OS_WINCE 1
19100 #else
19101 # define SQLITE_OS_WINCE 0
19102 #endif
19103
19104 /*
19105 ** Determine if we are dealing with WinRT, which provides only a subset of
19106 ** the full Win32 API.
19107 */
19108 #if !defined(SQLITE_OS_WINRT)
19109 # define SQLITE_OS_WINRT 0
19110 #endif
19111
19112 #endif /* _OS_WIN_H_ */
19113
19114 /************** End of os_win.h **********************************************/
19115 /************** Continuing where we left off in mutex_w32.c ******************/
19116 #endif
19117
19118 /*
19119 ** The code in this file is only used if we are compiling multithreaded
19120 ** on a win32 system.
19121 */
19122 #ifdef SQLITE_MUTEX_W32
@@ -21789,10 +21968,28 @@
21968 static unsigned dummy = 0;
21969 dummy += (unsigned)x;
21970 }
21971 #endif
21972
21973 /*
21974 ** Give a callback to the test harness that can be used to simulate faults
21975 ** in places where it is difficult or expensive to do so purely by means
21976 ** of inputs.
21977 **
21978 ** The intent of the integer argument is to let the fault simulator know
21979 ** which of multiple sqlite3FaultSim() calls has been hit.
21980 **
21981 ** Return whatever integer value the test callback returns, or return
21982 ** SQLITE_OK if no test callback is installed.
21983 */
21984 #ifndef SQLITE_OMIT_BUILTIN_TEST
21985 SQLITE_PRIVATE int sqlite3FaultSim(int iTest){
21986 int (*xCallback)(int) = sqlite3GlobalConfig.xTestCallback;
21987 return xCallback ? xCallback(iTest) : SQLITE_OK;
21988 }
21989 #endif
21990
21991 #ifndef SQLITE_OMIT_FLOATING_POINT
21992 /*
21993 ** Return true if the floating point value is Not a Number (NaN).
21994 **
21995 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN.
@@ -23004,12 +23201,12 @@
23201 return b+x[b-a];
23202 }
23203 }
23204
23205 /*
23206 ** Convert an integer into a LogEst. In other words, compute an
23207 ** approximation for 10*log2(x).
23208 */
23209 SQLITE_PRIVATE LogEst sqlite3LogEst(u64 x){
23210 static LogEst a[] = { 0, 2, 3, 5, 6, 7, 8, 9 };
23211 LogEst y = 40;
23212 if( x<8 ){
@@ -31226,15 +31423,10 @@
31423 **
31424 ** This file contains code that is specific to Windows.
31425 */
31426 #if SQLITE_OS_WIN /* This file is used for Windows only */
31427
 
 
 
 
 
31428 /*
31429 ** Include code that is common to all os_*.c files
31430 */
31431 /************** Include os_common.h in the middle of os_win.c ****************/
31432 /************** Begin file os_common.h ***************************************/
@@ -31443,10 +31635,14 @@
31635
31636 #endif /* !defined(_OS_COMMON_H_) */
31637
31638 /************** End of os_common.h *******************************************/
31639 /************** Continuing where we left off in os_win.c *********************/
31640
31641 /*
31642 ** Include the header file for the Windows VFS.
31643 */
31644
31645 /*
31646 ** Compiling and using WAL mode requires several APIs that are only
31647 ** available in Windows platforms based on the NT kernel.
31648 */
@@ -33255,10 +33451,36 @@
33451 # define SQLITE_WIN32_IOERR_RETRY_DELAY 25
33452 #endif
33453 static int winIoerrRetry = SQLITE_WIN32_IOERR_RETRY;
33454 static int winIoerrRetryDelay = SQLITE_WIN32_IOERR_RETRY_DELAY;
33455
33456 /*
33457 ** The "winIoerrCanRetry1" macro is used to determine if a particular I/O
33458 ** error code obtained via GetLastError() is eligible to be retried. It
33459 ** must accept the error code DWORD as its only argument and should return
33460 ** non-zero if the error code is transient in nature and the operation
33461 ** responsible for generating the original error might succeed upon being
33462 ** retried. The argument to this macro should be a variable.
33463 **
33464 ** Additionally, a macro named "winIoerrCanRetry2" may be defined. If it
33465 ** is defined, it will be consulted only when the macro "winIoerrCanRetry1"
33466 ** returns zero. The "winIoerrCanRetry2" macro is completely optional and
33467 ** may be used to include additional error codes in the set that should
33468 ** result in the failing I/O operation being retried by the caller. If
33469 ** defined, the "winIoerrCanRetry2" macro must exhibit external semantics
33470 ** identical to those of the "winIoerrCanRetry1" macro.
33471 */
33472 #if !defined(winIoerrCanRetry1)
33473 #define winIoerrCanRetry1(a) (((a)==ERROR_ACCESS_DENIED) || \
33474 ((a)==ERROR_SHARING_VIOLATION) || \
33475 ((a)==ERROR_LOCK_VIOLATION) || \
33476 ((a)==ERROR_DEV_NOT_EXIST) || \
33477 ((a)==ERROR_NETNAME_DELETED) || \
33478 ((a)==ERROR_SEM_TIMEOUT) || \
33479 ((a)==ERROR_NETWORK_UNREACHABLE))
33480 #endif
33481
33482 /*
33483 ** If a ReadFile() or WriteFile() error occurs, invoke this routine
33484 ** to see if it should be retried. Return TRUE to retry. Return FALSE
33485 ** to give up with an error.
33486 */
@@ -33268,17 +33490,22 @@
33490 if( pError ){
33491 *pError = e;
33492 }
33493 return 0;
33494 }
33495 if( winIoerrCanRetry1(e) ){
33496 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
33497 ++*pnRetry;
33498 return 1;
33499 }
33500 #if defined(winIoerrCanRetry2)
33501 else if( winIoerrCanRetry2(e) ){
33502 sqlite3_win32_sleep(winIoerrRetryDelay*(1+*pnRetry));
33503 ++*pnRetry;
33504 return 1;
33505 }
33506 #endif
33507 if( pError ){
33508 *pError = e;
33509 }
33510 return 0;
33511 }
@@ -34213,11 +34440,11 @@
34440 #endif
34441 if( res == 0 ){
34442 pFile->lastErrno = osGetLastError();
34443 /* No need to log a failure to lock */
34444 }
34445 OSTRACE(("READ-LOCK file=%p, result=%d\n", pFile->h, res));
34446 return res;
34447 }
34448
34449 /*
34450 ** Undo a readlock
@@ -34237,11 +34464,11 @@
34464 if( res==0 && ((lastErrno = osGetLastError())!=ERROR_NOT_LOCKED) ){
34465 pFile->lastErrno = lastErrno;
34466 winLogError(SQLITE_IOERR_UNLOCK, pFile->lastErrno,
34467 "winUnlockReadLock", pFile->zPath);
34468 }
34469 OSTRACE(("READ-UNLOCK file=%p, result=%d\n", pFile->h, res));
34470 return res;
34471 }
34472
34473 /*
34474 ** Lock the file with the lock specified by parameter locktype - one
@@ -34312,12 +34539,12 @@
34539 ** around problems caused by indexing and/or anti-virus software on
34540 ** Windows systems.
34541 ** If you are using this code as a model for alternative VFSes, do not
34542 ** copy this retry logic. It is a hack intended for Windows only.
34543 */
34544 OSTRACE(("LOCK-PENDING-FAIL file=%p, count=%d, result=%d\n",
34545 pFile->h, cnt, res));
34546 if( cnt ) sqlite3_win32_sleep(1);
34547 }
34548 gotPendingLock = res;
34549 if( !res ){
34550 lastErrno = osGetLastError();
@@ -34398,29 +34625,29 @@
34625 ** This routine checks if there is a RESERVED lock held on the specified
34626 ** file by this or any other process. If such a lock is held, return
34627 ** non-zero, otherwise zero.
34628 */
34629 static int winCheckReservedLock(sqlite3_file *id, int *pResOut){
34630 int res;
34631 winFile *pFile = (winFile*)id;
34632
34633 SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; );
34634 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p\n", pFile->h, pResOut));
34635
34636 assert( id!=0 );
34637 if( pFile->locktype>=RESERVED_LOCK ){
34638 res = 1;
34639 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (local)\n", pFile->h, res));
34640 }else{
34641 res = winLockFile(&pFile->h, SQLITE_LOCKFILEEX_FLAGS,RESERVED_BYTE, 0, 1, 0);
34642 if( res ){
34643 winUnlockFile(&pFile->h, RESERVED_BYTE, 0, 1, 0);
34644 }
34645 res = !res;
34646 OSTRACE(("TEST-WR-LOCK file=%p, result=%d (remote)\n", pFile->h, res));
34647 }
34648 *pResOut = res;
34649 OSTRACE(("TEST-WR-LOCK file=%p, pResOut=%p, *pResOut=%d, rc=SQLITE_OK\n",
34650 pFile->h, pResOut, *pResOut));
34651 return SQLITE_OK;
34652 }
34653
@@ -40231,11 +40458,12 @@
40458 u8 noSync; /* Do not sync the journal if true */
40459 u8 fullSync; /* Do extra syncs of the journal for robustness */
40460 u8 ckptSyncFlags; /* SYNC_NORMAL or SYNC_FULL for checkpoint */
40461 u8 walSyncFlags; /* SYNC_NORMAL or SYNC_FULL for wal writes */
40462 u8 syncFlags; /* SYNC_NORMAL or SYNC_FULL otherwise */
40463 u8 tempFile; /* zFilename is a temporary or immutable file */
40464 u8 noLock; /* Do not lock (except in WAL mode) */
40465 u8 readOnly; /* True for a read-only database */
40466 u8 memDb; /* True to inhibit all file I/O */
40467
40468 /**************************************************************************
40469 ** The following block contains those class members that change during
@@ -40696,11 +40924,11 @@
40924 assert( !pPager->exclusiveMode || pPager->eLock==eLock );
40925 assert( eLock==NO_LOCK || eLock==SHARED_LOCK );
40926 assert( eLock!=NO_LOCK || pagerUseWal(pPager)==0 );
40927 if( isOpen(pPager->fd) ){
40928 assert( pPager->eLock>=eLock );
40929 rc = pPager->noLock ? SQLITE_OK : sqlite3OsUnlock(pPager->fd, eLock);
40930 if( pPager->eLock!=UNKNOWN_LOCK ){
40931 pPager->eLock = (u8)eLock;
40932 }
40933 IOTRACE(("UNLOCK %p %d\n", pPager, eLock))
40934 }
@@ -40720,11 +40948,11 @@
40948 static int pagerLockDb(Pager *pPager, int eLock){
40949 int rc = SQLITE_OK;
40950
40951 assert( eLock==SHARED_LOCK || eLock==RESERVED_LOCK || eLock==EXCLUSIVE_LOCK );
40952 if( pPager->eLock<eLock || pPager->eLock==UNKNOWN_LOCK ){
40953 rc = pPager->noLock ? SQLITE_OK : sqlite3OsLock(pPager->fd, eLock);
40954 if( rc==SQLITE_OK && (pPager->eLock!=UNKNOWN_LOCK||eLock==EXCLUSIVE_LOCK) ){
40955 pPager->eLock = (u8)eLock;
40956 IOTRACE(("LOCK %p %d\n", pPager, eLock))
40957 }
40958 }
@@ -44279,47 +44507,59 @@
44507 **
44508 ** + SQLITE_DEFAULT_PAGE_SIZE,
44509 ** + The value returned by sqlite3OsSectorSize()
44510 ** + The largest page size that can be written atomically.
44511 */
44512 if( rc==SQLITE_OK ){
44513 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
44514 if( !readOnly ){
44515 setSectorSize(pPager);
44516 assert(SQLITE_DEFAULT_PAGE_SIZE<=SQLITE_MAX_DEFAULT_PAGE_SIZE);
44517 if( szPageDflt<pPager->sectorSize ){
44518 if( pPager->sectorSize>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
44519 szPageDflt = SQLITE_MAX_DEFAULT_PAGE_SIZE;
44520 }else{
44521 szPageDflt = (u32)pPager->sectorSize;
44522 }
44523 }
44524 #ifdef SQLITE_ENABLE_ATOMIC_WRITE
44525 {
44526 int ii;
44527 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
44528 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
44529 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
44530 for(ii=szPageDflt; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
44531 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ){
44532 szPageDflt = ii;
44533 }
44534 }
44535 }
44536 #endif
44537 }
44538 pPager->noLock = sqlite3_uri_boolean(zFilename, "nolock", 0);
44539 if( (iDc & SQLITE_IOCAP_IMMUTABLE)!=0
44540 || sqlite3_uri_boolean(zFilename, "immutable", 0) ){
44541 vfsFlags |= SQLITE_OPEN_READONLY;
44542 goto act_like_temp_file;
44543 }
44544 }
44545 }else{
44546 /* If a temporary file is requested, it is not opened immediately.
44547 ** In this case we accept the default page size and delay actually
44548 ** opening the file until the first call to OsWrite().
44549 **
44550 ** This branch is also run for an in-memory database. An in-memory
44551 ** database is the same as a temp-file that is never written out to
44552 ** disk and uses an in-memory rollback journal.
44553 **
44554 ** This branch also runs for files marked as immutable.
44555 */
44556 act_like_temp_file:
44557 tempFile = 1;
44558 pPager->eState = PAGER_READER; /* Pretend we already have a lock */
44559 pPager->eLock = EXCLUSIVE_LOCK; /* Pretend we are in EXCLUSIVE locking mode */
44560 pPager->noLock = 1; /* Do no locking */
44561 readOnly = (vfsFlags&SQLITE_OPEN_READONLY);
44562 }
44563
44564 /* The following call to PagerSetPagesize() serves to set the value of
44565 ** Pager.pageSize and to allocate the Pager.pTmpSpace buffer.
@@ -44356,13 +44596,10 @@
44596 /* pPager->stmtSize = 0; */
44597 /* pPager->stmtJSize = 0; */
44598 /* pPager->nPage = 0; */
44599 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
44600 /* pPager->state = PAGER_UNLOCK; */
 
 
 
44601 /* pPager->errMask = 0; */
44602 pPager->tempFile = (u8)tempFile;
44603 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
44604 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
44605 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
@@ -55106,16 +55343,10 @@
55343 assert( pCur->eState==CURSOR_VALID );
55344 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) );
55345 assert( cursorHoldsMutex(pCur) );
55346 assert( pCur->aiIdx[pCur->iPage]<pCur->apPage[pCur->iPage]->nCell );
55347 assert( pCur->info.nSize>0 );
 
 
 
 
 
 
55348 *pAmt = pCur->info.nLocal;
55349 return (void*)(pCur->info.pCell + pCur->info.nHeader);
55350 }
55351
55352
@@ -59414,10 +59645,17 @@
59645 */
59646 SQLITE_PRIVATE void sqlite3BtreeCursorHints(BtCursor *pCsr, unsigned int mask){
59647 assert( mask==BTREE_BULKLOAD || mask==0 );
59648 pCsr->hints = mask;
59649 }
59650
59651 /*
59652 ** Return true if the given Btree is read-only.
59653 */
59654 SQLITE_PRIVATE int sqlite3BtreeIsReadonly(Btree *p){
59655 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0;
59656 }
59657
59658 /************** End of btree.c ***********************************************/
59659 /************** Begin file backup.c ******************************************/
59660 /*
59661 ** 2009 January 28
@@ -70686,11 +70924,11 @@
70924 **
70925 ** Reposition cursor P1 so that it points to the smallest entry that
70926 ** is greater than or equal to the key value. If there are no records
70927 ** greater than or equal to the key and P2 is not zero, then jump to P2.
70928 **
70929 ** See also: Found, NotFound, SeekLt, SeekGt, SeekLe
70930 */
70931 /* Opcode: SeekGt P1 P2 P3 P4 *
70932 ** Synopsis: key=r[P3@P4]
70933 **
70934 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70700,11 +70938,11 @@
70938 **
70939 ** Reposition cursor P1 so that it points to the smallest entry that
70940 ** is greater than the key value. If there are no records greater than
70941 ** the key and P2 is not zero, then jump to P2.
70942 **
70943 ** See also: Found, NotFound, SeekLt, SeekGe, SeekLe
70944 */
70945 /* Opcode: SeekLt P1 P2 P3 P4 *
70946 ** Synopsis: key=r[P3@P4]
70947 **
70948 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70714,11 +70952,11 @@
70952 **
70953 ** Reposition cursor P1 so that it points to the largest entry that
70954 ** is less than the key value. If there are no records less than
70955 ** the key and P2 is not zero, then jump to P2.
70956 **
70957 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLe
70958 */
70959 /* Opcode: SeekLe P1 P2 P3 P4 *
70960 ** Synopsis: key=r[P3@P4]
70961 **
70962 ** If cursor P1 refers to an SQL table (B-Tree that uses integer keys),
@@ -70728,11 +70966,11 @@
70966 **
70967 ** Reposition cursor P1 so that it points to the largest entry that
70968 ** is less than or equal to the key value. If there are no records
70969 ** less than or equal to the key and P2 is not zero, then jump to P2.
70970 **
70971 ** See also: Found, NotFound, SeekGt, SeekGe, SeekLt
70972 */
70973 case OP_SeekLT: /* jump, in3 */
70974 case OP_SeekLE: /* jump, in3 */
70975 case OP_SeekGE: /* jump, in3 */
70976 case OP_SeekGT: { /* jump, in3 */
@@ -71454,10 +71692,11 @@
71692
71693 pOut = &aMem[pOp->p2];
71694 pC = p->apCsr[pOp->p1];
71695 assert( isSorter(pC) );
71696 rc = sqlite3VdbeSorterRowkey(pC, pOut);
71697 assert( rc!=SQLITE_OK || (pOut->flags & MEM_Blob) );
71698 break;
71699 }
71700
71701 /* Opcode: RowData P1 P2 * * *
71702 ** Synopsis: r[P2]=data
@@ -73502,11 +73741,11 @@
73741 #ifdef SQLITE_USE_FCNTL_TRACE
73742 zTrace = (pOp->p4.z ? pOp->p4.z : p->zSql);
73743 if( zTrace ){
73744 int i;
73745 for(i=0; i<db->nDb; i++){
73746 if( (MASKBIT(i) & p->btreeMask)==0 ) continue;
73747 sqlite3_file_control(db, db->aDb[i].zName, SQLITE_FCNTL_TRACE, zTrace);
73748 }
73749 }
73750 #endif /* SQLITE_USE_FCNTL_TRACE */
73751 #ifdef SQLITE_DEBUG
@@ -73545,12 +73784,12 @@
73784 *****************************************************************************/
73785 }
73786
73787 #ifdef VDBE_PROFILE
73788 {
73789 u64 endTime = sqlite3Hwtime();
73790 if( endTime>start ) pOp->cycles += endTime - start;
73791 pOp->cnt++;
73792 }
73793 #endif
73794
73795 /* The following code adds nothing to the actual functionality
@@ -74457,11 +74696,10 @@
74696 nRead = (int)(pSorter->iWriteOff - iStart);
74697 }
74698 rc = sqlite3OsRead(
74699 pSorter->pTemp1, &pIter->aBuffer[iBuf], nRead, iStart
74700 );
 
74701 }
74702
74703 if( rc==SQLITE_OK ){
74704 u64 nByte; /* Size of PMA in bytes */
74705 pIter->iEof = pSorter->iWriteOff;
@@ -83420,11 +83658,11 @@
83658 nCol = pIdx->nKeyCol;
83659 aGotoChng = sqlite3DbMallocRaw(db, sizeof(int)*(nCol+1));
83660 if( aGotoChng==0 ) continue;
83661
83662 /* Populate the register containing the index name. */
83663 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
83664 zIdxName = pTab->zName;
83665 }else{
83666 zIdxName = pIdx->zName;
83667 }
83668 sqlite3VdbeAddOp4(v, OP_String8, 0, regIdxname, 0, zIdxName, 0);
@@ -83789,10 +84027,11 @@
84027 */
84028 static void decodeIntArray(
84029 char *zIntArray, /* String containing int array to decode */
84030 int nOut, /* Number of slots in aOut[] */
84031 tRowcnt *aOut, /* Store integers here */
84032 LogEst *aLog, /* Or, if aOut==0, here */
84033 Index *pIndex /* Handle extra flags for this index, if not NULL */
84034 ){
84035 char *z = zIntArray;
84036 int c;
84037 int i;
@@ -83807,11 +84046,21 @@
84046 v = 0;
84047 while( (c=z[0])>='0' && c<='9' ){
84048 v = v*10 + c - '0';
84049 z++;
84050 }
84051 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
84052 if( aOut ){
84053 aOut[i] = v;
84054 }else
84055 #else
84056 assert( aOut==0 );
84057 UNUSED_PARAMETER(aOut);
84058 #endif
84059 {
84060 aLog[i] = sqlite3LogEst(v);
84061 }
84062 if( *z==' ' ) z++;
84063 }
84064 #ifndef SQLITE_ENABLE_STAT3_OR_STAT4
84065 assert( pIndex!=0 );
84066 #else
@@ -83863,16 +84112,16 @@
84112 pIndex = sqlite3FindIndex(pInfo->db, argv[1], pInfo->zDatabase);
84113 }
84114 z = argv[2];
84115
84116 if( pIndex ){
84117 decodeIntArray((char*)z, pIndex->nKeyCol+1, 0, pIndex->aiRowLogEst, pIndex);
84118 if( pIndex->pPartIdxWhere==0 ) pTable->nRowLogEst = pIndex->aiRowLogEst[0];
84119 }else{
84120 Index fakeIdx;
84121 fakeIdx.szIdxRow = pTable->szTabRow;
84122 decodeIntArray((char*)z, 1, 0, &pTable->nRowLogEst, &fakeIdx);
84123 pTable->szTabRow = fakeIdx.szIdxRow;
84124 }
84125
84126 return 0;
84127 }
@@ -84060,13 +84309,13 @@
84309 if( pIdx!=pPrevIdx ){
84310 initAvgEq(pPrevIdx);
84311 pPrevIdx = pIdx;
84312 }
84313 pSample = &pIdx->aSample[pIdx->nSample];
84314 decodeIntArray((char*)sqlite3_column_text(pStmt,1),nCol,pSample->anEq,0,0);
84315 decodeIntArray((char*)sqlite3_column_text(pStmt,2),nCol,pSample->anLt,0,0);
84316 decodeIntArray((char*)sqlite3_column_text(pStmt,3),nCol,pSample->anDLt,0,0);
84317
84318 /* Take a copy of the sample. Add two 0x00 bytes the end of the buffer.
84319 ** This is in case the sample record is corrupted. In that case, the
84320 ** sqlite3VdbeRecordCompare() may read up to two varints past the
84321 ** end of the allocated buffer before it realizes it is dealing with
@@ -85776,11 +86025,11 @@
86025 /*
86026 ** Return the PRIMARY KEY index of a table
86027 */
86028 SQLITE_PRIVATE Index *sqlite3PrimaryKeyIndex(Table *pTab){
86029 Index *p;
86030 for(p=pTab->pIndex; p && !IsPrimaryKeyIndex(p); p=p->pNext){}
86031 return p;
86032 }
86033
86034 /*
86035 ** Return the column of index pIdx that corresponds to table
@@ -85924,11 +86173,11 @@
86173 }
86174 pTable->zName = zName;
86175 pTable->iPKey = -1;
86176 pTable->pSchema = db->aDb[iDb].pSchema;
86177 pTable->nRef = 1;
86178 pTable->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
86179 assert( pParse->pNewTable==0 );
86180 pParse->pNewTable = pTable;
86181
86182 /* If this is the magic sqlite_sequence table used by autoincrement,
86183 ** then record a pointer to this table in the main database structure
@@ -86305,11 +86554,11 @@
86554 Index *p;
86555 if( v ) pParse->addrSkipPK = sqlite3VdbeAddOp0(v, OP_Noop);
86556 p = sqlite3CreateIndex(pParse, 0, 0, 0, pList, onError, 0,
86557 0, sortOrder, 0);
86558 if( p ){
86559 p->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
86560 if( v ) sqlite3VdbeJumpHere(v, pParse->addrSkipPK);
86561 }
86562 pList = 0;
86563 }
86564
@@ -86325,11 +86574,14 @@
86574 Parse *pParse, /* Parsing context */
86575 Expr *pCheckExpr /* The check expression */
86576 ){
86577 #ifndef SQLITE_OMIT_CHECK
86578 Table *pTab = pParse->pNewTable;
86579 sqlite3 *db = pParse->db;
86580 if( pTab && !IN_DECLARE_VTAB
86581 && !sqlite3BtreeIsReadonly(db->aDb[db->init.iDb].pBt)
86582 ){
86583 pTab->pCheck = sqlite3ExprListAppend(pParse, pTab->pCheck, pCheckExpr);
86584 if( pParse->constraintName.n ){
86585 sqlite3ExprListSetName(pParse, pTab->pCheck, &pParse->constraintName, 1);
86586 }
86587 }else
@@ -86677,11 +86929,11 @@
86929 pTab->aCol[pTab->iPKey].zName);
86930 pList->a[0].sortOrder = pParse->iPkSortOrder;
86931 assert( pParse->pNewTable==pTab );
86932 pPk = sqlite3CreateIndex(pParse, 0, 0, 0, pList, pTab->keyConf, 0, 0, 0, 0);
86933 if( pPk==0 ) return;
86934 pPk->idxType = SQLITE_IDXTYPE_PRIMARYKEY;
86935 pTab->iPKey = -1;
86936 }else{
86937 pPk = sqlite3PrimaryKeyIndex(pTab);
86938 }
86939 pPk->isCovering = 1;
@@ -86700,11 +86952,11 @@
86952 /* Update the in-memory representation of all UNIQUE indices by converting
86953 ** the final rowid column into one or more columns of the PRIMARY KEY.
86954 */
86955 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
86956 int n;
86957 if( IsPrimaryKeyIndex(pIdx) ) continue;
86958 for(i=n=0; i<nPk; i++){
86959 if( !hasColumn(pIdx->aiColumn, pIdx->nKeyCol, pPk->aiColumn[i]) ) n++;
86960 }
86961 if( n==0 ){
86962 /* This index is a superset of the primary key */
@@ -87749,19 +88001,19 @@
88001 Index *p; /* Allocated index object */
88002 int nByte; /* Bytes of space for Index object + arrays */
88003
88004 nByte = ROUND8(sizeof(Index)) + /* Index structure */
88005 ROUND8(sizeof(char*)*nCol) + /* Index.azColl */
88006 ROUND8(sizeof(LogEst)*(nCol+1) + /* Index.aiRowLogEst */
88007 sizeof(i16)*nCol + /* Index.aiColumn */
88008 sizeof(u8)*nCol); /* Index.aSortOrder */
88009 p = sqlite3DbMallocZero(db, nByte + nExtra);
88010 if( p ){
88011 char *pExtra = ((char*)p)+ROUND8(sizeof(Index));
88012 p->azColl = (char**)pExtra; pExtra += ROUND8(sizeof(char*)*nCol);
88013 p->aiRowLogEst = (LogEst*)pExtra; pExtra += sizeof(LogEst)*(nCol+1);
88014 p->aiColumn = (i16*)pExtra; pExtra += sizeof(i16)*nCol;
88015 p->aSortOrder = (u8*)pExtra;
88016 p->nColumn = nCol;
88017 p->nKeyCol = nCol - 1;
88018 *ppExtra = ((char*)p) + nByte;
88019 }
@@ -87780,11 +88032,11 @@
88032 ** is a primary key or unique-constraint on the most recent column added
88033 ** to the table currently under construction.
88034 **
88035 ** If the index is created successfully, return a pointer to the new Index
88036 ** structure. This is used by sqlite3AddPrimaryKey() to mark the index
88037 ** as the tables primary key (Index.idxType==SQLITE_IDXTYPE_PRIMARYKEY)
88038 */
88039 SQLITE_PRIVATE Index *sqlite3CreateIndex(
88040 Parse *pParse, /* All information about this parse */
88041 Token *pName1, /* First part of index name. May be NULL */
88042 Token *pName2, /* Second part of index name. May be NULL */
@@ -87987,19 +88239,19 @@
88239 pIndex = sqlite3AllocateIndexObject(db, pList->nExpr + nExtraCol,
88240 nName + nExtra + 1, &zExtra);
88241 if( db->mallocFailed ){
88242 goto exit_create_index;
88243 }
88244 assert( EIGHT_BYTE_ALIGNMENT(pIndex->aiRowLogEst) );
88245 assert( EIGHT_BYTE_ALIGNMENT(pIndex->azColl) );
88246 pIndex->zName = zExtra;
88247 zExtra += nName + 1;
88248 memcpy(pIndex->zName, zName, nName+1);
88249 pIndex->pTable = pTab;
88250 pIndex->onError = (u8)onError;
88251 pIndex->uniqNotNull = onError!=OE_None;
88252 pIndex->idxType = pName ? SQLITE_IDXTYPE_APPDEF : SQLITE_IDXTYPE_UNIQUE;
88253 pIndex->pSchema = db->aDb[iDb].pSchema;
88254 pIndex->nKeyCol = pList->nExpr;
88255 if( pPIWhere ){
88256 sqlite3ResolveSelfReference(pParse, pTab, NC_PartIdx, pPIWhere, 0);
88257 pIndex->pPartIdxWhere = pPIWhere;
@@ -88107,11 +88359,11 @@
88359 */
88360 Index *pIdx;
88361 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
88362 int k;
88363 assert( pIdx->onError!=OE_None );
88364 assert( pIdx->idxType!=SQLITE_IDXTYPE_APPDEF );
88365 assert( pIndex->onError!=OE_None );
88366
88367 if( pIdx->nKeyCol!=pIndex->nKeyCol ) continue;
88368 for(k=0; k<pIdx->nKeyCol; k++){
88369 const char *z1;
@@ -88268,11 +88520,11 @@
88520 **
88521 ** aiRowEst[0] is suppose to contain the number of elements in the index.
88522 ** Since we do not know, guess 1 million. aiRowEst[1] is an estimate of the
88523 ** number of rows in the table that match any particular value of the
88524 ** first column of the index. aiRowEst[2] is an estimate of the number
88525 ** of rows that match any particular combination of the first 2 columns
88526 ** of the index. And so forth. It must always be the case that
88527 *
88528 ** aiRowEst[N]<=aiRowEst[N-1]
88529 ** aiRowEst[N]>=1
88530 **
@@ -88279,24 +88531,31 @@
88531 ** Apart from that, we have little to go on besides intuition as to
88532 ** how aiRowEst[] should be initialized. The numbers generated here
88533 ** are based on typical values found in actual indices.
88534 */
88535 SQLITE_PRIVATE void sqlite3DefaultRowEst(Index *pIdx){
88536 /* 10, 9, 8, 7, 6 */
88537 LogEst aVal[] = { 33, 32, 30, 28, 26 };
88538 LogEst *a = pIdx->aiRowLogEst;
88539 int nCopy = MIN(ArraySize(aVal), pIdx->nKeyCol);
88540 int i;
88541
88542 /* Set the first entry (number of rows in the index) to the estimated
88543 ** number of rows in the table. Or 10, if the estimated number of rows
88544 ** in the table is less than that. */
88545 a[0] = pIdx->pTable->nRowLogEst;
88546 if( a[0]<33 ) a[0] = 33; assert( 33==sqlite3LogEst(10) );
88547
88548 /* Estimate that a[1] is 10, a[2] is 9, a[3] is 8, a[4] is 7, a[5] is
88549 ** 6 and each subsequent value (if any) is 5. */
88550 memcpy(&a[1], aVal, nCopy*sizeof(LogEst));
88551 for(i=nCopy+1; i<=pIdx->nKeyCol; i++){
88552 a[i] = 23; assert( 23==sqlite3LogEst(5) );
88553 }
88554
88555 assert( 0==sqlite3LogEst(1) );
88556 if( pIdx->onError!=OE_None ) a[pIdx->nKeyCol] = 0;
88557 }
88558
88559 /*
88560 ** This routine will drop an existing named index. This routine
88561 ** implements the DROP INDEX statement.
@@ -88323,11 +88582,11 @@
88582 sqlite3CodeVerifyNamedSchema(pParse, pName->a[0].zDatabase);
88583 }
88584 pParse->checkSchema = 1;
88585 goto exit_drop_index;
88586 }
88587 if( pIndex->idxType!=SQLITE_IDXTYPE_APPDEF ){
88588 sqlite3ErrorMsg(pParse, "index associated with UNIQUE "
88589 "or PRIMARY KEY constraint cannot be dropped", 0);
88590 goto exit_drop_index;
88591 }
88592 iDb = sqlite3SchemaToIndex(db, pIndex->pSchema);
@@ -88982,11 +89241,12 @@
89241 sqlite3StrAccumAppend(&errMsg, ".", 1);
89242 sqlite3StrAccumAppendAll(&errMsg, zCol);
89243 }
89244 zErr = sqlite3StrAccumFinish(&errMsg);
89245 sqlite3HaltConstraint(pParse,
89246 IsPrimaryKeyIndex(pIdx) ? SQLITE_CONSTRAINT_PRIMARYKEY
89247 : SQLITE_CONSTRAINT_UNIQUE,
89248 onError, zErr, P4_DYNAMIC, P5_ConstraintUnique);
89249 }
89250
89251
89252 /*
@@ -92116,11 +92376,11 @@
92376 }
92377 if( nSep ) sqlite3StrAccumAppend(pAccum, zSep, nSep);
92378 }
92379 zVal = (char*)sqlite3_value_text(argv[0]);
92380 nVal = sqlite3_value_bytes(argv[0]);
92381 if( zVal ) sqlite3StrAccumAppend(pAccum, zVal, nVal);
92382 }
92383 }
92384 static void groupConcatFinalize(sqlite3_context *context){
92385 StrAccum *pAccum;
92386 pAccum = sqlite3_aggregate_context(context, 0);
@@ -92560,12 +92820,12 @@
92820 ** column of pFKey, then this index is a winner. */
92821
92822 if( zKey==0 ){
92823 /* If zKey is NULL, then this foreign key is implicitly mapped to
92824 ** the PRIMARY KEY of table pParent. The PRIMARY KEY index may be
92825 ** identified by the test. */
92826 if( IsPrimaryKeyIndex(pIdx) ){
92827 if( aiCol ){
92828 int i;
92829 for(i=0; i<nCol; i++) aiCol[i] = pFKey->aCol[i].iFrom;
92830 }
92831 break;
@@ -94306,10 +94566,11 @@
94566 }
94567 }
94568 if( j>=pTab->nCol ){
94569 if( sqlite3IsRowid(pColumn->a[i].zName) && !withoutRowid ){
94570 ipkColumn = i;
94571 bIdListInOrder = 0;
94572 }else{
94573 sqlite3ErrorMsg(pParse, "table %S has no column named %s",
94574 pTabList, 0, pColumn->a[i].zName);
94575 pParse->checkSchema = 1;
94576 goto insert_cleanup;
@@ -95154,11 +95415,11 @@
95415 ** For a UNIQUE index, only conflict if the PRIMARY KEY values
95416 ** of the matched index row are different from the original PRIMARY
95417 ** KEY values of this row before the update. */
95418 int addrJump = sqlite3VdbeCurrentAddr(v)+pPk->nKeyCol;
95419 int op = OP_Ne;
95420 int regCmp = (IsPrimaryKeyIndex(pIdx) ? regIdx : regR);
95421
95422 for(i=0; i<pPk->nKeyCol; i++){
95423 char *p4 = (char*)sqlite3LocateCollSeq(pParse, pPk->azColl[i]);
95424 x = pPk->aiColumn[i];
95425 if( i==(pPk->nKeyCol-1) ){
@@ -95255,11 +95516,11 @@
95516 VdbeCoverage(v);
95517 }
95518 sqlite3VdbeAddOp2(v, OP_IdxInsert, iIdxCur+i, aRegIdx[i]);
95519 pik_flags = 0;
95520 if( useSeekResult ) pik_flags = OPFLAG_USESEEKRESULT;
95521 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) ){
95522 assert( pParse->nested==0 );
95523 pik_flags |= OPFLAG_NCHANGE;
95524 }
95525 if( pik_flags ) sqlite3VdbeChangeP5(v, pik_flags);
95526 }
@@ -95341,11 +95602,11 @@
95602 }
95603 if( piIdxCur ) *piIdxCur = iBase;
95604 for(i=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, i++){
95605 int iIdxCur = iBase++;
95606 assert( pIdx->pSchema==pTab->pSchema );
95607 if( IsPrimaryKeyIndex(pIdx) && !HasRowid(pTab) && piDataCur ){
95608 *piDataCur = iIdxCur;
95609 }
95610 if( aToOpen==0 || aToOpen[i+1] ){
95611 sqlite3VdbeAddOp3(v, op, iIdxCur, pIdx->tnum, iDb);
95612 sqlite3VdbeSetP4KeyInfo(pParse, pIdx);
@@ -95557,18 +95818,27 @@
95818 }
95819 if( pDest->iPKey!=pSrc->iPKey ){
95820 return 0; /* Both tables must have the same INTEGER PRIMARY KEY */
95821 }
95822 for(i=0; i<pDest->nCol; i++){
95823 Column *pDestCol = &pDest->aCol[i];
95824 Column *pSrcCol = &pSrc->aCol[i];
95825 if( pDestCol->affinity!=pSrcCol->affinity ){
95826 return 0; /* Affinity must be the same on all columns */
95827 }
95828 if( !xferCompatibleCollation(pDestCol->zColl, pSrcCol->zColl) ){
95829 return 0; /* Collating sequence must be the same on all columns */
95830 }
95831 if( pDestCol->notNull && !pSrcCol->notNull ){
95832 return 0; /* tab2 must be NOT NULL if tab1 is */
95833 }
95834 /* Default values for second and subsequent columns need to match. */
95835 if( i>0
95836 && ((pDestCol->zDflt==0)!=(pSrcCol->zDflt==0)
95837 || (pDestCol->zDflt && strcmp(pDestCol->zDflt, pSrcCol->zDflt)!=0))
95838 ){
95839 return 0; /* Default values must be the same for all columns */
95840 }
95841 }
95842 for(pDestIdx=pDest->pIndex; pDestIdx; pDestIdx=pDestIdx->pNext){
95843 if( pDestIdx->onError!=OE_None ){
95844 destHasUniqueIdx = 1;
@@ -98583,17 +98853,19 @@
98853 Table *pTab = sqliteHashData(i);
98854 sqlite3VdbeAddOp4(v, OP_String8, 0, 1, 0, pTab->zName, 0);
98855 sqlite3VdbeAddOp2(v, OP_Null, 0, 2);
98856 sqlite3VdbeAddOp2(v, OP_Integer,
98857 (int)sqlite3LogEstToInt(pTab->szTabRow), 3);
98858 sqlite3VdbeAddOp2(v, OP_Integer,
98859 (int)sqlite3LogEstToInt(pTab->nRowLogEst), 4);
98860 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
98861 for(pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext){
98862 sqlite3VdbeAddOp4(v, OP_String8, 0, 2, 0, pIdx->zName, 0);
98863 sqlite3VdbeAddOp2(v, OP_Integer,
98864 (int)sqlite3LogEstToInt(pIdx->szIdxRow), 3);
98865 sqlite3VdbeAddOp2(v, OP_Integer,
98866 (int)sqlite3LogEstToInt(pIdx->aiRowLogEst[0]), 4);
98867 sqlite3VdbeAddOp2(v, OP_ResultRow, 1, 4);
98868 }
98869 }
98870 }
98871 break;
@@ -100760,19 +101032,21 @@
101032 Select *pSelect, /* The whole SELECT statement */
101033 int regData /* Register holding data to be sorted */
101034 ){
101035 Vdbe *v = pParse->pVdbe;
101036 int nExpr = pSort->pOrderBy->nExpr;
101037 int regRecord = ++pParse->nMem;
101038 int regBase = pParse->nMem+1;
101039 int nOBSat = pSort->nOBSat;
101040 int op;
101041
101042 pParse->nMem += nExpr+2; /* nExpr+2 registers allocated at regBase */
101043 sqlite3ExprCacheClear(pParse);
101044 sqlite3ExprCodeExprList(pParse, pSort->pOrderBy, regBase, 0);
101045 sqlite3VdbeAddOp2(v, OP_Sequence, pSort->iECursor, regBase+nExpr);
101046 sqlite3ExprCodeMove(pParse, regData, regBase+nExpr+1, 1);
101047 sqlite3VdbeAddOp3(v, OP_MakeRecord, regBase+nOBSat, nExpr+2-nOBSat,regRecord);
101048 if( nOBSat>0 ){
101049 int regPrevKey; /* The first nOBSat columns of the previous row */
101050 int addrFirst; /* Address of the OP_IfNot opcode */
101051 int addrJmp; /* Address of the OP_Jump opcode */
101052 VdbeOp *pOp; /* Opcode that opens the sorter */
@@ -100805,14 +101079,10 @@
101079 op = OP_SorterInsert;
101080 }else{
101081 op = OP_IdxInsert;
101082 }
101083 sqlite3VdbeAddOp2(v, op, pSort->iECursor, regRecord);
 
 
 
 
101084 if( pSelect->iLimit ){
101085 int addr1, addr2;
101086 int iLimit;
101087 if( pSelect->iOffset ){
101088 iLimit = pSelect->iOffset+1;
@@ -101984,11 +102254,11 @@
102254 /* The sqlite3ResultSetOfSelect() is only used n contexts where lookaside
102255 ** is disabled */
102256 assert( db->lookaside.bEnabled==0 );
102257 pTab->nRef = 1;
102258 pTab->zName = 0;
102259 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
102260 selectColumnsFromExprList(pParse, pSelect->pEList, &pTab->nCol, &pTab->aCol);
102261 selectAddColumnTypeAndCollation(pParse, pTab, pSelect);
102262 pTab->iPKey = -1;
102263 if( db->mallocFailed ){
102264 sqlite3DeleteTable(db, pTab);
@@ -104123,11 +104393,11 @@
104393 pFrom->pTab = pTab = sqlite3DbMallocZero(db, sizeof(Table));
104394 if( pTab==0 ) return WRC_Abort;
104395 pTab->nRef = 1;
104396 pTab->zName = sqlite3DbStrDup(db, pCte->zName);
104397 pTab->iPKey = -1;
104398 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
104399 pTab->tabFlags |= TF_Ephemeral;
104400 pFrom->pSelect = sqlite3SelectDup(db, pCte->pSelect, 0);
104401 if( db->mallocFailed ) return SQLITE_NOMEM;
104402 assert( pFrom->pSelect );
104403
@@ -104299,11 +104569,11 @@
104569 pTab->nRef = 1;
104570 pTab->zName = sqlite3MPrintf(db, "sqlite_sq_%p", (void*)pTab);
104571 while( pSel->pPrior ){ pSel = pSel->pPrior; }
104572 selectColumnsFromExprList(pParse, pSel->pEList, &pTab->nCol, &pTab->aCol);
104573 pTab->iPKey = -1;
104574 pTab->nRowLogEst = 200; assert( 200==sqlite3LogEst(1048576) );
104575 pTab->tabFlags |= TF_Ephemeral;
104576 #endif
104577 }else{
104578 /* An ordinary table or view name in the FROM clause */
104579 assert( pFrom->pTab==0 );
@@ -104794,14 +105064,15 @@
105064 Parse *pParse, /* Parse context */
105065 Table *pTab, /* Table being queried */
105066 Index *pIdx /* Index used to optimize scan, or NULL */
105067 ){
105068 if( pParse->explain==2 ){
105069 int bCover = (pIdx!=0 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pIdx)));
105070 char *zEqp = sqlite3MPrintf(pParse->db, "SCAN TABLE %s%s%s",
105071 pTab->zName,
105072 bCover ? " USING COVERING INDEX " : "",
105073 bCover ? pIdx->zName : ""
105074 );
105075 sqlite3VdbeAddOp4(
105076 pParse->pVdbe, OP_Explain, pParse->iSelectId, 0, 0, zEqp, P4_DYNAMIC
105077 );
105078 }
@@ -104949,11 +105220,11 @@
105220 VdbeComment((v, "%s", pItem->pTab->zName));
105221 pItem->addrFillSub = addrTop;
105222 sqlite3SelectDestInit(&dest, SRT_Coroutine, pItem->regReturn);
105223 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
105224 sqlite3Select(pParse, pSub, &dest);
105225 pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
105226 pItem->viaCoroutine = 1;
105227 pItem->regResult = dest.iSdst;
105228 sqlite3VdbeAddOp1(v, OP_EndCoroutine, pItem->regReturn);
105229 sqlite3VdbeJumpHere(v, addrTop-1);
105230 sqlite3ClearTempRegCache(pParse);
@@ -104980,11 +105251,11 @@
105251 VdbeNoopComment((v, "materialize \"%s\"", pItem->pTab->zName));
105252 }
105253 sqlite3SelectDestInit(&dest, SRT_EphemTab, pItem->iCursor);
105254 explainSetInteger(pItem->iSelectId, (u8)pParse->iNextSelectId);
105255 sqlite3Select(pParse, pSub, &dest);
105256 pItem->pTab->nRowLogEst = sqlite3LogEst(pSub->nSelectRow);
105257 if( onceAddr ) sqlite3VdbeJumpHere(v, onceAddr);
105258 retAddr = sqlite3VdbeAddOp1(v, OP_Return, pItem->regReturn);
105259 VdbeComment((v, "end %s", pItem->pTab->zName));
105260 sqlite3VdbeChangeP1(v, topAddr, retAddr);
105261 sqlite3ClearTempRegCache(pParse);
@@ -105013,22 +105284,10 @@
105284 explainSetInteger(pParse->iSelectId, iRestoreSelectId);
105285 return rc;
105286 }
105287 #endif
105288
 
 
 
 
 
 
 
 
 
 
 
 
105289 /* If the query is DISTINCT with an ORDER BY but is not an aggregate, and
105290 ** if the select-list is the same as the ORDER BY list, then this query
105291 ** can be rewritten as a GROUP BY. In other words, this:
105292 **
105293 ** SELECT DISTINCT xyz FROM ... ORDER BY xyz
@@ -105153,10 +105412,11 @@
105412 int iAbortFlag; /* Mem address which causes query abort if positive */
105413 int groupBySort; /* Rows come from source in GROUP BY order */
105414 int addrEnd; /* End of processing for this SELECT */
105415 int sortPTab = 0; /* Pseudotable used to decode sorting results */
105416 int sortOut = 0; /* Output register from the sorter */
105417 int orderByGrp = 0; /* True if the GROUP BY and ORDER BY are the same */
105418
105419 /* Remove any and all aliases between the result set and the
105420 ** GROUP BY clause.
105421 */
105422 if( pGroupBy ){
@@ -105172,10 +105432,22 @@
105432 if( p->nSelectRow>100 ) p->nSelectRow = 100;
105433 }else{
105434 p->nSelectRow = 1;
105435 }
105436
105437
105438 /* If there is both a GROUP BY and an ORDER BY clause and they are
105439 ** identical, then it may be possible to disable the ORDER BY clause
105440 ** on the grounds that the GROUP BY will cause elements to come out
105441 ** in the correct order. It also may not - the GROUP BY may use a
105442 ** database index that causes rows to be grouped together as required
105443 ** but not actually sorted. Either way, record the fact that the
105444 ** ORDER BY and GROUP BY clauses are the same by setting the orderByGrp
105445 ** variable. */
105446 if( sqlite3ExprListCompare(pGroupBy, sSort.pOrderBy, -1)==0 ){
105447 orderByGrp = 1;
105448 }
105449
105450 /* Create a label to jump to when we want to abort the query */
105451 addrEnd = sqlite3VdbeMakeLabel(v);
105452
105453 /* Convert TK_COLUMN nodes into TK_AGG_COLUMN and make entries in
@@ -105252,11 +105524,12 @@
105524 ** it might be a single loop that uses an index to extract information
105525 ** in the right order to begin with.
105526 */
105527 sqlite3VdbeAddOp2(v, OP_Gosub, regReset, addrReset);
105528 pWInfo = sqlite3WhereBegin(pParse, pTabList, pWhere, pGroupBy, 0,
105529 WHERE_GROUPBY | (orderByGrp ? WHERE_SORTBYGROUP : 0), 0
105530 );
105531 if( pWInfo==0 ) goto select_end;
105532 if( sqlite3WhereIsOrdered(pWInfo)==pGroupBy->nExpr ){
105533 /* The optimizer is able to deliver rows in group by order so
105534 ** we do not have to sort. The OP_OpenEphemeral table will be
105535 ** cancelled later because we still need to use the pKeyInfo
@@ -105317,10 +105590,25 @@
105590 sqlite3VdbeAddOp3(v, OP_OpenPseudo, sortPTab, sortOut, nCol);
105591 sqlite3VdbeAddOp2(v, OP_SorterSort, sAggInfo.sortingIdx, addrEnd);
105592 VdbeComment((v, "GROUP BY sort")); VdbeCoverage(v);
105593 sAggInfo.useSortingIdx = 1;
105594 sqlite3ExprCacheClear(pParse);
105595
105596 }
105597
105598 /* If the index or temporary table used by the GROUP BY sort
105599 ** will naturally deliver rows in the order required by the ORDER BY
105600 ** clause, cancel the ephemeral table open coded earlier.
105601 **
105602 ** This is an optimization - the correct answer should result regardless.
105603 ** Use the SQLITE_GroupByOrder flag with SQLITE_TESTCTRL_OPTIMIZER to
105604 ** disable this optimization for testing purposes. */
105605 if( orderByGrp && OptimizationEnabled(db, SQLITE_GroupByOrder)
105606 && (groupBySort || sqlite3WhereIsSorted(pWInfo))
105607 ){
105608 sSort.pOrderBy = 0;
105609 sqlite3VdbeChangeToNoop(v, sSort.addrSortIndex);
105610 }
105611
105612 /* Evaluate the current GROUP BY terms and store in b0, b1, b2...
105613 ** (b0 is memory location iBMem+0, b1 is iBMem+1, and so forth)
105614 ** Then compare the current GROUP BY terms against the GROUP BY terms
@@ -107204,11 +107492,11 @@
107492 */
107493 pTabList->a[0].iCursor = iBaseCur = iDataCur = pParse->nTab++;
107494 iIdxCur = iDataCur+1;
107495 pPk = HasRowid(pTab) ? 0 : sqlite3PrimaryKeyIndex(pTab);
107496 for(nIdx=0, pIdx=pTab->pIndex; pIdx; pIdx=pIdx->pNext, nIdx++){
107497 if( IsPrimaryKeyIndex(pIdx) && pPk!=0 ){
107498 iDataCur = pParse->nTab;
107499 pTabList->a[0].iCursor = iDataCur;
107500 }
107501 pParse->nTab++;
107502 }
@@ -109680,10 +109968,11 @@
109968 WhereLoop *pLoops; /* List of all WhereLoop objects */
109969 Bitmask revMask; /* Mask of ORDER BY terms that need reversing */
109970 LogEst nRowOut; /* Estimated number of output rows */
109971 u16 wctrlFlags; /* Flags originally passed to sqlite3WhereBegin() */
109972 i8 nOBSat; /* Number of ORDER BY terms satisfied by indices */
109973 u8 sorted; /* True if really sorted (not just grouped) */
109974 u8 okOnePass; /* Ok to use one-pass algorithm for UPDATE/DELETE */
109975 u8 untestedTerms; /* Not all WHERE terms resolved by outer loop */
109976 u8 eDistinct; /* One of the WHERE_DISTINCT_* values below */
109977 u8 nLevel; /* Number of nested loop */
109978 int iTop; /* The very beginning of the WHERE loop */
@@ -109739,10 +110028,11 @@
110028 #define WHERE_ONEROW 0x00001000 /* Selects no more than one row */
110029 #define WHERE_MULTI_OR 0x00002000 /* OR using multiple indices */
110030 #define WHERE_AUTO_INDEX 0x00004000 /* Uses an ephemeral index */
110031 #define WHERE_SKIPSCAN 0x00008000 /* Uses the skip-scan algorithm */
110032 #define WHERE_UNQ_WANTED 0x00010000 /* WHERE_ONEROW would have been helpful*/
110033 #define WHERE_LIKELIHOOD 0x00020000 /* A likelihood() is affecting nOut */
110034
110035 /************** End of whereInt.h ********************************************/
110036 /************** Continuing where we left off in where.c **********************/
110037
110038 /*
@@ -109951,11 +110241,11 @@
110241 }
110242 pTerm = &pWC->a[idx = pWC->nTerm++];
110243 if( p && ExprHasProperty(p, EP_Unlikely) ){
110244 pTerm->truthProb = sqlite3LogEst(p->iTable) - 99;
110245 }else{
110246 pTerm->truthProb = 1;
110247 }
110248 pTerm->pExpr = sqlite3ExprSkipCollate(p);
110249 pTerm->wtFlags = wtFlags;
110250 pTerm->pWC = pWC;
110251 pTerm->iParent = -1;
@@ -111680,11 +111970,12 @@
111970 tRowcnt iLower, iUpper, iGap;
111971 if( i==0 ){
111972 iLower = 0;
111973 iUpper = aSample[0].anLt[iCol];
111974 }else{
111975 i64 nRow0 = sqlite3LogEstToInt(pIdx->aiRowLogEst[0]);
111976 iUpper = i>=pIdx->nSample ? nRow0 : aSample[i].anLt[iCol];
111977 iLower = aSample[i-1].anEq[iCol] + aSample[i-1].anLt[iCol];
111978 }
111979 aStat[1] = (pIdx->nKeyCol>iCol ? pIdx->aAvgEq[iCol] : 1);
111980 if( iLower>=iUpper ){
111981 iGap = 0;
@@ -111698,10 +111989,33 @@
111989 }
111990 aStat[0] = iLower + iGap;
111991 }
111992 }
111993 #endif /* SQLITE_ENABLE_STAT3_OR_STAT4 */
111994
111995 /*
111996 ** If it is not NULL, pTerm is a term that provides an upper or lower
111997 ** bound on a range scan. Without considering pTerm, it is estimated
111998 ** that the scan will visit nNew rows. This function returns the number
111999 ** estimated to be visited after taking pTerm into account.
112000 **
112001 ** If the user explicitly specified a likelihood() value for this term,
112002 ** then the return value is the likelihood multiplied by the number of
112003 ** input rows. Otherwise, this function assumes that an "IS NOT NULL" term
112004 ** has a likelihood of 0.50, and any other term a likelihood of 0.25.
112005 */
112006 static LogEst whereRangeAdjust(WhereTerm *pTerm, LogEst nNew){
112007 LogEst nRet = nNew;
112008 if( pTerm ){
112009 if( pTerm->truthProb<=0 ){
112010 nRet += pTerm->truthProb;
112011 }else if( (pTerm->wtFlags & TERM_VNULL)==0 ){
112012 nRet -= 20; assert( 20==sqlite3LogEst(4) );
112013 }
112014 }
112015 return nRet;
112016 }
112017
112018 /*
112019 ** This function is used to estimate the number of rows that will be visited
112020 ** by scanning an index for a range of values. The range may have an upper
112021 ** bound, a lower bound, or both. The WHERE clause terms that set the upper
@@ -111791,11 +112105,11 @@
112105 aff = p->pTable->aCol[p->aiColumn[nEq]].affinity;
112106 }
112107 /* Determine iLower and iUpper using ($P) only. */
112108 if( nEq==0 ){
112109 iLower = 0;
112110 iUpper = sqlite3LogEstToInt(p->aiRowLogEst[0]);
112111 }else{
112112 /* Note: this call could be optimized away - since the same values must
112113 ** have been requested when testing key $P in whereEqualScanEst(). */
112114 whereKeyStats(pParse, p, pRec, 0, a);
112115 iLower = a[0];
@@ -111851,21 +112165,22 @@
112165 #else
112166 UNUSED_PARAMETER(pParse);
112167 UNUSED_PARAMETER(pBuilder);
112168 #endif
112169 assert( pLower || pUpper );
112170 assert( pUpper==0 || (pUpper->wtFlags & TERM_VNULL)==0 );
112171 nNew = whereRangeAdjust(pLower, nOut);
112172 nNew = whereRangeAdjust(pUpper, nNew);
112173
112174 /* TUNING: If there is both an upper and lower limit, assume the range is
112175 ** reduced by an additional 75%. This means that, by default, an open-ended
112176 ** range query (e.g. col > ?) is assumed to match 1/4 of the rows in the
112177 ** index. While a closed range (e.g. col BETWEEN ? AND ?) is estimated to
112178 ** match 1/64 of the index. */
112179 if( pLower && pUpper ) nNew -= 20;
112180
112181 nOut -= (pLower!=0) + (pUpper!=0);
112182 if( nNew<10 ) nNew = 10;
112183 if( nNew<nOut ) nOut = nNew;
112184 pLoop->nOut = (LogEst)nOut;
112185 return rc;
112186 }
@@ -111958,26 +112273,27 @@
112273 WhereLoopBuilder *pBuilder,
112274 ExprList *pList, /* The value list on the RHS of "x IN (v1,v2,v3,...)" */
112275 tRowcnt *pnRow /* Write the revised row estimate here */
112276 ){
112277 Index *p = pBuilder->pNew->u.btree.pIndex;
112278 i64 nRow0 = sqlite3LogEstToInt(p->aiRowLogEst[0]);
112279 int nRecValid = pBuilder->nRecValid;
112280 int rc = SQLITE_OK; /* Subfunction return code */
112281 tRowcnt nEst; /* Number of rows for a single term */
112282 tRowcnt nRowEst = 0; /* New estimate of the number of rows */
112283 int i; /* Loop counter */
112284
112285 assert( p->aSample!=0 );
112286 for(i=0; rc==SQLITE_OK && i<pList->nExpr; i++){
112287 nEst = nRow0;
112288 rc = whereEqualScanEst(pParse, pBuilder, pList->a[i].pExpr, &nEst);
112289 nRowEst += nEst;
112290 pBuilder->nRecValid = nRecValid;
112291 }
112292
112293 if( rc==SQLITE_OK ){
112294 if( nRowEst > nRow0 ) nRowEst = nRow0;
112295 *pnRow = nRowEst;
112296 WHERETRACE(0x10,("IN row estimate: est=%g\n", nRowEst));
112297 }
112298 assert( pBuilder->nRecValid==nRecValid );
112299 return rc;
@@ -112416,17 +112732,24 @@
112732 zMsg = sqlite3MAppendf(db, zMsg, "%s AS %s", zMsg, pItem->zAlias);
112733 }
112734 if( (flags & (WHERE_IPK|WHERE_VIRTUALTABLE))==0
112735 && ALWAYS(pLoop->u.btree.pIndex!=0)
112736 ){
112737 const char *zFmt;
112738 Index *pIdx = pLoop->u.btree.pIndex;
112739 char *zWhere = explainIndexRange(db, pLoop, pItem->pTab);
112740 assert( !(flags&WHERE_AUTO_INDEX) || (flags&WHERE_IDX_ONLY) );
112741 if( !HasRowid(pItem->pTab) && IsPrimaryKeyIndex(pIdx) ){
112742 zFmt = zWhere ? "%s USING PRIMARY KEY%.0s%s" : "%s%.0s%s";
112743 }else if( flags & WHERE_AUTO_INDEX ){
112744 zFmt = "%s USING AUTOMATIC COVERING INDEX%.0s%s";
112745 }else if( flags & WHERE_IDX_ONLY ){
112746 zFmt = "%s USING COVERING INDEX %s%s";
112747 }else{
112748 zFmt = "%s USING INDEX %s%s";
112749 }
112750 zMsg = sqlite3MAppendf(db, zMsg, zFmt, zMsg, pIdx->zName, zWhere);
112751 sqlite3DbFree(db, zWhere);
112752 }else if( (flags & WHERE_IPK)!=0 && (flags & WHERE_CONSTRAINT)!=0 ){
112753 zMsg = sqlite3MAppendf(db, zMsg, "%s USING INTEGER PRIMARY KEY", zMsg);
112754
112755 if( flags&(WHERE_COLUMN_EQ|WHERE_COLUMN_IN) ){
@@ -112914,11 +113237,11 @@
113237 }else if( HasRowid(pIdx->pTable) ){
113238 iRowidReg = ++pParse->nMem;
113239 sqlite3VdbeAddOp2(v, OP_IdxRowid, iIdxCur, iRowidReg);
113240 sqlite3ExprCacheStore(pParse, iCur, -1, iRowidReg);
113241 sqlite3VdbeAddOp2(v, OP_Seek, iCur, iRowidReg); /* Deferred seek */
113242 }else if( iCur!=iIdxCur ){
113243 Index *pPk = sqlite3PrimaryKeyIndex(pIdx->pTable);
113244 iRowidReg = sqlite3GetTempRange(pParse, pPk->nKeyCol);
113245 for(j=0; j<pPk->nKeyCol; j++){
113246 k = sqlite3ColumnOfIndex(pIdx, pPk->aiColumn[j]);
113247 sqlite3VdbeAddOp3(v, OP_Column, iIdxCur, k, iRowidReg+j);
@@ -112984,10 +113307,14 @@
113307 **
113308 ** Return 2 # Jump back to the Gosub
113309 **
113310 ** B: <after the loop>
113311 **
113312 ** Added 2014-05-26: If the table is a WITHOUT ROWID table, then
113313 ** use an ephermeral index instead of a RowSet to record the primary
113314 ** keys of the rows we have already seen.
113315 **
113316 */
113317 WhereClause *pOrWc; /* The OR-clause broken out into subterms */
113318 SrcList *pOrTab; /* Shortened table list or OR-clause generation */
113319 Index *pCov = 0; /* Potential covering index (or NULL) */
113320 int iCovCur = pParse->nTab++; /* Cursor used for index scans (if any) */
@@ -112998,10 +113325,11 @@
113325 int iLoopBody = sqlite3VdbeMakeLabel(v); /* Start of loop body */
113326 int iRetInit; /* Address of regReturn init */
113327 int untestedTerms = 0; /* Some terms not completely tested */
113328 int ii; /* Loop counter */
113329 Expr *pAndExpr = 0; /* An ".. AND (...)" expression */
113330 Table *pTab = pTabItem->pTab;
113331
113332 pTerm = pLoop->aLTerm[0];
113333 assert( pTerm!=0 );
113334 assert( pTerm->eOperator & WO_OR );
113335 assert( (pTerm->wtFlags & TERM_ORINFO)!=0 );
@@ -113030,11 +113358,12 @@
113358 }else{
113359 pOrTab = pWInfo->pTabList;
113360 }
113361
113362 /* Initialize the rowset register to contain NULL. An SQL NULL is
113363 ** equivalent to an empty rowset. Or, create an ephermeral index
113364 ** capable of holding primary keys in the case of a WITHOUT ROWID.
113365 **
113366 ** Also initialize regReturn to contain the address of the instruction
113367 ** immediately following the OP_Return at the bottom of the loop. This
113368 ** is required in a few obscure LEFT JOIN cases where control jumps
113369 ** over the top of the loop into the body of it. In this case the
@@ -113041,13 +113370,20 @@
113370 ** correct response for the end-of-loop code (the OP_Return) is to
113371 ** fall through to the next instruction, just as an OP_Next does if
113372 ** called on an uninitialized cursor.
113373 */
113374 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
113375 if( HasRowid(pTab) ){
113376 regRowset = ++pParse->nMem;
113377 sqlite3VdbeAddOp2(v, OP_Null, 0, regRowset);
113378 }else{
113379 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
113380 regRowset = pParse->nTab++;
113381 sqlite3VdbeAddOp2(v, OP_OpenEphemeral, regRowset, pPk->nKeyCol);
113382 sqlite3VdbeSetP4KeyInfo(pParse, pPk);
113383 }
113384 regRowid = ++pParse->nMem;
 
113385 }
113386 iRetInit = sqlite3VdbeAddOp2(v, OP_Integer, 0, regReturn);
113387
113388 /* If the original WHERE clause is z of the form: (x1 OR x2 OR ...) AND y
113389 ** Then for every term xN, evaluate as the subexpression: xN AND z
@@ -113079,15 +113415,20 @@
113415 if( pAndExpr ){
113416 pAndExpr = sqlite3PExpr(pParse, TK_AND, 0, pAndExpr, 0);
113417 }
113418 }
113419
113420 /* Run a separate WHERE clause for each term of the OR clause. After
113421 ** eliminating duplicates from other WHERE clauses, the action for each
113422 ** sub-WHERE clause is to to invoke the main loop body as a subroutine.
113423 */
113424 for(ii=0; ii<pOrWc->nTerm; ii++){
113425 WhereTerm *pOrTerm = &pOrWc->a[ii];
113426 if( pOrTerm->leftCursor==iCur || (pOrTerm->eOperator & WO_AND)!=0 ){
113427 WhereInfo *pSubWInfo; /* Info for single OR-term scan */
113428 Expr *pOrExpr = pOrTerm->pExpr; /* Current OR clause term */
113429 int j1 = 0; /* Address of jump operation */
113430 if( pAndExpr && !ExprHasProperty(pOrExpr, EP_FromJoin) ){
113431 pAndExpr->pLeft = pOrExpr;
113432 pOrExpr = pAndExpr;
113433 }
113434 /* Loop through table entries that match term pOrTerm. */
@@ -113098,20 +113439,66 @@
113439 if( pSubWInfo ){
113440 WhereLoop *pSubLoop;
113441 explainOneScan(
113442 pParse, pOrTab, &pSubWInfo->a[0], iLevel, pLevel->iFrom, 0
113443 );
113444 /* This is the sub-WHERE clause body. First skip over
113445 ** duplicate rows from prior sub-WHERE clauses, and record the
113446 ** rowid (or PRIMARY KEY) for the current row so that the same
113447 ** row will be skipped in subsequent sub-WHERE clauses.
113448 */
113449 if( (pWInfo->wctrlFlags & WHERE_DUPLICATES_OK)==0 ){
 
113450 int r;
113451 int iSet = ((ii==pOrWc->nTerm-1)?-1:ii);
113452 if( HasRowid(pTab) ){
113453 r = sqlite3ExprCodeGetColumn(pParse, pTab, -1, iCur, regRowid, 0);
113454 j1 = sqlite3VdbeAddOp4Int(v, OP_RowSetTest, regRowset, 0, r,iSet);
113455 VdbeCoverage(v);
113456 }else{
113457 Index *pPk = sqlite3PrimaryKeyIndex(pTab);
113458 int nPk = pPk->nKeyCol;
113459 int iPk;
113460
113461 /* Read the PK into an array of temp registers. */
113462 r = sqlite3GetTempRange(pParse, nPk);
113463 for(iPk=0; iPk<nPk; iPk++){
113464 int iCol = pPk->aiColumn[iPk];
113465 sqlite3ExprCodeGetColumn(pParse, pTab, iCol, iCur, r+iPk, 0);
113466 }
113467
113468 /* Check if the temp table already contains this key. If so,
113469 ** the row has already been included in the result set and
113470 ** can be ignored (by jumping past the Gosub below). Otherwise,
113471 ** insert the key into the temp table and proceed with processing
113472 ** the row.
113473 **
113474 ** Use some of the same optimizations as OP_RowSetTest: If iSet
113475 ** is zero, assume that the key cannot already be present in
113476 ** the temp table. And if iSet is -1, assume that there is no
113477 ** need to insert the key into the temp table, as it will never
113478 ** be tested for. */
113479 if( iSet ){
113480 j1 = sqlite3VdbeAddOp4Int(v, OP_Found, regRowset, 0, r, nPk);
113481 VdbeCoverage(v);
113482 }
113483 if( iSet>=0 ){
113484 sqlite3VdbeAddOp3(v, OP_MakeRecord, r, nPk, regRowid);
113485 sqlite3VdbeAddOp3(v, OP_IdxInsert, regRowset, regRowid, 0);
113486 if( iSet ) sqlite3VdbeChangeP5(v, OPFLAG_USESEEKRESULT);
113487 }
113488
113489 /* Release the array of temp registers */
113490 sqlite3ReleaseTempRange(pParse, r, nPk);
113491 }
113492 }
113493
113494 /* Invoke the main loop body as a subroutine */
113495 sqlite3VdbeAddOp2(v, OP_Gosub, regReturn, iLoopBody);
113496
113497 /* Jump here (skipping the main loop body subroutine) if the
113498 ** current sub-WHERE row is a duplicate from prior sub-WHEREs. */
113499 if( j1 ) sqlite3VdbeJumpHere(v, j1);
113500
113501 /* The pSubWInfo->untestedTerms flag means that this OR term
113502 ** contained one or more AND term from a notReady table. The
113503 ** terms from the notReady table could not be tested and will
113504 ** need to be tested later.
@@ -113132,10 +113519,11 @@
113519 */
113520 pSubLoop = pSubWInfo->a[0].pWLoop;
113521 assert( (pSubLoop->wsFlags & WHERE_AUTO_INDEX)==0 );
113522 if( (pSubLoop->wsFlags & WHERE_INDEXED)!=0
113523 && (ii==0 || pSubLoop->u.btree.pIndex==pCov)
113524 && (HasRowid(pTab) || !IsPrimaryKeyIndex(pSubLoop->u.btree.pIndex))
113525 ){
113526 assert( pSubWInfo->a[0].iIdxCur==iCovCur );
113527 pCov = pSubLoop->u.btree.pIndex;
113528 }else{
113529 pCov = 0;
@@ -113459,11 +113847,11 @@
113847 if( pX->nLTerm >= pY->nLTerm ) return 0; /* X is not a subset of Y */
113848 if( pX->rRun >= pY->rRun ){
113849 if( pX->rRun > pY->rRun ) return 0; /* X costs more than Y */
113850 if( pX->nOut > pY->nOut ) return 0; /* X costs more than Y */
113851 }
113852 for(i=pX->nLTerm-1; i>=0; i--){
113853 for(j=pY->nLTerm-1; j>=0; j--){
113854 if( pY->aLTerm[j]==pX->aLTerm[i] ) break;
113855 }
113856 if( j<0 ) return 0; /* X not a subset of Y since term X[i] not used by Y */
113857 }
@@ -113481,16 +113869,29 @@
113869 ** is a proper subset.
113870 **
113871 ** To say "WhereLoop X is a proper subset of Y" means that X uses fewer
113872 ** WHERE clause terms than Y and that every WHERE clause term used by X is
113873 ** also used by Y.
113874 **
113875 ** This adjustment is omitted for SKIPSCAN loops. In a SKIPSCAN loop, the
113876 ** WhereLoop.nLTerm field is not an accurate measure of the number of WHERE
113877 ** clause terms covered, since some of the first nLTerm entries in aLTerm[]
113878 ** will be NULL (because they are skipped). That makes it more difficult
113879 ** to compare the loops. We could add extra code to do the comparison, and
113880 ** perhaps we will someday. But SKIPSCAN is sufficiently uncommon, and this
113881 ** adjustment is sufficient minor, that it is very difficult to construct
113882 ** a test case where the extra code would improve the query plan. Better
113883 ** to avoid the added complexity and just omit cost adjustments to SKIPSCAN
113884 ** loops.
113885 */
113886 static void whereLoopAdjustCost(const WhereLoop *p, WhereLoop *pTemplate){
113887 if( (pTemplate->wsFlags & WHERE_INDEXED)==0 ) return;
113888 if( (pTemplate->wsFlags & WHERE_SKIPSCAN)!=0 ) return;
113889 for(; p; p=p->pNextLoop){
113890 if( p->iTab!=pTemplate->iTab ) continue;
113891 if( (p->wsFlags & WHERE_INDEXED)==0 ) continue;
113892 if( (p->wsFlags & WHERE_SKIPSCAN)!=0 ) continue;
113893 if( whereLoopCheaperProperSubset(p, pTemplate) ){
113894 /* Adjust pTemplate cost downward so that it is cheaper than its
113895 ** subset p */
113896 pTemplate->rRun = p->rRun;
113897 pTemplate->nOut = p->nOut - 1;
@@ -113711,17 +114112,24 @@
114112 pX = pLoop->aLTerm[j];
114113 if( pX==0 ) continue;
114114 if( pX==pTerm ) break;
114115 if( pX->iParent>=0 && (&pWC->a[pX->iParent])==pTerm ) break;
114116 }
114117 if( j<0 ){
114118 pLoop->nOut += (pTerm->truthProb<=0 ? pTerm->truthProb : -1);
114119 }
114120 }
114121 }
114122
114123 /*
114124 ** We have so far matched pBuilder->pNew->u.btree.nEq terms of the
114125 ** index pIndex. Try to match one more.
114126 **
114127 ** When this function is called, pBuilder->pNew->nOut contains the
114128 ** number of rows expected to be visited by filtering using the nEq
114129 ** terms only. If it is modified, this value is restored before this
114130 ** function returns.
114131 **
114132 ** If pProbe->tnum==0, that means pIndex is a fake index used for the
114133 ** INTEGER PRIMARY KEY.
114134 */
114135 static int whereLoopAddBtreeIndex(
@@ -113743,11 +114151,10 @@
114151 u16 saved_nSkip; /* Original value of pNew->u.btree.nSkip */
114152 u32 saved_wsFlags; /* Original value of pNew->wsFlags */
114153 LogEst saved_nOut; /* Original value of pNew->nOut */
114154 int iCol; /* Index of the column in the table */
114155 int rc = SQLITE_OK; /* Return code */
 
114156 LogEst rLogSize; /* Logarithm of table size */
114157 WhereTerm *pTop = 0, *pBtm = 0; /* Top and bottom range constraints */
114158
114159 pNew = pBuilder->pNew;
114160 if( db->mallocFailed ) return SQLITE_NOMEM;
@@ -113764,15 +114171,12 @@
114171 if( pProbe->bUnordered ) opMask &= ~(WO_GT|WO_GE|WO_LT|WO_LE);
114172
114173 assert( pNew->u.btree.nEq<=pProbe->nKeyCol );
114174 if( pNew->u.btree.nEq < pProbe->nKeyCol ){
114175 iCol = pProbe->aiColumn[pNew->u.btree.nEq];
 
 
114176 }else{
114177 iCol = -1;
 
114178 }
114179 pTerm = whereScanInit(&scan, pBuilder->pWC, pSrc->iCursor, iCol,
114180 opMask, pProbe);
114181 saved_nEq = pNew->u.btree.nEq;
114182 saved_nSkip = pNew->u.btree.nSkip;
@@ -113779,57 +114183,68 @@
114183 saved_nLTerm = pNew->nLTerm;
114184 saved_wsFlags = pNew->wsFlags;
114185 saved_prereq = pNew->prereq;
114186 saved_nOut = pNew->nOut;
114187 pNew->rSetup = 0;
114188 rLogSize = estLog(pProbe->aiRowLogEst[0]);
114189
114190 /* Consider using a skip-scan if there are no WHERE clause constraints
114191 ** available for the left-most terms of the index, and if the average
114192 ** number of repeats in the left-most terms is at least 18.
114193 **
114194 ** The magic number 18 is selected on the basis that scanning 17 rows
114195 ** is almost always quicker than an index seek (even though if the index
114196 ** contains fewer than 2^17 rows we assume otherwise in other parts of
114197 ** the code). And, even if it is not, it should not be too much slower.
114198 ** On the other hand, the extra seeks could end up being significantly
114199 ** more expensive. */
114200 assert( 42==sqlite3LogEst(18) );
114201 if( pTerm==0
114202 && saved_nEq==saved_nSkip
114203 && saved_nEq+1<pProbe->nKeyCol
114204 && pProbe->aiRowLogEst[saved_nEq+1]>=42 /* TUNING: Minimum for skip-scan */
114205 && (rc = whereLoopResize(db, pNew, pNew->nLTerm+1))==SQLITE_OK
114206 ){
114207 LogEst nIter;
114208 pNew->u.btree.nEq++;
114209 pNew->u.btree.nSkip++;
114210 pNew->aLTerm[pNew->nLTerm++] = 0;
114211 pNew->wsFlags |= WHERE_SKIPSCAN;
114212 nIter = pProbe->aiRowLogEst[saved_nEq] - pProbe->aiRowLogEst[saved_nEq+1];
114213 pNew->nOut -= nIter;
114214 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nIter + nInMul);
 
114215 pNew->nOut = saved_nOut;
114216 }
114217 for(; rc==SQLITE_OK && pTerm!=0; pTerm = whereScanNext(&scan)){
114218 u16 eOp = pTerm->eOperator; /* Shorthand for pTerm->eOperator */
114219 LogEst rCostIdx;
114220 LogEst nOutUnadjusted; /* nOut before IN() and WHERE adjustments */
114221 int nIn = 0;
114222 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
114223 int nRecValid = pBuilder->nRecValid;
114224 #endif
114225 if( (eOp==WO_ISNULL || (pTerm->wtFlags&TERM_VNULL)!=0)
114226 && (iCol<0 || pSrc->pTab->aCol[iCol].notNull)
114227 ){
114228 continue; /* ignore IS [NOT] NULL constraints on NOT NULL columns */
114229 }
114230 if( pTerm->prereqRight & pNew->maskSelf ) continue;
114231
 
 
114232 pNew->wsFlags = saved_wsFlags;
114233 pNew->u.btree.nEq = saved_nEq;
114234 pNew->nLTerm = saved_nLTerm;
114235 if( whereLoopResize(db, pNew, pNew->nLTerm+1) ) break; /* OOM */
114236 pNew->aLTerm[pNew->nLTerm++] = pTerm;
114237 pNew->prereq = (saved_prereq | pTerm->prereqRight) & ~pNew->maskSelf;
114238
114239 assert( nInMul==0
114240 || (pNew->wsFlags & WHERE_COLUMN_NULL)!=0
114241 || (pNew->wsFlags & WHERE_COLUMN_IN)!=0
114242 || (pNew->wsFlags & WHERE_SKIPSCAN)!=0
114243 );
114244
114245 if( eOp & WO_IN ){
114246 Expr *pExpr = pTerm->pExpr;
114247 pNew->wsFlags |= WHERE_COLUMN_IN;
114248 if( ExprHasProperty(pExpr, EP_xIsSelect) ){
114249 /* "x IN (SELECT ...)": TUNING: the SELECT returns 25 rows */
114250 nIn = 46; assert( 46==sqlite3LogEst(25) );
@@ -113837,87 +114252,122 @@
114252 /* "x IN (value, value, ...)" */
114253 nIn = sqlite3LogEst(pExpr->x.pList->nExpr);
114254 }
114255 assert( nIn>0 ); /* RHS always has 2 or more terms... The parser
114256 ** changes "x IN (?)" into "x=?". */
114257
114258 }else if( eOp & (WO_EQ) ){
 
 
 
 
 
 
114259 pNew->wsFlags |= WHERE_COLUMN_EQ;
114260 if( iCol<0 || (nInMul==0 && pNew->u.btree.nEq==pProbe->nKeyCol-1) ){
 
114261 if( iCol>=0 && pProbe->onError==OE_None ){
114262 pNew->wsFlags |= WHERE_UNQ_WANTED;
114263 }else{
114264 pNew->wsFlags |= WHERE_ONEROW;
114265 }
114266 }
114267 }else if( eOp & WO_ISNULL ){
 
 
114268 pNew->wsFlags |= WHERE_COLUMN_NULL;
114269 }else if( eOp & (WO_GT|WO_GE) ){
114270 testcase( eOp & WO_GT );
114271 testcase( eOp & WO_GE );
 
 
 
 
114272 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_BTM_LIMIT;
114273 pBtm = pTerm;
114274 pTop = 0;
114275 }else{
114276 assert( eOp & (WO_LT|WO_LE) );
114277 testcase( eOp & WO_LT );
114278 testcase( eOp & WO_LE );
114279 pNew->wsFlags |= WHERE_COLUMN_RANGE|WHERE_TOP_LIMIT;
114280 pTop = pTerm;
114281 pBtm = (pNew->wsFlags & WHERE_BTM_LIMIT)!=0 ?
114282 pNew->aLTerm[pNew->nLTerm-2] : 0;
114283 }
114284
114285 /* At this point pNew->nOut is set to the number of rows expected to
114286 ** be visited by the index scan before considering term pTerm, or the
114287 ** values of nIn and nInMul. In other words, assuming that all
114288 ** "x IN(...)" terms are replaced with "x = ?". This block updates
114289 ** the value of pNew->nOut to account for pTerm (but not nIn/nInMul). */
114290 assert( pNew->nOut==saved_nOut );
114291 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
114292 /* Adjust nOut using stat3/stat4 data. Or, if there is no stat3/stat4
114293 ** data, using some other estimate. */
114294 whereRangeScanEst(pParse, pBuilder, pBtm, pTop, pNew);
114295 }else{
114296 int nEq = ++pNew->u.btree.nEq;
114297 assert( eOp & (WO_ISNULL|WO_EQ|WO_IN) );
114298
114299 assert( pNew->nOut==saved_nOut );
114300 if( pTerm->truthProb<=0 && iCol>=0 ){
114301 assert( (eOp & WO_IN) || nIn==0 );
114302 testcase( eOp & WO_IN );
114303 pNew->nOut += pTerm->truthProb;
114304 pNew->nOut -= nIn;
114305 pNew->wsFlags |= WHERE_LIKELIHOOD;
114306 }else{
114307 #ifdef SQLITE_ENABLE_STAT3_OR_STAT4
114308 tRowcnt nOut = 0;
114309 if( nInMul==0
114310 && pProbe->nSample
114311 && pNew->u.btree.nEq<=pProbe->nSampleCol
114312 && OptimizationEnabled(db, SQLITE_Stat3)
114313 && ((eOp & WO_IN)==0 || !ExprHasProperty(pTerm->pExpr, EP_xIsSelect))
114314 && (pNew->wsFlags & WHERE_LIKELIHOOD)==0
114315 ){
114316 Expr *pExpr = pTerm->pExpr;
114317 if( (eOp & (WO_EQ|WO_ISNULL))!=0 ){
114318 testcase( eOp & WO_EQ );
114319 testcase( eOp & WO_ISNULL );
114320 rc = whereEqualScanEst(pParse, pBuilder, pExpr->pRight, &nOut);
114321 }else{
114322 rc = whereInScanEst(pParse, pBuilder, pExpr->x.pList, &nOut);
114323 }
114324 assert( rc!=SQLITE_OK || nOut>0 );
114325 if( rc==SQLITE_NOTFOUND ) rc = SQLITE_OK;
114326 if( rc!=SQLITE_OK ) break; /* Jump out of the pTerm loop */
114327 if( nOut ){
114328 pNew->nOut = sqlite3LogEst(nOut);
114329 if( pNew->nOut>saved_nOut ) pNew->nOut = saved_nOut;
114330 pNew->nOut -= nIn;
114331 }
114332 }
114333 if( nOut==0 )
114334 #endif
114335 {
114336 pNew->nOut += (pProbe->aiRowLogEst[nEq] - pProbe->aiRowLogEst[nEq-1]);
114337 if( eOp & WO_ISNULL ){
114338 /* TUNING: If there is no likelihood() value, assume that a
114339 ** "col IS NULL" expression matches twice as many rows
114340 ** as (col=?). */
114341 pNew->nOut += 10;
114342 }
114343 }
114344 }
114345 }
114346
114347 /* Set rCostIdx to the cost of visiting selected rows in index. Add
114348 ** it to pNew->rRun, which is currently set to the cost of the index
114349 ** seek only. Then, if this is a non-covering index, add the cost of
114350 ** visiting the rows in the main table. */
114351 rCostIdx = pNew->nOut + 1 + (15*pProbe->szIdxRow)/pSrc->pTab->szTabRow;
114352 pNew->rRun = sqlite3LogEstAdd(rLogSize, rCostIdx);
114353 if( (pNew->wsFlags & (WHERE_IDX_ONLY|WHERE_IPK))==0 ){
114354 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, pNew->nOut + 16);
 
 
114355 }
114356
114357 nOutUnadjusted = pNew->nOut;
114358 pNew->rRun += nInMul + nIn;
114359 pNew->nOut += nInMul + nIn;
114360 whereLoopOutputAdjust(pBuilder->pWC, pNew);
114361 rc = whereLoopInsert(pBuilder, pNew);
114362
114363 if( pNew->wsFlags & WHERE_COLUMN_RANGE ){
114364 pNew->nOut = saved_nOut;
114365 }else{
114366 pNew->nOut = nOutUnadjusted;
114367 }
114368
114369 if( (pNew->wsFlags & WHERE_TOP_LIMIT)==0
114370 && pNew->u.btree.nEq<(pProbe->nKeyCol + (pProbe->zName!=0))
114371 ){
114372 whereLoopAddBtreeIndex(pBuilder, pSrc, pProbe, nInMul+nIn);
114373 }
@@ -113997,19 +114447,42 @@
114447
114448 /*
114449 ** Add all WhereLoop objects for a single table of the join where the table
114450 ** is idenfied by pBuilder->pNew->iTab. That table is guaranteed to be
114451 ** a b-tree table, not a virtual table.
114452 **
114453 ** The costs (WhereLoop.rRun) of the b-tree loops added by this function
114454 ** are calculated as follows:
114455 **
114456 ** For a full scan, assuming the table (or index) contains nRow rows:
114457 **
114458 ** cost = nRow * 3.0 // full-table scan
114459 ** cost = nRow * K // scan of covering index
114460 ** cost = nRow * (K+3.0) // scan of non-covering index
114461 **
114462 ** where K is a value between 1.1 and 3.0 set based on the relative
114463 ** estimated average size of the index and table records.
114464 **
114465 ** For an index scan, where nVisit is the number of index rows visited
114466 ** by the scan, and nSeek is the number of seek operations required on
114467 ** the index b-tree:
114468 **
114469 ** cost = nSeek * (log(nRow) + K * nVisit) // covering index
114470 ** cost = nSeek * (log(nRow) + (K+3.0) * nVisit) // non-covering index
114471 **
114472 ** Normally, nSeek is 1. nSeek values greater than 1 come about if the
114473 ** WHERE clause includes "x IN (....)" terms used in place of "x=?". Or when
114474 ** implicit "x IN (SELECT x FROM tbl)" terms are added for skip-scans.
114475 */
114476 static int whereLoopAddBtree(
114477 WhereLoopBuilder *pBuilder, /* WHERE clause information */
114478 Bitmask mExtra /* Extra prerequesites for using this table */
114479 ){
114480 WhereInfo *pWInfo; /* WHERE analysis context */
114481 Index *pProbe; /* An index we are evaluating */
114482 Index sPk; /* A fake index object for the primary key */
114483 LogEst aiRowEstPk[2]; /* The aiRowLogEst[] value for the sPk index */
114484 i16 aiColumnPk = -1; /* The aColumn[] value for the sPk index */
114485 SrcList *pTabList; /* The FROM clause */
114486 struct SrcList_item *pSrc; /* The FROM clause btree term to add */
114487 WhereLoop *pNew; /* Template WhereLoop object */
114488 int rc = SQLITE_OK; /* Return code */
@@ -114040,24 +114513,25 @@
114513 ** indices to follow */
114514 Index *pFirst; /* First of real indices on the table */
114515 memset(&sPk, 0, sizeof(Index));
114516 sPk.nKeyCol = 1;
114517 sPk.aiColumn = &aiColumnPk;
114518 sPk.aiRowLogEst = aiRowEstPk;
114519 sPk.onError = OE_Replace;
114520 sPk.pTable = pTab;
114521 sPk.szIdxRow = pTab->szTabRow;
114522 aiRowEstPk[0] = pTab->nRowLogEst;
114523 aiRowEstPk[1] = 0;
114524 pFirst = pSrc->pTab->pIndex;
114525 if( pSrc->notIndexed==0 ){
114526 /* The real indices of the table are only considered if the
114527 ** NOT INDEXED qualifier is omitted from the FROM clause */
114528 sPk.pNext = pFirst;
114529 }
114530 pProbe = &sPk;
114531 }
114532 rSize = pTab->nRowLogEst;
114533 rLogSize = estLog(rSize);
114534
114535 #ifndef SQLITE_OMIT_AUTOMATIC_INDEX
114536 /* Automatic indexes */
114537 if( !pBuilder->pOrSet
@@ -114103,10 +114577,11 @@
114577 for(; rc==SQLITE_OK && pProbe; pProbe=pProbe->pNext, iSortIdx++){
114578 if( pProbe->pPartIdxWhere!=0
114579 && !whereUsablePartialIndex(pNew->iTab, pWC, pProbe->pPartIdxWhere) ){
114580 continue; /* Partial index inappropriate for this query */
114581 }
114582 rSize = pProbe->aiRowLogEst[0];
114583 pNew->u.btree.nEq = 0;
114584 pNew->u.btree.nSkip = 0;
114585 pNew->nLTerm = 0;
114586 pNew->iSortIdx = 0;
114587 pNew->rSetup = 0;
@@ -114120,14 +114595,12 @@
114595 /* Integer primary key index */
114596 pNew->wsFlags = WHERE_IPK;
114597
114598 /* Full table scan */
114599 pNew->iSortIdx = b ? iSortIdx : 0;
114600 /* TUNING: Cost of full table scan is (N*3.0). */
114601 pNew->rRun = rSize + 16;
 
 
114602 whereLoopOutputAdjust(pWC, pNew);
114603 rc = whereLoopInsert(pBuilder, pNew);
114604 pNew->nOut = rSize;
114605 if( rc ) break;
114606 }else{
@@ -114150,39 +114623,20 @@
114623 && sqlite3GlobalConfig.bUseCis
114624 && OptimizationEnabled(pWInfo->pParse->db, SQLITE_CoverIdxScan)
114625 )
114626 ){
114627 pNew->iSortIdx = b ? iSortIdx : 0;
114628
114629 /* The cost of visiting the index rows is N*K, where K is
114630 ** between 1.1 and 3.0, depending on the relative sizes of the
114631 ** index and table rows. If this is a non-covering index scan,
114632 ** also add the cost of visiting table rows (N*3.0). */
114633 pNew->rRun = rSize + 1 + (15*pProbe->szIdxRow)/pTab->szTabRow;
114634 if( m!=0 ){
114635 pNew->rRun = sqlite3LogEstAdd(pNew->rRun, rSize+16);
114636 }
114637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114638 whereLoopOutputAdjust(pWC, pNew);
114639 rc = whereLoopInsert(pBuilder, pNew);
114640 pNew->nOut = rSize;
114641 if( rc ) break;
114642 }
@@ -114382,20 +114836,19 @@
114836 WhereTerm *pTerm, *pWCEnd;
114837 int rc = SQLITE_OK;
114838 int iCur;
114839 WhereClause tempWC;
114840 WhereLoopBuilder sSubBuild;
114841 WhereOrSet sSum, sCur;
114842 struct SrcList_item *pItem;
114843
114844 pWC = pBuilder->pWC;
114845 if( pWInfo->wctrlFlags & WHERE_AND_ONLY ) return SQLITE_OK;
114846 pWCEnd = pWC->a + pWC->nTerm;
114847 pNew = pBuilder->pNew;
114848 memset(&sSum, 0, sizeof(sSum));
114849 pItem = pWInfo->pTabList->a + pNew->iTab;
 
114850 iCur = pItem->iCursor;
114851
114852 for(pTerm=pWC->a; pTerm<pWCEnd && rc==SQLITE_OK; pTerm++){
114853 if( (pTerm->eOperator & WO_OR)!=0
114854 && (pTerm->u.pOrInfo->indexable & pNew->maskSelf)!=0
@@ -114438,10 +114891,11 @@
114891 break;
114892 }else if( once ){
114893 whereOrMove(&sSum, &sCur);
114894 once = 0;
114895 }else{
114896 WhereOrSet sPrev;
114897 whereOrMove(&sPrev, &sSum);
114898 sSum.n = 0;
114899 for(i=0; i<sPrev.n; i++){
114900 for(j=0; j<sCur.n; j++){
114901 whereOrInsert(&sSum, sPrev.a[i].prereq | sCur.a[j].prereq,
@@ -114456,12 +114910,23 @@
114910 pNew->wsFlags = WHERE_MULTI_OR;
114911 pNew->rSetup = 0;
114912 pNew->iSortIdx = 0;
114913 memset(&pNew->u, 0, sizeof(pNew->u));
114914 for(i=0; rc==SQLITE_OK && i<sSum.n; i++){
114915 /* TUNING: Currently sSum.a[i].rRun is set to the sum of the costs
114916 ** of all sub-scans required by the OR-scan. However, due to rounding
114917 ** errors, it may be that the cost of the OR-scan is equal to its
114918 ** most expensive sub-scan. Add the smallest possible penalty
114919 ** (equivalent to multiplying the cost by 1.07) to ensure that
114920 ** this does not happen. Otherwise, for WHERE clauses such as the
114921 ** following where there is an index on "y":
114922 **
114923 ** WHERE likelihood(x=?, 0.99) OR y=?
114924 **
114925 ** the planner may elect to "OR" together a full-table scan and an
114926 ** index lookup. And other similarly odd results. */
114927 pNew->rRun = sSum.a[i].rRun + 1;
114928 pNew->nOut = sSum.a[i].nOut;
114929 pNew->prereq = sSum.a[i].prereq;
114930 rc = whereLoopInsert(pBuilder, pNew);
114931 }
114932 }
@@ -114520,11 +114985,11 @@
114985 ** N<0: Unknown yet how many terms of ORDER BY might be satisfied.
114986 **
114987 ** Note that processing for WHERE_GROUPBY and WHERE_DISTINCTBY is not as
114988 ** strict. With GROUP BY and DISTINCT the only requirement is that
114989 ** equivalent rows appear immediately adjacent to one another. GROUP BY
114990 ** and DISTINCT do not require rows to appear in any particular order as long
114991 ** as equivelent rows are grouped together. Thus for GROUP BY and DISTINCT
114992 ** the pOrderBy terms can be matched in any order. With ORDER BY, the
114993 ** pOrderBy terms must be matched in strict left-to-right order.
114994 */
114995 static i8 wherePathSatisfiesOrderBy(
@@ -114581,18 +115046,10 @@
115046 ** rowid appears in the ORDER BY clause, the corresponding WhereLoop is
115047 ** automatically order-distinct.
115048 */
115049
115050 assert( pOrderBy!=0 );
 
 
 
 
 
 
 
 
115051 if( nLoop && OptimizationDisabled(db, SQLITE_OrderByIdxJoin) ) return 0;
115052
115053 nOrderBy = pOrderBy->nExpr;
115054 testcase( nOrderBy==BMS-1 );
115055 if( nOrderBy>BMS-1 ) return 0; /* Cannot optimize overly large ORDER BYs */
@@ -114601,11 +115058,14 @@
115058 orderDistinctMask = 0;
115059 ready = 0;
115060 for(iLoop=0; isOrderDistinct && obSat<obDone && iLoop<=nLoop; iLoop++){
115061 if( iLoop>0 ) ready |= pLoop->maskSelf;
115062 pLoop = iLoop<nLoop ? pPath->aLoop[iLoop] : pLast;
115063 if( pLoop->wsFlags & WHERE_VIRTUALTABLE ){
115064 if( pLoop->u.vtab.isOrdered ) obSat = obDone;
115065 break;
115066 }
115067 iCur = pWInfo->pTabList->a[pLoop->iTab].iCursor;
115068
115069 /* Mark off any ORDER BY term X that is a column in the table of
115070 ** the current loop for which there is term in the WHERE
115071 ** clause of the form X IS NULL or X=? that reference only outer
@@ -114689,11 +115149,11 @@
115149 ){
115150 isOrderDistinct = 0;
115151 }
115152
115153 /* Find the ORDER BY term that corresponds to the j-th column
115154 ** of the index and mark that ORDER BY term off
115155 */
115156 bOnce = 1;
115157 isMatch = 0;
115158 for(i=0; bOnce && i<nOrderBy; i++){
115159 if( MASKBIT(i) & obSat ) continue;
@@ -114769,10 +115229,40 @@
115229 return 0;
115230 }
115231 return -1;
115232 }
115233
115234
115235 /*
115236 ** If the WHERE_GROUPBY flag is set in the mask passed to sqlite3WhereBegin(),
115237 ** the planner assumes that the specified pOrderBy list is actually a GROUP
115238 ** BY clause - and so any order that groups rows as required satisfies the
115239 ** request.
115240 **
115241 ** Normally, in this case it is not possible for the caller to determine
115242 ** whether or not the rows are really being delivered in sorted order, or
115243 ** just in some other order that provides the required grouping. However,
115244 ** if the WHERE_SORTBYGROUP flag is also passed to sqlite3WhereBegin(), then
115245 ** this function may be called on the returned WhereInfo object. It returns
115246 ** true if the rows really will be sorted in the specified order, or false
115247 ** otherwise.
115248 **
115249 ** For example, assuming:
115250 **
115251 ** CREATE INDEX i1 ON t1(x, Y);
115252 **
115253 ** then
115254 **
115255 ** SELECT * FROM t1 GROUP BY x,y ORDER BY x,y; -- IsSorted()==1
115256 ** SELECT * FROM t1 GROUP BY y,x ORDER BY y,x; -- IsSorted()==0
115257 */
115258 SQLITE_PRIVATE int sqlite3WhereIsSorted(WhereInfo *pWInfo){
115259 assert( pWInfo->wctrlFlags & WHERE_GROUPBY );
115260 assert( pWInfo->wctrlFlags & WHERE_SORTBYGROUP );
115261 return pWInfo->sorted;
115262 }
115263
115264 #ifdef WHERETRACE_ENABLED
115265 /* For debugging use only: */
115266 static const char *wherePathName(WherePath *pPath, int nLoop, WhereLoop *pLast){
115267 static char zName[65];
115268 int i;
@@ -114780,11 +115270,10 @@
115270 if( pLast ) zName[i++] = pLast->cId;
115271 zName[i] = 0;
115272 return zName;
115273 }
115274 #endif
 
115275
115276 /*
115277 ** Given the list of WhereLoop objects at pWInfo->pLoops, this routine
115278 ** attempts to find the lowest cost path that visits each WhereLoop
115279 ** once. This path is then loaded into the pWInfo->a[].pWLoop fields.
@@ -114879,26 +115368,31 @@
115368 if( isOrdered<0 ){
115369 isOrdered = wherePathSatisfiesOrderBy(pWInfo,
115370 pWInfo->pOrderBy, pFrom, pWInfo->wctrlFlags,
115371 iLoop, pWLoop, &revMask);
115372 if( isOrdered>=0 && isOrdered<nOrderBy ){
115373 /* TUNING: Estimated cost of a full external sort, where N is
115374 ** the number of rows to sort is:
115375 **
115376 ** cost = (3.0 * N * log(N)).
115377 **
115378 ** Or, if the order-by clause has X terms but only the last Y
115379 ** terms are out of order, then block-sorting will reduce the
115380 ** sorting cost to:
115381 **
115382 ** cost = (3.0 * N * log(N)) * (Y/X)
115383 **
115384 ** The (Y/X) term is implemented using stack variable rScale
115385 ** below. */
115386 LogEst rScale, rSortCost;
115387 assert( nOrderBy>0 && 66==sqlite3LogEst(100) );
115388 rScale = sqlite3LogEst((nOrderBy-isOrdered)*100/nOrderBy) - 66;
115389 rSortCost = nRowEst + estLog(nRowEst) + rScale + 16;
115390
115391 /* TUNING: The cost of implementing DISTINCT using a B-TREE is
115392 ** similar but with a larger constant of proportionality.
115393 ** Multiply by an additional factor of 3.0. */
115394 if( pWInfo->wctrlFlags & WHERE_WANT_DISTINCT ){
115395 rSortCost += 16;
115396 }
115397 WHERETRACE(0x002,
115398 ("---- sort cost=%-3d (%d/%d) increases cost %3d to %-3d\n",
@@ -115062,11 +115556,23 @@
115556 }else{
115557 pWInfo->nOBSat = pFrom->isOrdered;
115558 if( pWInfo->nOBSat<0 ) pWInfo->nOBSat = 0;
115559 pWInfo->revMask = pFrom->revLoop;
115560 }
115561 if( (pWInfo->wctrlFlags & WHERE_SORTBYGROUP)
115562 && pWInfo->nOBSat==pWInfo->pOrderBy->nExpr
115563 ){
115564 Bitmask notUsed = 0;
115565 int nOrder = wherePathSatisfiesOrderBy(pWInfo, pWInfo->pOrderBy,
115566 pFrom, 0, nLoop-1, pFrom->aLoop[nLoop-1], &notUsed
115567 );
115568 assert( pWInfo->sorted==0 );
115569 pWInfo->sorted = (nOrder==pWInfo->pOrderBy->nExpr);
115570 }
115571 }
115572
115573
115574 pWInfo->nRowOut = pFrom->nRow;
115575
115576 /* Free temporary memory and return success */
115577 sqlite3DbFree(db, pSpace);
115578 return SQLITE_OK;
@@ -115587,11 +116093,18 @@
116093 Index *pIx = pLoop->u.btree.pIndex;
116094 int iIndexCur;
116095 int op = OP_OpenRead;
116096 /* iIdxCur is always set if to a positive value if ONEPASS is possible */
116097 assert( iIdxCur!=0 || (pWInfo->wctrlFlags & WHERE_ONEPASS_DESIRED)==0 );
116098 if( !HasRowid(pTab) && IsPrimaryKeyIndex(pIx)
116099 && (wctrlFlags & WHERE_ONETABLE_ONLY)!=0
116100 ){
116101 /* This is one term of an OR-optimization using the PRIMARY KEY of a
116102 ** WITHOUT ROWID table. No need for a separate index */
116103 iIndexCur = pLevel->iTabCur;
116104 op = 0;
116105 }else if( pWInfo->okOnePass ){
116106 Index *pJ = pTabItem->pTab->pIndex;
116107 iIndexCur = iIdxCur;
116108 assert( wctrlFlags & WHERE_ONEPASS_DESIRED );
116109 while( ALWAYS(pJ) && pJ!=pIx ){
116110 iIndexCur++;
@@ -115605,13 +116118,15 @@
116118 iIndexCur = pParse->nTab++;
116119 }
116120 pLevel->iIdxCur = iIndexCur;
116121 assert( pIx->pSchema==pTab->pSchema );
116122 assert( iIndexCur>=0 );
116123 if( op ){
116124 sqlite3VdbeAddOp3(v, op, iIndexCur, pIx->tnum, iDb);
116125 sqlite3VdbeSetP4KeyInfo(pParse, pIx);
116126 VdbeComment((v, "%s", pIx->zName));
116127 }
116128 }
116129 if( iDb>=0 ) sqlite3CodeVerifySchema(pParse, iDb);
116130 notReady &= ~getMask(&pWInfo->sMaskSet, pTabItem->iCursor);
116131 }
116132 pWInfo->iTop = sqlite3VdbeCurrentAddr(v);
@@ -123654,10 +124169,32 @@
124169 int sz = va_arg(ap, int);
124170 int *aProg = va_arg(ap, int*);
124171 rc = sqlite3BitvecBuiltinTest(sz, aProg);
124172 break;
124173 }
124174
124175 /*
124176 ** sqlite3_test_control(FAULT_INSTALL, xCallback)
124177 **
124178 ** Arrange to invoke xCallback() whenever sqlite3FaultSim() is called,
124179 ** if xCallback is not NULL.
124180 **
124181 ** As a test of the fault simulator mechanism itself, sqlite3FaultSim(0)
124182 ** is called immediately after installing the new callback and the return
124183 ** value from sqlite3FaultSim(0) becomes the return from
124184 ** sqlite3_test_control().
124185 */
124186 case SQLITE_TESTCTRL_FAULT_INSTALL: {
124187 /* MSVC is picky about pulling func ptrs from va lists.
124188 ** http://support.microsoft.com/kb/47961
124189 ** sqlite3Config.xTestCallback = va_arg(ap, int(*)(int));
124190 */
124191 typedef int(*TESTCALLBACKFUNC_t)(int);
124192 sqlite3Config.xTestCallback = va_arg(ap, TESTCALLBACKFUNC_t);
124193 rc = sqlite3FaultSim(0);
124194 break;
124195 }
124196
124197 /*
124198 ** sqlite3_test_control(BENIGN_MALLOC_HOOKS, xBegin, xEnd)
124199 **
124200 ** Register hooks to call to indicate which malloc() failures
@@ -123964,11 +124501,11 @@
124501 ** Return 1 if database is read-only or 0 if read/write. Return -1 if
124502 ** no such database exists.
124503 */
124504 SQLITE_API int sqlite3_db_readonly(sqlite3 *db, const char *zDbName){
124505 Btree *pBt = sqlite3DbNameToBtree(db, zDbName);
124506 return pBt ? sqlite3BtreeIsReadonly(pBt) : -1;
124507 }
124508
124509 /************** End of main.c ************************************************/
124510 /************** Begin file notify.c ******************************************/
124511 /*
@@ -125084,17 +125621,17 @@
125621 char **azColumn; /* column names. malloced */
125622 u8 *abNotindexed; /* True for 'notindexed' columns */
125623 sqlite3_tokenizer *pTokenizer; /* tokenizer for inserts and queries */
125624 char *zContentTbl; /* content=xxx option, or NULL */
125625 char *zLanguageid; /* languageid=xxx option, or NULL */
125626 int nAutoincrmerge; /* Value configured by 'automerge' */
125627 u32 nLeafAdd; /* Number of leaf blocks added this trans */
125628
125629 /* Precompiled statements used by the implementation. Each of these
125630 ** statements is run and reset within a single virtual table API call.
125631 */
125632 sqlite3_stmt *aStmt[40];
125633
125634 char *zReadExprlist;
125635 char *zWriteExprlist;
125636
125637 int nNodeSize; /* Soft limit for node size */
@@ -126512,11 +127049,11 @@
127049 p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
127050 p->bHasDocsize = (isFts4 && bNoDocsize==0);
127051 p->bHasStat = isFts4;
127052 p->bFts4 = isFts4;
127053 p->bDescIdx = bDescIdx;
127054 p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
127055 p->zContentTbl = zContent;
127056 p->zLanguageid = zLanguageid;
127057 zContent = 0;
127058 zLanguageid = 0;
127059 TESTONLY( p->inTransaction = -1 );
@@ -126555,11 +127092,13 @@
127092 /* Fill in the abNotindexed array */
127093 for(iCol=0; iCol<nCol; iCol++){
127094 int n = (int)strlen(p->azColumn[iCol]);
127095 for(i=0; i<nNotindexed; i++){
127096 char *zNot = azNotindexed[i];
127097 if( zNot && n==(int)strlen(zNot)
127098 && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
127099 ){
127100 p->abNotindexed[iCol] = 1;
127101 sqlite3_free(zNot);
127102 azNotindexed[i] = 0;
127103 }
127104 }
@@ -128481,19 +129020,22 @@
129020 const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
129021
129022 Fts3Table *p = (Fts3Table*)pVtab;
129023 int rc = sqlite3Fts3PendingTermsFlush(p);
129024
129025 if( rc==SQLITE_OK
129026 && p->nLeafAdd>(nMinMerge/16)
129027 && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
129028 ){
129029 int mxLevel = 0; /* Maximum relative level value in db */
129030 int A; /* Incr-merge parameter A */
129031
129032 rc = sqlite3Fts3MaxLevel(p, &mxLevel);
129033 assert( rc==SQLITE_OK || mxLevel==0 );
129034 A = p->nLeafAdd * mxLevel;
129035 A += (A/2);
129036 if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
129037 }
129038 sqlite3Fts3SegmentsClose(p);
129039 return rc;
129040 }
129041
@@ -131712,44 +132254,27 @@
132254 sqlite3_tokenizer *pTokenizer = pParse->pTokenizer;
132255 sqlite3_tokenizer_module const *pModule = pTokenizer->pModule;
132256 int rc;
132257 sqlite3_tokenizer_cursor *pCursor;
132258 Fts3Expr *pRet = 0;
132259 int i = 0;
132260
132261 /* Set variable i to the maximum number of bytes of input to tokenize. */
132262 for(i=0; i<n; i++){
132263 if( sqlite3_fts3_enable_parentheses && (z[i]=='(' || z[i]==')') ) break;
132264 if( z[i]=='*' || z[i]=='"' ) break;
132265 }
132266
132267 *pnConsumed = i;
132268 rc = sqlite3Fts3OpenTokenizer(pTokenizer, pParse->iLangid, z, i, &pCursor);
132269 if( rc==SQLITE_OK ){
132270 const char *zToken;
132271 int nToken = 0, iStart = 0, iEnd = 0, iPosition = 0;
132272 int nByte; /* total space to allocate */
132273
132274 rc = pModule->xNext(pCursor, &zToken, &nToken, &iStart, &iEnd, &iPosition);
132275 if( rc==SQLITE_OK ){
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
132276 nByte = sizeof(Fts3Expr) + sizeof(Fts3Phrase) + nToken;
132277 pRet = (Fts3Expr *)fts3MallocZero(nByte);
132278 if( !pRet ){
132279 rc = SQLITE_NOMEM;
132280 }else{
@@ -131779,17 +132304,18 @@
132304 break;
132305 }
132306 }
132307
132308 }
132309 *pnConsumed = iEnd;
132310 }else if( i && rc==SQLITE_DONE ){
132311 rc = SQLITE_OK;
132312 }
132313
132314 pModule->xClose(pCursor);
132315 }
132316
 
132317 *ppExpr = pRet;
132318 return rc;
132319 }
132320
132321
@@ -132035,10 +132561,25 @@
132561 return SQLITE_ERROR;
132562 }
132563 return getNextString(pParse, &zInput[1], ii-1, ppExpr);
132564 }
132565
132566 if( sqlite3_fts3_enable_parentheses ){
132567 if( *zInput=='(' ){
132568 int nConsumed = 0;
132569 pParse->nNest++;
132570 rc = fts3ExprParse(pParse, zInput+1, nInput-1, ppExpr, &nConsumed);
132571 if( rc==SQLITE_OK && !*ppExpr ){ rc = SQLITE_DONE; }
132572 *pnConsumed = (int)(zInput - z) + 1 + nConsumed;
132573 return rc;
132574 }else if( *zInput==')' ){
132575 pParse->nNest--;
132576 *pnConsumed = (int)((zInput - z) + 1);
132577 *ppExpr = 0;
132578 return SQLITE_DONE;
132579 }
132580 }
132581
132582 /* If control flows to this point, this must be a regular token, or
132583 ** the end of the input. Read a regular token using the sqlite3_tokenizer
132584 ** interface. Before doing so, figure out if there is an explicit
132585 ** column specifier for the token.
@@ -132153,100 +132694,104 @@
132694 int isRequirePhrase = 1;
132695
132696 while( rc==SQLITE_OK ){
132697 Fts3Expr *p = 0;
132698 int nByte = 0;
132699
132700 rc = getNextNode(pParse, zIn, nIn, &p, &nByte);
132701 assert( nByte>0 || (rc!=SQLITE_OK && p==0) );
132702 if( rc==SQLITE_OK ){
132703 if( p ){
132704 int isPhrase;
132705
132706 if( !sqlite3_fts3_enable_parentheses
132707 && p->eType==FTSQUERY_PHRASE && pParse->isNot
132708 ){
132709 /* Create an implicit NOT operator. */
132710 Fts3Expr *pNot = fts3MallocZero(sizeof(Fts3Expr));
132711 if( !pNot ){
132712 sqlite3Fts3ExprFree(p);
132713 rc = SQLITE_NOMEM;
132714 goto exprparse_out;
132715 }
132716 pNot->eType = FTSQUERY_NOT;
132717 pNot->pRight = p;
132718 p->pParent = pNot;
132719 if( pNotBranch ){
132720 pNot->pLeft = pNotBranch;
132721 pNotBranch->pParent = pNot;
132722 }
132723 pNotBranch = pNot;
132724 p = pPrev;
132725 }else{
132726 int eType = p->eType;
132727 isPhrase = (eType==FTSQUERY_PHRASE || p->pLeft);
132728
132729 /* The isRequirePhrase variable is set to true if a phrase or
132730 ** an expression contained in parenthesis is required. If a
132731 ** binary operator (AND, OR, NOT or NEAR) is encounted when
132732 ** isRequirePhrase is set, this is a syntax error.
132733 */
132734 if( !isPhrase && isRequirePhrase ){
132735 sqlite3Fts3ExprFree(p);
132736 rc = SQLITE_ERROR;
132737 goto exprparse_out;
132738 }
132739
132740 if( isPhrase && !isRequirePhrase ){
132741 /* Insert an implicit AND operator. */
132742 Fts3Expr *pAnd;
132743 assert( pRet && pPrev );
132744 pAnd = fts3MallocZero(sizeof(Fts3Expr));
132745 if( !pAnd ){
132746 sqlite3Fts3ExprFree(p);
132747 rc = SQLITE_NOMEM;
132748 goto exprparse_out;
132749 }
132750 pAnd->eType = FTSQUERY_AND;
132751 insertBinaryOperator(&pRet, pPrev, pAnd);
132752 pPrev = pAnd;
132753 }
132754
132755 /* This test catches attempts to make either operand of a NEAR
132756 ** operator something other than a phrase. For example, either of
132757 ** the following:
132758 **
132759 ** (bracketed expression) NEAR phrase
132760 ** phrase NEAR (bracketed expression)
132761 **
132762 ** Return an error in either case.
132763 */
132764 if( pPrev && (
132765 (eType==FTSQUERY_NEAR && !isPhrase && pPrev->eType!=FTSQUERY_PHRASE)
132766 || (eType!=FTSQUERY_PHRASE && isPhrase && pPrev->eType==FTSQUERY_NEAR)
132767 )){
132768 sqlite3Fts3ExprFree(p);
132769 rc = SQLITE_ERROR;
132770 goto exprparse_out;
132771 }
132772
132773 if( isPhrase ){
132774 if( pRet ){
132775 assert( pPrev && pPrev->pLeft && pPrev->pRight==0 );
132776 pPrev->pRight = p;
132777 p->pParent = pPrev;
132778 }else{
132779 pRet = p;
132780 }
132781 }else{
132782 insertBinaryOperator(&pRet, pPrev, p);
132783 }
132784 isRequirePhrase = !isPhrase;
132785 }
132786 pPrev = p;
132787 }
132788 assert( nByte>0 );
132789 }
132790 assert( rc!=SQLITE_OK || (nByte>0 && nByte<=nIn) );
132791 nIn -= nByte;
132792 zIn += nByte;
 
132793 }
132794
132795 if( rc==SQLITE_DONE && pRet && isRequirePhrase ){
132796 rc = SQLITE_ERROR;
132797 }
@@ -135230,10 +135775,11 @@
135775 int nMalloc; /* Size of malloc'd buffer at zMalloc */
135776 char *zMalloc; /* Malloc'd space (possibly) used for zTerm */
135777 int nSize; /* Size of allocation at aData */
135778 int nData; /* Bytes of data in aData */
135779 char *aData; /* Pointer to block from malloc() */
135780 i64 nLeafData; /* Number of bytes of leaf data written */
135781 };
135782
135783 /*
135784 ** Type SegmentNode is used by the following three functions to create
135785 ** the interior part of the segment b+-tree structures (everything except
@@ -135304,10 +135850,14 @@
135850 #define SQL_SELECT_SEGDIR 32
135851 #define SQL_CHOMP_SEGDIR 33
135852 #define SQL_SEGMENT_IS_APPENDABLE 34
135853 #define SQL_SELECT_INDEXES 35
135854 #define SQL_SELECT_MXLEVEL 36
135855
135856 #define SQL_SELECT_LEVEL_RANGE2 37
135857 #define SQL_UPDATE_LEVEL_IDX 38
135858 #define SQL_UPDATE_LEVEL 39
135859
135860 /*
135861 ** This function is used to obtain an SQLite prepared statement handle
135862 ** for the statement identified by the second argument. If successful,
135863 ** *pp is set to the requested statement handle and SQLITE_OK returned.
@@ -135406,11 +135956,22 @@
135956 ** Return the list of valid segment indexes for absolute level ? */
135957 /* 35 */ "SELECT idx FROM %Q.'%q_segdir' WHERE level=? ORDER BY 1 ASC",
135958
135959 /* SQL_SELECT_MXLEVEL
135960 ** Return the largest relative level in the FTS index or indexes. */
135961 /* 36 */ "SELECT max( level %% 1024 ) FROM %Q.'%q_segdir'",
135962
135963 /* Return segments in order from oldest to newest.*/
135964 /* 37 */ "SELECT level, idx, end_block "
135965 "FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ? "
135966 "ORDER BY level DESC, idx ASC",
135967
135968 /* Update statements used while promoting segments */
135969 /* 38 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=-1,idx=? "
135970 "WHERE level=? AND idx=?",
135971 /* 39 */ "UPDATE OR FAIL %Q.'%q_segdir' SET level=? WHERE level=-1"
135972
135973 };
135974 int rc = SQLITE_OK;
135975 sqlite3_stmt *pStmt;
135976
135977 assert( SizeofArray(azSql)==SizeofArray(p->aStmt) );
@@ -136947,10 +137508,11 @@
137508 sqlite3_int64 iLevel, /* Value for "level" field (absolute level) */
137509 int iIdx, /* Value for "idx" field */
137510 sqlite3_int64 iStartBlock, /* Value for "start_block" field */
137511 sqlite3_int64 iLeafEndBlock, /* Value for "leaves_end_block" field */
137512 sqlite3_int64 iEndBlock, /* Value for "end_block" field */
137513 sqlite3_int64 nLeafData, /* Bytes of leaf data in segment */
137514 char *zRoot, /* Blob value for "root" field */
137515 int nRoot /* Number of bytes in buffer zRoot */
137516 ){
137517 sqlite3_stmt *pStmt;
137518 int rc = fts3SqlStmt(p, SQL_INSERT_SEGDIR, &pStmt, 0);
@@ -136957,11 +137519,17 @@
137519 if( rc==SQLITE_OK ){
137520 sqlite3_bind_int64(pStmt, 1, iLevel);
137521 sqlite3_bind_int(pStmt, 2, iIdx);
137522 sqlite3_bind_int64(pStmt, 3, iStartBlock);
137523 sqlite3_bind_int64(pStmt, 4, iLeafEndBlock);
137524 if( nLeafData==0 ){
137525 sqlite3_bind_int64(pStmt, 5, iEndBlock);
137526 }else{
137527 char *zEnd = sqlite3_mprintf("%lld %lld", iEndBlock, nLeafData);
137528 if( !zEnd ) return SQLITE_NOMEM;
137529 sqlite3_bind_text(pStmt, 5, zEnd, -1, sqlite3_free);
137530 }
137531 sqlite3_bind_blob(pStmt, 6, zRoot, nRoot, SQLITE_STATIC);
137532 sqlite3_step(pStmt);
137533 rc = sqlite3_reset(pStmt);
137534 }
137535 return rc;
@@ -137282,10 +137850,13 @@
137850 sqlite3Fts3VarintLen(nTerm) + /* varint containing suffix size */
137851 nTerm + /* Term suffix */
137852 sqlite3Fts3VarintLen(nDoclist) + /* Size of doclist */
137853 nDoclist; /* Doclist data */
137854 }
137855
137856 /* Increase the total number of bytes written to account for the new entry. */
137857 pWriter->nLeafData += nReq;
137858
137859 /* If the buffer currently allocated is too small for this entry, realloc
137860 ** the buffer to make it large enough.
137861 */
137862 if( nReq>pWriter->nSize ){
@@ -137354,17 +137925,17 @@
137925 if( rc==SQLITE_OK ){
137926 rc = fts3NodeWrite(p, pWriter->pTree, 1,
137927 pWriter->iFirst, pWriter->iFree, &iLast, &zRoot, &nRoot);
137928 }
137929 if( rc==SQLITE_OK ){
137930 rc = fts3WriteSegdir(p, iLevel, iIdx,
137931 pWriter->iFirst, iLastLeaf, iLast, pWriter->nLeafData, zRoot, nRoot);
137932 }
137933 }else{
137934 /* The entire tree fits on the root node. Write it to the segdir table. */
137935 rc = fts3WriteSegdir(p, iLevel, iIdx,
137936 0, 0, 0, pWriter->nLeafData, pWriter->aData, pWriter->nData);
137937 }
137938 p->nLeafAdd++;
137939 return rc;
137940 }
137941
@@ -137443,10 +138014,41 @@
138014 if( SQLITE_ROW==sqlite3_step(pStmt) ){
138015 *pnMax = sqlite3_column_int64(pStmt, 0);
138016 }
138017 return sqlite3_reset(pStmt);
138018 }
138019
138020 /*
138021 ** iAbsLevel is an absolute level that may be assumed to exist within
138022 ** the database. This function checks if it is the largest level number
138023 ** within its index. Assuming no error occurs, *pbMax is set to 1 if
138024 ** iAbsLevel is indeed the largest level, or 0 otherwise, and SQLITE_OK
138025 ** is returned. If an error occurs, an error code is returned and the
138026 ** final value of *pbMax is undefined.
138027 */
138028 static int fts3SegmentIsMaxLevel(Fts3Table *p, i64 iAbsLevel, int *pbMax){
138029
138030 /* Set pStmt to the compiled version of:
138031 **
138032 ** SELECT max(level) FROM %Q.'%q_segdir' WHERE level BETWEEN ? AND ?
138033 **
138034 ** (1024 is actually the value of macro FTS3_SEGDIR_PREFIXLEVEL_STR).
138035 */
138036 sqlite3_stmt *pStmt;
138037 int rc = fts3SqlStmt(p, SQL_SELECT_SEGDIR_MAX_LEVEL, &pStmt, 0);
138038 if( rc!=SQLITE_OK ) return rc;
138039 sqlite3_bind_int64(pStmt, 1, iAbsLevel+1);
138040 sqlite3_bind_int64(pStmt, 2,
138041 ((iAbsLevel/FTS3_SEGDIR_MAXLEVEL)+1) * FTS3_SEGDIR_MAXLEVEL
138042 );
138043
138044 *pbMax = 0;
138045 if( SQLITE_ROW==sqlite3_step(pStmt) ){
138046 *pbMax = sqlite3_column_type(pStmt, 0)==SQLITE_NULL;
138047 }
138048 return sqlite3_reset(pStmt);
138049 }
138050
138051 /*
138052 ** Delete all entries in the %_segments table associated with the segment
138053 ** opened with seg-reader pSeg. This function does not affect the contents
138054 ** of the %_segdir table.
@@ -137978,10 +138580,144 @@
138580 pCsr->nSegment = 0;
138581 pCsr->apSegment = 0;
138582 pCsr->aBuffer = 0;
138583 }
138584 }
138585
138586 /*
138587 ** Decode the "end_block" field, selected by column iCol of the SELECT
138588 ** statement passed as the first argument.
138589 **
138590 ** The "end_block" field may contain either an integer, or a text field
138591 ** containing the text representation of two non-negative integers separated
138592 ** by one or more space (0x20) characters. In the first case, set *piEndBlock
138593 ** to the integer value and *pnByte to zero before returning. In the second,
138594 ** set *piEndBlock to the first value and *pnByte to the second.
138595 */
138596 static void fts3ReadEndBlockField(
138597 sqlite3_stmt *pStmt,
138598 int iCol,
138599 i64 *piEndBlock,
138600 i64 *pnByte
138601 ){
138602 const unsigned char *zText = sqlite3_column_text(pStmt, iCol);
138603 if( zText ){
138604 int i;
138605 int iMul = 1;
138606 i64 iVal = 0;
138607 for(i=0; zText[i]>='0' && zText[i]<='9'; i++){
138608 iVal = iVal*10 + (zText[i] - '0');
138609 }
138610 *piEndBlock = iVal;
138611 while( zText[i]==' ' ) i++;
138612 iVal = 0;
138613 if( zText[i]=='-' ){
138614 i++;
138615 iMul = -1;
138616 }
138617 for(/* no-op */; zText[i]>='0' && zText[i]<='9'; i++){
138618 iVal = iVal*10 + (zText[i] - '0');
138619 }
138620 *pnByte = (iVal * (i64)iMul);
138621 }
138622 }
138623
138624
138625 /*
138626 ** A segment of size nByte bytes has just been written to absolute level
138627 ** iAbsLevel. Promote any segments that should be promoted as a result.
138628 */
138629 static int fts3PromoteSegments(
138630 Fts3Table *p, /* FTS table handle */
138631 sqlite3_int64 iAbsLevel, /* Absolute level just updated */
138632 sqlite3_int64 nByte /* Size of new segment at iAbsLevel */
138633 ){
138634 int rc = SQLITE_OK;
138635 sqlite3_stmt *pRange;
138636
138637 rc = fts3SqlStmt(p, SQL_SELECT_LEVEL_RANGE2, &pRange, 0);
138638
138639 if( rc==SQLITE_OK ){
138640 int bOk = 0;
138641 i64 iLast = (iAbsLevel/FTS3_SEGDIR_MAXLEVEL + 1) * FTS3_SEGDIR_MAXLEVEL - 1;
138642 i64 nLimit = (nByte*3)/2;
138643
138644 /* Loop through all entries in the %_segdir table corresponding to
138645 ** segments in this index on levels greater than iAbsLevel. If there is
138646 ** at least one such segment, and it is possible to determine that all
138647 ** such segments are smaller than nLimit bytes in size, they will be
138648 ** promoted to level iAbsLevel. */
138649 sqlite3_bind_int64(pRange, 1, iAbsLevel+1);
138650 sqlite3_bind_int64(pRange, 2, iLast);
138651 while( SQLITE_ROW==sqlite3_step(pRange) ){
138652 i64 nSize = 0, dummy;
138653 fts3ReadEndBlockField(pRange, 2, &dummy, &nSize);
138654 if( nSize<=0 || nSize>nLimit ){
138655 /* If nSize==0, then the %_segdir.end_block field does not not
138656 ** contain a size value. This happens if it was written by an
138657 ** old version of FTS. In this case it is not possible to determine
138658 ** the size of the segment, and so segment promotion does not
138659 ** take place. */
138660 bOk = 0;
138661 break;
138662 }
138663 bOk = 1;
138664 }
138665 rc = sqlite3_reset(pRange);
138666
138667 if( bOk ){
138668 int iIdx = 0;
138669 sqlite3_stmt *pUpdate1;
138670 sqlite3_stmt *pUpdate2;
138671
138672 if( rc==SQLITE_OK ){
138673 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL_IDX, &pUpdate1, 0);
138674 }
138675 if( rc==SQLITE_OK ){
138676 rc = fts3SqlStmt(p, SQL_UPDATE_LEVEL, &pUpdate2, 0);
138677 }
138678
138679 if( rc==SQLITE_OK ){
138680
138681 /* Loop through all %_segdir entries for segments in this index with
138682 ** levels equal to or greater than iAbsLevel. As each entry is visited,
138683 ** updated it to set (level = -1) and (idx = N), where N is 0 for the
138684 ** oldest segment in the range, 1 for the next oldest, and so on.
138685 **
138686 ** In other words, move all segments being promoted to level -1,
138687 ** setting the "idx" fields as appropriate to keep them in the same
138688 ** order. The contents of level -1 (which is never used, except
138689 ** transiently here), will be moved back to level iAbsLevel below. */
138690 sqlite3_bind_int64(pRange, 1, iAbsLevel);
138691 while( SQLITE_ROW==sqlite3_step(pRange) ){
138692 sqlite3_bind_int(pUpdate1, 1, iIdx++);
138693 sqlite3_bind_int(pUpdate1, 2, sqlite3_column_int(pRange, 0));
138694 sqlite3_bind_int(pUpdate1, 3, sqlite3_column_int(pRange, 1));
138695 sqlite3_step(pUpdate1);
138696 rc = sqlite3_reset(pUpdate1);
138697 if( rc!=SQLITE_OK ){
138698 sqlite3_reset(pRange);
138699 break;
138700 }
138701 }
138702 }
138703 if( rc==SQLITE_OK ){
138704 rc = sqlite3_reset(pRange);
138705 }
138706
138707 /* Move level -1 to level iAbsLevel */
138708 if( rc==SQLITE_OK ){
138709 sqlite3_bind_int64(pUpdate2, 1, iAbsLevel);
138710 sqlite3_step(pUpdate2);
138711 rc = sqlite3_reset(pUpdate2);
138712 }
138713 }
138714 }
138715
138716
138717 return rc;
138718 }
138719
138720 /*
138721 ** Merge all level iLevel segments in the database into a single
138722 ** iLevel+1 segment. Or, if iLevel<0, merge all segments into a
138723 ** single segment with a level equal to the numerically largest level
@@ -138003,10 +138739,11 @@
138739 sqlite3_int64 iNewLevel = 0; /* Level/index to create new segment at */
138740 SegmentWriter *pWriter = 0; /* Used to write the new, merged, segment */
138741 Fts3SegFilter filter; /* Segment term filter condition */
138742 Fts3MultiSegReader csr; /* Cursor to iterate through level(s) */
138743 int bIgnoreEmpty = 0; /* True to ignore empty segments */
138744 i64 iMaxLevel = 0; /* Max level number for this index/langid */
138745
138746 assert( iLevel==FTS3_SEGCURSOR_ALL
138747 || iLevel==FTS3_SEGCURSOR_PENDING
138748 || iLevel>=0
138749 );
@@ -138013,10 +138750,15 @@
138750 assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
138751 assert( iIndex>=0 && iIndex<p->nIndex );
138752
138753 rc = sqlite3Fts3SegReaderCursor(p, iLangid, iIndex, iLevel, 0, 0, 1, 0, &csr);
138754 if( rc!=SQLITE_OK || csr.nSegment==0 ) goto finished;
138755
138756 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
138757 rc = fts3SegmentMaxLevel(p, iLangid, iIndex, &iMaxLevel);
138758 if( rc!=SQLITE_OK ) goto finished;
138759 }
138760
138761 if( iLevel==FTS3_SEGCURSOR_ALL ){
138762 /* This call is to merge all segments in the database to a single
138763 ** segment. The level of the new segment is equal to the numerically
138764 ** greatest segment level currently present in the database for this
@@ -138023,25 +138765,25 @@
138765 ** index. The idx of the new segment is always 0. */
138766 if( csr.nSegment==1 ){
138767 rc = SQLITE_DONE;
138768 goto finished;
138769 }
138770 iNewLevel = iMaxLevel;
138771 bIgnoreEmpty = 1;
138772
 
 
 
138773 }else{
138774 /* This call is to merge all segments at level iLevel. find the next
138775 ** available segment index at level iLevel+1. The call to
138776 ** fts3AllocateSegdirIdx() will merge the segments at level iLevel+1 to
138777 ** a single iLevel+2 segment if necessary. */
138778 assert( FTS3_SEGCURSOR_PENDING==-1 );
138779 iNewLevel = getAbsoluteLevel(p, iLangid, iIndex, iLevel+1);
138780 rc = fts3AllocateSegdirIdx(p, iLangid, iIndex, iLevel+1, &iIdx);
138781 bIgnoreEmpty = (iLevel!=FTS3_SEGCURSOR_PENDING) && (iNewLevel>iMaxLevel);
138782 }
138783 if( rc!=SQLITE_OK ) goto finished;
138784
138785 assert( csr.nSegment>0 );
138786 assert( iNewLevel>=getAbsoluteLevel(p, iLangid, iIndex, 0) );
138787 assert( iNewLevel<getAbsoluteLevel(p, iLangid, iIndex,FTS3_SEGDIR_MAXLEVEL) );
138788
138789 memset(&filter, 0, sizeof(Fts3SegFilter));
@@ -138054,29 +138796,36 @@
138796 if( rc!=SQLITE_ROW ) break;
138797 rc = fts3SegWriterAdd(p, &pWriter, 1,
138798 csr.zTerm, csr.nTerm, csr.aDoclist, csr.nDoclist);
138799 }
138800 if( rc!=SQLITE_OK ) goto finished;
138801 assert( pWriter || bIgnoreEmpty );
138802
138803 if( iLevel!=FTS3_SEGCURSOR_PENDING ){
138804 rc = fts3DeleteSegdir(
138805 p, iLangid, iIndex, iLevel, csr.apSegment, csr.nSegment
138806 );
138807 if( rc!=SQLITE_OK ) goto finished;
138808 }
138809 if( pWriter ){
138810 rc = fts3SegWriterFlush(p, pWriter, iNewLevel, iIdx);
138811 if( rc==SQLITE_OK ){
138812 if( iLevel==FTS3_SEGCURSOR_PENDING || iNewLevel<iMaxLevel ){
138813 rc = fts3PromoteSegments(p, iNewLevel, pWriter->nLeafData);
138814 }
138815 }
138816 }
138817
138818 finished:
138819 fts3SegWriterFree(pWriter);
138820 sqlite3Fts3SegReaderFinish(&csr);
138821 return rc;
138822 }
138823
138824
138825 /*
138826 ** Flush the contents of pendingTerms to level 0 segments.
138827 */
138828 SQLITE_PRIVATE int sqlite3Fts3PendingTermsFlush(Fts3Table *p){
138829 int rc = SQLITE_OK;
138830 int i;
138831
@@ -138088,18 +138837,23 @@
138837
138838 /* Determine the auto-incr-merge setting if unknown. If enabled,
138839 ** estimate the number of leaf blocks of content to be written
138840 */
138841 if( rc==SQLITE_OK && p->bHasStat
138842 && p->nAutoincrmerge==0xff && p->nLeafAdd>0
138843 ){
138844 sqlite3_stmt *pStmt = 0;
138845 rc = fts3SqlStmt(p, SQL_SELECT_STAT, &pStmt, 0);
138846 if( rc==SQLITE_OK ){
138847 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
138848 rc = sqlite3_step(pStmt);
138849 if( rc==SQLITE_ROW ){
138850 p->nAutoincrmerge = sqlite3_column_int(pStmt, 0);
138851 if( p->nAutoincrmerge==1 ) p->nAutoincrmerge = 8;
138852 }else if( rc==SQLITE_DONE ){
138853 p->nAutoincrmerge = 0;
138854 }
138855 rc = sqlite3_reset(pStmt);
138856 }
138857 }
138858 return rc;
138859 }
@@ -138463,10 +139217,12 @@
139217 int nWork; /* Number of leaf pages flushed */
139218 sqlite3_int64 iAbsLevel; /* Absolute level of input segments */
139219 int iIdx; /* Index of *output* segment in iAbsLevel+1 */
139220 sqlite3_int64 iStart; /* Block number of first allocated block */
139221 sqlite3_int64 iEnd; /* Block number of last allocated block */
139222 sqlite3_int64 nLeafData; /* Bytes of leaf page data so far */
139223 u8 bNoLeafData; /* If true, store 0 for segment size */
139224 NodeWriter aNodeWriter[FTS_MAX_APPENDABLE_HEIGHT];
139225 };
139226
139227 /*
139228 ** An object of the following type is used to read data from a single
@@ -138801,12 +139557,12 @@
139557 nSpace = 1;
139558 nSpace += sqlite3Fts3VarintLen(nSuffix) + nSuffix;
139559 nSpace += sqlite3Fts3VarintLen(nDoclist) + nDoclist;
139560 }
139561
139562 pWriter->nLeafData += nSpace;
139563 blobGrowBuffer(&pLeaf->block, pLeaf->block.n + nSpace, &rc);
 
139564 if( rc==SQLITE_OK ){
139565 if( pLeaf->block.n==0 ){
139566 pLeaf->block.n = 1;
139567 pLeaf->block.a[0] = '\0';
139568 }
@@ -138901,10 +139657,11 @@
139657 pWriter->iAbsLevel+1, /* level */
139658 pWriter->iIdx, /* idx */
139659 pWriter->iStart, /* start_block */
139660 pWriter->aNodeWriter[0].iBlock, /* leaves_end_block */
139661 pWriter->iEnd, /* end_block */
139662 (pWriter->bNoLeafData==0 ? pWriter->nLeafData : 0), /* end_block */
139663 pRoot->block.a, pRoot->block.n /* root */
139664 );
139665 }
139666 sqlite3_free(pRoot->block.a);
139667 sqlite3_free(pRoot->key.a);
@@ -139002,11 +139759,15 @@
139759 sqlite3_bind_int64(pSelect, 1, iAbsLevel+1);
139760 sqlite3_bind_int(pSelect, 2, iIdx);
139761 if( sqlite3_step(pSelect)==SQLITE_ROW ){
139762 iStart = sqlite3_column_int64(pSelect, 1);
139763 iLeafEnd = sqlite3_column_int64(pSelect, 2);
139764 fts3ReadEndBlockField(pSelect, 3, &iEnd, &pWriter->nLeafData);
139765 if( pWriter->nLeafData<0 ){
139766 pWriter->nLeafData = pWriter->nLeafData * -1;
139767 }
139768 pWriter->bNoLeafData = (pWriter->nLeafData==0);
139769 nRoot = sqlite3_column_bytes(pSelect, 4);
139770 aRoot = sqlite3_column_blob(pSelect, 4);
139771 }else{
139772 return sqlite3_reset(pSelect);
139773 }
@@ -139603,15 +140364,15 @@
140364
140365
140366 /*
140367 ** Attempt an incremental merge that writes nMerge leaf blocks.
140368 **
140369 ** Incremental merges happen nMin segments at a time. The segments
140370 ** to be merged are the nMin oldest segments (the ones with the smallest
140371 ** values for the _segdir.idx field) in the highest level that contains
140372 ** at least nMin segments. Multiple merges might occur in an attempt to
140373 ** write the quota of nMerge leaf blocks.
140374 */
140375 SQLITE_PRIVATE int sqlite3Fts3Incrmerge(Fts3Table *p, int nMerge, int nMin){
140376 int rc; /* Return code */
140377 int nRem = nMerge; /* Number of leaf pages yet to be written */
140378 Fts3MultiSegReader *pCsr; /* Cursor used to read input data */
@@ -139632,10 +140393,11 @@
140393 rc = fts3IncrmergeHintLoad(p, &hint);
140394 while( rc==SQLITE_OK && nRem>0 ){
140395 const i64 nMod = FTS3_SEGDIR_MAXLEVEL * p->nIndex;
140396 sqlite3_stmt *pFindLevel = 0; /* SQL used to determine iAbsLevel */
140397 int bUseHint = 0; /* True if attempting to append */
140398 int iIdx = 0; /* Largest idx in level (iAbsLevel+1) */
140399
140400 /* Search the %_segdir table for the absolute level with the smallest
140401 ** relative level number that contains at least nMin segments, if any.
140402 ** If one is found, set iAbsLevel to the absolute level number and
140403 ** nSeg to nMin. If no level with at least nMin segments can be found,
@@ -139685,27 +140447,36 @@
140447 ** segments available in level iAbsLevel. In this case, no work is
140448 ** done on iAbsLevel - fall through to the next iteration of the loop
140449 ** to start work on some other level. */
140450 memset(pWriter, 0, nAlloc);
140451 pFilter->flags = FTS3_SEGMENT_REQUIRE_POS;
140452
140453 if( rc==SQLITE_OK ){
140454 rc = fts3IncrmergeOutputIdx(p, iAbsLevel, &iIdx);
140455 assert( bUseHint==1 || bUseHint==0 );
140456 if( iIdx==0 || (bUseHint && iIdx==1) ){
140457 int bIgnore = 0;
140458 rc = fts3SegmentIsMaxLevel(p, iAbsLevel+1, &bIgnore);
140459 if( bIgnore ){
140460 pFilter->flags |= FTS3_SEGMENT_IGNORE_EMPTY;
140461 }
140462 }
140463 }
140464
140465 if( rc==SQLITE_OK ){
140466 rc = fts3IncrmergeCsr(p, iAbsLevel, nSeg, pCsr);
140467 }
140468 if( SQLITE_OK==rc && pCsr->nSegment==nSeg
140469 && SQLITE_OK==(rc = sqlite3Fts3SegReaderStart(p, pCsr, pFilter))
140470 && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pCsr))
140471 ){
140472 if( bUseHint && iIdx>0 ){
140473 const char *zKey = pCsr->zTerm;
140474 int nKey = pCsr->nTerm;
140475 rc = fts3IncrmergeLoad(p, iAbsLevel, iIdx-1, zKey, nKey, pWriter);
140476 }else{
140477 rc = fts3IncrmergeWriter(p, iAbsLevel, iIdx, pCsr, pWriter);
 
 
 
 
140478 }
140479
140480 if( rc==SQLITE_OK && pWriter->nLeafEst ){
140481 fts3LogMerge(nSeg, iAbsLevel);
140482 do {
@@ -139723,11 +140494,17 @@
140494 fts3IncrmergeHintPush(&hint, iAbsLevel, nSeg, &rc);
140495 }
140496 }
140497 }
140498
140499 if( nSeg!=0 ){
140500 pWriter->nLeafData = pWriter->nLeafData * -1;
140501 }
140502 fts3IncrmergeRelease(p, pWriter, &rc);
140503 if( nSeg==0 && pWriter->bNoLeafData==0 ){
140504 fts3PromoteSegments(p, iAbsLevel+1, pWriter->nLeafData);
140505 }
140506 }
140507
140508 sqlite3Fts3SegReaderFinish(pCsr);
140509 }
140510
@@ -139810,20 +140587,23 @@
140587 Fts3Table *p, /* FTS3 table handle */
140588 const char *zParam /* Nul-terminated string containing boolean */
140589 ){
140590 int rc = SQLITE_OK;
140591 sqlite3_stmt *pStmt = 0;
140592 p->nAutoincrmerge = fts3Getint(&zParam);
140593 if( p->nAutoincrmerge==1 || p->nAutoincrmerge>FTS3_MERGE_COUNT ){
140594 p->nAutoincrmerge = 8;
140595 }
140596 if( !p->bHasStat ){
140597 assert( p->bFts4==0 );
140598 sqlite3Fts3CreateStatTable(&rc, p);
140599 if( rc ) return rc;
140600 }
140601 rc = fts3SqlStmt(p, SQL_REPLACE_STAT, &pStmt, 0);
140602 if( rc ) return rc;
140603 sqlite3_bind_int(pStmt, 1, FTS_STAT_AUTOINCRMERGE);
140604 sqlite3_bind_int(pStmt, 2, p->nAutoincrmerge);
140605 sqlite3_step(pStmt);
140606 rc = sqlite3_reset(pStmt);
140607 return rc;
140608 }
140609
@@ -142802,64 +143582,24 @@
143582 ** child page.
143583 */
143584
143585 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_RTREE)
143586
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143587 #ifndef SQLITE_CORE
143588 SQLITE_EXTENSION_INIT1
143589 #else
143590 #endif
143591
143592 /* #include <string.h> */
143593 /* #include <assert.h> */
143594 /* #include <stdio.h> */
143595
143596 #ifndef SQLITE_AMALGAMATION
143597 #include "sqlite3rtree.h"
143598 typedef sqlite3_int64 i64;
143599 typedef unsigned char u8;
143600 typedef unsigned short u16;
143601 typedef unsigned int u32;
143602 #endif
143603
143604 /* The following macro is used to suppress compiler warnings.
143605 */
@@ -142873,19 +143613,20 @@
143613 typedef struct RtreeCell RtreeCell;
143614 typedef struct RtreeConstraint RtreeConstraint;
143615 typedef struct RtreeMatchArg RtreeMatchArg;
143616 typedef struct RtreeGeomCallback RtreeGeomCallback;
143617 typedef union RtreeCoord RtreeCoord;
143618 typedef struct RtreeSearchPoint RtreeSearchPoint;
143619
143620 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
143621 #define RTREE_MAX_DIMENSIONS 5
143622
143623 /* Size of hash table Rtree.aHash. This hash table is not expected to
143624 ** ever contain very many entries, so a fixed number of buckets is
143625 ** used.
143626 */
143627 #define HASHSIZE 97
143628
143629 /* The xBestIndex method of this virtual table requires an estimate of
143630 ** the number of rows in the virtual table to calculate the costs of
143631 ** various strategies. If possible, this estimate is loaded from the
143632 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
@@ -142897,19 +143638,19 @@
143638
143639 /*
143640 ** An rtree virtual-table object.
143641 */
143642 struct Rtree {
143643 sqlite3_vtab base; /* Base class. Must be first */
143644 sqlite3 *db; /* Host database connection */
143645 int iNodeSize; /* Size in bytes of each node in the node table */
143646 u8 nDim; /* Number of dimensions */
143647 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
143648 u8 nBytesPerCell; /* Bytes consumed per cell */
143649 int iDepth; /* Current depth of the r-tree structure */
143650 char *zDb; /* Name of database containing r-tree table */
143651 char *zName; /* Name of r-tree table */
 
143652 int nBusy; /* Current number of users of this structure */
143653 i64 nRowEst; /* Estimated number of rows in this table */
143654
143655 /* List of nodes removed during a CondenseTree operation. List is
143656 ** linked together via the pointer normally used for hash chains -
@@ -142932,14 +143673,14 @@
143673 /* Statements to read/write/delete a record from xxx_parent */
143674 sqlite3_stmt *pReadParent;
143675 sqlite3_stmt *pWriteParent;
143676 sqlite3_stmt *pDeleteParent;
143677
143678 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
143679 };
143680
143681 /* Possible values for Rtree.eCoordType: */
143682 #define RTREE_COORD_REAL32 0
143683 #define RTREE_COORD_INT32 1
143684
143685 /*
143686 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
@@ -142947,14 +143688,33 @@
143688 ** will be done.
143689 */
143690 #ifdef SQLITE_RTREE_INT_ONLY
143691 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
143692 typedef int RtreeValue; /* Low accuracy coordinate */
143693 # define RTREE_ZERO 0
143694 #else
143695 typedef double RtreeDValue; /* High accuracy coordinate */
143696 typedef float RtreeValue; /* Low accuracy coordinate */
143697 # define RTREE_ZERO 0.0
143698 #endif
143699
143700 /*
143701 ** When doing a search of an r-tree, instances of the following structure
143702 ** record intermediate results from the tree walk.
143703 **
143704 ** The id is always a node-id. For iLevel>=1 the id is the node-id of
143705 ** the node that the RtreeSearchPoint represents. When iLevel==0, however,
143706 ** the id is of the parent node and the cell that RtreeSearchPoint
143707 ** represents is the iCell-th entry in the parent node.
143708 */
143709 struct RtreeSearchPoint {
143710 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
143711 sqlite3_int64 id; /* Node ID */
143712 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
143713 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
143714 u8 iCell; /* Cell index within the node */
143715 };
143716
143717 /*
143718 ** The minimum number of cells allowed for a node is a third of the
143719 ** maximum. In Gutman's notation:
143720 **
@@ -142974,25 +143734,48 @@
143734 ** 2^40 is greater than 2^64, an r-tree structure always has a depth of
143735 ** 40 or less.
143736 */
143737 #define RTREE_MAX_DEPTH 40
143738
143739
143740 /*
143741 ** Number of entries in the cursor RtreeNode cache. The first entry is
143742 ** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
143743 ** entries cache the RtreeNode for the first elements of the priority queue.
143744 */
143745 #define RTREE_CACHE_SZ 5
143746
143747 /*
143748 ** An rtree cursor object.
143749 */
143750 struct RtreeCursor {
143751 sqlite3_vtab_cursor base; /* Base class. Must be first */
143752 u8 atEOF; /* True if at end of search */
143753 u8 bPoint; /* True if sPoint is valid */
143754 int iStrategy; /* Copy of idxNum search parameter */
143755 int nConstraint; /* Number of entries in aConstraint */
143756 RtreeConstraint *aConstraint; /* Search constraints. */
143757 int nPointAlloc; /* Number of slots allocated for aPoint[] */
143758 int nPoint; /* Number of slots used in aPoint[] */
143759 int mxLevel; /* iLevel value for root of the tree */
143760 RtreeSearchPoint *aPoint; /* Priority queue for search points */
143761 RtreeSearchPoint sPoint; /* Cached next search point */
143762 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
143763 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
143764 };
143765
143766 /* Return the Rtree of a RtreeCursor */
143767 #define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
143768
143769 /*
143770 ** A coordinate can be either a floating point number or a integer. All
143771 ** coordinates within a single R-Tree are always of the same time.
143772 */
143773 union RtreeCoord {
143774 RtreeValue f; /* Floating point value */
143775 int i; /* Integer value */
143776 u32 u; /* Unsigned for byte-order conversions */
143777 };
143778
143779 /*
143780 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
143781 ** formatted as a RtreeDValue (double or int64). This macro assumes that local
@@ -143013,42 +143796,71 @@
143796 ** A search constraint.
143797 */
143798 struct RtreeConstraint {
143799 int iCoord; /* Index of constrained coordinate */
143800 int op; /* Constraining operation */
143801 union {
143802 RtreeDValue rValue; /* Constraint value. */
143803 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
143804 int (*xQueryFunc)(sqlite3_rtree_query_info*);
143805 } u;
143806 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
143807 };
143808
143809 /* Possible values for RtreeConstraint.op */
143810 #define RTREE_EQ 0x41 /* A */
143811 #define RTREE_LE 0x42 /* B */
143812 #define RTREE_LT 0x43 /* C */
143813 #define RTREE_GE 0x44 /* D */
143814 #define RTREE_GT 0x45 /* E */
143815 #define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
143816 #define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
143817
143818
143819 /*
143820 ** An rtree structure node.
143821 */
143822 struct RtreeNode {
143823 RtreeNode *pParent; /* Parent node */
143824 i64 iNode; /* The node number */
143825 int nRef; /* Number of references to this node */
143826 int isDirty; /* True if the node needs to be written to disk */
143827 u8 *zData; /* Content of the node, as should be on disk */
143828 RtreeNode *pNext; /* Next node in this hash collision chain */
143829 };
143830
143831 /* Return the number of cells in a node */
143832 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
143833
143834 /*
143835 ** A single cell from a node, deserialized
143836 */
143837 struct RtreeCell {
143838 i64 iRowid; /* Node or entry ID */
143839 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
143840 };
143841
143842
143843 /*
143844 ** This object becomes the sqlite3_user_data() for the SQL functions
143845 ** that are created by sqlite3_rtree_geometry_callback() and
143846 ** sqlite3_rtree_query_callback() and which appear on the right of MATCH
143847 ** operators in order to constrain a search.
143848 **
143849 ** xGeom and xQueryFunc are the callback functions. Exactly one of
143850 ** xGeom and xQueryFunc fields is non-NULL, depending on whether the
143851 ** SQL function was created using sqlite3_rtree_geometry_callback() or
143852 ** sqlite3_rtree_query_callback().
143853 **
143854 ** This object is deleted automatically by the destructor mechanism in
143855 ** sqlite3_create_function_v2().
143856 */
143857 struct RtreeGeomCallback {
143858 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
143859 int (*xQueryFunc)(sqlite3_rtree_query_info*);
143860 void (*xDestructor)(void*);
143861 void *pContext;
143862 };
143863
143864
143865 /*
143866 ** Value for the first field of every RtreeMatchArg object. The MATCH
@@ -143056,33 +143868,20 @@
143868 ** value to avoid operating on invalid blobs (which could cause a segfault).
143869 */
143870 #define RTREE_GEOMETRY_MAGIC 0x891245AB
143871
143872 /*
143873 ** An instance of this structure (in the form of a BLOB) is returned by
143874 ** the SQL functions that sqlite3_rtree_geometry_callback() and
143875 ** sqlite3_rtree_query_callback() create, and is read as the right-hand
143876 ** operand to the MATCH operator of an R-Tree.
143877 */
143878 struct RtreeMatchArg {
143879 u32 magic; /* Always RTREE_GEOMETRY_MAGIC */
143880 RtreeGeomCallback cb; /* Info about the callback functions */
143881 int nParam; /* Number of parameters to the SQL function */
143882 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143883 };
143884
143885 #ifndef MAX
143886 # define MAX(x,y) ((x) < (y) ? (y) : (x))
143887 #endif
@@ -143172,14 +143971,11 @@
143971 /*
143972 ** Given a node number iNode, return the corresponding key to use
143973 ** in the Rtree.aHash table.
143974 */
143975 static int nodeHash(i64 iNode){
143976 return iNode % HASHSIZE;
 
 
 
143977 }
143978
143979 /*
143980 ** Search the node hash table for node iNode. If found, return a pointer
143981 ** to it. Otherwise, return 0.
@@ -143235,12 +144031,11 @@
144031 }
144032
144033 /*
144034 ** Obtain a reference to an r-tree node.
144035 */
144036 static int nodeAcquire(
 
144037 Rtree *pRtree, /* R-tree structure */
144038 i64 iNode, /* Node number to load */
144039 RtreeNode *pParent, /* Either the parent node or NULL */
144040 RtreeNode **ppNode /* OUT: Acquired node */
144041 ){
@@ -143325,14 +144120,14 @@
144120
144121 /*
144122 ** Overwrite cell iCell of node pNode with the contents of pCell.
144123 */
144124 static void nodeOverwriteCell(
144125 Rtree *pRtree, /* The overall R-Tree */
144126 RtreeNode *pNode, /* The node into which the cell is to be written */
144127 RtreeCell *pCell, /* The cell to write */
144128 int iCell /* Index into pNode into which pCell is written */
144129 ){
144130 int ii;
144131 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
144132 p += writeInt64(p, pCell->iRowid);
144133 for(ii=0; ii<(pRtree->nDim*2); ii++){
@@ -143340,11 +144135,11 @@
144135 }
144136 pNode->isDirty = 1;
144137 }
144138
144139 /*
144140 ** Remove the cell with index iCell from node pNode.
144141 */
144142 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
144143 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
144144 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
144145 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
@@ -143357,15 +144152,14 @@
144152 ** Insert the contents of cell pCell into node pNode. If the insert
144153 ** is successful, return SQLITE_OK.
144154 **
144155 ** If there is not enough free space in pNode, return SQLITE_FULL.
144156 */
144157 static int nodeInsertCell(
144158 Rtree *pRtree, /* The overall R-Tree */
144159 RtreeNode *pNode, /* Write new cell into this node */
144160 RtreeCell *pCell /* The cell to be inserted */
 
144161 ){
144162 int nCell; /* Current number of cells in pNode */
144163 int nMaxCell; /* Maximum number of cells for pNode */
144164
144165 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
@@ -143382,12 +144176,11 @@
144176 }
144177
144178 /*
144179 ** If the node is dirty, write it out to the database.
144180 */
144181 static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
 
144182 int rc = SQLITE_OK;
144183 if( pNode->isDirty ){
144184 sqlite3_stmt *p = pRtree->pWriteNode;
144185 if( pNode->iNode ){
144186 sqlite3_bind_int64(p, 1, pNode->iNode);
@@ -143408,12 +144201,11 @@
144201
144202 /*
144203 ** Release a reference to a node. If the node is dirty and the reference
144204 ** count drops to zero, the node data is written to the database.
144205 */
144206 static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
 
144207 int rc = SQLITE_OK;
144208 if( pNode ){
144209 assert( pNode->nRef>0 );
144210 pNode->nRef--;
144211 if( pNode->nRef==0 ){
@@ -143437,45 +144229,50 @@
144229 ** Return the 64-bit integer value associated with cell iCell of
144230 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
144231 ** an internal node, then the 64-bit integer is a child page number.
144232 */
144233 static i64 nodeGetRowid(
144234 Rtree *pRtree, /* The overall R-Tree */
144235 RtreeNode *pNode, /* The node from which to extract the ID */
144236 int iCell /* The cell index from which to extract the ID */
144237 ){
144238 assert( iCell<NCELL(pNode) );
144239 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
144240 }
144241
144242 /*
144243 ** Return coordinate iCoord from cell iCell in node pNode.
144244 */
144245 static void nodeGetCoord(
144246 Rtree *pRtree, /* The overall R-Tree */
144247 RtreeNode *pNode, /* The node from which to extract a coordinate */
144248 int iCell, /* The index of the cell within the node */
144249 int iCoord, /* Which coordinate to extract */
144250 RtreeCoord *pCoord /* OUT: Space to write result to */
144251 ){
144252 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
144253 }
144254
144255 /*
144256 ** Deserialize cell iCell of node pNode. Populate the structure pointed
144257 ** to by pCell with the results.
144258 */
144259 static void nodeGetCell(
144260 Rtree *pRtree, /* The overall R-Tree */
144261 RtreeNode *pNode, /* The node containing the cell to be read */
144262 int iCell, /* Index of the cell within the node */
144263 RtreeCell *pCell /* OUT: Write the cell contents here */
144264 ){
144265 u8 *pData;
144266 u8 *pEnd;
144267 RtreeCoord *pCoord;
144268 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
144269 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
144270 pEnd = pData + pRtree->nDim*8;
144271 pCoord = pCell->aCoord;
144272 for(; pData<pEnd; pData+=4, pCoord++){
144273 readCoord(pData, pCoord);
144274 }
144275 }
144276
144277
144278 /* Forward declaration for the function that does the work of
@@ -143597,14 +144394,14 @@
144394 */
144395 static void freeCursorConstraints(RtreeCursor *pCsr){
144396 if( pCsr->aConstraint ){
144397 int i; /* Used to iterate through constraint array */
144398 for(i=0; i<pCsr->nConstraint; i++){
144399 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
144400 if( pInfo ){
144401 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
144402 sqlite3_free(pInfo);
144403 }
144404 }
144405 sqlite3_free(pCsr->aConstraint);
144406 pCsr->aConstraint = 0;
144407 }
@@ -143613,16 +144410,17 @@
144410 /*
144411 ** Rtree virtual table module xClose method.
144412 */
144413 static int rtreeClose(sqlite3_vtab_cursor *cur){
144414 Rtree *pRtree = (Rtree *)(cur->pVtab);
144415 int ii;
144416 RtreeCursor *pCsr = (RtreeCursor *)cur;
144417 freeCursorConstraints(pCsr);
144418 sqlite3_free(pCsr->aPoint);
144419 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
144420 sqlite3_free(pCsr);
144421 return SQLITE_OK;
144422 }
144423
144424 /*
144425 ** Rtree virtual table module xEof method.
144426 **
@@ -143629,198 +144427,168 @@
144427 ** Return non-zero if the cursor does not currently point to a valid
144428 ** record (i.e if the scan has finished), or zero otherwise.
144429 */
144430 static int rtreeEof(sqlite3_vtab_cursor *cur){
144431 RtreeCursor *pCsr = (RtreeCursor *)cur;
144432 return pCsr->atEOF;
144433 }
144434
144435 /*
144436 ** Convert raw bits from the on-disk RTree record into a coordinate value.
144437 ** The on-disk format is big-endian and needs to be converted for little-
144438 ** endian platforms. The on-disk record stores integer coordinates if
144439 ** eInt is true and it stores 32-bit floating point records if eInt is
144440 ** false. a[] is the four bytes of the on-disk record to be decoded.
144441 ** Store the results in "r".
144442 **
144443 ** There are three versions of this macro, one each for little-endian and
144444 ** big-endian processors and a third generic implementation. The endian-
144445 ** specific implementations are much faster and are preferred if the
144446 ** processor endianness is known at compile-time. The SQLITE_BYTEORDER
144447 ** macro is part of sqliteInt.h and hence the endian-specific
144448 ** implementation will only be used if this module is compiled as part
144449 ** of the amalgamation.
144450 */
144451 #if defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==1234
144452 #define RTREE_DECODE_COORD(eInt, a, r) { \
144453 RtreeCoord c; /* Coordinate decoded */ \
144454 memcpy(&c.u,a,4); \
144455 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
144456 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
144457 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
144458 }
144459 #elif defined(SQLITE_BYTEORDER) && SQLITE_BYTEORDER==4321
144460 #define RTREE_DECODE_COORD(eInt, a, r) { \
144461 RtreeCoord c; /* Coordinate decoded */ \
144462 memcpy(&c.u,a,4); \
144463 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
144464 }
144465 #else
144466 #define RTREE_DECODE_COORD(eInt, a, r) { \
144467 RtreeCoord c; /* Coordinate decoded */ \
144468 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
144469 +((u32)a[2]<<8) + a[3]; \
144470 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
144471 }
144472 #endif
144473
144474 /*
144475 ** Check the RTree node or entry given by pCellData and p against the MATCH
144476 ** constraint pConstraint.
144477 */
144478 static int rtreeCallbackConstraint(
144479 RtreeConstraint *pConstraint, /* The constraint to test */
144480 int eInt, /* True if RTree holding integer coordinates */
144481 u8 *pCellData, /* Raw cell content */
144482 RtreeSearchPoint *pSearch, /* Container of this cell */
144483 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
144484 int *peWithin /* OUT: visibility of the cell */
144485 ){
144486 int i; /* Loop counter */
144487 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
144488 int nCoord = pInfo->nCoord; /* No. of coordinates */
144489 int rc; /* Callback return code */
144490 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
144491
144492 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
144493 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
144494
144495 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
144496 pInfo->iRowid = readInt64(pCellData);
144497 }
144498 pCellData += 8;
144499 for(i=0; i<nCoord; i++, pCellData += 4){
144500 RTREE_DECODE_COORD(eInt, pCellData, aCoord[i]);
144501 }
144502 if( pConstraint->op==RTREE_MATCH ){
144503 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
144504 nCoord, aCoord, &i);
144505 if( i==0 ) *peWithin = NOT_WITHIN;
144506 *prScore = RTREE_ZERO;
144507 }else{
144508 pInfo->aCoord = aCoord;
144509 pInfo->iLevel = pSearch->iLevel - 1;
144510 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
144511 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
144512 rc = pConstraint->u.xQueryFunc(pInfo);
144513 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
144514 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
144515 *prScore = pInfo->rScore;
144516 }
144517 }
144518 return rc;
144519 }
144520
144521 /*
144522 ** Check the internal RTree node given by pCellData against constraint p.
144523 ** If this constraint cannot be satisfied by any child within the node,
144524 ** set *peWithin to NOT_WITHIN.
144525 */
144526 static void rtreeNonleafConstraint(
144527 RtreeConstraint *p, /* The constraint to test */
144528 int eInt, /* True if RTree holds integer coordinates */
144529 u8 *pCellData, /* Raw cell content as appears on disk */
144530 int *peWithin /* Adjust downward, as appropriate */
144531 ){
144532 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
144533
144534 /* p->iCoord might point to either a lower or upper bound coordinate
144535 ** in a coordinate pair. But make pCellData point to the lower bound.
144536 */
144537 pCellData += 8 + 4*(p->iCoord&0xfe);
144538
144539 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
144540 || p->op==RTREE_GT || p->op==RTREE_EQ );
144541 switch( p->op ){
144542 case RTREE_LE:
144543 case RTREE_LT:
144544 case RTREE_EQ:
144545 RTREE_DECODE_COORD(eInt, pCellData, val);
144546 /* val now holds the lower bound of the coordinate pair */
144547 if( p->u.rValue>=val ) return;
144548 if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */
144549 /* Fall through for the RTREE_EQ case */
144550
144551 default: /* RTREE_GT or RTREE_GE, or fallthrough of RTREE_EQ */
144552 pCellData += 4;
144553 RTREE_DECODE_COORD(eInt, pCellData, val);
144554 /* val now holds the upper bound of the coordinate pair */
144555 if( p->u.rValue<=val ) return;
144556 }
144557 *peWithin = NOT_WITHIN;
144558 }
144559
144560 /*
144561 ** Check the leaf RTree cell given by pCellData against constraint p.
144562 ** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
144563 ** If the constraint is satisfied, leave *peWithin unchanged.
144564 **
144565 ** The constraint is of the form: xN op $val
144566 **
144567 ** The op is given by p->op. The xN is p->iCoord-th coordinate in
144568 ** pCellData. $val is given by p->u.rValue.
144569 */
144570 static void rtreeLeafConstraint(
144571 RtreeConstraint *p, /* The constraint to test */
144572 int eInt, /* True if RTree holds integer coordinates */
144573 u8 *pCellData, /* Raw cell content as appears on disk */
144574 int *peWithin /* Adjust downward, as appropriate */
144575 ){
144576 RtreeDValue xN; /* Coordinate value converted to a double */
144577
144578 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
144579 || p->op==RTREE_GT || p->op==RTREE_EQ );
144580 pCellData += 8 + p->iCoord*4;
144581 RTREE_DECODE_COORD(eInt, pCellData, xN);
144582 switch( p->op ){
144583 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
144584 case RTREE_LT: if( xN < p->u.rValue ) return; break;
144585 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
144586 case RTREE_GT: if( xN > p->u.rValue ) return; break;
144587 default: if( xN == p->u.rValue ) return; break;
144588 }
144589 *peWithin = NOT_WITHIN;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144590 }
144591
144592 /*
144593 ** One of the cells in node pNode is guaranteed to have a 64-bit
144594 ** integer value equal to iRowid. Return the index of this cell.
@@ -143831,10 +144599,11 @@
144599 i64 iRowid,
144600 int *piIndex
144601 ){
144602 int ii;
144603 int nCell = NCELL(pNode);
144604 assert( nCell<200 );
144605 for(ii=0; ii<nCell; ii++){
144606 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
144607 *piIndex = ii;
144608 return SQLITE_OK;
144609 }
@@ -143852,82 +144621,342 @@
144621 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
144622 }
144623 *piIndex = -1;
144624 return SQLITE_OK;
144625 }
144626
144627 /*
144628 ** Compare two search points. Return negative, zero, or positive if the first
144629 ** is less than, equal to, or greater than the second.
144630 **
144631 ** The rScore is the primary key. Smaller rScore values come first.
144632 ** If the rScore is a tie, then use iLevel as the tie breaker with smaller
144633 ** iLevel values coming first. In this way, if rScore is the same for all
144634 ** SearchPoints, then iLevel becomes the deciding factor and the result
144635 ** is a depth-first search, which is the desired default behavior.
144636 */
144637 static int rtreeSearchPointCompare(
144638 const RtreeSearchPoint *pA,
144639 const RtreeSearchPoint *pB
144640 ){
144641 if( pA->rScore<pB->rScore ) return -1;
144642 if( pA->rScore>pB->rScore ) return +1;
144643 if( pA->iLevel<pB->iLevel ) return -1;
144644 if( pA->iLevel>pB->iLevel ) return +1;
144645 return 0;
144646 }
144647
144648 /*
144649 ** Interchange to search points in a cursor.
144650 */
144651 static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
144652 RtreeSearchPoint t = p->aPoint[i];
144653 assert( i<j );
144654 p->aPoint[i] = p->aPoint[j];
144655 p->aPoint[j] = t;
144656 i++; j++;
144657 if( i<RTREE_CACHE_SZ ){
144658 if( j>=RTREE_CACHE_SZ ){
144659 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
144660 p->aNode[i] = 0;
144661 }else{
144662 RtreeNode *pTemp = p->aNode[i];
144663 p->aNode[i] = p->aNode[j];
144664 p->aNode[j] = pTemp;
144665 }
144666 }
144667 }
144668
144669 /*
144670 ** Return the search point with the lowest current score.
144671 */
144672 static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
144673 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
144674 }
144675
144676 /*
144677 ** Get the RtreeNode for the search point with the lowest score.
144678 */
144679 static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
144680 sqlite3_int64 id;
144681 int ii = 1 - pCur->bPoint;
144682 assert( ii==0 || ii==1 );
144683 assert( pCur->bPoint || pCur->nPoint );
144684 if( pCur->aNode[ii]==0 ){
144685 assert( pRC!=0 );
144686 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
144687 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
144688 }
144689 return pCur->aNode[ii];
144690 }
144691
144692 /*
144693 ** Push a new element onto the priority queue
144694 */
144695 static RtreeSearchPoint *rtreeEnqueue(
144696 RtreeCursor *pCur, /* The cursor */
144697 RtreeDValue rScore, /* Score for the new search point */
144698 u8 iLevel /* Level for the new search point */
144699 ){
144700 int i, j;
144701 RtreeSearchPoint *pNew;
144702 if( pCur->nPoint>=pCur->nPointAlloc ){
144703 int nNew = pCur->nPointAlloc*2 + 8;
144704 pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
144705 if( pNew==0 ) return 0;
144706 pCur->aPoint = pNew;
144707 pCur->nPointAlloc = nNew;
144708 }
144709 i = pCur->nPoint++;
144710 pNew = pCur->aPoint + i;
144711 pNew->rScore = rScore;
144712 pNew->iLevel = iLevel;
144713 assert( iLevel>=0 && iLevel<=RTREE_MAX_DEPTH );
144714 while( i>0 ){
144715 RtreeSearchPoint *pParent;
144716 j = (i-1)/2;
144717 pParent = pCur->aPoint + j;
144718 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
144719 rtreeSearchPointSwap(pCur, j, i);
144720 i = j;
144721 pNew = pParent;
144722 }
144723 return pNew;
144724 }
144725
144726 /*
144727 ** Allocate a new RtreeSearchPoint and return a pointer to it. Return
144728 ** NULL if malloc fails.
144729 */
144730 static RtreeSearchPoint *rtreeSearchPointNew(
144731 RtreeCursor *pCur, /* The cursor */
144732 RtreeDValue rScore, /* Score for the new search point */
144733 u8 iLevel /* Level for the new search point */
144734 ){
144735 RtreeSearchPoint *pNew, *pFirst;
144736 pFirst = rtreeSearchPointFirst(pCur);
144737 pCur->anQueue[iLevel]++;
144738 if( pFirst==0
144739 || pFirst->rScore>rScore
144740 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
144741 ){
144742 if( pCur->bPoint ){
144743 int ii;
144744 pNew = rtreeEnqueue(pCur, rScore, iLevel);
144745 if( pNew==0 ) return 0;
144746 ii = (int)(pNew - pCur->aPoint) + 1;
144747 if( ii<RTREE_CACHE_SZ ){
144748 assert( pCur->aNode[ii]==0 );
144749 pCur->aNode[ii] = pCur->aNode[0];
144750 }else{
144751 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
144752 }
144753 pCur->aNode[0] = 0;
144754 *pNew = pCur->sPoint;
144755 }
144756 pCur->sPoint.rScore = rScore;
144757 pCur->sPoint.iLevel = iLevel;
144758 pCur->bPoint = 1;
144759 return &pCur->sPoint;
144760 }else{
144761 return rtreeEnqueue(pCur, rScore, iLevel);
144762 }
144763 }
144764
144765 #if 0
144766 /* Tracing routines for the RtreeSearchPoint queue */
144767 static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
144768 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
144769 printf(" %d.%05lld.%02d %g %d",
144770 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
144771 );
144772 idx++;
144773 if( idx<RTREE_CACHE_SZ ){
144774 printf(" %p\n", pCur->aNode[idx]);
144775 }else{
144776 printf("\n");
144777 }
144778 }
144779 static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
144780 int ii;
144781 printf("=== %9s ", zPrefix);
144782 if( pCur->bPoint ){
144783 tracePoint(&pCur->sPoint, -1, pCur);
144784 }
144785 for(ii=0; ii<pCur->nPoint; ii++){
144786 if( ii>0 || pCur->bPoint ) printf(" ");
144787 tracePoint(&pCur->aPoint[ii], ii, pCur);
144788 }
144789 }
144790 # define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
144791 #else
144792 # define RTREE_QUEUE_TRACE(A,B) /* no-op */
144793 #endif
144794
144795 /* Remove the search point with the lowest current score.
144796 */
144797 static void rtreeSearchPointPop(RtreeCursor *p){
144798 int i, j, k, n;
144799 i = 1 - p->bPoint;
144800 assert( i==0 || i==1 );
144801 if( p->aNode[i] ){
144802 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
144803 p->aNode[i] = 0;
144804 }
144805 if( p->bPoint ){
144806 p->anQueue[p->sPoint.iLevel]--;
144807 p->bPoint = 0;
144808 }else if( p->nPoint ){
144809 p->anQueue[p->aPoint[0].iLevel]--;
144810 n = --p->nPoint;
144811 p->aPoint[0] = p->aPoint[n];
144812 if( n<RTREE_CACHE_SZ-1 ){
144813 p->aNode[1] = p->aNode[n+1];
144814 p->aNode[n+1] = 0;
144815 }
144816 i = 0;
144817 while( (j = i*2+1)<n ){
144818 k = j+1;
144819 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
144820 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
144821 rtreeSearchPointSwap(p, i, k);
144822 i = k;
144823 }else{
144824 break;
144825 }
144826 }else{
144827 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
144828 rtreeSearchPointSwap(p, i, j);
144829 i = j;
144830 }else{
144831 break;
144832 }
144833 }
144834 }
144835 }
144836 }
144837
144838
144839 /*
144840 ** Continue the search on cursor pCur until the front of the queue
144841 ** contains an entry suitable for returning as a result-set row,
144842 ** or until the RtreeSearchPoint queue is empty, indicating that the
144843 ** query has completed.
144844 */
144845 static int rtreeStepToLeaf(RtreeCursor *pCur){
144846 RtreeSearchPoint *p;
144847 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
144848 RtreeNode *pNode;
144849 int eWithin;
144850 int rc = SQLITE_OK;
144851 int nCell;
144852 int nConstraint = pCur->nConstraint;
144853 int ii;
144854 int eInt;
144855 RtreeSearchPoint x;
144856
144857 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
144858 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
144859 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
144860 if( rc ) return rc;
144861 nCell = NCELL(pNode);
144862 assert( nCell<200 );
144863 while( p->iCell<nCell ){
144864 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
144865 u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
144866 eWithin = FULLY_WITHIN;
144867 for(ii=0; ii<nConstraint; ii++){
144868 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
144869 if( pConstraint->op>=RTREE_MATCH ){
144870 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
144871 &rScore, &eWithin);
144872 if( rc ) return rc;
144873 }else if( p->iLevel==1 ){
144874 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
144875 }else{
144876 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
144877 }
144878 if( eWithin==NOT_WITHIN ) break;
144879 }
144880 p->iCell++;
144881 if( eWithin==NOT_WITHIN ) continue;
144882 x.iLevel = p->iLevel - 1;
144883 if( x.iLevel ){
144884 x.id = readInt64(pCellData);
144885 x.iCell = 0;
144886 }else{
144887 x.id = p->id;
144888 x.iCell = p->iCell - 1;
144889 }
144890 if( p->iCell>=nCell ){
144891 RTREE_QUEUE_TRACE(pCur, "POP-S:");
144892 rtreeSearchPointPop(pCur);
144893 }
144894 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
144895 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
144896 if( p==0 ) return SQLITE_NOMEM;
144897 p->eWithin = eWithin;
144898 p->id = x.id;
144899 p->iCell = x.iCell;
144900 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
144901 break;
144902 }
144903 if( p->iCell>=nCell ){
144904 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
144905 rtreeSearchPointPop(pCur);
144906 }
144907 }
144908 pCur->atEOF = p==0;
144909 return SQLITE_OK;
144910 }
144911
144912 /*
144913 ** Rtree virtual table module xNext method.
144914 */
144915 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
 
144916 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
144917 int rc = SQLITE_OK;
144918
144919 /* Move to the next entry that matches the configured constraints. */
144920 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
144921 rtreeSearchPointPop(pCsr);
144922 rc = rtreeStepToLeaf(pCsr);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144923 return rc;
144924 }
144925
144926 /*
144927 ** Rtree virtual table module xRowid method.
144928 */
144929 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
 
144930 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
144931 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
144932 int rc = SQLITE_OK;
144933 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
144934 if( rc==SQLITE_OK && p ){
144935 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
144936 }
144937 return rc;
144938 }
144939
144940 /*
144941 ** Rtree virtual table module xColumn method.
144942 */
144943 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
144944 Rtree *pRtree = (Rtree *)cur->pVtab;
144945 RtreeCursor *pCsr = (RtreeCursor *)cur;
144946 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
144947 RtreeCoord c;
144948 int rc = SQLITE_OK;
144949 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
144950
144951 if( rc ) return rc;
144952 if( p==0 ) return SQLITE_OK;
144953 if( i==0 ){
144954 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
 
144955 }else{
144956 if( rc ) return rc;
144957 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
144958 #ifndef SQLITE_RTREE_INT_ONLY
144959 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
144960 sqlite3_result_double(ctx, c.f);
144961 }else
144962 #endif
@@ -143934,11 +144963,10 @@
144963 {
144964 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
144965 sqlite3_result_int(ctx, c.i);
144966 }
144967 }
 
144968 return SQLITE_OK;
144969 }
144970
144971 /*
144972 ** Use nodeAcquire() to obtain the leaf node containing the record with
@@ -143945,16 +144973,22 @@
144973 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
144974 ** return SQLITE_OK. If there is no such record in the table, set
144975 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
144976 ** to zero and return an SQLite error code.
144977 */
144978 static int findLeafNode(
144979 Rtree *pRtree, /* RTree to search */
144980 i64 iRowid, /* The rowid searching for */
144981 RtreeNode **ppLeaf, /* Write the node here */
144982 sqlite3_int64 *piNode /* Write the node-id here */
144983 ){
144984 int rc;
144985 *ppLeaf = 0;
144986 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
144987 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
144988 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
144989 if( piNode ) *piNode = iNode;
144990 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
144991 sqlite3_reset(pRtree->pReadRowid);
144992 }else{
144993 rc = sqlite3_reset(pRtree->pReadRowid);
144994 }
@@ -143966,13 +145000,14 @@
145000 ** as the second argument for a MATCH constraint. The value passed as the
145001 ** first argument to this function is the right-hand operand to the MATCH
145002 ** operator.
145003 */
145004 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
145005 RtreeMatchArg *pBlob; /* BLOB returned by geometry function */
145006 sqlite3_rtree_query_info *pInfo; /* Callback information */
145007 int nBlob; /* Size of the geometry function blob */
145008 int nExpected; /* Expected size of the BLOB */
145009
145010 /* Check that value is actually a blob. */
145011 if( sqlite3_value_type(pValue)!=SQLITE_BLOB ) return SQLITE_ERROR;
145012
145013 /* Check that the blob is roughly the right size. */
@@ -143981,31 +145016,33 @@
145016 || ((nBlob-sizeof(RtreeMatchArg))%sizeof(RtreeDValue))!=0
145017 ){
145018 return SQLITE_ERROR;
145019 }
145020
145021 pInfo = (sqlite3_rtree_query_info*)sqlite3_malloc( sizeof(*pInfo)+nBlob );
145022 if( !pInfo ) return SQLITE_NOMEM;
145023 memset(pInfo, 0, sizeof(*pInfo));
145024 pBlob = (RtreeMatchArg*)&pInfo[1];
145025
145026 memcpy(pBlob, sqlite3_value_blob(pValue), nBlob);
145027 nExpected = (int)(sizeof(RtreeMatchArg) +
145028 (pBlob->nParam-1)*sizeof(RtreeDValue));
145029 if( pBlob->magic!=RTREE_GEOMETRY_MAGIC || nBlob!=nExpected ){
145030 sqlite3_free(pInfo);
 
 
145031 return SQLITE_ERROR;
145032 }
145033 pInfo->pContext = pBlob->cb.pContext;
145034 pInfo->nParam = pBlob->nParam;
145035 pInfo->aParam = pBlob->aParam;
145036
145037 if( pBlob->cb.xGeom ){
145038 pCons->u.xGeom = pBlob->cb.xGeom;
145039 }else{
145040 pCons->op = RTREE_QUERY;
145041 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
145042 }
145043 pCons->pInfo = pInfo;
145044 return SQLITE_OK;
145045 }
145046
145047 /*
145048 ** Rtree virtual table module xFilter method.
@@ -144015,91 +145052,96 @@
145052 int idxNum, const char *idxStr,
145053 int argc, sqlite3_value **argv
145054 ){
145055 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
145056 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
 
145057 RtreeNode *pRoot = 0;
145058 int ii;
145059 int rc = SQLITE_OK;
145060 int iCell = 0;
145061
145062 rtreeReference(pRtree);
145063
145064 freeCursorConstraints(pCsr);
145065 pCsr->iStrategy = idxNum;
145066
145067 if( idxNum==1 ){
145068 /* Special case - lookup by rowid. */
145069 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
145070 RtreeSearchPoint *p; /* Search point for the the leaf */
145071 i64 iRowid = sqlite3_value_int64(argv[0]);
145072 i64 iNode = 0;
145073 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
145074 if( rc==SQLITE_OK && pLeaf!=0 ){
145075 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
145076 assert( p!=0 ); /* Always returns pCsr->sPoint */
145077 pCsr->aNode[0] = pLeaf;
145078 p->id = iNode;
145079 p->eWithin = PARTLY_WITHIN;
145080 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
145081 p->iCell = iCell;
145082 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
145083 }else{
145084 pCsr->atEOF = 1;
145085 }
145086 }else{
145087 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
145088 ** with the configured constraints.
145089 */
145090 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
145091 if( rc==SQLITE_OK && argc>0 ){
145092 pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc);
145093 pCsr->nConstraint = argc;
145094 if( !pCsr->aConstraint ){
145095 rc = SQLITE_NOMEM;
145096 }else{
145097 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
145098 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
145099 assert( (idxStr==0 && argc==0)
145100 || (idxStr && (int)strlen(idxStr)==argc*2) );
145101 for(ii=0; ii<argc; ii++){
145102 RtreeConstraint *p = &pCsr->aConstraint[ii];
145103 p->op = idxStr[ii*2];
145104 p->iCoord = idxStr[ii*2+1]-'0';
145105 if( p->op>=RTREE_MATCH ){
145106 /* A MATCH operator. The right-hand-side must be a blob that
145107 ** can be cast into an RtreeMatchArg object. One created using
145108 ** an sqlite3_rtree_geometry_callback() SQL user function.
145109 */
145110 rc = deserializeGeometry(argv[ii], p);
145111 if( rc!=SQLITE_OK ){
145112 break;
145113 }
145114 p->pInfo->nCoord = pRtree->nDim*2;
145115 p->pInfo->anQueue = pCsr->anQueue;
145116 p->pInfo->mxLevel = pRtree->iDepth + 1;
145117 }else{
145118 #ifdef SQLITE_RTREE_INT_ONLY
145119 p->u.rValue = sqlite3_value_int64(argv[ii]);
145120 #else
145121 p->u.rValue = sqlite3_value_double(argv[ii]);
145122 #endif
145123 }
145124 }
145125 }
145126 }
145127 if( rc==SQLITE_OK ){
145128 RtreeSearchPoint *pNew;
145129 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, pRtree->iDepth+1);
145130 if( pNew==0 ) return SQLITE_NOMEM;
145131 pNew->id = 1;
145132 pNew->iCell = 0;
145133 pNew->eWithin = PARTLY_WITHIN;
145134 assert( pCsr->bPoint==1 );
145135 pCsr->aNode[0] = pRoot;
145136 pRoot = 0;
145137 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
145138 rc = rtreeStepToLeaf(pCsr);
145139 }
145140 }
145141
145142 nodeRelease(pRtree, pRoot);
 
 
 
 
 
 
 
 
 
145143 rtreeRelease(pRtree);
145144 return rc;
145145 }
145146
145147 /*
@@ -144197,11 +145239,11 @@
145239 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH );
145240 op = RTREE_MATCH;
145241 break;
145242 }
145243 zIdxStr[iIdx++] = op;
145244 zIdxStr[iIdx++] = p->iColumn - 1 + '0';
145245 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
145246 pIdxInfo->aConstraintUsage[ii].omit = 1;
145247 }
145248 }
145249
@@ -144290,66 +145332,36 @@
145332 area = cellArea(pRtree, &cell);
145333 cellUnion(pRtree, &cell, pCell);
145334 return (cellArea(pRtree, &cell)-area);
145335 }
145336
 
145337 static RtreeDValue cellOverlap(
145338 Rtree *pRtree,
145339 RtreeCell *p,
145340 RtreeCell *aCell,
145341 int nCell
 
145342 ){
145343 int ii;
145344 RtreeDValue overlap = RTREE_ZERO;
145345 for(ii=0; ii<nCell; ii++){
145346 int jj;
145347 RtreeDValue o = (RtreeDValue)1;
145348 for(jj=0; jj<(pRtree->nDim*2); jj+=2){
145349 RtreeDValue x1, x2;
145350 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
145351 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
145352 if( x2<x1 ){
145353 o = (RtreeDValue)0;
145354 break;
145355 }else{
145356 o = o * (x2-x1);
145357 }
145358 }
145359 overlap += o;
 
 
 
 
 
 
 
 
 
 
145360 }
145361 return overlap;
145362 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145363
145364
145365 /*
145366 ** This function implements the ChooseLeaf algorithm from Gutman[84].
145367 ** ChooseSubTree in r*tree terminology.
@@ -144367,39 +145379,19 @@
145379
145380 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
145381 int iCell;
145382 sqlite3_int64 iBest = 0;
145383
145384 RtreeDValue fMinGrowth = RTREE_ZERO;
145385 RtreeDValue fMinArea = RTREE_ZERO;
 
 
 
 
145386
145387 int nCell = NCELL(pNode);
145388 RtreeCell cell;
145389 RtreeNode *pChild;
145390
145391 RtreeCell *aCell = 0;
145392
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145393 /* Select the child node which will be enlarged the least if pCell
145394 ** is inserted into it. Resolve ties by choosing the entry with
145395 ** the smallest area.
145396 */
145397 for(iCell=0; iCell<nCell; iCell++){
@@ -144407,30 +145399,13 @@
145399 RtreeDValue growth;
145400 RtreeDValue area;
145401 nodeGetCell(pRtree, pNode, iCell, &cell);
145402 growth = cellGrowth(pRtree, &cell, pCell);
145403 area = cellArea(pRtree, &cell);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145404 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){
145405 bBest = 1;
145406 }
 
145407 if( bBest ){
145408 fMinGrowth = growth;
145409 fMinArea = area;
145410 iBest = cell.iRowid;
145411 }
@@ -144497,159 +145472,10 @@
145472 return sqlite3_reset(pRtree->pWriteParent);
145473 }
145474
145475 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
145476
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145477
145478 /*
145479 ** Arguments aIdx, aDistance and aSpare all point to arrays of size
145480 ** nIdx. The aIdx array contains the set of integers from 0 to
145481 ** (nIdx-1) in no particular order. This function sorts the values
@@ -144786,11 +145612,10 @@
145612 }
145613 #endif
145614 }
145615 }
145616
 
145617 /*
145618 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
145619 */
145620 static int splitNodeStartree(
145621 Rtree *pRtree,
@@ -144805,11 +145630,11 @@
145630 int *aSpare;
145631 int ii;
145632
145633 int iBestDim = 0;
145634 int iBestSplit = 0;
145635 RtreeDValue fBestMargin = RTREE_ZERO;
145636
145637 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
145638
145639 aaSorted = (int **)sqlite3_malloc(nByte);
145640 if( !aaSorted ){
@@ -144826,13 +145651,13 @@
145651 }
145652 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
145653 }
145654
145655 for(ii=0; ii<pRtree->nDim; ii++){
145656 RtreeDValue margin = RTREE_ZERO;
145657 RtreeDValue fBestOverlap = RTREE_ZERO;
145658 RtreeDValue fBestArea = RTREE_ZERO;
145659 int iBestLeft = 0;
145660 int nLeft;
145661
145662 for(
145663 nLeft=RTREE_MINCELLS(pRtree);
@@ -144854,11 +145679,11 @@
145679 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
145680 }
145681 }
145682 margin += cellMargin(pRtree, &left);
145683 margin += cellMargin(pRtree, &right);
145684 overlap = cellOverlap(pRtree, &left, &right, 1);
145685 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
145686 if( (nLeft==RTREE_MINCELLS(pRtree))
145687 || (overlap<fBestOverlap)
145688 || (overlap==fBestOverlap && area<fBestArea)
145689 ){
@@ -144886,67 +145711,11 @@
145711 }
145712
145713 sqlite3_free(aaSorted);
145714 return SQLITE_OK;
145715 }
145716
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145717
145718 static int updateMapping(
145719 Rtree *pRtree,
145720 i64 iRowid,
145721 RtreeNode *pNode,
@@ -145020,11 +145789,12 @@
145789 }
145790
145791 memset(pLeft->zData, 0, pRtree->iNodeSize);
145792 memset(pRight->zData, 0, pRtree->iNodeSize);
145793
145794 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
145795 &leftbbox, &rightbbox);
145796 if( rc!=SQLITE_OK ){
145797 goto splitnode_out;
145798 }
145799
145800 /* Ensure both child nodes have node numbers assigned to them by calling
@@ -145303,11 +146073,11 @@
146073 for(iDim=0; iDim<pRtree->nDim; iDim++){
146074 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2));
146075 }
146076
146077 for(ii=0; ii<nCell; ii++){
146078 aDistance[ii] = RTREE_ZERO;
146079 for(iDim=0; iDim<pRtree->nDim; iDim++){
146080 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) -
146081 DCOORD(aCell[ii].aCoord[iDim*2]));
146082 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]);
146083 }
@@ -145369,20 +146139,16 @@
146139 nodeReference(pNode);
146140 pChild->pParent = pNode;
146141 }
146142 }
146143 if( nodeInsertCell(pRtree, pNode, pCell) ){
 
146144 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){
146145 rc = SplitNode(pRtree, pNode, pCell, iHeight);
146146 }else{
146147 pRtree->iReinsertHeight = iHeight;
146148 rc = Reinsert(pRtree, pNode, pCell, iHeight);
146149 }
 
 
 
146150 }else{
146151 rc = AdjustTree(pRtree, pNode, pCell);
146152 if( rc==SQLITE_OK ){
146153 if( iHeight==0 ){
146154 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
@@ -145448,11 +146214,11 @@
146214
146215 /* Obtain a reference to the leaf node that contains the entry
146216 ** about to be deleted.
146217 */
146218 if( rc==SQLITE_OK ){
146219 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
146220 }
146221
146222 /* Delete the cell in question from the leaf node. */
146223 if( rc==SQLITE_OK ){
146224 int rc2;
@@ -145785,11 +146551,12 @@
146551
146552 if( isCreate ){
146553 char *zCreate = sqlite3_mprintf(
146554 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);"
146555 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);"
146556 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,"
146557 " parentnode INTEGER);"
146558 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))",
146559 zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize
146560 );
146561 if( !zCreate ){
146562 return SQLITE_NOMEM;
@@ -145999,14 +146766,14 @@
146766
146767 /*
146768 ** Implementation of a scalar function that decodes r-tree nodes to
146769 ** human readable strings. This can be used for debugging and analysis.
146770 **
146771 ** The scalar function takes two arguments: (1) the number of dimensions
146772 ** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
146773 ** an r-tree node. For a two-dimensional r-tree structure called "rt", to
146774 ** deserialize all nodes, a statement like:
146775 **
146776 ** SELECT rtreenode(2, data) FROM rt_node;
146777 **
146778 ** The human readable string takes the form of a Tcl list with one
146779 ** entry for each cell in the r-tree node. Each entry is itself a
@@ -146035,11 +146802,11 @@
146802 nodeGetCell(&tree, &node, ii, &cell);
146803 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid);
146804 nCell = (int)strlen(zCell);
146805 for(jj=0; jj<tree.nDim*2; jj++){
146806 #ifndef SQLITE_RTREE_INT_ONLY
146807 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
146808 (double)cell.aCoord[jj].f);
146809 #else
146810 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
146811 cell.aCoord[jj].i);
146812 #endif
@@ -146056,10 +146823,19 @@
146823 }
146824
146825 sqlite3_result_text(ctx, zText, -1, sqlite3_free);
146826 }
146827
146828 /* This routine implements an SQL function that returns the "depth" parameter
146829 ** from the front of a blob that is an r-tree node. For example:
146830 **
146831 ** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
146832 **
146833 ** The depth value is 0 for all nodes other than the root node, and the root
146834 ** node always has nodeno=1, so the example above is the primary use for this
146835 ** routine. This routine is intended for testing and analysis only.
146836 */
146837 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
146838 UNUSED_PARAMETER(nArg);
146839 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
146840 || sqlite3_value_bytes(apArg[0])<2
146841 ){
@@ -146098,26 +146874,35 @@
146874
146875 return rc;
146876 }
146877
146878 /*
146879 ** This routine deletes the RtreeGeomCallback object that was attached
146880 ** one of the SQL functions create by sqlite3_rtree_geometry_callback()
146881 ** or sqlite3_rtree_query_callback(). In other words, this routine is the
146882 ** destructor for an RtreeGeomCallback objecct. This routine is called when
146883 ** the corresponding SQL function is deleted.
146884 */
146885 static void rtreeFreeCallback(void *p){
146886 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
146887 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
146888 sqlite3_free(p);
146889 }
146890
146891 /*
146892 ** Each call to sqlite3_rtree_geometry_callback() or
146893 ** sqlite3_rtree_query_callback() creates an ordinary SQLite
146894 ** scalar function that is implemented by this routine.
146895 **
146896 ** All this function does is construct an RtreeMatchArg object that
146897 ** contains the geometry-checking callback routines and a list of
146898 ** parameters to this function, then return that RtreeMatchArg object
146899 ** as a BLOB.
146900 **
146901 ** The R-Tree MATCH operator will read the returned BLOB, deserialize
146902 ** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
146903 ** out which elements of the R-Tree should be returned by the query.
146904 */
146905 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
146906 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
146907 RtreeMatchArg *pBlob;
146908 int nBlob;
@@ -146127,45 +146912,68 @@
146912 if( !pBlob ){
146913 sqlite3_result_error_nomem(ctx);
146914 }else{
146915 int i;
146916 pBlob->magic = RTREE_GEOMETRY_MAGIC;
146917 pBlob->cb = pGeomCtx[0];
 
146918 pBlob->nParam = nArg;
146919 for(i=0; i<nArg; i++){
146920 #ifdef SQLITE_RTREE_INT_ONLY
146921 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
146922 #else
146923 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
146924 #endif
146925 }
146926 sqlite3_result_blob(ctx, pBlob, nBlob, sqlite3_free);
146927 }
146928 }
146929
146930 /*
146931 ** Register a new geometry function for use with the r-tree MATCH operator.
146932 */
146933 SQLITE_API int sqlite3_rtree_geometry_callback(
146934 sqlite3 *db, /* Register SQL function on this connection */
146935 const char *zGeom, /* Name of the new SQL function */
146936 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
146937 void *pContext /* Extra data associated with the callback */
146938 ){
146939 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
146940
146941 /* Allocate and populate the context object. */
146942 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
146943 if( !pGeomCtx ) return SQLITE_NOMEM;
146944 pGeomCtx->xGeom = xGeom;
146945 pGeomCtx->xQueryFunc = 0;
146946 pGeomCtx->xDestructor = 0;
146947 pGeomCtx->pContext = pContext;
 
 
 
146948 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
146949 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
146950 );
146951 }
146952
146953 /*
146954 ** Register a new 2nd-generation geometry function for use with the
146955 ** r-tree MATCH operator.
146956 */
146957 SQLITE_API int sqlite3_rtree_query_callback(
146958 sqlite3 *db, /* Register SQL function on this connection */
146959 const char *zQueryFunc, /* Name of new SQL function */
146960 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
146961 void *pContext, /* Extra data passed into the callback */
146962 void (*xDestructor)(void*) /* Destructor for the extra data */
146963 ){
146964 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
146965
146966 /* Allocate and populate the context object. */
146967 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
146968 if( !pGeomCtx ) return SQLITE_NOMEM;
146969 pGeomCtx->xGeom = 0;
146970 pGeomCtx->xQueryFunc = xQueryFunc;
146971 pGeomCtx->xDestructor = xDestructor;
146972 pGeomCtx->pContext = pContext;
146973 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
146974 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
146975 );
146976 }
146977
146978 #if !SQLITE_CORE
146979 #ifdef _WIN32
146980
+94 -10
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107107
** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108108
** [sqlite_version()] and [sqlite_source_id()].
109109
*/
110110
#define SQLITE_VERSION "3.8.5"
111111
#define SQLITE_VERSION_NUMBER 3008005
112
-#define SQLITE_SOURCE_ID "2014-04-18 22:20:31 9a5d38c79d2482a23bcfbc3ff35ca4fa269c768d"
112
+#define SQLITE_SOURCE_ID "2014-05-28 20:22:28 d018a34a05cec6adda61ed225d084c587343f2a6"
113113
114114
/*
115115
** CAPI3REF: Run-Time Library Version Numbers
116116
** KEYWORDS: sqlite3_version, sqlite3_sourceid
117117
**
@@ -558,11 +558,14 @@
558558
** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
559559
** after reboot following a crash or power loss, the only bytes in a
560560
** file that were written at the application level might have changed
561561
** and that adjacent bytes, even bytes within the same sector are
562562
** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
563
-** flag indicate that a file cannot be deleted when open.
563
+** flag indicate that a file cannot be deleted when open. The
564
+** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
565
+** read-only media and cannot be changed even by processes with
566
+** elevated privileges.
564567
*/
565568
#define SQLITE_IOCAP_ATOMIC 0x00000001
566569
#define SQLITE_IOCAP_ATOMIC512 0x00000002
567570
#define SQLITE_IOCAP_ATOMIC1K 0x00000004
568571
#define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -573,10 +576,11 @@
573576
#define SQLITE_IOCAP_ATOMIC64K 0x00000100
574577
#define SQLITE_IOCAP_SAFE_APPEND 0x00000200
575578
#define SQLITE_IOCAP_SEQUENTIAL 0x00000400
576579
#define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
577580
#define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
581
+#define SQLITE_IOCAP_IMMUTABLE 0x00002000
578582
579583
/*
580584
** CAPI3REF: File Locking Levels
581585
**
582586
** SQLite uses one of these integer values as the second
@@ -2777,10 +2781,34 @@
27772781
** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
27782782
** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
27792783
** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
27802784
** a URI filename, its value overrides any behavior requested by setting
27812785
** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2786
+**
2787
+** <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
2788
+** "1") or "false" (or "off" or "no" or "0") to indicate that the
2789
+** [powersafe overwrite] property does or does not apply to the
2790
+** storage media on which the database file resides. ^The psow query
2791
+** parameter only works for the built-in unix and Windows VFSes.
2792
+**
2793
+** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
2794
+** which if set disables file locking in rollback journal modes. This
2795
+** is useful for accessing a database on a filesystem that does not
2796
+** support locking. Caution: Database corruption might result if two
2797
+** or more processes write to the same database and any one of those
2798
+** processes uses nolock=1.
2799
+**
2800
+** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
2801
+** parameter that indicates that the database file is stored on
2802
+** read-only media. ^When immutable is set, SQLite assumes that the
2803
+** database file cannot be changed, even by a process with higher
2804
+** privilege, and so the database is opened read-only and all locking
2805
+** and change detection is disabled. Caution: Setting the immutable
2806
+** property on a database file that does in fact change can result
2807
+** in incorrect query results and/or [SQLITE_CORRUPT] errors.
2808
+** See also: [SQLITE_IOCAP_IMMUTABLE].
2809
+**
27822810
** </ul>
27832811
**
27842812
** ^Specifying an unknown parameter in the query component of a URI is not an
27852813
** error. Future versions of SQLite might understand additional query
27862814
** parameters. See "[query parameters with special meaning to SQLite]" for
@@ -2806,12 +2834,13 @@
28062834
** in URI filenames.
28072835
** <tr><td> file:data.db?mode=ro&cache=private <td>
28082836
** Open file "data.db" in the current directory for read-only access.
28092837
** Regardless of whether or not shared-cache mode is enabled by
28102838
** default, use a private cache.
2811
-** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
2812
-** Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
2839
+** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
2840
+** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
2841
+** that uses dot-files in place of posix advisory locking.
28132842
** <tr><td> file:data.db?mode=readonly <td>
28142843
** An error. "readonly" is not a valid option for the "mode" parameter.
28152844
** </table>
28162845
**
28172846
** ^URI hexadecimal escape sequences (%HH) are supported within the path and
@@ -7345,10 +7374,20 @@
73457374
#ifdef __cplusplus
73467375
extern "C" {
73477376
#endif
73487377
73497378
typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7379
+typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
7380
+
7381
+/* The double-precision datatype used by RTree depends on the
7382
+** SQLITE_RTREE_INT_ONLY compile-time option.
7383
+*/
7384
+#ifdef SQLITE_RTREE_INT_ONLY
7385
+ typedef sqlite3_int64 sqlite3_rtree_dbl;
7386
+#else
7387
+ typedef double sqlite3_rtree_dbl;
7388
+#endif
73507389
73517390
/*
73527391
** Register a geometry callback named zGeom that can be used as part of an
73537392
** R-Tree geometry query as follows:
73547393
**
@@ -7355,15 +7394,11 @@
73557394
** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
73567395
*/
73577396
SQLITE_API int sqlite3_rtree_geometry_callback(
73587397
sqlite3 *db,
73597398
const char *zGeom,
7360
-#ifdef SQLITE_RTREE_INT_ONLY
7361
- int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
7362
-#else
7363
- int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
7364
-#endif
7399
+ int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
73657400
void *pContext
73667401
);
73677402
73687403
73697404
/*
@@ -7371,17 +7406,66 @@
73717406
** argument to callbacks registered using rtree_geometry_callback().
73727407
*/
73737408
struct sqlite3_rtree_geometry {
73747409
void *pContext; /* Copy of pContext passed to s_r_g_c() */
73757410
int nParam; /* Size of array aParam[] */
7376
- double *aParam; /* Parameters passed to SQL geom function */
7411
+ sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
73777412
void *pUser; /* Callback implementation user data */
73787413
void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
73797414
};
73807415
7416
+/*
7417
+** Register a 2nd-generation geometry callback named zScore that can be
7418
+** used as part of an R-Tree geometry query as follows:
7419
+**
7420
+** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
7421
+*/
7422
+SQLITE_API int sqlite3_rtree_query_callback(
7423
+ sqlite3 *db,
7424
+ const char *zQueryFunc,
7425
+ int (*xQueryFunc)(sqlite3_rtree_query_info*),
7426
+ void *pContext,
7427
+ void (*xDestructor)(void*)
7428
+);
7429
+
7430
+
7431
+/*
7432
+** A pointer to a structure of the following type is passed as the
7433
+** argument to scored geometry callback registered using
7434
+** sqlite3_rtree_query_callback().
7435
+**
7436
+** Note that the first 5 fields of this structure are identical to
7437
+** sqlite3_rtree_geometry. This structure is a subclass of
7438
+** sqlite3_rtree_geometry.
7439
+*/
7440
+struct sqlite3_rtree_query_info {
7441
+ void *pContext; /* pContext from when function registered */
7442
+ int nParam; /* Number of function parameters */
7443
+ sqlite3_rtree_dbl *aParam; /* value of function parameters */
7444
+ void *pUser; /* callback can use this, if desired */
7445
+ void (*xDelUser)(void*); /* function to free pUser */
7446
+ sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
7447
+ unsigned int *anQueue; /* Number of pending entries in the queue */
7448
+ int nCoord; /* Number of coordinates */
7449
+ int iLevel; /* Level of current node or entry */
7450
+ int mxLevel; /* The largest iLevel value in the tree */
7451
+ sqlite3_int64 iRowid; /* Rowid for current entry */
7452
+ sqlite3_rtree_dbl rParentScore; /* Score of parent node */
7453
+ int eParentWithin; /* Visibility of parent node */
7454
+ int eWithin; /* OUT: Visiblity */
7455
+ sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
7456
+};
7457
+
7458
+/*
7459
+** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
7460
+*/
7461
+#define NOT_WITHIN 0 /* Object completely outside of query region */
7462
+#define PARTLY_WITHIN 1 /* Object partially overlaps query region */
7463
+#define FULLY_WITHIN 2 /* Object fully contained within query region */
7464
+
73817465
73827466
#ifdef __cplusplus
73837467
} /* end of the 'extern "C"' block */
73847468
#endif
73857469
73867470
#endif /* ifndef _SQLITE3RTREE_H_ */
73877471
73887472
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.5"
111 #define SQLITE_VERSION_NUMBER 3008005
112 #define SQLITE_SOURCE_ID "2014-04-18 22:20:31 9a5d38c79d2482a23bcfbc3ff35ca4fa269c768d"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -558,11 +558,14 @@
558 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
559 ** after reboot following a crash or power loss, the only bytes in a
560 ** file that were written at the application level might have changed
561 ** and that adjacent bytes, even bytes within the same sector are
562 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
563 ** flag indicate that a file cannot be deleted when open.
 
 
 
564 */
565 #define SQLITE_IOCAP_ATOMIC 0x00000001
566 #define SQLITE_IOCAP_ATOMIC512 0x00000002
567 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
568 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -573,10 +576,11 @@
573 #define SQLITE_IOCAP_ATOMIC64K 0x00000100
574 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
575 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
576 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
577 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
 
578
579 /*
580 ** CAPI3REF: File Locking Levels
581 **
582 ** SQLite uses one of these integer values as the second
@@ -2777,10 +2781,34 @@
2777 ** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
2778 ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2779 ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2780 ** a URI filename, its value overrides any behavior requested by setting
2781 ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2782 ** </ul>
2783 **
2784 ** ^Specifying an unknown parameter in the query component of a URI is not an
2785 ** error. Future versions of SQLite might understand additional query
2786 ** parameters. See "[query parameters with special meaning to SQLite]" for
@@ -2806,12 +2834,13 @@
2806 ** in URI filenames.
2807 ** <tr><td> file:data.db?mode=ro&cache=private <td>
2808 ** Open file "data.db" in the current directory for read-only access.
2809 ** Regardless of whether or not shared-cache mode is enabled by
2810 ** default, use a private cache.
2811 ** <tr><td> file:/home/fred/data.db?vfs=unix-nolock <td>
2812 ** Open file "/home/fred/data.db". Use the special VFS "unix-nolock".
 
2813 ** <tr><td> file:data.db?mode=readonly <td>
2814 ** An error. "readonly" is not a valid option for the "mode" parameter.
2815 ** </table>
2816 **
2817 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
@@ -7345,10 +7374,20 @@
7345 #ifdef __cplusplus
7346 extern "C" {
7347 #endif
7348
7349 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
 
 
 
 
 
 
 
 
 
 
7350
7351 /*
7352 ** Register a geometry callback named zGeom that can be used as part of an
7353 ** R-Tree geometry query as follows:
7354 **
@@ -7355,15 +7394,11 @@
7355 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7356 */
7357 SQLITE_API int sqlite3_rtree_geometry_callback(
7358 sqlite3 *db,
7359 const char *zGeom,
7360 #ifdef SQLITE_RTREE_INT_ONLY
7361 int (*xGeom)(sqlite3_rtree_geometry*, int n, sqlite3_int64 *a, int *pRes),
7362 #else
7363 int (*xGeom)(sqlite3_rtree_geometry*, int n, double *a, int *pRes),
7364 #endif
7365 void *pContext
7366 );
7367
7368
7369 /*
@@ -7371,17 +7406,66 @@
7371 ** argument to callbacks registered using rtree_geometry_callback().
7372 */
7373 struct sqlite3_rtree_geometry {
7374 void *pContext; /* Copy of pContext passed to s_r_g_c() */
7375 int nParam; /* Size of array aParam[] */
7376 double *aParam; /* Parameters passed to SQL geom function */
7377 void *pUser; /* Callback implementation user data */
7378 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
7379 };
7380
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7381
7382 #ifdef __cplusplus
7383 } /* end of the 'extern "C"' block */
7384 #endif
7385
7386 #endif /* ifndef _SQLITE3RTREE_H_ */
7387
7388
--- src/sqlite3.h
+++ src/sqlite3.h
@@ -107,11 +107,11 @@
107 ** [sqlite3_libversion_number()], [sqlite3_sourceid()],
108 ** [sqlite_version()] and [sqlite_source_id()].
109 */
110 #define SQLITE_VERSION "3.8.5"
111 #define SQLITE_VERSION_NUMBER 3008005
112 #define SQLITE_SOURCE_ID "2014-05-28 20:22:28 d018a34a05cec6adda61ed225d084c587343f2a6"
113
114 /*
115 ** CAPI3REF: Run-Time Library Version Numbers
116 ** KEYWORDS: sqlite3_version, sqlite3_sourceid
117 **
@@ -558,11 +558,14 @@
558 ** to xWrite(). The SQLITE_IOCAP_POWERSAFE_OVERWRITE property means that
559 ** after reboot following a crash or power loss, the only bytes in a
560 ** file that were written at the application level might have changed
561 ** and that adjacent bytes, even bytes within the same sector are
562 ** guaranteed to be unchanged. The SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN
563 ** flag indicate that a file cannot be deleted when open. The
564 ** SQLITE_IOCAP_IMMUTABLE flag indicates that the file is on
565 ** read-only media and cannot be changed even by processes with
566 ** elevated privileges.
567 */
568 #define SQLITE_IOCAP_ATOMIC 0x00000001
569 #define SQLITE_IOCAP_ATOMIC512 0x00000002
570 #define SQLITE_IOCAP_ATOMIC1K 0x00000004
571 #define SQLITE_IOCAP_ATOMIC2K 0x00000008
@@ -573,10 +576,11 @@
576 #define SQLITE_IOCAP_ATOMIC64K 0x00000100
577 #define SQLITE_IOCAP_SAFE_APPEND 0x00000200
578 #define SQLITE_IOCAP_SEQUENTIAL 0x00000400
579 #define SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN 0x00000800
580 #define SQLITE_IOCAP_POWERSAFE_OVERWRITE 0x00001000
581 #define SQLITE_IOCAP_IMMUTABLE 0x00002000
582
583 /*
584 ** CAPI3REF: File Locking Levels
585 **
586 ** SQLite uses one of these integer values as the second
@@ -2777,10 +2781,34 @@
2781 ** sqlite3_open_v2(). ^Setting the cache parameter to "private" is
2782 ** equivalent to setting the SQLITE_OPEN_PRIVATECACHE bit.
2783 ** ^If sqlite3_open_v2() is used and the "cache" parameter is present in
2784 ** a URI filename, its value overrides any behavior requested by setting
2785 ** SQLITE_OPEN_PRIVATECACHE or SQLITE_OPEN_SHAREDCACHE flag.
2786 **
2787 ** <li> <b>psow</b>: ^The psow parameter may be "true" (or "on" or "yes" or
2788 ** "1") or "false" (or "off" or "no" or "0") to indicate that the
2789 ** [powersafe overwrite] property does or does not apply to the
2790 ** storage media on which the database file resides. ^The psow query
2791 ** parameter only works for the built-in unix and Windows VFSes.
2792 **
2793 ** <li> <b>nolock</b>: ^The nolock parameter is a boolean query parameter
2794 ** which if set disables file locking in rollback journal modes. This
2795 ** is useful for accessing a database on a filesystem that does not
2796 ** support locking. Caution: Database corruption might result if two
2797 ** or more processes write to the same database and any one of those
2798 ** processes uses nolock=1.
2799 **
2800 ** <li> <b>immutable</b>: ^The immutable parameter is a boolean query
2801 ** parameter that indicates that the database file is stored on
2802 ** read-only media. ^When immutable is set, SQLite assumes that the
2803 ** database file cannot be changed, even by a process with higher
2804 ** privilege, and so the database is opened read-only and all locking
2805 ** and change detection is disabled. Caution: Setting the immutable
2806 ** property on a database file that does in fact change can result
2807 ** in incorrect query results and/or [SQLITE_CORRUPT] errors.
2808 ** See also: [SQLITE_IOCAP_IMMUTABLE].
2809 **
2810 ** </ul>
2811 **
2812 ** ^Specifying an unknown parameter in the query component of a URI is not an
2813 ** error. Future versions of SQLite might understand additional query
2814 ** parameters. See "[query parameters with special meaning to SQLite]" for
@@ -2806,12 +2834,13 @@
2834 ** in URI filenames.
2835 ** <tr><td> file:data.db?mode=ro&cache=private <td>
2836 ** Open file "data.db" in the current directory for read-only access.
2837 ** Regardless of whether or not shared-cache mode is enabled by
2838 ** default, use a private cache.
2839 ** <tr><td> file:/home/fred/data.db?vfs=unix-dotfile <td>
2840 ** Open file "/home/fred/data.db". Use the special VFS "unix-dotfile"
2841 ** that uses dot-files in place of posix advisory locking.
2842 ** <tr><td> file:data.db?mode=readonly <td>
2843 ** An error. "readonly" is not a valid option for the "mode" parameter.
2844 ** </table>
2845 **
2846 ** ^URI hexadecimal escape sequences (%HH) are supported within the path and
@@ -7345,10 +7374,20 @@
7374 #ifdef __cplusplus
7375 extern "C" {
7376 #endif
7377
7378 typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry;
7379 typedef struct sqlite3_rtree_query_info sqlite3_rtree_query_info;
7380
7381 /* The double-precision datatype used by RTree depends on the
7382 ** SQLITE_RTREE_INT_ONLY compile-time option.
7383 */
7384 #ifdef SQLITE_RTREE_INT_ONLY
7385 typedef sqlite3_int64 sqlite3_rtree_dbl;
7386 #else
7387 typedef double sqlite3_rtree_dbl;
7388 #endif
7389
7390 /*
7391 ** Register a geometry callback named zGeom that can be used as part of an
7392 ** R-Tree geometry query as follows:
7393 **
@@ -7355,15 +7394,11 @@
7394 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zGeom(... params ...)
7395 */
7396 SQLITE_API int sqlite3_rtree_geometry_callback(
7397 sqlite3 *db,
7398 const char *zGeom,
7399 int (*xGeom)(sqlite3_rtree_geometry*, int, sqlite3_rtree_dbl*,int*),
 
 
 
 
7400 void *pContext
7401 );
7402
7403
7404 /*
@@ -7371,17 +7406,66 @@
7406 ** argument to callbacks registered using rtree_geometry_callback().
7407 */
7408 struct sqlite3_rtree_geometry {
7409 void *pContext; /* Copy of pContext passed to s_r_g_c() */
7410 int nParam; /* Size of array aParam[] */
7411 sqlite3_rtree_dbl *aParam; /* Parameters passed to SQL geom function */
7412 void *pUser; /* Callback implementation user data */
7413 void (*xDelUser)(void *); /* Called by SQLite to clean up pUser */
7414 };
7415
7416 /*
7417 ** Register a 2nd-generation geometry callback named zScore that can be
7418 ** used as part of an R-Tree geometry query as follows:
7419 **
7420 ** SELECT ... FROM <rtree> WHERE <rtree col> MATCH $zQueryFunc(... params ...)
7421 */
7422 SQLITE_API int sqlite3_rtree_query_callback(
7423 sqlite3 *db,
7424 const char *zQueryFunc,
7425 int (*xQueryFunc)(sqlite3_rtree_query_info*),
7426 void *pContext,
7427 void (*xDestructor)(void*)
7428 );
7429
7430
7431 /*
7432 ** A pointer to a structure of the following type is passed as the
7433 ** argument to scored geometry callback registered using
7434 ** sqlite3_rtree_query_callback().
7435 **
7436 ** Note that the first 5 fields of this structure are identical to
7437 ** sqlite3_rtree_geometry. This structure is a subclass of
7438 ** sqlite3_rtree_geometry.
7439 */
7440 struct sqlite3_rtree_query_info {
7441 void *pContext; /* pContext from when function registered */
7442 int nParam; /* Number of function parameters */
7443 sqlite3_rtree_dbl *aParam; /* value of function parameters */
7444 void *pUser; /* callback can use this, if desired */
7445 void (*xDelUser)(void*); /* function to free pUser */
7446 sqlite3_rtree_dbl *aCoord; /* Coordinates of node or entry to check */
7447 unsigned int *anQueue; /* Number of pending entries in the queue */
7448 int nCoord; /* Number of coordinates */
7449 int iLevel; /* Level of current node or entry */
7450 int mxLevel; /* The largest iLevel value in the tree */
7451 sqlite3_int64 iRowid; /* Rowid for current entry */
7452 sqlite3_rtree_dbl rParentScore; /* Score of parent node */
7453 int eParentWithin; /* Visibility of parent node */
7454 int eWithin; /* OUT: Visiblity */
7455 sqlite3_rtree_dbl rScore; /* OUT: Write the score here */
7456 };
7457
7458 /*
7459 ** Allowed values for sqlite3_rtree_query.eWithin and .eParentWithin.
7460 */
7461 #define NOT_WITHIN 0 /* Object completely outside of query region */
7462 #define PARTLY_WITHIN 1 /* Object partially overlaps query region */
7463 #define FULLY_WITHIN 2 /* Object fully contained within query region */
7464
7465
7466 #ifdef __cplusplus
7467 } /* end of the 'extern "C"' block */
7468 #endif
7469
7470 #endif /* ifndef _SQLITE3RTREE_H_ */
7471
7472
+2 -1
--- src/style.c
+++ src/style.c
@@ -299,11 +299,12 @@
299299
Th_Store("project_name", db_get("project-name","Unnamed Fossil Project"));
300300
Th_Store("title", zTitle);
301301
Th_Store("baseurl", g.zBaseURL);
302302
Th_Store("home", g.zTop);
303303
Th_Store("index_page", db_get("index-page","/home"));
304
- Th_Store("current_page", local_zCurrentPage ? local_zCurrentPage : g.zPath);
304
+ if( local_zCurrentPage==0 ) style_set_current_page("%T", g.zPath);
305
+ Th_Store("current_page", local_zCurrentPage);
305306
Th_Store("csrf_token", g.zCsrfToken);
306307
Th_Store("release_version", RELEASE_VERSION);
307308
Th_Store("manifest_version", MANIFEST_VERSION);
308309
Th_Store("manifest_date", MANIFEST_DATE);
309310
Th_Store("compiler_name", COMPILER_NAME);
310311
--- src/style.c
+++ src/style.c
@@ -299,11 +299,12 @@
299 Th_Store("project_name", db_get("project-name","Unnamed Fossil Project"));
300 Th_Store("title", zTitle);
301 Th_Store("baseurl", g.zBaseURL);
302 Th_Store("home", g.zTop);
303 Th_Store("index_page", db_get("index-page","/home"));
304 Th_Store("current_page", local_zCurrentPage ? local_zCurrentPage : g.zPath);
 
305 Th_Store("csrf_token", g.zCsrfToken);
306 Th_Store("release_version", RELEASE_VERSION);
307 Th_Store("manifest_version", MANIFEST_VERSION);
308 Th_Store("manifest_date", MANIFEST_DATE);
309 Th_Store("compiler_name", COMPILER_NAME);
310
--- src/style.c
+++ src/style.c
@@ -299,11 +299,12 @@
299 Th_Store("project_name", db_get("project-name","Unnamed Fossil Project"));
300 Th_Store("title", zTitle);
301 Th_Store("baseurl", g.zBaseURL);
302 Th_Store("home", g.zTop);
303 Th_Store("index_page", db_get("index-page","/home"));
304 if( local_zCurrentPage==0 ) style_set_current_page("%T", g.zPath);
305 Th_Store("current_page", local_zCurrentPage);
306 Th_Store("csrf_token", g.zCsrfToken);
307 Th_Store("release_version", RELEASE_VERSION);
308 Th_Store("manifest_version", MANIFEST_VERSION);
309 Th_Store("manifest_date", MANIFEST_DATE);
310 Th_Store("compiler_name", COMPILER_NAME);
311
--- src/timeline.c
+++ src/timeline.c
@@ -1739,10 +1739,12 @@
17391739
** -v|--verbose Output the list of files changed by each commit
17401740
** and the type of each change (edited, deleted,
17411741
** etc.) after the checkin comment.
17421742
** -W|--width <num> With of lines (default 79). Must be >20 or 0
17431743
** (= no limit, resulting in a single line per entry).
1744
+** -R REPO_FILE Specifies the repository db to use. Default is
1745
+** the current checkout's repository.
17441746
*/
17451747
void timeline_cmd(void){
17461748
Stmt q;
17471749
int n, k, width;
17481750
const char *zLimit;
17491751
--- src/timeline.c
+++ src/timeline.c
@@ -1739,10 +1739,12 @@
1739 ** -v|--verbose Output the list of files changed by each commit
1740 ** and the type of each change (edited, deleted,
1741 ** etc.) after the checkin comment.
1742 ** -W|--width <num> With of lines (default 79). Must be >20 or 0
1743 ** (= no limit, resulting in a single line per entry).
 
 
1744 */
1745 void timeline_cmd(void){
1746 Stmt q;
1747 int n, k, width;
1748 const char *zLimit;
1749
--- src/timeline.c
+++ src/timeline.c
@@ -1739,10 +1739,12 @@
1739 ** -v|--verbose Output the list of files changed by each commit
1740 ** and the type of each change (edited, deleted,
1741 ** etc.) after the checkin comment.
1742 ** -W|--width <num> With of lines (default 79). Must be >20 or 0
1743 ** (= no limit, resulting in a single line per entry).
1744 ** -R REPO_FILE Specifies the repository db to use. Default is
1745 ** the current checkout's repository.
1746 */
1747 void timeline_cmd(void){
1748 Stmt q;
1749 int n, k, width;
1750 const char *zLimit;
1751
+3 -3
--- src/vfile.c
+++ src/vfile.c
@@ -728,11 +728,11 @@
728728
const char *zOrigName = db_column_text(&q, 2);
729729
char zBuf[100];
730730
Blob file;
731731
732732
if( zOrigName ) zName = zOrigName;
733
- if( rid>0 || vid==0 ){
733
+ if( rid>0 ){
734734
md5sum_step_text(zName, -1);
735735
blob_zero(&file);
736736
content_get(rid, &file);
737737
sqlite3_snprintf(sizeof(zBuf), zBuf, " %d\n", blob_size(&file));
738738
md5sum_step_text(zBuf, -1);
@@ -837,13 +837,13 @@
837837
db_must_be_within_tree();
838838
839839
db_prepare(&q, "SELECT pathname, origname, rid, is_selected(id)"
840840
" FROM vfile"
841841
" WHERE (NOT deleted OR NOT is_selected(id))"
842
- " %s AND vid=%d"
842
+ " AND rid>0 AND vid=%d"
843843
" ORDER BY if_selected(id,pathname,origname) /*scan*/",
844
- (vid ? "AND rid>0" : ""), vid);
844
+ vid);
845845
blob_zero(&file);
846846
md5sum_init();
847847
while( db_step(&q)==SQLITE_ROW ){
848848
const char *zName = db_column_text(&q, 0);
849849
const char *zOrigName = db_column_text(&q, 1);
850850
--- src/vfile.c
+++ src/vfile.c
@@ -728,11 +728,11 @@
728 const char *zOrigName = db_column_text(&q, 2);
729 char zBuf[100];
730 Blob file;
731
732 if( zOrigName ) zName = zOrigName;
733 if( rid>0 || vid==0 ){
734 md5sum_step_text(zName, -1);
735 blob_zero(&file);
736 content_get(rid, &file);
737 sqlite3_snprintf(sizeof(zBuf), zBuf, " %d\n", blob_size(&file));
738 md5sum_step_text(zBuf, -1);
@@ -837,13 +837,13 @@
837 db_must_be_within_tree();
838
839 db_prepare(&q, "SELECT pathname, origname, rid, is_selected(id)"
840 " FROM vfile"
841 " WHERE (NOT deleted OR NOT is_selected(id))"
842 " %s AND vid=%d"
843 " ORDER BY if_selected(id,pathname,origname) /*scan*/",
844 (vid ? "AND rid>0" : ""), vid);
845 blob_zero(&file);
846 md5sum_init();
847 while( db_step(&q)==SQLITE_ROW ){
848 const char *zName = db_column_text(&q, 0);
849 const char *zOrigName = db_column_text(&q, 1);
850
--- src/vfile.c
+++ src/vfile.c
@@ -728,11 +728,11 @@
728 const char *zOrigName = db_column_text(&q, 2);
729 char zBuf[100];
730 Blob file;
731
732 if( zOrigName ) zName = zOrigName;
733 if( rid>0 ){
734 md5sum_step_text(zName, -1);
735 blob_zero(&file);
736 content_get(rid, &file);
737 sqlite3_snprintf(sizeof(zBuf), zBuf, " %d\n", blob_size(&file));
738 md5sum_step_text(zBuf, -1);
@@ -837,13 +837,13 @@
837 db_must_be_within_tree();
838
839 db_prepare(&q, "SELECT pathname, origname, rid, is_selected(id)"
840 " FROM vfile"
841 " WHERE (NOT deleted OR NOT is_selected(id))"
842 " AND rid>0 AND vid=%d"
843 " ORDER BY if_selected(id,pathname,origname) /*scan*/",
844 vid);
845 blob_zero(&file);
846 md5sum_init();
847 while( db_step(&q)==SQLITE_ROW ){
848 const char *zName = db_column_text(&q, 0);
849 const char *zOrigName = db_column_text(&q, 1);
850
+45 -23
--- src/wiki.c
+++ src/wiki.c
@@ -269,11 +269,11 @@
269269
if( g.perm.Hyperlink ){
270270
style_submenu_element("History", "History", "%s/whistory?name=%T",
271271
g.zTop, zPageName);
272272
}
273273
}
274
- style_set_current_page("%s?name=%T", g.zPath, zPageName);
274
+ style_set_current_page("%T?name=%T", g.zPath, zPageName);
275275
style_header(zPageName);
276276
blob_init(&wiki, zBody, -1);
277277
wiki_render_by_mimetype(&wiki, zMimetype);
278278
blob_reset(&wiki);
279279
attachment_list(zPageName, "<hr /><h2>Attachments:</h2><ul>");
@@ -439,11 +439,11 @@
439439
return;
440440
}
441441
if( zBody==0 ){
442442
zBody = mprintf("<i>Empty Page</i>");
443443
}
444
- style_set_current_page("%s?name=%T", g.zPath, zPageName);
444
+ style_set_current_page("%T?name=%T", g.zPath, zPageName);
445445
style_header("Edit: %s", zPageName);
446446
if( !goodCaptcha ){
447447
@ <p class="generalError">Error: Incorrect security code.</p>
448448
}
449449
blob_zero(&wiki);
@@ -667,11 +667,11 @@
667667
}
668668
if( P("cancel")!=0 ){
669669
cgi_redirectf("wiki?name=%T", zPageName);
670670
return;
671671
}
672
- style_set_current_page("%s?name=%T", g.zPath, zPageName);
672
+ style_set_current_page("%T?name=%T", g.zPath, zPageName);
673673
style_header("Append Comment To: %s", zPageName);
674674
if( !goodCaptcha ){
675675
@ <p class="generalError">Error: Incorrect security code.</p>
676676
}
677677
if( P("preview")!=0 ){
@@ -955,18 +955,23 @@
955955
** given by the zPageName parameter. isNew must be true to create
956956
** a new page. If no previous page with the name zPageName exists
957957
** and isNew is false, then this routine throws an error.
958958
**
959959
** The content of the new page is given by the blob pContent.
960
+**
961
+** zMimeType specifies the N-card for the wiki page. If it is 0,
962
+** empty, or "text/x-fossil-wiki" (the default format) then it is
963
+** ignored.
960964
*/
961
-int wiki_cmd_commit(char const * zPageName, int isNew, Blob *pContent){
965
+int wiki_cmd_commit(char const * zPageName, int isNew, Blob *pContent,
966
+ char const * zMimeType){
962967
Blob wiki; /* Wiki page content */
963968
Blob cksum; /* wiki checksum */
964969
int rid; /* artifact ID of parent page */
965970
char *zDate; /* timestamp */
966971
char *zUuid; /* uuid for rid */
967
-
972
+
968973
rid = db_int(0,
969974
"SELECT x.rid FROM tag t, tagxref x"
970975
" WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
971976
" ORDER BY x.mtime DESC LIMIT 1",
972977
zPageName
@@ -987,10 +992,14 @@
987992
blob_zero(&wiki);
988993
zDate = date_in_standard_format("now");
989994
blob_appendf(&wiki, "D %s\n", zDate);
990995
free(zDate);
991996
blob_appendf(&wiki, "L %F\n", zPageName );
997
+ if( zMimeType && *zMimeType
998
+ && 0!=fossil_strcmp(zMimeType,"text/x-fossil-wiki") ){
999
+ blob_appendf(&wiki, "N %F\n", zMimeType);
1000
+ }
9921001
if( rid ){
9931002
zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
9941003
blob_appendf(&wiki, "P %s\n", zUuid);
9951004
free(zUuid);
9961005
}
@@ -1019,16 +1028,18 @@
10191028
** %fossil wiki export PAGENAME ?FILE?
10201029
**
10211030
** Sends the latest version of the PAGENAME wiki
10221031
** entry to the given file or standard output.
10231032
**
1024
-** %fossil wiki commit PAGENAME ?FILE?
1033
+** %fossil wiki commit PAGENAME ?FILE? [-mimetype TEXT-FORMAT]
10251034
**
10261035
** Commit changes to a wiki page from FILE or from standard
1027
-** input.
1036
+** input. The -mimetype (-M) flag specifies the mime type,
1037
+** defaulting to the type used by the previous version of
1038
+** the page or (for new pages) text/x-fossil-wiki.
10281039
**
1029
-** %fossil wiki create PAGENAME ?FILE?
1040
+** %fossil wiki create PAGENAME ?FILE? [-mimetype TEXT-FORMAT]
10301041
**
10311042
** Create a new wiki page with initial content taken from
10321043
** FILE or from standard input.
10331044
**
10341045
** %fossil wiki list
@@ -1054,11 +1065,10 @@
10541065
int rid; /* Artifact ID of the wiki page */
10551066
int i; /* Loop counter */
10561067
char *zBody = 0; /* Wiki page content */
10571068
Blob body; /* Wiki page content */
10581069
Manifest *pWiki = 0; /* Parsed wiki page content */
1059
-
10601070
if( (g.argc!=4) && (g.argc!=5) ){
10611071
usage("export PAGENAME ?FILE?");
10621072
}
10631073
zPageName = g.argv[3];
10641074
rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
@@ -1079,40 +1089,53 @@
10791089
blob_append(&body, "\n", 1);
10801090
blob_write_to_file(&body, zFile);
10811091
blob_reset(&body);
10821092
manifest_destroy(pWiki);
10831093
return;
1084
- }else
1085
- if( strncmp(g.argv[2],"commit",n)==0
1086
- || strncmp(g.argv[2],"create",n)==0 ){
1087
- char *zPageName;
1088
- Blob content;
1094
+ }else if( strncmp(g.argv[2],"commit",n)==0
1095
+ || strncmp(g.argv[2],"create",n)==0 ){
1096
+ char const *zPageName; /* page name */
1097
+ Blob content; /* Input content */
1098
+ int rid;
1099
+ Manifest *pWiki = 0; /* Parsed wiki page content */
1100
+ char const * zMimeType = find_option("mimetype", "M", 1);
10891101
if( g.argc!=4 && g.argc!=5 ){
1090
- usage("commit PAGENAME ?FILE?");
1102
+ usage("commit|create PAGENAME ?FILE? [-mimetype TEXT-FORMAT]");
10911103
}
10921104
zPageName = g.argv[3];
10931105
if( g.argc==4 ){
10941106
blob_read_from_channel(&content, stdin, -1);
10951107
}else{
10961108
blob_read_from_file(&content, g.argv[4]);
1109
+ }
1110
+ if(!zMimeType || !*zMimeType){
1111
+ /* Try to deduce the mime type based on the prior version. */
1112
+ rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
1113
+ " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
1114
+ " ORDER BY x.mtime DESC LIMIT 1",
1115
+ zPageName
1116
+ );
1117
+ if(rid>0 && (pWiki = manifest_get(rid, CFTYPE_WIKI, 0))!=0
1118
+ && (pWiki->zMimetype && *pWiki->zMimetype)){
1119
+ zMimeType = pWiki->zMimetype;
1120
+ }
10971121
}
10981122
if( g.argv[2][1]=='r' ){
1099
- wiki_cmd_commit(zPageName, 1, &content);
1123
+ wiki_cmd_commit(zPageName, 1, &content, zMimeType);
11001124
fossil_print("Created new wiki page %s.\n", zPageName);
11011125
}else{
1102
- wiki_cmd_commit(zPageName, 0, &content);
1126
+ wiki_cmd_commit(zPageName, 0, &content, zMimeType);
11031127
fossil_print("Updated wiki page %s.\n", zPageName);
11041128
}
1129
+ manifest_destroy(pWiki);
11051130
blob_reset(&content);
1106
- }else
1107
- if( strncmp(g.argv[2],"delete",n)==0 ){
1131
+ }else if( strncmp(g.argv[2],"delete",n)==0 ){
11081132
if( g.argc!=5 ){
11091133
usage("delete PAGENAME");
11101134
}
11111135
fossil_fatal("delete not yet implemented.");
1112
- }else
1113
- if( strncmp(g.argv[2],"list",n)==0 ){
1136
+ }else if( strncmp(g.argv[2],"list",n)==0 ){
11141137
Stmt q;
11151138
db_prepare(&q,
11161139
"SELECT substr(tagname, 6) FROM tag WHERE tagname GLOB 'wiki-*'"
11171140
" ORDER BY lower(tagname) /*sort*/"
11181141
);
@@ -1119,14 +1142,13 @@
11191142
while( db_step(&q)==SQLITE_ROW ){
11201143
const char *zName = db_column_text(&q, 0);
11211144
fossil_print( "%s\n",zName);
11221145
}
11231146
db_finalize(&q);
1124
- }else
1125
- {
1147
+ }else{
11261148
goto wiki_cmd_usage;
11271149
}
11281150
return;
11291151
11301152
wiki_cmd_usage:
11311153
usage("export|create|commit|list ...");
11321154
}
11331155
--- src/wiki.c
+++ src/wiki.c
@@ -269,11 +269,11 @@
269 if( g.perm.Hyperlink ){
270 style_submenu_element("History", "History", "%s/whistory?name=%T",
271 g.zTop, zPageName);
272 }
273 }
274 style_set_current_page("%s?name=%T", g.zPath, zPageName);
275 style_header(zPageName);
276 blob_init(&wiki, zBody, -1);
277 wiki_render_by_mimetype(&wiki, zMimetype);
278 blob_reset(&wiki);
279 attachment_list(zPageName, "<hr /><h2>Attachments:</h2><ul>");
@@ -439,11 +439,11 @@
439 return;
440 }
441 if( zBody==0 ){
442 zBody = mprintf("<i>Empty Page</i>");
443 }
444 style_set_current_page("%s?name=%T", g.zPath, zPageName);
445 style_header("Edit: %s", zPageName);
446 if( !goodCaptcha ){
447 @ <p class="generalError">Error: Incorrect security code.</p>
448 }
449 blob_zero(&wiki);
@@ -667,11 +667,11 @@
667 }
668 if( P("cancel")!=0 ){
669 cgi_redirectf("wiki?name=%T", zPageName);
670 return;
671 }
672 style_set_current_page("%s?name=%T", g.zPath, zPageName);
673 style_header("Append Comment To: %s", zPageName);
674 if( !goodCaptcha ){
675 @ <p class="generalError">Error: Incorrect security code.</p>
676 }
677 if( P("preview")!=0 ){
@@ -955,18 +955,23 @@
955 ** given by the zPageName parameter. isNew must be true to create
956 ** a new page. If no previous page with the name zPageName exists
957 ** and isNew is false, then this routine throws an error.
958 **
959 ** The content of the new page is given by the blob pContent.
 
 
 
 
960 */
961 int wiki_cmd_commit(char const * zPageName, int isNew, Blob *pContent){
 
962 Blob wiki; /* Wiki page content */
963 Blob cksum; /* wiki checksum */
964 int rid; /* artifact ID of parent page */
965 char *zDate; /* timestamp */
966 char *zUuid; /* uuid for rid */
967
968 rid = db_int(0,
969 "SELECT x.rid FROM tag t, tagxref x"
970 " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
971 " ORDER BY x.mtime DESC LIMIT 1",
972 zPageName
@@ -987,10 +992,14 @@
987 blob_zero(&wiki);
988 zDate = date_in_standard_format("now");
989 blob_appendf(&wiki, "D %s\n", zDate);
990 free(zDate);
991 blob_appendf(&wiki, "L %F\n", zPageName );
 
 
 
 
992 if( rid ){
993 zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
994 blob_appendf(&wiki, "P %s\n", zUuid);
995 free(zUuid);
996 }
@@ -1019,16 +1028,18 @@
1019 ** %fossil wiki export PAGENAME ?FILE?
1020 **
1021 ** Sends the latest version of the PAGENAME wiki
1022 ** entry to the given file or standard output.
1023 **
1024 ** %fossil wiki commit PAGENAME ?FILE?
1025 **
1026 ** Commit changes to a wiki page from FILE or from standard
1027 ** input.
 
 
1028 **
1029 ** %fossil wiki create PAGENAME ?FILE?
1030 **
1031 ** Create a new wiki page with initial content taken from
1032 ** FILE or from standard input.
1033 **
1034 ** %fossil wiki list
@@ -1054,11 +1065,10 @@
1054 int rid; /* Artifact ID of the wiki page */
1055 int i; /* Loop counter */
1056 char *zBody = 0; /* Wiki page content */
1057 Blob body; /* Wiki page content */
1058 Manifest *pWiki = 0; /* Parsed wiki page content */
1059
1060 if( (g.argc!=4) && (g.argc!=5) ){
1061 usage("export PAGENAME ?FILE?");
1062 }
1063 zPageName = g.argv[3];
1064 rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
@@ -1079,40 +1089,53 @@
1079 blob_append(&body, "\n", 1);
1080 blob_write_to_file(&body, zFile);
1081 blob_reset(&body);
1082 manifest_destroy(pWiki);
1083 return;
1084 }else
1085 if( strncmp(g.argv[2],"commit",n)==0
1086 || strncmp(g.argv[2],"create",n)==0 ){
1087 char *zPageName;
1088 Blob content;
 
 
1089 if( g.argc!=4 && g.argc!=5 ){
1090 usage("commit PAGENAME ?FILE?");
1091 }
1092 zPageName = g.argv[3];
1093 if( g.argc==4 ){
1094 blob_read_from_channel(&content, stdin, -1);
1095 }else{
1096 blob_read_from_file(&content, g.argv[4]);
 
 
 
 
 
 
 
 
 
 
 
 
1097 }
1098 if( g.argv[2][1]=='r' ){
1099 wiki_cmd_commit(zPageName, 1, &content);
1100 fossil_print("Created new wiki page %s.\n", zPageName);
1101 }else{
1102 wiki_cmd_commit(zPageName, 0, &content);
1103 fossil_print("Updated wiki page %s.\n", zPageName);
1104 }
 
1105 blob_reset(&content);
1106 }else
1107 if( strncmp(g.argv[2],"delete",n)==0 ){
1108 if( g.argc!=5 ){
1109 usage("delete PAGENAME");
1110 }
1111 fossil_fatal("delete not yet implemented.");
1112 }else
1113 if( strncmp(g.argv[2],"list",n)==0 ){
1114 Stmt q;
1115 db_prepare(&q,
1116 "SELECT substr(tagname, 6) FROM tag WHERE tagname GLOB 'wiki-*'"
1117 " ORDER BY lower(tagname) /*sort*/"
1118 );
@@ -1119,14 +1142,13 @@
1119 while( db_step(&q)==SQLITE_ROW ){
1120 const char *zName = db_column_text(&q, 0);
1121 fossil_print( "%s\n",zName);
1122 }
1123 db_finalize(&q);
1124 }else
1125 {
1126 goto wiki_cmd_usage;
1127 }
1128 return;
1129
1130 wiki_cmd_usage:
1131 usage("export|create|commit|list ...");
1132 }
1133
--- src/wiki.c
+++ src/wiki.c
@@ -269,11 +269,11 @@
269 if( g.perm.Hyperlink ){
270 style_submenu_element("History", "History", "%s/whistory?name=%T",
271 g.zTop, zPageName);
272 }
273 }
274 style_set_current_page("%T?name=%T", g.zPath, zPageName);
275 style_header(zPageName);
276 blob_init(&wiki, zBody, -1);
277 wiki_render_by_mimetype(&wiki, zMimetype);
278 blob_reset(&wiki);
279 attachment_list(zPageName, "<hr /><h2>Attachments:</h2><ul>");
@@ -439,11 +439,11 @@
439 return;
440 }
441 if( zBody==0 ){
442 zBody = mprintf("<i>Empty Page</i>");
443 }
444 style_set_current_page("%T?name=%T", g.zPath, zPageName);
445 style_header("Edit: %s", zPageName);
446 if( !goodCaptcha ){
447 @ <p class="generalError">Error: Incorrect security code.</p>
448 }
449 blob_zero(&wiki);
@@ -667,11 +667,11 @@
667 }
668 if( P("cancel")!=0 ){
669 cgi_redirectf("wiki?name=%T", zPageName);
670 return;
671 }
672 style_set_current_page("%T?name=%T", g.zPath, zPageName);
673 style_header("Append Comment To: %s", zPageName);
674 if( !goodCaptcha ){
675 @ <p class="generalError">Error: Incorrect security code.</p>
676 }
677 if( P("preview")!=0 ){
@@ -955,18 +955,23 @@
955 ** given by the zPageName parameter. isNew must be true to create
956 ** a new page. If no previous page with the name zPageName exists
957 ** and isNew is false, then this routine throws an error.
958 **
959 ** The content of the new page is given by the blob pContent.
960 **
961 ** zMimeType specifies the N-card for the wiki page. If it is 0,
962 ** empty, or "text/x-fossil-wiki" (the default format) then it is
963 ** ignored.
964 */
965 int wiki_cmd_commit(char const * zPageName, int isNew, Blob *pContent,
966 char const * zMimeType){
967 Blob wiki; /* Wiki page content */
968 Blob cksum; /* wiki checksum */
969 int rid; /* artifact ID of parent page */
970 char *zDate; /* timestamp */
971 char *zUuid; /* uuid for rid */
972
973 rid = db_int(0,
974 "SELECT x.rid FROM tag t, tagxref x"
975 " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
976 " ORDER BY x.mtime DESC LIMIT 1",
977 zPageName
@@ -987,10 +992,14 @@
992 blob_zero(&wiki);
993 zDate = date_in_standard_format("now");
994 blob_appendf(&wiki, "D %s\n", zDate);
995 free(zDate);
996 blob_appendf(&wiki, "L %F\n", zPageName );
997 if( zMimeType && *zMimeType
998 && 0!=fossil_strcmp(zMimeType,"text/x-fossil-wiki") ){
999 blob_appendf(&wiki, "N %F\n", zMimeType);
1000 }
1001 if( rid ){
1002 zUuid = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", rid);
1003 blob_appendf(&wiki, "P %s\n", zUuid);
1004 free(zUuid);
1005 }
@@ -1019,16 +1028,18 @@
1028 ** %fossil wiki export PAGENAME ?FILE?
1029 **
1030 ** Sends the latest version of the PAGENAME wiki
1031 ** entry to the given file or standard output.
1032 **
1033 ** %fossil wiki commit PAGENAME ?FILE? [-mimetype TEXT-FORMAT]
1034 **
1035 ** Commit changes to a wiki page from FILE or from standard
1036 ** input. The -mimetype (-M) flag specifies the mime type,
1037 ** defaulting to the type used by the previous version of
1038 ** the page or (for new pages) text/x-fossil-wiki.
1039 **
1040 ** %fossil wiki create PAGENAME ?FILE? [-mimetype TEXT-FORMAT]
1041 **
1042 ** Create a new wiki page with initial content taken from
1043 ** FILE or from standard input.
1044 **
1045 ** %fossil wiki list
@@ -1054,11 +1065,10 @@
1065 int rid; /* Artifact ID of the wiki page */
1066 int i; /* Loop counter */
1067 char *zBody = 0; /* Wiki page content */
1068 Blob body; /* Wiki page content */
1069 Manifest *pWiki = 0; /* Parsed wiki page content */
 
1070 if( (g.argc!=4) && (g.argc!=5) ){
1071 usage("export PAGENAME ?FILE?");
1072 }
1073 zPageName = g.argv[3];
1074 rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
@@ -1079,40 +1089,53 @@
1089 blob_append(&body, "\n", 1);
1090 blob_write_to_file(&body, zFile);
1091 blob_reset(&body);
1092 manifest_destroy(pWiki);
1093 return;
1094 }else if( strncmp(g.argv[2],"commit",n)==0
1095 || strncmp(g.argv[2],"create",n)==0 ){
1096 char const *zPageName; /* page name */
1097 Blob content; /* Input content */
1098 int rid;
1099 Manifest *pWiki = 0; /* Parsed wiki page content */
1100 char const * zMimeType = find_option("mimetype", "M", 1);
1101 if( g.argc!=4 && g.argc!=5 ){
1102 usage("commit|create PAGENAME ?FILE? [-mimetype TEXT-FORMAT]");
1103 }
1104 zPageName = g.argv[3];
1105 if( g.argc==4 ){
1106 blob_read_from_channel(&content, stdin, -1);
1107 }else{
1108 blob_read_from_file(&content, g.argv[4]);
1109 }
1110 if(!zMimeType || !*zMimeType){
1111 /* Try to deduce the mime type based on the prior version. */
1112 rid = db_int(0, "SELECT x.rid FROM tag t, tagxref x"
1113 " WHERE x.tagid=t.tagid AND t.tagname='wiki-%q'"
1114 " ORDER BY x.mtime DESC LIMIT 1",
1115 zPageName
1116 );
1117 if(rid>0 && (pWiki = manifest_get(rid, CFTYPE_WIKI, 0))!=0
1118 && (pWiki->zMimetype && *pWiki->zMimetype)){
1119 zMimeType = pWiki->zMimetype;
1120 }
1121 }
1122 if( g.argv[2][1]=='r' ){
1123 wiki_cmd_commit(zPageName, 1, &content, zMimeType);
1124 fossil_print("Created new wiki page %s.\n", zPageName);
1125 }else{
1126 wiki_cmd_commit(zPageName, 0, &content, zMimeType);
1127 fossil_print("Updated wiki page %s.\n", zPageName);
1128 }
1129 manifest_destroy(pWiki);
1130 blob_reset(&content);
1131 }else if( strncmp(g.argv[2],"delete",n)==0 ){
 
1132 if( g.argc!=5 ){
1133 usage("delete PAGENAME");
1134 }
1135 fossil_fatal("delete not yet implemented.");
1136 }else if( strncmp(g.argv[2],"list",n)==0 ){
 
1137 Stmt q;
1138 db_prepare(&q,
1139 "SELECT substr(tagname, 6) FROM tag WHERE tagname GLOB 'wiki-*'"
1140 " ORDER BY lower(tagname) /*sort*/"
1141 );
@@ -1119,14 +1142,13 @@
1142 while( db_step(&q)==SQLITE_ROW ){
1143 const char *zName = db_column_text(&q, 0);
1144 fossil_print( "%s\n",zName);
1145 }
1146 db_finalize(&q);
1147 }else{
 
1148 goto wiki_cmd_usage;
1149 }
1150 return;
1151
1152 wiki_cmd_usage:
1153 usage("export|create|commit|list ...");
1154 }
1155
--- test/release-checklist.wiki
+++ test/release-checklist.wiki
@@ -18,10 +18,13 @@
1818
<li><p>
1919
Click on each of the links in in the
2020
[./diff-test-1.wiki] document and verify that all diffs are
2121
rendered correctly.
2222
23
+<li><p>
24
+Click on the following link to verify that it works: [./test-page%2b%2b.wiki | ./test-page++.wiki]
25
+
2326
<li><p>
2427
Verify correct name-change tracking behavior (no net changes) for:
2528
<blockquote><b>
2629
fossil test-name-changes --debug b120bc8b262ac 374920b20944b
2730
</b></blockquote>
2831
2932
ADDED test/test-page++.wiki
--- test/release-checklist.wiki
+++ test/release-checklist.wiki
@@ -18,10 +18,13 @@
18 <li><p>
19 Click on each of the links in in the
20 [./diff-test-1.wiki] document and verify that all diffs are
21 rendered correctly.
22
 
 
 
23 <li><p>
24 Verify correct name-change tracking behavior (no net changes) for:
25 <blockquote><b>
26 fossil test-name-changes --debug b120bc8b262ac 374920b20944b
27 </b></blockquote>
28
29 DDED test/test-page++.wiki
--- test/release-checklist.wiki
+++ test/release-checklist.wiki
@@ -18,10 +18,13 @@
18 <li><p>
19 Click on each of the links in in the
20 [./diff-test-1.wiki] document and verify that all diffs are
21 rendered correctly.
22
23 <li><p>
24 Click on the following link to verify that it works: [./test-page%2b%2b.wiki | ./test-page++.wiki]
25
26 <li><p>
27 Verify correct name-change tracking behavior (no net changes) for:
28 <blockquote><b>
29 fossil test-name-changes --debug b120bc8b262ac 374920b20944b
30 </b></blockquote>
31
32 DDED test/test-page++.wiki
--- a/test/test-page++.wiki
+++ b/test/test-page++.wiki
@@ -0,0 +1,7 @@
1
+<title>Test Page</title>
2
+
3
+The purpose of this page is to test Fossil's ability to deal with
4
+embedded documentation pages that contain characters that should be
5
+escaped in URLs.
6
+
7
+Here is a link to the [./release-checklist.wiki | release checklist].
--- a/test/test-page++.wiki
+++ b/test/test-page++.wiki
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
--- a/test/test-page++.wiki
+++ b/test/test-page++.wiki
@@ -0,0 +1,7 @@
1 <title>Test Page</title>
2
3 The purpose of this page is to test Fossil's ability to deal with
4 embedded documentation pages that contain characters that should be
5 escaped in URLs.
6
7 Here is a link to the [./release-checklist.wiki | release checklist].
--- www/branching.wiki
+++ www/branching.wiki
@@ -56,11 +56,11 @@
5656
a leaf. Fossil sees that check-in 3 has occurred and aborts Bob's commit
5757
attempt with a message "would fork." This allows Bob to do a "fossil
5858
update" which pulls in Alice's changes, merging them into his own
5959
changes. After merging, Bob commits check-in 4 as a child of check-in 3.
6060
The result is a linear graph as shown in figure 1. This is how CVS
61
-works. This is also how fossil works in [concepts.wiki#workflow |
61
+works. This is also how fossil works in [./concepts.wiki#workflow |
6262
"autosync"] mode.
6363
6464
But perhaps Bob is off-network when he does his commit, so he
6565
has no way of knowing that Alice has already committed her changes.
6666
Or, it could be that Bob has turned off "autosync" mode in Fossil. Or,
6767
--- www/branching.wiki
+++ www/branching.wiki
@@ -56,11 +56,11 @@
56 a leaf. Fossil sees that check-in 3 has occurred and aborts Bob's commit
57 attempt with a message "would fork." This allows Bob to do a "fossil
58 update" which pulls in Alice's changes, merging them into his own
59 changes. After merging, Bob commits check-in 4 as a child of check-in 3.
60 The result is a linear graph as shown in figure 1. This is how CVS
61 works. This is also how fossil works in [concepts.wiki#workflow |
62 "autosync"] mode.
63
64 But perhaps Bob is off-network when he does his commit, so he
65 has no way of knowing that Alice has already committed her changes.
66 Or, it could be that Bob has turned off "autosync" mode in Fossil. Or,
67
--- www/branching.wiki
+++ www/branching.wiki
@@ -56,11 +56,11 @@
56 a leaf. Fossil sees that check-in 3 has occurred and aborts Bob's commit
57 attempt with a message "would fork." This allows Bob to do a "fossil
58 update" which pulls in Alice's changes, merging them into his own
59 changes. After merging, Bob commits check-in 4 as a child of check-in 3.
60 The result is a linear graph as shown in figure 1. This is how CVS
61 works. This is also how fossil works in [./concepts.wiki#workflow |
62 "autosync"] mode.
63
64 But perhaps Bob is off-network when he does his commit, so he
65 has no way of knowing that Alice has already committed her changes.
66 Or, it could be that Bob has turned off "autosync" mode in Fossil. Or,
67
--- www/changes.wiki
+++ www/changes.wiki
@@ -48,17 +48,18 @@
4848
* Issue a warning if a [/help?cmd=add|fossil add] command tries to add a file
4949
that matches the ignore-glob.
5050
* Add option -W|--width to "[/help?cmd=stash|fossil stash ls]"
5151
and "[/help?cmd=leaves|fossil leaves]" commands.
5252
* Enhance support for running as the root user. Now works on Haiku.
53
- * [/help?cmd=new|fossil new] no longer creates an initial empty commit by
54
- default. The first commit after checking out a new empty repository will
55
- become the initial commit.
53
+ * Added the <tt>-empty</tt> option to [/help?cmd=new|fossil new], which
54
+ causes it to not create an initial empty commit. The first commit after
55
+ checking out a repo created this way will become the initial commit.
5656
* Enhance sync operations by committing each round-trip to minimize number
5757
of retransmits when autosync fails. Include option for
5858
[/help?cmd=update| fossil update] and [/help?cmd=merge| fossil merge] to
5959
continue even if missing content.
60
+
6061
6162
<h2>Changes For Version 1.28 (2014-01-27)</h2>
6263
* Enhance [/help?cmd=/reports | /reports] to support event type filtering.
6364
* When cloning a repository, the user name passed via the URL (if any)
6465
is now used as the default local admin user's name.
6566
--- www/changes.wiki
+++ www/changes.wiki
@@ -48,17 +48,18 @@
48 * Issue a warning if a [/help?cmd=add|fossil add] command tries to add a file
49 that matches the ignore-glob.
50 * Add option -W|--width to "[/help?cmd=stash|fossil stash ls]"
51 and "[/help?cmd=leaves|fossil leaves]" commands.
52 * Enhance support for running as the root user. Now works on Haiku.
53 * [/help?cmd=new|fossil new] no longer creates an initial empty commit by
54 default. The first commit after checking out a new empty repository will
55 become the initial commit.
56 * Enhance sync operations by committing each round-trip to minimize number
57 of retransmits when autosync fails. Include option for
58 [/help?cmd=update| fossil update] and [/help?cmd=merge| fossil merge] to
59 continue even if missing content.
 
60
61 <h2>Changes For Version 1.28 (2014-01-27)</h2>
62 * Enhance [/help?cmd=/reports | /reports] to support event type filtering.
63 * When cloning a repository, the user name passed via the URL (if any)
64 is now used as the default local admin user's name.
65
--- www/changes.wiki
+++ www/changes.wiki
@@ -48,17 +48,18 @@
48 * Issue a warning if a [/help?cmd=add|fossil add] command tries to add a file
49 that matches the ignore-glob.
50 * Add option -W|--width to "[/help?cmd=stash|fossil stash ls]"
51 and "[/help?cmd=leaves|fossil leaves]" commands.
52 * Enhance support for running as the root user. Now works on Haiku.
53 * Added the <tt>-empty</tt> option to [/help?cmd=new|fossil new], which
54 causes it to not create an initial empty commit. The first commit after
55 checking out a repo created this way will become the initial commit.
56 * Enhance sync operations by committing each round-trip to minimize number
57 of retransmits when autosync fails. Include option for
58 [/help?cmd=update| fossil update] and [/help?cmd=merge| fossil merge] to
59 continue even if missing content.
60
61
62 <h2>Changes For Version 1.28 (2014-01-27)</h2>
63 * Enhance [/help?cmd=/reports | /reports] to support event type filtering.
64 * When cloning a repository, the user name passed via the URL (if any)
65 is now used as the default local admin user's name.
66
--- www/concepts.wiki
+++ www/concepts.wiki
@@ -124,11 +124,12 @@
124124
a software project.
125125
126126
<h3>2.2 Manifests</h3>
127127
128128
Associated with every check-in is a special file called the
129
-"manifest". The manifest is a listing of all other files in
129
+[./fileformat.wiki#manifest| "manifest"]. The manifest is a
130
+listing of all other files in
130131
that source tree. The manifest contains the (complete) artifact ID
131132
of the file and the name of the file as it appears on disk,
132133
and thus serves as a mapping from artifact ID to disk name. The artifact ID
133134
of the manifest is the identifier for the entire check-in. When
134135
you look at a "timeline" of changes in fossil, the ID associated
135136
--- www/concepts.wiki
+++ www/concepts.wiki
@@ -124,11 +124,12 @@
124 a software project.
125
126 <h3>2.2 Manifests</h3>
127
128 Associated with every check-in is a special file called the
129 "manifest". The manifest is a listing of all other files in
 
130 that source tree. The manifest contains the (complete) artifact ID
131 of the file and the name of the file as it appears on disk,
132 and thus serves as a mapping from artifact ID to disk name. The artifact ID
133 of the manifest is the identifier for the entire check-in. When
134 you look at a "timeline" of changes in fossil, the ID associated
135
--- www/concepts.wiki
+++ www/concepts.wiki
@@ -124,11 +124,12 @@
124 a software project.
125
126 <h3>2.2 Manifests</h3>
127
128 Associated with every check-in is a special file called the
129 [./fileformat.wiki#manifest| "manifest"]. The manifest is a
130 listing of all other files in
131 that source tree. The manifest contains the (complete) artifact ID
132 of the file and the name of the file as it appears on disk,
133 and thus serves as a mapping from artifact ID to disk name. The artifact ID
134 of the manifest is the identifier for the entire check-in. When
135 you look at a "timeline" of changes in fossil, the ID associated
136
+11 -1
--- www/server.wiki
+++ www/server.wiki
@@ -198,11 +198,11 @@
198198
and respond to the SimpleCGI or SCGI protocol rather than raw HTTP. This can
199199
be used in combination with a webserver (such as [http://nginx.org|Nginx])
200200
that does not support CGI. A typical Nginx configuration to support SCGI
201201
with Fossil would look something like this:
202202
<blockquote><pre>
203
-location ~ ^/demo_project/ {
203
+location /demo_project/ {
204204
include scgi_params;
205205
scgi_pass localhost:9000;
206206
scgi_param SCRIPT_NAME "/demo_project";
207207
}
208208
</pre></blockquote>
@@ -216,10 +216,20 @@
216216
<p>
217217
All of the features of the stand-alone server mode described above,
218218
such as the ability to serve a directory full of Fossil repositories
219219
rather than just a single repository, work the same way in SCGI mode.
220220
</p>
221
+<p>
222
+For security, it is probably a good idea to add the --localhost option
223
+to the [/help/server|fossil server] command to prevent Fossil from accepting
224
+off-site connections. And one might want to specify the listening TCP port
225
+number, rather than letting Fossil choose one for itself, just to avoid
226
+ambiguity. A typical command to start a Fossil SCGI server
227
+would be something like this:
228
+<blockquote><pre>
229
+fossil server $REPOSITORY --scgi --localhost --port 9000
230
+</pre></blockquote>
221231
</blockquote>
222232
223233
<h2>Securing a repository with SSL</h2><blockquote>
224234
<p>
225235
Using either CGI or SCGI, it is trivial to use SSL to
226236
--- www/server.wiki
+++ www/server.wiki
@@ -198,11 +198,11 @@
198 and respond to the SimpleCGI or SCGI protocol rather than raw HTTP. This can
199 be used in combination with a webserver (such as [http://nginx.org|Nginx])
200 that does not support CGI. A typical Nginx configuration to support SCGI
201 with Fossil would look something like this:
202 <blockquote><pre>
203 location ~ ^/demo_project/ {
204 include scgi_params;
205 scgi_pass localhost:9000;
206 scgi_param SCRIPT_NAME "/demo_project";
207 }
208 </pre></blockquote>
@@ -216,10 +216,20 @@
216 <p>
217 All of the features of the stand-alone server mode described above,
218 such as the ability to serve a directory full of Fossil repositories
219 rather than just a single repository, work the same way in SCGI mode.
220 </p>
 
 
 
 
 
 
 
 
 
 
221 </blockquote>
222
223 <h2>Securing a repository with SSL</h2><blockquote>
224 <p>
225 Using either CGI or SCGI, it is trivial to use SSL to
226
--- www/server.wiki
+++ www/server.wiki
@@ -198,11 +198,11 @@
198 and respond to the SimpleCGI or SCGI protocol rather than raw HTTP. This can
199 be used in combination with a webserver (such as [http://nginx.org|Nginx])
200 that does not support CGI. A typical Nginx configuration to support SCGI
201 with Fossil would look something like this:
202 <blockquote><pre>
203 location /demo_project/ {
204 include scgi_params;
205 scgi_pass localhost:9000;
206 scgi_param SCRIPT_NAME "/demo_project";
207 }
208 </pre></blockquote>
@@ -216,10 +216,20 @@
216 <p>
217 All of the features of the stand-alone server mode described above,
218 such as the ability to serve a directory full of Fossil repositories
219 rather than just a single repository, work the same way in SCGI mode.
220 </p>
221 <p>
222 For security, it is probably a good idea to add the --localhost option
223 to the [/help/server|fossil server] command to prevent Fossil from accepting
224 off-site connections. And one might want to specify the listening TCP port
225 number, rather than letting Fossil choose one for itself, just to avoid
226 ambiguity. A typical command to start a Fossil SCGI server
227 would be something like this:
228 <blockquote><pre>
229 fossil server $REPOSITORY --scgi --localhost --port 9000
230 </pre></blockquote>
231 </blockquote>
232
233 <h2>Securing a repository with SSL</h2><blockquote>
234 <p>
235 Using either CGI or SCGI, it is trivial to use SSL to
236
--- www/shunning.wiki
+++ www/shunning.wiki
@@ -34,42 +34,45 @@
3434
"rebuild" command.
3535
3636
<h3>Shunning lists are local state</h3>
3737
3838
The shunning list is part of the local state of a Fossil repository.
39
-In other words, shunning does not propagate using the normal "sync"
40
-mechanism. An artifact can be
39
+In other words, shunning does not propagate to a remote repository
40
+using the normal "sync" mechanism. An artifact can be
4141
shunned from one repository but be allowed to exist in another. The fact that
4242
the shunning list does not propagate is a security feature. If the
4343
shunning list propagated then a malicious user (or
4444
a bug in the fossil code) might introduce a shun record that would
4545
propagate through all repositories in a network and permanently
4646
destroy vital information. By refusing to propagate the shunning list,
4747
Fossil insures that no remote user will ever be able to remove
4848
information from your personal repositories without your permission.
4949
50
-The shunning list does not propagate by the normal "sync" mechanism,
50
+The shunning list does not propagate to a remote repository
51
+by the normal "sync" mechanism,
5152
but it is still possible to copy shuns from one repository to another
5253
using the "configuration" command:
5354
5455
<b>fossil configuration pull shun</b> <i>remote-url</i><br>
5556
<b>fossil configuration push shun</b> <i>remote-url</i>
5657
5758
The two command above will pull or push shunning lists from or to
5859
the <i>remote-url</i> indicated and merge the lists on the receiving
5960
end. "Admin" privilege on the remote server is required in order to
60
-push a shun list.
61
+push a shun list. In contrast, the shunning list will be automatically
62
+received by default as part of a normal client "pull" operation unless
63
+disabled by the "<tt>auto-shun</tt>" setting.
6164
6265
Note that the shunning list remains in the repository even after the
6366
shunned artifact has been removed. This is to prevent the artifact
6467
from being reintroduced into the repository the next time it syncs with
6568
another repository that has not shunned the artifact.
6669
6770
<h3>Managing the shunning list</h3>
6871
6972
The complete shunning list for a repository can be viewed by a user
70
-with "admin" privilege on the "/shunned" URL of the web interface to Fossil.
73
+with "admin" privilege on the "/shun" URL of the web interface to Fossil.
7174
That URL is accessible under the "Admin" button on the default menu
7275
bar. Items can be added to or removed from the shunning list. "Sync"
7376
operations are inhibited as soon as the artifact is added to the
7477
shunning list, but the content of the artifact is not actually removed
7578
from the repository until the next time the repository is rebuilt.
7679
--- www/shunning.wiki
+++ www/shunning.wiki
@@ -34,42 +34,45 @@
34 "rebuild" command.
35
36 <h3>Shunning lists are local state</h3>
37
38 The shunning list is part of the local state of a Fossil repository.
39 In other words, shunning does not propagate using the normal "sync"
40 mechanism. An artifact can be
41 shunned from one repository but be allowed to exist in another. The fact that
42 the shunning list does not propagate is a security feature. If the
43 shunning list propagated then a malicious user (or
44 a bug in the fossil code) might introduce a shun record that would
45 propagate through all repositories in a network and permanently
46 destroy vital information. By refusing to propagate the shunning list,
47 Fossil insures that no remote user will ever be able to remove
48 information from your personal repositories without your permission.
49
50 The shunning list does not propagate by the normal "sync" mechanism,
 
51 but it is still possible to copy shuns from one repository to another
52 using the "configuration" command:
53
54 <b>fossil configuration pull shun</b> <i>remote-url</i><br>
55 <b>fossil configuration push shun</b> <i>remote-url</i>
56
57 The two command above will pull or push shunning lists from or to
58 the <i>remote-url</i> indicated and merge the lists on the receiving
59 end. "Admin" privilege on the remote server is required in order to
60 push a shun list.
 
 
61
62 Note that the shunning list remains in the repository even after the
63 shunned artifact has been removed. This is to prevent the artifact
64 from being reintroduced into the repository the next time it syncs with
65 another repository that has not shunned the artifact.
66
67 <h3>Managing the shunning list</h3>
68
69 The complete shunning list for a repository can be viewed by a user
70 with "admin" privilege on the "/shunned" URL of the web interface to Fossil.
71 That URL is accessible under the "Admin" button on the default menu
72 bar. Items can be added to or removed from the shunning list. "Sync"
73 operations are inhibited as soon as the artifact is added to the
74 shunning list, but the content of the artifact is not actually removed
75 from the repository until the next time the repository is rebuilt.
76
--- www/shunning.wiki
+++ www/shunning.wiki
@@ -34,42 +34,45 @@
34 "rebuild" command.
35
36 <h3>Shunning lists are local state</h3>
37
38 The shunning list is part of the local state of a Fossil repository.
39 In other words, shunning does not propagate to a remote repository
40 using the normal "sync" mechanism. An artifact can be
41 shunned from one repository but be allowed to exist in another. The fact that
42 the shunning list does not propagate is a security feature. If the
43 shunning list propagated then a malicious user (or
44 a bug in the fossil code) might introduce a shun record that would
45 propagate through all repositories in a network and permanently
46 destroy vital information. By refusing to propagate the shunning list,
47 Fossil insures that no remote user will ever be able to remove
48 information from your personal repositories without your permission.
49
50 The shunning list does not propagate to a remote repository
51 by the normal "sync" mechanism,
52 but it is still possible to copy shuns from one repository to another
53 using the "configuration" command:
54
55 <b>fossil configuration pull shun</b> <i>remote-url</i><br>
56 <b>fossil configuration push shun</b> <i>remote-url</i>
57
58 The two command above will pull or push shunning lists from or to
59 the <i>remote-url</i> indicated and merge the lists on the receiving
60 end. "Admin" privilege on the remote server is required in order to
61 push a shun list. In contrast, the shunning list will be automatically
62 received by default as part of a normal client "pull" operation unless
63 disabled by the "<tt>auto-shun</tt>" setting.
64
65 Note that the shunning list remains in the repository even after the
66 shunned artifact has been removed. This is to prevent the artifact
67 from being reintroduced into the repository the next time it syncs with
68 another repository that has not shunned the artifact.
69
70 <h3>Managing the shunning list</h3>
71
72 The complete shunning list for a repository can be viewed by a user
73 with "admin" privilege on the "/shun" URL of the web interface to Fossil.
74 That URL is accessible under the "Admin" button on the default menu
75 bar. Items can be added to or removed from the shunning list. "Sync"
76 operations are inhibited as soon as the artifact is added to the
77 shunning list, but the content of the artifact is not actually removed
78 from the repository until the next time the repository is rebuilt.
79

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button