tdbPage.c 7.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 20
  u8 size[2];
  u8 nxOffset[2];
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
/* For small page */
H
Hongze Cheng 已提交
29 30
#define TDB_SPAGE_FREE_CELL_SIZE_PTR(PCELL)     (((SFreeCell *)(PCELL))->size)
#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 40 41 42 43
#define TDB_LPAGE_FREE_CELL_SIZE_PTR(PCELL)     (((SFreeCellL *)(PCELL))->size)
#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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#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 已提交
70

H
Hongze Cheng 已提交
71
static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell);
H
Hongze Cheng 已提交
72
static int tdbPageDefragment(SPage *pPage);
H
Hongze Cheng 已提交
73

H
Hongze Cheng 已提交
74
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg) {
H
Hongze Cheng 已提交
75 76 77 78
  SPage *pPage;
  u8    *ptr;
  int    size;

H
Hongze Cheng 已提交
79 80
  ASSERT(TDB_IS_PGSIZE_VLD(pageSize));

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

  /* TODO */

  *ppPage = pPage;
H
Hongze Cheng 已提交
108 109 110
  return 0;
}

H
Hongze Cheng 已提交
111
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg) {
H
Hongze Cheng 已提交
112 113 114
  u8 *ptr;

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

H
Hongze Cheng 已提交
117 118 119
  return 0;
}

H
Hongze Cheng 已提交
120
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell) {
H
Hongze Cheng 已提交
121 122 123
  int    ret;
  SCell *pTarget;

H
Hongze Cheng 已提交
124
  if (pPage->nOverflow || szCell + pPage->szOffset > pPage->nFree) {
H
Hongze Cheng 已提交
125
    // TODO: Page is full
H
Hongze Cheng 已提交
126 127 128 129 130
  } else {
    ret = tdbPageAllocate(pPage, szCell, &pTarget);
    if (ret < 0) {
      return -1;
    }
H
Hongze Cheng 已提交
131 132 133 134

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

H
Hongze Cheng 已提交
137 138 139 140 141 142
  return 0;
}

int tdbPageDropCell(SPage *pPage, int idx) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
143 144 145
}

static int tdbPageAllocate(SPage *pPage, int size, SCell **ppCell) {
H
Hongze Cheng 已提交
146 147
  SCell     *pCell;
  SFreeCell *pFreeCell;
H
Hongze Cheng 已提交
148
  u8        *pOffset;
H
Hongze Cheng 已提交
149
  int        ret;
H
Hongze Cheng 已提交
150

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

H
Hongze Cheng 已提交
153 154 155 156
  pCell = NULL;
  *ppCell = NULL;

  // 1. Try to allocate from the free space area
H
Hongze Cheng 已提交
157
  if (pPage->pFreeEnd - pPage->pFreeStart > size + pPage->szOffset) {
H
Hongze Cheng 已提交
158
    pPage->pFreeEnd -= size;
H
Hongze Cheng 已提交
159
    pPage->pFreeStart += pPage->szOffset;
H
Hongze Cheng 已提交
160 161 162
    pCell = pPage->pFreeEnd;
  }

H
Hongze Cheng 已提交
163
  // 2. Try to allocate from the page free list
H
Hongze Cheng 已提交
164
  if ((pCell == NULL) && (pPage->pFreeEnd - pPage->pFreeStart >= pPage->szOffset) && TDB_PAGE_FCELL(pPage)) {
H
Hongze Cheng 已提交
165
    pCell = pPage->pData + TDB_PAGE_FCELL(pPage);
H
Hongze Cheng 已提交
166 167 168 169 170 171 172
    pOffset = TDB_IS_LARGE_PAGE(pPage) ? ((SLPageHdr *)(pPage->pPageHdr))[0].fCell
                                       : (u8 *)&(((SPageHdr *)(pPage->pPageHdr))[0].fCell);

    for (;;) {
      if (TDB_PAGE_FREE_CELL_SIZE(pPage, pCell) >= size) {
      }
    }
H
Hongze Cheng 已提交
173

H
Hongze Cheng 已提交
174
#if 0
H
Hongze Cheng 已提交
175 176 177 178
    for (;;) {
      pFreeCell = (SFreeCell *)pCell;

      if (pFreeCell->size >= size) {
H
Hongze Cheng 已提交
179 180
        if (pFreeCell->size - size >= 4 /*TODO*/) {
          ((SFreeCell *)(pCell + size))[0].size = pFreeCell->size - size;
H
Hongze Cheng 已提交
181
          ((SFreeCell *)(pCell + size))[0].nxOffset = pFreeCell->nxOffset;
H
Hongze Cheng 已提交
182 183 184 185 186
          // *(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 已提交
187 188 189
        break;
      }

H
Hongze Cheng 已提交
190 191 192
      if (pFreeCell->nxOffset) {
        pCell = pPage->pData + pFreeCell->nxOffset;
        // TODO: pOffset = &(pFreeCell->nxOffset);
H
Hongze Cheng 已提交
193 194 195 196 197 198 199
      } else {
        pCell = NULL;
        break;
      }

      continue;
    }
H
Hongze Cheng 已提交
200 201 202 203 204

    if (pCell) {
      // TODO
    }
#endif
H
Hongze Cheng 已提交
205 206
  }

H
Hongze Cheng 已提交
207
  // 3. Try to dfragment and allocate again
H
Hongze Cheng 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
  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 已提交
226
  *ppCell = pCell;
H
Hongze Cheng 已提交
227 228 229
  return 0;
}

H
Hongze Cheng 已提交
230
static int tdbPageFree(SPage *pPage, int idx, SCell *pCell, int size) {
H
Hongze Cheng 已提交
231 232
  // TODO
  return 0;
H
Hongze Cheng 已提交
233 234 235 236 237
}

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