sdb.h 6.6 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
#define SDB_SET_INT64(pRaw, dataPos, val)          \
S
Shengliang Guan 已提交
60 61
  {                                                \
    if (sdbSetRawInt64(pRaw, dataPos, val) != 0) { \
S
Shengliang Guan 已提交
62
      sdbFreeRaw(pRaw);                            \
S
Shengliang Guan 已提交
63
      return NULL;                                 \
S
Shengliang Guan 已提交
64
    }                                              \
S
Shengliang Guan 已提交
65
    dataPos += sizeof(int64_t);                    \
S
Shengliang Guan 已提交
66 67
  }

S
Shengliang Guan 已提交
68
#define SDB_SET_INT32(pRaw, dataPos, val)          \
S
Shengliang Guan 已提交
69 70
  {                                                \
    if (sdbSetRawInt32(pRaw, dataPos, val) != 0) { \
S
Shengliang Guan 已提交
71
      sdbFreeRaw(pRaw);                            \
S
Shengliang Guan 已提交
72
      return NULL;                                 \
S
Shengliang Guan 已提交
73
    }                                              \
S
Shengliang Guan 已提交
74 75 76
    dataPos += sizeof(int32_t);                    \
  }

S
Shengliang Guan 已提交
77
#define SDB_SET_INT8(pRaw, dataPos, val)          \
S
Shengliang Guan 已提交
78 79
  {                                               \
    if (sdbSetRawInt8(pRaw, dataPos, val) != 0) { \
S
Shengliang Guan 已提交
80
      sdbFreeRaw(pRaw);                           \
S
Shengliang Guan 已提交
81
      return NULL;                                \
S
Shengliang Guan 已提交
82
    }                                             \
S
Shengliang Guan 已提交
83 84 85 86 87 88
    dataPos += sizeof(int8_t);                    \
  }

#define SDB_SET_BINARY(pRaw, dataPos, val, valLen)          \
  {                                                         \
    if (sdbSetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
S
Shengliang Guan 已提交
89
      sdbFreeRaw(pRaw);                                     \
S
Shengliang Guan 已提交
90
      return NULL;                                          \
S
Shengliang Guan 已提交
91
    }                                                       \
S
Shengliang Guan 已提交
92 93 94 95 96 97
    dataPos += valLen;                                      \
  }

#define SDB_SET_DATALEN(pRaw, dataLen)          \
  {                                             \
    if (sdbSetRawDataLen(pRaw, dataLen) != 0) { \
S
Shengliang Guan 已提交
98
      sdbFreeRaw(pRaw);                         \
S
Shengliang Guan 已提交
99
      return NULL;                              \
S
Shengliang Guan 已提交
100
    }                                           \
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
} ESdbType;

129 130 131 132
typedef struct SSdbOpt {
  const char *path;
} SSdbOpt;

S
Shengliang Guan 已提交
133 134 135 136
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 已提交
137
typedef SSdbRow *(*SdbDecodeFp)(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
138
typedef SSdbRaw *(*SdbEncodeFp)(void *pObj);
S
Shengliang Guan 已提交
139

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

S
Shengliang Guan 已提交
151 152
typedef struct SSdb SSdb;

153 154 155
SSdb *sdbOpen(SSdbOpt *pOption);
void  sdbClose(SSdb *pSdb);
void  sdbSetTable(SSdb *pSdb, SSdbTable table);
S
Shengliang Guan 已提交
156

157 158
// int32_t sdbOpen();
// void    sdbClose();
S
Shengliang Guan 已提交
159
int32_t sdbWrite(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
160 161 162 163

int32_t sdbDeploy();
void    sdbUnDeploy();

S
Shengliang Guan 已提交
164
void   *sdbAcquire(ESdbType sdb, void *pKey);
S
Shengliang Guan 已提交
165
void    sdbRelease(void *pObj);
S
Shengliang Guan 已提交
166 167
void   *sdbFetch(ESdbType sdb, void *pIter, void **ppObj);
void    sdbCancelFetch(void *pIter);
S
Shengliang Guan 已提交
168
int32_t sdbGetSize(ESdbType sdb);
S
Shengliang Guan 已提交
169

S
Shengliang Guan 已提交
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
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 已提交
189 190 191
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
192

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