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

taosdata / TDengine / #3631

07 Mar 2025 03:18PM UTC coverage: 60.671% (-3.0%) from 63.629%
#3631

push

travis-ci

web-flow
Merge pull request #30074 from taosdata/ciup30

ci: update ci workflow to fix path issue

141481 of 300084 branches covered (47.15%)

Branch coverage included in aggregate %.

223132 of 300884 relevant lines covered (74.16%)

7878557.0 hits per line

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

68.41
/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) {
26,327✔
24
  SRowBuffPos* pPos2 = taosArrayGetP(pDatas, pos);
26,327✔
25
  SSessionKey* pWin2 = (SSessionKey*)pPos2->pKey;
26,321✔
26
  return sessionWinKeyCmpr((SSessionKey*)pWin1, pWin2);
26,321✔
27
}
28

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

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

39
  if (num <= 0) return -1;
20,318✔
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;
20,658✔
44
    if (cmpFn(key, keyList, firstPos) == 0) return firstPos;
5,482✔
45
    if (cmpFn(key, keyList, firstPos) < 0) return firstPos - 1;
4,666✔
46

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

50
    if (cmpFn(key, keyList, midPos) < 0) {
2,145✔
51
      lastPos = midPos - 1;
1,155✔
52
    } else if (cmpFn(key, keyList, midPos) > 0) {
992✔
53
      firstPos = midPos + 1;
423✔
54
    } else {
55
      break;
571✔
56
    }
57
  }
58

59
  return midPos;
571✔
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) {
5,334✔
74
  if (ts + gap >= pKey->win.skey && ts - gap <= pKey->win.ekey) {
5,334✔
75
    return true;
1,591✔
76
  }
77
  return false;
3,743✔
78
}
79

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

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

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

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

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

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

131
  *ppPos = pNewPos;
126✔
132

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

274
  (*pWinCode) = TSDB_CODE_FAILED;
77✔
275

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

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

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

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

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

320
  // find the first position which is smaller than the pKey
321
  int32_t index = binarySearch(pWinStates, size, pKey, sessionStateKeyCompare);
13✔
322
  if (index >= 0) {
13!
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);
13✔
330
    if (!tmp) {
13!
331
      code = terrno;
×
332
      QUERY_CHECK_CODE(code, lino, _end);
×
333
    }
334
  }
335

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

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

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

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

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

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

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

436
    goto _end;
52✔
437
  }
438

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

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

450
    goto _end;
13✔
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:
144✔
476
  (*ppVal) = pNewPos;
144✔
477
  if (code != TSDB_CODE_SUCCESS) {
144!
478
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
479
  }
480
  return code;
144✔
481
}
482

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

499
void sessionWinStateCleanup(void* pBuff) {
5,666✔
500
  void*   pIte = NULL;
5,666✔
501
  size_t  keyLen = 0;
5,666✔
502
  int32_t iter = 0;
5,666✔
503
  while ((pIte = tSimpleHashIterate(pBuff, pIte, &iter)) != NULL) {
7,099✔
504
    SArray* pWinStates = (SArray*)(*(void**)pIte);
1,433✔
505
    taosArrayDestroy(pWinStates);
1,433✔
506
  }
507
  tSimpleHashCleanup(pBuff);
5,667✔
508
}
5,667✔
509

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

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

524
  if (pWins) {
9,937✔
525
    (*pWins) = pWinStates;
6,304✔
526
  }
527

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

536
  if (index >= 0) {
9,937✔
537
    pCur = createStateCursor(pFileState);
7,825✔
538
    if (pCur == NULL) {
7,831!
539
      return NULL;
×
540
    }
541
    pCur->buffIndex = index;
7,831✔
542
    if (pIndex) {
7,831✔
543
      *pIndex = index;
5,311✔
544
    }
545
  }
546

547
  return pCur;
9,943✔
548
}
549

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

556
  void* pFileStore = getStateFileStore(pFileState);
1,314✔
557
  pCur = streamStateSessionSeekKeyCurrentPrev_rocksdb(pFileStore, pWinKey);
1,314✔
558
  if (!pCur) {
1,314✔
559
    return NULL;
608✔
560
  }
561
  pCur->buffIndex = -1;
706✔
562
  pCur->pStreamFileState = pFileState;
706✔
563
  return pCur;
706✔
564
}
565

566
SStreamStateCur* sessionWinStateSeekKeyPrev(SStreamFileState *pFileState, const SSessionKey *pWinKey) {
×
567
  SArray*          pWinStates = NULL;
×
568
  int32_t          index = -1;
×
569
  SStreamStateCur *pCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, &pWinStates, &index);
×
570
  if (pCur) {
×
571
    int32_t cmpRes= sessionStateRangeKeyCompare(pWinKey, pWinStates, index);
×
572
    if (cmpRes > 0) {
×
573
      return pCur;
×
574
    } else if (cmpRes == 0 && index > 0) {
×
575
      sessionWinStateMoveToPrev(pCur);
×
576
      return pCur;
×
577
    }
578
    streamStateFreeCur(pCur);
×
579
    pCur = NULL;
×
580
  }
581

582
  void* pFileStore = getStateFileStore(pFileState);
×
583
  pCur = streamStateSessionSeekKeyPrev_rocksdb(pFileStore, pWinKey);
×
584
  if (!pCur) {
×
585
    return NULL;
×
586
  }
587
  pCur->buffIndex = -1;
×
588
  pCur->pStreamFileState = pFileState;
×
589
  return pCur;
×
590
}
591

592
static void transformCursor(SStreamFileState* pFileState, SStreamStateCur* pCur) {
333✔
593
  if (!pCur) {
333!
594
    return;
×
595
  }
596
  streamStateResetCur(pCur);
333✔
597
  pCur->buffIndex = 0;
333✔
598
  pCur->pStreamFileState = pFileState;
333✔
599
}
600

601
static void checkAndTransformCursor(SStreamFileState* pFileState, const uint64_t groupId, SArray* pWinStates,
1,415✔
602
                                    SStreamStateCur** ppCur) {
603
  SSessionKey key = {.groupId = groupId};
1,415✔
604
  int32_t     code = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), *ppCur, &key, NULL, NULL);
1,415✔
605
  if (taosArrayGetSize(pWinStates) > 0 &&
1,415✔
606
      (code == TSDB_CODE_FAILED || sessionStateKeyCompare(&key, pWinStates, 0) >= 0)) {
134!
607
    if (!(*ppCur)) {
327✔
608
      (*ppCur) = createStateCursor(pFileState);
322✔
609
    }
610
    transformCursor(pFileState, *ppCur);
327✔
611
  } else if (*ppCur) {
1,088✔
612
    (*ppCur)->buffIndex = -1;
441✔
613
    (*ppCur)->pStreamFileState = pFileState;
441✔
614
  }
615
}
1,415✔
616

617
SStreamStateCur* sessionWinStateSeekKeyCurrentNext(SStreamFileState* pFileState, const SSessionKey* pWinKey) {
165✔
618
  SArray*          pWinStates = NULL;
165✔
619
  int32_t          index = -1;
165✔
620
  SStreamStateCur* pCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, &pWinStates, &index);
165✔
621
  if (pCur) {
165✔
622
    if (sessionStateRangeKeyCompare(pWinKey, pWinStates, index) > 0) {
36✔
623
      sessionWinStateMoveToNext(pCur);
10✔
624
    }
625
    return pCur;
36✔
626
  }
627

628
  void* pFileStore = getStateFileStore(pFileState);
129✔
629
  pCur = streamStateSessionSeekKeyCurrentNext_rocksdb(pFileStore, (SSessionKey*)pWinKey);
129✔
630
  checkAndTransformCursor(pFileState, pWinKey->groupId, pWinStates, &pCur);
129✔
631
  return pCur;
129✔
632
}
633

634
SStreamStateCur* sessionWinStateSeekKeyNext(SStreamFileState* pFileState, const SSessionKey* pWinKey) {
6,505✔
635
  SArray*          pWinStates = NULL;
6,505✔
636
  int32_t          index = -1;
6,505✔
637
  SStreamStateCur* pCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, &pWinStates, &index);
6,505✔
638
  if (pCur) {
6,507✔
639
    sessionWinStateMoveToNext(pCur);
5,221✔
640
    return pCur;
5,221✔
641
  }
642

643
  void* pFileStore = getStateFileStore(pFileState);
1,286✔
644
  pCur = streamStateSessionSeekKeyNext_rocksdb(pFileStore, pWinKey);
1,286✔
645
  checkAndTransformCursor(pFileState, pWinKey->groupId, pWinStates, &pCur);
1,286✔
646
  return pCur;
1,285✔
647
}
648

649
SStreamStateCur* countWinStateSeekKeyPrev(SStreamFileState* pFileState, const SSessionKey* pWinKey, COUNT_TYPE count) {
61✔
650
  SArray*          pWinStates = NULL;
61✔
651
  int32_t          index = -1;
61✔
652
  SStreamStateCur* pBuffCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, &pWinStates, &index);
61✔
653
  int32_t          resSize = getRowStateRowSize(pFileState);
61✔
654
  COUNT_TYPE       winCount = 0;
61✔
655
  if (pBuffCur) {
61✔
656
    while (index >= 0) {
123✔
657
      SRowBuffPos* pPos = taosArrayGetP(pWinStates, index);
101✔
658
      winCount = *((COUNT_TYPE*)((char*)pPos->pRowBuff + (resSize - sizeof(COUNT_TYPE))));
101✔
659
      if (sessionStateRangeKeyCompare(pWinKey, pWinStates, index) == 0 || winCount < count) {
101✔
660
        index--;
69✔
661
      } else if (index >= 0) {
32!
662
        pBuffCur->buffIndex = index + 1;
32✔
663
        return pBuffCur;
32✔
664
      }
665
    }
666
    pBuffCur->buffIndex = 0;
22✔
667
  } else if (taosArrayGetSize(pWinStates) > 0) {
7!
668
    pBuffCur = createStateCursor(pFileState);
×
669
    if (pBuffCur == NULL) {
×
670
      return NULL;
×
671
    }
672
    pBuffCur->buffIndex = 0;
×
673
  }
674

675
  void*            pFileStore = getStateFileStore(pFileState);
29✔
676
  SStreamStateCur* pCur = streamStateSessionSeekKeyPrev_rocksdb(pFileStore, pWinKey);
29✔
677
  if (pCur) {
29!
678
    pCur->pStreamFileState = pFileState;
×
679
    SSessionKey key = {0};
×
680
    void*       pVal = NULL;
×
681
    int         len = 0;
×
682
    int32_t     code = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, &key, &pVal, &len);
×
683
    if (code == TSDB_CODE_FAILED) {
×
684
      streamStateFreeCur(pCur);
×
685
      return pBuffCur;
×
686
    }
687
    winCount = *((COUNT_TYPE*)((char*)pVal + (resSize - sizeof(COUNT_TYPE))));
×
688
    taosMemoryFreeClear(pVal);
×
689
    streamStateFreeCur(pBuffCur);
×
690
    if (sessionRangeKeyCmpr(pWinKey, &key) != 0 && winCount == count) {
×
691
      streamStateCurNext(pFileStore, pCur);
×
692
      return pCur;
×
693
    }
694
    streamStateCurPrev(pFileStore, pCur);
×
695
    while (1) {
696
      code = streamStateSessionGetKVByCur_rocksdb(NULL, pCur, &key, &pVal, &len);
×
697
      if (code == TSDB_CODE_FAILED) {
×
698
        streamStateCurNext(pFileStore, pCur);
×
699
        return pCur;
×
700
      }
701
      winCount = *((COUNT_TYPE*)((char*)pVal + (resSize - sizeof(COUNT_TYPE))));
×
702
      taosMemoryFreeClear(pVal);
×
703
      if (sessionRangeKeyCmpr(pWinKey, &key) == 0 || winCount < count) {
×
704
        streamStateCurPrev(pFileStore, pCur);
×
705
      } else {
706
        streamStateCurNext(pFileStore, pCur);
×
707
        return pCur;
×
708
      }
709
    }
710
  }
711
  return pBuffCur;
29✔
712
}
713

714
int32_t sessionWinStateGetKVByCur(SStreamStateCur* pCur, SSessionKey* pKey, void** pVal, int32_t* pVLen) {
11,468✔
715
  if (!pCur) {
11,468✔
716
    return TSDB_CODE_FAILED;
1,314✔
717
  }
718
  int32_t code = TSDB_CODE_SUCCESS;
10,154✔
719

720
  SSHashObj* pSessionBuff = getRowStateBuff(pCur->pStreamFileState);
10,154✔
721
  void**     ppBuff = tSimpleHashGet(pSessionBuff, &pKey->groupId, sizeof(uint64_t));
10,155✔
722
  SArray*    pWinStates = NULL;
10,156✔
723
  if (ppBuff) {
10,156✔
724
    pWinStates = (SArray*)(*ppBuff);
9,897✔
725
  }
726

727
  if (pCur->buffIndex >= 0) {
10,156✔
728
    int32_t size = taosArrayGetSize(pWinStates);
8,942✔
729
    if (pCur->buffIndex >= size) {
8,942✔
730
      return TSDB_CODE_FAILED;
5,330✔
731
    }
732
    SRowBuffPos* pPos = taosArrayGetP(pWinStates, pCur->buffIndex);
3,612✔
733
    if (pVal) {
3,611✔
734
      *pVal = pPos;
636✔
735
    }
736
    *pKey = *(SSessionKey*)(pPos->pKey);
3,611✔
737
  } else {
738
    void* pData = NULL;
1,214✔
739
    code = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pCur->pStreamFileState), pCur, pKey, &pData, pVLen);
1,214✔
740
    if (taosArrayGetSize(pWinStates) > 0 &&
1,214✔
741
        (code == TSDB_CODE_FAILED || sessionStateRangeKeyCompare(pKey, pWinStates, 0) >= 0)) {
621!
742
      transformCursor(pCur->pStreamFileState, pCur);
6✔
743
      SRowBuffPos* pPos = taosArrayGetP(pWinStates, pCur->buffIndex);
6✔
744
      if (pVal) {
6!
745
        *pVal = pPos;
×
746
      }
747
      *pKey = *(SSessionKey*)(pPos->pKey);
6✔
748
      code = TSDB_CODE_SUCCESS;
6✔
749
    } else if (code == TSDB_CODE_SUCCESS && pVal) {
1,208✔
750
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pCur->pStreamFileState);
136✔
751
      if (!pNewPos || !pNewPos->pRowBuff) {
136!
752
        code = TSDB_CODE_OUT_OF_MEMORY;
1✔
753
        taosMemoryFreeClear(pData);
1!
754
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
1!
755
        return code;
1✔
756
      }
757
      memcpy(pNewPos->pKey, pKey, sizeof(SSessionKey));
135✔
758
      pNewPos->needFree = true;
135✔
759
      pNewPos->beFlushed = true;
135✔
760
      memcpy(pNewPos->pRowBuff, pData, *pVLen);
135✔
761
      (*pVal) = pNewPos;
135✔
762
    }
763
    taosMemoryFreeClear(pData);
1,213!
764
  }
765
  return code;
4,824✔
766
}
767

768
void sessionWinStateMoveToNext(SStreamStateCur* pCur) {
8,621✔
769
  qTrace("move cursor to next");
8,621!
770
  if (pCur && pCur->buffIndex >= 0) {
8,621✔
771
    pCur->buffIndex++;
6,024✔
772
  } else {
773
    streamStateCurNext_rocksdb(pCur);
2,597✔
774
  }
775
}
8,621✔
776

777
void sessionWinStateMoveToPrev(SStreamStateCur* pCur) {
×
778
  qTrace("move cursor to prev");
×
779
  if (pCur && pCur->buffIndex >= 1) {
×
780
    pCur->buffIndex--;
×
781
  } else {
782
    streamStateCurPrev_rocksdb(pCur);
×
783
  }
784
}
×
785

786
int32_t sessionWinStateGetKeyByRange(SStreamFileState* pFileState, const SSessionKey* key, SSessionKey* curKey,
3,629✔
787
                                     range_cmpr_fn cmpFn) {
788
  SStreamStateCur* pCur = sessionWinStateSeekKeyCurrentPrev(pFileState, key);
3,629✔
789
  SSessionKey      tmpKey = *key;
3,630✔
790
  int32_t          code = sessionWinStateGetKVByCur(pCur, &tmpKey, NULL, NULL);
3,630✔
791
  bool             hasCurrentPrev = true;
3,629✔
792
  if (code == TSDB_CODE_FAILED) {
3,629✔
793
    streamStateFreeCur(pCur);
691✔
794
    pCur = sessionWinStateSeekKeyNext(pFileState, key);
691✔
795
    code = sessionWinStateGetKVByCur(pCur, &tmpKey, NULL, NULL);
691✔
796
    hasCurrentPrev = false;
691✔
797
  }
798

799
  if (code == TSDB_CODE_FAILED) {
3,629✔
800
    code = TSDB_CODE_FAILED;
512✔
801
    goto _end;
512✔
802
  }
803

804
  if (cmpFn(key, &tmpKey) == 0) {
3,117✔
805
    *curKey = tmpKey;
2,338✔
806
    goto _end;
2,338✔
807
  } else if (!hasCurrentPrev) {
779✔
808
    code = TSDB_CODE_FAILED;
158✔
809
    goto _end;
158✔
810
  }
811

812
  sessionWinStateMoveToNext(pCur);
621✔
813
  code = sessionWinStateGetKVByCur(pCur, &tmpKey, NULL, NULL);
622✔
814
  if (code == TSDB_CODE_SUCCESS && cmpFn(key, &tmpKey) == 0) {
622✔
815
    *curKey = tmpKey;
260✔
816
  } else {
817
    code = TSDB_CODE_FAILED;
362✔
818
  }
819

820
_end:
3,630✔
821
  streamStateFreeCur(pCur);
3,630✔
822
  return code;
3,630✔
823
}
824

825
int32_t getStateWinResultBuff(SStreamFileState* pFileState, SSessionKey* key, char* pKeyData, int32_t keyDataLen,
2,926✔
826
                              state_key_cmpr_fn fn, void** pVal, int32_t* pVLen, int32_t* pWinCode) {
827
  int32_t code = TSDB_CODE_SUCCESS;
2,926✔
828
  int32_t lino = 0;
2,926✔
829
  (*pWinCode) = TSDB_CODE_SUCCESS;
2,926✔
830

831
  SSessionKey* pWinKey = key;
2,926✔
832
  TSKEY        gap = 0;
2,926✔
833
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
2,926✔
834
  SArray*      pWinStates = NULL;
2,927✔
835

836
  void** ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
2,927✔
837
  if (ppBuff) {
2,927✔
838
    pWinStates = (SArray*)(*ppBuff);
2,544✔
839
  } else {
840
    pWinStates = taosArrayInit(16, POINTER_BYTES);
383✔
841
    if (!pWinStates) {
383!
842
      code = terrno;
×
843
      QUERY_CHECK_CODE(code, lino, _end);
×
844
    }
845

846
    code = tSimpleHashPut(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
383✔
847
    QUERY_CHECK_CODE(code, lino, _end);
383!
848
  }
849

850
  TSKEY startTs = pWinKey->win.skey;
2,927✔
851
  TSKEY endTs = pWinKey->win.ekey;
2,927✔
852

853
  int32_t size = taosArrayGetSize(pWinStates);
2,927✔
854
  if (size == 0) {
2,927✔
855
    void*   pFileStore = getStateFileStore(pFileState);
661✔
856
    void*   p = NULL;
661✔
857
    int32_t code_file = streamStateStateAddIfNotExist_rocksdb(pFileStore, pWinKey, pKeyData, keyDataLen, fn, &p, pVLen);
661✔
858
    if (code_file == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
661✔
859
      (*pVal) = createSessionWinBuff(pFileState, pWinKey, p, pVLen);
17✔
860
      if (!(*pVal)) {
17!
861
        code = TSDB_CODE_OUT_OF_MEMORY;
×
862
        QUERY_CHECK_CODE(code, lino, _end);
×
863
      }
864

865
      (*pWinCode) = code_file;
17✔
866
      qDebug("===stream===0 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
17✔
867
             pWinKey->win.ekey, code_file);
868
    } else {
869
      code = addNewSessionWindow(pFileState, pWinStates, key, (SRowBuffPos**)pVal);
644✔
870
      (*pWinCode) = TSDB_CODE_FAILED;
644✔
871
      taosMemoryFree(p);
644!
872
      QUERY_CHECK_CODE(code, lino, _end);
644!
873
    }
874
    goto _end;
661✔
875
  }
876

877
  // find the first position which is smaller than the pWinKey
878
  int32_t      index = binarySearch(pWinStates, size, pWinKey, sessionStateKeyCompare);
2,266✔
879
  SRowBuffPos* pPos = NULL;
2,266✔
880
  int32_t      valSize = *pVLen;
2,266✔
881

882
  if (index >= 0) {
2,266✔
883
    pPos = taosArrayGetP(pWinStates, index);
2,158✔
884
    void* stateKey = (char*)(pPos->pRowBuff) + (valSize - keyDataLen);
2,158✔
885
    if (inSessionWindow(pPos->pKey, startTs, gap) || fn(pKeyData, stateKey) == true) {
2,158✔
886
      (*pVal) = pPos;
1,704✔
887
      SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
1,704✔
888
      pPos->beUsed = true;
1,704✔
889
      pPos->beFlushed = false;
1,704✔
890
      *key = *pDestWinKey;
1,704✔
891
      goto _end;
1,704✔
892
    }
893
  }
894

895
  if (index + 1 < size) {
562✔
896
    pPos = taosArrayGetP(pWinStates, index + 1);
137✔
897
    void* stateKey = (char*)(pPos->pRowBuff) + (valSize - keyDataLen);
137✔
898
    if (inSessionWindow(pPos->pKey, startTs, gap) || (endTs != INT64_MIN && inSessionWindow(pPos->pKey, endTs, gap)) ||
272!
899
        fn(pKeyData, stateKey) == true) {
135✔
900
      (*pVal) = pPos;
7✔
901
      SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
7✔
902
      pPos->beUsed = true;
7✔
903
      pPos->beFlushed = false;
7✔
904
      *key = *pDestWinKey;
7✔
905
      goto _end;
7✔
906
    }
907
  }
908

909
  if (index + 1 == 0) {
555✔
910
    if (!isDeteled(pFileState, endTs)) {
103!
911
      void*   p = NULL;
103✔
912
      void*   pFileStore = getStateFileStore(pFileState);
103✔
913
      int32_t code_file =
914
          streamStateStateAddIfNotExist_rocksdb(pFileStore, pWinKey, pKeyData, keyDataLen, fn, &p, pVLen);
103✔
915
      if (code_file == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
103✔
916
        (*pVal) = createSessionWinBuff(pFileState, pWinKey, p, pVLen);
94✔
917
        if (!(*pVal)) {
94!
918
          code = TSDB_CODE_OUT_OF_MEMORY;
×
919
          QUERY_CHECK_CODE(code, lino, _end);
94!
920
        }
921

922
        (*pWinCode) = code_file;
94✔
923
        qDebug("===stream===1 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
94✔
924
               pWinKey->win.ekey, code_file);
925
        goto _end;
94✔
926
      } else {
927
        taosMemoryFree(p);
9!
928
      }
929
    }
930
  }
931

932
  if (index == size - 1) {
461✔
933
    code = addNewSessionWindow(pFileState, pWinStates, key, (SRowBuffPos**)pVal);
425✔
934
    QUERY_CHECK_CODE(code, lino, _end);
425!
935

936
    (*pWinCode) = TSDB_CODE_FAILED;
425✔
937
    goto _end;
425✔
938
  }
939
  code = insertNewSessionWindow(pFileState, pWinStates, key, index + 1, (SRowBuffPos**)pVal);
36✔
940
  QUERY_CHECK_CODE(code, lino, _end);
36!
941

942
  (*pWinCode) = TSDB_CODE_FAILED;
36✔
943

944
_end:
2,927✔
945
  if (code != TSDB_CODE_SUCCESS) {
2,927!
946
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
947
  }
948
  return code;
2,927✔
949
}
950

951
int32_t getCountWinStateFromDisc(SStreamState* pState, SSessionKey* pKey, void** pVal, int32_t* pVLen) {
31✔
952
  SStreamStateCur* pCur = streamStateSessionSeekKeyCurrentNext_rocksdb(pState, pKey);
31✔
953
  int32_t          code = streamStateSessionGetKVByCur_rocksdb(pState, pCur, pKey, pVal, pVLen);
31✔
954
  streamStateFreeCur(pCur);
31✔
955
  if (code == TSDB_CODE_SUCCESS) {
31!
956
    return code;
×
957
  } else {
958
    pCur = streamStateSessionSeekKeyPrev_rocksdb(pState, pKey);
31✔
959
  }
960

961
  code = streamStateSessionGetKVByCur_rocksdb(pState, pCur, pKey, pVal, pVLen);
31✔
962
  streamStateFreeCur(pCur);
31✔
963
  return code;
31✔
964
}
965

966
int32_t getCountWinResultBuff(SStreamFileState* pFileState, SSessionKey* pKey, COUNT_TYPE winCount, void** pVal,
90✔
967
                              int32_t* pVLen, int32_t* pWinCount) {
968
  int32_t code = TSDB_CODE_SUCCESS;
90✔
969
  int32_t lino = 0;
90✔
970
  (*pWinCount) = TSDB_CODE_SUCCESS;
90✔
971

972
  SSessionKey* pWinKey = pKey;
90✔
973
  const TSKEY  gap = 0;
90✔
974
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
90✔
975
  SArray*      pWinStates = NULL;
90✔
976
  void**       ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
90✔
977
  if (ppBuff) {
90✔
978
    pWinStates = (SArray*)(*ppBuff);
73✔
979
  } else {
980
    pWinStates = taosArrayInit(16, POINTER_BYTES);
17✔
981
    if (!pWinStates) {
17!
982
      code = terrno;
×
983
      QUERY_CHECK_CODE(code, lino, _end);
×
984
    }
985

986
    code = tSimpleHashPut(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
17✔
987
    QUERY_CHECK_CODE(code, lino, _end);
17!
988
  }
989

990
  TSKEY startTs = pWinKey->win.skey;
90✔
991
  TSKEY endTs = pWinKey->win.ekey;
90✔
992

993
  int32_t size = taosArrayGetSize(pWinStates);
90✔
994
  if (size == 0) {
90✔
995
    void* pFileStore = getStateFileStore(pFileState);
24✔
996
    void* pRockVal = NULL;
24✔
997
    (*pWinCount) = getCountWinStateFromDisc(pFileStore, pWinKey, &pRockVal, pVLen);
24✔
998
    if ((*pWinCount) == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
24!
999
      qDebug("===stream===0 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
1!
1000
             pWinKey->win.ekey, (*pWinCount));
1001
      if ((*pWinCount) == TSDB_CODE_SUCCESS) {
1!
1002
        int32_t     valSize = *pVLen;
1✔
1003
        COUNT_TYPE* pWinStateCout = (COUNT_TYPE*)((char*)(pRockVal) + (valSize - sizeof(COUNT_TYPE)));
1✔
1004
        if (inSessionWindow(pWinKey, startTs, gap) || (*pWinStateCout) < winCount) {
1!
1005
          (*pVal) = createSessionWinBuff(pFileState, pWinKey, pRockVal, pVLen);
×
1006
          if (!(*pVal)) {
×
1007
            code = TSDB_CODE_OUT_OF_MEMORY;
×
1008
            QUERY_CHECK_CODE(code, lino, _end);
×
1009
          }
1010

1011
          goto _end;
×
1012
        }
1013
      }
1014
      pWinKey->win.skey = startTs;
1✔
1015
      pWinKey->win.ekey = endTs;
1✔
1016
      (*pVal) = createSessionWinBuff(pFileState, pWinKey, NULL, NULL);
1✔
1017
      taosMemoryFree(pRockVal);
1!
1018
      if (!(*pVal)) {
1!
1019
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1020
        QUERY_CHECK_CODE(code, lino, _end);
×
1021
      }
1022
    } else {
1023
      code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
23✔
1024
      QUERY_CHECK_CODE(code, lino, _end);
23!
1025

1026
      (*pWinCount) = TSDB_CODE_FAILED;
23✔
1027
    }
1028
    goto _end;
24✔
1029
  }
1030

1031
  // find the first position which is smaller than the pWinKey
1032
  int32_t      index = binarySearch(pWinStates, size, pWinKey, sessionStateKeyCompare);
66✔
1033
  SRowBuffPos* pPos = NULL;
66✔
1034
  int32_t      valSize = *pVLen;
66✔
1035

1036
  if (index >= 0) {
66✔
1037
    pPos = taosArrayGetP(pWinStates, index);
58✔
1038
    COUNT_TYPE* pWinStateCout = (COUNT_TYPE*)((char*)(pPos->pRowBuff) + (valSize - sizeof(COUNT_TYPE)));
58✔
1039
    if (inSessionWindow(pPos->pKey, startTs, gap) || (index == size - 1 && (*pWinStateCout) < winCount)) {
58!
1040
      (*pVal) = pPos;
39✔
1041
      SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
39✔
1042
      pPos->beUsed = true;
39✔
1043
      pPos->beFlushed = false;
39✔
1044
      *pWinKey = *pDestWinKey;
39✔
1045
      goto _end;
39✔
1046
    }
1047
  }
1048

1049
  if (index == -1) {
27✔
1050
    if (!isDeteled(pFileState, endTs) && isFlushedState(pFileState, endTs, 0)) {
8!
1051
      SSessionKey tmpKey = *pWinKey;
×
1052
      void*       pRockVal = NULL;
×
1053
      void*       pFileStore = getStateFileStore(pFileState);
×
1054
      int32_t     code_file = getCountWinStateFromDisc(pFileStore, &tmpKey, &pRockVal, pVLen);
×
1055
      if (code_file == TSDB_CODE_SUCCESS) {
×
1056
        SRowBuffPos* pFirstPos = taosArrayGetP(pWinStates, 0);
×
1057
        SSessionKey* pFirstWinKey = (SSessionKey*)pFirstPos->pKey;
×
1058
        if (tmpKey.win.ekey < pFirstWinKey->win.skey) {
×
1059
          *pWinKey = tmpKey;
×
1060
          (*pVal) = createSessionWinBuff(pFileState, pWinKey, pRockVal, pVLen);
×
1061
          if (!(*pVal)) {
×
1062
            code = TSDB_CODE_OUT_OF_MEMORY;
×
1063
            QUERY_CHECK_CODE(code, lino, _end);
×
1064
          }
1065

1066
          (*pWinCount) = code_file;
×
1067
          qDebug("===stream===1 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
×
1068
                 pWinKey->win.ekey, code_file);
1069
          goto _end;
×
1070
        }
1071
      }
1072
      taosMemoryFree(pRockVal);
×
1073
    }
1074
  }
1075

1076
  if (index + 1 < size) {
27✔
1077
    pPos = taosArrayGetP(pWinStates, index + 1);
8✔
1078
    (*pVal) = pPos;
8✔
1079
    SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
8✔
1080
    pPos->beUsed = true;
8✔
1081
    pPos->beFlushed = false;
8✔
1082
    *pWinKey = *pDestWinKey;
8✔
1083
    goto _end;
8✔
1084
  }
1085

1086
  code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
19✔
1087
  QUERY_CHECK_CODE(code, lino, _end);
19!
1088

1089
  (*pWinCount) = TSDB_CODE_FAILED;
19✔
1090

1091
_end:
90✔
1092
  if (code != TSDB_CODE_SUCCESS) {
90!
1093
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1094
  }
1095
  return code;
90✔
1096
}
1097

1098
int32_t createCountWinResultBuff(SStreamFileState* pFileState, SSessionKey* pKey, COUNT_TYPE winCount, void** pVal,
42✔
1099
                                 int32_t* pVLen) {
1100
  SSessionKey* pWinKey = pKey;
42✔
1101
  const TSKEY  gap = 0;
42✔
1102
  int32_t      code = TSDB_CODE_SUCCESS;
42✔
1103
  int32_t      lino = 0;
42✔
1104
  SSHashObj*   pSessionBuff = getRowStateBuff(pFileState);
42✔
1105
  SArray*      pWinStates = NULL;
42✔
1106
  void**       ppBuff = tSimpleHashGet(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t));
42✔
1107
  if (ppBuff) {
42✔
1108
    pWinStates = (SArray*)(*ppBuff);
38✔
1109
  } else {
1110
    pWinStates = taosArrayInit(16, POINTER_BYTES);
4✔
1111
    code = tSimpleHashPut(pSessionBuff, &pWinKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
4✔
1112
    QUERY_CHECK_CODE(code, lino, _end);
4!
1113
  }
1114

1115
  TSKEY startTs = pWinKey->win.skey;
42✔
1116
  TSKEY endTs = pWinKey->win.ekey;
42✔
1117

1118
  int32_t size = taosArrayGetSize(pWinStates);
42✔
1119
  if (size == 0) {
42✔
1120
    void* pFileStore = getStateFileStore(pFileState);
7✔
1121
    void* pRockVal = NULL;
7✔
1122

1123
    int32_t code_file = getCountWinStateFromDisc(pFileStore, pWinKey, &pRockVal, pVLen);
7✔
1124
    if (code_file == TSDB_CODE_SUCCESS && isFlushedState(pFileState, endTs, 0)) {
7!
1125
      int32_t     valSize = *pVLen;
×
1126
      COUNT_TYPE* pWinStateCount = (COUNT_TYPE*)((char*)(pRockVal) + (valSize - sizeof(COUNT_TYPE)));
×
1127
      if ((*pWinStateCount) == winCount) {
×
1128
        code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
×
1129
        QUERY_CHECK_CODE(code, lino, _end);
×
1130
      } else {
1131
        (*pVal) = createSessionWinBuff(pFileState, pWinKey, pRockVal, pVLen);
×
1132
        if (!(*pVal)) {
×
1133
          code = TSDB_CODE_OUT_OF_MEMORY;
×
1134
          QUERY_CHECK_CODE(code, lino, _end);
×
1135
        }
1136
        qDebug("===stream===0 get state win:%" PRId64 ",%" PRId64 " from disc, res %d", pWinKey->win.skey,
×
1137
               pWinKey->win.ekey, code_file);
1138
      }
1139
    } else {
1140
      code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
7✔
1141
      taosMemoryFree(pRockVal);
7!
1142
      QUERY_CHECK_CODE(code, lino, _end);
7!
1143
    }
1144
  } else {
1145
    code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
35✔
1146
    QUERY_CHECK_CODE(code, lino, _end);
35!
1147
  }
1148

1149
_end:
35✔
1150
  if (code != TSDB_CODE_SUCCESS) {
42!
1151
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1152
  }
1153
  return code;
42✔
1154
}
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