Update fts5 comment/docs to include the order matches are visited in by the xPhraseFi...
[sqlite.git] / ext / rtree / rtree.c
blob299b5b54b906701761b878d65a47793c212cbf1b
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.
11 *************************************************************************
12 ** This file contains code for implementations of the r-tree and r*-tree
13 ** algorithms packaged as an SQLite virtual table module.
17 ** Database Format of R-Tree Tables
18 ** --------------------------------
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.
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, ...)
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. If the r-tree contains auxiliary columns, those are stored
35 ** on the end of the %_rowid table.
37 ** The root node of an r-tree always exists, even if the r-tree table is
38 ** empty. The nodeno of the root node is always 1. All other nodes in the
39 ** table must be the same size as the root node. The content of each node
40 ** is formatted as follows:
42 ** 1. If the node is the root node (node 1), then the first 2 bytes
43 ** of the node contain the tree depth as a big-endian integer.
44 ** For non-root nodes, the first 2 bytes are left unused.
46 ** 2. The next 2 bytes contain the number of entries currently
47 ** stored in the node.
49 ** 3. The remainder of the node contains the node entries. Each entry
50 ** consists of a single 8-byte integer followed by an even number
51 ** of 4-byte coordinates. For leaf nodes the integer is the rowid
52 ** of a record. For internal nodes it is the node number of a
53 ** child page.
56 #if !defined(SQLITE_CORE) \
57 || (defined(SQLITE_ENABLE_RTREE) && !defined(SQLITE_OMIT_VIRTUALTABLE))
59 #ifndef SQLITE_CORE
60 #include "sqlite3ext.h"
61 SQLITE_EXTENSION_INIT1
62 #else
63 #include "sqlite3.h"
64 #endif
65 int sqlite3GetToken(const unsigned char*,int*); /* In the SQLite core */
68 ** If building separately, we will need some setup that is normally
69 ** found in sqliteInt.h
71 #if !defined(SQLITE_AMALGAMATION)
72 #include "sqlite3rtree.h"
73 typedef sqlite3_int64 i64;
74 typedef sqlite3_uint64 u64;
75 typedef unsigned char u8;
76 typedef unsigned short u16;
77 typedef unsigned int u32;
78 #if !defined(NDEBUG) && !defined(SQLITE_DEBUG)
79 # define NDEBUG 1
80 #endif
81 #if defined(NDEBUG) && defined(SQLITE_DEBUG)
82 # undef NDEBUG
83 #endif
84 #if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_MUTATION_TEST)
85 # define SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS 1
86 #endif
87 #if defined(SQLITE_OMIT_AUXILIARY_SAFETY_CHECKS)
88 # define ALWAYS(X) (1)
89 # define NEVER(X) (0)
90 #elif !defined(NDEBUG)
91 # define ALWAYS(X) ((X)?1:(assert(0),0))
92 # define NEVER(X) ((X)?(assert(0),1):0)
93 #else
94 # define ALWAYS(X) (X)
95 # define NEVER(X) (X)
96 #endif
97 #endif /* !defined(SQLITE_AMALGAMATION) */
99 /* Macro to check for 4-byte alignment. Only used inside of assert() */
100 #ifdef SQLITE_DEBUG
101 # define FOUR_BYTE_ALIGNED(X) ((((char*)(X) - (char*)0) & 3)==0)
102 #endif
104 #include <string.h>
105 #include <stdio.h>
106 #include <assert.h>
107 #include <stdlib.h>
109 /* The following macro is used to suppress compiler warnings.
111 #ifndef UNUSED_PARAMETER
112 # define UNUSED_PARAMETER(x) (void)(x)
113 #endif
115 typedef struct Rtree Rtree;
116 typedef struct RtreeCursor RtreeCursor;
117 typedef struct RtreeNode RtreeNode;
118 typedef struct RtreeCell RtreeCell;
119 typedef struct RtreeConstraint RtreeConstraint;
120 typedef struct RtreeMatchArg RtreeMatchArg;
121 typedef struct RtreeGeomCallback RtreeGeomCallback;
122 typedef union RtreeCoord RtreeCoord;
123 typedef struct RtreeSearchPoint RtreeSearchPoint;
125 /* The rtree may have between 1 and RTREE_MAX_DIMENSIONS dimensions. */
126 #define RTREE_MAX_DIMENSIONS 5
128 /* Maximum number of auxiliary columns */
129 #define RTREE_MAX_AUX_COLUMN 100
131 /* Size of hash table Rtree.aHash. This hash table is not expected to
132 ** ever contain very many entries, so a fixed number of buckets is
133 ** used.
135 #define HASHSIZE 97
137 /* The xBestIndex method of this virtual table requires an estimate of
138 ** the number of rows in the virtual table to calculate the costs of
139 ** various strategies. If possible, this estimate is loaded from the
140 ** sqlite_stat1 table (with RTREE_MIN_ROWEST as a hard-coded minimum).
141 ** Otherwise, if no sqlite_stat1 entry is available, use
142 ** RTREE_DEFAULT_ROWEST.
144 #define RTREE_DEFAULT_ROWEST 1048576
145 #define RTREE_MIN_ROWEST 100
148 ** An rtree virtual-table object.
150 struct Rtree {
151 sqlite3_vtab base; /* Base class. Must be first */
152 sqlite3 *db; /* Host database connection */
153 int iNodeSize; /* Size in bytes of each node in the node table */
154 u8 nDim; /* Number of dimensions */
155 u8 nDim2; /* Twice the number of dimensions */
156 u8 eCoordType; /* RTREE_COORD_REAL32 or RTREE_COORD_INT32 */
157 u8 nBytesPerCell; /* Bytes consumed per cell */
158 u8 inWrTrans; /* True if inside write transaction */
159 u8 nAux; /* # of auxiliary columns in %_rowid */
160 #ifdef SQLITE_ENABLE_GEOPOLY
161 u8 nAuxNotNull; /* Number of initial not-null aux columns */
162 #endif
163 #ifdef SQLITE_DEBUG
164 u8 bCorrupt; /* Shadow table corruption detected */
165 #endif
166 int iDepth; /* Current depth of the r-tree structure */
167 char *zDb; /* Name of database containing r-tree table */
168 char *zName; /* Name of r-tree table */
169 char *zNodeName; /* Name of the %_node table */
170 u32 nBusy; /* Current number of users of this structure */
171 i64 nRowEst; /* Estimated number of rows in this table */
172 u32 nCursor; /* Number of open cursors */
173 u32 nNodeRef; /* Number RtreeNodes with positive nRef */
174 char *zReadAuxSql; /* SQL for statement to read aux data */
176 /* List of nodes removed during a CondenseTree operation. List is
177 ** linked together via the pointer normally used for hash chains -
178 ** RtreeNode.pNext. RtreeNode.iNode stores the depth of the sub-tree
179 ** headed by the node (leaf nodes have RtreeNode.iNode==0).
181 RtreeNode *pDeleted;
183 /* Blob I/O on xxx_node */
184 sqlite3_blob *pNodeBlob;
186 /* Statements to read/write/delete a record from xxx_node */
187 sqlite3_stmt *pWriteNode;
188 sqlite3_stmt *pDeleteNode;
190 /* Statements to read/write/delete a record from xxx_rowid */
191 sqlite3_stmt *pReadRowid;
192 sqlite3_stmt *pWriteRowid;
193 sqlite3_stmt *pDeleteRowid;
195 /* Statements to read/write/delete a record from xxx_parent */
196 sqlite3_stmt *pReadParent;
197 sqlite3_stmt *pWriteParent;
198 sqlite3_stmt *pDeleteParent;
200 /* Statement for writing to the "aux:" fields, if there are any */
201 sqlite3_stmt *pWriteAux;
203 RtreeNode *aHash[HASHSIZE]; /* Hash table of in-memory nodes. */
206 /* Possible values for Rtree.eCoordType: */
207 #define RTREE_COORD_REAL32 0
208 #define RTREE_COORD_INT32 1
211 ** If SQLITE_RTREE_INT_ONLY is defined, then this virtual table will
212 ** only deal with integer coordinates. No floating point operations
213 ** will be done.
215 #ifdef SQLITE_RTREE_INT_ONLY
216 typedef sqlite3_int64 RtreeDValue; /* High accuracy coordinate */
217 typedef int RtreeValue; /* Low accuracy coordinate */
218 # define RTREE_ZERO 0
219 #else
220 typedef double RtreeDValue; /* High accuracy coordinate */
221 typedef float RtreeValue; /* Low accuracy coordinate */
222 # define RTREE_ZERO 0.0
223 #endif
226 ** Set the Rtree.bCorrupt flag
228 #ifdef SQLITE_DEBUG
229 # define RTREE_IS_CORRUPT(X) ((X)->bCorrupt = 1)
230 #else
231 # define RTREE_IS_CORRUPT(X)
232 #endif
235 ** When doing a search of an r-tree, instances of the following structure
236 ** record intermediate results from the tree walk.
238 ** The id is always a node-id. For iLevel>=1 the id is the node-id of
239 ** the node that the RtreeSearchPoint represents. When iLevel==0, however,
240 ** the id is of the parent node and the cell that RtreeSearchPoint
241 ** represents is the iCell-th entry in the parent node.
243 struct RtreeSearchPoint {
244 RtreeDValue rScore; /* The score for this node. Smallest goes first. */
245 sqlite3_int64 id; /* Node ID */
246 u8 iLevel; /* 0=entries. 1=leaf node. 2+ for higher */
247 u8 eWithin; /* PARTLY_WITHIN or FULLY_WITHIN */
248 u8 iCell; /* Cell index within the node */
252 ** The minimum number of cells allowed for a node is a third of the
253 ** maximum. In Gutman's notation:
255 ** m = M/3
257 ** If an R*-tree "Reinsert" operation is required, the same number of
258 ** cells are removed from the overfull node and reinserted into the tree.
260 #define RTREE_MINCELLS(p) ((((p)->iNodeSize-4)/(p)->nBytesPerCell)/3)
261 #define RTREE_REINSERT(p) RTREE_MINCELLS(p)
262 #define RTREE_MAXCELLS 51
265 ** The smallest possible node-size is (512-64)==448 bytes. And the largest
266 ** supported cell size is 48 bytes (8 byte rowid + ten 4 byte coordinates).
267 ** Therefore all non-root nodes must contain at least 3 entries. Since
268 ** 3^40 is greater than 2^64, an r-tree structure always has a depth of
269 ** 40 or less.
271 #define RTREE_MAX_DEPTH 40
275 ** Number of entries in the cursor RtreeNode cache. The first entry is
276 ** used to cache the RtreeNode for RtreeCursor.sPoint. The remaining
277 ** entries cache the RtreeNode for the first elements of the priority queue.
279 #define RTREE_CACHE_SZ 5
282 ** An rtree cursor object.
284 struct RtreeCursor {
285 sqlite3_vtab_cursor base; /* Base class. Must be first */
286 u8 atEOF; /* True if at end of search */
287 u8 bPoint; /* True if sPoint is valid */
288 u8 bAuxValid; /* True if pReadAux is valid */
289 int iStrategy; /* Copy of idxNum search parameter */
290 int nConstraint; /* Number of entries in aConstraint */
291 RtreeConstraint *aConstraint; /* Search constraints. */
292 int nPointAlloc; /* Number of slots allocated for aPoint[] */
293 int nPoint; /* Number of slots used in aPoint[] */
294 int mxLevel; /* iLevel value for root of the tree */
295 RtreeSearchPoint *aPoint; /* Priority queue for search points */
296 sqlite3_stmt *pReadAux; /* Statement to read aux-data */
297 RtreeSearchPoint sPoint; /* Cached next search point */
298 RtreeNode *aNode[RTREE_CACHE_SZ]; /* Rtree node cache */
299 u32 anQueue[RTREE_MAX_DEPTH+1]; /* Number of queued entries by iLevel */
302 /* Return the Rtree of a RtreeCursor */
303 #define RTREE_OF_CURSOR(X) ((Rtree*)((X)->base.pVtab))
306 ** A coordinate can be either a floating point number or a integer. All
307 ** coordinates within a single R-Tree are always of the same time.
309 union RtreeCoord {
310 RtreeValue f; /* Floating point value */
311 int i; /* Integer value */
312 u32 u; /* Unsigned for byte-order conversions */
316 ** The argument is an RtreeCoord. Return the value stored within the RtreeCoord
317 ** formatted as a RtreeDValue (double or int64). This macro assumes that local
318 ** variable pRtree points to the Rtree structure associated with the
319 ** RtreeCoord.
321 #ifdef SQLITE_RTREE_INT_ONLY
322 # define DCOORD(coord) ((RtreeDValue)coord.i)
323 #else
324 # define DCOORD(coord) ( \
325 (pRtree->eCoordType==RTREE_COORD_REAL32) ? \
326 ((double)coord.f) : \
327 ((double)coord.i) \
329 #endif
332 ** A search constraint.
334 struct RtreeConstraint {
335 int iCoord; /* Index of constrained coordinate */
336 int op; /* Constraining operation */
337 union {
338 RtreeDValue rValue; /* Constraint value. */
339 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*);
340 int (*xQueryFunc)(sqlite3_rtree_query_info*);
341 } u;
342 sqlite3_rtree_query_info *pInfo; /* xGeom and xQueryFunc argument */
345 /* Possible values for RtreeConstraint.op */
346 #define RTREE_EQ 0x41 /* A */
347 #define RTREE_LE 0x42 /* B */
348 #define RTREE_LT 0x43 /* C */
349 #define RTREE_GE 0x44 /* D */
350 #define RTREE_GT 0x45 /* E */
351 #define RTREE_MATCH 0x46 /* F: Old-style sqlite3_rtree_geometry_callback() */
352 #define RTREE_QUERY 0x47 /* G: New-style sqlite3_rtree_query_callback() */
354 /* Special operators available only on cursors. Needs to be consecutive
355 ** with the normal values above, but must be less than RTREE_MATCH. These
356 ** are used in the cursor for contraints such as x=NULL (RTREE_FALSE) or
357 ** x<'xyz' (RTREE_TRUE) */
358 #define RTREE_TRUE 0x3f /* ? */
359 #define RTREE_FALSE 0x40 /* @ */
362 ** An rtree structure node.
364 struct RtreeNode {
365 RtreeNode *pParent; /* Parent node */
366 i64 iNode; /* The node number */
367 int nRef; /* Number of references to this node */
368 int isDirty; /* True if the node needs to be written to disk */
369 u8 *zData; /* Content of the node, as should be on disk */
370 RtreeNode *pNext; /* Next node in this hash collision chain */
373 /* Return the number of cells in a node */
374 #define NCELL(pNode) readInt16(&(pNode)->zData[2])
377 ** A single cell from a node, deserialized
379 struct RtreeCell {
380 i64 iRowid; /* Node or entry ID */
381 RtreeCoord aCoord[RTREE_MAX_DIMENSIONS*2]; /* Bounding box coordinates */
386 ** This object becomes the sqlite3_user_data() for the SQL functions
387 ** that are created by sqlite3_rtree_geometry_callback() and
388 ** sqlite3_rtree_query_callback() and which appear on the right of MATCH
389 ** operators in order to constrain a search.
391 ** xGeom and xQueryFunc are the callback functions. Exactly one of
392 ** xGeom and xQueryFunc fields is non-NULL, depending on whether the
393 ** SQL function was created using sqlite3_rtree_geometry_callback() or
394 ** sqlite3_rtree_query_callback().
396 ** This object is deleted automatically by the destructor mechanism in
397 ** sqlite3_create_function_v2().
399 struct RtreeGeomCallback {
400 int (*xGeom)(sqlite3_rtree_geometry*, int, RtreeDValue*, int*);
401 int (*xQueryFunc)(sqlite3_rtree_query_info*);
402 void (*xDestructor)(void*);
403 void *pContext;
407 ** An instance of this structure (in the form of a BLOB) is returned by
408 ** the SQL functions that sqlite3_rtree_geometry_callback() and
409 ** sqlite3_rtree_query_callback() create, and is read as the right-hand
410 ** operand to the MATCH operator of an R-Tree.
412 struct RtreeMatchArg {
413 u32 iSize; /* Size of this object */
414 RtreeGeomCallback cb; /* Info about the callback functions */
415 int nParam; /* Number of parameters to the SQL function */
416 sqlite3_value **apSqlParam; /* Original SQL parameter values */
417 RtreeDValue aParam[1]; /* Values for parameters to the SQL function */
420 #ifndef MAX
421 # define MAX(x,y) ((x) < (y) ? (y) : (x))
422 #endif
423 #ifndef MIN
424 # define MIN(x,y) ((x) > (y) ? (y) : (x))
425 #endif
427 /* What version of GCC is being used. 0 means GCC is not being used .
428 ** Note that the GCC_VERSION macro will also be set correctly when using
429 ** clang, since clang works hard to be gcc compatible. So the gcc
430 ** optimizations will also work when compiling with clang.
432 #ifndef GCC_VERSION
433 #if defined(__GNUC__) && !defined(SQLITE_DISABLE_INTRINSIC)
434 # define GCC_VERSION (__GNUC__*1000000+__GNUC_MINOR__*1000+__GNUC_PATCHLEVEL__)
435 #else
436 # define GCC_VERSION 0
437 #endif
438 #endif
440 /* The testcase() macro should already be defined in the amalgamation. If
441 ** it is not, make it a no-op.
443 #ifndef SQLITE_AMALGAMATION
444 # if defined(SQLITE_COVERAGE_TEST) || defined(SQLITE_DEBUG)
445 unsigned int sqlite3RtreeTestcase = 0;
446 # define testcase(X) if( X ){ sqlite3RtreeTestcase += __LINE__; }
447 # else
448 # define testcase(X)
449 # endif
450 #endif
453 ** Make sure that the compiler intrinsics we desire are enabled when
454 ** compiling with an appropriate version of MSVC unless prevented by
455 ** the SQLITE_DISABLE_INTRINSIC define.
457 #if !defined(SQLITE_DISABLE_INTRINSIC)
458 # if defined(_MSC_VER) && _MSC_VER>=1400
459 # if !defined(_WIN32_WCE)
460 # include <intrin.h>
461 # pragma intrinsic(_byteswap_ulong)
462 # pragma intrinsic(_byteswap_uint64)
463 # else
464 # include <cmnintrin.h>
465 # endif
466 # endif
467 #endif
470 ** Macros to determine whether the machine is big or little endian,
471 ** and whether or not that determination is run-time or compile-time.
473 ** For best performance, an attempt is made to guess at the byte-order
474 ** using C-preprocessor macros. If that is unsuccessful, or if
475 ** -DSQLITE_RUNTIME_BYTEORDER=1 is set, then byte-order is determined
476 ** at run-time.
478 #ifndef SQLITE_BYTEORDER /* Replicate changes at tag-20230904a */
479 # if defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_BIG_ENDIAN__
480 # define SQLITE_BYTEORDER 4321
481 # elif defined(__BYTE_ORDER__) && __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
482 # define SQLITE_BYTEORDER 1234
483 # elif defined(__BIG_ENDIAN__) && __BIG_ENDIAN__==1
484 # define SQLITE_BYTEORDER 4321
485 # elif defined(i386) || defined(__i386__) || defined(_M_IX86) || \
486 defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || \
487 defined(_M_AMD64) || defined(_M_ARM) || defined(__x86) || \
488 defined(__ARMEL__) || defined(__AARCH64EL__) || defined(_M_ARM64)
489 # define SQLITE_BYTEORDER 1234
490 # elif defined(sparc) || defined(__ARMEB__) || defined(__AARCH64EB__)
491 # define SQLITE_BYTEORDER 4321
492 # else
493 # define SQLITE_BYTEORDER 0
494 # endif
495 #endif
498 /* What version of MSVC is being used. 0 means MSVC is not being used */
499 #ifndef MSVC_VERSION
500 #if defined(_MSC_VER) && !defined(SQLITE_DISABLE_INTRINSIC)
501 # define MSVC_VERSION _MSC_VER
502 #else
503 # define MSVC_VERSION 0
504 #endif
505 #endif
508 ** Functions to deserialize a 16 bit integer, 32 bit real number and
509 ** 64 bit integer. The deserialized value is returned.
511 static int readInt16(u8 *p){
512 return (p[0]<<8) + p[1];
514 static void readCoord(u8 *p, RtreeCoord *pCoord){
515 assert( FOUR_BYTE_ALIGNED(p) );
516 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
517 pCoord->u = _byteswap_ulong(*(u32*)p);
518 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
519 pCoord->u = __builtin_bswap32(*(u32*)p);
520 #elif SQLITE_BYTEORDER==4321
521 pCoord->u = *(u32*)p;
522 #else
523 pCoord->u = (
524 (((u32)p[0]) << 24) +
525 (((u32)p[1]) << 16) +
526 (((u32)p[2]) << 8) +
527 (((u32)p[3]) << 0)
529 #endif
531 static i64 readInt64(u8 *p){
532 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
533 u64 x;
534 memcpy(&x, p, 8);
535 return (i64)_byteswap_uint64(x);
536 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
537 u64 x;
538 memcpy(&x, p, 8);
539 return (i64)__builtin_bswap64(x);
540 #elif SQLITE_BYTEORDER==4321
541 i64 x;
542 memcpy(&x, p, 8);
543 return x;
544 #else
545 return (i64)(
546 (((u64)p[0]) << 56) +
547 (((u64)p[1]) << 48) +
548 (((u64)p[2]) << 40) +
549 (((u64)p[3]) << 32) +
550 (((u64)p[4]) << 24) +
551 (((u64)p[5]) << 16) +
552 (((u64)p[6]) << 8) +
553 (((u64)p[7]) << 0)
555 #endif
559 ** Functions to serialize a 16 bit integer, 32 bit real number and
560 ** 64 bit integer. The value returned is the number of bytes written
561 ** to the argument buffer (always 2, 4 and 8 respectively).
563 static void writeInt16(u8 *p, int i){
564 p[0] = (i>> 8)&0xFF;
565 p[1] = (i>> 0)&0xFF;
567 static int writeCoord(u8 *p, RtreeCoord *pCoord){
568 u32 i;
569 assert( FOUR_BYTE_ALIGNED(p) );
570 assert( sizeof(RtreeCoord)==4 );
571 assert( sizeof(u32)==4 );
572 #if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
573 i = __builtin_bswap32(pCoord->u);
574 memcpy(p, &i, 4);
575 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
576 i = _byteswap_ulong(pCoord->u);
577 memcpy(p, &i, 4);
578 #elif SQLITE_BYTEORDER==4321
579 i = pCoord->u;
580 memcpy(p, &i, 4);
581 #else
582 i = pCoord->u;
583 p[0] = (i>>24)&0xFF;
584 p[1] = (i>>16)&0xFF;
585 p[2] = (i>> 8)&0xFF;
586 p[3] = (i>> 0)&0xFF;
587 #endif
588 return 4;
590 static int writeInt64(u8 *p, i64 i){
591 #if SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
592 i = (i64)__builtin_bswap64((u64)i);
593 memcpy(p, &i, 8);
594 #elif SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
595 i = (i64)_byteswap_uint64((u64)i);
596 memcpy(p, &i, 8);
597 #elif SQLITE_BYTEORDER==4321
598 memcpy(p, &i, 8);
599 #else
600 p[0] = (i>>56)&0xFF;
601 p[1] = (i>>48)&0xFF;
602 p[2] = (i>>40)&0xFF;
603 p[3] = (i>>32)&0xFF;
604 p[4] = (i>>24)&0xFF;
605 p[5] = (i>>16)&0xFF;
606 p[6] = (i>> 8)&0xFF;
607 p[7] = (i>> 0)&0xFF;
608 #endif
609 return 8;
613 ** Increment the reference count of node p.
615 static void nodeReference(RtreeNode *p){
616 if( p ){
617 assert( p->nRef>0 );
618 p->nRef++;
623 ** Clear the content of node p (set all bytes to 0x00).
625 static void nodeZero(Rtree *pRtree, RtreeNode *p){
626 memset(&p->zData[2], 0, pRtree->iNodeSize-2);
627 p->isDirty = 1;
631 ** Given a node number iNode, return the corresponding key to use
632 ** in the Rtree.aHash table.
634 static unsigned int nodeHash(i64 iNode){
635 return ((unsigned)iNode) % HASHSIZE;
639 ** Search the node hash table for node iNode. If found, return a pointer
640 ** to it. Otherwise, return 0.
642 static RtreeNode *nodeHashLookup(Rtree *pRtree, i64 iNode){
643 RtreeNode *p;
644 for(p=pRtree->aHash[nodeHash(iNode)]; p && p->iNode!=iNode; p=p->pNext);
645 return p;
649 ** Add node pNode to the node hash table.
651 static void nodeHashInsert(Rtree *pRtree, RtreeNode *pNode){
652 int iHash;
653 assert( pNode->pNext==0 );
654 iHash = nodeHash(pNode->iNode);
655 pNode->pNext = pRtree->aHash[iHash];
656 pRtree->aHash[iHash] = pNode;
660 ** Remove node pNode from the node hash table.
662 static void nodeHashDelete(Rtree *pRtree, RtreeNode *pNode){
663 RtreeNode **pp;
664 if( pNode->iNode!=0 ){
665 pp = &pRtree->aHash[nodeHash(pNode->iNode)];
666 for( ; (*pp)!=pNode; pp = &(*pp)->pNext){ assert(*pp); }
667 *pp = pNode->pNext;
668 pNode->pNext = 0;
673 ** Allocate and return new r-tree node. Initially, (RtreeNode.iNode==0),
674 ** indicating that node has not yet been assigned a node number. It is
675 ** assigned a node number when nodeWrite() is called to write the
676 ** node contents out to the database.
678 static RtreeNode *nodeNew(Rtree *pRtree, RtreeNode *pParent){
679 RtreeNode *pNode;
680 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode) + pRtree->iNodeSize);
681 if( pNode ){
682 memset(pNode, 0, sizeof(RtreeNode) + pRtree->iNodeSize);
683 pNode->zData = (u8 *)&pNode[1];
684 pNode->nRef = 1;
685 pRtree->nNodeRef++;
686 pNode->pParent = pParent;
687 pNode->isDirty = 1;
688 nodeReference(pParent);
690 return pNode;
694 ** Clear the Rtree.pNodeBlob object
696 static void nodeBlobReset(Rtree *pRtree){
697 sqlite3_blob *pBlob = pRtree->pNodeBlob;
698 pRtree->pNodeBlob = 0;
699 sqlite3_blob_close(pBlob);
703 ** Obtain a reference to an r-tree node.
705 static int nodeAcquire(
706 Rtree *pRtree, /* R-tree structure */
707 i64 iNode, /* Node number to load */
708 RtreeNode *pParent, /* Either the parent node or NULL */
709 RtreeNode **ppNode /* OUT: Acquired node */
711 int rc = SQLITE_OK;
712 RtreeNode *pNode = 0;
714 /* Check if the requested node is already in the hash table. If so,
715 ** increase its reference count and return it.
717 if( (pNode = nodeHashLookup(pRtree, iNode))!=0 ){
718 if( pParent && ALWAYS(pParent!=pNode->pParent) ){
719 RTREE_IS_CORRUPT(pRtree);
720 return SQLITE_CORRUPT_VTAB;
722 pNode->nRef++;
723 *ppNode = pNode;
724 return SQLITE_OK;
727 if( pRtree->pNodeBlob ){
728 sqlite3_blob *pBlob = pRtree->pNodeBlob;
729 pRtree->pNodeBlob = 0;
730 rc = sqlite3_blob_reopen(pBlob, iNode);
731 pRtree->pNodeBlob = pBlob;
732 if( rc ){
733 nodeBlobReset(pRtree);
734 if( rc==SQLITE_NOMEM ) return SQLITE_NOMEM;
737 if( pRtree->pNodeBlob==0 ){
738 rc = sqlite3_blob_open(pRtree->db, pRtree->zDb, pRtree->zNodeName,
739 "data", iNode, 0,
740 &pRtree->pNodeBlob);
742 if( rc ){
743 *ppNode = 0;
744 /* If unable to open an sqlite3_blob on the desired row, that can only
745 ** be because the shadow tables hold erroneous data. */
746 if( rc==SQLITE_ERROR ){
747 rc = SQLITE_CORRUPT_VTAB;
748 RTREE_IS_CORRUPT(pRtree);
750 }else if( pRtree->iNodeSize==sqlite3_blob_bytes(pRtree->pNodeBlob) ){
751 pNode = (RtreeNode *)sqlite3_malloc64(sizeof(RtreeNode)+pRtree->iNodeSize);
752 if( !pNode ){
753 rc = SQLITE_NOMEM;
754 }else{
755 pNode->pParent = pParent;
756 pNode->zData = (u8 *)&pNode[1];
757 pNode->nRef = 1;
758 pRtree->nNodeRef++;
759 pNode->iNode = iNode;
760 pNode->isDirty = 0;
761 pNode->pNext = 0;
762 rc = sqlite3_blob_read(pRtree->pNodeBlob, pNode->zData,
763 pRtree->iNodeSize, 0);
767 /* If the root node was just loaded, set pRtree->iDepth to the height
768 ** of the r-tree structure. A height of zero means all data is stored on
769 ** the root node. A height of one means the children of the root node
770 ** are the leaves, and so on. If the depth as specified on the root node
771 ** is greater than RTREE_MAX_DEPTH, the r-tree structure must be corrupt.
773 if( rc==SQLITE_OK && pNode && iNode==1 ){
774 pRtree->iDepth = readInt16(pNode->zData);
775 if( pRtree->iDepth>RTREE_MAX_DEPTH ){
776 rc = SQLITE_CORRUPT_VTAB;
777 RTREE_IS_CORRUPT(pRtree);
781 /* If no error has occurred so far, check if the "number of entries"
782 ** field on the node is too large. If so, set the return code to
783 ** SQLITE_CORRUPT_VTAB.
785 if( pNode && rc==SQLITE_OK ){
786 if( NCELL(pNode)>((pRtree->iNodeSize-4)/pRtree->nBytesPerCell) ){
787 rc = SQLITE_CORRUPT_VTAB;
788 RTREE_IS_CORRUPT(pRtree);
792 if( rc==SQLITE_OK ){
793 if( pNode!=0 ){
794 nodeReference(pParent);
795 nodeHashInsert(pRtree, pNode);
796 }else{
797 rc = SQLITE_CORRUPT_VTAB;
798 RTREE_IS_CORRUPT(pRtree);
800 *ppNode = pNode;
801 }else{
802 nodeBlobReset(pRtree);
803 if( pNode ){
804 pRtree->nNodeRef--;
805 sqlite3_free(pNode);
807 *ppNode = 0;
810 return rc;
814 ** Overwrite cell iCell of node pNode with the contents of pCell.
816 static void nodeOverwriteCell(
817 Rtree *pRtree, /* The overall R-Tree */
818 RtreeNode *pNode, /* The node into which the cell is to be written */
819 RtreeCell *pCell, /* The cell to write */
820 int iCell /* Index into pNode into which pCell is written */
822 int ii;
823 u8 *p = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
824 p += writeInt64(p, pCell->iRowid);
825 for(ii=0; ii<pRtree->nDim2; ii++){
826 p += writeCoord(p, &pCell->aCoord[ii]);
828 pNode->isDirty = 1;
832 ** Remove the cell with index iCell from node pNode.
834 static void nodeDeleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell){
835 u8 *pDst = &pNode->zData[4 + pRtree->nBytesPerCell*iCell];
836 u8 *pSrc = &pDst[pRtree->nBytesPerCell];
837 int nByte = (NCELL(pNode) - iCell - 1) * pRtree->nBytesPerCell;
838 memmove(pDst, pSrc, nByte);
839 writeInt16(&pNode->zData[2], NCELL(pNode)-1);
840 pNode->isDirty = 1;
844 ** Insert the contents of cell pCell into node pNode. If the insert
845 ** is successful, return SQLITE_OK.
847 ** If there is not enough free space in pNode, return SQLITE_FULL.
849 static int nodeInsertCell(
850 Rtree *pRtree, /* The overall R-Tree */
851 RtreeNode *pNode, /* Write new cell into this node */
852 RtreeCell *pCell /* The cell to be inserted */
854 int nCell; /* Current number of cells in pNode */
855 int nMaxCell; /* Maximum number of cells for pNode */
857 nMaxCell = (pRtree->iNodeSize-4)/pRtree->nBytesPerCell;
858 nCell = NCELL(pNode);
860 assert( nCell<=nMaxCell );
861 if( nCell<nMaxCell ){
862 nodeOverwriteCell(pRtree, pNode, pCell, nCell);
863 writeInt16(&pNode->zData[2], nCell+1);
864 pNode->isDirty = 1;
867 return (nCell==nMaxCell);
871 ** If the node is dirty, write it out to the database.
873 static int nodeWrite(Rtree *pRtree, RtreeNode *pNode){
874 int rc = SQLITE_OK;
875 if( pNode->isDirty ){
876 sqlite3_stmt *p = pRtree->pWriteNode;
877 if( pNode->iNode ){
878 sqlite3_bind_int64(p, 1, pNode->iNode);
879 }else{
880 sqlite3_bind_null(p, 1);
882 sqlite3_bind_blob(p, 2, pNode->zData, pRtree->iNodeSize, SQLITE_STATIC);
883 sqlite3_step(p);
884 pNode->isDirty = 0;
885 rc = sqlite3_reset(p);
886 sqlite3_bind_null(p, 2);
887 if( pNode->iNode==0 && rc==SQLITE_OK ){
888 pNode->iNode = sqlite3_last_insert_rowid(pRtree->db);
889 nodeHashInsert(pRtree, pNode);
892 return rc;
896 ** Release a reference to a node. If the node is dirty and the reference
897 ** count drops to zero, the node data is written to the database.
899 static int nodeRelease(Rtree *pRtree, RtreeNode *pNode){
900 int rc = SQLITE_OK;
901 if( pNode ){
902 assert( pNode->nRef>0 );
903 assert( pRtree->nNodeRef>0 );
904 pNode->nRef--;
905 if( pNode->nRef==0 ){
906 pRtree->nNodeRef--;
907 if( pNode->iNode==1 ){
908 pRtree->iDepth = -1;
910 if( pNode->pParent ){
911 rc = nodeRelease(pRtree, pNode->pParent);
913 if( rc==SQLITE_OK ){
914 rc = nodeWrite(pRtree, pNode);
916 nodeHashDelete(pRtree, pNode);
917 sqlite3_free(pNode);
920 return rc;
924 ** Return the 64-bit integer value associated with cell iCell of
925 ** node pNode. If pNode is a leaf node, this is a rowid. If it is
926 ** an internal node, then the 64-bit integer is a child page number.
928 static i64 nodeGetRowid(
929 Rtree *pRtree, /* The overall R-Tree */
930 RtreeNode *pNode, /* The node from which to extract the ID */
931 int iCell /* The cell index from which to extract the ID */
933 assert( iCell<NCELL(pNode) );
934 return readInt64(&pNode->zData[4 + pRtree->nBytesPerCell*iCell]);
938 ** Return coordinate iCoord from cell iCell in node pNode.
940 static void nodeGetCoord(
941 Rtree *pRtree, /* The overall R-Tree */
942 RtreeNode *pNode, /* The node from which to extract a coordinate */
943 int iCell, /* The index of the cell within the node */
944 int iCoord, /* Which coordinate to extract */
945 RtreeCoord *pCoord /* OUT: Space to write result to */
947 assert( iCell<NCELL(pNode) );
948 readCoord(&pNode->zData[12 + pRtree->nBytesPerCell*iCell + 4*iCoord], pCoord);
952 ** Deserialize cell iCell of node pNode. Populate the structure pointed
953 ** to by pCell with the results.
955 static void nodeGetCell(
956 Rtree *pRtree, /* The overall R-Tree */
957 RtreeNode *pNode, /* The node containing the cell to be read */
958 int iCell, /* Index of the cell within the node */
959 RtreeCell *pCell /* OUT: Write the cell contents here */
961 u8 *pData;
962 RtreeCoord *pCoord;
963 int ii = 0;
964 pCell->iRowid = nodeGetRowid(pRtree, pNode, iCell);
965 pData = pNode->zData + (12 + pRtree->nBytesPerCell*iCell);
966 pCoord = pCell->aCoord;
968 readCoord(pData, &pCoord[ii]);
969 readCoord(pData+4, &pCoord[ii+1]);
970 pData += 8;
971 ii += 2;
972 }while( ii<pRtree->nDim2 );
976 /* Forward declaration for the function that does the work of
977 ** the virtual table module xCreate() and xConnect() methods.
979 static int rtreeInit(
980 sqlite3 *, void *, int, const char *const*, sqlite3_vtab **, char **, int
984 ** Rtree virtual table module xCreate method.
986 static int rtreeCreate(
987 sqlite3 *db,
988 void *pAux,
989 int argc, const char *const*argv,
990 sqlite3_vtab **ppVtab,
991 char **pzErr
993 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 1);
997 ** Rtree virtual table module xConnect method.
999 static int rtreeConnect(
1000 sqlite3 *db,
1001 void *pAux,
1002 int argc, const char *const*argv,
1003 sqlite3_vtab **ppVtab,
1004 char **pzErr
1006 return rtreeInit(db, pAux, argc, argv, ppVtab, pzErr, 0);
1010 ** Increment the r-tree reference count.
1012 static void rtreeReference(Rtree *pRtree){
1013 pRtree->nBusy++;
1017 ** Decrement the r-tree reference count. When the reference count reaches
1018 ** zero the structure is deleted.
1020 static void rtreeRelease(Rtree *pRtree){
1021 pRtree->nBusy--;
1022 if( pRtree->nBusy==0 ){
1023 pRtree->inWrTrans = 0;
1024 assert( pRtree->nCursor==0 );
1025 nodeBlobReset(pRtree);
1026 assert( pRtree->nNodeRef==0 || pRtree->bCorrupt );
1027 sqlite3_finalize(pRtree->pWriteNode);
1028 sqlite3_finalize(pRtree->pDeleteNode);
1029 sqlite3_finalize(pRtree->pReadRowid);
1030 sqlite3_finalize(pRtree->pWriteRowid);
1031 sqlite3_finalize(pRtree->pDeleteRowid);
1032 sqlite3_finalize(pRtree->pReadParent);
1033 sqlite3_finalize(pRtree->pWriteParent);
1034 sqlite3_finalize(pRtree->pDeleteParent);
1035 sqlite3_finalize(pRtree->pWriteAux);
1036 sqlite3_free(pRtree->zReadAuxSql);
1037 sqlite3_free(pRtree);
1042 ** Rtree virtual table module xDisconnect method.
1044 static int rtreeDisconnect(sqlite3_vtab *pVtab){
1045 rtreeRelease((Rtree *)pVtab);
1046 return SQLITE_OK;
1050 ** Rtree virtual table module xDestroy method.
1052 static int rtreeDestroy(sqlite3_vtab *pVtab){
1053 Rtree *pRtree = (Rtree *)pVtab;
1054 int rc;
1055 char *zCreate = sqlite3_mprintf(
1056 "DROP TABLE '%q'.'%q_node';"
1057 "DROP TABLE '%q'.'%q_rowid';"
1058 "DROP TABLE '%q'.'%q_parent';",
1059 pRtree->zDb, pRtree->zName,
1060 pRtree->zDb, pRtree->zName,
1061 pRtree->zDb, pRtree->zName
1063 if( !zCreate ){
1064 rc = SQLITE_NOMEM;
1065 }else{
1066 nodeBlobReset(pRtree);
1067 rc = sqlite3_exec(pRtree->db, zCreate, 0, 0, 0);
1068 sqlite3_free(zCreate);
1070 if( rc==SQLITE_OK ){
1071 rtreeRelease(pRtree);
1074 return rc;
1078 ** Rtree virtual table module xOpen method.
1080 static int rtreeOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
1081 int rc = SQLITE_NOMEM;
1082 Rtree *pRtree = (Rtree *)pVTab;
1083 RtreeCursor *pCsr;
1085 pCsr = (RtreeCursor *)sqlite3_malloc64(sizeof(RtreeCursor));
1086 if( pCsr ){
1087 memset(pCsr, 0, sizeof(RtreeCursor));
1088 pCsr->base.pVtab = pVTab;
1089 rc = SQLITE_OK;
1090 pRtree->nCursor++;
1092 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
1094 return rc;
1099 ** Reset a cursor back to its initial state.
1101 static void resetCursor(RtreeCursor *pCsr){
1102 Rtree *pRtree = (Rtree *)(pCsr->base.pVtab);
1103 int ii;
1104 sqlite3_stmt *pStmt;
1105 if( pCsr->aConstraint ){
1106 int i; /* Used to iterate through constraint array */
1107 for(i=0; i<pCsr->nConstraint; i++){
1108 sqlite3_rtree_query_info *pInfo = pCsr->aConstraint[i].pInfo;
1109 if( pInfo ){
1110 if( pInfo->xDelUser ) pInfo->xDelUser(pInfo->pUser);
1111 sqlite3_free(pInfo);
1114 sqlite3_free(pCsr->aConstraint);
1115 pCsr->aConstraint = 0;
1117 for(ii=0; ii<RTREE_CACHE_SZ; ii++) nodeRelease(pRtree, pCsr->aNode[ii]);
1118 sqlite3_free(pCsr->aPoint);
1119 pStmt = pCsr->pReadAux;
1120 memset(pCsr, 0, sizeof(RtreeCursor));
1121 pCsr->base.pVtab = (sqlite3_vtab*)pRtree;
1122 pCsr->pReadAux = pStmt;
1127 ** Rtree virtual table module xClose method.
1129 static int rtreeClose(sqlite3_vtab_cursor *cur){
1130 Rtree *pRtree = (Rtree *)(cur->pVtab);
1131 RtreeCursor *pCsr = (RtreeCursor *)cur;
1132 assert( pRtree->nCursor>0 );
1133 resetCursor(pCsr);
1134 sqlite3_finalize(pCsr->pReadAux);
1135 sqlite3_free(pCsr);
1136 pRtree->nCursor--;
1137 if( pRtree->nCursor==0 && pRtree->inWrTrans==0 ){
1138 nodeBlobReset(pRtree);
1140 return SQLITE_OK;
1144 ** Rtree virtual table module xEof method.
1146 ** Return non-zero if the cursor does not currently point to a valid
1147 ** record (i.e if the scan has finished), or zero otherwise.
1149 static int rtreeEof(sqlite3_vtab_cursor *cur){
1150 RtreeCursor *pCsr = (RtreeCursor *)cur;
1151 return pCsr->atEOF;
1155 ** Convert raw bits from the on-disk RTree record into a coordinate value.
1156 ** The on-disk format is big-endian and needs to be converted for little-
1157 ** endian platforms. The on-disk record stores integer coordinates if
1158 ** eInt is true and it stores 32-bit floating point records if eInt is
1159 ** false. a[] is the four bytes of the on-disk record to be decoded.
1160 ** Store the results in "r".
1162 ** There are five versions of this macro. The last one is generic. The
1163 ** other four are various architectures-specific optimizations.
1165 #if SQLITE_BYTEORDER==1234 && MSVC_VERSION>=1300
1166 #define RTREE_DECODE_COORD(eInt, a, r) { \
1167 RtreeCoord c; /* Coordinate decoded */ \
1168 c.u = _byteswap_ulong(*(u32*)a); \
1169 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
1171 #elif SQLITE_BYTEORDER==1234 && GCC_VERSION>=4003000
1172 #define RTREE_DECODE_COORD(eInt, a, r) { \
1173 RtreeCoord c; /* Coordinate decoded */ \
1174 c.u = __builtin_bswap32(*(u32*)a); \
1175 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
1177 #elif SQLITE_BYTEORDER==1234
1178 #define RTREE_DECODE_COORD(eInt, a, r) { \
1179 RtreeCoord c; /* Coordinate decoded */ \
1180 memcpy(&c.u,a,4); \
1181 c.u = ((c.u>>24)&0xff)|((c.u>>8)&0xff00)| \
1182 ((c.u&0xff)<<24)|((c.u&0xff00)<<8); \
1183 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
1185 #elif SQLITE_BYTEORDER==4321
1186 #define RTREE_DECODE_COORD(eInt, a, r) { \
1187 RtreeCoord c; /* Coordinate decoded */ \
1188 memcpy(&c.u,a,4); \
1189 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
1191 #else
1192 #define RTREE_DECODE_COORD(eInt, a, r) { \
1193 RtreeCoord c; /* Coordinate decoded */ \
1194 c.u = ((u32)a[0]<<24) + ((u32)a[1]<<16) \
1195 +((u32)a[2]<<8) + a[3]; \
1196 r = eInt ? (sqlite3_rtree_dbl)c.i : (sqlite3_rtree_dbl)c.f; \
1198 #endif
1201 ** Check the RTree node or entry given by pCellData and p against the MATCH
1202 ** constraint pConstraint.
1204 static int rtreeCallbackConstraint(
1205 RtreeConstraint *pConstraint, /* The constraint to test */
1206 int eInt, /* True if RTree holding integer coordinates */
1207 u8 *pCellData, /* Raw cell content */
1208 RtreeSearchPoint *pSearch, /* Container of this cell */
1209 sqlite3_rtree_dbl *prScore, /* OUT: score for the cell */
1210 int *peWithin /* OUT: visibility of the cell */
1212 sqlite3_rtree_query_info *pInfo = pConstraint->pInfo; /* Callback info */
1213 int nCoord = pInfo->nCoord; /* No. of coordinates */
1214 int rc; /* Callback return code */
1215 RtreeCoord c; /* Translator union */
1216 sqlite3_rtree_dbl aCoord[RTREE_MAX_DIMENSIONS*2]; /* Decoded coordinates */
1218 assert( pConstraint->op==RTREE_MATCH || pConstraint->op==RTREE_QUERY );
1219 assert( nCoord==2 || nCoord==4 || nCoord==6 || nCoord==8 || nCoord==10 );
1221 if( pConstraint->op==RTREE_QUERY && pSearch->iLevel==1 ){
1222 pInfo->iRowid = readInt64(pCellData);
1224 pCellData += 8;
1225 #ifndef SQLITE_RTREE_INT_ONLY
1226 if( eInt==0 ){
1227 switch( nCoord ){
1228 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.f;
1229 readCoord(pCellData+32, &c); aCoord[8] = c.f;
1230 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.f;
1231 readCoord(pCellData+24, &c); aCoord[6] = c.f;
1232 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.f;
1233 readCoord(pCellData+16, &c); aCoord[4] = c.f;
1234 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.f;
1235 readCoord(pCellData+8, &c); aCoord[2] = c.f;
1236 default: readCoord(pCellData+4, &c); aCoord[1] = c.f;
1237 readCoord(pCellData, &c); aCoord[0] = c.f;
1239 }else
1240 #endif
1242 switch( nCoord ){
1243 case 10: readCoord(pCellData+36, &c); aCoord[9] = c.i;
1244 readCoord(pCellData+32, &c); aCoord[8] = c.i;
1245 case 8: readCoord(pCellData+28, &c); aCoord[7] = c.i;
1246 readCoord(pCellData+24, &c); aCoord[6] = c.i;
1247 case 6: readCoord(pCellData+20, &c); aCoord[5] = c.i;
1248 readCoord(pCellData+16, &c); aCoord[4] = c.i;
1249 case 4: readCoord(pCellData+12, &c); aCoord[3] = c.i;
1250 readCoord(pCellData+8, &c); aCoord[2] = c.i;
1251 default: readCoord(pCellData+4, &c); aCoord[1] = c.i;
1252 readCoord(pCellData, &c); aCoord[0] = c.i;
1255 if( pConstraint->op==RTREE_MATCH ){
1256 int eWithin = 0;
1257 rc = pConstraint->u.xGeom((sqlite3_rtree_geometry*)pInfo,
1258 nCoord, aCoord, &eWithin);
1259 if( eWithin==0 ) *peWithin = NOT_WITHIN;
1260 *prScore = RTREE_ZERO;
1261 }else{
1262 pInfo->aCoord = aCoord;
1263 pInfo->iLevel = pSearch->iLevel - 1;
1264 pInfo->rScore = pInfo->rParentScore = pSearch->rScore;
1265 pInfo->eWithin = pInfo->eParentWithin = pSearch->eWithin;
1266 rc = pConstraint->u.xQueryFunc(pInfo);
1267 if( pInfo->eWithin<*peWithin ) *peWithin = pInfo->eWithin;
1268 if( pInfo->rScore<*prScore || *prScore<RTREE_ZERO ){
1269 *prScore = pInfo->rScore;
1272 return rc;
1276 ** Check the internal RTree node given by pCellData against constraint p.
1277 ** If this constraint cannot be satisfied by any child within the node,
1278 ** set *peWithin to NOT_WITHIN.
1280 static void rtreeNonleafConstraint(
1281 RtreeConstraint *p, /* The constraint to test */
1282 int eInt, /* True if RTree holds integer coordinates */
1283 u8 *pCellData, /* Raw cell content as appears on disk */
1284 int *peWithin /* Adjust downward, as appropriate */
1286 sqlite3_rtree_dbl val; /* Coordinate value convert to a double */
1288 /* p->iCoord might point to either a lower or upper bound coordinate
1289 ** in a coordinate pair. But make pCellData point to the lower bound.
1291 pCellData += 8 + 4*(p->iCoord&0xfe);
1293 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
1294 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
1295 || p->op==RTREE_FALSE );
1296 assert( FOUR_BYTE_ALIGNED(pCellData) );
1297 switch( p->op ){
1298 case RTREE_TRUE: return; /* Always satisfied */
1299 case RTREE_FALSE: break; /* Never satisfied */
1300 case RTREE_EQ:
1301 RTREE_DECODE_COORD(eInt, pCellData, val);
1302 /* val now holds the lower bound of the coordinate pair */
1303 if( p->u.rValue>=val ){
1304 pCellData += 4;
1305 RTREE_DECODE_COORD(eInt, pCellData, val);
1306 /* val now holds the upper bound of the coordinate pair */
1307 if( p->u.rValue<=val ) return;
1309 break;
1310 case RTREE_LE:
1311 case RTREE_LT:
1312 RTREE_DECODE_COORD(eInt, pCellData, val);
1313 /* val now holds the lower bound of the coordinate pair */
1314 if( p->u.rValue>=val ) return;
1315 break;
1317 default:
1318 pCellData += 4;
1319 RTREE_DECODE_COORD(eInt, pCellData, val);
1320 /* val now holds the upper bound of the coordinate pair */
1321 if( p->u.rValue<=val ) return;
1322 break;
1324 *peWithin = NOT_WITHIN;
1328 ** Check the leaf RTree cell given by pCellData against constraint p.
1329 ** If this constraint is not satisfied, set *peWithin to NOT_WITHIN.
1330 ** If the constraint is satisfied, leave *peWithin unchanged.
1332 ** The constraint is of the form: xN op $val
1334 ** The op is given by p->op. The xN is p->iCoord-th coordinate in
1335 ** pCellData. $val is given by p->u.rValue.
1337 static void rtreeLeafConstraint(
1338 RtreeConstraint *p, /* The constraint to test */
1339 int eInt, /* True if RTree holds integer coordinates */
1340 u8 *pCellData, /* Raw cell content as appears on disk */
1341 int *peWithin /* Adjust downward, as appropriate */
1343 RtreeDValue xN; /* Coordinate value converted to a double */
1345 assert(p->op==RTREE_LE || p->op==RTREE_LT || p->op==RTREE_GE
1346 || p->op==RTREE_GT || p->op==RTREE_EQ || p->op==RTREE_TRUE
1347 || p->op==RTREE_FALSE );
1348 pCellData += 8 + p->iCoord*4;
1349 assert( FOUR_BYTE_ALIGNED(pCellData) );
1350 RTREE_DECODE_COORD(eInt, pCellData, xN);
1351 switch( p->op ){
1352 case RTREE_TRUE: return; /* Always satisfied */
1353 case RTREE_FALSE: break; /* Never satisfied */
1354 case RTREE_LE: if( xN <= p->u.rValue ) return; break;
1355 case RTREE_LT: if( xN < p->u.rValue ) return; break;
1356 case RTREE_GE: if( xN >= p->u.rValue ) return; break;
1357 case RTREE_GT: if( xN > p->u.rValue ) return; break;
1358 default: if( xN == p->u.rValue ) return; break;
1360 *peWithin = NOT_WITHIN;
1364 ** One of the cells in node pNode is guaranteed to have a 64-bit
1365 ** integer value equal to iRowid. Return the index of this cell.
1367 static int nodeRowidIndex(
1368 Rtree *pRtree,
1369 RtreeNode *pNode,
1370 i64 iRowid,
1371 int *piIndex
1373 int ii;
1374 int nCell = NCELL(pNode);
1375 assert( nCell<200 );
1376 for(ii=0; ii<nCell; ii++){
1377 if( nodeGetRowid(pRtree, pNode, ii)==iRowid ){
1378 *piIndex = ii;
1379 return SQLITE_OK;
1382 RTREE_IS_CORRUPT(pRtree);
1383 return SQLITE_CORRUPT_VTAB;
1387 ** Return the index of the cell containing a pointer to node pNode
1388 ** in its parent. If pNode is the root node, return -1.
1390 static int nodeParentIndex(Rtree *pRtree, RtreeNode *pNode, int *piIndex){
1391 RtreeNode *pParent = pNode->pParent;
1392 if( ALWAYS(pParent) ){
1393 return nodeRowidIndex(pRtree, pParent, pNode->iNode, piIndex);
1394 }else{
1395 *piIndex = -1;
1396 return SQLITE_OK;
1401 ** Compare two search points. Return negative, zero, or positive if the first
1402 ** is less than, equal to, or greater than the second.
1404 ** The rScore is the primary key. Smaller rScore values come first.
1405 ** If the rScore is a tie, then use iLevel as the tie breaker with smaller
1406 ** iLevel values coming first. In this way, if rScore is the same for all
1407 ** SearchPoints, then iLevel becomes the deciding factor and the result
1408 ** is a depth-first search, which is the desired default behavior.
1410 static int rtreeSearchPointCompare(
1411 const RtreeSearchPoint *pA,
1412 const RtreeSearchPoint *pB
1414 if( pA->rScore<pB->rScore ) return -1;
1415 if( pA->rScore>pB->rScore ) return +1;
1416 if( pA->iLevel<pB->iLevel ) return -1;
1417 if( pA->iLevel>pB->iLevel ) return +1;
1418 return 0;
1422 ** Interchange two search points in a cursor.
1424 static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
1425 RtreeSearchPoint t = p->aPoint[i];
1426 assert( i<j );
1427 p->aPoint[i] = p->aPoint[j];
1428 p->aPoint[j] = t;
1429 i++; j++;
1430 if( i<RTREE_CACHE_SZ ){
1431 if( j>=RTREE_CACHE_SZ ){
1432 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
1433 p->aNode[i] = 0;
1434 }else{
1435 RtreeNode *pTemp = p->aNode[i];
1436 p->aNode[i] = p->aNode[j];
1437 p->aNode[j] = pTemp;
1443 ** Return the search point with the lowest current score.
1445 static RtreeSearchPoint *rtreeSearchPointFirst(RtreeCursor *pCur){
1446 return pCur->bPoint ? &pCur->sPoint : pCur->nPoint ? pCur->aPoint : 0;
1450 ** Get the RtreeNode for the search point with the lowest score.
1452 static RtreeNode *rtreeNodeOfFirstSearchPoint(RtreeCursor *pCur, int *pRC){
1453 sqlite3_int64 id;
1454 int ii = 1 - pCur->bPoint;
1455 assert( ii==0 || ii==1 );
1456 assert( pCur->bPoint || pCur->nPoint );
1457 if( pCur->aNode[ii]==0 ){
1458 assert( pRC!=0 );
1459 id = ii ? pCur->aPoint[0].id : pCur->sPoint.id;
1460 *pRC = nodeAcquire(RTREE_OF_CURSOR(pCur), id, 0, &pCur->aNode[ii]);
1462 return pCur->aNode[ii];
1466 ** Push a new element onto the priority queue
1468 static RtreeSearchPoint *rtreeEnqueue(
1469 RtreeCursor *pCur, /* The cursor */
1470 RtreeDValue rScore, /* Score for the new search point */
1471 u8 iLevel /* Level for the new search point */
1473 int i, j;
1474 RtreeSearchPoint *pNew;
1475 if( pCur->nPoint>=pCur->nPointAlloc ){
1476 int nNew = pCur->nPointAlloc*2 + 8;
1477 pNew = sqlite3_realloc64(pCur->aPoint, nNew*sizeof(pCur->aPoint[0]));
1478 if( pNew==0 ) return 0;
1479 pCur->aPoint = pNew;
1480 pCur->nPointAlloc = nNew;
1482 i = pCur->nPoint++;
1483 pNew = pCur->aPoint + i;
1484 pNew->rScore = rScore;
1485 pNew->iLevel = iLevel;
1486 assert( iLevel<=RTREE_MAX_DEPTH );
1487 while( i>0 ){
1488 RtreeSearchPoint *pParent;
1489 j = (i-1)/2;
1490 pParent = pCur->aPoint + j;
1491 if( rtreeSearchPointCompare(pNew, pParent)>=0 ) break;
1492 rtreeSearchPointSwap(pCur, j, i);
1493 i = j;
1494 pNew = pParent;
1496 return pNew;
1500 ** Allocate a new RtreeSearchPoint and return a pointer to it. Return
1501 ** NULL if malloc fails.
1503 static RtreeSearchPoint *rtreeSearchPointNew(
1504 RtreeCursor *pCur, /* The cursor */
1505 RtreeDValue rScore, /* Score for the new search point */
1506 u8 iLevel /* Level for the new search point */
1508 RtreeSearchPoint *pNew, *pFirst;
1509 pFirst = rtreeSearchPointFirst(pCur);
1510 pCur->anQueue[iLevel]++;
1511 if( pFirst==0
1512 || pFirst->rScore>rScore
1513 || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
1515 if( pCur->bPoint ){
1516 int ii;
1517 pNew = rtreeEnqueue(pCur, rScore, iLevel);
1518 if( pNew==0 ) return 0;
1519 ii = (int)(pNew - pCur->aPoint) + 1;
1520 assert( ii==1 );
1521 if( ALWAYS(ii<RTREE_CACHE_SZ) ){
1522 assert( pCur->aNode[ii]==0 );
1523 pCur->aNode[ii] = pCur->aNode[0];
1524 }else{
1525 nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
1527 pCur->aNode[0] = 0;
1528 *pNew = pCur->sPoint;
1530 pCur->sPoint.rScore = rScore;
1531 pCur->sPoint.iLevel = iLevel;
1532 pCur->bPoint = 1;
1533 return &pCur->sPoint;
1534 }else{
1535 return rtreeEnqueue(pCur, rScore, iLevel);
1539 #if 0
1540 /* Tracing routines for the RtreeSearchPoint queue */
1541 static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
1542 if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
1543 printf(" %d.%05lld.%02d %g %d",
1544 p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
1546 idx++;
1547 if( idx<RTREE_CACHE_SZ ){
1548 printf(" %p\n", pCur->aNode[idx]);
1549 }else{
1550 printf("\n");
1553 static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
1554 int ii;
1555 printf("=== %9s ", zPrefix);
1556 if( pCur->bPoint ){
1557 tracePoint(&pCur->sPoint, -1, pCur);
1559 for(ii=0; ii<pCur->nPoint; ii++){
1560 if( ii>0 || pCur->bPoint ) printf(" ");
1561 tracePoint(&pCur->aPoint[ii], ii, pCur);
1564 # define RTREE_QUEUE_TRACE(A,B) traceQueue(A,B)
1565 #else
1566 # define RTREE_QUEUE_TRACE(A,B) /* no-op */
1567 #endif
1569 /* Remove the search point with the lowest current score.
1571 static void rtreeSearchPointPop(RtreeCursor *p){
1572 int i, j, k, n;
1573 i = 1 - p->bPoint;
1574 assert( i==0 || i==1 );
1575 if( p->aNode[i] ){
1576 nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
1577 p->aNode[i] = 0;
1579 if( p->bPoint ){
1580 p->anQueue[p->sPoint.iLevel]--;
1581 p->bPoint = 0;
1582 }else if( ALWAYS(p->nPoint) ){
1583 p->anQueue[p->aPoint[0].iLevel]--;
1584 n = --p->nPoint;
1585 p->aPoint[0] = p->aPoint[n];
1586 if( n<RTREE_CACHE_SZ-1 ){
1587 p->aNode[1] = p->aNode[n+1];
1588 p->aNode[n+1] = 0;
1590 i = 0;
1591 while( (j = i*2+1)<n ){
1592 k = j+1;
1593 if( k<n && rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[j])<0 ){
1594 if( rtreeSearchPointCompare(&p->aPoint[k], &p->aPoint[i])<0 ){
1595 rtreeSearchPointSwap(p, i, k);
1596 i = k;
1597 }else{
1598 break;
1600 }else{
1601 if( rtreeSearchPointCompare(&p->aPoint[j], &p->aPoint[i])<0 ){
1602 rtreeSearchPointSwap(p, i, j);
1603 i = j;
1604 }else{
1605 break;
1614 ** Continue the search on cursor pCur until the front of the queue
1615 ** contains an entry suitable for returning as a result-set row,
1616 ** or until the RtreeSearchPoint queue is empty, indicating that the
1617 ** query has completed.
1619 static int rtreeStepToLeaf(RtreeCursor *pCur){
1620 RtreeSearchPoint *p;
1621 Rtree *pRtree = RTREE_OF_CURSOR(pCur);
1622 RtreeNode *pNode;
1623 int eWithin;
1624 int rc = SQLITE_OK;
1625 int nCell;
1626 int nConstraint = pCur->nConstraint;
1627 int ii;
1628 int eInt;
1629 RtreeSearchPoint x;
1631 eInt = pRtree->eCoordType==RTREE_COORD_INT32;
1632 while( (p = rtreeSearchPointFirst(pCur))!=0 && p->iLevel>0 ){
1633 u8 *pCellData;
1634 pNode = rtreeNodeOfFirstSearchPoint(pCur, &rc);
1635 if( rc ) return rc;
1636 nCell = NCELL(pNode);
1637 assert( nCell<200 );
1638 pCellData = pNode->zData + (4+pRtree->nBytesPerCell*p->iCell);
1639 while( p->iCell<nCell ){
1640 sqlite3_rtree_dbl rScore = (sqlite3_rtree_dbl)-1;
1641 eWithin = FULLY_WITHIN;
1642 for(ii=0; ii<nConstraint; ii++){
1643 RtreeConstraint *pConstraint = pCur->aConstraint + ii;
1644 if( pConstraint->op>=RTREE_MATCH ){
1645 rc = rtreeCallbackConstraint(pConstraint, eInt, pCellData, p,
1646 &rScore, &eWithin);
1647 if( rc ) return rc;
1648 }else if( p->iLevel==1 ){
1649 rtreeLeafConstraint(pConstraint, eInt, pCellData, &eWithin);
1650 }else{
1651 rtreeNonleafConstraint(pConstraint, eInt, pCellData, &eWithin);
1653 if( eWithin==NOT_WITHIN ){
1654 p->iCell++;
1655 pCellData += pRtree->nBytesPerCell;
1656 break;
1659 if( eWithin==NOT_WITHIN ) continue;
1660 p->iCell++;
1661 x.iLevel = p->iLevel - 1;
1662 if( x.iLevel ){
1663 x.id = readInt64(pCellData);
1664 for(ii=0; ii<pCur->nPoint; ii++){
1665 if( pCur->aPoint[ii].id==x.id ){
1666 RTREE_IS_CORRUPT(pRtree);
1667 return SQLITE_CORRUPT_VTAB;
1670 x.iCell = 0;
1671 }else{
1672 x.id = p->id;
1673 x.iCell = p->iCell - 1;
1675 if( p->iCell>=nCell ){
1676 RTREE_QUEUE_TRACE(pCur, "POP-S:");
1677 rtreeSearchPointPop(pCur);
1679 if( rScore<RTREE_ZERO ) rScore = RTREE_ZERO;
1680 p = rtreeSearchPointNew(pCur, rScore, x.iLevel);
1681 if( p==0 ) return SQLITE_NOMEM;
1682 p->eWithin = (u8)eWithin;
1683 p->id = x.id;
1684 p->iCell = x.iCell;
1685 RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
1686 break;
1688 if( p->iCell>=nCell ){
1689 RTREE_QUEUE_TRACE(pCur, "POP-Se:");
1690 rtreeSearchPointPop(pCur);
1693 pCur->atEOF = p==0;
1694 return SQLITE_OK;
1698 ** Rtree virtual table module xNext method.
1700 static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
1701 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
1702 int rc = SQLITE_OK;
1704 /* Move to the next entry that matches the configured constraints. */
1705 RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
1706 if( pCsr->bAuxValid ){
1707 pCsr->bAuxValid = 0;
1708 sqlite3_reset(pCsr->pReadAux);
1710 rtreeSearchPointPop(pCsr);
1711 rc = rtreeStepToLeaf(pCsr);
1712 return rc;
1716 ** Rtree virtual table module xRowid method.
1718 static int rtreeRowid(sqlite3_vtab_cursor *pVtabCursor, sqlite_int64 *pRowid){
1719 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
1720 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
1721 int rc = SQLITE_OK;
1722 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
1723 if( rc==SQLITE_OK && ALWAYS(p) ){
1724 if( p->iCell>=NCELL(pNode) ){
1725 rc = SQLITE_ABORT;
1726 }else{
1727 *pRowid = nodeGetRowid(RTREE_OF_CURSOR(pCsr), pNode, p->iCell);
1730 return rc;
1734 ** Rtree virtual table module xColumn method.
1736 static int rtreeColumn(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int i){
1737 Rtree *pRtree = (Rtree *)cur->pVtab;
1738 RtreeCursor *pCsr = (RtreeCursor *)cur;
1739 RtreeSearchPoint *p = rtreeSearchPointFirst(pCsr);
1740 RtreeCoord c;
1741 int rc = SQLITE_OK;
1742 RtreeNode *pNode = rtreeNodeOfFirstSearchPoint(pCsr, &rc);
1744 if( rc ) return rc;
1745 if( NEVER(p==0) ) return SQLITE_OK;
1746 if( p->iCell>=NCELL(pNode) ) return SQLITE_ABORT;
1747 if( i==0 ){
1748 sqlite3_result_int64(ctx, nodeGetRowid(pRtree, pNode, p->iCell));
1749 }else if( i<=pRtree->nDim2 ){
1750 nodeGetCoord(pRtree, pNode, p->iCell, i-1, &c);
1751 #ifndef SQLITE_RTREE_INT_ONLY
1752 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
1753 sqlite3_result_double(ctx, c.f);
1754 }else
1755 #endif
1757 assert( pRtree->eCoordType==RTREE_COORD_INT32 );
1758 sqlite3_result_int(ctx, c.i);
1760 }else{
1761 if( !pCsr->bAuxValid ){
1762 if( pCsr->pReadAux==0 ){
1763 rc = sqlite3_prepare_v3(pRtree->db, pRtree->zReadAuxSql, -1, 0,
1764 &pCsr->pReadAux, 0);
1765 if( rc ) return rc;
1767 sqlite3_bind_int64(pCsr->pReadAux, 1,
1768 nodeGetRowid(pRtree, pNode, p->iCell));
1769 rc = sqlite3_step(pCsr->pReadAux);
1770 if( rc==SQLITE_ROW ){
1771 pCsr->bAuxValid = 1;
1772 }else{
1773 sqlite3_reset(pCsr->pReadAux);
1774 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
1775 return rc;
1778 sqlite3_result_value(ctx,
1779 sqlite3_column_value(pCsr->pReadAux, i - pRtree->nDim2 + 1));
1781 return SQLITE_OK;
1785 ** Use nodeAcquire() to obtain the leaf node containing the record with
1786 ** rowid iRowid. If successful, set *ppLeaf to point to the node and
1787 ** return SQLITE_OK. If there is no such record in the table, set
1788 ** *ppLeaf to 0 and return SQLITE_OK. If an error occurs, set *ppLeaf
1789 ** to zero and return an SQLite error code.
1791 static int findLeafNode(
1792 Rtree *pRtree, /* RTree to search */
1793 i64 iRowid, /* The rowid searching for */
1794 RtreeNode **ppLeaf, /* Write the node here */
1795 sqlite3_int64 *piNode /* Write the node-id here */
1797 int rc;
1798 *ppLeaf = 0;
1799 sqlite3_bind_int64(pRtree->pReadRowid, 1, iRowid);
1800 if( sqlite3_step(pRtree->pReadRowid)==SQLITE_ROW ){
1801 i64 iNode = sqlite3_column_int64(pRtree->pReadRowid, 0);
1802 if( piNode ) *piNode = iNode;
1803 rc = nodeAcquire(pRtree, iNode, 0, ppLeaf);
1804 sqlite3_reset(pRtree->pReadRowid);
1805 }else{
1806 rc = sqlite3_reset(pRtree->pReadRowid);
1808 return rc;
1812 ** This function is called to configure the RtreeConstraint object passed
1813 ** as the second argument for a MATCH constraint. The value passed as the
1814 ** first argument to this function is the right-hand operand to the MATCH
1815 ** operator.
1817 static int deserializeGeometry(sqlite3_value *pValue, RtreeConstraint *pCons){
1818 RtreeMatchArg *pBlob, *pSrc; /* BLOB returned by geometry function */
1819 sqlite3_rtree_query_info *pInfo; /* Callback information */
1821 pSrc = sqlite3_value_pointer(pValue, "RtreeMatchArg");
1822 if( pSrc==0 ) return SQLITE_ERROR;
1823 pInfo = (sqlite3_rtree_query_info*)
1824 sqlite3_malloc64( sizeof(*pInfo)+pSrc->iSize );
1825 if( !pInfo ) return SQLITE_NOMEM;
1826 memset(pInfo, 0, sizeof(*pInfo));
1827 pBlob = (RtreeMatchArg*)&pInfo[1];
1828 memcpy(pBlob, pSrc, pSrc->iSize);
1829 pInfo->pContext = pBlob->cb.pContext;
1830 pInfo->nParam = pBlob->nParam;
1831 pInfo->aParam = pBlob->aParam;
1832 pInfo->apSqlParam = pBlob->apSqlParam;
1834 if( pBlob->cb.xGeom ){
1835 pCons->u.xGeom = pBlob->cb.xGeom;
1836 }else{
1837 pCons->op = RTREE_QUERY;
1838 pCons->u.xQueryFunc = pBlob->cb.xQueryFunc;
1840 pCons->pInfo = pInfo;
1841 return SQLITE_OK;
1844 int sqlite3IntFloatCompare(i64,double);
1847 ** Rtree virtual table module xFilter method.
1849 static int rtreeFilter(
1850 sqlite3_vtab_cursor *pVtabCursor,
1851 int idxNum, const char *idxStr,
1852 int argc, sqlite3_value **argv
1854 Rtree *pRtree = (Rtree *)pVtabCursor->pVtab;
1855 RtreeCursor *pCsr = (RtreeCursor *)pVtabCursor;
1856 RtreeNode *pRoot = 0;
1857 int ii;
1858 int rc = SQLITE_OK;
1859 int iCell = 0;
1861 rtreeReference(pRtree);
1863 /* Reset the cursor to the same state as rtreeOpen() leaves it in. */
1864 resetCursor(pCsr);
1866 pCsr->iStrategy = idxNum;
1867 if( idxNum==1 ){
1868 /* Special case - lookup by rowid. */
1869 RtreeNode *pLeaf; /* Leaf on which the required cell resides */
1870 RtreeSearchPoint *p; /* Search point for the leaf */
1871 i64 iRowid = sqlite3_value_int64(argv[0]);
1872 i64 iNode = 0;
1873 int eType = sqlite3_value_numeric_type(argv[0]);
1874 if( eType==SQLITE_INTEGER
1875 || (eType==SQLITE_FLOAT
1876 && 0==sqlite3IntFloatCompare(iRowid,sqlite3_value_double(argv[0])))
1878 rc = findLeafNode(pRtree, iRowid, &pLeaf, &iNode);
1879 }else{
1880 rc = SQLITE_OK;
1881 pLeaf = 0;
1883 if( rc==SQLITE_OK && pLeaf!=0 ){
1884 p = rtreeSearchPointNew(pCsr, RTREE_ZERO, 0);
1885 assert( p!=0 ); /* Always returns pCsr->sPoint */
1886 pCsr->aNode[0] = pLeaf;
1887 p->id = iNode;
1888 p->eWithin = PARTLY_WITHIN;
1889 rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
1890 p->iCell = (u8)iCell;
1891 RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
1892 }else{
1893 pCsr->atEOF = 1;
1895 }else{
1896 /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array
1897 ** with the configured constraints.
1899 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
1900 if( rc==SQLITE_OK && argc>0 ){
1901 pCsr->aConstraint = sqlite3_malloc64(sizeof(RtreeConstraint)*argc);
1902 pCsr->nConstraint = argc;
1903 if( !pCsr->aConstraint ){
1904 rc = SQLITE_NOMEM;
1905 }else{
1906 memset(pCsr->aConstraint, 0, sizeof(RtreeConstraint)*argc);
1907 memset(pCsr->anQueue, 0, sizeof(u32)*(pRtree->iDepth + 1));
1908 assert( (idxStr==0 && argc==0)
1909 || (idxStr && (int)strlen(idxStr)==argc*2) );
1910 for(ii=0; ii<argc; ii++){
1911 RtreeConstraint *p = &pCsr->aConstraint[ii];
1912 int eType = sqlite3_value_numeric_type(argv[ii]);
1913 p->op = idxStr[ii*2];
1914 p->iCoord = idxStr[ii*2+1]-'0';
1915 if( p->op>=RTREE_MATCH ){
1916 /* A MATCH operator. The right-hand-side must be a blob that
1917 ** can be cast into an RtreeMatchArg object. One created using
1918 ** an sqlite3_rtree_geometry_callback() SQL user function.
1920 rc = deserializeGeometry(argv[ii], p);
1921 if( rc!=SQLITE_OK ){
1922 break;
1924 p->pInfo->nCoord = pRtree->nDim2;
1925 p->pInfo->anQueue = pCsr->anQueue;
1926 p->pInfo->mxLevel = pRtree->iDepth + 1;
1927 }else if( eType==SQLITE_INTEGER ){
1928 sqlite3_int64 iVal = sqlite3_value_int64(argv[ii]);
1929 #ifdef SQLITE_RTREE_INT_ONLY
1930 p->u.rValue = iVal;
1931 #else
1932 p->u.rValue = (double)iVal;
1933 if( iVal>=((sqlite3_int64)1)<<48
1934 || iVal<=-(((sqlite3_int64)1)<<48)
1936 if( p->op==RTREE_LT ) p->op = RTREE_LE;
1937 if( p->op==RTREE_GT ) p->op = RTREE_GE;
1939 #endif
1940 }else if( eType==SQLITE_FLOAT ){
1941 #ifdef SQLITE_RTREE_INT_ONLY
1942 p->u.rValue = sqlite3_value_int64(argv[ii]);
1943 #else
1944 p->u.rValue = sqlite3_value_double(argv[ii]);
1945 #endif
1946 }else{
1947 p->u.rValue = RTREE_ZERO;
1948 if( eType==SQLITE_NULL ){
1949 p->op = RTREE_FALSE;
1950 }else if( p->op==RTREE_LT || p->op==RTREE_LE ){
1951 p->op = RTREE_TRUE;
1952 }else{
1953 p->op = RTREE_FALSE;
1959 if( rc==SQLITE_OK ){
1960 RtreeSearchPoint *pNew;
1961 assert( pCsr->bPoint==0 ); /* Due to the resetCursor() call above */
1962 pNew = rtreeSearchPointNew(pCsr, RTREE_ZERO, (u8)(pRtree->iDepth+1));
1963 if( NEVER(pNew==0) ){ /* Because pCsr->bPoint was FALSE */
1964 return SQLITE_NOMEM;
1966 pNew->id = 1;
1967 pNew->iCell = 0;
1968 pNew->eWithin = PARTLY_WITHIN;
1969 assert( pCsr->bPoint==1 );
1970 pCsr->aNode[0] = pRoot;
1971 pRoot = 0;
1972 RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
1973 rc = rtreeStepToLeaf(pCsr);
1977 nodeRelease(pRtree, pRoot);
1978 rtreeRelease(pRtree);
1979 return rc;
1983 ** Rtree virtual table module xBestIndex method. There are three
1984 ** table scan strategies to choose from (in order from most to
1985 ** least desirable):
1987 ** idxNum idxStr Strategy
1988 ** ------------------------------------------------
1989 ** 1 Unused Direct lookup by rowid.
1990 ** 2 See below R-tree query or full-table scan.
1991 ** ------------------------------------------------
1993 ** If strategy 1 is used, then idxStr is not meaningful. If strategy
1994 ** 2 is used, idxStr is formatted to contain 2 bytes for each
1995 ** constraint used. The first two bytes of idxStr correspond to
1996 ** the constraint in sqlite3_index_info.aConstraintUsage[] with
1997 ** (argvIndex==1) etc.
1999 ** The first of each pair of bytes in idxStr identifies the constraint
2000 ** operator as follows:
2002 ** Operator Byte Value
2003 ** ----------------------
2004 ** = 0x41 ('A')
2005 ** <= 0x42 ('B')
2006 ** < 0x43 ('C')
2007 ** >= 0x44 ('D')
2008 ** > 0x45 ('E')
2009 ** MATCH 0x46 ('F')
2010 ** ----------------------
2012 ** The second of each pair of bytes identifies the coordinate column
2013 ** to which the constraint applies. The leftmost coordinate column
2014 ** is 'a', the second from the left 'b' etc.
2016 static int rtreeBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
2017 Rtree *pRtree = (Rtree*)tab;
2018 int rc = SQLITE_OK;
2019 int ii;
2020 int bMatch = 0; /* True if there exists a MATCH constraint */
2021 i64 nRow; /* Estimated rows returned by this scan */
2023 int iIdx = 0;
2024 char zIdxStr[RTREE_MAX_DIMENSIONS*8+1];
2025 memset(zIdxStr, 0, sizeof(zIdxStr));
2027 /* Check if there exists a MATCH constraint - even an unusable one. If there
2028 ** is, do not consider the lookup-by-rowid plan as using such a plan would
2029 ** require the VDBE to evaluate the MATCH constraint, which is not currently
2030 ** possible. */
2031 for(ii=0; ii<pIdxInfo->nConstraint; ii++){
2032 if( pIdxInfo->aConstraint[ii].op==SQLITE_INDEX_CONSTRAINT_MATCH ){
2033 bMatch = 1;
2037 assert( pIdxInfo->idxStr==0 );
2038 for(ii=0; ii<pIdxInfo->nConstraint && iIdx<(int)(sizeof(zIdxStr)-1); ii++){
2039 struct sqlite3_index_constraint *p = &pIdxInfo->aConstraint[ii];
2041 if( bMatch==0 && p->usable
2042 && p->iColumn<=0 && p->op==SQLITE_INDEX_CONSTRAINT_EQ
2044 /* We have an equality constraint on the rowid. Use strategy 1. */
2045 int jj;
2046 for(jj=0; jj<ii; jj++){
2047 pIdxInfo->aConstraintUsage[jj].argvIndex = 0;
2048 pIdxInfo->aConstraintUsage[jj].omit = 0;
2050 pIdxInfo->idxNum = 1;
2051 pIdxInfo->aConstraintUsage[ii].argvIndex = 1;
2052 pIdxInfo->aConstraintUsage[jj].omit = 1;
2054 /* This strategy involves a two rowid lookups on an B-Tree structures
2055 ** and then a linear search of an R-Tree node. This should be
2056 ** considered almost as quick as a direct rowid lookup (for which
2057 ** sqlite uses an internal cost of 0.0). It is expected to return
2058 ** a single row.
2060 pIdxInfo->estimatedCost = 30.0;
2061 pIdxInfo->estimatedRows = 1;
2062 pIdxInfo->idxFlags = SQLITE_INDEX_SCAN_UNIQUE;
2063 return SQLITE_OK;
2066 if( p->usable
2067 && ((p->iColumn>0 && p->iColumn<=pRtree->nDim2)
2068 || p->op==SQLITE_INDEX_CONSTRAINT_MATCH)
2070 u8 op;
2071 u8 doOmit = 1;
2072 switch( p->op ){
2073 case SQLITE_INDEX_CONSTRAINT_EQ: op = RTREE_EQ; doOmit = 0; break;
2074 case SQLITE_INDEX_CONSTRAINT_GT: op = RTREE_GT; doOmit = 0; break;
2075 case SQLITE_INDEX_CONSTRAINT_LE: op = RTREE_LE; break;
2076 case SQLITE_INDEX_CONSTRAINT_LT: op = RTREE_LT; doOmit = 0; break;
2077 case SQLITE_INDEX_CONSTRAINT_GE: op = RTREE_GE; break;
2078 case SQLITE_INDEX_CONSTRAINT_MATCH: op = RTREE_MATCH; break;
2079 default: op = 0; break;
2081 if( op ){
2082 zIdxStr[iIdx++] = op;
2083 zIdxStr[iIdx++] = (char)(p->iColumn - 1 + '0');
2084 pIdxInfo->aConstraintUsage[ii].argvIndex = (iIdx/2);
2085 pIdxInfo->aConstraintUsage[ii].omit = doOmit;
2090 pIdxInfo->idxNum = 2;
2091 pIdxInfo->needToFreeIdxStr = 1;
2092 if( iIdx>0 ){
2093 pIdxInfo->idxStr = sqlite3_malloc( iIdx+1 );
2094 if( pIdxInfo->idxStr==0 ){
2095 return SQLITE_NOMEM;
2097 memcpy(pIdxInfo->idxStr, zIdxStr, iIdx+1);
2100 nRow = pRtree->nRowEst >> (iIdx/2);
2101 pIdxInfo->estimatedCost = (double)6.0 * (double)nRow;
2102 pIdxInfo->estimatedRows = nRow;
2104 return rc;
2108 ** Return the N-dimensional volumn of the cell stored in *p.
2110 static RtreeDValue cellArea(Rtree *pRtree, RtreeCell *p){
2111 RtreeDValue area = (RtreeDValue)1;
2112 assert( pRtree->nDim>=1 && pRtree->nDim<=5 );
2113 #ifndef SQLITE_RTREE_INT_ONLY
2114 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
2115 switch( pRtree->nDim ){
2116 case 5: area = p->aCoord[9].f - p->aCoord[8].f;
2117 case 4: area *= p->aCoord[7].f - p->aCoord[6].f;
2118 case 3: area *= p->aCoord[5].f - p->aCoord[4].f;
2119 case 2: area *= p->aCoord[3].f - p->aCoord[2].f;
2120 default: area *= p->aCoord[1].f - p->aCoord[0].f;
2122 }else
2123 #endif
2125 switch( pRtree->nDim ){
2126 case 5: area = (i64)p->aCoord[9].i - (i64)p->aCoord[8].i;
2127 case 4: area *= (i64)p->aCoord[7].i - (i64)p->aCoord[6].i;
2128 case 3: area *= (i64)p->aCoord[5].i - (i64)p->aCoord[4].i;
2129 case 2: area *= (i64)p->aCoord[3].i - (i64)p->aCoord[2].i;
2130 default: area *= (i64)p->aCoord[1].i - (i64)p->aCoord[0].i;
2133 return area;
2137 ** Return the margin length of cell p. The margin length is the sum
2138 ** of the objects size in each dimension.
2140 static RtreeDValue cellMargin(Rtree *pRtree, RtreeCell *p){
2141 RtreeDValue margin = 0;
2142 int ii = pRtree->nDim2 - 2;
2144 margin += (DCOORD(p->aCoord[ii+1]) - DCOORD(p->aCoord[ii]));
2145 ii -= 2;
2146 }while( ii>=0 );
2147 return margin;
2151 ** Store the union of cells p1 and p2 in p1.
2153 static void cellUnion(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
2154 int ii = 0;
2155 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
2157 p1->aCoord[ii].f = MIN(p1->aCoord[ii].f, p2->aCoord[ii].f);
2158 p1->aCoord[ii+1].f = MAX(p1->aCoord[ii+1].f, p2->aCoord[ii+1].f);
2159 ii += 2;
2160 }while( ii<pRtree->nDim2 );
2161 }else{
2163 p1->aCoord[ii].i = MIN(p1->aCoord[ii].i, p2->aCoord[ii].i);
2164 p1->aCoord[ii+1].i = MAX(p1->aCoord[ii+1].i, p2->aCoord[ii+1].i);
2165 ii += 2;
2166 }while( ii<pRtree->nDim2 );
2171 ** Return true if the area covered by p2 is a subset of the area covered
2172 ** by p1. False otherwise.
2174 static int cellContains(Rtree *pRtree, RtreeCell *p1, RtreeCell *p2){
2175 int ii;
2176 if( pRtree->eCoordType==RTREE_COORD_INT32 ){
2177 for(ii=0; ii<pRtree->nDim2; ii+=2){
2178 RtreeCoord *a1 = &p1->aCoord[ii];
2179 RtreeCoord *a2 = &p2->aCoord[ii];
2180 if( a2[0].i<a1[0].i || a2[1].i>a1[1].i ) return 0;
2182 }else{
2183 for(ii=0; ii<pRtree->nDim2; ii+=2){
2184 RtreeCoord *a1 = &p1->aCoord[ii];
2185 RtreeCoord *a2 = &p2->aCoord[ii];
2186 if( a2[0].f<a1[0].f || a2[1].f>a1[1].f ) return 0;
2189 return 1;
2192 static RtreeDValue cellOverlap(
2193 Rtree *pRtree,
2194 RtreeCell *p,
2195 RtreeCell *aCell,
2196 int nCell
2198 int ii;
2199 RtreeDValue overlap = RTREE_ZERO;
2200 for(ii=0; ii<nCell; ii++){
2201 int jj;
2202 RtreeDValue o = (RtreeDValue)1;
2203 for(jj=0; jj<pRtree->nDim2; jj+=2){
2204 RtreeDValue x1, x2;
2205 x1 = MAX(DCOORD(p->aCoord[jj]), DCOORD(aCell[ii].aCoord[jj]));
2206 x2 = MIN(DCOORD(p->aCoord[jj+1]), DCOORD(aCell[ii].aCoord[jj+1]));
2207 if( x2<x1 ){
2208 o = (RtreeDValue)0;
2209 break;
2210 }else{
2211 o = o * (x2-x1);
2214 overlap += o;
2216 return overlap;
2221 ** This function implements the ChooseLeaf algorithm from Gutman[84].
2222 ** ChooseSubTree in r*tree terminology.
2224 static int ChooseLeaf(
2225 Rtree *pRtree, /* Rtree table */
2226 RtreeCell *pCell, /* Cell to insert into rtree */
2227 int iHeight, /* Height of sub-tree rooted at pCell */
2228 RtreeNode **ppLeaf /* OUT: Selected leaf page */
2230 int rc;
2231 int ii;
2232 RtreeNode *pNode = 0;
2233 rc = nodeAcquire(pRtree, 1, 0, &pNode);
2235 for(ii=0; rc==SQLITE_OK && ii<(pRtree->iDepth-iHeight); ii++){
2236 int iCell;
2237 sqlite3_int64 iBest = 0;
2238 int bFound = 0;
2239 RtreeDValue fMinGrowth = RTREE_ZERO;
2240 RtreeDValue fMinArea = RTREE_ZERO;
2241 int nCell = NCELL(pNode);
2242 RtreeNode *pChild = 0;
2244 /* First check to see if there is are any cells in pNode that completely
2245 ** contains pCell. If two or more cells in pNode completely contain pCell
2246 ** then pick the smallest.
2248 for(iCell=0; iCell<nCell; iCell++){
2249 RtreeCell cell;
2250 nodeGetCell(pRtree, pNode, iCell, &cell);
2251 if( cellContains(pRtree, &cell, pCell) ){
2252 RtreeDValue area = cellArea(pRtree, &cell);
2253 if( bFound==0 || area<fMinArea ){
2254 iBest = cell.iRowid;
2255 fMinArea = area;
2256 bFound = 1;
2260 if( !bFound ){
2261 /* No cells of pNode will completely contain pCell. So pick the
2262 ** cell of pNode that grows by the least amount when pCell is added.
2263 ** Break ties by selecting the smaller cell.
2265 for(iCell=0; iCell<nCell; iCell++){
2266 RtreeCell cell;
2267 RtreeDValue growth;
2268 RtreeDValue area;
2269 nodeGetCell(pRtree, pNode, iCell, &cell);
2270 area = cellArea(pRtree, &cell);
2271 cellUnion(pRtree, &cell, pCell);
2272 growth = cellArea(pRtree, &cell)-area;
2273 if( iCell==0
2274 || growth<fMinGrowth
2275 || (growth==fMinGrowth && area<fMinArea)
2277 fMinGrowth = growth;
2278 fMinArea = area;
2279 iBest = cell.iRowid;
2284 rc = nodeAcquire(pRtree, iBest, pNode, &pChild);
2285 nodeRelease(pRtree, pNode);
2286 pNode = pChild;
2289 *ppLeaf = pNode;
2290 return rc;
2294 ** A cell with the same content as pCell has just been inserted into
2295 ** the node pNode. This function updates the bounding box cells in
2296 ** all ancestor elements.
2298 static int AdjustTree(
2299 Rtree *pRtree, /* Rtree table */
2300 RtreeNode *pNode, /* Adjust ancestry of this node. */
2301 RtreeCell *pCell /* This cell was just inserted */
2303 RtreeNode *p = pNode;
2304 int cnt = 0;
2305 int rc;
2306 while( p->pParent ){
2307 RtreeNode *pParent = p->pParent;
2308 RtreeCell cell;
2309 int iCell;
2311 cnt++;
2312 if( NEVER(cnt>100) ){
2313 RTREE_IS_CORRUPT(pRtree);
2314 return SQLITE_CORRUPT_VTAB;
2316 rc = nodeParentIndex(pRtree, p, &iCell);
2317 if( NEVER(rc!=SQLITE_OK) ){
2318 RTREE_IS_CORRUPT(pRtree);
2319 return SQLITE_CORRUPT_VTAB;
2322 nodeGetCell(pRtree, pParent, iCell, &cell);
2323 if( !cellContains(pRtree, &cell, pCell) ){
2324 cellUnion(pRtree, &cell, pCell);
2325 nodeOverwriteCell(pRtree, pParent, &cell, iCell);
2328 p = pParent;
2330 return SQLITE_OK;
2334 ** Write mapping (iRowid->iNode) to the <rtree>_rowid table.
2336 static int rowidWrite(Rtree *pRtree, sqlite3_int64 iRowid, sqlite3_int64 iNode){
2337 sqlite3_bind_int64(pRtree->pWriteRowid, 1, iRowid);
2338 sqlite3_bind_int64(pRtree->pWriteRowid, 2, iNode);
2339 sqlite3_step(pRtree->pWriteRowid);
2340 return sqlite3_reset(pRtree->pWriteRowid);
2344 ** Write mapping (iNode->iPar) to the <rtree>_parent table.
2346 static int parentWrite(Rtree *pRtree, sqlite3_int64 iNode, sqlite3_int64 iPar){
2347 sqlite3_bind_int64(pRtree->pWriteParent, 1, iNode);
2348 sqlite3_bind_int64(pRtree->pWriteParent, 2, iPar);
2349 sqlite3_step(pRtree->pWriteParent);
2350 return sqlite3_reset(pRtree->pWriteParent);
2353 static int rtreeInsertCell(Rtree *, RtreeNode *, RtreeCell *, int);
2358 ** Arguments aIdx, aCell and aSpare all point to arrays of size
2359 ** nIdx. The aIdx array contains the set of integers from 0 to
2360 ** (nIdx-1) in no particular order. This function sorts the values
2361 ** in aIdx according to dimension iDim of the cells in aCell. The
2362 ** minimum value of dimension iDim is considered first, the
2363 ** maximum used to break ties.
2365 ** The aSpare array is used as temporary working space by the
2366 ** sorting algorithm.
2368 static void SortByDimension(
2369 Rtree *pRtree,
2370 int *aIdx,
2371 int nIdx,
2372 int iDim,
2373 RtreeCell *aCell,
2374 int *aSpare
2376 if( nIdx>1 ){
2378 int iLeft = 0;
2379 int iRight = 0;
2381 int nLeft = nIdx/2;
2382 int nRight = nIdx-nLeft;
2383 int *aLeft = aIdx;
2384 int *aRight = &aIdx[nLeft];
2386 SortByDimension(pRtree, aLeft, nLeft, iDim, aCell, aSpare);
2387 SortByDimension(pRtree, aRight, nRight, iDim, aCell, aSpare);
2389 memcpy(aSpare, aLeft, sizeof(int)*nLeft);
2390 aLeft = aSpare;
2391 while( iLeft<nLeft || iRight<nRight ){
2392 RtreeDValue xleft1 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2]);
2393 RtreeDValue xleft2 = DCOORD(aCell[aLeft[iLeft]].aCoord[iDim*2+1]);
2394 RtreeDValue xright1 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2]);
2395 RtreeDValue xright2 = DCOORD(aCell[aRight[iRight]].aCoord[iDim*2+1]);
2396 if( (iLeft!=nLeft) && ((iRight==nRight)
2397 || (xleft1<xright1)
2398 || (xleft1==xright1 && xleft2<xright2)
2400 aIdx[iLeft+iRight] = aLeft[iLeft];
2401 iLeft++;
2402 }else{
2403 aIdx[iLeft+iRight] = aRight[iRight];
2404 iRight++;
2408 #if 0
2409 /* Check that the sort worked */
2411 int jj;
2412 for(jj=1; jj<nIdx; jj++){
2413 RtreeDValue xleft1 = aCell[aIdx[jj-1]].aCoord[iDim*2];
2414 RtreeDValue xleft2 = aCell[aIdx[jj-1]].aCoord[iDim*2+1];
2415 RtreeDValue xright1 = aCell[aIdx[jj]].aCoord[iDim*2];
2416 RtreeDValue xright2 = aCell[aIdx[jj]].aCoord[iDim*2+1];
2417 assert( xleft1<=xright1 && (xleft1<xright1 || xleft2<=xright2) );
2420 #endif
2425 ** Implementation of the R*-tree variant of SplitNode from Beckman[1990].
2427 static int splitNodeStartree(
2428 Rtree *pRtree,
2429 RtreeCell *aCell,
2430 int nCell,
2431 RtreeNode *pLeft,
2432 RtreeNode *pRight,
2433 RtreeCell *pBboxLeft,
2434 RtreeCell *pBboxRight
2436 int **aaSorted;
2437 int *aSpare;
2438 int ii;
2440 int iBestDim = 0;
2441 int iBestSplit = 0;
2442 RtreeDValue fBestMargin = RTREE_ZERO;
2444 sqlite3_int64 nByte = (pRtree->nDim+1)*(sizeof(int*)+nCell*sizeof(int));
2446 aaSorted = (int **)sqlite3_malloc64(nByte);
2447 if( !aaSorted ){
2448 return SQLITE_NOMEM;
2451 aSpare = &((int *)&aaSorted[pRtree->nDim])[pRtree->nDim*nCell];
2452 memset(aaSorted, 0, nByte);
2453 for(ii=0; ii<pRtree->nDim; ii++){
2454 int jj;
2455 aaSorted[ii] = &((int *)&aaSorted[pRtree->nDim])[ii*nCell];
2456 for(jj=0; jj<nCell; jj++){
2457 aaSorted[ii][jj] = jj;
2459 SortByDimension(pRtree, aaSorted[ii], nCell, ii, aCell, aSpare);
2462 for(ii=0; ii<pRtree->nDim; ii++){
2463 RtreeDValue margin = RTREE_ZERO;
2464 RtreeDValue fBestOverlap = RTREE_ZERO;
2465 RtreeDValue fBestArea = RTREE_ZERO;
2466 int iBestLeft = 0;
2467 int nLeft;
2469 for(
2470 nLeft=RTREE_MINCELLS(pRtree);
2471 nLeft<=(nCell-RTREE_MINCELLS(pRtree));
2472 nLeft++
2474 RtreeCell left;
2475 RtreeCell right;
2476 int kk;
2477 RtreeDValue overlap;
2478 RtreeDValue area;
2480 memcpy(&left, &aCell[aaSorted[ii][0]], sizeof(RtreeCell));
2481 memcpy(&right, &aCell[aaSorted[ii][nCell-1]], sizeof(RtreeCell));
2482 for(kk=1; kk<(nCell-1); kk++){
2483 if( kk<nLeft ){
2484 cellUnion(pRtree, &left, &aCell[aaSorted[ii][kk]]);
2485 }else{
2486 cellUnion(pRtree, &right, &aCell[aaSorted[ii][kk]]);
2489 margin += cellMargin(pRtree, &left);
2490 margin += cellMargin(pRtree, &right);
2491 overlap = cellOverlap(pRtree, &left, &right, 1);
2492 area = cellArea(pRtree, &left) + cellArea(pRtree, &right);
2493 if( (nLeft==RTREE_MINCELLS(pRtree))
2494 || (overlap<fBestOverlap)
2495 || (overlap==fBestOverlap && area<fBestArea)
2497 iBestLeft = nLeft;
2498 fBestOverlap = overlap;
2499 fBestArea = area;
2503 if( ii==0 || margin<fBestMargin ){
2504 iBestDim = ii;
2505 fBestMargin = margin;
2506 iBestSplit = iBestLeft;
2510 memcpy(pBboxLeft, &aCell[aaSorted[iBestDim][0]], sizeof(RtreeCell));
2511 memcpy(pBboxRight, &aCell[aaSorted[iBestDim][iBestSplit]], sizeof(RtreeCell));
2512 for(ii=0; ii<nCell; ii++){
2513 RtreeNode *pTarget = (ii<iBestSplit)?pLeft:pRight;
2514 RtreeCell *pBbox = (ii<iBestSplit)?pBboxLeft:pBboxRight;
2515 RtreeCell *pCell = &aCell[aaSorted[iBestDim][ii]];
2516 nodeInsertCell(pRtree, pTarget, pCell);
2517 cellUnion(pRtree, pBbox, pCell);
2520 sqlite3_free(aaSorted);
2521 return SQLITE_OK;
2525 static int updateMapping(
2526 Rtree *pRtree,
2527 i64 iRowid,
2528 RtreeNode *pNode,
2529 int iHeight
2531 int (*xSetMapping)(Rtree *, sqlite3_int64, sqlite3_int64);
2532 xSetMapping = ((iHeight==0)?rowidWrite:parentWrite);
2533 if( iHeight>0 ){
2534 RtreeNode *pChild = nodeHashLookup(pRtree, iRowid);
2535 RtreeNode *p;
2536 for(p=pNode; p; p=p->pParent){
2537 if( p==pChild ) return SQLITE_CORRUPT_VTAB;
2539 if( pChild ){
2540 nodeRelease(pRtree, pChild->pParent);
2541 nodeReference(pNode);
2542 pChild->pParent = pNode;
2545 if( NEVER(pNode==0) ) return SQLITE_ERROR;
2546 return xSetMapping(pRtree, iRowid, pNode->iNode);
2549 static int SplitNode(
2550 Rtree *pRtree,
2551 RtreeNode *pNode,
2552 RtreeCell *pCell,
2553 int iHeight
2555 int i;
2556 int newCellIsRight = 0;
2558 int rc = SQLITE_OK;
2559 int nCell = NCELL(pNode);
2560 RtreeCell *aCell;
2561 int *aiUsed;
2563 RtreeNode *pLeft = 0;
2564 RtreeNode *pRight = 0;
2566 RtreeCell leftbbox;
2567 RtreeCell rightbbox;
2569 /* Allocate an array and populate it with a copy of pCell and
2570 ** all cells from node pLeft. Then zero the original node.
2572 aCell = sqlite3_malloc64((sizeof(RtreeCell)+sizeof(int))*(nCell+1));
2573 if( !aCell ){
2574 rc = SQLITE_NOMEM;
2575 goto splitnode_out;
2577 aiUsed = (int *)&aCell[nCell+1];
2578 memset(aiUsed, 0, sizeof(int)*(nCell+1));
2579 for(i=0; i<nCell; i++){
2580 nodeGetCell(pRtree, pNode, i, &aCell[i]);
2582 nodeZero(pRtree, pNode);
2583 memcpy(&aCell[nCell], pCell, sizeof(RtreeCell));
2584 nCell++;
2586 if( pNode->iNode==1 ){
2587 pRight = nodeNew(pRtree, pNode);
2588 pLeft = nodeNew(pRtree, pNode);
2589 pRtree->iDepth++;
2590 pNode->isDirty = 1;
2591 writeInt16(pNode->zData, pRtree->iDepth);
2592 }else{
2593 pLeft = pNode;
2594 pRight = nodeNew(pRtree, pLeft->pParent);
2595 pLeft->nRef++;
2598 if( !pLeft || !pRight ){
2599 rc = SQLITE_NOMEM;
2600 goto splitnode_out;
2603 memset(pLeft->zData, 0, pRtree->iNodeSize);
2604 memset(pRight->zData, 0, pRtree->iNodeSize);
2606 rc = splitNodeStartree(pRtree, aCell, nCell, pLeft, pRight,
2607 &leftbbox, &rightbbox);
2608 if( rc!=SQLITE_OK ){
2609 goto splitnode_out;
2612 /* Ensure both child nodes have node numbers assigned to them by calling
2613 ** nodeWrite(). Node pRight always needs a node number, as it was created
2614 ** by nodeNew() above. But node pLeft sometimes already has a node number.
2615 ** In this case avoid the all to nodeWrite().
2617 if( SQLITE_OK!=(rc = nodeWrite(pRtree, pRight))
2618 || (0==pLeft->iNode && SQLITE_OK!=(rc = nodeWrite(pRtree, pLeft)))
2620 goto splitnode_out;
2623 rightbbox.iRowid = pRight->iNode;
2624 leftbbox.iRowid = pLeft->iNode;
2626 if( pNode->iNode==1 ){
2627 rc = rtreeInsertCell(pRtree, pLeft->pParent, &leftbbox, iHeight+1);
2628 if( rc!=SQLITE_OK ){
2629 goto splitnode_out;
2631 }else{
2632 RtreeNode *pParent = pLeft->pParent;
2633 int iCell;
2634 rc = nodeParentIndex(pRtree, pLeft, &iCell);
2635 if( ALWAYS(rc==SQLITE_OK) ){
2636 nodeOverwriteCell(pRtree, pParent, &leftbbox, iCell);
2637 rc = AdjustTree(pRtree, pParent, &leftbbox);
2638 assert( rc==SQLITE_OK );
2640 if( NEVER(rc!=SQLITE_OK) ){
2641 goto splitnode_out;
2644 if( (rc = rtreeInsertCell(pRtree, pRight->pParent, &rightbbox, iHeight+1)) ){
2645 goto splitnode_out;
2648 for(i=0; i<NCELL(pRight); i++){
2649 i64 iRowid = nodeGetRowid(pRtree, pRight, i);
2650 rc = updateMapping(pRtree, iRowid, pRight, iHeight);
2651 if( iRowid==pCell->iRowid ){
2652 newCellIsRight = 1;
2654 if( rc!=SQLITE_OK ){
2655 goto splitnode_out;
2658 if( pNode->iNode==1 ){
2659 for(i=0; i<NCELL(pLeft); i++){
2660 i64 iRowid = nodeGetRowid(pRtree, pLeft, i);
2661 rc = updateMapping(pRtree, iRowid, pLeft, iHeight);
2662 if( rc!=SQLITE_OK ){
2663 goto splitnode_out;
2666 }else if( newCellIsRight==0 ){
2667 rc = updateMapping(pRtree, pCell->iRowid, pLeft, iHeight);
2670 if( rc==SQLITE_OK ){
2671 rc = nodeRelease(pRtree, pRight);
2672 pRight = 0;
2674 if( rc==SQLITE_OK ){
2675 rc = nodeRelease(pRtree, pLeft);
2676 pLeft = 0;
2679 splitnode_out:
2680 nodeRelease(pRtree, pRight);
2681 nodeRelease(pRtree, pLeft);
2682 sqlite3_free(aCell);
2683 return rc;
2687 ** If node pLeaf is not the root of the r-tree and its pParent pointer is
2688 ** still NULL, load all ancestor nodes of pLeaf into memory and populate
2689 ** the pLeaf->pParent chain all the way up to the root node.
2691 ** This operation is required when a row is deleted (or updated - an update
2692 ** is implemented as a delete followed by an insert). SQLite provides the
2693 ** rowid of the row to delete, which can be used to find the leaf on which
2694 ** the entry resides (argument pLeaf). Once the leaf is located, this
2695 ** function is called to determine its ancestry.
2697 static int fixLeafParent(Rtree *pRtree, RtreeNode *pLeaf){
2698 int rc = SQLITE_OK;
2699 RtreeNode *pChild = pLeaf;
2700 while( rc==SQLITE_OK && pChild->iNode!=1 && pChild->pParent==0 ){
2701 int rc2 = SQLITE_OK; /* sqlite3_reset() return code */
2702 sqlite3_bind_int64(pRtree->pReadParent, 1, pChild->iNode);
2703 rc = sqlite3_step(pRtree->pReadParent);
2704 if( rc==SQLITE_ROW ){
2705 RtreeNode *pTest; /* Used to test for reference loops */
2706 i64 iNode; /* Node number of parent node */
2708 /* Before setting pChild->pParent, test that we are not creating a
2709 ** loop of references (as we would if, say, pChild==pParent). We don't
2710 ** want to do this as it leads to a memory leak when trying to delete
2711 ** the referenced counted node structures.
2713 iNode = sqlite3_column_int64(pRtree->pReadParent, 0);
2714 for(pTest=pLeaf; pTest && pTest->iNode!=iNode; pTest=pTest->pParent);
2715 if( pTest==0 ){
2716 rc2 = nodeAcquire(pRtree, iNode, 0, &pChild->pParent);
2719 rc = sqlite3_reset(pRtree->pReadParent);
2720 if( rc==SQLITE_OK ) rc = rc2;
2721 if( rc==SQLITE_OK && !pChild->pParent ){
2722 RTREE_IS_CORRUPT(pRtree);
2723 rc = SQLITE_CORRUPT_VTAB;
2725 pChild = pChild->pParent;
2727 return rc;
2730 static int deleteCell(Rtree *, RtreeNode *, int, int);
2732 static int removeNode(Rtree *pRtree, RtreeNode *pNode, int iHeight){
2733 int rc;
2734 int rc2;
2735 RtreeNode *pParent = 0;
2736 int iCell;
2738 assert( pNode->nRef==1 );
2740 /* Remove the entry in the parent cell. */
2741 rc = nodeParentIndex(pRtree, pNode, &iCell);
2742 if( rc==SQLITE_OK ){
2743 pParent = pNode->pParent;
2744 pNode->pParent = 0;
2745 rc = deleteCell(pRtree, pParent, iCell, iHeight+1);
2746 testcase( rc!=SQLITE_OK );
2748 rc2 = nodeRelease(pRtree, pParent);
2749 if( rc==SQLITE_OK ){
2750 rc = rc2;
2752 if( rc!=SQLITE_OK ){
2753 return rc;
2756 /* Remove the xxx_node entry. */
2757 sqlite3_bind_int64(pRtree->pDeleteNode, 1, pNode->iNode);
2758 sqlite3_step(pRtree->pDeleteNode);
2759 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteNode)) ){
2760 return rc;
2763 /* Remove the xxx_parent entry. */
2764 sqlite3_bind_int64(pRtree->pDeleteParent, 1, pNode->iNode);
2765 sqlite3_step(pRtree->pDeleteParent);
2766 if( SQLITE_OK!=(rc = sqlite3_reset(pRtree->pDeleteParent)) ){
2767 return rc;
2770 /* Remove the node from the in-memory hash table and link it into
2771 ** the Rtree.pDeleted list. Its contents will be re-inserted later on.
2773 nodeHashDelete(pRtree, pNode);
2774 pNode->iNode = iHeight;
2775 pNode->pNext = pRtree->pDeleted;
2776 pNode->nRef++;
2777 pRtree->pDeleted = pNode;
2779 return SQLITE_OK;
2782 static int fixBoundingBox(Rtree *pRtree, RtreeNode *pNode){
2783 RtreeNode *pParent = pNode->pParent;
2784 int rc = SQLITE_OK;
2785 if( pParent ){
2786 int ii;
2787 int nCell = NCELL(pNode);
2788 RtreeCell box; /* Bounding box for pNode */
2789 nodeGetCell(pRtree, pNode, 0, &box);
2790 for(ii=1; ii<nCell; ii++){
2791 RtreeCell cell;
2792 nodeGetCell(pRtree, pNode, ii, &cell);
2793 cellUnion(pRtree, &box, &cell);
2795 box.iRowid = pNode->iNode;
2796 rc = nodeParentIndex(pRtree, pNode, &ii);
2797 if( rc==SQLITE_OK ){
2798 nodeOverwriteCell(pRtree, pParent, &box, ii);
2799 rc = fixBoundingBox(pRtree, pParent);
2802 return rc;
2806 ** Delete the cell at index iCell of node pNode. After removing the
2807 ** cell, adjust the r-tree data structure if required.
2809 static int deleteCell(Rtree *pRtree, RtreeNode *pNode, int iCell, int iHeight){
2810 RtreeNode *pParent;
2811 int rc;
2813 if( SQLITE_OK!=(rc = fixLeafParent(pRtree, pNode)) ){
2814 return rc;
2817 /* Remove the cell from the node. This call just moves bytes around
2818 ** the in-memory node image, so it cannot fail.
2820 nodeDeleteCell(pRtree, pNode, iCell);
2822 /* If the node is not the tree root and now has less than the minimum
2823 ** number of cells, remove it from the tree. Otherwise, update the
2824 ** cell in the parent node so that it tightly contains the updated
2825 ** node.
2827 pParent = pNode->pParent;
2828 assert( pParent || pNode->iNode==1 );
2829 if( pParent ){
2830 if( NCELL(pNode)<RTREE_MINCELLS(pRtree) ){
2831 rc = removeNode(pRtree, pNode, iHeight);
2832 }else{
2833 rc = fixBoundingBox(pRtree, pNode);
2837 return rc;
2841 ** Insert cell pCell into node pNode. Node pNode is the head of a
2842 ** subtree iHeight high (leaf nodes have iHeight==0).
2844 static int rtreeInsertCell(
2845 Rtree *pRtree,
2846 RtreeNode *pNode,
2847 RtreeCell *pCell,
2848 int iHeight
2850 int rc = SQLITE_OK;
2851 if( iHeight>0 ){
2852 RtreeNode *pChild = nodeHashLookup(pRtree, pCell->iRowid);
2853 if( pChild ){
2854 nodeRelease(pRtree, pChild->pParent);
2855 nodeReference(pNode);
2856 pChild->pParent = pNode;
2859 if( nodeInsertCell(pRtree, pNode, pCell) ){
2860 rc = SplitNode(pRtree, pNode, pCell, iHeight);
2861 }else{
2862 rc = AdjustTree(pRtree, pNode, pCell);
2863 if( ALWAYS(rc==SQLITE_OK) ){
2864 if( iHeight==0 ){
2865 rc = rowidWrite(pRtree, pCell->iRowid, pNode->iNode);
2866 }else{
2867 rc = parentWrite(pRtree, pCell->iRowid, pNode->iNode);
2871 return rc;
2874 static int reinsertNodeContent(Rtree *pRtree, RtreeNode *pNode){
2875 int ii;
2876 int rc = SQLITE_OK;
2877 int nCell = NCELL(pNode);
2879 for(ii=0; rc==SQLITE_OK && ii<nCell; ii++){
2880 RtreeNode *pInsert;
2881 RtreeCell cell;
2882 nodeGetCell(pRtree, pNode, ii, &cell);
2884 /* Find a node to store this cell in. pNode->iNode currently contains
2885 ** the height of the sub-tree headed by the cell.
2887 rc = ChooseLeaf(pRtree, &cell, (int)pNode->iNode, &pInsert);
2888 if( rc==SQLITE_OK ){
2889 int rc2;
2890 rc = rtreeInsertCell(pRtree, pInsert, &cell, (int)pNode->iNode);
2891 rc2 = nodeRelease(pRtree, pInsert);
2892 if( rc==SQLITE_OK ){
2893 rc = rc2;
2897 return rc;
2901 ** Select a currently unused rowid for a new r-tree record.
2903 static int rtreeNewRowid(Rtree *pRtree, i64 *piRowid){
2904 int rc;
2905 sqlite3_bind_null(pRtree->pWriteRowid, 1);
2906 sqlite3_bind_null(pRtree->pWriteRowid, 2);
2907 sqlite3_step(pRtree->pWriteRowid);
2908 rc = sqlite3_reset(pRtree->pWriteRowid);
2909 *piRowid = sqlite3_last_insert_rowid(pRtree->db);
2910 return rc;
2914 ** Remove the entry with rowid=iDelete from the r-tree structure.
2916 static int rtreeDeleteRowid(Rtree *pRtree, sqlite3_int64 iDelete){
2917 int rc; /* Return code */
2918 RtreeNode *pLeaf = 0; /* Leaf node containing record iDelete */
2919 int iCell; /* Index of iDelete cell in pLeaf */
2920 RtreeNode *pRoot = 0; /* Root node of rtree structure */
2923 /* Obtain a reference to the root node to initialize Rtree.iDepth */
2924 rc = nodeAcquire(pRtree, 1, 0, &pRoot);
2926 /* Obtain a reference to the leaf node that contains the entry
2927 ** about to be deleted.
2929 if( rc==SQLITE_OK ){
2930 rc = findLeafNode(pRtree, iDelete, &pLeaf, 0);
2933 #ifdef CORRUPT_DB
2934 assert( pLeaf!=0 || rc!=SQLITE_OK || CORRUPT_DB );
2935 #endif
2937 /* Delete the cell in question from the leaf node. */
2938 if( rc==SQLITE_OK && pLeaf ){
2939 int rc2;
2940 rc = nodeRowidIndex(pRtree, pLeaf, iDelete, &iCell);
2941 if( rc==SQLITE_OK ){
2942 rc = deleteCell(pRtree, pLeaf, iCell, 0);
2944 rc2 = nodeRelease(pRtree, pLeaf);
2945 if( rc==SQLITE_OK ){
2946 rc = rc2;
2950 /* Delete the corresponding entry in the <rtree>_rowid table. */
2951 if( rc==SQLITE_OK ){
2952 sqlite3_bind_int64(pRtree->pDeleteRowid, 1, iDelete);
2953 sqlite3_step(pRtree->pDeleteRowid);
2954 rc = sqlite3_reset(pRtree->pDeleteRowid);
2957 /* Check if the root node now has exactly one child. If so, remove
2958 ** it, schedule the contents of the child for reinsertion and
2959 ** reduce the tree height by one.
2961 ** This is equivalent to copying the contents of the child into
2962 ** the root node (the operation that Gutman's paper says to perform
2963 ** in this scenario).
2965 if( rc==SQLITE_OK && pRtree->iDepth>0 && NCELL(pRoot)==1 ){
2966 int rc2;
2967 RtreeNode *pChild = 0;
2968 i64 iChild = nodeGetRowid(pRtree, pRoot, 0);
2969 rc = nodeAcquire(pRtree, iChild, pRoot, &pChild); /* tag-20210916a */
2970 if( rc==SQLITE_OK ){
2971 rc = removeNode(pRtree, pChild, pRtree->iDepth-1);
2973 rc2 = nodeRelease(pRtree, pChild);
2974 if( rc==SQLITE_OK ) rc = rc2;
2975 if( rc==SQLITE_OK ){
2976 pRtree->iDepth--;
2977 writeInt16(pRoot->zData, pRtree->iDepth);
2978 pRoot->isDirty = 1;
2982 /* Re-insert the contents of any underfull nodes removed from the tree. */
2983 for(pLeaf=pRtree->pDeleted; pLeaf; pLeaf=pRtree->pDeleted){
2984 if( rc==SQLITE_OK ){
2985 rc = reinsertNodeContent(pRtree, pLeaf);
2987 pRtree->pDeleted = pLeaf->pNext;
2988 pRtree->nNodeRef--;
2989 sqlite3_free(pLeaf);
2992 /* Release the reference to the root node. */
2993 if( rc==SQLITE_OK ){
2994 rc = nodeRelease(pRtree, pRoot);
2995 }else{
2996 nodeRelease(pRtree, pRoot);
2999 return rc;
3003 ** Rounding constants for float->double conversion.
3005 #define RNDTOWARDS (1.0 - 1.0/8388608.0) /* Round towards zero */
3006 #define RNDAWAY (1.0 + 1.0/8388608.0) /* Round away from zero */
3008 #if !defined(SQLITE_RTREE_INT_ONLY)
3010 ** Convert an sqlite3_value into an RtreeValue (presumably a float)
3011 ** while taking care to round toward negative or positive, respectively.
3013 static RtreeValue rtreeValueDown(sqlite3_value *v){
3014 double d = sqlite3_value_double(v);
3015 float f = (float)d;
3016 if( f>d ){
3017 f = (float)(d*(d<0 ? RNDAWAY : RNDTOWARDS));
3019 return f;
3021 static RtreeValue rtreeValueUp(sqlite3_value *v){
3022 double d = sqlite3_value_double(v);
3023 float f = (float)d;
3024 if( f<d ){
3025 f = (float)(d*(d<0 ? RNDTOWARDS : RNDAWAY));
3027 return f;
3029 #endif /* !defined(SQLITE_RTREE_INT_ONLY) */
3032 ** A constraint has failed while inserting a row into an rtree table.
3033 ** Assuming no OOM error occurs, this function sets the error message
3034 ** (at pRtree->base.zErrMsg) to an appropriate value and returns
3035 ** SQLITE_CONSTRAINT.
3037 ** Parameter iCol is the index of the leftmost column involved in the
3038 ** constraint failure. If it is 0, then the constraint that failed is
3039 ** the unique constraint on the id column. Otherwise, it is the rtree
3040 ** (c1<=c2) constraint on columns iCol and iCol+1 that has failed.
3042 ** If an OOM occurs, SQLITE_NOMEM is returned instead of SQLITE_CONSTRAINT.
3044 static int rtreeConstraintError(Rtree *pRtree, int iCol){
3045 sqlite3_stmt *pStmt = 0;
3046 char *zSql;
3047 int rc;
3049 assert( iCol==0 || iCol%2 );
3050 zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", pRtree->zDb, pRtree->zName);
3051 if( zSql ){
3052 rc = sqlite3_prepare_v2(pRtree->db, zSql, -1, &pStmt, 0);
3053 }else{
3054 rc = SQLITE_NOMEM;
3056 sqlite3_free(zSql);
3058 if( rc==SQLITE_OK ){
3059 if( iCol==0 ){
3060 const char *zCol = sqlite3_column_name(pStmt, 0);
3061 pRtree->base.zErrMsg = sqlite3_mprintf(
3062 "UNIQUE constraint failed: %s.%s", pRtree->zName, zCol
3064 }else{
3065 const char *zCol1 = sqlite3_column_name(pStmt, iCol);
3066 const char *zCol2 = sqlite3_column_name(pStmt, iCol+1);
3067 pRtree->base.zErrMsg = sqlite3_mprintf(
3068 "rtree constraint failed: %s.(%s<=%s)", pRtree->zName, zCol1, zCol2
3073 sqlite3_finalize(pStmt);
3074 return (rc==SQLITE_OK ? SQLITE_CONSTRAINT : rc);
3080 ** The xUpdate method for rtree module virtual tables.
3082 static int rtreeUpdate(
3083 sqlite3_vtab *pVtab,
3084 int nData,
3085 sqlite3_value **aData,
3086 sqlite_int64 *pRowid
3088 Rtree *pRtree = (Rtree *)pVtab;
3089 int rc = SQLITE_OK;
3090 RtreeCell cell; /* New cell to insert if nData>1 */
3091 int bHaveRowid = 0; /* Set to 1 after new rowid is determined */
3093 if( pRtree->nNodeRef ){
3094 /* Unable to write to the btree while another cursor is reading from it,
3095 ** since the write might do a rebalance which would disrupt the read
3096 ** cursor. */
3097 return SQLITE_LOCKED_VTAB;
3099 rtreeReference(pRtree);
3100 assert(nData>=1);
3102 memset(&cell, 0, sizeof(cell));
3104 /* Constraint handling. A write operation on an r-tree table may return
3105 ** SQLITE_CONSTRAINT for two reasons:
3107 ** 1. A duplicate rowid value, or
3108 ** 2. The supplied data violates the "x2>=x1" constraint.
3110 ** In the first case, if the conflict-handling mode is REPLACE, then
3111 ** the conflicting row can be removed before proceeding. In the second
3112 ** case, SQLITE_CONSTRAINT must be returned regardless of the
3113 ** conflict-handling mode specified by the user.
3115 if( nData>1 ){
3116 int ii;
3117 int nn = nData - 4;
3119 if( nn > pRtree->nDim2 ) nn = pRtree->nDim2;
3120 /* Populate the cell.aCoord[] array. The first coordinate is aData[3].
3122 ** NB: nData can only be less than nDim*2+3 if the rtree is mis-declared
3123 ** with "column" that are interpreted as table constraints.
3124 ** Example: CREATE VIRTUAL TABLE bad USING rtree(x,y,CHECK(y>5));
3125 ** This problem was discovered after years of use, so we silently ignore
3126 ** these kinds of misdeclared tables to avoid breaking any legacy.
3129 #ifndef SQLITE_RTREE_INT_ONLY
3130 if( pRtree->eCoordType==RTREE_COORD_REAL32 ){
3131 for(ii=0; ii<nn; ii+=2){
3132 cell.aCoord[ii].f = rtreeValueDown(aData[ii+3]);
3133 cell.aCoord[ii+1].f = rtreeValueUp(aData[ii+4]);
3134 if( cell.aCoord[ii].f>cell.aCoord[ii+1].f ){
3135 rc = rtreeConstraintError(pRtree, ii+1);
3136 goto constraint;
3139 }else
3140 #endif
3142 for(ii=0; ii<nn; ii+=2){
3143 cell.aCoord[ii].i = sqlite3_value_int(aData[ii+3]);
3144 cell.aCoord[ii+1].i = sqlite3_value_int(aData[ii+4]);
3145 if( cell.aCoord[ii].i>cell.aCoord[ii+1].i ){
3146 rc = rtreeConstraintError(pRtree, ii+1);
3147 goto constraint;
3152 /* If a rowid value was supplied, check if it is already present in
3153 ** the table. If so, the constraint has failed. */
3154 if( sqlite3_value_type(aData[2])!=SQLITE_NULL ){
3155 cell.iRowid = sqlite3_value_int64(aData[2]);
3156 if( sqlite3_value_type(aData[0])==SQLITE_NULL
3157 || sqlite3_value_int64(aData[0])!=cell.iRowid
3159 int steprc;
3160 sqlite3_bind_int64(pRtree->pReadRowid, 1, cell.iRowid);
3161 steprc = sqlite3_step(pRtree->pReadRowid);
3162 rc = sqlite3_reset(pRtree->pReadRowid);
3163 if( SQLITE_ROW==steprc ){
3164 if( sqlite3_vtab_on_conflict(pRtree->db)==SQLITE_REPLACE ){
3165 rc = rtreeDeleteRowid(pRtree, cell.iRowid);
3166 }else{
3167 rc = rtreeConstraintError(pRtree, 0);
3168 goto constraint;
3172 bHaveRowid = 1;
3176 /* If aData[0] is not an SQL NULL value, it is the rowid of a
3177 ** record to delete from the r-tree table. The following block does
3178 ** just that.
3180 if( sqlite3_value_type(aData[0])!=SQLITE_NULL ){
3181 rc = rtreeDeleteRowid(pRtree, sqlite3_value_int64(aData[0]));
3184 /* If the aData[] array contains more than one element, elements
3185 ** (aData[2]..aData[argc-1]) contain a new record to insert into
3186 ** the r-tree structure.
3188 if( rc==SQLITE_OK && nData>1 ){
3189 /* Insert the new record into the r-tree */
3190 RtreeNode *pLeaf = 0;
3192 /* Figure out the rowid of the new row. */
3193 if( bHaveRowid==0 ){
3194 rc = rtreeNewRowid(pRtree, &cell.iRowid);
3196 *pRowid = cell.iRowid;
3198 if( rc==SQLITE_OK ){
3199 rc = ChooseLeaf(pRtree, &cell, 0, &pLeaf);
3201 if( rc==SQLITE_OK ){
3202 int rc2;
3203 rc = rtreeInsertCell(pRtree, pLeaf, &cell, 0);
3204 rc2 = nodeRelease(pRtree, pLeaf);
3205 if( rc==SQLITE_OK ){
3206 rc = rc2;
3209 if( rc==SQLITE_OK && pRtree->nAux ){
3210 sqlite3_stmt *pUp = pRtree->pWriteAux;
3211 int jj;
3212 sqlite3_bind_int64(pUp, 1, *pRowid);
3213 for(jj=0; jj<pRtree->nAux; jj++){
3214 sqlite3_bind_value(pUp, jj+2, aData[pRtree->nDim2+3+jj]);
3216 sqlite3_step(pUp);
3217 rc = sqlite3_reset(pUp);
3221 constraint:
3222 rtreeRelease(pRtree);
3223 return rc;
3227 ** Called when a transaction starts.
3229 static int rtreeBeginTransaction(sqlite3_vtab *pVtab){
3230 Rtree *pRtree = (Rtree *)pVtab;
3231 assert( pRtree->inWrTrans==0 );
3232 pRtree->inWrTrans = 1;
3233 return SQLITE_OK;
3237 ** Called when a transaction completes (either by COMMIT or ROLLBACK).
3238 ** The sqlite3_blob object should be released at this point.
3240 static int rtreeEndTransaction(sqlite3_vtab *pVtab){
3241 Rtree *pRtree = (Rtree *)pVtab;
3242 pRtree->inWrTrans = 0;
3243 nodeBlobReset(pRtree);
3244 return SQLITE_OK;
3246 static int rtreeRollback(sqlite3_vtab *pVtab){
3247 return rtreeEndTransaction(pVtab);
3251 ** The xRename method for rtree module virtual tables.
3253 static int rtreeRename(sqlite3_vtab *pVtab, const char *zNewName){
3254 Rtree *pRtree = (Rtree *)pVtab;
3255 int rc = SQLITE_NOMEM;
3256 char *zSql = sqlite3_mprintf(
3257 "ALTER TABLE %Q.'%q_node' RENAME TO \"%w_node\";"
3258 "ALTER TABLE %Q.'%q_parent' RENAME TO \"%w_parent\";"
3259 "ALTER TABLE %Q.'%q_rowid' RENAME TO \"%w_rowid\";"
3260 , pRtree->zDb, pRtree->zName, zNewName
3261 , pRtree->zDb, pRtree->zName, zNewName
3262 , pRtree->zDb, pRtree->zName, zNewName
3264 if( zSql ){
3265 nodeBlobReset(pRtree);
3266 rc = sqlite3_exec(pRtree->db, zSql, 0, 0, 0);
3267 sqlite3_free(zSql);
3269 return rc;
3273 ** The xSavepoint method.
3275 ** This module does not need to do anything to support savepoints. However,
3276 ** it uses this hook to close any open blob handle. This is done because a
3277 ** DROP TABLE command - which fortunately always opens a savepoint - cannot
3278 ** succeed if there are any open blob handles. i.e. if the blob handle were
3279 ** not closed here, the following would fail:
3281 ** BEGIN;
3282 ** INSERT INTO rtree...
3283 ** DROP TABLE <tablename>; -- Would fail with SQLITE_LOCKED
3284 ** COMMIT;
3286 static int rtreeSavepoint(sqlite3_vtab *pVtab, int iSavepoint){
3287 Rtree *pRtree = (Rtree *)pVtab;
3288 u8 iwt = pRtree->inWrTrans;
3289 UNUSED_PARAMETER(iSavepoint);
3290 pRtree->inWrTrans = 0;
3291 nodeBlobReset(pRtree);
3292 pRtree->inWrTrans = iwt;
3293 return SQLITE_OK;
3297 ** This function populates the pRtree->nRowEst variable with an estimate
3298 ** of the number of rows in the virtual table. If possible, this is based
3299 ** on sqlite_stat1 data. Otherwise, use RTREE_DEFAULT_ROWEST.
3301 static int rtreeQueryStat1(sqlite3 *db, Rtree *pRtree){
3302 const char *zFmt = "SELECT stat FROM %Q.sqlite_stat1 WHERE tbl = '%q_rowid'";
3303 char *zSql;
3304 sqlite3_stmt *p;
3305 int rc;
3306 i64 nRow = RTREE_MIN_ROWEST;
3308 rc = sqlite3_table_column_metadata(
3309 db, pRtree->zDb, "sqlite_stat1",0,0,0,0,0,0
3311 if( rc!=SQLITE_OK ){
3312 pRtree->nRowEst = RTREE_DEFAULT_ROWEST;
3313 return rc==SQLITE_ERROR ? SQLITE_OK : rc;
3315 zSql = sqlite3_mprintf(zFmt, pRtree->zDb, pRtree->zName);
3316 if( zSql==0 ){
3317 rc = SQLITE_NOMEM;
3318 }else{
3319 rc = sqlite3_prepare_v2(db, zSql, -1, &p, 0);
3320 if( rc==SQLITE_OK ){
3321 if( sqlite3_step(p)==SQLITE_ROW ) nRow = sqlite3_column_int64(p, 0);
3322 rc = sqlite3_finalize(p);
3324 sqlite3_free(zSql);
3326 pRtree->nRowEst = MAX(nRow, RTREE_MIN_ROWEST);
3327 return rc;
3332 ** Return true if zName is the extension on one of the shadow tables used
3333 ** by this module.
3335 static int rtreeShadowName(const char *zName){
3336 static const char *azName[] = {
3337 "node", "parent", "rowid"
3339 unsigned int i;
3340 for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
3341 if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
3343 return 0;
3346 /* Forward declaration */
3347 static int rtreeIntegrity(sqlite3_vtab*, const char*, const char*, int, char**);
3349 static sqlite3_module rtreeModule = {
3350 4, /* iVersion */
3351 rtreeCreate, /* xCreate - create a table */
3352 rtreeConnect, /* xConnect - connect to an existing table */
3353 rtreeBestIndex, /* xBestIndex - Determine search strategy */
3354 rtreeDisconnect, /* xDisconnect - Disconnect from a table */
3355 rtreeDestroy, /* xDestroy - Drop a table */
3356 rtreeOpen, /* xOpen - open a cursor */
3357 rtreeClose, /* xClose - close a cursor */
3358 rtreeFilter, /* xFilter - configure scan constraints */
3359 rtreeNext, /* xNext - advance a cursor */
3360 rtreeEof, /* xEof */
3361 rtreeColumn, /* xColumn - read data */
3362 rtreeRowid, /* xRowid - read data */
3363 rtreeUpdate, /* xUpdate - write data */
3364 rtreeBeginTransaction, /* xBegin - begin transaction */
3365 rtreeEndTransaction, /* xSync - sync transaction */
3366 rtreeEndTransaction, /* xCommit - commit transaction */
3367 rtreeRollback, /* xRollback - rollback transaction */
3368 0, /* xFindFunction - function overloading */
3369 rtreeRename, /* xRename - rename the table */
3370 rtreeSavepoint, /* xSavepoint */
3371 0, /* xRelease */
3372 0, /* xRollbackTo */
3373 rtreeShadowName, /* xShadowName */
3374 rtreeIntegrity /* xIntegrity */
3377 static int rtreeSqlInit(
3378 Rtree *pRtree,
3379 sqlite3 *db,
3380 const char *zDb,
3381 const char *zPrefix,
3382 int isCreate
3384 int rc = SQLITE_OK;
3386 #define N_STATEMENT 8
3387 static const char *azSql[N_STATEMENT] = {
3388 /* Write the xxx_node table */
3389 "INSERT OR REPLACE INTO '%q'.'%q_node' VALUES(?1, ?2)",
3390 "DELETE FROM '%q'.'%q_node' WHERE nodeno = ?1",
3392 /* Read and write the xxx_rowid table */
3393 "SELECT nodeno FROM '%q'.'%q_rowid' WHERE rowid = ?1",
3394 "INSERT OR REPLACE INTO '%q'.'%q_rowid' VALUES(?1, ?2)",
3395 "DELETE FROM '%q'.'%q_rowid' WHERE rowid = ?1",
3397 /* Read and write the xxx_parent table */
3398 "SELECT parentnode FROM '%q'.'%q_parent' WHERE nodeno = ?1",
3399 "INSERT OR REPLACE INTO '%q'.'%q_parent' VALUES(?1, ?2)",
3400 "DELETE FROM '%q'.'%q_parent' WHERE nodeno = ?1"
3402 sqlite3_stmt **appStmt[N_STATEMENT];
3403 int i;
3404 const int f = SQLITE_PREPARE_PERSISTENT|SQLITE_PREPARE_NO_VTAB;
3406 pRtree->db = db;
3408 if( isCreate ){
3409 char *zCreate;
3410 sqlite3_str *p = sqlite3_str_new(db);
3411 int ii;
3412 sqlite3_str_appendf(p,
3413 "CREATE TABLE \"%w\".\"%w_rowid\"(rowid INTEGER PRIMARY KEY,nodeno",
3414 zDb, zPrefix);
3415 for(ii=0; ii<pRtree->nAux; ii++){
3416 sqlite3_str_appendf(p,",a%d",ii);
3418 sqlite3_str_appendf(p,
3419 ");CREATE TABLE \"%w\".\"%w_node\"(nodeno INTEGER PRIMARY KEY,data);",
3420 zDb, zPrefix);
3421 sqlite3_str_appendf(p,
3422 "CREATE TABLE \"%w\".\"%w_parent\"(nodeno INTEGER PRIMARY KEY,parentnode);",
3423 zDb, zPrefix);
3424 sqlite3_str_appendf(p,
3425 "INSERT INTO \"%w\".\"%w_node\"VALUES(1,zeroblob(%d))",
3426 zDb, zPrefix, pRtree->iNodeSize);
3427 zCreate = sqlite3_str_finish(p);
3428 if( !zCreate ){
3429 return SQLITE_NOMEM;
3431 rc = sqlite3_exec(db, zCreate, 0, 0, 0);
3432 sqlite3_free(zCreate);
3433 if( rc!=SQLITE_OK ){
3434 return rc;
3438 appStmt[0] = &pRtree->pWriteNode;
3439 appStmt[1] = &pRtree->pDeleteNode;
3440 appStmt[2] = &pRtree->pReadRowid;
3441 appStmt[3] = &pRtree->pWriteRowid;
3442 appStmt[4] = &pRtree->pDeleteRowid;
3443 appStmt[5] = &pRtree->pReadParent;
3444 appStmt[6] = &pRtree->pWriteParent;
3445 appStmt[7] = &pRtree->pDeleteParent;
3447 rc = rtreeQueryStat1(db, pRtree);
3448 for(i=0; i<N_STATEMENT && rc==SQLITE_OK; i++){
3449 char *zSql;
3450 const char *zFormat;
3451 if( i!=3 || pRtree->nAux==0 ){
3452 zFormat = azSql[i];
3453 }else {
3454 /* An UPSERT is very slightly slower than REPLACE, but it is needed
3455 ** if there are auxiliary columns */
3456 zFormat = "INSERT INTO\"%w\".\"%w_rowid\"(rowid,nodeno)VALUES(?1,?2)"
3457 "ON CONFLICT(rowid)DO UPDATE SET nodeno=excluded.nodeno";
3459 zSql = sqlite3_mprintf(zFormat, zDb, zPrefix);
3460 if( zSql ){
3461 rc = sqlite3_prepare_v3(db, zSql, -1, f, appStmt[i], 0);
3462 }else{
3463 rc = SQLITE_NOMEM;
3465 sqlite3_free(zSql);
3467 if( pRtree->nAux && rc!=SQLITE_NOMEM ){
3468 pRtree->zReadAuxSql = sqlite3_mprintf(
3469 "SELECT * FROM \"%w\".\"%w_rowid\" WHERE rowid=?1",
3470 zDb, zPrefix);
3471 if( pRtree->zReadAuxSql==0 ){
3472 rc = SQLITE_NOMEM;
3473 }else{
3474 sqlite3_str *p = sqlite3_str_new(db);
3475 int ii;
3476 char *zSql;
3477 sqlite3_str_appendf(p, "UPDATE \"%w\".\"%w_rowid\"SET ", zDb, zPrefix);
3478 for(ii=0; ii<pRtree->nAux; ii++){
3479 if( ii ) sqlite3_str_append(p, ",", 1);
3480 #ifdef SQLITE_ENABLE_GEOPOLY
3481 if( ii<pRtree->nAuxNotNull ){
3482 sqlite3_str_appendf(p,"a%d=coalesce(?%d,a%d)",ii,ii+2,ii);
3483 }else
3484 #endif
3486 sqlite3_str_appendf(p,"a%d=?%d",ii,ii+2);
3489 sqlite3_str_appendf(p, " WHERE rowid=?1");
3490 zSql = sqlite3_str_finish(p);
3491 if( zSql==0 ){
3492 rc = SQLITE_NOMEM;
3493 }else{
3494 rc = sqlite3_prepare_v3(db, zSql, -1, f, &pRtree->pWriteAux, 0);
3495 sqlite3_free(zSql);
3500 return rc;
3504 ** The second argument to this function contains the text of an SQL statement
3505 ** that returns a single integer value. The statement is compiled and executed
3506 ** using database connection db. If successful, the integer value returned
3507 ** is written to *piVal and SQLITE_OK returned. Otherwise, an SQLite error
3508 ** code is returned and the value of *piVal after returning is not defined.
3510 static int getIntFromStmt(sqlite3 *db, const char *zSql, int *piVal){
3511 int rc = SQLITE_NOMEM;
3512 if( zSql ){
3513 sqlite3_stmt *pStmt = 0;
3514 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, 0);
3515 if( rc==SQLITE_OK ){
3516 if( SQLITE_ROW==sqlite3_step(pStmt) ){
3517 *piVal = sqlite3_column_int(pStmt, 0);
3519 rc = sqlite3_finalize(pStmt);
3522 return rc;
3526 ** This function is called from within the xConnect() or xCreate() method to
3527 ** determine the node-size used by the rtree table being created or connected
3528 ** to. If successful, pRtree->iNodeSize is populated and SQLITE_OK returned.
3529 ** Otherwise, an SQLite error code is returned.
3531 ** If this function is being called as part of an xConnect(), then the rtree
3532 ** table already exists. In this case the node-size is determined by inspecting
3533 ** the root node of the tree.
3535 ** Otherwise, for an xCreate(), use 64 bytes less than the database page-size.
3536 ** This ensures that each node is stored on a single database page. If the
3537 ** database page-size is so large that more than RTREE_MAXCELLS entries
3538 ** would fit in a single node, use a smaller node-size.
3540 static int getNodeSize(
3541 sqlite3 *db, /* Database handle */
3542 Rtree *pRtree, /* Rtree handle */
3543 int isCreate, /* True for xCreate, false for xConnect */
3544 char **pzErr /* OUT: Error message, if any */
3546 int rc;
3547 char *zSql;
3548 if( isCreate ){
3549 int iPageSize = 0;
3550 zSql = sqlite3_mprintf("PRAGMA %Q.page_size", pRtree->zDb);
3551 rc = getIntFromStmt(db, zSql, &iPageSize);
3552 if( rc==SQLITE_OK ){
3553 pRtree->iNodeSize = iPageSize-64;
3554 if( (4+pRtree->nBytesPerCell*RTREE_MAXCELLS)<pRtree->iNodeSize ){
3555 pRtree->iNodeSize = 4+pRtree->nBytesPerCell*RTREE_MAXCELLS;
3557 }else{
3558 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
3560 }else{
3561 zSql = sqlite3_mprintf(
3562 "SELECT length(data) FROM '%q'.'%q_node' WHERE nodeno = 1",
3563 pRtree->zDb, pRtree->zName
3565 rc = getIntFromStmt(db, zSql, &pRtree->iNodeSize);
3566 if( rc!=SQLITE_OK ){
3567 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
3568 }else if( pRtree->iNodeSize<(512-64) ){
3569 rc = SQLITE_CORRUPT_VTAB;
3570 RTREE_IS_CORRUPT(pRtree);
3571 *pzErr = sqlite3_mprintf("undersize RTree blobs in \"%q_node\"",
3572 pRtree->zName);
3576 sqlite3_free(zSql);
3577 return rc;
3581 ** Return the length of a token
3583 static int rtreeTokenLength(const char *z){
3584 int dummy = 0;
3585 return sqlite3GetToken((const unsigned char*)z,&dummy);
3589 ** This function is the implementation of both the xConnect and xCreate
3590 ** methods of the r-tree virtual table.
3592 ** argv[0] -> module name
3593 ** argv[1] -> database name
3594 ** argv[2] -> table name
3595 ** argv[...] -> column names...
3597 static int rtreeInit(
3598 sqlite3 *db, /* Database connection */
3599 void *pAux, /* One of the RTREE_COORD_* constants */
3600 int argc, const char *const*argv, /* Parameters to CREATE TABLE statement */
3601 sqlite3_vtab **ppVtab, /* OUT: New virtual table */
3602 char **pzErr, /* OUT: Error message, if any */
3603 int isCreate /* True for xCreate, false for xConnect */
3605 int rc = SQLITE_OK;
3606 Rtree *pRtree;
3607 int nDb; /* Length of string argv[1] */
3608 int nName; /* Length of string argv[2] */
3609 int eCoordType = (pAux ? RTREE_COORD_INT32 : RTREE_COORD_REAL32);
3610 sqlite3_str *pSql;
3611 char *zSql;
3612 int ii = 4;
3613 int iErr;
3615 const char *aErrMsg[] = {
3616 0, /* 0 */
3617 "Wrong number of columns for an rtree table", /* 1 */
3618 "Too few columns for an rtree table", /* 2 */
3619 "Too many columns for an rtree table", /* 3 */
3620 "Auxiliary rtree columns must be last" /* 4 */
3623 assert( RTREE_MAX_AUX_COLUMN<256 ); /* Aux columns counted by a u8 */
3624 if( argc<6 || argc>RTREE_MAX_AUX_COLUMN+3 ){
3625 *pzErr = sqlite3_mprintf("%s", aErrMsg[2 + (argc>=6)]);
3626 return SQLITE_ERROR;
3629 sqlite3_vtab_config(db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
3630 sqlite3_vtab_config(db, SQLITE_VTAB_INNOCUOUS);
3633 /* Allocate the sqlite3_vtab structure */
3634 nDb = (int)strlen(argv[1]);
3635 nName = (int)strlen(argv[2]);
3636 pRtree = (Rtree *)sqlite3_malloc64(sizeof(Rtree)+nDb+nName*2+8);
3637 if( !pRtree ){
3638 return SQLITE_NOMEM;
3640 memset(pRtree, 0, sizeof(Rtree)+nDb+nName*2+8);
3641 pRtree->nBusy = 1;
3642 pRtree->base.pModule = &rtreeModule;
3643 pRtree->zDb = (char *)&pRtree[1];
3644 pRtree->zName = &pRtree->zDb[nDb+1];
3645 pRtree->zNodeName = &pRtree->zName[nName+1];
3646 pRtree->eCoordType = (u8)eCoordType;
3647 memcpy(pRtree->zDb, argv[1], nDb);
3648 memcpy(pRtree->zName, argv[2], nName);
3649 memcpy(pRtree->zNodeName, argv[2], nName);
3650 memcpy(&pRtree->zNodeName[nName], "_node", 6);
3653 /* Create/Connect to the underlying relational database schema. If
3654 ** that is successful, call sqlite3_declare_vtab() to configure
3655 ** the r-tree table schema.
3657 pSql = sqlite3_str_new(db);
3658 sqlite3_str_appendf(pSql, "CREATE TABLE x(%.*s INT",
3659 rtreeTokenLength(argv[3]), argv[3]);
3660 for(ii=4; ii<argc; ii++){
3661 const char *zArg = argv[ii];
3662 if( zArg[0]=='+' ){
3663 pRtree->nAux++;
3664 sqlite3_str_appendf(pSql, ",%.*s", rtreeTokenLength(zArg+1), zArg+1);
3665 }else if( pRtree->nAux>0 ){
3666 break;
3667 }else{
3668 static const char *azFormat[] = {",%.*s REAL", ",%.*s INT"};
3669 pRtree->nDim2++;
3670 sqlite3_str_appendf(pSql, azFormat[eCoordType],
3671 rtreeTokenLength(zArg), zArg);
3674 sqlite3_str_appendf(pSql, ");");
3675 zSql = sqlite3_str_finish(pSql);
3676 if( !zSql ){
3677 rc = SQLITE_NOMEM;
3678 }else if( ii<argc ){
3679 *pzErr = sqlite3_mprintf("%s", aErrMsg[4]);
3680 rc = SQLITE_ERROR;
3681 }else if( SQLITE_OK!=(rc = sqlite3_declare_vtab(db, zSql)) ){
3682 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
3684 sqlite3_free(zSql);
3685 if( rc ) goto rtreeInit_fail;
3686 pRtree->nDim = pRtree->nDim2/2;
3687 if( pRtree->nDim<1 ){
3688 iErr = 2;
3689 }else if( pRtree->nDim2>RTREE_MAX_DIMENSIONS*2 ){
3690 iErr = 3;
3691 }else if( pRtree->nDim2 % 2 ){
3692 iErr = 1;
3693 }else{
3694 iErr = 0;
3696 if( iErr ){
3697 *pzErr = sqlite3_mprintf("%s", aErrMsg[iErr]);
3698 goto rtreeInit_fail;
3700 pRtree->nBytesPerCell = 8 + pRtree->nDim2*4;
3702 /* Figure out the node size to use. */
3703 rc = getNodeSize(db, pRtree, isCreate, pzErr);
3704 if( rc ) goto rtreeInit_fail;
3705 rc = rtreeSqlInit(pRtree, db, argv[1], argv[2], isCreate);
3706 if( rc ){
3707 *pzErr = sqlite3_mprintf("%s", sqlite3_errmsg(db));
3708 goto rtreeInit_fail;
3711 *ppVtab = (sqlite3_vtab *)pRtree;
3712 return SQLITE_OK;
3714 rtreeInit_fail:
3715 if( rc==SQLITE_OK ) rc = SQLITE_ERROR;
3716 assert( *ppVtab==0 );
3717 assert( pRtree->nBusy==1 );
3718 rtreeRelease(pRtree);
3719 return rc;
3724 ** Implementation of a scalar function that decodes r-tree nodes to
3725 ** human readable strings. This can be used for debugging and analysis.
3727 ** The scalar function takes two arguments: (1) the number of dimensions
3728 ** to the rtree (between 1 and 5, inclusive) and (2) a blob of data containing
3729 ** an r-tree node. For a two-dimensional r-tree structure called "rt", to
3730 ** deserialize all nodes, a statement like:
3732 ** SELECT rtreenode(2, data) FROM rt_node;
3734 ** The human readable string takes the form of a Tcl list with one
3735 ** entry for each cell in the r-tree node. Each entry is itself a
3736 ** list, containing the 8-byte rowid/pageno followed by the
3737 ** <num-dimension>*2 coordinates.
3739 static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
3740 RtreeNode node;
3741 Rtree tree;
3742 int ii;
3743 int nData;
3744 int errCode;
3745 sqlite3_str *pOut;
3747 UNUSED_PARAMETER(nArg);
3748 memset(&node, 0, sizeof(RtreeNode));
3749 memset(&tree, 0, sizeof(Rtree));
3750 tree.nDim = (u8)sqlite3_value_int(apArg[0]);
3751 if( tree.nDim<1 || tree.nDim>5 ) return;
3752 tree.nDim2 = tree.nDim*2;
3753 tree.nBytesPerCell = 8 + 8 * tree.nDim;
3754 node.zData = (u8 *)sqlite3_value_blob(apArg[1]);
3755 if( node.zData==0 ) return;
3756 nData = sqlite3_value_bytes(apArg[1]);
3757 if( nData<4 ) return;
3758 if( nData<NCELL(&node)*tree.nBytesPerCell ) return;
3760 pOut = sqlite3_str_new(0);
3761 for(ii=0; ii<NCELL(&node); ii++){
3762 RtreeCell cell;
3763 int jj;
3765 nodeGetCell(&tree, &node, ii, &cell);
3766 if( ii>0 ) sqlite3_str_append(pOut, " ", 1);
3767 sqlite3_str_appendf(pOut, "{%lld", cell.iRowid);
3768 for(jj=0; jj<tree.nDim2; jj++){
3769 #ifndef SQLITE_RTREE_INT_ONLY
3770 sqlite3_str_appendf(pOut, " %g", (double)cell.aCoord[jj].f);
3771 #else
3772 sqlite3_str_appendf(pOut, " %d", cell.aCoord[jj].i);
3773 #endif
3775 sqlite3_str_append(pOut, "}", 1);
3777 errCode = sqlite3_str_errcode(pOut);
3778 sqlite3_result_text(ctx, sqlite3_str_finish(pOut), -1, sqlite3_free);
3779 sqlite3_result_error_code(ctx, errCode);
3782 /* This routine implements an SQL function that returns the "depth" parameter
3783 ** from the front of a blob that is an r-tree node. For example:
3785 ** SELECT rtreedepth(data) FROM rt_node WHERE nodeno=1;
3787 ** The depth value is 0 for all nodes other than the root node, and the root
3788 ** node always has nodeno=1, so the example above is the primary use for this
3789 ** routine. This routine is intended for testing and analysis only.
3791 static void rtreedepth(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
3792 UNUSED_PARAMETER(nArg);
3793 if( sqlite3_value_type(apArg[0])!=SQLITE_BLOB
3794 || sqlite3_value_bytes(apArg[0])<2
3797 sqlite3_result_error(ctx, "Invalid argument to rtreedepth()", -1);
3798 }else{
3799 u8 *zBlob = (u8 *)sqlite3_value_blob(apArg[0]);
3800 if( zBlob ){
3801 sqlite3_result_int(ctx, readInt16(zBlob));
3802 }else{
3803 sqlite3_result_error_nomem(ctx);
3809 ** Context object passed between the various routines that make up the
3810 ** implementation of integrity-check function rtreecheck().
3812 typedef struct RtreeCheck RtreeCheck;
3813 struct RtreeCheck {
3814 sqlite3 *db; /* Database handle */
3815 const char *zDb; /* Database containing rtree table */
3816 const char *zTab; /* Name of rtree table */
3817 int bInt; /* True for rtree_i32 table */
3818 int nDim; /* Number of dimensions for this rtree tbl */
3819 sqlite3_stmt *pGetNode; /* Statement used to retrieve nodes */
3820 sqlite3_stmt *aCheckMapping[2]; /* Statements to query %_parent/%_rowid */
3821 int nLeaf; /* Number of leaf cells in table */
3822 int nNonLeaf; /* Number of non-leaf cells in table */
3823 int rc; /* Return code */
3824 char *zReport; /* Message to report */
3825 int nErr; /* Number of lines in zReport */
3828 #define RTREE_CHECK_MAX_ERROR 100
3831 ** Reset SQL statement pStmt. If the sqlite3_reset() call returns an error,
3832 ** and RtreeCheck.rc==SQLITE_OK, set RtreeCheck.rc to the error code.
3834 static void rtreeCheckReset(RtreeCheck *pCheck, sqlite3_stmt *pStmt){
3835 int rc = sqlite3_reset(pStmt);
3836 if( pCheck->rc==SQLITE_OK ) pCheck->rc = rc;
3840 ** The second and subsequent arguments to this function are a format string
3841 ** and printf style arguments. This function formats the string and attempts
3842 ** to compile it as an SQL statement.
3844 ** If successful, a pointer to the new SQL statement is returned. Otherwise,
3845 ** NULL is returned and an error code left in RtreeCheck.rc.
3847 static sqlite3_stmt *rtreeCheckPrepare(
3848 RtreeCheck *pCheck, /* RtreeCheck object */
3849 const char *zFmt, ... /* Format string and trailing args */
3851 va_list ap;
3852 char *z;
3853 sqlite3_stmt *pRet = 0;
3855 va_start(ap, zFmt);
3856 z = sqlite3_vmprintf(zFmt, ap);
3858 if( pCheck->rc==SQLITE_OK ){
3859 if( z==0 ){
3860 pCheck->rc = SQLITE_NOMEM;
3861 }else{
3862 pCheck->rc = sqlite3_prepare_v2(pCheck->db, z, -1, &pRet, 0);
3866 sqlite3_free(z);
3867 va_end(ap);
3868 return pRet;
3872 ** The second and subsequent arguments to this function are a printf()
3873 ** style format string and arguments. This function formats the string and
3874 ** appends it to the report being accumuated in pCheck.
3876 static void rtreeCheckAppendMsg(RtreeCheck *pCheck, const char *zFmt, ...){
3877 va_list ap;
3878 va_start(ap, zFmt);
3879 if( pCheck->rc==SQLITE_OK && pCheck->nErr<RTREE_CHECK_MAX_ERROR ){
3880 char *z = sqlite3_vmprintf(zFmt, ap);
3881 if( z==0 ){
3882 pCheck->rc = SQLITE_NOMEM;
3883 }else{
3884 pCheck->zReport = sqlite3_mprintf("%z%s%z",
3885 pCheck->zReport, (pCheck->zReport ? "\n" : ""), z
3887 if( pCheck->zReport==0 ){
3888 pCheck->rc = SQLITE_NOMEM;
3891 pCheck->nErr++;
3893 va_end(ap);
3897 ** This function is a no-op if there is already an error code stored
3898 ** in the RtreeCheck object indicated by the first argument. NULL is
3899 ** returned in this case.
3901 ** Otherwise, the contents of rtree table node iNode are loaded from
3902 ** the database and copied into a buffer obtained from sqlite3_malloc().
3903 ** If no error occurs, a pointer to the buffer is returned and (*pnNode)
3904 ** is set to the size of the buffer in bytes.
3906 ** Or, if an error does occur, NULL is returned and an error code left
3907 ** in the RtreeCheck object. The final value of *pnNode is undefined in
3908 ** this case.
3910 static u8 *rtreeCheckGetNode(RtreeCheck *pCheck, i64 iNode, int *pnNode){
3911 u8 *pRet = 0; /* Return value */
3913 if( pCheck->rc==SQLITE_OK && pCheck->pGetNode==0 ){
3914 pCheck->pGetNode = rtreeCheckPrepare(pCheck,
3915 "SELECT data FROM %Q.'%q_node' WHERE nodeno=?",
3916 pCheck->zDb, pCheck->zTab
3920 if( pCheck->rc==SQLITE_OK ){
3921 sqlite3_bind_int64(pCheck->pGetNode, 1, iNode);
3922 if( sqlite3_step(pCheck->pGetNode)==SQLITE_ROW ){
3923 int nNode = sqlite3_column_bytes(pCheck->pGetNode, 0);
3924 const u8 *pNode = (const u8*)sqlite3_column_blob(pCheck->pGetNode, 0);
3925 pRet = sqlite3_malloc64(nNode);
3926 if( pRet==0 ){
3927 pCheck->rc = SQLITE_NOMEM;
3928 }else{
3929 memcpy(pRet, pNode, nNode);
3930 *pnNode = nNode;
3933 rtreeCheckReset(pCheck, pCheck->pGetNode);
3934 if( pCheck->rc==SQLITE_OK && pRet==0 ){
3935 rtreeCheckAppendMsg(pCheck, "Node %lld missing from database", iNode);
3939 return pRet;
3943 ** This function is used to check that the %_parent (if bLeaf==0) or %_rowid
3944 ** (if bLeaf==1) table contains a specified entry. The schemas of the
3945 ** two tables are:
3947 ** CREATE TABLE %_parent(nodeno INTEGER PRIMARY KEY, parentnode INTEGER)
3948 ** CREATE TABLE %_rowid(rowid INTEGER PRIMARY KEY, nodeno INTEGER, ...)
3950 ** In both cases, this function checks that there exists an entry with
3951 ** IPK value iKey and the second column set to iVal.
3954 static void rtreeCheckMapping(
3955 RtreeCheck *pCheck, /* RtreeCheck object */
3956 int bLeaf, /* True for a leaf cell, false for interior */
3957 i64 iKey, /* Key for mapping */
3958 i64 iVal /* Expected value for mapping */
3960 int rc;
3961 sqlite3_stmt *pStmt;
3962 const char *azSql[2] = {
3963 "SELECT parentnode FROM %Q.'%q_parent' WHERE nodeno=?1",
3964 "SELECT nodeno FROM %Q.'%q_rowid' WHERE rowid=?1"
3967 assert( bLeaf==0 || bLeaf==1 );
3968 if( pCheck->aCheckMapping[bLeaf]==0 ){
3969 pCheck->aCheckMapping[bLeaf] = rtreeCheckPrepare(pCheck,
3970 azSql[bLeaf], pCheck->zDb, pCheck->zTab
3973 if( pCheck->rc!=SQLITE_OK ) return;
3975 pStmt = pCheck->aCheckMapping[bLeaf];
3976 sqlite3_bind_int64(pStmt, 1, iKey);
3977 rc = sqlite3_step(pStmt);
3978 if( rc==SQLITE_DONE ){
3979 rtreeCheckAppendMsg(pCheck, "Mapping (%lld -> %lld) missing from %s table",
3980 iKey, iVal, (bLeaf ? "%_rowid" : "%_parent")
3982 }else if( rc==SQLITE_ROW ){
3983 i64 ii = sqlite3_column_int64(pStmt, 0);
3984 if( ii!=iVal ){
3985 rtreeCheckAppendMsg(pCheck,
3986 "Found (%lld -> %lld) in %s table, expected (%lld -> %lld)",
3987 iKey, ii, (bLeaf ? "%_rowid" : "%_parent"), iKey, iVal
3991 rtreeCheckReset(pCheck, pStmt);
3995 ** Argument pCell points to an array of coordinates stored on an rtree page.
3996 ** This function checks that the coordinates are internally consistent (no
3997 ** x1>x2 conditions) and adds an error message to the RtreeCheck object
3998 ** if they are not.
4000 ** Additionally, if pParent is not NULL, then it is assumed to point to
4001 ** the array of coordinates on the parent page that bound the page
4002 ** containing pCell. In this case it is also verified that the two
4003 ** sets of coordinates are mutually consistent and an error message added
4004 ** to the RtreeCheck object if they are not.
4006 static void rtreeCheckCellCoord(
4007 RtreeCheck *pCheck,
4008 i64 iNode, /* Node id to use in error messages */
4009 int iCell, /* Cell number to use in error messages */
4010 u8 *pCell, /* Pointer to cell coordinates */
4011 u8 *pParent /* Pointer to parent coordinates */
4013 RtreeCoord c1, c2;
4014 RtreeCoord p1, p2;
4015 int i;
4017 for(i=0; i<pCheck->nDim; i++){
4018 readCoord(&pCell[4*2*i], &c1);
4019 readCoord(&pCell[4*(2*i + 1)], &c2);
4021 /* printf("%e, %e\n", c1.u.f, c2.u.f); */
4022 if( pCheck->bInt ? c1.i>c2.i : c1.f>c2.f ){
4023 rtreeCheckAppendMsg(pCheck,
4024 "Dimension %d of cell %d on node %lld is corrupt", i, iCell, iNode
4028 if( pParent ){
4029 readCoord(&pParent[4*2*i], &p1);
4030 readCoord(&pParent[4*(2*i + 1)], &p2);
4032 if( (pCheck->bInt ? c1.i<p1.i : c1.f<p1.f)
4033 || (pCheck->bInt ? c2.i>p2.i : c2.f>p2.f)
4035 rtreeCheckAppendMsg(pCheck,
4036 "Dimension %d of cell %d on node %lld is corrupt relative to parent"
4037 , i, iCell, iNode
4045 ** Run rtreecheck() checks on node iNode, which is at depth iDepth within
4046 ** the r-tree structure. Argument aParent points to the array of coordinates
4047 ** that bound node iNode on the parent node.
4049 ** If any problems are discovered, an error message is appended to the
4050 ** report accumulated in the RtreeCheck object.
4052 static void rtreeCheckNode(
4053 RtreeCheck *pCheck,
4054 int iDepth, /* Depth of iNode (0==leaf) */
4055 u8 *aParent, /* Buffer containing parent coords */
4056 i64 iNode /* Node to check */
4058 u8 *aNode = 0;
4059 int nNode = 0;
4061 assert( iNode==1 || aParent!=0 );
4062 assert( pCheck->nDim>0 );
4064 aNode = rtreeCheckGetNode(pCheck, iNode, &nNode);
4065 if( aNode ){
4066 if( nNode<4 ){
4067 rtreeCheckAppendMsg(pCheck,
4068 "Node %lld is too small (%d bytes)", iNode, nNode
4070 }else{
4071 int nCell; /* Number of cells on page */
4072 int i; /* Used to iterate through cells */
4073 if( aParent==0 ){
4074 iDepth = readInt16(aNode);
4075 if( iDepth>RTREE_MAX_DEPTH ){
4076 rtreeCheckAppendMsg(pCheck, "Rtree depth out of range (%d)", iDepth);
4077 sqlite3_free(aNode);
4078 return;
4081 nCell = readInt16(&aNode[2]);
4082 if( (4 + nCell*(8 + pCheck->nDim*2*4))>nNode ){
4083 rtreeCheckAppendMsg(pCheck,
4084 "Node %lld is too small for cell count of %d (%d bytes)",
4085 iNode, nCell, nNode
4087 }else{
4088 for(i=0; i<nCell; i++){
4089 u8 *pCell = &aNode[4 + i*(8 + pCheck->nDim*2*4)];
4090 i64 iVal = readInt64(pCell);
4091 rtreeCheckCellCoord(pCheck, iNode, i, &pCell[8], aParent);
4093 if( iDepth>0 ){
4094 rtreeCheckMapping(pCheck, 0, iVal, iNode);
4095 rtreeCheckNode(pCheck, iDepth-1, &pCell[8], iVal);
4096 pCheck->nNonLeaf++;
4097 }else{
4098 rtreeCheckMapping(pCheck, 1, iVal, iNode);
4099 pCheck->nLeaf++;
4104 sqlite3_free(aNode);
4109 ** The second argument to this function must be either "_rowid" or
4110 ** "_parent". This function checks that the number of entries in the
4111 ** %_rowid or %_parent table is exactly nExpect. If not, it adds
4112 ** an error message to the report in the RtreeCheck object indicated
4113 ** by the first argument.
4115 static void rtreeCheckCount(RtreeCheck *pCheck, const char *zTbl, i64 nExpect){
4116 if( pCheck->rc==SQLITE_OK ){
4117 sqlite3_stmt *pCount;
4118 pCount = rtreeCheckPrepare(pCheck, "SELECT count(*) FROM %Q.'%q%s'",
4119 pCheck->zDb, pCheck->zTab, zTbl
4121 if( pCount ){
4122 if( sqlite3_step(pCount)==SQLITE_ROW ){
4123 i64 nActual = sqlite3_column_int64(pCount, 0);
4124 if( nActual!=nExpect ){
4125 rtreeCheckAppendMsg(pCheck, "Wrong number of entries in %%%s table"
4126 " - expected %lld, actual %lld" , zTbl, nExpect, nActual
4130 pCheck->rc = sqlite3_finalize(pCount);
4136 ** This function does the bulk of the work for the rtree integrity-check.
4137 ** It is called by rtreecheck(), which is the SQL function implementation.
4139 static int rtreeCheckTable(
4140 sqlite3 *db, /* Database handle to access db through */
4141 const char *zDb, /* Name of db ("main", "temp" etc.) */
4142 const char *zTab, /* Name of rtree table to check */
4143 char **pzReport /* OUT: sqlite3_malloc'd report text */
4145 RtreeCheck check; /* Common context for various routines */
4146 sqlite3_stmt *pStmt = 0; /* Used to find column count of rtree table */
4147 int nAux = 0; /* Number of extra columns. */
4149 /* Initialize the context object */
4150 memset(&check, 0, sizeof(check));
4151 check.db = db;
4152 check.zDb = zDb;
4153 check.zTab = zTab;
4155 /* Find the number of auxiliary columns */
4156 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.'%q_rowid'", zDb, zTab);
4157 if( pStmt ){
4158 nAux = sqlite3_column_count(pStmt) - 2;
4159 sqlite3_finalize(pStmt);
4160 }else
4161 if( check.rc!=SQLITE_NOMEM ){
4162 check.rc = SQLITE_OK;
4165 /* Find number of dimensions in the rtree table. */
4166 pStmt = rtreeCheckPrepare(&check, "SELECT * FROM %Q.%Q", zDb, zTab);
4167 if( pStmt ){
4168 int rc;
4169 check.nDim = (sqlite3_column_count(pStmt) - 1 - nAux) / 2;
4170 if( check.nDim<1 ){
4171 rtreeCheckAppendMsg(&check, "Schema corrupt or not an rtree");
4172 }else if( SQLITE_ROW==sqlite3_step(pStmt) ){
4173 check.bInt = (sqlite3_column_type(pStmt, 1)==SQLITE_INTEGER);
4175 rc = sqlite3_finalize(pStmt);
4176 if( rc!=SQLITE_CORRUPT ) check.rc = rc;
4179 /* Do the actual integrity-check */
4180 if( check.nDim>=1 ){
4181 if( check.rc==SQLITE_OK ){
4182 rtreeCheckNode(&check, 0, 0, 1);
4184 rtreeCheckCount(&check, "_rowid", check.nLeaf);
4185 rtreeCheckCount(&check, "_parent", check.nNonLeaf);
4188 /* Finalize SQL statements used by the integrity-check */
4189 sqlite3_finalize(check.pGetNode);
4190 sqlite3_finalize(check.aCheckMapping[0]);
4191 sqlite3_finalize(check.aCheckMapping[1]);
4193 *pzReport = check.zReport;
4194 return check.rc;
4198 ** Implementation of the xIntegrity method for Rtree.
4200 static int rtreeIntegrity(
4201 sqlite3_vtab *pVtab, /* The virtual table to check */
4202 const char *zSchema, /* Schema in which the virtual table lives */
4203 const char *zName, /* Name of the virtual table */
4204 int isQuick, /* True for a quick_check */
4205 char **pzErr /* Write results here */
4207 Rtree *pRtree = (Rtree*)pVtab;
4208 int rc;
4209 assert( pzErr!=0 && *pzErr==0 );
4210 UNUSED_PARAMETER(zSchema);
4211 UNUSED_PARAMETER(zName);
4212 UNUSED_PARAMETER(isQuick);
4213 rc = rtreeCheckTable(pRtree->db, pRtree->zDb, pRtree->zName, pzErr);
4214 if( rc==SQLITE_OK && *pzErr ){
4215 *pzErr = sqlite3_mprintf("In RTree %s.%s:\n%z",
4216 pRtree->zDb, pRtree->zName, *pzErr);
4217 if( (*pzErr)==0 ) rc = SQLITE_NOMEM;
4219 return rc;
4223 ** Usage:
4225 ** rtreecheck(<rtree-table>);
4226 ** rtreecheck(<database>, <rtree-table>);
4228 ** Invoking this SQL function runs an integrity-check on the named rtree
4229 ** table. The integrity-check verifies the following:
4231 ** 1. For each cell in the r-tree structure (%_node table), that:
4233 ** a) for each dimension, (coord1 <= coord2).
4235 ** b) unless the cell is on the root node, that the cell is bounded
4236 ** by the parent cell on the parent node.
4238 ** c) for leaf nodes, that there is an entry in the %_rowid
4239 ** table corresponding to the cell's rowid value that
4240 ** points to the correct node.
4242 ** d) for cells on non-leaf nodes, that there is an entry in the
4243 ** %_parent table mapping from the cell's child node to the
4244 ** node that it resides on.
4246 ** 2. That there are the same number of entries in the %_rowid table
4247 ** as there are leaf cells in the r-tree structure, and that there
4248 ** is a leaf cell that corresponds to each entry in the %_rowid table.
4250 ** 3. That there are the same number of entries in the %_parent table
4251 ** as there are non-leaf cells in the r-tree structure, and that
4252 ** there is a non-leaf cell that corresponds to each entry in the
4253 ** %_parent table.
4255 static void rtreecheck(
4256 sqlite3_context *ctx,
4257 int nArg,
4258 sqlite3_value **apArg
4260 if( nArg!=1 && nArg!=2 ){
4261 sqlite3_result_error(ctx,
4262 "wrong number of arguments to function rtreecheck()", -1
4264 }else{
4265 int rc;
4266 char *zReport = 0;
4267 const char *zDb = (const char*)sqlite3_value_text(apArg[0]);
4268 const char *zTab;
4269 if( nArg==1 ){
4270 zTab = zDb;
4271 zDb = "main";
4272 }else{
4273 zTab = (const char*)sqlite3_value_text(apArg[1]);
4275 rc = rtreeCheckTable(sqlite3_context_db_handle(ctx), zDb, zTab, &zReport);
4276 if( rc==SQLITE_OK ){
4277 sqlite3_result_text(ctx, zReport ? zReport : "ok", -1, SQLITE_TRANSIENT);
4278 }else{
4279 sqlite3_result_error_code(ctx, rc);
4281 sqlite3_free(zReport);
4285 /* Conditionally include the geopoly code */
4286 #ifdef SQLITE_ENABLE_GEOPOLY
4287 # include "geopoly.c"
4288 #endif
4291 ** Register the r-tree module with database handle db. This creates the
4292 ** virtual table module "rtree" and the debugging/analysis scalar
4293 ** function "rtreenode".
4295 int sqlite3RtreeInit(sqlite3 *db){
4296 const int utf8 = SQLITE_UTF8;
4297 int rc;
4299 rc = sqlite3_create_function(db, "rtreenode", 2, utf8, 0, rtreenode, 0, 0);
4300 if( rc==SQLITE_OK ){
4301 rc = sqlite3_create_function(db, "rtreedepth", 1, utf8, 0,rtreedepth, 0, 0);
4303 if( rc==SQLITE_OK ){
4304 rc = sqlite3_create_function(db, "rtreecheck", -1, utf8, 0,rtreecheck, 0,0);
4306 if( rc==SQLITE_OK ){
4307 #ifdef SQLITE_RTREE_INT_ONLY
4308 void *c = (void *)RTREE_COORD_INT32;
4309 #else
4310 void *c = (void *)RTREE_COORD_REAL32;
4311 #endif
4312 rc = sqlite3_create_module_v2(db, "rtree", &rtreeModule, c, 0);
4314 if( rc==SQLITE_OK ){
4315 void *c = (void *)RTREE_COORD_INT32;
4316 rc = sqlite3_create_module_v2(db, "rtree_i32", &rtreeModule, c, 0);
4318 #ifdef SQLITE_ENABLE_GEOPOLY
4319 if( rc==SQLITE_OK ){
4320 rc = sqlite3_geopoly_init(db);
4322 #endif
4324 return rc;
4328 ** This routine deletes the RtreeGeomCallback object that was attached
4329 ** one of the SQL functions create by sqlite3_rtree_geometry_callback()
4330 ** or sqlite3_rtree_query_callback(). In other words, this routine is the
4331 ** destructor for an RtreeGeomCallback objecct. This routine is called when
4332 ** the corresponding SQL function is deleted.
4334 static void rtreeFreeCallback(void *p){
4335 RtreeGeomCallback *pInfo = (RtreeGeomCallback*)p;
4336 if( pInfo->xDestructor ) pInfo->xDestructor(pInfo->pContext);
4337 sqlite3_free(p);
4341 ** This routine frees the BLOB that is returned by geomCallback().
4343 static void rtreeMatchArgFree(void *pArg){
4344 int i;
4345 RtreeMatchArg *p = (RtreeMatchArg*)pArg;
4346 for(i=0; i<p->nParam; i++){
4347 sqlite3_value_free(p->apSqlParam[i]);
4349 sqlite3_free(p);
4353 ** Each call to sqlite3_rtree_geometry_callback() or
4354 ** sqlite3_rtree_query_callback() creates an ordinary SQLite
4355 ** scalar function that is implemented by this routine.
4357 ** All this function does is construct an RtreeMatchArg object that
4358 ** contains the geometry-checking callback routines and a list of
4359 ** parameters to this function, then return that RtreeMatchArg object
4360 ** as a BLOB.
4362 ** The R-Tree MATCH operator will read the returned BLOB, deserialize
4363 ** the RtreeMatchArg object, and use the RtreeMatchArg object to figure
4364 ** out which elements of the R-Tree should be returned by the query.
4366 static void geomCallback(sqlite3_context *ctx, int nArg, sqlite3_value **aArg){
4367 RtreeGeomCallback *pGeomCtx = (RtreeGeomCallback *)sqlite3_user_data(ctx);
4368 RtreeMatchArg *pBlob;
4369 sqlite3_int64 nBlob;
4370 int memErr = 0;
4372 nBlob = sizeof(RtreeMatchArg) + (nArg-1)*sizeof(RtreeDValue)
4373 + nArg*sizeof(sqlite3_value*);
4374 pBlob = (RtreeMatchArg *)sqlite3_malloc64(nBlob);
4375 if( !pBlob ){
4376 sqlite3_result_error_nomem(ctx);
4377 }else{
4378 int i;
4379 pBlob->iSize = nBlob;
4380 pBlob->cb = pGeomCtx[0];
4381 pBlob->apSqlParam = (sqlite3_value**)&pBlob->aParam[nArg];
4382 pBlob->nParam = nArg;
4383 for(i=0; i<nArg; i++){
4384 pBlob->apSqlParam[i] = sqlite3_value_dup(aArg[i]);
4385 if( pBlob->apSqlParam[i]==0 ) memErr = 1;
4386 #ifdef SQLITE_RTREE_INT_ONLY
4387 pBlob->aParam[i] = sqlite3_value_int64(aArg[i]);
4388 #else
4389 pBlob->aParam[i] = sqlite3_value_double(aArg[i]);
4390 #endif
4392 if( memErr ){
4393 sqlite3_result_error_nomem(ctx);
4394 rtreeMatchArgFree(pBlob);
4395 }else{
4396 sqlite3_result_pointer(ctx, pBlob, "RtreeMatchArg", rtreeMatchArgFree);
4402 ** Register a new geometry function for use with the r-tree MATCH operator.
4404 int sqlite3_rtree_geometry_callback(
4405 sqlite3 *db, /* Register SQL function on this connection */
4406 const char *zGeom, /* Name of the new SQL function */
4407 int (*xGeom)(sqlite3_rtree_geometry*,int,RtreeDValue*,int*), /* Callback */
4408 void *pContext /* Extra data associated with the callback */
4410 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
4412 /* Allocate and populate the context object. */
4413 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
4414 if( !pGeomCtx ) return SQLITE_NOMEM;
4415 pGeomCtx->xGeom = xGeom;
4416 pGeomCtx->xQueryFunc = 0;
4417 pGeomCtx->xDestructor = 0;
4418 pGeomCtx->pContext = pContext;
4419 return sqlite3_create_function_v2(db, zGeom, -1, SQLITE_ANY,
4420 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
4425 ** Register a new 2nd-generation geometry function for use with the
4426 ** r-tree MATCH operator.
4428 int sqlite3_rtree_query_callback(
4429 sqlite3 *db, /* Register SQL function on this connection */
4430 const char *zQueryFunc, /* Name of new SQL function */
4431 int (*xQueryFunc)(sqlite3_rtree_query_info*), /* Callback */
4432 void *pContext, /* Extra data passed into the callback */
4433 void (*xDestructor)(void*) /* Destructor for the extra data */
4435 RtreeGeomCallback *pGeomCtx; /* Context object for new user-function */
4437 /* Allocate and populate the context object. */
4438 pGeomCtx = (RtreeGeomCallback *)sqlite3_malloc(sizeof(RtreeGeomCallback));
4439 if( !pGeomCtx ){
4440 if( xDestructor ) xDestructor(pContext);
4441 return SQLITE_NOMEM;
4443 pGeomCtx->xGeom = 0;
4444 pGeomCtx->xQueryFunc = xQueryFunc;
4445 pGeomCtx->xDestructor = xDestructor;
4446 pGeomCtx->pContext = pContext;
4447 return sqlite3_create_function_v2(db, zQueryFunc, -1, SQLITE_ANY,
4448 (void *)pGeomCtx, geomCallback, 0, 0, rtreeFreeCallback
4452 #if !SQLITE_CORE
4453 #ifdef _WIN32
4454 __declspec(dllexport)
4455 #endif
4456 int sqlite3_rtree_init(
4457 sqlite3 *db,
4458 char **pzErrMsg,
4459 const sqlite3_api_routines *pApi
4461 SQLITE_EXTENSION_INIT2(pApi)
4462 return sqlite3RtreeInit(db);
4464 #endif
4466 #endif