]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Bug fixes to the priority-queue implementation for R-Trees. Improved tracing
authordrh <drh@noemail.net>
Wed, 16 Apr 2014 13:00:08 +0000 (13:00 +0000)
committerdrh <drh@noemail.net>
Wed, 16 Apr 2014 13:00:08 +0000 (13:00 +0000)
capability.  Some queries work now, but still many problems.

FossilOrigin-Name: a439ddd629c6bb5ea2e7e274673fee4f5c207acf

ext/rtree/rtree.c
manifest
manifest.uuid

index 9f41da3534c6f9201a0e98e08bf088ad261d0bf2..3754655bb0620ba686a065027b19677b9247bc20 100644 (file)
@@ -1098,14 +1098,15 @@ static void rtreeSearchPointSwap(RtreeCursor *p, int i, int j){
   assert( i<j );
   p->aPoint[i] = p->aPoint[j];
   p->aPoint[j] = t;
-  if( i<RTREE_CACHE_SZ-1 ){
-    if( j>=RTREE_CACHE_SZ-1 ){
-      nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i+1]);
-      p->aNode[i+1] = 0;
+  i++; j++;
+  if( i<RTREE_CACHE_SZ ){
+    if( j>=RTREE_CACHE_SZ ){
+      nodeRelease(RTREE_OF_CURSOR(p), p->aNode[i]);
+      p->aNode[i] = 0;
     }else{
-      RtreeNode *pTemp = p->aNode[i+i];
-      p->aNode[i+1] = p->aNode[j+1];
-      p->aNode[j+1] = pTemp;
+      RtreeNode *pTemp = p->aNode[i];
+      p->aNode[i] = p->aNode[j];
+      p->aNode[j] = pTemp;
     }
   }
 }
@@ -1182,10 +1183,16 @@ static RtreeSearchPoint *rtreeSearchPointNew(
    || (pFirst->rScore==rScore && pFirst->iLevel>iLevel)
   ){
     if( pCur->bPoint ){
+      int ii;
       pNew = rtreeEnqueue(pCur, rScore, iLevel);
       if( pNew==0 ) return 0;
-      assert( pCur->aNode[1]==0 );
-      pCur->aNode[1] = pCur->aNode[0];
+      ii = (int)(pNew - pCur->aPoint) + 1;
+      if( ii<RTREE_CACHE_SZ ){
+        assert( pCur->aNode[ii]==0 );
+        pCur->aNode[ii] = pCur->aNode[0];
+       }else{
+        nodeRelease(RTREE_OF_CURSOR(pCur), pCur->aNode[0]);
+      }
       pCur->aNode[0] = 0;
       *pNew = pCur->sPoint;
     }
@@ -1198,13 +1205,34 @@ static RtreeSearchPoint *rtreeSearchPointNew(
   }
 }
 
-static void traceTop(RtreeCursor *pCur, const char *zPrefix){
-  RtreeSearchPoint *p = rtreeSearchPointFirst(pCur);
-  if( p ){
-    printf("=== %6s id=%lld lvl=%d iCell=%d rScore=%g eWithin=%d\n",
-       zPrefix, p->id, p->iLevel, p->iCell, p->rScore, p->eWithin);
+#if 0
+/* Tracing routines for the RtreeSearchPoint queue */
+static void tracePoint(RtreeSearchPoint *p, int idx, RtreeCursor *pCur){
+  if( idx<0 ){ printf(" s"); }else{ printf("%2d", idx); }
+  printf(" %d.%05lld.%02d %g %d",
+    p->iLevel, p->id, p->iCell, p->rScore, p->eWithin
+  );
+  idx++;
+  if( idx<RTREE_CACHE_SZ ){
+    printf(" %p\n", pCur->aNode[idx]);
+  }else{
+    printf("\n");
+  }
+}
+static void traceQueue(RtreeCursor *pCur, const char *zPrefix){
+  int ii;
+  printf("=== %9s ", zPrefix);
+  if( pCur->bPoint ){
+    tracePoint(&pCur->sPoint, -1, pCur);
+  }
+  for(ii=0; ii<pCur->nPoint; ii++){
+    if( ii>0 || pCur->bPoint ) printf("              ");
+    tracePoint(&pCur->aPoint[ii], ii, pCur);
   }
 }
+#else
+# define RTREE_QUEUE_TRACE(A,B)   /* no-op */
+#endif
 
 /* Remove the search point with the lowest current score.
 */
@@ -1221,6 +1249,10 @@ static void rtreeSearchPointPop(RtreeCursor *p){
   }else if( p->nPoint ){
     n = --p->nPoint;
     p->aPoint[0] = p->aPoint[n];
+    if( n<RTREE_CACHE_SZ-1 ){
+      p->aNode[1] = p->aNode[n+1];
+      p->aNode[n+1] = 0;
+    }
     i = 0;
     while( (j = i*2+1)<n ){
       k = j+1;
@@ -1276,11 +1308,11 @@ static int rtreeStepToLeaf(RtreeCursor *pCur){
       if( rc ) return rc;
       x = *p;
       p->iCell++;
+      if( eWithin==NOT_WITHIN ) continue;
       if( p->iCell>=nCell ){
-traceTop(pCur, "POP:");
+        RTREE_QUEUE_TRACE(pCur, "POP-S:");
         rtreeSearchPointPop(pCur);
       }
-      if( eWithin==NOT_WITHIN ) continue;
       pNew = rtreeSearchPointNew(pCur, /*rScore*/0.0, x.iLevel-1);
       if( pNew==0 ) return SQLITE_NOMEM;
       pNew->eWithin = eWithin;
@@ -1291,9 +1323,14 @@ traceTop(pCur, "POP:");
         pNew->id = x.id;
         pNew->iCell = x.iCell;
       }
-traceTop(pCur, "PUSH:");
+      p = pNew;
+      RTREE_QUEUE_TRACE(pCur, "PUSH-S:");
       break;
     }
+    if( p->iCell>=nCell ){
+      RTREE_QUEUE_TRACE(pCur, "POP-Se:");
+      rtreeSearchPointPop(pCur);
+    }
   }
   pCur->atEOF = p==0;
   return SQLITE_OK;
@@ -1307,7 +1344,7 @@ static int rtreeNext(sqlite3_vtab_cursor *pVtabCursor){
   int rc = SQLITE_OK;
 
   /* Move to the next entry that matches the configured constraints. */
-traceTop(pCsr, "POP:");
+  RTREE_QUEUE_TRACE(pCsr, "POP-Nx:");
   rtreeSearchPointPop(pCsr);
   rtreeStepToLeaf(pCsr);
   return rc;
@@ -1461,7 +1498,7 @@ static int rtreeFilter(
     p->eWithin = PARTLY_WITHIN;
     if( rc ) rc = nodeRowidIndex(pRtree, pLeaf, iRowid, &iCell);
     p->iCell = iCell;
-traceTop(pCsr, "PUSH:");
+    RTREE_QUEUE_TRACE(pCsr, "PUSH-F1:");
   }else{
     /* Normal case - r-tree scan. Set up the RtreeCursor.aConstraint array 
     ** with the configured constraints. 
@@ -1510,7 +1547,7 @@ traceTop(pCsr, "PUSH:");
       pNew->eWithin = PARTLY_WITHIN;
       assert( pCsr->bPoint==1 );
       pCsr->aNode[0] = pRoot;
-traceTop(pCsr, "PUSH:");
+      RTREE_QUEUE_TRACE(pCsr, "PUSH-Fm:");
       rc = rtreeStepToLeaf(pCsr);
     }
   }
@@ -3177,7 +3214,7 @@ static void rtreenode(sqlite3_context *ctx, int nArg, sqlite3_value **apArg){
     nCell = (int)strlen(zCell);
     for(jj=0; jj<tree.nDim*2; jj++){
 #ifndef SQLITE_RTREE_INT_ONLY
-      sqlite3_snprintf(512-nCell,&zCell[nCell], " %f",
+      sqlite3_snprintf(512-nCell,&zCell[nCell], " %g",
                        (double)cell.aCoord[jj].f);
 #else
       sqlite3_snprintf(512-nCell,&zCell[nCell], " %d",
index 7acb80fc79742e2718dcd57b02021ea69768f16c..83f2beda77d9952d4442ecaeded1a10d60d9ca04 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Initial\sattempt\sat\sgetting\sR-Tree\squeries\sto\swork\susing\sa\spriority\squeue.\nThis\scheck-in\scompiles,\sbut\sR-Trees\sdo\snot\swork\swell.\s\sAnd\sthere\sare\ndebugging\sprintf()s\sleft\sin\sthe\scode.\s\sThis\sis\san\sincremental\scheck-in.
-D 2014-04-15T21:06:14.359
+C Bug\sfixes\sto\sthe\spriority-queue\simplementation\sfor\sR-Trees.\s\sImproved\stracing\ncapability.\s\sSome\squeries\swork\snow,\sbut\sstill\smany\sproblems.
+D 2014-04-16T13:00:08.915
 F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
 F Makefile.in e4ee6d36cdf6136aee0158675a3b24dd3bf31a5a
 F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@@ -120,7 +120,7 @@ F ext/misc/vfslog.c fe40fab5c077a40477f7e5eba994309ecac6cc95
 F ext/misc/vtshim.c babb0dc2bf116029e3e7c9a618b8a1377045303e
 F ext/misc/wholenumber.c 784b12543d60702ebdd47da936e278aa03076212
 F ext/rtree/README 6315c0d73ebf0ec40dedb5aa0e942bc8b54e3761
-F ext/rtree/rtree.c 2f4d35a0689d588698cd0dd7d513761dff20c8fa
+F ext/rtree/rtree.c 3105c5514d6dbf99c39aceac84c4c0f9e00f90ea
 F ext/rtree/rtree.h 834dbcb82dc85b2481cde6a07cdadfddc99e9b9e
 F ext/rtree/rtree1.test cf679265ecafff494a768ac9c2f43a70915a6290
 F ext/rtree/rtree2.test acbb3a4ce0f4fbc2c304d2b4b784cfa161856bba
@@ -1175,10 +1175,7 @@ F tool/vdbe_profile.tcl 67746953071a9f8f2f668b73fe899074e2c6d8c1
 F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
 F tool/warnings.sh d1a6de74685f360ab718efda6265994b99bbea01
 F tool/win/sqlite.vsix 030f3eeaf2cb811a3692ab9c14d021a75ce41fff
-P ade5b986e8baab9df7bdaf7ccfaee2d6ba55fa3c
-R 52e87385ec75daf64fa5f31a0250db29
-T *branch * rtree-queue
-T *sym-rtree-queue *
-T -sym-rtree-enhancements *
+P 53688a25c23c394278a357829793889970aa4157
+R 67a30187386908beb1eb82f50c3b2dfd
 U drh
-Z d0176a391caa75c92333b568fa6cbbbf
+Z ba82d149b9f56c655f1e6f2a959cfc71
index ddde4e05251c7a8a29fcb82181468632cc85cf44..b2db9c77b5e1f52fa8609bf1bf3bf9bb0986c4c7 100644 (file)
@@ -1 +1 @@
-53688a25c23c394278a357829793889970aa4157
\ No newline at end of file
+a439ddd629c6bb5ea2e7e274673fee4f5c207acf
\ No newline at end of file