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