tdbBtree.c 4.5 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
Hongze Cheng 已提交
18 19 20 21 22 23 24 25 26 27
struct SBTree {
  SPgno   root;
  int     keyLen;
  int     valLen;
  SPFile *pFile;
  int (*FKeyComparator)(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2);
};

struct SBtCursor {
  SBTree *pBt;
H
more  
Hongze Cheng 已提交
28
  int8_t  iPage;
H
Hongze Cheng 已提交
29 30
};

H
more  
Hongze Cheng 已提交
31 32 33 34
typedef struct SBPage {
  /* TODO */
} SBPage;

H
Hongze Cheng 已提交
35 36 37 38 39 40 41 42 43 44 45
int tdbBtreeOpen(SPgno root, SBTree **ppBt) {
  *ppBt = NULL;
  /* TODO */
  return 0;
}

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

H
more  
Hongze Cheng 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
int tdbBtreeCursor(SBTree *pBt, SBtCursor *pCur) {
  pCur->pBt = pBt;
  pCur->iPage = -1;
  return 0;
}

int tdbBtreeCursorMoveTo(SBtCursor *pCur) {
  /* TODO */
  return 0;
}

static int tdbBtreeCursorMoveToRoot(SBtCursor *pCur) {
  SPFile *pFile = pCur->pBt->pFile;

  tdbPFileGet(pFile, pCur->pBt->root);
  /* TODO */
  return 0;
}

H
Hongze Cheng 已提交
65
#if 0
H
Hongze Cheng 已提交
66
struct SBtCursor {
H
Hongze Cheng 已提交
67
  SBTree *pBtree;
H
more  
Hongze Cheng 已提交
68
  SPgno  pgno;
H
Hongze Cheng 已提交
69
  SPage * pPage;  // current page traversing
H
Hongze Cheng 已提交
70 71
};

H
Hongze Cheng 已提交
72
typedef struct {
H
more  
Hongze Cheng 已提交
73
  SPgno pgno;
H
Hongze Cheng 已提交
74
  pgsz_t offset;
H
Hongze Cheng 已提交
75 76
} SBtIdx;

H
Hongze Cheng 已提交
77
// Btree page header definition
H
Hongze Cheng 已提交
78
typedef struct __attribute__((__packed__)) {
H
Hongze Cheng 已提交
79 80 81 82 83 84
  uint8_t  flag;        // page flag
  int32_t  vlen;        // value length of current page, TDB_VARIANT_LEN for variant length
  uint16_t nPayloads;   // number of total payloads
  pgoff_t  freeOff;     // free payload offset
  pgsz_t   fragSize;    // total fragment size
  pgoff_t  offPayload;  // payload offset
H
more  
Hongze Cheng 已提交
85
  SPgno   rChildPgno;  // right most child page number
H
Hongze Cheng 已提交
86 87
} SBtPgHdr;

H
Hongze Cheng 已提交
88 89
typedef int (*BtreeCmprFn)(const void *, const void *);

H
Hongze Cheng 已提交
90
#define BTREE_PAGE_HDR(pPage)             NULL /* TODO */
H
Hongze Cheng 已提交
91
#define BTREE_PAGE_PAYLOAD_AT(pPage, idx) NULL /*TODO*/
H
Hongze Cheng 已提交
92
#define BTREE_PAGE_IS_LEAF(pPage)         0    /* TODO */
H
Hongze Cheng 已提交
93

H
Hongze Cheng 已提交
94
static int btreeCreate(SBTree **ppBt);
H
Hongze Cheng 已提交
95
static int btreeDestroy(SBTree *pBt);
H
more  
Hongze Cheng 已提交
96
static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno);
H
Hongze Cheng 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

int btreeOpen(SBTree **ppBt, SPgFile *pPgFile) {
  SBTree *pBt;
  int     ret;

  ret = btreeCreate(&pBt);
  if (ret != 0) {
    return -1;
  }

  *ppBt = pBt;
  return 0;
}

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

H
Hongze Cheng 已提交
116
static int btreeCreate(SBTree **ppBt) {
H
Hongze Cheng 已提交
117 118 119 120 121 122 123
  SBTree *pBt;

  pBt = (SBTree *)calloc(1, sizeof(*pBt));
  if (pBt == NULL) {
    return -1;
  }

H
Hongze Cheng 已提交
124 125 126 127 128
  // TODO
  return 0;
}

static int btreeDestroy(SBTree *pBt) {
H
Hongze Cheng 已提交
129 130 131
  if (pBt) {
    free(pBt);
  }
H
Hongze Cheng 已提交
132
  return 0;
H
Hongze Cheng 已提交
133 134 135 136 137 138 139 140 141 142 143 144
}

int btreeCursorOpen(SBtCursor *pBtCur, SBTree *pBt) {
  // TODO
  return 0;
}

int btreeCursorClose(SBtCursor *pBtCur) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
145
int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) {
H
Hongze Cheng 已提交
146 147 148
  SPage *     pPage;
  SBtPgHdr *  pBtPgHdr;
  SPgFile *   pPgFile;
H
more  
Hongze Cheng 已提交
149 150
  SPgno      childPgno;
  SPgno      rootPgno;
H
Hongze Cheng 已提交
151
  int         nPayloads;
H
Hongze Cheng 已提交
152 153
  void *      pPayload;
  BtreeCmprFn cmpFn;
H
Hongze Cheng 已提交
154 155

  // 1. Move the cursor to the root page
H
Hongze Cheng 已提交
156 157 158 159 160 161 162 163 164
  if (rootPgno == TDB_IVLD_PGNO) {
    // No any data in this btree, just return not found (TODO)
    return 0;
  } else {
    // Load the page from the file by the SPgFile handle
    pPage = pgFileFetch(pPgFile, rootPgno);

    pBtCur->pPage = pPage;
  }
H
Hongze Cheng 已提交
165 166 167

  // 2. Loop to search over the whole tree
  for (;;) {
H
Hongze Cheng 已提交
168 169
    int lidx, ridx, midx, cret;

H
Hongze Cheng 已提交
170
    pPage = pBtCur->pPage;
H
Hongze Cheng 已提交
171
    pBtPgHdr = BTREE_PAGE_HDR(pPage);
H
Hongze Cheng 已提交
172
    nPayloads = pBtPgHdr->nPayloads;
H
Hongze Cheng 已提交
173

H
Hongze Cheng 已提交
174 175
    // Binary search the page
    lidx = 0;
H
Hongze Cheng 已提交
176
    ridx = nPayloads - 1;
H
Hongze Cheng 已提交
177
    midx = (lidx + ridx) >> 1;
H
Hongze Cheng 已提交
178
    for (;;) {
H
Hongze Cheng 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      // get the payload ptr at midx
      pPayload = BTREE_PAGE_PAYLOAD_AT(pPage, midx);

      // the payload and the key
      cret = cmpFn(pKey, pPayload);

      if (cret < 0) {
        /* TODO */
      } else if (cret > 0) {
        /* TODO */
      } else {
        /* TODO */
      }

      if (lidx > ridx) break;
      midx = (lidx + ridx) >> 1;
    }
    if (BTREE_PAGE_IS_LEAF(pPage)) {
      /* TODO */
      break;
    } else {
      /* TODO */
      btreeCursorMoveToChild(pBtCur, childPgno);
H
Hongze Cheng 已提交
202 203 204 205 206 207
    }
  }

  return 0;
}

H
more  
Hongze Cheng 已提交
208
static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno) {
H
Hongze Cheng 已提交
209 210 211
  SPgFile *pPgFile;
  // TODO
  return 0;
H
Hongze Cheng 已提交
212 213
}
#endif