tdbBtree.c 4.1 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 28 29 30 31 32 33 34 35 36 37 38 39 40
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;
};

int tdbBtreeOpen(SPgno root, SBTree **ppBt) {
  *ppBt = NULL;
  /* TODO */
  return 0;
}

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

H
Hongze Cheng 已提交
41
#if 0
H
Hongze Cheng 已提交
42
struct SBtCursor {
H
Hongze Cheng 已提交
43
  SBTree *pBtree;
H
more  
Hongze Cheng 已提交
44
  SPgno  pgno;
H
Hongze Cheng 已提交
45
  SPage * pPage;  // current page traversing
H
Hongze Cheng 已提交
46 47
};

H
Hongze Cheng 已提交
48
typedef struct {
H
more  
Hongze Cheng 已提交
49
  SPgno pgno;
H
Hongze Cheng 已提交
50
  pgsz_t offset;
H
Hongze Cheng 已提交
51 52
} SBtIdx;

H
Hongze Cheng 已提交
53
// Btree page header definition
H
Hongze Cheng 已提交
54
typedef struct __attribute__((__packed__)) {
H
Hongze Cheng 已提交
55 56 57 58 59 60
  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 已提交
61
  SPgno   rChildPgno;  // right most child page number
H
Hongze Cheng 已提交
62 63
} SBtPgHdr;

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

H
Hongze Cheng 已提交
66
#define BTREE_PAGE_HDR(pPage)             NULL /* TODO */
H
Hongze Cheng 已提交
67
#define BTREE_PAGE_PAYLOAD_AT(pPage, idx) NULL /*TODO*/
H
Hongze Cheng 已提交
68
#define BTREE_PAGE_IS_LEAF(pPage)         0    /* TODO */
H
Hongze Cheng 已提交
69

H
Hongze Cheng 已提交
70
static int btreeCreate(SBTree **ppBt);
H
Hongze Cheng 已提交
71
static int btreeDestroy(SBTree *pBt);
H
more  
Hongze Cheng 已提交
72
static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno);
H
Hongze Cheng 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

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 已提交
92
static int btreeCreate(SBTree **ppBt) {
H
Hongze Cheng 已提交
93 94 95 96 97 98 99
  SBTree *pBt;

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

H
Hongze Cheng 已提交
100 101 102 103 104
  // TODO
  return 0;
}

static int btreeDestroy(SBTree *pBt) {
H
Hongze Cheng 已提交
105 106 107
  if (pBt) {
    free(pBt);
  }
H
Hongze Cheng 已提交
108
  return 0;
H
Hongze Cheng 已提交
109 110 111 112 113 114 115 116 117 118 119 120
}

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

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

H
Hongze Cheng 已提交
121
int btreeCursorMoveTo(SBtCursor *pBtCur, int kLen, const void *pKey) {
H
Hongze Cheng 已提交
122 123 124
  SPage *     pPage;
  SBtPgHdr *  pBtPgHdr;
  SPgFile *   pPgFile;
H
more  
Hongze Cheng 已提交
125 126
  SPgno      childPgno;
  SPgno      rootPgno;
H
Hongze Cheng 已提交
127
  int         nPayloads;
H
Hongze Cheng 已提交
128 129
  void *      pPayload;
  BtreeCmprFn cmpFn;
H
Hongze Cheng 已提交
130 131

  // 1. Move the cursor to the root page
H
Hongze Cheng 已提交
132 133 134 135 136 137 138 139 140
  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 已提交
141 142 143

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

H
Hongze Cheng 已提交
146
    pPage = pBtCur->pPage;
H
Hongze Cheng 已提交
147
    pBtPgHdr = BTREE_PAGE_HDR(pPage);
H
Hongze Cheng 已提交
148
    nPayloads = pBtPgHdr->nPayloads;
H
Hongze Cheng 已提交
149

H
Hongze Cheng 已提交
150 151
    // Binary search the page
    lidx = 0;
H
Hongze Cheng 已提交
152
    ridx = nPayloads - 1;
H
Hongze Cheng 已提交
153
    midx = (lidx + ridx) >> 1;
H
Hongze Cheng 已提交
154
    for (;;) {
H
Hongze Cheng 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
      // 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 已提交
178 179 180 181 182 183
    }
  }

  return 0;
}

H
more  
Hongze Cheng 已提交
184
static int btreeCursorMoveToChild(SBtCursor *pBtCur, SPgno pgno) {
H
Hongze Cheng 已提交
185 186 187
  SPgFile *pPgFile;
  // TODO
  return 0;
H
Hongze Cheng 已提交
188 189
}
#endif