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

taosdata / TDengine / #3627

02 Mar 2025 11:16PM UTC coverage: 63.596% (-0.2%) from 63.764%
#3627

push

travis-ci

GitHub
Merge pull request #29973 from taosdata/doc/internal

148665 of 299855 branches covered (49.58%)

Branch coverage included in aggregate %.

233076 of 300407 relevant lines covered (77.59%)

17543856.65 hits per line

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

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

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

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

39
  if (num <= 0) return -1;
29,954✔
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;
30,697✔
44
    if (cmpFn(key, keyList, firstPos) == 0) return firstPos;
7,697✔
45
    if (cmpFn(key, keyList, firstPos) < 0) return firstPos - 1;
6,743✔
46

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

50
    if (cmpFn(key, keyList, midPos) < 0) {
3,129✔
51
      lastPos = midPos - 1;
1,608✔
52
    } else if (cmpFn(key, keyList, midPos) > 0) {
1,525✔
53
      firstPos = midPos + 1;
837✔
54
    } else {
55
      break;
695✔
56
    }
57
  }
58

59
  return midPos;
695✔
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) {
10,163✔
74
  if (ts + gap >= pKey->win.skey && ts - gap <= pKey->win.ekey) {
10,163✔
75
    return true;
2,171✔
76
  }
77
  return false;
7,992✔
78
}
79

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

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

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

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

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

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

131
  *ppPos = pNewPos;
156✔
132

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

274
  (*pWinCode) = TSDB_CODE_FAILED;
74✔
275

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

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

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

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

310
  int32_t size = taosArrayGetSize(pWinStates);
154✔
311
  if (size == 0) {
154✔
312
    void* tmp = taosArrayPush(pWinStates, &pPos);
141✔
313
    if (!tmp) {
141!
314
      code = terrno;
×
315
      QUERY_CHECK_CODE(code, lino, _end);
×
316
    }
317
    goto _end;
141✔
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;
154✔
338
  if (code != TSDB_CODE_SUCCESS) {
154!
339
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
340
  }
341
  return code;
154✔
342
}
343

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

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

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

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

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

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

436
    goto _end;
97✔
437
  }
438

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

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

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

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

499
void sessionWinStateCleanup(void* pBuff) {
7,957✔
500
  void*   pIte = NULL;
7,957✔
501
  size_t  keyLen = 0;
7,957✔
502
  int32_t iter = 0;
7,957✔
503
  while ((pIte = tSimpleHashIterate(pBuff, pIte, &iter)) != NULL) {
11,403✔
504
    SArray* pWinStates = (SArray*)(*(void**)pIte);
3,446✔
505
    taosArrayDestroy(pWinStates);
3,446✔
506
  }
507
  tSimpleHashCleanup(pBuff);
7,958✔
508
}
7,958✔
509

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

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

524
  if (pWins) {
13,206✔
525
    (*pWins) = pWinStates;
8,262✔
526
  }
527

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

536
  if (index >= 0) {
13,206✔
537
    pCur = createStateCursor(pFileState);
10,407✔
538
    if (pCur == NULL) {
10,406!
539
      return NULL;
×
540
    }
541
    pCur->buffIndex = index;
10,406✔
542
    if (pIndex) {
10,406✔
543
      *pIndex = index;
6,949✔
544
    }
545
  }
546

547
  return pCur;
13,205✔
548
}
549

550
SStreamStateCur* sessionWinStateSeekKeyCurrentPrev(SStreamFileState* pFileState, const SSessionKey* pWinKey) {
5,235✔
551
  SStreamStateCur* pCur = seekKeyCurrentPrev_buff(pFileState, pWinKey, NULL, NULL);
5,235✔
552
  if (pCur) {
5,236✔
553
    return pCur;
3,459✔
554
  }
555

556
  void* pFileStore = getStateFileStore(pFileState);
1,777✔
557
  pCur = streamStateSessionSeekKeyCurrentPrev_rocksdb(pFileStore, pWinKey);
1,778✔
558
  if (!pCur) {
1,778✔
559
    return NULL;
758✔
560
  }
561
  pCur->buffIndex = -1;
1,020✔
562
  pCur->pStreamFileState = pFileState;
1,020✔
563
  return pCur;
1,020✔
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) {
537✔
593
  if (!pCur) {
537!
594
    return;
×
595
  }
596
  streamStateResetCur(pCur);
537✔
597
  pCur->buffIndex = 0;
537✔
598
  pCur->pStreamFileState = pFileState;
537✔
599
}
600

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

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

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

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

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

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

675
  void*            pFileStore = getStateFileStore(pFileState);
344✔
676
  SStreamStateCur* pCur = streamStateSessionSeekKeyPrev_rocksdb(pFileStore, pWinKey);
344✔
677
  if (pCur) {
344!
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;
344✔
712
}
713

714
int32_t sessionWinStateGetKVByCur(SStreamStateCur* pCur, SSessionKey* pKey, void** pVal, int32_t* pVLen) {
16,120✔
715
  if (!pCur) {
16,120✔
716
    return TSDB_CODE_FAILED;
1,918✔
717
  }
718
  int32_t code = TSDB_CODE_SUCCESS;
14,202✔
719

720
  SSHashObj* pSessionBuff = getRowStateBuff(pCur->pStreamFileState);
14,202✔
721
  void**     ppBuff = tSimpleHashGet(pSessionBuff, &pKey->groupId, sizeof(uint64_t));
14,201✔
722
  SArray*    pWinStates = NULL;
14,203✔
723
  if (ppBuff) {
14,203✔
724
    pWinStates = (SArray*)(*ppBuff);
13,789✔
725
  }
726

727
  if (pCur->buffIndex >= 0) {
14,203✔
728
    int32_t size = taosArrayGetSize(pWinStates);
12,483✔
729
    if (pCur->buffIndex >= size) {
12,482✔
730
      return TSDB_CODE_FAILED;
7,170✔
731
    }
732
    SRowBuffPos* pPos = taosArrayGetP(pWinStates, pCur->buffIndex);
5,312✔
733
    if (pVal) {
5,312✔
734
      *pVal = pPos;
1,237✔
735
    }
736
    *pKey = *(SSessionKey*)(pPos->pKey);
5,312✔
737
  } else {
738
    void* pData = NULL;
1,720✔
739
    code = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pCur->pStreamFileState), pCur, pKey, &pData, pVLen);
1,720✔
740
    if (taosArrayGetSize(pWinStates) > 0 &&
1,720✔
741
        (code == TSDB_CODE_FAILED || sessionStateRangeKeyCompare(pKey, pWinStates, 0) >= 0)) {
804!
742
      transformCursor(pCur->pStreamFileState, pCur);
38✔
743
      SRowBuffPos* pPos = taosArrayGetP(pWinStates, pCur->buffIndex);
38✔
744
      if (pVal) {
38!
745
        *pVal = pPos;
×
746
      }
747
      *pKey = *(SSessionKey*)(pPos->pKey);
38✔
748
      code = TSDB_CODE_SUCCESS;
38✔
749
    } else if (code == TSDB_CODE_SUCCESS && pVal) {
1,682✔
750
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pCur->pStreamFileState);
180✔
751
      if (!pNewPos || !pNewPos->pRowBuff) {
180!
752
        code = TSDB_CODE_OUT_OF_MEMORY;
×
753
        taosMemoryFreeClear(pData);
×
754
        qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code));
×
755
        return code;
×
756
      }
757
      memcpy(pNewPos->pKey, pKey, sizeof(SSessionKey));
180✔
758
      pNewPos->needFree = true;
180✔
759
      pNewPos->beFlushed = true;
180✔
760
      memcpy(pNewPos->pRowBuff, pData, *pVLen);
180✔
761
      (*pVal) = pNewPos;
180✔
762
    }
763
    taosMemoryFreeClear(pData);
1,720!
764
  }
765
  return code;
7,032✔
766
}
767

768
void sessionWinStateMoveToNext(SStreamStateCur* pCur) {
11,920✔
769
  qTrace("move cursor to next");
11,920!
770
  if (pCur && pCur->buffIndex >= 0) {
11,920✔
771
    pCur->buffIndex++;
8,235✔
772
  } else {
773
    streamStateCurNext_rocksdb(pCur);
3,685✔
774
  }
775
}
11,923✔
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,
4,636✔
787
                                     range_cmpr_fn cmpFn) {
788
  SStreamStateCur* pCur = sessionWinStateSeekKeyCurrentPrev(pFileState, key);
4,636✔
789
  SSessionKey      tmpKey = *key;
4,638✔
790
  int32_t          code = sessionWinStateGetKVByCur(pCur, &tmpKey, NULL, NULL);
4,638✔
791
  bool             hasCurrentPrev = true;
4,638✔
792
  if (code == TSDB_CODE_FAILED) {
4,638✔
793
    streamStateFreeCur(pCur);
860✔
794
    pCur = sessionWinStateSeekKeyNext(pFileState, key);
859✔
795
    code = sessionWinStateGetKVByCur(pCur, &tmpKey, NULL, NULL);
860✔
796
    hasCurrentPrev = false;
861✔
797
  }
798

799
  if (code == TSDB_CODE_FAILED) {
4,639✔
800
    code = TSDB_CODE_FAILED;
609✔
801
    goto _end;
609✔
802
  }
803

804
  if (cmpFn(key, &tmpKey) == 0) {
4,030✔
805
    *curKey = tmpKey;
2,900✔
806
    goto _end;
2,900✔
807
  } else if (!hasCurrentPrev) {
1,129✔
808
    code = TSDB_CODE_FAILED;
221✔
809
    goto _end;
221✔
810
  }
811

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

820
_end:
4,638✔
821
  streamStateFreeCur(pCur);
4,638✔
822
  return code;
4,639✔
823
}
824

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

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

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

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

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

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

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

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

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

895
  if (index + 1 < size) {
585✔
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) {
578✔
910
    if (!isDeteled(pFileState, endTs)) {
101!
911
      void*   p = NULL;
101✔
912
      void*   pFileStore = getStateFileStore(pFileState);
101✔
913
      int32_t code_file =
914
          streamStateStateAddIfNotExist_rocksdb(pFileStore, pWinKey, pKeyData, keyDataLen, fn, &p, pVLen);
101✔
915
      if (code_file == TSDB_CODE_SUCCESS || isFlushedState(pFileState, endTs, 0)) {
101✔
916
        (*pVal) = createSessionWinBuff(pFileState, pWinKey, p, pVLen);
92✔
917
        if (!(*pVal)) {
92!
918
          code = TSDB_CODE_OUT_OF_MEMORY;
×
919
          QUERY_CHECK_CODE(code, lino, _end);
92!
920
        }
921

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

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

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

942
  (*pWinCode) = TSDB_CODE_FAILED;
38✔
943

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

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

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

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

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

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

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

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

1011
          goto _end;
54✔
1012
        }
1013
      }
1014
      pWinKey->win.skey = startTs;
8✔
1015
      pWinKey->win.ekey = endTs;
8✔
1016
      (*pVal) = createSessionWinBuff(pFileState, pWinKey, NULL, NULL);
8✔
1017
      taosMemoryFree(pRockVal);
8!
1018
      if (!(*pVal)) {
8!
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);
1,260✔
1024
      QUERY_CHECK_CODE(code, lino, _end);
1,260!
1025

1026
      (*pWinCount) = TSDB_CODE_FAILED;
1,260✔
1027
    }
1028
    goto _end;
1,268✔
1029
  }
1030

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

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

1049
  if (index == -1) {
737✔
1050
    if (!isDeteled(pFileState, endTs) && isFlushedState(pFileState, endTs, 0)) {
14!
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) {
737✔
1077
    pPos = taosArrayGetP(pWinStates, index + 1);
51✔
1078
    (*pVal) = pPos;
51✔
1079
    SSessionKey* pDestWinKey = (SSessionKey*)pPos->pKey;
51✔
1080
    pPos->beUsed = true;
51✔
1081
    pPos->beFlushed = false;
51✔
1082
    *pWinKey = *pDestWinKey;
51✔
1083
    goto _end;
51✔
1084
  }
1085

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

1089
  (*pWinCount) = TSDB_CODE_FAILED;
686✔
1090

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

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

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

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

1123
    int32_t code_file = getCountWinStateFromDisc(pFileStore, pWinKey, &pRockVal, pVLen);
214✔
1124
    if (code_file == TSDB_CODE_SUCCESS && isFlushedState(pFileState, endTs, 0)) {
214!
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);
214✔
1141
      taosMemoryFree(pRockVal);
214!
1142
      QUERY_CHECK_CODE(code, lino, _end);
214!
1143
    }
1144
  } else {
1145
    code = addNewSessionWindow(pFileState, pWinStates, pWinKey, (SRowBuffPos**)pVal);
60✔
1146
    QUERY_CHECK_CODE(code, lino, _end);
60!
1147
  }
1148

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