tdbPage.c 5.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
typedef struct __attribute__((__packed__)) {
  u16 size;
H
Hongze Cheng 已提交
20
  u16 nxOffset;
H
Hongze Cheng 已提交
21 22
} SFreeCell;

H
Hongze Cheng 已提交
23 24
typedef struct __attribute__((__packed__)) {
  u8 size[3];
H
Hongze Cheng 已提交
25
  u8 nxOffset[3];
H
Hongze Cheng 已提交
26 27
} SFreeCellL;

H
Hongze Cheng 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
/* For small page */
#define TDB_SPAGE_FREE_CELL_SIZE(PCELL)     (((SFreeCell *)(PCELL))[0].size)
#define TDB_SPAGE_FREE_CELL_NXOFFSET(PCELL) (((SFreeCell *)(PCELL))[0].nxOffset)

#define TDB_SPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE)       (((SFreeCell *)(PCELL))[0].size = (SIZE))
#define TDB_SPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET) (((SFreeCell *)(PCELL))[0].nxOffset = (OFFSET))

/* For large page */
#define TDB_LPAGE_FREE_CELL_SIZE(PCELL)     TDB_GET_U24(((SFreeCellL *)(PCELL))[0].size)
#define TDB_LPAGE_FREE_CELL_NXOFFSET(PCELL) TDB_GET_U24(((SFreeCellL *)(PCELL))[0].nxOffset)

#define TDB_LPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE)       TDB_PUT_U24(((SFreeCellL *)(PCELL))[0].size, SIZE)
#define TDB_LPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET) TDB_PUT_U24(((SFreeCellL *)(PCELL))[0].nxOffset, OFFSET)

/* For page */

H
Hongze Cheng 已提交
44
static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell);
H
Hongze Cheng 已提交
45
static int tdbPageDefragment(SPage *pPage);
H
Hongze Cheng 已提交
46

H
Hongze Cheng 已提交
47
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg) {
H
Hongze Cheng 已提交
48 49 50 51
  SPage *pPage;
  u8    *ptr;
  int    size;

H
Hongze Cheng 已提交
52 53
  ASSERT(TDB_IS_PGSIZE_VLD(pageSize));

H
Hongze Cheng 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66
  *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 已提交
67 68
  if (pageSize < 65536) {
    pPage->szOffset = 2;
H
Hongze Cheng 已提交
69
    pPage->szPageHdr = sizeof(SPageHdr);
H
Hongze Cheng 已提交
70
    pPage->szFreeCell = sizeof(SFreeCell);
H
Hongze Cheng 已提交
71 72
  } else {
    pPage->szOffset = 3;
H
Hongze Cheng 已提交
73
    pPage->szPageHdr = sizeof(SLPageHdr);
H
Hongze Cheng 已提交
74
    pPage->szFreeCell = sizeof(SFreeCellL);
H
Hongze Cheng 已提交
75
  }
H
Hongze Cheng 已提交
76
  TDB_INIT_PAGE_LOCK(pPage);
H
Hongze Cheng 已提交
77 78 79 80

  /* TODO */

  *ppPage = pPage;
H
Hongze Cheng 已提交
81 82 83
  return 0;
}

H
Hongze Cheng 已提交
84
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
H
Hongze Cheng 已提交
85 86 87
  u8 *ptr;

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

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

H
Hongze Cheng 已提交
93
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
H
Hongze Cheng 已提交
94 95 96
  int    ret;
  SCell *pTarget;

H
Hongze Cheng 已提交
97
  if (pPage->nOverflow || szCell + pPage->szOffset > pPage->nFree) {
H
Hongze Cheng 已提交
98
    // TODO: Page is full
H
Hongze Cheng 已提交
99 100 101 102 103
  } else {
    ret = tdbPageAllocate(pPage, szCell, &pTarget);
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
104 105 106 107

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

H
Hongze Cheng 已提交
110 111 112 113 114 115
  return 0;
}

int tdbPageDropCell(SPage *pPage, int idx) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
116 117 118
}

static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell) {
H
Hongze Cheng 已提交
119 120
  SCell     *pCell;
  SFreeCell *pFreeCell;
H
Hongze Cheng 已提交
121
  u8        *pOffset;
H
Hongze Cheng 已提交
122
  int        ret;
H
Hongze Cheng 已提交
123

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

H
Hongze Cheng 已提交
126 127 128 129
  pCell = NULL;
  *ppCell = NULL;

  // 1. Try to allocate from the free space area
H
Hongze Cheng 已提交
130
  if (pPage->pFreeEnd - pPage->pFreeStart > size + pPage->szOffset) {
H
Hongze Cheng 已提交
131
    pPage->pFreeEnd -= size;
H
Hongze Cheng 已提交
132
    pPage->pFreeStart += pPage->szOffset;
H
Hongze Cheng 已提交
133 134 135
    pCell = pPage->pFreeEnd;
  }

H
Hongze Cheng 已提交
136
  // 2. Try to allocate from the page free list
H
Hongze Cheng 已提交
137
  if ((pCell == NULL) && (pPage->pFreeEnd - pPage->pFreeStart >= pPage->szOffset) && TDB_PAGE_FCELL(pPage)) {
H
Hongze Cheng 已提交
138
    pCell = pPage->pData + TDB_PAGE_FCELL(pPage);
H
Hongze Cheng 已提交
139 140
    // TODO: pOffset = TDB_PAGE_FCELL(pPage);

H
Hongze Cheng 已提交
141 142 143 144
    for (;;) {
      pFreeCell = (SFreeCell *)pCell;

      if (pFreeCell->size >= size) {
H
Hongze Cheng 已提交
145 146
        if (pFreeCell->size - size >= 4 /*TODO*/) {
          ((SFreeCell *)(pCell + size))[0].size = pFreeCell->size - size;
H
Hongze Cheng 已提交
147
          ((SFreeCell *)(pCell + size))[0].nxOffset = pFreeCell->nxOffset;
H
Hongze Cheng 已提交
148 149 150 151 152
          // *(u16 *)pOffset =  pCell + size - pPage->pData;
        } else {
          TDB_PAGE_NFREE_SET(pPage, TDB_PAGE_NFREE(pPage) + pFreeCell->size - size);
          // *(u16 *)pOffset = pFreeCell->nOffset;
        }
H
Hongze Cheng 已提交
153 154 155
        break;
      }

H
Hongze Cheng 已提交
156 157 158
      if (pFreeCell->nxOffset) {
        pCell = pPage->pData + pFreeCell->nxOffset;
        // TODO: pOffset = &(pFreeCell->nxOffset);
H
Hongze Cheng 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
      } else {
        pCell = NULL;
        break;
      }

      continue;
    }
  }

  // 3. Try to dfragment
  if (pCell == NULL) {
    ret = tdbPageDefragment(pPage);
    if (ret < 0) {
      return -1;
    }

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

    // Allocate from the free space area again
    pPage->pFreeEnd -= size;
    pPage->pFreeStart += pPage->szOffset;
    pCell = pPage->pFreeEnd;
  }

  ASSERT(pCell != NULL);

  pPage->nFree = pPage->nFree - size - pPage->szOffset;
H
Hongze Cheng 已提交
187
  *ppCell = pCell;
H
Hongze Cheng 已提交
188 189 190
  return 0;
}

H
Hongze Cheng 已提交
191
static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int size) {
H
Hongze Cheng 已提交
192 193
  // TODO
  return 0;
H
Hongze Cheng 已提交
194 195 196 197 198
}

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