tdbPage.h 4.8 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 48
// 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);
} 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 {
wafwerar's avatar
wafwerar 已提交
56
  TdThreadSpinlock lock;
H
Hongze Cheng 已提交
57
  u8                *pData;
H
Hongze Cheng 已提交
58
  int                pageSize;
H
Hongze Cheng 已提交
59
  SPageMethods      *pPageMethods;
H
refact  
Hongze Cheng 已提交
60
  // Fields below used by pager and am
H
Hongze Cheng 已提交
61
  u8        szAmHdr;
H
Hongze Cheng 已提交
62
  u8       *pPageHdr;
H
Hongze Cheng 已提交
63
  u8       *pAmHdr;
H
Hongze Cheng 已提交
64
  u8       *pCellIdx;
H
Hongze Cheng 已提交
65 66
  u8       *pFreeStart;
  u8       *pFreeEnd;
H
Hongze Cheng 已提交
67
  SPageFtr *pPageFtr;
H
Hongze Cheng 已提交
68 69
  int       kLen;  // key length of the page, -1 for unknown
  int       vLen;  // value length of the page, -1 for unknown
H
Hongze Cheng 已提交
70
  int       nFree;
H
Hongze Cheng 已提交
71 72 73
  int       maxLocal;
  int       minLocal;
  int       nOverflow;
H
Hongze Cheng 已提交
74
  SCell    *apOvfl[4];
H
Hongze Cheng 已提交
75
  int       aiOvfl[4];
H
Hongze Cheng 已提交
76 77
  // Fields used by SPCache
  TDB_PCACHE_PAGE
H
refact  
Hongze Cheng 已提交
78
};
H
Hongze Cheng 已提交
79

H
Hongze Cheng 已提交
80
/* For page */
H
Hongze Cheng 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#define TDB_PAGE_FLAGS(pPage)               (*(pPage)->pPageMethods->getFlags)(pPage)
#define TDB_PAGE_NCELLS(pPage)              (*(pPage)->pPageMethods->getCellNum)(pPage)
#define TDB_PAGE_CCELLS(pPage)              (*(pPage)->pPageMethods->getCellBody)(pPage)
#define TDB_PAGE_FCELL(pPage)               (*(pPage)->pPageMethods->getCellFree)(pPage)
#define TDB_PAGE_NFREE(pPage)               (*(pPage)->pPageMethods->getFreeBytes)(pPage)
#define TDB_PAGE_CELL_OFFSET_AT(pPage, idx) (*(pPage)->pPageMethods->getCellOffset)(pPage, idx)

#define TDB_PAGE_FLAGS_SET(pPage, FLAGS)                (*(pPage)->pPageMethods->setFlags)(pPage, FLAGS)
#define TDB_PAGE_NCELLS_SET(pPage, NCELLS)              (*(pPage)->pPageMethods->setCellNum)(pPage, NCELLS)
#define TDB_PAGE_CCELLS_SET(pPage, CCELLS)              (*(pPage)->pPageMethods->setCellBody)(pPage, CCELLS)
#define TDB_PAGE_FCELL_SET(pPage, FCELL)                (*(pPage)->pPageMethods->setCellFree)(pPage, FCELL)
#define TDB_PAGE_NFREE_SET(pPage, NFREE)                (*(pPage)->pPageMethods->setFreeBytes)(pPage, NFREE)
#define TDB_PAGE_CELL_OFFSET_AT_SET(pPage, idx, OFFSET) (*(pPage)->pPageMethods->setCellOffset)(pPage, idx, OFFSET)

#define TDB_PAGE_OFFSET_SIZE(pPage) ((pPage)->pPageMethods->szOffset)
H
Hongze Cheng 已提交
96

H
Hongze Cheng 已提交
97
#define TDB_PAGE_CELL_AT(pPage, idx) ((pPage)->pData + TDB_PAGE_CELL_OFFSET_AT(pPage, idx))
H
Hongze Cheng 已提交
98

H
Hongze Cheng 已提交
99
// For page lock
H
Hongze Cheng 已提交
100 101 102 103
#define P_LOCK_SUCC 0
#define P_LOCK_BUSY 1
#define P_LOCK_FAIL -1

wafwerar's avatar
wafwerar 已提交
104 105 106 107
#define TDB_INIT_PAGE_LOCK(pPage)    taosThreadSpinInit(&((pPage)->lock), 0)
#define TDB_DESTROY_PAGE_LOCK(pPage) taosThreadSpinDestroy(&((pPage)->lock))
#define TDB_LOCK_PAGE(pPage)         taosThreadSpinLock(&((pPage)->lock))
#define TDB_UNLOCK_PAGE(pPage)       taosThreadSpinUnlock(&((pPage)->lock))
H
Hongze Cheng 已提交
108 109 110 111 112 113 114 115 116 117 118 119
#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 已提交
120

H
Hongze Cheng 已提交
121
// APIs
H
Hongze Cheng 已提交
122
int tdbPageCreate(int pageSize, SPage **ppPage, void *(*xMalloc)(void *, size_t), void *arg);
H
Hongze Cheng 已提交
123
int tdbPageDestroy(SPage *pPage, void (*xFree)(void *arg, void *ptr), void *arg);
H
Hongze Cheng 已提交
124
int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell);
H
Hongze Cheng 已提交
125 126
int tdbPageDropCell(SPage *pPage, int idx);

H
Hongze Cheng 已提交
127 128 129 130 131
#ifdef __cplusplus
}
#endif

#endif /*_TDB_PAGE_H_*/