sdb.h 10.4 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

L
Liu Jicong 已提交
19 20
#include "os.h"

21 22 23 24 25
#include "thash.h"
#include "tlockfree.h"
#include "tlog.h"
#include "tmsg.h"

S
Shengliang Guan 已提交
26 27 28
#ifdef __cplusplus
extern "C" {
#endif
S
Shengliang Guan 已提交
29

30 31 32 33 34 35
#define SDB_GET_VAL(pData, dataPos, val, pos, func, type) \
  {                                                       \
    if (func(pRaw, dataPos, val) != 0) {                  \
      goto pos;                                           \
    }                                                     \
    dataPos += sizeof(type);                              \
S
Shengliang Guan 已提交
36 37
  }

38
#define SDB_GET_BINARY(pRaw, dataPos, val, valLen, pos)     \
S
Shengliang Guan 已提交
39 40
  {                                                         \
    if (sdbGetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
41
      goto pos;                                             \
S
Shengliang Guan 已提交
42 43
    }                                                       \
    dataPos += valLen;                                      \
S
Shengliang Guan 已提交
44 45
  }

46
#define SDB_GET_INT64(pData, dataPos, val, pos) SDB_GET_VAL(pData, dataPos, val, pos, sdbGetRawInt64, int64_t)
S
Shengliang Guan 已提交
47

48
#define SDB_GET_INT32(pData, dataPos, val, pos) SDB_GET_VAL(pData, dataPos, val, pos, sdbGetRawInt32, int32_t)
S
Shengliang Guan 已提交
49

50 51 52
#define SDB_GET_INT16(pData, dataPos, val, pos) SDB_GET_VAL(pData, dataPos, val, pos, sdbGetRawInt16, int16_t)

#define SDB_GET_INT8(pData, dataPos, val, pos) SDB_GET_VAL(pData, dataPos, val, pos, sdbGetRawInt8, int8_t)
S
Shengliang Guan 已提交
53

54 55 56 57
#define SDB_GET_RESERVE(pRaw, dataPos, valLen, pos) \
  {                                                 \
    char val[valLen] = {0};                         \
    SDB_GET_BINARY(pRaw, dataPos, val, valLen, pos) \
S
Shengliang Guan 已提交
58 59
  }

60 61 62 63 64 65
#define SDB_SET_VAL(pRaw, dataPos, val, pos, func, type) \
  {                                                      \
    if (func(pRaw, dataPos, val) != 0) {                 \
      goto pos;                                          \
    }                                                    \
    dataPos += sizeof(type);                             \
S
Shengliang Guan 已提交
66 67
  }

68 69 70 71 72 73 74 75 76
#define SDB_SET_INT64(pRaw, dataPos, val, pos) SDB_SET_VAL(pRaw, dataPos, val, pos, sdbSetRawInt64, int64_t)

#define SDB_SET_INT32(pRaw, dataPos, val, pos) SDB_SET_VAL(pRaw, dataPos, val, pos, sdbSetRawInt32, int32_t)

#define SDB_SET_INT16(pRaw, dataPos, val, pos) SDB_SET_VAL(pRaw, dataPos, val, pos, sdbSetRawInt16, int16_t)

#define SDB_SET_INT8(pRaw, dataPos, val, pos) SDB_SET_VAL(pRaw, dataPos, val, pos, sdbSetRawInt8, int8_t)

#define SDB_SET_BINARY(pRaw, dataPos, val, valLen, pos)     \
S
Shengliang Guan 已提交
77 78
  {                                                         \
    if (sdbSetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
79
      goto pos;                                             \
S
Shengliang Guan 已提交
80 81 82 83
    }                                                       \
    dataPos += valLen;                                      \
  }

84 85 86 87
#define SDB_SET_RESERVE(pRaw, dataPos, valLen, pos) \
  {                                                 \
    char val[valLen] = {0};                         \
    SDB_SET_BINARY(pRaw, dataPos, val, valLen, pos) \
S
Shengliang Guan 已提交
88 89
  }

90
#define SDB_SET_DATALEN(pRaw, dataLen, pos)     \
S
Shengliang Guan 已提交
91 92
  {                                             \
    if (sdbSetRawDataLen(pRaw, dataLen) != 0) { \
93
      goto pos;                                 \
S
Shengliang Guan 已提交
94
    }                                           \
S
Shengliang Guan 已提交
95 96
  }

S
Shengliang Guan 已提交
97
typedef struct SMnode  SMnode;
S
Shengliang Guan 已提交
98 99
typedef struct SSdbRaw SSdbRaw;
typedef struct SSdbRow SSdbRow;
100 101 102 103 104 105 106

typedef enum {
  SDB_KEY_BINARY = 1,
  SDB_KEY_INT32 = 2,
  SDB_KEY_INT64 = 3,
} EKeyType;

S
Shengliang Guan 已提交
107
typedef enum {
108
  SDB_STATUS_INIT = 0,
S
Shengliang Guan 已提交
109
  SDB_STATUS_CREATING = 1,
S
Shengliang Guan 已提交
110
  SDB_STATUS_UPDATING = 2,
S
Shengliang Guan 已提交
111
  SDB_STATUS_DROPPING = 3,
S
Shengliang Guan 已提交
112 113
  SDB_STATUS_READY = 4,
  SDB_STATUS_DROPPED = 5
S
Shengliang Guan 已提交
114 115
} ESdbStatus;

S
Shengliang Guan 已提交
116
typedef enum {
S
Shengliang Guan 已提交
117 118 119 120 121 122 123 124 125 126
  SDB_TRANS = 0,
  SDB_CLUSTER = 1,
  SDB_MNODE = 2,
  SDB_QNODE = 3,
  SDB_SNODE = 4,
  SDB_BNODE = 5,
  SDB_DNODE = 6,
  SDB_USER = 7,
  SDB_AUTH = 8,
  SDB_ACCT = 9,
L
Liu Jicong 已提交
127 128 129 130 131 132
  SDB_STREAM = 10,
  SDB_OFFSET = 11,
  SDB_SUBSCRIBE = 12,
  SDB_CONSUMER = 13,
  SDB_TOPIC = 14,
  SDB_VGROUP = 15,
S
sma  
Shengliang Guan 已提交
133 134 135 136 137
  SDB_SMA = 16,
  SDB_STB = 17,
  SDB_DB = 18,
  SDB_FUNC = 19,
  SDB_MAX = 20
S
Shengliang Guan 已提交
138 139
} ESdbType;

S
Shengliang Guan 已提交
140 141 142
typedef struct SSdb SSdb;
typedef int32_t (*SdbInsertFp)(SSdb *pSdb, void *pObj);
typedef int32_t (*SdbUpdateFp)(SSdb *pSdb, void *pSrcObj, void *pDstObj);
143
typedef int32_t (*SdbDeleteFp)(SSdb *pSdb, void *pObj, bool callFunc);
S
Shengliang Guan 已提交
144
typedef int32_t (*SdbDeployFp)(SMnode *pMnode);
S
Shengliang Guan 已提交
145
typedef SSdbRow *(*SdbDecodeFp)(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
146
typedef SSdbRaw *(*SdbEncodeFp)(void *pObj);
S
Shengliang Guan 已提交
147
typedef bool (*sdbTraverseFp)(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3);
S
Shengliang Guan 已提交
148

S
Shengliang Guan 已提交
149
typedef struct {
S
Shengliang Guan 已提交
150 151
  ESdbType    sdbType;
  EKeyType    keyType;
S
Shengliang Guan 已提交
152 153 154 155 156 157
  SdbDeployFp deployFp;
  SdbEncodeFp encodeFp;
  SdbDecodeFp decodeFp;
  SdbInsertFp insertFp;
  SdbUpdateFp updateFp;
  SdbDeleteFp deleteFp;
S
Shengliang Guan 已提交
158
} SSdbTable;
S
Shengliang Guan 已提交
159

S
Shengliang Guan 已提交
160 161
typedef struct SSdbOpt {
  const char *path;
S
Shengliang Guan 已提交
162
  SMnode     *pMnode;
S
Shengliang Guan 已提交
163
} SSdbOpt;
S
Shengliang Guan 已提交
164

S
Shengliang Guan 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
/**
 * @brief Initialize and start the sdb.
 *
 * @param pOption Option of the sdb.
 * @return SSdb* The sdb object.
 */
SSdb *sdbInit(SSdbOpt *pOption);

/**
 * @brief Stop and cleanup the sdb.
 *
 * @param pSdb The sdb object to close.
 */
void sdbCleanup(SSdb *pSdb);

/**
 * @brief Set the properties of sdb table.
 *
 * @param pSdb The sdb object.
 * @param table The properties of the table.
 * @return int32_t 0 for success, -1 for failure.
 */
S
Shengliang Guan 已提交
187
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table);
S
Shengliang Guan 已提交
188 189 190 191 192 193 194

/**
 * @brief Set the initial rows of sdb.
 *
 * @param pSdb The sdb object.
 * @return int32_t 0 for success, -1 for failure.
 */
S
Shengliang Guan 已提交
195
int32_t sdbDeploy(SSdb *pSdb);
S
Shengliang Guan 已提交
196 197 198 199 200 201 202

/**
 * @brief Load sdb from file.
 *
 * @param pSdb The sdb object.
 * @return int32_t 0 for success, -1 for failure.
 */
S
Shengliang Guan 已提交
203
int32_t sdbReadFile(SSdb *pSdb);
S
Shengliang Guan 已提交
204

205 206 207 208 209 210 211 212
/**
 * @brief Write sdb file.
 *
 * @param pSdb The sdb object.
 * @return int32_t 0 for success, -1 for failure.
 */
int32_t sdbWriteFile(SSdb *pSdb);

S
Shengliang Guan 已提交
213
/**
S
Shengliang Guan 已提交
214
 * @brief Parse and write raw data to sdb, then free the pRaw object
S
Shengliang Guan 已提交
215 216 217 218 219
 *
 * @param pSdb The sdb object.
 * @param pRaw The raw data.
 * @return int32_t 0 for success, -1 for failure.
 */
S
Shengliang Guan 已提交
220
int32_t sdbWrite(SSdb *pSdb, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
221

S
Shengliang Guan 已提交
222 223 224 225 226 227 228
/**
 * @brief Parse and write raw data to sdb.
 *
 * @param pSdb The sdb object.
 * @param pRaw The raw data.
 * @return int32_t 0 for success, -1 for failure.
 */
229
int32_t sdbWriteWithoutFree(SSdb *pSdb, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
230

S
Shengliang Guan 已提交
231 232 233 234 235 236 237 238
/**
 * @brief Acquire a row from sdb
 *
 * @param pSdb The sdb object.
 * @param type The type of the row.
 * @param pKey The key value of the row.
 * @return void* The object of the row.
 */
L
Liu Jicong 已提交
239
void *sdbAcquire(SSdb *pSdb, ESdbType type, const void *pKey);
S
Shengliang Guan 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253

/**
 * @brief Release a row from sdb.
 *
 * @param pSdb The sdb object.
 * @param pObj The object of the row.
 */
void sdbRelease(SSdb *pSdb, void *pObj);

/**
 * @brief Traverse a sdb table
 *
 * @param pSdb The sdb object.
 * @param type The type of the table.
S
Shengliang Guan 已提交
254
 * @param pIter The initial iterator of the table.
S
Shengliang Guan 已提交
255 256 257 258 259 260 261 262 263 264 265 266 267
 * @param pObj The object of the row just fetched.
 * @return void* The next iterator of the table.
 */
void *sdbFetch(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj);

/**
 * @brief Cancel a traversal
 *
 * @param pSdb The sdb object.
 * @param type The initial iterator of table.
 */
void sdbCancelFetch(SSdb *pSdb, void *pIter);

S
Shengliang Guan 已提交
268 269 270 271 272 273 274 275 276 277 278 279
/**
 * @brief Traverse a sdb
 *
 * @param pSdb The sdb object.
 * @param type The initial iterator of table.
 * @param fp The function pointer.
 * @param p1 The callback param.
 * @param p2 The callback param.
 * @param p3 The callback param.
 */
void sdbTraverse(SSdb *pSdb, ESdbType type, sdbTraverseFp fp, void *p1, void *p2, void *p3);

S
Shengliang Guan 已提交
280 281 282 283 284
/**
 * @brief Get the number of rows in the table
 *
 * @param pSdb The sdb object.
 * @param pIter The type of the table.
S
Shengliang Guan 已提交
285
 * @return int32_t The number of rows in the table
S
Shengliang Guan 已提交
286
 */
S
Shengliang Guan 已提交
287
int32_t sdbGetSize(SSdb *pSdb, ESdbType type);
S
Shengliang Guan 已提交
288

S
Shengliang Guan 已提交
289 290 291 292 293
/**
 * @brief Get the max id of the table, keyType of table should be INT32
 *
 * @param pSdb The sdb object.
 * @param pIter The type of the table.
S
Shengliang Guan 已提交
294
 * @return int32_t The max id of the table
S
Shengliang Guan 已提交
295 296 297
 */
int32_t sdbGetMaxId(SSdb *pSdb, ESdbType type);

298 299 300 301 302 303 304 305 306
/**
 * @brief Get the version of the table
 *
 * @param pSdb The sdb object.
 * @param pIter The type of the table.
 * @return int32_t The version of the table
 */
int64_t sdbGetTableVer(SSdb *pSdb, ESdbType type);

S
Shengliang Guan 已提交
307 308 309 310 311 312 313 314 315
/**
 * @brief Update the version of sdb
 *
 * @param pSdb The sdb object.
 * @param val The update value of the version.
 * @return int32_t The current version of sdb
 */
int64_t sdbUpdateVer(SSdb *pSdb, int32_t val);

S
Shengliang Guan 已提交
316
SSdbRaw *sdbAllocRaw(ESdbType type, int8_t sver, int32_t dataLen);
S
Shengliang Guan 已提交
317 318
void     sdbFreeRaw(SSdbRaw *pRaw);
int32_t  sdbSetRawInt8(SSdbRaw *pRaw, int32_t dataPos, int8_t val);
S
Shengliang Guan 已提交
319
int32_t  sdbSetRawInt16(SSdbRaw *pRaw, int32_t dataPos, int16_t val);
S
Shengliang Guan 已提交
320 321 322 323 324 325
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);
S
Shengliang Guan 已提交
326
int32_t  sdbGetRawInt16(SSdbRaw *pRaw, int32_t dataPos, int16_t *val);
S
Shengliang Guan 已提交
327 328 329 330 331 332 333 334 335
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    *sdbGetRowObj(SSdbRow *pRow);

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
typedef struct SSdb {
  SMnode     *pMnode;
  char       *currDir;
  char       *syncDir;
  char       *tmpDir;
  int64_t     lastCommitVer;
  int64_t     curVer;
  int64_t     tableVer[SDB_MAX];
  int64_t     maxId[SDB_MAX];
  EKeyType    keyTypes[SDB_MAX];
  SHashObj   *hashObjs[SDB_MAX];
  SRWLatch    locks[SDB_MAX];
  SdbInsertFp insertFps[SDB_MAX];
  SdbUpdateFp updateFps[SDB_MAX];
  SdbDeleteFp deleteFps[SDB_MAX];
  SdbDeployFp deployFps[SDB_MAX];
  SdbEncodeFp encodeFps[SDB_MAX];
  SdbDecodeFp decodeFps[SDB_MAX];
} SSdb;

S
Shengliang Guan 已提交
356 357 358
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
359

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