fts3.c 204 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195
  1. /*
  2. ** 2006 Oct 10
  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. **
  13. ** This is an SQLite module implementing full-text search.
  14. */
  15. /*
  16. ** The code in this file is only compiled if:
  17. **
  18. ** * The FTS3 module is being built as an extension
  19. ** (in which case SQLITE_CORE is not defined), or
  20. **
  21. ** * The FTS3 module is being built into the core of
  22. ** SQLite (in which case SQLITE_ENABLE_FTS3 is defined).
  23. */
  24. /* The full-text index is stored in a series of b+tree (-like)
  25. ** structures called segments which map terms to doclists. The
  26. ** structures are like b+trees in layout, but are constructed from the
  27. ** bottom up in optimal fashion and are not updatable. Since trees
  28. ** are built from the bottom up, things will be described from the
  29. ** bottom up.
  30. **
  31. **
  32. **** Varints ****
  33. ** The basic unit of encoding is a variable-length integer called a
  34. ** varint. We encode variable-length integers in little-endian order
  35. ** using seven bits * per byte as follows:
  36. **
  37. ** KEY:
  38. ** A = 0xxxxxxx 7 bits of data and one flag bit
  39. ** B = 1xxxxxxx 7 bits of data and one flag bit
  40. **
  41. ** 7 bits - A
  42. ** 14 bits - BA
  43. ** 21 bits - BBA
  44. ** and so on.
  45. **
  46. ** This is similar in concept to how sqlite encodes "varints" but
  47. ** the encoding is not the same. SQLite varints are big-endian
  48. ** are are limited to 9 bytes in length whereas FTS3 varints are
  49. ** little-endian and can be up to 10 bytes in length (in theory).
  50. **
  51. ** Example encodings:
  52. **
  53. ** 1: 0x01
  54. ** 127: 0x7f
  55. ** 128: 0x81 0x00
  56. **
  57. **
  58. **** Document lists ****
  59. ** A doclist (document list) holds a docid-sorted list of hits for a
  60. ** given term. Doclists hold docids and associated token positions.
  61. ** A docid is the unique integer identifier for a single document.
  62. ** A position is the index of a word within the document. The first
  63. ** word of the document has a position of 0.
  64. **
  65. ** FTS3 used to optionally store character offsets using a compile-time
  66. ** option. But that functionality is no longer supported.
  67. **
  68. ** A doclist is stored like this:
  69. **
  70. ** array {
  71. ** varint docid; (delta from previous doclist)
  72. ** array { (position list for column 0)
  73. ** varint position; (2 more than the delta from previous position)
  74. ** }
  75. ** array {
  76. ** varint POS_COLUMN; (marks start of position list for new column)
  77. ** varint column; (index of new column)
  78. ** array {
  79. ** varint position; (2 more than the delta from previous position)
  80. ** }
  81. ** }
  82. ** varint POS_END; (marks end of positions for this document.
  83. ** }
  84. **
  85. ** Here, array { X } means zero or more occurrences of X, adjacent in
  86. ** memory. A "position" is an index of a token in the token stream
  87. ** generated by the tokenizer. Note that POS_END and POS_COLUMN occur
  88. ** in the same logical place as the position element, and act as sentinals
  89. ** ending a position list array. POS_END is 0. POS_COLUMN is 1.
  90. ** The positions numbers are not stored literally but rather as two more
  91. ** than the difference from the prior position, or the just the position plus
  92. ** 2 for the first position. Example:
  93. **
  94. ** label: A B C D E F G H I J K
  95. ** value: 123 5 9 1 1 14 35 0 234 72 0
  96. **
  97. ** The 123 value is the first docid. For column zero in this document
  98. ** there are two matches at positions 3 and 10 (5-2 and 9-2+3). The 1
  99. ** at D signals the start of a new column; the 1 at E indicates that the
  100. ** new column is column number 1. There are two positions at 12 and 45
  101. ** (14-2 and 35-2+12). The 0 at H indicate the end-of-document. The
  102. ** 234 at I is the delta to next docid (357). It has one position 70
  103. ** (72-2) and then terminates with the 0 at K.
  104. **
  105. ** A "position-list" is the list of positions for multiple columns for
  106. ** a single docid. A "column-list" is the set of positions for a single
  107. ** column. Hence, a position-list consists of one or more column-lists,
  108. ** a document record consists of a docid followed by a position-list and
  109. ** a doclist consists of one or more document records.
  110. **
  111. ** A bare doclist omits the position information, becoming an
  112. ** array of varint-encoded docids.
  113. **
  114. **** Segment leaf nodes ****
  115. ** Segment leaf nodes store terms and doclists, ordered by term. Leaf
  116. ** nodes are written using LeafWriter, and read using LeafReader (to
  117. ** iterate through a single leaf node's data) and LeavesReader (to
  118. ** iterate through a segment's entire leaf layer). Leaf nodes have
  119. ** the format:
  120. **
  121. ** varint iHeight; (height from leaf level, always 0)
  122. ** varint nTerm; (length of first term)
  123. ** char pTerm[nTerm]; (content of first term)
  124. ** varint nDoclist; (length of term's associated doclist)
  125. ** char pDoclist[nDoclist]; (content of doclist)
  126. ** array {
  127. ** (further terms are delta-encoded)
  128. ** varint nPrefix; (length of prefix shared with previous term)
  129. ** varint nSuffix; (length of unshared suffix)
  130. ** char pTermSuffix[nSuffix];(unshared suffix of next term)
  131. ** varint nDoclist; (length of term's associated doclist)
  132. ** char pDoclist[nDoclist]; (content of doclist)
  133. ** }
  134. **
  135. ** Here, array { X } means zero or more occurrences of X, adjacent in
  136. ** memory.
  137. **
  138. ** Leaf nodes are broken into blocks which are stored contiguously in
  139. ** the %_segments table in sorted order. This means that when the end
  140. ** of a node is reached, the next term is in the node with the next
  141. ** greater node id.
  142. **
  143. ** New data is spilled to a new leaf node when the current node
  144. ** exceeds LEAF_MAX bytes (default 2048). New data which itself is
  145. ** larger than STANDALONE_MIN (default 1024) is placed in a standalone
  146. ** node (a leaf node with a single term and doclist). The goal of
  147. ** these settings is to pack together groups of small doclists while
  148. ** making it efficient to directly access large doclists. The
  149. ** assumption is that large doclists represent terms which are more
  150. ** likely to be query targets.
  151. **
  152. ** TODO(shess) It may be useful for blocking decisions to be more
  153. ** dynamic. For instance, it may make more sense to have a 2.5k leaf
  154. ** node rather than splitting into 2k and .5k nodes. My intuition is
  155. ** that this might extend through 2x or 4x the pagesize.
  156. **
  157. **
  158. **** Segment interior nodes ****
  159. ** Segment interior nodes store blockids for subtree nodes and terms
  160. ** to describe what data is stored by the each subtree. Interior
  161. ** nodes are written using InteriorWriter, and read using
  162. ** InteriorReader. InteriorWriters are created as needed when
  163. ** SegmentWriter creates new leaf nodes, or when an interior node
  164. ** itself grows too big and must be split. The format of interior
  165. ** nodes:
  166. **
  167. ** varint iHeight; (height from leaf level, always >0)
  168. ** varint iBlockid; (block id of node's leftmost subtree)
  169. ** optional {
  170. ** varint nTerm; (length of first term)
  171. ** char pTerm[nTerm]; (content of first term)
  172. ** array {
  173. ** (further terms are delta-encoded)
  174. ** varint nPrefix; (length of shared prefix with previous term)
  175. ** varint nSuffix; (length of unshared suffix)
  176. ** char pTermSuffix[nSuffix]; (unshared suffix of next term)
  177. ** }
  178. ** }
  179. **
  180. ** Here, optional { X } means an optional element, while array { X }
  181. ** means zero or more occurrences of X, adjacent in memory.
  182. **
  183. ** An interior node encodes n terms separating n+1 subtrees. The
  184. ** subtree blocks are contiguous, so only the first subtree's blockid
  185. ** is encoded. The subtree at iBlockid will contain all terms less
  186. ** than the first term encoded (or all terms if no term is encoded).
  187. ** Otherwise, for terms greater than or equal to pTerm[i] but less
  188. ** than pTerm[i+1], the subtree for that term will be rooted at
  189. ** iBlockid+i. Interior nodes only store enough term data to
  190. ** distinguish adjacent children (if the rightmost term of the left
  191. ** child is "something", and the leftmost term of the right child is
  192. ** "wicked", only "w" is stored).
  193. **
  194. ** New data is spilled to a new interior node at the same height when
  195. ** the current node exceeds INTERIOR_MAX bytes (default 2048).
  196. ** INTERIOR_MIN_TERMS (default 7) keeps large terms from monopolizing
  197. ** interior nodes and making the tree too skinny. The interior nodes
  198. ** at a given height are naturally tracked by interior nodes at
  199. ** height+1, and so on.
  200. **
  201. **
  202. **** Segment directory ****
  203. ** The segment directory in table %_segdir stores meta-information for
  204. ** merging and deleting segments, and also the root node of the
  205. ** segment's tree.
  206. **
  207. ** The root node is the top node of the segment's tree after encoding
  208. ** the entire segment, restricted to ROOT_MAX bytes (default 1024).
  209. ** This could be either a leaf node or an interior node. If the top
  210. ** node requires more than ROOT_MAX bytes, it is flushed to %_segments
  211. ** and a new root interior node is generated (which should always fit
  212. ** within ROOT_MAX because it only needs space for 2 varints, the
  213. ** height and the blockid of the previous root).
  214. **
  215. ** The meta-information in the segment directory is:
  216. ** level - segment level (see below)
  217. ** idx - index within level
  218. ** - (level,idx uniquely identify a segment)
  219. ** start_block - first leaf node
  220. ** leaves_end_block - last leaf node
  221. ** end_block - last block (including interior nodes)
  222. ** root - contents of root node
  223. **
  224. ** If the root node is a leaf node, then start_block,
  225. ** leaves_end_block, and end_block are all 0.
  226. **
  227. **
  228. **** Segment merging ****
  229. ** To amortize update costs, segments are grouped into levels and
  230. ** merged in batches. Each increase in level represents exponentially
  231. ** more documents.
  232. **
  233. ** New documents (actually, document updates) are tokenized and
  234. ** written individually (using LeafWriter) to a level 0 segment, with
  235. ** incrementing idx. When idx reaches MERGE_COUNT (default 16), all
  236. ** level 0 segments are merged into a single level 1 segment. Level 1
  237. ** is populated like level 0, and eventually MERGE_COUNT level 1
  238. ** segments are merged to a single level 2 segment (representing
  239. ** MERGE_COUNT^2 updates), and so on.
  240. **
  241. ** A segment merge traverses all segments at a given level in
  242. ** parallel, performing a straightforward sorted merge. Since segment
  243. ** leaf nodes are written in to the %_segments table in order, this
  244. ** merge traverses the underlying sqlite disk structures efficiently.
  245. ** After the merge, all segment blocks from the merged level are
  246. ** deleted.
  247. **
  248. ** MERGE_COUNT controls how often we merge segments. 16 seems to be
  249. ** somewhat of a sweet spot for insertion performance. 32 and 64 show
  250. ** very similar performance numbers to 16 on insertion, though they're
  251. ** a tiny bit slower (perhaps due to more overhead in merge-time
  252. ** sorting). 8 is about 20% slower than 16, 4 about 50% slower than
  253. ** 16, 2 about 66% slower than 16.
  254. **
  255. ** At query time, high MERGE_COUNT increases the number of segments
  256. ** which need to be scanned and merged. For instance, with 100k docs
  257. ** inserted:
  258. **
  259. ** MERGE_COUNT segments
  260. ** 16 25
  261. ** 8 12
  262. ** 4 10
  263. ** 2 6
  264. **
  265. ** This appears to have only a moderate impact on queries for very
  266. ** frequent terms (which are somewhat dominated by segment merge
  267. ** costs), and infrequent and non-existent terms still seem to be fast
  268. ** even with many segments.
  269. **
  270. ** TODO(shess) That said, it would be nice to have a better query-side
  271. ** argument for MERGE_COUNT of 16. Also, it is possible/likely that
  272. ** optimizations to things like doclist merging will swing the sweet
  273. ** spot around.
  274. **
  275. **
  276. **
  277. **** Handling of deletions and updates ****
  278. ** Since we're using a segmented structure, with no docid-oriented
  279. ** index into the term index, we clearly cannot simply update the term
  280. ** index when a document is deleted or updated. For deletions, we
  281. ** write an empty doclist (varint(docid) varint(POS_END)), for updates
  282. ** we simply write the new doclist. Segment merges overwrite older
  283. ** data for a particular docid with newer data, so deletes or updates
  284. ** will eventually overtake the earlier data and knock it out. The
  285. ** query logic likewise merges doclists so that newer data knocks out
  286. ** older data.
  287. */
  288. #include "fts3Int.h"
  289. #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_FTS3)
  290. #if defined(SQLITE_ENABLE_FTS3) && !defined(SQLITE_CORE)
  291. # define SQLITE_CORE 1
  292. #endif
  293. #include <assert.h>
  294. #include <stdlib.h>
  295. #include <stddef.h>
  296. #include <stdio.h>
  297. #include <string.h>
  298. #include <stdarg.h>
  299. #include "fts3.h"
  300. #ifndef SQLITE_CORE
  301. # include "sqlite3ext.h"
  302. SQLITE_EXTENSION_INIT1
  303. #endif
  304. typedef struct Fts3HashWrapper Fts3HashWrapper;
  305. struct Fts3HashWrapper {
  306. Fts3Hash hash; /* Hash table */
  307. int nRef; /* Number of pointers to this object */
  308. };
  309. static int fts3EvalNext(Fts3Cursor *pCsr);
  310. static int fts3EvalStart(Fts3Cursor *pCsr);
  311. static int fts3TermSegReaderCursor(
  312. Fts3Cursor *, const char *, int, int, Fts3MultiSegReader **);
  313. /*
  314. ** This variable is set to false when running tests for which the on disk
  315. ** structures should not be corrupt. Otherwise, true. If it is false, extra
  316. ** assert() conditions in the fts3 code are activated - conditions that are
  317. ** only true if it is guaranteed that the fts3 database is not corrupt.
  318. */
  319. #ifdef SQLITE_DEBUG
  320. int sqlite3_fts3_may_be_corrupt = 1;
  321. #endif
  322. /*
  323. ** Write a 64-bit variable-length integer to memory starting at p[0].
  324. ** The length of data written will be between 1 and FTS3_VARINT_MAX bytes.
  325. ** The number of bytes written is returned.
  326. */
  327. int sqlite3Fts3PutVarint(char *p, sqlite_int64 v){
  328. unsigned char *q = (unsigned char *) p;
  329. sqlite_uint64 vu = v;
  330. do{
  331. *q++ = (unsigned char) ((vu & 0x7f) | 0x80);
  332. vu >>= 7;
  333. }while( vu!=0 );
  334. q[-1] &= 0x7f; /* turn off high bit in final byte */
  335. assert( q - (unsigned char *)p <= FTS3_VARINT_MAX );
  336. return (int) (q - (unsigned char *)p);
  337. }
  338. #define GETVARINT_STEP(v, ptr, shift, mask1, mask2, var, ret) \
  339. v = (v & mask1) | ( (*(const unsigned char*)(ptr++)) << shift ); \
  340. if( (v & mask2)==0 ){ var = v; return ret; }
  341. #define GETVARINT_INIT(v, ptr, shift, mask1, mask2, var, ret) \
  342. v = (*ptr++); \
  343. if( (v & mask2)==0 ){ var = v; return ret; }
  344. int sqlite3Fts3GetVarintU(const char *pBuf, sqlite_uint64 *v){
  345. const unsigned char *p = (const unsigned char*)pBuf;
  346. const unsigned char *pStart = p;
  347. u32 a;
  348. u64 b;
  349. int shift;
  350. GETVARINT_INIT(a, p, 0, 0x00, 0x80, *v, 1);
  351. GETVARINT_STEP(a, p, 7, 0x7F, 0x4000, *v, 2);
  352. GETVARINT_STEP(a, p, 14, 0x3FFF, 0x200000, *v, 3);
  353. GETVARINT_STEP(a, p, 21, 0x1FFFFF, 0x10000000, *v, 4);
  354. b = (a & 0x0FFFFFFF );
  355. for(shift=28; shift<=63; shift+=7){
  356. u64 c = *p++;
  357. b += (c&0x7F) << shift;
  358. if( (c & 0x80)==0 ) break;
  359. }
  360. *v = b;
  361. return (int)(p - pStart);
  362. }
  363. /*
  364. ** Read a 64-bit variable-length integer from memory starting at p[0].
  365. ** Return the number of bytes read, or 0 on error.
  366. ** The value is stored in *v.
  367. */
  368. int sqlite3Fts3GetVarint(const char *pBuf, sqlite_int64 *v){
  369. return sqlite3Fts3GetVarintU(pBuf, (sqlite3_uint64*)v);
  370. }
  371. /*
  372. ** Read a 64-bit variable-length integer from memory starting at p[0] and
  373. ** not extending past pEnd[-1].
  374. ** Return the number of bytes read, or 0 on error.
  375. ** The value is stored in *v.
  376. */
  377. int sqlite3Fts3GetVarintBounded(
  378. const char *pBuf,
  379. const char *pEnd,
  380. sqlite_int64 *v
  381. ){
  382. const unsigned char *p = (const unsigned char*)pBuf;
  383. const unsigned char *pStart = p;
  384. const unsigned char *pX = (const unsigned char*)pEnd;
  385. u64 b = 0;
  386. int shift;
  387. for(shift=0; shift<=63; shift+=7){
  388. u64 c = p<pX ? *p : 0;
  389. p++;
  390. b += (c&0x7F) << shift;
  391. if( (c & 0x80)==0 ) break;
  392. }
  393. *v = b;
  394. return (int)(p - pStart);
  395. }
  396. /*
  397. ** Similar to sqlite3Fts3GetVarint(), except that the output is truncated to
  398. ** a non-negative 32-bit integer before it is returned.
  399. */
  400. int sqlite3Fts3GetVarint32(const char *p, int *pi){
  401. const unsigned char *ptr = (const unsigned char*)p;
  402. u32 a;
  403. #ifndef fts3GetVarint32
  404. GETVARINT_INIT(a, ptr, 0, 0x00, 0x80, *pi, 1);
  405. #else
  406. a = (*ptr++);
  407. assert( a & 0x80 );
  408. #endif
  409. GETVARINT_STEP(a, ptr, 7, 0x7F, 0x4000, *pi, 2);
  410. GETVARINT_STEP(a, ptr, 14, 0x3FFF, 0x200000, *pi, 3);
  411. GETVARINT_STEP(a, ptr, 21, 0x1FFFFF, 0x10000000, *pi, 4);
  412. a = (a & 0x0FFFFFFF );
  413. *pi = (int)(a | ((u32)(*ptr & 0x07) << 28));
  414. assert( 0==(a & 0x80000000) );
  415. assert( *pi>=0 );
  416. return 5;
  417. }
  418. /*
  419. ** Return the number of bytes required to encode v as a varint
  420. */
  421. int sqlite3Fts3VarintLen(sqlite3_uint64 v){
  422. int i = 0;
  423. do{
  424. i++;
  425. v >>= 7;
  426. }while( v!=0 );
  427. return i;
  428. }
  429. /*
  430. ** Convert an SQL-style quoted string into a normal string by removing
  431. ** the quote characters. The conversion is done in-place. If the
  432. ** input does not begin with a quote character, then this routine
  433. ** is a no-op.
  434. **
  435. ** Examples:
  436. **
  437. ** "abc" becomes abc
  438. ** 'xyz' becomes xyz
  439. ** [pqr] becomes pqr
  440. ** `mno` becomes mno
  441. **
  442. */
  443. void sqlite3Fts3Dequote(char *z){
  444. char quote; /* Quote character (if any ) */
  445. quote = z[0];
  446. if( quote=='[' || quote=='\'' || quote=='"' || quote=='`' ){
  447. int iIn = 1; /* Index of next byte to read from input */
  448. int iOut = 0; /* Index of next byte to write to output */
  449. /* If the first byte was a '[', then the close-quote character is a ']' */
  450. if( quote=='[' ) quote = ']';
  451. while( z[iIn] ){
  452. if( z[iIn]==quote ){
  453. if( z[iIn+1]!=quote ) break;
  454. z[iOut++] = quote;
  455. iIn += 2;
  456. }else{
  457. z[iOut++] = z[iIn++];
  458. }
  459. }
  460. z[iOut] = '\0';
  461. }
  462. }
  463. /*
  464. ** Read a single varint from the doclist at *pp and advance *pp to point
  465. ** to the first byte past the end of the varint. Add the value of the varint
  466. ** to *pVal.
  467. */
  468. static void fts3GetDeltaVarint(char **pp, sqlite3_int64 *pVal){
  469. sqlite3_int64 iVal;
  470. *pp += sqlite3Fts3GetVarint(*pp, &iVal);
  471. *pVal += iVal;
  472. }
  473. /*
  474. ** When this function is called, *pp points to the first byte following a
  475. ** varint that is part of a doclist (or position-list, or any other list
  476. ** of varints). This function moves *pp to point to the start of that varint,
  477. ** and sets *pVal by the varint value.
  478. **
  479. ** Argument pStart points to the first byte of the doclist that the
  480. ** varint is part of.
  481. */
  482. static void fts3GetReverseVarint(
  483. char **pp,
  484. char *pStart,
  485. sqlite3_int64 *pVal
  486. ){
  487. sqlite3_int64 iVal;
  488. char *p;
  489. /* Pointer p now points at the first byte past the varint we are
  490. ** interested in. So, unless the doclist is corrupt, the 0x80 bit is
  491. ** clear on character p[-1]. */
  492. for(p = (*pp)-2; p>=pStart && *p&0x80; p--);
  493. p++;
  494. *pp = p;
  495. sqlite3Fts3GetVarint(p, &iVal);
  496. *pVal = iVal;
  497. }
  498. /*
  499. ** The xDisconnect() virtual table method.
  500. */
  501. static int fts3DisconnectMethod(sqlite3_vtab *pVtab){
  502. Fts3Table *p = (Fts3Table *)pVtab;
  503. int i;
  504. assert( p->nPendingData==0 );
  505. assert( p->pSegments==0 );
  506. /* Free any prepared statements held */
  507. sqlite3_finalize(p->pSeekStmt);
  508. for(i=0; i<SizeofArray(p->aStmt); i++){
  509. sqlite3_finalize(p->aStmt[i]);
  510. }
  511. sqlite3_free(p->zSegmentsTbl);
  512. sqlite3_free(p->zReadExprlist);
  513. sqlite3_free(p->zWriteExprlist);
  514. sqlite3_free(p->zContentTbl);
  515. sqlite3_free(p->zLanguageid);
  516. /* Invoke the tokenizer destructor to free the tokenizer. */
  517. p->pTokenizer->pModule->xDestroy(p->pTokenizer);
  518. sqlite3_free(p);
  519. return SQLITE_OK;
  520. }
  521. /*
  522. ** Write an error message into *pzErr
  523. */
  524. void sqlite3Fts3ErrMsg(char **pzErr, const char *zFormat, ...){
  525. va_list ap;
  526. sqlite3_free(*pzErr);
  527. va_start(ap, zFormat);
  528. *pzErr = sqlite3_vmprintf(zFormat, ap);
  529. va_end(ap);
  530. }
  531. /*
  532. ** Construct one or more SQL statements from the format string given
  533. ** and then evaluate those statements. The success code is written
  534. ** into *pRc.
  535. **
  536. ** If *pRc is initially non-zero then this routine is a no-op.
  537. */
  538. static void fts3DbExec(
  539. int *pRc, /* Success code */
  540. sqlite3 *db, /* Database in which to run SQL */
  541. const char *zFormat, /* Format string for SQL */
  542. ... /* Arguments to the format string */
  543. ){
  544. va_list ap;
  545. char *zSql;
  546. if( *pRc ) return;
  547. va_start(ap, zFormat);
  548. zSql = sqlite3_vmprintf(zFormat, ap);
  549. va_end(ap);
  550. if( zSql==0 ){
  551. *pRc = SQLITE_NOMEM;
  552. }else{
  553. *pRc = sqlite3_exec(db, zSql, 0, 0, 0);
  554. sqlite3_free(zSql);
  555. }
  556. }
  557. /*
  558. ** The xDestroy() virtual table method.
  559. */
  560. static int fts3DestroyMethod(sqlite3_vtab *pVtab){
  561. Fts3Table *p = (Fts3Table *)pVtab;
  562. int rc = SQLITE_OK; /* Return code */
  563. const char *zDb = p->zDb; /* Name of database (e.g. "main", "temp") */
  564. sqlite3 *db = p->db; /* Database handle */
  565. /* Drop the shadow tables */
  566. fts3DbExec(&rc, db,
  567. "DROP TABLE IF EXISTS %Q.'%q_segments';"
  568. "DROP TABLE IF EXISTS %Q.'%q_segdir';"
  569. "DROP TABLE IF EXISTS %Q.'%q_docsize';"
  570. "DROP TABLE IF EXISTS %Q.'%q_stat';"
  571. "%s DROP TABLE IF EXISTS %Q.'%q_content';",
  572. zDb, p->zName,
  573. zDb, p->zName,
  574. zDb, p->zName,
  575. zDb, p->zName,
  576. (p->zContentTbl ? "--" : ""), zDb,p->zName
  577. );
  578. /* If everything has worked, invoke fts3DisconnectMethod() to free the
  579. ** memory associated with the Fts3Table structure and return SQLITE_OK.
  580. ** Otherwise, return an SQLite error code.
  581. */
  582. return (rc==SQLITE_OK ? fts3DisconnectMethod(pVtab) : rc);
  583. }
  584. /*
  585. ** Invoke sqlite3_declare_vtab() to declare the schema for the FTS3 table
  586. ** passed as the first argument. This is done as part of the xConnect()
  587. ** and xCreate() methods.
  588. **
  589. ** If *pRc is non-zero when this function is called, it is a no-op.
  590. ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
  591. ** before returning.
  592. */
  593. static void fts3DeclareVtab(int *pRc, Fts3Table *p){
  594. if( *pRc==SQLITE_OK ){
  595. int i; /* Iterator variable */
  596. int rc; /* Return code */
  597. char *zSql; /* SQL statement passed to declare_vtab() */
  598. char *zCols; /* List of user defined columns */
  599. const char *zLanguageid;
  600. zLanguageid = (p->zLanguageid ? p->zLanguageid : "__langid");
  601. sqlite3_vtab_config(p->db, SQLITE_VTAB_CONSTRAINT_SUPPORT, 1);
  602. sqlite3_vtab_config(p->db, SQLITE_VTAB_INNOCUOUS);
  603. /* Create a list of user columns for the virtual table */
  604. zCols = sqlite3_mprintf("%Q, ", p->azColumn[0]);
  605. for(i=1; zCols && i<p->nColumn; i++){
  606. zCols = sqlite3_mprintf("%z%Q, ", zCols, p->azColumn[i]);
  607. }
  608. /* Create the whole "CREATE TABLE" statement to pass to SQLite */
  609. zSql = sqlite3_mprintf(
  610. "CREATE TABLE x(%s %Q HIDDEN, docid HIDDEN, %Q HIDDEN)",
  611. zCols, p->zName, zLanguageid
  612. );
  613. if( !zCols || !zSql ){
  614. rc = SQLITE_NOMEM;
  615. }else{
  616. rc = sqlite3_declare_vtab(p->db, zSql);
  617. }
  618. sqlite3_free(zSql);
  619. sqlite3_free(zCols);
  620. *pRc = rc;
  621. }
  622. }
  623. /*
  624. ** Create the %_stat table if it does not already exist.
  625. */
  626. void sqlite3Fts3CreateStatTable(int *pRc, Fts3Table *p){
  627. fts3DbExec(pRc, p->db,
  628. "CREATE TABLE IF NOT EXISTS %Q.'%q_stat'"
  629. "(id INTEGER PRIMARY KEY, value BLOB);",
  630. p->zDb, p->zName
  631. );
  632. if( (*pRc)==SQLITE_OK ) p->bHasStat = 1;
  633. }
  634. /*
  635. ** Create the backing store tables (%_content, %_segments and %_segdir)
  636. ** required by the FTS3 table passed as the only argument. This is done
  637. ** as part of the vtab xCreate() method.
  638. **
  639. ** If the p->bHasDocsize boolean is true (indicating that this is an
  640. ** FTS4 table, not an FTS3 table) then also create the %_docsize and
  641. ** %_stat tables required by FTS4.
  642. */
  643. static int fts3CreateTables(Fts3Table *p){
  644. int rc = SQLITE_OK; /* Return code */
  645. int i; /* Iterator variable */
  646. sqlite3 *db = p->db; /* The database connection */
  647. if( p->zContentTbl==0 ){
  648. const char *zLanguageid = p->zLanguageid;
  649. char *zContentCols; /* Columns of %_content table */
  650. /* Create a list of user columns for the content table */
  651. zContentCols = sqlite3_mprintf("docid INTEGER PRIMARY KEY");
  652. for(i=0; zContentCols && i<p->nColumn; i++){
  653. char *z = p->azColumn[i];
  654. zContentCols = sqlite3_mprintf("%z, 'c%d%q'", zContentCols, i, z);
  655. }
  656. if( zLanguageid && zContentCols ){
  657. zContentCols = sqlite3_mprintf("%z, langid", zContentCols, zLanguageid);
  658. }
  659. if( zContentCols==0 ) rc = SQLITE_NOMEM;
  660. /* Create the content table */
  661. fts3DbExec(&rc, db,
  662. "CREATE TABLE %Q.'%q_content'(%s)",
  663. p->zDb, p->zName, zContentCols
  664. );
  665. sqlite3_free(zContentCols);
  666. }
  667. /* Create other tables */
  668. fts3DbExec(&rc, db,
  669. "CREATE TABLE %Q.'%q_segments'(blockid INTEGER PRIMARY KEY, block BLOB);",
  670. p->zDb, p->zName
  671. );
  672. fts3DbExec(&rc, db,
  673. "CREATE TABLE %Q.'%q_segdir'("
  674. "level INTEGER,"
  675. "idx INTEGER,"
  676. "start_block INTEGER,"
  677. "leaves_end_block INTEGER,"
  678. "end_block INTEGER,"
  679. "root BLOB,"
  680. "PRIMARY KEY(level, idx)"
  681. ");",
  682. p->zDb, p->zName
  683. );
  684. if( p->bHasDocsize ){
  685. fts3DbExec(&rc, db,
  686. "CREATE TABLE %Q.'%q_docsize'(docid INTEGER PRIMARY KEY, size BLOB);",
  687. p->zDb, p->zName
  688. );
  689. }
  690. assert( p->bHasStat==p->bFts4 );
  691. if( p->bHasStat ){
  692. sqlite3Fts3CreateStatTable(&rc, p);
  693. }
  694. return rc;
  695. }
  696. /*
  697. ** Store the current database page-size in bytes in p->nPgsz.
  698. **
  699. ** If *pRc is non-zero when this function is called, it is a no-op.
  700. ** Otherwise, if an error occurs, an SQLite error code is stored in *pRc
  701. ** before returning.
  702. */
  703. static void fts3DatabasePageSize(int *pRc, Fts3Table *p){
  704. if( *pRc==SQLITE_OK ){
  705. int rc; /* Return code */
  706. char *zSql; /* SQL text "PRAGMA %Q.page_size" */
  707. sqlite3_stmt *pStmt; /* Compiled "PRAGMA %Q.page_size" statement */
  708. zSql = sqlite3_mprintf("PRAGMA %Q.page_size", p->zDb);
  709. if( !zSql ){
  710. rc = SQLITE_NOMEM;
  711. }else{
  712. rc = sqlite3_prepare(p->db, zSql, -1, &pStmt, 0);
  713. if( rc==SQLITE_OK ){
  714. sqlite3_step(pStmt);
  715. p->nPgsz = sqlite3_column_int(pStmt, 0);
  716. rc = sqlite3_finalize(pStmt);
  717. }else if( rc==SQLITE_AUTH ){
  718. p->nPgsz = 1024;
  719. rc = SQLITE_OK;
  720. }
  721. }
  722. assert( p->nPgsz>0 || rc!=SQLITE_OK );
  723. sqlite3_free(zSql);
  724. *pRc = rc;
  725. }
  726. }
  727. /*
  728. ** "Special" FTS4 arguments are column specifications of the following form:
  729. **
  730. ** <key> = <value>
  731. **
  732. ** There may not be whitespace surrounding the "=" character. The <value>
  733. ** term may be quoted, but the <key> may not.
  734. */
  735. static int fts3IsSpecialColumn(
  736. const char *z,
  737. int *pnKey,
  738. char **pzValue
  739. ){
  740. char *zValue;
  741. const char *zCsr = z;
  742. while( *zCsr!='=' ){
  743. if( *zCsr=='\0' ) return 0;
  744. zCsr++;
  745. }
  746. *pnKey = (int)(zCsr-z);
  747. zValue = sqlite3_mprintf("%s", &zCsr[1]);
  748. if( zValue ){
  749. sqlite3Fts3Dequote(zValue);
  750. }
  751. *pzValue = zValue;
  752. return 1;
  753. }
  754. /*
  755. ** Append the output of a printf() style formatting to an existing string.
  756. */
  757. static void fts3Appendf(
  758. int *pRc, /* IN/OUT: Error code */
  759. char **pz, /* IN/OUT: Pointer to string buffer */
  760. const char *zFormat, /* Printf format string to append */
  761. ... /* Arguments for printf format string */
  762. ){
  763. if( *pRc==SQLITE_OK ){
  764. va_list ap;
  765. char *z;
  766. va_start(ap, zFormat);
  767. z = sqlite3_vmprintf(zFormat, ap);
  768. va_end(ap);
  769. if( z && *pz ){
  770. char *z2 = sqlite3_mprintf("%s%s", *pz, z);
  771. sqlite3_free(z);
  772. z = z2;
  773. }
  774. if( z==0 ) *pRc = SQLITE_NOMEM;
  775. sqlite3_free(*pz);
  776. *pz = z;
  777. }
  778. }
  779. /*
  780. ** Return a copy of input string zInput enclosed in double-quotes (") and
  781. ** with all double quote characters escaped. For example:
  782. **
  783. ** fts3QuoteId("un \"zip\"") -> "un \"\"zip\"\""
  784. **
  785. ** The pointer returned points to memory obtained from sqlite3_malloc(). It
  786. ** is the callers responsibility to call sqlite3_free() to release this
  787. ** memory.
  788. */
  789. static char *fts3QuoteId(char const *zInput){
  790. sqlite3_int64 nRet;
  791. char *zRet;
  792. nRet = 2 + (int)strlen(zInput)*2 + 1;
  793. zRet = sqlite3_malloc64(nRet);
  794. if( zRet ){
  795. int i;
  796. char *z = zRet;
  797. *(z++) = '"';
  798. for(i=0; zInput[i]; i++){
  799. if( zInput[i]=='"' ) *(z++) = '"';
  800. *(z++) = zInput[i];
  801. }
  802. *(z++) = '"';
  803. *(z++) = '\0';
  804. }
  805. return zRet;
  806. }
  807. /*
  808. ** Return a list of comma separated SQL expressions and a FROM clause that
  809. ** could be used in a SELECT statement such as the following:
  810. **
  811. ** SELECT <list of expressions> FROM %_content AS x ...
  812. **
  813. ** to return the docid, followed by each column of text data in order
  814. ** from left to write. If parameter zFunc is not NULL, then instead of
  815. ** being returned directly each column of text data is passed to an SQL
  816. ** function named zFunc first. For example, if zFunc is "unzip" and the
  817. ** table has the three user-defined columns "a", "b", and "c", the following
  818. ** string is returned:
  819. **
  820. ** "docid, unzip(x.'a'), unzip(x.'b'), unzip(x.'c') FROM %_content AS x"
  821. **
  822. ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
  823. ** is the responsibility of the caller to eventually free it.
  824. **
  825. ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
  826. ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
  827. ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
  828. ** no error occurs, *pRc is left unmodified.
  829. */
  830. static char *fts3ReadExprList(Fts3Table *p, const char *zFunc, int *pRc){
  831. char *zRet = 0;
  832. char *zFree = 0;
  833. char *zFunction;
  834. int i;
  835. if( p->zContentTbl==0 ){
  836. if( !zFunc ){
  837. zFunction = "";
  838. }else{
  839. zFree = zFunction = fts3QuoteId(zFunc);
  840. }
  841. fts3Appendf(pRc, &zRet, "docid");
  842. for(i=0; i<p->nColumn; i++){
  843. fts3Appendf(pRc, &zRet, ",%s(x.'c%d%q')", zFunction, i, p->azColumn[i]);
  844. }
  845. if( p->zLanguageid ){
  846. fts3Appendf(pRc, &zRet, ", x.%Q", "langid");
  847. }
  848. sqlite3_free(zFree);
  849. }else{
  850. fts3Appendf(pRc, &zRet, "rowid");
  851. for(i=0; i<p->nColumn; i++){
  852. fts3Appendf(pRc, &zRet, ", x.'%q'", p->azColumn[i]);
  853. }
  854. if( p->zLanguageid ){
  855. fts3Appendf(pRc, &zRet, ", x.%Q", p->zLanguageid);
  856. }
  857. }
  858. fts3Appendf(pRc, &zRet, " FROM '%q'.'%q%s' AS x",
  859. p->zDb,
  860. (p->zContentTbl ? p->zContentTbl : p->zName),
  861. (p->zContentTbl ? "" : "_content")
  862. );
  863. return zRet;
  864. }
  865. /*
  866. ** Return a list of N comma separated question marks, where N is the number
  867. ** of columns in the %_content table (one for the docid plus one for each
  868. ** user-defined text column).
  869. **
  870. ** If argument zFunc is not NULL, then all but the first question mark
  871. ** is preceded by zFunc and an open bracket, and followed by a closed
  872. ** bracket. For example, if zFunc is "zip" and the FTS3 table has three
  873. ** user-defined text columns, the following string is returned:
  874. **
  875. ** "?, zip(?), zip(?), zip(?)"
  876. **
  877. ** The pointer returned points to a buffer allocated by sqlite3_malloc(). It
  878. ** is the responsibility of the caller to eventually free it.
  879. **
  880. ** If *pRc is not SQLITE_OK when this function is called, it is a no-op (and
  881. ** a NULL pointer is returned). Otherwise, if an OOM error is encountered
  882. ** by this function, NULL is returned and *pRc is set to SQLITE_NOMEM. If
  883. ** no error occurs, *pRc is left unmodified.
  884. */
  885. static char *fts3WriteExprList(Fts3Table *p, const char *zFunc, int *pRc){
  886. char *zRet = 0;
  887. char *zFree = 0;
  888. char *zFunction;
  889. int i;
  890. if( !zFunc ){
  891. zFunction = "";
  892. }else{
  893. zFree = zFunction = fts3QuoteId(zFunc);
  894. }
  895. fts3Appendf(pRc, &zRet, "?");
  896. for(i=0; i<p->nColumn; i++){
  897. fts3Appendf(pRc, &zRet, ",%s(?)", zFunction);
  898. }
  899. if( p->zLanguageid ){
  900. fts3Appendf(pRc, &zRet, ", ?");
  901. }
  902. sqlite3_free(zFree);
  903. return zRet;
  904. }
  905. /*
  906. ** Buffer z contains a positive integer value encoded as utf-8 text.
  907. ** Decode this value and store it in *pnOut, returning the number of bytes
  908. ** consumed. If an overflow error occurs return a negative value.
  909. */
  910. int sqlite3Fts3ReadInt(const char *z, int *pnOut){
  911. u64 iVal = 0;
  912. int i;
  913. for(i=0; z[i]>='0' && z[i]<='9'; i++){
  914. iVal = iVal*10 + (z[i] - '0');
  915. if( iVal>0x7FFFFFFF ) return -1;
  916. }
  917. *pnOut = (int)iVal;
  918. return i;
  919. }
  920. /*
  921. ** This function interprets the string at (*pp) as a non-negative integer
  922. ** value. It reads the integer and sets *pnOut to the value read, then
  923. ** sets *pp to point to the byte immediately following the last byte of
  924. ** the integer value.
  925. **
  926. ** Only decimal digits ('0'..'9') may be part of an integer value.
  927. **
  928. ** If *pp does not being with a decimal digit SQLITE_ERROR is returned and
  929. ** the output value undefined. Otherwise SQLITE_OK is returned.
  930. **
  931. ** This function is used when parsing the "prefix=" FTS4 parameter.
  932. */
  933. static int fts3GobbleInt(const char **pp, int *pnOut){
  934. const int MAX_NPREFIX = 10000000;
  935. int nInt = 0; /* Output value */
  936. int nByte;
  937. nByte = sqlite3Fts3ReadInt(*pp, &nInt);
  938. if( nInt>MAX_NPREFIX ){
  939. nInt = 0;
  940. }
  941. if( nByte==0 ){
  942. return SQLITE_ERROR;
  943. }
  944. *pnOut = nInt;
  945. *pp += nByte;
  946. return SQLITE_OK;
  947. }
  948. /*
  949. ** This function is called to allocate an array of Fts3Index structures
  950. ** representing the indexes maintained by the current FTS table. FTS tables
  951. ** always maintain the main "terms" index, but may also maintain one or
  952. ** more "prefix" indexes, depending on the value of the "prefix=" parameter
  953. ** (if any) specified as part of the CREATE VIRTUAL TABLE statement.
  954. **
  955. ** Argument zParam is passed the value of the "prefix=" option if one was
  956. ** specified, or NULL otherwise.
  957. **
  958. ** If no error occurs, SQLITE_OK is returned and *apIndex set to point to
  959. ** the allocated array. *pnIndex is set to the number of elements in the
  960. ** array. If an error does occur, an SQLite error code is returned.
  961. **
  962. ** Regardless of whether or not an error is returned, it is the responsibility
  963. ** of the caller to call sqlite3_free() on the output array to free it.
  964. */
  965. static int fts3PrefixParameter(
  966. const char *zParam, /* ABC in prefix=ABC parameter to parse */
  967. int *pnIndex, /* OUT: size of *apIndex[] array */
  968. struct Fts3Index **apIndex /* OUT: Array of indexes for this table */
  969. ){
  970. struct Fts3Index *aIndex; /* Allocated array */
  971. int nIndex = 1; /* Number of entries in array */
  972. if( zParam && zParam[0] ){
  973. const char *p;
  974. nIndex++;
  975. for(p=zParam; *p; p++){
  976. if( *p==',' ) nIndex++;
  977. }
  978. }
  979. aIndex = sqlite3_malloc64(sizeof(struct Fts3Index) * nIndex);
  980. *apIndex = aIndex;
  981. if( !aIndex ){
  982. return SQLITE_NOMEM;
  983. }
  984. memset(aIndex, 0, sizeof(struct Fts3Index) * nIndex);
  985. if( zParam ){
  986. const char *p = zParam;
  987. int i;
  988. for(i=1; i<nIndex; i++){
  989. int nPrefix = 0;
  990. if( fts3GobbleInt(&p, &nPrefix) ) return SQLITE_ERROR;
  991. assert( nPrefix>=0 );
  992. if( nPrefix==0 ){
  993. nIndex--;
  994. i--;
  995. }else{
  996. aIndex[i].nPrefix = nPrefix;
  997. }
  998. p++;
  999. }
  1000. }
  1001. *pnIndex = nIndex;
  1002. return SQLITE_OK;
  1003. }
  1004. /*
  1005. ** This function is called when initializing an FTS4 table that uses the
  1006. ** content=xxx option. It determines the number of and names of the columns
  1007. ** of the new FTS4 table.
  1008. **
  1009. ** The third argument passed to this function is the value passed to the
  1010. ** config=xxx option (i.e. "xxx"). This function queries the database for
  1011. ** a table of that name. If found, the output variables are populated
  1012. ** as follows:
  1013. **
  1014. ** *pnCol: Set to the number of columns table xxx has,
  1015. **
  1016. ** *pnStr: Set to the total amount of space required to store a copy
  1017. ** of each columns name, including the nul-terminator.
  1018. **
  1019. ** *pazCol: Set to point to an array of *pnCol strings. Each string is
  1020. ** the name of the corresponding column in table xxx. The array
  1021. ** and its contents are allocated using a single allocation. It
  1022. ** is the responsibility of the caller to free this allocation
  1023. ** by eventually passing the *pazCol value to sqlite3_free().
  1024. **
  1025. ** If the table cannot be found, an error code is returned and the output
  1026. ** variables are undefined. Or, if an OOM is encountered, SQLITE_NOMEM is
  1027. ** returned (and the output variables are undefined).
  1028. */
  1029. static int fts3ContentColumns(
  1030. sqlite3 *db, /* Database handle */
  1031. const char *zDb, /* Name of db (i.e. "main", "temp" etc.) */
  1032. const char *zTbl, /* Name of content table */
  1033. const char ***pazCol, /* OUT: Malloc'd array of column names */
  1034. int *pnCol, /* OUT: Size of array *pazCol */
  1035. int *pnStr, /* OUT: Bytes of string content */
  1036. char **pzErr /* OUT: error message */
  1037. ){
  1038. int rc = SQLITE_OK; /* Return code */
  1039. char *zSql; /* "SELECT *" statement on zTbl */
  1040. sqlite3_stmt *pStmt = 0; /* Compiled version of zSql */
  1041. zSql = sqlite3_mprintf("SELECT * FROM %Q.%Q", zDb, zTbl);
  1042. if( !zSql ){
  1043. rc = SQLITE_NOMEM;
  1044. }else{
  1045. rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
  1046. if( rc!=SQLITE_OK ){
  1047. sqlite3Fts3ErrMsg(pzErr, "%s", sqlite3_errmsg(db));
  1048. }
  1049. }
  1050. sqlite3_free(zSql);
  1051. if( rc==SQLITE_OK ){
  1052. const char **azCol; /* Output array */
  1053. sqlite3_int64 nStr = 0; /* Size of all column names (incl. 0x00) */
  1054. int nCol; /* Number of table columns */
  1055. int i; /* Used to iterate through columns */
  1056. /* Loop through the returned columns. Set nStr to the number of bytes of
  1057. ** space required to store a copy of each column name, including the
  1058. ** nul-terminator byte. */
  1059. nCol = sqlite3_column_count(pStmt);
  1060. for(i=0; i<nCol; i++){
  1061. const char *zCol = sqlite3_column_name(pStmt, i);
  1062. nStr += strlen(zCol) + 1;
  1063. }
  1064. /* Allocate and populate the array to return. */
  1065. azCol = (const char **)sqlite3_malloc64(sizeof(char *) * nCol + nStr);
  1066. if( azCol==0 ){
  1067. rc = SQLITE_NOMEM;
  1068. }else{
  1069. char *p = (char *)&azCol[nCol];
  1070. for(i=0; i<nCol; i++){
  1071. const char *zCol = sqlite3_column_name(pStmt, i);
  1072. int n = (int)strlen(zCol)+1;
  1073. memcpy(p, zCol, n);
  1074. azCol[i] = p;
  1075. p += n;
  1076. }
  1077. }
  1078. sqlite3_finalize(pStmt);
  1079. /* Set the output variables. */
  1080. *pnCol = nCol;
  1081. *pnStr = nStr;
  1082. *pazCol = azCol;
  1083. }
  1084. return rc;
  1085. }
  1086. /*
  1087. ** This function is the implementation of both the xConnect and xCreate
  1088. ** methods of the FTS3 virtual table.
  1089. **
  1090. ** The argv[] array contains the following:
  1091. **
  1092. ** argv[0] -> module name ("fts3" or "fts4")
  1093. ** argv[1] -> database name
  1094. ** argv[2] -> table name
  1095. ** argv[...] -> "column name" and other module argument fields.
  1096. */
  1097. static int fts3InitVtab(
  1098. int isCreate, /* True for xCreate, false for xConnect */
  1099. sqlite3 *db, /* The SQLite database connection */
  1100. void *pAux, /* Hash table containing tokenizers */
  1101. int argc, /* Number of elements in argv array */
  1102. const char * const *argv, /* xCreate/xConnect argument array */
  1103. sqlite3_vtab **ppVTab, /* Write the resulting vtab structure here */
  1104. char **pzErr /* Write any error message here */
  1105. ){
  1106. Fts3Hash *pHash = &((Fts3HashWrapper*)pAux)->hash;
  1107. Fts3Table *p = 0; /* Pointer to allocated vtab */
  1108. int rc = SQLITE_OK; /* Return code */
  1109. int i; /* Iterator variable */
  1110. sqlite3_int64 nByte; /* Size of allocation used for *p */
  1111. int iCol; /* Column index */
  1112. int nString = 0; /* Bytes required to hold all column names */
  1113. int nCol = 0; /* Number of columns in the FTS table */
  1114. char *zCsr; /* Space for holding column names */
  1115. int nDb; /* Bytes required to hold database name */
  1116. int nName; /* Bytes required to hold table name */
  1117. int isFts4 = (argv[0][3]=='4'); /* True for FTS4, false for FTS3 */
  1118. const char **aCol; /* Array of column names */
  1119. sqlite3_tokenizer *pTokenizer = 0; /* Tokenizer for this table */
  1120. int nIndex = 0; /* Size of aIndex[] array */
  1121. struct Fts3Index *aIndex = 0; /* Array of indexes for this table */
  1122. /* The results of parsing supported FTS4 key=value options: */
  1123. int bNoDocsize = 0; /* True to omit %_docsize table */
  1124. int bDescIdx = 0; /* True to store descending indexes */
  1125. char *zPrefix = 0; /* Prefix parameter value (or NULL) */
  1126. char *zCompress = 0; /* compress=? parameter (or NULL) */
  1127. char *zUncompress = 0; /* uncompress=? parameter (or NULL) */
  1128. char *zContent = 0; /* content=? parameter (or NULL) */
  1129. char *zLanguageid = 0; /* languageid=? parameter (or NULL) */
  1130. char **azNotindexed = 0; /* The set of notindexed= columns */
  1131. int nNotindexed = 0; /* Size of azNotindexed[] array */
  1132. assert( strlen(argv[0])==4 );
  1133. assert( (sqlite3_strnicmp(argv[0], "fts4", 4)==0 && isFts4)
  1134. || (sqlite3_strnicmp(argv[0], "fts3", 4)==0 && !isFts4)
  1135. );
  1136. nDb = (int)strlen(argv[1]) + 1;
  1137. nName = (int)strlen(argv[2]) + 1;
  1138. nByte = sizeof(const char *) * (argc-2);
  1139. aCol = (const char **)sqlite3_malloc64(nByte);
  1140. if( aCol ){
  1141. memset((void*)aCol, 0, nByte);
  1142. azNotindexed = (char **)sqlite3_malloc64(nByte);
  1143. }
  1144. if( azNotindexed ){
  1145. memset(azNotindexed, 0, nByte);
  1146. }
  1147. if( !aCol || !azNotindexed ){
  1148. rc = SQLITE_NOMEM;
  1149. goto fts3_init_out;
  1150. }
  1151. /* Loop through all of the arguments passed by the user to the FTS3/4
  1152. ** module (i.e. all the column names and special arguments). This loop
  1153. ** does the following:
  1154. **
  1155. ** + Figures out the number of columns the FTSX table will have, and
  1156. ** the number of bytes of space that must be allocated to store copies
  1157. ** of the column names.
  1158. **
  1159. ** + If there is a tokenizer specification included in the arguments,
  1160. ** initializes the tokenizer pTokenizer.
  1161. */
  1162. for(i=3; rc==SQLITE_OK && i<argc; i++){
  1163. char const *z = argv[i];
  1164. int nKey;
  1165. char *zVal;
  1166. /* Check if this is a tokenizer specification */
  1167. if( !pTokenizer
  1168. && strlen(z)>8
  1169. && 0==sqlite3_strnicmp(z, "tokenize", 8)
  1170. && 0==sqlite3Fts3IsIdChar(z[8])
  1171. ){
  1172. rc = sqlite3Fts3InitTokenizer(pHash, &z[9], &pTokenizer, pzErr);
  1173. }
  1174. /* Check if it is an FTS4 special argument. */
  1175. else if( isFts4 && fts3IsSpecialColumn(z, &nKey, &zVal) ){
  1176. struct Fts4Option {
  1177. const char *zOpt;
  1178. int nOpt;
  1179. } aFts4Opt[] = {
  1180. { "matchinfo", 9 }, /* 0 -> MATCHINFO */
  1181. { "prefix", 6 }, /* 1 -> PREFIX */
  1182. { "compress", 8 }, /* 2 -> COMPRESS */
  1183. { "uncompress", 10 }, /* 3 -> UNCOMPRESS */
  1184. { "order", 5 }, /* 4 -> ORDER */
  1185. { "content", 7 }, /* 5 -> CONTENT */
  1186. { "languageid", 10 }, /* 6 -> LANGUAGEID */
  1187. { "notindexed", 10 } /* 7 -> NOTINDEXED */
  1188. };
  1189. int iOpt;
  1190. if( !zVal ){
  1191. rc = SQLITE_NOMEM;
  1192. }else{
  1193. for(iOpt=0; iOpt<SizeofArray(aFts4Opt); iOpt++){
  1194. struct Fts4Option *pOp = &aFts4Opt[iOpt];
  1195. if( nKey==pOp->nOpt && !sqlite3_strnicmp(z, pOp->zOpt, pOp->nOpt) ){
  1196. break;
  1197. }
  1198. }
  1199. switch( iOpt ){
  1200. case 0: /* MATCHINFO */
  1201. if( strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "fts3", 4) ){
  1202. sqlite3Fts3ErrMsg(pzErr, "unrecognized matchinfo: %s", zVal);
  1203. rc = SQLITE_ERROR;
  1204. }
  1205. bNoDocsize = 1;
  1206. break;
  1207. case 1: /* PREFIX */
  1208. sqlite3_free(zPrefix);
  1209. zPrefix = zVal;
  1210. zVal = 0;
  1211. break;
  1212. case 2: /* COMPRESS */
  1213. sqlite3_free(zCompress);
  1214. zCompress = zVal;
  1215. zVal = 0;
  1216. break;
  1217. case 3: /* UNCOMPRESS */
  1218. sqlite3_free(zUncompress);
  1219. zUncompress = zVal;
  1220. zVal = 0;
  1221. break;
  1222. case 4: /* ORDER */
  1223. if( (strlen(zVal)!=3 || sqlite3_strnicmp(zVal, "asc", 3))
  1224. && (strlen(zVal)!=4 || sqlite3_strnicmp(zVal, "desc", 4))
  1225. ){
  1226. sqlite3Fts3ErrMsg(pzErr, "unrecognized order: %s", zVal);
  1227. rc = SQLITE_ERROR;
  1228. }
  1229. bDescIdx = (zVal[0]=='d' || zVal[0]=='D');
  1230. break;
  1231. case 5: /* CONTENT */
  1232. sqlite3_free(zContent);
  1233. zContent = zVal;
  1234. zVal = 0;
  1235. break;
  1236. case 6: /* LANGUAGEID */
  1237. assert( iOpt==6 );
  1238. sqlite3_free(zLanguageid);
  1239. zLanguageid = zVal;
  1240. zVal = 0;
  1241. break;
  1242. case 7: /* NOTINDEXED */
  1243. azNotindexed[nNotindexed++] = zVal;
  1244. zVal = 0;
  1245. break;
  1246. default:
  1247. assert( iOpt==SizeofArray(aFts4Opt) );
  1248. sqlite3Fts3ErrMsg(pzErr, "unrecognized parameter: %s", z);
  1249. rc = SQLITE_ERROR;
  1250. break;
  1251. }
  1252. sqlite3_free(zVal);
  1253. }
  1254. }
  1255. /* Otherwise, the argument is a column name. */
  1256. else {
  1257. nString += (int)(strlen(z) + 1);
  1258. aCol[nCol++] = z;
  1259. }
  1260. }
  1261. /* If a content=xxx option was specified, the following:
  1262. **
  1263. ** 1. Ignore any compress= and uncompress= options.
  1264. **
  1265. ** 2. If no column names were specified as part of the CREATE VIRTUAL
  1266. ** TABLE statement, use all columns from the content table.
  1267. */
  1268. if( rc==SQLITE_OK && zContent ){
  1269. sqlite3_free(zCompress);
  1270. sqlite3_free(zUncompress);
  1271. zCompress = 0;
  1272. zUncompress = 0;
  1273. if( nCol==0 ){
  1274. sqlite3_free((void*)aCol);
  1275. aCol = 0;
  1276. rc = fts3ContentColumns(db, argv[1], zContent,&aCol,&nCol,&nString,pzErr);
  1277. /* If a languageid= option was specified, remove the language id
  1278. ** column from the aCol[] array. */
  1279. if( rc==SQLITE_OK && zLanguageid ){
  1280. int j;
  1281. for(j=0; j<nCol; j++){
  1282. if( sqlite3_stricmp(zLanguageid, aCol[j])==0 ){
  1283. int k;
  1284. for(k=j; k<nCol; k++) aCol[k] = aCol[k+1];
  1285. nCol--;
  1286. break;
  1287. }
  1288. }
  1289. }
  1290. }
  1291. }
  1292. if( rc!=SQLITE_OK ) goto fts3_init_out;
  1293. if( nCol==0 ){
  1294. assert( nString==0 );
  1295. aCol[0] = "content";
  1296. nString = 8;
  1297. nCol = 1;
  1298. }
  1299. if( pTokenizer==0 ){
  1300. rc = sqlite3Fts3InitTokenizer(pHash, "simple", &pTokenizer, pzErr);
  1301. if( rc!=SQLITE_OK ) goto fts3_init_out;
  1302. }
  1303. assert( pTokenizer );
  1304. rc = fts3PrefixParameter(zPrefix, &nIndex, &aIndex);
  1305. if( rc==SQLITE_ERROR ){
  1306. assert( zPrefix );
  1307. sqlite3Fts3ErrMsg(pzErr, "error parsing prefix parameter: %s", zPrefix);
  1308. }
  1309. if( rc!=SQLITE_OK ) goto fts3_init_out;
  1310. /* Allocate and populate the Fts3Table structure. */
  1311. nByte = sizeof(Fts3Table) + /* Fts3Table */
  1312. nCol * sizeof(char *) + /* azColumn */
  1313. nIndex * sizeof(struct Fts3Index) + /* aIndex */
  1314. nCol * sizeof(u8) + /* abNotindexed */
  1315. nName + /* zName */
  1316. nDb + /* zDb */
  1317. nString; /* Space for azColumn strings */
  1318. p = (Fts3Table*)sqlite3_malloc64(nByte);
  1319. if( p==0 ){
  1320. rc = SQLITE_NOMEM;
  1321. goto fts3_init_out;
  1322. }
  1323. memset(p, 0, nByte);
  1324. p->db = db;
  1325. p->nColumn = nCol;
  1326. p->nPendingData = 0;
  1327. p->azColumn = (char **)&p[1];
  1328. p->pTokenizer = pTokenizer;
  1329. p->nMaxPendingData = FTS3_MAX_PENDING_DATA;
  1330. p->bHasDocsize = (isFts4 && bNoDocsize==0);
  1331. p->bHasStat = (u8)isFts4;
  1332. p->bFts4 = (u8)isFts4;
  1333. p->bDescIdx = (u8)bDescIdx;
  1334. p->nAutoincrmerge = 0xff; /* 0xff means setting unknown */
  1335. p->zContentTbl = zContent;
  1336. p->zLanguageid = zLanguageid;
  1337. zContent = 0;
  1338. zLanguageid = 0;
  1339. TESTONLY( p->inTransaction = -1 );
  1340. TESTONLY( p->mxSavepoint = -1 );
  1341. p->aIndex = (struct Fts3Index *)&p->azColumn[nCol];
  1342. memcpy(p->aIndex, aIndex, sizeof(struct Fts3Index) * nIndex);
  1343. p->nIndex = nIndex;
  1344. for(i=0; i<nIndex; i++){
  1345. fts3HashInit(&p->aIndex[i].hPending, FTS3_HASH_STRING, 1);
  1346. }
  1347. p->abNotindexed = (u8 *)&p->aIndex[nIndex];
  1348. /* Fill in the zName and zDb fields of the vtab structure. */
  1349. zCsr = (char *)&p->abNotindexed[nCol];
  1350. p->zName = zCsr;
  1351. memcpy(zCsr, argv[2], nName);
  1352. zCsr += nName;
  1353. p->zDb = zCsr;
  1354. memcpy(zCsr, argv[1], nDb);
  1355. zCsr += nDb;
  1356. /* Fill in the azColumn array */
  1357. for(iCol=0; iCol<nCol; iCol++){
  1358. char *z;
  1359. int n = 0;
  1360. z = (char *)sqlite3Fts3NextToken(aCol[iCol], &n);
  1361. if( n>0 ){
  1362. memcpy(zCsr, z, n);
  1363. }
  1364. zCsr[n] = '\0';
  1365. sqlite3Fts3Dequote(zCsr);
  1366. p->azColumn[iCol] = zCsr;
  1367. zCsr += n+1;
  1368. assert( zCsr <= &((char *)p)[nByte] );
  1369. }
  1370. /* Fill in the abNotindexed array */
  1371. for(iCol=0; iCol<nCol; iCol++){
  1372. int n = (int)strlen(p->azColumn[iCol]);
  1373. for(i=0; i<nNotindexed; i++){
  1374. char *zNot = azNotindexed[i];
  1375. if( zNot && n==(int)strlen(zNot)
  1376. && 0==sqlite3_strnicmp(p->azColumn[iCol], zNot, n)
  1377. ){
  1378. p->abNotindexed[iCol] = 1;
  1379. sqlite3_free(zNot);
  1380. azNotindexed[i] = 0;
  1381. }
  1382. }
  1383. }
  1384. for(i=0; i<nNotindexed; i++){
  1385. if( azNotindexed[i] ){
  1386. sqlite3Fts3ErrMsg(pzErr, "no such column: %s", azNotindexed[i]);
  1387. rc = SQLITE_ERROR;
  1388. }
  1389. }
  1390. if( rc==SQLITE_OK && (zCompress==0)!=(zUncompress==0) ){
  1391. char const *zMiss = (zCompress==0 ? "compress" : "uncompress");
  1392. rc = SQLITE_ERROR;
  1393. sqlite3Fts3ErrMsg(pzErr, "missing %s parameter in fts4 constructor", zMiss);
  1394. }
  1395. p->zReadExprlist = fts3ReadExprList(p, zUncompress, &rc);
  1396. p->zWriteExprlist = fts3WriteExprList(p, zCompress, &rc);
  1397. if( rc!=SQLITE_OK ) goto fts3_init_out;
  1398. /* If this is an xCreate call, create the underlying tables in the
  1399. ** database. TODO: For xConnect(), it could verify that said tables exist.
  1400. */
  1401. if( isCreate ){
  1402. rc = fts3CreateTables(p);
  1403. }
  1404. /* Check to see if a legacy fts3 table has been "upgraded" by the
  1405. ** addition of a %_stat table so that it can use incremental merge.
  1406. */
  1407. if( !isFts4 && !isCreate ){
  1408. p->bHasStat = 2;
  1409. }
  1410. /* Figure out the page-size for the database. This is required in order to
  1411. ** estimate the cost of loading large doclists from the database. */
  1412. fts3DatabasePageSize(&rc, p);
  1413. p->nNodeSize = p->nPgsz-35;
  1414. #if defined(SQLITE_DEBUG)||defined(SQLITE_TEST)
  1415. p->nMergeCount = FTS3_MERGE_COUNT;
  1416. #endif
  1417. /* Declare the table schema to SQLite. */
  1418. fts3DeclareVtab(&rc, p);
  1419. fts3_init_out:
  1420. sqlite3_free(zPrefix);
  1421. sqlite3_free(aIndex);
  1422. sqlite3_free(zCompress);
  1423. sqlite3_free(zUncompress);
  1424. sqlite3_free(zContent);
  1425. sqlite3_free(zLanguageid);
  1426. for(i=0; i<nNotindexed; i++) sqlite3_free(azNotindexed[i]);
  1427. sqlite3_free((void *)aCol);
  1428. sqlite3_free((void *)azNotindexed);
  1429. if( rc!=SQLITE_OK ){
  1430. if( p ){
  1431. fts3DisconnectMethod((sqlite3_vtab *)p);
  1432. }else if( pTokenizer ){
  1433. pTokenizer->pModule->xDestroy(pTokenizer);
  1434. }
  1435. }else{
  1436. assert( p->pSegments==0 );
  1437. *ppVTab = &p->base;
  1438. }
  1439. return rc;
  1440. }
  1441. /*
  1442. ** The xConnect() and xCreate() methods for the virtual table. All the
  1443. ** work is done in function fts3InitVtab().
  1444. */
  1445. static int fts3ConnectMethod(
  1446. sqlite3 *db, /* Database connection */
  1447. void *pAux, /* Pointer to tokenizer hash table */
  1448. int argc, /* Number of elements in argv array */
  1449. const char * const *argv, /* xCreate/xConnect argument array */
  1450. sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
  1451. char **pzErr /* OUT: sqlite3_malloc'd error message */
  1452. ){
  1453. return fts3InitVtab(0, db, pAux, argc, argv, ppVtab, pzErr);
  1454. }
  1455. static int fts3CreateMethod(
  1456. sqlite3 *db, /* Database connection */
  1457. void *pAux, /* Pointer to tokenizer hash table */
  1458. int argc, /* Number of elements in argv array */
  1459. const char * const *argv, /* xCreate/xConnect argument array */
  1460. sqlite3_vtab **ppVtab, /* OUT: New sqlite3_vtab object */
  1461. char **pzErr /* OUT: sqlite3_malloc'd error message */
  1462. ){
  1463. return fts3InitVtab(1, db, pAux, argc, argv, ppVtab, pzErr);
  1464. }
  1465. /*
  1466. ** Set the pIdxInfo->estimatedRows variable to nRow. Unless this
  1467. ** extension is currently being used by a version of SQLite too old to
  1468. ** support estimatedRows. In that case this function is a no-op.
  1469. */
  1470. static void fts3SetEstimatedRows(sqlite3_index_info *pIdxInfo, i64 nRow){
  1471. #if SQLITE_VERSION_NUMBER>=3008002
  1472. if( sqlite3_libversion_number()>=3008002 ){
  1473. pIdxInfo->estimatedRows = nRow;
  1474. }
  1475. #endif
  1476. }
  1477. /*
  1478. ** Set the SQLITE_INDEX_SCAN_UNIQUE flag in pIdxInfo->flags. Unless this
  1479. ** extension is currently being used by a version of SQLite too old to
  1480. ** support index-info flags. In that case this function is a no-op.
  1481. */
  1482. static void fts3SetUniqueFlag(sqlite3_index_info *pIdxInfo){
  1483. #if SQLITE_VERSION_NUMBER>=3008012
  1484. if( sqlite3_libversion_number()>=3008012 ){
  1485. pIdxInfo->idxFlags |= SQLITE_INDEX_SCAN_UNIQUE;
  1486. }
  1487. #endif
  1488. }
  1489. /*
  1490. ** Implementation of the xBestIndex method for FTS3 tables. There
  1491. ** are three possible strategies, in order of preference:
  1492. **
  1493. ** 1. Direct lookup by rowid or docid.
  1494. ** 2. Full-text search using a MATCH operator on a non-docid column.
  1495. ** 3. Linear scan of %_content table.
  1496. */
  1497. static int fts3BestIndexMethod(sqlite3_vtab *pVTab, sqlite3_index_info *pInfo){
  1498. Fts3Table *p = (Fts3Table *)pVTab;
  1499. int i; /* Iterator variable */
  1500. int iCons = -1; /* Index of constraint to use */
  1501. int iLangidCons = -1; /* Index of langid=x constraint, if present */
  1502. int iDocidGe = -1; /* Index of docid>=x constraint, if present */
  1503. int iDocidLe = -1; /* Index of docid<=x constraint, if present */
  1504. int iIdx;
  1505. if( p->bLock ){
  1506. return SQLITE_ERROR;
  1507. }
  1508. /* By default use a full table scan. This is an expensive option,
  1509. ** so search through the constraints to see if a more efficient
  1510. ** strategy is possible.
  1511. */
  1512. pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
  1513. pInfo->estimatedCost = 5000000;
  1514. for(i=0; i<pInfo->nConstraint; i++){
  1515. int bDocid; /* True if this constraint is on docid */
  1516. struct sqlite3_index_constraint *pCons = &pInfo->aConstraint[i];
  1517. if( pCons->usable==0 ){
  1518. if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH ){
  1519. /* There exists an unusable MATCH constraint. This means that if
  1520. ** the planner does elect to use the results of this call as part
  1521. ** of the overall query plan the user will see an "unable to use
  1522. ** function MATCH in the requested context" error. To discourage
  1523. ** this, return a very high cost here. */
  1524. pInfo->idxNum = FTS3_FULLSCAN_SEARCH;
  1525. pInfo->estimatedCost = 1e50;
  1526. fts3SetEstimatedRows(pInfo, ((sqlite3_int64)1) << 50);
  1527. return SQLITE_OK;
  1528. }
  1529. continue;
  1530. }
  1531. bDocid = (pCons->iColumn<0 || pCons->iColumn==p->nColumn+1);
  1532. /* A direct lookup on the rowid or docid column. Assign a cost of 1.0. */
  1533. if( iCons<0 && pCons->op==SQLITE_INDEX_CONSTRAINT_EQ && bDocid ){
  1534. pInfo->idxNum = FTS3_DOCID_SEARCH;
  1535. pInfo->estimatedCost = 1.0;
  1536. iCons = i;
  1537. }
  1538. /* A MATCH constraint. Use a full-text search.
  1539. **
  1540. ** If there is more than one MATCH constraint available, use the first
  1541. ** one encountered. If there is both a MATCH constraint and a direct
  1542. ** rowid/docid lookup, prefer the MATCH strategy. This is done even
  1543. ** though the rowid/docid lookup is faster than a MATCH query, selecting
  1544. ** it would lead to an "unable to use function MATCH in the requested
  1545. ** context" error.
  1546. */
  1547. if( pCons->op==SQLITE_INDEX_CONSTRAINT_MATCH
  1548. && pCons->iColumn>=0 && pCons->iColumn<=p->nColumn
  1549. ){
  1550. pInfo->idxNum = FTS3_FULLTEXT_SEARCH + pCons->iColumn;
  1551. pInfo->estimatedCost = 2.0;
  1552. iCons = i;
  1553. }
  1554. /* Equality constraint on the langid column */
  1555. if( pCons->op==SQLITE_INDEX_CONSTRAINT_EQ
  1556. && pCons->iColumn==p->nColumn + 2
  1557. ){
  1558. iLangidCons = i;
  1559. }
  1560. if( bDocid ){
  1561. switch( pCons->op ){
  1562. case SQLITE_INDEX_CONSTRAINT_GE:
  1563. case SQLITE_INDEX_CONSTRAINT_GT:
  1564. iDocidGe = i;
  1565. break;
  1566. case SQLITE_INDEX_CONSTRAINT_LE:
  1567. case SQLITE_INDEX_CONSTRAINT_LT:
  1568. iDocidLe = i;
  1569. break;
  1570. }
  1571. }
  1572. }
  1573. /* If using a docid=? or rowid=? strategy, set the UNIQUE flag. */
  1574. if( pInfo->idxNum==FTS3_DOCID_SEARCH ) fts3SetUniqueFlag(pInfo);
  1575. iIdx = 1;
  1576. if( iCons>=0 ){
  1577. pInfo->aConstraintUsage[iCons].argvIndex = iIdx++;
  1578. pInfo->aConstraintUsage[iCons].omit = 1;
  1579. }
  1580. if( iLangidCons>=0 ){
  1581. pInfo->idxNum |= FTS3_HAVE_LANGID;
  1582. pInfo->aConstraintUsage[iLangidCons].argvIndex = iIdx++;
  1583. }
  1584. if( iDocidGe>=0 ){
  1585. pInfo->idxNum |= FTS3_HAVE_DOCID_GE;
  1586. pInfo->aConstraintUsage[iDocidGe].argvIndex = iIdx++;
  1587. }
  1588. if( iDocidLe>=0 ){
  1589. pInfo->idxNum |= FTS3_HAVE_DOCID_LE;
  1590. pInfo->aConstraintUsage[iDocidLe].argvIndex = iIdx++;
  1591. }
  1592. /* Regardless of the strategy selected, FTS can deliver rows in rowid (or
  1593. ** docid) order. Both ascending and descending are possible.
  1594. */
  1595. if( pInfo->nOrderBy==1 ){
  1596. struct sqlite3_index_orderby *pOrder = &pInfo->aOrderBy[0];
  1597. if( pOrder->iColumn<0 || pOrder->iColumn==p->nColumn+1 ){
  1598. if( pOrder->desc ){
  1599. pInfo->idxStr = "DESC";
  1600. }else{
  1601. pInfo->idxStr = "ASC";
  1602. }
  1603. pInfo->orderByConsumed = 1;
  1604. }
  1605. }
  1606. assert( p->pSegments==0 );
  1607. return SQLITE_OK;
  1608. }
  1609. /*
  1610. ** Implementation of xOpen method.
  1611. */
  1612. static int fts3OpenMethod(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCsr){
  1613. sqlite3_vtab_cursor *pCsr; /* Allocated cursor */
  1614. UNUSED_PARAMETER(pVTab);
  1615. /* Allocate a buffer large enough for an Fts3Cursor structure. If the
  1616. ** allocation succeeds, zero it and return SQLITE_OK. Otherwise,
  1617. ** if the allocation fails, return SQLITE_NOMEM.
  1618. */
  1619. *ppCsr = pCsr = (sqlite3_vtab_cursor *)sqlite3_malloc(sizeof(Fts3Cursor));
  1620. if( !pCsr ){
  1621. return SQLITE_NOMEM;
  1622. }
  1623. memset(pCsr, 0, sizeof(Fts3Cursor));
  1624. return SQLITE_OK;
  1625. }
  1626. /*
  1627. ** Finalize the statement handle at pCsr->pStmt.
  1628. **
  1629. ** Or, if that statement handle is one created by fts3CursorSeekStmt(),
  1630. ** and the Fts3Table.pSeekStmt slot is currently NULL, save the statement
  1631. ** pointer there instead of finalizing it.
  1632. */
  1633. static void fts3CursorFinalizeStmt(Fts3Cursor *pCsr){
  1634. if( pCsr->bSeekStmt ){
  1635. Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
  1636. if( p->pSeekStmt==0 ){
  1637. p->pSeekStmt = pCsr->pStmt;
  1638. sqlite3_reset(pCsr->pStmt);
  1639. pCsr->pStmt = 0;
  1640. }
  1641. pCsr->bSeekStmt = 0;
  1642. }
  1643. sqlite3_finalize(pCsr->pStmt);
  1644. }
  1645. /*
  1646. ** Free all resources currently held by the cursor passed as the only
  1647. ** argument.
  1648. */
  1649. static void fts3ClearCursor(Fts3Cursor *pCsr){
  1650. fts3CursorFinalizeStmt(pCsr);
  1651. sqlite3Fts3FreeDeferredTokens(pCsr);
  1652. sqlite3_free(pCsr->aDoclist);
  1653. sqlite3Fts3MIBufferFree(pCsr->pMIBuffer);
  1654. sqlite3Fts3ExprFree(pCsr->pExpr);
  1655. memset(&(&pCsr->base)[1], 0, sizeof(Fts3Cursor)-sizeof(sqlite3_vtab_cursor));
  1656. }
  1657. /*
  1658. ** Close the cursor. For additional information see the documentation
  1659. ** on the xClose method of the virtual table interface.
  1660. */
  1661. static int fts3CloseMethod(sqlite3_vtab_cursor *pCursor){
  1662. Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
  1663. assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  1664. fts3ClearCursor(pCsr);
  1665. assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  1666. sqlite3_free(pCsr);
  1667. return SQLITE_OK;
  1668. }
  1669. /*
  1670. ** If pCsr->pStmt has not been prepared (i.e. if pCsr->pStmt==0), then
  1671. ** compose and prepare an SQL statement of the form:
  1672. **
  1673. ** "SELECT <columns> FROM %_content WHERE rowid = ?"
  1674. **
  1675. ** (or the equivalent for a content=xxx table) and set pCsr->pStmt to
  1676. ** it. If an error occurs, return an SQLite error code.
  1677. */
  1678. static int fts3CursorSeekStmt(Fts3Cursor *pCsr){
  1679. int rc = SQLITE_OK;
  1680. if( pCsr->pStmt==0 ){
  1681. Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
  1682. char *zSql;
  1683. if( p->pSeekStmt ){
  1684. pCsr->pStmt = p->pSeekStmt;
  1685. p->pSeekStmt = 0;
  1686. }else{
  1687. zSql = sqlite3_mprintf("SELECT %s WHERE rowid = ?", p->zReadExprlist);
  1688. if( !zSql ) return SQLITE_NOMEM;
  1689. p->bLock++;
  1690. rc = sqlite3_prepare_v3(
  1691. p->db, zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0
  1692. );
  1693. p->bLock--;
  1694. sqlite3_free(zSql);
  1695. }
  1696. if( rc==SQLITE_OK ) pCsr->bSeekStmt = 1;
  1697. }
  1698. return rc;
  1699. }
  1700. /*
  1701. ** Position the pCsr->pStmt statement so that it is on the row
  1702. ** of the %_content table that contains the last match. Return
  1703. ** SQLITE_OK on success.
  1704. */
  1705. static int fts3CursorSeek(sqlite3_context *pContext, Fts3Cursor *pCsr){
  1706. int rc = SQLITE_OK;
  1707. if( pCsr->isRequireSeek ){
  1708. rc = fts3CursorSeekStmt(pCsr);
  1709. if( rc==SQLITE_OK ){
  1710. Fts3Table *pTab = (Fts3Table*)pCsr->base.pVtab;
  1711. pTab->bLock++;
  1712. sqlite3_bind_int64(pCsr->pStmt, 1, pCsr->iPrevId);
  1713. pCsr->isRequireSeek = 0;
  1714. if( SQLITE_ROW==sqlite3_step(pCsr->pStmt) ){
  1715. pTab->bLock--;
  1716. return SQLITE_OK;
  1717. }else{
  1718. pTab->bLock--;
  1719. rc = sqlite3_reset(pCsr->pStmt);
  1720. if( rc==SQLITE_OK && ((Fts3Table *)pCsr->base.pVtab)->zContentTbl==0 ){
  1721. /* If no row was found and no error has occurred, then the %_content
  1722. ** table is missing a row that is present in the full-text index.
  1723. ** The data structures are corrupt. */
  1724. rc = FTS_CORRUPT_VTAB;
  1725. pCsr->isEof = 1;
  1726. }
  1727. }
  1728. }
  1729. }
  1730. if( rc!=SQLITE_OK && pContext ){
  1731. sqlite3_result_error_code(pContext, rc);
  1732. }
  1733. return rc;
  1734. }
  1735. /*
  1736. ** This function is used to process a single interior node when searching
  1737. ** a b-tree for a term or term prefix. The node data is passed to this
  1738. ** function via the zNode/nNode parameters. The term to search for is
  1739. ** passed in zTerm/nTerm.
  1740. **
  1741. ** If piFirst is not NULL, then this function sets *piFirst to the blockid
  1742. ** of the child node that heads the sub-tree that may contain the term.
  1743. **
  1744. ** If piLast is not NULL, then *piLast is set to the right-most child node
  1745. ** that heads a sub-tree that may contain a term for which zTerm/nTerm is
  1746. ** a prefix.
  1747. **
  1748. ** If an OOM error occurs, SQLITE_NOMEM is returned. Otherwise, SQLITE_OK.
  1749. */
  1750. static int fts3ScanInteriorNode(
  1751. const char *zTerm, /* Term to select leaves for */
  1752. int nTerm, /* Size of term zTerm in bytes */
  1753. const char *zNode, /* Buffer containing segment interior node */
  1754. int nNode, /* Size of buffer at zNode */
  1755. sqlite3_int64 *piFirst, /* OUT: Selected child node */
  1756. sqlite3_int64 *piLast /* OUT: Selected child node */
  1757. ){
  1758. int rc = SQLITE_OK; /* Return code */
  1759. const char *zCsr = zNode; /* Cursor to iterate through node */
  1760. const char *zEnd = &zCsr[nNode];/* End of interior node buffer */
  1761. char *zBuffer = 0; /* Buffer to load terms into */
  1762. i64 nAlloc = 0; /* Size of allocated buffer */
  1763. int isFirstTerm = 1; /* True when processing first term on page */
  1764. u64 iChild; /* Block id of child node to descend to */
  1765. int nBuffer = 0; /* Total term size */
  1766. /* Skip over the 'height' varint that occurs at the start of every
  1767. ** interior node. Then load the blockid of the left-child of the b-tree
  1768. ** node into variable iChild.
  1769. **
  1770. ** Even if the data structure on disk is corrupted, this (reading two
  1771. ** varints from the buffer) does not risk an overread. If zNode is a
  1772. ** root node, then the buffer comes from a SELECT statement. SQLite does
  1773. ** not make this guarantee explicitly, but in practice there are always
  1774. ** either more than 20 bytes of allocated space following the nNode bytes of
  1775. ** contents, or two zero bytes. Or, if the node is read from the %_segments
  1776. ** table, then there are always 20 bytes of zeroed padding following the
  1777. ** nNode bytes of content (see sqlite3Fts3ReadBlock() for details).
  1778. */
  1779. zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild);
  1780. zCsr += sqlite3Fts3GetVarintU(zCsr, &iChild);
  1781. if( zCsr>zEnd ){
  1782. return FTS_CORRUPT_VTAB;
  1783. }
  1784. while( zCsr<zEnd && (piFirst || piLast) ){
  1785. int cmp; /* memcmp() result */
  1786. int nSuffix; /* Size of term suffix */
  1787. int nPrefix = 0; /* Size of term prefix */
  1788. /* Load the next term on the node into zBuffer. Use realloc() to expand
  1789. ** the size of zBuffer if required. */
  1790. if( !isFirstTerm ){
  1791. zCsr += fts3GetVarint32(zCsr, &nPrefix);
  1792. if( nPrefix>nBuffer ){
  1793. rc = FTS_CORRUPT_VTAB;
  1794. goto finish_scan;
  1795. }
  1796. }
  1797. isFirstTerm = 0;
  1798. zCsr += fts3GetVarint32(zCsr, &nSuffix);
  1799. assert( nPrefix>=0 && nSuffix>=0 );
  1800. if( nPrefix>zCsr-zNode || nSuffix>zEnd-zCsr || nSuffix==0 ){
  1801. rc = FTS_CORRUPT_VTAB;
  1802. goto finish_scan;
  1803. }
  1804. if( (i64)nPrefix+nSuffix>nAlloc ){
  1805. char *zNew;
  1806. nAlloc = ((i64)nPrefix+nSuffix) * 2;
  1807. zNew = (char *)sqlite3_realloc64(zBuffer, nAlloc);
  1808. if( !zNew ){
  1809. rc = SQLITE_NOMEM;
  1810. goto finish_scan;
  1811. }
  1812. zBuffer = zNew;
  1813. }
  1814. assert( zBuffer );
  1815. memcpy(&zBuffer[nPrefix], zCsr, nSuffix);
  1816. nBuffer = nPrefix + nSuffix;
  1817. zCsr += nSuffix;
  1818. /* Compare the term we are searching for with the term just loaded from
  1819. ** the interior node. If the specified term is greater than or equal
  1820. ** to the term from the interior node, then all terms on the sub-tree
  1821. ** headed by node iChild are smaller than zTerm. No need to search
  1822. ** iChild.
  1823. **
  1824. ** If the interior node term is larger than the specified term, then
  1825. ** the tree headed by iChild may contain the specified term.
  1826. */
  1827. cmp = memcmp(zTerm, zBuffer, (nBuffer>nTerm ? nTerm : nBuffer));
  1828. if( piFirst && (cmp<0 || (cmp==0 && nBuffer>nTerm)) ){
  1829. *piFirst = (i64)iChild;
  1830. piFirst = 0;
  1831. }
  1832. if( piLast && cmp<0 ){
  1833. *piLast = (i64)iChild;
  1834. piLast = 0;
  1835. }
  1836. iChild++;
  1837. };
  1838. if( piFirst ) *piFirst = (i64)iChild;
  1839. if( piLast ) *piLast = (i64)iChild;
  1840. finish_scan:
  1841. sqlite3_free(zBuffer);
  1842. return rc;
  1843. }
  1844. /*
  1845. ** The buffer pointed to by argument zNode (size nNode bytes) contains an
  1846. ** interior node of a b-tree segment. The zTerm buffer (size nTerm bytes)
  1847. ** contains a term. This function searches the sub-tree headed by the zNode
  1848. ** node for the range of leaf nodes that may contain the specified term
  1849. ** or terms for which the specified term is a prefix.
  1850. **
  1851. ** If piLeaf is not NULL, then *piLeaf is set to the blockid of the
  1852. ** left-most leaf node in the tree that may contain the specified term.
  1853. ** If piLeaf2 is not NULL, then *piLeaf2 is set to the blockid of the
  1854. ** right-most leaf node that may contain a term for which the specified
  1855. ** term is a prefix.
  1856. **
  1857. ** It is possible that the range of returned leaf nodes does not contain
  1858. ** the specified term or any terms for which it is a prefix. However, if the
  1859. ** segment does contain any such terms, they are stored within the identified
  1860. ** range. Because this function only inspects interior segment nodes (and
  1861. ** never loads leaf nodes into memory), it is not possible to be sure.
  1862. **
  1863. ** If an error occurs, an error code other than SQLITE_OK is returned.
  1864. */
  1865. static int fts3SelectLeaf(
  1866. Fts3Table *p, /* Virtual table handle */
  1867. const char *zTerm, /* Term to select leaves for */
  1868. int nTerm, /* Size of term zTerm in bytes */
  1869. const char *zNode, /* Buffer containing segment interior node */
  1870. int nNode, /* Size of buffer at zNode */
  1871. sqlite3_int64 *piLeaf, /* Selected leaf node */
  1872. sqlite3_int64 *piLeaf2 /* Selected leaf node */
  1873. ){
  1874. int rc = SQLITE_OK; /* Return code */
  1875. int iHeight; /* Height of this node in tree */
  1876. assert( piLeaf || piLeaf2 );
  1877. fts3GetVarint32(zNode, &iHeight);
  1878. rc = fts3ScanInteriorNode(zTerm, nTerm, zNode, nNode, piLeaf, piLeaf2);
  1879. assert_fts3_nc( !piLeaf2 || !piLeaf || rc!=SQLITE_OK || (*piLeaf<=*piLeaf2) );
  1880. if( rc==SQLITE_OK && iHeight>1 ){
  1881. char *zBlob = 0; /* Blob read from %_segments table */
  1882. int nBlob = 0; /* Size of zBlob in bytes */
  1883. if( piLeaf && piLeaf2 && (*piLeaf!=*piLeaf2) ){
  1884. rc = sqlite3Fts3ReadBlock(p, *piLeaf, &zBlob, &nBlob, 0);
  1885. if( rc==SQLITE_OK ){
  1886. rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, 0);
  1887. }
  1888. sqlite3_free(zBlob);
  1889. piLeaf = 0;
  1890. zBlob = 0;
  1891. }
  1892. if( rc==SQLITE_OK ){
  1893. rc = sqlite3Fts3ReadBlock(p, piLeaf?*piLeaf:*piLeaf2, &zBlob, &nBlob, 0);
  1894. }
  1895. if( rc==SQLITE_OK ){
  1896. int iNewHeight = 0;
  1897. fts3GetVarint32(zBlob, &iNewHeight);
  1898. if( iNewHeight>=iHeight ){
  1899. rc = FTS_CORRUPT_VTAB;
  1900. }else{
  1901. rc = fts3SelectLeaf(p, zTerm, nTerm, zBlob, nBlob, piLeaf, piLeaf2);
  1902. }
  1903. }
  1904. sqlite3_free(zBlob);
  1905. }
  1906. return rc;
  1907. }
  1908. /*
  1909. ** This function is used to create delta-encoded serialized lists of FTS3
  1910. ** varints. Each call to this function appends a single varint to a list.
  1911. */
  1912. static void fts3PutDeltaVarint(
  1913. char **pp, /* IN/OUT: Output pointer */
  1914. sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
  1915. sqlite3_int64 iVal /* Write this value to the list */
  1916. ){
  1917. assert_fts3_nc( iVal-*piPrev > 0 || (*piPrev==0 && iVal==0) );
  1918. *pp += sqlite3Fts3PutVarint(*pp, iVal-*piPrev);
  1919. *piPrev = iVal;
  1920. }
  1921. /*
  1922. ** When this function is called, *ppPoslist is assumed to point to the
  1923. ** start of a position-list. After it returns, *ppPoslist points to the
  1924. ** first byte after the position-list.
  1925. **
  1926. ** A position list is list of positions (delta encoded) and columns for
  1927. ** a single document record of a doclist. So, in other words, this
  1928. ** routine advances *ppPoslist so that it points to the next docid in
  1929. ** the doclist, or to the first byte past the end of the doclist.
  1930. **
  1931. ** If pp is not NULL, then the contents of the position list are copied
  1932. ** to *pp. *pp is set to point to the first byte past the last byte copied
  1933. ** before this function returns.
  1934. */
  1935. static void fts3PoslistCopy(char **pp, char **ppPoslist){
  1936. char *pEnd = *ppPoslist;
  1937. char c = 0;
  1938. /* The end of a position list is marked by a zero encoded as an FTS3
  1939. ** varint. A single POS_END (0) byte. Except, if the 0 byte is preceded by
  1940. ** a byte with the 0x80 bit set, then it is not a varint 0, but the tail
  1941. ** of some other, multi-byte, value.
  1942. **
  1943. ** The following while-loop moves pEnd to point to the first byte that is not
  1944. ** immediately preceded by a byte with the 0x80 bit set. Then increments
  1945. ** pEnd once more so that it points to the byte immediately following the
  1946. ** last byte in the position-list.
  1947. */
  1948. while( *pEnd | c ){
  1949. c = *pEnd++ & 0x80;
  1950. testcase( c!=0 && (*pEnd)==0 );
  1951. }
  1952. pEnd++; /* Advance past the POS_END terminator byte */
  1953. if( pp ){
  1954. int n = (int)(pEnd - *ppPoslist);
  1955. char *p = *pp;
  1956. memcpy(p, *ppPoslist, n);
  1957. p += n;
  1958. *pp = p;
  1959. }
  1960. *ppPoslist = pEnd;
  1961. }
  1962. /*
  1963. ** When this function is called, *ppPoslist is assumed to point to the
  1964. ** start of a column-list. After it returns, *ppPoslist points to the
  1965. ** to the terminator (POS_COLUMN or POS_END) byte of the column-list.
  1966. **
  1967. ** A column-list is list of delta-encoded positions for a single column
  1968. ** within a single document within a doclist.
  1969. **
  1970. ** The column-list is terminated either by a POS_COLUMN varint (1) or
  1971. ** a POS_END varint (0). This routine leaves *ppPoslist pointing to
  1972. ** the POS_COLUMN or POS_END that terminates the column-list.
  1973. **
  1974. ** If pp is not NULL, then the contents of the column-list are copied
  1975. ** to *pp. *pp is set to point to the first byte past the last byte copied
  1976. ** before this function returns. The POS_COLUMN or POS_END terminator
  1977. ** is not copied into *pp.
  1978. */
  1979. static void fts3ColumnlistCopy(char **pp, char **ppPoslist){
  1980. char *pEnd = *ppPoslist;
  1981. char c = 0;
  1982. /* A column-list is terminated by either a 0x01 or 0x00 byte that is
  1983. ** not part of a multi-byte varint.
  1984. */
  1985. while( 0xFE & (*pEnd | c) ){
  1986. c = *pEnd++ & 0x80;
  1987. testcase( c!=0 && ((*pEnd)&0xfe)==0 );
  1988. }
  1989. if( pp ){
  1990. int n = (int)(pEnd - *ppPoslist);
  1991. char *p = *pp;
  1992. memcpy(p, *ppPoslist, n);
  1993. p += n;
  1994. *pp = p;
  1995. }
  1996. *ppPoslist = pEnd;
  1997. }
  1998. /*
  1999. ** Value used to signify the end of an position-list. This must be
  2000. ** as large or larger than any value that might appear on the
  2001. ** position-list, even a position list that has been corrupted.
  2002. */
  2003. #define POSITION_LIST_END LARGEST_INT64
  2004. /*
  2005. ** This function is used to help parse position-lists. When this function is
  2006. ** called, *pp may point to the start of the next varint in the position-list
  2007. ** being parsed, or it may point to 1 byte past the end of the position-list
  2008. ** (in which case **pp will be a terminator bytes POS_END (0) or
  2009. ** (1)).
  2010. **
  2011. ** If *pp points past the end of the current position-list, set *pi to
  2012. ** POSITION_LIST_END and return. Otherwise, read the next varint from *pp,
  2013. ** increment the current value of *pi by the value read, and set *pp to
  2014. ** point to the next value before returning.
  2015. **
  2016. ** Before calling this routine *pi must be initialized to the value of
  2017. ** the previous position, or zero if we are reading the first position
  2018. ** in the position-list. Because positions are delta-encoded, the value
  2019. ** of the previous position is needed in order to compute the value of
  2020. ** the next position.
  2021. */
  2022. static void fts3ReadNextPos(
  2023. char **pp, /* IN/OUT: Pointer into position-list buffer */
  2024. sqlite3_int64 *pi /* IN/OUT: Value read from position-list */
  2025. ){
  2026. if( (**pp)&0xFE ){
  2027. int iVal;
  2028. *pp += fts3GetVarint32((*pp), &iVal);
  2029. *pi += iVal;
  2030. *pi -= 2;
  2031. }else{
  2032. *pi = POSITION_LIST_END;
  2033. }
  2034. }
  2035. /*
  2036. ** If parameter iCol is not 0, write an POS_COLUMN (1) byte followed by
  2037. ** the value of iCol encoded as a varint to *pp. This will start a new
  2038. ** column list.
  2039. **
  2040. ** Set *pp to point to the byte just after the last byte written before
  2041. ** returning (do not modify it if iCol==0). Return the total number of bytes
  2042. ** written (0 if iCol==0).
  2043. */
  2044. static int fts3PutColNumber(char **pp, int iCol){
  2045. int n = 0; /* Number of bytes written */
  2046. if( iCol ){
  2047. char *p = *pp; /* Output pointer */
  2048. n = 1 + sqlite3Fts3PutVarint(&p[1], iCol);
  2049. *p = 0x01;
  2050. *pp = &p[n];
  2051. }
  2052. return n;
  2053. }
  2054. /*
  2055. ** Compute the union of two position lists. The output written
  2056. ** into *pp contains all positions of both *pp1 and *pp2 in sorted
  2057. ** order and with any duplicates removed. All pointers are
  2058. ** updated appropriately. The caller is responsible for insuring
  2059. ** that there is enough space in *pp to hold the complete output.
  2060. */
  2061. static int fts3PoslistMerge(
  2062. char **pp, /* Output buffer */
  2063. char **pp1, /* Left input list */
  2064. char **pp2 /* Right input list */
  2065. ){
  2066. char *p = *pp;
  2067. char *p1 = *pp1;
  2068. char *p2 = *pp2;
  2069. while( *p1 || *p2 ){
  2070. int iCol1; /* The current column index in pp1 */
  2071. int iCol2; /* The current column index in pp2 */
  2072. if( *p1==POS_COLUMN ){
  2073. fts3GetVarint32(&p1[1], &iCol1);
  2074. if( iCol1==0 ) return FTS_CORRUPT_VTAB;
  2075. }
  2076. else if( *p1==POS_END ) iCol1 = 0x7fffffff;
  2077. else iCol1 = 0;
  2078. if( *p2==POS_COLUMN ){
  2079. fts3GetVarint32(&p2[1], &iCol2);
  2080. if( iCol2==0 ) return FTS_CORRUPT_VTAB;
  2081. }
  2082. else if( *p2==POS_END ) iCol2 = 0x7fffffff;
  2083. else iCol2 = 0;
  2084. if( iCol1==iCol2 ){
  2085. sqlite3_int64 i1 = 0; /* Last position from pp1 */
  2086. sqlite3_int64 i2 = 0; /* Last position from pp2 */
  2087. sqlite3_int64 iPrev = 0;
  2088. int n = fts3PutColNumber(&p, iCol1);
  2089. p1 += n;
  2090. p2 += n;
  2091. /* At this point, both p1 and p2 point to the start of column-lists
  2092. ** for the same column (the column with index iCol1 and iCol2).
  2093. ** A column-list is a list of non-negative delta-encoded varints, each
  2094. ** incremented by 2 before being stored. Each list is terminated by a
  2095. ** POS_END (0) or POS_COLUMN (1). The following block merges the two lists
  2096. ** and writes the results to buffer p. p is left pointing to the byte
  2097. ** after the list written. No terminator (POS_END or POS_COLUMN) is
  2098. ** written to the output.
  2099. */
  2100. fts3GetDeltaVarint(&p1, &i1);
  2101. fts3GetDeltaVarint(&p2, &i2);
  2102. if( i1<2 || i2<2 ){
  2103. break;
  2104. }
  2105. do {
  2106. fts3PutDeltaVarint(&p, &iPrev, (i1<i2) ? i1 : i2);
  2107. iPrev -= 2;
  2108. if( i1==i2 ){
  2109. fts3ReadNextPos(&p1, &i1);
  2110. fts3ReadNextPos(&p2, &i2);
  2111. }else if( i1<i2 ){
  2112. fts3ReadNextPos(&p1, &i1);
  2113. }else{
  2114. fts3ReadNextPos(&p2, &i2);
  2115. }
  2116. }while( i1!=POSITION_LIST_END || i2!=POSITION_LIST_END );
  2117. }else if( iCol1<iCol2 ){
  2118. p1 += fts3PutColNumber(&p, iCol1);
  2119. fts3ColumnlistCopy(&p, &p1);
  2120. }else{
  2121. p2 += fts3PutColNumber(&p, iCol2);
  2122. fts3ColumnlistCopy(&p, &p2);
  2123. }
  2124. }
  2125. *p++ = POS_END;
  2126. *pp = p;
  2127. *pp1 = p1 + 1;
  2128. *pp2 = p2 + 1;
  2129. return SQLITE_OK;
  2130. }
  2131. /*
  2132. ** This function is used to merge two position lists into one. When it is
  2133. ** called, *pp1 and *pp2 must both point to position lists. A position-list is
  2134. ** the part of a doclist that follows each document id. For example, if a row
  2135. ** contains:
  2136. **
  2137. ** 'a b c'|'x y z'|'a b b a'
  2138. **
  2139. ** Then the position list for this row for token 'b' would consist of:
  2140. **
  2141. ** 0x02 0x01 0x02 0x03 0x03 0x00
  2142. **
  2143. ** When this function returns, both *pp1 and *pp2 are left pointing to the
  2144. ** byte following the 0x00 terminator of their respective position lists.
  2145. **
  2146. ** If isSaveLeft is 0, an entry is added to the output position list for
  2147. ** each position in *pp2 for which there exists one or more positions in
  2148. ** *pp1 so that (pos(*pp2)>pos(*pp1) && pos(*pp2)-pos(*pp1)<=nToken). i.e.
  2149. ** when the *pp1 token appears before the *pp2 token, but not more than nToken
  2150. ** slots before it.
  2151. **
  2152. ** e.g. nToken==1 searches for adjacent positions.
  2153. */
  2154. static int fts3PoslistPhraseMerge(
  2155. char **pp, /* IN/OUT: Preallocated output buffer */
  2156. int nToken, /* Maximum difference in token positions */
  2157. int isSaveLeft, /* Save the left position */
  2158. int isExact, /* If *pp1 is exactly nTokens before *pp2 */
  2159. char **pp1, /* IN/OUT: Left input list */
  2160. char **pp2 /* IN/OUT: Right input list */
  2161. ){
  2162. char *p = *pp;
  2163. char *p1 = *pp1;
  2164. char *p2 = *pp2;
  2165. int iCol1 = 0;
  2166. int iCol2 = 0;
  2167. /* Never set both isSaveLeft and isExact for the same invocation. */
  2168. assert( isSaveLeft==0 || isExact==0 );
  2169. assert_fts3_nc( p!=0 && *p1!=0 && *p2!=0 );
  2170. if( *p1==POS_COLUMN ){
  2171. p1++;
  2172. p1 += fts3GetVarint32(p1, &iCol1);
  2173. /* iCol1==0 indicates corruption. Column 0 does not have a POS_COLUMN
  2174. ** entry, so this is actually end-of-doclist. */
  2175. if( iCol1==0 ) return 0;
  2176. }
  2177. if( *p2==POS_COLUMN ){
  2178. p2++;
  2179. p2 += fts3GetVarint32(p2, &iCol2);
  2180. /* As above, iCol2==0 indicates corruption. */
  2181. if( iCol2==0 ) return 0;
  2182. }
  2183. while( 1 ){
  2184. if( iCol1==iCol2 ){
  2185. char *pSave = p;
  2186. sqlite3_int64 iPrev = 0;
  2187. sqlite3_int64 iPos1 = 0;
  2188. sqlite3_int64 iPos2 = 0;
  2189. if( iCol1 ){
  2190. *p++ = POS_COLUMN;
  2191. p += sqlite3Fts3PutVarint(p, iCol1);
  2192. }
  2193. fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
  2194. fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
  2195. if( iPos1<0 || iPos2<0 ) break;
  2196. while( 1 ){
  2197. if( iPos2==iPos1+nToken
  2198. || (isExact==0 && iPos2>iPos1 && iPos2<=iPos1+nToken)
  2199. ){
  2200. sqlite3_int64 iSave;
  2201. iSave = isSaveLeft ? iPos1 : iPos2;
  2202. fts3PutDeltaVarint(&p, &iPrev, iSave+2); iPrev -= 2;
  2203. pSave = 0;
  2204. assert( p );
  2205. }
  2206. if( (!isSaveLeft && iPos2<=(iPos1+nToken)) || iPos2<=iPos1 ){
  2207. if( (*p2&0xFE)==0 ) break;
  2208. fts3GetDeltaVarint(&p2, &iPos2); iPos2 -= 2;
  2209. }else{
  2210. if( (*p1&0xFE)==0 ) break;
  2211. fts3GetDeltaVarint(&p1, &iPos1); iPos1 -= 2;
  2212. }
  2213. }
  2214. if( pSave ){
  2215. assert( pp && p );
  2216. p = pSave;
  2217. }
  2218. fts3ColumnlistCopy(0, &p1);
  2219. fts3ColumnlistCopy(0, &p2);
  2220. assert( (*p1&0xFE)==0 && (*p2&0xFE)==0 );
  2221. if( 0==*p1 || 0==*p2 ) break;
  2222. p1++;
  2223. p1 += fts3GetVarint32(p1, &iCol1);
  2224. p2++;
  2225. p2 += fts3GetVarint32(p2, &iCol2);
  2226. }
  2227. /* Advance pointer p1 or p2 (whichever corresponds to the smaller of
  2228. ** iCol1 and iCol2) so that it points to either the 0x00 that marks the
  2229. ** end of the position list, or the 0x01 that precedes the next
  2230. ** column-number in the position list.
  2231. */
  2232. else if( iCol1<iCol2 ){
  2233. fts3ColumnlistCopy(0, &p1);
  2234. if( 0==*p1 ) break;
  2235. p1++;
  2236. p1 += fts3GetVarint32(p1, &iCol1);
  2237. }else{
  2238. fts3ColumnlistCopy(0, &p2);
  2239. if( 0==*p2 ) break;
  2240. p2++;
  2241. p2 += fts3GetVarint32(p2, &iCol2);
  2242. }
  2243. }
  2244. fts3PoslistCopy(0, &p2);
  2245. fts3PoslistCopy(0, &p1);
  2246. *pp1 = p1;
  2247. *pp2 = p2;
  2248. if( *pp==p ){
  2249. return 0;
  2250. }
  2251. *p++ = 0x00;
  2252. *pp = p;
  2253. return 1;
  2254. }
  2255. /*
  2256. ** Merge two position-lists as required by the NEAR operator. The argument
  2257. ** position lists correspond to the left and right phrases of an expression
  2258. ** like:
  2259. **
  2260. ** "phrase 1" NEAR "phrase number 2"
  2261. **
  2262. ** Position list *pp1 corresponds to the left-hand side of the NEAR
  2263. ** expression and *pp2 to the right. As usual, the indexes in the position
  2264. ** lists are the offsets of the last token in each phrase (tokens "1" and "2"
  2265. ** in the example above).
  2266. **
  2267. ** The output position list - written to *pp - is a copy of *pp2 with those
  2268. ** entries that are not sufficiently NEAR entries in *pp1 removed.
  2269. */
  2270. static int fts3PoslistNearMerge(
  2271. char **pp, /* Output buffer */
  2272. char *aTmp, /* Temporary buffer space */
  2273. int nRight, /* Maximum difference in token positions */
  2274. int nLeft, /* Maximum difference in token positions */
  2275. char **pp1, /* IN/OUT: Left input list */
  2276. char **pp2 /* IN/OUT: Right input list */
  2277. ){
  2278. char *p1 = *pp1;
  2279. char *p2 = *pp2;
  2280. char *pTmp1 = aTmp;
  2281. char *pTmp2;
  2282. char *aTmp2;
  2283. int res = 1;
  2284. fts3PoslistPhraseMerge(&pTmp1, nRight, 0, 0, pp1, pp2);
  2285. aTmp2 = pTmp2 = pTmp1;
  2286. *pp1 = p1;
  2287. *pp2 = p2;
  2288. fts3PoslistPhraseMerge(&pTmp2, nLeft, 1, 0, pp2, pp1);
  2289. if( pTmp1!=aTmp && pTmp2!=aTmp2 ){
  2290. fts3PoslistMerge(pp, &aTmp, &aTmp2);
  2291. }else if( pTmp1!=aTmp ){
  2292. fts3PoslistCopy(pp, &aTmp);
  2293. }else if( pTmp2!=aTmp2 ){
  2294. fts3PoslistCopy(pp, &aTmp2);
  2295. }else{
  2296. res = 0;
  2297. }
  2298. return res;
  2299. }
  2300. /*
  2301. ** An instance of this function is used to merge together the (potentially
  2302. ** large number of) doclists for each term that matches a prefix query.
  2303. ** See function fts3TermSelectMerge() for details.
  2304. */
  2305. typedef struct TermSelect TermSelect;
  2306. struct TermSelect {
  2307. char *aaOutput[16]; /* Malloc'd output buffers */
  2308. int anOutput[16]; /* Size each output buffer in bytes */
  2309. };
  2310. /*
  2311. ** This function is used to read a single varint from a buffer. Parameter
  2312. ** pEnd points 1 byte past the end of the buffer. When this function is
  2313. ** called, if *pp points to pEnd or greater, then the end of the buffer
  2314. ** has been reached. In this case *pp is set to 0 and the function returns.
  2315. **
  2316. ** If *pp does not point to or past pEnd, then a single varint is read
  2317. ** from *pp. *pp is then set to point 1 byte past the end of the read varint.
  2318. **
  2319. ** If bDescIdx is false, the value read is added to *pVal before returning.
  2320. ** If it is true, the value read is subtracted from *pVal before this
  2321. ** function returns.
  2322. */
  2323. static void fts3GetDeltaVarint3(
  2324. char **pp, /* IN/OUT: Point to read varint from */
  2325. char *pEnd, /* End of buffer */
  2326. int bDescIdx, /* True if docids are descending */
  2327. sqlite3_int64 *pVal /* IN/OUT: Integer value */
  2328. ){
  2329. if( *pp>=pEnd ){
  2330. *pp = 0;
  2331. }else{
  2332. u64 iVal;
  2333. *pp += sqlite3Fts3GetVarintU(*pp, &iVal);
  2334. if( bDescIdx ){
  2335. *pVal = (i64)((u64)*pVal - iVal);
  2336. }else{
  2337. *pVal = (i64)((u64)*pVal + iVal);
  2338. }
  2339. }
  2340. }
  2341. /*
  2342. ** This function is used to write a single varint to a buffer. The varint
  2343. ** is written to *pp. Before returning, *pp is set to point 1 byte past the
  2344. ** end of the value written.
  2345. **
  2346. ** If *pbFirst is zero when this function is called, the value written to
  2347. ** the buffer is that of parameter iVal.
  2348. **
  2349. ** If *pbFirst is non-zero when this function is called, then the value
  2350. ** written is either (iVal-*piPrev) (if bDescIdx is zero) or (*piPrev-iVal)
  2351. ** (if bDescIdx is non-zero).
  2352. **
  2353. ** Before returning, this function always sets *pbFirst to 1 and *piPrev
  2354. ** to the value of parameter iVal.
  2355. */
  2356. static void fts3PutDeltaVarint3(
  2357. char **pp, /* IN/OUT: Output pointer */
  2358. int bDescIdx, /* True for descending docids */
  2359. sqlite3_int64 *piPrev, /* IN/OUT: Previous value written to list */
  2360. int *pbFirst, /* IN/OUT: True after first int written */
  2361. sqlite3_int64 iVal /* Write this value to the list */
  2362. ){
  2363. sqlite3_uint64 iWrite;
  2364. if( bDescIdx==0 || *pbFirst==0 ){
  2365. assert_fts3_nc( *pbFirst==0 || iVal>=*piPrev );
  2366. iWrite = (u64)iVal - (u64)*piPrev;
  2367. }else{
  2368. assert_fts3_nc( *piPrev>=iVal );
  2369. iWrite = (u64)*piPrev - (u64)iVal;
  2370. }
  2371. assert( *pbFirst || *piPrev==0 );
  2372. assert_fts3_nc( *pbFirst==0 || iWrite>0 );
  2373. *pp += sqlite3Fts3PutVarint(*pp, iWrite);
  2374. *piPrev = iVal;
  2375. *pbFirst = 1;
  2376. }
  2377. /*
  2378. ** This macro is used by various functions that merge doclists. The two
  2379. ** arguments are 64-bit docid values. If the value of the stack variable
  2380. ** bDescDoclist is 0 when this macro is invoked, then it returns (i1-i2).
  2381. ** Otherwise, (i2-i1).
  2382. **
  2383. ** Using this makes it easier to write code that can merge doclists that are
  2384. ** sorted in either ascending or descending order.
  2385. */
  2386. /* #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i64)((u64)i1-i2)) */
  2387. #define DOCID_CMP(i1, i2) ((bDescDoclist?-1:1) * (i1>i2?1:((i1==i2)?0:-1)))
  2388. /*
  2389. ** This function does an "OR" merge of two doclists (output contains all
  2390. ** positions contained in either argument doclist). If the docids in the
  2391. ** input doclists are sorted in ascending order, parameter bDescDoclist
  2392. ** should be false. If they are sorted in ascending order, it should be
  2393. ** passed a non-zero value.
  2394. **
  2395. ** If no error occurs, *paOut is set to point at an sqlite3_malloc'd buffer
  2396. ** containing the output doclist and SQLITE_OK is returned. In this case
  2397. ** *pnOut is set to the number of bytes in the output doclist.
  2398. **
  2399. ** If an error occurs, an SQLite error code is returned. The output values
  2400. ** are undefined in this case.
  2401. */
  2402. static int fts3DoclistOrMerge(
  2403. int bDescDoclist, /* True if arguments are desc */
  2404. char *a1, int n1, /* First doclist */
  2405. char *a2, int n2, /* Second doclist */
  2406. char **paOut, int *pnOut /* OUT: Malloc'd doclist */
  2407. ){
  2408. int rc = SQLITE_OK;
  2409. sqlite3_int64 i1 = 0;
  2410. sqlite3_int64 i2 = 0;
  2411. sqlite3_int64 iPrev = 0;
  2412. char *pEnd1 = &a1[n1];
  2413. char *pEnd2 = &a2[n2];
  2414. char *p1 = a1;
  2415. char *p2 = a2;
  2416. char *p;
  2417. char *aOut;
  2418. int bFirstOut = 0;
  2419. *paOut = 0;
  2420. *pnOut = 0;
  2421. /* Allocate space for the output. Both the input and output doclists
  2422. ** are delta encoded. If they are in ascending order (bDescDoclist==0),
  2423. ** then the first docid in each list is simply encoded as a varint. For
  2424. ** each subsequent docid, the varint stored is the difference between the
  2425. ** current and previous docid (a positive number - since the list is in
  2426. ** ascending order).
  2427. **
  2428. ** The first docid written to the output is therefore encoded using the
  2429. ** same number of bytes as it is in whichever of the input lists it is
  2430. ** read from. And each subsequent docid read from the same input list
  2431. ** consumes either the same or less bytes as it did in the input (since
  2432. ** the difference between it and the previous value in the output must
  2433. ** be a positive value less than or equal to the delta value read from
  2434. ** the input list). The same argument applies to all but the first docid
  2435. ** read from the 'other' list. And to the contents of all position lists
  2436. ** that will be copied and merged from the input to the output.
  2437. **
  2438. ** However, if the first docid copied to the output is a negative number,
  2439. ** then the encoding of the first docid from the 'other' input list may
  2440. ** be larger in the output than it was in the input (since the delta value
  2441. ** may be a larger positive integer than the actual docid).
  2442. **
  2443. ** The space required to store the output is therefore the sum of the
  2444. ** sizes of the two inputs, plus enough space for exactly one of the input
  2445. ** docids to grow.
  2446. **
  2447. ** A symetric argument may be made if the doclists are in descending
  2448. ** order.
  2449. */
  2450. aOut = sqlite3_malloc64((i64)n1+n2+FTS3_VARINT_MAX-1+FTS3_BUFFER_PADDING);
  2451. if( !aOut ) return SQLITE_NOMEM;
  2452. p = aOut;
  2453. fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
  2454. fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
  2455. while( p1 || p2 ){
  2456. sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
  2457. if( p2 && p1 && iDiff==0 ){
  2458. fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
  2459. rc = fts3PoslistMerge(&p, &p1, &p2);
  2460. if( rc ) break;
  2461. fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
  2462. fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  2463. }else if( !p2 || (p1 && iDiff<0) ){
  2464. fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
  2465. fts3PoslistCopy(&p, &p1);
  2466. fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
  2467. }else{
  2468. fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i2);
  2469. fts3PoslistCopy(&p, &p2);
  2470. fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  2471. }
  2472. assert( (p-aOut)<=((p1?(p1-a1):n1)+(p2?(p2-a2):n2)+FTS3_VARINT_MAX-1) );
  2473. }
  2474. if( rc!=SQLITE_OK ){
  2475. sqlite3_free(aOut);
  2476. p = aOut = 0;
  2477. }else{
  2478. assert( (p-aOut)<=n1+n2+FTS3_VARINT_MAX-1 );
  2479. memset(&aOut[(p-aOut)], 0, FTS3_BUFFER_PADDING);
  2480. }
  2481. *paOut = aOut;
  2482. *pnOut = (int)(p-aOut);
  2483. return rc;
  2484. }
  2485. /*
  2486. ** This function does a "phrase" merge of two doclists. In a phrase merge,
  2487. ** the output contains a copy of each position from the right-hand input
  2488. ** doclist for which there is a position in the left-hand input doclist
  2489. ** exactly nDist tokens before it.
  2490. **
  2491. ** If the docids in the input doclists are sorted in ascending order,
  2492. ** parameter bDescDoclist should be false. If they are sorted in ascending
  2493. ** order, it should be passed a non-zero value.
  2494. **
  2495. ** The right-hand input doclist is overwritten by this function.
  2496. */
  2497. static int fts3DoclistPhraseMerge(
  2498. int bDescDoclist, /* True if arguments are desc */
  2499. int nDist, /* Distance from left to right (1=adjacent) */
  2500. char *aLeft, int nLeft, /* Left doclist */
  2501. char **paRight, int *pnRight /* IN/OUT: Right/output doclist */
  2502. ){
  2503. sqlite3_int64 i1 = 0;
  2504. sqlite3_int64 i2 = 0;
  2505. sqlite3_int64 iPrev = 0;
  2506. char *aRight = *paRight;
  2507. char *pEnd1 = &aLeft[nLeft];
  2508. char *pEnd2 = &aRight[*pnRight];
  2509. char *p1 = aLeft;
  2510. char *p2 = aRight;
  2511. char *p;
  2512. int bFirstOut = 0;
  2513. char *aOut;
  2514. assert( nDist>0 );
  2515. if( bDescDoclist ){
  2516. aOut = sqlite3_malloc64((sqlite3_int64)*pnRight + FTS3_VARINT_MAX);
  2517. if( aOut==0 ) return SQLITE_NOMEM;
  2518. }else{
  2519. aOut = aRight;
  2520. }
  2521. p = aOut;
  2522. fts3GetDeltaVarint3(&p1, pEnd1, 0, &i1);
  2523. fts3GetDeltaVarint3(&p2, pEnd2, 0, &i2);
  2524. while( p1 && p2 ){
  2525. sqlite3_int64 iDiff = DOCID_CMP(i1, i2);
  2526. if( iDiff==0 ){
  2527. char *pSave = p;
  2528. sqlite3_int64 iPrevSave = iPrev;
  2529. int bFirstOutSave = bFirstOut;
  2530. fts3PutDeltaVarint3(&p, bDescDoclist, &iPrev, &bFirstOut, i1);
  2531. if( 0==fts3PoslistPhraseMerge(&p, nDist, 0, 1, &p1, &p2) ){
  2532. p = pSave;
  2533. iPrev = iPrevSave;
  2534. bFirstOut = bFirstOutSave;
  2535. }
  2536. fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
  2537. fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  2538. }else if( iDiff<0 ){
  2539. fts3PoslistCopy(0, &p1);
  2540. fts3GetDeltaVarint3(&p1, pEnd1, bDescDoclist, &i1);
  2541. }else{
  2542. fts3PoslistCopy(0, &p2);
  2543. fts3GetDeltaVarint3(&p2, pEnd2, bDescDoclist, &i2);
  2544. }
  2545. }
  2546. *pnRight = (int)(p - aOut);
  2547. if( bDescDoclist ){
  2548. sqlite3_free(aRight);
  2549. *paRight = aOut;
  2550. }
  2551. return SQLITE_OK;
  2552. }
  2553. /*
  2554. ** Argument pList points to a position list nList bytes in size. This
  2555. ** function checks to see if the position list contains any entries for
  2556. ** a token in position 0 (of any column). If so, it writes argument iDelta
  2557. ** to the output buffer pOut, followed by a position list consisting only
  2558. ** of the entries from pList at position 0, and terminated by an 0x00 byte.
  2559. ** The value returned is the number of bytes written to pOut (if any).
  2560. */
  2561. int sqlite3Fts3FirstFilter(
  2562. sqlite3_int64 iDelta, /* Varint that may be written to pOut */
  2563. char *pList, /* Position list (no 0x00 term) */
  2564. int nList, /* Size of pList in bytes */
  2565. char *pOut /* Write output here */
  2566. ){
  2567. int nOut = 0;
  2568. int bWritten = 0; /* True once iDelta has been written */
  2569. char *p = pList;
  2570. char *pEnd = &pList[nList];
  2571. if( *p!=0x01 ){
  2572. if( *p==0x02 ){
  2573. nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
  2574. pOut[nOut++] = 0x02;
  2575. bWritten = 1;
  2576. }
  2577. fts3ColumnlistCopy(0, &p);
  2578. }
  2579. while( p<pEnd ){
  2580. sqlite3_int64 iCol;
  2581. p++;
  2582. p += sqlite3Fts3GetVarint(p, &iCol);
  2583. if( *p==0x02 ){
  2584. if( bWritten==0 ){
  2585. nOut += sqlite3Fts3PutVarint(&pOut[nOut], iDelta);
  2586. bWritten = 1;
  2587. }
  2588. pOut[nOut++] = 0x01;
  2589. nOut += sqlite3Fts3PutVarint(&pOut[nOut], iCol);
  2590. pOut[nOut++] = 0x02;
  2591. }
  2592. fts3ColumnlistCopy(0, &p);
  2593. }
  2594. if( bWritten ){
  2595. pOut[nOut++] = 0x00;
  2596. }
  2597. return nOut;
  2598. }
  2599. /*
  2600. ** Merge all doclists in the TermSelect.aaOutput[] array into a single
  2601. ** doclist stored in TermSelect.aaOutput[0]. If successful, delete all
  2602. ** other doclists (except the aaOutput[0] one) and return SQLITE_OK.
  2603. **
  2604. ** If an OOM error occurs, return SQLITE_NOMEM. In this case it is
  2605. ** the responsibility of the caller to free any doclists left in the
  2606. ** TermSelect.aaOutput[] array.
  2607. */
  2608. static int fts3TermSelectFinishMerge(Fts3Table *p, TermSelect *pTS){
  2609. char *aOut = 0;
  2610. int nOut = 0;
  2611. int i;
  2612. /* Loop through the doclists in the aaOutput[] array. Merge them all
  2613. ** into a single doclist.
  2614. */
  2615. for(i=0; i<SizeofArray(pTS->aaOutput); i++){
  2616. if( pTS->aaOutput[i] ){
  2617. if( !aOut ){
  2618. aOut = pTS->aaOutput[i];
  2619. nOut = pTS->anOutput[i];
  2620. pTS->aaOutput[i] = 0;
  2621. }else{
  2622. int nNew;
  2623. char *aNew;
  2624. int rc = fts3DoclistOrMerge(p->bDescIdx,
  2625. pTS->aaOutput[i], pTS->anOutput[i], aOut, nOut, &aNew, &nNew
  2626. );
  2627. if( rc!=SQLITE_OK ){
  2628. sqlite3_free(aOut);
  2629. return rc;
  2630. }
  2631. sqlite3_free(pTS->aaOutput[i]);
  2632. sqlite3_free(aOut);
  2633. pTS->aaOutput[i] = 0;
  2634. aOut = aNew;
  2635. nOut = nNew;
  2636. }
  2637. }
  2638. }
  2639. pTS->aaOutput[0] = aOut;
  2640. pTS->anOutput[0] = nOut;
  2641. return SQLITE_OK;
  2642. }
  2643. /*
  2644. ** Merge the doclist aDoclist/nDoclist into the TermSelect object passed
  2645. ** as the first argument. The merge is an "OR" merge (see function
  2646. ** fts3DoclistOrMerge() for details).
  2647. **
  2648. ** This function is called with the doclist for each term that matches
  2649. ** a queried prefix. It merges all these doclists into one, the doclist
  2650. ** for the specified prefix. Since there can be a very large number of
  2651. ** doclists to merge, the merging is done pair-wise using the TermSelect
  2652. ** object.
  2653. **
  2654. ** This function returns SQLITE_OK if the merge is successful, or an
  2655. ** SQLite error code (SQLITE_NOMEM) if an error occurs.
  2656. */
  2657. static int fts3TermSelectMerge(
  2658. Fts3Table *p, /* FTS table handle */
  2659. TermSelect *pTS, /* TermSelect object to merge into */
  2660. char *aDoclist, /* Pointer to doclist */
  2661. int nDoclist /* Size of aDoclist in bytes */
  2662. ){
  2663. if( pTS->aaOutput[0]==0 ){
  2664. /* If this is the first term selected, copy the doclist to the output
  2665. ** buffer using memcpy().
  2666. **
  2667. ** Add FTS3_VARINT_MAX bytes of unused space to the end of the
  2668. ** allocation. This is so as to ensure that the buffer is big enough
  2669. ** to hold the current doclist AND'd with any other doclist. If the
  2670. ** doclists are stored in order=ASC order, this padding would not be
  2671. ** required (since the size of [doclistA AND doclistB] is always less
  2672. ** than or equal to the size of [doclistA] in that case). But this is
  2673. ** not true for order=DESC. For example, a doclist containing (1, -1)
  2674. ** may be smaller than (-1), as in the first example the -1 may be stored
  2675. ** as a single-byte delta, whereas in the second it must be stored as a
  2676. ** FTS3_VARINT_MAX byte varint.
  2677. **
  2678. ** Similar padding is added in the fts3DoclistOrMerge() function.
  2679. */
  2680. pTS->aaOutput[0] = sqlite3_malloc64((i64)nDoclist + FTS3_VARINT_MAX + 1);
  2681. pTS->anOutput[0] = nDoclist;
  2682. if( pTS->aaOutput[0] ){
  2683. memcpy(pTS->aaOutput[0], aDoclist, nDoclist);
  2684. memset(&pTS->aaOutput[0][nDoclist], 0, FTS3_VARINT_MAX);
  2685. }else{
  2686. return SQLITE_NOMEM;
  2687. }
  2688. }else{
  2689. char *aMerge = aDoclist;
  2690. int nMerge = nDoclist;
  2691. int iOut;
  2692. for(iOut=0; iOut<SizeofArray(pTS->aaOutput); iOut++){
  2693. if( pTS->aaOutput[iOut]==0 ){
  2694. assert( iOut>0 );
  2695. pTS->aaOutput[iOut] = aMerge;
  2696. pTS->anOutput[iOut] = nMerge;
  2697. break;
  2698. }else{
  2699. char *aNew;
  2700. int nNew;
  2701. int rc = fts3DoclistOrMerge(p->bDescIdx, aMerge, nMerge,
  2702. pTS->aaOutput[iOut], pTS->anOutput[iOut], &aNew, &nNew
  2703. );
  2704. if( rc!=SQLITE_OK ){
  2705. if( aMerge!=aDoclist ) sqlite3_free(aMerge);
  2706. return rc;
  2707. }
  2708. if( aMerge!=aDoclist ) sqlite3_free(aMerge);
  2709. sqlite3_free(pTS->aaOutput[iOut]);
  2710. pTS->aaOutput[iOut] = 0;
  2711. aMerge = aNew;
  2712. nMerge = nNew;
  2713. if( (iOut+1)==SizeofArray(pTS->aaOutput) ){
  2714. pTS->aaOutput[iOut] = aMerge;
  2715. pTS->anOutput[iOut] = nMerge;
  2716. }
  2717. }
  2718. }
  2719. }
  2720. return SQLITE_OK;
  2721. }
  2722. /*
  2723. ** Append SegReader object pNew to the end of the pCsr->apSegment[] array.
  2724. */
  2725. static int fts3SegReaderCursorAppend(
  2726. Fts3MultiSegReader *pCsr,
  2727. Fts3SegReader *pNew
  2728. ){
  2729. if( (pCsr->nSegment%16)==0 ){
  2730. Fts3SegReader **apNew;
  2731. sqlite3_int64 nByte = (pCsr->nSegment + 16)*sizeof(Fts3SegReader*);
  2732. apNew = (Fts3SegReader **)sqlite3_realloc64(pCsr->apSegment, nByte);
  2733. if( !apNew ){
  2734. sqlite3Fts3SegReaderFree(pNew);
  2735. return SQLITE_NOMEM;
  2736. }
  2737. pCsr->apSegment = apNew;
  2738. }
  2739. pCsr->apSegment[pCsr->nSegment++] = pNew;
  2740. return SQLITE_OK;
  2741. }
  2742. /*
  2743. ** Add seg-reader objects to the Fts3MultiSegReader object passed as the
  2744. ** 8th argument.
  2745. **
  2746. ** This function returns SQLITE_OK if successful, or an SQLite error code
  2747. ** otherwise.
  2748. */
  2749. static int fts3SegReaderCursor(
  2750. Fts3Table *p, /* FTS3 table handle */
  2751. int iLangid, /* Language id */
  2752. int iIndex, /* Index to search (from 0 to p->nIndex-1) */
  2753. int iLevel, /* Level of segments to scan */
  2754. const char *zTerm, /* Term to query for */
  2755. int nTerm, /* Size of zTerm in bytes */
  2756. int isPrefix, /* True for a prefix search */
  2757. int isScan, /* True to scan from zTerm to EOF */
  2758. Fts3MultiSegReader *pCsr /* Cursor object to populate */
  2759. ){
  2760. int rc = SQLITE_OK; /* Error code */
  2761. sqlite3_stmt *pStmt = 0; /* Statement to iterate through segments */
  2762. int rc2; /* Result of sqlite3_reset() */
  2763. /* If iLevel is less than 0 and this is not a scan, include a seg-reader
  2764. ** for the pending-terms. If this is a scan, then this call must be being
  2765. ** made by an fts4aux module, not an FTS table. In this case calling
  2766. ** Fts3SegReaderPending might segfault, as the data structures used by
  2767. ** fts4aux are not completely populated. So it's easiest to filter these
  2768. ** calls out here. */
  2769. if( iLevel<0 && p->aIndex && p->iPrevLangid==iLangid ){
  2770. Fts3SegReader *pSeg = 0;
  2771. rc = sqlite3Fts3SegReaderPending(p, iIndex, zTerm, nTerm, isPrefix||isScan, &pSeg);
  2772. if( rc==SQLITE_OK && pSeg ){
  2773. rc = fts3SegReaderCursorAppend(pCsr, pSeg);
  2774. }
  2775. }
  2776. if( iLevel!=FTS3_SEGCURSOR_PENDING ){
  2777. if( rc==SQLITE_OK ){
  2778. rc = sqlite3Fts3AllSegdirs(p, iLangid, iIndex, iLevel, &pStmt);
  2779. }
  2780. while( rc==SQLITE_OK && SQLITE_ROW==(rc = sqlite3_step(pStmt)) ){
  2781. Fts3SegReader *pSeg = 0;
  2782. /* Read the values returned by the SELECT into local variables. */
  2783. sqlite3_int64 iStartBlock = sqlite3_column_int64(pStmt, 1);
  2784. sqlite3_int64 iLeavesEndBlock = sqlite3_column_int64(pStmt, 2);
  2785. sqlite3_int64 iEndBlock = sqlite3_column_int64(pStmt, 3);
  2786. int nRoot = sqlite3_column_bytes(pStmt, 4);
  2787. char const *zRoot = sqlite3_column_blob(pStmt, 4);
  2788. /* If zTerm is not NULL, and this segment is not stored entirely on its
  2789. ** root node, the range of leaves scanned can be reduced. Do this. */
  2790. if( iStartBlock && zTerm && zRoot ){
  2791. sqlite3_int64 *pi = (isPrefix ? &iLeavesEndBlock : 0);
  2792. rc = fts3SelectLeaf(p, zTerm, nTerm, zRoot, nRoot, &iStartBlock, pi);
  2793. if( rc!=SQLITE_OK ) goto finished;
  2794. if( isPrefix==0 && isScan==0 ) iLeavesEndBlock = iStartBlock;
  2795. }
  2796. rc = sqlite3Fts3SegReaderNew(pCsr->nSegment+1,
  2797. (isPrefix==0 && isScan==0),
  2798. iStartBlock, iLeavesEndBlock,
  2799. iEndBlock, zRoot, nRoot, &pSeg
  2800. );
  2801. if( rc!=SQLITE_OK ) goto finished;
  2802. rc = fts3SegReaderCursorAppend(pCsr, pSeg);
  2803. }
  2804. }
  2805. finished:
  2806. rc2 = sqlite3_reset(pStmt);
  2807. if( rc==SQLITE_DONE ) rc = rc2;
  2808. return rc;
  2809. }
  2810. /*
  2811. ** Set up a cursor object for iterating through a full-text index or a
  2812. ** single level therein.
  2813. */
  2814. int sqlite3Fts3SegReaderCursor(
  2815. Fts3Table *p, /* FTS3 table handle */
  2816. int iLangid, /* Language-id to search */
  2817. int iIndex, /* Index to search (from 0 to p->nIndex-1) */
  2818. int iLevel, /* Level of segments to scan */
  2819. const char *zTerm, /* Term to query for */
  2820. int nTerm, /* Size of zTerm in bytes */
  2821. int isPrefix, /* True for a prefix search */
  2822. int isScan, /* True to scan from zTerm to EOF */
  2823. Fts3MultiSegReader *pCsr /* Cursor object to populate */
  2824. ){
  2825. assert( iIndex>=0 && iIndex<p->nIndex );
  2826. assert( iLevel==FTS3_SEGCURSOR_ALL
  2827. || iLevel==FTS3_SEGCURSOR_PENDING
  2828. || iLevel>=0
  2829. );
  2830. assert( iLevel<FTS3_SEGDIR_MAXLEVEL );
  2831. assert( FTS3_SEGCURSOR_ALL<0 && FTS3_SEGCURSOR_PENDING<0 );
  2832. assert( isPrefix==0 || isScan==0 );
  2833. memset(pCsr, 0, sizeof(Fts3MultiSegReader));
  2834. return fts3SegReaderCursor(
  2835. p, iLangid, iIndex, iLevel, zTerm, nTerm, isPrefix, isScan, pCsr
  2836. );
  2837. }
  2838. /*
  2839. ** In addition to its current configuration, have the Fts3MultiSegReader
  2840. ** passed as the 4th argument also scan the doclist for term zTerm/nTerm.
  2841. **
  2842. ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  2843. */
  2844. static int fts3SegReaderCursorAddZero(
  2845. Fts3Table *p, /* FTS virtual table handle */
  2846. int iLangid,
  2847. const char *zTerm, /* Term to scan doclist of */
  2848. int nTerm, /* Number of bytes in zTerm */
  2849. Fts3MultiSegReader *pCsr /* Fts3MultiSegReader to modify */
  2850. ){
  2851. return fts3SegReaderCursor(p,
  2852. iLangid, 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0,pCsr
  2853. );
  2854. }
  2855. /*
  2856. ** Open an Fts3MultiSegReader to scan the doclist for term zTerm/nTerm. Or,
  2857. ** if isPrefix is true, to scan the doclist for all terms for which
  2858. ** zTerm/nTerm is a prefix. If successful, return SQLITE_OK and write
  2859. ** a pointer to the new Fts3MultiSegReader to *ppSegcsr. Otherwise, return
  2860. ** an SQLite error code.
  2861. **
  2862. ** It is the responsibility of the caller to free this object by eventually
  2863. ** passing it to fts3SegReaderCursorFree()
  2864. **
  2865. ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  2866. ** Output parameter *ppSegcsr is set to 0 if an error occurs.
  2867. */
  2868. static int fts3TermSegReaderCursor(
  2869. Fts3Cursor *pCsr, /* Virtual table cursor handle */
  2870. const char *zTerm, /* Term to query for */
  2871. int nTerm, /* Size of zTerm in bytes */
  2872. int isPrefix, /* True for a prefix search */
  2873. Fts3MultiSegReader **ppSegcsr /* OUT: Allocated seg-reader cursor */
  2874. ){
  2875. Fts3MultiSegReader *pSegcsr; /* Object to allocate and return */
  2876. int rc = SQLITE_NOMEM; /* Return code */
  2877. pSegcsr = sqlite3_malloc(sizeof(Fts3MultiSegReader));
  2878. if( pSegcsr ){
  2879. int i;
  2880. int bFound = 0; /* True once an index has been found */
  2881. Fts3Table *p = (Fts3Table *)pCsr->base.pVtab;
  2882. if( isPrefix ){
  2883. for(i=1; bFound==0 && i<p->nIndex; i++){
  2884. if( p->aIndex[i].nPrefix==nTerm ){
  2885. bFound = 1;
  2886. rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
  2887. i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 0, 0, pSegcsr
  2888. );
  2889. pSegcsr->bLookup = 1;
  2890. }
  2891. }
  2892. for(i=1; bFound==0 && i<p->nIndex; i++){
  2893. if( p->aIndex[i].nPrefix==nTerm+1 ){
  2894. bFound = 1;
  2895. rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
  2896. i, FTS3_SEGCURSOR_ALL, zTerm, nTerm, 1, 0, pSegcsr
  2897. );
  2898. if( rc==SQLITE_OK ){
  2899. rc = fts3SegReaderCursorAddZero(
  2900. p, pCsr->iLangid, zTerm, nTerm, pSegcsr
  2901. );
  2902. }
  2903. }
  2904. }
  2905. }
  2906. if( bFound==0 ){
  2907. rc = sqlite3Fts3SegReaderCursor(p, pCsr->iLangid,
  2908. 0, FTS3_SEGCURSOR_ALL, zTerm, nTerm, isPrefix, 0, pSegcsr
  2909. );
  2910. pSegcsr->bLookup = !isPrefix;
  2911. }
  2912. }
  2913. *ppSegcsr = pSegcsr;
  2914. return rc;
  2915. }
  2916. /*
  2917. ** Free an Fts3MultiSegReader allocated by fts3TermSegReaderCursor().
  2918. */
  2919. static void fts3SegReaderCursorFree(Fts3MultiSegReader *pSegcsr){
  2920. sqlite3Fts3SegReaderFinish(pSegcsr);
  2921. sqlite3_free(pSegcsr);
  2922. }
  2923. /*
  2924. ** This function retrieves the doclist for the specified term (or term
  2925. ** prefix) from the database.
  2926. */
  2927. static int fts3TermSelect(
  2928. Fts3Table *p, /* Virtual table handle */
  2929. Fts3PhraseToken *pTok, /* Token to query for */
  2930. int iColumn, /* Column to query (or -ve for all columns) */
  2931. int *pnOut, /* OUT: Size of buffer at *ppOut */
  2932. char **ppOut /* OUT: Malloced result buffer */
  2933. ){
  2934. int rc; /* Return code */
  2935. Fts3MultiSegReader *pSegcsr; /* Seg-reader cursor for this term */
  2936. TermSelect tsc; /* Object for pair-wise doclist merging */
  2937. Fts3SegFilter filter; /* Segment term filter configuration */
  2938. pSegcsr = pTok->pSegcsr;
  2939. memset(&tsc, 0, sizeof(TermSelect));
  2940. filter.flags = FTS3_SEGMENT_IGNORE_EMPTY | FTS3_SEGMENT_REQUIRE_POS
  2941. | (pTok->isPrefix ? FTS3_SEGMENT_PREFIX : 0)
  2942. | (pTok->bFirst ? FTS3_SEGMENT_FIRST : 0)
  2943. | (iColumn<p->nColumn ? FTS3_SEGMENT_COLUMN_FILTER : 0);
  2944. filter.iCol = iColumn;
  2945. filter.zTerm = pTok->z;
  2946. filter.nTerm = pTok->n;
  2947. rc = sqlite3Fts3SegReaderStart(p, pSegcsr, &filter);
  2948. while( SQLITE_OK==rc
  2949. && SQLITE_ROW==(rc = sqlite3Fts3SegReaderStep(p, pSegcsr))
  2950. ){
  2951. rc = fts3TermSelectMerge(p, &tsc, pSegcsr->aDoclist, pSegcsr->nDoclist);
  2952. }
  2953. if( rc==SQLITE_OK ){
  2954. rc = fts3TermSelectFinishMerge(p, &tsc);
  2955. }
  2956. if( rc==SQLITE_OK ){
  2957. *ppOut = tsc.aaOutput[0];
  2958. *pnOut = tsc.anOutput[0];
  2959. }else{
  2960. int i;
  2961. for(i=0; i<SizeofArray(tsc.aaOutput); i++){
  2962. sqlite3_free(tsc.aaOutput[i]);
  2963. }
  2964. }
  2965. fts3SegReaderCursorFree(pSegcsr);
  2966. pTok->pSegcsr = 0;
  2967. return rc;
  2968. }
  2969. /*
  2970. ** This function counts the total number of docids in the doclist stored
  2971. ** in buffer aList[], size nList bytes.
  2972. **
  2973. ** If the isPoslist argument is true, then it is assumed that the doclist
  2974. ** contains a position-list following each docid. Otherwise, it is assumed
  2975. ** that the doclist is simply a list of docids stored as delta encoded
  2976. ** varints.
  2977. */
  2978. static int fts3DoclistCountDocids(char *aList, int nList){
  2979. int nDoc = 0; /* Return value */
  2980. if( aList ){
  2981. char *aEnd = &aList[nList]; /* Pointer to one byte after EOF */
  2982. char *p = aList; /* Cursor */
  2983. while( p<aEnd ){
  2984. nDoc++;
  2985. while( (*p++)&0x80 ); /* Skip docid varint */
  2986. fts3PoslistCopy(0, &p); /* Skip over position list */
  2987. }
  2988. }
  2989. return nDoc;
  2990. }
  2991. /*
  2992. ** Advance the cursor to the next row in the %_content table that
  2993. ** matches the search criteria. For a MATCH search, this will be
  2994. ** the next row that matches. For a full-table scan, this will be
  2995. ** simply the next row in the %_content table. For a docid lookup,
  2996. ** this routine simply sets the EOF flag.
  2997. **
  2998. ** Return SQLITE_OK if nothing goes wrong. SQLITE_OK is returned
  2999. ** even if we reach end-of-file. The fts3EofMethod() will be called
  3000. ** subsequently to determine whether or not an EOF was hit.
  3001. */
  3002. static int fts3NextMethod(sqlite3_vtab_cursor *pCursor){
  3003. int rc;
  3004. Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
  3005. if( pCsr->eSearch==FTS3_DOCID_SEARCH || pCsr->eSearch==FTS3_FULLSCAN_SEARCH ){
  3006. Fts3Table *pTab = (Fts3Table*)pCursor->pVtab;
  3007. pTab->bLock++;
  3008. if( SQLITE_ROW!=sqlite3_step(pCsr->pStmt) ){
  3009. pCsr->isEof = 1;
  3010. rc = sqlite3_reset(pCsr->pStmt);
  3011. }else{
  3012. pCsr->iPrevId = sqlite3_column_int64(pCsr->pStmt, 0);
  3013. rc = SQLITE_OK;
  3014. }
  3015. pTab->bLock--;
  3016. }else{
  3017. rc = fts3EvalNext((Fts3Cursor *)pCursor);
  3018. }
  3019. assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  3020. return rc;
  3021. }
  3022. /*
  3023. ** If the numeric type of argument pVal is "integer", then return it
  3024. ** converted to a 64-bit signed integer. Otherwise, return a copy of
  3025. ** the second parameter, iDefault.
  3026. */
  3027. static sqlite3_int64 fts3DocidRange(sqlite3_value *pVal, i64 iDefault){
  3028. if( pVal ){
  3029. int eType = sqlite3_value_numeric_type(pVal);
  3030. if( eType==SQLITE_INTEGER ){
  3031. return sqlite3_value_int64(pVal);
  3032. }
  3033. }
  3034. return iDefault;
  3035. }
  3036. /*
  3037. ** This is the xFilter interface for the virtual table. See
  3038. ** the virtual table xFilter method documentation for additional
  3039. ** information.
  3040. **
  3041. ** If idxNum==FTS3_FULLSCAN_SEARCH then do a full table scan against
  3042. ** the %_content table.
  3043. **
  3044. ** If idxNum==FTS3_DOCID_SEARCH then do a docid lookup for a single entry
  3045. ** in the %_content table.
  3046. **
  3047. ** If idxNum>=FTS3_FULLTEXT_SEARCH then use the full text index. The
  3048. ** column on the left-hand side of the MATCH operator is column
  3049. ** number idxNum-FTS3_FULLTEXT_SEARCH, 0 indexed. argv[0] is the right-hand
  3050. ** side of the MATCH operator.
  3051. */
  3052. static int fts3FilterMethod(
  3053. sqlite3_vtab_cursor *pCursor, /* The cursor used for this query */
  3054. int idxNum, /* Strategy index */
  3055. const char *idxStr, /* Unused */
  3056. int nVal, /* Number of elements in apVal */
  3057. sqlite3_value **apVal /* Arguments for the indexing scheme */
  3058. ){
  3059. int rc = SQLITE_OK;
  3060. char *zSql; /* SQL statement used to access %_content */
  3061. int eSearch;
  3062. Fts3Table *p = (Fts3Table *)pCursor->pVtab;
  3063. Fts3Cursor *pCsr = (Fts3Cursor *)pCursor;
  3064. sqlite3_value *pCons = 0; /* The MATCH or rowid constraint, if any */
  3065. sqlite3_value *pLangid = 0; /* The "langid = ?" constraint, if any */
  3066. sqlite3_value *pDocidGe = 0; /* The "docid >= ?" constraint, if any */
  3067. sqlite3_value *pDocidLe = 0; /* The "docid <= ?" constraint, if any */
  3068. int iIdx;
  3069. UNUSED_PARAMETER(idxStr);
  3070. UNUSED_PARAMETER(nVal);
  3071. if( p->bLock ){
  3072. return SQLITE_ERROR;
  3073. }
  3074. eSearch = (idxNum & 0x0000FFFF);
  3075. assert( eSearch>=0 && eSearch<=(FTS3_FULLTEXT_SEARCH+p->nColumn) );
  3076. assert( p->pSegments==0 );
  3077. /* Collect arguments into local variables */
  3078. iIdx = 0;
  3079. if( eSearch!=FTS3_FULLSCAN_SEARCH ) pCons = apVal[iIdx++];
  3080. if( idxNum & FTS3_HAVE_LANGID ) pLangid = apVal[iIdx++];
  3081. if( idxNum & FTS3_HAVE_DOCID_GE ) pDocidGe = apVal[iIdx++];
  3082. if( idxNum & FTS3_HAVE_DOCID_LE ) pDocidLe = apVal[iIdx++];
  3083. assert( iIdx==nVal );
  3084. /* In case the cursor has been used before, clear it now. */
  3085. fts3ClearCursor(pCsr);
  3086. /* Set the lower and upper bounds on docids to return */
  3087. pCsr->iMinDocid = fts3DocidRange(pDocidGe, SMALLEST_INT64);
  3088. pCsr->iMaxDocid = fts3DocidRange(pDocidLe, LARGEST_INT64);
  3089. if( idxStr ){
  3090. pCsr->bDesc = (idxStr[0]=='D');
  3091. }else{
  3092. pCsr->bDesc = p->bDescIdx;
  3093. }
  3094. pCsr->eSearch = (i16)eSearch;
  3095. if( eSearch!=FTS3_DOCID_SEARCH && eSearch!=FTS3_FULLSCAN_SEARCH ){
  3096. int iCol = eSearch-FTS3_FULLTEXT_SEARCH;
  3097. const char *zQuery = (const char *)sqlite3_value_text(pCons);
  3098. if( zQuery==0 && sqlite3_value_type(pCons)!=SQLITE_NULL ){
  3099. return SQLITE_NOMEM;
  3100. }
  3101. pCsr->iLangid = 0;
  3102. if( pLangid ) pCsr->iLangid = sqlite3_value_int(pLangid);
  3103. assert( p->base.zErrMsg==0 );
  3104. rc = sqlite3Fts3ExprParse(p->pTokenizer, pCsr->iLangid,
  3105. p->azColumn, p->bFts4, p->nColumn, iCol, zQuery, -1, &pCsr->pExpr,
  3106. &p->base.zErrMsg
  3107. );
  3108. if( rc!=SQLITE_OK ){
  3109. return rc;
  3110. }
  3111. rc = fts3EvalStart(pCsr);
  3112. sqlite3Fts3SegmentsClose(p);
  3113. if( rc!=SQLITE_OK ) return rc;
  3114. pCsr->pNextId = pCsr->aDoclist;
  3115. pCsr->iPrevId = 0;
  3116. }
  3117. /* Compile a SELECT statement for this cursor. For a full-table-scan, the
  3118. ** statement loops through all rows of the %_content table. For a
  3119. ** full-text query or docid lookup, the statement retrieves a single
  3120. ** row by docid.
  3121. */
  3122. if( eSearch==FTS3_FULLSCAN_SEARCH ){
  3123. if( pDocidGe || pDocidLe ){
  3124. zSql = sqlite3_mprintf(
  3125. "SELECT %s WHERE rowid BETWEEN %lld AND %lld ORDER BY rowid %s",
  3126. p->zReadExprlist, pCsr->iMinDocid, pCsr->iMaxDocid,
  3127. (pCsr->bDesc ? "DESC" : "ASC")
  3128. );
  3129. }else{
  3130. zSql = sqlite3_mprintf("SELECT %s ORDER BY rowid %s",
  3131. p->zReadExprlist, (pCsr->bDesc ? "DESC" : "ASC")
  3132. );
  3133. }
  3134. if( zSql ){
  3135. p->bLock++;
  3136. rc = sqlite3_prepare_v3(
  3137. p->db,zSql,-1,SQLITE_PREPARE_PERSISTENT,&pCsr->pStmt,0
  3138. );
  3139. p->bLock--;
  3140. sqlite3_free(zSql);
  3141. }else{
  3142. rc = SQLITE_NOMEM;
  3143. }
  3144. }else if( eSearch==FTS3_DOCID_SEARCH ){
  3145. rc = fts3CursorSeekStmt(pCsr);
  3146. if( rc==SQLITE_OK ){
  3147. rc = sqlite3_bind_value(pCsr->pStmt, 1, pCons);
  3148. }
  3149. }
  3150. if( rc!=SQLITE_OK ) return rc;
  3151. return fts3NextMethod(pCursor);
  3152. }
  3153. /*
  3154. ** This is the xEof method of the virtual table. SQLite calls this
  3155. ** routine to find out if it has reached the end of a result set.
  3156. */
  3157. static int fts3EofMethod(sqlite3_vtab_cursor *pCursor){
  3158. Fts3Cursor *pCsr = (Fts3Cursor*)pCursor;
  3159. if( pCsr->isEof ){
  3160. fts3ClearCursor(pCsr);
  3161. pCsr->isEof = 1;
  3162. }
  3163. return pCsr->isEof;
  3164. }
  3165. /*
  3166. ** This is the xRowid method. The SQLite core calls this routine to
  3167. ** retrieve the rowid for the current row of the result set. fts3
  3168. ** exposes %_content.docid as the rowid for the virtual table. The
  3169. ** rowid should be written to *pRowid.
  3170. */
  3171. static int fts3RowidMethod(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
  3172. Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
  3173. *pRowid = pCsr->iPrevId;
  3174. return SQLITE_OK;
  3175. }
  3176. /*
  3177. ** This is the xColumn method, called by SQLite to request a value from
  3178. ** the row that the supplied cursor currently points to.
  3179. **
  3180. ** If:
  3181. **
  3182. ** (iCol < p->nColumn) -> The value of the iCol'th user column.
  3183. ** (iCol == p->nColumn) -> Magic column with the same name as the table.
  3184. ** (iCol == p->nColumn+1) -> Docid column
  3185. ** (iCol == p->nColumn+2) -> Langid column
  3186. */
  3187. static int fts3ColumnMethod(
  3188. sqlite3_vtab_cursor *pCursor, /* Cursor to retrieve value from */
  3189. sqlite3_context *pCtx, /* Context for sqlite3_result_xxx() calls */
  3190. int iCol /* Index of column to read value from */
  3191. ){
  3192. int rc = SQLITE_OK; /* Return Code */
  3193. Fts3Cursor *pCsr = (Fts3Cursor *) pCursor;
  3194. Fts3Table *p = (Fts3Table *)pCursor->pVtab;
  3195. /* The column value supplied by SQLite must be in range. */
  3196. assert( iCol>=0 && iCol<=p->nColumn+2 );
  3197. switch( iCol-p->nColumn ){
  3198. case 0:
  3199. /* The special 'table-name' column */
  3200. sqlite3_result_pointer(pCtx, pCsr, "fts3cursor", 0);
  3201. break;
  3202. case 1:
  3203. /* The docid column */
  3204. sqlite3_result_int64(pCtx, pCsr->iPrevId);
  3205. break;
  3206. case 2:
  3207. if( pCsr->pExpr ){
  3208. sqlite3_result_int64(pCtx, pCsr->iLangid);
  3209. break;
  3210. }else if( p->zLanguageid==0 ){
  3211. sqlite3_result_int(pCtx, 0);
  3212. break;
  3213. }else{
  3214. iCol = p->nColumn;
  3215. /* no break */ deliberate_fall_through
  3216. }
  3217. default:
  3218. /* A user column. Or, if this is a full-table scan, possibly the
  3219. ** language-id column. Seek the cursor. */
  3220. rc = fts3CursorSeek(0, pCsr);
  3221. if( rc==SQLITE_OK && sqlite3_data_count(pCsr->pStmt)-1>iCol ){
  3222. sqlite3_result_value(pCtx, sqlite3_column_value(pCsr->pStmt, iCol+1));
  3223. }
  3224. break;
  3225. }
  3226. assert( ((Fts3Table *)pCsr->base.pVtab)->pSegments==0 );
  3227. return rc;
  3228. }
  3229. /*
  3230. ** This function is the implementation of the xUpdate callback used by
  3231. ** FTS3 virtual tables. It is invoked by SQLite each time a row is to be
  3232. ** inserted, updated or deleted.
  3233. */
  3234. static int fts3UpdateMethod(
  3235. sqlite3_vtab *pVtab, /* Virtual table handle */
  3236. int nArg, /* Size of argument array */
  3237. sqlite3_value **apVal, /* Array of arguments */
  3238. sqlite_int64 *pRowid /* OUT: The affected (or effected) rowid */
  3239. ){
  3240. return sqlite3Fts3UpdateMethod(pVtab, nArg, apVal, pRowid);
  3241. }
  3242. /*
  3243. ** Implementation of xSync() method. Flush the contents of the pending-terms
  3244. ** hash-table to the database.
  3245. */
  3246. static int fts3SyncMethod(sqlite3_vtab *pVtab){
  3247. /* Following an incremental-merge operation, assuming that the input
  3248. ** segments are not completely consumed (the usual case), they are updated
  3249. ** in place to remove the entries that have already been merged. This
  3250. ** involves updating the leaf block that contains the smallest unmerged
  3251. ** entry and each block (if any) between the leaf and the root node. So
  3252. ** if the height of the input segment b-trees is N, and input segments
  3253. ** are merged eight at a time, updating the input segments at the end
  3254. ** of an incremental-merge requires writing (8*(1+N)) blocks. N is usually
  3255. ** small - often between 0 and 2. So the overhead of the incremental
  3256. ** merge is somewhere between 8 and 24 blocks. To avoid this overhead
  3257. ** dwarfing the actual productive work accomplished, the incremental merge
  3258. ** is only attempted if it will write at least 64 leaf blocks. Hence
  3259. ** nMinMerge.
  3260. **
  3261. ** Of course, updating the input segments also involves deleting a bunch
  3262. ** of blocks from the segments table. But this is not considered overhead
  3263. ** as it would also be required by a crisis-merge that used the same input
  3264. ** segments.
  3265. */
  3266. const u32 nMinMerge = 64; /* Minimum amount of incr-merge work to do */
  3267. Fts3Table *p = (Fts3Table*)pVtab;
  3268. int rc;
  3269. i64 iLastRowid = sqlite3_last_insert_rowid(p->db);
  3270. rc = sqlite3Fts3PendingTermsFlush(p);
  3271. if( rc==SQLITE_OK
  3272. && p->nLeafAdd>(nMinMerge/16)
  3273. && p->nAutoincrmerge && p->nAutoincrmerge!=0xff
  3274. ){
  3275. int mxLevel = 0; /* Maximum relative level value in db */
  3276. int A; /* Incr-merge parameter A */
  3277. rc = sqlite3Fts3MaxLevel(p, &mxLevel);
  3278. assert( rc==SQLITE_OK || mxLevel==0 );
  3279. A = p->nLeafAdd * mxLevel;
  3280. A += (A/2);
  3281. if( A>(int)nMinMerge ) rc = sqlite3Fts3Incrmerge(p, A, p->nAutoincrmerge);
  3282. }
  3283. sqlite3Fts3SegmentsClose(p);
  3284. sqlite3_set_last_insert_rowid(p->db, iLastRowid);
  3285. return rc;
  3286. }
  3287. /*
  3288. ** If it is currently unknown whether or not the FTS table has an %_stat
  3289. ** table (if p->bHasStat==2), attempt to determine this (set p->bHasStat
  3290. ** to 0 or 1). Return SQLITE_OK if successful, or an SQLite error code
  3291. ** if an error occurs.
  3292. */
  3293. static int fts3SetHasStat(Fts3Table *p){
  3294. int rc = SQLITE_OK;
  3295. if( p->bHasStat==2 ){
  3296. char *zTbl = sqlite3_mprintf("%s_stat", p->zName);
  3297. if( zTbl ){
  3298. int res = sqlite3_table_column_metadata(p->db, p->zDb, zTbl, 0,0,0,0,0,0);
  3299. sqlite3_free(zTbl);
  3300. p->bHasStat = (res==SQLITE_OK);
  3301. }else{
  3302. rc = SQLITE_NOMEM;
  3303. }
  3304. }
  3305. return rc;
  3306. }
  3307. /*
  3308. ** Implementation of xBegin() method.
  3309. */
  3310. static int fts3BeginMethod(sqlite3_vtab *pVtab){
  3311. Fts3Table *p = (Fts3Table*)pVtab;
  3312. int rc;
  3313. UNUSED_PARAMETER(pVtab);
  3314. assert( p->pSegments==0 );
  3315. assert( p->nPendingData==0 );
  3316. assert( p->inTransaction!=1 );
  3317. p->nLeafAdd = 0;
  3318. rc = fts3SetHasStat(p);
  3319. #ifdef SQLITE_DEBUG
  3320. if( rc==SQLITE_OK ){
  3321. p->inTransaction = 1;
  3322. p->mxSavepoint = -1;
  3323. }
  3324. #endif
  3325. return rc;
  3326. }
  3327. /*
  3328. ** Implementation of xCommit() method. This is a no-op. The contents of
  3329. ** the pending-terms hash-table have already been flushed into the database
  3330. ** by fts3SyncMethod().
  3331. */
  3332. static int fts3CommitMethod(sqlite3_vtab *pVtab){
  3333. TESTONLY( Fts3Table *p = (Fts3Table*)pVtab );
  3334. UNUSED_PARAMETER(pVtab);
  3335. assert( p->nPendingData==0 );
  3336. assert( p->inTransaction!=0 );
  3337. assert( p->pSegments==0 );
  3338. TESTONLY( p->inTransaction = 0 );
  3339. TESTONLY( p->mxSavepoint = -1; );
  3340. return SQLITE_OK;
  3341. }
  3342. /*
  3343. ** Implementation of xRollback(). Discard the contents of the pending-terms
  3344. ** hash-table. Any changes made to the database are reverted by SQLite.
  3345. */
  3346. static int fts3RollbackMethod(sqlite3_vtab *pVtab){
  3347. Fts3Table *p = (Fts3Table*)pVtab;
  3348. sqlite3Fts3PendingTermsClear(p);
  3349. assert( p->inTransaction!=0 );
  3350. TESTONLY( p->inTransaction = 0 );
  3351. TESTONLY( p->mxSavepoint = -1; );
  3352. return SQLITE_OK;
  3353. }
  3354. /*
  3355. ** When called, *ppPoslist must point to the byte immediately following the
  3356. ** end of a position-list. i.e. ( (*ppPoslist)[-1]==POS_END ). This function
  3357. ** moves *ppPoslist so that it instead points to the first byte of the
  3358. ** same position list.
  3359. */
  3360. static void fts3ReversePoslist(char *pStart, char **ppPoslist){
  3361. char *p = &(*ppPoslist)[-2];
  3362. char c = 0;
  3363. /* Skip backwards passed any trailing 0x00 bytes added by NearTrim() */
  3364. while( p>pStart && (c=*p--)==0 );
  3365. /* Search backwards for a varint with value zero (the end of the previous
  3366. ** poslist). This is an 0x00 byte preceded by some byte that does not
  3367. ** have the 0x80 bit set. */
  3368. while( p>pStart && (*p & 0x80) | c ){
  3369. c = *p--;
  3370. }
  3371. assert( p==pStart || c==0 );
  3372. /* At this point p points to that preceding byte without the 0x80 bit
  3373. ** set. So to find the start of the poslist, skip forward 2 bytes then
  3374. ** over a varint.
  3375. **
  3376. ** Normally. The other case is that p==pStart and the poslist to return
  3377. ** is the first in the doclist. In this case do not skip forward 2 bytes.
  3378. ** The second part of the if condition (c==0 && *ppPoslist>&p[2])
  3379. ** is required for cases where the first byte of a doclist and the
  3380. ** doclist is empty. For example, if the first docid is 10, a doclist
  3381. ** that begins with:
  3382. **
  3383. ** 0x0A 0x00 <next docid delta varint>
  3384. */
  3385. if( p>pStart || (c==0 && *ppPoslist>&p[2]) ){ p = &p[2]; }
  3386. while( *p++&0x80 );
  3387. *ppPoslist = p;
  3388. }
  3389. /*
  3390. ** Helper function used by the implementation of the overloaded snippet(),
  3391. ** offsets() and optimize() SQL functions.
  3392. **
  3393. ** If the value passed as the third argument is a blob of size
  3394. ** sizeof(Fts3Cursor*), then the blob contents are copied to the
  3395. ** output variable *ppCsr and SQLITE_OK is returned. Otherwise, an error
  3396. ** message is written to context pContext and SQLITE_ERROR returned. The
  3397. ** string passed via zFunc is used as part of the error message.
  3398. */
  3399. static int fts3FunctionArg(
  3400. sqlite3_context *pContext, /* SQL function call context */
  3401. const char *zFunc, /* Function name */
  3402. sqlite3_value *pVal, /* argv[0] passed to function */
  3403. Fts3Cursor **ppCsr /* OUT: Store cursor handle here */
  3404. ){
  3405. int rc;
  3406. *ppCsr = (Fts3Cursor*)sqlite3_value_pointer(pVal, "fts3cursor");
  3407. if( (*ppCsr)!=0 ){
  3408. rc = SQLITE_OK;
  3409. }else{
  3410. char *zErr = sqlite3_mprintf("illegal first argument to %s", zFunc);
  3411. sqlite3_result_error(pContext, zErr, -1);
  3412. sqlite3_free(zErr);
  3413. rc = SQLITE_ERROR;
  3414. }
  3415. return rc;
  3416. }
  3417. /*
  3418. ** Implementation of the snippet() function for FTS3
  3419. */
  3420. static void fts3SnippetFunc(
  3421. sqlite3_context *pContext, /* SQLite function call context */
  3422. int nVal, /* Size of apVal[] array */
  3423. sqlite3_value **apVal /* Array of arguments */
  3424. ){
  3425. Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
  3426. const char *zStart = "<b>";
  3427. const char *zEnd = "</b>";
  3428. const char *zEllipsis = "<b>...</b>";
  3429. int iCol = -1;
  3430. int nToken = 15; /* Default number of tokens in snippet */
  3431. /* There must be at least one argument passed to this function (otherwise
  3432. ** the non-overloaded version would have been called instead of this one).
  3433. */
  3434. assert( nVal>=1 );
  3435. if( nVal>6 ){
  3436. sqlite3_result_error(pContext,
  3437. "wrong number of arguments to function snippet()", -1);
  3438. return;
  3439. }
  3440. if( fts3FunctionArg(pContext, "snippet", apVal[0], &pCsr) ) return;
  3441. switch( nVal ){
  3442. case 6: nToken = sqlite3_value_int(apVal[5]);
  3443. /* no break */ deliberate_fall_through
  3444. case 5: iCol = sqlite3_value_int(apVal[4]);
  3445. /* no break */ deliberate_fall_through
  3446. case 4: zEllipsis = (const char*)sqlite3_value_text(apVal[3]);
  3447. /* no break */ deliberate_fall_through
  3448. case 3: zEnd = (const char*)sqlite3_value_text(apVal[2]);
  3449. /* no break */ deliberate_fall_through
  3450. case 2: zStart = (const char*)sqlite3_value_text(apVal[1]);
  3451. }
  3452. if( !zEllipsis || !zEnd || !zStart ){
  3453. sqlite3_result_error_nomem(pContext);
  3454. }else if( nToken==0 ){
  3455. sqlite3_result_text(pContext, "", -1, SQLITE_STATIC);
  3456. }else if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
  3457. sqlite3Fts3Snippet(pContext, pCsr, zStart, zEnd, zEllipsis, iCol, nToken);
  3458. }
  3459. }
  3460. /*
  3461. ** Implementation of the offsets() function for FTS3
  3462. */
  3463. static void fts3OffsetsFunc(
  3464. sqlite3_context *pContext, /* SQLite function call context */
  3465. int nVal, /* Size of argument array */
  3466. sqlite3_value **apVal /* Array of arguments */
  3467. ){
  3468. Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
  3469. UNUSED_PARAMETER(nVal);
  3470. assert( nVal==1 );
  3471. if( fts3FunctionArg(pContext, "offsets", apVal[0], &pCsr) ) return;
  3472. assert( pCsr );
  3473. if( SQLITE_OK==fts3CursorSeek(pContext, pCsr) ){
  3474. sqlite3Fts3Offsets(pContext, pCsr);
  3475. }
  3476. }
  3477. /*
  3478. ** Implementation of the special optimize() function for FTS3. This
  3479. ** function merges all segments in the database to a single segment.
  3480. ** Example usage is:
  3481. **
  3482. ** SELECT optimize(t) FROM t LIMIT 1;
  3483. **
  3484. ** where 't' is the name of an FTS3 table.
  3485. */
  3486. static void fts3OptimizeFunc(
  3487. sqlite3_context *pContext, /* SQLite function call context */
  3488. int nVal, /* Size of argument array */
  3489. sqlite3_value **apVal /* Array of arguments */
  3490. ){
  3491. int rc; /* Return code */
  3492. Fts3Table *p; /* Virtual table handle */
  3493. Fts3Cursor *pCursor; /* Cursor handle passed through apVal[0] */
  3494. UNUSED_PARAMETER(nVal);
  3495. assert( nVal==1 );
  3496. if( fts3FunctionArg(pContext, "optimize", apVal[0], &pCursor) ) return;
  3497. p = (Fts3Table *)pCursor->base.pVtab;
  3498. assert( p );
  3499. rc = sqlite3Fts3Optimize(p);
  3500. switch( rc ){
  3501. case SQLITE_OK:
  3502. sqlite3_result_text(pContext, "Index optimized", -1, SQLITE_STATIC);
  3503. break;
  3504. case SQLITE_DONE:
  3505. sqlite3_result_text(pContext, "Index already optimal", -1, SQLITE_STATIC);
  3506. break;
  3507. default:
  3508. sqlite3_result_error_code(pContext, rc);
  3509. break;
  3510. }
  3511. }
  3512. /*
  3513. ** Implementation of the matchinfo() function for FTS3
  3514. */
  3515. static void fts3MatchinfoFunc(
  3516. sqlite3_context *pContext, /* SQLite function call context */
  3517. int nVal, /* Size of argument array */
  3518. sqlite3_value **apVal /* Array of arguments */
  3519. ){
  3520. Fts3Cursor *pCsr; /* Cursor handle passed through apVal[0] */
  3521. assert( nVal==1 || nVal==2 );
  3522. if( SQLITE_OK==fts3FunctionArg(pContext, "matchinfo", apVal[0], &pCsr) ){
  3523. const char *zArg = 0;
  3524. if( nVal>1 ){
  3525. zArg = (const char *)sqlite3_value_text(apVal[1]);
  3526. }
  3527. sqlite3Fts3Matchinfo(pContext, pCsr, zArg);
  3528. }
  3529. }
  3530. /*
  3531. ** This routine implements the xFindFunction method for the FTS3
  3532. ** virtual table.
  3533. */
  3534. static int fts3FindFunctionMethod(
  3535. sqlite3_vtab *pVtab, /* Virtual table handle */
  3536. int nArg, /* Number of SQL function arguments */
  3537. const char *zName, /* Name of SQL function */
  3538. void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), /* OUT: Result */
  3539. void **ppArg /* Unused */
  3540. ){
  3541. struct Overloaded {
  3542. const char *zName;
  3543. void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
  3544. } aOverload[] = {
  3545. { "snippet", fts3SnippetFunc },
  3546. { "offsets", fts3OffsetsFunc },
  3547. { "optimize", fts3OptimizeFunc },
  3548. { "matchinfo", fts3MatchinfoFunc },
  3549. };
  3550. int i; /* Iterator variable */
  3551. UNUSED_PARAMETER(pVtab);
  3552. UNUSED_PARAMETER(nArg);
  3553. UNUSED_PARAMETER(ppArg);
  3554. for(i=0; i<SizeofArray(aOverload); i++){
  3555. if( strcmp(zName, aOverload[i].zName)==0 ){
  3556. *pxFunc = aOverload[i].xFunc;
  3557. return 1;
  3558. }
  3559. }
  3560. /* No function of the specified name was found. Return 0. */
  3561. return 0;
  3562. }
  3563. /*
  3564. ** Implementation of FTS3 xRename method. Rename an fts3 table.
  3565. */
  3566. static int fts3RenameMethod(
  3567. sqlite3_vtab *pVtab, /* Virtual table handle */
  3568. const char *zName /* New name of table */
  3569. ){
  3570. Fts3Table *p = (Fts3Table *)pVtab;
  3571. sqlite3 *db = p->db; /* Database connection */
  3572. int rc; /* Return Code */
  3573. /* At this point it must be known if the %_stat table exists or not.
  3574. ** So bHasStat may not be 2. */
  3575. rc = fts3SetHasStat(p);
  3576. /* As it happens, the pending terms table is always empty here. This is
  3577. ** because an "ALTER TABLE RENAME TABLE" statement inside a transaction
  3578. ** always opens a savepoint transaction. And the xSavepoint() method
  3579. ** flushes the pending terms table. But leave the (no-op) call to
  3580. ** PendingTermsFlush() in in case that changes.
  3581. */
  3582. assert( p->nPendingData==0 );
  3583. if( rc==SQLITE_OK ){
  3584. rc = sqlite3Fts3PendingTermsFlush(p);
  3585. }
  3586. p->bIgnoreSavepoint = 1;
  3587. if( p->zContentTbl==0 ){
  3588. fts3DbExec(&rc, db,
  3589. "ALTER TABLE %Q.'%q_content' RENAME TO '%q_content';",
  3590. p->zDb, p->zName, zName
  3591. );
  3592. }
  3593. if( p->bHasDocsize ){
  3594. fts3DbExec(&rc, db,
  3595. "ALTER TABLE %Q.'%q_docsize' RENAME TO '%q_docsize';",
  3596. p->zDb, p->zName, zName
  3597. );
  3598. }
  3599. if( p->bHasStat ){
  3600. fts3DbExec(&rc, db,
  3601. "ALTER TABLE %Q.'%q_stat' RENAME TO '%q_stat';",
  3602. p->zDb, p->zName, zName
  3603. );
  3604. }
  3605. fts3DbExec(&rc, db,
  3606. "ALTER TABLE %Q.'%q_segments' RENAME TO '%q_segments';",
  3607. p->zDb, p->zName, zName
  3608. );
  3609. fts3DbExec(&rc, db,
  3610. "ALTER TABLE %Q.'%q_segdir' RENAME TO '%q_segdir';",
  3611. p->zDb, p->zName, zName
  3612. );
  3613. p->bIgnoreSavepoint = 0;
  3614. return rc;
  3615. }
  3616. /*
  3617. ** The xSavepoint() method.
  3618. **
  3619. ** Flush the contents of the pending-terms table to disk.
  3620. */
  3621. static int fts3SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3622. int rc = SQLITE_OK;
  3623. Fts3Table *pTab = (Fts3Table*)pVtab;
  3624. assert( pTab->inTransaction );
  3625. assert( pTab->mxSavepoint<=iSavepoint );
  3626. TESTONLY( pTab->mxSavepoint = iSavepoint );
  3627. if( pTab->bIgnoreSavepoint==0 ){
  3628. if( fts3HashCount(&pTab->aIndex[0].hPending)>0 ){
  3629. char *zSql = sqlite3_mprintf("INSERT INTO %Q.%Q(%Q) VALUES('flush')",
  3630. pTab->zDb, pTab->zName, pTab->zName
  3631. );
  3632. if( zSql ){
  3633. pTab->bIgnoreSavepoint = 1;
  3634. rc = sqlite3_exec(pTab->db, zSql, 0, 0, 0);
  3635. pTab->bIgnoreSavepoint = 0;
  3636. sqlite3_free(zSql);
  3637. }else{
  3638. rc = SQLITE_NOMEM;
  3639. }
  3640. }
  3641. if( rc==SQLITE_OK ){
  3642. pTab->iSavepoint = iSavepoint+1;
  3643. }
  3644. }
  3645. return rc;
  3646. }
  3647. /*
  3648. ** The xRelease() method.
  3649. **
  3650. ** This is a no-op.
  3651. */
  3652. static int fts3ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3653. Fts3Table *pTab = (Fts3Table*)pVtab;
  3654. assert( pTab->inTransaction );
  3655. assert( pTab->mxSavepoint >= iSavepoint );
  3656. TESTONLY( pTab->mxSavepoint = iSavepoint-1 );
  3657. pTab->iSavepoint = iSavepoint;
  3658. return SQLITE_OK;
  3659. }
  3660. /*
  3661. ** The xRollbackTo() method.
  3662. **
  3663. ** Discard the contents of the pending terms table.
  3664. */
  3665. static int fts3RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
  3666. Fts3Table *pTab = (Fts3Table*)pVtab;
  3667. UNUSED_PARAMETER(iSavepoint);
  3668. assert( pTab->inTransaction );
  3669. TESTONLY( pTab->mxSavepoint = iSavepoint );
  3670. if( (iSavepoint+1)<=pTab->iSavepoint ){
  3671. sqlite3Fts3PendingTermsClear(pTab);
  3672. }
  3673. return SQLITE_OK;
  3674. }
  3675. /*
  3676. ** Return true if zName is the extension on one of the shadow tables used
  3677. ** by this module.
  3678. */
  3679. static int fts3ShadowName(const char *zName){
  3680. static const char *azName[] = {
  3681. "content", "docsize", "segdir", "segments", "stat",
  3682. };
  3683. unsigned int i;
  3684. for(i=0; i<sizeof(azName)/sizeof(azName[0]); i++){
  3685. if( sqlite3_stricmp(zName, azName[i])==0 ) return 1;
  3686. }
  3687. return 0;
  3688. }
  3689. /*
  3690. ** Implementation of the xIntegrity() method on the FTS3/FTS4 virtual
  3691. ** table.
  3692. */
  3693. static int fts3IntegrityMethod(
  3694. sqlite3_vtab *pVtab, /* The virtual table to be checked */
  3695. const char *zSchema, /* Name of schema in which pVtab lives */
  3696. const char *zTabname, /* Name of the pVTab table */
  3697. int isQuick, /* True if this is a quick_check */
  3698. char **pzErr /* Write error message here */
  3699. ){
  3700. Fts3Table *p = (Fts3Table*)pVtab;
  3701. int rc = SQLITE_OK;
  3702. int bOk = 0;
  3703. UNUSED_PARAMETER(isQuick);
  3704. rc = sqlite3Fts3IntegrityCheck(p, &bOk);
  3705. assert( rc!=SQLITE_CORRUPT_VTAB );
  3706. if( rc==SQLITE_ERROR || (rc&0xFF)==SQLITE_CORRUPT ){
  3707. *pzErr = sqlite3_mprintf("unable to validate the inverted index for"
  3708. " FTS%d table %s.%s: %s",
  3709. p->bFts4 ? 4 : 3, zSchema, zTabname, sqlite3_errstr(rc));
  3710. if( *pzErr ) rc = SQLITE_OK;
  3711. }else if( rc==SQLITE_OK && bOk==0 ){
  3712. *pzErr = sqlite3_mprintf("malformed inverted index for FTS%d table %s.%s",
  3713. p->bFts4 ? 4 : 3, zSchema, zTabname);
  3714. if( *pzErr==0 ) rc = SQLITE_NOMEM;
  3715. }
  3716. sqlite3Fts3SegmentsClose(p);
  3717. return rc;
  3718. }
  3719. static const sqlite3_module fts3Module = {
  3720. /* iVersion */ 4,
  3721. /* xCreate */ fts3CreateMethod,
  3722. /* xConnect */ fts3ConnectMethod,
  3723. /* xBestIndex */ fts3BestIndexMethod,
  3724. /* xDisconnect */ fts3DisconnectMethod,
  3725. /* xDestroy */ fts3DestroyMethod,
  3726. /* xOpen */ fts3OpenMethod,
  3727. /* xClose */ fts3CloseMethod,
  3728. /* xFilter */ fts3FilterMethod,
  3729. /* xNext */ fts3NextMethod,
  3730. /* xEof */ fts3EofMethod,
  3731. /* xColumn */ fts3ColumnMethod,
  3732. /* xRowid */ fts3RowidMethod,
  3733. /* xUpdate */ fts3UpdateMethod,
  3734. /* xBegin */ fts3BeginMethod,
  3735. /* xSync */ fts3SyncMethod,
  3736. /* xCommit */ fts3CommitMethod,
  3737. /* xRollback */ fts3RollbackMethod,
  3738. /* xFindFunction */ fts3FindFunctionMethod,
  3739. /* xRename */ fts3RenameMethod,
  3740. /* xSavepoint */ fts3SavepointMethod,
  3741. /* xRelease */ fts3ReleaseMethod,
  3742. /* xRollbackTo */ fts3RollbackToMethod,
  3743. /* xShadowName */ fts3ShadowName,
  3744. /* xIntegrity */ fts3IntegrityMethod,
  3745. };
  3746. /*
  3747. ** This function is registered as the module destructor (called when an
  3748. ** FTS3 enabled database connection is closed). It frees the memory
  3749. ** allocated for the tokenizer hash table.
  3750. */
  3751. static void hashDestroy(void *p){
  3752. Fts3HashWrapper *pHash = (Fts3HashWrapper *)p;
  3753. pHash->nRef--;
  3754. if( pHash->nRef<=0 ){
  3755. sqlite3Fts3HashClear(&pHash->hash);
  3756. sqlite3_free(pHash);
  3757. }
  3758. }
  3759. /*
  3760. ** The fts3 built-in tokenizers - "simple", "porter" and "icu"- are
  3761. ** implemented in files fts3_tokenizer1.c, fts3_porter.c and fts3_icu.c
  3762. ** respectively. The following three forward declarations are for functions
  3763. ** declared in these files used to retrieve the respective implementations.
  3764. **
  3765. ** Calling sqlite3Fts3SimpleTokenizerModule() sets the value pointed
  3766. ** to by the argument to point to the "simple" tokenizer implementation.
  3767. ** And so on.
  3768. */
  3769. void sqlite3Fts3SimpleTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  3770. void sqlite3Fts3PorterTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  3771. #ifndef SQLITE_DISABLE_FTS3_UNICODE
  3772. void sqlite3Fts3UnicodeTokenizer(sqlite3_tokenizer_module const**ppModule);
  3773. #endif
  3774. #ifdef SQLITE_ENABLE_ICU
  3775. void sqlite3Fts3IcuTokenizerModule(sqlite3_tokenizer_module const**ppModule);
  3776. #endif
  3777. /*
  3778. ** Initialize the fts3 extension. If this extension is built as part
  3779. ** of the sqlite library, then this function is called directly by
  3780. ** SQLite. If fts3 is built as a dynamically loadable extension, this
  3781. ** function is called by the sqlite3_extension_init() entry point.
  3782. */
  3783. int sqlite3Fts3Init(sqlite3 *db){
  3784. int rc = SQLITE_OK;
  3785. Fts3HashWrapper *pHash = 0;
  3786. const sqlite3_tokenizer_module *pSimple = 0;
  3787. const sqlite3_tokenizer_module *pPorter = 0;
  3788. #ifndef SQLITE_DISABLE_FTS3_UNICODE
  3789. const sqlite3_tokenizer_module *pUnicode = 0;
  3790. #endif
  3791. #ifdef SQLITE_ENABLE_ICU
  3792. const sqlite3_tokenizer_module *pIcu = 0;
  3793. sqlite3Fts3IcuTokenizerModule(&pIcu);
  3794. #endif
  3795. #ifndef SQLITE_DISABLE_FTS3_UNICODE
  3796. sqlite3Fts3UnicodeTokenizer(&pUnicode);
  3797. #endif
  3798. #ifdef SQLITE_TEST
  3799. rc = sqlite3Fts3InitTerm(db);
  3800. if( rc!=SQLITE_OK ) return rc;
  3801. #endif
  3802. rc = sqlite3Fts3InitAux(db);
  3803. if( rc!=SQLITE_OK ) return rc;
  3804. sqlite3Fts3SimpleTokenizerModule(&pSimple);
  3805. sqlite3Fts3PorterTokenizerModule(&pPorter);
  3806. /* Allocate and initialize the hash-table used to store tokenizers. */
  3807. pHash = sqlite3_malloc(sizeof(Fts3HashWrapper));
  3808. if( !pHash ){
  3809. rc = SQLITE_NOMEM;
  3810. }else{
  3811. sqlite3Fts3HashInit(&pHash->hash, FTS3_HASH_STRING, 1);
  3812. pHash->nRef = 0;
  3813. }
  3814. /* Load the built-in tokenizers into the hash table */
  3815. if( rc==SQLITE_OK ){
  3816. if( sqlite3Fts3HashInsert(&pHash->hash, "simple", 7, (void *)pSimple)
  3817. || sqlite3Fts3HashInsert(&pHash->hash, "porter", 7, (void *)pPorter)
  3818. #ifndef SQLITE_DISABLE_FTS3_UNICODE
  3819. || sqlite3Fts3HashInsert(&pHash->hash, "unicode61", 10, (void *)pUnicode)
  3820. #endif
  3821. #ifdef SQLITE_ENABLE_ICU
  3822. || (pIcu && sqlite3Fts3HashInsert(&pHash->hash, "icu", 4, (void *)pIcu))
  3823. #endif
  3824. ){
  3825. rc = SQLITE_NOMEM;
  3826. }
  3827. }
  3828. #ifdef SQLITE_TEST
  3829. if( rc==SQLITE_OK ){
  3830. rc = sqlite3Fts3ExprInitTestInterface(db, &pHash->hash);
  3831. }
  3832. #endif
  3833. /* Create the virtual table wrapper around the hash-table and overload
  3834. ** the four scalar functions. If this is successful, register the
  3835. ** module with sqlite.
  3836. */
  3837. if( SQLITE_OK==rc
  3838. && SQLITE_OK==(rc=sqlite3Fts3InitHashTable(db,&pHash->hash,"fts3_tokenizer"))
  3839. && SQLITE_OK==(rc = sqlite3_overload_function(db, "snippet", -1))
  3840. && SQLITE_OK==(rc = sqlite3_overload_function(db, "offsets", 1))
  3841. && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 1))
  3842. && SQLITE_OK==(rc = sqlite3_overload_function(db, "matchinfo", 2))
  3843. && SQLITE_OK==(rc = sqlite3_overload_function(db, "optimize", 1))
  3844. ){
  3845. pHash->nRef++;
  3846. rc = sqlite3_create_module_v2(
  3847. db, "fts3", &fts3Module, (void *)pHash, hashDestroy
  3848. );
  3849. if( rc==SQLITE_OK ){
  3850. pHash->nRef++;
  3851. rc = sqlite3_create_module_v2(
  3852. db, "fts4", &fts3Module, (void *)pHash, hashDestroy
  3853. );
  3854. }
  3855. if( rc==SQLITE_OK ){
  3856. pHash->nRef++;
  3857. rc = sqlite3Fts3InitTok(db, (void *)pHash, hashDestroy);
  3858. }
  3859. return rc;
  3860. }
  3861. /* An error has occurred. Delete the hash table and return the error code. */
  3862. assert( rc!=SQLITE_OK );
  3863. if( pHash ){
  3864. sqlite3Fts3HashClear(&pHash->hash);
  3865. sqlite3_free(pHash);
  3866. }
  3867. return rc;
  3868. }
  3869. /*
  3870. ** Allocate an Fts3MultiSegReader for each token in the expression headed
  3871. ** by pExpr.
  3872. **
  3873. ** An Fts3SegReader object is a cursor that can seek or scan a range of
  3874. ** entries within a single segment b-tree. An Fts3MultiSegReader uses multiple
  3875. ** Fts3SegReader objects internally to provide an interface to seek or scan
  3876. ** within the union of all segments of a b-tree. Hence the name.
  3877. **
  3878. ** If the allocated Fts3MultiSegReader just seeks to a single entry in a
  3879. ** segment b-tree (if the term is not a prefix or it is a prefix for which
  3880. ** there exists prefix b-tree of the right length) then it may be traversed
  3881. ** and merged incrementally. Otherwise, it has to be merged into an in-memory
  3882. ** doclist and then traversed.
  3883. */
  3884. static void fts3EvalAllocateReaders(
  3885. Fts3Cursor *pCsr, /* FTS cursor handle */
  3886. Fts3Expr *pExpr, /* Allocate readers for this expression */
  3887. int *pnToken, /* OUT: Total number of tokens in phrase. */
  3888. int *pnOr, /* OUT: Total number of OR nodes in expr. */
  3889. int *pRc /* IN/OUT: Error code */
  3890. ){
  3891. if( pExpr && SQLITE_OK==*pRc ){
  3892. if( pExpr->eType==FTSQUERY_PHRASE ){
  3893. int i;
  3894. int nToken = pExpr->pPhrase->nToken;
  3895. *pnToken += nToken;
  3896. for(i=0; i<nToken; i++){
  3897. Fts3PhraseToken *pToken = &pExpr->pPhrase->aToken[i];
  3898. int rc = fts3TermSegReaderCursor(pCsr,
  3899. pToken->z, pToken->n, pToken->isPrefix, &pToken->pSegcsr
  3900. );
  3901. if( rc!=SQLITE_OK ){
  3902. *pRc = rc;
  3903. return;
  3904. }
  3905. }
  3906. assert( pExpr->pPhrase->iDoclistToken==0 );
  3907. pExpr->pPhrase->iDoclistToken = -1;
  3908. }else{
  3909. *pnOr += (pExpr->eType==FTSQUERY_OR);
  3910. fts3EvalAllocateReaders(pCsr, pExpr->pLeft, pnToken, pnOr, pRc);
  3911. fts3EvalAllocateReaders(pCsr, pExpr->pRight, pnToken, pnOr, pRc);
  3912. }
  3913. }
  3914. }
  3915. /*
  3916. ** Arguments pList/nList contain the doclist for token iToken of phrase p.
  3917. ** It is merged into the main doclist stored in p->doclist.aAll/nAll.
  3918. **
  3919. ** This function assumes that pList points to a buffer allocated using
  3920. ** sqlite3_malloc(). This function takes responsibility for eventually
  3921. ** freeing the buffer.
  3922. **
  3923. ** SQLITE_OK is returned if successful, or SQLITE_NOMEM if an error occurs.
  3924. */
  3925. static int fts3EvalPhraseMergeToken(
  3926. Fts3Table *pTab, /* FTS Table pointer */
  3927. Fts3Phrase *p, /* Phrase to merge pList/nList into */
  3928. int iToken, /* Token pList/nList corresponds to */
  3929. char *pList, /* Pointer to doclist */
  3930. int nList /* Number of bytes in pList */
  3931. ){
  3932. int rc = SQLITE_OK;
  3933. assert( iToken!=p->iDoclistToken );
  3934. if( pList==0 ){
  3935. sqlite3_free(p->doclist.aAll);
  3936. p->doclist.aAll = 0;
  3937. p->doclist.nAll = 0;
  3938. }
  3939. else if( p->iDoclistToken<0 ){
  3940. p->doclist.aAll = pList;
  3941. p->doclist.nAll = nList;
  3942. }
  3943. else if( p->doclist.aAll==0 ){
  3944. sqlite3_free(pList);
  3945. }
  3946. else {
  3947. char *pLeft;
  3948. char *pRight;
  3949. int nLeft;
  3950. int nRight;
  3951. int nDiff;
  3952. if( p->iDoclistToken<iToken ){
  3953. pLeft = p->doclist.aAll;
  3954. nLeft = p->doclist.nAll;
  3955. pRight = pList;
  3956. nRight = nList;
  3957. nDiff = iToken - p->iDoclistToken;
  3958. }else{
  3959. pRight = p->doclist.aAll;
  3960. nRight = p->doclist.nAll;
  3961. pLeft = pList;
  3962. nLeft = nList;
  3963. nDiff = p->iDoclistToken - iToken;
  3964. }
  3965. rc = fts3DoclistPhraseMerge(
  3966. pTab->bDescIdx, nDiff, pLeft, nLeft, &pRight, &nRight
  3967. );
  3968. sqlite3_free(pLeft);
  3969. p->doclist.aAll = pRight;
  3970. p->doclist.nAll = nRight;
  3971. }
  3972. if( iToken>p->iDoclistToken ) p->iDoclistToken = iToken;
  3973. return rc;
  3974. }
  3975. /*
  3976. ** Load the doclist for phrase p into p->doclist.aAll/nAll. The loaded doclist
  3977. ** does not take deferred tokens into account.
  3978. **
  3979. ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  3980. */
  3981. static int fts3EvalPhraseLoad(
  3982. Fts3Cursor *pCsr, /* FTS Cursor handle */
  3983. Fts3Phrase *p /* Phrase object */
  3984. ){
  3985. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  3986. int iToken;
  3987. int rc = SQLITE_OK;
  3988. for(iToken=0; rc==SQLITE_OK && iToken<p->nToken; iToken++){
  3989. Fts3PhraseToken *pToken = &p->aToken[iToken];
  3990. assert( pToken->pDeferred==0 || pToken->pSegcsr==0 );
  3991. if( pToken->pSegcsr ){
  3992. int nThis = 0;
  3993. char *pThis = 0;
  3994. rc = fts3TermSelect(pTab, pToken, p->iColumn, &nThis, &pThis);
  3995. if( rc==SQLITE_OK ){
  3996. rc = fts3EvalPhraseMergeToken(pTab, p, iToken, pThis, nThis);
  3997. }
  3998. }
  3999. assert( pToken->pSegcsr==0 );
  4000. }
  4001. return rc;
  4002. }
  4003. #ifndef SQLITE_DISABLE_FTS4_DEFERRED
  4004. /*
  4005. ** This function is called on each phrase after the position lists for
  4006. ** any deferred tokens have been loaded into memory. It updates the phrases
  4007. ** current position list to include only those positions that are really
  4008. ** instances of the phrase (after considering deferred tokens). If this
  4009. ** means that the phrase does not appear in the current row, doclist.pList
  4010. ** and doclist.nList are both zeroed.
  4011. **
  4012. ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  4013. */
  4014. static int fts3EvalDeferredPhrase(Fts3Cursor *pCsr, Fts3Phrase *pPhrase){
  4015. int iToken; /* Used to iterate through phrase tokens */
  4016. char *aPoslist = 0; /* Position list for deferred tokens */
  4017. int nPoslist = 0; /* Number of bytes in aPoslist */
  4018. int iPrev = -1; /* Token number of previous deferred token */
  4019. char *aFree = (pPhrase->doclist.bFreeList ? pPhrase->doclist.pList : 0);
  4020. for(iToken=0; iToken<pPhrase->nToken; iToken++){
  4021. Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
  4022. Fts3DeferredToken *pDeferred = pToken->pDeferred;
  4023. if( pDeferred ){
  4024. char *pList;
  4025. int nList;
  4026. int rc = sqlite3Fts3DeferredTokenList(pDeferred, &pList, &nList);
  4027. if( rc!=SQLITE_OK ) return rc;
  4028. if( pList==0 ){
  4029. sqlite3_free(aPoslist);
  4030. sqlite3_free(aFree);
  4031. pPhrase->doclist.pList = 0;
  4032. pPhrase->doclist.nList = 0;
  4033. return SQLITE_OK;
  4034. }else if( aPoslist==0 ){
  4035. aPoslist = pList;
  4036. nPoslist = nList;
  4037. }else{
  4038. char *aOut = pList;
  4039. char *p1 = aPoslist;
  4040. char *p2 = aOut;
  4041. assert( iPrev>=0 );
  4042. fts3PoslistPhraseMerge(&aOut, iToken-iPrev, 0, 1, &p1, &p2);
  4043. sqlite3_free(aPoslist);
  4044. aPoslist = pList;
  4045. nPoslist = (int)(aOut - aPoslist);
  4046. if( nPoslist==0 ){
  4047. sqlite3_free(aPoslist);
  4048. sqlite3_free(aFree);
  4049. pPhrase->doclist.pList = 0;
  4050. pPhrase->doclist.nList = 0;
  4051. return SQLITE_OK;
  4052. }
  4053. }
  4054. iPrev = iToken;
  4055. }
  4056. }
  4057. if( iPrev>=0 ){
  4058. int nMaxUndeferred = pPhrase->iDoclistToken;
  4059. if( nMaxUndeferred<0 ){
  4060. pPhrase->doclist.pList = aPoslist;
  4061. pPhrase->doclist.nList = nPoslist;
  4062. pPhrase->doclist.iDocid = pCsr->iPrevId;
  4063. pPhrase->doclist.bFreeList = 1;
  4064. }else{
  4065. int nDistance;
  4066. char *p1;
  4067. char *p2;
  4068. char *aOut;
  4069. if( nMaxUndeferred>iPrev ){
  4070. p1 = aPoslist;
  4071. p2 = pPhrase->doclist.pList;
  4072. nDistance = nMaxUndeferred - iPrev;
  4073. }else{
  4074. p1 = pPhrase->doclist.pList;
  4075. p2 = aPoslist;
  4076. nDistance = iPrev - nMaxUndeferred;
  4077. }
  4078. aOut = (char *)sqlite3Fts3MallocZero(nPoslist+FTS3_BUFFER_PADDING);
  4079. if( !aOut ){
  4080. sqlite3_free(aPoslist);
  4081. return SQLITE_NOMEM;
  4082. }
  4083. pPhrase->doclist.pList = aOut;
  4084. assert( p1 && p2 );
  4085. if( fts3PoslistPhraseMerge(&aOut, nDistance, 0, 1, &p1, &p2) ){
  4086. pPhrase->doclist.bFreeList = 1;
  4087. pPhrase->doclist.nList = (int)(aOut - pPhrase->doclist.pList);
  4088. }else{
  4089. sqlite3_free(aOut);
  4090. pPhrase->doclist.pList = 0;
  4091. pPhrase->doclist.nList = 0;
  4092. }
  4093. sqlite3_free(aPoslist);
  4094. }
  4095. }
  4096. if( pPhrase->doclist.pList!=aFree ) sqlite3_free(aFree);
  4097. return SQLITE_OK;
  4098. }
  4099. #endif /* SQLITE_DISABLE_FTS4_DEFERRED */
  4100. /*
  4101. ** Maximum number of tokens a phrase may have to be considered for the
  4102. ** incremental doclists strategy.
  4103. */
  4104. #define MAX_INCR_PHRASE_TOKENS 4
  4105. /*
  4106. ** This function is called for each Fts3Phrase in a full-text query
  4107. ** expression to initialize the mechanism for returning rows. Once this
  4108. ** function has been called successfully on an Fts3Phrase, it may be
  4109. ** used with fts3EvalPhraseNext() to iterate through the matching docids.
  4110. **
  4111. ** If parameter bOptOk is true, then the phrase may (or may not) use the
  4112. ** incremental loading strategy. Otherwise, the entire doclist is loaded into
  4113. ** memory within this call.
  4114. **
  4115. ** SQLITE_OK is returned if no error occurs, otherwise an SQLite error code.
  4116. */
  4117. static int fts3EvalPhraseStart(Fts3Cursor *pCsr, int bOptOk, Fts3Phrase *p){
  4118. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  4119. int rc = SQLITE_OK; /* Error code */
  4120. int i;
  4121. /* Determine if doclists may be loaded from disk incrementally. This is
  4122. ** possible if the bOptOk argument is true, the FTS doclists will be
  4123. ** scanned in forward order, and the phrase consists of
  4124. ** MAX_INCR_PHRASE_TOKENS or fewer tokens, none of which are are "^first"
  4125. ** tokens or prefix tokens that cannot use a prefix-index. */
  4126. int bHaveIncr = 0;
  4127. int bIncrOk = (bOptOk
  4128. && pCsr->bDesc==pTab->bDescIdx
  4129. && p->nToken<=MAX_INCR_PHRASE_TOKENS && p->nToken>0
  4130. #if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
  4131. && pTab->bNoIncrDoclist==0
  4132. #endif
  4133. );
  4134. for(i=0; bIncrOk==1 && i<p->nToken; i++){
  4135. Fts3PhraseToken *pToken = &p->aToken[i];
  4136. if( pToken->bFirst || (pToken->pSegcsr!=0 && !pToken->pSegcsr->bLookup) ){
  4137. bIncrOk = 0;
  4138. }
  4139. if( pToken->pSegcsr ) bHaveIncr = 1;
  4140. }
  4141. if( bIncrOk && bHaveIncr ){
  4142. /* Use the incremental approach. */
  4143. int iCol = (p->iColumn >= pTab->nColumn ? -1 : p->iColumn);
  4144. for(i=0; rc==SQLITE_OK && i<p->nToken; i++){
  4145. Fts3PhraseToken *pToken = &p->aToken[i];
  4146. Fts3MultiSegReader *pSegcsr = pToken->pSegcsr;
  4147. if( pSegcsr ){
  4148. rc = sqlite3Fts3MsrIncrStart(pTab, pSegcsr, iCol, pToken->z, pToken->n);
  4149. }
  4150. }
  4151. p->bIncr = 1;
  4152. }else{
  4153. /* Load the full doclist for the phrase into memory. */
  4154. rc = fts3EvalPhraseLoad(pCsr, p);
  4155. p->bIncr = 0;
  4156. }
  4157. assert( rc!=SQLITE_OK || p->nToken<1 || p->aToken[0].pSegcsr==0 || p->bIncr );
  4158. return rc;
  4159. }
  4160. /*
  4161. ** This function is used to iterate backwards (from the end to start)
  4162. ** through doclists. It is used by this module to iterate through phrase
  4163. ** doclists in reverse and by the fts3_write.c module to iterate through
  4164. ** pending-terms lists when writing to databases with "order=desc".
  4165. **
  4166. ** The doclist may be sorted in ascending (parameter bDescIdx==0) or
  4167. ** descending (parameter bDescIdx==1) order of docid. Regardless, this
  4168. ** function iterates from the end of the doclist to the beginning.
  4169. */
  4170. void sqlite3Fts3DoclistPrev(
  4171. int bDescIdx, /* True if the doclist is desc */
  4172. char *aDoclist, /* Pointer to entire doclist */
  4173. int nDoclist, /* Length of aDoclist in bytes */
  4174. char **ppIter, /* IN/OUT: Iterator pointer */
  4175. sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
  4176. int *pnList, /* OUT: List length pointer */
  4177. u8 *pbEof /* OUT: End-of-file flag */
  4178. ){
  4179. char *p = *ppIter;
  4180. assert( nDoclist>0 );
  4181. assert( *pbEof==0 );
  4182. assert_fts3_nc( p || *piDocid==0 );
  4183. assert( !p || (p>aDoclist && p<&aDoclist[nDoclist]) );
  4184. if( p==0 ){
  4185. sqlite3_int64 iDocid = 0;
  4186. char *pNext = 0;
  4187. char *pDocid = aDoclist;
  4188. char *pEnd = &aDoclist[nDoclist];
  4189. int iMul = 1;
  4190. while( pDocid<pEnd ){
  4191. sqlite3_int64 iDelta;
  4192. pDocid += sqlite3Fts3GetVarint(pDocid, &iDelta);
  4193. iDocid += (iMul * iDelta);
  4194. pNext = pDocid;
  4195. fts3PoslistCopy(0, &pDocid);
  4196. while( pDocid<pEnd && *pDocid==0 ) pDocid++;
  4197. iMul = (bDescIdx ? -1 : 1);
  4198. }
  4199. *pnList = (int)(pEnd - pNext);
  4200. *ppIter = pNext;
  4201. *piDocid = iDocid;
  4202. }else{
  4203. int iMul = (bDescIdx ? -1 : 1);
  4204. sqlite3_int64 iDelta;
  4205. fts3GetReverseVarint(&p, aDoclist, &iDelta);
  4206. *piDocid -= (iMul * iDelta);
  4207. if( p==aDoclist ){
  4208. *pbEof = 1;
  4209. }else{
  4210. char *pSave = p;
  4211. fts3ReversePoslist(aDoclist, &p);
  4212. *pnList = (int)(pSave - p);
  4213. }
  4214. *ppIter = p;
  4215. }
  4216. }
  4217. /*
  4218. ** Iterate forwards through a doclist.
  4219. */
  4220. void sqlite3Fts3DoclistNext(
  4221. int bDescIdx, /* True if the doclist is desc */
  4222. char *aDoclist, /* Pointer to entire doclist */
  4223. int nDoclist, /* Length of aDoclist in bytes */
  4224. char **ppIter, /* IN/OUT: Iterator pointer */
  4225. sqlite3_int64 *piDocid, /* IN/OUT: Docid pointer */
  4226. u8 *pbEof /* OUT: End-of-file flag */
  4227. ){
  4228. char *p = *ppIter;
  4229. assert( nDoclist>0 );
  4230. assert( *pbEof==0 );
  4231. assert_fts3_nc( p || *piDocid==0 );
  4232. assert( !p || (p>=aDoclist && p<=&aDoclist[nDoclist]) );
  4233. if( p==0 ){
  4234. p = aDoclist;
  4235. p += sqlite3Fts3GetVarint(p, piDocid);
  4236. }else{
  4237. fts3PoslistCopy(0, &p);
  4238. while( p<&aDoclist[nDoclist] && *p==0 ) p++;
  4239. if( p>=&aDoclist[nDoclist] ){
  4240. *pbEof = 1;
  4241. }else{
  4242. sqlite3_int64 iVar;
  4243. p += sqlite3Fts3GetVarint(p, &iVar);
  4244. *piDocid += ((bDescIdx ? -1 : 1) * iVar);
  4245. }
  4246. }
  4247. *ppIter = p;
  4248. }
  4249. /*
  4250. ** Advance the iterator pDL to the next entry in pDL->aAll/nAll. Set *pbEof
  4251. ** to true if EOF is reached.
  4252. */
  4253. static void fts3EvalDlPhraseNext(
  4254. Fts3Table *pTab,
  4255. Fts3Doclist *pDL,
  4256. u8 *pbEof
  4257. ){
  4258. char *pIter; /* Used to iterate through aAll */
  4259. char *pEnd; /* 1 byte past end of aAll */
  4260. if( pDL->pNextDocid ){
  4261. pIter = pDL->pNextDocid;
  4262. assert( pDL->aAll!=0 || pIter==0 );
  4263. }else{
  4264. pIter = pDL->aAll;
  4265. }
  4266. if( pIter==0 || pIter>=(pEnd = pDL->aAll + pDL->nAll) ){
  4267. /* We have already reached the end of this doclist. EOF. */
  4268. *pbEof = 1;
  4269. }else{
  4270. sqlite3_int64 iDelta;
  4271. pIter += sqlite3Fts3GetVarint(pIter, &iDelta);
  4272. if( pTab->bDescIdx==0 || pDL->pNextDocid==0 ){
  4273. pDL->iDocid += iDelta;
  4274. }else{
  4275. pDL->iDocid -= iDelta;
  4276. }
  4277. pDL->pList = pIter;
  4278. fts3PoslistCopy(0, &pIter);
  4279. pDL->nList = (int)(pIter - pDL->pList);
  4280. /* pIter now points just past the 0x00 that terminates the position-
  4281. ** list for document pDL->iDocid. However, if this position-list was
  4282. ** edited in place by fts3EvalNearTrim(), then pIter may not actually
  4283. ** point to the start of the next docid value. The following line deals
  4284. ** with this case by advancing pIter past the zero-padding added by
  4285. ** fts3EvalNearTrim(). */
  4286. while( pIter<pEnd && *pIter==0 ) pIter++;
  4287. pDL->pNextDocid = pIter;
  4288. assert( pIter>=&pDL->aAll[pDL->nAll] || *pIter );
  4289. *pbEof = 0;
  4290. }
  4291. }
  4292. /*
  4293. ** Helper type used by fts3EvalIncrPhraseNext() and incrPhraseTokenNext().
  4294. */
  4295. typedef struct TokenDoclist TokenDoclist;
  4296. struct TokenDoclist {
  4297. int bIgnore;
  4298. sqlite3_int64 iDocid;
  4299. char *pList;
  4300. int nList;
  4301. };
  4302. /*
  4303. ** Token pToken is an incrementally loaded token that is part of a
  4304. ** multi-token phrase. Advance it to the next matching document in the
  4305. ** database and populate output variable *p with the details of the new
  4306. ** entry. Or, if the iterator has reached EOF, set *pbEof to true.
  4307. **
  4308. ** If an error occurs, return an SQLite error code. Otherwise, return
  4309. ** SQLITE_OK.
  4310. */
  4311. static int incrPhraseTokenNext(
  4312. Fts3Table *pTab, /* Virtual table handle */
  4313. Fts3Phrase *pPhrase, /* Phrase to advance token of */
  4314. int iToken, /* Specific token to advance */
  4315. TokenDoclist *p, /* OUT: Docid and doclist for new entry */
  4316. u8 *pbEof /* OUT: True if iterator is at EOF */
  4317. ){
  4318. int rc = SQLITE_OK;
  4319. if( pPhrase->iDoclistToken==iToken ){
  4320. assert( p->bIgnore==0 );
  4321. assert( pPhrase->aToken[iToken].pSegcsr==0 );
  4322. fts3EvalDlPhraseNext(pTab, &pPhrase->doclist, pbEof);
  4323. p->pList = pPhrase->doclist.pList;
  4324. p->nList = pPhrase->doclist.nList;
  4325. p->iDocid = pPhrase->doclist.iDocid;
  4326. }else{
  4327. Fts3PhraseToken *pToken = &pPhrase->aToken[iToken];
  4328. assert( pToken->pDeferred==0 );
  4329. assert( pToken->pSegcsr || pPhrase->iDoclistToken>=0 );
  4330. if( pToken->pSegcsr ){
  4331. assert( p->bIgnore==0 );
  4332. rc = sqlite3Fts3MsrIncrNext(
  4333. pTab, pToken->pSegcsr, &p->iDocid, &p->pList, &p->nList
  4334. );
  4335. if( p->pList==0 ) *pbEof = 1;
  4336. }else{
  4337. p->bIgnore = 1;
  4338. }
  4339. }
  4340. return rc;
  4341. }
  4342. /*
  4343. ** The phrase iterator passed as the second argument:
  4344. **
  4345. ** * features at least one token that uses an incremental doclist, and
  4346. **
  4347. ** * does not contain any deferred tokens.
  4348. **
  4349. ** Advance it to the next matching documnent in the database and populate
  4350. ** the Fts3Doclist.pList and nList fields.
  4351. **
  4352. ** If there is no "next" entry and no error occurs, then *pbEof is set to
  4353. ** 1 before returning. Otherwise, if no error occurs and the iterator is
  4354. ** successfully advanced, *pbEof is set to 0.
  4355. **
  4356. ** If an error occurs, return an SQLite error code. Otherwise, return
  4357. ** SQLITE_OK.
  4358. */
  4359. static int fts3EvalIncrPhraseNext(
  4360. Fts3Cursor *pCsr, /* FTS Cursor handle */
  4361. Fts3Phrase *p, /* Phrase object to advance to next docid */
  4362. u8 *pbEof /* OUT: Set to 1 if EOF */
  4363. ){
  4364. int rc = SQLITE_OK;
  4365. Fts3Doclist *pDL = &p->doclist;
  4366. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  4367. u8 bEof = 0;
  4368. /* This is only called if it is guaranteed that the phrase has at least
  4369. ** one incremental token. In which case the bIncr flag is set. */
  4370. assert( p->bIncr==1 );
  4371. if( p->nToken==1 ){
  4372. rc = sqlite3Fts3MsrIncrNext(pTab, p->aToken[0].pSegcsr,
  4373. &pDL->iDocid, &pDL->pList, &pDL->nList
  4374. );
  4375. if( pDL->pList==0 ) bEof = 1;
  4376. }else{
  4377. int bDescDoclist = pCsr->bDesc;
  4378. struct TokenDoclist a[MAX_INCR_PHRASE_TOKENS];
  4379. memset(a, 0, sizeof(a));
  4380. assert( p->nToken<=MAX_INCR_PHRASE_TOKENS );
  4381. assert( p->iDoclistToken<MAX_INCR_PHRASE_TOKENS );
  4382. while( bEof==0 ){
  4383. int bMaxSet = 0;
  4384. sqlite3_int64 iMax = 0; /* Largest docid for all iterators */
  4385. int i; /* Used to iterate through tokens */
  4386. /* Advance the iterator for each token in the phrase once. */
  4387. for(i=0; rc==SQLITE_OK && i<p->nToken && bEof==0; i++){
  4388. rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
  4389. if( a[i].bIgnore==0 && (bMaxSet==0 || DOCID_CMP(iMax, a[i].iDocid)<0) ){
  4390. iMax = a[i].iDocid;
  4391. bMaxSet = 1;
  4392. }
  4393. }
  4394. assert( rc!=SQLITE_OK || (p->nToken>=1 && a[p->nToken-1].bIgnore==0) );
  4395. assert( rc!=SQLITE_OK || bMaxSet );
  4396. /* Keep advancing iterators until they all point to the same document */
  4397. for(i=0; i<p->nToken; i++){
  4398. while( rc==SQLITE_OK && bEof==0
  4399. && a[i].bIgnore==0 && DOCID_CMP(a[i].iDocid, iMax)<0
  4400. ){
  4401. rc = incrPhraseTokenNext(pTab, p, i, &a[i], &bEof);
  4402. if( DOCID_CMP(a[i].iDocid, iMax)>0 ){
  4403. iMax = a[i].iDocid;
  4404. i = 0;
  4405. }
  4406. }
  4407. }
  4408. /* Check if the current entries really are a phrase match */
  4409. if( bEof==0 ){
  4410. int nList = 0;
  4411. int nByte = a[p->nToken-1].nList;
  4412. char *aDoclist = sqlite3_malloc64((i64)nByte+FTS3_BUFFER_PADDING);
  4413. if( !aDoclist ) return SQLITE_NOMEM;
  4414. memcpy(aDoclist, a[p->nToken-1].pList, nByte+1);
  4415. memset(&aDoclist[nByte], 0, FTS3_BUFFER_PADDING);
  4416. for(i=0; i<(p->nToken-1); i++){
  4417. if( a[i].bIgnore==0 ){
  4418. char *pL = a[i].pList;
  4419. char *pR = aDoclist;
  4420. char *pOut = aDoclist;
  4421. int nDist = p->nToken-1-i;
  4422. int res = fts3PoslistPhraseMerge(&pOut, nDist, 0, 1, &pL, &pR);
  4423. if( res==0 ) break;
  4424. nList = (int)(pOut - aDoclist);
  4425. }
  4426. }
  4427. if( i==(p->nToken-1) ){
  4428. pDL->iDocid = iMax;
  4429. pDL->pList = aDoclist;
  4430. pDL->nList = nList;
  4431. pDL->bFreeList = 1;
  4432. break;
  4433. }
  4434. sqlite3_free(aDoclist);
  4435. }
  4436. }
  4437. }
  4438. *pbEof = bEof;
  4439. return rc;
  4440. }
  4441. /*
  4442. ** Attempt to move the phrase iterator to point to the next matching docid.
  4443. ** If an error occurs, return an SQLite error code. Otherwise, return
  4444. ** SQLITE_OK.
  4445. **
  4446. ** If there is no "next" entry and no error occurs, then *pbEof is set to
  4447. ** 1 before returning. Otherwise, if no error occurs and the iterator is
  4448. ** successfully advanced, *pbEof is set to 0.
  4449. */
  4450. static int fts3EvalPhraseNext(
  4451. Fts3Cursor *pCsr, /* FTS Cursor handle */
  4452. Fts3Phrase *p, /* Phrase object to advance to next docid */
  4453. u8 *pbEof /* OUT: Set to 1 if EOF */
  4454. ){
  4455. int rc = SQLITE_OK;
  4456. Fts3Doclist *pDL = &p->doclist;
  4457. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  4458. if( p->bIncr ){
  4459. rc = fts3EvalIncrPhraseNext(pCsr, p, pbEof);
  4460. }else if( pCsr->bDesc!=pTab->bDescIdx && pDL->nAll ){
  4461. sqlite3Fts3DoclistPrev(pTab->bDescIdx, pDL->aAll, pDL->nAll,
  4462. &pDL->pNextDocid, &pDL->iDocid, &pDL->nList, pbEof
  4463. );
  4464. pDL->pList = pDL->pNextDocid;
  4465. }else{
  4466. fts3EvalDlPhraseNext(pTab, pDL, pbEof);
  4467. }
  4468. return rc;
  4469. }
  4470. /*
  4471. **
  4472. ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
  4473. ** Otherwise, fts3EvalPhraseStart() is called on all phrases within the
  4474. ** expression. Also the Fts3Expr.bDeferred variable is set to true for any
  4475. ** expressions for which all descendent tokens are deferred.
  4476. **
  4477. ** If parameter bOptOk is zero, then it is guaranteed that the
  4478. ** Fts3Phrase.doclist.aAll/nAll variables contain the entire doclist for
  4479. ** each phrase in the expression (subject to deferred token processing).
  4480. ** Or, if bOptOk is non-zero, then one or more tokens within the expression
  4481. ** may be loaded incrementally, meaning doclist.aAll/nAll is not available.
  4482. **
  4483. ** If an error occurs within this function, *pRc is set to an SQLite error
  4484. ** code before returning.
  4485. */
  4486. static void fts3EvalStartReaders(
  4487. Fts3Cursor *pCsr, /* FTS Cursor handle */
  4488. Fts3Expr *pExpr, /* Expression to initialize phrases in */
  4489. int *pRc /* IN/OUT: Error code */
  4490. ){
  4491. if( pExpr && SQLITE_OK==*pRc ){
  4492. if( pExpr->eType==FTSQUERY_PHRASE ){
  4493. int nToken = pExpr->pPhrase->nToken;
  4494. if( nToken ){
  4495. int i;
  4496. for(i=0; i<nToken; i++){
  4497. if( pExpr->pPhrase->aToken[i].pDeferred==0 ) break;
  4498. }
  4499. pExpr->bDeferred = (i==nToken);
  4500. }
  4501. *pRc = fts3EvalPhraseStart(pCsr, 1, pExpr->pPhrase);
  4502. }else{
  4503. fts3EvalStartReaders(pCsr, pExpr->pLeft, pRc);
  4504. fts3EvalStartReaders(pCsr, pExpr->pRight, pRc);
  4505. pExpr->bDeferred = (pExpr->pLeft->bDeferred && pExpr->pRight->bDeferred);
  4506. }
  4507. }
  4508. }
  4509. /*
  4510. ** An array of the following structures is assembled as part of the process
  4511. ** of selecting tokens to defer before the query starts executing (as part
  4512. ** of the xFilter() method). There is one element in the array for each
  4513. ** token in the FTS expression.
  4514. **
  4515. ** Tokens are divided into AND/NEAR clusters. All tokens in a cluster belong
  4516. ** to phrases that are connected only by AND and NEAR operators (not OR or
  4517. ** NOT). When determining tokens to defer, each AND/NEAR cluster is considered
  4518. ** separately. The root of a tokens AND/NEAR cluster is stored in
  4519. ** Fts3TokenAndCost.pRoot.
  4520. */
  4521. typedef struct Fts3TokenAndCost Fts3TokenAndCost;
  4522. struct Fts3TokenAndCost {
  4523. Fts3Phrase *pPhrase; /* The phrase the token belongs to */
  4524. int iToken; /* Position of token in phrase */
  4525. Fts3PhraseToken *pToken; /* The token itself */
  4526. Fts3Expr *pRoot; /* Root of NEAR/AND cluster */
  4527. int nOvfl; /* Number of overflow pages to load doclist */
  4528. int iCol; /* The column the token must match */
  4529. };
  4530. /*
  4531. ** This function is used to populate an allocated Fts3TokenAndCost array.
  4532. **
  4533. ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
  4534. ** Otherwise, if an error occurs during execution, *pRc is set to an
  4535. ** SQLite error code.
  4536. */
  4537. static void fts3EvalTokenCosts(
  4538. Fts3Cursor *pCsr, /* FTS Cursor handle */
  4539. Fts3Expr *pRoot, /* Root of current AND/NEAR cluster */
  4540. Fts3Expr *pExpr, /* Expression to consider */
  4541. Fts3TokenAndCost **ppTC, /* Write new entries to *(*ppTC)++ */
  4542. Fts3Expr ***ppOr, /* Write new OR root to *(*ppOr)++ */
  4543. int *pRc /* IN/OUT: Error code */
  4544. ){
  4545. if( *pRc==SQLITE_OK ){
  4546. if( pExpr->eType==FTSQUERY_PHRASE ){
  4547. Fts3Phrase *pPhrase = pExpr->pPhrase;
  4548. int i;
  4549. for(i=0; *pRc==SQLITE_OK && i<pPhrase->nToken; i++){
  4550. Fts3TokenAndCost *pTC = (*ppTC)++;
  4551. pTC->pPhrase = pPhrase;
  4552. pTC->iToken = i;
  4553. pTC->pRoot = pRoot;
  4554. pTC->pToken = &pPhrase->aToken[i];
  4555. pTC->iCol = pPhrase->iColumn;
  4556. *pRc = sqlite3Fts3MsrOvfl(pCsr, pTC->pToken->pSegcsr, &pTC->nOvfl);
  4557. }
  4558. }else if( pExpr->eType!=FTSQUERY_NOT ){
  4559. assert( pExpr->eType==FTSQUERY_OR
  4560. || pExpr->eType==FTSQUERY_AND
  4561. || pExpr->eType==FTSQUERY_NEAR
  4562. );
  4563. assert( pExpr->pLeft && pExpr->pRight );
  4564. if( pExpr->eType==FTSQUERY_OR ){
  4565. pRoot = pExpr->pLeft;
  4566. **ppOr = pRoot;
  4567. (*ppOr)++;
  4568. }
  4569. fts3EvalTokenCosts(pCsr, pRoot, pExpr->pLeft, ppTC, ppOr, pRc);
  4570. if( pExpr->eType==FTSQUERY_OR ){
  4571. pRoot = pExpr->pRight;
  4572. **ppOr = pRoot;
  4573. (*ppOr)++;
  4574. }
  4575. fts3EvalTokenCosts(pCsr, pRoot, pExpr->pRight, ppTC, ppOr, pRc);
  4576. }
  4577. }
  4578. }
  4579. /*
  4580. ** Determine the average document (row) size in pages. If successful,
  4581. ** write this value to *pnPage and return SQLITE_OK. Otherwise, return
  4582. ** an SQLite error code.
  4583. **
  4584. ** The average document size in pages is calculated by first calculating
  4585. ** determining the average size in bytes, B. If B is less than the amount
  4586. ** of data that will fit on a single leaf page of an intkey table in
  4587. ** this database, then the average docsize is 1. Otherwise, it is 1 plus
  4588. ** the number of overflow pages consumed by a record B bytes in size.
  4589. */
  4590. static int fts3EvalAverageDocsize(Fts3Cursor *pCsr, int *pnPage){
  4591. int rc = SQLITE_OK;
  4592. if( pCsr->nRowAvg==0 ){
  4593. /* The average document size, which is required to calculate the cost
  4594. ** of each doclist, has not yet been determined. Read the required
  4595. ** data from the %_stat table to calculate it.
  4596. **
  4597. ** Entry 0 of the %_stat table is a blob containing (nCol+1) FTS3
  4598. ** varints, where nCol is the number of columns in the FTS3 table.
  4599. ** The first varint is the number of documents currently stored in
  4600. ** the table. The following nCol varints contain the total amount of
  4601. ** data stored in all rows of each column of the table, from left
  4602. ** to right.
  4603. */
  4604. Fts3Table *p = (Fts3Table*)pCsr->base.pVtab;
  4605. sqlite3_stmt *pStmt;
  4606. sqlite3_int64 nDoc = 0;
  4607. sqlite3_int64 nByte = 0;
  4608. const char *pEnd;
  4609. const char *a;
  4610. rc = sqlite3Fts3SelectDoctotal(p, &pStmt);
  4611. if( rc!=SQLITE_OK ) return rc;
  4612. a = sqlite3_column_blob(pStmt, 0);
  4613. testcase( a==0 ); /* If %_stat.value set to X'' */
  4614. if( a ){
  4615. pEnd = &a[sqlite3_column_bytes(pStmt, 0)];
  4616. a += sqlite3Fts3GetVarintBounded(a, pEnd, &nDoc);
  4617. while( a<pEnd ){
  4618. a += sqlite3Fts3GetVarintBounded(a, pEnd, &nByte);
  4619. }
  4620. }
  4621. if( nDoc==0 || nByte==0 ){
  4622. sqlite3_reset(pStmt);
  4623. return FTS_CORRUPT_VTAB;
  4624. }
  4625. pCsr->nDoc = nDoc;
  4626. pCsr->nRowAvg = (int)(((nByte / nDoc) + p->nPgsz) / p->nPgsz);
  4627. assert( pCsr->nRowAvg>0 );
  4628. rc = sqlite3_reset(pStmt);
  4629. }
  4630. *pnPage = pCsr->nRowAvg;
  4631. return rc;
  4632. }
  4633. /*
  4634. ** This function is called to select the tokens (if any) that will be
  4635. ** deferred. The array aTC[] has already been populated when this is
  4636. ** called.
  4637. **
  4638. ** This function is called once for each AND/NEAR cluster in the
  4639. ** expression. Each invocation determines which tokens to defer within
  4640. ** the cluster with root node pRoot. See comments above the definition
  4641. ** of struct Fts3TokenAndCost for more details.
  4642. **
  4643. ** If no error occurs, SQLITE_OK is returned and sqlite3Fts3DeferToken()
  4644. ** called on each token to defer. Otherwise, an SQLite error code is
  4645. ** returned.
  4646. */
  4647. static int fts3EvalSelectDeferred(
  4648. Fts3Cursor *pCsr, /* FTS Cursor handle */
  4649. Fts3Expr *pRoot, /* Consider tokens with this root node */
  4650. Fts3TokenAndCost *aTC, /* Array of expression tokens and costs */
  4651. int nTC /* Number of entries in aTC[] */
  4652. ){
  4653. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  4654. int nDocSize = 0; /* Number of pages per doc loaded */
  4655. int rc = SQLITE_OK; /* Return code */
  4656. int ii; /* Iterator variable for various purposes */
  4657. int nOvfl = 0; /* Total overflow pages used by doclists */
  4658. int nToken = 0; /* Total number of tokens in cluster */
  4659. int nMinEst = 0; /* The minimum count for any phrase so far. */
  4660. int nLoad4 = 1; /* (Phrases that will be loaded)^4. */
  4661. /* Tokens are never deferred for FTS tables created using the content=xxx
  4662. ** option. The reason being that it is not guaranteed that the content
  4663. ** table actually contains the same data as the index. To prevent this from
  4664. ** causing any problems, the deferred token optimization is completely
  4665. ** disabled for content=xxx tables. */
  4666. if( pTab->zContentTbl ){
  4667. return SQLITE_OK;
  4668. }
  4669. /* Count the tokens in this AND/NEAR cluster. If none of the doclists
  4670. ** associated with the tokens spill onto overflow pages, or if there is
  4671. ** only 1 token, exit early. No tokens to defer in this case. */
  4672. for(ii=0; ii<nTC; ii++){
  4673. if( aTC[ii].pRoot==pRoot ){
  4674. nOvfl += aTC[ii].nOvfl;
  4675. nToken++;
  4676. }
  4677. }
  4678. if( nOvfl==0 || nToken<2 ) return SQLITE_OK;
  4679. /* Obtain the average docsize (in pages). */
  4680. rc = fts3EvalAverageDocsize(pCsr, &nDocSize);
  4681. assert( rc!=SQLITE_OK || nDocSize>0 );
  4682. /* Iterate through all tokens in this AND/NEAR cluster, in ascending order
  4683. ** of the number of overflow pages that will be loaded by the pager layer
  4684. ** to retrieve the entire doclist for the token from the full-text index.
  4685. ** Load the doclists for tokens that are either:
  4686. **
  4687. ** a. The cheapest token in the entire query (i.e. the one visited by the
  4688. ** first iteration of this loop), or
  4689. **
  4690. ** b. Part of a multi-token phrase.
  4691. **
  4692. ** After each token doclist is loaded, merge it with the others from the
  4693. ** same phrase and count the number of documents that the merged doclist
  4694. ** contains. Set variable "nMinEst" to the smallest number of documents in
  4695. ** any phrase doclist for which 1 or more token doclists have been loaded.
  4696. ** Let nOther be the number of other phrases for which it is certain that
  4697. ** one or more tokens will not be deferred.
  4698. **
  4699. ** Then, for each token, defer it if loading the doclist would result in
  4700. ** loading N or more overflow pages into memory, where N is computed as:
  4701. **
  4702. ** (nMinEst + 4^nOther - 1) / (4^nOther)
  4703. */
  4704. for(ii=0; ii<nToken && rc==SQLITE_OK; ii++){
  4705. int iTC; /* Used to iterate through aTC[] array. */
  4706. Fts3TokenAndCost *pTC = 0; /* Set to cheapest remaining token. */
  4707. /* Set pTC to point to the cheapest remaining token. */
  4708. for(iTC=0; iTC<nTC; iTC++){
  4709. if( aTC[iTC].pToken && aTC[iTC].pRoot==pRoot
  4710. && (!pTC || aTC[iTC].nOvfl<pTC->nOvfl)
  4711. ){
  4712. pTC = &aTC[iTC];
  4713. }
  4714. }
  4715. assert( pTC );
  4716. if( ii && pTC->nOvfl>=((nMinEst+(nLoad4/4)-1)/(nLoad4/4))*nDocSize ){
  4717. /* The number of overflow pages to load for this (and therefore all
  4718. ** subsequent) tokens is greater than the estimated number of pages
  4719. ** that will be loaded if all subsequent tokens are deferred.
  4720. */
  4721. Fts3PhraseToken *pToken = pTC->pToken;
  4722. rc = sqlite3Fts3DeferToken(pCsr, pToken, pTC->iCol);
  4723. fts3SegReaderCursorFree(pToken->pSegcsr);
  4724. pToken->pSegcsr = 0;
  4725. }else{
  4726. /* Set nLoad4 to the value of (4^nOther) for the next iteration of the
  4727. ** for-loop. Except, limit the value to 2^24 to prevent it from
  4728. ** overflowing the 32-bit integer it is stored in. */
  4729. if( ii<12 ) nLoad4 = nLoad4*4;
  4730. if( ii==0 || (pTC->pPhrase->nToken>1 && ii!=nToken-1) ){
  4731. /* Either this is the cheapest token in the entire query, or it is
  4732. ** part of a multi-token phrase. Either way, the entire doclist will
  4733. ** (eventually) be loaded into memory. It may as well be now. */
  4734. Fts3PhraseToken *pToken = pTC->pToken;
  4735. int nList = 0;
  4736. char *pList = 0;
  4737. rc = fts3TermSelect(pTab, pToken, pTC->iCol, &nList, &pList);
  4738. assert( rc==SQLITE_OK || pList==0 );
  4739. if( rc==SQLITE_OK ){
  4740. rc = fts3EvalPhraseMergeToken(
  4741. pTab, pTC->pPhrase, pTC->iToken,pList,nList
  4742. );
  4743. }
  4744. if( rc==SQLITE_OK ){
  4745. int nCount;
  4746. nCount = fts3DoclistCountDocids(
  4747. pTC->pPhrase->doclist.aAll, pTC->pPhrase->doclist.nAll
  4748. );
  4749. if( ii==0 || nCount<nMinEst ) nMinEst = nCount;
  4750. }
  4751. }
  4752. }
  4753. pTC->pToken = 0;
  4754. }
  4755. return rc;
  4756. }
  4757. /*
  4758. ** This function is called from within the xFilter method. It initializes
  4759. ** the full-text query currently stored in pCsr->pExpr. To iterate through
  4760. ** the results of a query, the caller does:
  4761. **
  4762. ** fts3EvalStart(pCsr);
  4763. ** while( 1 ){
  4764. ** fts3EvalNext(pCsr);
  4765. ** if( pCsr->bEof ) break;
  4766. ** ... return row pCsr->iPrevId to the caller ...
  4767. ** }
  4768. */
  4769. static int fts3EvalStart(Fts3Cursor *pCsr){
  4770. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  4771. int rc = SQLITE_OK;
  4772. int nToken = 0;
  4773. int nOr = 0;
  4774. /* Allocate a MultiSegReader for each token in the expression. */
  4775. fts3EvalAllocateReaders(pCsr, pCsr->pExpr, &nToken, &nOr, &rc);
  4776. /* Determine which, if any, tokens in the expression should be deferred. */
  4777. #ifndef SQLITE_DISABLE_FTS4_DEFERRED
  4778. if( rc==SQLITE_OK && nToken>1 && pTab->bFts4 ){
  4779. Fts3TokenAndCost *aTC;
  4780. aTC = (Fts3TokenAndCost *)sqlite3_malloc64(
  4781. sizeof(Fts3TokenAndCost) * nToken
  4782. + sizeof(Fts3Expr *) * nOr * 2
  4783. );
  4784. if( !aTC ){
  4785. rc = SQLITE_NOMEM;
  4786. }else{
  4787. Fts3Expr **apOr = (Fts3Expr **)&aTC[nToken];
  4788. int ii;
  4789. Fts3TokenAndCost *pTC = aTC;
  4790. Fts3Expr **ppOr = apOr;
  4791. fts3EvalTokenCosts(pCsr, 0, pCsr->pExpr, &pTC, &ppOr, &rc);
  4792. nToken = (int)(pTC-aTC);
  4793. nOr = (int)(ppOr-apOr);
  4794. if( rc==SQLITE_OK ){
  4795. rc = fts3EvalSelectDeferred(pCsr, 0, aTC, nToken);
  4796. for(ii=0; rc==SQLITE_OK && ii<nOr; ii++){
  4797. rc = fts3EvalSelectDeferred(pCsr, apOr[ii], aTC, nToken);
  4798. }
  4799. }
  4800. sqlite3_free(aTC);
  4801. }
  4802. }
  4803. #endif
  4804. fts3EvalStartReaders(pCsr, pCsr->pExpr, &rc);
  4805. return rc;
  4806. }
  4807. /*
  4808. ** Invalidate the current position list for phrase pPhrase.
  4809. */
  4810. static void fts3EvalInvalidatePoslist(Fts3Phrase *pPhrase){
  4811. if( pPhrase->doclist.bFreeList ){
  4812. sqlite3_free(pPhrase->doclist.pList);
  4813. }
  4814. pPhrase->doclist.pList = 0;
  4815. pPhrase->doclist.nList = 0;
  4816. pPhrase->doclist.bFreeList = 0;
  4817. }
  4818. /*
  4819. ** This function is called to edit the position list associated with
  4820. ** the phrase object passed as the fifth argument according to a NEAR
  4821. ** condition. For example:
  4822. **
  4823. ** abc NEAR/5 "def ghi"
  4824. **
  4825. ** Parameter nNear is passed the NEAR distance of the expression (5 in
  4826. ** the example above). When this function is called, *paPoslist points to
  4827. ** the position list, and *pnToken is the number of phrase tokens in the
  4828. ** phrase on the other side of the NEAR operator to pPhrase. For example,
  4829. ** if pPhrase refers to the "def ghi" phrase, then *paPoslist points to
  4830. ** the position list associated with phrase "abc".
  4831. **
  4832. ** All positions in the pPhrase position list that are not sufficiently
  4833. ** close to a position in the *paPoslist position list are removed. If this
  4834. ** leaves 0 positions, zero is returned. Otherwise, non-zero.
  4835. **
  4836. ** Before returning, *paPoslist is set to point to the position lsit
  4837. ** associated with pPhrase. And *pnToken is set to the number of tokens in
  4838. ** pPhrase.
  4839. */
  4840. static int fts3EvalNearTrim(
  4841. int nNear, /* NEAR distance. As in "NEAR/nNear". */
  4842. char *aTmp, /* Temporary space to use */
  4843. char **paPoslist, /* IN/OUT: Position list */
  4844. int *pnToken, /* IN/OUT: Tokens in phrase of *paPoslist */
  4845. Fts3Phrase *pPhrase /* The phrase object to trim the doclist of */
  4846. ){
  4847. int nParam1 = nNear + pPhrase->nToken;
  4848. int nParam2 = nNear + *pnToken;
  4849. int nNew;
  4850. char *p2;
  4851. char *pOut;
  4852. int res;
  4853. assert( pPhrase->doclist.pList );
  4854. p2 = pOut = pPhrase->doclist.pList;
  4855. res = fts3PoslistNearMerge(
  4856. &pOut, aTmp, nParam1, nParam2, paPoslist, &p2
  4857. );
  4858. if( res ){
  4859. nNew = (int)(pOut - pPhrase->doclist.pList) - 1;
  4860. assert_fts3_nc( nNew<=pPhrase->doclist.nList && nNew>0 );
  4861. if( nNew>=0 && nNew<=pPhrase->doclist.nList ){
  4862. assert( pPhrase->doclist.pList[nNew]=='\0' );
  4863. memset(&pPhrase->doclist.pList[nNew], 0, pPhrase->doclist.nList - nNew);
  4864. pPhrase->doclist.nList = nNew;
  4865. }
  4866. *paPoslist = pPhrase->doclist.pList;
  4867. *pnToken = pPhrase->nToken;
  4868. }
  4869. return res;
  4870. }
  4871. /*
  4872. ** This function is a no-op if *pRc is other than SQLITE_OK when it is called.
  4873. ** Otherwise, it advances the expression passed as the second argument to
  4874. ** point to the next matching row in the database. Expressions iterate through
  4875. ** matching rows in docid order. Ascending order if Fts3Cursor.bDesc is zero,
  4876. ** or descending if it is non-zero.
  4877. **
  4878. ** If an error occurs, *pRc is set to an SQLite error code. Otherwise, if
  4879. ** successful, the following variables in pExpr are set:
  4880. **
  4881. ** Fts3Expr.bEof (non-zero if EOF - there is no next row)
  4882. ** Fts3Expr.iDocid (valid if bEof==0. The docid of the next row)
  4883. **
  4884. ** If the expression is of type FTSQUERY_PHRASE, and the expression is not
  4885. ** at EOF, then the following variables are populated with the position list
  4886. ** for the phrase for the visited row:
  4887. **
  4888. ** FTs3Expr.pPhrase->doclist.nList (length of pList in bytes)
  4889. ** FTs3Expr.pPhrase->doclist.pList (pointer to position list)
  4890. **
  4891. ** It says above that this function advances the expression to the next
  4892. ** matching row. This is usually true, but there are the following exceptions:
  4893. **
  4894. ** 1. Deferred tokens are not taken into account. If a phrase consists
  4895. ** entirely of deferred tokens, it is assumed to match every row in
  4896. ** the db. In this case the position-list is not populated at all.
  4897. **
  4898. ** Or, if a phrase contains one or more deferred tokens and one or
  4899. ** more non-deferred tokens, then the expression is advanced to the
  4900. ** next possible match, considering only non-deferred tokens. In other
  4901. ** words, if the phrase is "A B C", and "B" is deferred, the expression
  4902. ** is advanced to the next row that contains an instance of "A * C",
  4903. ** where "*" may match any single token. The position list in this case
  4904. ** is populated as for "A * C" before returning.
  4905. **
  4906. ** 2. NEAR is treated as AND. If the expression is "x NEAR y", it is
  4907. ** advanced to point to the next row that matches "x AND y".
  4908. **
  4909. ** See sqlite3Fts3EvalTestDeferred() for details on testing if a row is
  4910. ** really a match, taking into account deferred tokens and NEAR operators.
  4911. */
  4912. static void fts3EvalNextRow(
  4913. Fts3Cursor *pCsr, /* FTS Cursor handle */
  4914. Fts3Expr *pExpr, /* Expr. to advance to next matching row */
  4915. int *pRc /* IN/OUT: Error code */
  4916. ){
  4917. if( *pRc==SQLITE_OK && pExpr->bEof==0 ){
  4918. int bDescDoclist = pCsr->bDesc; /* Used by DOCID_CMP() macro */
  4919. pExpr->bStart = 1;
  4920. switch( pExpr->eType ){
  4921. case FTSQUERY_NEAR:
  4922. case FTSQUERY_AND: {
  4923. Fts3Expr *pLeft = pExpr->pLeft;
  4924. Fts3Expr *pRight = pExpr->pRight;
  4925. assert( !pLeft->bDeferred || !pRight->bDeferred );
  4926. if( pLeft->bDeferred ){
  4927. /* LHS is entirely deferred. So we assume it matches every row.
  4928. ** Advance the RHS iterator to find the next row visited. */
  4929. fts3EvalNextRow(pCsr, pRight, pRc);
  4930. pExpr->iDocid = pRight->iDocid;
  4931. pExpr->bEof = pRight->bEof;
  4932. }else if( pRight->bDeferred ){
  4933. /* RHS is entirely deferred. So we assume it matches every row.
  4934. ** Advance the LHS iterator to find the next row visited. */
  4935. fts3EvalNextRow(pCsr, pLeft, pRc);
  4936. pExpr->iDocid = pLeft->iDocid;
  4937. pExpr->bEof = pLeft->bEof;
  4938. }else{
  4939. /* Neither the RHS or LHS are deferred. */
  4940. fts3EvalNextRow(pCsr, pLeft, pRc);
  4941. fts3EvalNextRow(pCsr, pRight, pRc);
  4942. while( !pLeft->bEof && !pRight->bEof && *pRc==SQLITE_OK ){
  4943. sqlite3_int64 iDiff = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
  4944. if( iDiff==0 ) break;
  4945. if( iDiff<0 ){
  4946. fts3EvalNextRow(pCsr, pLeft, pRc);
  4947. }else{
  4948. fts3EvalNextRow(pCsr, pRight, pRc);
  4949. }
  4950. }
  4951. pExpr->iDocid = pLeft->iDocid;
  4952. pExpr->bEof = (pLeft->bEof || pRight->bEof);
  4953. if( pExpr->eType==FTSQUERY_NEAR && pExpr->bEof ){
  4954. assert( pRight->eType==FTSQUERY_PHRASE );
  4955. if( pRight->pPhrase->doclist.aAll ){
  4956. Fts3Doclist *pDl = &pRight->pPhrase->doclist;
  4957. while( *pRc==SQLITE_OK && pRight->bEof==0 ){
  4958. memset(pDl->pList, 0, pDl->nList);
  4959. fts3EvalNextRow(pCsr, pRight, pRc);
  4960. }
  4961. }
  4962. if( pLeft->pPhrase && pLeft->pPhrase->doclist.aAll ){
  4963. Fts3Doclist *pDl = &pLeft->pPhrase->doclist;
  4964. while( *pRc==SQLITE_OK && pLeft->bEof==0 ){
  4965. memset(pDl->pList, 0, pDl->nList);
  4966. fts3EvalNextRow(pCsr, pLeft, pRc);
  4967. }
  4968. }
  4969. pRight->bEof = pLeft->bEof = 1;
  4970. }
  4971. }
  4972. break;
  4973. }
  4974. case FTSQUERY_OR: {
  4975. Fts3Expr *pLeft = pExpr->pLeft;
  4976. Fts3Expr *pRight = pExpr->pRight;
  4977. sqlite3_int64 iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
  4978. assert_fts3_nc( pLeft->bStart || pLeft->iDocid==pRight->iDocid );
  4979. assert_fts3_nc( pRight->bStart || pLeft->iDocid==pRight->iDocid );
  4980. if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
  4981. fts3EvalNextRow(pCsr, pLeft, pRc);
  4982. }else if( pLeft->bEof || iCmp>0 ){
  4983. fts3EvalNextRow(pCsr, pRight, pRc);
  4984. }else{
  4985. fts3EvalNextRow(pCsr, pLeft, pRc);
  4986. fts3EvalNextRow(pCsr, pRight, pRc);
  4987. }
  4988. pExpr->bEof = (pLeft->bEof && pRight->bEof);
  4989. iCmp = DOCID_CMP(pLeft->iDocid, pRight->iDocid);
  4990. if( pRight->bEof || (pLeft->bEof==0 && iCmp<0) ){
  4991. pExpr->iDocid = pLeft->iDocid;
  4992. }else{
  4993. pExpr->iDocid = pRight->iDocid;
  4994. }
  4995. break;
  4996. }
  4997. case FTSQUERY_NOT: {
  4998. Fts3Expr *pLeft = pExpr->pLeft;
  4999. Fts3Expr *pRight = pExpr->pRight;
  5000. if( pRight->bStart==0 ){
  5001. fts3EvalNextRow(pCsr, pRight, pRc);
  5002. assert( *pRc!=SQLITE_OK || pRight->bStart );
  5003. }
  5004. fts3EvalNextRow(pCsr, pLeft, pRc);
  5005. if( pLeft->bEof==0 ){
  5006. while( !*pRc
  5007. && !pRight->bEof
  5008. && DOCID_CMP(pLeft->iDocid, pRight->iDocid)>0
  5009. ){
  5010. fts3EvalNextRow(pCsr, pRight, pRc);
  5011. }
  5012. }
  5013. pExpr->iDocid = pLeft->iDocid;
  5014. pExpr->bEof = pLeft->bEof;
  5015. break;
  5016. }
  5017. default: {
  5018. Fts3Phrase *pPhrase = pExpr->pPhrase;
  5019. fts3EvalInvalidatePoslist(pPhrase);
  5020. *pRc = fts3EvalPhraseNext(pCsr, pPhrase, &pExpr->bEof);
  5021. pExpr->iDocid = pPhrase->doclist.iDocid;
  5022. break;
  5023. }
  5024. }
  5025. }
  5026. }
  5027. /*
  5028. ** If *pRc is not SQLITE_OK, or if pExpr is not the root node of a NEAR
  5029. ** cluster, then this function returns 1 immediately.
  5030. **
  5031. ** Otherwise, it checks if the current row really does match the NEAR
  5032. ** expression, using the data currently stored in the position lists
  5033. ** (Fts3Expr->pPhrase.doclist.pList/nList) for each phrase in the expression.
  5034. **
  5035. ** If the current row is a match, the position list associated with each
  5036. ** phrase in the NEAR expression is edited in place to contain only those
  5037. ** phrase instances sufficiently close to their peers to satisfy all NEAR
  5038. ** constraints. In this case it returns 1. If the NEAR expression does not
  5039. ** match the current row, 0 is returned. The position lists may or may not
  5040. ** be edited if 0 is returned.
  5041. */
  5042. static int fts3EvalNearTest(Fts3Expr *pExpr, int *pRc){
  5043. int res = 1;
  5044. /* The following block runs if pExpr is the root of a NEAR query.
  5045. ** For example, the query:
  5046. **
  5047. ** "w" NEAR "x" NEAR "y" NEAR "z"
  5048. **
  5049. ** which is represented in tree form as:
  5050. **
  5051. ** |
  5052. ** +--NEAR--+ <-- root of NEAR query
  5053. ** | |
  5054. ** +--NEAR--+ "z"
  5055. ** | |
  5056. ** +--NEAR--+ "y"
  5057. ** | |
  5058. ** "w" "x"
  5059. **
  5060. ** The right-hand child of a NEAR node is always a phrase. The
  5061. ** left-hand child may be either a phrase or a NEAR node. There are
  5062. ** no exceptions to this - it's the way the parser in fts3_expr.c works.
  5063. */
  5064. if( *pRc==SQLITE_OK
  5065. && pExpr->eType==FTSQUERY_NEAR
  5066. && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
  5067. ){
  5068. Fts3Expr *p;
  5069. sqlite3_int64 nTmp = 0; /* Bytes of temp space */
  5070. char *aTmp; /* Temp space for PoslistNearMerge() */
  5071. /* Allocate temporary working space. */
  5072. for(p=pExpr; p->pLeft; p=p->pLeft){
  5073. assert( p->pRight->pPhrase->doclist.nList>0 );
  5074. nTmp += p->pRight->pPhrase->doclist.nList;
  5075. }
  5076. nTmp += p->pPhrase->doclist.nList;
  5077. aTmp = sqlite3_malloc64(nTmp*2 + FTS3_VARINT_MAX);
  5078. if( !aTmp ){
  5079. *pRc = SQLITE_NOMEM;
  5080. res = 0;
  5081. }else{
  5082. char *aPoslist = p->pPhrase->doclist.pList;
  5083. int nToken = p->pPhrase->nToken;
  5084. for(p=p->pParent;res && p && p->eType==FTSQUERY_NEAR; p=p->pParent){
  5085. Fts3Phrase *pPhrase = p->pRight->pPhrase;
  5086. int nNear = p->nNear;
  5087. res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
  5088. }
  5089. aPoslist = pExpr->pRight->pPhrase->doclist.pList;
  5090. nToken = pExpr->pRight->pPhrase->nToken;
  5091. for(p=pExpr->pLeft; p && res; p=p->pLeft){
  5092. int nNear;
  5093. Fts3Phrase *pPhrase;
  5094. assert( p->pParent && p->pParent->pLeft==p );
  5095. nNear = p->pParent->nNear;
  5096. pPhrase = (
  5097. p->eType==FTSQUERY_NEAR ? p->pRight->pPhrase : p->pPhrase
  5098. );
  5099. res = fts3EvalNearTrim(nNear, aTmp, &aPoslist, &nToken, pPhrase);
  5100. }
  5101. }
  5102. sqlite3_free(aTmp);
  5103. }
  5104. return res;
  5105. }
  5106. /*
  5107. ** This function is a helper function for sqlite3Fts3EvalTestDeferred().
  5108. ** Assuming no error occurs or has occurred, It returns non-zero if the
  5109. ** expression passed as the second argument matches the row that pCsr
  5110. ** currently points to, or zero if it does not.
  5111. **
  5112. ** If *pRc is not SQLITE_OK when this function is called, it is a no-op.
  5113. ** If an error occurs during execution of this function, *pRc is set to
  5114. ** the appropriate SQLite error code. In this case the returned value is
  5115. ** undefined.
  5116. */
  5117. static int fts3EvalTestExpr(
  5118. Fts3Cursor *pCsr, /* FTS cursor handle */
  5119. Fts3Expr *pExpr, /* Expr to test. May or may not be root. */
  5120. int *pRc /* IN/OUT: Error code */
  5121. ){
  5122. int bHit = 1; /* Return value */
  5123. if( *pRc==SQLITE_OK ){
  5124. switch( pExpr->eType ){
  5125. case FTSQUERY_NEAR:
  5126. case FTSQUERY_AND:
  5127. bHit = (
  5128. fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
  5129. && fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
  5130. && fts3EvalNearTest(pExpr, pRc)
  5131. );
  5132. /* If the NEAR expression does not match any rows, zero the doclist for
  5133. ** all phrases involved in the NEAR. This is because the snippet(),
  5134. ** offsets() and matchinfo() functions are not supposed to recognize
  5135. ** any instances of phrases that are part of unmatched NEAR queries.
  5136. ** For example if this expression:
  5137. **
  5138. ** ... MATCH 'a OR (b NEAR c)'
  5139. **
  5140. ** is matched against a row containing:
  5141. **
  5142. ** 'a b d e'
  5143. **
  5144. ** then any snippet() should ony highlight the "a" term, not the "b"
  5145. ** (as "b" is part of a non-matching NEAR clause).
  5146. */
  5147. if( bHit==0
  5148. && pExpr->eType==FTSQUERY_NEAR
  5149. && (pExpr->pParent==0 || pExpr->pParent->eType!=FTSQUERY_NEAR)
  5150. ){
  5151. Fts3Expr *p;
  5152. for(p=pExpr; p->pPhrase==0; p=p->pLeft){
  5153. if( p->pRight->iDocid==pCsr->iPrevId ){
  5154. fts3EvalInvalidatePoslist(p->pRight->pPhrase);
  5155. }
  5156. }
  5157. if( p->iDocid==pCsr->iPrevId ){
  5158. fts3EvalInvalidatePoslist(p->pPhrase);
  5159. }
  5160. }
  5161. break;
  5162. case FTSQUERY_OR: {
  5163. int bHit1 = fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc);
  5164. int bHit2 = fts3EvalTestExpr(pCsr, pExpr->pRight, pRc);
  5165. bHit = bHit1 || bHit2;
  5166. break;
  5167. }
  5168. case FTSQUERY_NOT:
  5169. bHit = (
  5170. fts3EvalTestExpr(pCsr, pExpr->pLeft, pRc)
  5171. && !fts3EvalTestExpr(pCsr, pExpr->pRight, pRc)
  5172. );
  5173. break;
  5174. default: {
  5175. #ifndef SQLITE_DISABLE_FTS4_DEFERRED
  5176. if( pCsr->pDeferred && (pExpr->bDeferred || (
  5177. pExpr->iDocid==pCsr->iPrevId && pExpr->pPhrase->doclist.pList
  5178. ))){
  5179. Fts3Phrase *pPhrase = pExpr->pPhrase;
  5180. if( pExpr->bDeferred ){
  5181. fts3EvalInvalidatePoslist(pPhrase);
  5182. }
  5183. *pRc = fts3EvalDeferredPhrase(pCsr, pPhrase);
  5184. bHit = (pPhrase->doclist.pList!=0);
  5185. pExpr->iDocid = pCsr->iPrevId;
  5186. }else
  5187. #endif
  5188. {
  5189. bHit = (
  5190. pExpr->bEof==0 && pExpr->iDocid==pCsr->iPrevId
  5191. && pExpr->pPhrase->doclist.nList>0
  5192. );
  5193. }
  5194. break;
  5195. }
  5196. }
  5197. }
  5198. return bHit;
  5199. }
  5200. /*
  5201. ** This function is called as the second part of each xNext operation when
  5202. ** iterating through the results of a full-text query. At this point the
  5203. ** cursor points to a row that matches the query expression, with the
  5204. ** following caveats:
  5205. **
  5206. ** * Up until this point, "NEAR" operators in the expression have been
  5207. ** treated as "AND".
  5208. **
  5209. ** * Deferred tokens have not yet been considered.
  5210. **
  5211. ** If *pRc is not SQLITE_OK when this function is called, it immediately
  5212. ** returns 0. Otherwise, it tests whether or not after considering NEAR
  5213. ** operators and deferred tokens the current row is still a match for the
  5214. ** expression. It returns 1 if both of the following are true:
  5215. **
  5216. ** 1. *pRc is SQLITE_OK when this function returns, and
  5217. **
  5218. ** 2. After scanning the current FTS table row for the deferred tokens,
  5219. ** it is determined that the row does *not* match the query.
  5220. **
  5221. ** Or, if no error occurs and it seems the current row does match the FTS
  5222. ** query, return 0.
  5223. */
  5224. int sqlite3Fts3EvalTestDeferred(Fts3Cursor *pCsr, int *pRc){
  5225. int rc = *pRc;
  5226. int bMiss = 0;
  5227. if( rc==SQLITE_OK ){
  5228. /* If there are one or more deferred tokens, load the current row into
  5229. ** memory and scan it to determine the position list for each deferred
  5230. ** token. Then, see if this row is really a match, considering deferred
  5231. ** tokens and NEAR operators (neither of which were taken into account
  5232. ** earlier, by fts3EvalNextRow()).
  5233. */
  5234. if( pCsr->pDeferred ){
  5235. rc = fts3CursorSeek(0, pCsr);
  5236. if( rc==SQLITE_OK ){
  5237. rc = sqlite3Fts3CacheDeferredDoclists(pCsr);
  5238. }
  5239. }
  5240. bMiss = (0==fts3EvalTestExpr(pCsr, pCsr->pExpr, &rc));
  5241. /* Free the position-lists accumulated for each deferred token above. */
  5242. sqlite3Fts3FreeDeferredDoclists(pCsr);
  5243. *pRc = rc;
  5244. }
  5245. return (rc==SQLITE_OK && bMiss);
  5246. }
  5247. /*
  5248. ** Advance to the next document that matches the FTS expression in
  5249. ** Fts3Cursor.pExpr.
  5250. */
  5251. static int fts3EvalNext(Fts3Cursor *pCsr){
  5252. int rc = SQLITE_OK; /* Return Code */
  5253. Fts3Expr *pExpr = pCsr->pExpr;
  5254. assert( pCsr->isEof==0 );
  5255. if( pExpr==0 ){
  5256. pCsr->isEof = 1;
  5257. }else{
  5258. do {
  5259. if( pCsr->isRequireSeek==0 ){
  5260. sqlite3_reset(pCsr->pStmt);
  5261. }
  5262. assert( sqlite3_data_count(pCsr->pStmt)==0 );
  5263. fts3EvalNextRow(pCsr, pExpr, &rc);
  5264. pCsr->isEof = pExpr->bEof;
  5265. pCsr->isRequireSeek = 1;
  5266. pCsr->isMatchinfoNeeded = 1;
  5267. pCsr->iPrevId = pExpr->iDocid;
  5268. }while( pCsr->isEof==0 && sqlite3Fts3EvalTestDeferred(pCsr, &rc) );
  5269. }
  5270. /* Check if the cursor is past the end of the docid range specified
  5271. ** by Fts3Cursor.iMinDocid/iMaxDocid. If so, set the EOF flag. */
  5272. if( rc==SQLITE_OK && (
  5273. (pCsr->bDesc==0 && pCsr->iPrevId>pCsr->iMaxDocid)
  5274. || (pCsr->bDesc!=0 && pCsr->iPrevId<pCsr->iMinDocid)
  5275. )){
  5276. pCsr->isEof = 1;
  5277. }
  5278. return rc;
  5279. }
  5280. /*
  5281. ** Restart interation for expression pExpr so that the next call to
  5282. ** fts3EvalNext() visits the first row. Do not allow incremental
  5283. ** loading or merging of phrase doclists for this iteration.
  5284. **
  5285. ** If *pRc is other than SQLITE_OK when this function is called, it is
  5286. ** a no-op. If an error occurs within this function, *pRc is set to an
  5287. ** SQLite error code before returning.
  5288. */
  5289. static void fts3EvalRestart(
  5290. Fts3Cursor *pCsr,
  5291. Fts3Expr *pExpr,
  5292. int *pRc
  5293. ){
  5294. if( pExpr && *pRc==SQLITE_OK ){
  5295. Fts3Phrase *pPhrase = pExpr->pPhrase;
  5296. if( pPhrase ){
  5297. fts3EvalInvalidatePoslist(pPhrase);
  5298. if( pPhrase->bIncr ){
  5299. int i;
  5300. for(i=0; i<pPhrase->nToken; i++){
  5301. Fts3PhraseToken *pToken = &pPhrase->aToken[i];
  5302. assert( pToken->pDeferred==0 );
  5303. if( pToken->pSegcsr ){
  5304. sqlite3Fts3MsrIncrRestart(pToken->pSegcsr);
  5305. }
  5306. }
  5307. *pRc = fts3EvalPhraseStart(pCsr, 0, pPhrase);
  5308. }
  5309. pPhrase->doclist.pNextDocid = 0;
  5310. pPhrase->doclist.iDocid = 0;
  5311. pPhrase->pOrPoslist = 0;
  5312. }
  5313. pExpr->iDocid = 0;
  5314. pExpr->bEof = 0;
  5315. pExpr->bStart = 0;
  5316. fts3EvalRestart(pCsr, pExpr->pLeft, pRc);
  5317. fts3EvalRestart(pCsr, pExpr->pRight, pRc);
  5318. }
  5319. }
  5320. /*
  5321. ** After allocating the Fts3Expr.aMI[] array for each phrase in the
  5322. ** expression rooted at pExpr, the cursor iterates through all rows matched
  5323. ** by pExpr, calling this function for each row. This function increments
  5324. ** the values in Fts3Expr.aMI[] according to the position-list currently
  5325. ** found in Fts3Expr.pPhrase->doclist.pList for each of the phrase
  5326. ** expression nodes.
  5327. */
  5328. static void fts3EvalUpdateCounts(Fts3Expr *pExpr, int nCol){
  5329. if( pExpr ){
  5330. Fts3Phrase *pPhrase = pExpr->pPhrase;
  5331. if( pPhrase && pPhrase->doclist.pList ){
  5332. int iCol = 0;
  5333. char *p = pPhrase->doclist.pList;
  5334. do{
  5335. u8 c = 0;
  5336. int iCnt = 0;
  5337. while( 0xFE & (*p | c) ){
  5338. if( (c&0x80)==0 ) iCnt++;
  5339. c = *p++ & 0x80;
  5340. }
  5341. /* aMI[iCol*3 + 1] = Number of occurrences
  5342. ** aMI[iCol*3 + 2] = Number of rows containing at least one instance
  5343. */
  5344. pExpr->aMI[iCol*3 + 1] += iCnt;
  5345. pExpr->aMI[iCol*3 + 2] += (iCnt>0);
  5346. if( *p==0x00 ) break;
  5347. p++;
  5348. p += fts3GetVarint32(p, &iCol);
  5349. }while( iCol<nCol );
  5350. }
  5351. fts3EvalUpdateCounts(pExpr->pLeft, nCol);
  5352. fts3EvalUpdateCounts(pExpr->pRight, nCol);
  5353. }
  5354. }
  5355. /*
  5356. ** This is an sqlite3Fts3ExprIterate() callback. If the Fts3Expr.aMI[] array
  5357. ** has not yet been allocated, allocate and zero it. Otherwise, just zero
  5358. ** it.
  5359. */
  5360. static int fts3AllocateMSI(Fts3Expr *pExpr, int iPhrase, void *pCtx){
  5361. Fts3Table *pTab = (Fts3Table*)pCtx;
  5362. UNUSED_PARAMETER(iPhrase);
  5363. if( pExpr->aMI==0 ){
  5364. pExpr->aMI = (u32 *)sqlite3_malloc64(pTab->nColumn * 3 * sizeof(u32));
  5365. if( pExpr->aMI==0 ) return SQLITE_NOMEM;
  5366. }
  5367. memset(pExpr->aMI, 0, pTab->nColumn * 3 * sizeof(u32));
  5368. return SQLITE_OK;
  5369. }
  5370. /*
  5371. ** Expression pExpr must be of type FTSQUERY_PHRASE.
  5372. **
  5373. ** If it is not already allocated and populated, this function allocates and
  5374. ** populates the Fts3Expr.aMI[] array for expression pExpr. If pExpr is part
  5375. ** of a NEAR expression, then it also allocates and populates the same array
  5376. ** for all other phrases that are part of the NEAR expression.
  5377. **
  5378. ** SQLITE_OK is returned if the aMI[] array is successfully allocated and
  5379. ** populated. Otherwise, if an error occurs, an SQLite error code is returned.
  5380. */
  5381. static int fts3EvalGatherStats(
  5382. Fts3Cursor *pCsr, /* Cursor object */
  5383. Fts3Expr *pExpr /* FTSQUERY_PHRASE expression */
  5384. ){
  5385. int rc = SQLITE_OK; /* Return code */
  5386. assert( pExpr->eType==FTSQUERY_PHRASE );
  5387. if( pExpr->aMI==0 ){
  5388. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  5389. Fts3Expr *pRoot; /* Root of NEAR expression */
  5390. sqlite3_int64 iPrevId = pCsr->iPrevId;
  5391. sqlite3_int64 iDocid;
  5392. u8 bEof;
  5393. /* Find the root of the NEAR expression */
  5394. pRoot = pExpr;
  5395. while( pRoot->pParent
  5396. && (pRoot->pParent->eType==FTSQUERY_NEAR || pRoot->bDeferred)
  5397. ){
  5398. pRoot = pRoot->pParent;
  5399. }
  5400. iDocid = pRoot->iDocid;
  5401. bEof = pRoot->bEof;
  5402. assert( pRoot->bStart );
  5403. /* Allocate space for the aMSI[] array of each FTSQUERY_PHRASE node */
  5404. rc = sqlite3Fts3ExprIterate(pRoot, fts3AllocateMSI, (void*)pTab);
  5405. if( rc!=SQLITE_OK ) return rc;
  5406. fts3EvalRestart(pCsr, pRoot, &rc);
  5407. while( pCsr->isEof==0 && rc==SQLITE_OK ){
  5408. do {
  5409. /* Ensure the %_content statement is reset. */
  5410. if( pCsr->isRequireSeek==0 ) sqlite3_reset(pCsr->pStmt);
  5411. assert( sqlite3_data_count(pCsr->pStmt)==0 );
  5412. /* Advance to the next document */
  5413. fts3EvalNextRow(pCsr, pRoot, &rc);
  5414. pCsr->isEof = pRoot->bEof;
  5415. pCsr->isRequireSeek = 1;
  5416. pCsr->isMatchinfoNeeded = 1;
  5417. pCsr->iPrevId = pRoot->iDocid;
  5418. }while( pCsr->isEof==0
  5419. && pRoot->eType==FTSQUERY_NEAR
  5420. && sqlite3Fts3EvalTestDeferred(pCsr, &rc)
  5421. );
  5422. if( rc==SQLITE_OK && pCsr->isEof==0 ){
  5423. fts3EvalUpdateCounts(pRoot, pTab->nColumn);
  5424. }
  5425. }
  5426. pCsr->isEof = 0;
  5427. pCsr->iPrevId = iPrevId;
  5428. if( bEof ){
  5429. pRoot->bEof = bEof;
  5430. }else{
  5431. /* Caution: pRoot may iterate through docids in ascending or descending
  5432. ** order. For this reason, even though it seems more defensive, the
  5433. ** do loop can not be written:
  5434. **
  5435. ** do {...} while( pRoot->iDocid<iDocid && rc==SQLITE_OK );
  5436. */
  5437. fts3EvalRestart(pCsr, pRoot, &rc);
  5438. do {
  5439. fts3EvalNextRow(pCsr, pRoot, &rc);
  5440. assert_fts3_nc( pRoot->bEof==0 );
  5441. if( pRoot->bEof ) rc = FTS_CORRUPT_VTAB;
  5442. }while( pRoot->iDocid!=iDocid && rc==SQLITE_OK );
  5443. }
  5444. }
  5445. return rc;
  5446. }
  5447. /*
  5448. ** This function is used by the matchinfo() module to query a phrase
  5449. ** expression node for the following information:
  5450. **
  5451. ** 1. The total number of occurrences of the phrase in each column of
  5452. ** the FTS table (considering all rows), and
  5453. **
  5454. ** 2. For each column, the number of rows in the table for which the
  5455. ** column contains at least one instance of the phrase.
  5456. **
  5457. ** If no error occurs, SQLITE_OK is returned and the values for each column
  5458. ** written into the array aiOut as follows:
  5459. **
  5460. ** aiOut[iCol*3 + 1] = Number of occurrences
  5461. ** aiOut[iCol*3 + 2] = Number of rows containing at least one instance
  5462. **
  5463. ** Caveats:
  5464. **
  5465. ** * If a phrase consists entirely of deferred tokens, then all output
  5466. ** values are set to the number of documents in the table. In other
  5467. ** words we assume that very common tokens occur exactly once in each
  5468. ** column of each row of the table.
  5469. **
  5470. ** * If a phrase contains some deferred tokens (and some non-deferred
  5471. ** tokens), count the potential occurrence identified by considering
  5472. ** the non-deferred tokens instead of actual phrase occurrences.
  5473. **
  5474. ** * If the phrase is part of a NEAR expression, then only phrase instances
  5475. ** that meet the NEAR constraint are included in the counts.
  5476. */
  5477. int sqlite3Fts3EvalPhraseStats(
  5478. Fts3Cursor *pCsr, /* FTS cursor handle */
  5479. Fts3Expr *pExpr, /* Phrase expression */
  5480. u32 *aiOut /* Array to write results into (see above) */
  5481. ){
  5482. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  5483. int rc = SQLITE_OK;
  5484. int iCol;
  5485. if( pExpr->bDeferred && pExpr->pParent->eType!=FTSQUERY_NEAR ){
  5486. assert( pCsr->nDoc>0 );
  5487. for(iCol=0; iCol<pTab->nColumn; iCol++){
  5488. aiOut[iCol*3 + 1] = (u32)pCsr->nDoc;
  5489. aiOut[iCol*3 + 2] = (u32)pCsr->nDoc;
  5490. }
  5491. }else{
  5492. rc = fts3EvalGatherStats(pCsr, pExpr);
  5493. if( rc==SQLITE_OK ){
  5494. assert( pExpr->aMI );
  5495. for(iCol=0; iCol<pTab->nColumn; iCol++){
  5496. aiOut[iCol*3 + 1] = pExpr->aMI[iCol*3 + 1];
  5497. aiOut[iCol*3 + 2] = pExpr->aMI[iCol*3 + 2];
  5498. }
  5499. }
  5500. }
  5501. return rc;
  5502. }
  5503. /*
  5504. ** The expression pExpr passed as the second argument to this function
  5505. ** must be of type FTSQUERY_PHRASE.
  5506. **
  5507. ** The returned value is either NULL or a pointer to a buffer containing
  5508. ** a position-list indicating the occurrences of the phrase in column iCol
  5509. ** of the current row.
  5510. **
  5511. ** More specifically, the returned buffer contains 1 varint for each
  5512. ** occurrence of the phrase in the column, stored using the normal (delta+2)
  5513. ** compression and is terminated by either an 0x01 or 0x00 byte. For example,
  5514. ** if the requested column contains "a b X c d X X" and the position-list
  5515. ** for 'X' is requested, the buffer returned may contain:
  5516. **
  5517. ** 0x04 0x05 0x03 0x01 or 0x04 0x05 0x03 0x00
  5518. **
  5519. ** This function works regardless of whether or not the phrase is deferred,
  5520. ** incremental, or neither.
  5521. */
  5522. int sqlite3Fts3EvalPhrasePoslist(
  5523. Fts3Cursor *pCsr, /* FTS3 cursor object */
  5524. Fts3Expr *pExpr, /* Phrase to return doclist for */
  5525. int iCol, /* Column to return position list for */
  5526. char **ppOut /* OUT: Pointer to position list */
  5527. ){
  5528. Fts3Phrase *pPhrase = pExpr->pPhrase;
  5529. Fts3Table *pTab = (Fts3Table *)pCsr->base.pVtab;
  5530. char *pIter;
  5531. int iThis;
  5532. sqlite3_int64 iDocid;
  5533. /* If this phrase is applies specifically to some column other than
  5534. ** column iCol, return a NULL pointer. */
  5535. *ppOut = 0;
  5536. assert( iCol>=0 && iCol<pTab->nColumn );
  5537. if( (pPhrase->iColumn<pTab->nColumn && pPhrase->iColumn!=iCol) ){
  5538. return SQLITE_OK;
  5539. }
  5540. iDocid = pExpr->iDocid;
  5541. pIter = pPhrase->doclist.pList;
  5542. if( iDocid!=pCsr->iPrevId || pExpr->bEof ){
  5543. int rc = SQLITE_OK;
  5544. int bDescDoclist = pTab->bDescIdx; /* For DOCID_CMP macro */
  5545. int bOr = 0;
  5546. u8 bTreeEof = 0;
  5547. Fts3Expr *p; /* Used to iterate from pExpr to root */
  5548. Fts3Expr *pNear; /* Most senior NEAR ancestor (or pExpr) */
  5549. Fts3Expr *pRun; /* Closest non-deferred ancestor of pNear */
  5550. int bMatch;
  5551. /* Check if this phrase descends from an OR expression node. If not,
  5552. ** return NULL. Otherwise, the entry that corresponds to docid
  5553. ** pCsr->iPrevId may lie earlier in the doclist buffer. Or, if the
  5554. ** tree that the node is part of has been marked as EOF, but the node
  5555. ** itself is not EOF, then it may point to an earlier entry. */
  5556. pNear = pExpr;
  5557. for(p=pExpr->pParent; p; p=p->pParent){
  5558. if( p->eType==FTSQUERY_OR ) bOr = 1;
  5559. if( p->eType==FTSQUERY_NEAR ) pNear = p;
  5560. if( p->bEof ) bTreeEof = 1;
  5561. }
  5562. if( bOr==0 ) return SQLITE_OK;
  5563. pRun = pNear;
  5564. while( pRun->bDeferred ){
  5565. assert( pRun->pParent );
  5566. pRun = pRun->pParent;
  5567. }
  5568. /* This is the descendent of an OR node. In this case we cannot use
  5569. ** an incremental phrase. Load the entire doclist for the phrase
  5570. ** into memory in this case. */
  5571. if( pPhrase->bIncr ){
  5572. int bEofSave = pRun->bEof;
  5573. fts3EvalRestart(pCsr, pRun, &rc);
  5574. while( rc==SQLITE_OK && !pRun->bEof ){
  5575. fts3EvalNextRow(pCsr, pRun, &rc);
  5576. if( bEofSave==0 && pRun->iDocid==iDocid ) break;
  5577. }
  5578. assert( rc!=SQLITE_OK || pPhrase->bIncr==0 );
  5579. if( rc==SQLITE_OK && pRun->bEof!=bEofSave ){
  5580. rc = FTS_CORRUPT_VTAB;
  5581. }
  5582. }
  5583. if( bTreeEof ){
  5584. while( rc==SQLITE_OK && !pRun->bEof ){
  5585. fts3EvalNextRow(pCsr, pRun, &rc);
  5586. }
  5587. }
  5588. if( rc!=SQLITE_OK ) return rc;
  5589. bMatch = 1;
  5590. for(p=pNear; p; p=p->pLeft){
  5591. u8 bEof = 0;
  5592. Fts3Expr *pTest = p;
  5593. Fts3Phrase *pPh;
  5594. assert( pTest->eType==FTSQUERY_NEAR || pTest->eType==FTSQUERY_PHRASE );
  5595. if( pTest->eType==FTSQUERY_NEAR ) pTest = pTest->pRight;
  5596. assert( pTest->eType==FTSQUERY_PHRASE );
  5597. pPh = pTest->pPhrase;
  5598. pIter = pPh->pOrPoslist;
  5599. iDocid = pPh->iOrDocid;
  5600. if( pCsr->bDesc==bDescDoclist ){
  5601. bEof = !pPh->doclist.nAll ||
  5602. (pIter >= (pPh->doclist.aAll + pPh->doclist.nAll));
  5603. while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)<0 ) && bEof==0 ){
  5604. sqlite3Fts3DoclistNext(
  5605. bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
  5606. &pIter, &iDocid, &bEof
  5607. );
  5608. }
  5609. }else{
  5610. bEof = !pPh->doclist.nAll || (pIter && pIter<=pPh->doclist.aAll);
  5611. while( (pIter==0 || DOCID_CMP(iDocid, pCsr->iPrevId)>0 ) && bEof==0 ){
  5612. int dummy;
  5613. sqlite3Fts3DoclistPrev(
  5614. bDescDoclist, pPh->doclist.aAll, pPh->doclist.nAll,
  5615. &pIter, &iDocid, &dummy, &bEof
  5616. );
  5617. }
  5618. }
  5619. pPh->pOrPoslist = pIter;
  5620. pPh->iOrDocid = iDocid;
  5621. if( bEof || iDocid!=pCsr->iPrevId ) bMatch = 0;
  5622. }
  5623. if( bMatch ){
  5624. pIter = pPhrase->pOrPoslist;
  5625. }else{
  5626. pIter = 0;
  5627. }
  5628. }
  5629. if( pIter==0 ) return SQLITE_OK;
  5630. if( *pIter==0x01 ){
  5631. pIter++;
  5632. pIter += fts3GetVarint32(pIter, &iThis);
  5633. }else{
  5634. iThis = 0;
  5635. }
  5636. while( iThis<iCol ){
  5637. fts3ColumnlistCopy(0, &pIter);
  5638. if( *pIter==0x00 ) return SQLITE_OK;
  5639. pIter++;
  5640. pIter += fts3GetVarint32(pIter, &iThis);
  5641. }
  5642. if( *pIter==0x00 ){
  5643. pIter = 0;
  5644. }
  5645. *ppOut = ((iCol==iThis)?pIter:0);
  5646. return SQLITE_OK;
  5647. }
  5648. /*
  5649. ** Free all components of the Fts3Phrase structure that were allocated by
  5650. ** the eval module. Specifically, this means to free:
  5651. **
  5652. ** * the contents of pPhrase->doclist, and
  5653. ** * any Fts3MultiSegReader objects held by phrase tokens.
  5654. */
  5655. void sqlite3Fts3EvalPhraseCleanup(Fts3Phrase *pPhrase){
  5656. if( pPhrase ){
  5657. int i;
  5658. sqlite3_free(pPhrase->doclist.aAll);
  5659. fts3EvalInvalidatePoslist(pPhrase);
  5660. memset(&pPhrase->doclist, 0, sizeof(Fts3Doclist));
  5661. for(i=0; i<pPhrase->nToken; i++){
  5662. fts3SegReaderCursorFree(pPhrase->aToken[i].pSegcsr);
  5663. pPhrase->aToken[i].pSegcsr = 0;
  5664. }
  5665. }
  5666. }
  5667. /*
  5668. ** Return SQLITE_CORRUPT_VTAB.
  5669. */
  5670. #ifdef SQLITE_DEBUG
  5671. int sqlite3Fts3Corrupt(){
  5672. return SQLITE_CORRUPT_VTAB;
  5673. }
  5674. #endif
  5675. #if !defined(SQLITE_CORE)
  5676. /*
  5677. ** Initialize API pointer table, if required.
  5678. */
  5679. #ifdef _WIN32
  5680. __declspec(dllexport)
  5681. #endif
  5682. int sqlite3_fts3_init(
  5683. sqlite3 *db,
  5684. char **pzErrMsg,
  5685. const sqlite3_api_routines *pApi
  5686. ){
  5687. SQLITE_EXTENSION_INIT2(pApi)
  5688. return sqlite3Fts3Init(db);
  5689. }
  5690. #endif
  5691. #endif