tdbPage.h 4.5 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 45 46 47
// PAGE APIS implemented
typedef struct {
  int szOffset;
  int szPageHdr;
  int szFreeCell;
  // flags
  u16 (*getFlags)(SPage *);
  void (*setFlags)(SPage *, u16);
  // 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 已提交
48 49 50
  // free cell info
  void (*getFreeCellInfo)(SCell *pCell, int *szCell, int *nxOffset);
  void (*setFreeCellInfo)(SCell *pCell, int szCell, int nxOffset);
H
Hongze Cheng 已提交
51
} SPageMethods;
H
Hongze Cheng 已提交
52

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

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

H
Hongze Cheng 已提交
82
/* For page */
H
Hongze Cheng 已提交
83 84
#define TDB_PAGE_FLAGS(pPage)            (*(pPage)->pPageMethods->getFlags)(pPage)
#define TDB_PAGE_FLAGS_SET(pPage, FLAGS) (*(pPage)->pPageMethods->setFlags)(pPage, FLAGS)
H
Hongze Cheng 已提交
85

H
Hongze Cheng 已提交
86
// For page lock
H
Hongze Cheng 已提交
87 88 89 90
#define P_LOCK_SUCC 0
#define P_LOCK_BUSY 1
#define P_LOCK_FAIL -1

H
Hongze Cheng 已提交
91
#define TDB_INIT_PAGE_LOCK(pPage)    pthread_spin_init(&((pPage)->lock), 0)
H
Hongze Cheng 已提交
92 93 94
#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 已提交
95 96 97 98 99 100 101 102 103 104 105 106
#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 已提交
107

H
Hongze Cheng 已提交
108
// APIs
H
Hongze Cheng 已提交
109 110
#define TDB_PAGE_TOTAL_CELLS(pPage) ((pPage)->nOverflow + (pPage)->pPageMethods->getCellNum(pPage))

H
Hongze Cheng 已提交
111 112
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 已提交
113 114
void tdbPageZero(SPage *pPage, u8 szAmHdr);
void tdbPageInit(SPage *pPage, u8 szAmHdr);
H
Hongze Cheng 已提交
115 116
int  tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell);
int  tdbPageDropCell(SPage *pPage, int idx);
H
Hongze Cheng 已提交
117

H
Hongze Cheng 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static inline SCell *tdbPageGetCell(SPage *pPage, int idx) {
  SCell *pCell;
  int    iOvfl;
  int    lidx;

  ASSERT(idx >= 0 && idx < pPage->nOverflow + pPage->pPageMethods->getCellNum(pPage));

  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 已提交
136
  ASSERT(lidx >= 0 && lidx < pPage->pPageMethods->getCellNum(pPage));
H
Hongze Cheng 已提交
137 138 139 140 141
  pCell = pPage->pData + pPage->pPageMethods->getCellOffset(pPage, lidx);

  return pCell;
}

H
Hongze Cheng 已提交
142 143 144 145 146
#ifdef __cplusplus
}
#endif

#endif /*_TDB_PAGE_H_*/