• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

69.51
/source/libs/stream/src/streamSessionState.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tstreamFileState.h"
17

18
#include "query.h"
19
#include "streamBackendRocksdb.h"
20
#include "tcommon.h"
21
#include "tsimplehash.h"
22

23
int sessionStateKeyCompare(const void* pWin1, const void* pDatas, int pos) {
18,368✔
24
  SRowBuffPos* pPos2 = taosArrayGetP(pDatas, pos);
18,368✔
25
  SSessionKey* pWin2 = (SSessionKey*)pPos2->pKey;
18,368✔
26
  return sessionWinKeyCmpr((SSessionKey*)pWin1, pWin2);
18,368✔
27
}
28

29
int sessionStateRangeKeyCompare(const SSessionKey* pWin1, const void* pDatas, int pos) {
728✔
30
  SRowBuffPos* pPos2 = taosArrayGetP(pDatas, pos);
728✔
31
  SSessionKey* pWin2 = (SSessionKey*)pPos2->pKey;
728✔
32
  return sessionRangeKeyCmpr(pWin1, pWin2);
728✔
33
}
34

35
int32_t binarySearch(void* keyList, int num, const void* key, __session_compare_fn_t cmpFn) {
16,796✔
36
  int firstPos = 0, lastPos = num - 1, midPos = -1;
16,796✔
37
  int numOfRows = 0;
16,796✔
38

39
  if (num <= 0) return -1;
16,796✔
40
  // find the first position which is smaller or equal than the key.
41
  // if all data is bigger than the key return -1
42
  while (1) {
43
    if (cmpFn(key, keyList, lastPos) >= 0) return lastPos;
16,834✔
44
    if (cmpFn(key, keyList, firstPos) == 0) return firstPos;
4,857✔
45
    if (cmpFn(key, keyList, firstPos) < 0) return firstPos - 1;
4,059✔
46

47
    numOfRows = lastPos - firstPos + 1;
1,511✔
48
    midPos = (numOfRows >> 1) + firstPos;
1,511✔
49

50
    if (cmpFn(key, keyList, midPos) < 0) {
1,511✔
51
      lastPos = midPos - 1;
788✔
52
    } else if (cmpFn(key, keyList, midPos) > 0) {
723✔
53
      firstPos = midPos + 1;
267✔
54
    } else {
55
      break;
456✔
56
    }
57
  }
58

59
  return midPos;
456✔
60
}
61

62
int64_t getSessionWindowEndkey(void* data, int32_t index) {
×
63
  SArray*       pWinInfos = (SArray*)data;
×
64
  SRowBuffPos** ppos = taosArrayGet(pWinInfos, index);
×
65
  if (ppos != NULL) {
×
66
    SSessionKey* pWin = (SSessionKey*)((*ppos)->pKey);
×
67
    return pWin->win.ekey;
×
68
  } else {
69
    return 0;
×
70
  }
71
}
72

73
bool inSessionWindow(SSessionKey* pKey, TSKEY ts, int64_t gap) {
3,710✔
74
  if (ts + gap >= pKey->win.skey && ts - gap <= pKey->win.ekey) {
3,710✔
75
    return true;
1,270✔
76
  }
77
  return false;
2,440✔
78
}
79

80
SStreamStateCur* createStateCursor(SStreamFileState* pFileState) {
7,645✔
81
  SStreamStateCur* pCur = createStreamStateCursor();
7,645✔
82
  if (pCur == NULL) {
7,645!
83
    return NULL;
×
84
  }
85
  pCur->pStreamFileState = pFileState;
7,645✔
86
  return pCur;
7,645✔
87
}
88

89
static int32_t addNewSessionWindow(SStreamFileState* pFileState, SArray* pWinInfos, const SSessionKey* pKey,
1,594✔
90
                                   SRowBuffPos** ppPos) {
91
  int32_t      code = TSDB_CODE_SUCCESS;
1,594✔
92
  int32_t      lino = 0;
1,594✔
93
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
1,594✔
94
  if (!pNewPos || !pNewPos->pRowBuff) {
1,594!
95
    code = TSDB_CODE_OUT_OF_MEMORY;
×
96
    QUERY_CHECK_CODE(code, lino, _end);
×
97
  }
98

99
  memcpy(pNewPos->pKey, pKey, sizeof(SSessionKey));
1,594✔
100
  void* tmp = taosArrayPush(pWinInfos, &pNewPos);
1,594✔
101
  if (!tmp) {
1,594!
102
    code = terrno;
×
103
    QUERY_CHECK_CODE(code, lino, _end);
×
104
  }
105
  (*ppPos) = pNewPos;
1,594✔
106

107
_end:
1,594✔
108
  if (code != TSDB_CODE_SUCCESS) {
1,594!
109
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
110
  }
111
  return code;
1,594✔
112
}
113

114
static int32_t insertNewSessionWindow(SStreamFileState* pFileState, SArray* pWinInfos, const SSessionKey* pKey,
87✔
115
                                      int32_t index, SRowBuffPos** ppPos) {
116
  int32_t      code = TSDB_CODE_SUCCESS;
87✔
117
  int32_t      lino = 0;
87✔
118
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
87✔
119
  if (!pNewPos || !pNewPos->pRowBuff) {
87!
120
    code = TSDB_CODE_OUT_OF_MEMORY;
×
121
    QUERY_CHECK_CODE(code, lino, _end);
×
122
  }
123

124
  memcpy(pNewPos->pKey, pKey, sizeof(SSessionKey));
87✔
125
  void* tmp = taosArrayInsert(pWinInfos, index, &pNewPos);
87✔
126
  if (!tmp) {
87!
127
    code = terrno;
×
128
    QUERY_CHECK_CODE(code, lino, _end);
×
129
  }
130

131
  *ppPos = pNewPos;
87✔
132

133
_end:
87✔
134
  if (code != TSDB_CODE_SUCCESS) {
87!
135
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
136
  }
137
  return code;
87✔
138
}
139

140
SRowBuffPos* createSessionWinBuff(SStreamFileState* pFileState, SSessionKey* pKey, void* p, int32_t* pVLen) {
272✔
141
  int32_t      code = TSDB_CODE_SUCCESS;
272✔
142
  int32_t      lino = 0;
272✔
143
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
272✔
144
  if (!pNewPos || !pNewPos->pRowBuff) {
272!
145
    code = TSDB_CODE_OUT_OF_MEMORY;
×
146
    QUERY_CHECK_CODE(code, lino, _end);
×
147
  }
148

149
  memcpy(pNewPos->pKey, pKey, sizeof(SSessionKey));
272✔
150
  pNewPos->needFree = true;
272✔
151
  pNewPos->beFlushed = true;
272✔
152
  if (p) {
272✔
153
    memcpy(pNewPos->pRowBuff, p, *pVLen);
271✔
154
  } else {
155
    int32_t len = getRowStateRowSize(pFileState);
1✔
156
    memset(pNewPos->pRowBuff, 0, len);
1✔
157
  }
158

159
_end:
272✔
160
  taosMemoryFree(p);
272!
161
  if (code != TSDB_CODE_SUCCESS) {
272!
162
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
163
    return NULL;
×
164
  }
165
  return pNewPos;
272✔
166
}
167

168
int32_t getSessionWinResultBuff(SStreamFileState* pFileState, SSessionKey* pKey, TSKEY gap, void** pVal, int32_t* pVLen,
1,763✔
169
                                int32_t* pWinCode) {
170
  int32_t code = TSDB_CODE_SUCCESS;
1,763✔
171
  int32_t lino = 0;
1,763✔
172
  (*pWinCode) = TSDB_CODE_SUCCESS;
1,763✔
173
  SSHashObj* pSessionBuff = getRowStateBuff(pFileState);
1,763✔
174
  SArray*    pWinStates = NULL;
1,763✔
175
  void**     ppBuff = tSimpleHashGet(pSessionBuff, &pKey->groupId, sizeof(uint64_t));
1,763✔
176
  if (ppBuff) {
1,763✔
177
    pWinStates = (SArray*)(*ppBuff);
1,398✔
178
  } else {
179
    pWinStates = taosArrayInit(16, POINTER_BYTES);
365✔
180
    if (!pWinStates) {
365!
181
      code = terrno;
×
182
      QUERY_CHECK_CODE(code, lino, _end);
×
183
    }
184
    code = tSimpleHashPut(pSessionBuff, &pKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
365✔
185
    QUERY_CHECK_CODE(code, lino, _end);
365!
186
  }
187

188
  TSKEY startTs = pKey->win.skey;
1,763✔
189
  TSKEY endTs = pKey->win.ekey;
1,763✔
190

191
  int32_t size = taosArrayGetSize(pWinStates);
1,763✔
192
  if (size == 0) {
1,763✔
193
    void*   pFileStore = getStateFileStore(pFileState);
466✔
194
    void*   p = NULL;
466✔
195
    int32_t code_file = streamStateSessionAddIfNotExist_rocksdb(pFileStore, pKey, gap, &p, pVLen);
466✔
196
    if (code_file == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
466✔
197
      (*pVal) = createSessionWinBuff(pFileState, pKey, p, pVLen);
17✔
198
      if (!(*pVal)) {
17!
199
        code = TSDB_CODE_OUT_OF_MEMORY;
×
200
        QUERY_CHECK_CODE(code, lino, _end);
×
201
      }
202

203
      (*pWinCode) = code_file;
17✔
204
      qDebug("===stream===0 get session win:%" PRId64 ",%" PRId64 " from disc, res %d", startTs, endTs, code_file);
17!
205
    } else {
206
      code = addNewSessionWindow(pFileState, pWinStates, pKey, (SRowBuffPos**)pVal);
449✔
207
      (*pWinCode) = TSDB_CODE_FAILED;
449✔
208
      taosMemoryFree(p);
449!
209
      QUERY_CHECK_CODE(code, lino, _end);
449!
210
    }
211
    goto _end;
466✔
212
  }
213

214
  // find the first position which is smaller than the pKey
215
  int32_t      index = binarySearch(pWinStates, size, pKey, sessionStateKeyCompare);
1,297✔
216
  SRowBuffPos* pPos = NULL;
1,297✔
217

218
  if (index >= 0) {
1,297✔
219
    pPos = taosArrayGetP(pWinStates, index);
1,056✔
220
    if (inSessionWindow(pPos->pKey, startTs, gap)) {
1,056✔
221
      (*pVal) = pPos;
592✔
222
      SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
592✔
223
      pPos->beUsed = true;
592✔
224
      pPos->beFlushed = false;
592✔
225
      *pKey = *pDestWinKey;
592✔
226
      goto _end;
592✔
227
    }
228
  }
229

230
  if (index + 1 < size) {
705✔
231
    pPos = taosArrayGetP(pWinStates, index + 1);
302✔
232
    if (inSessionWindow(pPos->pKey, startTs, gap) || (endTs != INT64_MIN && inSessionWindow(pPos->pKey, endTs, gap))) {
302!
233
      (*pVal) = pPos;
91✔
234
      SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
91✔
235
      pPos->beUsed = true;
91✔
236
      pPos->beFlushed = false;
91✔
237
      *pKey = *pDestWinKey;
91✔
238
      goto _end;
91✔
239
    }
240
  }
241

242
  if (index + 1 == 0) {
614✔
243
    if (!isDeteled(pFileState, endTs) && isFlushedState(pFileState, endTs, gap)) {
189!
244
      void*   p = NULL;
162✔
245
      void*   pFileStore = getStateFileStore(pFileState);
162✔
246
      int32_t code_file = streamStateSessionAddIfNotExist_rocksdb(pFileStore, pKey, gap, &p, pVLen);
162✔
247
      if (code_file == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
162!
248
        (*pVal) = createSessionWinBuff(pFileState, pKey, p, pVLen);
162✔
249
        if (!(*pVal)) {
162!
250
          code = TSDB_CODE_OUT_OF_MEMORY;
×
251
          QUERY_CHECK_CODE(code, lino, _end);
162!
252
        }
253

254
        (*pWinCode) = code_file;
162✔
255
        qDebug("===stream===1 get session win:%" PRId64 ",%" PRId64 " from disc, res %d", startTs, endTs, code_file);
162!
256
        goto _end;
162✔
257
      } else {
258
        taosMemoryFree(p);
×
259
      }
260
    }
261
  }
262

263
  if (index == size - 1) {
452✔
264
    code = addNewSessionWindow(pFileState, pWinStates, pKey, (SRowBuffPos**)pVal);
403✔
265
    QUERY_CHECK_CODE(code, lino, _end);
403!
266

267
    (*pWinCode) = TSDB_CODE_FAILED;
403✔
268
    goto _end;
403✔
269
  }
270

271
  code = insertNewSessionWindow(pFileState, pWinStates, pKey, index + 1, (SRowBuffPos**)pVal);
49✔
272
  QUERY_CHECK_CODE(code, lino, _end);
49!
273

274
  (*pWinCode) = TSDB_CODE_FAILED;
49✔
275

276
_end:
1,763✔
277
  if (code != TSDB_CODE_SUCCESS) {
1,763!
278
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
279
  }
280
  return code;
1,763✔
281
}
282

283
int32_t getSessionRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen,
225✔
284
                          int32_t* pWinCode) {
285
  SWinKey*    pTmpkey = pKey;
225✔
286
  SSessionKey pWinKey = {.groupId = pTmpkey->groupId, .win.skey = pTmpkey->ts, .win.ekey = pTmpkey->ts};
225✔
287
  return getSessionWinResultBuff(pFileState, &pWinKey, 0, pVal, pVLen, pWinCode);
225✔
288
}
289

290
int32_t putSessionWinResultBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
21✔
291
  int32_t      code = TSDB_CODE_SUCCESS;
21✔
292
  int32_t      lino = 0;
21✔
293
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
21✔
294
  SSessionKey* pKey = pPos->pKey;
21✔
295
  SArray*      pWinStates = NULL;
21✔
296
  void**       ppBuff = tSimpleHashGet(pSessionBuff, &pKey->groupId, sizeof(uint64_t));
21✔
297
  if (ppBuff) {
21✔
298
    pWinStates = (SArray*)(*ppBuff);
19✔
299
  } else {
300
    pWinStates = taosArrayInit(16, POINTER_BYTES);
2✔
301
    if (!pWinStates) {
2!
302
      code = terrno;
×
303
      QUERY_CHECK_CODE(code, lino, _end);
×
304
    }
305

306
    code = tSimpleHashPut(pSessionBuff, &pKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
2✔
307
    QUERY_CHECK_CODE(code, lino, _end);
2!
308
  }
309

310
  int32_t size = taosArrayGetSize(pWinStates);
21✔
311
  if (size == 0) {
21✔
312
    void* tmp = taosArrayPush(pWinStates, &pPos);
17✔
313
    if (!tmp) {
17!
314
      code = terrno;
×
315
      QUERY_CHECK_CODE(code, lino, _end);
×
316
    }
317
    goto _end;
17✔
318
  }
319

320
  // find the first position which is smaller than the pKey
321
  int32_t index = binarySearch(pWinStates, size, pKey, sessionStateKeyCompare);
4✔
322
  if (index >= 0) {
4!
323
    void* tmp = taosArrayInsert(pWinStates, index, &pPos);
×
324
    if (!tmp) {
×
325
      code = terrno;
×
326
      QUERY_CHECK_CODE(code, lino, _end);
×
327
    }
328
  } else {
329
    void* tmp = taosArrayInsert(pWinStates, 0, &pPos);
4✔
330
    if (!tmp) {
4!
331
      code = terrno;
×
332
      QUERY_CHECK_CODE(code, lino, _end);
×
333
    }
334
  }
335

336
_end:
4✔
337
  pPos->needFree = false;
21✔
338
  if (code != TSDB_CODE_SUCCESS) {
21!
339
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
340
  }
341
  return code;
21✔
342
}
343

344
int32_t getSessionFlushedBuff(SStreamFileState* pFileState, SSessionKey* pKey, void** pVal, int32_t* pVLen,
106✔
345
                              int32_t* pWinCode) {
346
  int32_t      code = TSDB_CODE_SUCCESS;
106✔
347
  int32_t      lino = 0;
106✔
348
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
106✔
349
  if (!pNewPos || !pNewPos->pRowBuff) {
106!
350
    code = TSDB_CODE_OUT_OF_MEMORY;
×
351
    QUERY_CHECK_CODE(code, lino, _end);
×
352
  }
353
  pNewPos->needFree = true;
106✔
354
  pNewPos->beFlushed = true;
106✔
355
  void* pBuff = NULL;
106✔
356
  (*pWinCode) = streamStateSessionGet_rocksdb(getStateFileStore(pFileState), pKey, &pBuff, pVLen);
106✔
357
  if ((*pWinCode) != TSDB_CODE_SUCCESS) {
106!
358
    goto _end;
×
359
  }
360
  memcpy(pNewPos->pKey, pKey, sizeof(SSessionKey));
106✔
361
  memcpy(pNewPos->pRowBuff, pBuff, *pVLen);
106✔
362
  taosMemoryFreeClear(pBuff);
106!
363
  (*pVal) = pNewPos;
106✔
364

365
_end:
106✔
366
  if (code != TSDB_CODE_SUCCESS) {
106!
367
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
368
  }
369
  return code;
106✔
370
}
371

372
int32_t deleteSessionWinStateBuffFn(void* pBuff, const void* key, size_t keyLen) {
994✔
373
  SSHashObj*   pSessionBuff = (SSHashObj*)pBuff;
994✔
374
  SSessionKey* pWinKey = (SSessionKey*)key;
994✔
375
  void**       ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
994✔
376
  if (!ppBuff) {
994✔
377
    return TSDB_CODE_SUCCESS;
177✔
378
  }
379
  SArray* pWinStates = (SArray*)(*ppBuff);
817✔
380
  int32_t size = taosArrayGetSize(pWinStates);
817✔
381
  TSKEY   gap = 0;
817✔
382
  int32_t index = binarySearch(pWinStates, size, pWinKey, sessionStateKeyCompare);
817✔
383
  if (index >= 0) {
817✔
384
    SRowBuffPos* pPos = taosArrayGetP(pWinStates, index);
548✔
385
    if (inSessionWindow(pPos->pKey, pWinKey->win.skey, gap)) {
548!
386
      pPos->beFlushed = true;
548✔
387
      taosArrayRemove(pWinStates, index);
548✔
388
    }
389
  }
390
  return TSDB_CODE_SUCCESS;
817✔
391
}
392

393
void deleteSessionWinStateBuffByPosFn(SStreamFileState* pFileState, SRowBuffPos* pPos) {
373✔
394
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
373✔
395
  SSessionKey* pWinKey = (SSessionKey*)pPos->pKey;
373✔
396
  void**       ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
373✔
397
  if (!ppBuff) {
373!
398
    return;
×
399
  }
400
  SArray* pWinStates = (SArray*)(*ppBuff);
373✔
401
  int32_t size = taosArrayGetSize(pWinStates);
373✔
402
  TSKEY   gap = 0;
373✔
403
  int32_t index = binarySearch(pWinStates, size, pWinKey, sessionStateKeyCompare);
373✔
404
  if (index >= 0) {
373✔
405
    SRowBuffPos* pItemPos = taosArrayGetP(pWinStates, index);
87✔
406
    if (pItemPos == pPos) {
87✔
407
      taosArrayRemove(pWinStates, index);
78✔
408
    }
409
  }
410
}
411

412
int32_t allocSessioncWinBuffByNextPosition(SStreamFileState* pFileState, SStreamStateCur* pCur,
51✔
413
                                           const SSessionKey* pWinKey, void** ppVal, int32_t* pVLen) {
414
  int32_t      code = TSDB_CODE_SUCCESS;
51✔
415
  int32_t      lino = 0;
51✔
416
  SRowBuffPos* pNewPos = NULL;
51✔
417
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
51✔
418
  void**       ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
51✔
419
  SArray*      pWinStates = NULL;
51✔
420
  if (!ppBuff) {
51✔
421
    pWinStates = taosArrayInit(16, POINTER_BYTES);
22✔
422
    if (!pWinStates) {
22!
423
      code = terrno;
×
424
      QUERY_CHECK_CODE(code, lino, _end);
×
425
    }
426

427
    code = tSimpleHashPut(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
22✔
428
    QUERY_CHECK_CODE(code, lino, _end);
22!
429
  } else {
430
    pWinStates = (SArray*)(*ppBuff);
29✔
431
  }
432
  if (!pCur) {
51✔
433
    code = addNewSessionWindow(pFileState, pWinStates, pWinKey, &pNewPos);
24✔
434
    QUERY_CHECK_CODE(code, lino, _end);
24!
435

436
    goto _end;
24✔
437
  }
438

439
  int32_t size = taosArrayGetSize(pWinStates);
27✔
440
  if (pCur->buffIndex >= 0) {
27✔
441
    if (pCur->buffIndex >= size) {
25✔
442
      code = addNewSessionWindow(pFileState, pWinStates, pWinKey, &pNewPos);
18✔
443
      QUERY_CHECK_CODE(code, lino, _end);
18!
444

445
      goto _end;
18✔
446
    }
447
    code = insertNewSessionWindow(pFileState, pWinStates, pWinKey, pCur->buffIndex, &pNewPos);
7✔
448
    QUERY_CHECK_CODE(code, lino, _end);
7!
449

450
    goto _end;
7✔
451
  } else {
452
    if (size > 0) {
2!
453
      SRowBuffPos* pPos = taosArrayGetP(pWinStates, 0);
×
454
      if (sessionWinKeyCmpr(pWinKey, pPos->pKey) >= 0) {
×
455
        // pCur is invalid
456
        SSessionKey pTmpKey = *pWinKey;
×
457
        int32_t     winCode = TSDB_CODE_SUCCESS;
×
458
        code = getSessionWinResultBuff(pFileState, &pTmpKey, 0, (void**)&pNewPos, pVLen, &winCode);
×
459
        QUERY_CHECK_CONDITION((winCode == TSDB_CODE_FAILED), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
460
        QUERY_CHECK_CODE(code, lino, _end);
×
461
        goto _end;
×
462
      }
463
    }
464
    pNewPos = getNewRowPosForWrite(pFileState);
2✔
465
    if (!pNewPos || !pNewPos->pRowBuff) {
2!
466
      code = TSDB_CODE_OUT_OF_MEMORY;
×
467
      QUERY_CHECK_CODE(code, lino, _end);
×
468
    }
469

470
    memcpy(pNewPos->pKey, pWinKey, sizeof(SSessionKey));
2✔
471
    pNewPos->needFree = true;
2✔
472
    pNewPos->beFlushed = true;
2✔
473
  }
474

475
_end:
51✔
476
  (*ppVal) = pNewPos;
51✔
477
  if (code != TSDB_CODE_SUCCESS) {
51!
478
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
479
  }
480
  return code;
51✔
481
}
482

483
void sessionWinStateClear(SStreamFileState* pFileState) {
172✔
484
  int32_t buffSize = getRowStateRowSize(pFileState);
172✔
485
  void*   pIte = NULL;
172✔
486
  size_t  keyLen = 0;
172✔
487
  int32_t iter = 0;
172✔
488
  void*   pBuff = getRowStateBuff(pFileState);
172✔
489
  while ((pIte = tSimpleHashIterate(pBuff, pIte, &iter)) != NULL) {
377✔
490
    SArray* pWinStates = *((void**)pIte);
205✔
491
    int32_t size = taosArrayGetSize(pWinStates);
205✔
492
    for (int32_t i = 0; i < size; i++) {
426✔
493
      SRowBuffPos* pPos = taosArrayGetP(pWinStates, i);
221✔
494
      memset(pPos->pRowBuff, 0, buffSize);
221✔
495
    }
496
  }
497
}
172✔
498

499
void sessionWinStateCleanup(void* pBuff) {
4,781✔
500
  void*   pIte = NULL;
4,781✔
501
  size_t  keyLen = 0;
4,781✔
502
  int32_t iter = 0;
4,781✔
503
  while ((pIte = tSimpleHashIterate(pBuff, pIte, &iter)) != NULL) {
5,698✔
504
    SArray* pWinStates = (SArray*)(*(void**)pIte);
917✔
505
    taosArrayDestroy(pWinStates);
917✔
506
  }
507
  tSimpleHashCleanup(pBuff);
4,781✔
508
}
4,781✔
509

510
static SStreamStateCur* seekKeyCurrentPrev_buff(SStreamFileState* pFileState, const SSessionKey* pWinKey,
6,800✔
511
                                                SArray** pWins, int32_t* pIndex) {
512
  SStreamStateCur* pCur = NULL;
6,800✔
513
  SSHashObj*       pSessionBuff = getRowStateBuff(pFileState);
6,800✔
514
  void**           ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
6,800✔
515
  if (!ppBuff) {
6,800✔
516
    return NULL;
346✔
517
  }
518

519
  SArray* pWinStates = (SArray*)(*ppBuff);
6,454✔
520
  int32_t size = taosArrayGetSize(pWinStates);
6,454✔
521
  TSKEY   gap = 0;
6,454✔
522
  int32_t index = binarySearch(pWinStates, size, pWinKey, sessionStateKeyCompare);
6,454✔
523

524
  if (pWins) {
6,454✔
525
    (*pWins) = pWinStates;
3,946✔
526
  }
527

528
  if (size > 0 && index == -1) {
6,454✔
529
    SRowBuffPos* pPos = taosArrayGetP(pWinStates, 0);
1,173✔
530
    SSessionKey* pWin = (SSessionKey*)pPos->pKey;
1,173✔
531
    if (pWinKey->win.skey == pWin->win.skey) {
1,173✔
532
      index = 0;
177✔
533
    }
534
  }
535

536
  if (index >= 0) {
6,454✔
537
    pCur = createStateCursor(pFileState);
4,768✔
538
    if (pCur == NULL) {
4,768!
539
      return NULL;
×
540
    }
541
    pCur->buffIndex = index;
4,768✔
542
    if (pIndex) {
4,768✔
543
      *pIndex = index;
3,174✔
544
    }
545
  }
546

547
  return pCur;
6,454✔
548
}
549

550
SStreamStateCur* sessionWinStateSeekKeyCurrentPrev(SStreamFileState* pFileState, const SSessionKey* pWinKey) {
2,613✔
551
  SStreamStateCur* pCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, NULL, NULL);
2,613✔
552
  if (pCur) {
2,613✔
553
    return pCur;
1,594✔
554
  }
555

556
  void* pFileStore = getStateFileStore(pFileState);
1,019✔
557
  pCur = streamStateSessionSeekKeyCurrentPrev_rocksdb(pFileStore, pWinKey);
1,019✔
558
  if (!pCur) {
1,019✔
559
    return NULL;
458✔
560
  }
561
  pCur->buffIndex = -1;
561✔
562
  pCur->pStreamFileState = pFileState;
561✔
563
  return pCur;
561✔
564
}
565
static void transformCursor(SStreamFileState* pFileState, SStreamStateCur* pCur) {
292✔
566
  if (!pCur) {
292!
567
    return;
×
568
  }
569
  streamStateResetCur(pCur);
292✔
570
  pCur->buffIndex = 0;
292✔
571
  pCur->pStreamFileState = pFileState;
292✔
572
}
573

574
static void checkAndTransformCursor(SStreamFileState* pFileState, const uint64_t groupId, SArray* pWinStates,
1,006✔
575
                                    SStreamStateCur** ppCur) {
576
  SSessionKey key = {.groupId = groupId};
1,006✔
577
  int32_t     code = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), *ppCur, &key, NULL, NULL);
1,006✔
578
  if (taosArrayGetSize(pWinStates) > 0 &&
1,006✔
579
      (code == TSDB_CODE_FAILED || sessionStateKeyCompare(&key, pWinStates, 0) >= 0)) {
124!
580
    if (!(*ppCur)) {
290✔
581
      (*ppCur) = createStateCursor(pFileState);
289✔
582
    }
583
    transformCursor(pFileState, *ppCur);
290✔
584
  } else if (*ppCur) {
716✔
585
    (*ppCur)->buffIndex = -1;
305✔
586
    (*ppCur)->pStreamFileState = pFileState;
305✔
587
  }
588
}
1,006✔
589

590
SStreamStateCur* sessionWinStateSeekKeyCurrentNext(SStreamFileState* pFileState, const SSessionKey* pWinKey) {
150✔
591
  SArray*          pWinStates = NULL;
150✔
592
  int32_t          index = -1;
150✔
593
  SStreamStateCur* pCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, &pWinStates, &index);
150✔
594
  if (pCur) {
150✔
595
    if (sessionStateRangeKeyCompare(pWinKey, pWinStates, index) > 0) {
39✔
596
      sessionWinStateMoveToNext(pCur);
10✔
597
    }
598
    return pCur;
39✔
599
  }
600

601
  void* pFileStore = getStateFileStore(pFileState);
111✔
602
  pCur = streamStateSessionSeekKeyCurrentNext_rocksdb(pFileStore, (SSessionKey*)pWinKey);
111✔
603
  checkAndTransformCursor(pFileState, pWinKey->groupId, pWinStates, &pCur);
111✔
604
  return pCur;
111✔
605
}
606

607
SStreamStateCur* sessionWinStateSeekKeyNext(SStreamFileState* pFileState, const SSessionKey* pWinKey) {
3,976✔
608
  SArray*          pWinStates = NULL;
3,976✔
609
  int32_t          index = -1;
3,976✔
610
  SStreamStateCur* pCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, &pWinStates, &index);
3,976✔
611
  if (pCur) {
3,976✔
612
    sessionWinStateMoveToNext(pCur);
3,081✔
613
    return pCur;
3,081✔
614
  }
615

616
  void* pFileStore = getStateFileStore(pFileState);
895✔
617
  pCur = streamStateSessionSeekKeyNext_rocksdb(pFileStore, pWinKey);
895✔
618
  checkAndTransformCursor(pFileState, pWinKey->groupId, pWinStates, &pCur);
895✔
619
  return pCur;
895✔
620
}
621

622
SStreamStateCur* countWinStateSeekKeyPrev(SStreamFileState* pFileState, const SSessionKey* pWinKey, COUNT_TYPE count) {
61✔
623
  SArray*          pWinStates = NULL;
61✔
624
  int32_t          index = -1;
61✔
625
  SStreamStateCur* pBuffCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, &pWinStates, &index);
61✔
626
  int32_t          resSize = getRowStateRowSize(pFileState);
61✔
627
  COUNT_TYPE       winCount = 0;
61✔
628
  if (pBuffCur) {
61✔
629
    while (index >= 0) {
123✔
630
      SRowBuffPos* pPos = taosArrayGetP(pWinStates, index);
101✔
631
      winCount = *((COUNT_TYPE*)((char*)pPos->pRowBuff + (resSize - sizeof(COUNT_TYPE))));
101✔
632
      if (sessionStateRangeKeyCompare(pWinKey, pWinStates, index) == 0 || winCount < count) {
101✔
633
        index--;
69✔
634
      } else if (index >= 0) {
32!
635
        pBuffCur->buffIndex = index + 1;
32✔
636
        return pBuffCur;
32✔
637
      }
638
    }
639
    pBuffCur->buffIndex = 0;
22✔
640
  } else if (taosArrayGetSize(pWinStates) > 0) {
7!
641
    pBuffCur = createStateCursor(pFileState);
×
642
    if (pBuffCur == NULL) {
×
643
      return NULL;
×
644
    }
645
    pBuffCur->buffIndex = 0;
×
646
  }
647

648
  void*            pFileStore = getStateFileStore(pFileState);
29✔
649
  SStreamStateCur* pCur = streamStateSessionSeekKeyPrev_rocksdb(pFileStore, pWinKey);
29✔
650
  if (pCur) {
29!
651
    pCur->pStreamFileState = pFileState;
×
652
    SSessionKey key = {0};
×
653
    void*       pVal = NULL;
×
654
    int         len = 0;
×
655
    int32_t     code = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, &key, &pVal, &len);
×
656
    if (code == TSDB_CODE_FAILED) {
×
657
      streamStateFreeCur(pCur);
×
658
      return pBuffCur;
×
659
    }
660
    winCount = *((COUNT_TYPE*)((char*)pVal + (resSize - sizeof(COUNT_TYPE))));
×
661
    taosMemoryFreeClear(pVal);
×
662
    streamStateFreeCur(pBuffCur);
×
663
    if (sessionRangeKeyCmpr(pWinKey, &key) != 0 && winCount == count) {
×
664
      streamStateCurNext(pFileStore, pCur);
×
665
      return pCur;
×
666
    }
667
    streamStateCurPrev(pFileStore, pCur);
×
668
    while (1) {
669
      code = streamStateSessionGetKVByCur_rocksdb(NULL, pCur, &key, &pVal, &len);
×
670
      if (code == TSDB_CODE_FAILED) {
×
671
        streamStateCurNext(pFileStore, pCur);
×
672
        return pCur;
×
673
      }
674
      winCount = *((COUNT_TYPE*)((char*)pVal + (resSize - sizeof(COUNT_TYPE))));
×
675
      taosMemoryFreeClear(pVal);
×
676
      if (sessionRangeKeyCmpr(pWinKey, &key) == 0 || winCount < count) {
×
677
        streamStateCurPrev(pFileStore, pCur);
×
678
      } else {
679
        streamStateCurNext(pFileStore, pCur);
×
680
        return pCur;
×
681
      }
682
    }
683
  }
684
  return pBuffCur;
29✔
685
}
686

687
int32_t sessionWinStateGetKVByCur(SStreamStateCur* pCur, SSessionKey* pKey, void** pVal, int32_t* pVLen) {
7,381✔
688
  if (!pCur) {
7,381✔
689
    return TSDB_CODE_FAILED;
900✔
690
  }
691
  int32_t code = TSDB_CODE_SUCCESS;
6,481✔
692

693
  SSHashObj* pSessionBuff = getRowStateBuff(pCur->pStreamFileState);
6,481✔
694
  void**     ppBuff = tSimpleHashGet(pSessionBuff, &pKey->groupId, sizeof(uint64_t));
6,481✔
695
  SArray*    pWinStates = NULL;
6,481✔
696
  if (ppBuff) {
6,481✔
697
    pWinStates = (SArray*)(*ppBuff);
6,334✔
698
  }
699

700
  if (pCur->buffIndex >= 0) {
6,481✔
701
    int32_t size = taosArrayGetSize(pWinStates);
5,561✔
702
    if (pCur->buffIndex >= size) {
5,561✔
703
      return TSDB_CODE_FAILED;
3,151✔
704
    }
705
    SRowBuffPos* pPos = taosArrayGetP(pWinStates, pCur->buffIndex);
2,410✔
706
    if (pVal) {
2,410✔
707
      *pVal = pPos;
464✔
708
    }
709
    *pKey = *(SSessionKey*)(pPos->pKey);
2,410✔
710
  } else {
711
    void* pData = NULL;
920✔
712
    code = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pCur->pStreamFileState), pCur, pKey, &pData, pVLen);
920✔
713
    if (taosArrayGetSize(pWinStates) > 0 &&
920✔
714
        (code == TSDB_CODE_FAILED || sessionStateRangeKeyCompare(pKey, pWinStates, 0) >= 0)) {
588!
715
      transformCursor(pCur->pStreamFileState, pCur);
2✔
716
      SRowBuffPos* pPos = taosArrayGetP(pWinStates, pCur->buffIndex);
2✔
717
      if (pVal) {
2!
718
        *pVal = pPos;
×
719
      }
720
      *pKey = *(SSessionKey*)(pPos->pKey);
2✔
721
      code = TSDB_CODE_SUCCESS;
2✔
722
    } else if (code == TSDB_CODE_SUCCESS && pVal) {
918✔
723
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pCur->pStreamFileState);
126✔
724
      if (!pNewPos || !pNewPos->pRowBuff) {
126!
725
        code = TSDB_CODE_OUT_OF_MEMORY;
×
726
        taosMemoryFreeClear(pData);
×
727
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
728
        return code;
×
729
      }
730
      memcpy(pNewPos->pKey, pKey, sizeof(SSessionKey));
126✔
731
      pNewPos->needFree = true;
126✔
732
      pNewPos->beFlushed = true;
126✔
733
      memcpy(pNewPos->pRowBuff, pData, *pVLen);
126✔
734
      (*pVal) = pNewPos;
126✔
735
    }
736
    taosMemoryFreeClear(pData);
920!
737
  }
738
  return code;
3,330✔
739
}
740

741
void sessionWinStateMoveToNext(SStreamStateCur* pCur) {
6,172✔
742
  qTrace("move cursor to next");
6,172!
743
  if (pCur && pCur->buffIndex >= 0) {
6,172✔
744
    pCur->buffIndex++;
3,602✔
745
  } else {
746
    streamStateCurNext_rocksdb(pCur);
2,570✔
747
  }
748
}
6,172✔
749

750
int32_t sessionWinStateGetKeyByRange(SStreamFileState* pFileState, const SSessionKey* key, SSessionKey* curKey,
2,520✔
751
                                     range_cmpr_fn cmpFn) {
752
  SStreamStateCur* pCur = sessionWinStateSeekKeyCurrentPrev(pFileState, key);
2,520✔
753
  SSessionKey      tmpKey = *key;
2,520✔
754
  int32_t          code = sessionWinStateGetKVByCur(pCur, &tmpKey, NULL, NULL);
2,520✔
755
  bool             hasCurrentPrev = true;
2,520✔
756
  if (code == TSDB_CODE_FAILED) {
2,520✔
757
    streamStateFreeCur(pCur);
481✔
758
    pCur = sessionWinStateSeekKeyNext(pFileState, key);
481✔
759
    code = sessionWinStateGetKVByCur(pCur, &tmpKey, NULL, NULL);
481✔
760
    hasCurrentPrev = false;
481✔
761
  }
762

763
  if (code == TSDB_CODE_FAILED) {
2,520✔
764
    code = TSDB_CODE_FAILED;
322✔
765
    goto _end;
322✔
766
  }
767

768
  if (cmpFn(key, &tmpKey) == 0) {
2,198✔
769
    *curKey = tmpKey;
1,658✔
770
    goto _end;
1,658✔
771
  } else if (!hasCurrentPrev) {
540✔
772
    code = TSDB_CODE_FAILED;
136✔
773
    goto _end;
136✔
774
  }
775

776
  sessionWinStateMoveToNext(pCur);
404✔
777
  code = sessionWinStateGetKVByCur(pCur, &tmpKey, NULL, NULL);
404✔
778
  if (code == TSDB_CODE_SUCCESS && cmpFn(key, &tmpKey) == 0) {
404✔
779
    *curKey = tmpKey;
169✔
780
  } else {
781
    code = TSDB_CODE_FAILED;
235✔
782
  }
783

784
_end:
2,520✔
785
  streamStateFreeCur(pCur);
2,520✔
786
  return code;
2,520✔
787
}
788

789
int32_t getStateWinResultBuff(SStreamFileState* pFileState, SSessionKey* key, char* pKeyData, int32_t keyDataLen,
1,746✔
790
                              state_key_cmpr_fn fn, void** pVal, int32_t* pVLen, int32_t* pWinCode) {
791
  int32_t code = TSDB_CODE_SUCCESS;
1,746✔
792
  int32_t lino = 0;
1,746✔
793
  (*pWinCode) = TSDB_CODE_SUCCESS;
1,746✔
794

795
  SSessionKey* pWinKey = key;
1,746✔
796
  TSKEY        gap = 0;
1,746✔
797
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
1,746✔
798
  SArray*      pWinStates = NULL;
1,747✔
799

800
  void** ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
1,747✔
801
  if (ppBuff) {
1,747✔
802
    pWinStates = (SArray*)(*ppBuff);
1,539✔
803
  } else {
804
    pWinStates = taosArrayInit(16, POINTER_BYTES);
208✔
805
    if (!pWinStates) {
208!
806
      code = terrno;
×
807
      QUERY_CHECK_CODE(code, lino, _end);
×
808
    }
809

810
    code = tSimpleHashPut(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
208✔
811
    QUERY_CHECK_CODE(code, lino, _end);
208!
812
  }
813

814
  TSKEY startTs = pWinKey->win.skey;
1,747✔
815
  TSKEY endTs = pWinKey->win.ekey;
1,747✔
816

817
  int32_t size = taosArrayGetSize(pWinStates);
1,747✔
818
  if (size == 0) {
1,747✔
819
    void*   pFileStore = getStateFileStore(pFileState);
354✔
820
    void*   p = NULL;
354✔
821
    int32_t code_file = streamStateStateAddIfNotExist_rocksdb(pFileStore, pWinKey, pKeyData, keyDataLen, fn, &p, pVLen);
354✔
822
    if (code_file == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
354✔
823
      (*pVal) = createSessionWinBuff(pFileState, pWinKey, p, pVLen);
12✔
824
      if (!(*pVal)) {
12!
825
        code = TSDB_CODE_OUT_OF_MEMORY;
×
826
        QUERY_CHECK_CODE(code, lino, _end);
×
827
      }
828

829
      (*pWinCode) = code_file;
12✔
830
      qDebug("===stream===0 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
12!
831
             pWinKey->win.ekey, code_file);
832
    } else {
833
      code = addNewSessionWindow(pFileState, pWinStates, key, (SRowBuffPos**)pVal);
342✔
834
      (*pWinCode) = TSDB_CODE_FAILED;
342✔
835
      taosMemoryFree(p);
342!
836
      QUERY_CHECK_CODE(code, lino, _end);
342!
837
    }
838
    goto _end;
354✔
839
  }
840

841
  // find the first position which is smaller than the pWinKey
842
  int32_t      index = binarySearch(pWinStates, size, pWinKey, sessionStateKeyCompare);
1,393✔
843
  SRowBuffPos* pPos = NULL;
1,393✔
844
  int32_t      valSize = *pVLen;
1,393✔
845

846
  if (index >= 0) {
1,393✔
847
    pPos = taosArrayGetP(pWinStates, index);
1,302✔
848
    void* stateKey = (char*)(pPos->pRowBuff) + (valSize - keyDataLen);
1,302✔
849
    if (inSessionWindow(pPos->pKey, startTs, gap) || fn(pKeyData, stateKey) == true) {
1,302✔
850
      (*pVal) = pPos;
1,004✔
851
      SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
1,004✔
852
      pPos->beUsed = true;
1,004✔
853
      pPos->beFlushed = false;
1,004✔
854
      *key = *pDestWinKey;
1,004✔
855
      goto _end;
1,004✔
856
    }
857
  }
858

859
  if (index + 1 < size) {
389✔
860
    pPos = taosArrayGetP(pWinStates, index + 1);
115✔
861
    void* stateKey = (char*)(pPos->pRowBuff) + (valSize - keyDataLen);
115✔
862
    if (inSessionWindow(pPos->pKey, startTs, gap) || (endTs != INT64_MIN && inSessionWindow(pPos->pKey, endTs, gap)) ||
228!
863
        fn(pKeyData, stateKey) == true) {
113✔
864
      (*pVal) = pPos;
4✔
865
      SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
4✔
866
      pPos->beUsed = true;
4✔
867
      pPos->beFlushed = false;
4✔
868
      *key = *pDestWinKey;
4✔
869
      goto _end;
4✔
870
    }
871
  }
872

873
  if (index + 1 == 0) {
385✔
874
    if (!isDeteled(pFileState, endTs)) {
89!
875
      void*   p = NULL;
89✔
876
      void*   pFileStore = getStateFileStore(pFileState);
89✔
877
      int32_t code_file =
878
          streamStateStateAddIfNotExist_rocksdb(pFileStore, pWinKey, pKeyData, keyDataLen, fn, &p, pVLen);
89✔
879
      if (code_file == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
89✔
880
        (*pVal) = createSessionWinBuff(pFileState, pWinKey, p, pVLen);
80✔
881
        if (!(*pVal)) {
80!
882
          code = TSDB_CODE_OUT_OF_MEMORY;
×
883
          QUERY_CHECK_CODE(code, lino, _end);
80!
884
        }
885

886
        (*pWinCode) = code_file;
80✔
887
        qDebug("===stream===1 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
80!
888
               pWinKey->win.ekey, code_file);
889
        goto _end;
80✔
890
      } else {
891
        taosMemoryFree(p);
9!
892
      }
893
    }
894
  }
895

896
  if (index == size - 1) {
305✔
897
    code = addNewSessionWindow(pFileState, pWinStates, key, (SRowBuffPos**)pVal);
274✔
898
    QUERY_CHECK_CODE(code, lino, _end);
274!
899

900
    (*pWinCode) = TSDB_CODE_FAILED;
274✔
901
    goto _end;
274✔
902
  }
903
  code = insertNewSessionWindow(pFileState, pWinStates, key, index + 1, (SRowBuffPos**)pVal);
31✔
904
  QUERY_CHECK_CODE(code, lino, _end);
31!
905

906
  (*pWinCode) = TSDB_CODE_FAILED;
31✔
907

908
_end:
1,747✔
909
  if (code != TSDB_CODE_SUCCESS) {
1,747!
910
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
911
  }
912
  return code;
1,747✔
913
}
914

915
int32_t getCountWinStateFromDisc(SStreamState* pState, SSessionKey* pKey, void** pVal, int32_t* pVLen) {
31✔
916
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentNext_rocksdb(pState, pKey);
31✔
917
  int32_t          code = streamStateSessionGetKVByCur_rocksdb(pState, pCur, pKey, pVal, pVLen);
31✔
918
  streamStateFreeCur(pCur);
31✔
919
  if (code == TSDB_CODE_SUCCESS) {
31!
920
    return code;
×
921
  } else {
922
    pCur = streamStateSessionSeekKeyPrev_rocksdb(pState, pKey);
31✔
923
  }
924

925
  code = streamStateSessionGetKVByCur_rocksdb(pState, pCur, pKey, pVal, pVLen);
31✔
926
  streamStateFreeCur(pCur);
31✔
927
  return code;
31✔
928
}
929

930
int32_t getCountWinResultBuff(SStreamFileState* pFileState, SSessionKey* pKey, COUNT_TYPE winCount, void** pVal,
90✔
931
                              int32_t* pVLen, int32_t* pWinCount) {
932
  int32_t code = TSDB_CODE_SUCCESS;
90✔
933
  int32_t lino = 0;
90✔
934
  (*pWinCount) = TSDB_CODE_SUCCESS;
90✔
935

936
  SSessionKey* pWinKey = pKey;
90✔
937
  const TSKEY  gap = 0;
90✔
938
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
90✔
939
  SArray*      pWinStates = NULL;
90✔
940
  void**       ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
90✔
941
  if (ppBuff) {
90✔
942
    pWinStates = (SArray*)(*ppBuff);
73✔
943
  } else {
944
    pWinStates = taosArrayInit(16, POINTER_BYTES);
17✔
945
    if (!pWinStates) {
17!
946
      code = terrno;
×
947
      QUERY_CHECK_CODE(code, lino, _end);
×
948
    }
949

950
    code = tSimpleHashPut(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
17✔
951
    QUERY_CHECK_CODE(code, lino, _end);
17!
952
  }
953

954
  TSKEY startTs = pWinKey->win.skey;
90✔
955
  TSKEY endTs = pWinKey->win.ekey;
90✔
956

957
  int32_t size = taosArrayGetSize(pWinStates);
90✔
958
  if (size == 0) {
90✔
959
    void* pFileStore = getStateFileStore(pFileState);
24✔
960
    void* pRockVal = NULL;
24✔
961
    (*pWinCount) = getCountWinStateFromDisc(pFileStore, pWinKey, &pRockVal, pVLen);
24✔
962
    if ((*pWinCount) == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
24!
963
      qDebug("===stream===0 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
1!
964
             pWinKey->win.ekey, (*pWinCount));
965
      if ((*pWinCount) == TSDB_CODE_SUCCESS) {
1!
966
        int32_t     valSize = *pVLen;
1✔
967
        COUNT_TYPE* pWinStateCout = (COUNT_TYPE*)((char*)(pRockVal) + (valSize - sizeof(COUNT_TYPE)));
1✔
968
        if (inSessionWindow(pWinKey, startTs, gap) || (*pWinStateCout) < winCount) {
1!
UNCOV
969
          (*pVal) = createSessionWinBuff(pFileState, pWinKey, pRockVal, pVLen);
×
UNCOV
970
          if (!(*pVal)) {
×
971
            code = TSDB_CODE_OUT_OF_MEMORY;
×
972
            QUERY_CHECK_CODE(code, lino, _end);
×
973
          }
974

UNCOV
975
          goto _end;
×
976
        }
977
      }
978
      pWinKey->win.skey = startTs;
1✔
979
      pWinKey->win.ekey = endTs;
1✔
980
      (*pVal) = createSessionWinBuff(pFileState, pWinKey, NULL, NULL);
1✔
981
      taosMemoryFree(pRockVal);
1!
982
      if (!(*pVal)) {
1!
983
        code = TSDB_CODE_OUT_OF_MEMORY;
×
984
        QUERY_CHECK_CODE(code, lino, _end);
×
985
      }
986
    } else {
987
      code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
23✔
988
      QUERY_CHECK_CODE(code, lino, _end);
23!
989

990
      (*pWinCount) = TSDB_CODE_FAILED;
23✔
991
    }
992
    goto _end;
24✔
993
  }
994

995
  // find the first position which is smaller than the pWinKey
996
  int32_t      index = binarySearch(pWinStates, size, pWinKey, sessionStateKeyCompare);
66✔
997
  SRowBuffPos* pPos = NULL;
66✔
998
  int32_t      valSize = *pVLen;
66✔
999

1000
  if (index >= 0) {
66✔
1001
    pPos = taosArrayGetP(pWinStates, index);
58✔
1002
    COUNT_TYPE* pWinStateCout = (COUNT_TYPE*)((char*)(pPos->pRowBuff) + (valSize - sizeof(COUNT_TYPE)));
58✔
1003
    if (inSessionWindow(pPos->pKey, startTs, gap) || (index == size - 1 && (*pWinStateCout) < winCount)) {
58!
1004
      (*pVal) = pPos;
39✔
1005
      SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
39✔
1006
      pPos->beUsed = true;
39✔
1007
      pPos->beFlushed = false;
39✔
1008
      *pWinKey = *pDestWinKey;
39✔
1009
      goto _end;
39✔
1010
    }
1011
  }
1012

1013
  if (index == -1) {
27✔
1014
    if (!isDeteled(pFileState, endTs) && isFlushedState(pFileState, endTs, 0)) {
8!
1015
      SSessionKey tmpKey = *pWinKey;
×
1016
      void*       pRockVal = NULL;
×
1017
      void*       pFileStore = getStateFileStore(pFileState);
×
1018
      int32_t     code_file = getCountWinStateFromDisc(pFileStore, &tmpKey, &pRockVal, pVLen);
×
1019
      if (code_file == TSDB_CODE_SUCCESS) {
×
1020
        SRowBuffPos* pFirstPos = taosArrayGetP(pWinStates, 0);
×
1021
        SSessionKey* pFirstWinKey = (SSessionKey*)pFirstPos->pKey;
×
1022
        if (tmpKey.win.ekey < pFirstWinKey->win.skey) {
×
1023
          *pWinKey = tmpKey;
×
1024
          (*pVal) = createSessionWinBuff(pFileState, pWinKey, pRockVal, pVLen);
×
1025
          if (!(*pVal)) {
×
1026
            code = TSDB_CODE_OUT_OF_MEMORY;
×
1027
            QUERY_CHECK_CODE(code, lino, _end);
×
1028
          }
1029

1030
          (*pWinCount) = code_file;
×
1031
          qDebug("===stream===1 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
×
1032
                 pWinKey->win.ekey, code_file);
1033
          goto _end;
×
1034
        }
1035
      }
1036
      taosMemoryFree(pRockVal);
×
1037
    }
1038
  }
1039

1040
  if (index + 1 < size) {
27✔
1041
    pPos = taosArrayGetP(pWinStates, index + 1);
8✔
1042
    (*pVal) = pPos;
8✔
1043
    SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
8✔
1044
    pPos->beUsed = true;
8✔
1045
    pPos->beFlushed = false;
8✔
1046
    *pWinKey = *pDestWinKey;
8✔
1047
    goto _end;
8✔
1048
  }
1049

1050
  code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
19✔
1051
  QUERY_CHECK_CODE(code, lino, _end);
19!
1052

1053
  (*pWinCount) = TSDB_CODE_FAILED;
19✔
1054

1055
_end:
90✔
1056
  if (code != TSDB_CODE_SUCCESS) {
90!
1057
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1058
  }
1059
  return code;
90✔
1060
}
1061

1062
int32_t createCountWinResultBuff(SStreamFileState* pFileState, SSessionKey* pKey, COUNT_TYPE winCount, void** pVal,
42✔
1063
                                 int32_t* pVLen) {
1064
  SSessionKey* pWinKey = pKey;
42✔
1065
  const TSKEY  gap = 0;
42✔
1066
  int32_t      code = TSDB_CODE_SUCCESS;
42✔
1067
  int32_t      lino = 0;
42✔
1068
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
42✔
1069
  SArray*      pWinStates = NULL;
42✔
1070
  void**       ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
42✔
1071
  if (ppBuff) {
42✔
1072
    pWinStates = (SArray*)(*ppBuff);
38✔
1073
  } else {
1074
    pWinStates = taosArrayInit(16, POINTER_BYTES);
4✔
1075
    code = tSimpleHashPut(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
4✔
1076
    QUERY_CHECK_CODE(code, lino, _end);
4!
1077
  }
1078

1079
  TSKEY startTs = pWinKey->win.skey;
42✔
1080
  TSKEY endTs = pWinKey->win.ekey;
42✔
1081

1082
  int32_t size = taosArrayGetSize(pWinStates);
42✔
1083
  if (size == 0) {
42✔
1084
    void* pFileStore = getStateFileStore(pFileState);
7✔
1085
    void* pRockVal = NULL;
7✔
1086

1087
    int32_t code_file = getCountWinStateFromDisc(pFileStore, pWinKey, &pRockVal, pVLen);
7✔
1088
    if (code_file == TSDB_CODE_SUCCESS && isFlushedState(pFileState, endTs, 0)) {
7!
1089
      int32_t     valSize = *pVLen;
×
1090
      COUNT_TYPE* pWinStateCount = (COUNT_TYPE*)((char*)(pRockVal) + (valSize - sizeof(COUNT_TYPE)));
×
1091
      if ((*pWinStateCount) == winCount) {
×
1092
        code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
×
1093
        QUERY_CHECK_CODE(code, lino, _end);
×
1094
      } else {
1095
        (*pVal) = createSessionWinBuff(pFileState, pWinKey, pRockVal, pVLen);
×
1096
        if (!(*pVal)) {
×
1097
          code = TSDB_CODE_OUT_OF_MEMORY;
×
1098
          QUERY_CHECK_CODE(code, lino, _end);
×
1099
        }
1100
        qDebug("===stream===0 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
×
1101
               pWinKey->win.ekey, code_file);
1102
      }
1103
    } else {
1104
      code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
7✔
1105
      taosMemoryFree(pRockVal);
7!
1106
      QUERY_CHECK_CODE(code, lino, _end);
7!
1107
    }
1108
  } else {
1109
    code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
35✔
1110
    QUERY_CHECK_CODE(code, lino, _end);
35!
1111
  }
1112

1113
_end:
35✔
1114
  if (code != TSDB_CODE_SUCCESS) {
42!
1115
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1116
  }
1117
  return code;
42✔
1118
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc