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