sdb.h 4.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
#define SDB_GET_BINARY_VAL(pData, dataLen, val, valLen, code) \
S
Shengliang Guan 已提交
24
  if (code == 0) {                                            \
S
Shengliang Guan 已提交
25 26 27 28 29
    if ((dataLen) >= (valLen)) {                              \
      memcpy((val), (char *)(pData), (valLen));               \
      (dataLen) -= (valLen);                                  \
      (pData) = (char *)(pData) + (valLen);                   \
    } else {                                                  \
S
Shengliang Guan 已提交
30
      code = TSDB_CODE_SDB_INVALID_DATA_LEN;                  \
S
Shengliang Guan 已提交
31 32 33 34
    }                                                         \
  }

#define SDB_GET_INT32_VAL(pData, dataLen, val, code) \
S
Shengliang Guan 已提交
35
  if (code == 0) {                                   \
S
Shengliang Guan 已提交
36 37 38 39 40
    if (dataLen >= sizeof(int32_t)) {                \
      *(int32_t *)(pData) = (int32_t)(val);          \
      (dataLen) -= sizeof(int32_t);                  \
      (pData) = (char *)(pData) + sizeof(int32_t);   \
    } else {                                         \
S
Shengliang Guan 已提交
41
      code = TSDB_CODE_SDB_INVALID_DATA_LEN;         \
S
Shengliang Guan 已提交
42 43 44 45
    }                                                \
  }

#define SDB_GET_INT64_VAL(pData, dataLen, val, code) \
S
Shengliang Guan 已提交
46
  if (code == 0) {                                   \
S
Shengliang Guan 已提交
47 48 49 50 51
    if (dataLen >= sizeof(int64_t)) {                \
      *(int64_t *)(pData) = (int64_t)(val);          \
      (dataLen) -= sizeof(int64_t);                  \
      (pData) = (char *)(pData) + sizeof(int64_t);   \
    } else {                                         \
S
Shengliang Guan 已提交
52
      code = TSDB_CODE_SDB_INVALID_DATA_LEN;         \
S
Shengliang Guan 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
    }                                                \
  }

#define SDB_SET_INT64_VAL(pData, dataLen, val) \
  {                                            \
    *(int64_t *)(pData) = (int64_t)(val);      \
    (dataLen) += sizeof(int64_t);              \
    (pData) += sizeof(int64_t);                \
  }

#define SDB_SET_INT32_VAL(pData, dataLen, val) \
  {                                            \
    *(int32_t *)(pData) = (int32_t)(val);      \
    (dataLen) += sizeof(int32_t);              \
    (pData) += sizeof(int32_t);                \
  }

#define SDB_SET_BINARY_VAL(pData, dataLen, val, valLen) \
  {                                                     \
    memcpy((char *)(pData), (val), (valLen));           \
    (dataLen) += (valLen);                              \
    (pData) += (valLen);                                \
  }

S
Shengliang Guan 已提交
77
typedef enum {
S
Shengliang Guan 已提交
78
  SDB_START = 0,
S
Shengliang Guan 已提交
79
  SDB_TRANS = 1,
S
Shengliang Guan 已提交
80 81 82 83 84 85 86 87 88 89 90
  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 已提交
91 92 93 94
} 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 已提交
95
typedef enum { SDB_STATUS_CREATING = 1, SDB_STATUS_READY = 2, SDB_STATUS_DROPPING = 3 } ESdbStatus;
S
Shengliang Guan 已提交
96 97

typedef struct {
S
Shengliang Guan 已提交
98 99 100 101 102 103 104 105
  int8_t  type;
  int8_t  sver;
  int8_t  status;
  int8_t  action;
  int8_t  reserved[4];
  int32_t cksum;
  int32_t dataLen;
  char    data[];
S
Shengliang Guan 已提交
106
} SSdbRaw;
S
Shengliang Guan 已提交
107 108 109 110 111

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 已提交
112 113
typedef void *(*SdbDecodeFp)(SSdbRaw *pRaw);
typedef SSdbRaw *(*SdbEncodeFp)(void *pObj);
S
Shengliang Guan 已提交
114

S
Shengliang Guan 已提交
115 116 117 118 119 120 121 122 123
typedef struct {
  ESdbType    sdbType;
  EKeyType    keyType;
  SdbDeployFp deployFp;
  SdbEncodeFp encodeFp;
  SdbDecodeFp decodeFp;
  SdbInsertFp insertFp;
  SdbUpdateFp updateFp;
  SdbDeleteFp deleteFp;
S
Shengliang Guan 已提交
124
} SSdbTable;
S
Shengliang Guan 已提交
125

S
Shengliang Guan 已提交
126 127
int32_t sdbInit();
void    sdbCleanup();
S
Shengliang Guan 已提交
128
void    sdbSetTable(SSdbTable table);
S
Shengliang Guan 已提交
129

S
Shengliang Guan 已提交
130
int32_t sdbRead();
S
Shengliang Guan 已提交
131
int32_t sdbWrite(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
132 133 134 135 136
int32_t sdbCommit();

int32_t sdbDeploy();
void    sdbUnDeploy();

S
Shengliang Guan 已提交
137
void   *sdbAcquire(ESdbType sdb, void *pKey);
S
Shengliang Guan 已提交
138
void    sdbRelease(void *pObj);
S
Shengliang Guan 已提交
139 140 141
void   *sdbFetch(ESdbType sdb, void *pIter);
void    sdbCancelFetch(ESdbType sdb, void *pIter);
int32_t sdbGetSize(ESdbType sdb);
S
Shengliang Guan 已提交
142

S
Shengliang Guan 已提交
143 144 145
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
146

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