streamState.c 34.6 KB
Newer Older
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/>.
 */

dengyihao's avatar
dengyihao 已提交
16
#include "streamState.h"
17
#include "executor.h"
dengyihao's avatar
dengyihao 已提交
18
#include "osMemory.h"
dengyihao's avatar
dengyihao 已提交
19
#include "rocksdb/c.h"
dengyihao's avatar
dengyihao 已提交
20
#include "streamBackendRocksdb.h"
21
#include "streamInc.h"
dengyihao's avatar
dengyihao 已提交
22
#include "tcoding.h"
23
#include "tcommon.h"
24
#include "tcompare.h"
dengyihao's avatar
dengyihao 已提交
25
#include "tref.h"
26

L
liuyao 已提交
27
#define MAX_TABLE_NAME_NUM 200000
L
liuyao 已提交
28

dengyihao's avatar
dengyihao 已提交
29
int sessionRangeKeyCmpr(const SSessionKey* pWin1, const SSessionKey* pWin2) {
5
54liuyao 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
  if (pWin1->groupId > pWin2->groupId) {
    return 1;
  } else if (pWin1->groupId < pWin2->groupId) {
    return -1;
  }

  if (pWin1->win.skey > pWin2->win.ekey) {
    return 1;
  } else if (pWin1->win.ekey < pWin2->win.skey) {
    return -1;
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
45
int sessionWinKeyCmpr(const SSessionKey* pWin1, const SSessionKey* pWin2) {
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  if (pWin1->groupId > pWin2->groupId) {
    return 1;
  } else if (pWin1->groupId < pWin2->groupId) {
    return -1;
  }

  if (pWin1->win.skey > pWin2->win.skey) {
    return 1;
  } else if (pWin1->win.skey < pWin2->win.skey) {
    return -1;
  }

  if (pWin1->win.ekey > pWin2->win.ekey) {
    return 1;
  } else if (pWin1->win.ekey < pWin2->win.ekey) {
    return -1;
  }

  return 0;
}

dengyihao's avatar
dengyihao 已提交
67
int stateSessionKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2) {
5
54liuyao 已提交
68 69 70 71 72 73 74 75 76
  SStateSessionKey* pWin1 = (SStateSessionKey*)pKey1;
  SStateSessionKey* pWin2 = (SStateSessionKey*)pKey2;

  if (pWin1->opNum > pWin2->opNum) {
    return 1;
  } else if (pWin1->opNum < pWin2->opNum) {
    return -1;
  }

77
  return sessionWinKeyCmpr(&pWin1->key, &pWin2->key);
5
54liuyao 已提交
78 79
}

dengyihao's avatar
dengyihao 已提交
80
int stateKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2) {
81 82 83 84 85 86 87 88 89
  SStateKey* pWin1 = (SStateKey*)pKey1;
  SStateKey* pWin2 = (SStateKey*)pKey2;

  if (pWin1->opNum > pWin2->opNum) {
    return 1;
  } else if (pWin1->opNum < pWin2->opNum) {
    return -1;
  }

L
liuyao 已提交
90
  return winKeyCmprImpl(&pWin1->key, &pWin2->key);
91 92
}

93 94
SStreamState* streamStateOpen(char* path, void* pTask, bool specPath, int32_t szPage, int32_t pages) {
  qDebug("open stream state, %s", path);
95 96 97 98 99
  SStreamState* pState = taosMemoryCalloc(1, sizeof(SStreamState));
  if (pState == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
100

5
54liuyao 已提交
101 102 103
  pState->pTdbState = taosMemoryCalloc(1, sizeof(STdbState));
  if (pState->pTdbState == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
dengyihao's avatar
dengyihao 已提交
104
    streamStateDestroy(pState, true);
5
54liuyao 已提交
105 106
    return NULL;
  }
L
Liu Jicong 已提交
107

108
  SStreamTask* pStreamTask = pTask;
dengyihao's avatar
dengyihao 已提交
109
  char         statePath[1024];
L
Liu Jicong 已提交
110
  if (!specPath) {
111
    sprintf(statePath, "%s/%d", path, pStreamTask->id.taskId);
L
Liu Jicong 已提交
112
  } else {
113 114
    memset(statePath, 0, 1024);
    tstrncpy(statePath, path, 1024);
L
Liu Jicong 已提交
115
  }
116 117 118

  pState->taskId = pStreamTask->id.taskId;
  pState->streamId = pStreamTask->id.streamId;
Y
yihaoDeng 已提交
119
  sprintf(pState->pTdbState->idstr, "0x%" PRIx64 "-%d", pState->streamId, pState->taskId);
120

dengyihao's avatar
dengyihao 已提交
121
#ifdef USE_ROCKSDB
122
  SStreamMeta* pMeta = pStreamTask->pMeta;
dengyihao's avatar
dengyihao 已提交
123
  pState->streamBackendRid = pMeta->streamBackendRid;
Y
yihaoDeng 已提交
124 125 126 127 128 129 130 131 132 133 134 135 136
  // taosWLockLatch(&pMeta->lock);
  taosThreadMutexLock(&pMeta->backendMutex);
  void* uniqueId =
      taosHashGet(pMeta->pTaskBackendUnique, pState->pTdbState->idstr, strlen(pState->pTdbState->idstr) + 1);
  if (uniqueId == NULL) {
    int code = streamStateOpenBackend(pMeta->streamBackend, pState);
    if (code == -1) {
      taosReleaseRef(streamBackendId, pState->streamBackendRid);
      taosThreadMutexUnlock(&pMeta->backendMutex);
      taosMemoryFree(pState);
      return NULL;
    }
    taosHashPut(pMeta->pTaskBackendUnique, pState->pTdbState->idstr, strlen(pState->pTdbState->idstr) + 1,
Y
yihaoDeng 已提交
137
                &pState->pTdbState->backendCfWrapperId, sizeof(pState->pTdbState->backendCfWrapperId));
Y
yihaoDeng 已提交
138 139
  } else {
    int64_t id = *(int64_t*)uniqueId;
Y
yihaoDeng 已提交
140 141
    pState->pTdbState->backendCfWrapperId = id;
    pState->pTdbState->pBackendCfWrapper = taosAcquireRef(streamBackendCfWrapperId, id);
Y
yihaoDeng 已提交
142 143

    taosAcquireRef(streamBackendId, pState->streamBackendRid);
dengyihao's avatar
dengyihao 已提交
144
  }
Y
yihaoDeng 已提交
145
  taosThreadMutexUnlock(&pMeta->backendMutex);
146

dengyihao's avatar
dengyihao 已提交
147
  pState->pTdbState->pOwner = pTask;
5
54liuyao 已提交
148
  pState->pFileState = NULL;
L
liuyao 已提交
149
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT);
dengyihao's avatar
dengyihao 已提交
150

151
  pState->parNameMap = tSimpleHashInit(1024, hashFn);
dengyihao's avatar
dengyihao 已提交
152 153 154
  return pState;

#else
L
add cfg  
Liu Jicong 已提交
155

L
Liu Jicong 已提交
156
  char cfgPath[1030];
L
add cfg  
Liu Jicong 已提交
157 158
  sprintf(cfgPath, "%s/cfg", statePath);

D
dapan1121 已提交
159 160
  szPage = szPage < 0 ? 4096 : szPage;
  pages = pages < 0 ? 256 : pages;
L
add cfg  
Liu Jicong 已提交
161 162 163 164
  char cfg[1024];
  memset(cfg, 0, 1024);
  TdFilePtr pCfgFile = taosOpenFile(cfgPath, TD_FILE_READ);
  if (pCfgFile != NULL) {
D
dapan1121 已提交
165
    int64_t size = 0;
L
add cfg  
Liu Jicong 已提交
166
    taosFStatFile(pCfgFile, &size, NULL);
D
dapan1121 已提交
167 168 169 170
    if (size > 0) {
      taosReadFile(pCfgFile, cfg, size);
      sscanf(cfg, "%d\n%d\n", &szPage, &pages);
    }
L
add cfg  
Liu Jicong 已提交
171
  } else {
D
dapan1121 已提交
172 173 174 175 176 177
    int32_t code = taosMulModeMkDir(statePath, 0755);
    if (code == 0) {
      pCfgFile = taosOpenFile(cfgPath, TD_FILE_WRITE | TD_FILE_CREATE);
      sprintf(cfg, "%d\n%d\n", szPage, pages);
      taosWriteFile(pCfgFile, cfg, strlen(cfg));
    }
L
add cfg  
Liu Jicong 已提交
178 179 180
  }
  taosCloseFile(&pCfgFile);

181
  if (tdbOpen(statePath, szPage, pages, &pState->pTdbState->db, 1) < 0) {
182 183 184 185
    goto _err;
  }

  // open state storage backend
5
54liuyao 已提交
186 187
  if (tdbTbOpen("state.db", sizeof(SStateKey), -1, stateKeyCmpr, pState->pTdbState->db, &pState->pTdbState->pStateDb,
                0) < 0) {
188 189 190
    goto _err;
  }

5
54liuyao 已提交
191
  // todo refactor
5
54liuyao 已提交
192 193
  if (tdbTbOpen("fill.state.db", sizeof(SWinKey), -1, winKeyCmpr, pState->pTdbState->db,
                &pState->pTdbState->pFillStateDb, 0) < 0) {
5
54liuyao 已提交
194 195 196
    goto _err;
  }

5
54liuyao 已提交
197 198
  if (tdbTbOpen("session.state.db", sizeof(SStateSessionKey), -1, stateSessionKeyCmpr, pState->pTdbState->db,
                &pState->pTdbState->pSessionStateDb, 0) < 0) {
5
54liuyao 已提交
199 200 201
    goto _err;
  }

5
54liuyao 已提交
202 203
  if (tdbTbOpen("func.state.db", sizeof(STupleKey), -1, STupleKeyCmpr, pState->pTdbState->db,
                &pState->pTdbState->pFuncStateDb, 0) < 0) {
204 205 206
    goto _err;
  }

5
54liuyao 已提交
207 208
  if (tdbTbOpen("parname.state.db", sizeof(int64_t), TSDB_TABLE_NAME_LEN, NULL, pState->pTdbState->db,
                &pState->pTdbState->pParNameDb, 0) < 0) {
209 210 211
    goto _err;
  }

L
Liu Jicong 已提交
212 213 214 215 216
  if (tdbTbOpen("partag.state.db", sizeof(int64_t), -1, NULL, pState->pTdbState->db, &pState->pTdbState->pParTagDb, 0) <
      0) {
    goto _err;
  }

217
  if (streamStateBegin(pState) < 0) {
218 219 220
    goto _err;
  }

5
54liuyao 已提交
221
  pState->pTdbState->pOwner = pTask;
L
liuyao 已提交
222
  pState->checkPointId = 0;
223 224 225 226

  return pState;

_err:
5
54liuyao 已提交
227 228 229 230 231
  tdbTbClose(pState->pTdbState->pStateDb);
  tdbTbClose(pState->pTdbState->pFuncStateDb);
  tdbTbClose(pState->pTdbState->pFillStateDb);
  tdbTbClose(pState->pTdbState->pSessionStateDb);
  tdbTbClose(pState->pTdbState->pParNameDb);
L
Liu Jicong 已提交
232
  tdbTbClose(pState->pTdbState->pParTagDb);
5
54liuyao 已提交
233
  tdbClose(pState->pTdbState->db);
dengyihao's avatar
dengyihao 已提交
234
  streamStateDestroy(pState, false);
235
  return NULL;
dengyihao's avatar
dengyihao 已提交
236
#endif
237 238
}

dengyihao's avatar
dengyihao 已提交
239
void streamStateClose(SStreamState* pState, bool remove) {
dengyihao's avatar
dengyihao 已提交
240
  SStreamTask* pTask = pState->pTdbState->pOwner;
dengyihao's avatar
dengyihao 已提交
241
#ifdef USE_ROCKSDB
dengyihao's avatar
dengyihao 已提交
242
  streamStateDestroy(pState, remove);
dengyihao's avatar
dengyihao 已提交
243
#else
244 245
  tdbCommit(pState->pTdbState->db, pState->pTdbState->txn);
  tdbPostCommit(pState->pTdbState->db, pState->pTdbState->txn);
5
54liuyao 已提交
246 247 248 249 250
  tdbTbClose(pState->pTdbState->pStateDb);
  tdbTbClose(pState->pTdbState->pFuncStateDb);
  tdbTbClose(pState->pTdbState->pFillStateDb);
  tdbTbClose(pState->pTdbState->pSessionStateDb);
  tdbTbClose(pState->pTdbState->pParNameDb);
L
Liu Jicong 已提交
251
  tdbTbClose(pState->pTdbState->pParTagDb);
5
54liuyao 已提交
252
  tdbClose(pState->pTdbState->db);
dengyihao's avatar
dengyihao 已提交
253
#endif
254 255 256
}

int32_t streamStateBegin(SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
257 258 259
#ifdef USE_ROCKSDB
  return 0;
#else
260
  if (tdbBegin(pState->pTdbState->db, &pState->pTdbState->txn, NULL, NULL, NULL,
261
               TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) < 0) {
262
    tdbAbort(pState->pTdbState->db, pState->pTdbState->txn);
263 264 265
    return -1;
  }
  return 0;
dengyihao's avatar
dengyihao 已提交
266
#endif
267 268 269
}

int32_t streamStateCommit(SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
270
#ifdef USE_ROCKSDB
L
liuyao 已提交
271 272 273 274
  if (pState->pFileState) {
    SStreamSnapshot* pShot = getSnapshot(pState->pFileState);
    flushSnapshot(pState->pFileState, pShot, true);
  }
5
54liuyao 已提交
275
  pState->checkPointId++;
dengyihao's avatar
dengyihao 已提交
276 277
  return 0;
#else
278
  if (tdbCommit(pState->pTdbState->db, pState->pTdbState->txn) < 0) {
M
Minglei Jin 已提交
279 280
    return -1;
  }
281
  if (tdbPostCommit(pState->pTdbState->db, pState->pTdbState->txn) < 0) {
282 283
    return -1;
  }
284

285
  if (tdbBegin(pState->pTdbState->db, &pState->pTdbState->txn, NULL, NULL, NULL,
286
               TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) < 0) {
287 288
    return -1;
  }
L
liuyao 已提交
289
  pState->checkPointId++;
290
  return 0;
dengyihao's avatar
dengyihao 已提交
291
#endif
292 293
}

L
liuyao 已提交
294
int32_t streamStateFuncPut(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen) {
dengyihao's avatar
dengyihao 已提交
295
#ifdef USE_ROCKSDB
dengyihao's avatar
dengyihao 已提交
296 297 298 299
  void*    pVal = NULL;
  int32_t  len = 0;
  int32_t  code = getRowBuff(pState->pFileState, (void*)key, sizeof(SWinKey), &pVal, &len);
  char*    buf = ((SRowBuffPos*)pVal)->pRowBuff;
L
liuyao 已提交
300 301 302
  uint32_t rowSize = streamFileStateGeSelectRowSize(pState->pFileState);
  memcpy(buf + len - rowSize, value, vLen);
  return code;
dengyihao's avatar
dengyihao 已提交
303
#else
304
  return tdbTbUpsert(pState->pTdbState->pFuncStateDb, key, sizeof(STupleKey), value, vLen, pState->pTdbState->txn);
dengyihao's avatar
dengyihao 已提交
305
#endif
306
}
L
liuyao 已提交
307
int32_t streamStateFuncGet(SStreamState* pState, const SWinKey* key, void** ppVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
308
#ifdef USE_ROCKSDB
dengyihao's avatar
dengyihao 已提交
309 310 311 312
  void*    pVal = NULL;
  int32_t  len = 0;
  int32_t  code = getRowBuff(pState->pFileState, (void*)key, sizeof(SWinKey), (void**)(&pVal), &len);
  char*    buf = ((SRowBuffPos*)pVal)->pRowBuff;
L
liuyao 已提交
313 314 315
  uint32_t rowSize = streamFileStateGeSelectRowSize(pState->pFileState);
  *ppVal = buf + len - rowSize;
  return code;
dengyihao's avatar
dengyihao 已提交
316
#else
L
liuyao 已提交
317
  return tdbTbGet(pState->pTdbState->pFuncStateDb, key, sizeof(STupleKey), ppVal, pVLen);
dengyihao's avatar
dengyihao 已提交
318
#endif
319 320
}

321
// todo refactor
322
int32_t streamStatePut(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen) {
dengyihao's avatar
dengyihao 已提交
323
#ifdef USE_ROCKSDB
5
54liuyao 已提交
324 325
  return 0;
  // return streamStatePut_rocksdb(pState, key, value, vLen);
dengyihao's avatar
dengyihao 已提交
326
#else
327
  SStateKey sKey = {.key = *key, .opNum = pState->number};
328
  return tdbTbUpsert(pState->pTdbState->pStateDb, &sKey, sizeof(SStateKey), value, vLen, pState->pTdbState->txn);
dengyihao's avatar
dengyihao 已提交
329
#endif
330
}
5
54liuyao 已提交
331

dengyihao's avatar
dengyihao 已提交
332
int32_t streamStateGet(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
333
#ifdef USE_ROCKSDB
5
54liuyao 已提交
334
  return getRowBuff(pState->pFileState, (void*)key, sizeof(SWinKey), pVal, pVLen);
dengyihao's avatar
dengyihao 已提交
335
#else
dengyihao's avatar
dengyihao 已提交
336 337
  SStateKey sKey = {.key = *key, .opNum = pState->number};
  return tdbTbGet(pState->pTdbState->pStateDb, &sKey, sizeof(SStateKey), pVal, pVLen);
dengyihao's avatar
dengyihao 已提交
338
#endif
5
54liuyao 已提交
339
}
5
54liuyao 已提交
340 341

bool streamStateCheck(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
342
#ifdef USE_ROCKSDB
5
54liuyao 已提交
343 344 345 346 347 348 349 350
  return hasRowBuff(pState->pFileState, (void*)key, sizeof(SWinKey));
#else
  SStateKey sKey = {.key = *key, .opNum = pState->number};
  return tdbTbGet(pState->pTdbState->pStateDb, &sKey, sizeof(SStateKey), pVal, pVLen);
#endif
}

int32_t streamStateGetByPos(SStreamState* pState, void* pos, void** pVal) {
L
liuyao 已提交
351 352 353
  int32_t code = getRowBuffByPos(pState->pFileState, pos, pVal);
  releaseRowBuffPos(pos);
  return code;
5
54liuyao 已提交
354 355
}

356
// todo refactor
dengyihao's avatar
dengyihao 已提交
357
int32_t streamStateDel(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
358
#ifdef USE_ROCKSDB
5
54liuyao 已提交
359
  return deleteRowBuff(pState->pFileState, key, sizeof(SWinKey));
dengyihao's avatar
dengyihao 已提交
360
#else
361
  SStateKey sKey = {.key = *key, .opNum = pState->number};
dengyihao's avatar
dengyihao 已提交
362 363 364 365 366 367 368 369 370 371
  return tdbTbDelete(pState->pTdbState->pStateDb, &sKey, sizeof(SStateKey), pState->pTdbState->txn);
#endif
}

// todo refactor
int32_t streamStateFillPut(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen) {
#ifdef USE_ROCKSDB
  return streamStateFillPut_rocksdb(pState, key, value, vLen);
#else
  return tdbTbUpsert(pState->pTdbState->pFillStateDb, key, sizeof(SWinKey), value, vLen, pState->pTdbState->txn);
dengyihao's avatar
dengyihao 已提交
372
#endif
373 374
}

5
54liuyao 已提交
375 376
// todo refactor
int32_t streamStateFillGet(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
377 378 379
#ifdef USE_ROCKSDB
  return streamStateFillGet_rocksdb(pState, key, pVal, pVLen);
#else
5
54liuyao 已提交
380
  return tdbTbGet(pState->pTdbState->pFillStateDb, key, sizeof(SWinKey), pVal, pVLen);
dengyihao's avatar
dengyihao 已提交
381
#endif
5
54liuyao 已提交
382 383
}

384
// todo refactor
dengyihao's avatar
dengyihao 已提交
385
int32_t streamStateFillDel(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
386
#ifdef USE_ROCKSDB
dengyihao's avatar
dengyihao 已提交
387
  return streamStateFillDel_rocksdb(pState, key);
dengyihao's avatar
dengyihao 已提交
388
#else
dengyihao's avatar
dengyihao 已提交
389
  return tdbTbDelete(pState->pTdbState->pFillStateDb, key, sizeof(SWinKey), pState->pTdbState->txn);
dengyihao's avatar
dengyihao 已提交
390
#endif
391 392
}

393
int32_t streamStateClear(SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
394
#ifdef USE_ROCKSDB
5
54liuyao 已提交
395
  streamFileStateClear(pState->pFileState);
L
liuyao 已提交
396 397 398 399
  if (needClearDiskBuff(pState->pFileState)) {
    streamStateClear_rocksdb(pState);
  }
  return 0;
dengyihao's avatar
dengyihao 已提交
400
#else
401 402 403 404
  SWinKey key = {.ts = 0, .groupId = 0};
  streamStatePut(pState, &key, NULL, 0);
  while (1) {
    SStreamStateCur* pCur = streamStateSeekKeyNext(pState, &key);
Y
yihaoDeng 已提交
405 406
    SWinKey          delKey = {0};
    int32_t          code = streamStateGetKVByCur(pCur, &delKey, NULL, 0);
5
54liuyao 已提交
407
    streamStateFreeCur(pCur);
408 409 410 411 412 413 414
    if (code == 0) {
      streamStateDel(pState, &delKey);
    } else {
      break;
    }
  }
  return 0;
dengyihao's avatar
dengyihao 已提交
415
#endif
416 417 418 419
}

void streamStateSetNumber(SStreamState* pState, int32_t number) { pState->number = number; }

L
fix bug  
liuyao 已提交
420 421 422
int32_t streamStateSaveInfo(SStreamState* pState, void* pKey, int32_t keyLen, void* pVal, int32_t vLen) {
#ifdef USE_ROCKSDB
  int32_t code = 0;
dengyihao's avatar
dengyihao 已提交
423
  void*   batch = streamStateCreateBatch();
dengyihao's avatar
dengyihao 已提交
424

dengyihao's avatar
dengyihao 已提交
425
  code = streamStatePutBatch(pState, "default", batch, pKey, pVal, vLen, 0);
L
fix bug  
liuyao 已提交
426 427 428 429 430 431 432
  if (code != 0) {
    return code;
  }
  code = streamStatePutBatch_rocksdb(pState, batch);
  streamStateDestroyBatch(batch);
  return code;
#else
dengyihao's avatar
dengyihao 已提交
433
  return 0;
L
fix bug  
liuyao 已提交
434 435 436 437 438
#endif
}

int32_t streamStateGetInfo(SStreamState* pState, void* pKey, int32_t keyLen, void** pVal, int32_t* pLen) {
#ifdef USE_ROCKSDB
dengyihao's avatar
dengyihao 已提交
439 440 441
  int32_t code = 0;
  code = streamDefaultGet_rocksdb(pState, pKey, pVal, pLen);
  return code;
L
fix bug  
liuyao 已提交
442
#else
dengyihao's avatar
dengyihao 已提交
443
  return 0;
L
fix bug  
liuyao 已提交
444 445 446
#endif
}

447
int32_t streamStateAddIfNotExist(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
448
#ifdef USE_ROCKSDB
5
54liuyao 已提交
449
  return streamStateGet(pState, key, pVal, pVLen);
dengyihao's avatar
dengyihao 已提交
450
#else
451 452 453 454 455
  // todo refactor
  int32_t size = *pVLen;
  if (streamStateGet(pState, key, pVal, pVLen) == 0) {
    return 0;
  }
5
54liuyao 已提交
456 457 458
  *pVal = tdbRealloc(NULL, size);
  memset(*pVal, 0, size);
  return 0;
dengyihao's avatar
dengyihao 已提交
459
#endif
460 461 462 463
}

int32_t streamStateReleaseBuf(SStreamState* pState, const SWinKey* key, void* pVal) {
  // todo refactor
dengyihao's avatar
dengyihao 已提交
464
  qDebug("streamStateReleaseBuf");
5
54liuyao 已提交
465 466 467
  if (!pVal) {
    return 0;
  }
dengyihao's avatar
dengyihao 已提交
468 469 470
#ifdef USE_ROCKSDB
  taosMemoryFree(pVal);
#else
471
  streamStateFreeVal(pVal);
dengyihao's avatar
dengyihao 已提交
472
#endif
473 474 475
  return 0;
}

5
54liuyao 已提交
476
SStreamStateCur* streamStateFillGetCur(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
477 478 479
#ifdef USE_ROCKSDB
  return streamStateFillGetCur_rocksdb(pState, key);
#else
5
54liuyao 已提交
480 481
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) return NULL;
5
54liuyao 已提交
482
  tdbTbcOpen(pState->pTdbState->pFillStateDb, &pCur->pCur, NULL);
5
54liuyao 已提交
483

5
54liuyao 已提交
484
  int32_t c = 0;
5
54liuyao 已提交
485 486
  tdbTbcMoveTo(pCur->pCur, key, sizeof(SWinKey), &c);
  if (c != 0) {
5
54liuyao 已提交
487
    streamStateFreeCur(pCur);
5
54liuyao 已提交
488 489 490
    return NULL;
  }
  return pCur;
dengyihao's avatar
dengyihao 已提交
491
#endif
5
54liuyao 已提交
492 493 494
}

SStreamStateCur* streamStateGetAndCheckCur(SStreamState* pState, SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
495 496 497
#ifdef USE_ROCKSDB
  return streamStateGetAndCheckCur_rocksdb(pState, key);
#else
5
54liuyao 已提交
498 499 500 501 502 503
  SStreamStateCur* pCur = streamStateFillGetCur(pState, key);
  if (pCur) {
    int32_t code = streamStateGetGroupKVByCur(pCur, key, NULL, 0);
    if (code == 0) {
      return pCur;
    }
5
54liuyao 已提交
504
    streamStateFreeCur(pCur);
5
54liuyao 已提交
505 506
  }
  return NULL;
dengyihao's avatar
dengyihao 已提交
507
#endif
5
54liuyao 已提交
508 509
}

510
int32_t streamStateGetKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
511 512 513
#ifdef USE_ROCKSDB
  return streamStateGetKVByCur_rocksdb(pCur, pKey, pVal, pVLen);
#else
514 515 516 517
  if (!pCur) {
    return -1;
  }
  const SStateKey* pKTmp = NULL;
Y
yihaoDeng 已提交
518
  int32_t          kLen;
519 520 521 522 523 524 525 526
  if (tdbTbcGet(pCur->pCur, (const void**)&pKTmp, &kLen, pVal, pVLen) < 0) {
    return -1;
  }
  if (pKTmp->opNum != pCur->number) {
    return -1;
  }
  *pKey = pKTmp->key;
  return 0;
dengyihao's avatar
dengyihao 已提交
527
#endif
528 529 530
}

int32_t streamStateFillGetKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
531 532 533
#ifdef USE_ROCKSDB
  return streamStateFillGetKVByCur_rocksdb(pCur, pKey, pVal, pVLen);
#else
534 535 536
  if (!pCur) {
    return -1;
  }
537
  const SWinKey* pKTmp = NULL;
Y
yihaoDeng 已提交
538
  int32_t        kLen;
539 540 541 542 543
  if (tdbTbcGet(pCur->pCur, (const void**)&pKTmp, &kLen, pVal, pVLen) < 0) {
    return -1;
  }
  *pKey = *pKTmp;
  return 0;
dengyihao's avatar
dengyihao 已提交
544
#endif
545 546
}

5
54liuyao 已提交
547
int32_t streamStateGetGroupKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
548 549 550
#ifdef USE_ROCKSDB
  return streamStateGetGroupKVByCur_rocksdb(pCur, pKey, pVal, pVLen);
#else
5
54liuyao 已提交
551 552 553
  if (!pCur) {
    return -1;
  }
5
54liuyao 已提交
554
  uint64_t groupId = pKey->groupId;
Y
yihaoDeng 已提交
555
  int32_t  code = streamStateFillGetKVByCur(pCur, pKey, pVal, pVLen);
5
54liuyao 已提交
556 557 558 559 560 561
  if (code == 0) {
    if (pKey->groupId == groupId) {
      return 0;
    }
  }
  return -1;
dengyihao's avatar
dengyihao 已提交
562
#endif
5
54liuyao 已提交
563 564
}

565
int32_t streamStateGetFirst(SStreamState* pState, SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
566 567 568
#ifdef USE_ROCKSDB
  return streamStateGetFirst_rocksdb(pState, key);
#else
569 570 571 572
  // todo refactor
  SWinKey tmp = {.ts = 0, .groupId = 0};
  streamStatePut(pState, &tmp, NULL, 0);
  SStreamStateCur* pCur = streamStateSeekKeyNext(pState, &tmp);
Y
yihaoDeng 已提交
573
  int32_t          code = streamStateGetKVByCur(pCur, key, NULL, 0);
M
Minglei Jin 已提交
574
  streamStateFreeCur(pCur);
575 576
  streamStateDel(pState, &tmp);
  return code;
dengyihao's avatar
dengyihao 已提交
577
#endif
578 579
}

580
int32_t streamStateSeekFirst(SStreamState* pState, SStreamStateCur* pCur) {
dengyihao's avatar
dengyihao 已提交
581 582 583 584
#ifdef USE_ROCKSDB
  rocksdb_iter_seek_to_first(pCur->iter);
  return 0;
#else
585
  return tdbTbcMoveToFirst(pCur->pCur);
dengyihao's avatar
dengyihao 已提交
586
#endif
587 588 589
}

int32_t streamStateSeekLast(SStreamState* pState, SStreamStateCur* pCur) {
dengyihao's avatar
dengyihao 已提交
590 591 592 593
#ifdef USE_ROCKSDB
  rocksdb_iter_seek_to_last(pCur->iter);
  return 0;
#else
594
  return tdbTbcMoveToLast(pCur->pCur);
dengyihao's avatar
dengyihao 已提交
595
#endif
596 597
}

598
SStreamStateCur* streamStateSeekKeyNext(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
599 600 601
#ifdef USE_ROCKSDB
  return streamStateSeekKeyNext_rocksdb(pState, key);
#else
602 603 604 605 606
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
5
54liuyao 已提交
607
  if (tdbTbcOpen(pState->pTdbState->pStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
608
    streamStateFreeCur(pCur);
609 610 611 612
    return NULL;
  }

  SStateKey sKey = {.key = *key, .opNum = pState->number};
Y
yihaoDeng 已提交
613
  int32_t   c = 0;
614
  if (tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateKey), &c) < 0) {
5
54liuyao 已提交
615
    streamStateFreeCur(pCur);
616 617 618 619 620
    return NULL;
  }
  if (c > 0) return pCur;

  if (tdbTbcMoveToNext(pCur->pCur) < 0) {
5
54liuyao 已提交
621
    streamStateFreeCur(pCur);
622 623 624 625
    return NULL;
  }

  return pCur;
dengyihao's avatar
dengyihao 已提交
626
#endif
627 628
}

5
54liuyao 已提交
629
SStreamStateCur* streamStateFillSeekKeyNext(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
630 631 632
#ifdef USE_ROCKSDB
  return streamStateFillSeekKeyNext_rocksdb(pState, key);
#else
633
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
5
54liuyao 已提交
634
  if (!pCur) {
635 636
    return NULL;
  }
5
54liuyao 已提交
637
  if (tdbTbcOpen(pState->pTdbState->pFillStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
638
    streamStateFreeCur(pCur);
5
54liuyao 已提交
639 640
    return NULL;
  }
641

5
54liuyao 已提交
642
  int32_t c = 0;
643
  if (tdbTbcMoveTo(pCur->pCur, key, sizeof(SWinKey), &c) < 0) {
5
54liuyao 已提交
644
    streamStateFreeCur(pCur);
645 646 647 648 649
    return NULL;
  }
  if (c > 0) return pCur;

  if (tdbTbcMoveToNext(pCur->pCur) < 0) {
5
54liuyao 已提交
650
    streamStateFreeCur(pCur);
651 652 653 654
    return NULL;
  }

  return pCur;
dengyihao's avatar
dengyihao 已提交
655
#endif
656 657
}

5
54liuyao 已提交
658
SStreamStateCur* streamStateFillSeekKeyPrev(SStreamState* pState, const SWinKey* key) {
dengyihao's avatar
dengyihao 已提交
659 660 661
#ifdef USE_ROCKSDB
  return streamStateFillSeekKeyPrev_rocksdb(pState, key);
#else
662 663 664 665
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
5
54liuyao 已提交
666
  if (tdbTbcOpen(pState->pTdbState->pFillStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
667
    streamStateFreeCur(pCur);
5
54liuyao 已提交
668 669
    return NULL;
  }
670

5
54liuyao 已提交
671
  int32_t c = 0;
672
  if (tdbTbcMoveTo(pCur->pCur, key, sizeof(SWinKey), &c) < 0) {
5
54liuyao 已提交
673
    streamStateFreeCur(pCur);
674 675 676 677 678
    return NULL;
  }
  if (c < 0) return pCur;

  if (tdbTbcMoveToPrev(pCur->pCur) < 0) {
5
54liuyao 已提交
679
    streamStateFreeCur(pCur);
680 681 682 683
    return NULL;
  }

  return pCur;
dengyihao's avatar
dengyihao 已提交
684
#endif
685 686 687
}

int32_t streamStateCurNext(SStreamState* pState, SStreamStateCur* pCur) {
dengyihao's avatar
dengyihao 已提交
688 689 690
#ifdef USE_ROCKSDB
  return streamStateCurNext_rocksdb(pState, pCur);
#else
5
54liuyao 已提交
691 692 693
  if (!pCur) {
    return -1;
  }
694 695
  //
  return tdbTbcMoveToNext(pCur->pCur);
dengyihao's avatar
dengyihao 已提交
696
#endif
697 698 699
}

int32_t streamStateCurPrev(SStreamState* pState, SStreamStateCur* pCur) {
dengyihao's avatar
dengyihao 已提交
700 701 702
#ifdef USE_ROCKSDB
  return streamStateCurPrev_rocksdb(pState, pCur);
#else
703 704 705
  if (!pCur) {
    return -1;
  }
706
  return tdbTbcMoveToPrev(pCur->pCur);
dengyihao's avatar
dengyihao 已提交
707
#endif
708
}
709
void streamStateFreeCur(SStreamStateCur* pCur) {
710 711 712
  if (!pCur) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
713
  qDebug("streamStateFreeCur");
dengyihao's avatar
dengyihao 已提交
714
  rocksdb_iter_destroy(pCur->iter);
dengyihao's avatar
dengyihao 已提交
715
  if (pCur->snapshot) rocksdb_release_snapshot(pCur->db, pCur->snapshot);
dengyihao's avatar
dengyihao 已提交
716 717
  rocksdb_readoptions_destroy(pCur->readOpt);

dengyihao's avatar
dengyihao 已提交
718
  tdbTbcClose(pCur->pCur);
719 720 721
  taosMemoryFree(pCur);
}

722
void streamStateFreeVal(void* val) {
dengyihao's avatar
dengyihao 已提交
723 724 725 726 727 728
#ifdef USE_ROCKSDB
  taosMemoryFree(val);
#else
  tdbFree(val);
#endif
}
5
54liuyao 已提交
729 730

int32_t streamStateSessionPut(SStreamState* pState, const SSessionKey* key, const void* value, int32_t vLen) {
dengyihao's avatar
dengyihao 已提交
731 732 733
#ifdef USE_ROCKSDB
  return streamStateSessionPut_rocksdb(pState, key, value, vLen);
#else
5
54liuyao 已提交
734
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
5
54liuyao 已提交
735
  return tdbTbUpsert(pState->pTdbState->pSessionStateDb, &sKey, sizeof(SStateSessionKey), value, vLen,
736
                     pState->pTdbState->txn);
dengyihao's avatar
dengyihao 已提交
737
#endif
5
54liuyao 已提交
738 739 740
}

int32_t streamStateSessionGet(SStreamState* pState, SSessionKey* key, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
741 742 743 744
#ifdef USE_ROCKSDB
  return streamStateSessionGet_rocksdb(pState, key, pVal, pVLen);
#else

745
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentNext(pState, key);
Y
yihaoDeng 已提交
746 747 748
  SSessionKey      resKey = *key;
  void*            tmp = NULL;
  int32_t          code = streamStateSessionGetKVByCur(pCur, &resKey, &tmp, pVLen);
749
  if (code == 0) {
5
54liuyao 已提交
750 751 752 753 754 755 756
    if (key->win.skey != resKey.win.skey) {
      code = -1;
    } else {
      *key = resKey;
      *pVal = tdbRealloc(NULL, *pVLen);
      memcpy(*pVal, tmp, *pVLen);
    }
5
54liuyao 已提交
757 758
  }
  streamStateFreeCur(pCur);
759
  return code;
dengyihao's avatar
dengyihao 已提交
760
#endif
5
54liuyao 已提交
761 762 763
}

int32_t streamStateSessionDel(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
764
#ifdef USE_ROCKSDB
L
liuyao 已提交
765
  qDebug("===stream===delete skey:%" PRId64 ", ekey:%" PRId64 ", groupId:%" PRIu64, key->win.skey,key->win.ekey, key->groupId);
dengyihao's avatar
dengyihao 已提交
766 767
  return streamStateSessionDel_rocksdb(pState, key);
#else
5
54liuyao 已提交
768
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
769
  return tdbTbDelete(pState->pTdbState->pSessionStateDb, &sKey, sizeof(SStateSessionKey), pState->pTdbState->txn);
dengyihao's avatar
dengyihao 已提交
770
#endif
5
54liuyao 已提交
771 772
}

773
SStreamStateCur* streamStateSessionSeekKeyCurrentPrev(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
774 775 776
#ifdef USE_ROCKSDB
  return streamStateSessionSeekKeyCurrentPrev_rocksdb(pState, key);
#else
5
54liuyao 已提交
777 778 779 780 781
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
5
54liuyao 已提交
782
  if (tdbTbcOpen(pState->pTdbState->pSessionStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
783 784 785 786 787
    streamStateFreeCur(pCur);
    return NULL;
  }

  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
Y
yihaoDeng 已提交
788
  int32_t          c = 0;
5
54liuyao 已提交
789 790 791 792
  if (tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateSessionKey), &c) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }
793
  if (c >= 0) return pCur;
5
54liuyao 已提交
794 795 796 797 798 799 800

  if (tdbTbcMoveToPrev(pCur->pCur) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }

  return pCur;
dengyihao's avatar
dengyihao 已提交
801
#endif
5
54liuyao 已提交
802 803
}

804
SStreamStateCur* streamStateSessionSeekKeyCurrentNext(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
805 806 807
#ifdef USE_ROCKSDB
  return streamStateSessionSeekKeyCurrentNext_rocksdb(pState, (SSessionKey*)key);
#else
808 809 810 811 812
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
5
54liuyao 已提交
813
  if (tdbTbcOpen(pState->pTdbState->pSessionStateDb, &pCur->pCur, NULL) < 0) {
814 815 816 817 818
    streamStateFreeCur(pCur);
    return NULL;
  }

  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
Y
yihaoDeng 已提交
819
  int32_t          c = 0;
820 821 822 823 824 825 826 827 828 829 830 831 832
  if (tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateSessionKey), &c) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }

  if (c <= 0) return pCur;

  if (tdbTbcMoveToNext(pCur->pCur) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }

  return pCur;
dengyihao's avatar
dengyihao 已提交
833
#endif
834 835
}

5
54liuyao 已提交
836
SStreamStateCur* streamStateSessionSeekKeyNext(SStreamState* pState, const SSessionKey* key) {
dengyihao's avatar
dengyihao 已提交
837 838 839
#ifdef USE_ROCKSDB
  return streamStateSessionSeekKeyNext_rocksdb(pState, key);
#else
5
54liuyao 已提交
840 841 842 843 844
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
5
54liuyao 已提交
845
  if (tdbTbcOpen(pState->pTdbState->pSessionStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
846 847 848 849 850
    streamStateFreeCur(pCur);
    return NULL;
  }

  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
Y
yihaoDeng 已提交
851
  int32_t          c = 0;
5
54liuyao 已提交
852 853 854 855
  if (tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateSessionKey), &c) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }
856
  if (c < 0) return pCur;
5
54liuyao 已提交
857 858 859 860 861 862 863

  if (tdbTbcMoveToNext(pCur->pCur) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }

  return pCur;
dengyihao's avatar
dengyihao 已提交
864
#endif
5
54liuyao 已提交
865 866
}

867
int32_t streamStateSessionGetKVByCur(SStreamStateCur* pCur, SSessionKey* pKey, void** pVal, int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
868 869 870
#ifdef USE_ROCKSDB
  return streamStateSessionGetKVByCur_rocksdb(pCur, pKey, pVal, pVLen);
#else
5
54liuyao 已提交
871 872 873
  if (!pCur) {
    return -1;
  }
874
  SStateSessionKey* pKTmp = NULL;
Y
yihaoDeng 已提交
875
  int32_t           kLen;
876
  if (tdbTbcGet(pCur->pCur, (const void**)&pKTmp, &kLen, (const void**)pVal, pVLen) < 0) {
5
54liuyao 已提交
877 878 879 880 881 882 883 884 885 886
    return -1;
  }
  if (pKTmp->opNum != pCur->number) {
    return -1;
  }
  if (pKey->groupId != 0 && pKey->groupId != pKTmp->key.groupId) {
    return -1;
  }
  *pKey = pKTmp->key;
  return 0;
dengyihao's avatar
dengyihao 已提交
887
#endif
5
54liuyao 已提交
888 889 890
}

int32_t streamStateSessionClear(SStreamState* pState) {
dengyihao's avatar
dengyihao 已提交
891 892 893
#ifdef USE_ROCKSDB
  return streamStateSessionClear_rocksdb(pState);
#else
Y
yihaoDeng 已提交
894
  SSessionKey      key = {.win.skey = 0, .win.ekey = 0, .groupId = 0};
895
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentNext(pState, &key);
5
54liuyao 已提交
896 897
  while (1) {
    SSessionKey delKey = {0};
Y
yihaoDeng 已提交
898 899 900
    void*       buf = NULL;
    int32_t     size = 0;
    int32_t     code = streamStateSessionGetKVByCur(pCur, &delKey, &buf, &size);
5
54liuyao 已提交
901
    if (code == 0 && size > 0) {
5
54liuyao 已提交
902 903 904 905 906 907 908 909 910
      memset(buf, 0, size);
      streamStateSessionPut(pState, &delKey, buf, size);
    } else {
      break;
    }
    streamStateCurNext(pState, pCur);
  }
  streamStateFreeCur(pCur);
  return 0;
dengyihao's avatar
dengyihao 已提交
911
#endif
5
54liuyao 已提交
912 913
}

914
int32_t streamStateSessionGetKeyByRange(SStreamState* pState, const SSessionKey* key, SSessionKey* curKey) {
dengyihao's avatar
dengyihao 已提交
915 916 917
#ifdef USE_ROCKSDB
  return streamStateSessionGetKeyByRange_rocksdb(pState, key, curKey);
#else
918 919 920 921 922
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return -1;
  }
  pCur->number = pState->number;
5
54liuyao 已提交
923
  if (tdbTbcOpen(pState->pTdbState->pSessionStateDb, &pCur->pCur, NULL) < 0) {
924 925
    streamStateFreeCur(pCur);
    return -1;
5
54liuyao 已提交
926 927
  }

928
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
Y
yihaoDeng 已提交
929
  int32_t          c = 0;
930 931 932 933 934 935
  if (tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateSessionKey), &c) < 0) {
    streamStateFreeCur(pCur);
    return -1;
  }

  SSessionKey resKey = *key;
Y
yihaoDeng 已提交
936
  int32_t     code = streamStateSessionGetKVByCur(pCur, &resKey, NULL, 0);
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957
  if (code == 0 && sessionRangeKeyCmpr(key, &resKey) == 0) {
    *curKey = resKey;
    streamStateFreeCur(pCur);
    return code;
  }

  if (c > 0) {
    streamStateCurNext(pState, pCur);
    code = streamStateSessionGetKVByCur(pCur, &resKey, NULL, 0);
    if (code == 0 && sessionRangeKeyCmpr(key, &resKey) == 0) {
      *curKey = resKey;
      streamStateFreeCur(pCur);
      return code;
    }
  } else if (c < 0) {
    streamStateCurPrev(pState, pCur);
    code = streamStateSessionGetKVByCur(pCur, &resKey, NULL, 0);
    if (code == 0 && sessionRangeKeyCmpr(key, &resKey) == 0) {
      *curKey = resKey;
      streamStateFreeCur(pCur);
      return code;
958 959
    }
  }
960

961
  streamStateFreeCur(pCur);
962
  return -1;
dengyihao's avatar
dengyihao 已提交
963
#endif
964 965
}

966 967
int32_t streamStateSessionAddIfNotExist(SStreamState* pState, SSessionKey* key, TSKEY gap, void** pVal,
                                        int32_t* pVLen) {
dengyihao's avatar
dengyihao 已提交
968 969 970
#ifdef USE_ROCKSDB
  return streamStateSessionAddIfNotExist_rocksdb(pState, key, gap, pVal, pVLen);
#else
5
54liuyao 已提交
971
  // todo refactor
Y
yihaoDeng 已提交
972
  int32_t     res = 0;
973 974 975 976 977
  SSessionKey originKey = *key;
  SSessionKey searchKey = *key;
  searchKey.win.skey = key->win.skey - gap;
  searchKey.win.ekey = key->win.ekey + gap;
  int32_t valSize = *pVLen;
Y
yihaoDeng 已提交
978
  void*   tmp = tdbRealloc(NULL, valSize);
979 980 981 982 983
  if (!tmp) {
    return -1;
  }

  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentPrev(pState, key);
Y
yihaoDeng 已提交
984
  int32_t          code = streamStateSessionGetKVByCur(pCur, key, pVal, pVLen);
985 986 987 988 989 990 991 992 993
  if (code == 0) {
    if (sessionRangeKeyCmpr(&searchKey, key) == 0) {
      memcpy(tmp, *pVal, valSize);
      streamStateSessionDel(pState, key);
      goto _end;
    }
    streamStateCurNext(pState, pCur);
  } else {
    *key = originKey;
5
54liuyao 已提交
994
    streamStateFreeCur(pCur);
995
    pCur = streamStateSessionSeekKeyNext(pState, key);
5
54liuyao 已提交
996
  }
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013

  code = streamStateSessionGetKVByCur(pCur, key, pVal, pVLen);
  if (code == 0) {
    if (sessionRangeKeyCmpr(&searchKey, key) == 0) {
      memcpy(tmp, *pVal, valSize);
      streamStateSessionDel(pState, key);
      goto _end;
    }
  }

  *key = originKey;
  res = 1;
  memset(tmp, 0, valSize);

_end:

  *pVal = tmp;
5
54liuyao 已提交
1014
  streamStateFreeCur(pCur);
1015
  return res;
dengyihao's avatar
dengyihao 已提交
1016 1017

#endif
5
54liuyao 已提交
1018 1019 1020 1021 1022
}

int32_t streamStateStateAddIfNotExist(SStreamState* pState, SSessionKey* key, char* pKeyData, int32_t keyDataLen,
                                      state_key_cmpr_fn fn, void** pVal, int32_t* pVLen) {
  // todo refactor
dengyihao's avatar
dengyihao 已提交
1023 1024 1025 1026

#ifdef USE_ROCKSDB
  return streamStateStateAddIfNotExist_rocksdb(pState, key, pKeyData, keyDataLen, fn, pVal, pVLen);
#else
Y
yihaoDeng 已提交
1027
  int32_t     res = 0;
5
54liuyao 已提交
1028
  SSessionKey tmpKey = *key;
Y
yihaoDeng 已提交
1029 1030
  int32_t     valSize = *pVLen;
  void*       tmp = tdbRealloc(NULL, valSize);
5
54liuyao 已提交
1031 1032 1033 1034
  if (!tmp) {
    return -1;
  }

1035
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentPrev(pState, key);
Y
yihaoDeng 已提交
1036
  int32_t          code = streamStateSessionGetKVByCur(pCur, key, pVal, pVLen);
1037 1038 1039
  if (code == 0) {
    if (key->win.skey <= tmpKey.win.skey && tmpKey.win.ekey <= key->win.ekey) {
      memcpy(tmp, *pVal, valSize);
1040
      streamStateSessionDel(pState, key);
1041 1042
      goto _end;
    }
5
54liuyao 已提交
1043 1044 1045 1046

    void* stateKey = (char*)(*pVal) + (valSize - keyDataLen);
    if (fn(pKeyData, stateKey) == true) {
      memcpy(tmp, *pVal, valSize);
1047
      streamStateSessionDel(pState, key);
5
54liuyao 已提交
1048 1049
      goto _end;
    }
5
54liuyao 已提交
1050 1051 1052 1053 1054 1055

    streamStateCurNext(pState, pCur);
  } else {
    *key = tmpKey;
    streamStateFreeCur(pCur);
    pCur = streamStateSessionSeekKeyNext(pState, key);
5
54liuyao 已提交
1056 1057
  }

1058
  code = streamStateSessionGetKVByCur(pCur, key, pVal, pVLen);
1059
  if (code == 0) {
5
54liuyao 已提交
1060 1061 1062
    void* stateKey = (char*)(*pVal) + (valSize - keyDataLen);
    if (fn(pKeyData, stateKey) == true) {
      memcpy(tmp, *pVal, valSize);
1063
      streamStateSessionDel(pState, key);
5
54liuyao 已提交
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
      goto _end;
    }
  }

  *key = tmpKey;
  res = 1;
  memset(tmp, 0, valSize);

_end:

  *pVal = tmp;
  streamStateFreeCur(pCur);
  return res;
dengyihao's avatar
dengyihao 已提交
1077
#endif
5
54liuyao 已提交
1078
}
5
54liuyao 已提交
1079

1080
int32_t streamStatePutParName(SStreamState* pState, int64_t groupId, const char tbname[TSDB_TABLE_NAME_LEN]) {
L
liuyao 已提交
1081
  qDebug("try to write to cf parname");
dengyihao's avatar
dengyihao 已提交
1082
#ifdef USE_ROCKSDB
L
liuyao 已提交
1083 1084 1085 1086 1087 1088 1089 1090
  if (tSimpleHashGetSize(pState->parNameMap) > MAX_TABLE_NAME_NUM) {
    if (tSimpleHashGet(pState->parNameMap, &groupId, sizeof(int64_t)) == NULL) {
      streamStatePutParName_rocksdb(pState, groupId, tbname);
    }
    return TSDB_CODE_SUCCESS;
  }
  tSimpleHashPut(pState->parNameMap, &groupId, sizeof(int64_t), tbname, TSDB_TABLE_NAME_LEN);
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1091
#else
L
Liu Jicong 已提交
1092 1093
  return tdbTbUpsert(pState->pTdbState->pParNameDb, &groupId, sizeof(int64_t), tbname, TSDB_TABLE_NAME_LEN,
                     pState->pTdbState->txn);
dengyihao's avatar
dengyihao 已提交
1094
#endif
1095 1096 1097
}

int32_t streamStateGetParName(SStreamState* pState, int64_t groupId, void** pVal) {
dengyihao's avatar
dengyihao 已提交
1098
#ifdef USE_ROCKSDB
L
liuyao 已提交
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
  void* pStr = tSimpleHashGet(pState->parNameMap, &groupId, sizeof(int64_t));
  if (!pStr) {
    if (tSimpleHashGetSize(pState->parNameMap) > MAX_TABLE_NAME_NUM) {
      return streamStateGetParName_rocksdb(pState, groupId, pVal);
    }
    return TSDB_CODE_FAILED;
  }
  *pVal = taosMemoryCalloc(1, TSDB_TABLE_NAME_LEN);
  memcpy(*pVal, pStr, TSDB_TABLE_NAME_LEN);
  return TSDB_CODE_SUCCESS;
dengyihao's avatar
dengyihao 已提交
1109
#else
1110
  int32_t len;
5
54liuyao 已提交
1111
  return tdbTbGet(pState->pTdbState->pParNameDb, &groupId, sizeof(int64_t), pVal, &len);
dengyihao's avatar
dengyihao 已提交
1112
#endif
5
54liuyao 已提交
1113 1114
}

dengyihao's avatar
dengyihao 已提交
1115
void streamStateDestroy(SStreamState* pState, bool remove) {
dengyihao's avatar
dengyihao 已提交
1116
#ifdef USE_ROCKSDB
5
54liuyao 已提交
1117
  streamFileStateDestroy(pState->pFileState);
dengyihao's avatar
dengyihao 已提交
1118
  streamStateDestroy_rocksdb(pState, remove);
L
fix bug  
liuyao 已提交
1119
  tSimpleHashCleanup(pState->parNameMap);
dengyihao's avatar
dengyihao 已提交
1120 1121
  // do nothong
#endif
5
54liuyao 已提交
1122 1123
  taosMemoryFreeClear(pState->pTdbState);
  taosMemoryFreeClear(pState);
1124 1125
}

L
liuyao 已提交
1126 1127 1128 1129 1130 1131 1132 1133
int32_t streamStateDeleteCheckPoint(SStreamState* pState, TSKEY mark) {
#ifdef USE_ROCKSDB
  return deleteExpiredCheckPoint(pState->pFileState, mark);
#else
  return 0;
#endif
}

Y
yihaoDeng 已提交
1134
void streamStateReloadInfo(SStreamState* pState, TSKEY ts) { streamFileStateReloadInfo(pState->pFileState, ts); }
L
liuyao 已提交
1135

5
54liuyao 已提交
1136 1137 1138 1139 1140 1141 1142
#if 0
char* streamStateSessionDump(SStreamState* pState) {
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
5
54liuyao 已提交
1143
  if (tdbTbcOpen(pState->pTdbState->pSessionStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
1144 1145 1146 1147 1148 1149
    streamStateFreeCur(pCur);
    return NULL;
  }
  tdbTbcMoveToFirst(pCur->pCur);

  SSessionKey key = {0};
1150 1151 1152
  void*       buf = NULL;
  int32_t     bufSize = 0;
  int32_t     code = streamStateSessionGetKVByCur(pCur, &key, &buf, &bufSize);
5
54liuyao 已提交
1153
  if (code != 0) {
1154
    streamStateFreeCur(pCur);
5
54liuyao 已提交
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168
    return NULL;
  }

  int32_t size = 2048;
  char*   dumpBuf = taosMemoryCalloc(size, 1);
  int64_t len = 0;
  len += snprintf(dumpBuf + len, size - len, "||s:%15" PRId64 ",", key.win.skey);
  len += snprintf(dumpBuf + len, size - len, "e:%15" PRId64 ",", key.win.ekey);
  len += snprintf(dumpBuf + len, size - len, "g:%15" PRId64 "||", key.groupId);
  while (1) {
    tdbTbcMoveToNext(pCur->pCur);
    key = (SSessionKey){0};
    code = streamStateSessionGetKVByCur(pCur, &key, NULL, 0);
    if (code != 0) {
1169
      streamStateFreeCur(pCur);
5
54liuyao 已提交
1170 1171 1172 1173 1174 1175
      return dumpBuf;
    }
    len += snprintf(dumpBuf + len, size - len, "||s:%15" PRId64 ",", key.win.skey);
    len += snprintf(dumpBuf + len, size - len, "e:%15" PRId64 ",", key.win.ekey);
    len += snprintf(dumpBuf + len, size - len, "g:%15" PRId64 "||", key.groupId);
  }
1176
  streamStateFreeCur(pCur);
5
54liuyao 已提交
1177 1178
  return dumpBuf;
}
5
54liuyao 已提交
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221

char* streamStateIntervalDump(SStreamState* pState) {
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
  if (tdbTbcOpen(pState->pTdbState->pStateDb, &pCur->pCur, NULL) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }
  tdbTbcMoveToFirst(pCur->pCur);

  SWinKey key = {0};
  void*       buf = NULL;
  int32_t     bufSize = 0;
  int32_t     code = streamStateGetKVByCur(pCur, &key, (const void **)&buf, &bufSize);
  if (code != 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }

  int32_t size = 2048;
  char*   dumpBuf = taosMemoryCalloc(size, 1);
  int64_t len = 0;
  len += snprintf(dumpBuf + len, size - len, "||s:%15" PRId64 ",", key.ts);
  // len += snprintf(dumpBuf + len, size - len, "e:%15" PRId64 ",", key.win.ekey);
  len += snprintf(dumpBuf + len, size - len, "g:%15" PRId64 "||", key.groupId);
  while (1) {
    tdbTbcMoveToNext(pCur->pCur);
    key = (SWinKey){0};
    code = streamStateGetKVByCur(pCur, &key, NULL, 0);
    if (code != 0) {
      streamStateFreeCur(pCur);
      return dumpBuf;
    }
    len += snprintf(dumpBuf + len, size - len, "||s:%15" PRId64 ",", key.ts);
    // len += snprintf(dumpBuf + len, size - len, "e:%15" PRId64 ",", key.win.ekey);
    len += snprintf(dumpBuf + len, size - len, "g:%15" PRId64 "||", key.groupId);
  }
  streamStateFreeCur(pCur);
  return dumpBuf;
}
5
54liuyao 已提交
1222
#endif