sdb.h 6.5 KB
Newer Older
H
refact  
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/>.
 */

S
Shengliang Guan 已提交
16 17
#ifndef _TD_SDB_H_
#define _TD_SDB_H_
S
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20 21
#ifdef __cplusplus
extern "C" {
#endif
S
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23 24 25 26 27 28 29
#define SDB_GET_INT64(pData, pRow, dataPos, val)   \
  {                                                \
    if (sdbGetRawInt64(pRaw, dataPos, val) != 0) { \
      sdbFreeRow(pRow);                            \
      return NULL;                                 \
    }                                              \
    dataPos += sizeof(int64_t);                    \
S
Shengliang Guan 已提交
30 31
  }

S
Shengliang Guan 已提交
32 33 34 35 36 37 38
#define SDB_GET_INT32(pData, pRow, dataPos, val)   \
  {                                                \
    if (sdbGetRawInt32(pRaw, dataPos, val) != 0) { \
      sdbFreeRow(pRow);                            \
      return NULL;                                 \
    }                                              \
    dataPos += sizeof(int32_t);                    \
S
Shengliang Guan 已提交
39 40
  }

S
Shengliang Guan 已提交
41 42 43 44 45 46 47
#define SDB_GET_INT8(pData, pRow, dataPos, val)   \
  {                                               \
    if (sdbGetRawInt8(pRaw, dataPos, val) != 0) { \
      sdbFreeRow(pRow);                           \
      return NULL;                                \
    }                                             \
    dataPos += sizeof(int8_t);                    \
S
Shengliang Guan 已提交
48 49
  }

S
Shengliang Guan 已提交
50 51 52 53 54 55 56
#define SDB_GET_BINARY(pRaw, pRow, dataPos, val, valLen)    \
  {                                                         \
    if (sdbGetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
      sdbFreeRow(pRow);                                     \
      return NULL;                                          \
    }                                                       \
    dataPos += valLen;                                      \
S
Shengliang Guan 已提交
57 58
  }

S
Shengliang Guan 已提交
59 60 61 62 63 64 65
#define SDB_SET_INT64(pData, dataPos, val)         \
  {                                                \
    if (sdbSetRawInt64(pRaw, dataPos, val) != 0) { \
      sdbFreeRaw(pRaw);                               \
      return NULL;                                 \
    };                                             \
    dataPos += sizeof(int64_t);                    \
S
Shengliang Guan 已提交
66 67
  }

S
Shengliang Guan 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
#define SDB_SET_INT32(pData, dataPos, val)         \
  {                                                \
    if (sdbSetRawInt32(pRaw, dataPos, val) != 0) { \
      sdbFreeRaw(pRaw);                               \
      return NULL;                                 \
    };                                             \
    dataPos += sizeof(int32_t);                    \
  }

#define SDB_SET_INT8(pData, dataPos, val)         \
  {                                               \
    if (sdbSetRawInt8(pRaw, dataPos, val) != 0) { \
      sdbFreeRaw(pRaw);                              \
      return NULL;                                \
    };                                            \
    dataPos += sizeof(int8_t);                    \
  }

#define SDB_SET_BINARY(pRaw, dataPos, val, valLen)          \
  {                                                         \
    if (sdbSetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
      sdbFreeRaw(pRaw);                                        \
      return NULL;                                          \
    };                                                      \
    dataPos += valLen;                                      \
  }

#define SDB_SET_DATALEN(pRaw, dataLen)          \
  {                                             \
    if (sdbSetRawDataLen(pRaw, dataLen) != 0) { \
S
Shengliang Guan 已提交
98
      sdbFreeRaw(pRaw);                         \
S
Shengliang Guan 已提交
99 100
      return NULL;                              \
    };                                          \
S
Shengliang Guan 已提交
101 102
  }

S
Shengliang Guan 已提交
103 104 105 106 107 108 109 110 111 112
typedef struct SSdbRaw SSdbRaw;
typedef struct SSdbRow SSdbRow;
typedef enum { SDB_KEY_BINARY = 1, SDB_KEY_INT32 = 2, SDB_KEY_INT64 = 3 } EKeyType;
typedef enum {
  SDB_STATUS_CREATING = 1,
  SDB_STATUS_READY = 2,
  SDB_STATUS_DROPPING = 3,
  SDB_STATUS_DROPPED = 4
} ESdbStatus;

S
Shengliang Guan 已提交
113
typedef enum {
S
Shengliang Guan 已提交
114
  SDB_START = 0,
S
Shengliang Guan 已提交
115
  SDB_TRANS = 1,
S
Shengliang Guan 已提交
116 117 118
  SDB_CLUSTER = 2,
  SDB_DNODE = 3,
  SDB_MNODE = 4,
119
  SDB_USER = 5,
S
Shengliang Guan 已提交
120
  SDB_AUTH = 6,
121
  SDB_ACCT = 7,
S
Shengliang Guan 已提交
122 123 124 125 126
  SDB_DB = 8,
  SDB_VGROUP = 9,
  SDB_STABLE = 10,
  SDB_FUNC = 11,
  SDB_MAX = 12
S
Shengliang Guan 已提交
127 128 129 130 131 132
} ESdbType;

typedef int32_t (*SdbInsertFp)(void *pObj);
typedef int32_t (*SdbUpdateFp)(void *pSrcObj, void *pDstObj);
typedef int32_t (*SdbDeleteFp)(void *pObj);
typedef int32_t (*SdbDeployFp)();
S
Shengliang Guan 已提交
133
typedef SSdbRow *(*SdbDecodeFp)(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
134
typedef SSdbRaw *(*SdbEncodeFp)(void *pObj);
S
Shengliang Guan 已提交
135

S
Shengliang Guan 已提交
136 137 138 139 140 141 142 143 144
typedef struct {
  ESdbType    sdbType;
  EKeyType    keyType;
  SdbDeployFp deployFp;
  SdbEncodeFp encodeFp;
  SdbDecodeFp decodeFp;
  SdbInsertFp insertFp;
  SdbUpdateFp updateFp;
  SdbDeleteFp deleteFp;
S
Shengliang Guan 已提交
145
} SSdbTable;
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147 148
int32_t sdbInit();
void    sdbCleanup();
S
Shengliang Guan 已提交
149
void    sdbSetTable(SSdbTable table);
S
Shengliang Guan 已提交
150

151 152
int32_t sdbOpen();
void    sdbClose();
S
Shengliang Guan 已提交
153
int32_t sdbWrite(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
154 155 156 157

int32_t sdbDeploy();
void    sdbUnDeploy();

S
Shengliang Guan 已提交
158
void   *sdbAcquire(ESdbType sdb, void *pKey);
S
Shengliang Guan 已提交
159
void    sdbRelease(void *pObj);
S
Shengliang Guan 已提交
160 161
void   *sdbFetch(ESdbType sdb, void *pIter, void **ppObj);
void    sdbCancelFetch(void *pIter);
S
Shengliang Guan 已提交
162
int32_t sdbGetSize(ESdbType sdb);
S
Shengliang Guan 已提交
163

S
Shengliang Guan 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
SSdbRaw *sdbAllocRaw(ESdbType sdb, int8_t sver, int32_t dataLen);
void     sdbFreeRaw(SSdbRaw *pRaw);
int32_t  sdbSetRawInt8(SSdbRaw *pRaw, int32_t dataPos, int8_t val);
int32_t  sdbSetRawInt32(SSdbRaw *pRaw, int32_t dataPos, int32_t val);
int32_t  sdbSetRawInt64(SSdbRaw *pRaw, int32_t dataPos, int64_t val);
int32_t  sdbSetRawBinary(SSdbRaw *pRaw, int32_t dataPos, const char *pVal, int32_t valLen);
int32_t  sdbSetRawDataLen(SSdbRaw *pRaw, int32_t dataLen);
int32_t  sdbSetRawStatus(SSdbRaw *pRaw, ESdbStatus status);
int32_t  sdbGetRawInt8(SSdbRaw *pRaw, int32_t dataPos, int8_t *val);
int32_t  sdbGetRawInt32(SSdbRaw *pRaw, int32_t dataPos, int32_t *val);
int32_t  sdbGetRawInt64(SSdbRaw *pRaw, int32_t dataPos, int64_t *val);
int32_t  sdbGetRawBinary(SSdbRaw *pRaw, int32_t dataPos, char *pVal, int32_t valLen);
int32_t  sdbGetRawSoftVer(SSdbRaw *pRaw, int8_t *sver);
int32_t  sdbGetRawTotalSize(SSdbRaw *pRaw);

SSdbRow *sdbAllocRow(int32_t objSize);
void     sdbFreeRow(SSdbRow *pRow);
void    *sdbGetRowObj(SSdbRow *pRow);

S
Shengliang Guan 已提交
183 184 185
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
186

S
Shengliang Guan 已提交
187
#endif /*_TD_SDB_H_*/