1 /* 2 ** 2001 September 15 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file contains code for implementations of the r-tree and r*-tree 13 ** algorithms packaged as an SQLite virtual table module. 14 */ 15 16 /* 17 ** Database Format of R-Tree Tables 18 ** -------------------------------- 19 ** 20 ** The data structure for a single virtual r-tree table is stored in three 21 ** native SQLite tables declared as follows. In each case, the '%' character 22 ** in the table name is replaced with the user-supplied name of the r-tree 23 ** table. 24 ** 25 ** CREATE TABLE %_node(nodeno INTEGER PRIMARY KEY, data BLOB) 26 ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) 27 ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER) 28 ** 29 ** The data for each node of the r-tree structure is stored in the %_node 30 ** table. For each node that is not the root node of the r-tree, there is 31 ** an entry in the %_parent table associating the node with its parent. 32 ** And for each row of data in the table, there is an entry in the %_rowid 33 ** table that maps from the entries rowid to the id of the node that it 34 ** is stored on. 35 ** 36 ** The root node of an r-tree always exists, even if the r-tree table is 37 ** empty. The nodeno of the root node is always 1. All other nodes in the 38 ** table must be the same size as the root node. The content of each node 39 ** is formatted as follows: 40 ** 41 ** 1. If the node is the root node (node 1), then the first 2 bytes 42 ** of the node contain the tree depth as a big-endian integer. 43 ** For non-root nodes, the first 2 bytes are left unused. 44 ** 45 ** 2. The next 2 bytes contain the number of entries currently 46 ** stored in the node. 47 ** 48 ** 3. The remainder of the node contains the node entries. Each entry 49 ** consists of a single 8-byte integer followed by an even number 50 ** of 4-byte coordinates. For leaf nodes the integer is the rowid 51 ** of a record. For internal nodes it is the node number of a 52 ** child page. 53 */ 54 55 #if !defined(SQLITE_CORE) \ 56 || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE)) 57 58 #ifndef SQLITE_CORE 59 #include "sqlite3ext.h" 60 SQLITE_EXTENSION_INIT1 61 #else 62 #include "sqlite3.h" 63 #endif 64 65 #include <string.h> 66 #include <assert.h> 67 #include <stdio.h> 68 69 #ifndef SQLITE_AMALGAMATION 70 #include "sqlite3rtree.h" 71 typedef sqlite3_int64 i64; 72 typedef sqlite3_uint64 u64; 73 typedef unsigned char u8; 74 typedef unsigned short u16; 75 typedef unsigned int u32; 76 #endif 77 78 /* The following macro is used to suppress compiler warnings. 79 */ 80 #ifndef UNUSED_PARAMETER 81 # define UNUSED_PARAMETER(x) (void)(x) 82 #endif 83 84 typedef struct Rtree Rtree; 85 typedef struct RtreeCursor RtreeCursor; 86 typedef struct RtreeNode RtreeNode; 87 typedef struct RtreeCell RtreeCell; 88 typedef struct RtreeConstraint RtreeConstraint; 89 typedef struct RtreeMatchArg RtreeMatchArg; 90 typedef struct RtreeGeomCallback RtreeGeomCallback; 91 typedef union RtreeCoord RtreeCoord; 92 typedef struct RtreeSearchPoint RtreeSearchPoint; 93 94 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */ 95 #define RTREE_MAX_DIMENSIONS 5 96 97 /* Size of hash table Rtree.aHash. This hash table is not expected to 98 ** ever contain very many entries, so a fixed number of buckets is 99 ** used. 100 */ 101 #define HASHSIZE 97 102 103 /* The xBestIndex method of this virtual table requires an estimate of 104 ** the number of rows in the virtual table to calculate the costs of 105 ** various strategies. If possible, this estimate is loaded from the 106 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum). 107 ** Otherwise, if no sqlite_stat1 entry is available, use 108 ** RTREE_DEFAULT_ROWEST. 109 */ 110 #define RTREE_DEFAULT_ROWEST 1048576 111 #define RTREE_MIN_ROWEST 100 112 113 /* 114 ** An rtree virtual-table object. 115 */ 116 struct Rtree { 117 sqlite3_vtab base; /* Base class. Must be first */ 118 sqlite3 *db; /* Host database connection */ 119 int iNodeSize; /* Size in bytes of each node in the node table */ 120 u8 nDim; /* Number of dimensions */ 121 u8 nDim2; /* Twice the number of dimensions */ 122 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */ 123 u8 nBytesPerCell; /* Bytes consumed per cell */ 124 u8 inWrTrans; /* True if inside write transaction */ 125 int iDepth; /* Current depth of the r-tree structure */ 126 char *zDb; /* Name of database containing r-tree table */ 127 char *zName; /* Name of r-tree table */ 128 u32 nBusy; /* Current number of users of this structure */ 129 i64 nRowEst; /* Estimated number of rows in this table */ 130 u32 nCursor; /* Number of open cursors */ 131 132 /* List of nodes removed during a CondenseTree operation. List is 133 ** linked together via the pointer normally used for hash chains - 134 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree 135 ** headed by the node (leaf nodes have RtreeNode.iNode==0). 136 */ 137 RtreeNode *pDeleted; 138 int iReinsertHeight; /* Height of sub-trees Reinsert() has run on */ 139 140 /* Blob I/O on xxx_node */ 141 sqlite3_blob *pNodeBlob; 142 143 /* Statements to read/write/delete a record from xxx_node */ 144 sqlite3_stmt *pWriteNode; 145 sqlite3_stmt *pDeleteNode; 146 147 /* Statements to read/write/delete a record from xxx_rowid */ 148 sqlite3_stmt *pReadRowid; 149 sqlite3_stmt *pWriteRowid; 150 sqlite3_stmt *pDeleteRowid; 151 152 /* Statements to read/write/delete a record from xxx_parent */ 153 sqlite3_stmt *pReadParent; 154 sqlite3_stmt *pWriteParent; 155 sqlite3_stmt *pDeleteParent; 156 157 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */ 158 }; 159 160 /* Possible values for Rtree.eCoordType: */ 161 #define RTREE_COORD_REAL32 0 162 #define RTREE_COORD_INT32 1 163 164 /* 165 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will 166 ** only deal with integer coordinates. No floating point operations 167 ** will be done. 168 */ 169 #ifdef SQLITE_RTREE_INT_ONLY 170 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */ 171 typedef int RtreeValue; /* Low accuracy coordinate */ 172 # define RTREE_ZERO 0 173 #else 174 typedef double RtreeDValue; /* High accuracy coordinate */ 175 typedef float RtreeValue; /* Low accuracy coordinate */ 176 # define RTREE_ZERO 0.0 177 #endif 178 179 /* 180 ** When doing a search of an r-tree, instances of the following structure 181 ** record intermediate results from the tree walk. 182 ** 183 ** The id is always a node-id. For iLevel>=1 the id is the node-id of 184 ** the node that the RtreeSearchPoint represents. When iLevel==0, however, 185 ** the id is of the parent node and the cell that RtreeSearchPoint 186 ** represents is the iCell-th entry in the parent node. 187 */ 188 struct RtreeSearchPoint { 189 RtreeDValue rScore; /* The score for this node. Smallest goes first. */ 190 sqlite3_int64 id; /* Node ID */ 191 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */ 192 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */ 193 u8 iCell; /* Cell index within the node */ 194 }; 195 196 /* 197 ** The minimum number of cells allowed for a node is a third of the 198 ** maximum. In Gutman's notation: 199 ** 200 ** m = M/3 201 ** 202 ** If an R*-tree "Reinsert" operation is required, the same number of 203 ** cells are removed from the overfull node and reinserted into the tree. 204 */ 205 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3) 206 #define RTREE_REINSERT(p) RTREE_MINCELLS(p) 207 #define RTREE_MAXCELLS 51 208 209 /* 210 ** The smallest possible node-size is (512-64)==448 bytes. And the largest 211 ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates). 212 ** Therefore all non-root nodes must contain at least 3 entries. Since 213 ** 3^40 is greater than 2^64, an r-tree structure always has a depth of 214 ** 40 or less. 215 */ 216 #define RTREE_MAX_DEPTH 40 217 218 219 /* 220 ** Number of entries in the cursor RtreeNode cache. The first entry is 221 ** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining 222 ** entries cache the RtreeNode for the first elements of the priority queue. 223 */ 224 #define RTREE_CACHE_SZ 5 225 226 /* 227 ** An rtree cursor object. 228 */ 229 struct RtreeCursor { 230 sqlite3_vtab_cursor base; /* Base class. Must be first */ 231 u8 atEOF; /* True if at end of search */ 232 u8 bPoint; /* True if sPoint is valid */ 233 int iStrategy; /* Copy of idxNum search parameter */ 234 int nConstraint; /* Number of entries in aConstraint */ 235 RtreeConstraint *aConstraint; /* Search constraints. */ 236 int nPointAlloc; /* Number of slots allocated for aPoint[] */ 237 int nPoint; /* Number of slots used in aPoint[] */ 238 int mxLevel; /* iLevel value for root of the tree */ 239 RtreeSearchPoint *aPoint; /* Priority queue for search points */ 240 RtreeSearchPoint sPoint; /* Cached next search point */ 241 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */ 242 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */ 243 }; 244 245 /* Return the Rtree of a RtreeCursor */ 246 #define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab)) 247 248 /* 249 ** A coordinate can be either a floating point number or a integer. All 250 ** coordinates within a single R-Tree are always of the same time. 251 */ 252 union RtreeCoord { 253 RtreeValue f; /* Floating point value */ 254 int i; /* Integer value */ 255 u32 u; /* Unsigned for byte-order conversions */ 256 }; 257 258 /* 259 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord 260 ** formatted as a RtreeDValue (double or int64). This macro assumes that local 261 ** variable pRtree points to the Rtree structure associated with the 262 ** RtreeCoord. 263 */ 264 #ifdef SQLITE_RTREE_INT_ONLY 265 # define DCOORD(coord) ((RtreeDValue)coord.i) 266 #else 267 # define DCOORD(coord) ( \ 268 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \ 269 ((double)coord.f) : \ 270 ((double)coord.i) \ 271 ) 272 #endif 273 274 /* 275 ** A search constraint. 276 */ 277 struct RtreeConstraint { 278 int iCoord; /* Index of constrained coordinate */ 279 int op; /* Constraining operation */ 280 union { 281 RtreeDValue rValue; /* Constraint value. */ 282 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*); 283 int (*xQueryFunc)(sqlite3_rtree_query_info*); 284 } u; 285 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */ 286 }; 287 288 /* Possible values for RtreeConstraint.op */ 289 #define RTREE_EQ 0x41 /* A */ 290 #define RTREE_LE 0x42 /* B */ 291 #define RTREE_LT 0x43 /* C */ 292 #define RTREE_GE 0x44 /* D */ 293 #define RTREE_GT 0x45 /* E */ 294 #define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */ 295 #define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */ 296 297 298 /* 299 ** An rtree structure node. 300 */ 301 struct RtreeNode { 302 RtreeNode *pParent; /* Parent node */ 303 i64 iNode; /* The node number */ 304 int nRef; /* Number of references to this node */ 305 int isDirty; /* True if the node needs to be written to disk */ 306 u8 *zData; /* Content of the node, as should be on disk */ 307 RtreeNode *pNext; /* Next node in this hash collision chain */ 308 }; 309 310 /* Return the number of cells in a node */ 311 #define NCELL(pNode) readInt16(&(pNode)->zData[2]) 312 313 /* 314 ** A single cell from a node, deserialized 315 */ 316 struct RtreeCell { 317 i64 iRowid; /* Node or entry ID */ 318 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */ 319 }; 320 321 322 /* 323 ** This object becomes the sqlite3_user_data() for the SQL functions 324 ** that are created by sqlite3_rtree_geometry_callback() and 325 ** sqlite3_rtree_query_callback() and which appear on the right of MATCH 326 ** operators in order to constrain a search. 327 ** 328 ** xGeom and xQueryFunc are the callback functions. Exactly one of 329 ** xGeom and xQueryFunc fields is non-NULL, depending on whether the 330 ** SQL function was created using sqlite3_rtree_geometry_callback() or 331 ** sqlite3_rtree_query_callback(). 332 ** 333 ** This object is deleted automatically by the destructor mechanism in 334 ** sqlite3_create_function_v2(). 335 */ 336 struct RtreeGeomCallback { 337 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*); 338 int (*xQueryFunc)(sqlite3_rtree_query_info*); 339 void (*xDestructor)(void*); 340 void *pContext; 341 }; 342 343 /* 344 ** An instance of this structure (in the form of a BLOB) is returned by 345 ** the SQL functions that sqlite3_rtree_geometry_callback() and 346 ** sqlite3_rtree_query_callback() create, and is read as the right-hand 347 ** operand to the MATCH operator of an R-Tree. 348 */ 349 struct RtreeMatchArg { 350 u32 iSize; /* Size of this object */ 351 RtreeGeomCallback cb; /* Info about the callback functions */ 352 int nParam; /* Number of parameters to the SQL function */ 353 sqlite3_value **apSqlParam; /* Original SQL parameter values */ 354 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */ 355 }; 356 357 #ifndef MAX 358 # define MAX(x,y) ((x) < (y) ? (y) : (x)) 359 #endif 360 #ifndef MIN 361 # define MIN(x,y) ((x) > (y) ? (y) : (x)) 362 #endif 363 364 /* What version of GCC is being used. 0 means GCC is not being used . 365 ** Note that the GCC_VERSION macro will also be set correctly when using 366 ** clang, since clang works hard to be gcc compatible. So the gcc 367 ** optimizations will also work when compiling with clang. 368 */ 369 #ifndef GCC_VERSION 370 #if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC) 371 # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__) 372 #else 373 # define GCC_VERSION 0 374 #endif 375 #endif 376 377 /* The testcase() macro should already be defined in the amalgamation. If 378 ** it is not, make it a no-op. 379 */ 380 #ifndef SQLITE_AMALGAMATION 381 # define testcase(X) 382 #endif 383 384 /* 385 ** Macros to determine whether the machine is big or little endian, 386 ** and whether or not that determination is run-time or compile-time. 387 ** 388 ** For best performance, an attempt is made to guess at the byte-order 389 ** using C-preprocessor macros. If that is unsuccessful, or if 390 ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined 391 ** at run-time. 392 */ 393 #ifndef SQLITE_BYTEORDER 394 #if defined(i386) || defined(__i386__) || defined(_M_IX86) || \ 395 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \ 396 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \ 397 defined(__arm__) 398 # define SQLITE_BYTEORDER 1234 399 #elif defined(sparc) || defined(__ppc__) 400 # define SQLITE_BYTEORDER 4321 401 #else 402 # define SQLITE_BYTEORDER 0 /* 0 means "unknown at compile-time" */ 403 #endif 404 #endif 405 406 407 /* What version of MSVC is being used. 0 means MSVC is not being used */ 408 #ifndef MSVC_VERSION 409 #if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC) 410 # define MSVC_VERSION _MSC_VER 411 #else 412 # define MSVC_VERSION 0 413 #endif 414 #endif 415 416 /* 417 ** Functions to deserialize a 16 bit integer, 32 bit real number and 418 ** 64 bit integer. The deserialized value is returned. 419 */ 420 static int readInt16(u8 *p){ 421 return (p[0]<<8) + p[1]; 422 } 423 static void readCoord(u8 *p, RtreeCoord *pCoord){ 424 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */ 425 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 426 pCoord->u = _byteswap_ulong(*(u32*)p); 427 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 428 pCoord->u = __builtin_bswap32(*(u32*)p); 429 #elif SQLITE_BYTEORDER==4321 430 pCoord->u = *(u32*)p; 431 #else 432 pCoord->u = ( 433 (((u32)p[0]) << 24) + 434 (((u32)p[1]) << 16) + 435 (((u32)p[2]) << 8) + 436 (((u32)p[3]) << 0) 437 ); 438 #endif 439 } 440 static i64 readInt64(u8 *p){ 441 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 442 u64 x; 443 memcpy(&x, p, 8); 444 return (i64)_byteswap_uint64(x); 445 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 446 u64 x; 447 memcpy(&x, p, 8); 448 return (i64)__builtin_bswap64(x); 449 #elif SQLITE_BYTEORDER==4321 450 i64 x; 451 memcpy(&x, p, 8); 452 return x; 453 #else 454 return (i64)( 455 (((u64)p[0]) << 56) + 456 (((u64)p[1]) << 48) + 457 (((u64)p[2]) << 40) + 458 (((u64)p[3]) << 32) + 459 (((u64)p[4]) << 24) + 460 (((u64)p[5]) << 16) + 461 (((u64)p[6]) << 8) + 462 (((u64)p[7]) << 0) 463 ); 464 #endif 465 } 466 467 /* 468 ** Functions to serialize a 16 bit integer, 32 bit real number and 469 ** 64 bit integer. The value returned is the number of bytes written 470 ** to the argument buffer (always 2, 4 and 8 respectively). 471 */ 472 static void writeInt16(u8 *p, int i){ 473 p[0] = (i>> 8)&0xFF; 474 p[1] = (i>> 0)&0xFF; 475 } 476 static int writeCoord(u8 *p, RtreeCoord *pCoord){ 477 u32 i; 478 assert( ((((char*)p) - (char*)0)&3)==0 ); /* p is always 4-byte aligned */ 479 assert( sizeof(RtreeCoord)==4 ); 480 assert( sizeof(u32)==4 ); 481 #if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 482 i = __builtin_bswap32(pCoord->u); 483 memcpy(p, &i, 4); 484 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 485 i = _byteswap_ulong(pCoord->u); 486 memcpy(p, &i, 4); 487 #elif SQLITE_BYTEORDER==4321 488 i = pCoord->u; 489 memcpy(p, &i, 4); 490 #else 491 i = pCoord->u; 492 p[0] = (i>>24)&0xFF; 493 p[1] = (i>>16)&0xFF; 494 p[2] = (i>> 8)&0xFF; 495 p[3] = (i>> 0)&0xFF; 496 #endif 497 return 4; 498 } 499 static int writeInt64(u8 *p, i64 i){ 500 #if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 501 i = (i64)__builtin_bswap64((u64)i); 502 memcpy(p, &i, 8); 503 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 504 i = (i64)_byteswap_uint64((u64)i); 505 memcpy(p, &i, 8); 506 #elif SQLITE_BYTEORDER==4321 507 memcpy(p, &i, 8); 508 #else 509 p[0] = (i>>56)&0xFF; 510 p[1] = (i>>48)&0xFF; 511 p[2] = (i>>40)&0xFF; 512 p[3] = (i>>32)&0xFF; 513 p[4] = (i>>24)&0xFF; 514 p[5] = (i>>16)&0xFF; 515 p[6] = (i>> 8)&0xFF; 516 p[7] = (i>> 0)&0xFF; 517 #endif 518 return 8; 519 } 520 521 /* 522 ** Increment the reference count of node p. 523 */ 524 static void nodeReference(RtreeNode *p){ 525 if( p ){ 526 p->nRef++; 527 } 528 } 529 530 /* 531 ** Clear the content of node p (set all bytes to 0x00). 532 */ 533 static void nodeZero(Rtree *pRtree, RtreeNode *p){ 534 memset(&p->zData[2], 0, pRtree->iNodeSize-2); 535 p->isDirty = 1; 536 } 537 538 /* 539 ** Given a node number iNode, return the corresponding key to use 540 ** in the Rtree.aHash table. 541 */ 542 static int nodeHash(i64 iNode){ 543 return iNode % HASHSIZE; 544 } 545 546 /* 547 ** Search the node hash table for node iNode. If found, return a pointer 548 ** to it. Otherwise, return 0. 549 */ 550 static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){ 551 RtreeNode *p; 552 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext); 553 return p; 554 } 555 556 /* 557 ** Add node pNode to the node hash table. 558 */ 559 static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){ 560 int iHash; 561 assert( pNode->pNext==0 ); 562 iHash = nodeHash(pNode->iNode); 563 pNode->pNext = pRtree->aHash[iHash]; 564 pRtree->aHash[iHash] = pNode; 565 } 566 567 /* 568 ** Remove node pNode from the node hash table. 569 */ 570 static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){ 571 RtreeNode **pp; 572 if( pNode->iNode!=0 ){ 573 pp = &pRtree->aHash[nodeHash(pNode->iNode)]; 574 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); } 575 *pp = pNode->pNext; 576 pNode->pNext = 0; 577 } 578 } 579 580 /* 581 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0), 582 ** indicating that node has not yet been assigned a node number. It is 583 ** assigned a node number when nodeWrite() is called to write the 584 ** node contents out to the database. 585 */ 586 static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){ 587 RtreeNode *pNode; 588 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode) + pRtree->iNodeSize); 589 if( pNode ){ 590 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize); 591 pNode->zData = (u8 *)&pNode[1]; 592 pNode->nRef = 1; 593 pNode->pParent = pParent; 594 pNode->isDirty = 1; 595 nodeReference(pParent); 596 } 597 return pNode; 598 } 599 600 /* 601 ** Clear the Rtree.pNodeBlob object 602 */ 603 static void nodeBlobReset(Rtree *pRtree){ 604 if( pRtree->pNodeBlob && pRtree->inWrTrans==0 && pRtree->nCursor==0 ){ 605 sqlite3_blob *pBlob = pRtree->pNodeBlob; 606 pRtree->pNodeBlob = 0; 607 sqlite3_blob_close(pBlob); 608 } 609 } 610 611 /* 612 ** Obtain a reference to an r-tree node. 613 */ 614 static int nodeAcquire( 615 Rtree *pRtree, /* R-tree structure */ 616 i64 iNode, /* Node number to load */ 617 RtreeNode *pParent, /* Either the parent node or NULL */ 618 RtreeNode **ppNode /* OUT: Acquired node */ 619 ){ 620 int rc = SQLITE_OK; 621 RtreeNode *pNode = 0; 622 623 /* Check if the requested node is already in the hash table. If so, 624 ** increase its reference count and return it. 625 */ 626 if( (pNode = nodeHashLookup(pRtree, iNode)) ){ 627 assert( !pParent || !pNode->pParent || pNode->pParent==pParent ); 628 if( pParent && !pNode->pParent ){ 629 nodeReference(pParent); 630 pNode->pParent = pParent; 631 } 632 pNode->nRef++; 633 *ppNode = pNode; 634 return SQLITE_OK; 635 } 636 637 if( pRtree->pNodeBlob ){ 638 sqlite3_blob *pBlob = pRtree->pNodeBlob; 639 pRtree->pNodeBlob = 0; 640 rc = sqlite3_blob_reopen(pBlob, iNode); 641 pRtree->pNodeBlob = pBlob; 642 if( rc ){ 643 nodeBlobReset(pRtree); 644 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM; 645 } 646 } 647 if( pRtree->pNodeBlob==0 ){ 648 char *zTab = sqlite3_mprintf("%s_node", pRtree->zName); 649 if( zTab==0 ) return SQLITE_NOMEM; 650 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, zTab, "data", iNode, 0, 651 &pRtree->pNodeBlob); 652 sqlite3_free(zTab); 653 } 654 if( rc ){ 655 nodeBlobReset(pRtree); 656 *ppNode = 0; 657 /* If unable to open an sqlite3_blob on the desired row, that can only 658 ** be because the shadow tables hold erroneous data. */ 659 if( rc==SQLITE_ERROR ) rc = SQLITE_CORRUPT_VTAB; 660 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){ 661 pNode = (RtreeNode *)sqlite3_malloc(sizeof(RtreeNode)+pRtree->iNodeSize); 662 if( !pNode ){ 663 rc = SQLITE_NOMEM; 664 }else{ 665 pNode->pParent = pParent; 666 pNode->zData = (u8 *)&pNode[1]; 667 pNode->nRef = 1; 668 pNode->iNode = iNode; 669 pNode->isDirty = 0; 670 pNode->pNext = 0; 671 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData, 672 pRtree->iNodeSize, 0); 673 nodeReference(pParent); 674 } 675 } 676 677 /* If the root node was just loaded, set pRtree->iDepth to the height 678 ** of the r-tree structure. A height of zero means all data is stored on 679 ** the root node. A height of one means the children of the root node 680 ** are the leaves, and so on. If the depth as specified on the root node 681 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt. 682 */ 683 if( pNode && iNode==1 ){ 684 pRtree->iDepth = readInt16(pNode->zData); 685 if( pRtree->iDepth>RTREE_MAX_DEPTH ){ 686 rc = SQLITE_CORRUPT_VTAB; 687 } 688 } 689 690 /* If no error has occurred so far, check if the "number of entries" 691 ** field on the node is too large. If so, set the return code to 692 ** SQLITE_CORRUPT_VTAB. 693 */ 694 if( pNode && rc==SQLITE_OK ){ 695 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){ 696 rc = SQLITE_CORRUPT_VTAB; 697 } 698 } 699 700 if( rc==SQLITE_OK ){ 701 if( pNode!=0 ){ 702 nodeHashInsert(pRtree, pNode); 703 }else{ 704 rc = SQLITE_CORRUPT_VTAB; 705 } 706 *ppNode = pNode; 707 }else{ 708 sqlite3_free(pNode); 709 *ppNode = 0; 710 } 711 712 return rc; 713 } 714 715 /* 716 ** Overwrite cell iCell of node pNode with the contents of pCell. 717 */ 718 static void nodeOverwriteCell( 719 Rtree *pRtree, /* The overall R-Tree */ 720 RtreeNode *pNode, /* The node into which the cell is to be written */ 721 RtreeCell *pCell, /* The cell to write */ 722 int iCell /* Index into pNode into which pCell is written */ 723 ){ 724 int ii; 725 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell]; 726 p += writeInt64(p, pCell->iRowid); 727 for(ii=0; ii<pRtree->nDim2; ii++){ 728 p += writeCoord(p, &pCell->aCoord[ii]); 729 } 730 pNode->isDirty = 1; 731 } 732 733 /* 734 ** Remove the cell with index iCell from node pNode. 735 */ 736 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){ 737 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell]; 738 u8 *pSrc = &pDst[pRtree->nBytesPerCell]; 739 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell; 740 memmove(pDst, pSrc, nByte); 741 writeInt16(&pNode->zData[2], NCELL(pNode)-1); 742 pNode->isDirty = 1; 743 } 744 745 /* 746 ** Insert the contents of cell pCell into node pNode. If the insert 747 ** is successful, return SQLITE_OK. 748 ** 749 ** If there is not enough free space in pNode, return SQLITE_FULL. 750 */ 751 static int nodeInsertCell( 752 Rtree *pRtree, /* The overall R-Tree */ 753 RtreeNode *pNode, /* Write new cell into this node */ 754 RtreeCell *pCell /* The cell to be inserted */ 755 ){ 756 int nCell; /* Current number of cells in pNode */ 757 int nMaxCell; /* Maximum number of cells for pNode */ 758 759 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell; 760 nCell = NCELL(pNode); 761 762 assert( nCell<=nMaxCell ); 763 if( nCell<nMaxCell ){ 764 nodeOverwriteCell(pRtree, pNode, pCell, nCell); 765 writeInt16(&pNode->zData[2], nCell+1); 766 pNode->isDirty = 1; 767 } 768 769 return (nCell==nMaxCell); 770 } 771 772 /* 773 ** If the node is dirty, write it out to the database. 774 */ 775 static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){ 776 int rc = SQLITE_OK; 777 if( pNode->isDirty ){ 778 sqlite3_stmt *p = pRtree->pWriteNode; 779 if( pNode->iNode ){ 780 sqlite3_bind_int64(p, 1, pNode->iNode); 781 }else{ 782 sqlite3_bind_null(p, 1); 783 } 784 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC); 785 sqlite3_step(p); 786 pNode->isDirty = 0; 787 rc = sqlite3_reset(p); 788 sqlite3_bind_null(p, 2); 789 if( pNode->iNode==0 && rc==SQLITE_OK ){ 790 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db); 791 nodeHashInsert(pRtree, pNode); 792 } 793 } 794 return rc; 795 } 796 797 /* 798 ** Release a reference to a node. If the node is dirty and the reference 799 ** count drops to zero, the node data is written to the database. 800 */ 801 static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){ 802 int rc = SQLITE_OK; 803 if( pNode ){ 804 assert( pNode->nRef>0 ); 805 pNode->nRef--; 806 if( pNode->nRef==0 ){ 807 if( pNode->iNode==1 ){ 808 pRtree->iDepth = -1; 809 } 810 if( pNode->pParent ){ 811 rc = nodeRelease(pRtree, pNode->pParent); 812 } 813 if( rc==SQLITE_OK ){ 814 rc = nodeWrite(pRtree, pNode); 815 } 816 nodeHashDelete(pRtree, pNode); 817 sqlite3_free(pNode); 818 } 819 } 820 return rc; 821 } 822 823 /* 824 ** Return the 64-bit integer value associated with cell iCell of 825 ** node pNode. If pNode is a leaf node, this is a rowid. If it is 826 ** an internal node, then the 64-bit integer is a child page number. 827 */ 828 static i64 nodeGetRowid( 829 Rtree *pRtree, /* The overall R-Tree */ 830 RtreeNode *pNode, /* The node from which to extract the ID */ 831 int iCell /* The cell index from which to extract the ID */ 832 ){ 833 assert( iCell<NCELL(pNode) ); 834 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]); 835 } 836 837 /* 838 ** Return coordinate iCoord from cell iCell in node pNode. 839 */ 840 static void nodeGetCoord( 841 Rtree *pRtree, /* The overall R-Tree */ 842 RtreeNode *pNode, /* The node from which to extract a coordinate */ 843 int iCell, /* The index of the cell within the node */ 844 int iCoord, /* Which coordinate to extract */ 845 RtreeCoord *pCoord /* OUT: Space to write result to */ 846 ){ 847 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord); 848 } 849 850 /* 851 ** Deserialize cell iCell of node pNode. Populate the structure pointed 852 ** to by pCell with the results. 853 */ 854 static void nodeGetCell( 855 Rtree *pRtree, /* The overall R-Tree */ 856 RtreeNode *pNode, /* The node containing the cell to be read */ 857 int iCell, /* Index of the cell within the node */ 858 RtreeCell *pCell /* OUT: Write the cell contents here */ 859 ){ 860 u8 *pData; 861 RtreeCoord *pCoord; 862 int ii = 0; 863 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell); 864 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell); 865 pCoord = pCell->aCoord; 866 do{ 867 readCoord(pData, &pCoord[ii]); 868 readCoord(pData+4, &pCoord[ii+1]); 869 pData += 8; 870 ii += 2; 871 }while( ii<pRtree->nDim2 ); 872 } 873 874 875 /* Forward declaration for the function that does the work of 876 ** the virtual table module xCreate() and xConnect() methods. 877 */ 878 static int rtreeInit( 879 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int 880 ); 881 882 /* 883 ** Rtree virtual table module xCreate method. 884 */ 885 static int rtreeCreate( 886 sqlite3 *db, 887 void *pAux, 888 int argc, const char *const*argv, 889 sqlite3_vtab **ppVtab, 890 char **pzErr 891 ){ 892 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1); 893 } 894 895 /* 896 ** Rtree virtual table module xConnect method. 897 */ 898 static int rtreeConnect( 899 sqlite3 *db, 900 void *pAux, 901 int argc, const char *const*argv, 902 sqlite3_vtab **ppVtab, 903 char **pzErr 904 ){ 905 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0); 906 } 907 908 /* 909 ** Increment the r-tree reference count. 910 */ 911 static void rtreeReference(Rtree *pRtree){ 912 pRtree->nBusy++; 913 } 914 915 /* 916 ** Decrement the r-tree reference count. When the reference count reaches 917 ** zero the structure is deleted. 918 */ 919 static void rtreeRelease(Rtree *pRtree){ 920 pRtree->nBusy--; 921 if( pRtree->nBusy==0 ){ 922 pRtree->inWrTrans = 0; 923 pRtree->nCursor = 0; 924 nodeBlobReset(pRtree); 925 sqlite3_finalize(pRtree->pWriteNode); 926 sqlite3_finalize(pRtree->pDeleteNode); 927 sqlite3_finalize(pRtree->pReadRowid); 928 sqlite3_finalize(pRtree->pWriteRowid); 929 sqlite3_finalize(pRtree->pDeleteRowid); 930 sqlite3_finalize(pRtree->pReadParent); 931 sqlite3_finalize(pRtree->pWriteParent); 932 sqlite3_finalize(pRtree->pDeleteParent); 933 sqlite3_free(pRtree); 934 } 935 } 936 937 /* 938 ** Rtree virtual table module xDisconnect method. 939 */ 940 static int rtreeDisconnect(sqlite3_vtab *pVtab){ 941 rtreeRelease((Rtree *)pVtab); 942 return SQLITE_OK; 943 } 944 945 /* 946 ** Rtree virtual table module xDestroy method. 947 */ 948 static int rtreeDestroy(sqlite3_vtab *pVtab){ 949 Rtree *pRtree = (Rtree *)pVtab; 950 int rc; 951 char *zCreate = sqlite3_mprintf( 952 "DROP TABLE '%q'.'%q_node';" 953 "DROP TABLE '%q'.'%q_rowid';" 954 "DROP TABLE '%q'.'%q_parent';", 955 pRtree->zDb, pRtree->zName, 956 pRtree->zDb, pRtree->zName, 957 pRtree->zDb, pRtree->zName 958 ); 959 if( !zCreate ){ 960 rc = SQLITE_NOMEM; 961 }else{ 962 nodeBlobReset(pRtree); 963 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0); 964 sqlite3_free(zCreate); 965 } 966 if( rc==SQLITE_OK ){ 967 rtreeRelease(pRtree); 968 } 969 970 return rc; 971 } 972 973 /* 974 ** Rtree virtual table module xOpen method. 975 */ 976 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ 977 int rc = SQLITE_NOMEM; 978 Rtree *pRtree = (Rtree *)pVTab; 979 RtreeCursor *pCsr; 980 981 pCsr = (RtreeCursor *)sqlite3_malloc(sizeof(RtreeCursor)); 982 if( pCsr ){ 983 memset(pCsr, 0, sizeof(RtreeCursor)); 984 pCsr->base.pVtab = pVTab; 985 rc = SQLITE_OK; 986 pRtree->nCursor++; 987 } 988 *ppCursor = (sqlite3_vtab_cursor *)pCsr; 989 990 return rc; 991 } 992 993 994 /* 995 ** Free the RtreeCursor.aConstraint[] array and its contents. 996 */ 997 static void freeCursorConstraints(RtreeCursor *pCsr){ 998 if( pCsr->aConstraint ){ 999 int i; /* Used to iterate through constraint array */ 1000 for(i=0; i<pCsr->nConstraint; i++){ 1001 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo; 1002 if( pInfo ){ 1003 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser); 1004 sqlite3_free(pInfo); 1005 } 1006 } 1007 sqlite3_free(pCsr->aConstraint); 1008 pCsr->aConstraint = 0; 1009 } 1010 } 1011 1012 /* 1013 ** Rtree virtual table module xClose method. 1014 */ 1015 static int rtreeClose(sqlite3_vtab_cursor *cur){ 1016 Rtree *pRtree = (Rtree *)(cur->pVtab); 1017 int ii; 1018 RtreeCursor *pCsr = (RtreeCursor *)cur; 1019 assert( pRtree->nCursor>0 ); 1020 freeCursorConstraints(pCsr); 1021 sqlite3_free(pCsr->aPoint); 1022 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]); 1023 sqlite3_free(pCsr); 1024 pRtree->nCursor--; 1025 nodeBlobReset(pRtree); 1026 return SQLITE_OK; 1027 } 1028 1029 /* 1030 ** Rtree virtual table module xEof method. 1031 ** 1032 ** Return non-zero if the cursor does not currently point to a valid 1033 ** record (i.e if the scan has finished), or zero otherwise. 1034 */ 1035 static int rtreeEof(sqlite3_vtab_cursor *cur){ 1036 RtreeCursor *pCsr = (RtreeCursor *)cur; 1037 return pCsr->atEOF; 1038 } 1039 1040 /* 1041 ** Convert raw bits from the on-disk RTree record into a coordinate value. 1042 ** The on-disk format is big-endian and needs to be converted for little- 1043 ** endian platforms. The on-disk record stores integer coordinates if 1044 ** eInt is true and it stores 32-bit floating point records if eInt is 1045 ** false. a[] is the four bytes of the on-disk record to be decoded. 1046 ** Store the results in "r". 1047 ** 1048 ** There are five versions of this macro. The last one is generic. The 1049 ** other four are various architectures-specific optimizations. 1050 */ 1051 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300 1052 #define RTREE_DECODE_COORD(eInt, a, r) { \ 1053 RtreeCoord c; /* Coordinate decoded */ \ 1054 c.u = _byteswap_ulong(*(u32*)a); \ 1055 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ 1056 } 1057 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000 1058 #define RTREE_DECODE_COORD(eInt, a, r) { \ 1059 RtreeCoord c; /* Coordinate decoded */ \ 1060 c.u = __builtin_bswap32(*(u32*)a); \ 1061 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ 1062 } 1063 #elif SQLITE_BYTEORDER==1234 1064 #define RTREE_DECODE_COORD(eInt, a, r) { \ 1065 RtreeCoord c; /* Coordinate decoded */ \ 1066 memcpy(&c.u,a,4); \ 1067 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \ 1068 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \ 1069 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ 1070 } 1071 #elif SQLITE_BYTEORDER==4321 1072 #define RTREE_DECODE_COORD(eInt, a, r) { \ 1073 RtreeCoord c; /* Coordinate decoded */ \ 1074 memcpy(&c.u,a,4); \ 1075 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ 1076 } 1077 #else 1078 #define RTREE_DECODE_COORD(eInt, a, r) { \ 1079 RtreeCoord c; /* Coordinate decoded */ \ 1080 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \ 1081 +((u32)a[2]<<8) + a[3]; \ 1082 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \ 1083 } 1084 #endif 1085 1086 /* 1087 ** Check the RTree node or entry given by pCellData and p against the MATCH 1088 ** constraint pConstraint. 1089 */ 1090 static int rtreeCallbackConstraint( 1091 RtreeConstraint *pConstraint, /* The constraint to test */ 1092 int eInt, /* True if RTree holding integer coordinates */ 1093 u8 *pCellData, /* Raw cell content */ 1094 RtreeSearchPoint *pSearch, /* Container of this cell */ 1095 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */ 1096 int *peWithin /* OUT: visibility of the cell */ 1097 ){ 1098 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */ 1099 int nCoord = pInfo->nCoord; /* No. of coordinates */ 1100 int rc; /* Callback return code */ 1101 RtreeCoord c; /* Translator union */ 1102 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */ 1103 1104 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY ); 1105 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 ); 1106 1107 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){ 1108 pInfo->iRowid = readInt64(pCellData); 1109 } 1110 pCellData += 8; 1111 #ifndef SQLITE_RTREE_INT_ONLY 1112 if( eInt==0 ){ 1113 switch( nCoord ){ 1114 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f; 1115 readCoord(pCellData+32, &c); aCoord[8] = c.f; 1116 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f; 1117 readCoord(pCellData+24, &c); aCoord[6] = c.f; 1118 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f; 1119 readCoord(pCellData+16, &c); aCoord[4] = c.f; 1120 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f; 1121 readCoord(pCellData+8, &c); aCoord[2] = c.f; 1122 default: readCoord(pCellData+4, &c); aCoord[1] = c.f; 1123 readCoord(pCellData, &c); aCoord[0] = c.f; 1124 } 1125 }else 1126 #endif 1127 { 1128 switch( nCoord ){ 1129 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i; 1130 readCoord(pCellData+32, &c); aCoord[8] = c.i; 1131 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i; 1132 readCoord(pCellData+24, &c); aCoord[6] = c.i; 1133 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i; 1134 readCoord(pCellData+16, &c); aCoord[4] = c.i; 1135 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i; 1136 readCoord(pCellData+8, &c); aCoord[2] = c.i; 1137 default: readCoord(pCellData+4, &c); aCoord[1] = c.i; 1138 readCoord(pCellData, &c); aCoord[0] = c.i; 1139 } 1140 } 1141 if( pConstraint->op==RTREE_MATCH ){ 1142 int eWithin = 0; 1143 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo, 1144 nCoord, aCoord, &eWithin); 1145 if( eWithin==0 ) *peWithin = NOT_WITHIN; 1146 *prScore = RTREE_ZERO; 1147 }else{ 1148 pInfo->aCoord = aCoord; 1149 pInfo->iLevel = pSearch->iLevel - 1; 1150 pInfo->rScore = pInfo->rParentScore = pSearch->rScore; 1151 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin; 1152 rc = pConstraint->u.xQueryFunc(pInfo); 1153 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin; 1154 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){ 1155 *prScore = pInfo->rScore; 1156 } 1157 } 1158 return rc; 1159 } 1160 1161 /* 1162 ** Check the internal RTree node given by pCellData against constraint p. 1163 ** If this constraint cannot be satisfied by any child within the node, 1164 ** set *peWithin to NOT_WITHIN. 1165 */ 1166 static void rtreeNonleafConstraint( 1167 RtreeConstraint *p, /* The constraint to test */ 1168 int eInt, /* True if RTree holds integer coordinates */ 1169 u8 *pCellData, /* Raw cell content as appears on disk */ 1170 int *peWithin /* Adjust downward, as appropriate */ 1171 ){ 1172 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */ 1173 1174 /* p->iCoord might point to either a lower or upper bound coordinate 1175 ** in a coordinate pair. But make pCellData point to the lower bound. 1176 */ 1177 pCellData += 8 + 4*(p->iCoord&0xfe); 1178 1179 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 1180 || p->op==RTREE_GT || p->op==RTREE_EQ ); 1181 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */ 1182 switch( p->op ){ 1183 case RTREE_LE: 1184 case RTREE_LT: 1185 case RTREE_EQ: 1186 RTREE_DECODE_COORD(eInt, pCellData, val); 1187 /* val now holds the lower bound of the coordinate pair */ 1188 if( p->u.rValue>=val ) return; 1189 if( p->op!=RTREE_EQ ) break; /* RTREE_LE and RTREE_LT end here */ 1190 /* Fall through for the RTREE_EQ case */ 1191 1192 default: /* RTREE_GT or RTREE_GE, or fallthrough of RTREE_EQ */ 1193 pCellData += 4; 1194 RTREE_DECODE_COORD(eInt, pCellData, val); 1195 /* val now holds the upper bound of the coordinate pair */ 1196 if( p->u.rValue<=val ) return; 1197 } 1198 *peWithin = NOT_WITHIN; 1199 } 1200 1201 /* 1202 ** Check the leaf RTree cell given by pCellData against constraint p. 1203 ** If this constraint is not satisfied, set *peWithin to NOT_WITHIN. 1204 ** If the constraint is satisfied, leave *peWithin unchanged. 1205 ** 1206 ** The constraint is of the form: xN op $val 1207 ** 1208 ** The op is given by p->op. The xN is p->iCoord-th coordinate in 1209 ** pCellData. $val is given by p->u.rValue. 1210 */ 1211 static void rtreeLeafConstraint( 1212 RtreeConstraint *p, /* The constraint to test */ 1213 int eInt, /* True if RTree holds integer coordinates */ 1214 u8 *pCellData, /* Raw cell content as appears on disk */ 1215 int *peWithin /* Adjust downward, as appropriate */ 1216 ){ 1217 RtreeDValue xN; /* Coordinate value converted to a double */ 1218 1219 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE 1220 || p->op==RTREE_GT || p->op==RTREE_EQ ); 1221 pCellData += 8 + p->iCoord*4; 1222 assert( ((((char*)pCellData) - (char*)0)&3)==0 ); /* 4-byte aligned */ 1223 RTREE_DECODE_COORD(eInt, pCellData, xN); 1224 switch( p->op ){ 1225 case RTREE_LE: if( xN <= p->u.rValue ) return; break; 1226 case RTREE_LT: if( xN < p->u.rValue ) return; break; 1227 case RTREE_GE: if( xN >= p->u.rValue ) return; break; 1228 case RTREE_GT: if( xN > p->u.rValue ) return; break; 1229 default: if( xN == p->u.rValue ) return; break; 1230 } 1231 *peWithin = NOT_WITHIN; 1232 } 1233 1234 /* 1235 ** One of the cells in node pNode is guaranteed to have a 64-bit 1236 ** integer value equal to iRowid. Return the index of this cell. 1237 */ 1238 static int nodeRowidIndex( 1239 Rtree *pRtree, 1240 RtreeNode *pNode, 1241 i64 iRowid, 1242 int *piIndex 1243 ){ 1244 int ii; 1245 int nCell = NCELL(pNode); 1246 assert( nCell<200 ); 1247 for(ii=0; ii<nCell; ii++){ 1248 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){ 1249 *piIndex = ii; 1250 return SQLITE_OK; 1251 } 1252 } 1253 return SQLITE_CORRUPT_VTAB; 1254 } 1255 1256 /* 1257 ** Return the index of the cell containing a pointer to node pNode 1258 ** in its parent. If pNode is the root node, return -1. 1259 */ 1260 static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){ 1261 RtreeNode *pParent = pNode->pParent; 1262 if( pParent ){ 1263 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex); 1264 } 1265 *piIndex = -1; 1266 return SQLITE_OK; 1267 } 1268 1269 /* 1270 ** Compare two search points. Return negative, zero, or positive if the first 1271 ** is less than, equal to, or greater than the second. 1272 ** 1273 ** The rScore is the primary key. Smaller rScore values come first. 1274 ** If the rScore is a tie, then use iLevel as the tie breaker with smaller 1275 ** iLevel values coming first. In this way, if rScore is the same for all 1276 ** SearchPoints, then iLevel becomes the deciding factor and the result 1277 ** is a depth-first search, which is the desired default behavior. 1278 */ 1279 static int rtreeSearchPointCompare( 1280 const RtreeSearchPoint *pA, 1281 const RtreeSearchPoint *pB 1282 ){ 1283 if( pA->rScore<pB->rScore ) return -1; 1284 if( pA->rScore>pB->rScore ) return +1; 1285 if( pA->iLevel<pB->iLevel ) return -1; 1286 if( pA->iLevel>pB->iLevel ) return +1; 1287 return 0; 1288 } 1289 1290 /* 1291 ** Interchange two search points in a cursor. 1292 */ 1293 static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){ 1294 RtreeSearchPoint t = p->aPoint[i]; 1295 assert( i<j ); 1296 p->aPoint[i] = p->aPoint[j]; 1297 p->aPoint[j] = t; 1298 i++; j++; 1299 if( i<RTREE_CACHE_SZ ){ 1300 if( j>=RTREE_CACHE_SZ ){ 1301 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]); 1302 p->aNode[i] = 0; 1303 }else{ 1304 RtreeNode *pTemp = p->aNode[i]; 1305 p->aNode[i] = p->aNode[j]; 1306 p->aNode[j] = pTemp; 1307 } 1308 } 1309 } 1310 1311 /* 1312 ** Return the search point with the lowest current score. 1313 */ 1314 static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){ 1315 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0; 1316 } 1317 1318 /* 1319 ** Get the RtreeNode for the search point with the lowest score. 1320 */ 1321 static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){ 1322 sqlite3_int64 id; 1323 int ii = 1 - pCur->bPoint; 1324 assert( ii==0 || ii==1 ); 1325 assert( pCur->bPoint || pCur->nPoint ); 1326 if( pCur->aNode[ii]==0 ){ 1327 assert( pRC!=0 ); 1328 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id; 1329 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]); 1330 } 1331 return pCur->aNode[ii]; 1332 } 1333 1334 /* 1335 ** Push a new element onto the priority queue 1336 */ 1337 static RtreeSearchPoint *rtreeEnqueue( 1338 RtreeCursor *pCur, /* The cursor */ 1339 RtreeDValue rScore, /* Score for the new search point */ 1340 u8 iLevel /* Level for the new search point */ 1341 ){ 1342 int i, j; 1343 RtreeSearchPoint *pNew; 1344 if( pCur->nPoint>=pCur->nPointAlloc ){ 1345 int nNew = pCur->nPointAlloc*2 + 8; 1346 pNew = sqlite3_realloc(pCur->aPoint, nNew*sizeof(pCur->aPoint[0])); 1347 if( pNew==0 ) return 0; 1348 pCur->aPoint = pNew; 1349 pCur->nPointAlloc = nNew; 1350 } 1351 i = pCur->nPoint++; 1352 pNew = pCur->aPoint + i; 1353 pNew->rScore = rScore; 1354 pNew->iLevel = iLevel; 1355 assert( iLevel<=RTREE_MAX_DEPTH ); 1356 while( i>0 ){ 1357 RtreeSearchPoint *pParent; 1358 j = (i-1)/2; 1359 pParent = pCur->aPoint + j; 1360 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break; 1361 rtreeSearchPointSwap(pCur, j, i); 1362 i = j; 1363 pNew = pParent; 1364 } 1365 return pNew; 1366 } 1367 1368 /* 1369 ** Allocate a new RtreeSearchPoint and return a pointer to it. Return 1370 ** NULL if malloc fails. 1371 */ 1372 static RtreeSearchPoint *rtreeSearchPointNew( 1373 RtreeCursor *pCur, /* The cursor */ 1374 RtreeDValue rScore, /* Score for the new search point */ 1375 u8 iLevel /* Level for the new search point */ 1376 ){ 1377 RtreeSearchPoint *pNew, *pFirst; 1378 pFirst = rtreeSearchPointFirst(pCur); 1379 pCur->anQueue[iLevel]++; 1380 if( pFirst==0 1381 || pFirst->rScore>rScore 1382 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel) 1383 ){ 1384 if( pCur->bPoint ){ 1385 int ii; 1386 pNew = rtreeEnqueue(pCur, rScore, iLevel); 1387 if( pNew==0 ) return 0; 1388 ii = (int)(pNew - pCur->aPoint) + 1; 1389 if( ii<RTREE_CACHE_SZ ){ 1390 assert( pCur->aNode[ii]==0 ); 1391 pCur->aNode[ii] = pCur->aNode[0]; 1392 }else{ 1393 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]); 1394 } 1395 pCur->aNode[0] = 0; 1396 *pNew = pCur->sPoint; 1397 } 1398 pCur->sPoint.rScore = rScore; 1399 pCur->sPoint.iLevel = iLevel; 1400 pCur->bPoint = 1; 1401 return &pCur->sPoint; 1402 }else{ 1403 return rtreeEnqueue(pCur, rScore, iLevel); 1404 } 1405 } 1406 1407 #if 0 1408 /* Tracing routines for the RtreeSearchPoint queue */ 1409 static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){ 1410 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); } 1411 printf(" %d.%05lld.%02d %g %d", 1412 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin 1413 ); 1414 idx++; 1415 if( idx<RTREE_CACHE_SZ ){ 1416 printf(" %p\n", pCur->aNode[idx]); 1417 }else{ 1418 printf("\n"); 1419 } 1420 } 1421 static void traceQueue(RtreeCursor *pCur, const char *zPrefix){ 1422 int ii; 1423 printf("=== %9s ", zPrefix); 1424 if( pCur->bPoint ){ 1425 tracePoint(&pCur->sPoint, -1, pCur); 1426 } 1427 for(ii=0; ii<pCur->nPoint; ii++){ 1428 if( ii>0 || pCur->bPoint ) printf(" "); 1429 tracePoint(&pCur->aPoint[ii], ii, pCur); 1430 } 1431 } 1432 # define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B) 1433 #else 1434 # define RTREE_QUEUE_TRACE(A,B) /* no-op */ 1435 #endif 1436 1437 /* Remove the search point with the lowest current score. 1438 */ 1439 static void rtreeSearchPointPop(RtreeCursor *p){ 1440 int i, j, k, n; 1441 i = 1 - p->bPoint; 1442 assert( i==0 || i==1 ); 1443 if( p->aNode[i] ){ 1444 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]); 1445 p->aNode[i] = 0; 1446 } 1447 if( p->bPoint ){ 1448 p->anQueue[p->sPoint.iLevel]--; 1449 p->bPoint = 0; 1450 }else if( p->nPoint ){ 1451 p->anQueue[p->aPoint[0].iLevel]--; 1452 n = --p->nPoint; 1453 p->aPoint[0] = p->aPoint[n]; 1454 if( n<RTREE_CACHE_SZ-1 ){ 1455 p->aNode[1] = p->aNode[n+1]; 1456 p->aNode[n+1] = 0; 1457 } 1458 i = 0; 1459 while( (j = i*2+1)<n ){ 1460 k = j+1; 1461 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){ 1462 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){ 1463 rtreeSearchPointSwap(p, i, k); 1464 i = k; 1465 }else{ 1466 break; 1467 } 1468 }else{ 1469 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){ 1470 rtreeSearchPointSwap(p, i, j); 1471 i = j; 1472 }else{ 1473 break; 1474 } 1475 } 1476 } 1477 } 1478 } 1479 1480 1481 /* 1482 ** Continue the search on cursor pCur until the front of the queue 1483 ** contains an entry suitable for returning as a result-set row, 1484 ** or until the RtreeSearchPoint queue is empty, indicating that the 1485 ** query has completed. 1486 */ 1487 static int rtreeStepToLeaf(RtreeCursor *pCur){ 1488 RtreeSearchPoint *p; 1489 Rtree *pRtree = RTREE_OF_CURSOR(pCur); 1490 RtreeNode *pNode; 1491 int eWithin; 1492 int rc = SQLITE_OK; 1493 int nCell; 1494 int nConstraint = pCur->nConstraint; 1495 int ii; 1496 int eInt; 1497 RtreeSearchPoint x; 1498 1499 eInt = pRtree->eCoordType==RTREE_COORD_INT32; 1500 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){ 1501 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc); 1502 if( rc ) return rc; 1503 nCell = NCELL(pNode); 1504 assert( nCell<200 ); 1505 while( p->iCell<nCell ){ 1506 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1; 1507 u8 *pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell); 1508 eWithin = FULLY_WITHIN; 1509 for(ii=0; ii<nConstraint; ii++){ 1510 RtreeConstraint *pConstraint = pCur->aConstraint + ii; 1511 if( pConstraint->op>=RTREE_MATCH ){ 1512 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p, 1513 &rScore, &eWithin); 1514 if( rc ) return rc; 1515 }else if( p->iLevel==1 ){ 1516 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin); 1517 }else{ 1518 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin); 1519 } 1520 if( eWithin==NOT_WITHIN ) break; 1521 } 1522 p->iCell++; 1523 if( eWithin==NOT_WITHIN ) continue; 1524 x.iLevel = p->iLevel - 1; 1525 if( x.iLevel ){ 1526 x.id = readInt64(pCellData); 1527 x.iCell = 0; 1528 }else{ 1529 x.id = p->id; 1530 x.iCell = p->iCell - 1; 1531 } 1532 if( p->iCell>=nCell ){ 1533 RTREE_QUEUE_TRACE(pCur, "POP-S:"); 1534 rtreeSearchPointPop(pCur); 1535 } 1536 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO; 1537 p = rtreeSearchPointNew(pCur, rScore, x.iLevel); 1538 if( p==0 ) return SQLITE_NOMEM; 1539 p->eWithin = (u8)eWithin; 1540 p->id = x.id; 1541 p->iCell = x.iCell; 1542 RTREE_QUEUE_TRACE(pCur, "PUSH-S:"); 1543 break; 1544 } 1545 if( p->iCell>=nCell ){ 1546 RTREE_QUEUE_TRACE(pCur, "POP-Se:"); 1547 rtreeSearchPointPop(pCur); 1548 } 1549 } 1550 pCur->atEOF = p==0; 1551 return SQLITE_OK; 1552 } 1553 1554 /* 1555 ** Rtree virtual table module xNext method. 1556 */ 1557 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){ 1558 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; 1559 int rc = SQLITE_OK; 1560 1561 /* Move to the next entry that matches the configured constraints. */ 1562 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:"); 1563 rtreeSearchPointPop(pCsr); 1564 rc = rtreeStepToLeaf(pCsr); 1565 return rc; 1566 } 1567 1568 /* 1569 ** Rtree virtual table module xRowid method. 1570 */ 1571 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){ 1572 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; 1573 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr); 1574 int rc = SQLITE_OK; 1575 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc); 1576 if( rc==SQLITE_OK && p ){ 1577 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell); 1578 } 1579 return rc; 1580 } 1581 1582 /* 1583 ** Rtree virtual table module xColumn method. 1584 */ 1585 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){ 1586 Rtree *pRtree = (Rtree *)cur->pVtab; 1587 RtreeCursor *pCsr = (RtreeCursor *)cur; 1588 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr); 1589 RtreeCoord c; 1590 int rc = SQLITE_OK; 1591 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc); 1592 1593 if( rc ) return rc; 1594 if( p==0 ) return SQLITE_OK; 1595 if( i==0 ){ 1596 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell)); 1597 }else{ 1598 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c); 1599 #ifndef SQLITE_RTREE_INT_ONLY 1600 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ 1601 sqlite3_result_double(ctx, c.f); 1602 }else 1603 #endif 1604 { 1605 assert( pRtree->eCoordType==RTREE_COORD_INT32 ); 1606 sqlite3_result_int(ctx, c.i); 1607 } 1608 } 1609 return SQLITE_OK; 1610 } 1611 1612 /* 1613 ** Use nodeAcquire() to obtain the leaf node containing the record with 1614 ** rowid iRowid. If successful, set *ppLeaf to point to the node and 1615 ** return SQLITE_OK. If there is no such record in the table, set 1616 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf 1617 ** to zero and return an SQLite error code. 1618 */ 1619 static int findLeafNode( 1620 Rtree *pRtree, /* RTree to search */ 1621 i64 iRowid, /* The rowid searching for */ 1622 RtreeNode **ppLeaf, /* Write the node here */ 1623 sqlite3_int64 *piNode /* Write the node-id here */ 1624 ){ 1625 int rc; 1626 *ppLeaf = 0; 1627 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid); 1628 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){ 1629 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0); 1630 if( piNode ) *piNode = iNode; 1631 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf); 1632 sqlite3_reset(pRtree->pReadRowid); 1633 }else{ 1634 rc = sqlite3_reset(pRtree->pReadRowid); 1635 } 1636 return rc; 1637 } 1638 1639 /* 1640 ** This function is called to configure the RtreeConstraint object passed 1641 ** as the second argument for a MATCH constraint. The value passed as the 1642 ** first argument to this function is the right-hand operand to the MATCH 1643 ** operator. 1644 */ 1645 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){ 1646 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */ 1647 sqlite3_rtree_query_info *pInfo; /* Callback information */ 1648 1649 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg"); 1650 if( pSrc==0 ) return SQLITE_ERROR; 1651 pInfo = (sqlite3_rtree_query_info*) 1652 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize ); 1653 if( !pInfo ) return SQLITE_NOMEM; 1654 memset(pInfo, 0, sizeof(*pInfo)); 1655 pBlob = (RtreeMatchArg*)&pInfo[1]; 1656 memcpy(pBlob, pSrc, pSrc->iSize); 1657 pInfo->pContext = pBlob->cb.pContext; 1658 pInfo->nParam = pBlob->nParam; 1659 pInfo->aParam = pBlob->aParam; 1660 pInfo->apSqlParam = pBlob->apSqlParam; 1661 1662 if( pBlob->cb.xGeom ){ 1663 pCons->u.xGeom = pBlob->cb.xGeom; 1664 }else{ 1665 pCons->op = RTREE_QUERY; 1666 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc; 1667 } 1668 pCons->pInfo = pInfo; 1669 return SQLITE_OK; 1670 } 1671 1672 /* 1673 ** Rtree virtual table module xFilter method. 1674 */ 1675 static int rtreeFilter( 1676 sqlite3_vtab_cursor *pVtabCursor, 1677 int idxNum, const char *idxStr, 1678 int argc, sqlite3_value **argv 1679 ){ 1680 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab; 1681 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor; 1682 RtreeNode *pRoot = 0; 1683 int ii; 1684 int rc = SQLITE_OK; 1685 int iCell = 0; 1686 1687 rtreeReference(pRtree); 1688 1689 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */ 1690 freeCursorConstraints(pCsr); 1691 sqlite3_free(pCsr->aPoint); 1692 memset(pCsr, 0, sizeof(RtreeCursor)); 1693 pCsr->base.pVtab = (sqlite3_vtab*)pRtree; 1694 1695 pCsr->iStrategy = idxNum; 1696 if( idxNum==1 ){ 1697 /* Special case - lookup by rowid. */ 1698 RtreeNode *pLeaf; /* Leaf on which the required cell resides */ 1699 RtreeSearchPoint *p; /* Search point for the leaf */ 1700 i64 iRowid = sqlite3_value_int64(argv[0]); 1701 i64 iNode = 0; 1702 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode); 1703 if( rc==SQLITE_OK && pLeaf!=0 ){ 1704 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0); 1705 assert( p!=0 ); /* Always returns pCsr->sPoint */ 1706 pCsr->aNode[0] = pLeaf; 1707 p->id = iNode; 1708 p->eWithin = PARTLY_WITHIN; 1709 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell); 1710 p->iCell = (u8)iCell; 1711 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:"); 1712 }else{ 1713 pCsr->atEOF = 1; 1714 } 1715 }else{ 1716 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 1717 ** with the configured constraints. 1718 */ 1719 rc = nodeAcquire(pRtree, 1, 0, &pRoot); 1720 if( rc==SQLITE_OK && argc>0 ){ 1721 pCsr->aConstraint = sqlite3_malloc(sizeof(RtreeConstraint)*argc); 1722 pCsr->nConstraint = argc; 1723 if( !pCsr->aConstraint ){ 1724 rc = SQLITE_NOMEM; 1725 }else{ 1726 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc); 1727 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1)); 1728 assert( (idxStr==0 && argc==0) 1729 || (idxStr && (int)strlen(idxStr)==argc*2) ); 1730 for(ii=0; ii<argc; ii++){ 1731 RtreeConstraint *p = &pCsr->aConstraint[ii]; 1732 p->op = idxStr[ii*2]; 1733 p->iCoord = idxStr[ii*2+1]-'0'; 1734 if( p->op>=RTREE_MATCH ){ 1735 /* A MATCH operator. The right-hand-side must be a blob that 1736 ** can be cast into an RtreeMatchArg object. One created using 1737 ** an sqlite3_rtree_geometry_callback() SQL user function. 1738 */ 1739 rc = deserializeGeometry(argv[ii], p); 1740 if( rc!=SQLITE_OK ){ 1741 break; 1742 } 1743 p->pInfo->nCoord = pRtree->nDim2; 1744 p->pInfo->anQueue = pCsr->anQueue; 1745 p->pInfo->mxLevel = pRtree->iDepth + 1; 1746 }else{ 1747 #ifdef SQLITE_RTREE_INT_ONLY 1748 p->u.rValue = sqlite3_value_int64(argv[ii]); 1749 #else 1750 p->u.rValue = sqlite3_value_double(argv[ii]); 1751 #endif 1752 } 1753 } 1754 } 1755 } 1756 if( rc==SQLITE_OK ){ 1757 RtreeSearchPoint *pNew; 1758 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1)); 1759 if( pNew==0 ) return SQLITE_NOMEM; 1760 pNew->id = 1; 1761 pNew->iCell = 0; 1762 pNew->eWithin = PARTLY_WITHIN; 1763 assert( pCsr->bPoint==1 ); 1764 pCsr->aNode[0] = pRoot; 1765 pRoot = 0; 1766 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:"); 1767 rc = rtreeStepToLeaf(pCsr); 1768 } 1769 } 1770 1771 nodeRelease(pRtree, pRoot); 1772 rtreeRelease(pRtree); 1773 return rc; 1774 } 1775 1776 /* 1777 ** Rtree virtual table module xBestIndex method. There are three 1778 ** table scan strategies to choose from (in order from most to 1779 ** least desirable): 1780 ** 1781 ** idxNum idxStr Strategy 1782 ** ------------------------------------------------ 1783 ** 1 Unused Direct lookup by rowid. 1784 ** 2 See below R-tree query or full-table scan. 1785 ** ------------------------------------------------ 1786 ** 1787 ** If strategy 1 is used, then idxStr is not meaningful. If strategy 1788 ** 2 is used, idxStr is formatted to contain 2 bytes for each 1789 ** constraint used. The first two bytes of idxStr correspond to 1790 ** the constraint in sqlite3_index_info.aConstraintUsage[] with 1791 ** (argvIndex==1) etc. 1792 ** 1793 ** The first of each pair of bytes in idxStr identifies the constraint 1794 ** operator as follows: 1795 ** 1796 ** Operator Byte Value 1797 ** ---------------------- 1798 ** = 0x41 ('A') 1799 ** <= 0x42 ('B') 1800 ** < 0x43 ('C') 1801 ** >= 0x44 ('D') 1802 ** > 0x45 ('E') 1803 ** MATCH 0x46 ('F') 1804 ** ---------------------- 1805 ** 1806 ** The second of each pair of bytes identifies the coordinate column 1807 ** to which the constraint applies. The leftmost coordinate column 1808 ** is 'a', the second from the left 'b' etc. 1809 */ 1810 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ 1811 Rtree *pRtree = (Rtree*)tab; 1812 int rc = SQLITE_OK; 1813 int ii; 1814 int bMatch = 0; /* True if there exists a MATCH constraint */ 1815 i64 nRow; /* Estimated rows returned by this scan */ 1816 1817 int iIdx = 0; 1818 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1]; 1819 memset(zIdxStr, 0, sizeof(zIdxStr)); 1820 1821 /* Check if there exists a MATCH constraint - even an unusable one. If there 1822 ** is, do not consider the lookup-by-rowid plan as using such a plan would 1823 ** require the VDBE to evaluate the MATCH constraint, which is not currently 1824 ** possible. */ 1825 for(ii=0; ii<pIdxInfo->nConstraint; ii++){ 1826 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){ 1827 bMatch = 1; 1828 } 1829 } 1830 1831 assert( pIdxInfo->idxStr==0 ); 1832 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){ 1833 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii]; 1834 1835 if( bMatch==0 && p->usable 1836 && p->iColumn==0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ 1837 ){ 1838 /* We have an equality constraint on the rowid. Use strategy 1. */ 1839 int jj; 1840 for(jj=0; jj<ii; jj++){ 1841 pIdxInfo->aConstraintUsage[jj].argvIndex = 0; 1842 pIdxInfo->aConstraintUsage[jj].omit = 0; 1843 } 1844 pIdxInfo->idxNum = 1; 1845 pIdxInfo->aConstraintUsage[ii].argvIndex = 1; 1846 pIdxInfo->aConstraintUsage[jj].omit = 1; 1847 1848 /* This strategy involves a two rowid lookups on an B-Tree structures 1849 ** and then a linear search of an R-Tree node. This should be 1850 ** considered almost as quick as a direct rowid lookup (for which 1851 ** sqlite uses an internal cost of 0.0). It is expected to return 1852 ** a single row. 1853 */ 1854 pIdxInfo->estimatedCost = 30.0; 1855 pIdxInfo->estimatedRows = 1; 1856 return SQLITE_OK; 1857 } 1858 1859 if( p->usable && (p->iColumn>0 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH) ){ 1860 u8 op; 1861 switch( p->op ){ 1862 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; break; 1863 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; break; 1864 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break; 1865 case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; break; 1866 case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break; 1867 default: 1868 assert( p->op==SQLITE_INDEX_CONSTRAINT_MATCH ); 1869 op = RTREE_MATCH; 1870 break; 1871 } 1872 zIdxStr[iIdx++] = op; 1873 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0'); 1874 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2); 1875 pIdxInfo->aConstraintUsage[ii].omit = 1; 1876 } 1877 } 1878 1879 pIdxInfo->idxNum = 2; 1880 pIdxInfo->needToFreeIdxStr = 1; 1881 if( iIdx>0 && 0==(pIdxInfo->idxStr = sqlite3_mprintf("%s", zIdxStr)) ){ 1882 return SQLITE_NOMEM; 1883 } 1884 1885 nRow = pRtree->nRowEst >> (iIdx/2); 1886 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow; 1887 pIdxInfo->estimatedRows = nRow; 1888 1889 return rc; 1890 } 1891 1892 /* 1893 ** Return the N-dimensional volumn of the cell stored in *p. 1894 */ 1895 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){ 1896 RtreeDValue area = (RtreeDValue)1; 1897 assert( pRtree->nDim>=1 && pRtree->nDim<=5 ); 1898 #ifndef SQLITE_RTREE_INT_ONLY 1899 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ 1900 switch( pRtree->nDim ){ 1901 case 5: area = p->aCoord[9].f - p->aCoord[8].f; 1902 case 4: area *= p->aCoord[7].f - p->aCoord[6].f; 1903 case 3: area *= p->aCoord[5].f - p->aCoord[4].f; 1904 case 2: area *= p->aCoord[3].f - p->aCoord[2].f; 1905 default: area *= p->aCoord[1].f - p->aCoord[0].f; 1906 } 1907 }else 1908 #endif 1909 { 1910 switch( pRtree->nDim ){ 1911 case 5: area = p->aCoord[9].i - p->aCoord[8].i; 1912 case 4: area *= p->aCoord[7].i - p->aCoord[6].i; 1913 case 3: area *= p->aCoord[5].i - p->aCoord[4].i; 1914 case 2: area *= p->aCoord[3].i - p->aCoord[2].i; 1915 default: area *= p->aCoord[1].i - p->aCoord[0].i; 1916 } 1917 } 1918 return area; 1919 } 1920 1921 /* 1922 ** Return the margin length of cell p. The margin length is the sum 1923 ** of the objects size in each dimension. 1924 */ 1925 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){ 1926 RtreeDValue margin = 0; 1927 int ii = pRtree->nDim2 - 2; 1928 do{ 1929 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii])); 1930 ii -= 2; 1931 }while( ii>=0 ); 1932 return margin; 1933 } 1934 1935 /* 1936 ** Store the union of cells p1 and p2 in p1. 1937 */ 1938 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ 1939 int ii = 0; 1940 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ 1941 do{ 1942 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f); 1943 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f); 1944 ii += 2; 1945 }while( ii<pRtree->nDim2 ); 1946 }else{ 1947 do{ 1948 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i); 1949 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i); 1950 ii += 2; 1951 }while( ii<pRtree->nDim2 ); 1952 } 1953 } 1954 1955 /* 1956 ** Return true if the area covered by p2 is a subset of the area covered 1957 ** by p1. False otherwise. 1958 */ 1959 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){ 1960 int ii; 1961 int isInt = (pRtree->eCoordType==RTREE_COORD_INT32); 1962 for(ii=0; ii<pRtree->nDim2; ii+=2){ 1963 RtreeCoord *a1 = &p1->aCoord[ii]; 1964 RtreeCoord *a2 = &p2->aCoord[ii]; 1965 if( (!isInt && (a2[0].f<a1[0].f || a2[1].f>a1[1].f)) 1966 || ( isInt && (a2[0].i<a1[0].i || a2[1].i>a1[1].i)) 1967 ){ 1968 return 0; 1969 } 1970 } 1971 return 1; 1972 } 1973 1974 /* 1975 ** Return the amount cell p would grow by if it were unioned with pCell. 1976 */ 1977 static RtreeDValue cellGrowth(Rtree *pRtree, RtreeCell *p, RtreeCell *pCell){ 1978 RtreeDValue area; 1979 RtreeCell cell; 1980 memcpy(&cell, p, sizeof(RtreeCell)); 1981 area = cellArea(pRtree, &cell); 1982 cellUnion(pRtree, &cell, pCell); 1983 return (cellArea(pRtree, &cell)-area); 1984 } 1985 1986 static RtreeDValue cellOverlap( 1987 Rtree *pRtree, 1988 RtreeCell *p, 1989 RtreeCell *aCell, 1990 int nCell 1991 ){ 1992 int ii; 1993 RtreeDValue overlap = RTREE_ZERO; 1994 for(ii=0; ii<nCell; ii++){ 1995 int jj; 1996 RtreeDValue o = (RtreeDValue)1; 1997 for(jj=0; jj<pRtree->nDim2; jj+=2){ 1998 RtreeDValue x1, x2; 1999 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj])); 2000 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1])); 2001 if( x2<x1 ){ 2002 o = (RtreeDValue)0; 2003 break; 2004 }else{ 2005 o = o * (x2-x1); 2006 } 2007 } 2008 overlap += o; 2009 } 2010 return overlap; 2011 } 2012 2013 2014 /* 2015 ** This function implements the ChooseLeaf algorithm from Gutman[84]. 2016 ** ChooseSubTree in r*tree terminology. 2017 */ 2018 static int ChooseLeaf( 2019 Rtree *pRtree, /* Rtree table */ 2020 RtreeCell *pCell, /* Cell to insert into rtree */ 2021 int iHeight, /* Height of sub-tree rooted at pCell */ 2022 RtreeNode **ppLeaf /* OUT: Selected leaf page */ 2023 ){ 2024 int rc; 2025 int ii; 2026 RtreeNode *pNode = 0; 2027 rc = nodeAcquire(pRtree, 1, 0, &pNode); 2028 2029 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){ 2030 int iCell; 2031 sqlite3_int64 iBest = 0; 2032 2033 RtreeDValue fMinGrowth = RTREE_ZERO; 2034 RtreeDValue fMinArea = RTREE_ZERO; 2035 2036 int nCell = NCELL(pNode); 2037 RtreeCell cell; 2038 RtreeNode *pChild; 2039 2040 RtreeCell *aCell = 0; 2041 2042 /* Select the child node which will be enlarged the least if pCell 2043 ** is inserted into it. Resolve ties by choosing the entry with 2044 ** the smallest area. 2045 */ 2046 for(iCell=0; iCell<nCell; iCell++){ 2047 int bBest = 0; 2048 RtreeDValue growth; 2049 RtreeDValue area; 2050 nodeGetCell(pRtree, pNode, iCell, &cell); 2051 growth = cellGrowth(pRtree, &cell, pCell); 2052 area = cellArea(pRtree, &cell); 2053 if( iCell==0||growth<fMinGrowth||(growth==fMinGrowth && area<fMinArea) ){ 2054 bBest = 1; 2055 } 2056 if( bBest ){ 2057 fMinGrowth = growth; 2058 fMinArea = area; 2059 iBest = cell.iRowid; 2060 } 2061 } 2062 2063 sqlite3_free(aCell); 2064 rc = nodeAcquire(pRtree, iBest, pNode, &pChild); 2065 nodeRelease(pRtree, pNode); 2066 pNode = pChild; 2067 } 2068 2069 *ppLeaf = pNode; 2070 return rc; 2071 } 2072 2073 /* 2074 ** A cell with the same content as pCell has just been inserted into 2075 ** the node pNode. This function updates the bounding box cells in 2076 ** all ancestor elements. 2077 */ 2078 static int AdjustTree( 2079 Rtree *pRtree, /* Rtree table */ 2080 RtreeNode *pNode, /* Adjust ancestry of this node. */ 2081 RtreeCell *pCell /* This cell was just inserted */ 2082 ){ 2083 RtreeNode *p = pNode; 2084 while( p->pParent ){ 2085 RtreeNode *pParent = p->pParent; 2086 RtreeCell cell; 2087 int iCell; 2088 2089 if( nodeParentIndex(pRtree, p, &iCell) ){ 2090 return SQLITE_CORRUPT_VTAB; 2091 } 2092 2093 nodeGetCell(pRtree, pParent, iCell, &cell); 2094 if( !cellContains(pRtree, &cell, pCell) ){ 2095 cellUnion(pRtree, &cell, pCell); 2096 nodeOverwriteCell(pRtree, pParent, &cell, iCell); 2097 } 2098 2099 p = pParent; 2100 } 2101 return SQLITE_OK; 2102 } 2103 2104 /* 2105 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table. 2106 */ 2107 static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){ 2108 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid); 2109 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode); 2110 sqlite3_step(pRtree->pWriteRowid); 2111 return sqlite3_reset(pRtree->pWriteRowid); 2112 } 2113 2114 /* 2115 ** Write mapping (iNode->iPar) to the <rtree>_parent table. 2116 */ 2117 static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){ 2118 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode); 2119 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar); 2120 sqlite3_step(pRtree->pWriteParent); 2121 return sqlite3_reset(pRtree->pWriteParent); 2122 } 2123 2124 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int); 2125 2126 2127 /* 2128 ** Arguments aIdx, aDistance and aSpare all point to arrays of size 2129 ** nIdx. The aIdx array contains the set of integers from 0 to 2130 ** (nIdx-1) in no particular order. This function sorts the values 2131 ** in aIdx according to the indexed values in aDistance. For 2132 ** example, assuming the inputs: 2133 ** 2134 ** aIdx = { 0, 1, 2, 3 } 2135 ** aDistance = { 5.0, 2.0, 7.0, 6.0 } 2136 ** 2137 ** this function sets the aIdx array to contain: 2138 ** 2139 ** aIdx = { 0, 1, 2, 3 } 2140 ** 2141 ** The aSpare array is used as temporary working space by the 2142 ** sorting algorithm. 2143 */ 2144 static void SortByDistance( 2145 int *aIdx, 2146 int nIdx, 2147 RtreeDValue *aDistance, 2148 int *aSpare 2149 ){ 2150 if( nIdx>1 ){ 2151 int iLeft = 0; 2152 int iRight = 0; 2153 2154 int nLeft = nIdx/2; 2155 int nRight = nIdx-nLeft; 2156 int *aLeft = aIdx; 2157 int *aRight = &aIdx[nLeft]; 2158 2159 SortByDistance(aLeft, nLeft, aDistance, aSpare); 2160 SortByDistance(aRight, nRight, aDistance, aSpare); 2161 2162 memcpy(aSpare, aLeft, sizeof(int)*nLeft); 2163 aLeft = aSpare; 2164 2165 while( iLeft<nLeft || iRight<nRight ){ 2166 if( iLeft==nLeft ){ 2167 aIdx[iLeft+iRight] = aRight[iRight]; 2168 iRight++; 2169 }else if( iRight==nRight ){ 2170 aIdx[iLeft+iRight] = aLeft[iLeft]; 2171 iLeft++; 2172 }else{ 2173 RtreeDValue fLeft = aDistance[aLeft[iLeft]]; 2174 RtreeDValue fRight = aDistance[aRight[iRight]]; 2175 if( fLeft<fRight ){ 2176 aIdx[iLeft+iRight] = aLeft[iLeft]; 2177 iLeft++; 2178 }else{ 2179 aIdx[iLeft+iRight] = aRight[iRight]; 2180 iRight++; 2181 } 2182 } 2183 } 2184 2185 #if 0 2186 /* Check that the sort worked */ 2187 { 2188 int jj; 2189 for(jj=1; jj<nIdx; jj++){ 2190 RtreeDValue left = aDistance[aIdx[jj-1]]; 2191 RtreeDValue right = aDistance[aIdx[jj]]; 2192 assert( left<=right ); 2193 } 2194 } 2195 #endif 2196 } 2197 } 2198 2199 /* 2200 ** Arguments aIdx, aCell and aSpare all point to arrays of size 2201 ** nIdx. The aIdx array contains the set of integers from 0 to 2202 ** (nIdx-1) in no particular order. This function sorts the values 2203 ** in aIdx according to dimension iDim of the cells in aCell. The 2204 ** minimum value of dimension iDim is considered first, the 2205 ** maximum used to break ties. 2206 ** 2207 ** The aSpare array is used as temporary working space by the 2208 ** sorting algorithm. 2209 */ 2210 static void SortByDimension( 2211 Rtree *pRtree, 2212 int *aIdx, 2213 int nIdx, 2214 int iDim, 2215 RtreeCell *aCell, 2216 int *aSpare 2217 ){ 2218 if( nIdx>1 ){ 2219 2220 int iLeft = 0; 2221 int iRight = 0; 2222 2223 int nLeft = nIdx/2; 2224 int nRight = nIdx-nLeft; 2225 int *aLeft = aIdx; 2226 int *aRight = &aIdx[nLeft]; 2227 2228 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare); 2229 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare); 2230 2231 memcpy(aSpare, aLeft, sizeof(int)*nLeft); 2232 aLeft = aSpare; 2233 while( iLeft<nLeft || iRight<nRight ){ 2234 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]); 2235 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]); 2236 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]); 2237 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]); 2238 if( (iLeft!=nLeft) && ((iRight==nRight) 2239 || (xleft1<xright1) 2240 || (xleft1==xright1 && xleft2<xright2) 2241 )){ 2242 aIdx[iLeft+iRight] = aLeft[iLeft]; 2243 iLeft++; 2244 }else{ 2245 aIdx[iLeft+iRight] = aRight[iRight]; 2246 iRight++; 2247 } 2248 } 2249 2250 #if 0 2251 /* Check that the sort worked */ 2252 { 2253 int jj; 2254 for(jj=1; jj<nIdx; jj++){ 2255 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2]; 2256 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1]; 2257 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2]; 2258 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1]; 2259 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) ); 2260 } 2261 } 2262 #endif 2263 } 2264 } 2265 2266 /* 2267 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990]. 2268 */ 2269 static int splitNodeStartree( 2270 Rtree *pRtree, 2271 RtreeCell *aCell, 2272 int nCell, 2273 RtreeNode *pLeft, 2274 RtreeNode *pRight, 2275 RtreeCell *pBboxLeft, 2276 RtreeCell *pBboxRight 2277 ){ 2278 int **aaSorted; 2279 int *aSpare; 2280 int ii; 2281 2282 int iBestDim = 0; 2283 int iBestSplit = 0; 2284 RtreeDValue fBestMargin = RTREE_ZERO; 2285 2286 int nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int)); 2287 2288 aaSorted = (int **)sqlite3_malloc(nByte); 2289 if( !aaSorted ){ 2290 return SQLITE_NOMEM; 2291 } 2292 2293 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell]; 2294 memset(aaSorted, 0, nByte); 2295 for(ii=0; ii<pRtree->nDim; ii++){ 2296 int jj; 2297 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell]; 2298 for(jj=0; jj<nCell; jj++){ 2299 aaSorted[ii][jj] = jj; 2300 } 2301 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare); 2302 } 2303 2304 for(ii=0; ii<pRtree->nDim; ii++){ 2305 RtreeDValue margin = RTREE_ZERO; 2306 RtreeDValue fBestOverlap = RTREE_ZERO; 2307 RtreeDValue fBestArea = RTREE_ZERO; 2308 int iBestLeft = 0; 2309 int nLeft; 2310 2311 for( 2312 nLeft=RTREE_MINCELLS(pRtree); 2313 nLeft<=(nCell-RTREE_MINCELLS(pRtree)); 2314 nLeft++ 2315 ){ 2316 RtreeCell left; 2317 RtreeCell right; 2318 int kk; 2319 RtreeDValue overlap; 2320 RtreeDValue area; 2321 2322 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell)); 2323 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell)); 2324 for(kk=1; kk<(nCell-1); kk++){ 2325 if( kk<nLeft ){ 2326 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]); 2327 }else{ 2328 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]); 2329 } 2330 } 2331 margin += cellMargin(pRtree, &left); 2332 margin += cellMargin(pRtree, &right); 2333 overlap = cellOverlap(pRtree, &left, &right, 1); 2334 area = cellArea(pRtree, &left) + cellArea(pRtree, &right); 2335 if( (nLeft==RTREE_MINCELLS(pRtree)) 2336 || (overlap<fBestOverlap) 2337 || (overlap==fBestOverlap && area<fBestArea) 2338 ){ 2339 iBestLeft = nLeft; 2340 fBestOverlap = overlap; 2341 fBestArea = area; 2342 } 2343 } 2344 2345 if( ii==0 || margin<fBestMargin ){ 2346 iBestDim = ii; 2347 fBestMargin = margin; 2348 iBestSplit = iBestLeft; 2349 } 2350 } 2351 2352 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell)); 2353 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell)); 2354 for(ii=0; ii<nCell; ii++){ 2355 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight; 2356 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight; 2357 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]]; 2358 nodeInsertCell(pRtree, pTarget, pCell); 2359 cellUnion(pRtree, pBbox, pCell); 2360 } 2361 2362 sqlite3_free(aaSorted); 2363 return SQLITE_OK; 2364 } 2365 2366 2367 static int updateMapping( 2368 Rtree *pRtree, 2369 i64 iRowid, 2370 RtreeNode *pNode, 2371 int iHeight 2372 ){ 2373 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64); 2374 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite); 2375 if( iHeight>0 ){ 2376 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid); 2377 if( pChild ){ 2378 nodeRelease(pRtree, pChild->pParent); 2379 nodeReference(pNode); 2380 pChild->pParent = pNode; 2381 } 2382 } 2383 return xSetMapping(pRtree, iRowid, pNode->iNode); 2384 } 2385 2386 static int SplitNode( 2387 Rtree *pRtree, 2388 RtreeNode *pNode, 2389 RtreeCell *pCell, 2390 int iHeight 2391 ){ 2392 int i; 2393 int newCellIsRight = 0; 2394 2395 int rc = SQLITE_OK; 2396 int nCell = NCELL(pNode); 2397 RtreeCell *aCell; 2398 int *aiUsed; 2399 2400 RtreeNode *pLeft = 0; 2401 RtreeNode *pRight = 0; 2402 2403 RtreeCell leftbbox; 2404 RtreeCell rightbbox; 2405 2406 /* Allocate an array and populate it with a copy of pCell and 2407 ** all cells from node pLeft. Then zero the original node. 2408 */ 2409 aCell = sqlite3_malloc((sizeof(RtreeCell)+sizeof(int))*(nCell+1)); 2410 if( !aCell ){ 2411 rc = SQLITE_NOMEM; 2412 goto splitnode_out; 2413 } 2414 aiUsed = (int *)&aCell[nCell+1]; 2415 memset(aiUsed, 0, sizeof(int)*(nCell+1)); 2416 for(i=0; i<nCell; i++){ 2417 nodeGetCell(pRtree, pNode, i, &aCell[i]); 2418 } 2419 nodeZero(pRtree, pNode); 2420 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell)); 2421 nCell++; 2422 2423 if( pNode->iNode==1 ){ 2424 pRight = nodeNew(pRtree, pNode); 2425 pLeft = nodeNew(pRtree, pNode); 2426 pRtree->iDepth++; 2427 pNode->isDirty = 1; 2428 writeInt16(pNode->zData, pRtree->iDepth); 2429 }else{ 2430 pLeft = pNode; 2431 pRight = nodeNew(pRtree, pLeft->pParent); 2432 nodeReference(pLeft); 2433 } 2434 2435 if( !pLeft || !pRight ){ 2436 rc = SQLITE_NOMEM; 2437 goto splitnode_out; 2438 } 2439 2440 memset(pLeft->zData, 0, pRtree->iNodeSize); 2441 memset(pRight->zData, 0, pRtree->iNodeSize); 2442 2443 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight, 2444 &leftbbox, &rightbbox); 2445 if( rc!=SQLITE_OK ){ 2446 goto splitnode_out; 2447 } 2448 2449 /* Ensure both child nodes have node numbers assigned to them by calling 2450 ** nodeWrite(). Node pRight always needs a node number, as it was created 2451 ** by nodeNew() above. But node pLeft sometimes already has a node number. 2452 ** In this case avoid the all to nodeWrite(). 2453 */ 2454 if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight)) 2455 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft))) 2456 ){ 2457 goto splitnode_out; 2458 } 2459 2460 rightbbox.iRowid = pRight->iNode; 2461 leftbbox.iRowid = pLeft->iNode; 2462 2463 if( pNode->iNode==1 ){ 2464 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1); 2465 if( rc!=SQLITE_OK ){ 2466 goto splitnode_out; 2467 } 2468 }else{ 2469 RtreeNode *pParent = pLeft->pParent; 2470 int iCell; 2471 rc = nodeParentIndex(pRtree, pLeft, &iCell); 2472 if( rc==SQLITE_OK ){ 2473 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell); 2474 rc = AdjustTree(pRtree, pParent, &leftbbox); 2475 } 2476 if( rc!=SQLITE_OK ){ 2477 goto splitnode_out; 2478 } 2479 } 2480 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){ 2481 goto splitnode_out; 2482 } 2483 2484 for(i=0; i<NCELL(pRight); i++){ 2485 i64 iRowid = nodeGetRowid(pRtree, pRight, i); 2486 rc = updateMapping(pRtree, iRowid, pRight, iHeight); 2487 if( iRowid==pCell->iRowid ){ 2488 newCellIsRight = 1; 2489 } 2490 if( rc!=SQLITE_OK ){ 2491 goto splitnode_out; 2492 } 2493 } 2494 if( pNode->iNode==1 ){ 2495 for(i=0; i<NCELL(pLeft); i++){ 2496 i64 iRowid = nodeGetRowid(pRtree, pLeft, i); 2497 rc = updateMapping(pRtree, iRowid, pLeft, iHeight); 2498 if( rc!=SQLITE_OK ){ 2499 goto splitnode_out; 2500 } 2501 } 2502 }else if( newCellIsRight==0 ){ 2503 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight); 2504 } 2505 2506 if( rc==SQLITE_OK ){ 2507 rc = nodeRelease(pRtree, pRight); 2508 pRight = 0; 2509 } 2510 if( rc==SQLITE_OK ){ 2511 rc = nodeRelease(pRtree, pLeft); 2512 pLeft = 0; 2513 } 2514 2515 splitnode_out: 2516 nodeRelease(pRtree, pRight); 2517 nodeRelease(pRtree, pLeft); 2518 sqlite3_free(aCell); 2519 return rc; 2520 } 2521 2522 /* 2523 ** If node pLeaf is not the root of the r-tree and its pParent pointer is 2524 ** still NULL, load all ancestor nodes of pLeaf into memory and populate 2525 ** the pLeaf->pParent chain all the way up to the root node. 2526 ** 2527 ** This operation is required when a row is deleted (or updated - an update 2528 ** is implemented as a delete followed by an insert). SQLite provides the 2529 ** rowid of the row to delete, which can be used to find the leaf on which 2530 ** the entry resides (argument pLeaf). Once the leaf is located, this 2531 ** function is called to determine its ancestry. 2532 */ 2533 static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){ 2534 int rc = SQLITE_OK; 2535 RtreeNode *pChild = pLeaf; 2536 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){ 2537 int rc2 = SQLITE_OK; /* sqlite3_reset() return code */ 2538 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode); 2539 rc = sqlite3_step(pRtree->pReadParent); 2540 if( rc==SQLITE_ROW ){ 2541 RtreeNode *pTest; /* Used to test for reference loops */ 2542 i64 iNode; /* Node number of parent node */ 2543 2544 /* Before setting pChild->pParent, test that we are not creating a 2545 ** loop of references (as we would if, say, pChild==pParent). We don't 2546 ** want to do this as it leads to a memory leak when trying to delete 2547 ** the referenced counted node structures. 2548 */ 2549 iNode = sqlite3_column_int64(pRtree->pReadParent, 0); 2550 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent); 2551 if( !pTest ){ 2552 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent); 2553 } 2554 } 2555 rc = sqlite3_reset(pRtree->pReadParent); 2556 if( rc==SQLITE_OK ) rc = rc2; 2557 if( rc==SQLITE_OK && !pChild->pParent ) rc = SQLITE_CORRUPT_VTAB; 2558 pChild = pChild->pParent; 2559 } 2560 return rc; 2561 } 2562 2563 static int deleteCell(Rtree *, RtreeNode *, int, int); 2564 2565 static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){ 2566 int rc; 2567 int rc2; 2568 RtreeNode *pParent = 0; 2569 int iCell; 2570 2571 assert( pNode->nRef==1 ); 2572 2573 /* Remove the entry in the parent cell. */ 2574 rc = nodeParentIndex(pRtree, pNode, &iCell); 2575 if( rc==SQLITE_OK ){ 2576 pParent = pNode->pParent; 2577 pNode->pParent = 0; 2578 rc = deleteCell(pRtree, pParent, iCell, iHeight+1); 2579 } 2580 rc2 = nodeRelease(pRtree, pParent); 2581 if( rc==SQLITE_OK ){ 2582 rc = rc2; 2583 } 2584 if( rc!=SQLITE_OK ){ 2585 return rc; 2586 } 2587 2588 /* Remove the xxx_node entry. */ 2589 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode); 2590 sqlite3_step(pRtree->pDeleteNode); 2591 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){ 2592 return rc; 2593 } 2594 2595 /* Remove the xxx_parent entry. */ 2596 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode); 2597 sqlite3_step(pRtree->pDeleteParent); 2598 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){ 2599 return rc; 2600 } 2601 2602 /* Remove the node from the in-memory hash table and link it into 2603 ** the Rtree.pDeleted list. Its contents will be re-inserted later on. 2604 */ 2605 nodeHashDelete(pRtree, pNode); 2606 pNode->iNode = iHeight; 2607 pNode->pNext = pRtree->pDeleted; 2608 pNode->nRef++; 2609 pRtree->pDeleted = pNode; 2610 2611 return SQLITE_OK; 2612 } 2613 2614 static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){ 2615 RtreeNode *pParent = pNode->pParent; 2616 int rc = SQLITE_OK; 2617 if( pParent ){ 2618 int ii; 2619 int nCell = NCELL(pNode); 2620 RtreeCell box; /* Bounding box for pNode */ 2621 nodeGetCell(pRtree, pNode, 0, &box); 2622 for(ii=1; ii<nCell; ii++){ 2623 RtreeCell cell; 2624 nodeGetCell(pRtree, pNode, ii, &cell); 2625 cellUnion(pRtree, &box, &cell); 2626 } 2627 box.iRowid = pNode->iNode; 2628 rc = nodeParentIndex(pRtree, pNode, &ii); 2629 if( rc==SQLITE_OK ){ 2630 nodeOverwriteCell(pRtree, pParent, &box, ii); 2631 rc = fixBoundingBox(pRtree, pParent); 2632 } 2633 } 2634 return rc; 2635 } 2636 2637 /* 2638 ** Delete the cell at index iCell of node pNode. After removing the 2639 ** cell, adjust the r-tree data structure if required. 2640 */ 2641 static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){ 2642 RtreeNode *pParent; 2643 int rc; 2644 2645 if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){ 2646 return rc; 2647 } 2648 2649 /* Remove the cell from the node. This call just moves bytes around 2650 ** the in-memory node image, so it cannot fail. 2651 */ 2652 nodeDeleteCell(pRtree, pNode, iCell); 2653 2654 /* If the node is not the tree root and now has less than the minimum 2655 ** number of cells, remove it from the tree. Otherwise, update the 2656 ** cell in the parent node so that it tightly contains the updated 2657 ** node. 2658 */ 2659 pParent = pNode->pParent; 2660 assert( pParent || pNode->iNode==1 ); 2661 if( pParent ){ 2662 if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){ 2663 rc = removeNode(pRtree, pNode, iHeight); 2664 }else{ 2665 rc = fixBoundingBox(pRtree, pNode); 2666 } 2667 } 2668 2669 return rc; 2670 } 2671 2672 static int Reinsert( 2673 Rtree *pRtree, 2674 RtreeNode *pNode, 2675 RtreeCell *pCell, 2676 int iHeight 2677 ){ 2678 int *aOrder; 2679 int *aSpare; 2680 RtreeCell *aCell; 2681 RtreeDValue *aDistance; 2682 int nCell; 2683 RtreeDValue aCenterCoord[RTREE_MAX_DIMENSIONS]; 2684 int iDim; 2685 int ii; 2686 int rc = SQLITE_OK; 2687 int n; 2688 2689 memset(aCenterCoord, 0, sizeof(RtreeDValue)*RTREE_MAX_DIMENSIONS); 2690 2691 nCell = NCELL(pNode)+1; 2692 n = (nCell+1)&(~1); 2693 2694 /* Allocate the buffers used by this operation. The allocation is 2695 ** relinquished before this function returns. 2696 */ 2697 aCell = (RtreeCell *)sqlite3_malloc(n * ( 2698 sizeof(RtreeCell) + /* aCell array */ 2699 sizeof(int) + /* aOrder array */ 2700 sizeof(int) + /* aSpare array */ 2701 sizeof(RtreeDValue) /* aDistance array */ 2702 )); 2703 if( !aCell ){ 2704 return SQLITE_NOMEM; 2705 } 2706 aOrder = (int *)&aCell[n]; 2707 aSpare = (int *)&aOrder[n]; 2708 aDistance = (RtreeDValue *)&aSpare[n]; 2709 2710 for(ii=0; ii<nCell; ii++){ 2711 if( ii==(nCell-1) ){ 2712 memcpy(&aCell[ii], pCell, sizeof(RtreeCell)); 2713 }else{ 2714 nodeGetCell(pRtree, pNode, ii, &aCell[ii]); 2715 } 2716 aOrder[ii] = ii; 2717 for(iDim=0; iDim<pRtree->nDim; iDim++){ 2718 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2]); 2719 aCenterCoord[iDim] += DCOORD(aCell[ii].aCoord[iDim*2+1]); 2720 } 2721 } 2722 for(iDim=0; iDim<pRtree->nDim; iDim++){ 2723 aCenterCoord[iDim] = (aCenterCoord[iDim]/(nCell*(RtreeDValue)2)); 2724 } 2725 2726 for(ii=0; ii<nCell; ii++){ 2727 aDistance[ii] = RTREE_ZERO; 2728 for(iDim=0; iDim<pRtree->nDim; iDim++){ 2729 RtreeDValue coord = (DCOORD(aCell[ii].aCoord[iDim*2+1]) - 2730 DCOORD(aCell[ii].aCoord[iDim*2])); 2731 aDistance[ii] += (coord-aCenterCoord[iDim])*(coord-aCenterCoord[iDim]); 2732 } 2733 } 2734 2735 SortByDistance(aOrder, nCell, aDistance, aSpare); 2736 nodeZero(pRtree, pNode); 2737 2738 for(ii=0; rc==SQLITE_OK && ii<(nCell-(RTREE_MINCELLS(pRtree)+1)); ii++){ 2739 RtreeCell *p = &aCell[aOrder[ii]]; 2740 nodeInsertCell(pRtree, pNode, p); 2741 if( p->iRowid==pCell->iRowid ){ 2742 if( iHeight==0 ){ 2743 rc = rowidWrite(pRtree, p->iRowid, pNode->iNode); 2744 }else{ 2745 rc = parentWrite(pRtree, p->iRowid, pNode->iNode); 2746 } 2747 } 2748 } 2749 if( rc==SQLITE_OK ){ 2750 rc = fixBoundingBox(pRtree, pNode); 2751 } 2752 for(; rc==SQLITE_OK && ii<nCell; ii++){ 2753 /* Find a node to store this cell in. pNode->iNode currently contains 2754 ** the height of the sub-tree headed by the cell. 2755 */ 2756 RtreeNode *pInsert; 2757 RtreeCell *p = &aCell[aOrder[ii]]; 2758 rc = ChooseLeaf(pRtree, p, iHeight, &pInsert); 2759 if( rc==SQLITE_OK ){ 2760 int rc2; 2761 rc = rtreeInsertCell(pRtree, pInsert, p, iHeight); 2762 rc2 = nodeRelease(pRtree, pInsert); 2763 if( rc==SQLITE_OK ){ 2764 rc = rc2; 2765 } 2766 } 2767 } 2768 2769 sqlite3_free(aCell); 2770 return rc; 2771 } 2772 2773 /* 2774 ** Insert cell pCell into node pNode. Node pNode is the head of a 2775 ** subtree iHeight high (leaf nodes have iHeight==0). 2776 */ 2777 static int rtreeInsertCell( 2778 Rtree *pRtree, 2779 RtreeNode *pNode, 2780 RtreeCell *pCell, 2781 int iHeight 2782 ){ 2783 int rc = SQLITE_OK; 2784 if( iHeight>0 ){ 2785 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid); 2786 if( pChild ){ 2787 nodeRelease(pRtree, pChild->pParent); 2788 nodeReference(pNode); 2789 pChild->pParent = pNode; 2790 } 2791 } 2792 if( nodeInsertCell(pRtree, pNode, pCell) ){ 2793 if( iHeight<=pRtree->iReinsertHeight || pNode->iNode==1){ 2794 rc = SplitNode(pRtree, pNode, pCell, iHeight); 2795 }else{ 2796 pRtree->iReinsertHeight = iHeight; 2797 rc = Reinsert(pRtree, pNode, pCell, iHeight); 2798 } 2799 }else{ 2800 rc = AdjustTree(pRtree, pNode, pCell); 2801 if( rc==SQLITE_OK ){ 2802 if( iHeight==0 ){ 2803 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode); 2804 }else{ 2805 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode); 2806 } 2807 } 2808 } 2809 return rc; 2810 } 2811 2812 static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){ 2813 int ii; 2814 int rc = SQLITE_OK; 2815 int nCell = NCELL(pNode); 2816 2817 for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){ 2818 RtreeNode *pInsert; 2819 RtreeCell cell; 2820 nodeGetCell(pRtree, pNode, ii, &cell); 2821 2822 /* Find a node to store this cell in. pNode->iNode currently contains 2823 ** the height of the sub-tree headed by the cell. 2824 */ 2825 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert); 2826 if( rc==SQLITE_OK ){ 2827 int rc2; 2828 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode); 2829 rc2 = nodeRelease(pRtree, pInsert); 2830 if( rc==SQLITE_OK ){ 2831 rc = rc2; 2832 } 2833 } 2834 } 2835 return rc; 2836 } 2837 2838 /* 2839 ** Select a currently unused rowid for a new r-tree record. 2840 */ 2841 static int newRowid(Rtree *pRtree, i64 *piRowid){ 2842 int rc; 2843 sqlite3_bind_null(pRtree->pWriteRowid, 1); 2844 sqlite3_bind_null(pRtree->pWriteRowid, 2); 2845 sqlite3_step(pRtree->pWriteRowid); 2846 rc = sqlite3_reset(pRtree->pWriteRowid); 2847 *piRowid = sqlite3_last_insert_rowid(pRtree->db); 2848 return rc; 2849 } 2850 2851 /* 2852 ** Remove the entry with rowid=iDelete from the r-tree structure. 2853 */ 2854 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){ 2855 int rc; /* Return code */ 2856 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */ 2857 int iCell; /* Index of iDelete cell in pLeaf */ 2858 RtreeNode *pRoot = 0; /* Root node of rtree structure */ 2859 2860 2861 /* Obtain a reference to the root node to initialize Rtree.iDepth */ 2862 rc = nodeAcquire(pRtree, 1, 0, &pRoot); 2863 2864 /* Obtain a reference to the leaf node that contains the entry 2865 ** about to be deleted. 2866 */ 2867 if( rc==SQLITE_OK ){ 2868 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0); 2869 } 2870 2871 /* Delete the cell in question from the leaf node. */ 2872 if( rc==SQLITE_OK ){ 2873 int rc2; 2874 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell); 2875 if( rc==SQLITE_OK ){ 2876 rc = deleteCell(pRtree, pLeaf, iCell, 0); 2877 } 2878 rc2 = nodeRelease(pRtree, pLeaf); 2879 if( rc==SQLITE_OK ){ 2880 rc = rc2; 2881 } 2882 } 2883 2884 /* Delete the corresponding entry in the <rtree>_rowid table. */ 2885 if( rc==SQLITE_OK ){ 2886 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete); 2887 sqlite3_step(pRtree->pDeleteRowid); 2888 rc = sqlite3_reset(pRtree->pDeleteRowid); 2889 } 2890 2891 /* Check if the root node now has exactly one child. If so, remove 2892 ** it, schedule the contents of the child for reinsertion and 2893 ** reduce the tree height by one. 2894 ** 2895 ** This is equivalent to copying the contents of the child into 2896 ** the root node (the operation that Gutman's paper says to perform 2897 ** in this scenario). 2898 */ 2899 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){ 2900 int rc2; 2901 RtreeNode *pChild = 0; 2902 i64 iChild = nodeGetRowid(pRtree, pRoot, 0); 2903 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); 2904 if( rc==SQLITE_OK ){ 2905 rc = removeNode(pRtree, pChild, pRtree->iDepth-1); 2906 } 2907 rc2 = nodeRelease(pRtree, pChild); 2908 if( rc==SQLITE_OK ) rc = rc2; 2909 if( rc==SQLITE_OK ){ 2910 pRtree->iDepth--; 2911 writeInt16(pRoot->zData, pRtree->iDepth); 2912 pRoot->isDirty = 1; 2913 } 2914 } 2915 2916 /* Re-insert the contents of any underfull nodes removed from the tree. */ 2917 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){ 2918 if( rc==SQLITE_OK ){ 2919 rc = reinsertNodeContent(pRtree, pLeaf); 2920 } 2921 pRtree->pDeleted = pLeaf->pNext; 2922 sqlite3_free(pLeaf); 2923 } 2924 2925 /* Release the reference to the root node. */ 2926 if( rc==SQLITE_OK ){ 2927 rc = nodeRelease(pRtree, pRoot); 2928 }else{ 2929 nodeRelease(pRtree, pRoot); 2930 } 2931 2932 return rc; 2933 } 2934 2935 /* 2936 ** Rounding constants for float->double conversion. 2937 */ 2938 #define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */ 2939 #define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */ 2940 2941 #if !defined(SQLITE_RTREE_INT_ONLY) 2942 /* 2943 ** Convert an sqlite3_value into an RtreeValue (presumably a float) 2944 ** while taking care to round toward negative or positive, respectively. 2945 */ 2946 static RtreeValue rtreeValueDown(sqlite3_value *v){ 2947 double d = sqlite3_value_double(v); 2948 float f = (float)d; 2949 if( f>d ){ 2950 f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS)); 2951 } 2952 return f; 2953 } 2954 static RtreeValue rtreeValueUp(sqlite3_value *v){ 2955 double d = sqlite3_value_double(v); 2956 float f = (float)d; 2957 if( f<d ){ 2958 f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY)); 2959 } 2960 return f; 2961 } 2962 #endif /* !defined(SQLITE_RTREE_INT_ONLY) */ 2963 2964 /* 2965 ** A constraint has failed while inserting a row into an rtree table. 2966 ** Assuming no OOM error occurs, this function sets the error message 2967 ** (at pRtree->base.zErrMsg) to an appropriate value and returns 2968 ** SQLITE_CONSTRAINT. 2969 ** 2970 ** Parameter iCol is the index of the leftmost column involved in the 2971 ** constraint failure. If it is 0, then the constraint that failed is 2972 ** the unique constraint on the id column. Otherwise, it is the rtree 2973 ** (c1<=c2) constraint on columns iCol and iCol+1 that has failed. 2974 ** 2975 ** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT. 2976 */ 2977 static int rtreeConstraintError(Rtree *pRtree, int iCol){ 2978 sqlite3_stmt *pStmt = 0; 2979 char *zSql; 2980 int rc; 2981 2982 assert( iCol==0 || iCol%2 ); 2983 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName); 2984 if( zSql ){ 2985 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0); 2986 }else{ 2987 rc = SQLITE_NOMEM; 2988 } 2989 sqlite3_free(zSql); 2990 2991 if( rc==SQLITE_OK ){ 2992 if( iCol==0 ){ 2993 const char *zCol = sqlite3_column_name(pStmt, 0); 2994 pRtree->base.zErrMsg = sqlite3_mprintf( 2995 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol 2996 ); 2997 }else{ 2998 const char *zCol1 = sqlite3_column_name(pStmt, iCol); 2999 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1); 3000 pRtree->base.zErrMsg = sqlite3_mprintf( 3001 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2 3002 ); 3003 } 3004 } 3005 3006 sqlite3_finalize(pStmt); 3007 return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc); 3008 } 3009 3010 3011 3012 /* 3013 ** The xUpdate method for rtree module virtual tables. 3014 */ 3015 static int rtreeUpdate( 3016 sqlite3_vtab *pVtab, 3017 int nData, 3018 sqlite3_value **azData, 3019 sqlite_int64 *pRowid 3020 ){ 3021 Rtree *pRtree = (Rtree *)pVtab; 3022 int rc = SQLITE_OK; 3023 RtreeCell cell; /* New cell to insert if nData>1 */ 3024 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */ 3025 3026 rtreeReference(pRtree); 3027 assert(nData>=1); 3028 3029 cell.iRowid = 0; /* Used only to suppress a compiler warning */ 3030 3031 /* Constraint handling. A write operation on an r-tree table may return 3032 ** SQLITE_CONSTRAINT for two reasons: 3033 ** 3034 ** 1. A duplicate rowid value, or 3035 ** 2. The supplied data violates the "x2>=x1" constraint. 3036 ** 3037 ** In the first case, if the conflict-handling mode is REPLACE, then 3038 ** the conflicting row can be removed before proceeding. In the second 3039 ** case, SQLITE_CONSTRAINT must be returned regardless of the 3040 ** conflict-handling mode specified by the user. 3041 */ 3042 if( nData>1 ){ 3043 int ii; 3044 3045 /* Populate the cell.aCoord[] array. The first coordinate is azData[3]. 3046 ** 3047 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared 3048 ** with "column" that are interpreted as table constraints. 3049 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5)); 3050 ** This problem was discovered after years of use, so we silently ignore 3051 ** these kinds of misdeclared tables to avoid breaking any legacy. 3052 */ 3053 assert( nData<=(pRtree->nDim2 + 3) ); 3054 3055 #ifndef SQLITE_RTREE_INT_ONLY 3056 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){ 3057 for(ii=0; ii<nData-4; ii+=2){ 3058 cell.aCoord[ii].f = rtreeValueDown(azData[ii+3]); 3059 cell.aCoord[ii+1].f = rtreeValueUp(azData[ii+4]); 3060 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){ 3061 rc = rtreeConstraintError(pRtree, ii+1); 3062 goto constraint; 3063 } 3064 } 3065 }else 3066 #endif 3067 { 3068 for(ii=0; ii<nData-4; ii+=2){ 3069 cell.aCoord[ii].i = sqlite3_value_int(azData[ii+3]); 3070 cell.aCoord[ii+1].i = sqlite3_value_int(azData[ii+4]); 3071 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){ 3072 rc = rtreeConstraintError(pRtree, ii+1); 3073 goto constraint; 3074 } 3075 } 3076 } 3077 3078 /* If a rowid value was supplied, check if it is already present in 3079 ** the table. If so, the constraint has failed. */ 3080 if( sqlite3_value_type(azData[2])!=SQLITE_NULL ){ 3081 cell.iRowid = sqlite3_value_int64(azData[2]); 3082 if( sqlite3_value_type(azData[0])==SQLITE_NULL 3083 || sqlite3_value_int64(azData[0])!=cell.iRowid 3084 ){ 3085 int steprc; 3086 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid); 3087 steprc = sqlite3_step(pRtree->pReadRowid); 3088 rc = sqlite3_reset(pRtree->pReadRowid); 3089 if( SQLITE_ROW==steprc ){ 3090 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){ 3091 rc = rtreeDeleteRowid(pRtree, cell.iRowid); 3092 }else{ 3093 rc = rtreeConstraintError(pRtree, 0); 3094 goto constraint; 3095 } 3096 } 3097 } 3098 bHaveRowid = 1; 3099 } 3100 } 3101 3102 /* If azData[0] is not an SQL NULL value, it is the rowid of a 3103 ** record to delete from the r-tree table. The following block does 3104 ** just that. 3105 */ 3106 if( sqlite3_value_type(azData[0])!=SQLITE_NULL ){ 3107 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(azData[0])); 3108 } 3109 3110 /* If the azData[] array contains more than one element, elements 3111 ** (azData[2]..azData[argc-1]) contain a new record to insert into 3112 ** the r-tree structure. 3113 */ 3114 if( rc==SQLITE_OK && nData>1 ){ 3115 /* Insert the new record into the r-tree */ 3116 RtreeNode *pLeaf = 0; 3117 3118 /* Figure out the rowid of the new row. */ 3119 if( bHaveRowid==0 ){ 3120 rc = newRowid(pRtree, &cell.iRowid); 3121 } 3122 *pRowid = cell.iRowid; 3123 3124 if( rc==SQLITE_OK ){ 3125 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf); 3126 } 3127 if( rc==SQLITE_OK ){ 3128 int rc2; 3129 pRtree->iReinsertHeight = -1; 3130 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0); 3131 rc2 = nodeRelease(pRtree, pLeaf); 3132 if( rc==SQLITE_OK ){ 3133 rc = rc2; 3134 } 3135 } 3136 } 3137 3138 constraint: 3139 rtreeRelease(pRtree); 3140 return rc; 3141 } 3142 3143 /* 3144 ** Called when a transaction starts. 3145 */ 3146 static int rtreeBeginTransaction(sqlite3_vtab *pVtab){ 3147 Rtree *pRtree = (Rtree *)pVtab; 3148 assert( pRtree->inWrTrans==0 ); 3149 pRtree->inWrTrans++; 3150 return SQLITE_OK; 3151 } 3152 3153 /* 3154 ** Called when a transaction completes (either by COMMIT or ROLLBACK). 3155 ** The sqlite3_blob object should be released at this point. 3156 */ 3157 static int rtreeEndTransaction(sqlite3_vtab *pVtab){ 3158 Rtree *pRtree = (Rtree *)pVtab; 3159 pRtree->inWrTrans = 0; 3160 nodeBlobReset(pRtree); 3161 return SQLITE_OK; 3162 } 3163 3164 /* 3165 ** The xRename method for rtree module virtual tables. 3166 */ 3167 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){ 3168 Rtree *pRtree = (Rtree *)pVtab; 3169 int rc = SQLITE_NOMEM; 3170 char *zSql = sqlite3_mprintf( 3171 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";" 3172 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";" 3173 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";" 3174 , pRtree->zDb, pRtree->zName, zNewName 3175 , pRtree->zDb, pRtree->zName, zNewName 3176 , pRtree->zDb, pRtree->zName, zNewName 3177 ); 3178 if( zSql ){ 3179 nodeBlobReset(pRtree); 3180 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0); 3181 sqlite3_free(zSql); 3182 } 3183 return rc; 3184 } 3185 3186 /* 3187 ** The xSavepoint method. 3188 ** 3189 ** This module does not need to do anything to support savepoints. However, 3190 ** it uses this hook to close any open blob handle. This is done because a 3191 ** DROP TABLE command - which fortunately always opens a savepoint - cannot 3192 ** succeed if there are any open blob handles. i.e. if the blob handle were 3193 ** not closed here, the following would fail: 3194 ** 3195 ** BEGIN; 3196 ** INSERT INTO rtree... 3197 ** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED 3198 ** COMMIT; 3199 */ 3200 static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){ 3201 Rtree *pRtree = (Rtree *)pVtab; 3202 int iwt = pRtree->inWrTrans; 3203 UNUSED_PARAMETER(iSavepoint); 3204 pRtree->inWrTrans = 0; 3205 nodeBlobReset(pRtree); 3206 pRtree->inWrTrans = iwt; 3207 return SQLITE_OK; 3208 } 3209 3210 /* 3211 ** This function populates the pRtree->nRowEst variable with an estimate 3212 ** of the number of rows in the virtual table. If possible, this is based 3213 ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST. 3214 */ 3215 static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){ 3216 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'"; 3217 char *zSql; 3218 sqlite3_stmt *p; 3219 int rc; 3220 i64 nRow = 0; 3221 3222 rc = sqlite3_table_column_metadata( 3223 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0 3224 ); 3225 if( rc!=SQLITE_OK ){ 3226 pRtree->nRowEst = RTREE_DEFAULT_ROWEST; 3227 return rc==SQLITE_ERROR ? SQLITE_OK : rc; 3228 } 3229 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName); 3230 if( zSql==0 ){ 3231 rc = SQLITE_NOMEM; 3232 }else{ 3233 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0); 3234 if( rc==SQLITE_OK ){ 3235 if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0); 3236 rc = sqlite3_finalize(p); 3237 }else if( rc!=SQLITE_NOMEM ){ 3238 rc = SQLITE_OK; 3239 } 3240 3241 if( rc==SQLITE_OK ){ 3242 if( nRow==0 ){ 3243 pRtree->nRowEst = RTREE_DEFAULT_ROWEST; 3244 }else{ 3245 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST); 3246 } 3247 } 3248 sqlite3_free(zSql); 3249 } 3250 3251 return rc; 3252 } 3253 3254 static sqlite3_module rtreeModule = { 3255 2, /* iVersion */ 3256 rtreeCreate, /* xCreate - create a table */ 3257 rtreeConnect, /* xConnect - connect to an existing table */ 3258 rtreeBestIndex, /* xBestIndex - Determine search strategy */ 3259 rtreeDisconnect, /* xDisconnect - Disconnect from a table */ 3260 rtreeDestroy, /* xDestroy - Drop a table */ 3261 rtreeOpen, /* xOpen - open a cursor */ 3262 rtreeClose, /* xClose - close a cursor */ 3263 rtreeFilter, /* xFilter - configure scan constraints */ 3264 rtreeNext, /* xNext - advance a cursor */ 3265 rtreeEof, /* xEof */ 3266 rtreeColumn, /* xColumn - read data */ 3267 rtreeRowid, /* xRowid - read data */ 3268 rtreeUpdate, /* xUpdate - write data */ 3269 rtreeBeginTransaction, /* xBegin - begin transaction */ 3270 rtreeEndTransaction, /* xSync - sync transaction */ 3271 rtreeEndTransaction, /* xCommit - commit transaction */ 3272 rtreeEndTransaction, /* xRollback - rollback transaction */ 3273 0, /* xFindFunction - function overloading */ 3274 rtreeRename, /* xRename - rename the table */ 3275 rtreeSavepoint, /* xSavepoint */ 3276 0, /* xRelease */ 3277 0, /* xRollbackTo */ 3278 }; 3279 3280 static int rtreeSqlInit( 3281 Rtree *pRtree, 3282 sqlite3 *db, 3283 const char *zDb, 3284 const char *zPrefix, 3285 int isCreate 3286 ){ 3287 int rc = SQLITE_OK; 3288 3289 #define N_STATEMENT 8 3290 static const char *azSql[N_STATEMENT] = { 3291 /* Write the xxx_node table */ 3292 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(:1, :2)", 3293 "DELETE FROM '%q'.'%q_node' WHERE nodeno = :1", 3294 3295 /* Read and write the xxx_rowid table */ 3296 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = :1", 3297 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(:1, :2)", 3298 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = :1", 3299 3300 /* Read and write the xxx_parent table */ 3301 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = :1", 3302 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(:1, :2)", 3303 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = :1" 3304 }; 3305 sqlite3_stmt **appStmt[N_STATEMENT]; 3306 int i; 3307 3308 pRtree->db = db; 3309 3310 if( isCreate ){ 3311 char *zCreate = sqlite3_mprintf( 3312 "CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY, data BLOB);" 3313 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY, nodeno INTEGER);" 3314 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY," 3315 " parentnode INTEGER);" 3316 "INSERT INTO '%q'.'%q_node' VALUES(1, zeroblob(%d))", 3317 zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, zDb, zPrefix, pRtree->iNodeSize 3318 ); 3319 if( !zCreate ){ 3320 return SQLITE_NOMEM; 3321 } 3322 rc = sqlite3_exec(db, zCreate, 0, 0, 0); 3323 sqlite3_free(zCreate); 3324 if( rc!=SQLITE_OK ){ 3325 return rc; 3326 } 3327 } 3328 3329 appStmt[0] = &pRtree->pWriteNode; 3330 appStmt[1] = &pRtree->pDeleteNode; 3331 appStmt[2] = &pRtree->pReadRowid; 3332 appStmt[3] = &pRtree->pWriteRowid; 3333 appStmt[4] = &pRtree->pDeleteRowid; 3334 appStmt[5] = &pRtree->pReadParent; 3335 appStmt[6] = &pRtree->pWriteParent; 3336 appStmt[7] = &pRtree->pDeleteParent; 3337 3338 rc = rtreeQueryStat1(db, pRtree); 3339 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){ 3340 char *zSql = sqlite3_mprintf(azSql[i], zDb, zPrefix); 3341 if( zSql ){ 3342 rc = sqlite3_prepare_v3(db, zSql, -1, SQLITE_PREPARE_PERSISTENT, 3343 appStmt[i], 0); 3344 }else{ 3345 rc = SQLITE_NOMEM; 3346 } 3347 sqlite3_free(zSql); 3348 } 3349 3350 return rc; 3351 } 3352 3353 /* 3354 ** The second argument to this function contains the text of an SQL statement 3355 ** that returns a single integer value. The statement is compiled and executed 3356 ** using database connection db. If successful, the integer value returned 3357 ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error 3358 ** code is returned and the value of *piVal after returning is not defined. 3359 */ 3360 static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){ 3361 int rc = SQLITE_NOMEM; 3362 if( zSql ){ 3363 sqlite3_stmt *pStmt = 0; 3364 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0); 3365 if( rc==SQLITE_OK ){ 3366 if( SQLITE_ROW==sqlite3_step(pStmt) ){ 3367 *piVal = sqlite3_column_int(pStmt, 0); 3368 } 3369 rc = sqlite3_finalize(pStmt); 3370 } 3371 } 3372 return rc; 3373 } 3374 3375 /* 3376 ** This function is called from within the xConnect() or xCreate() method to 3377 ** determine the node-size used by the rtree table being created or connected 3378 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned. 3379 ** Otherwise, an SQLite error code is returned. 3380 ** 3381 ** If this function is being called as part of an xConnect(), then the rtree 3382 ** table already exists. In this case the node-size is determined by inspecting 3383 ** the root node of the tree. 3384 ** 3385 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size. 3386 ** This ensures that each node is stored on a single database page. If the 3387 ** database page-size is so large that more than RTREE_MAXCELLS entries 3388 ** would fit in a single node, use a smaller node-size. 3389 */ 3390 static int getNodeSize( 3391 sqlite3 *db, /* Database handle */ 3392 Rtree *pRtree, /* Rtree handle */ 3393 int isCreate, /* True for xCreate, false for xConnect */ 3394 char **pzErr /* OUT: Error message, if any */ 3395 ){ 3396 int rc; 3397 char *zSql; 3398 if( isCreate ){ 3399 int iPageSize = 0; 3400 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb); 3401 rc = getIntFromStmt(db, zSql, &iPageSize); 3402 if( rc==SQLITE_OK ){ 3403 pRtree->iNodeSize = iPageSize-64; 3404 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){ 3405 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS; 3406 } 3407 }else{ 3408 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); 3409 } 3410 }else{ 3411 zSql = sqlite3_mprintf( 3412 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1", 3413 pRtree->zDb, pRtree->zName 3414 ); 3415 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize); 3416 if( rc!=SQLITE_OK ){ 3417 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); 3418 }else if( pRtree->iNodeSize<(512-64) ){ 3419 rc = SQLITE_CORRUPT_VTAB; 3420 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"", 3421 pRtree->zName); 3422 } 3423 } 3424 3425 sqlite3_free(zSql); 3426 return rc; 3427 } 3428 3429 /* 3430 ** This function is the implementation of both the xConnect and xCreate 3431 ** methods of the r-tree virtual table. 3432 ** 3433 ** argv[0] -> module name 3434 ** argv[1] -> database name 3435 ** argv[2] -> table name 3436 ** argv[...] -> column names... 3437 */ 3438 static int rtreeInit( 3439 sqlite3 *db, /* Database connection */ 3440 void *pAux, /* One of the RTREE_COORD_* constants */ 3441 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */ 3442 sqlite3_vtab **ppVtab, /* OUT: New virtual table */ 3443 char **pzErr, /* OUT: Error message, if any */ 3444 int isCreate /* True for xCreate, false for xConnect */ 3445 ){ 3446 int rc = SQLITE_OK; 3447 Rtree *pRtree; 3448 int nDb; /* Length of string argv[1] */ 3449 int nName; /* Length of string argv[2] */ 3450 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32); 3451 3452 const char *aErrMsg[] = { 3453 0, /* 0 */ 3454 "Wrong number of columns for an rtree table", /* 1 */ 3455 "Too few columns for an rtree table", /* 2 */ 3456 "Too many columns for an rtree table" /* 3 */ 3457 }; 3458 3459 int iErr = (argc<6) ? 2 : argc>(RTREE_MAX_DIMENSIONS*2+4) ? 3 : argc%2; 3460 if( aErrMsg[iErr] ){ 3461 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]); 3462 return SQLITE_ERROR; 3463 } 3464 3465 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1); 3466 3467 /* Allocate the sqlite3_vtab structure */ 3468 nDb = (int)strlen(argv[1]); 3469 nName = (int)strlen(argv[2]); 3470 pRtree = (Rtree *)sqlite3_malloc(sizeof(Rtree)+nDb+nName+2); 3471 if( !pRtree ){ 3472 return SQLITE_NOMEM; 3473 } 3474 memset(pRtree, 0, sizeof(Rtree)+nDb+nName+2); 3475 pRtree->nBusy = 1; 3476 pRtree->base.pModule = &rtreeModule; 3477 pRtree->zDb = (char *)&pRtree[1]; 3478 pRtree->zName = &pRtree->zDb[nDb+1]; 3479 pRtree->nDim = (u8)((argc-4)/2); 3480 pRtree->nDim2 = pRtree->nDim*2; 3481 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4; 3482 pRtree->eCoordType = (u8)eCoordType; 3483 memcpy(pRtree->zDb, argv[1], nDb); 3484 memcpy(pRtree->zName, argv[2], nName); 3485 3486 /* Figure out the node size to use. */ 3487 rc = getNodeSize(db, pRtree, isCreate, pzErr); 3488 3489 /* Create/Connect to the underlying relational database schema. If 3490 ** that is successful, call sqlite3_declare_vtab() to configure 3491 ** the r-tree table schema. 3492 */ 3493 if( rc==SQLITE_OK ){ 3494 if( (rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate)) ){ 3495 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); 3496 }else{ 3497 char *zSql = sqlite3_mprintf("CREATE TABLE x(%s", argv[3]); 3498 char *zTmp; 3499 int ii; 3500 for(ii=4; zSql && ii<argc; ii++){ 3501 zTmp = zSql; 3502 zSql = sqlite3_mprintf("%s, %s", zTmp, argv[ii]); 3503 sqlite3_free(zTmp); 3504 } 3505 if( zSql ){ 3506 zTmp = zSql; 3507 zSql = sqlite3_mprintf("%s);", zTmp); 3508 sqlite3_free(zTmp); 3509 } 3510 if( !zSql ){ 3511 rc = SQLITE_NOMEM; 3512 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){ 3513 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db)); 3514 } 3515 sqlite3_free(zSql); 3516 } 3517 } 3518 3519 if( rc==SQLITE_OK ){ 3520 *ppVtab = (sqlite3_vtab *)pRtree; 3521 }else{ 3522 assert( *ppVtab==0 ); 3523 assert( pRtree->nBusy==1 ); 3524 rtreeRelease(pRtree); 3525 } 3526 return rc; 3527 } 3528 3529 3530 /* 3531 ** Implementation of a scalar function that decodes r-tree nodes to 3532 ** human readable strings. This can be used for debugging and analysis. 3533 ** 3534 ** The scalar function takes two arguments: (1) the number of dimensions 3535 ** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing 3536 ** an r-tree node. For a two-dimensional r-tree structure called "rt", to 3537 ** deserialize all nodes, a statement like: 3538 ** 3539 ** SELECT rtreenode(2, data) FROM rt_node; 3540 ** 3541 ** The human readable string takes the form of a Tcl list with one 3542 ** entry for each cell in the r-tree node. Each entry is itself a 3543 ** list, containing the 8-byte rowid/pageno followed by the 3544 ** <num-dimension>*2 coordinates. 3545 */ 3546 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){ 3547 char *zText = 0; 3548 RtreeNode node; 3549 Rtree tree; 3550 int ii; 3551 3552 UNUSED_PARAMETER(nArg); 3553 memset(&node, 0, sizeof(RtreeNode)); 3554 memset(&tree, 0, sizeof(Rtree)); 3555 tree.nDim = (u8)sqlite3_value_int(apArg[0]); 3556 tree.nDim2 = tree.nDim*2; 3557 tree.nBytesPerCell = 8 + 8 * tree.nDim; 3558 node.zData = (u8 *)sqlite3_value_blob(apArg[1]); 3559 3560 for(ii=0; ii<NCELL(&node); ii++){ 3561 char zCell[512]; 3562 int nCell = 0; 3563 RtreeCell cell; 3564 int jj; 3565 3566 nodeGetCell(&tree, &node, ii, &cell); 3567 sqlite3_snprintf(512-nCell,&zCell[nCell],"%lld", cell.iRowid); 3568 nCell = (int)strlen(zCell); 3569 for(jj=0; jj<tree.nDim2; jj++){ 3570 #ifndef SQLITE_RTREE_INT_ONLY 3571 sqlite3_snprintf(512-nCell,&zCell[nCell], " %g", 3572 (double)cell.aCoord[jj].f); 3573 #else 3574 sqlite3_snprintf(512-nCell,&zCell[nCell], " %d", 3575 cell.aCoord[jj].i); 3576 #endif 3577 nCell = (int)strlen(zCell); 3578 } 3579 3580 if( zText ){ 3581 char *zTextNew = sqlite3_mprintf("%s {%s}", zText, zCell); 3582 sqlite3_free(zText); 3583 zText = zTextNew; 3584 }else{ 3585 zText = sqlite3_mprintf("{%s}", zCell); 3586 } 3587 } 3588 3589 sqlite3_result_text(ctx, zText, -1, sqlite3_free); 3590 } 3591 3592 /* This routine implements an SQL function that returns the "depth" parameter 3593 ** from the front of a blob that is an r-tree node. For example: 3594 ** 3595 ** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1; 3596 ** 3597 ** The depth value is 0 for all nodes other than the root node, and the root 3598 ** node always has nodeno=1, so the example above is the primary use for this 3599 ** routine. This routine is intended for testing and analysis only. 3600 */ 3601 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){ 3602 UNUSED_PARAMETER(nArg); 3603 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB 3604 || sqlite3_value_bytes(apArg[0])<2 3605 ){ 3606 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1); 3607 }else{ 3608 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]); 3609 sqlite3_result_int(ctx, readInt16(zBlob)); 3610 } 3611 } 3612 3613 /* 3614 ** Context object passed between the various routines that make up the 3615 ** implementation of integrity-check function rtreecheck(). 3616 */ 3617 typedef struct RtreeCheck RtreeCheck; 3618 struct RtreeCheck { 3619 sqlite3 *db; /* Database handle */ 3620 const char *zDb; /* Database containing rtree table */ 3621 const char *zTab; /* Name of rtree table */ 3622 int bInt; /* True for rtree_i32 table */ 3623 int nDim; /* Number of dimensions for this rtree tbl */ 3624 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */ 3625 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */ 3626 int nLeaf; /* Number of leaf cells in table */ 3627 int nNonLeaf; /* Number of non-leaf cells in table */ 3628 int rc; /* Return code */ 3629 char *zReport; /* Message to report */ 3630 int nErr; /* Number of lines in zReport */ 3631 }; 3632 3633 #define RTREE_CHECK_MAX_ERROR 100 3634 3635 /* 3636 ** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error, 3637 ** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code. 3638 */ 3639 static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){ 3640 int rc = sqlite3_reset(pStmt); 3641 if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc; 3642 } 3643 3644 /* 3645 ** The second and subsequent arguments to this function are a format string 3646 ** and printf style arguments. This function formats the string and attempts 3647 ** to compile it as an SQL statement. 3648 ** 3649 ** If successful, a pointer to the new SQL statement is returned. Otherwise, 3650 ** NULL is returned and an error code left in RtreeCheck.rc. 3651 */ 3652 static sqlite3_stmt *rtreeCheckPrepare( 3653 RtreeCheck *pCheck, /* RtreeCheck object */ 3654 const char *zFmt, ... /* Format string and trailing args */ 3655 ){ 3656 va_list ap; 3657 char *z; 3658 sqlite3_stmt *pRet = 0; 3659 3660 va_start(ap, zFmt); 3661 z = sqlite3_vmprintf(zFmt, ap); 3662 3663 if( pCheck->rc==SQLITE_OK ){ 3664 if( z==0 ){ 3665 pCheck->rc = SQLITE_NOMEM; 3666 }else{ 3667 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0); 3668 } 3669 } 3670 3671 sqlite3_free(z); 3672 va_end(ap); 3673 return pRet; 3674 } 3675 3676 /* 3677 ** The second and subsequent arguments to this function are a printf() 3678 ** style format string and arguments. This function formats the string and 3679 ** appends it to the report being accumuated in pCheck. 3680 */ 3681 static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){ 3682 va_list ap; 3683 va_start(ap, zFmt); 3684 if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){ 3685 char *z = sqlite3_vmprintf(zFmt, ap); 3686 if( z==0 ){ 3687 pCheck->rc = SQLITE_NOMEM; 3688 }else{ 3689 pCheck->zReport = sqlite3_mprintf("%z%s%z", 3690 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z 3691 ); 3692 if( pCheck->zReport==0 ){ 3693 pCheck->rc = SQLITE_NOMEM; 3694 } 3695 } 3696 pCheck->nErr++; 3697 } 3698 va_end(ap); 3699 } 3700 3701 /* 3702 ** This function is a no-op if there is already an error code stored 3703 ** in the RtreeCheck object indicated by the first argument. NULL is 3704 ** returned in this case. 3705 ** 3706 ** Otherwise, the contents of rtree table node iNode are loaded from 3707 ** the database and copied into a buffer obtained from sqlite3_malloc(). 3708 ** If no error occurs, a pointer to the buffer is returned and (*pnNode) 3709 ** is set to the size of the buffer in bytes. 3710 ** 3711 ** Or, if an error does occur, NULL is returned and an error code left 3712 ** in the RtreeCheck object. The final value of *pnNode is undefined in 3713 ** this case. 3714 */ 3715 static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){ 3716 u8 *pRet = 0; /* Return value */ 3717 3718 assert( pCheck->rc==SQLITE_OK ); 3719 if( pCheck->pGetNode==0 ){ 3720 pCheck->pGetNode = rtreeCheckPrepare(pCheck, 3721 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?", 3722 pCheck->zDb, pCheck->zTab 3723 ); 3724 } 3725 3726 if( pCheck->rc==SQLITE_OK ){ 3727 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode); 3728 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){ 3729 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0); 3730 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0); 3731 pRet = sqlite3_malloc(nNode); 3732 if( pRet==0 ){ 3733 pCheck->rc = SQLITE_NOMEM; 3734 }else{ 3735 memcpy(pRet, pNode, nNode); 3736 *pnNode = nNode; 3737 } 3738 } 3739 rtreeCheckReset(pCheck, pCheck->pGetNode); 3740 if( pCheck->rc==SQLITE_OK && pRet==0 ){ 3741 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode); 3742 } 3743 } 3744 3745 return pRet; 3746 } 3747 3748 /* 3749 ** This function is used to check that the %_parent (if bLeaf==0) or %_rowid 3750 ** (if bLeaf==1) table contains a specified entry. The schemas of the 3751 ** two tables are: 3752 ** 3753 ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER) 3754 ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER) 3755 ** 3756 ** In both cases, this function checks that there exists an entry with 3757 ** IPK value iKey and the second column set to iVal. 3758 ** 3759 */ 3760 static void rtreeCheckMapping( 3761 RtreeCheck *pCheck, /* RtreeCheck object */ 3762 int bLeaf, /* True for a leaf cell, false for interior */ 3763 i64 iKey, /* Key for mapping */ 3764 i64 iVal /* Expected value for mapping */ 3765 ){ 3766 int rc; 3767 sqlite3_stmt *pStmt; 3768 const char *azSql[2] = { 3769 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?", 3770 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?" 3771 }; 3772 3773 assert( bLeaf==0 || bLeaf==1 ); 3774 if( pCheck->aCheckMapping[bLeaf]==0 ){ 3775 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck, 3776 azSql[bLeaf], pCheck->zDb, pCheck->zTab 3777 ); 3778 } 3779 if( pCheck->rc!=SQLITE_OK ) return; 3780 3781 pStmt = pCheck->aCheckMapping[bLeaf]; 3782 sqlite3_bind_int64(pStmt, 1, iKey); 3783 rc = sqlite3_step(pStmt); 3784 if( rc==SQLITE_DONE ){ 3785 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table", 3786 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent") 3787 ); 3788 }else if( rc==SQLITE_ROW ){ 3789 i64 ii = sqlite3_column_int64(pStmt, 0); 3790 if( ii!=iVal ){ 3791 rtreeCheckAppendMsg(pCheck, 3792 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)", 3793 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal 3794 ); 3795 } 3796 } 3797 rtreeCheckReset(pCheck, pStmt); 3798 } 3799 3800 /* 3801 ** Argument pCell points to an array of coordinates stored on an rtree page. 3802 ** This function checks that the coordinates are internally consistent (no 3803 ** x1>x2 conditions) and adds an error message to the RtreeCheck object 3804 ** if they are not. 3805 ** 3806 ** Additionally, if pParent is not NULL, then it is assumed to point to 3807 ** the array of coordinates on the parent page that bound the page 3808 ** containing pCell. In this case it is also verified that the two 3809 ** sets of coordinates are mutually consistent and an error message added 3810 ** to the RtreeCheck object if they are not. 3811 */ 3812 static void rtreeCheckCellCoord( 3813 RtreeCheck *pCheck, 3814 i64 iNode, /* Node id to use in error messages */ 3815 int iCell, /* Cell number to use in error messages */ 3816 u8 *pCell, /* Pointer to cell coordinates */ 3817 u8 *pParent /* Pointer to parent coordinates */ 3818 ){ 3819 RtreeCoord c1, c2; 3820 RtreeCoord p1, p2; 3821 int i; 3822 3823 for(i=0; i<pCheck->nDim; i++){ 3824 readCoord(&pCell[4*2*i], &c1); 3825 readCoord(&pCell[4*(2*i + 1)], &c2); 3826 3827 /* printf("%e, %e\n", c1.u.f, c2.u.f); */ 3828 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){ 3829 rtreeCheckAppendMsg(pCheck, 3830 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode 3831 ); 3832 } 3833 3834 if( pParent ){ 3835 readCoord(&pParent[4*2*i], &p1); 3836 readCoord(&pParent[4*(2*i + 1)], &p2); 3837 3838 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f) 3839 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f) 3840 ){ 3841 rtreeCheckAppendMsg(pCheck, 3842 "Dimension %d of cell %d on node %lld is corrupt relative to parent" 3843 , i, iCell, iNode 3844 ); 3845 } 3846 } 3847 } 3848 } 3849 3850 /* 3851 ** Run rtreecheck() checks on node iNode, which is at depth iDepth within 3852 ** the r-tree structure. Argument aParent points to the array of coordinates 3853 ** that bound node iNode on the parent node. 3854 ** 3855 ** If any problems are discovered, an error message is appended to the 3856 ** report accumulated in the RtreeCheck object. 3857 */ 3858 static void rtreeCheckNode( 3859 RtreeCheck *pCheck, 3860 int iDepth, /* Depth of iNode (0==leaf) */ 3861 u8 *aParent, /* Buffer containing parent coords */ 3862 i64 iNode /* Node to check */ 3863 ){ 3864 u8 *aNode = 0; 3865 int nNode = 0; 3866 3867 assert( iNode==1 || aParent!=0 ); 3868 assert( pCheck->nDim>0 ); 3869 3870 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode); 3871 if( aNode ){ 3872 if( nNode<4 ){ 3873 rtreeCheckAppendMsg(pCheck, 3874 "Node %lld is too small (%d bytes)", iNode, nNode 3875 ); 3876 }else{ 3877 int nCell; /* Number of cells on page */ 3878 int i; /* Used to iterate through cells */ 3879 if( aParent==0 ){ 3880 iDepth = readInt16(aNode); 3881 if( iDepth>RTREE_MAX_DEPTH ){ 3882 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth); 3883 sqlite3_free(aNode); 3884 return; 3885 } 3886 } 3887 nCell = readInt16(&aNode[2]); 3888 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){ 3889 rtreeCheckAppendMsg(pCheck, 3890 "Node %lld is too small for cell count of %d (%d bytes)", 3891 iNode, nCell, nNode 3892 ); 3893 }else{ 3894 for(i=0; i<nCell; i++){ 3895 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)]; 3896 i64 iVal = readInt64(pCell); 3897 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent); 3898 3899 if( iDepth>0 ){ 3900 rtreeCheckMapping(pCheck, 0, iVal, iNode); 3901 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal); 3902 pCheck->nNonLeaf++; 3903 }else{ 3904 rtreeCheckMapping(pCheck, 1, iVal, iNode); 3905 pCheck->nLeaf++; 3906 } 3907 } 3908 } 3909 } 3910 sqlite3_free(aNode); 3911 } 3912 } 3913 3914 /* 3915 ** The second argument to this function must be either "_rowid" or 3916 ** "_parent". This function checks that the number of entries in the 3917 ** %_rowid or %_parent table is exactly nExpect. If not, it adds 3918 ** an error message to the report in the RtreeCheck object indicated 3919 ** by the first argument. 3920 */ 3921 static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){ 3922 if( pCheck->rc==SQLITE_OK ){ 3923 sqlite3_stmt *pCount; 3924 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'", 3925 pCheck->zDb, pCheck->zTab, zTbl 3926 ); 3927 if( pCount ){ 3928 if( sqlite3_step(pCount)==SQLITE_ROW ){ 3929 i64 nActual = sqlite3_column_int64(pCount, 0); 3930 if( nActual!=nExpect ){ 3931 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table" 3932 " - expected %lld, actual %lld" , zTbl, nExpect, nActual 3933 ); 3934 } 3935 } 3936 pCheck->rc = sqlite3_finalize(pCount); 3937 } 3938 } 3939 } 3940 3941 /* 3942 ** This function does the bulk of the work for the rtree integrity-check. 3943 ** It is called by rtreecheck(), which is the SQL function implementation. 3944 */ 3945 static int rtreeCheckTable( 3946 sqlite3 *db, /* Database handle to access db through */ 3947 const char *zDb, /* Name of db ("main", "temp" etc.) */ 3948 const char *zTab, /* Name of rtree table to check */ 3949 char **pzReport /* OUT: sqlite3_malloc'd report text */ 3950 ){ 3951 RtreeCheck check; /* Common context for various routines */ 3952 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */ 3953 int bEnd = 0; /* True if transaction should be closed */ 3954 3955 /* Initialize the context object */ 3956 memset(&check, 0, sizeof(check)); 3957 check.db = db; 3958 check.zDb = zDb; 3959 check.zTab = zTab; 3960 3961 /* If there is not already an open transaction, open one now. This is 3962 ** to ensure that the queries run as part of this integrity-check operate 3963 ** on a consistent snapshot. */ 3964 if( sqlite3_get_autocommit(db) ){ 3965 check.rc = sqlite3_exec(db, "BEGIN", 0, 0, 0); 3966 bEnd = 1; 3967 } 3968 3969 /* Find number of dimensions in the rtree table. */ 3970 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab); 3971 if( pStmt ){ 3972 int rc; 3973 check.nDim = (sqlite3_column_count(pStmt) - 1) / 2; 3974 if( check.nDim<1 ){ 3975 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree"); 3976 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){ 3977 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER); 3978 } 3979 rc = sqlite3_finalize(pStmt); 3980 if( rc!=SQLITE_CORRUPT ) check.rc = rc; 3981 } 3982 3983 /* Do the actual integrity-check */ 3984 if( check.nDim>=1 ){ 3985 if( check.rc==SQLITE_OK ){ 3986 rtreeCheckNode(&check, 0, 0, 1); 3987 } 3988 rtreeCheckCount(&check, "_rowid", check.nLeaf); 3989 rtreeCheckCount(&check, "_parent", check.nNonLeaf); 3990 } 3991 3992 /* Finalize SQL statements used by the integrity-check */ 3993 sqlite3_finalize(check.pGetNode); 3994 sqlite3_finalize(check.aCheckMapping[0]); 3995 sqlite3_finalize(check.aCheckMapping[1]); 3996 3997 /* If one was opened, close the transaction */ 3998 if( bEnd ){ 3999 int rc = sqlite3_exec(db, "END", 0, 0, 0); 4000 if( check.rc==SQLITE_OK ) check.rc = rc; 4001 } 4002 *pzReport = check.zReport; 4003 return check.rc; 4004 } 4005 4006 /* 4007 ** Usage: 4008 ** 4009 ** rtreecheck(<rtree-table>); 4010 ** rtreecheck(<database>, <rtree-table>); 4011 ** 4012 ** Invoking this SQL function runs an integrity-check on the named rtree 4013 ** table. The integrity-check verifies the following: 4014 ** 4015 ** 1. For each cell in the r-tree structure (%_node table), that: 4016 ** 4017 ** a) for each dimension, (coord1 <= coord2). 4018 ** 4019 ** b) unless the cell is on the root node, that the cell is bounded 4020 ** by the parent cell on the parent node. 4021 ** 4022 ** c) for leaf nodes, that there is an entry in the %_rowid 4023 ** table corresponding to the cell's rowid value that 4024 ** points to the correct node. 4025 ** 4026 ** d) for cells on non-leaf nodes, that there is an entry in the 4027 ** %_parent table mapping from the cell's child node to the 4028 ** node that it resides on. 4029 ** 4030 ** 2. That there are the same number of entries in the %_rowid table 4031 ** as there are leaf cells in the r-tree structure, and that there 4032 ** is a leaf cell that corresponds to each entry in the %_rowid table. 4033 ** 4034 ** 3. That there are the same number of entries in the %_parent table 4035 ** as there are non-leaf cells in the r-tree structure, and that 4036 ** there is a non-leaf cell that corresponds to each entry in the 4037 ** %_parent table. 4038 */ 4039 static void rtreecheck( 4040 sqlite3_context *ctx, 4041 int nArg, 4042 sqlite3_value **apArg 4043 ){ 4044 if( nArg!=1 && nArg!=2 ){ 4045 sqlite3_result_error(ctx, 4046 "wrong number of arguments to function rtreecheck()", -1 4047 ); 4048 }else{ 4049 int rc; 4050 char *zReport = 0; 4051 const char *zDb = (const char*)sqlite3_value_text(apArg[0]); 4052 const char *zTab; 4053 if( nArg==1 ){ 4054 zTab = zDb; 4055 zDb = "main"; 4056 }else{ 4057 zTab = (const char*)sqlite3_value_text(apArg[1]); 4058 } 4059 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport); 4060 if( rc==SQLITE_OK ){ 4061 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT); 4062 }else{ 4063 sqlite3_result_error_code(ctx, rc); 4064 } 4065 sqlite3_free(zReport); 4066 } 4067 } 4068 4069 4070 /* 4071 ** Register the r-tree module with database handle db. This creates the 4072 ** virtual table module "rtree" and the debugging/analysis scalar 4073 ** function "rtreenode". 4074 */ 4075 int sqlite3RtreeInit(sqlite3 *db){ 4076 const int utf8 = SQLITE_UTF8; 4077 int rc; 4078 4079 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0); 4080 if( rc==SQLITE_OK ){ 4081 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0); 4082 } 4083 if( rc==SQLITE_OK ){ 4084 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0); 4085 } 4086 if( rc==SQLITE_OK ){ 4087 #ifdef SQLITE_RTREE_INT_ONLY 4088 void *c = (void *)RTREE_COORD_INT32; 4089 #else 4090 void *c = (void *)RTREE_COORD_REAL32; 4091 #endif 4092 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0); 4093 } 4094 if( rc==SQLITE_OK ){ 4095 void *c = (void *)RTREE_COORD_INT32; 4096 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0); 4097 } 4098 4099 return rc; 4100 } 4101 4102 /* 4103 ** This routine deletes the RtreeGeomCallback object that was attached 4104 ** one of the SQL functions create by sqlite3_rtree_geometry_callback() 4105 ** or sqlite3_rtree_query_callback(). In other words, this routine is the 4106 ** destructor for an RtreeGeomCallback objecct. This routine is called when 4107 ** the corresponding SQL function is deleted. 4108 */ 4109 static void rtreeFreeCallback(void *p){ 4110 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p; 4111 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext); 4112 sqlite3_free(p); 4113 } 4114 4115 /* 4116 ** This routine frees the BLOB that is returned by geomCallback(). 4117 */ 4118 static void rtreeMatchArgFree(void *pArg){ 4119 int i; 4120 RtreeMatchArg *p = (RtreeMatchArg*)pArg; 4121 for(i=0; i<p->nParam; i++){ 4122 sqlite3_value_free(p->apSqlParam[i]); 4123 } 4124 sqlite3_free(p); 4125 } 4126 4127 /* 4128 ** Each call to sqlite3_rtree_geometry_callback() or 4129 ** sqlite3_rtree_query_callback() creates an ordinary SQLite 4130 ** scalar function that is implemented by this routine. 4131 ** 4132 ** All this function does is construct an RtreeMatchArg object that 4133 ** contains the geometry-checking callback routines and a list of 4134 ** parameters to this function, then return that RtreeMatchArg object 4135 ** as a BLOB. 4136 ** 4137 ** The R-Tree MATCH operator will read the returned BLOB, deserialize 4138 ** the RtreeMatchArg object, and use the RtreeMatchArg object to figure 4139 ** out which elements of the R-Tree should be returned by the query. 4140 */ 4141 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){ 4142 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx); 4143 RtreeMatchArg *pBlob; 4144 int nBlob; 4145 int memErr = 0; 4146 4147 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue) 4148 + nArg*sizeof(sqlite3_value*); 4149 pBlob = (RtreeMatchArg *)sqlite3_malloc(nBlob); 4150 if( !pBlob ){ 4151 sqlite3_result_error_nomem(ctx); 4152 }else{ 4153 int i; 4154 pBlob->iSize = nBlob; 4155 pBlob->cb = pGeomCtx[0]; 4156 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg]; 4157 pBlob->nParam = nArg; 4158 for(i=0; i<nArg; i++){ 4159 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]); 4160 if( pBlob->apSqlParam[i]==0 ) memErr = 1; 4161 #ifdef SQLITE_RTREE_INT_ONLY 4162 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]); 4163 #else 4164 pBlob->aParam[i] = sqlite3_value_double(aArg[i]); 4165 #endif 4166 } 4167 if( memErr ){ 4168 sqlite3_result_error_nomem(ctx); 4169 rtreeMatchArgFree(pBlob); 4170 }else{ 4171 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree); 4172 } 4173 } 4174 } 4175 4176 /* 4177 ** Register a new geometry function for use with the r-tree MATCH operator. 4178 */ 4179 int sqlite3_rtree_geometry_callback( 4180 sqlite3 *db, /* Register SQL function on this connection */ 4181 const char *zGeom, /* Name of the new SQL function */ 4182 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */ 4183 void *pContext /* Extra data associated with the callback */ 4184 ){ 4185 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */ 4186 4187 /* Allocate and populate the context object. */ 4188 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback)); 4189 if( !pGeomCtx ) return SQLITE_NOMEM; 4190 pGeomCtx->xGeom = xGeom; 4191 pGeomCtx->xQueryFunc = 0; 4192 pGeomCtx->xDestructor = 0; 4193 pGeomCtx->pContext = pContext; 4194 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY, 4195 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback 4196 ); 4197 } 4198 4199 /* 4200 ** Register a new 2nd-generation geometry function for use with the 4201 ** r-tree MATCH operator. 4202 */ 4203 int sqlite3_rtree_query_callback( 4204 sqlite3 *db, /* Register SQL function on this connection */ 4205 const char *zQueryFunc, /* Name of new SQL function */ 4206 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */ 4207 void *pContext, /* Extra data passed into the callback */ 4208 void (*xDestructor)(void*) /* Destructor for the extra data */ 4209 ){ 4210 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */ 4211 4212 /* Allocate and populate the context object. */ 4213 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback)); 4214 if( !pGeomCtx ) return SQLITE_NOMEM; 4215 pGeomCtx->xGeom = 0; 4216 pGeomCtx->xQueryFunc = xQueryFunc; 4217 pGeomCtx->xDestructor = xDestructor; 4218 pGeomCtx->pContext = pContext; 4219 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY, 4220 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback 4221 ); 4222 } 4223 4224 #if !SQLITE_CORE 4225 #ifdef _WIN32 4226 __declspec(dllexport) 4227 #endif 4228 int sqlite3_rtree_init( 4229 sqlite3 *db, 4230 char **pzErrMsg, 4231 const sqlite3_api_routines *pApi 4232 ){ 4233 SQLITE_EXTENSION_INIT2(pApi) 4234 return sqlite3RtreeInit(db); 4235 } 4236 #endif 4237 4238 #endif 4239