Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Btree fixes so that the lsmtest "data*" tests work.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b0fe8e5b45653faf554228129646dd3fee3ebdc4
User & Date: dan 2013-10-11 20:04:18.396
Context
2013-10-12
19:50
Fix page reference leaks in btree code. check-in: f69b88077c user: dan tags: trunk
2013-10-11
20:04
Btree fixes so that the lsmtest "data*" tests work. check-in: b0fe8e5b45 user: dan tags: trunk
18:07
Fixes for keys that spill over onto overflow pages. check-in: bca95ca536 user: dan tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to lsm-test/lsmtest_tdb.c.
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712



713
714
715

716



717
718


719

720















721
722
723
724
725
726
727
728
729
730
731
732











































733
734
735
736
737
738
739
740

int bt_close(TestDb *pTestDb){
  BtDb *p = (BtDb*)pTestDb;
  free(p->aBuffer);
  return sqlite4BtClose(p->pBt);
}

int bt_write(TestDb *pTestDb, void *pK, int nK, void *pV, int nV){
  BtDb *p = (BtDb*)pTestDb;
  int iLevel;
  int rc = SQLITE4_OK;

  iLevel = sqlite4BtTransactionLevel(p->pBt);
  if( iLevel<2 ){ 
    rc = sqlite4BtBegin(p->pBt, 2); 



  }

  rc = sqlite4BtReplace(p->pBt, pK, nK, pV, nV);





  if( rc==SQLITE4_OK && iLevel<2 ){
    rc = sqlite4BtCommit(p->pBt, iLevel); 


  }

















  return rc;
}

int bt_delete(TestDb *pTestDb, void *pK, int nK){
  return bt_write(pTestDb, pK, nK, 0, -1);
}

int bt_delete_range(
  TestDb *pTestDb, 
  void *pKey1, int nKey1,
  void *pKey2, int nKey2
){











































  return SQLITE4_OK;
}

int bt_fetch(TestDb *pTestDb, void *pK, int nK, void **ppVal, int *pnVal){
  BtDb *p = (BtDb*)pTestDb;
  bt_cursor *pCsr = 0;
  int iLevel;
  int rc = SQLITE4_OK;







<
|




|
|
>
>
>


<
>
|
>
>
>
|
|
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>












>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







698
699
700
701
702
703
704

705
706
707
708
709
710
711
712
713
714
715
716

717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806

int bt_close(TestDb *pTestDb){
  BtDb *p = (BtDb*)pTestDb;
  free(p->aBuffer);
  return sqlite4BtClose(p->pBt);
}


static int btMinTransaction(BtDb *p, int iMin, int *piLevel){
  int iLevel;
  int rc = SQLITE4_OK;

  iLevel = sqlite4BtTransactionLevel(p->pBt);
  if( iLevel<iMin ){ 
    rc = sqlite4BtBegin(p->pBt, iMin); 
    *piLevel = iLevel;
  }else{
    *piLevel = -1;
  }


  return rc;
}
static int btRestoreTransaction(BtDb *p, int iLevel, int rcin){
  int rc = rcin;
  if( iLevel>=0 ){
    if( rc==SQLITE4_OK ){
      rc = sqlite4BtCommit(p->pBt, iLevel);
    }else{
      sqlite4BtRollback(p->pBt, iLevel);
    }
    assert( iLevel==sqlite4BtTransactionLevel(p->pBt) );
  }
  return rc;
}



int bt_write(TestDb *pTestDb, void *pK, int nK, void *pV, int nV){
  BtDb *p = (BtDb*)pTestDb;
  int iLevel;
  int rc;

  rc = btMinTransaction(p, 2, &iLevel);
  if( rc==SQLITE4_OK ){
    rc = sqlite4BtReplace(p->pBt, pK, nK, pV, nV);
    rc = btRestoreTransaction(p, iLevel, rc);
  }
  return rc;
}

int bt_delete(TestDb *pTestDb, void *pK, int nK){
  return bt_write(pTestDb, pK, nK, 0, -1);
}

int bt_delete_range(
  TestDb *pTestDb, 
  void *pKey1, int nKey1,
  void *pKey2, int nKey2
){
  BtDb *p = (BtDb*)pTestDb;
  bt_cursor *pCsr = 0;
  int rc = SQLITE4_OK;
  int iLevel;

  rc = btMinTransaction(p, 2, &iLevel);
  if( rc==SQLITE4_OK ){
    rc = sqlite4BtCsrOpen(p->pBt, 0, &pCsr);
  }
  while( rc==SQLITE4_OK ){
    const void *pK;
    int n;
    int nCmp;
    int res;

    rc = sqlite4BtCsrSeek(pCsr, pKey1, nKey1, BT_SEEK_GE);
    if( rc==SQLITE4_INEXACT ) rc = SQLITE4_OK;
    if( rc!=SQLITE4_OK ) break;

    rc = sqlite4BtCsrKey(pCsr, &pK, &n);
    if( rc!=SQLITE4_OK ) break;

    nCmp = MIN(n, nKey1);
    res = memcmp(pKey1, pK, nCmp);
    assert( res<0 || (res==0 && nKey1<=n) );
    if( res==0 && nKey1==n ){
      rc = sqlite4BtCsrNext(pCsr);
      if( rc!=SQLITE4_OK ) break;
      rc = sqlite4BtCsrKey(pCsr, &pK, &n);
      if( rc!=SQLITE4_OK ) break;
    }

    nCmp = MIN(n, nKey2);
    res = memcmp(pKey2, pK, nCmp);
    if( res<0 || (res==0 && nKey2<=n) ) break;
    
    rc = sqlite4BtDelete(pCsr);
  }
  if( rc==SQLITE4_NOTFOUND ) rc = SQLITE4_OK;

  sqlite4BtCsrClose(pCsr);

  rc = btRestoreTransaction(p, iLevel, rc);
  return rc;
}

int bt_fetch(TestDb *pTestDb, void *pK, int nK, void **ppVal, int *pnVal){
  BtDb *p = (BtDb*)pTestDb;
  bt_cursor *pCsr = 0;
  int iLevel;
  int rc = SQLITE4_OK;
782
783
784
785
786
787
788

789



790

791







792

793

794
795
796
797
798
799
800
801
802
803



804





805

806
807
808
809

810



811

812
813
814
815
816
817

818
819
820
821
822
823
824
825
826

827
828
829
830
831
832
833
  void *pFirst, int nFirst,
  void *pLast, int nLast,
  void (*xCallback)(void *, void *, int , void *, int)
){
  BtDb *p = (BtDb*)pTestDb;
  bt_cursor *pCsr = 0;
  int rc;





  rc = sqlite4BtCsrOpen(p->pBt, 0, &pCsr);

  if( rc==SQLITE4_OK ){







    rc = sqlite4BtCsrSeek(pCsr, pFirst, nFirst, BT_SEEK_GE);

    if( rc==SQLITE4_INEXACT ) rc = SQLITE4_OK;

    while( rc==SQLITE4_OK ){
      const void *pK = 0; int nK = 0;
      const void *pV = 0; int nV = 0;

      rc = sqlite4BtCsrKey(pCsr, &pK, &nK);
      if( rc==SQLITE4_OK ){
        rc = sqlite4BtCsrData(pCsr, 0, -1, &pV, &nV);
      }

      if( pLast && rc==SQLITE4_OK ){



        int nCmp = MAX(nK, nLast);





        int res;

        res = memcmp(pLast, pK, nCmp);
        if( res<0 || (res==0 && nK>nLast) ) break;
      }


      xCallback(pCtx, (void*)pK, nK, (void*)pV, nV);



      rc = sqlite4BtCsrNext(pCsr);

    }
    if( rc==SQLITE4_NOTFOUND ) rc = SQLITE4_OK;

    sqlite4BtCsrClose(pCsr);
  }


  return rc;
}

int bt_open(const char *zFilename, int bClear, TestDb **ppDb){
  static const DatabaseMethods SqlMethods = {
    bt_close,
    bt_write,
    bt_delete,
    0,

    bt_fetch,
    bt_scan,
    0,
    0,
    0
  };
  BtDb *p = 0;







>

>
>
>
|
>

>
>
>
>
>
>
>
|
>

>









|
>
>
>
|
>
>
>
>
>
|
>
|
|
|
|
>

>
>
>
|
>






>








<
>







848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920

921
922
923
924
925
926
927
928
  void *pFirst, int nFirst,
  void *pLast, int nLast,
  void (*xCallback)(void *, void *, int , void *, int)
){
  BtDb *p = (BtDb*)pTestDb;
  bt_cursor *pCsr = 0;
  int rc;
  int iLevel;

  rc = btMinTransaction(p, 1, &iLevel);

  if( rc==SQLITE4_OK ){
    rc = sqlite4BtCsrOpen(p->pBt, 0, &pCsr);
  }
  if( rc==SQLITE4_OK ){
    if( bReverse ){
      if( pLast ){
        rc = sqlite4BtCsrSeek(pCsr, pLast, nLast, BT_SEEK_LE);
      }else{
        rc = sqlite4BtCsrLast(pCsr);
      }
    }else{
      rc = sqlite4BtCsrSeek(pCsr, pFirst, nFirst, BT_SEEK_GE);
    }
    if( rc==SQLITE4_INEXACT ) rc = SQLITE4_OK;

    while( rc==SQLITE4_OK ){
      const void *pK = 0; int nK = 0;
      const void *pV = 0; int nV = 0;

      rc = sqlite4BtCsrKey(pCsr, &pK, &nK);
      if( rc==SQLITE4_OK ){
        rc = sqlite4BtCsrData(pCsr, 0, -1, &pV, &nV);
      }

      if( rc!=SQLITE4_OK ) break;
      if( bReverse ){
        if( pFirst ){
          int res;
          int nCmp = MIN(nK, nFirst);
          res = memcmp(pFirst, pK, nCmp);
          if( res>0 || (res==0 && nK<nFirst) ) break;
        }
      }else{
        if( pLast ){
          int res;
          int nCmp = MIN(nK, nLast);
          res = memcmp(pLast, pK, nCmp);
          if( res<0 || (res==0 && nK>nLast) ) break;
        }
      }

      xCallback(pCtx, (void*)pK, nK, (void*)pV, nV);
      if( bReverse ){
        rc = sqlite4BtCsrPrev(pCsr);
      }else{
        rc = sqlite4BtCsrNext(pCsr);
      }
    }
    if( rc==SQLITE4_NOTFOUND ) rc = SQLITE4_OK;

    sqlite4BtCsrClose(pCsr);
  }

  rc = btRestoreTransaction(p, iLevel, rc);
  return rc;
}

int bt_open(const char *zFilename, int bClear, TestDb **ppDb){
  static const DatabaseMethods SqlMethods = {
    bt_close,
    bt_write,
    bt_delete,

    bt_delete_range,
    bt_fetch,
    bt_scan,
    0,
    0,
    0
  };
  BtDb *p = 0;
Changes to src/bt.h.
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
**   SQLITE4_OK:
**   SQLITE4_NOTFOUND:
**   SQLITE4_INEXACT:
**   other:
*/
int sqlite4BtCsrSeek(bt_cursor *pCsr, const void *pK, int nK, int eSeek);

/*
** TODO: Are these actually even required for SQLite4 KV stores?
*/
int sqlite4BtCsrFirst(bt_cursor *pCsr);
int sqlite4BtCsrLast(bt_cursor *pCsr);

int sqlite4BtCsrNext(bt_cursor *pCsr);
int sqlite4BtCsrPrev(bt_cursor *pCsr);

int sqlite4BtCsrKey(bt_cursor *pCsr, const void **ppK, int *pnK);







<
<
<







100
101
102
103
104
105
106



107
108
109
110
111
112
113
**   SQLITE4_OK:
**   SQLITE4_NOTFOUND:
**   SQLITE4_INEXACT:
**   other:
*/
int sqlite4BtCsrSeek(bt_cursor *pCsr, const void *pK, int nK, int eSeek);




int sqlite4BtCsrFirst(bt_cursor *pCsr);
int sqlite4BtCsrLast(bt_cursor *pCsr);

int sqlite4BtCsrNext(bt_cursor *pCsr);
int sqlite4BtCsrPrev(bt_cursor *pCsr);

int sqlite4BtCsrKey(bt_cursor *pCsr, const void **ppK, int *pnK);
Changes to src/bt_main.c.
594
595
596
597
598
599
600

601
602
603
604
605
606
607
        pCell += nByte;
        sqlite4BtVarintGet32(pCell, (int *)&pgno);
      }else{
        pgno = btGetU32(&aData[1]);
      }
    }
  }

  return rc;
}

/*
** Position cursor pCsr to point to the smallest key in the database.
*/
int sqlite4BtCsrFirst(bt_cursor *pCsr){







>







594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
        pCell += nByte;
        sqlite4BtVarintGet32(pCell, (int *)&pgno);
      }else{
        pgno = btGetU32(&aData[1]);
      }
    }
  }
  if( pCsr->aiCell[pCsr->nPg-1] ) pCsr->aiCell[pCsr->nPg-1]--;
  return rc;
}

/*
** Position cursor pCsr to point to the smallest key in the database.
*/
int sqlite4BtCsrFirst(bt_cursor *pCsr){