]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Refactor PLWriter in preparation for buffered-document change.
authorshess <shess@noemail.net>
Tue, 20 Mar 2007 23:52:37 +0000 (23:52 +0000)
committershess <shess@noemail.net>
Tue, 20 Mar 2007 23:52:37 +0000 (23:52 +0000)
Currently, PLWriter (Position List Writer) creates a locally-owned
DataBuffer to write into.  This is necessary to support doclist
collection during tokenization, where there is no obvious buffer to
write output to, but is not necessary for the other users of PLWriter.
 This change adds a DLCollector (Doc List Collector) structure to
handle the tokenization case.

Also fix a potential memory leak in writeZeroSegment().  In case of
error from leafWriterStep(), the DataBuffer dl was being leaked. (CVS 3706)

FossilOrigin-Name: 1b9918e20767aebc9c1e7523027139e5fbc12688

ext/fts2/fts2.c
manifest
manifest.uuid

index 606e2f4c45459d5cc0faa25fed1187b60498a6d9..aedae5186b3d0d8352c371fd56c75d262212de00 100644 (file)
@@ -942,18 +942,44 @@ static void plwInit(PLWriter *pWriter, sqlite_int64 iDocid, DocListType iType){
   dataBufferInit(&pWriter->b, 0);
   plwReset(pWriter, iDocid, iType);
 }
-static PLWriter *plwNew(sqlite_int64 iDocid, DocListType iType){
-  PLWriter *pWriter = malloc(sizeof(PLWriter));
-  plwInit(pWriter, iDocid, iType);
-  return pWriter;
-}
 static void plwDestroy(PLWriter *pWriter){
   dataBufferDestroy(&pWriter->b);
   SCRAMBLE(pWriter);
 }
-static void plwDelete(PLWriter *pWriter){
-  plwDestroy(pWriter);
-  free(pWriter);
+
+/*******************************************************************/
+/* DLCollector wraps PLWriter and DLWriter to provide a
+** dynamically-allocated doclist area to use during tokenization.
+**
+** dlcNew - malloc up and initialize a collector.
+** dlcDelete - destroy a collector and all contained items.
+** dlcAddPos - append position and offset information.
+** dlcAddDoclist - add the collected doclist to the given buffer.
+*/
+typedef struct DLCollector {
+  PLWriter plw;
+} DLCollector;
+
+static void dlcAddDoclist(DLCollector *pCollector, DataBuffer *b){
+  DLWriter dlw;
+  dlwInit(&dlw, pCollector->plw.iType, b);
+  plwDlwAdd(&pCollector->plw, &dlw);
+  dlwDestroy(&dlw);
+}
+static void dlcAddPos(DLCollector *pCollector, int iColumn, int iPos,
+                      int iStartOffset, int iEndOffset){
+  plwAdd(&pCollector->plw, iColumn, iPos, iStartOffset, iEndOffset);
+}
+
+static DLCollector *dlcNew(sqlite_int64 iDocid, DocListType iType){
+  DLCollector *pCollector = malloc(sizeof(DLCollector));
+  plwInit(&pCollector->plw, iDocid, iType);
+  return pCollector;
+}
+static void dlcDelete(DLCollector *pCollector){
+  plwDestroy(&pCollector->plw);
+  SCRAMBLE(pCollector);
+  free(pCollector);
 }
 
 
@@ -3533,7 +3559,7 @@ static int buildTerms(fulltext_vtab *v, fts2Hash *terms, sqlite_int64 iDocid,
                                                &pToken, &nTokenBytes,
                                                &iStartOffset, &iEndOffset,
                                                &iPosition) ){
-    PLWriter *p;
+    DLCollector *p;
 
     /* Positions can't be negative; we use -1 as a terminator internally. */
     if( iPosition<0 ){
@@ -3543,11 +3569,11 @@ static int buildTerms(fulltext_vtab *v, fts2Hash *terms, sqlite_int64 iDocid,
 
     p = fts2HashFind(terms, pToken, nTokenBytes);
     if( p==NULL ){
-      p = plwNew(iDocid, DL_DEFAULT);
+      p = dlcNew(iDocid, DL_DEFAULT);
       fts2HashInsert(terms, pToken, nTokenBytes, p);
     }
     if( iColumn>=0 ){
-      plwAdd(p, iColumn, iPosition, iStartOffset, iEndOffset);
+      dlcAddPos(p, iColumn, iPosition, iStartOffset, iEndOffset);
     }
   }
 
@@ -5045,7 +5071,7 @@ static int termSelect(fulltext_vtab *v, int iColumn,
 typedef struct TermData {
   const char *pTerm;
   int nTerm;
-  PLWriter *pWriter;
+  DLCollector *pCollector;
 } TermData;
 
 /* Orders TermData elements in strcmp fashion ( <0 for less-than, 0
@@ -5081,7 +5107,7 @@ static int writeZeroSegment(fulltext_vtab *v, fts2Hash *pTerms){
     assert( i<n );
     pData[i].pTerm = fts2HashKey(e);
     pData[i].nTerm = fts2HashKeysize(e);
-    pData[i].pWriter = fts2HashData(e);
+    pData[i].pCollector = fts2HashData(e);
   }
   assert( i==n );
 
@@ -5096,19 +5122,16 @@ static int writeZeroSegment(fulltext_vtab *v, fts2Hash *pTerms){
   leafWriterInit(0, idx, &writer);
   dataBufferInit(&dl, 0);
   for(i=0; i<n; i++){
-    DLWriter dlw;
     dataBufferReset(&dl);
-    dlwInit(&dlw, DL_DEFAULT, &dl);
-    plwDlwAdd(pData[i].pWriter, &dlw);
+    dlcAddDoclist(pData[i].pCollector, &dl);
     rc = leafWriterStep(v, &writer,
                         pData[i].pTerm, pData[i].nTerm, dl.pData, dl.nData);
-    dlwDestroy(&dlw);
     if( rc!=SQLITE_OK ) goto err;
   }
-  dataBufferDestroy(&dl);
   rc = leafWriterFinalize(v, &writer);
 
  err:
+  dataBufferDestroy(&dl);
   free(pData);
   leafWriterDestroy(&writer);
   return rc;
@@ -5158,7 +5181,7 @@ static int fulltextUpdate(sqlite3_vtab *pVtab, int nArg, sqlite3_value **ppArg,
 
   /* clean up */
   for(e=fts2HashFirst(&terms); e; e=fts2HashNext(e)){
-    plwDelete(fts2HashData(e));
+    dlcDelete(fts2HashData(e));
   }
   fts2HashClear(&terms);
 
index 274877ef69cf5481503d67d7f862825ecb3a0246..16907b08a421036948acd5e3d3e059e463e97b8a 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Modify\sthe\sinterface\sto\sthe\spager\ssub-system\sin\spreparation\sfor\sperforming\sIO\sin\sblocks\sbased\son\ssector-size,\snot\sdatabase\spage-size.\s(CVS\s3705)
-D 2007-03-19T17:44:27
+C Refactor\sPLWriter\sin\spreparation\sfor\sbuffered-document\schange.\nCurrently,\sPLWriter\s(Position\sList\sWriter)\screates\sa\slocally-owned\nDataBuffer\sto\swrite\sinto.\s\sThis\sis\snecessary\sto\ssupport\sdoclist\ncollection\sduring\stokenization,\swhere\sthere\sis\sno\sobvious\sbuffer\sto\nwrite\soutput\sto,\sbut\sis\snot\snecessary\sfor\sthe\sother\susers\sof\sPLWriter.\n\sThis\schange\sadds\sa\sDLCollector\s(Doc\sList\sCollector)\sstructure\sto\nhandle\sthe\stokenization\scase.\n\nAlso\sfix\sa\spotential\smemory\sleak\sin\swriteZeroSegment().\s\sIn\scase\sof\nerror\sfrom\sleafWriterStep(),\sthe\sDataBuffer\sdl\swas\sbeing\sleaked.\s(CVS\s3706)
+D 2007-03-20T23:52:38
 F Makefile.in 1fe3d0b46e40fd684e1e61f8e8056cefed16de9f
 F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935
 F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@@ -34,7 +34,7 @@ F ext/fts1/fulltext.h 08525a47852d1d62a0be81d3fc3fe2d23b094efd
 F ext/fts1/simple_tokenizer.c 1844d72f7194c3fd3d7e4173053911bf0661b70d
 F ext/fts1/tokenizer.h 0c53421b832366d20d720d21ea3e1f6e66a36ef9
 F ext/fts2/README.txt 8c18f41574404623b76917b9da66fcb0ab38328d
-F ext/fts2/fts2.c a49ed7292cbacbfcde6fdee1be4f6529277be3fa
+F ext/fts2/fts2.c aba63e7f4892a2e7cf50054181cda3d246c3ba0a
 F ext/fts2/fts2.h bbdab26d34f91974d5b9ade8b7836c140a7c4ce1
 F ext/fts2/fts2_hash.c b3f22116d4ef0bc8f2da6e3fdc435c86d0951a9b
 F ext/fts2/fts2_hash.h e283308156018329f042816eb09334df714e105e
@@ -437,7 +437,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
 F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
 F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
 F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
-P c3c5f658cf9c69b6e81e63fbde5dd0a854be9fc7
-R 8bc9543d27c6957ab236f6b14f2fea11
-U danielk1977
-Z 733a417800b51f433ae2631c0b87c550
+P 7dc7658887046f066b564a5994578074a99756ba
+R 28415623e14534daa33e7418f28a0adb
+U shess
+Z d8903aa3843e1c017cd54e70c455deff
index d479982a5899e2a91b6dd48b6f9583f2a7288f79..8516d5c30f23ec41c1e2f1ead4dde22db7fc8227 100644 (file)
@@ -1 +1 @@
-7dc7658887046f066b564a5994578074a99756ba
\ No newline at end of file
+1b9918e20767aebc9c1e7523027139e5fbc12688
\ No newline at end of file