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

H
Hongze Cheng 已提交
18 19
static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell);

H
Hongze Cheng 已提交
20
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg) {
H
Hongze Cheng 已提交
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  SPage *pPage;
  u8    *ptr;
  int    size;

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

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

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

  pPage->pData = ptr;
  pPage->pageSize = pageSize;
H
Hongze Cheng 已提交
38
  TDB_INIT_PAGE_LOCK(pPage);
H
Hongze Cheng 已提交
39 40 41 42

  /* TODO */

  *ppPage = pPage;
H
Hongze Cheng 已提交
43 44 45
  return 0;
}

H
Hongze Cheng 已提交
46
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
H
Hongze Cheng 已提交
47 48 49
  u8 *ptr;

  ptr = pPage->pData;
H
Hongze Cheng 已提交
50
  (*xFree)(arg, ptr);
H
Hongze Cheng 已提交
51

H
Hongze Cheng 已提交
52 53 54
  return 0;
}

H
Hongze Cheng 已提交
55
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
H
Hongze Cheng 已提交
56 57 58 59 60 61 62 63 64 65 66 67
  int    ret;
  SCell *pTarget;

  if (pPage->nOverflow || szCell + TDB_PAGE_CELL_OFFSET_SIZE(pPage) > pPage->nFree) {
    // TODO
  } else {
    ret = tdbPageAllocate(pPage, szCell, &pTarget);
    if (ret < 0) {
      return -1;
    }
  }

H
Hongze Cheng 已提交
68 69 70 71 72 73
  return 0;
}

int tdbPageDropCell(SPage *pPage, int idx) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
74 75 76
}

static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell) {
H
Hongze Cheng 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90
  SCell *pCell;
  int    szOffset;

  szOffset = TDB_PAGE_CELL_OFFSET_SIZE(pPage);
  ASSERT(pPage->nFree > size + szOffset);

  if (pPage->pFreeEnd - pPage->pFreeStart > size + szOffset) {
    pPage->pFreeEnd -= size;
    pPage->pFreeStart += szOffset;

    pCell = pPage->pFreeEnd;
  } else {
  }

H
Hongze Cheng 已提交
91 92 93
  return 0;
}

H
Hongze Cheng 已提交
94
static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int size) {
H
Hongze Cheng 已提交
95 96
  // TODO
  return 0;
H
Hongze Cheng 已提交
97 98 99 100 101
}

static int tdbPageDefragment(SPage *pPage) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
102
}