tdbBtree.c 4.8 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
H
refact  
Hongze Cheng 已提交
14 15
 */

H
Hongze Cheng 已提交
16 17
#include "tdbInt.h"

H
more  
Hongze Cheng 已提交
18 19
#define BTREE_MAX_DEPTH 20

H
Hongze Cheng 已提交
20
struct SBTree {
H
more  
Hongze Cheng 已提交
21 22 23 24 25
  SPgno          root;
  int            keyLen;
  int            valLen;
  SPFile *       pFile;
  FKeyComparator kcmpr;
H
Hongze Cheng 已提交
26 27
};

H
Hongze Cheng 已提交
28
typedef struct SPgHdr {
H
more  
Hongze Cheng 已提交
29
  u8    flags;
H
more  
Hongze Cheng 已提交
30 31
  u8    fragmentTotalSize;
  u16   firstFreeCellOffset;
H
more  
Hongze Cheng 已提交
32 33 34 35 36
  u16   nCells;
  u16   pCell;
  i32   kLen;
  i32   vLen;
  SPgno rightChild;
H
Hongze Cheng 已提交
37 38 39 40 41
} SPgHdr;

typedef struct SBtPage {
  SPgHdr *pHdr;
  u16 *   aCellIdx;
H
more  
Hongze Cheng 已提交
42
  u8 *    aData;
H
Hongze Cheng 已提交
43 44
} SBtPage;

H
Hongze Cheng 已提交
45
struct SBtCursor {
H
Hongze Cheng 已提交
46 47 48
  SBTree * pBt;
  i8       iPage;
  SBtPage *pPage;
H
more  
Hongze Cheng 已提交
49 50 51 52
  u16      idx;
  u16      idxStack[BTREE_MAX_DEPTH + 1];
  SBtPage *pgStack[BTREE_MAX_DEPTH + 1];
  void *   pBuf;
H
Hongze Cheng 已提交
53 54
};

H
more  
Hongze Cheng 已提交
55 56 57 58 59
typedef struct SFreeCell {
  u16 size;
  u16 next;
} SFreeCell;

H
Hongze Cheng 已提交
60 61
static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen);
static int tdbEncodeLength(u8 *pBuf, uint len);
H
more  
Hongze Cheng 已提交
62 63
static int tdbBtCursorMoveToRoot(SBtCursor *pCur);
static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage);
H
more  
Hongze Cheng 已提交
64
static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell);
H
more  
Hongze Cheng 已提交
65

H
more  
Hongze Cheng 已提交
66
int tdbBtreeOpen(SBTree **ppBt) {
H
Hongze Cheng 已提交
67 68 69 70 71 72 73 74 75 76
  *ppBt = NULL;
  /* TODO */
  return 0;
}

int tdbBtreeClose(SBTree *pBt) {
  // TODO
  return 0;
}

H
more  
Hongze Cheng 已提交
77
int tdbBtreeCursor(SBtCursor *pCur, SBTree *pBt) {
H
more  
Hongze Cheng 已提交
78 79
  pCur->pBt = pBt;
  pCur->iPage = -1;
H
more  
Hongze Cheng 已提交
80 81 82
  pCur->pPage = NULL;
  pCur->idx = 0;

H
more  
Hongze Cheng 已提交
83 84 85
  return 0;
}

H
Hongze Cheng 已提交
86
int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen) {
H
more  
Hongze Cheng 已提交
87 88 89
  int      ret;
  SPFile * pFile;
  SBtPage *pPage;
H
Hongze Cheng 已提交
90 91 92 93 94 95 96

  ret = tdbBtCursorMoveTo(pCur, pKey, kLen);
  if (ret < 0) {
    // TODO: handle error
    return -1;
  }

H
more  
Hongze Cheng 已提交
97
  pPage = pCur->pPage;
H
Hongze Cheng 已提交
98

H
more  
Hongze Cheng 已提交
99 100 101
  return 0;
}

H
Hongze Cheng 已提交
102
static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen) {
H
more  
Hongze Cheng 已提交
103 104 105
  int      ret;
  SBtPage *pBtPage;
  void *   pCell;
H
more  
Hongze Cheng 已提交
106 107

  ret = tdbBtCursorMoveToRoot(pCur);
H
more  
Hongze Cheng 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  if (ret < 0) {
    return -1;
  }

  for (;;) {
    int lidx, ridx, midx, c;

    pBtPage = pCur->pPage;
    lidx = 0;
    ridx = pBtPage->pHdr->nCells - 1;
    while (lidx <= ridx) {
      midx = (lidx + ridx) >> 1;
      pCell = (void *)(pBtPage->aData + pBtPage->aCellIdx[midx]);

      c = tdbCompareKeyAndCell(pKey, kLen, pCell);
      if (c == 0) {
        break;
      } else if (c < 0) {
        lidx = lidx + 1;
      } else {
        ridx = ridx - 1;
      }
    }

    /* code */
  }

H
Hongze Cheng 已提交
135 136
  return 0;
}
H
more  
Hongze Cheng 已提交
137

H
Hongze Cheng 已提交
138 139 140
static int tdbEncodeKeyValue(const void *pKey, int kLen, int kLenG, const void *pVal, int vLen, int vLenG, void *pBuf,
                             int *bLen) {
  u8 *pPtr;
H
more  
Hongze Cheng 已提交
141

H
Hongze Cheng 已提交
142 143 144 145 146 147 148 149 150 151 152
  ASSERT(kLen > 0 && vLen > 0);
  ASSERT(kLenG == TDB_VARIANT_LEN || kLenG == kLen);
  ASSERT(vLenG == TDB_VARIANT_LEN || vLenG == vLen);

  pPtr = (u8 *)pBuf;
  if (kLenG == TDB_VARIANT_LEN) {
    pPtr += tdbEncodeLength(pPtr, kLen);
  }

  if (vLenG == TDB_VARIANT_LEN) {
    pPtr += tdbEncodeLength(pPtr, vLen);
H
more  
Hongze Cheng 已提交
153
  }
H
more  
Hongze Cheng 已提交
154

H
Hongze Cheng 已提交
155 156 157 158 159 160 161
  memcpy(pPtr, pKey, kLen);
  pPtr += kLen;

  memcpy(pPtr, pVal, vLen);
  pPtr += vLen;

  *bLen = pPtr - (u8 *)pBuf;
H
more  
Hongze Cheng 已提交
162
  return 0;
H
Hongze Cheng 已提交
163 164 165
}

static int tdbDecodeKeyValue(const void *pBuf, void *pKey, int *kLen, void *pVal, int *vLen) {
H
more  
Hongze Cheng 已提交
166 167 168 169 170 171 172 173 174 175
  if (*kLen == TDB_VARIANT_LEN) {
    // Decode the key length
  }

  if (*vLen == TDB_VARIANT_LEN) {
    // Decode the value length
  }

  // TODO: decode the key and value

H
Hongze Cheng 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189
  return 0;
}

static int tdbEncodeLength(u8 *pBuf, uint len) {
  int iCount = 0;

  while (len > 127) {
    pBuf[iCount++] = (u8)((len & 0xff) | 128);
    len >>= 7;
  }

  pBuf[iCount++] = (u8)len;

  return iCount;
H
more  
Hongze Cheng 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
}

static int tdbBtCursorMoveToRoot(SBtCursor *pCur) {
  SBTree * pBt;
  SPFile * pFile;
  SPage *  pPage;
  SBtPage *pBtPage;
  int      ret;

  pBt = pCur->pBt;
  pFile = pBt->pFile;

  pPage = tdbPFileGet(pFile, pBt->root);
  if (pPage == NULL) {
    // TODO: handle error
  }

  ret = tdbInitBtPage(pPage, &pBtPage);
  if (ret < 0) {
    // TODO
    return 0;
  }

  pCur->pPage = pBtPage;
  pCur->iPage = 0;

  return 0;
}

static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage) {
H
more  
Hongze Cheng 已提交
220 221 222 223 224 225 226 227 228 229 230 231
  SBtPage *pBtPage;

  pBtPage = (SBtPage *)pPage->pExtra;
  pBtPage->pHdr = (SPgHdr *)pPage->pData;
  pBtPage->aCellIdx = (u16 *)(&((pBtPage->pHdr)[1]));
  pBtPage->aData = (u8 *)pPage->pData;

  *ppBtPage = pBtPage;
  return 0;
}
static int tdbCompareKeyAndCell(const void *pKey, int kLen, const void *pCell) {
  /* TODO */
H
more  
Hongze Cheng 已提交
232
  return 0;
H
more  
Hongze Cheng 已提交
233
}