tdbPage.c 2.4 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
  SPage *pPage;
  u8    *ptr;
  int    size;

H
Hongze Cheng 已提交
25 26
  ASSERT(TDB_IS_PGSIZE_VLD(pageSize));

H
Hongze Cheng 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39
  *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 已提交
40 41 42 43 44
  if (pageSize < 65536) {
    pPage->szOffset = 2;
  } else {
    pPage->szOffset = 3;
  }
H
Hongze Cheng 已提交
45
  TDB_INIT_PAGE_LOCK(pPage);
H
Hongze Cheng 已提交
46 47 48 49

  /* TODO */

  *ppPage = pPage;
H
Hongze Cheng 已提交
50 51 52
  return 0;
}

H
Hongze Cheng 已提交
53
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
H
Hongze Cheng 已提交
54 55 56
  u8 *ptr;

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

H
Hongze Cheng 已提交
59 60 61
  return 0;
}

H
Hongze Cheng 已提交
62
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
H
Hongze Cheng 已提交
63 64 65
  int    ret;
  SCell *pTarget;

H
Hongze Cheng 已提交
66
  if (pPage->nOverflow || szCell + pPage->szOffset > pPage->nFree) {
H
Hongze Cheng 已提交
67 68 69 70 71 72
    // TODO
  } else {
    ret = tdbPageAllocate(pPage, szCell, &pTarget);
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
73 74 75 76

    memcpy(pTarget, pCell, szCell);
    // TODO: memmove();
    // pPage->pPaggHdr->nCells++;
H
Hongze Cheng 已提交
77 78
  }

H
Hongze Cheng 已提交
79 80 81 82 83 84
  return 0;
}

int tdbPageDropCell(SPage *pPage, int idx) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
85 86 87
}

static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell) {
H
Hongze Cheng 已提交
88 89
  SCell *pCell;

H
Hongze Cheng 已提交
90
  ASSERT(pPage->nFree > size + pPage->szOffset);
H
Hongze Cheng 已提交
91

H
Hongze Cheng 已提交
92
  if (pPage->pFreeEnd - pPage->pFreeStart > size + pPage->szOffset) {
H
Hongze Cheng 已提交
93
    pPage->pFreeEnd -= size;
H
Hongze Cheng 已提交
94
    pPage->pFreeStart += pPage->szOffset;
H
Hongze Cheng 已提交
95 96 97 98 99

    pCell = pPage->pFreeEnd;
  } else {
  }

H
Hongze Cheng 已提交
100 101 102
  return 0;
}

H
Hongze Cheng 已提交
103
static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int size) {
H
Hongze Cheng 已提交
104 105
  // TODO
  return 0;
H
Hongze Cheng 已提交
106 107 108 109 110
}

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