tdbPage.c 15.5 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "tdbInt.h"

H
Hongze Cheng 已提交
18 19 20
extern SPageMethods pageMethods;
extern SPageMethods pageLargeMethods;

H
Hongze Cheng 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34
#define TDB_PAGE_OFFSET_SIZE(pPage)                     ((pPage)->pPageMethods->szOffset)
#define TDB_PAGE_HDR_SIZE(pPage)                        ((pPage)->pPageMethods->szPageHdr)
#define TDB_PAGE_FREE_CELL_SIZE(pPage)                  ((pPage)->pPageMethods->szFreeCell)
#define TDB_PAGE_NCELLS(pPage)                          (*(pPage)->pPageMethods->getCellNum)(pPage)
#define TDB_PAGE_CCELLS(pPage)                          (*(pPage)->pPageMethods->getCellBody)(pPage)
#define TDB_PAGE_FCELL(pPage)                           (*(pPage)->pPageMethods->getCellFree)(pPage)
#define TDB_PAGE_NFREE(pPage)                           (*(pPage)->pPageMethods->getFreeBytes)(pPage)
#define TDB_PAGE_CELL_OFFSET_AT(pPage, idx)             (*(pPage)->pPageMethods->getCellOffset)(pPage, idx)
#define TDB_PAGE_NCELLS_SET(pPage, NCELLS)              (*(pPage)->pPageMethods->setCellNum)(pPage, NCELLS)
#define TDB_PAGE_CCELLS_SET(pPage, CCELLS)              (*(pPage)->pPageMethods->setCellBody)(pPage, CCELLS)
#define TDB_PAGE_FCELL_SET(pPage, FCELL)                (*(pPage)->pPageMethods->setCellFree)(pPage, FCELL)
#define TDB_PAGE_NFREE_SET(pPage, NFREE)                (*(pPage)->pPageMethods->setFreeBytes)(pPage, NFREE)
#define TDB_PAGE_CELL_OFFSET_AT_SET(pPage, idx, OFFSET) (*(pPage)->pPageMethods->setCellOffset)(pPage, idx, OFFSET)
#define TDB_PAGE_CELL_AT(pPage, idx)                    ((pPage)->pData + TDB_PAGE_CELL_OFFSET_AT(pPage, idx))
H
Hongze Cheng 已提交
35 36
#define TDB_PAGE_MAX_FREE_BLOCK(pPage, szAmHdr) \
  ((pPage)->pageSize - (szAmHdr)-TDB_PAGE_HDR_SIZE(pPage) - sizeof(SPageFtr))
H
Hongze Cheng 已提交
37

H
Hongze Cheng 已提交
38 39
static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell);
static int tdbPageDefragment(SPage *pPage);
H
Hongze Cheng 已提交
40
static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int szCell);
H
Hongze Cheng 已提交
41

H
Hongze Cheng 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg) {
  SPage *pPage;
  u8    *ptr;
  int    size;

  ASSERT(TDB_IS_PGSIZE_VLD(pageSize));

  *ppPage = NULL;
  size = pageSize + sizeof(*pPage);

  ptr = (u8 *)((*xMalloc)(arg, size));
  if (pPage == NULL) {
    return -1;
  }

  memset(ptr, 0, size);
  pPage = (SPage *)(ptr + pageSize);

H
Hongze Cheng 已提交
60
  TDB_INIT_PAGE_LOCK(pPage);
H
Hongze Cheng 已提交
61
  pPage->pageSize = pageSize;
H
Hongze Cheng 已提交
62
  pPage->pData = ptr;
H
Hongze Cheng 已提交
63
  if (pageSize < 65536) {
H
Hongze Cheng 已提交
64
    pPage->pPageMethods = &pageMethods;
H
Hongze Cheng 已提交
65
  } else {
H
Hongze Cheng 已提交
66
    pPage->pPageMethods = &pageLargeMethods;
H
Hongze Cheng 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  }

  *ppPage = pPage;
  return 0;
}

int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
  u8 *ptr;

  ptr = pPage->pData;
  (*xFree)(arg, ptr);

  return 0;
}

H
Hongze Cheng 已提交
82
void tdbPageZero(SPage *pPage, u8 szAmHdr, int (*xCellSize)(const SPage *, SCell *)) {
H
Hongze Cheng 已提交
83 84
  pPage->pAmHdr = pPage->pData;
  pPage->pPageHdr = pPage->pAmHdr + szAmHdr;
H
Hongze Cheng 已提交
85 86 87
  TDB_PAGE_NCELLS_SET(pPage, 0);
  TDB_PAGE_CCELLS_SET(pPage, pPage->pageSize - sizeof(SPageFtr));
  TDB_PAGE_FCELL_SET(pPage, 0);
H
Hongze Cheng 已提交
88 89 90 91 92 93
  TDB_PAGE_NFREE_SET(pPage, TDB_PAGE_MAX_FREE_BLOCK(pPage, szAmHdr));
  pPage->pCellIdx = pPage->pPageHdr + TDB_PAGE_HDR_SIZE(pPage);
  pPage->pFreeStart = pPage->pCellIdx;
  pPage->pFreeEnd = pPage->pData + TDB_PAGE_CCELLS(pPage);
  pPage->pPageFtr = (SPageFtr *)(pPage->pData + pPage->pageSize - sizeof(SPageFtr));
  pPage->nOverflow = 0;
H
Hongze Cheng 已提交
94
  pPage->xCellSize = xCellSize;
H
Hongze Cheng 已提交
95 96

  ASSERT((u8 *)pPage->pPageFtr == pPage->pFreeEnd);
H
Hongze Cheng 已提交
97 98
}

H
Hongze Cheng 已提交
99
void tdbPageInit(SPage *pPage, u8 szAmHdr, int (*xCellSize)(const SPage *, SCell *)) {
H
Hongze Cheng 已提交
100
  pPage->pAmHdr = pPage->pData;
H
Hongze Cheng 已提交
101
  pPage->pPageHdr = pPage->pAmHdr + szAmHdr;
H
Hongze Cheng 已提交
102 103 104 105 106
  pPage->pCellIdx = pPage->pPageHdr + TDB_PAGE_HDR_SIZE(pPage);
  pPage->pFreeStart = pPage->pCellIdx + TDB_PAGE_OFFSET_SIZE(pPage) * TDB_PAGE_NCELLS(pPage);
  pPage->pFreeEnd = pPage->pData + TDB_PAGE_CCELLS(pPage);
  pPage->pPageFtr = (SPageFtr *)(pPage->pData + pPage->pageSize - sizeof(SPageFtr));
  pPage->nOverflow = 0;
H
Hongze Cheng 已提交
107
  pPage->xCellSize = xCellSize;
H
Hongze Cheng 已提交
108 109 110 111 112

  ASSERT(pPage->pFreeEnd >= pPage->pFreeStart);
  ASSERT(pPage->pFreeEnd - pPage->pFreeStart <= TDB_PAGE_NFREE(pPage));
}

H
Hongze Cheng 已提交
113
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
H
Hongze Cheng 已提交
114 115
  int    nFree;
  int    nCells;
H
Hongze Cheng 已提交
116
  int    iOvfl;
H
Hongze Cheng 已提交
117 118
  int    lidx;  // local idx
  SCell *pNewCell;
H
Hongze Cheng 已提交
119

H
Hongze Cheng 已提交
120
  ASSERT(szCell <= TDB_PAGE_MAX_FREE_BLOCK(pPage, pPage->pPageHdr - pPage->pAmHdr));
H
Hongze Cheng 已提交
121

H
Hongze Cheng 已提交
122 123
  nFree = TDB_PAGE_NFREE(pPage);
  nCells = TDB_PAGE_NCELLS(pPage);
H
Hongze Cheng 已提交
124 125 126 127 128 129 130 131 132
  iOvfl = 0;

  for (; iOvfl < pPage->nOverflow; iOvfl++) {
    if (pPage->aiOvfl[iOvfl] >= idx) {
      break;
    }
  }

  lidx = idx - iOvfl;
H
Hongze Cheng 已提交
133 134 135

  if (nFree >= szCell + TDB_PAGE_OFFSET_SIZE(pPage)) {
    // page must has enough space to hold the cell locally
H
Hongze Cheng 已提交
136
    tdbPageAllocate(pPage, szCell, &pNewCell);
H
Hongze Cheng 已提交
137 138 139 140 141 142 143

    memcpy(pNewCell, pCell, szCell);

    // no overflow cell exists in this page
    u8 *src = pPage->pCellIdx + TDB_PAGE_OFFSET_SIZE(pPage) * lidx;
    u8 *dest = src + TDB_PAGE_OFFSET_SIZE(pPage);
    memmove(dest, src, pPage->pFreeStart - dest);
H
Hongze Cheng 已提交
144
    TDB_PAGE_CELL_OFFSET_AT_SET(pPage, lidx, pNewCell - pPage->pData);
H
Hongze Cheng 已提交
145
    TDB_PAGE_NCELLS_SET(pPage, nCells + 1);
H
Hongze Cheng 已提交
146 147

    ASSERT(pPage->pFreeStart == pPage->pCellIdx + TDB_PAGE_OFFSET_SIZE(pPage) * (nCells + 1));
H
Hongze Cheng 已提交
148
  } else {
H
Hongze Cheng 已提交
149
    // TODO: make it extensible
H
Hongze Cheng 已提交
150 151 152 153 154 155 156 157
    // add the cell as an overflow cell
    for (int i = pPage->nOverflow; i > iOvfl; i--) {
      pPage->apOvfl[i] = pPage->apOvfl[i - 1];
      pPage->aiOvfl[i] = pPage->aiOvfl[i - 1];
    }

    pPage->apOvfl[iOvfl] = pCell;
    pPage->aiOvfl[iOvfl] = idx;
H
Hongze Cheng 已提交
158
    pPage->nOverflow++;
H
Hongze Cheng 已提交
159 160 161 162 163
    iOvfl++;
  }

  for (; iOvfl < pPage->nOverflow; iOvfl++) {
    pPage->aiOvfl[iOvfl]++;
H
Hongze Cheng 已提交
164 165 166 167 168 169
  }

  return 0;
}

int tdbPageDropCell(SPage *pPage, int idx) {
H
Hongze Cheng 已提交
170 171 172 173
  int    lidx;
  SCell *pCell;
  int    szCell;
  int    nCells;
H
Hongze Cheng 已提交
174
  int    iOvfl;
H
Hongze Cheng 已提交
175 176 177

  nCells = TDB_PAGE_NCELLS(pPage);

H
Hongze Cheng 已提交
178 179 180 181 182
  ASSERT(idx >= 0 && idx < nCells + pPage->nOverflow);

  iOvfl = 0;
  for (; iOvfl < pPage->nOverflow; iOvfl++) {
    if (pPage->aiOvfl[iOvfl] == idx) {
H
Hongze Cheng 已提交
183 184 185 186 187 188 189
      // remove the over flow cell
      for (; (++iOvfl) < pPage->nOverflow;) {
        pPage->aiOvfl[iOvfl - 1] = pPage->aiOvfl[iOvfl] - 1;
        pPage->apOvfl[iOvfl - 1] = pPage->apOvfl[iOvfl];
      }

      pPage->nOverflow--;
H
Hongze Cheng 已提交
190 191 192 193
      return 0;
    } else if (pPage->aiOvfl[iOvfl] > idx) {
      break;
    }
H
Hongze Cheng 已提交
194 195
  }

H
Hongze Cheng 已提交
196
  lidx = idx - iOvfl;
H
Hongze Cheng 已提交
197
  pCell = TDB_PAGE_CELL_AT(pPage, lidx);
H
Hongze Cheng 已提交
198
  szCell = (*pPage->xCellSize)(pPage, pCell);
H
Hongze Cheng 已提交
199 200 201
  tdbPageFree(pPage, lidx, pCell, szCell);
  TDB_PAGE_NCELLS_SET(pPage, nCells - 1);

H
Hongze Cheng 已提交
202 203 204 205 206
  for (; iOvfl < pPage->nOverflow; iOvfl++) {
    pPage->aiOvfl[iOvfl]--;
    ASSERT(pPage->aiOvfl[iOvfl] > 0);
  }

H
Hongze Cheng 已提交
207 208 209
  return 0;
}

H
Hongze Cheng 已提交
210
void tdbPageCopy(SPage *pFromPage, SPage *pToPage) {
H
Hongze Cheng 已提交
211 212
  int delta, nFree;

H
Hongze Cheng 已提交
213 214 215 216 217 218 219
  pToPage->pFreeStart = pToPage->pPageHdr + (pFromPage->pFreeStart - pFromPage->pPageHdr);
  pToPage->pFreeEnd = (u8 *)(pToPage->pPageFtr) - ((u8 *)pFromPage->pPageFtr - pFromPage->pFreeEnd);

  ASSERT(pToPage->pFreeEnd >= pToPage->pFreeStart);

  memcpy(pToPage->pPageHdr, pFromPage->pPageHdr, pFromPage->pFreeStart - pFromPage->pPageHdr);
  memcpy(pToPage->pFreeEnd, pFromPage->pFreeEnd, (u8 *)pFromPage->pPageFtr - pFromPage->pFreeEnd);
H
Hongze Cheng 已提交
220 221 222 223 224 225 226 227 228 229

  ASSERT(TDB_PAGE_CCELLS(pToPage) == pToPage->pFreeEnd - pToPage->pData);

  delta = (pToPage->pPageHdr - pToPage->pAmHdr) - (pFromPage->pPageHdr - pFromPage->pAmHdr);
  if (delta != 0) {
    nFree = TDB_PAGE_NFREE(pFromPage);
    TDB_PAGE_NFREE_SET(pToPage, nFree - delta);
  }

  // TODO: do we need to copy the overflow part ???
H
Hongze Cheng 已提交
230 231
}

H
Hongze Cheng 已提交
232 233 234 235 236 237
static int tdbPageAllocate(SPage *pPage, int szCell, SCell **ppCell) {
  SCell *pFreeCell;
  u8    *pOffset;
  int    nFree;
  int    ret;
  int    cellFree;
H
Hongze Cheng 已提交
238
  SCell *pCell = NULL;
H
Hongze Cheng 已提交
239 240

  *ppCell = NULL;
H
Hongze Cheng 已提交
241
  nFree = TDB_PAGE_NFREE(pPage);
H
Hongze Cheng 已提交
242

H
Hongze Cheng 已提交
243 244 245 246 247 248
  ASSERT(nFree >= szCell + TDB_PAGE_OFFSET_SIZE(pPage));
  ASSERT(TDB_PAGE_CCELLS(pPage) == pPage->pFreeEnd - pPage->pData);

  // 1. Try to allocate from the free space block area
  if (pPage->pFreeEnd - pPage->pFreeStart >= szCell + TDB_PAGE_OFFSET_SIZE(pPage)) {
    pPage->pFreeEnd -= szCell;
H
Hongze Cheng 已提交
249
    pCell = pPage->pFreeEnd;
H
Hongze Cheng 已提交
250
    TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData);
H
Hongze Cheng 已提交
251
    goto _alloc_finish;
H
Hongze Cheng 已提交
252 253 254
  }

  // 2. Try to allocate from the page free list
H
Hongze Cheng 已提交
255 256 257 258 259 260 261 262
  cellFree = TDB_PAGE_FCELL(pPage);
  ASSERT(cellFree == 0 || cellFree > pPage->pFreeEnd - pPage->pData);
  if (cellFree && pPage->pFreeEnd - pPage->pFreeStart >= TDB_PAGE_OFFSET_SIZE(pPage)) {
    SCell *pPrevFreeCell = NULL;
    int    szPrevFreeCell;
    int    szFreeCell;
    int    nxFreeCell;
    int    newSize;
H
Hongze Cheng 已提交
263 264

    for (;;) {
H
Hongze Cheng 已提交
265 266 267 268 269 270
      if (cellFree == 0) break;

      pFreeCell = pPage->pData + cellFree;
      pPage->pPageMethods->getFreeCellInfo(pFreeCell, &szFreeCell, &nxFreeCell);

      if (szFreeCell >= szCell) {
H
Hongze Cheng 已提交
271
        pCell = pFreeCell;
H
Hongze Cheng 已提交
272 273 274 275 276 277 278 279 280 281 282

        newSize = szFreeCell - szCell;
        pFreeCell += szCell;
        if (newSize >= TDB_PAGE_FREE_CELL_SIZE(pPage)) {
          pPage->pPageMethods->setFreeCellInfo(pFreeCell, newSize, nxFreeCell);
          if (pPrevFreeCell) {
            pPage->pPageMethods->setFreeCellInfo(pPrevFreeCell, szPrevFreeCell, pFreeCell - pPage->pData);
          } else {
            TDB_PAGE_FCELL_SET(pPage, pFreeCell - pPage->pData);
          }

H
Hongze Cheng 已提交
283
          goto _alloc_finish;
H
Hongze Cheng 已提交
284
        } else {
H
Hongze Cheng 已提交
285 286 287 288 289
          if (pPrevFreeCell) {
            pPage->pPageMethods->setFreeCellInfo(pPrevFreeCell, szPrevFreeCell, nxFreeCell);
          } else {
            TDB_PAGE_FCELL_SET(pPage, nxFreeCell);
          }
H
Hongze Cheng 已提交
290 291
        }
      } else {
H
Hongze Cheng 已提交
292 293 294
        pPrevFreeCell = pFreeCell;
        szPrevFreeCell = szFreeCell;
        cellFree = nxFreeCell;
H
Hongze Cheng 已提交
295 296 297 298
      }
    }
  }

H
Hongze Cheng 已提交
299 300 301 302 303
  // 3. Try to dfragment and allocate again
  tdbPageDefragment(pPage);
  ASSERT(pPage->pFreeEnd - pPage->pFreeStart == nFree);
  ASSERT(nFree == TDB_PAGE_NFREE(pPage));
  ASSERT(pPage->pFreeEnd - pPage->pData == TDB_PAGE_CCELLS(pPage));
H
Hongze Cheng 已提交
304

H
Hongze Cheng 已提交
305 306 307
  pPage->pFreeEnd -= szCell;
  pCell = pPage->pFreeEnd;
  TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData);
H
Hongze Cheng 已提交
308

H
Hongze Cheng 已提交
309 310 311 312
_alloc_finish:
  ASSERT(pCell);
  pPage->pFreeStart += TDB_PAGE_OFFSET_SIZE(pPage);
  TDB_PAGE_NFREE_SET(pPage, nFree - szCell - TDB_PAGE_OFFSET_SIZE(pPage));
H
Hongze Cheng 已提交
313 314 315 316
  *ppCell = pCell;
  return 0;
}

H
Hongze Cheng 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int szCell) {
  int nFree;
  int cellFree;
  u8 *dest;
  u8 *src;

  ASSERT(pCell >= pPage->pFreeEnd);
  ASSERT(pCell + szCell <= (u8 *)(pPage->pPageFtr));
  ASSERT(pCell == TDB_PAGE_CELL_AT(pPage, idx));

  nFree = TDB_PAGE_NFREE(pPage);

  if (pCell == pPage->pFreeEnd) {
    pPage->pFreeEnd += szCell;
    TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData);
  } else {
    if (szCell >= TDB_PAGE_FREE_CELL_SIZE(pPage)) {
      cellFree = TDB_PAGE_FCELL(pPage);
      pPage->pPageMethods->setFreeCellInfo(pCell, szCell, cellFree);
      TDB_PAGE_FCELL_SET(pPage, pCell - pPage->pData);
    } else {
      ASSERT(0);
    }
  }

  dest = pPage->pCellIdx + TDB_PAGE_OFFSET_SIZE(pPage) * idx;
  src = dest + TDB_PAGE_OFFSET_SIZE(pPage);
  memmove(dest, src, pPage->pFreeStart - src);

  pPage->pFreeStart -= TDB_PAGE_OFFSET_SIZE(pPage);
  nFree = nFree + szCell + TDB_PAGE_OFFSET_SIZE(pPage);
  TDB_PAGE_NFREE_SET(pPage, nFree);
H
Hongze Cheng 已提交
349 350 351 352
  return 0;
}

static int tdbPageDefragment(SPage *pPage) {
H
Hongze Cheng 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
  int    nFree;
  int    nCells;
  SCell *pCell;
  SCell *pNextCell;
  SCell *pTCell;
  int    szCell;
  int    idx;
  int    iCell;

  ASSERT(pPage->pFreeEnd - pPage->pFreeStart < nFree);

  nFree = TDB_PAGE_NFREE(pPage);
  nCells = TDB_PAGE_NCELLS(pPage);

  // Loop to compact the page content
  // Here we use an O(n^2) algorithm to do the job since
  // this is a low frequency job.
  pNextCell = (u8 *)pPage->pPageFtr;
  pCell = NULL;
  for (iCell = 0;; iCell++) {
    // compact over
    if (iCell == nCells) {
      pPage->pFreeEnd = pNextCell;
      break;
    }

    for (int i = 0; i < nCells; i++) {
      if (TDB_PAGE_CELL_OFFSET_AT(pPage, i) < pNextCell - pPage->pData) {
        pTCell = TDB_PAGE_CELL_AT(pPage, i);
        if (pCell == NULL || pCell < pTCell) {
          pCell = pTCell;
          idx = i;
        }
      } else {
        continue;
      }
    }

    ASSERT(pCell != NULL);

H
Hongze Cheng 已提交
393
    szCell = (*pPage->xCellSize)(pPage, pCell);
H
Hongze Cheng 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408

    ASSERT(pCell + szCell <= pNextCell);
    if (pCell + szCell < pNextCell) {
      memmove(pNextCell - szCell, pCell, szCell);
    }

    pCell = NULL;
    pNextCell = pNextCell - szCell;
    TDB_PAGE_CELL_OFFSET_AT_SET(pPage, idx, pNextCell - pPage->pData);
  }

  ASSERT(pPage->pFreeEnd - pPage->pFreeStart == nFree);
  TDB_PAGE_CCELLS_SET(pPage, pPage->pFreeEnd - pPage->pData);
  TDB_PAGE_FCELL_SET(pPage, 0);

H
Hongze Cheng 已提交
409 410 411 412
  return 0;
}

/* ---------------------------------------------------------------------------------------------------------- */
H
Hongze Cheng 已提交
413 414 415 416 417 418 419 420
typedef struct __attribute__((__packed__)) {
  u16 flags;
  u16 cellNum;
  u16 cellBody;
  u16 cellFree;
  u16 nFree;
} SPageHdr;

H
Hongze Cheng 已提交
421 422 423 424 425
typedef struct __attribute__((__packed__)) {
  u16 szCell;
  u16 nxOffset;
} SFreeCell;

H
Hongze Cheng 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
// cellNum
static inline int  getPageCellNum(SPage *pPage) { return ((SPageHdr *)(pPage->pPageHdr))[0].cellNum; }
static inline void setPageCellNum(SPage *pPage, int cellNum) {
  ASSERT(cellNum < 65536);
  ((SPageHdr *)(pPage->pPageHdr))[0].cellNum = (u16)cellNum;
}

// cellBody
static inline int  getPageCellBody(SPage *pPage) { return ((SPageHdr *)(pPage->pPageHdr))[0].cellBody; }
static inline void setPageCellBody(SPage *pPage, int cellBody) {
  ASSERT(cellBody < 65536);
  ((SPageHdr *)(pPage->pPageHdr))[0].cellBody = (u16)cellBody;
}

// cellFree
static inline int  getPageCellFree(SPage *pPage) { return ((SPageHdr *)(pPage->pPageHdr))[0].cellFree; }
static inline void setPageCellFree(SPage *pPage, int cellFree) {
  ASSERT(cellFree < 65536);
  ((SPageHdr *)(pPage->pPageHdr))[0].cellFree = (u16)cellFree;
}

// nFree
static inline int  getPageNFree(SPage *pPage) { return ((SPageHdr *)(pPage->pPageHdr))[0].nFree; }
static inline void setPageNFree(SPage *pPage, int nFree) {
H
Hongze Cheng 已提交
450
  ASSERT(nFree < 65536);
H
Hongze Cheng 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464
  ((SPageHdr *)(pPage->pPageHdr))[0].nFree = (u16)nFree;
}

// cell offset
static inline int getPageCellOffset(SPage *pPage, int idx) {
  ASSERT(idx >= 0 && idx < getPageCellNum(pPage));
  return ((u16 *)pPage->pCellIdx)[idx];
}

static inline void setPageCellOffset(SPage *pPage, int idx, int offset) {
  ASSERT(offset < 65536);
  ((u16 *)pPage->pCellIdx)[idx] = (u16)offset;
}

H
Hongze Cheng 已提交
465 466 467 468 469 470 471 472 473 474 475 476 477
// free cell info
static inline void getPageFreeCellInfo(SCell *pCell, int *szCell, int *nxOffset) {
  SFreeCell *pFreeCell = (SFreeCell *)pCell;
  *szCell = pFreeCell->szCell;
  *nxOffset = pFreeCell->nxOffset;
}

static inline void setPageFreeCellInfo(SCell *pCell, int szCell, int nxOffset) {
  SFreeCell *pFreeCell = (SFreeCell *)pCell;
  pFreeCell->szCell = szCell;
  pFreeCell->nxOffset = nxOffset;
}

H
Hongze Cheng 已提交
478
SPageMethods pageMethods = {
H
Hongze Cheng 已提交
479 480 481
    2,                    // szOffset
    sizeof(SPageHdr),     // szPageHdr
    sizeof(SFreeCell),    // szFreeCell
H
Hongze Cheng 已提交
482 483 484 485 486 487 488 489 490 491 492 493
    getPageCellNum,       // getCellNum
    setPageCellNum,       // setCellNum
    getPageCellBody,      // getCellBody
    setPageCellBody,      // setCellBody
    getPageCellFree,      // getCellFree
    setPageCellFree,      // setCellFree
    getPageNFree,         // getFreeBytes
    setPageNFree,         // setFreeBytes
    getPageCellOffset,    // getCellOffset
    setPageCellOffset,    // setCellOffset
    getPageFreeCellInfo,  // getFreeCellInfo
    setPageFreeCellInfo   // setFreeCellInfo
H
Hongze Cheng 已提交
494
};