sdb.h 11.2 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 48 49
#define SDB_GET_INT16(pData, pRow, dataPos, val)   \
  {                                                \
    if (sdbGetRawInt16(pRaw, dataPos, val) != 0) { \
      sdbFreeRow(pRow);                            \
      return NULL;                                 \
    }                                              \
    dataPos += sizeof(int16_t);                    \
  }

S
Shengliang Guan 已提交
50 51 52 53 54 55 56
#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 已提交
57 58
  }

S
Shengliang Guan 已提交
59 60 61 62 63 64 65
#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 已提交
66 67
  }

S
Shengliang Guan 已提交
68 69 70 71 72 73 74 75 76 77
#define SDB_GET_RESERVE(pRaw, pRow, dataPos, valLen)        \
  {                                                         \
    char val[valLen] = {0};                                 \
    if (sdbGetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
      sdbFreeRow(pRow);                                     \
      return NULL;                                          \
    }                                                       \
    dataPos += valLen;                                      \
  }

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

S
Shengliang Guan 已提交
87
#define SDB_SET_INT32(pRaw, dataPos, val)          \
S
Shengliang Guan 已提交
88 89
  {                                                \
    if (sdbSetRawInt32(pRaw, dataPos, val) != 0) { \
S
Shengliang Guan 已提交
90
      sdbFreeRaw(pRaw);                            \
S
Shengliang Guan 已提交
91
      return NULL;                                 \
S
Shengliang Guan 已提交
92
    }                                              \
S
Shengliang Guan 已提交
93 94 95
    dataPos += sizeof(int32_t);                    \
  }

S
Shengliang Guan 已提交
96 97 98 99 100 101 102 103 104
#define SDB_SET_INT16(pRaw, dataPos, val)          \
  {                                                \
    if (sdbSetRawInt16(pRaw, dataPos, val) != 0) { \
      sdbFreeRaw(pRaw);                            \
      return NULL;                                 \
    }                                              \
    dataPos += sizeof(int16_t);                     \
  }

S
Shengliang Guan 已提交
105
#define SDB_SET_INT8(pRaw, dataPos, val)          \
S
Shengliang Guan 已提交
106 107
  {                                               \
    if (sdbSetRawInt8(pRaw, dataPos, val) != 0) { \
S
Shengliang Guan 已提交
108
      sdbFreeRaw(pRaw);                           \
S
Shengliang Guan 已提交
109
      return NULL;                                \
S
Shengliang Guan 已提交
110
    }                                             \
S
Shengliang Guan 已提交
111 112 113 114 115 116
    dataPos += sizeof(int8_t);                    \
  }

#define SDB_SET_BINARY(pRaw, dataPos, val, valLen)          \
  {                                                         \
    if (sdbSetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
S
Shengliang Guan 已提交
117 118 119 120 121 122 123 124 125 126
      sdbFreeRaw(pRaw);                                     \
      return NULL;                                          \
    }                                                       \
    dataPos += valLen;                                      \
  }

#define SDB_SET_RESERVE(pRaw, dataPos, valLen)              \
  {                                                         \
    char val[valLen] = {0};                                 \
    if (sdbSetRawBinary(pRaw, dataPos, val, valLen) != 0) { \
S
Shengliang Guan 已提交
127
      sdbFreeRaw(pRaw);                                     \
S
Shengliang Guan 已提交
128
      return NULL;                                          \
S
Shengliang Guan 已提交
129
    }                                                       \
S
Shengliang Guan 已提交
130 131 132 133 134 135
    dataPos += valLen;                                      \
  }

#define SDB_SET_DATALEN(pRaw, dataLen)          \
  {                                             \
    if (sdbSetRawDataLen(pRaw, dataLen) != 0) { \
S
Shengliang Guan 已提交
136
      sdbFreeRaw(pRaw);                         \
S
Shengliang Guan 已提交
137
      return NULL;                              \
S
Shengliang Guan 已提交
138
    }                                           \
S
Shengliang Guan 已提交
139 140
  }

S
Shengliang Guan 已提交
141
typedef struct SMnode  SMnode;
S
Shengliang Guan 已提交
142 143 144 145 146 147
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,
S
Shengliang Guan 已提交
148 149
  SDB_STATUS_DROPPING = 3,
  SDB_STATUS_DROPPED = 4
S
Shengliang Guan 已提交
150 151
} ESdbStatus;

S
Shengliang Guan 已提交
152
typedef enum {
S
Shengliang Guan 已提交
153
  SDB_START = 0,
S
Shengliang Guan 已提交
154
  SDB_TRANS = 1,
S
Shengliang Guan 已提交
155
  SDB_CLUSTER = 2,
S
Shengliang Guan 已提交
156 157
  SDB_MNODE = 3,
  SDB_DNODE = 4,
158
  SDB_USER = 5,
S
Shengliang Guan 已提交
159
  SDB_AUTH = 6,
160
  SDB_ACCT = 7,
S
Shengliang Guan 已提交
161
  SDB_VGROUP = 9,
S
Shengliang Guan 已提交
162 163
  SDB_STABLE = 9,
  SDB_DB = 10,
S
Shengliang Guan 已提交
164 165
  SDB_FUNC = 11,
  SDB_MAX = 12
S
Shengliang Guan 已提交
166 167
} ESdbType;

S
Shengliang Guan 已提交
168 169 170 171
typedef struct SSdb SSdb;
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);
S
Shengliang Guan 已提交
172
typedef int32_t (*SdbDeployFp)(SMnode *pMnode);
S
Shengliang Guan 已提交
173
typedef SSdbRow *(*SdbDecodeFp)(SSdbRaw *pRaw);
S
Shengliang Guan 已提交
174
typedef SSdbRaw *(*SdbEncodeFp)(void *pObj);
S
Shengliang Guan 已提交
175

S
Shengliang Guan 已提交
176
typedef struct {
S
Shengliang Guan 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
  /**
   * @brief The sdb type of the table.
   *
   */
  ESdbType sdbType;

  /**
   * @brief The key type of the table.
   *
   */
  EKeyType keyType;

  /**
   * @brief The callback function when the table is first deployed.
   *
   */
S
Shengliang Guan 已提交
193
  SdbDeployFp deployFp;
S
Shengliang Guan 已提交
194 195 196 197 198

  /**
   * @brief Encode one row of the table into rawdata.
   *
   */
S
Shengliang Guan 已提交
199
  SdbEncodeFp encodeFp;
S
Shengliang Guan 已提交
200 201 202 203 204

  /**
   * @brief Decode one row of the table from rawdata.
   *
   */
S
Shengliang Guan 已提交
205
  SdbDecodeFp decodeFp;
S
Shengliang Guan 已提交
206 207 208 209 210

  /**
   * @brief The callback function when insert a row to sdb.
   *
   */
S
Shengliang Guan 已提交
211
  SdbInsertFp insertFp;
S
Shengliang Guan 已提交
212 213 214 215 216

  /**
   * @brief The callback function when undate a row in sdb.
   *
   */
S
Shengliang Guan 已提交
217
  SdbUpdateFp updateFp;
S
Shengliang Guan 已提交
218 219 220 221 222

  /**
   * @brief The callback function when delete a row from sdb.
   *
   */
S
Shengliang Guan 已提交
223
  SdbDeleteFp deleteFp;
S
Shengliang Guan 已提交
224
} SSdbTable;
S
Shengliang Guan 已提交
225

S
Shengliang Guan 已提交
226
typedef struct SSdbOpt {
S
Shengliang Guan 已提交
227 228 229 230
  /**
   * @brief The path of the sdb file.
   *
   */
S
Shengliang Guan 已提交
231
  const char *path;
S
Shengliang Guan 已提交
232 233 234 235 236 237

  /**
   * @brief The mnode object.
   *
   */
  SMnode *pMnode;
S
Shengliang Guan 已提交
238
} SSdbOpt;
S
Shengliang Guan 已提交
239

S
Shengliang Guan 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
/**
 * @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 已提交
262
int32_t sdbSetTable(SSdb *pSdb, SSdbTable table);
S
Shengliang Guan 已提交
263 264 265 266 267 268 269

/**
 * @brief Set the initial rows of sdb.
 *
 * @param pSdb The sdb object.
 * @return int32_t 0 for success, -1 for failure.
 */
S
Shengliang Guan 已提交
270
int32_t sdbDeploy(SSdb *pSdb);
S
Shengliang Guan 已提交
271 272 273 274 275 276 277

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

/**
S
Shengliang Guan 已提交
281
 * @brief Parse and write raw data to sdb, then free the pRaw object
S
Shengliang Guan 已提交
282 283 284 285 286
 *
 * @param pSdb The sdb object.
 * @param pRaw The raw data.
 * @return int32_t 0 for success, -1 for failure.
 */
S
Shengliang Guan 已提交
287
int32_t sdbWrite(SSdb *pSdb, SSdbRaw *pRaw);
S
Shengliang Guan 已提交
288

S
Shengliang Guan 已提交
289 290 291 292 293 294 295 296 297
/**
 * @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.
 */
int32_t sdbWriteNotFree(SSdb *pSdb, SSdbRaw *pRaw);

S
Shengliang Guan 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
/**
 * @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.
 */
void *sdbAcquire(SSdb *pSdb, ESdbType type, void *pKey);

/**
 * @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.
 * @param type The initial iterator of the table.
 * @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 pIter The iterator of the table.
 * @param type The initial iterator of table.
 */
void sdbCancelFetch(SSdb *pSdb, void *pIter);

/**
 * @brief Get the number of rows in the table
 *
 * @param pSdb The sdb object.
 * @param pIter The type of the table.
 * @record int32_t The number of rows in the table
 */
S
Shengliang Guan 已提交
343
int32_t sdbGetSize(SSdb *pSdb, ESdbType type);
S
Shengliang Guan 已提交
344

S
Shengliang Guan 已提交
345
SSdbRaw *sdbAllocRaw(ESdbType type, int8_t sver, int32_t dataLen);
S
Shengliang Guan 已提交
346 347
void     sdbFreeRaw(SSdbRaw *pRaw);
int32_t  sdbSetRawInt8(SSdbRaw *pRaw, int32_t dataPos, int8_t val);
S
Shengliang Guan 已提交
348
int32_t  sdbSetRawInt16(SSdbRaw *pRaw, int32_t dataPos, int16_t val);
S
Shengliang Guan 已提交
349 350 351 352 353 354
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 已提交
355
int32_t  sdbGetRawInt16(SSdbRaw *pRaw, int32_t dataPos, int16_t *val);
S
Shengliang Guan 已提交
356 357 358 359 360 361 362 363 364 365
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 已提交
366 367 368
#ifdef __cplusplus
}
#endif
S
Shengliang Guan 已提交
369

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