]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Cache the value of the "totals" record in memory during transactions.
authordan <dan@noemail.net>
Tue, 12 Aug 2014 16:07:35 +0000 (16:07 +0000)
committerdan <dan@noemail.net>
Tue, 12 Aug 2014 16:07:35 +0000 (16:07 +0000)
FossilOrigin-Name: 05dfdad445b22f375b71abe0b1fa1bf7ca331be7

ext/fts5/fts5.c
ext/fts5/fts5Int.h
ext/fts5/fts5_storage.c
manifest
manifest.uuid

index 06d5b8c70b37dd3c1dea5d68ee8628f2abac0516..8b07047aec9af013a4aeaae7338da796e63d96ba 100644 (file)
@@ -934,7 +934,7 @@ static int fts5SyncMethod(sqlite3_vtab *pVtab){
   int rc;
   Fts5Table *pTab = (Fts5Table*)pVtab;
   fts5CheckTransactionState(pTab, FTS5_SYNC, 0);
-  rc = sqlite3Fts5IndexSync(pTab->pIndex, 1);
+  rc = sqlite3Fts5StorageSync(pTab->pStorage, 1);
   return rc;
 }
 
@@ -964,7 +964,7 @@ static int fts5RollbackMethod(sqlite3_vtab *pVtab){
   int rc;
   Fts5Table *pTab = (Fts5Table*)pVtab;
   fts5CheckTransactionState(pTab, FTS5_ROLLBACK, 0);
-  rc = sqlite3Fts5IndexRollback(pTab->pIndex);
+  rc = sqlite3Fts5StorageRollback(pTab->pStorage);
   return rc;
 }
 
@@ -1353,7 +1353,7 @@ static int fts5RenameMethod(
 static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
   Fts5Table *pTab = (Fts5Table*)pVtab;
   fts5CheckTransactionState(pTab, FTS5_SAVEPOINT, iSavepoint);
-  return sqlite3Fts5IndexSync(pTab->pIndex, 0);
+  return sqlite3Fts5StorageSync(pTab->pStorage, 0);
 }
 
 /*
@@ -1364,7 +1364,7 @@ static int fts5SavepointMethod(sqlite3_vtab *pVtab, int iSavepoint){
 static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
   Fts5Table *pTab = (Fts5Table*)pVtab;
   fts5CheckTransactionState(pTab, FTS5_RELEASE, iSavepoint);
-  return sqlite3Fts5IndexSync(pTab->pIndex, 0);
+  return sqlite3Fts5StorageSync(pTab->pStorage, 0);
 }
 
 /*
@@ -1375,7 +1375,7 @@ static int fts5ReleaseMethod(sqlite3_vtab *pVtab, int iSavepoint){
 static int fts5RollbackToMethod(sqlite3_vtab *pVtab, int iSavepoint){
   Fts5Table *pTab = (Fts5Table*)pVtab;
   fts5CheckTransactionState(pTab, FTS5_ROLLBACKTO, iSavepoint);
-  return sqlite3Fts5IndexRollback(pTab->pIndex);
+  return sqlite3Fts5StorageRollback(pTab->pStorage);
 }
 
 /*
index 4ef8454e1fb1715eed8e7abe75a36d66847caa3b..602f293097f62bf4ca16fb3aa80f6e2237344baa 100644 (file)
@@ -353,6 +353,8 @@ int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol);
 int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnAvg);
 int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow);
 
+int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit);
+int sqlite3Fts5StorageRollback(Fts5Storage *p);
 
 /*
 ** End of interface to code in fts5_storage.c.
index a67421075f5f1a4dfa39fb4fc2af26bb3435e370..ff0add5bad72d9e0794ec82ef239bdc5637a35a8 100644 (file)
@@ -17,6 +17,7 @@
 struct Fts5Storage {
   Fts5Config *pConfig;
   Fts5Index *pIndex;
+  int bTotalsValid;               /* True if nTotalRow/aTotalSize[] are valid */
   i64 nTotalRow;                  /* Total number of rows in FTS table */
   i64 *aTotalSize;                /* Total sizes of each column */ 
   sqlite3_stmt *aStmt[9];
@@ -317,31 +318,36 @@ static int fts5StorageInsertDocsize(
 }
 
 /*
-** Load the contents of the "averages" record from disk into the
-** p->nTotalRow and p->aTotalSize[] variables.
+** Load the contents of the "averages" record from disk into the 
+** p->nTotalRow and p->aTotalSize[] variables. If successful, and if
+** argument bCache is true, set the p->bTotalsValid flag to indicate
+** that the contents of aTotalSize[] and nTotalRow are valid until
+** further notice.
 **
 ** Return SQLITE_OK if successful, or an SQLite error code if an error
 ** occurs.
 */
-static int fts5StorageLoadTotals(Fts5Storage *p){
-  int nCol = p->pConfig->nCol;
-  Fts5Buffer buf;
-  int rc;
-  memset(&buf, 0, sizeof(buf));
-
-  memset(p->aTotalSize, 0, sizeof(i64) * nCol);
-  p->nTotalRow = 0;
-  rc = sqlite3Fts5IndexGetAverages(p->pIndex, &buf);
-  if( rc==SQLITE_OK && buf.n ){
-    int i = 0;
-    int iCol;
-    i += getVarint(&buf.p[i], (u64*)&p->nTotalRow);
-    for(iCol=0; i<buf.n && iCol<nCol; iCol++){
-      i += getVarint(&buf.p[i], (u64*)&p->aTotalSize[iCol]);
+static int fts5StorageLoadTotals(Fts5Storage *p, int bCache){
+  int rc = SQLITE_OK;
+  if( p->bTotalsValid==0 ){
+    int nCol = p->pConfig->nCol;
+    Fts5Buffer buf;
+    memset(&buf, 0, sizeof(buf));
+
+    memset(p->aTotalSize, 0, sizeof(i64) * nCol);
+    p->nTotalRow = 0;
+    rc = sqlite3Fts5IndexGetAverages(p->pIndex, &buf);
+    if( rc==SQLITE_OK && buf.n ){
+      int i = 0;
+      int iCol;
+      i += getVarint(&buf.p[i], (u64*)&p->nTotalRow);
+      for(iCol=0; i<buf.n && iCol<nCol; iCol++){
+        i += getVarint(&buf.p[i], (u64*)&p->aTotalSize[iCol]);
+      }
     }
+    sqlite3_free(buf.p);
+    p->bTotalsValid = bCache;
   }
-  sqlite3_free(buf.p);
-
   return rc;
 }
 
@@ -378,7 +384,7 @@ int sqlite3Fts5StorageDelete(Fts5Storage *p, i64 iDel){
   int rc;
   sqlite3_stmt *pDel;
 
-  rc = fts5StorageLoadTotals(p);
+  rc = fts5StorageLoadTotals(p, 1);
 
   /* Delete the index records */
   if( rc==SQLITE_OK ){
@@ -425,13 +431,13 @@ int sqlite3Fts5StorageInsert(
   Fts5Config *pConfig = p->pConfig;
   int rc = SQLITE_OK;             /* Return code */
   sqlite3_stmt *pInsert;          /* Statement used to write %_content table */
-  int eStmt;                      /* Type of statement used on %_content */
+  int eStmt = 0;                  /* Type of statement used on %_content */
   int i;                          /* Counter variable */
   Fts5InsertCtx ctx;              /* Tokenization callback context object */
   Fts5Buffer buf;                 /* Buffer used to build up %_docsize blob */
 
   memset(&buf, 0, sizeof(Fts5Buffer));
-  rc = fts5StorageLoadTotals(p);
+  rc = fts5StorageLoadTotals(p, 1);
 
   /* Insert the new row into the %_content table. */
   if( rc==SQLITE_OK ){
@@ -592,7 +598,7 @@ int sqlite3Fts5StorageIntegrity(Fts5Storage *p){
   /* Test that the "totals" (sometimes called "averages") record looks Ok */
   if( rc==SQLITE_OK ){
     int i;
-    rc = fts5StorageLoadTotals(p);
+    rc = fts5StorageLoadTotals(p, 0);
     for(i=0; rc==SQLITE_OK && i<pConfig->nCol; i++){
       if( p->aTotalSize[i]!=aTotalSize[i] ) rc = SQLITE_CORRUPT_VTAB;
     }
@@ -706,7 +712,7 @@ int sqlite3Fts5StorageDocsize(Fts5Storage *p, i64 iRowid, int *aCol){
 }
 
 int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
-  int rc = fts5StorageLoadTotals(p);
+  int rc = fts5StorageLoadTotals(p, 0);
   if( rc==SQLITE_OK ){
     *pnToken = p->aTotalSize[iCol];
   }
@@ -714,10 +720,27 @@ int sqlite3Fts5StorageSize(Fts5Storage *p, int iCol, i64 *pnToken){
 }
 
 int sqlite3Fts5StorageRowCount(Fts5Storage *p, i64 *pnRow){
-  int rc = fts5StorageLoadTotals(p);
+  int rc = fts5StorageLoadTotals(p, 0);
   if( rc==SQLITE_OK ){
     *pnRow = p->nTotalRow;
   }
   return rc;
 }
 
+/*
+** Flush any data currently held in-memory to disk.
+*/
+int sqlite3Fts5StorageSync(Fts5Storage *p, int bCommit){
+  if( bCommit && p->bTotalsValid ){
+    int rc = fts5StorageSaveTotals(p);
+    p->bTotalsValid = 0;
+    if( rc!=SQLITE_OK ) return rc;
+  }
+  return sqlite3Fts5IndexSync(p->pIndex, bCommit);
+}
+
+int sqlite3Fts5StorageRollback(Fts5Storage *p){
+  p->bTotalsValid = 0;
+  return sqlite3Fts5IndexRollback(p->pIndex);
+}
+
index 0e007fa42a43f65f86e51e79081b84eeca517f67..1f03c49271cdba9639776d5632c0f8244165dcfb 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Automatically\sresize\sthe\shash\stable\sused\sby\sfts5.
-D 2014-08-12T08:36:00.189
+C Cache\sthe\svalue\sof\sthe\s"totals"\srecord\sin\smemory\sduring\stransactions.
+D 2014-08-12T16:07:35.119
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in b03432313a3aad96c706f8164fb9f5307eaf19f5
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -103,16 +103,16 @@ F ext/fts3/tool/fts3view.c 6cfc5b67a5f0e09c0d698f9fd012c784bfaa9197
 F ext/fts3/unicode/CaseFolding.txt 8c678ca52ecc95e16bc7afc2dbf6fc9ffa05db8c
 F ext/fts3/unicode/UnicodeData.txt cd07314edb62d49fde34debdaf92fa2aa69011e7
 F ext/fts3/unicode/mkunicode.tcl dc6f268eb526710e2c6e496c372471d773d0c368
-F ext/fts5/fts5.c 15e585ed0194f94a1da360808f29184f9d44554c
+F ext/fts5/fts5.c 31db0b90774201820915db17916a9a4d9ac1c80b
 F ext/fts5/fts5.h 8ace10d5b249a3baa983c79e7a1306d2a79cfd6a
-F ext/fts5/fts5Int.h f17a25546d598fdc5cc47f576d38063fd9290963
+F ext/fts5/fts5Int.h b0eb5cd422ba74148b30753f01031d546ffb98e4
 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 63fa8379c5f2ac107d47c2b7d9ac04c95ef8a279
 F ext/fts5/fts5_index.c 0453bb593fe0ef6245762b6823e88839757fdc75
-F ext/fts5/fts5_storage.c fa3c8fc4766d850a4977bf1d4b71c37e7b07ab8b
+F ext/fts5/fts5_storage.c 5913aa01a1dada1c5e1a39e4cbb44e84c5f7f350
 F ext/fts5/fts5parse.y 777da8e5819f75c217982c79c29d014c293acac9
 F ext/icu/README.txt d9fbbad0c2f647c3fdf715fc9fd64af53aedfc43
 F ext/icu/icu.c d415ccf984defeb9df2c0e1afcfaa2f6dc05eacb
@@ -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 9f8d678a0ea75e169daf8b3f00bd05f52a050ea6
-R 0ec55f1e05fde288099bbb5e345c2533
+P f1cb48f412a5f200f1fe04f91072864f379db08f
+R 6321f5990f75eb3b76570e3e341050f0
 U dan
-Z 0fd4a4dea7b2432712fcac248306942e
+Z f483f9b1471761b27d0fa8b15d969ed4
index aa7d7d2665343c386c439c6e577840e8eecde847..dc8ddf389dace3fe76ce2b7d3bf3bb679de6554b 100644 (file)
@@ -1 +1 @@
-f1cb48f412a5f200f1fe04f91072864f379db08f
\ No newline at end of file
+05dfdad445b22f375b71abe0b1fa1bf7ca331be7
\ No newline at end of file