]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Automatically resize the hash table used by fts5.
authordan <dan@noemail.net>
Tue, 12 Aug 2014 08:36:00 +0000 (08:36 +0000)
committerdan <dan@noemail.net>
Tue, 12 Aug 2014 08:36:00 +0000 (08:36 +0000)
FossilOrigin-Name: f1cb48f412a5f200f1fe04f91072864f379db08f

ext/fts5/fts5_hash.c
ext/fts5/fts5_index.c
manifest
manifest.uuid

index 3b50f3ac787ae054f199045b580837e808de11c0..97dd0dc0ec312c29844e35027c6c065a3d86519e 100644 (file)
@@ -116,15 +116,46 @@ void sqlite3Fts5HashClear(Fts5Hash *pHash){
       pHash->aSlot[i] = 0;
     }
   }
+  pHash->nEntry = 0;
 }
 
-static unsigned int fts5HashKey(Fts5Hash *pHash, const char *p, int n){
+static unsigned int fts5HashKey(int nSlot, const char *p, int n){
   int i;
   unsigned int h = 13;
   for(i=n-1; i>=0; i--){
     h = (h << 3) ^ h ^ p[i];
   }
-  return (h % pHash->nSlot);
+  return (h % nSlot);
+}
+
+/*
+** Resize the hash table by doubling the number of slots.
+*/
+static int fts5HashResize(Fts5Hash *pHash){
+  int nNew = pHash->nSlot*2;
+  int i;
+  Fts5HashEntry **apNew;
+  Fts5HashEntry **apOld = pHash->aSlot;
+
+  apNew = (Fts5HashEntry**)sqlite3_malloc(nNew*sizeof(Fts5HashEntry*));
+  if( !apNew ) return SQLITE_NOMEM;
+  memset(apNew, 0, nNew*sizeof(Fts5HashEntry*));
+
+  for(i=0; i<pHash->nSlot; i++){
+    while( apOld[i] ){
+      int iHash;
+      Fts5HashEntry *p = apOld[i];
+      apOld[i] = p->pNext;
+      iHash = fts5HashKey(nNew, p->zKey, strlen(p->zKey));
+      p->pNext = apNew[iHash];
+      apNew[iHash] = p;
+    }
+  }
+
+  sqlite3_free(apOld);
+  pHash->nSlot = nNew;
+  pHash->aSlot = apNew;
+  return SQLITE_OK;
 }
 
 /*
@@ -153,7 +184,7 @@ int sqlite3Fts5HashWrite(
   int iPos,                       /* Position of token within column */
   const char *pToken, int nToken  /* Token to add or remove to or from index */
 ){
-  unsigned int iHash = fts5HashKey(pHash, pToken, nToken);
+  unsigned int iHash = fts5HashKey(pHash->nSlot, pToken, nToken);
   Fts5HashEntry *p;
   u8 *pPtr;
   int nIncr = 0;                  /* Amount to increment (*pHash->pnByte) by */
@@ -168,6 +199,12 @@ int sqlite3Fts5HashWrite(
     int nByte = sizeof(Fts5HashEntry) + nToken + 1 + 64;
     if( nByte<128 ) nByte = 128;
 
+    if( (pHash->nEntry*2)>=pHash->nSlot ){
+      int rc = fts5HashResize(pHash);
+      if( rc!=SQLITE_OK ) return rc;
+      iHash = fts5HashKey(pHash->nSlot, pToken, nToken);
+    }
+
     p = (Fts5HashEntry*)sqlite3_malloc(nByte);
     if( !p ) return SQLITE_NOMEM;
     memset(p, 0, sizeof(Fts5HashEntry));
@@ -179,6 +216,7 @@ int sqlite3Fts5HashWrite(
     p->iRowid = iRowid;
     p->pNext = pHash->aSlot[iHash];
     pHash->aSlot[iHash] = p;
+    pHash->nEntry++;
 
     nIncr += p->nData;
   }
@@ -317,6 +355,7 @@ static int fts5HashEntrySort(Fts5Hash *pHash, Fts5HashEntry **ppSorted){
     pList = fts5HashEntryMerge(pList, ap[i]);
   }
 
+  pHash->nEntry = 0;
   sqlite3_free(ap);
   *ppSorted = pList;
   return SQLITE_OK;
index a21f881e61c056cdd1a281c7870878925226567a..9676ad4b458dbcceccc05b130dd9d106008acead 100644 (file)
@@ -3293,7 +3293,7 @@ static void fts5IndexFlush(Fts5Index *p){
 ** to the document with rowid iRowid.
 */
 void sqlite3Fts5IndexBeginWrite(Fts5Index *p, i64 iRowid){
-  if( iRowid<=p->iWriteRowid ){
+  if( iRowid<=p->iWriteRowid || (p->nPendingData > p->nMaxPendingData) ){
     fts5IndexFlush(p);
   }
   p->iWriteRowid = iRowid;
index fab8b2701624461948896853a8c00fa54271e0bc..0e007fa42a43f65f86e51e79081b84eeca517f67 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Simplify\sthe\sway\sposition\slists\sare\scopied\swhen\smerging\sdata.
-D 2014-08-11T20:26:34.077
+C Automatically\sresize\sthe\shash\stable\sused\sby\sfts5.
+D 2014-08-12T08:36:00.189
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in b03432313a3aad96c706f8164fb9f5307eaf19f5
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -110,8 +110,8 @@ F ext/fts5/fts5_aux.c 31e581413ecab0962ce2b37468f9f658f36f4b0e
 F ext/fts5/fts5_buffer.c 248c61ac9fec001602efc72a45704f3b8d367c00
 F ext/fts5/fts5_config.c f4ebf143e141b8c77355e3b15aba81b7be51d710
 F ext/fts5/fts5_expr.c 7b8e380233176053841904a86006696ee8f6cd24
-F ext/fts5/fts5_hash.c 2af412d00f65ad427f18acbe421c113413cdef06
-F ext/fts5/fts5_index.c 687736cba90a735ecd3cf9bf0e4174e7e5cc3f60
+F ext/fts5/fts5_hash.c 63fa8379c5f2ac107d47c2b7d9ac04c95ef8a279
+F ext/fts5/fts5_index.c 0453bb593fe0ef6245762b6823e88839757fdc75
 F ext/fts5/fts5_storage.c fa3c8fc4766d850a4977bf1d4b71c37e7b07ab8b
 F ext/fts5/fts5parse.y 777da8e5819f75c217982c79c29d014c293acac9
 F ext/icu/README.txt d9fbbad0c2f647c3fdf715fc9fd64af53aedfc43
@@ -1202,7 +1202,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh 0abfd78ceb09b7f7c27c688c8e3fe93268a13b32
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
-P 617e2fac1c128212254f71b1a8fddaf0d1d90262
-R b29d729688c110acdf84d0128dad3fdf
+P 9f8d678a0ea75e169daf8b3f00bd05f52a050ea6
+R 0ec55f1e05fde288099bbb5e345c2533
 U dan
-Z 772c48dd1029e003fed3d46062572012
+Z 0fd4a4dea7b2432712fcac248306942e
index 7afe20b5698a5f4582f9e479f36fbe56e142a04f..aa7d7d2665343c386c439c6e577840e8eecde847 100644 (file)
@@ -1 +1 @@
-9f8d678a0ea75e169daf8b3f00bd05f52a050ea6
\ No newline at end of file
+f1cb48f412a5f200f1fe04f91072864f379db08f
\ No newline at end of file