tdbPage.c 8.0 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
typedef struct __attribute__((__packed__)) {
H
Hongze Cheng 已提交
19
  u8 szCell[2];
H
Hongze Cheng 已提交
20
  u8 nxOffset[2];
H
Hongze Cheng 已提交
21 22
} SFreeCell;

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

H
Hongze Cheng 已提交
28
/* For small page */
H
Hongze Cheng 已提交
29
#define TDB_SPAGE_FREE_CELL_SIZE_PTR(PCELL)     (((SFreeCell *)(PCELL))->szCell)
H
Hongze Cheng 已提交
30
#define TDB_SPAGE_FREE_CELL_NXOFFSET_PTR(PCELL) (((SFreeCell *)(PCELL))->nxOffset)
H
Hongze Cheng 已提交
31

H
Hongze Cheng 已提交
32 33 34 35 36
#define TDB_SPAGE_FREE_CELL_SIZE(PCELL)     ((u16 *)TDB_SPAGE_FREE_CELL_SIZE_PTR(PCELL))[0]
#define TDB_SPAGE_FREE_CELL_NXOFFSET(PCELL) ((u16 *)TDB_SPAGE_FREE_CELL_NXOFFSET_PTR(PCELL))[0]

#define TDB_SPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE)       (TDB_SPAGE_FREE_CELL_SIZE(PCELL) = (SIZE))
#define TDB_SPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET) (TDB_SPAGE_FREE_CELL_NXOFFSET(PCELL) = (OFFSET))
H
Hongze Cheng 已提交
37 38

/* For large page */
H
Hongze Cheng 已提交
39
#define TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL)     (((SFreeCellL *)(PCELL))->szCell)
H
Hongze Cheng 已提交
40 41 42 43
#define TDB_LPAGE_FREE_CELL_NXOFFSET_PTR(PCELL) (((SFreeCellL *)(PCELL))->nxOffset)

#define TDB_LPAGE_FREE_CELL_SIZE(PCELL)     TDB_GET_U24(TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL))
#define TDB_LPAGE_FREE_CELL_NXOFFSET(PCELL) TDB_GET_U24(TDB_LPAGE_FREE_CELL_NXOFFSET_PTR(PCELL))
H
Hongze Cheng 已提交
44

H
Hongze Cheng 已提交
45 46
#define TDB_LPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE)       TDB_PUT_U24(TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL), SIZE)
#define TDB_LPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET) TDB_PUT_U24(TDB_LPAGE_FREE_CELL_NXOFFSET_PTR(PCELL), OFFSET)
H
Hongze Cheng 已提交
47 48

/* For page */
H
Hongze Cheng 已提交
49 50 51 52 53
#define TDB_PAGE_FREE_CELL_SIZE_PTR(PPAGE, PCELL) \
  (TDB_IS_LARGE_PAGE(pPage) ? TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL) : TDB_SPAGE_FREE_CELL_SIZE_PTR(PCELL))
#define TDB_PAGE_FREE_CELL_NXOFFSET_PTR(PPAGE, PCELL) \
  (TDB_IS_LARGE_PAGE(pPage) ? TDB_LPAGE_FREE_CELL_NXOFFSET_PTR(PCELL) : TDB_SPAGE_FREE_CELL_NXOFFSET_PTR(PCELL))

H
Hongze Cheng 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
#define TDB_PAGE_FREE_CELL_SIZE(PPAGE, PCELL) \
  (TDB_IS_LARGE_PAGE(pPage) ? TDB_LPAGE_FREE_CELL_SIZE(PCELL) : TDB_SPAGE_FREE_CELL_SIZE(PCELL))
#define TDB_PAGE_FREE_CELL_NXOFFSET(PPAGE, PCELL) \
  (TDB_IS_LARGE_PAGE(pPage) ? TDB_LPAGE_FREE_CELL_NXOFFSET(PCELL) : TDB_SPAGE_FREE_CELL_NXOFFSET(PCELL))

#define TDB_PAGE_FREE_CELL_SIZE_SET(PPAGE, PCELL, SIZE) \
  do {                                                  \
    if (TDB_IS_LARGE_PAGE(PPAGE)) {                     \
      TDB_LPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE);        \
    } else {                                            \
      TDB_SPAGE_FREE_CELL_SIZE_SET(PCELL, SIZE);        \
    }                                                   \
  } while (0)
#define TDB_PAGE_FREE_CELL_NXOFFSET_SET(PPAGE, PCELL, OFFSET) \
  do {                                                        \
    if (TDB_IS_LARGE_PAGE(PPAGE)) {                           \
      TDB_LPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET);        \
    } else {                                                  \
      TDB_SPAGE_FREE_CELL_NXOFFSET_SET(PCELL, OFFSET);        \
    }                                                         \
  } while (0)
H
Hongze Cheng 已提交
75

H
Hongze Cheng 已提交
76
static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell);
H
Hongze Cheng 已提交
77
static int tdbPageDefragment(SPage *pPage);
H
Hongze Cheng 已提交
78

H
Hongze Cheng 已提交
79
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg) {
H
Hongze Cheng 已提交
80 81 82 83
  SPage *pPage;
  u8    *ptr;
  int    size;

H
Hongze Cheng 已提交
84 85
  ASSERT(TDB_IS_PGSIZE_VLD(pageSize));

H
Hongze Cheng 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98
  *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 已提交
99 100
  if (pageSize < 65536) {
    pPage->szOffset = 2;
H
Hongze Cheng 已提交
101
    pPage->szPageHdr = sizeof(SPageHdr);
H
Hongze Cheng 已提交
102
    pPage->szFreeCell = sizeof(SFreeCell);
H
Hongze Cheng 已提交
103 104
  } else {
    pPage->szOffset = 3;
H
Hongze Cheng 已提交
105
    pPage->szPageHdr = sizeof(SPageHdrL);
H
Hongze Cheng 已提交
106
    pPage->szFreeCell = sizeof(SFreeCellL);
H
Hongze Cheng 已提交
107
  }
H
Hongze Cheng 已提交
108
  TDB_INIT_PAGE_LOCK(pPage);
H
Hongze Cheng 已提交
109 110 111 112

  /* TODO */

  *ppPage = pPage;
H
Hongze Cheng 已提交
113 114 115
  return 0;
}

H
Hongze Cheng 已提交
116
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
H
Hongze Cheng 已提交
117 118 119
  u8 *ptr;

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

H
Hongze Cheng 已提交
122 123 124
  return 0;
}

H
Hongze Cheng 已提交
125
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
H
Hongze Cheng 已提交
126 127
  int    ret;
  SCell *pTarget;
H
Hongze Cheng 已提交
128
  u8    *pTmp;
H
Hongze Cheng 已提交
129
  int    j;
H
Hongze Cheng 已提交
130

H
Hongze Cheng 已提交
131
  if (pPage->nOverflow || szCell + pPage->szOffset > pPage->nFree) {
H
Hongze Cheng 已提交
132 133 134 135 136
    // TODO: need to figure out if pCell may be used by outside of this function
    j = pPage->nOverflow++;

    pPage->apOvfl[j] = pCell;
    pPage->aiOvfl[j] = idx;
H
Hongze Cheng 已提交
137 138 139 140 141
  } else {
    ret = tdbPageAllocate(pPage, szCell, &pTarget);
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
142 143

    memcpy(pTarget, pCell, szCell);
H
Hongze Cheng 已提交
144
    pTmp = pPage->pCellIdx + idx * pPage->szOffset;
H
Hongze Cheng 已提交
145
    memmove(pTmp + pPage->szOffset, pTmp, pPage->pFreeStart - pTmp - pPage->szOffset);
H
Hongze Cheng 已提交
146
    TDB_PAGE_CELL_OFFSET_AT_SET(pPage, idx, pTarget - pPage->pData);
H
Hongze Cheng 已提交
147
    TDB_PAGE_NCELLS_SET(pPage, TDB_PAGE_NCELLS(pPage) + 1);
H
Hongze Cheng 已提交
148 149
  }

H
Hongze Cheng 已提交
150 151 152 153 154 155
  return 0;
}

int tdbPageDropCell(SPage *pPage, int idx) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
156 157 158
}

static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell) {
H
Hongze Cheng 已提交
159 160
  SCell     *pCell;
  SFreeCell *pFreeCell;
H
Hongze Cheng 已提交
161
  u8        *pOffset;
H
Hongze Cheng 已提交
162
  int        ret;
H
Hongze Cheng 已提交
163

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

H
Hongze Cheng 已提交
166 167 168 169
  pCell = NULL;
  *ppCell = NULL;

  // 1. Try to allocate from the free space area
H
Hongze Cheng 已提交
170
  if (pPage->pFreeEnd - pPage->pFreeStart > size + pPage->szOffset) {
H
Hongze Cheng 已提交
171
    pPage->pFreeEnd -= size;
H
Hongze Cheng 已提交
172
    pPage->pFreeStart += pPage->szOffset;
H
Hongze Cheng 已提交
173 174 175
    pCell = pPage->pFreeEnd;
  }

H
Hongze Cheng 已提交
176
  // 2. Try to allocate from the page free list
H
Hongze Cheng 已提交
177
  if ((pCell == NULL) && (pPage->pFreeEnd - pPage->pFreeStart >= pPage->szOffset) && TDB_PAGE_FCELL(pPage)) {
H
Hongze Cheng 已提交
178 179 180
    int szCell;
    int nxOffset;

H
Hongze Cheng 已提交
181
    pCell = pPage->pData + TDB_PAGE_FCELL(pPage);
H
Hongze Cheng 已提交
182
    pOffset = TDB_IS_LARGE_PAGE(pPage) ? ((SPageHdrL *)(pPage->pPageHdr))[0].fCell
H
Hongze Cheng 已提交
183
                                       : (u8 *)&(((SPageHdr *)(pPage->pPageHdr))[0].fCell);
H
Hongze Cheng 已提交
184 185
    szCell = TDB_PAGE_FREE_CELL_SIZE(pPage, pCell);
    nxOffset = TDB_PAGE_FREE_CELL_NXOFFSET(pPage, pCell);
H
Hongze Cheng 已提交
186 187

    for (;;) {
H
Hongze Cheng 已提交
188 189 190 191 192 193 194 195
      // Find a cell
      if (szCell >= size) {
        if (szCell - size >= pPage->szFreeCell) {
          SCell *pTmpCell = pCell + size;

          TDB_PAGE_FREE_CELL_SIZE_SET(pPage, pTmpCell, szCell - size);
          TDB_PAGE_FREE_CELL_NXOFFSET_SET(pPage, pTmpCell, nxOffset);
          // TODO: *pOffset = pTmpCell - pPage->pData;
H
Hongze Cheng 已提交
196
        } else {
H
Hongze Cheng 已提交
197 198
          TDB_PAGE_NFREE_SET(pPage, TDB_PAGE_NFREE(pPage) + szCell - size);
          // TODO: *pOffset = nxOffset;
H
Hongze Cheng 已提交
199
        }
H
Hongze Cheng 已提交
200 201 202
        break;
      }

H
Hongze Cheng 已提交
203 204 205 206 207 208 209
      // Not find a cell yet
      if (nxOffset > 0) {
        pCell = pPage->pData + nxOffset;
        pOffset = TDB_PAGE_FREE_CELL_NXOFFSET_PTR(pPage, pCell);
        szCell = TDB_PAGE_FREE_CELL_SIZE(pPage, pCell);
        nxOffset = TDB_PAGE_FREE_CELL_NXOFFSET(pPage, pCell);
        continue;
H
Hongze Cheng 已提交
210 211 212 213 214
      } else {
        pCell = NULL;
        break;
      }
    }
H
Hongze Cheng 已提交
215 216

    if (pCell) {
H
Hongze Cheng 已提交
217
      pPage->pFreeStart = pPage->pFreeStart + pPage->szOffset;
H
Hongze Cheng 已提交
218
    }
H
Hongze Cheng 已提交
219 220
  }

H
Hongze Cheng 已提交
221
  // 3. Try to dfragment and allocate again
H
Hongze Cheng 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
  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 已提交
240
  *ppCell = pCell;
H
Hongze Cheng 已提交
241 242 243
  return 0;
}

H
Hongze Cheng 已提交
244
static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int size) {
H
Hongze Cheng 已提交
245 246
  // TODO
  return 0;
H
Hongze Cheng 已提交
247 248 249 250
}

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