tdbBtree.c 3.7 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
#if 0
H
Hongze Cheng 已提交
19
struct SBtCursor {
H
Hongze Cheng 已提交
20
  SBTree *pBtree;
H
more  
Hongze Cheng 已提交
21
  SPgno  pgno;
H
Hongze Cheng 已提交
22
  SPage * pPage;  // current page traversing
H
Hongze Cheng 已提交
23 24
};

H
Hongze Cheng 已提交
25
typedef struct {
H
more  
Hongze Cheng 已提交
26
  SPgno pgno;
H
Hongze Cheng 已提交
27
  pgsz_t offset;
H
Hongze Cheng 已提交
28 29
} SBtIdx;

H
Hongze Cheng 已提交
30
// Btree page header definition
H
Hongze Cheng 已提交
31
typedef struct __attribute__((__packed__)) {
H
Hongze Cheng 已提交
32 33 34 35 36 37
  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 已提交
38
  SPgno   rChildPgno;  // right most child page number
H
Hongze Cheng 已提交
39 40
} SBtPgHdr;

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

H
Hongze Cheng 已提交
43
#define BTREE_PAGE_HDR(pPage)             NULL /* TODO */
H
Hongze Cheng 已提交
44
#define BTREE_PAGE_PAYLOAD_AT(pPage, idx) NULL /*TODO*/
H
Hongze Cheng 已提交
45
#define BTREE_PAGE_IS_LEAF(pPage)         0    /* TODO */
H
Hongze Cheng 已提交
46

H
Hongze Cheng 已提交
47
static int btreeCreate(SBTree **ppBt);
H
Hongze Cheng 已提交
48
static int btreeDestroy(SBTree *pBt);
H
more  
Hongze Cheng 已提交
49
static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno);
H
Hongze Cheng 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

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 已提交
69
static int btreeCreate(SBTree **ppBt) {
H
Hongze Cheng 已提交
70 71 72 73 74 75 76
  SBTree *pBt;

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

H
Hongze Cheng 已提交
77 78 79 80 81
  // TODO
  return 0;
}

static int btreeDestroy(SBTree *pBt) {
H
Hongze Cheng 已提交
82 83 84
  if (pBt) {
    free(pBt);
  }
H
Hongze Cheng 已提交
85
  return 0;
H
Hongze Cheng 已提交
86 87 88 89 90 91 92 93 94 95 96 97
}

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

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

H
Hongze Cheng 已提交
98
int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) {
H
Hongze Cheng 已提交
99 100 101
  SPage *     pPage;
  SBtPgHdr *  pBtPgHdr;
  SPgFile *   pPgFile;
H
more  
Hongze Cheng 已提交
102 103
  SPgno      childPgno;
  SPgno      rootPgno;
H
Hongze Cheng 已提交
104
  int         nPayloads;
H
Hongze Cheng 已提交
105 106
  void *      pPayload;
  BtreeCmprFn cmpFn;
H
Hongze Cheng 已提交
107 108

  // 1. Move the cursor to the root page
H
Hongze Cheng 已提交
109 110 111 112 113 114 115 116 117
  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 已提交
118 119 120

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

H
Hongze Cheng 已提交
123
    pPage = pBtCur->pPage;
H
Hongze Cheng 已提交
124
    pBtPgHdr = BTREE_PAGE_HDR(pPage);
H
Hongze Cheng 已提交
125
    nPayloads = pBtPgHdr->nPayloads;
H
Hongze Cheng 已提交
126

H
Hongze Cheng 已提交
127 128
    // Binary search the page
    lidx = 0;
H
Hongze Cheng 已提交
129
    ridx = nPayloads - 1;
H
Hongze Cheng 已提交
130
    midx = (lidx + ridx) >> 1;
H
Hongze Cheng 已提交
131
    for (;;) {
H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      // 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 已提交
155 156 157 158 159 160
    }
  }

  return 0;
}

H
more  
Hongze Cheng 已提交
161
static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno) {
H
Hongze Cheng 已提交
162 163 164
  SPgFile *pPgFile;
  // TODO
  return 0;
H
Hongze Cheng 已提交
165 166
}
#endif