streamState.c 21.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "executor.h"
#include "streamInc.h"
18
#include "tcommon.h"
19 20
#include "ttimer.h"

21 22 23 24 25 26
// todo refactor
typedef struct SStateKey {
  SWinKey key;
  int64_t opNum;
} SStateKey;

5
54liuyao 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
typedef struct SStateSessionKey {
  SSessionKey key;
  int64_t     opNum;
} SStateSessionKey;

static inline int sessionKeyCmpr(const SSessionKey* pWin1, const SSessionKey* pWin2) {
  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;
}

static inline int stateSessionKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2) {
  SStateSessionKey* pWin1 = (SStateSessionKey*)pKey1;
  SStateSessionKey* pWin2 = (SStateSessionKey*)pKey2;

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

  return sessionKeyCmpr(&pWin1->key, &pWin2->key);
}

61
static inline int stateKeyCmpr(const void* pKey1, int kLen1, const void* pKey2, int kLen2) {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  SStateKey* pWin1 = (SStateKey*)pKey1;
  SStateKey* pWin2 = (SStateKey*)pKey2;

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

  if (pWin1->key.ts > pWin2->key.ts) {
    return 1;
  } else if (pWin1->key.ts < pWin2->key.ts) {
    return -1;
  }

  if (pWin1->key.groupId > pWin2->key.groupId) {
    return 1;
  } else if (pWin1->key.groupId < pWin2->key.groupId) {
    return -1;
  }

  return 0;
}

86 87 88
SStreamState* streamStateOpen(char* path, SStreamTask* pTask, bool specPath, int32_t szPage, int32_t pages) {
  szPage = szPage < 0 ? 4096 : szPage;
  pages = pages < 0 ? 256 : pages;
89 90 91 92 93
  SStreamState* pState = taosMemoryCalloc(1, sizeof(SStreamState));
  if (pState == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
L
Liu Jicong 已提交
94

95
  char statePath[300];
L
Liu Jicong 已提交
96 97 98
  if (!specPath) {
    sprintf(statePath, "%s/%d", path, pTask->taskId);
  } else {
99
    memset(statePath, 0, 300);
L
Liu Jicong 已提交
100
    tstrncpy(statePath, path, 300);
L
Liu Jicong 已提交
101
  }
102
  if (tdbOpen(statePath, szPage, pages, &pState->db, 0) < 0) {
103 104 105 106
    goto _err;
  }

  // open state storage backend
107
  if (tdbTbOpen("state.db", sizeof(SStateKey), -1, stateKeyCmpr, pState->db, &pState->pStateDb, 0) < 0) {
108 109 110
    goto _err;
  }

5
54liuyao 已提交
111
  // todo refactor
112
  if (tdbTbOpen("fill.state.db", sizeof(SWinKey), -1, winKeyCmpr, pState->db, &pState->pFillStateDb, 0) < 0) {
5
54liuyao 已提交
113 114 115
    goto _err;
  }

5
54liuyao 已提交
116
  if (tdbTbOpen("session.state.db", sizeof(SStateSessionKey), -1, stateSessionKeyCmpr, pState->db,
H
Hongze Cheng 已提交
117
                &pState->pSessionStateDb, 0) < 0) {
5
54liuyao 已提交
118 119 120
    goto _err;
  }

H
Hongze Cheng 已提交
121
  if (tdbTbOpen("func.state.db", sizeof(STupleKey), -1, STupleKeyCmpr, pState->db, &pState->pFuncStateDb, 0) < 0) {
122 123 124
    goto _err;
  }

125
  if (streamStateBegin(pState) < 0) {
126 127 128 129 130 131 132 133
    goto _err;
  }

  pState->pOwner = pTask;

  return pState;

_err:
134 135
  tdbTbClose(pState->pStateDb);
  tdbTbClose(pState->pFuncStateDb);
5
54liuyao 已提交
136
  tdbTbClose(pState->pFillStateDb);
5
54liuyao 已提交
137
  tdbTbClose(pState->pSessionStateDb);
138
  tdbClose(pState->db);
139 140 141 142 143 144 145
  taosMemoryFree(pState);
  return NULL;
}

void streamStateClose(SStreamState* pState) {
  tdbCommit(pState->db, &pState->txn);
  tdbTbClose(pState->pStateDb);
146
  tdbTbClose(pState->pFuncStateDb);
5
54liuyao 已提交
147
  tdbTbClose(pState->pFillStateDb);
5
54liuyao 已提交
148
  tdbTbClose(pState->pSessionStateDb);
149 150 151 152 153 154 155 156 157 158 159 160
  tdbClose(pState->db);

  taosMemoryFree(pState);
}

int32_t streamStateBegin(SStreamState* pState) {
  if (tdbTxnOpen(&pState->txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) <
      0) {
    return -1;
  }

  if (tdbBegin(pState->db, &pState->txn) < 0) {
161
    tdbTxnClose(&pState->txn);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    return -1;
  }
  return 0;
}

int32_t streamStateCommit(SStreamState* pState) {
  if (tdbCommit(pState->db, &pState->txn) < 0) {
    return -1;
  }
  memset(&pState->txn, 0, sizeof(TXN));
  if (tdbTxnOpen(&pState->txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) <
      0) {
    return -1;
  }
  if (tdbBegin(pState->db, &pState->txn) < 0) {
    return -1;
  }
  return 0;
}

int32_t streamStateAbort(SStreamState* pState) {
  if (tdbAbort(pState->db, &pState->txn) < 0) {
    return -1;
  }
  memset(&pState->txn, 0, sizeof(TXN));
  if (tdbTxnOpen(&pState->txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) <
      0) {
    return -1;
  }
  if (tdbBegin(pState->db, &pState->txn) < 0) {
    return -1;
  }
  return 0;
}

197 198 199 200 201 202 203 204 205 206 207
int32_t streamStateFuncPut(SStreamState* pState, const STupleKey* key, const void* value, int32_t vLen) {
  return tdbTbUpsert(pState->pFuncStateDb, key, sizeof(STupleKey), value, vLen, &pState->txn);
}
int32_t streamStateFuncGet(SStreamState* pState, const STupleKey* key, void** pVal, int32_t* pVLen) {
  return tdbTbGet(pState->pFuncStateDb, key, sizeof(STupleKey), pVal, pVLen);
}

int32_t streamStateFuncDel(SStreamState* pState, const STupleKey* key) {
  return tdbTbDelete(pState->pFuncStateDb, key, sizeof(STupleKey), &pState->txn);
}

208
// todo refactor
209
int32_t streamStatePut(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen) {
210 211
  SStateKey sKey = {.key = *key, .opNum = pState->number};
  return tdbTbUpsert(pState->pStateDb, &sKey, sizeof(SStateKey), value, vLen, &pState->txn);
212
}
5
54liuyao 已提交
213 214 215 216 217 218

// todo refactor
int32_t streamStateFillPut(SStreamState* pState, const SWinKey* key, const void* value, int32_t vLen) {
  return tdbTbUpsert(pState->pFillStateDb, key, sizeof(SWinKey), value, vLen, &pState->txn);
}

219
// todo refactor
220
int32_t streamStateGet(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
221 222
  SStateKey sKey = {.key = *key, .opNum = pState->number};
  return tdbTbGet(pState->pStateDb, &sKey, sizeof(SStateKey), pVal, pVLen);
223 224
}

5
54liuyao 已提交
225 226 227 228 229
// todo refactor
int32_t streamStateFillGet(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
  return tdbTbGet(pState->pFillStateDb, key, sizeof(SWinKey), pVal, pVLen);
}

230
// todo refactor
231
int32_t streamStateDel(SStreamState* pState, const SWinKey* key) {
232 233
  SStateKey sKey = {.key = *key, .opNum = pState->number};
  return tdbTbDelete(pState->pStateDb, &sKey, sizeof(SStateKey), &pState->txn);
234 235
}

236 237 238 239 240 241 242
int32_t streamStateClear(SStreamState* pState) {
  SWinKey key = {.ts = 0, .groupId = 0};
  streamStatePut(pState, &key, NULL, 0);
  while (1) {
    SStreamStateCur* pCur = streamStateSeekKeyNext(pState, &key);
    SWinKey          delKey = {0};
    int32_t          code = streamStateGetKVByCur(pCur, &delKey, NULL, 0);
5
54liuyao 已提交
243
    streamStateFreeCur(pCur);
244 245 246 247 248 249 250 251 252 253 254
    if (code == 0) {
      streamStateDel(pState, &delKey);
    } else {
      break;
    }
  }
  return 0;
}

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

5
54liuyao 已提交
255 256 257 258 259
// todo refactor
int32_t streamStateFillDel(SStreamState* pState, const SWinKey* key) {
  return tdbTbDelete(pState->pFillStateDb, key, sizeof(SWinKey), &pState->txn);
}

260 261 262 263 264 265
int32_t streamStateAddIfNotExist(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen) {
  // todo refactor
  int32_t size = *pVLen;
  if (streamStateGet(pState, key, pVal, pVLen) == 0) {
    return 0;
  }
5
54liuyao 已提交
266 267 268
  *pVal = tdbRealloc(NULL, size);
  memset(*pVal, 0, size);
  return 0;
269 270 271 272
}

int32_t streamStateReleaseBuf(SStreamState* pState, const SWinKey* key, void* pVal) {
  // todo refactor
5
54liuyao 已提交
273 274 275
  if (!pVal) {
    return 0;
  }
276 277 278 279
  streamFreeVal(pVal);
  return 0;
}

280
SStreamStateCur* streamStateGetCur(SStreamState* pState, const SWinKey* key) {
281 282 283 284
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) return NULL;
  tdbTbcOpen(pState->pStateDb, &pCur->pCur, NULL);

5
54liuyao 已提交
285
  int32_t   c = 0;
286
  SStateKey sKey = {.key = *key, .opNum = pState->number};
5
54liuyao 已提交
287
  tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateKey), &c);
288
  if (c != 0) {
5
54liuyao 已提交
289
    streamStateFreeCur(pCur);
290 291
    return NULL;
  }
292
  pCur->number = pState->number;
293
  return pCur;
294 295
}

5
54liuyao 已提交
296 297 298 299 300
SStreamStateCur* streamStateFillGetCur(SStreamState* pState, const SWinKey* key) {
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) return NULL;
  tdbTbcOpen(pState->pFillStateDb, &pCur->pCur, NULL);

5
54liuyao 已提交
301
  int32_t c = 0;
5
54liuyao 已提交
302 303
  tdbTbcMoveTo(pCur->pCur, key, sizeof(SWinKey), &c);
  if (c != 0) {
5
54liuyao 已提交
304
    streamStateFreeCur(pCur);
5
54liuyao 已提交
305 306 307 308 309 310 311 312 313 314 315 316
    return NULL;
  }
  return pCur;
}

SStreamStateCur* streamStateGetAndCheckCur(SStreamState* pState, SWinKey* key) {
  SStreamStateCur* pCur = streamStateFillGetCur(pState, key);
  if (pCur) {
    int32_t code = streamStateGetGroupKVByCur(pCur, key, NULL, 0);
    if (code == 0) {
      return pCur;
    }
5
54liuyao 已提交
317
    streamStateFreeCur(pCur);
5
54liuyao 已提交
318 319 320 321
  }
  return NULL;
}

322
int32_t streamStateGetKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  if (!pCur) {
    return -1;
  }
  const SStateKey* pKTmp = NULL;
  int32_t          kLen;
  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;
}

int32_t streamStateFillGetKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
  if (!pCur) {
    return -1;
  }
342 343 344 345 346 347 348
  const SWinKey* pKTmp = NULL;
  int32_t        kLen;
  if (tdbTbcGet(pCur->pCur, (const void**)&pKTmp, &kLen, pVal, pVLen) < 0) {
    return -1;
  }
  *pKey = *pKTmp;
  return 0;
349 350
}

5
54liuyao 已提交
351
int32_t streamStateGetGroupKVByCur(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) {
5
54liuyao 已提交
352 353 354
  if (!pCur) {
    return -1;
  }
5
54liuyao 已提交
355
  uint64_t groupId = pKey->groupId;
356
  int32_t  code = streamStateFillGetKVByCur(pCur, pKey, pVal, pVLen);
5
54liuyao 已提交
357 358 359 360 361 362 363 364
  if (code == 0) {
    if (pKey->groupId == groupId) {
      return 0;
    }
  }
  return -1;
}

365 366 367 368 369 370 371 372 373 374
int32_t streamStateGetFirst(SStreamState* pState, SWinKey* key) {
  // todo refactor
  SWinKey tmp = {.ts = 0, .groupId = 0};
  streamStatePut(pState, &tmp, NULL, 0);
  SStreamStateCur* pCur = streamStateSeekKeyNext(pState, &tmp);
  int32_t          code = streamStateGetKVByCur(pCur, key, NULL, 0);
  streamStateDel(pState, &tmp);
  return code;
}

375 376 377 378 379 380 381 382 383 384
int32_t streamStateSeekFirst(SStreamState* pState, SStreamStateCur* pCur) {
  //
  return tdbTbcMoveToFirst(pCur->pCur);
}

int32_t streamStateSeekLast(SStreamState* pState, SStreamStateCur* pCur) {
  //
  return tdbTbcMoveToLast(pCur->pCur);
}

385 386 387 388 389 390 391
SStreamStateCur* streamStateSeekKeyNext(SStreamState* pState, const SWinKey* key) {
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
  if (tdbTbcOpen(pState->pStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
392
    streamStateFreeCur(pCur);
393 394 395 396
    return NULL;
  }

  SStateKey sKey = {.key = *key, .opNum = pState->number};
5
54liuyao 已提交
397
  int32_t   c = 0;
398 399
  if (tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateKey), &c) < 0) {
    tdbTbcClose(pCur->pCur);
5
54liuyao 已提交
400
    streamStateFreeCur(pCur);
401 402 403 404 405
    return NULL;
  }
  if (c > 0) return pCur;

  if (tdbTbcMoveToNext(pCur->pCur) < 0) {
5
54liuyao 已提交
406
    streamStateFreeCur(pCur);
407 408 409 410 411 412
    return NULL;
  }

  return pCur;
}

5
54liuyao 已提交
413
SStreamStateCur* streamStateFillSeekKeyNext(SStreamState* pState, const SWinKey* key) {
414
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
5
54liuyao 已提交
415
  if (!pCur) {
416 417
    return NULL;
  }
5
54liuyao 已提交
418
  if (tdbTbcOpen(pState->pFillStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
419
    streamStateFreeCur(pCur);
5
54liuyao 已提交
420 421
    return NULL;
  }
422

5
54liuyao 已提交
423
  int32_t c = 0;
424
  if (tdbTbcMoveTo(pCur->pCur, key, sizeof(SWinKey), &c) < 0) {
5
54liuyao 已提交
425
    tdbTbcClose(pCur->pCur);
5
54liuyao 已提交
426
    streamStateFreeCur(pCur);
427 428 429 430 431
    return NULL;
  }
  if (c > 0) return pCur;

  if (tdbTbcMoveToNext(pCur->pCur) < 0) {
5
54liuyao 已提交
432
    streamStateFreeCur(pCur);
433 434 435 436 437 438
    return NULL;
  }

  return pCur;
}

5
54liuyao 已提交
439
SStreamStateCur* streamStateFillSeekKeyPrev(SStreamState* pState, const SWinKey* key) {
440 441 442 443
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
5
54liuyao 已提交
444
  if (tdbTbcOpen(pState->pFillStateDb, &pCur->pCur, NULL) < 0) {
5
54liuyao 已提交
445
    streamStateFreeCur(pCur);
5
54liuyao 已提交
446 447
    return NULL;
  }
448

5
54liuyao 已提交
449
  int32_t c = 0;
450
  if (tdbTbcMoveTo(pCur->pCur, key, sizeof(SWinKey), &c) < 0) {
5
54liuyao 已提交
451
    tdbTbcClose(pCur->pCur);
5
54liuyao 已提交
452
    streamStateFreeCur(pCur);
453 454 455 456 457
    return NULL;
  }
  if (c < 0) return pCur;

  if (tdbTbcMoveToPrev(pCur->pCur) < 0) {
5
54liuyao 已提交
458
    streamStateFreeCur(pCur);
459 460 461 462 463 464 465
    return NULL;
  }

  return pCur;
}

int32_t streamStateCurNext(SStreamState* pState, SStreamStateCur* pCur) {
5
54liuyao 已提交
466 467 468
  if (!pCur) {
    return -1;
  }
469 470 471 472 473 474
  //
  return tdbTbcMoveToNext(pCur->pCur);
}

int32_t streamStateCurPrev(SStreamState* pState, SStreamStateCur* pCur) {
  //
475 476 477
  if (!pCur) {
    return -1;
  }
478 479
  return tdbTbcMoveToPrev(pCur->pCur);
}
480
void streamStateFreeCur(SStreamStateCur* pCur) {
481 482 483
  if (!pCur) {
    return;
  }
484 485 486 487 488
  tdbTbcClose(pCur->pCur);
  taosMemoryFree(pCur);
}

void streamFreeVal(void* val) { tdbFree(val); }
5
54liuyao 已提交
489 490 491 492 493 494 495 496 497 498 499

int32_t streamStateSessionPut(SStreamState* pState, const SSessionKey* key, const void* value, int32_t vLen) {
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  return tdbTbUpsert(pState->pSessionStateDb, &sKey, sizeof(SStateSessionKey), value, vLen, &pState->txn);
}

SStreamStateCur* streamStateSessionGetRanomCur(SStreamState* pState, const SSessionKey* key) {
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) return NULL;
  tdbTbcOpen(pState->pSessionStateDb, &pCur->pCur, NULL);

5
54liuyao 已提交
500
  int32_t          c = -2;
5
54liuyao 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateSessionKey), &c);
  if (c != 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }
  pCur->number = pState->number;
  return pCur;
}

int32_t streamStateSessionGet(SStreamState* pState, SSessionKey* key, void** pVal, int32_t* pVLen) {
  SStreamStateCur* pCur = streamStateSessionGetRanomCur(pState, key);
  void*            tmp = NULL;
  if (streamStateSessionGetKVByCur(pCur, key, (const void**)&tmp, pVLen) == 0) {
    *pVal = tdbRealloc(NULL, *pVLen);
    memcpy(*pVal, tmp, *pVLen);
    streamStateFreeCur(pCur);
    return 0;
  }
  streamStateFreeCur(pCur);
  return -1;
}

int32_t streamStateSessionDel(SStreamState* pState, const SSessionKey* key) {
  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  return tdbTbDelete(pState->pSessionStateDb, &sKey, sizeof(SStateSessionKey), &pState->txn);
}

529
SStreamStateCur* streamStateSessionSeekKeyCurrentPrev(SStreamState* pState, const SSessionKey* key) {
5
54liuyao 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
  if (tdbTbcOpen(pState->pSessionStateDb, &pCur->pCur, NULL) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }

  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  int32_t          c = 0;
  if (tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateSessionKey), &c) < 0) {
    tdbTbcClose(pCur->pCur);
    streamStateFreeCur(pCur);
    return NULL;
  }
547
  if (c >= 0) return pCur;
5
54liuyao 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574

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

  return pCur;
}

SStreamStateCur* streamStateSessionSeekKeyNext(SStreamState* pState, const SSessionKey* key) {
  SStreamStateCur* pCur = taosMemoryCalloc(1, sizeof(SStreamStateCur));
  if (pCur == NULL) {
    return NULL;
  }
  pCur->number = pState->number;
  if (tdbTbcOpen(pState->pSessionStateDb, &pCur->pCur, NULL) < 0) {
    streamStateFreeCur(pCur);
    return NULL;
  }

  SStateSessionKey sKey = {.key = *key, .opNum = pState->number};
  int32_t          c = 0;
  if (tdbTbcMoveTo(pCur->pCur, &sKey, sizeof(SStateSessionKey), &c) < 0) {
    tdbTbcClose(pCur->pCur);
    streamStateFreeCur(pCur);
    return NULL;
  }
575
  if (c < 0) return pCur;
5
54liuyao 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632

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

  return pCur;
}

int32_t streamStateSessionGetKVByCur(SStreamStateCur* pCur, SSessionKey* pKey, const void** pVal, int32_t* pVLen) {
  if (!pCur) {
    return -1;
  }
  const SStateSessionKey* pKTmp = NULL;
  int32_t                 kLen;
  if (tdbTbcGet(pCur->pCur, (const void**)&pKTmp, &kLen, pVal, pVLen) < 0) {
    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;
}

int32_t streamStateSessionClear(SStreamState* pState) {
  SSessionKey key = {.win.skey = 0, .win.ekey = 0, .groupId = 0};
  streamStateSessionPut(pState, &key, NULL, 0);
  SStreamStateCur* pCur = streamStateSessionSeekKeyNext(pState, &key);
  while (1) {
    SSessionKey delKey = {0};
    void*       buf = NULL;
    int32_t     size = 0;
    int32_t     code = streamStateSessionGetKVByCur(pCur, &delKey, buf, &size);
    if (code == 0) {
      memset(buf, 0, size);
      streamStateSessionPut(pState, &delKey, buf, size);
    } else {
      break;
    }
    streamStateCurNext(pState, pCur);
  }
  streamStateFreeCur(pCur);
  streamStateSessionDel(pState, &key);
  return 0;
}

SStreamStateCur* streamStateSessionGetCur(SStreamState* pState, const SSessionKey* key) {
  SStreamStateCur* pCur = streamStateSessionGetRanomCur(pState, key);
  SSessionKey      resKey = *key;
  while (1) {
    streamStateCurPrev(pState, pCur);
    SSessionKey tmpKey = *key;
    int32_t     code = streamStateSessionGetKVByCur(pCur, &tmpKey, NULL, 0);
633
    if (code == 0 && sessionKeyCmpr(key, &tmpKey) == 0) {
5
54liuyao 已提交
634 635 636 637 638 639 640 641 642
      resKey = tmpKey;
    } else {
      break;
    }
  }
  streamStateFreeCur(pCur);
  return streamStateSessionGetRanomCur(pState, &resKey);
}

643 644 645 646 647 648 649 650 651 652
int32_t streamStateSessionGetKey(SStreamState* pState, const SSessionKey* key, SSessionKey* curKey) {
  SStreamStateCur* pCur = streamStateSessionGetRanomCur(pState, key);
  SSessionKey      resKey = *key;
  int32_t          res = -1;
  while (1) {
    SSessionKey tmpKey = *key;
    int32_t     code = streamStateSessionGetKVByCur(pCur, &tmpKey, NULL, 0);
    if (code == 0 && sessionKeyCmpr(key, &tmpKey) == 0) {
      res = 0;
      resKey = tmpKey;
5
54liuyao 已提交
653
      streamStateCurPrev(pState, pCur);
654 655 656 657 658
    } else {
      break;
    }
  }
  *curKey = resKey;
659
  streamStateFreeCur(pCur);
660 661 662
  return res;
}

5
54liuyao 已提交
663 664
int32_t streamStateSessionAddIfNotExist(SStreamState* pState, SSessionKey* key, void** pVal, int32_t* pVLen) {
  // todo refactor
665
  SStreamStateCur* pCur = streamStateSessionGetRanomCur(pState, key);
5
54liuyao 已提交
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
  int32_t          size = *pVLen;
  void*            tmp = NULL;
  *pVal = tdbRealloc(NULL, size);
  memset(*pVal, 0, size);
  if (streamStateSessionGetKVByCur(pCur, key, (const void**)&tmp, pVLen) == 0) {
    memcpy(*pVal, tmp, *pVLen);
    streamStateFreeCur(pCur);
    return 0;
  }
  streamStateFreeCur(pCur);
  return 1;
}

int32_t streamStateStateAddIfNotExist(SStreamState* pState, SSessionKey* key, char* pKeyData, int32_t keyDataLen,
                                      state_key_cmpr_fn fn, void** pVal, int32_t* pVLen) {
  // todo refactor
682
  int32_t     res = 0;
5
54liuyao 已提交
683 684 685 686 687 688 689
  SSessionKey tmpKey = *key;
  int32_t     valSize = *pVLen;
  void*       tmp = tdbRealloc(NULL, valSize);
  if (!tmp) {
    return -1;
  }

690
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentPrev(pState, key);
5
54liuyao 已提交
691
  int32_t          code = streamStateSessionGetKVByCur(pCur, key, (const void**)pVal, pVLen);
692 693 694 695 696
  if (code == 0) {
    if (key->win.skey <= tmpKey.win.skey && tmpKey.win.ekey <= key->win.ekey) {
      memcpy(tmp, *pVal, valSize);
      goto _end;
    }
5
54liuyao 已提交
697 698 699 700 701 702

    void* stateKey = (char*)(*pVal) + (valSize - keyDataLen);
    if (fn(pKeyData, stateKey) == true) {
      memcpy(tmp, *pVal, valSize);
      goto _end;
    }
5
54liuyao 已提交
703 704 705 706 707 708

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

  code = streamStateSessionGetKVByCur(pCur, key, (const void**)pVal, pVLen);
712
  if (code == 0) {
5
54liuyao 已提交
713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
    void* stateKey = (char*)(*pVal) + (valSize - keyDataLen);
    if (fn(pKeyData, stateKey) == true) {
      memcpy(tmp, *pVal, valSize);
      goto _end;
    }
  }

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

_end:

  *pVal = tmp;
  streamStateFreeCur(pCur);
  return res;
}
5
54liuyao 已提交
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769

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

  SSessionKey key = {0};
  int32_t     code = streamStateSessionGetKVByCur(pCur, &key, NULL, 0);
  if (code != 0) {
    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) {
      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);
  }
  return dumpBuf;
}
#endif