tdbPage.h 4.7 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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/>.
 */

#ifndef _TDB_PAGE_H_
#define _TDB_PAGE_H_

#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
23 24
typedef u8 SCell;

H
Hongze Cheng 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
// PAGE APIS implemented
typedef struct {
  int szOffset;
  int szPageHdr;
  int szFreeCell;
  // cell number
  int (*getCellNum)(SPage *);
  void (*setCellNum)(SPage *, int);
  // cell content offset
  int (*getCellBody)(SPage *);
  void (*setCellBody)(SPage *, int);
  // first free cell offset (0 means no free cells)
  int (*getCellFree)(SPage *);
  void (*setCellFree)(SPage *, int);
  // total free bytes
  int (*getFreeBytes)(SPage *);
  void (*setFreeBytes)(SPage *, int);
  // cell offset at idx
  int (*getCellOffset)(SPage *, int);
  void (*setCellOffset)(SPage *, int, int);
H
Hongze Cheng 已提交
45 46 47
  // free cell info
  void (*getFreeCellInfo)(SCell *pCell, int *szCell, int *nxOffset);
  void (*setFreeCellInfo)(SCell *pCell, int szCell, int nxOffset);
H
Hongze Cheng 已提交
48
} SPageMethods;
H
Hongze Cheng 已提交
49

H
Hongze Cheng 已提交
50
// Page footer
H
Hongze Cheng 已提交
51 52 53 54
typedef struct __attribute__((__packed__)) {
  u8 cksm[4];
} SPageFtr;

H
refact  
Hongze Cheng 已提交
55
struct SPage {
H
Hongze Cheng 已提交
56
  pthread_spinlock_t lock;
H
Hongze Cheng 已提交
57
  int                pageSize;
H
Hongze Cheng 已提交
58
  u8                *pData;
H
Hongze Cheng 已提交
59
  SPageMethods      *pPageMethods;
H
refact  
Hongze Cheng 已提交
60
  // Fields below used by pager and am
H
Hongze Cheng 已提交
61
  u8       *pPageHdr;
H
Hongze Cheng 已提交
62
  u8       *pCellIdx;
H
Hongze Cheng 已提交
63 64
  u8       *pFreeStart;
  u8       *pFreeEnd;
H
Hongze Cheng 已提交
65
  SPageFtr *pPageFtr;
H
Hongze Cheng 已提交
66 67 68
  int       nOverflow;
  SCell    *apOvfl[4];
  int       aiOvfl[4];
H
Hongze Cheng 已提交
69 70
  int       kLen;  // key length of the page, -1 for unknown
  int       vLen;  // value length of the page, -1 for unknown
H
Hongze Cheng 已提交
71 72
  int       maxLocal;
  int       minLocal;
H
Hongze Cheng 已提交
73
  int (*xCellSize)(const SPage *, SCell *);
H
Hongze Cheng 已提交
74 75
  // Fields used by SPCache
  TDB_PCACHE_PAGE
H
refact  
Hongze Cheng 已提交
76
};
H
Hongze Cheng 已提交
77

H
Hongze Cheng 已提交
78
// For page lock
H
Hongze Cheng 已提交
79 80 81 82
#define P_LOCK_SUCC 0
#define P_LOCK_BUSY 1
#define P_LOCK_FAIL -1

H
Hongze Cheng 已提交
83
#define TDB_INIT_PAGE_LOCK(pPage)    pthread_spin_init(&((pPage)->lock), 0)
H
Hongze Cheng 已提交
84 85 86
#define TDB_DESTROY_PAGE_LOCK(pPage) pthread_spin_destroy(&((pPage)->lock))
#define TDB_LOCK_PAGE(pPage)         pthread_spin_lock(&((pPage)->lock))
#define TDB_UNLOCK_PAGE(pPage)       pthread_spin_unlock(&((pPage)->lock))
H
Hongze Cheng 已提交
87 88 89 90 91 92 93 94 95 96 97 98
#define TDB_TRY_LOCK_PAGE(pPage)                       \
  ({                                                   \
    int ret;                                           \
    if (pthread_spin_trylock(&((pPage)->lock)) == 0) { \
      ret = P_LOCK_SUCC;                               \
    } else if (errno == EBUSY) {                       \
      ret = P_LOCK_BUSY;                               \
    } else {                                           \
      ret = P_LOCK_FAIL;                               \
    }                                                  \
    ret;                                               \
  })
H
Hongze Cheng 已提交
99

H
Hongze Cheng 已提交
100
// APIs
H
Hongze Cheng 已提交
101
#define TDB_PAGE_TOTAL_CELLS(pPage)        ((pPage)->nOverflow + (pPage)->pPageMethods->getCellNum(pPage))
H
Hongze Cheng 已提交
102
#define TDB_PAGE_USABLE_SIZE(pPage)        ((u8 *)(pPage)->pPageFtr - (pPage)->pCellIdx)
H
Hongze Cheng 已提交
103
#define TDB_PAGE_PGNO(pPage)               ((pPage)->pgid.pgno)
H
Hongze Cheng 已提交
104
#define TDB_BYTES_CELL_TAKEN(pPage, pCell) ((*(pPage)->xCellSize)(pPage, pCell) + (pPage)->pPageMethods->szOffset)
H
Hongze Cheng 已提交
105
#define TDB_PAGE_OFFSET_SIZE(pPage)        ((pPage)->pPageMethods->szOffset)
H
Hongze Cheng 已提交
106

H
Hongze Cheng 已提交
107 108
int  tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg);
int  tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg);
H
Hongze Cheng 已提交
109 110
void tdbPageZero(SPage *pPage, u8 szAmHdr, int (*xCellSize)(const SPage *, SCell *));
void tdbPageInit(SPage *pPage, u8 szAmHdr, int (*xCellSize)(const SPage *, SCell *));
H
Hongze Cheng 已提交
111
int  tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell, u8 asOvfl);
H
Hongze Cheng 已提交
112
int  tdbPageDropCell(SPage *pPage, int idx);
H
Hongze Cheng 已提交
113
void tdbPageCopy(SPage *pFromPage, SPage *pToPage);
H
Hongze Cheng 已提交
114

H
Hongze Cheng 已提交
115 116 117 118 119
static inline SCell *tdbPageGetCell(SPage *pPage, int idx) {
  SCell *pCell;
  int    iOvfl;
  int    lidx;

H
Hongze Cheng 已提交
120
  ASSERT(idx >= 0 && idx < TDB_PAGE_TOTAL_CELLS(pPage));
H
Hongze Cheng 已提交
121 122 123 124 125 126 127 128 129 130 131 132

  iOvfl = 0;
  for (; iOvfl < pPage->nOverflow; iOvfl++) {
    if (pPage->aiOvfl[iOvfl] == idx) {
      pCell = pPage->apOvfl[iOvfl];
      return pCell;
    } else if (pPage->aiOvfl[iOvfl] > idx) {
      break;
    }
  }

  lidx = idx - iOvfl;
H
Hongze Cheng 已提交
133
  ASSERT(lidx >= 0 && lidx < pPage->pPageMethods->getCellNum(pPage));
H
Hongze Cheng 已提交
134 135 136 137 138
  pCell = pPage->pData + pPage->pPageMethods->getCellOffset(pPage, lidx);

  return pCell;
}

H
Hongze Cheng 已提交
139 140 141 142 143
#ifdef __cplusplus
}
#endif

#endif /*_TDB_PAGE_H_*/