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