tdbBtree.c 55.9 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
#define TDB_BTREE_ROOT 0x1
#define TDB_BTREE_LEAF 0x2
20
#define TDB_BTREE_OVFL 0x4
H
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22
struct SBTree {
H
Hongze Cheng 已提交
23 24 25 26 27 28 29 30 31 32
  SPgno         root;
  int           keyLen;
  int           valLen;
  SPager       *pPager;
  tdb_cmpr_fn_t kcmpr;
  int           pageSize;
  int           maxLocal;
  int           minLocal;
  int           maxLeaf;
  int           minLeaf;
M
Minglei Jin 已提交
33 34
  SBtInfo       info;
  char         *tbname;
H
Hongze Cheng 已提交
35
  void         *pBuf;
H
Hongze Cheng 已提交
36 37
};

H
Hongze Cheng 已提交
38 39
#define TDB_BTREE_PAGE_COMMON_HDR u8 flags;

H
Hongze Cheng 已提交
40
#define TDB_BTREE_PAGE_GET_FLAGS(PAGE)        (PAGE)->pData[0]
H
Hongze Cheng 已提交
41
#define TDB_BTREE_PAGE_SET_FLAGS(PAGE, flags) ((PAGE)->pData[0] = (flags))
H
Hongze Cheng 已提交
42 43
#define TDB_BTREE_PAGE_IS_ROOT(PAGE)          (TDB_BTREE_PAGE_GET_FLAGS(PAGE) & TDB_BTREE_ROOT)
#define TDB_BTREE_PAGE_IS_LEAF(PAGE)          (TDB_BTREE_PAGE_GET_FLAGS(PAGE) & TDB_BTREE_LEAF)
44
#define TDB_BTREE_PAGE_IS_OVFL(PAGE)          (TDB_BTREE_PAGE_GET_FLAGS(PAGE) & TDB_BTREE_OVFL)
45
#define TDB_BTREE_ASSERT_FLAG(flags)                                                     \
46
  tAssert(TDB_FLAG_IS(flags, TDB_BTREE_ROOT) || TDB_FLAG_IS(flags, TDB_BTREE_LEAF) ||     \
47 48
         TDB_FLAG_IS(flags, TDB_BTREE_ROOT | TDB_BTREE_LEAF) || TDB_FLAG_IS(flags, 0) || \
         TDB_FLAG_IS(flags, TDB_BTREE_OVFL))
H
Hongze Cheng 已提交
49

H
Hongze Cheng 已提交
50
#pragma pack(push, 1)
wafwerar's avatar
wafwerar 已提交
51
typedef struct {
H
Hongze Cheng 已提交
52 53 54
  TDB_BTREE_PAGE_COMMON_HDR
} SLeafHdr;

wafwerar's avatar
wafwerar 已提交
55 56
typedef struct {
  TDB_BTREE_PAGE_COMMON_HDR
H
Hongze Cheng 已提交
57 58
  SPgno pgno;  // right-most child
} SIntHdr;
wafwerar's avatar
wafwerar 已提交
59
#pragma pack(pop)
H
more  
Hongze Cheng 已提交
60

H
Hongze Cheng 已提交
61
static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2);
H
Hongze Cheng 已提交
62
static int tdbBtreeOpenImpl(SBTree *pBt);
63
// static int tdbBtreeInitPage(SPage *pPage, void *arg, int init);
H
Hongze Cheng 已提交
64
static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const void *pVal, int vLen, SCell *pCell,
65 66
                              int *szCell, TXN *pTxn, SBTree *pBt);
static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pDecoder, TXN *pTxn, SBTree *pBt);
H
Hongze Cheng 已提交
67
static int tdbBtreeBalance(SBTC *pBtc);
68
static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell, int dropOfp, TXN *pTxn, SBTree *pBt);
H
Hongze Cheng 已提交
69
static int tdbBtcMoveDownward(SBTC *pBtc);
H
Hongze Cheng 已提交
70
static int tdbBtcMoveUpward(SBTC *pBtc);
H
Hongze Cheng 已提交
71

72
int tdbBtreeOpen(int keyLen, int valLen, SPager *pPager, char const *tbname, SPgno pgno, tdb_cmpr_fn_t kcmpr, TDB *pEnv,
73
                 SBTree **ppBt) {
H
Hongze Cheng 已提交
74
  SBTree *pBt;
H
Hongze Cheng 已提交
75
  int     ret;
H
more  
Hongze Cheng 已提交
76

77
  tAssert(keyLen != 0);
H
Hongze Cheng 已提交
78

H
Hongze Cheng 已提交
79
  *ppBt = NULL;
H
Hongze Cheng 已提交
80

H
Hongze Cheng 已提交
81
  pBt = (SBTree *)tdbOsCalloc(1, sizeof(*pBt));
H
Hongze Cheng 已提交
82 83 84 85 86
  if (pBt == NULL) {
    return -1;
  }

  // pBt->keyLen
H
Hongze Cheng 已提交
87
  pBt->keyLen = keyLen < 0 ? TDB_VARIANT_LEN : keyLen;
H
Hongze Cheng 已提交
88
  // pBt->valLen
H
Hongze Cheng 已提交
89
  pBt->valLen = valLen < 0 ? TDB_VARIANT_LEN : valLen;
H
refact  
Hongze Cheng 已提交
90 91
  // pBt->pPager
  pBt->pPager = pPager;
H
Hongze Cheng 已提交
92 93
  // pBt->kcmpr
  pBt->kcmpr = kcmpr ? kcmpr : tdbDefaultKeyCmprFn;
H
Hongze Cheng 已提交
94
  // pBt->pageSize
H
Hongze Cheng 已提交
95
  pBt->pageSize = pPager->pageSize;
H
Hongze Cheng 已提交
96
  // pBt->maxLocal
H
Hongze Cheng 已提交
97
  pBt->maxLocal = tdbPageCapacity(pBt->pageSize, sizeof(SIntHdr)) / 4;
H
Hongze Cheng 已提交
98
  // pBt->minLocal: Should not be allowed smaller than 15, which is [nPayload][nKey][nData]
H
Hongze Cheng 已提交
99
  pBt->minLocal = pBt->maxLocal / 2;
H
Hongze Cheng 已提交
100
  // pBt->maxLeaf
H
Hongze Cheng 已提交
101
  pBt->maxLeaf = tdbPageCapacity(pBt->pageSize, sizeof(SLeafHdr));
H
Hongze Cheng 已提交
102 103
  // pBt->minLeaf
  pBt->minLeaf = pBt->minLocal;
H
Hongze Cheng 已提交
104

105 106 107 108
  // if pgno == 0 fetch new btree root leaf page
  if (pgno == 0) {
    // fetch page & insert into main db
    SPage *pPage;
109
    TXN   *txn;
110

111 112 113 114
    ret = tdbBegin(pEnv, &txn, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED);
    if (ret < 0) {
      return -1;
    }
115 116 117 118

    SBtreeInitPageArg zArg;
    zArg.flags = 0x1 | 0x2;  // root leaf node;
    zArg.pBt = pBt;
119
    ret = tdbPagerFetchPage(pPager, &pgno, &pPage, tdbBtreeInitPage, &zArg, txn);
120
    if (ret < 0) {
121
      tdbAbort(pEnv, txn);
122 123 124 125 126
      return -1;
    }

    ret = tdbPagerWrite(pPager, pPage);
    if (ret < 0) {
127
      tdbError("failed to write page since %s", terrstr());
128
      tdbAbort(pEnv, txn);
129 130
      return -1;
    }
M
Minglei Jin 已提交
131

132
    if (strcmp(TDB_MAINDB_NAME, tbname)) {
M
Minglei Jin 已提交
133 134 135 136
      pBt->info.root = pgno;
      pBt->info.nLevel = 1;
      pBt->info.nData = 0;
      pBt->tbname = (char *)tbname;
137 138

      ret = tdbTbInsert(pPager->pEnv->pMainDb, tbname, strlen(tbname) + 1, &pBt->info, sizeof(pBt->info), txn);
139
      if (ret < 0) {
140
        tdbAbort(pEnv, txn);
141 142 143
        return -1;
      }
    }
M
Minglei Jin 已提交
144

145 146 147 148
    tdbPCacheRelease(pPager->pCache, pPage, txn);

    tdbCommit(pPager->pEnv, txn);
    tdbPostCommit(pPager->pEnv, txn);
149 150
  }

151
  tAssert(pgno != 0);
152 153
  pBt->root = pgno;
  /*
H
Hongze Cheng 已提交
154 155 156
  // TODO: pBt->root
  ret = tdbBtreeOpenImpl(pBt);
  if (ret < 0) {
H
Hongze Cheng 已提交
157
    tdbOsFree(pBt);
H
Hongze Cheng 已提交
158 159
    return -1;
  }
160
  */
H
Hongze Cheng 已提交
161
  *ppBt = pBt;
H
Hongze Cheng 已提交
162 163 164 165
  return 0;
}

int tdbBtreeClose(SBTree *pBt) {
H
Hongze Cheng 已提交
166
  if (pBt) {
H
Hongze Cheng 已提交
167
    tdbFree(pBt->pBuf);
H
Hongze Cheng 已提交
168 169
    tdbOsFree(pBt);
  }
H
Hongze Cheng 已提交
170 171 172
  return 0;
}

H
Hongze Cheng 已提交
173
int tdbBtreeInsert(SBTree *pBt, const void *pKey, int kLen, const void *pVal, int vLen, TXN *pTxn) {
H
Hongze Cheng 已提交
174 175 176 177 178 179 180 181 182
  SBTC   btc;
  SCell *pCell;
  void  *pBuf;
  int    szCell;
  int    szBuf;
  int    ret;
  int    idx;
  int    c;

H
Hongze Cheng 已提交
183
  tdbBtcOpen(&btc, pBt, pTxn);
H
Hongze Cheng 已提交
184

185 186
  tdbTrace("tdb insert, btc: %p, pTxn: %p", &btc, pTxn);

H
Hongze Cheng 已提交
187 188
  // move to the position to insert
  ret = tdbBtcMoveTo(&btc, pKey, kLen, &c);
H
Hongze Cheng 已提交
189
  if (ret < 0) {
H
Hongze Cheng 已提交
190
    tdbBtcClose(&btc);
191
    tAssert(0);
H
Hongze Cheng 已提交
192 193 194
    return -1;
  }

H
Hongze Cheng 已提交
195
  if (btc.idx == -1) {
H
Hongze Cheng 已提交
196
    btc.idx = 0;
H
Hongze Cheng 已提交
197
  } else {
H
Hongze Cheng 已提交
198
    if (c > 0) {
H
Hongze Cheng 已提交
199 200 201
      btc.idx++;
    } else if (c == 0) {
      // dup key not allowed
B
Benguang Zhao 已提交
202
      tdbError("unable to insert dup key. pKey: %p, kLen: %d, btc: %p, pTxn: %p", pKey, kLen, &btc, pTxn);
203
      // tAssert(0);
H
Hongze Cheng 已提交
204 205 206 207
      return -1;
    }
  }

H
Hongze Cheng 已提交
208 209
  ret = tdbBtcUpsert(&btc, pKey, kLen, pVal, vLen, 1);
  if (ret < 0) {
210
    tAssert(0);
H
Hongze Cheng 已提交
211
    tdbBtcClose(&btc);
H
Hongze Cheng 已提交
212 213
    return -1;
  }
H
Hongze Cheng 已提交
214

H
Hongze Cheng 已提交
215 216 217 218 219 220 221 222 223 224 225
  tdbBtcClose(&btc);
  return 0;
}

int tdbBtreeDelete(SBTree *pBt, const void *pKey, int kLen, TXN *pTxn) {
  SBTC btc;
  int  c;
  int  ret;

  tdbBtcOpen(&btc, pBt, pTxn);

226 227
  tdbTrace("tdb delete, btc: %p, pTxn: %p", &btc, pTxn);

H
Hongze Cheng 已提交
228 229
  // move the cursor
  ret = tdbBtcMoveTo(&btc, pKey, kLen, &c);
H
Hongze Cheng 已提交
230
  if (ret < 0) {
H
Hongze Cheng 已提交
231
    tdbBtcClose(&btc);
232
    tAssert(0);
H
more  
Hongze Cheng 已提交
233 234 235
    return -1;
  }

H
Hongze Cheng 已提交
236
  if (btc.idx < 0 || c != 0) {
H
more  
Hongze Cheng 已提交
237
    tdbBtcClose(&btc);
H
Hongze Cheng 已提交
238 239
    return -1;
  }
H
Hongze Cheng 已提交
240

H
Hongze Cheng 已提交
241 242
  // delete the key
  if (tdbBtcDelete(&btc) < 0) {
H
Hongze Cheng 已提交
243
    tdbBtcClose(&btc);
H
Hongze Cheng 已提交
244
    return -1;
H
Hongze Cheng 已提交
245 246
  }

H
Hongze Cheng 已提交
247
  tdbBtcClose(&btc);
H
more  
Hongze Cheng 已提交
248 249 250
  return 0;
}

H
Hongze Cheng 已提交
251
int tdbBtreeUpsert(SBTree *pBt, const void *pKey, int nKey, const void *pData, int nData, TXN *pTxn) {
H
Hongze Cheng 已提交
252 253 254 255 256 257
  SBTC btc;
  int  c;
  int  ret;

  tdbBtcOpen(&btc, pBt, pTxn);

258 259
  tdbTrace("tdb upsert, btc: %p, pTxn: %p", &btc, pTxn);

H
Hongze Cheng 已提交
260
  // move the cursor
H
Hongze Cheng 已提交
261
  ret = tdbBtcMoveTo(&btc, pKey, nKey, &c);
H
Hongze Cheng 已提交
262
  if (ret < 0) {
263
    tAssert(0);
H
Hongze Cheng 已提交
264
    tdbBtcClose(&btc);
H
Hongze Cheng 已提交
265 266 267
    return -1;
  }

H
Hongze Cheng 已提交
268 269 270 271 272 273 274
  if (btc.idx == -1) {
    btc.idx = 0;
    c = 1;
  } else {
    if (c > 0) {
      btc.idx = btc.idx + 1;
    }
H
Hongze Cheng 已提交
275 276
  }

H
Hongze Cheng 已提交
277 278
  ret = tdbBtcUpsert(&btc, pKey, nKey, pData, nData, c);
  if (ret < 0) {
279
    tAssert(0);
H
Hongze Cheng 已提交
280 281 282 283 284 285 286 287
    tdbBtcClose(&btc);
    return -1;
  }

  tdbBtcClose(&btc);
  return 0;
}

H
Hongze Cheng 已提交
288
int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen) {
H
Hongze Cheng 已提交
289 290 291 292
  return tdbBtreePGet(pBt, pKey, kLen, NULL, NULL, ppVal, vLen);
}

int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen) {
H
refact  
Hongze Cheng 已提交
293
  SBTC         btc;
H
Hongze Cheng 已提交
294 295
  SCell       *pCell;
  int          cret;
H
Hongze Cheng 已提交
296
  int          ret;
H
Hongze Cheng 已提交
297 298
  void        *pTKey = NULL;
  void        *pTVal = NULL;
299
  SCellDecoder cd = {0};
H
Hongze Cheng 已提交
300

H
Hongze Cheng 已提交
301
  tdbBtcOpen(&btc, pBt, NULL);
H
Hongze Cheng 已提交
302

303 304
  tdbTrace("tdb pget, btc: %p", &btc);

H
Hongze Cheng 已提交
305 306 307
  ret = tdbBtcMoveTo(&btc, pKey, kLen, &cret);
  if (ret < 0) {
    tdbBtcClose(&btc);
308
    tAssert(0);
H
Hongze Cheng 已提交
309
  }
H
Hongze Cheng 已提交
310

H
Hongze Cheng 已提交
311
  if (btc.idx < 0 || cret) {
H
Hongze Cheng 已提交
312
    tdbBtcClose(&btc);
313

H
Hongze Cheng 已提交
314
    return -1;
H
Hongze Cheng 已提交
315 316 317
  }

  pCell = tdbPageGetCell(btc.pPage, btc.idx);
318
  tdbBtreeDecodeCell(btc.pPage, pCell, &cd, btc.pTxn, pBt);
H
Hongze Cheng 已提交
319

H
Hongze Cheng 已提交
320
  if (ppKey) {
H
Hongze Cheng 已提交
321
    pTKey = tdbRealloc(*ppKey, cd.kLen);
H
Hongze Cheng 已提交
322 323
    if (pTKey == NULL) {
      tdbBtcClose(&btc);
324
      tAssert(0);
H
Hongze Cheng 已提交
325 326 327 328 329
      return -1;
    }
    *ppKey = pTKey;
    *pkLen = cd.kLen;
    memcpy(*ppKey, cd.pKey, cd.kLen);
330 331
  }

H
Hongze Cheng 已提交
332
  if (ppVal) {
H
Hongze Cheng 已提交
333
    pTVal = tdbRealloc(*ppVal, cd.vLen);
H
Hongze Cheng 已提交
334 335
    if (pTVal == NULL) {
      tdbBtcClose(&btc);
336
      tAssert(0);
H
Hongze Cheng 已提交
337 338 339 340 341
      return -1;
    }
    *ppVal = pTVal;
    *vLen = cd.vLen;
    memcpy(*ppVal, cd.pVal, cd.vLen);
342
  }
H
Hongze Cheng 已提交
343

344 345 346 347 348
  if (TDB_CELLDECODER_FREE_KEY(&cd)) {
    tdbFree(cd.pKey);
  }

  if (TDB_CELLDECODER_FREE_VAL(&cd)) {
349 350
    tdbDebug("tdb btc/pget/2 decoder: %p pVal free: %p", &cd, cd.pVal);

351 352 353
    tdbFree(cd.pVal);
  }

354 355
  tdbTrace("tdb pget end, btc decoder: %p/0x%x, local decoder:%p", &btc.coder, btc.coder.freeKV, &cd);

H
Hongze Cheng 已提交
356 357
  tdbBtcClose(&btc);

358 359 360
  return 0;
}

H
Hongze Cheng 已提交
361 362 363 364
static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2) {
  int mlen;
  int cret;

365
  tAssert(keyLen1 > 0 && keyLen2 > 0 && pKey1 != NULL && pKey2 != NULL);
H
Hongze Cheng 已提交
366 367 368 369 370 371 372 373 374 375 376 377 378

  mlen = keyLen1 < keyLen2 ? keyLen1 : keyLen2;
  cret = memcmp(pKey1, pKey2, mlen);
  if (cret == 0) {
    if (keyLen1 < keyLen2) {
      cret = -1;
    } else if (keyLen1 > keyLen2) {
      cret = 1;
    } else {
      cret = 0;
    }
  }
  return cret;
H
Hongze Cheng 已提交
379
}
380
/*
H
Hongze Cheng 已提交
381 382 383 384 385 386 387
static int tdbBtreeOpenImpl(SBTree *pBt) {
  // Try to get the root page of the an existing btree
  SPgno  pgno;
  SPage *pPage;
  int    ret;

  {
H
Hongze Cheng 已提交
388
    // 1. TODO: Search the main DB to check if the DB exists
389
    ret = tdbPagerOpenDB(pBt->pPager, &pgno, true, pBt);
390
    tAssert(ret == 0);
H
Hongze Cheng 已提交
391 392 393 394 395 396 397 398
  }

  if (pgno != 0) {
    pBt->root = pgno;
    return 0;
  }

  // Try to create a new database
H
Hongze Cheng 已提交
399
  ret = tdbPagerAllocPage(pBt->pPager, &pgno);
H
Hongze Cheng 已提交
400
  if (ret < 0) {
401
    tAssert(0);
H
Hongze Cheng 已提交
402 403 404
    return -1;
  }

405
  tAssert(pgno != 0);
H
Hongze Cheng 已提交
406
  pBt->root = pgno;
H
Hongze Cheng 已提交
407 408
  return 0;
}
409
*/
410
int tdbBtreeInitPage(SPage *pPage, void *arg, int init) {
H
Hongze Cheng 已提交
411
  SBTree *pBt;
H
Hongze Cheng 已提交
412
  u8      flags;
H
Hongze Cheng 已提交
413
  u8      leaf;
H
Hongze Cheng 已提交
414

H
Hongze Cheng 已提交
415
  pBt = ((SBtreeInitPageArg *)arg)->pBt;
H
Hongze Cheng 已提交
416

H
Hongze Cheng 已提交
417 418 419 420 421
  if (init) {
    // init page
    flags = TDB_BTREE_PAGE_GET_FLAGS(pPage);
    leaf = TDB_BTREE_PAGE_IS_LEAF(pPage);
    TDB_BTREE_ASSERT_FLAG(flags);
H
Hongze Cheng 已提交
422

H
Hongze Cheng 已提交
423
    tdbPageInit(pPage, leaf ? sizeof(SLeafHdr) : sizeof(SIntHdr), tdbBtreeCellSize);
H
Hongze Cheng 已提交
424
  } else {
H
Hongze Cheng 已提交
425 426 427 428
    // zero page
    flags = ((SBtreeInitPageArg *)arg)->flags;
    leaf = flags & TDB_BTREE_LEAF;
    TDB_BTREE_ASSERT_FLAG(flags);
H
Hongze Cheng 已提交
429

H
Hongze Cheng 已提交
430
    tdbPageZero(pPage, leaf ? sizeof(SLeafHdr) : sizeof(SIntHdr), tdbBtreeCellSize);
H
Hongze Cheng 已提交
431

H
Hongze Cheng 已提交
432 433 434
    if (leaf) {
      SLeafHdr *pLeafHdr = (SLeafHdr *)(pPage->pData);
      pLeafHdr->flags = flags;
H
Hongze Cheng 已提交
435

H
Hongze Cheng 已提交
436 437 438 439 440 441
    } else {
      SIntHdr *pIntHdr = (SIntHdr *)(pPage->pData);
      pIntHdr->flags = flags;
      pIntHdr->pgno = 0;
    }
  }
H
Hongze Cheng 已提交
442

H
refact  
Hongze Cheng 已提交
443
  if (leaf) {
H
Hongze Cheng 已提交
444 445 446 447
    pPage->kLen = pBt->keyLen;
    pPage->vLen = pBt->valLen;
    pPage->maxLocal = pBt->maxLeaf;
    pPage->minLocal = pBt->minLeaf;
448 449 450 451 452
  } else if (TDB_BTREE_PAGE_IS_OVFL(pPage)) {
    pPage->kLen = pBt->keyLen;
    pPage->vLen = pBt->valLen;
    pPage->maxLocal = tdbPageCapacity(pBt->pageSize, sizeof(SIntHdr));
    pPage->minLocal = pBt->minLocal;
H
Hongze Cheng 已提交
453 454 455 456 457 458
  } else {
    pPage->kLen = pBt->keyLen;
    pPage->vLen = sizeof(SPgno);
    pPage->maxLocal = pBt->maxLocal;
    pPage->minLocal = pBt->minLocal;
  }
H
Hongze Cheng 已提交
459

H
Hongze Cheng 已提交
460
  return 0;
H
Hongze Cheng 已提交
461 462
}

H
Hongze Cheng 已提交
463
// TDB_BTREE_BALANCE =====================
H
Hongze Cheng 已提交
464
static int tdbBtreeBalanceDeeper(SBTree *pBt, SPage *pRoot, SPage **ppChild, TXN *pTxn) {
H
Hongze Cheng 已提交
465 466 467 468
  SPager           *pPager;
  SPage            *pChild;
  SPgno             pgnoChild;
  int               ret;
H
Hongze Cheng 已提交
469
  u8                flags;
H
Hongze Cheng 已提交
470
  SIntHdr          *pIntHdr;
H
Hongze Cheng 已提交
471
  SBtreeInitPageArg zArg;
H
Hongze Cheng 已提交
472
  u8                leaf;
H
Hongze Cheng 已提交
473 474

  pPager = pRoot->pPager;
H
Hongze Cheng 已提交
475
  flags = TDB_BTREE_PAGE_GET_FLAGS(pRoot);
H
refact  
Hongze Cheng 已提交
476
  leaf = TDB_BTREE_PAGE_IS_LEAF(pRoot);
H
Hongze Cheng 已提交
477

H
Hongze Cheng 已提交
478 479
  // allocate a new child page
  pgnoChild = 0;
H
Hongze Cheng 已提交
480
  zArg.flags = TDB_FLAG_REMOVE(flags, TDB_BTREE_ROOT);
H
Hongze Cheng 已提交
481
  zArg.pBt = pBt;
H
Hongze Cheng 已提交
482
  ret = tdbPagerFetchPage(pPager, &pgnoChild, &pChild, tdbBtreeInitPage, &zArg, pTxn);
H
Hongze Cheng 已提交
483 484 485 486
  if (ret < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
487 488 489 490
  if (!leaf) {
    ((SIntHdr *)pChild->pData)->pgno = ((SIntHdr *)(pRoot->pData))->pgno;
  }

H
Hongze Cheng 已提交
491 492
  ret = tdbPagerWrite(pPager, pChild);
  if (ret < 0) {
493 494
    tdbError("failed to write page since %s", terrstr());
    return -1;
H
Hongze Cheng 已提交
495 496
  }

H
Hongze Cheng 已提交
497
  // Copy the root page content to the child page
498
  tdbPageCopy(pRoot, pChild, 0);
H
Hongze Cheng 已提交
499 500 501 502

  // Reinitialize the root page
  zArg.flags = TDB_BTREE_ROOT;
  zArg.pBt = pBt;
H
Hongze Cheng 已提交
503
  ret = tdbBtreeInitPage(pRoot, &zArg, 0);
H
Hongze Cheng 已提交
504 505 506 507
  if (ret < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
508
  pIntHdr = (SIntHdr *)(pRoot->pData);
H
Hongze Cheng 已提交
509
  pIntHdr->pgno = pgnoChild;
H
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511 512 513 514
  *ppChild = pChild;
  return 0;
}

H
Hongze Cheng 已提交
515
static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTxn) {
H
Hongze Cheng 已提交
516 517
  int ret;

M
Minglei Jin 已提交
518
  int    nOlds, pageIdx;
H
Hongze Cheng 已提交
519
  SPage *pOlds[3] = {0};
H
Hongze Cheng 已提交
520 521
  SCell *pDivCell[3] = {0};
  int    szDivCell[3];
H
Hongze Cheng 已提交
522
  int    sIdx;
H
Hongze Cheng 已提交
523
  u8     childNotLeaf;
H
Hongze Cheng 已提交
524
  SPgno  rPgno;
H
Hongze Cheng 已提交
525

H
Hongze Cheng 已提交
526
  {  // Find 3 child pages at most to do balance
H
Hongze Cheng 已提交
527 528 529
    int    nCells = TDB_PAGE_TOTAL_CELLS(pParent);
    SCell *pCell;

H
Hongze Cheng 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543
    if (nCells <= 2) {
      sIdx = 0;
      nOlds = nCells + 1;
    } else {
      // has more than three child pages
      if (idx == 0) {
        sIdx = 0;
      } else if (idx == nCells) {
        sIdx = idx - 2;
      } else {
        sIdx = idx - 1;
      }
      nOlds = 3;
    }
H
Hongze Cheng 已提交
544
    for (int i = 0; i < nOlds; i++) {
545
      tAssert(sIdx + i <= nCells);
H
Hongze Cheng 已提交
546

H
Hongze Cheng 已提交
547
      SPgno pgno;
H
Hongze Cheng 已提交
548
      if (sIdx + i == nCells) {
549
        tAssert(!TDB_BTREE_PAGE_IS_LEAF(pParent));
H
Hongze Cheng 已提交
550 551
        pgno = ((SIntHdr *)(pParent->pData))->pgno;
      } else {
H
Hongze Cheng 已提交
552
        pCell = tdbPageGetCell(pParent, sIdx + i);
H
Hongze Cheng 已提交
553 554 555
        pgno = *(SPgno *)pCell;
      }

H
Hongze Cheng 已提交
556 557
      ret = tdbPagerFetchPage(pBt->pPager, &pgno, pOlds + i, tdbBtreeInitPage,
                              &((SBtreeInitPageArg){.pBt = pBt, .flags = 0}), pTxn);
H
Hongze Cheng 已提交
558
      if (ret < 0) {
559
        tAssert(0);
H
Hongze Cheng 已提交
560 561
        return -1;
      }
H
Hongze Cheng 已提交
562 563 564

      ret = tdbPagerWrite(pBt->pPager, pOlds[i]);
      if (ret < 0) {
565
        tdbError("failed to write page since %s", terrstr());
H
Hongze Cheng 已提交
566 567
        return -1;
      }
H
Hongze Cheng 已提交
568
    }
H
Hongze Cheng 已提交
569
    // copy the parent key out if child pages are not leaf page
H
refact  
Hongze Cheng 已提交
570
    childNotLeaf = !TDB_BTREE_PAGE_IS_LEAF(pOlds[0]);
H
Hongze Cheng 已提交
571
    if (childNotLeaf) {
H
Hongze Cheng 已提交
572 573 574
      for (int i = 0; i < nOlds; i++) {
        if (sIdx + i < TDB_PAGE_TOTAL_CELLS(pParent)) {
          pCell = tdbPageGetCell(pParent, sIdx + i);
575
          szDivCell[i] = tdbBtreeCellSize(pParent, pCell, 0, NULL, NULL);
H
Hongze Cheng 已提交
576
          pDivCell[i] = tdbOsMalloc(szDivCell[i]);
H
Hongze Cheng 已提交
577 578
          memcpy(pDivCell[i], pCell, szDivCell[i]);
        }
H
Hongze Cheng 已提交
579

H
Hongze Cheng 已提交
580 581 582
        if (i < nOlds - 1) {
          ((SPgno *)pDivCell[i])[0] = ((SIntHdr *)pOlds[i]->pData)->pgno;
          ((SIntHdr *)pOlds[i]->pData)->pgno = 0;
H
Hongze Cheng 已提交
583
          tdbPageInsertCell(pOlds[i], TDB_PAGE_TOTAL_CELLS(pOlds[i]), pDivCell[i], szDivCell[i], 1);
H
Hongze Cheng 已提交
584
        }
H
Hongze Cheng 已提交
585
      }
H
Hongze Cheng 已提交
586
      rPgno = ((SIntHdr *)pOlds[nOlds - 1]->pData)->pgno;
H
Hongze Cheng 已提交
587
    }
H
Hongze Cheng 已提交
588 589 590

    ret = tdbPagerWrite(pBt->pPager, pParent);
    if (ret < 0) {
591
      tdbError("failed to write page since %s", terrstr());
H
Hongze Cheng 已提交
592 593 594
      return -1;
    }

H
Hongze Cheng 已提交
595
    // drop the cells on parent page
H
Hongze Cheng 已提交
596 597 598
    for (int i = 0; i < nOlds; i++) {
      nCells = TDB_PAGE_TOTAL_CELLS(pParent);
      if (sIdx < nCells) {
599
        tdbPageDropCell(pParent, sIdx, pTxn, pBt);
H
Hongze Cheng 已提交
600 601 602 603
      } else {
        ((SIntHdr *)pParent->pData)->pgno = 0;
      }
    }
H
Hongze Cheng 已提交
604 605
  }

H
Hongze Cheng 已提交
606
  int nNews = 0;
H
Hongze Cheng 已提交
607
  struct {
H
Hongze Cheng 已提交
608 609 610 611
    int cnt;
    int size;
    int iPage;
    int oIdx;
H
Hongze Cheng 已提交
612
  } infoNews[5] = {0};
H
Hongze Cheng 已提交
613 614 615

  {  // Get how many new pages are needed and the new distribution

H
Hongze Cheng 已提交
616 617 618
    // first loop to find minimum number of pages needed
    for (int oPage = 0; oPage < nOlds; oPage++) {
      SPage *pPage = pOlds[oPage];
H
Hongze Cheng 已提交
619 620
      SCell *pCell;
      int    cellBytes;
H
Hongze Cheng 已提交
621
      int    oIdx;
H
Hongze Cheng 已提交
622

H
Hongze Cheng 已提交
623
      for (oIdx = 0; oIdx < TDB_PAGE_TOTAL_CELLS(pPage); oIdx++) {
H
Hongze Cheng 已提交
624
        pCell = tdbPageGetCell(pPage, oIdx);
H
Hongze Cheng 已提交
625 626
        cellBytes = TDB_BYTES_CELL_TAKEN(pPage, pCell);

H
Hongze Cheng 已提交
627 628
        if (infoNews[nNews].size + cellBytes > TDB_PAGE_USABLE_SIZE(pPage)) {
          // page is full, use a new page
H
Hongze Cheng 已提交
629
          nNews++;
H
Hongze Cheng 已提交
630

631
          tAssert(infoNews[nNews].size + cellBytes <= TDB_PAGE_USABLE_SIZE(pPage));
H
Hongze Cheng 已提交
632 633 634 635 636 637

          if (childNotLeaf) {
            // for non-child page, this cell is used as the right-most child,
            // the divider cell to parent as well
            continue;
          }
H
Hongze Cheng 已提交
638
        }
H
Hongze Cheng 已提交
639 640
        infoNews[nNews].cnt++;
        infoNews[nNews].size += cellBytes;
H
Hongze Cheng 已提交
641
        infoNews[nNews].iPage = oPage;
H
Hongze Cheng 已提交
642
        infoNews[nNews].oIdx = oIdx;
H
Hongze Cheng 已提交
643 644 645
      }
    }

H
Hongze Cheng 已提交
646
    nNews++;
H
Hongze Cheng 已提交
647 648 649

    // back loop to make the distribution even
    for (int iNew = nNews - 1; iNew > 0; iNew--) {
H
Hongze Cheng 已提交
650
      SCell *pCell;
H
Hongze Cheng 已提交
651 652
      int    szLCell, szRCell;

H
Hongze Cheng 已提交
653
      // balance page (iNew) and (iNew-1)
H
Hongze Cheng 已提交
654
      for (;;) {
H
Hongze Cheng 已提交
655
        pCell = tdbPageGetCell(pOlds[infoNews[iNew - 1].iPage], infoNews[iNew - 1].oIdx);
H
Hongze Cheng 已提交
656

657
        szLCell = tdbBtreeCellSize(pOlds[infoNews[iNew - 1].iPage], pCell, 0, NULL, NULL);
H
Hongze Cheng 已提交
658 659
        if (!childNotLeaf) {
          szRCell = szLCell;
H
Hongze Cheng 已提交
660
        } else {
H
Hongze Cheng 已提交
661
          int    iPage = infoNews[iNew - 1].iPage;
H
Hongze Cheng 已提交
662
          int    oIdx = infoNews[iNew - 1].oIdx + 1;
H
Hongze Cheng 已提交
663
          SPage *pPage;
H
Hongze Cheng 已提交
664
          for (;;) {
H
Hongze Cheng 已提交
665
            pPage = pOlds[iPage];
H
Hongze Cheng 已提交
666 667 668 669
            if (oIdx < TDB_PAGE_TOTAL_CELLS(pPage)) {
              break;
            }

H
Hongze Cheng 已提交
670
            iPage++;
H
Hongze Cheng 已提交
671 672 673 674
            oIdx = 0;
          }

          pCell = tdbPageGetCell(pPage, oIdx);
675
          szRCell = tdbBtreeCellSize(pPage, pCell, 0, NULL, NULL);
H
Hongze Cheng 已提交
676 677
        }

678
        tAssert(infoNews[iNew - 1].cnt > 0);
H
Hongze Cheng 已提交
679 680 681 682 683

        if (infoNews[iNew].size + szRCell >= infoNews[iNew - 1].size - szRCell) {
          break;
        }

H
Hongze Cheng 已提交
684
        // Move a cell right forward
H
Hongze Cheng 已提交
685 686
        infoNews[iNew - 1].cnt--;
        infoNews[iNew - 1].size -= szLCell;
H
Hongze Cheng 已提交
687
        infoNews[iNew - 1].oIdx--;
H
Hongze Cheng 已提交
688
        for (;;) {
H
Hongze Cheng 已提交
689
          if (infoNews[iNew - 1].oIdx >= 0) {
H
Hongze Cheng 已提交
690 691
            break;
          }
H
Hongze Cheng 已提交
692

H
Hongze Cheng 已提交
693 694
          infoNews[iNew - 1].iPage--;
          infoNews[iNew - 1].oIdx = TDB_PAGE_TOTAL_CELLS(pOlds[infoNews[iNew - 1].iPage]) - 1;
H
Hongze Cheng 已提交
695 696 697
        }

        infoNews[iNew].cnt++;
H
Hongze Cheng 已提交
698
        infoNews[iNew].size += szRCell;
H
Hongze Cheng 已提交
699
      }
H
Hongze Cheng 已提交
700
    }
H
Hongze Cheng 已提交
701 702
  }

H
Hongze Cheng 已提交
703 704 705 706 707 708 709 710 711 712 713 714 715
  SPage *pNews[5] = {0};
  {  // Allocate new pages, reuse the old page when possible

    SPgno             pgno;
    SBtreeInitPageArg iarg;
    u8                flags;

    flags = TDB_BTREE_PAGE_GET_FLAGS(pOlds[0]);

    for (int iNew = 0; iNew < nNews; iNew++) {
      if (iNew < nOlds) {
        pNews[iNew] = pOlds[iNew];
      } else {
H
Hongze Cheng 已提交
716
        pgno = 0;
H
Hongze Cheng 已提交
717 718
        iarg.pBt = pBt;
        iarg.flags = flags;
H
Hongze Cheng 已提交
719
        ret = tdbPagerFetchPage(pBt->pPager, &pgno, pNews + iNew, tdbBtreeInitPage, &iarg, pTxn);
H
Hongze Cheng 已提交
720
        if (ret < 0) {
721
          tAssert(0);
H
Hongze Cheng 已提交
722
        }
H
Hongze Cheng 已提交
723 724 725

        ret = tdbPagerWrite(pBt->pPager, pNews[iNew]);
        if (ret < 0) {
726
          tdbError("failed to write page since %s", terrstr());
H
Hongze Cheng 已提交
727 728
          return -1;
        }
H
Hongze Cheng 已提交
729 730
      }
    }
H
Hongze Cheng 已提交
731 732

    // TODO: sort the page according to the page number
H
Hongze Cheng 已提交
733 734
  }

H
Hongze Cheng 已提交
735
  {  // Do the real cell distribution
H
Hongze Cheng 已提交
736
    SPage            *pOldsCopy[3] = {0};
H
Hongze Cheng 已提交
737 738 739 740
    SCell            *pCell;
    int               szCell;
    SBtreeInitPageArg iarg;
    int               iNew, nNewCells;
M
Minglei Jin 已提交
741
    SCellDecoder      cd = {0};
H
Hongze Cheng 已提交
742 743 744 745

    iarg.pBt = pBt;
    iarg.flags = TDB_BTREE_PAGE_GET_FLAGS(pOlds[0]);
    for (int i = 0; i < nOlds; i++) {
H
Hongze Cheng 已提交
746 747
      tdbPageCreate(pOlds[0]->pageSize, &pOldsCopy[i], tdbDefaultMalloc, NULL);
      tdbBtreeInitPage(pOldsCopy[i], &iarg, 0);
748
      tdbPageCopy(pOlds[i], pOldsCopy[i], 0);
749
      pOlds[i]->nOverflow = 0;
750 751
    }

H
Hongze Cheng 已提交
752 753
    iNew = 0;
    nNewCells = 0;
754
    tdbBtreeInitPage(pNews[iNew], &iarg, 0);
H
Hongze Cheng 已提交
755 756 757 758 759 760 761 762

    for (int iOld = 0; iOld < nOlds; iOld++) {
      SPage *pPage;

      pPage = pOldsCopy[iOld];

      for (int oIdx = 0; oIdx < TDB_PAGE_TOTAL_CELLS(pPage); oIdx++) {
        pCell = tdbPageGetCell(pPage, oIdx);
763
        szCell = tdbBtreeCellSize(pPage, pCell, 0, NULL, NULL);
H
Hongze Cheng 已提交
764

765 766
        tAssert(nNewCells <= infoNews[iNew].cnt);
        tAssert(iNew < nNews);
H
Hongze Cheng 已提交
767 768 769 770

        if (nNewCells < infoNews[iNew].cnt) {
          tdbPageInsertCell(pNews[iNew], nNewCells, pCell, szCell, 0);
          nNewCells++;
H
Hongze Cheng 已提交
771

H
Hongze Cheng 已提交
772 773 774 775 776 777 778
          // insert parent page
          if (!childNotLeaf && nNewCells == infoNews[iNew].cnt) {
            SIntHdr *pIntHdr = (SIntHdr *)pParent->pData;

            if (iNew == nNews - 1 && pIntHdr->pgno == 0) {
              pIntHdr->pgno = TDB_PAGE_PGNO(pNews[iNew]);
            } else {
779
              tdbBtreeDecodeCell(pPage, pCell, &cd, pTxn, pBt);
H
Hongze Cheng 已提交
780

H
Hongze Cheng 已提交
781
              // TODO: pCell here may be inserted as an overflow cell, handle it
H
Hongze Cheng 已提交
782
              SCell *pNewCell = tdbOsMalloc(cd.kLen + 9);
H
Hongze Cheng 已提交
783 784 785
              int    szNewCell;
              SPgno  pgno;
              pgno = TDB_PAGE_PGNO(pNews[iNew]);
786 787
              tdbBtreeEncodeCell(pParent, cd.pKey, cd.kLen, (void *)&pgno, sizeof(SPgno), pNewCell, &szNewCell, pTxn,
                                 pBt);
H
Hongze Cheng 已提交
788
              tdbPageInsertCell(pParent, sIdx++, pNewCell, szNewCell, 0);
H
Hongze Cheng 已提交
789
              tdbOsFree(pNewCell);
M
Minglei Jin 已提交
790 791 792 793 794

              if (TDB_CELLDECODER_FREE_VAL(&cd)) {
                tdbFree(cd.pVal);
                cd.pVal = NULL;
              }
H
Hongze Cheng 已提交
795
            }
H
Hongze Cheng 已提交
796 797

            // move to next new page
H
Hongze Cheng 已提交
798
            iNew++;
H
Hongze Cheng 已提交
799 800
            nNewCells = 0;
            if (iNew < nNews) {
H
Hongze Cheng 已提交
801
              tdbBtreeInitPage(pNews[iNew], &iarg, 0);
H
Hongze Cheng 已提交
802
            }
H
Hongze Cheng 已提交
803 804
          }
        } else {
805 806
          tAssert(childNotLeaf);
          tAssert(iNew < nNews - 1);
H
Hongze Cheng 已提交
807

H
Hongze Cheng 已提交
808 809 810 811
          // set current new page right-most child
          ((SIntHdr *)pNews[iNew]->pData)->pgno = ((SPgno *)pCell)[0];

          // insert to parent as divider cell
812
          tAssert(iNew < nNews - 1);
H
Hongze Cheng 已提交
813 814
          ((SPgno *)pCell)[0] = TDB_PAGE_PGNO(pNews[iNew]);
          tdbPageInsertCell(pParent, sIdx++, pCell, szCell, 0);
H
Hongze Cheng 已提交
815 816 817 818

          // move to next new page
          iNew++;
          nNewCells = 0;
H
Hongze Cheng 已提交
819
          if (iNew < nNews) {
H
Hongze Cheng 已提交
820
            tdbBtreeInitPage(pNews[iNew], &iarg, 0);
H
Hongze Cheng 已提交
821
          }
H
Hongze Cheng 已提交
822 823
        }
      }
H
Hongze Cheng 已提交
824
    }
H
Hongze Cheng 已提交
825

H
Hongze Cheng 已提交
826
    if (childNotLeaf) {
827
      tAssert(TDB_PAGE_TOTAL_CELLS(pNews[nNews - 1]) == infoNews[nNews - 1].cnt);
H
Hongze Cheng 已提交
828
      ((SIntHdr *)(pNews[nNews - 1]->pData))->pgno = rPgno;
H
Hongze Cheng 已提交
829

H
Hongze Cheng 已提交
830 831 832 833 834 835
      SIntHdr *pIntHdr = (SIntHdr *)pParent->pData;
      if (pIntHdr->pgno == 0) {
        pIntHdr->pgno = TDB_PAGE_PGNO(pNews[nNews - 1]);
      } else {
        ((SPgno *)pDivCell[nOlds - 1])[0] = TDB_PAGE_PGNO(pNews[nNews - 1]);
        tdbPageInsertCell(pParent, sIdx, pDivCell[nOlds - 1], szDivCell[nOlds - 1], 0);
H
Hongze Cheng 已提交
836
      }
H
Hongze Cheng 已提交
837 838 839
    }

    for (int i = 0; i < nOlds; i++) {
H
Hongze Cheng 已提交
840
      tdbPageDestroy(pOldsCopy[i], tdbDefaultFree, NULL);
H
Hongze Cheng 已提交
841 842 843
    }
  }

H
Hongze Cheng 已提交
844 845 846 847
  if (TDB_BTREE_PAGE_IS_ROOT(pParent) && TDB_PAGE_TOTAL_CELLS(pParent) == 0) {
    i8 flags = TDB_BTREE_ROOT | TDB_BTREE_PAGE_IS_LEAF(pNews[0]);
    // copy content to the parent page
    tdbBtreeInitPage(pParent, &(SBtreeInitPageArg){.flags = flags, .pBt = pBt}, 0);
848
    tdbPageCopy(pNews[0], pParent, 1);
849 850 851 852

    if (!TDB_BTREE_PAGE_IS_LEAF(pNews[0])) {
      ((SIntHdr *)(pParent->pData))->pgno = ((SIntHdr *)(pNews[0]->pData))->pgno;
    }
H
Hongze Cheng 已提交
853 854
  }

H
Hongze Cheng 已提交
855 856
  for (int i = 0; i < 3; i++) {
    if (pDivCell[i]) {
H
Hongze Cheng 已提交
857
      tdbOsFree(pDivCell[i]);
H
Hongze Cheng 已提交
858 859
    }
  }
H
Hongze Cheng 已提交
860

M
Minglei Jin 已提交
861 862 863 864 865
  for (pageIdx = 0; pageIdx < nOlds; ++pageIdx) {
    tdbPagerReturnPage(pBt->pPager, pOlds[pageIdx], pTxn);
  }
  for (; pageIdx < nNews; ++pageIdx) {
    tdbPagerReturnPage(pBt->pPager, pNews[pageIdx], pTxn);
H
more  
Hongze Cheng 已提交
866 867
  }

H
Hongze Cheng 已提交
868
  return 0;
H
Hongze Cheng 已提交
869 870
}

H
Hongze Cheng 已提交
871
static int tdbBtreeBalance(SBTC *pBtc) {
H
Hongze Cheng 已提交
872
  int    iPage;
H
Hongze Cheng 已提交
873 874
  int    ret;
  int    nFree;
H
Hongze Cheng 已提交
875
  SPage *pParent;
H
Hongze Cheng 已提交
876
  SPage *pPage;
H
Hongze Cheng 已提交
877
  u8     flags;
H
Hongze Cheng 已提交
878 879
  u8     leaf;
  u8     root;
H
Hongze Cheng 已提交
880 881 882

  // Main loop to balance the BTree
  for (;;) {
H
Hongze Cheng 已提交
883 884
    iPage = pBtc->iPage;
    pPage = pBtc->pPage;
H
refact  
Hongze Cheng 已提交
885 886
    leaf = TDB_BTREE_PAGE_IS_LEAF(pPage);
    root = TDB_BTREE_PAGE_IS_ROOT(pPage);
H
Hongze Cheng 已提交
887
    nFree = TDB_PAGE_FREE_SIZE(pPage);
H
Hongze Cheng 已提交
888

H
Hongze Cheng 已提交
889 890
    // when the page is not overflow and not too empty, the balance work
    // is finished. Just break out the balance loop.
H
Hongze Cheng 已提交
891
    if (pPage->nOverflow == 0 && nFree < TDB_PAGE_USABLE_SIZE(pPage) * 2 / 3) {
H
Hongze Cheng 已提交
892 893 894 895
      break;
    }

    if (iPage == 0) {
H
Hongze Cheng 已提交
896 897 898
      // For the root page, only balance when the page is overfull,
      // ignore the case of empty
      if (pPage->nOverflow == 0) break;
H
Hongze Cheng 已提交
899

H
Hongze Cheng 已提交
900
      ret = tdbBtreeBalanceDeeper(pBtc->pBt, pPage, &(pBtc->pgStack[1]), pBtc->pTxn);
H
Hongze Cheng 已提交
901 902 903 904
      if (ret < 0) {
        return -1;
      }

H
Hongze Cheng 已提交
905 906 907 908 909
      pBtc->idx = 0;
      pBtc->idxStack[0] = 0;
      pBtc->pgStack[0] = pBtc->pPage;
      pBtc->iPage = 1;
      pBtc->pPage = pBtc->pgStack[1];
H
Hongze Cheng 已提交
910
    } else {
H
Hongze Cheng 已提交
911
      // Generalized balance step
H
Hongze Cheng 已提交
912
      pParent = pBtc->pgStack[iPage - 1];
H
Hongze Cheng 已提交
913

H
Hongze Cheng 已提交
914
      ret = tdbBtreeBalanceNonRoot(pBtc->pBt, pParent, pBtc->idxStack[pBtc->iPage - 1], pBtc->pTxn);
H
Hongze Cheng 已提交
915 916 917 918
      if (ret < 0) {
        return -1;
      }

H
Hongze Cheng 已提交
919
      tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn);
H
more  
Hongze Cheng 已提交
920

H
Hongze Cheng 已提交
921 922
      pBtc->iPage--;
      pBtc->pPage = pBtc->pgStack[pBtc->iPage];
H
Hongze Cheng 已提交
923 924 925 926 927
    }
  }

  return 0;
}
H
Hongze Cheng 已提交
928
// TDB_BTREE_BALANCE
H
Hongze Cheng 已提交
929

930
static int tdbFetchOvflPage(SPgno *pPgno, SPage **ppOfp, TXN *pTxn, SBTree *pBt) {
931 932 933 934 935 936
  int ret = 0;

  *pPgno = 0;
  SBtreeInitPageArg iArg;
  iArg.pBt = pBt;
  iArg.flags = TDB_FLAG_ADD(0, TDB_BTREE_OVFL);
937
  ret = tdbPagerFetchPage(pBt->pPager, pPgno, ppOfp, tdbBtreeInitPage, &iArg, pTxn);
938 939 940 941 942
  if (ret < 0) {
    return -1;
  }

  // mark dirty
943
  ret = tdbPagerWrite(pBt->pPager, *ppOfp);
944
  if (ret < 0) {
945
    tdbError("failed to write page since %s", terrstr());
946 947 948
    return -1;
  }

949 950
  tdbPCacheRelease(pBt->pPager->pCache, *ppOfp, pTxn);

951 952 953
  return ret;
}

954
static int tdbLoadOvflPage(SPgno *pPgno, SPage **ppOfp, TXN *pTxn, SBTree *pBt) {
955 956 957 958 959
  int ret = 0;

  SBtreeInitPageArg iArg;
  iArg.pBt = pBt;
  iArg.flags = TDB_FLAG_ADD(0, TDB_BTREE_OVFL);
960
  ret = tdbPagerFetchPage(pBt->pPager, pPgno, ppOfp, tdbBtreeInitPage, &iArg, pTxn);
961 962 963 964 965 966 967
  if (ret < 0) {
    return -1;
  }

  return ret;
}

H
Hongze Cheng 已提交
968
// TDB_BTREE_CELL =====================
H
Hongze Cheng 已提交
969
static int tdbBtreeEncodePayload(SPage *pPage, SCell *pCell, int nHeader, const void *pKey, int kLen, const void *pVal,
970 971 972 973
                                 int vLen, int *szPayload, TXN *pTxn, SBTree *pBt) {
  int ret = 0;
  int nPayload = kLen + vLen;
  int maxLocal = pPage->maxLocal;
H
Hongze Cheng 已提交
974

975
  if (nPayload + nHeader <= maxLocal) {
H
Hongze Cheng 已提交
976 977
    // no overflow page is needed
    memcpy(pCell + nHeader, pKey, kLen);
H
Hongze Cheng 已提交
978
    if (pVal) {
H
Hongze Cheng 已提交
979
      memcpy(pCell + nHeader + kLen, pVal, vLen);
H
Hongze Cheng 已提交
980 981 982 983
    }

    *szPayload = nPayload;
    return 0;
984 985 986 987 988 989
  } else {
    // handle overflow case
    // calc local storage size
    int minLocal = pPage->minLocal;
    int surplus = minLocal + (nPayload + nHeader - minLocal) % (maxLocal - sizeof(SPgno));
    int nLocal = surplus <= maxLocal ? surplus : minLocal;
H
Hongze Cheng 已提交
990

991
    // int ofpCap = tdbPageCapacity(pBt->pageSize, sizeof(SIntHdr));
992 993

    // fetch a new ofp and make it dirty
994
    SPgno  pgno = 0;
995
    SPage *ofp = NULL, *nextOfp = NULL;
996

997
    ret = tdbFetchOvflPage(&pgno, &ofp, pTxn, pBt);
998 999 1000 1001 1002
    if (ret < 0) {
      return -1;
    }

    // local buffer for cell
M
Minglei Jin 已提交
1003
    SCell *pBuf = tdbRealloc(NULL, pBt->pageSize);
1004 1005 1006 1007 1008 1009 1010
    if (pBuf == NULL) {
      return -1;
    }

    int nLeft = nPayload;
    int bytes;
    int lastPage = 0;
1011
    if (nLocal >= nHeader + kLen + sizeof(SPgno)) {
1012 1013 1014 1015
      // pack key to local
      memcpy(pCell + nHeader, pKey, kLen);
      nLeft -= kLen;
      // pack partial val to local if any space left
1016
      if (nLocal > nHeader + kLen + sizeof(SPgno)) {
1017
        tAssert(pVal != NULL && vLen != 0);
1018 1019
        memcpy(pCell + nHeader + kLen, pVal, nLocal - nHeader - kLen - sizeof(SPgno));
        nLeft -= nLocal - nHeader - kLen - sizeof(SPgno);
1020 1021 1022 1023 1024 1025 1026
      }

      // pack nextPgno
      memcpy(pCell + nHeader + nPayload - nLeft, &pgno, sizeof(pgno));

      // pack left val data to ovpages
      do {
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
        lastPage = 0;
        if (nLeft <= ofp->maxLocal - sizeof(SPgno)) {
          bytes = nLeft;
          lastPage = 1;
        } else {
          bytes = ofp->maxLocal - sizeof(SPgno);
        }

        // fetch next ofp if not last page
        if (!lastPage) {
          // fetch a new ofp and make it dirty
          ret = tdbFetchOvflPage(&pgno, &nextOfp, pTxn, pBt);
          if (ret < 0) {
            tdbFree(pBuf);
            return -1;
          }
        } else {
          pgno = 0;
        }

        memcpy(pBuf, ((SCell *)pVal) + vLen - nLeft, bytes);
        memcpy(pBuf + bytes, &pgno, sizeof(pgno));

        ret = tdbPageInsertCell(ofp, 0, pBuf, bytes + sizeof(pgno), 0);
        if (ret < 0) {
          tdbFree(pBuf);
          return -1;
        }

        ofp = nextOfp;
        nLeft -= bytes;
1058 1059 1060 1061 1062 1063
      } while (nLeft > 0);
    } else {
      int nLeftKey = kLen;
      // pack partial key and nextPgno
      memcpy(pCell + nHeader, pKey, nLocal - 4);
      nLeft -= nLocal - 4;
1064
      nLeftKey -= nLocal - 4;
1065 1066 1067 1068 1069 1070

      memcpy(pCell + nHeader + nLocal - 4, &pgno, sizeof(pgno));

      int lastKeyPageSpace = 0;
      // pack left key & val to ovpages
      do {
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
        // cal key to cpy
        int lastKeyPage = 0;
        if (nLeftKey <= ofp->maxLocal - sizeof(SPgno)) {
          bytes = nLeftKey;
          lastKeyPage = 1;
          lastKeyPageSpace = ofp->maxLocal - sizeof(SPgno) - nLeftKey;
        } else {
          bytes = ofp->maxLocal - sizeof(SPgno);
        }

        // cpy key
        memcpy(pBuf, ((SCell *)pKey) + kLen - nLeftKey, bytes);

        if (lastKeyPage) {
          if (lastKeyPageSpace >= vLen) {
            memcpy(pBuf + kLen - nLeftKey, pVal, vLen);

            nLeft -= vLen;
            pgno = 0;
          } else {
            memcpy(pBuf + kLen - nLeftKey, pVal, lastKeyPageSpace);
            nLeft -= lastKeyPageSpace;

            // fetch next ofp, a new ofp and make it dirty
            ret = tdbFetchOvflPage(&pgno, &nextOfp, pTxn, pBt);
            if (ret < 0) {
M
Minglei Jin 已提交
1097
              tdbFree(pBuf);
1098 1099 1100 1101 1102 1103 1104
              return -1;
            }
          }
        } else {
          // fetch next ofp, a new ofp and make it dirty
          ret = tdbFetchOvflPage(&pgno, &nextOfp, pTxn, pBt);
          if (ret < 0) {
M
Minglei Jin 已提交
1105
            tdbFree(pBuf);
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
            return -1;
          }
        }

        memcpy(pBuf + kLen - nLeft, &pgno, sizeof(pgno));

        ret = tdbPageInsertCell(ofp, 0, pBuf, bytes + sizeof(pgno), 0);
        if (ret < 0) {
          return -1;
        }

        ofp = nextOfp;
        nLeftKey -= bytes;
        nLeft -= bytes;
1120 1121 1122
      } while (nLeftKey > 0);

      while (nLeft > 0) {
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146
        // pack left val data to ovpages
        lastPage = 0;
        if (nLeft <= maxLocal - sizeof(SPgno)) {
          bytes = nLeft;
          lastPage = 1;
        } else {
          bytes = maxLocal - sizeof(SPgno);
        }

        // fetch next ofp if not last page
        if (!lastPage) {
          // fetch a new ofp and make it dirty
          ret = tdbFetchOvflPage(&pgno, &nextOfp, pTxn, pBt);
          if (ret < 0) {
            tdbFree(pBuf);
            return -1;
          }
        } else {
          pgno = 0;
        }

        memcpy(pBuf, ((SCell *)pVal) + vLen - nLeft, bytes);
        memcpy(pBuf + bytes, &pgno, sizeof(pgno));

M
Minglei Jin 已提交
1147 1148 1149 1150 1151
        if (ofp == NULL) {
          tdbFree(pBuf);
          return -1;
        }

1152 1153 1154 1155 1156 1157 1158 1159
        ret = tdbPageInsertCell(ofp, 0, pBuf, bytes + sizeof(pgno), 0);
        if (ret < 0) {
          tdbFree(pBuf);
          return -1;
        }

        ofp = nextOfp;
        nLeft -= bytes;
1160 1161 1162 1163 1164 1165
      }
    }

    // free local buffer
    tdbFree(pBuf);

1166
    *szPayload = nLocal - nHeader;
H
Hongze Cheng 已提交
1167 1168 1169 1170 1171
  }

  return 0;
}

H
Hongze Cheng 已提交
1172
static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const void *pVal, int vLen, SCell *pCell,
1173
                              int *szCell, TXN *pTxn, SBTree *pBt) {
H
Hongze Cheng 已提交
1174 1175 1176 1177
  u8  leaf;
  int nHeader;
  int nPayload;
  int ret;
H
Hongze Cheng 已提交
1178

1179 1180 1181
  tAssert(pPage->kLen == TDB_VARIANT_LEN || pPage->kLen == kLen);
  tAssert(pPage->vLen == TDB_VARIANT_LEN || pPage->vLen == vLen);
  tAssert(pKey != NULL && kLen > 0);
H
Hongze Cheng 已提交
1182

H
Hongze Cheng 已提交
1183 1184
  nPayload = 0;
  nHeader = 0;
H
refact  
Hongze Cheng 已提交
1185
  leaf = TDB_BTREE_PAGE_IS_LEAF(pPage);
H
Hongze Cheng 已提交
1186

H
Hongze Cheng 已提交
1187
  // 1. Encode Header part
H
Hongze Cheng 已提交
1188 1189
  /* Encode SPgno if interior page */
  if (!leaf) {
1190
    tAssert(pPage->vLen == sizeof(SPgno));
H
Hongze Cheng 已提交
1191 1192 1193 1194 1195

    ((SPgno *)(pCell + nHeader))[0] = ((SPgno *)pVal)[0];
    nHeader = nHeader + sizeof(SPgno);
  }

H
Hongze Cheng 已提交
1196
  /* Encode kLen if need */
H
Hongze Cheng 已提交
1197
  if (pPage->kLen == TDB_VARIANT_LEN) {
H
Hongze Cheng 已提交
1198
    nHeader += tdbPutVarInt(pCell + nHeader, kLen);
H
Hongze Cheng 已提交
1199 1200
  }

H
Hongze Cheng 已提交
1201
  /* Encode vLen if need */
H
Hongze Cheng 已提交
1202
  if (pPage->vLen == TDB_VARIANT_LEN) {
H
Hongze Cheng 已提交
1203
    nHeader += tdbPutVarInt(pCell + nHeader, vLen);
H
Hongze Cheng 已提交
1204 1205
  }

H
Hongze Cheng 已提交
1206
  // 2. Encode payload part
H
Hongze Cheng 已提交
1207 1208 1209
  if ((!leaf) || pPage->vLen == 0) {
    pVal = NULL;
    vLen = 0;
H
Hongze Cheng 已提交
1210
  }
H
Hongze Cheng 已提交
1211

1212
  ret = tdbBtreeEncodePayload(pPage, pCell, nHeader, pKey, kLen, pVal, vLen, &nPayload, pTxn, pBt);
H
Hongze Cheng 已提交
1213
  if (ret < 0) {
H
Hongze Cheng 已提交
1214
    // TODO
1215
    tAssert(0);
H
Hongze Cheng 已提交
1216
    return 0;
H
Hongze Cheng 已提交
1217 1218
  }

H
Hongze Cheng 已提交
1219
  *szCell = nHeader + nPayload;
H
Hongze Cheng 已提交
1220 1221 1222
  return 0;
}

1223 1224
static int tdbBtreeDecodePayload(SPage *pPage, const SCell *pCell, int nHeader, SCellDecoder *pDecoder, TXN *pTxn,
                                 SBTree *pBt) {
1225
  int ret = 0;
H
Hongze Cheng 已提交
1226
  int nPayload;
1227 1228 1229 1230
  int maxLocal = pPage->maxLocal;

  int kLen = pDecoder->kLen;
  int vLen = pDecoder->vLen;
H
Hongze Cheng 已提交
1231 1232

  if (pDecoder->pVal) {
1233
    tAssert(!TDB_BTREE_PAGE_IS_LEAF(pPage));
H
Hongze Cheng 已提交
1234
    nPayload = pDecoder->kLen;
H
Hongze Cheng 已提交
1235 1236
  } else {
    nPayload = pDecoder->kLen + pDecoder->vLen;
H
Hongze Cheng 已提交
1237 1238
  }

1239
  if (nHeader + nPayload <= maxLocal) {
H
Hongze Cheng 已提交
1240
    // no over flow case
1241
    pDecoder->pKey = (SCell *)pCell + nHeader;
H
Hongze Cheng 已提交
1242
    if (pDecoder->pVal == NULL && pDecoder->vLen > 0) {
1243
      pDecoder->pVal = (SCell *)pCell + nHeader + pDecoder->kLen;
H
Hongze Cheng 已提交
1244
    }
H
Hongze Cheng 已提交
1245
    return 0;
1246 1247 1248 1249 1250 1251 1252
  } else {
    // handle overflow case
    // calc local storage size
    int minLocal = pPage->minLocal;
    int surplus = minLocal + (nPayload + nHeader - minLocal) % (maxLocal - sizeof(SPgno));
    int nLocal = surplus <= maxLocal ? surplus : minLocal;

1253 1254
    int    nLeft = nPayload;
    SPgno  pgno = 0;
1255 1256
    SPage *ofp;
    SCell *ofpCell;
1257 1258
    int    bytes;
    int    lastPage = 0;
1259

1260
    if (nLocal >= pDecoder->kLen + nHeader + sizeof(SPgno)) {
1261 1262
      pDecoder->pKey = (SCell *)pCell + nHeader;
      nLeft -= kLen;
1263
      if (nLocal > kLen + nHeader + sizeof(SPgno)) {
1264 1265 1266 1267 1268 1269
        // read partial val to local
        pDecoder->pVal = tdbRealloc(pDecoder->pVal, vLen);
        if (pDecoder->pVal == NULL) {
          return -1;
        }
        TDB_CELLDECODER_SET_FREE_VAL(pDecoder);
1270

1271 1272
        tdbDebug("tdb btc decoder: %p/0x%x pVal: %p ", pDecoder, pDecoder->freeKV, pDecoder->pVal);

1273
        memcpy(pDecoder->pVal, pCell + nHeader + kLen, nLocal - nHeader - kLen - sizeof(SPgno));
1274

1275
        nLeft -= nLocal - nHeader - kLen - sizeof(SPgno);
1276
      }
H
Hongze Cheng 已提交
1277

1278 1279 1280
      memcpy(&pgno, pCell + nHeader + nPayload - nLeft, sizeof(pgno));

      // unpack left val data from ovpages
1281 1282 1283 1284 1285
      while (pgno != 0) {
        ret = tdbLoadOvflPage(&pgno, &ofp, pTxn, pBt);
        if (ret < 0) {
          return -1;
        }
1286

1287
        ofpCell = tdbPageGetCell(ofp, 0);
1288

1289 1290 1291 1292 1293 1294
        if (nLeft <= ofp->maxLocal - sizeof(SPgno)) {
          bytes = nLeft;
          lastPage = 1;
        } else {
          bytes = ofp->maxLocal - sizeof(SPgno);
        }
1295

1296 1297
        memcpy(pDecoder->pVal + vLen - nLeft, ofpCell, bytes);
        nLeft -= bytes;
1298

1299
        memcpy(&pgno, ofpCell + bytes, sizeof(pgno));
1300 1301

        tdbPCacheRelease(pBt->pPager->pCache, ofp, pTxn);
1302 1303 1304 1305 1306 1307
      }
    } else {
      int nLeftKey = kLen;
      // load partial key and nextPgno
      pDecoder->pKey = tdbRealloc(pDecoder->pKey, kLen);
      if (pDecoder->pKey == NULL) {
1308
        return -1;
1309 1310 1311 1312 1313
      }
      TDB_CELLDECODER_SET_FREE_KEY(pDecoder);

      memcpy(pDecoder->pKey, pCell + nHeader, nLocal - 4);
      nLeft -= nLocal - 4;
1314
      nLeftKey -= nLocal - 4;
1315 1316 1317 1318 1319 1320

      memcpy(&pgno, pCell + nHeader + nLocal - 4, sizeof(pgno));

      int lastKeyPageSpace = 0;
      // load left key & val to ovpages
      while (pgno != 0) {
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
        ret = tdbLoadOvflPage(&pgno, &ofp, pTxn, pBt);
        if (ret < 0) {
          return -1;
        }

        ofpCell = tdbPageGetCell(ofp, 0);

        int lastKeyPage = 0;
        if (nLeftKey <= maxLocal - sizeof(SPgno)) {
          bytes = nLeftKey;
          lastKeyPage = 1;
          lastKeyPageSpace = ofp->maxLocal - sizeof(SPgno) - nLeftKey;
        } else {
          bytes = ofp->maxLocal - sizeof(SPgno);
        }

        // cpy key
        memcpy(pDecoder->pKey + kLen - nLeftKey, ofpCell, bytes);

        if (lastKeyPage) {
          if (lastKeyPageSpace >= vLen) {
            pDecoder->pVal = ofpCell + kLen - nLeftKey;

            nLeft -= vLen;
            pgno = 0;
          } else {
            // read partial val to local
            pDecoder->pVal = tdbRealloc(pDecoder->pVal, vLen);
            if (pDecoder->pVal == NULL) {
              return -1;
            }
            TDB_CELLDECODER_SET_FREE_VAL(pDecoder);

            memcpy(pDecoder->pVal, ofpCell + kLen - nLeftKey, lastKeyPageSpace);
            nLeft -= lastKeyPageSpace;
          }
        }

        memcpy(&pgno, ofpCell + bytes, sizeof(pgno));

1361 1362
        tdbPCacheRelease(pBt->pPager->pCache, ofp, pTxn);

1363 1364
        nLeftKey -= bytes;
        nLeft -= bytes;
1365 1366 1367
      }

      while (nLeft > 0) {
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
        ret = tdbLoadOvflPage(&pgno, &ofp, pTxn, pBt);
        if (ret < 0) {
          return -1;
        }

        ofpCell = tdbPageGetCell(ofp, 0);

        // load left val data to ovpages
        lastPage = 0;
        if (nLeft <= ofp->maxLocal - sizeof(SPgno)) {
          bytes = nLeft;
          lastPage = 1;
        } else {
          bytes = ofp->maxLocal - sizeof(SPgno);
        }

        if (lastPage) {
          pgno = 0;
        }

        if (!pDecoder->pVal) {
          pDecoder->pVal = tdbRealloc(pDecoder->pVal, vLen);
          if (pDecoder->pVal == NULL) {
            return -1;
          }
          TDB_CELLDECODER_SET_FREE_VAL(pDecoder);
        }

        memcpy(pDecoder->pVal, ofpCell + vLen - nLeft, bytes);
        nLeft -= bytes;

        memcpy(&pgno, ofpCell + vLen - nLeft + bytes, sizeof(pgno));

1401 1402
        tdbPCacheRelease(pBt->pPager->pCache, ofp, pTxn);

1403
        nLeft -= bytes;
1404 1405
      }
    }
H
Hongze Cheng 已提交
1406 1407
  }

H
Hongze Cheng 已提交
1408 1409 1410
  return 0;
}

1411
static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pDecoder, TXN *pTxn, SBTree *pBt) {
H
Hongze Cheng 已提交
1412 1413 1414 1415 1416
  u8  leaf;
  int nHeader;
  int ret;

  nHeader = 0;
H
refact  
Hongze Cheng 已提交
1417
  leaf = TDB_BTREE_PAGE_IS_LEAF(pPage);
H
Hongze Cheng 已提交
1418

H
Hongze Cheng 已提交
1419
  // Clear the state of decoder
1420 1421 1422
  if (TDB_CELLDECODER_FREE_VAL(pDecoder)) {
    tdbFree(pDecoder->pVal);
  }
H
Hongze Cheng 已提交
1423 1424 1425 1426 1427
  pDecoder->kLen = -1;
  pDecoder->pKey = NULL;
  pDecoder->vLen = -1;
  pDecoder->pVal = NULL;
  pDecoder->pgno = 0;
1428
  TDB_CELLDECODER_SET_FREE_NIL(pDecoder);
H
Hongze Cheng 已提交
1429

1430
  // tdbTrace("tdb btc decoder set nil: %p/0x%x ", pDecoder, pDecoder->freeKV);
1431

H
Hongze Cheng 已提交
1432
  // 1. Decode header part
H
Hongze Cheng 已提交
1433
  if (!leaf) {
1434
    tAssert(pPage->vLen == sizeof(SPgno));
H
Hongze Cheng 已提交
1435 1436 1437 1438 1439 1440

    pDecoder->pgno = ((SPgno *)(pCell + nHeader))[0];
    pDecoder->pVal = (u8 *)(&(pDecoder->pgno));
    nHeader = nHeader + sizeof(SPgno);
  }

H
Hongze Cheng 已提交
1441 1442 1443 1444 1445 1446 1447
  if (pPage->kLen == TDB_VARIANT_LEN) {
    nHeader += tdbGetVarInt(pCell + nHeader, &(pDecoder->kLen));
  } else {
    pDecoder->kLen = pPage->kLen;
  }

  if (pPage->vLen == TDB_VARIANT_LEN) {
1448
    tAssert(leaf);
H
Hongze Cheng 已提交
1449
    nHeader += tdbGetVarInt(pCell + nHeader, &(pDecoder->vLen));
H
Hongze Cheng 已提交
1450
  } else {
H
Hongze Cheng 已提交
1451 1452
    pDecoder->vLen = pPage->vLen;
  }
H
Hongze Cheng 已提交
1453

H
Hongze Cheng 已提交
1454
  // 2. Decode payload part
1455
  ret = tdbBtreeDecodePayload(pPage, pCell, nHeader, pDecoder, pTxn, pBt);
H
Hongze Cheng 已提交
1456 1457
  if (ret < 0) {
    return -1;
H
Hongze Cheng 已提交
1458 1459 1460 1461 1462
  }

  return 0;
}

1463
static int tdbBtreeCellSize(const SPage *pPage, SCell *pCell, int dropOfp, TXN *pTxn, SBTree *pBt) {
H
refact  
Hongze Cheng 已提交
1464
  u8  leaf;
1465
  int kLen = 0, vLen = 0, nHeader = 0;
H
Hongze Cheng 已提交
1466

H
refact  
Hongze Cheng 已提交
1467
  leaf = TDB_BTREE_PAGE_IS_LEAF(pPage);
H
Hongze Cheng 已提交
1468

H
refact  
Hongze Cheng 已提交
1469
  if (!leaf) {
1470
    nHeader += sizeof(SPgno);
H
Hongze Cheng 已提交
1471 1472 1473
  }

  if (pPage->kLen == TDB_VARIANT_LEN) {
1474
    nHeader += tdbGetVarInt(pCell + nHeader, &kLen);
H
Hongze Cheng 已提交
1475 1476 1477 1478
  } else {
    kLen = pPage->kLen;
  }

H
refact  
Hongze Cheng 已提交
1479
  if (pPage->vLen == TDB_VARIANT_LEN) {
1480
    tAssert(leaf);
1481
    nHeader += tdbGetVarInt(pCell + nHeader, &vLen);
H
refact  
Hongze Cheng 已提交
1482 1483
  } else if (leaf) {
    vLen = pPage->vLen;
H
Hongze Cheng 已提交
1484 1485
  }

1486 1487
  int nPayload = kLen + vLen;
  if (nHeader + nPayload <= pPage->maxLocal) {
1488
    return nHeader + nPayload;
1489 1490
  } else {
    int maxLocal = pPage->maxLocal;
H
Hongze Cheng 已提交
1491

1492 1493 1494 1495
    // calc local storage size
    int minLocal = pPage->minLocal;
    int surplus = minLocal + (nPayload + nHeader - minLocal) % (maxLocal - sizeof(SPgno));
    int nLocal = surplus <= maxLocal ? surplus : minLocal;
H
refact  
Hongze Cheng 已提交
1496

1497 1498
    // free ofp pages' cells
    if (dropOfp) {
1499
      int    ret = 0;
1500
      SPgno  pgno = *(SPgno *)(pCell + nLocal - sizeof(SPgno));
1501
      int    nLeft = nPayload - nLocal + sizeof(SPgno) + nHeader;
1502
      SPage *ofp;
1503
      int    bytes;
1504 1505

      while (pgno != 0) {
1506 1507 1508 1509
        ret = tdbLoadOvflPage(&pgno, &ofp, pTxn, pBt);
        if (ret < 0) {
          return -1;
        }
1510

1511
        SCell *ofpCell = tdbPageGetCell(ofp, 0);
1512

1513 1514 1515 1516 1517
        if (nLeft <= ofp->maxLocal - sizeof(SPgno)) {
          bytes = nLeft;
        } else {
          bytes = ofp->maxLocal - sizeof(SPgno);
        }
1518

1519
        memcpy(&pgno, ofpCell + bytes, sizeof(pgno));
1520

1521
        tdbPagerReturnPage(pPage->pPager, ofp, pTxn);
1522

1523
        nLeft -= bytes;
1524 1525 1526
      }
    }

1527
    return nLocal;
H
refact  
Hongze Cheng 已提交
1528
  }
H
Hongze Cheng 已提交
1529
}
H
Hongze Cheng 已提交
1530
// TDB_BTREE_CELL
H
Hongze Cheng 已提交
1531

H
refact  
Hongze Cheng 已提交
1532
// TDB_BTREE_CURSOR =====================
H
Hongze Cheng 已提交
1533
int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn) {
H
Hongze Cheng 已提交
1534 1535 1536 1537
  pBtc->pBt = pBt;
  pBtc->iPage = -1;
  pBtc->pPage = NULL;
  pBtc->idx = -1;
H
Hongze Cheng 已提交
1538
  memset(&pBtc->coder, 0, sizeof(SCellDecoder));
H
Hongze Cheng 已提交
1539 1540

  if (pTxn == NULL) {
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552
    TXN *pTxn = tdbOsCalloc(1, sizeof(*pTxn));
    if (!pTxn) {
      return -1;
    }

    if (tdbTxnOpen(pTxn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, 0) < 0) {
      tdbOsFree(pTxn);
      return -1;
    }

    pBtc->pTxn = pTxn;
    pBtc->freeTxn = 1;
H
Hongze Cheng 已提交
1553 1554
  } else {
    pBtc->pTxn = pTxn;
1555
    pBtc->freeTxn = 0;
H
Hongze Cheng 已提交
1556
  }
H
Hongze Cheng 已提交
1557 1558 1559 1560

  return 0;
}

H
Hongze Cheng 已提交
1561
int tdbBtcMoveToFirst(SBTC *pBtc) {
H
refact  
Hongze Cheng 已提交
1562 1563 1564
  int     ret;
  SBTree *pBt;
  SPager *pPager;
H
Hongze Cheng 已提交
1565 1566
  SCell  *pCell;
  SPgno   pgno;
H
refact  
Hongze Cheng 已提交
1567 1568 1569 1570 1571

  pBt = pBtc->pBt;
  pPager = pBt->pPager;

  if (pBtc->iPage < 0) {
H
Hongze Cheng 已提交
1572
    // move a clean cursor
H
Hongze Cheng 已提交
1573
    ret = tdbPagerFetchPage(pPager, &pBt->root, &(pBtc->pPage), tdbBtreeInitPage,
H
Hongze Cheng 已提交
1574
                            &((SBtreeInitPageArg){.pBt = pBt, .flags = TDB_BTREE_ROOT | TDB_BTREE_LEAF}), pBtc->pTxn);
H
refact  
Hongze Cheng 已提交
1575
    if (ret < 0) {
1576
      tAssert(0);
H
refact  
Hongze Cheng 已提交
1577 1578 1579
      return -1;
    }

1580
    tAssert(TDB_BTREE_PAGE_IS_ROOT(pBtc->pPage));
H
refact  
Hongze Cheng 已提交
1581

H
Hongze Cheng 已提交
1582 1583 1584 1585
    pBtc->iPage = 0;
    if (TDB_PAGE_TOTAL_CELLS(pBtc->pPage) > 0) {
      pBtc->idx = 0;
    } else {
H
more  
Hongze Cheng 已提交
1586
      // no any data, point to an invalid position
1587
      tAssert(TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
H
Hongze Cheng 已提交
1588
      pBtc->idx = -1;
H
refact  
Hongze Cheng 已提交
1589 1590
      return 0;
    }
H
Hongze Cheng 已提交
1591
  } else {
1592
    tAssert(0);
H
Hongze Cheng 已提交
1593
#if 0
H
Hongze Cheng 已提交
1594 1595
    // move from a position
    int iPage = 0;
H
refact  
Hongze Cheng 已提交
1596

H
Hongze Cheng 已提交
1597
    for (; iPage < pBtc->iPage; iPage++) {
1598
      tAssert(pBtc->idxStack[iPage] >= 0);
H
Hongze Cheng 已提交
1599 1600
      if (pBtc->idxStack[iPage]) break;
    }
H
refact  
Hongze Cheng 已提交
1601

H
Hongze Cheng 已提交
1602 1603
    // move upward
    for (;;) {
H
Hongze Cheng 已提交
1604
      if (pBtc->iPage == iPage) {
H
Hongze Cheng 已提交
1605
        pBtc->idx = 0;
H
refact  
Hongze Cheng 已提交
1606 1607
        break;
      }
H
Hongze Cheng 已提交
1608

H
Hongze Cheng 已提交
1609
      tdbBtcMoveUpward(pBtc);
H
Hongze Cheng 已提交
1610
    }
H
Hongze Cheng 已提交
1611
#endif
H
Hongze Cheng 已提交
1612 1613 1614 1615
  }

  // move downward
  for (;;) {
H
refact  
Hongze Cheng 已提交
1616
    if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break;
H
Hongze Cheng 已提交
1617

H
Hongze Cheng 已提交
1618
    ret = tdbBtcMoveDownward(pBtc);
H
Hongze Cheng 已提交
1619
    if (ret < 0) {
1620
      tAssert(0);
H
Hongze Cheng 已提交
1621 1622 1623 1624 1625 1626
      return -1;
    }

    pBtc->idx = 0;
  }

H
Hongze Cheng 已提交
1627 1628 1629 1630
  return 0;
}

int tdbBtcMoveToLast(SBTC *pBtc) {
H
Hongze Cheng 已提交
1631
  int     ret;
H
Hongze Cheng 已提交
1632
  int     nCells;
H
Hongze Cheng 已提交
1633 1634 1635 1636 1637 1638 1639 1640 1641
  SBTree *pBt;
  SPager *pPager;
  SPgno   pgno;

  pBt = pBtc->pBt;
  pPager = pBt->pPager;

  if (pBtc->iPage < 0) {
    // move a clean cursor
H
Hongze Cheng 已提交
1642
    ret = tdbPagerFetchPage(pPager, &pBt->root, &(pBtc->pPage), tdbBtreeInitPage,
H
Hongze Cheng 已提交
1643
                            &((SBtreeInitPageArg){.pBt = pBt, .flags = TDB_BTREE_ROOT | TDB_BTREE_LEAF}), pBtc->pTxn);
H
Hongze Cheng 已提交
1644
    if (ret < 0) {
1645
      tAssert(0);
H
Hongze Cheng 已提交
1646 1647 1648
      return -1;
    }

H
Hongze Cheng 已提交
1649
    nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
H
Hongze Cheng 已提交
1650
    pBtc->iPage = 0;
H
Hongze Cheng 已提交
1651 1652 1653 1654
    if (nCells > 0) {
      pBtc->idx = TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage) ? nCells - 1 : nCells;
    } else {
      // no data at all, point to an invalid position
1655
      tAssert(TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
H
Hongze Cheng 已提交
1656 1657 1658
      pBtc->idx = -1;
      return 0;
    }
H
Hongze Cheng 已提交
1659
  } else {
1660
    tAssert(0);
H
Hongze Cheng 已提交
1661
#if 0
H
Hongze Cheng 已提交
1662 1663 1664 1665
    int iPage = 0;

    // downward search
    for (; iPage < pBtc->iPage; iPage++) {
1666
      tAssert(!TDB_BTREE_PAGE_IS_LEAF(pBtc->pgStack[iPage]));
H
Hongze Cheng 已提交
1667 1668 1669 1670 1671 1672
      nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pgStack[iPage]);
      if (pBtc->idxStack[iPage] != nCells) break;
    }

    // move upward
    for (;;) {
H
Hongze Cheng 已提交
1673
      if (pBtc->iPage == iPage) {
H
Hongze Cheng 已提交
1674 1675 1676 1677 1678
        if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) {
          pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage) - 1;
        } else {
          pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
        }
H
Hongze Cheng 已提交
1679
        break;
H
Hongze Cheng 已提交
1680 1681 1682 1683
      }

      tdbBtcMoveUpward(pBtc);
    }
H
Hongze Cheng 已提交
1684
#endif
H
Hongze Cheng 已提交
1685 1686 1687 1688
  }

  // move downward
  for (;;) {
H
Hongze Cheng 已提交
1689 1690 1691 1692
    if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break;

    ret = tdbBtcMoveDownward(pBtc);
    if (ret < 0) {
1693
      tAssert(0);
H
Hongze Cheng 已提交
1694 1695 1696 1697
      return -1;
    }

    nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
H
refact  
Hongze Cheng 已提交
1698
    if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) {
H
Hongze Cheng 已提交
1699
      pBtc->idx = nCells - 1;
H
Hongze Cheng 已提交
1700
    } else {
H
Hongze Cheng 已提交
1701
      pBtc->idx = nCells;
H
Hongze Cheng 已提交
1702 1703 1704
    }
  }

H
Hongze Cheng 已提交
1705 1706 1707 1708
  return 0;
}

int tdbBtreeNext(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
H
Hongze Cheng 已提交
1709
  SCell       *pCell;
1710
  SCellDecoder cd = {0};
H
Hongze Cheng 已提交
1711 1712 1713
  void        *pKey, *pVal;
  int          ret;

H
Hongze Cheng 已提交
1714
  // current cursor points to an invalid position
H
Hongze Cheng 已提交
1715
  if (pBtc->idx < 0) {
H
Hongze Cheng 已提交
1716 1717 1718 1719 1720
    return -1;
  }

  pCell = tdbPageGetCell(pBtc->pPage, pBtc->idx);

1721
  tdbBtreeDecodeCell(pBtc->pPage, pCell, &cd, pBtc->pTxn, pBtc->pBt);
H
Hongze Cheng 已提交
1722

H
Hongze Cheng 已提交
1723
  pKey = tdbRealloc(*ppKey, cd.kLen);
H
Hongze Cheng 已提交
1724 1725 1726 1727 1728 1729 1730
  if (pKey == NULL) {
    return -1;
  }

  *ppKey = pKey;
  *kLen = cd.kLen;
  memcpy(pKey, cd.pKey, cd.kLen);
C
Cary Xu 已提交
1731 1732

  if (ppVal) {
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742
    if (cd.vLen > 0) {
      pVal = tdbRealloc(*ppVal, cd.vLen);
      if (pVal == NULL) {
        tdbFree(pKey);
        return -1;
      }

      memcpy(pVal, cd.pVal, cd.vLen);
    } else {
      pVal = NULL;
C
Cary Xu 已提交
1743 1744 1745 1746 1747
    }

    *ppVal = pVal;
    *vLen = cd.vLen;
  }
H
Hongze Cheng 已提交
1748 1749

  ret = tdbBtcMoveToNext(pBtc);
H
Hongze Cheng 已提交
1750
  if (ret < 0) {
1751
    tAssert(0);
H
Hongze Cheng 已提交
1752 1753
    return -1;
  }
H
Hongze Cheng 已提交
1754 1755 1756 1757

  return 0;
}

1758 1759
int tdbBtreePrev(SBTC *pBtc, void **ppKey, int *kLen, void **ppVal, int *vLen) {
  SCell       *pCell;
M
Minglei Jin 已提交
1760
  SCellDecoder cd = {0};
1761 1762 1763 1764 1765 1766 1767 1768 1769 1770
  void        *pKey, *pVal;
  int          ret;

  // current cursor points to an invalid position
  if (pBtc->idx < 0) {
    return -1;
  }

  pCell = tdbPageGetCell(pBtc->pPage, pBtc->idx);

1771
  tdbBtreeDecodeCell(pBtc->pPage, pCell, &cd, pBtc->pTxn, pBtc->pBt);
1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

  pKey = tdbRealloc(*ppKey, cd.kLen);
  if (pKey == NULL) {
    return -1;
  }

  *ppKey = pKey;
  *kLen = cd.kLen;
  memcpy(pKey, cd.pKey, cd.kLen);

  if (ppVal) {
    // TODO: vLen may be zero
    pVal = tdbRealloc(*ppVal, cd.vLen);
    if (pVal == NULL) {
      tdbFree(pKey);
      return -1;
    }

    *ppVal = pVal;
    *vLen = cd.vLen;
    memcpy(pVal, cd.pVal, cd.vLen);
  }

  ret = tdbBtcMoveToPrev(pBtc);
  if (ret < 0) {
1797
    tAssert(0);
1798 1799 1800 1801 1802 1803
    return -1;
  }

  return 0;
}

H
Hongze Cheng 已提交
1804
int tdbBtcMoveToNext(SBTC *pBtc) {
H
Hongze Cheng 已提交
1805
  int    nCells;
H
Hongze Cheng 已提交
1806
  int    ret;
H
Hongze Cheng 已提交
1807 1808
  SCell *pCell;

1809
  tAssert(TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
H
Hongze Cheng 已提交
1810 1811 1812

  if (pBtc->idx < 0) return -1;

H
Hongze Cheng 已提交
1813
  pBtc->idx++;
H
Hongze Cheng 已提交
1814 1815 1816 1817
  if (pBtc->idx < TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) {
    return 0;
  }

H
Hongze Cheng 已提交
1818
  // move upward
H
Hongze Cheng 已提交
1819
  for (;;) {
H
Hongze Cheng 已提交
1820 1821 1822 1823 1824
    if (pBtc->iPage == 0) {
      pBtc->idx = -1;
      return 0;
    }

H
Hongze Cheng 已提交
1825 1826 1827
    tdbBtcMoveUpward(pBtc);
    pBtc->idx++;

1828
    tAssert(!TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
H
Hongze Cheng 已提交
1829
    if (pBtc->idx <= TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) {
H
Hongze Cheng 已提交
1830 1831
      break;
    }
H
Hongze Cheng 已提交
1832 1833
  }

H
Hongze Cheng 已提交
1834
  // move downward
H
Hongze Cheng 已提交
1835
  for (;;) {
H
Hongze Cheng 已提交
1836
    if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break;
H
Hongze Cheng 已提交
1837

H
Hongze Cheng 已提交
1838 1839
    ret = tdbBtcMoveDownward(pBtc);
    if (ret < 0) {
1840
      tAssert(0);
H
Hongze Cheng 已提交
1841
      return -1;
H
Hongze Cheng 已提交
1842
    }
H
Hongze Cheng 已提交
1843 1844

    pBtc->idx = 0;
H
Hongze Cheng 已提交
1845 1846
  }

H
Hongze Cheng 已提交
1847 1848 1849
  return 0;
}

H
Hongze Cheng 已提交
1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886
int tdbBtcMoveToPrev(SBTC *pBtc) {
  if (pBtc->idx < 0) return -1;

  pBtc->idx--;
  if (pBtc->idx >= 0) {
    return 0;
  }

  // move upward
  for (;;) {
    if (pBtc->iPage == 0) {
      pBtc->idx = -1;
      return 0;
    }

    tdbBtcMoveUpward(pBtc);
    pBtc->idx--;
    if (pBtc->idx >= 0) {
      break;
    }
  }

  // move downward
  for (;;) {
    if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) break;

    tdbBtcMoveDownward(pBtc);
    if (TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage)) {
      pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage) - 1;
    } else {
      pBtc->idx = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
1887 1888 1889 1890 1891
static int tdbBtcMoveDownward(SBTC *pBtc) {
  int    ret;
  SPgno  pgno;
  SCell *pCell;

1892 1893
  tAssert(pBtc->idx >= 0);
  tAssert(!TDB_BTREE_PAGE_IS_LEAF(pBtc->pPage));
H
Hongze Cheng 已提交
1894 1895 1896 1897 1898 1899 1900

  if (pBtc->idx < TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) {
    pCell = tdbPageGetCell(pBtc->pPage, pBtc->idx);
    pgno = ((SPgno *)pCell)[0];
  } else {
    pgno = ((SIntHdr *)pBtc->pPage->pData)->pgno;
  }
H
Hongze Cheng 已提交
1901

1902
  tAssert(pgno);
H
Hongze Cheng 已提交
1903

H
Hongze Cheng 已提交
1904 1905 1906 1907 1908
  pBtc->pgStack[pBtc->iPage] = pBtc->pPage;
  pBtc->idxStack[pBtc->iPage] = pBtc->idx;
  pBtc->iPage++;
  pBtc->pPage = NULL;
  pBtc->idx = -1;
H
Hongze Cheng 已提交
1909

H
Hongze Cheng 已提交
1910 1911
  ret = tdbPagerFetchPage(pBtc->pBt->pPager, &pgno, &pBtc->pPage, tdbBtreeInitPage,
                          &((SBtreeInitPageArg){.pBt = pBtc->pBt, .flags = 0}), pBtc->pTxn);
H
Hongze Cheng 已提交
1912
  if (ret < 0) {
1913
    tAssert(0);
H
Hongze Cheng 已提交
1914
    return -1;
H
Hongze Cheng 已提交
1915 1916 1917 1918 1919 1920
  }

  return 0;
}

static int tdbBtcMoveUpward(SBTC *pBtc) {
H
Hongze Cheng 已提交
1921 1922
  if (pBtc->iPage == 0) return -1;

H
Hongze Cheng 已提交
1923
  tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn);
H
Hongze Cheng 已提交
1924 1925 1926 1927 1928

  pBtc->iPage--;
  pBtc->pPage = pBtc->pgStack[pBtc->iPage];
  pBtc->idx = pBtc->idxStack[pBtc->iPage];

H
Hongze Cheng 已提交
1929
  return 0;
H
Hongze Cheng 已提交
1930
}
H
refact  
Hongze Cheng 已提交
1931

H
Hongze Cheng 已提交
1932 1933 1934 1935 1936 1937 1938 1939
int tdbBtcGet(SBTC *pBtc, const void **ppKey, int *kLen, const void **ppVal, int *vLen) {
  SCell *pCell;

  if (pBtc->idx < 0 || pBtc->idx >= TDB_PAGE_TOTAL_CELLS(pBtc->pPage)) {
    return -1;
  }

  pCell = tdbPageGetCell(pBtc->pPage, pBtc->idx);
1940
  tdbBtreeDecodeCell(pBtc->pPage, pCell, &pBtc->coder, pBtc->pTxn, pBtc->pBt);
H
Hongze Cheng 已提交
1941 1942 1943 1944 1945 1946 1947 1948

  if (ppKey) {
    *ppKey = (void *)pBtc->coder.pKey;
    *kLen = pBtc->coder.kLen;
  }

  if (ppVal) {
    *ppVal = (void *)pBtc->coder.pVal;
H
Hongze Cheng 已提交
1949
    *vLen = pBtc->coder.vLen;
H
Hongze Cheng 已提交
1950 1951 1952 1953 1954
  }

  return 0;
}

H
Hongze Cheng 已提交
1955
int tdbBtcDelete(SBTC *pBtc) {
H
Hongze Cheng 已提交
1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
  int         idx = pBtc->idx;
  int         nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
  SPager     *pPager = pBtc->pBt->pPager;
  const void *pKey;
  i8          iPage;
  SPage      *pPage;
  SPgno       pgno;
  SCell      *pCell;
  int         szCell;
  int         nKey;
  int         ret;

1968
  tAssert(idx >= 0 && idx < nCells);
H
Hongze Cheng 已提交
1969 1970 1971 1972

  // drop the cell on the leaf
  ret = tdbPagerWrite(pPager, pBtc->pPage);
  if (ret < 0) {
1973
    tdbError("failed to write page since %s", terrstr());
H
Hongze Cheng 已提交
1974 1975 1976
    return -1;
  }

1977
  tdbPageDropCell(pBtc->pPage, idx, pBtc->pTxn, pBtc->pBt);
H
Hongze Cheng 已提交
1978

H
Hongze Cheng 已提交
1979
  // update interior page or do balance
H
Hongze Cheng 已提交
1980
  if (idx == nCells - 1) {
H
Hongze Cheng 已提交
1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994
    if (idx) {
      pBtc->idx--;
      tdbBtcGet(pBtc, &pKey, &nKey, NULL, NULL);

      // loop to update the interial page
      pgno = TDB_PAGE_PGNO(pBtc->pPage);
      for (iPage = pBtc->iPage - 1; iPage >= 0; iPage--) {
        pPage = pBtc->pgStack[iPage];
        idx = pBtc->idxStack[iPage];
        nCells = TDB_PAGE_TOTAL_CELLS(pPage);

        if (idx < nCells) {
          ret = tdbPagerWrite(pPager, pPage);
          if (ret < 0) {
1995
            tdbError("failed to write page since %s", terrstr());
H
Hongze Cheng 已提交
1996 1997 1998 1999 2000
            return -1;
          }

          // update the cell with new key
          pCell = tdbOsMalloc(nKey + 9);
2001
          tdbBtreeEncodeCell(pPage, pKey, nKey, &pgno, sizeof(pgno), pCell, &szCell, pBtc->pTxn, pBtc->pBt);
H
Hongze Cheng 已提交
2002

2003
          ret = tdbPageUpdateCell(pPage, idx, pCell, szCell, pBtc->pTxn, pBtc->pBt);
H
Hongze Cheng 已提交
2004 2005
          if (ret < 0) {
            tdbOsFree(pCell);
2006
            tAssert(0);
H
Hongze Cheng 已提交
2007 2008 2009 2010 2011 2012 2013 2014 2015
            return -1;
          }
          tdbOsFree(pCell);
          break;
        } else {
          pgno = TDB_PAGE_PGNO(pPage);
        }
      }
    } else {
H
Hongze Cheng 已提交
2016
      // delete the leaf page and do balance
2017
      tAssert(TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0);
H
Hongze Cheng 已提交
2018 2019 2020

      ret = tdbBtreeBalance(pBtc);
      if (ret < 0) {
2021
        tAssert(0);
H
Hongze Cheng 已提交
2022 2023
        return -1;
      }
H
Hongze Cheng 已提交
2024
    }
H
Hongze Cheng 已提交
2025 2026 2027 2028 2029
  }

  return 0;
}

H
Hongze Cheng 已提交
2030 2031 2032 2033 2034 2035 2036 2037
int tdbBtcUpsert(SBTC *pBtc, const void *pKey, int kLen, const void *pData, int nData, int insert) {
  SCell *pCell;
  int    szCell;
  int    nCells = TDB_PAGE_TOTAL_CELLS(pBtc->pPage);
  int    szBuf;
  void  *pBuf;
  int    ret;

2038
  tAssert(pBtc->idx >= 0);
H
Hongze Cheng 已提交
2039 2040 2041

  // alloc space
  szBuf = kLen + nData + 14;
2042
  pBuf = tdbRealloc(pBtc->pBt->pBuf, pBtc->pBt->pageSize > szBuf ? szBuf : pBtc->pBt->pageSize);
H
Hongze Cheng 已提交
2043
  if (pBuf == NULL) {
2044
    tAssert(0);
H
Hongze Cheng 已提交
2045 2046 2047 2048 2049 2050
    return -1;
  }
  pBtc->pBt->pBuf = pBuf;
  pCell = (SCell *)pBtc->pBt->pBuf;

  // encode cell
2051
  ret = tdbBtreeEncodeCell(pBtc->pPage, pKey, kLen, pData, nData, pCell, &szCell, pBtc->pTxn, pBtc->pBt);
H
Hongze Cheng 已提交
2052
  if (ret < 0) {
2053
    tAssert(0);
H
Hongze Cheng 已提交
2054 2055 2056 2057 2058 2059
    return -1;
  }

  // mark dirty
  ret = tdbPagerWrite(pBtc->pBt->pPager, pBtc->pPage);
  if (ret < 0) {
2060
    tdbError("failed to write page since %s", terrstr());
H
Hongze Cheng 已提交
2061 2062 2063 2064 2065
    return -1;
  }

  // insert or update
  if (insert) {
2066
    tAssert(pBtc->idx <= nCells);
H
Hongze Cheng 已提交
2067 2068 2069

    ret = tdbPageInsertCell(pBtc->pPage, pBtc->idx, pCell, szCell, 0);
  } else {
2070
    tAssert(pBtc->idx < nCells);
H
Hongze Cheng 已提交
2071

2072
    ret = tdbPageUpdateCell(pBtc->pPage, pBtc->idx, pCell, szCell, pBtc->pTxn, pBtc->pBt);
H
Hongze Cheng 已提交
2073 2074
  }
  if (ret < 0) {
2075
    tAssert(0);
H
Hongze Cheng 已提交
2076 2077 2078 2079 2080 2081 2082
    return -1;
  }

  // check balance
  if (pBtc->pPage->nOverflow > 0) {
    ret = tdbBtreeBalance(pBtc);
    if (ret < 0) {
2083
      tAssert(0);
H
Hongze Cheng 已提交
2084 2085 2086 2087
      return -1;
    }
  }

H
Hongze Cheng 已提交
2088 2089 2090
  return 0;
}

H
Hongze Cheng 已提交
2091
int tdbBtcMoveTo(SBTC *pBtc, const void *pKey, int kLen, int *pCRst) {
H
Hongze Cheng 已提交
2092 2093 2094 2095 2096 2097 2098 2099
  int         ret;
  int         nCells;
  int         c;
  SCell      *pCell;
  SBTree     *pBt = pBtc->pBt;
  SPager     *pPager = pBt->pPager;
  const void *pTKey;
  int         tkLen;
H
Hongze Cheng 已提交
2100

2101
  tdbTrace("tdb moveto, pager:%p, ipage:%d", pPager, pBtc->iPage);
H
Hongze Cheng 已提交
2102
  if (pBtc->iPage < 0) {
H
Hongze Cheng 已提交
2103
    // move from a clear cursor
H
Hongze Cheng 已提交
2104 2105
    ret = tdbPagerFetchPage(pPager, &pBt->root, &(pBtc->pPage), tdbBtreeInitPage,
                            &((SBtreeInitPageArg){.pBt = pBt, .flags = TDB_BTREE_ROOT | TDB_BTREE_LEAF}), pBtc->pTxn);
H
Hongze Cheng 已提交
2106
    if (ret < 0) {
H
Hongze Cheng 已提交
2107
      // TODO
2108
      tAssert(0);
H
Hongze Cheng 已提交
2109
      return 0;
H
Hongze Cheng 已提交
2110 2111 2112
    }

    pBtc->iPage = 0;
H
Hongze Cheng 已提交
2113 2114 2115 2116
    pBtc->idx = -1;
    // for empty tree, just return with an invalid position
    if (TDB_PAGE_TOTAL_CELLS(pBtc->pPage) == 0) return 0;
  } else {
2117
    tAssert(0);
H
Hongze Cheng 已提交
2118
#if 0
H
Hongze Cheng 已提交
2119 2120 2121 2122 2123 2124 2125 2126 2127 2128
    SPage *pPage;
    int    idx;
    int    iPage = 0;

    // downward search
    for (; iPage < pBtc->iPage; iPage++) {
      pPage = pBtc->pgStack[iPage];
      idx = pBtc->idxStack[iPage];
      nCells = TDB_PAGE_TOTAL_CELLS(pPage);

2129
      tAssert(!TDB_BTREE_PAGE_IS_LEAF(pPage));
H
Hongze Cheng 已提交
2130 2131 2132 2133

      // check if key <= current position
      if (idx < nCells) {
        pCell = tdbPageGetCell(pPage, idx);
2134
        tdbBtreeDecodeCell(pPage, pCell, &cd);
H
Hongze Cheng 已提交
2135 2136 2137 2138 2139 2140 2141 2142
        c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen);
        if (c > 0) break;
      }

      // check if key > current - 1 position
      if (idx > 0) {
        pCell = tdbPageGetCell(pPage, idx - 1);
        tdbBtreeDecodeCell(pPage, pCell, &cd);
2143
        c = pBt->kcmpr(pKey, kLen, cd.pKey, cd.kLen, pBtc->pTxn, pBtc->pBt);
H
Hongze Cheng 已提交
2144 2145 2146 2147 2148 2149 2150 2151 2152
        if (c <= 0) break;
      }
    }

    // move upward
    for (;;) {
      if (pBtc->iPage == iPage) break;
      tdbBtcMoveUpward(pBtc);
    }
H
Hongze Cheng 已提交
2153
#endif
H
Hongze Cheng 已提交
2154
  }
H
Hongze Cheng 已提交
2155

H
Hongze Cheng 已提交
2156
  // search downward to the leaf
2157
  tdbTrace("tdb search downward, pager:%p, ipage:%d", pPager, pBtc->iPage);
H
Hongze Cheng 已提交
2158
  for (;;) {
H
Hongze Cheng 已提交
2159
    int    lidx, ridx;
H
Hongze Cheng 已提交
2160
    SPage *pPage;
H
Hongze Cheng 已提交
2161

H
Hongze Cheng 已提交
2162 2163 2164 2165
    pPage = pBtc->pPage;
    nCells = TDB_PAGE_TOTAL_CELLS(pPage);
    lidx = 0;
    ridx = nCells - 1;
H
Hongze Cheng 已提交
2166

2167
    tAssert(nCells > 0);
H
Hongze Cheng 已提交
2168

H
Hongze Cheng 已提交
2169
    // compare first cell
H
Hongze Cheng 已提交
2170 2171 2172
    pBtc->idx = lidx;
    tdbBtcGet(pBtc, &pTKey, &tkLen, NULL, NULL);
    c = pBt->kcmpr(pKey, kLen, pTKey, tkLen);
H
Hongze Cheng 已提交
2173 2174 2175 2176 2177 2178 2179 2180
    if (c <= 0) {
      ridx = lidx - 1;
    } else {
      lidx = lidx + 1;
    }

    // compare last cell
    if (lidx <= ridx) {
H
Hongze Cheng 已提交
2181 2182 2183
      pBtc->idx = ridx;
      tdbBtcGet(pBtc, &pTKey, &tkLen, NULL, NULL);
      c = pBt->kcmpr(pKey, kLen, pTKey, tkLen);
H
Hongze Cheng 已提交
2184 2185 2186 2187 2188 2189 2190
      if (c >= 0) {
        lidx = ridx + 1;
      } else {
        ridx = ridx - 1;
      }
    }

H
Hongze Cheng 已提交
2191
    // binary search
2192
    tdbTrace("tdb binary search, pager:%p, ipage:%d", pPager, pBtc->iPage);
H
Hongze Cheng 已提交
2193 2194
    for (;;) {
      if (lidx > ridx) break;
H
Hongze Cheng 已提交
2195

H
Hongze Cheng 已提交
2196 2197 2198
      pBtc->idx = (lidx + ridx) >> 1;
      tdbBtcGet(pBtc, &pTKey, &tkLen, NULL, NULL);
      c = pBt->kcmpr(pKey, kLen, pTKey, tkLen);
H
Hongze Cheng 已提交
2199 2200
      if (c < 0) {
        // pKey < cd.pKey
H
Hongze Cheng 已提交
2201
        ridx = pBtc->idx - 1;
H
Hongze Cheng 已提交
2202 2203
      } else if (c > 0) {
        // pKey > cd.pKey
H
Hongze Cheng 已提交
2204
        lidx = pBtc->idx + 1;
H
Hongze Cheng 已提交
2205 2206 2207
      } else {
        // pKey == cd.pKey
        break;
H
Hongze Cheng 已提交
2208
      }
H
Hongze Cheng 已提交
2209
    }
H
Hongze Cheng 已提交
2210

H
Hongze Cheng 已提交
2211 2212 2213 2214 2215
    // keep search downward or break
    if (TDB_BTREE_PAGE_IS_LEAF(pPage)) {
      *pCRst = c;
      break;
    } else {
H
Hongze Cheng 已提交
2216 2217
      if (c > 0) {
        pBtc->idx += 1;
H
Hongze Cheng 已提交
2218
      }
H
Hongze Cheng 已提交
2219
      tdbBtcMoveDownward(pBtc);
H
Hongze Cheng 已提交
2220 2221 2222
    }
  }

2223 2224
  tdbTrace("tdb moveto end, pager:%p, ipage:%d", pPager, pBtc->iPage);

H
Hongze Cheng 已提交
2225 2226 2227
  return 0;
}

H
refact  
Hongze Cheng 已提交
2228 2229 2230 2231
int tdbBtcClose(SBTC *pBtc) {
  if (pBtc->iPage < 0) return 0;

  for (;;) {
2232
    tAssert(pBtc->pPage);
H
refact  
Hongze Cheng 已提交
2233

H
Hongze Cheng 已提交
2234
    tdbPagerReturnPage(pBtc->pBt->pPager, pBtc->pPage, pBtc->pTxn);
H
refact  
Hongze Cheng 已提交
2235 2236 2237 2238 2239 2240 2241 2242

    pBtc->iPage--;
    if (pBtc->iPage < 0) break;

    pBtc->pPage = pBtc->pgStack[pBtc->iPage];
    pBtc->idx = pBtc->idxStack[pBtc->iPage];
  }

2243
  if (TDB_CELLDECODER_FREE_VAL(&pBtc->coder)) {
2244 2245
    tdbDebug("tdb btc/close decoder: %p pVal free: %p", &pBtc->coder, pBtc->coder.pVal);

2246 2247 2248
    tdbFree(pBtc->coder.pVal);
  }

2249 2250 2251 2252
  if (pBtc->freeTxn) {
    tdbTxnClose(pBtc->pTxn);
  }

H
refact  
Hongze Cheng 已提交
2253 2254
  return 0;
}
H
Hongze Cheng 已提交
2255 2256 2257 2258 2259 2260 2261 2262

int tdbBtcIsValid(SBTC *pBtc) {
  if (pBtc->idx < 0) {
    return 0;
  } else {
    return 1;
  }
}
H
refact  
Hongze Cheng 已提交
2263
// TDB_BTREE_CURSOR
H
Hongze Cheng 已提交
2264

H
refact  
Hongze Cheng 已提交
2265
// TDB_BTREE_DEBUG =====================
H
Hongze Cheng 已提交
2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284
#ifndef NODEBUG
typedef struct {
  SPgno pgno;
  u8    root;
  u8    leaf;
  SPgno rChild;
  int   nCells;
  int   nOvfl;
} SBtPageInfo;

SBtPageInfo btPageInfos[20];

void tdbBtPageInfo(SPage *pPage, int idx) {
  SBtPageInfo *pBtPageInfo;

  pBtPageInfo = btPageInfos + idx;

  pBtPageInfo->pgno = TDB_PAGE_PGNO(pPage);

H
refact  
Hongze Cheng 已提交
2285 2286
  pBtPageInfo->root = TDB_BTREE_PAGE_IS_ROOT(pPage);
  pBtPageInfo->leaf = TDB_BTREE_PAGE_IS_LEAF(pPage);
H
Hongze Cheng 已提交
2287 2288 2289 2290 2291 2292 2293 2294 2295

  pBtPageInfo->rChild = 0;
  if (!pBtPageInfo->leaf) {
    pBtPageInfo->rChild = *(SPgno *)(pPage->pData + 1);
  }

  pBtPageInfo->nCells = TDB_PAGE_TOTAL_CELLS(pPage) - pPage->nOverflow;
  pBtPageInfo->nOvfl = pPage->nOverflow;
}
H
refact  
Hongze Cheng 已提交
2296
#endif
2297
// TDB_BTREE_DEBUG