sdb.h 13.0 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
#include "thash.h"
#include "tlockfree.h"
#include "tlog.h"
#include "tmsg.h"
25
#include "wal.h"
26

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

S
Shengliang Guan 已提交
31 32 33 34 35 36 37 38 39
// clang-format off
#define mFatal(...) { if (mDebugFlag & DEBUG_FATAL) { taosPrintLog("MND FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }}
#define mError(...) { if (mDebugFlag & DEBUG_ERROR) { taosPrintLog("MND ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }}
#define mWarn(...)  { if (mDebugFlag & DEBUG_WARN)  { taosPrintLog("MND WARN ", DEBUG_WARN, 255, __VA_ARGS__); }}
#define mInfo(...)  { if (mDebugFlag & DEBUG_INFO)  { taosPrintLog("MND ", DEBUG_INFO, 255, __VA_ARGS__); }}
#define mDebug(...) { if (mDebugFlag & DEBUG_DEBUG) { taosPrintLog("MND ", DEBUG_DEBUG, mDebugFlag, __VA_ARGS__); }}
#define mTrace(...) { if (mDebugFlag & DEBUG_TRACE) { taosPrintLog("MND ", DEBUG_TRACE, mDebugFlag, __VA_ARGS__); }}
// clang-format on

40
#define SDB_WRITE_DELTA 20
41

42 43 44 45 46 47
#define SDB_GET_VAL(pData, dataPos, val, pos, func, type) \
  {                                                       \
    if (func(pRaw, dataPos, val) != 0) {                  \
      goto pos;                                           \
    }                                                     \
    dataPos += sizeof(type);                              \
S
Shengliang Guan 已提交
48 49
  }

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

58 59 60
#define SDB_GET_INT64(pData, dataPos, val, pos) SDB_GET_VAL(pData, dataPos, val, pos, sdbGetRawInt64, int64_t)
#define SDB_GET_INT32(pData, dataPos, val, pos) SDB_GET_VAL(pData, dataPos, val, pos, sdbGetRawInt32, int32_t)
#define SDB_GET_INT16(pData, dataPos, val, pos) SDB_GET_VAL(pData, dataPos, val, pos, sdbGetRawInt16, int16_t)
S
Shengliang Guan 已提交
61
#define SDB_GET_INT8(pData, dataPos, val, pos)  SDB_GET_VAL(pData, dataPos, val, pos, sdbGetRawInt8, int8_t)
S
Shengliang Guan 已提交
62

63 64 65 66
#define SDB_GET_RESERVE(pRaw, dataPos, valLen, pos) \
  {                                                 \
    char val[valLen] = {0};                         \
    SDB_GET_BINARY(pRaw, dataPos, val, valLen, pos) \
S
Shengliang Guan 已提交
67 68
  }

69 70 71 72 73 74
#define SDB_SET_VAL(pRaw, dataPos, val, pos, func, type) \
  {                                                      \
    if (func(pRaw, dataPos, val) != 0) {                 \
      goto pos;                                          \
    }                                                    \
    dataPos += sizeof(type);                             \
S
Shengliang Guan 已提交
75 76
  }

77 78 79
#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)
S
Shengliang Guan 已提交
80
#define SDB_SET_INT8(pRaw, dataPos, val, pos)  SDB_SET_VAL(pRaw, dataPos, val, pos, sdbSetRawInt8, int8_t)
81 82

#define SDB_SET_BINARY(pRaw, dataPos, val, valLen, pos)     \
S
Shengliang Guan 已提交
83 84
  {                                                         \
    if (sdbSetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
85
      goto pos;                                             \
S
Shengliang Guan 已提交
86 87 88 89
    }                                                       \
    dataPos += valLen;                                      \
  }

90 91 92 93
#define SDB_SET_RESERVE(pRaw, dataPos, valLen, pos) \
  {                                                 \
    char val[valLen] = {0};                         \
    SDB_SET_BINARY(pRaw, dataPos, val, valLen, pos) \
S
Shengliang Guan 已提交
94 95
  }

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

S
Shengliang Guan 已提交
103
typedef struct SMnode  SMnode;
S
Shengliang Guan 已提交
104
typedef struct SSdb    SSdb;
S
Shengliang Guan 已提交
105 106
typedef struct SSdbRaw SSdbRaw;
typedef struct SSdbRow SSdbRow;
S
Shengliang Guan 已提交
107 108 109 110 111 112 113
typedef int32_t (*SdbInsertFp)(SSdb *pSdb, void *pObj);
typedef int32_t (*SdbUpdateFp)(SSdb *pSdb, void *pSrcObj, void *pDstObj);
typedef int32_t (*SdbDeleteFp)(SSdb *pSdb, void *pObj, bool callFunc);
typedef int32_t (*SdbDeployFp)(SMnode *pMnode);
typedef SSdbRow *(*SdbDecodeFp)(SSdbRaw *pRaw);
typedef SSdbRaw *(*SdbEncodeFp)(void *pObj);
typedef bool (*sdbTraverseFp)(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3);
114 115 116 117 118 119 120

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

S
Shengliang Guan 已提交
121
typedef enum {
122
  SDB_STATUS_INIT = 0,
S
Shengliang Guan 已提交
123
  SDB_STATUS_CREATING = 1,
S
Shengliang Guan 已提交
124 125
  SDB_STATUS_DROPPING = 2,
  SDB_STATUS_DROPPED = 3,
S
Shengliang Guan 已提交
126
  SDB_STATUS_READY = 4,
S
Shengliang Guan 已提交
127 128
} ESdbStatus;

S
Shengliang Guan 已提交
129
typedef enum {
S
Shengliang Guan 已提交
130 131 132 133 134 135 136 137 138
  SDB_TRANS = 0,
  SDB_CLUSTER = 1,
  SDB_MNODE = 2,
  SDB_QNODE = 3,
  SDB_SNODE = 4,
  SDB_DNODE = 6,
  SDB_USER = 7,
  SDB_AUTH = 8,
  SDB_ACCT = 9,
L
Liu Jicong 已提交
139 140 141 142 143 144 145 146 147 148 149
  SDB_STREAM_CK = 10,
  SDB_STREAM = 11,
  SDB_OFFSET = 12,
  SDB_SUBSCRIBE = 13,
  SDB_CONSUMER = 14,
  SDB_TOPIC = 15,
  SDB_VGROUP = 16,
  SDB_SMA = 17,
  SDB_STB = 18,
  SDB_DB = 19,
  SDB_FUNC = 20,
dengyihao's avatar
dengyihao 已提交
150 151
  SDB_IDX = 21,
  SDB_MAX = 22
S
Shengliang Guan 已提交
152 153
} ESdbType;

S
Shengliang Guan 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
typedef struct SSdbRaw {
  int8_t  type;
  int8_t  status;
  int8_t  sver;
  int8_t  reserved;
  int32_t dataLen;
  char    pData[];
} SSdbRaw;

typedef struct SSdbRow {
  ESdbType   type;
  ESdbStatus status;
  int32_t    refCount;
  char       pObj[];
} SSdbRow;

typedef struct SSdb {
  SMnode        *pMnode;
172
  SWal          *pWal;
173
  int64_t        sync;
S
Shengliang Guan 已提交
174 175
  char          *currDir;
  char          *tmpDir;
S
Shengliang Guan 已提交
176 177 178 179 180 181
  int64_t        commitIndex;
  int64_t        commitTerm;
  int64_t        commitConfig;
  int64_t        applyIndex;
  int64_t        applyTerm;
  int64_t        applyConfig;
S
Shengliang Guan 已提交
182 183 184 185 186 187 188 189 190 191 192
  int64_t        tableVer[SDB_MAX];
  int64_t        maxId[SDB_MAX];
  EKeyType       keyTypes[SDB_MAX];
  SHashObj      *hashObjs[SDB_MAX];
  TdThreadRwlock 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];
S
Shengliang Guan 已提交
193
  TdThreadMutex  filelock;
S
Shengliang Guan 已提交
194 195 196 197
} SSdb;

typedef struct SSdbIter {
  TdFilePtr file;
S
Shengliang Guan 已提交
198
  int64_t   total;
S
Shengliang Guan 已提交
199
  char     *name;
S
Shengliang Guan 已提交
200
} SSdbIter;
S
Shengliang Guan 已提交
201

S
Shengliang Guan 已提交
202
typedef struct {
S
Shengliang Guan 已提交
203 204
  ESdbType    sdbType;
  EKeyType    keyType;
S
Shengliang Guan 已提交
205 206 207 208 209 210
  SdbDeployFp deployFp;
  SdbEncodeFp encodeFp;
  SdbDecodeFp decodeFp;
  SdbInsertFp insertFp;
  SdbUpdateFp updateFp;
  SdbDeleteFp deleteFp;
S
Shengliang Guan 已提交
211
} SSdbTable;
S
Shengliang Guan 已提交
212

S
Shengliang Guan 已提交
213 214
typedef struct SSdbOpt {
  const char *path;
S
Shengliang Guan 已提交
215
  SMnode     *pMnode;
216
  SWal       *pWal;
217
  int64_t     sync;
S
Shengliang Guan 已提交
218
} SSdbOpt;
S
Shengliang Guan 已提交
219

S
Shengliang Guan 已提交
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
/**
 * @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 已提交
242
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table);
S
Shengliang Guan 已提交
243 244 245 246 247 248 249

/**
 * @brief Set the initial rows of sdb.
 *
 * @param pSdb The sdb object.
 * @return int32_t 0 for success, -1 for failure.
 */
S
Shengliang Guan 已提交
250
int32_t sdbDeploy(SSdb *pSdb);
S
Shengliang Guan 已提交
251 252 253 254 255 256 257

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

260 261 262 263 264 265
/**
 * @brief Write sdb file.
 *
 * @param pSdb The sdb object.
 * @return int32_t 0 for success, -1 for failure.
 */
266
int32_t sdbWriteFile(SSdb *pSdb, int32_t delta);
267

S
Shengliang Guan 已提交
268
/**
S
Shengliang Guan 已提交
269
 * @brief Parse and write raw data to sdb, then free the pRaw object
S
Shengliang Guan 已提交
270 271 272 273 274
 *
 * @param pSdb The sdb object.
 * @param pRaw The raw data.
 * @return int32_t 0 for success, -1 for failure.
 */
S
Shengliang Guan 已提交
275
int32_t sdbWrite(SSdb *pSdb, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
276

S
Shengliang Guan 已提交
277 278 279 280 281 282 283
/**
 * @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.
 */
284
int32_t sdbWriteWithoutFree(SSdb *pSdb, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
285

S
Shengliang Guan 已提交
286 287 288 289 290 291 292 293
/**
 * @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 已提交
294
void *sdbAcquire(SSdb *pSdb, ESdbType type, const void *pKey);
295
void *sdbAcquireNotReadyObj(SSdb *pSdb, ESdbType type, const void *pKey);
S
Shengliang Guan 已提交
296 297 298 299 300 301 302 303

/**
 * @brief Release a row from sdb.
 *
 * @param pSdb The sdb object.
 * @param pObj The object of the row.
 */
void sdbRelease(SSdb *pSdb, void *pObj);
304
void sdbReleaseLock(SSdb *pSdb, void *pObj, bool lock);
S
Shengliang Guan 已提交
305 306 307 308 309 310

/**
 * @brief Traverse a sdb table
 *
 * @param pSdb The sdb object.
 * @param type The type of the table.
S
Shengliang Guan 已提交
311
 * @param pIter The initial iterator of the table.
S
Shengliang Guan 已提交
312 313 314 315
 * @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);
316
void *sdbFetchAll(SSdb *pSdb, ESdbType type, void *pIter, void **ppObj, ESdbStatus *status, bool lock);
S
Shengliang Guan 已提交
317 318 319 320 321 322 323 324 325

/**
 * @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 已提交
326 327 328 329 330 331 332 333 334 335 336 337
/**
 * @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 已提交
338 339 340 341 342
/**
 * @brief Get the number of rows in the table
 *
 * @param pSdb The sdb object.
 * @param pIter The type of the table.
S
Shengliang Guan 已提交
343
 * @return int32_t The number of rows in the table
S
Shengliang Guan 已提交
344
 */
S
Shengliang Guan 已提交
345
int32_t sdbGetSize(SSdb *pSdb, ESdbType type);
S
Shengliang Guan 已提交
346

S
Shengliang Guan 已提交
347 348 349 350 351
/**
 * @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 已提交
352
 * @return int32_t The max id of the table
S
Shengliang Guan 已提交
353 354 355
 */
int32_t sdbGetMaxId(SSdb *pSdb, ESdbType type);

356 357 358 359 360 361 362 363 364
/**
 * @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 已提交
365
/**
366
 * @brief Update the index of sdb
S
Shengliang Guan 已提交
367 368
 *
 * @param pSdb The sdb object.
369 370
 * @param index The update value of the apply index.
 * @return int32_t The current index of sdb
S
Shengliang Guan 已提交
371
 */
372 373
void sdbSetApplyInfo(SSdb *pSdb, int64_t index, int64_t term, int64_t config);
void sdbGetCommitInfo(SSdb *pSdb, int64_t *index, int64_t *term, int64_t *config);
S
Shengliang Guan 已提交
374

S
Shengliang Guan 已提交
375
SSdbRaw *sdbAllocRaw(ESdbType type, int8_t sver, int32_t dataLen);
S
Shengliang Guan 已提交
376 377
void     sdbFreeRaw(SSdbRaw *pRaw);
int32_t  sdbSetRawInt8(SSdbRaw *pRaw, int32_t dataPos, int8_t val);
S
Shengliang Guan 已提交
378
int32_t  sdbSetRawInt16(SSdbRaw *pRaw, int32_t dataPos, int16_t val);
S
Shengliang Guan 已提交
379 380 381 382 383 384
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 已提交
385
int32_t  sdbGetRawInt16(SSdbRaw *pRaw, int32_t dataPos, int16_t *val);
S
Shengliang Guan 已提交
386 387 388 389 390 391 392 393
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);
S
Shengliang Guan 已提交
394
void     sdbFreeRow(SSdb *pSdb, SSdbRow *pRow, bool callFunc);
S
Shengliang Guan 已提交
395

396
int32_t sdbStartRead(SSdb *pSdb, SSdbIter **ppIter, int64_t *index, int64_t *term, int64_t *config);
397
void    sdbStopRead(SSdb *pSdb, SSdbIter *pIter);
S
Shengliang Guan 已提交
398 399 400
int32_t sdbDoRead(SSdb *pSdb, SSdbIter *pIter, void **ppBuf, int32_t *len);

int32_t sdbStartWrite(SSdb *pSdb, SSdbIter **ppIter);
401
int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, int64_t term, int64_t config);
S
Shengliang Guan 已提交
402
int32_t sdbDoWrite(SSdb *pSdb, SSdbIter *pIter, void *pBuf, int32_t len);
S
Shengliang Guan 已提交
403

S
Shengliang Guan 已提交
404
const char *sdbTableName(ESdbType type);
S
Shengliang Guan 已提交
405
const char *sdbStatusName(ESdbStatus status);
S
Shengliang Guan 已提交
406
void        sdbPrintOper(SSdb *pSdb, SSdbRow *pRow, const char *oper);
S
Shengliang Guan 已提交
407
int32_t     sdbGetIdFromRaw(SSdb *pSdb, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
408

409 410 411 412
void sdbWriteLock(SSdb *pSdb, int32_t type);
void sdbReadLock(SSdb *pSdb, int32_t type);
void sdbUnLock(SSdb *pSdb, int32_t type);

S
Shengliang Guan 已提交
413 414 415
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
416

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