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 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 98 99 100
#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) { \
      sdbFreeRaw(pRaw);                            \
      return NULL;                              \
    };                                          \
S
Shengliang Guan 已提交
101 102
  }

S
Shengliang Guan 已提交
103
typedef enum {
S
Shengliang Guan 已提交
104
  SDB_START = 0,
S
Shengliang Guan 已提交
105
  SDB_TRANS = 1,
S
Shengliang Guan 已提交
106 107 108 109 110 111 112 113 114 115 116
  SDB_CLUSTER = 2,
  SDB_DNODE = 3,
  SDB_MNODE = 4,
  SDB_ACCT = 5,
  SDB_AUTH = 6,
  SDB_USER = 7,
  SDB_DB = 8,
  SDB_VGROUP = 9,
  SDB_STABLE = 10,
  SDB_FUNC = 11,
  SDB_MAX = 12
S
Shengliang Guan 已提交
117 118 119 120
} ESdbType;

typedef enum { SDB_ACTION_INSERT = 1, SDB_ACTION_UPDATE = 2, SDB_ACTION_DELETE = 3 } ESdbAction;
typedef enum { SDB_KEY_BINARY = 1, SDB_KEY_INT32 = 2, SDB_KEY_INT64 = 3 } EKeyType;
S
Shengliang Guan 已提交
121
typedef enum { SDB_STATUS_CREATING = 1, SDB_STATUS_READY = 2, SDB_STATUS_DROPPING = 3 } ESdbStatus;
S
Shengliang Guan 已提交
122 123
typedef struct SSdbRaw SSdbRaw;
typedef struct SSdbRow SSdbRow;
S
Shengliang Guan 已提交
124 125 126 127 128

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 已提交
129
typedef SSdbRow *(*SdbDecodeFp)(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
130
typedef SSdbRaw *(*SdbEncodeFp)(void *pObj);
S
Shengliang Guan 已提交
131

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

S
Shengliang Guan 已提交
143 144
int32_t sdbInit();
void    sdbCleanup();
S
Shengliang Guan 已提交
145
void    sdbSetTable(SSdbTable table);
S
Shengliang Guan 已提交
146

S
Shengliang Guan 已提交
147
int32_t sdbRead();
S
Shengliang Guan 已提交
148
int32_t sdbWrite(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
149 150 151 152 153
int32_t sdbCommit();

int32_t sdbDeploy();
void    sdbUnDeploy();

S
Shengliang Guan 已提交
154
void   *sdbAcquire(ESdbType sdb, void *pKey);
S
Shengliang Guan 已提交
155
void    sdbRelease(void *pObj);
S
Shengliang Guan 已提交
156 157 158
void   *sdbFetch(ESdbType sdb, void *pIter);
void    sdbCancelFetch(ESdbType sdb, void *pIter);
int32_t sdbGetSize(ESdbType sdb);
S
Shengliang Guan 已提交
159

S
Shengliang Guan 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
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  sdbSetRawAction(SSdbRaw *pRaw, ESdbAction action);
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 已提交
180 181 182
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
183

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