rtree.c 138 KB

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