tdbPage.c 6.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
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
/* 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 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#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 已提交
64

H
Hongze Cheng 已提交
65
static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell);
H
Hongze Cheng 已提交
66
static int tdbPageDefragment(SPage *pPage);
H
Hongze Cheng 已提交
67

H
Hongze Cheng 已提交
68
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg) {
H
Hongze Cheng 已提交
69 70 71 72
  SPage *pPage;
  u8    *ptr;
  int    size;

H
Hongze Cheng 已提交
73 74
  ASSERT(TDB_IS_PGSIZE_VLD(pageSize));

H
Hongze Cheng 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87
  *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 已提交
88 89
  if (pageSize < 65536) {
    pPage->szOffset = 2;
H
Hongze Cheng 已提交
90
    pPage->szPageHdr = sizeof(SPageHdr);
H
Hongze Cheng 已提交
91
    pPage->szFreeCell = sizeof(SFreeCell);
H
Hongze Cheng 已提交
92 93
  } else {
    pPage->szOffset = 3;
H
Hongze Cheng 已提交
94
    pPage->szPageHdr = sizeof(SLPageHdr);
H
Hongze Cheng 已提交
95
    pPage->szFreeCell = sizeof(SFreeCellL);
H
Hongze Cheng 已提交
96
  }
H
Hongze Cheng 已提交
97
  TDB_INIT_PAGE_LOCK(pPage);
H
Hongze Cheng 已提交
98 99 100 101

  /* TODO */

  *ppPage = pPage;
H
Hongze Cheng 已提交
102 103 104
  return 0;
}

H
Hongze Cheng 已提交
105
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
H
Hongze Cheng 已提交
106 107 108
  u8 *ptr;

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

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

H
Hongze Cheng 已提交
114
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
H
Hongze Cheng 已提交
115 116 117
  int    ret;
  SCell *pTarget;

H
Hongze Cheng 已提交
118
  if (pPage->nOverflow || szCell + pPage->szOffset > pPage->nFree) {
H
Hongze Cheng 已提交
119
    // TODO: Page is full
H
Hongze Cheng 已提交
120 121 122 123 124
  } else {
    ret = tdbPageAllocate(pPage, szCell, &pTarget);
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
125 126 127 128

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

H
Hongze Cheng 已提交
131 132 133 134 135 136
  return 0;
}

int tdbPageDropCell(SPage *pPage, int idx) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
137 138 139
}

static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell) {
H
Hongze Cheng 已提交
140 141
  SCell     *pCell;
  SFreeCell *pFreeCell;
H
Hongze Cheng 已提交
142
  u8        *pOffset;
H
Hongze Cheng 已提交
143
  int        ret;
H
Hongze Cheng 已提交
144

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

H
Hongze Cheng 已提交
147 148 149 150
  pCell = NULL;
  *ppCell = NULL;

  // 1. Try to allocate from the free space area
H
Hongze Cheng 已提交
151
  if (pPage->pFreeEnd - pPage->pFreeStart > size + pPage->szOffset) {
H
Hongze Cheng 已提交
152
    pPage->pFreeEnd -= size;
H
Hongze Cheng 已提交
153
    pPage->pFreeStart += pPage->szOffset;
H
Hongze Cheng 已提交
154 155 156
    pCell = pPage->pFreeEnd;
  }

H
Hongze Cheng 已提交
157
  // 2. Try to allocate from the page free list
H
Hongze Cheng 已提交
158
  if ((pCell == NULL) && (pPage->pFreeEnd - pPage->pFreeStart >= pPage->szOffset) && TDB_PAGE_FCELL(pPage)) {
H
Hongze Cheng 已提交
159
    pCell = pPage->pData + TDB_PAGE_FCELL(pPage);
H
Hongze Cheng 已提交
160 161
    // TODO: pOffset = TDB_PAGE_FCELL(pPage);

H
Hongze Cheng 已提交
162 163 164 165
    for (;;) {
      pFreeCell = (SFreeCell *)pCell;

      if (pFreeCell->size >= size) {
H
Hongze Cheng 已提交
166 167
        if (pFreeCell->size - size >= 4 /*TODO*/) {
          ((SFreeCell *)(pCell + size))[0].size = pFreeCell->size - size;
H
Hongze Cheng 已提交
168
          ((SFreeCell *)(pCell + size))[0].nxOffset = pFreeCell->nxOffset;
H
Hongze Cheng 已提交
169 170 171 172 173
          // *(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 已提交
174 175 176
        break;
      }

H
Hongze Cheng 已提交
177 178 179
      if (pFreeCell->nxOffset) {
        pCell = pPage->pData + pFreeCell->nxOffset;
        // TODO: pOffset = &(pFreeCell->nxOffset);
H
Hongze Cheng 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
      } 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 已提交
208
  *ppCell = pCell;
H
Hongze Cheng 已提交
209 210 211
  return 0;
}

H
Hongze Cheng 已提交
212
static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int size) {
H
Hongze Cheng 已提交
213 214
  // TODO
  return 0;
H
Hongze Cheng 已提交
215 216 217 218 219
}

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