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