tdbBtree.c 3.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
more  
Hongze Cheng 已提交
20 21
typedef int (*FKeyComparator)(const void *pKey1, int kLen1, const void *pKey2, int kLen2);

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

H
Hongze Cheng 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
typedef struct SPgHdr {
  u8  flags;
  u8  nFree;
  u16 firstFree;
  u16 nCells;
  u16 pCell;
  i32 kLen;
  i32 vLen;
} SPgHdr;

typedef struct SBtPage {
  SPgHdr *pHdr;
  u16 *   aCellIdx;
} SBtPage;

H
Hongze Cheng 已提交
45
struct SBtCursor {
H
Hongze Cheng 已提交
46 47 48
  SBTree * pBt;
  i8       iPage;
  SBtPage *pPage;
H
Hongze Cheng 已提交
49 50
};

H
Hongze Cheng 已提交
51 52
static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen);
static int tdbEncodeLength(u8 *pBuf, uint len);
H
more  
Hongze Cheng 已提交
53 54
static int tdbBtCursorMoveToRoot(SBtCursor *pCur);
static int tdbInitBtPage(SPage *pPage, SBtPage **ppBtPage);
H
more  
Hongze Cheng 已提交
55

H
Hongze Cheng 已提交
56 57 58 59 60 61 62 63 64 65 66
int tdbBtreeOpen(SPgno root, SBTree **ppBt) {
  *ppBt = NULL;
  /* TODO */
  return 0;
}

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

H
more  
Hongze Cheng 已提交
67 68 69
int tdbBtreeCursor(SBTree *pBt, SBtCursor *pCur) {
  pCur->pBt = pBt;
  pCur->iPage = -1;
H
more  
Hongze Cheng 已提交
70
  /* TODO */
H
more  
Hongze Cheng 已提交
71 72 73
  return 0;
}

H
Hongze Cheng 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
int tdbBtCursorInsert(SBtCursor *pCur, const void *pKey, int kLen, const void *pVal, int vLen) {
  int     ret;
  SPFile *pFile;

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

  pFile = pCur->pBt->pFile;
  // ret = tdbPFileWrite(pFile, pCur->pPage);
  // if (ret < 0) {
  //   // TODO: handle error
  //   return -1;
  // }

H
more  
Hongze Cheng 已提交
91 92 93
  return 0;
}

H
Hongze Cheng 已提交
94
static int tdbBtCursorMoveTo(SBtCursor *pCur, const void *pKey, int kLen) {
H
more  
Hongze Cheng 已提交
95 96 97
  int ret;

  ret = tdbBtCursorMoveToRoot(pCur);
H
Hongze Cheng 已提交
98 99 100
  // TODO
  return 0;
}
H
more  
Hongze Cheng 已提交
101

H
Hongze Cheng 已提交
102 103 104
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 已提交
105

H
Hongze Cheng 已提交
106 107 108 109 110 111 112 113 114 115 116
  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 已提交
117
  }
H
more  
Hongze Cheng 已提交
118

H
Hongze Cheng 已提交
119 120 121 122 123 124 125
  memcpy(pPtr, pKey, kLen);
  pPtr += kLen;

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

  *bLen = pPtr - (u8 *)pBuf;
H
more  
Hongze Cheng 已提交
126
  return 0;
H
Hongze Cheng 已提交
127 128 129
}

static int tdbDecodeKeyValue(const void *pBuf, void *pKey, int *kLen, void *pVal, int *vLen) {
H
more  
Hongze Cheng 已提交
130 131 132 133 134 135 136 137 138 139
  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 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153
  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 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
}

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) {
  // TODO
  return 0;
H
more  
Hongze Cheng 已提交
186
}