fts3.c 190 KB

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