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

taosdata / TDengine / #4579

25 Jul 2025 01:12AM UTC coverage: 57.921% (+1.7%) from 56.254%
#4579

push

travis-ci

web-flow
enh: refactor some tsdb log info (#32159)

142530 of 316723 branches covered (45.0%)

Branch coverage included in aggregate %.

0 of 13 new or added lines in 2 files covered. (0.0%)

151 existing lines in 26 files now uncovered.

215698 of 301752 relevant lines covered (71.48%)

4618632.12 hits per line

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

78.23
/source/dnode/vnode/src/tsdb/tsdbMemTable.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 "tsdb.h"
17
#include "util/tsimplehash.h"
18

19
#define MEM_MIN_HASH 1024
20
#define SL_MAX_LEVEL 5
21

22
// sizeof(SMemSkipListNode) + sizeof(SMemSkipListNode *) * (l) * 2
23
#define SL_NODE_SIZE(l)               (sizeof(SMemSkipListNode) + ((l) << 4))
24
#define SL_NODE_FORWARD(n, l)         ((n)->forwards[l])
25
#define SL_NODE_BACKWARD(n, l)        ((n)->forwards[(n)->level + (l)])
26
#define SL_GET_NODE_FORWARD(n, l)     ((SMemSkipListNode *)atomic_load_ptr(&SL_NODE_FORWARD(n, l)))
27
#define SL_GET_NODE_BACKWARD(n, l)    ((SMemSkipListNode *)atomic_load_ptr(&SL_NODE_BACKWARD(n, l)))
28
#define SL_SET_NODE_FORWARD(n, l, p)  atomic_store_ptr(&SL_NODE_FORWARD(n, l), p)
29
#define SL_SET_NODE_BACKWARD(n, l, p) atomic_store_ptr(&SL_NODE_BACKWARD(n, l), p)
30

31
#define SL_MOVE_BACKWARD 0x1
32
#define SL_MOVE_FROM_POS 0x2
33

34
static void    tbDataMovePosTo(STbData *pTbData, SMemSkipListNode **pos, STsdbRowKey *pKey, int32_t flags);
35
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData);
36
static int32_t tsdbInsertRowDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
37
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows);
38
static int32_t tsdbInsertColDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
39
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows);
40

41
static int32_t tTbDataCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
2,901,047✔
42
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
2,901,047✔
43
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
2,901,047✔
44
  if (tbData1->suid < tbData2->suid) return -1;
2,901,047✔
45
  if (tbData1->suid > tbData2->suid) return 1;
2,703,478✔
46
  if (tbData1->uid < tbData2->uid) return -1;
2,664,792✔
47
  if (tbData1->uid > tbData2->uid) return 1;
2,366,651!
48
  return 0;
×
49
}
50

51
int32_t tsdbMemTableCreate(STsdb *pTsdb, SMemTable **ppMemTable) {
38,527✔
52
  int32_t    code = 0;
38,527✔
53
  SMemTable *pMemTable = NULL;
38,527✔
54

55
  pMemTable = (SMemTable *)taosMemoryCalloc(1, sizeof(*pMemTable));
38,527!
56
  if (pMemTable == NULL) {
38,538!
57
    code = terrno;
×
58
    goto _err;
×
59
  }
60
  taosInitRWLatch(&pMemTable->latch);
38,538✔
61
  pMemTable->pTsdb = pTsdb;
38,538✔
62
  pMemTable->pPool = pTsdb->pVnode->inUse;
38,538✔
63
  pMemTable->nRef = 1;
38,538✔
64
  pMemTable->minVer = VERSION_MAX;
38,538✔
65
  pMemTable->maxVer = VERSION_MIN;
38,538✔
66
  pMemTable->minKey = TSKEY_MAX;
38,538✔
67
  pMemTable->maxKey = TSKEY_MIN;
38,538✔
68
  pMemTable->nRow = 0;
38,538✔
69
  pMemTable->nDel = 0;
38,538✔
70
  pMemTable->nTbData = 0;
38,538✔
71
  pMemTable->nBucket = MEM_MIN_HASH;
38,538✔
72
  pMemTable->aBucket = (STbData **)taosMemoryCalloc(pMemTable->nBucket, sizeof(STbData *));
38,538!
73
  if (pMemTable->aBucket == NULL) {
38,537!
74
    code = terrno;
×
75
    taosMemoryFree(pMemTable);
×
76
    goto _err;
×
77
  }
78
  vnodeBufPoolRef(pMemTable->pPool);
38,537✔
79
  tRBTreeCreate(pMemTable->tbDataTree, tTbDataCmprFn);
38,539✔
80

81
  *ppMemTable = pMemTable;
38,538✔
82
  return code;
38,538✔
83

84
_err:
×
85
  *ppMemTable = NULL;
×
86
  return code;
×
87
}
88

89
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
38,538✔
90
  if (pMemTable) {
38,538!
91
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
38,538✔
92
    taosMemoryFree(pMemTable->aBucket);
38,539!
93
    taosMemoryFree(pMemTable);
38,538!
94
  }
95
}
38,537✔
96

97
static FORCE_INLINE STbData *tsdbGetTbDataFromMemTableImpl(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
98
  STbData *pTbData = pMemTable->aBucket[TABS(uid) % pMemTable->nBucket];
6,053,405✔
99

100
  while (pTbData) {
6,084,581✔
101
    if (pTbData->uid == uid) break;
4,890,976✔
102
    pTbData = pTbData->next;
31,176✔
103
  }
104

105
  return pTbData;
6,053,405✔
106
}
107

108
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
3,128,436✔
109
  STbData *pTbData;
110

111
  taosRLockLatch(&pMemTable->latch);
3,128,436✔
112
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
3,130,544✔
113
  taosRUnLockLatch(&pMemTable->latch);
3,130,544✔
114

115
  return pTbData;
3,130,721✔
116
}
117

118
int32_t tsdbInsertTableData(STsdb *pTsdb, int64_t version, SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
2,914,542✔
119
  int32_t    code = 0;
2,914,542✔
120
  SMemTable *pMemTable = pTsdb->mem;
2,914,542✔
121
  STbData   *pTbData = NULL;
2,914,542✔
122
  tb_uid_t   suid = pSubmitTbData->suid;
2,914,542✔
123
  tb_uid_t   uid = pSubmitTbData->uid;
2,914,542✔
124

125
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
2,914,542✔
126
    goto _err;
2✔
127
  }
128

129
  // create/get STbData to op
130
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
2,914,540✔
131
  if (code) {
2,914,557!
132
    goto _err;
×
133
  }
134

135
  // do insert impl
136
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
2,914,557✔
137
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
561✔
138
  } else {
139
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
2,913,996✔
140
  }
141
  if (code) goto _err;
2,914,561!
142

143
  // update
144
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
2,914,561✔
145
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
2,914,561✔
146

147
  return code;
2,914,561✔
148

149
_err:
2✔
150
  terrno = code;
2✔
151
  return code;
2✔
152
}
153

154
int32_t tsdbDeleteTableData(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey) {
8,327✔
155
  int32_t    code = 0;
8,327✔
156
  SMemTable *pMemTable = pTsdb->mem;
8,327✔
157
  STbData   *pTbData = NULL;
8,327✔
158
  SVBufPool *pPool = pTsdb->pVnode->inUse;
8,327✔
159

160
  // check if table exists
161
  SMetaInfo info;
162
  code = metaGetInfo(pTsdb->pVnode->pMeta, uid, &info, NULL);
8,327✔
163
  if (code) {
8,327!
164
    code = TSDB_CODE_TDB_TABLE_NOT_EXIST;
×
165
    goto _err;
×
166
  }
167
  if (info.suid != suid) {
8,327!
168
    code = TSDB_CODE_INVALID_MSG;
×
169
    goto _err;
×
170
  }
171

172
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
8,327✔
173
  if (code) {
8,327!
174
    goto _err;
×
175
  }
176

177
  // do delete
178
  SDelData *pDelData = (SDelData *)vnodeBufPoolMalloc(pPool, sizeof(*pDelData));
8,327✔
179
  if (pDelData == NULL) {
8,327!
180
    code = terrno;
×
181
    goto _err;
×
182
  }
183
  pDelData->version = version;
8,327✔
184
  pDelData->sKey = sKey;
8,327✔
185
  pDelData->eKey = eKey;
8,327✔
186
  pDelData->pNext = NULL;
8,327✔
187
  taosWLockLatch(&pTbData->lock);
8,327✔
188
  if (pTbData->pHead == NULL) {
8,327✔
189
    pTbData->pHead = pTbData->pTail = pDelData;
7,101✔
190
  } else {
191
    pTbData->pTail->pNext = pDelData;
1,226✔
192
    pTbData->pTail = pDelData;
1,226✔
193
  }
194
  taosWUnLockLatch(&pTbData->lock);
8,327✔
195

196
  pMemTable->nDel++;
8,327✔
197
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
8,327✔
198
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
8,327✔
199

200
  if (tsdbCacheDel(pTsdb, suid, uid, sKey, eKey) != 0) {
8,327!
201
    tsdbError("vgId:%d, failed to delete cache data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64
×
202
              " eKey:%" PRId64 " at version %" PRId64,
203
              TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version);
204
  }
205

206
  tsdbTrace("vgId:%d, delete data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64 " eKey:%" PRId64
8,327✔
207
            " at version %" PRId64,
208
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version);
209
  return code;
8,327✔
210

211
_err:
×
212
  tsdbError("vgId:%d, failed to delete data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64 " eKey:%" PRId64
×
213
            " at version %" PRId64 " since %s",
214
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version, tstrerror(code));
215
  return code;
×
216
}
217

218
int32_t tsdbTbDataIterCreate(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter **ppIter) {
1,659,019✔
219
  int32_t code = 0;
1,659,019✔
220

221
  (*ppIter) = (STbDataIter *)taosMemoryCalloc(1, sizeof(STbDataIter));
1,659,019!
222
  if ((*ppIter) == NULL) {
1,659,027!
223
    code = terrno;
×
224
    goto _exit;
×
225
  }
226

227
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
1,659,027✔
228

229
_exit:
1,658,242✔
230
  return code;
1,658,242✔
231
}
232

233
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
1,658,100✔
234
  if (pIter) {
1,658,100!
235
    taosMemoryFree(pIter);
1,658,112!
236
  }
237
  return NULL;
1,659,061✔
238
}
239

240
void tsdbTbDataIterOpen(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter *pIter) {
2,220,327✔
241
  SMemSkipListNode *pos[SL_MAX_LEVEL];
242
  SMemSkipListNode *pHead;
243
  SMemSkipListNode *pTail;
244

245
  pHead = pTbData->sl.pHead;
2,220,327✔
246
  pTail = pTbData->sl.pTail;
2,220,327✔
247
  pIter->pTbData = pTbData;
2,220,327✔
248
  pIter->backward = backward;
2,220,327✔
249
  pIter->pRow = NULL;
2,220,327✔
250
  if (pFrom == NULL) {
2,220,327✔
251
    // create from head or tail
252
    if (backward) {
7,696!
253
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
7,696✔
254
    } else {
255
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
×
256
    }
257
  } else {
258
    // create from a key
259
    if (backward) {
2,212,631✔
260
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
130,790✔
261
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
130,682✔
262
    } else {
263
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
2,081,841✔
264
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
2,073,722✔
265
    }
266
  }
267
}
2,209,504✔
268

269
bool tsdbTbDataIterNext(STbDataIter *pIter) {
595,200,260✔
270
  pIter->pRow = NULL;
595,200,260✔
271
  if (pIter->backward) {
595,200,260✔
272
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
14,608,310!
273
      return false;
×
274
    }
275

276
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
14,608,310✔
277
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
14,528,800✔
278
      return false;
120,422✔
279
    }
280
  } else {
281
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
580,591,950!
282
      return false;
×
283
    }
284

285
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
580,591,950✔
286
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
580,398,500✔
287
      return false;
1,637,879✔
288
    }
289
  }
290

291
  return true;
593,168,999✔
292
}
293

294
int64_t tsdbCountTbDataRows(STbData *pTbData) {
×
295
  SMemSkipListNode *pNode = pTbData->sl.pHead;
×
296
  int64_t           rowsNum = 0;
×
297

298
  while (NULL != pNode) {
×
299
    pNode = SL_GET_NODE_FORWARD(pNode, 0);
×
300
    if (pNode == pTbData->sl.pTail) {
×
301
      return rowsNum;
×
302
    }
303

304
    rowsNum++;
×
305
  }
306

307
  return rowsNum;
×
308
}
309

310
void tsdbMemTableCountRows(SMemTable *pMemTable, SSHashObj *pTableMap, int64_t *rowsNum) {
×
311
  taosRLockLatch(&pMemTable->latch);
×
312
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
×
313
    STbData *pTbData = pMemTable->aBucket[i];
×
314
    while (pTbData) {
×
315
      void *p = tSimpleHashGet(pTableMap, &pTbData->uid, sizeof(pTbData->uid));
×
316
      if (p == NULL) {
×
317
        pTbData = pTbData->next;
×
318
        continue;
×
319
      }
320

321
      *rowsNum += tsdbCountTbDataRows(pTbData);
×
322
      pTbData = pTbData->next;
×
323
    }
324
  }
325
  taosRUnLockLatch(&pMemTable->latch);
×
326
}
×
327

328
typedef int32_t (*__tsdb_cache_update)(SMemTable *imem, int64_t suid, int64_t uid);
329

330
int32_t tsdbMemTableSaveToCache(SMemTable *pMemTable, void *func) {
158✔
331
  int32_t             code = 0;
158✔
332
  __tsdb_cache_update cb = (__tsdb_cache_update)func;
158✔
333

334
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
161,950✔
335
    STbData *pTbData = pMemTable->aBucket[i];
161,792✔
336
    while (pTbData) {
165,067✔
337
      code = (*cb)(pMemTable, pTbData->suid, pTbData->uid);
3,275✔
338
      if (code) {
3,275!
339
        TAOS_RETURN(code);
×
340
      }
341

342
      pTbData = pTbData->next;
3,275✔
343
    }
344
  }
345

346
  return code;
158✔
347
}
348

349
static int32_t tsdbMemTableRehash(SMemTable *pMemTable) {
36✔
350
  int32_t code = 0;
36✔
351

352
  int32_t   nBucket = pMemTable->nBucket * 2;
36✔
353
  STbData **aBucket = (STbData **)taosMemoryCalloc(nBucket, sizeof(STbData *));
36!
354
  if (aBucket == NULL) {
36!
355
    code = terrno;
×
356
    goto _exit;
×
357
  }
358

359
  for (int32_t iBucket = 0; iBucket < pMemTable->nBucket; iBucket++) {
119,844✔
360
    STbData *pTbData = pMemTable->aBucket[iBucket];
119,808✔
361

362
    while (pTbData) {
239,616✔
363
      STbData *pNext = pTbData->next;
119,808✔
364

365
      int32_t idx = TABS(pTbData->uid) % nBucket;
119,808✔
366
      pTbData->next = aBucket[idx];
119,808✔
367
      aBucket[idx] = pTbData;
119,808✔
368

369
      pTbData = pNext;
119,808✔
370
    }
371
  }
372

373
  taosMemoryFree(pMemTable->aBucket);
36!
374
  pMemTable->nBucket = nBucket;
36✔
375
  pMemTable->aBucket = aBucket;
36✔
376

377
_exit:
36✔
378
  return code;
36✔
379
}
380

381
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
2,922,861✔
382
  int32_t code = 0;
2,922,861✔
383

384
  // get
385
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
2,922,861✔
386
  if (pTbData) goto _exit;
2,922,861✔
387

388
  // create
389
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
186,144✔
390
  int8_t     maxLevel = pMemTable->pTsdb->pVnode->config.tsdbCfg.slLevel;
186,144✔
391

392
  pTbData = vnodeBufPoolMallocAligned(pPool, sizeof(*pTbData) + SL_NODE_SIZE(maxLevel) * 2);
186,144✔
393
  if (pTbData == NULL) {
186,165!
394
    code = terrno;
×
395
    goto _exit;
×
396
  }
397
  pTbData->suid = suid;
186,165✔
398
  pTbData->uid = uid;
186,165✔
399
  pTbData->minKey = TSKEY_MAX;
186,165✔
400
  pTbData->maxKey = TSKEY_MIN;
186,165✔
401
  pTbData->pHead = NULL;
186,165✔
402
  pTbData->pTail = NULL;
186,165✔
403
  pTbData->sl.seed = taosRand();
186,165✔
404
  pTbData->sl.size = 0;
186,170✔
405
  pTbData->sl.maxLevel = maxLevel;
186,170✔
406
  pTbData->sl.level = 0;
186,170✔
407
  pTbData->sl.pHead = (SMemSkipListNode *)&pTbData[1];
186,170✔
408
  pTbData->sl.pTail = (SMemSkipListNode *)POINTER_SHIFT(pTbData->sl.pHead, SL_NODE_SIZE(maxLevel));
186,170✔
409
  pTbData->sl.pHead->level = maxLevel;
186,170✔
410
  pTbData->sl.pTail->level = maxLevel;
186,170✔
411
  for (int8_t iLevel = 0; iLevel < maxLevel; iLevel++) {
1,117,000✔
412
    SL_NODE_FORWARD(pTbData->sl.pHead, iLevel) = pTbData->sl.pTail;
930,830✔
413
    SL_NODE_BACKWARD(pTbData->sl.pTail, iLevel) = pTbData->sl.pHead;
930,830✔
414

415
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
930,830✔
416
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
930,830✔
417
  }
418
  taosInitRWLatch(&pTbData->lock);
186,170✔
419

420
  taosWLockLatch(&pMemTable->latch);
186,166✔
421

422
  if (pMemTable->nTbData >= pMemTable->nBucket) {
186,168✔
423
    code = tsdbMemTableRehash(pMemTable);
36✔
424
    if (code) {
36!
425
      taosWUnLockLatch(&pMemTable->latch);
×
426
      goto _exit;
×
427
    }
428
  }
429

430
  int32_t idx = TABS(uid) % pMemTable->nBucket;
186,168✔
431
  pTbData->next = pMemTable->aBucket[idx];
186,168✔
432
  pMemTable->aBucket[idx] = pTbData;
186,168✔
433
  pMemTable->nTbData++;
186,168✔
434

435
  if (tRBTreePut(pMemTable->tbDataTree, pTbData->rbtn) == NULL) {
186,168!
436
    taosWUnLockLatch(&pMemTable->latch);
×
437
    code = TSDB_CODE_INTERNAL_ERROR;
×
438
    goto _exit;
×
439
  }
440

441
  taosWUnLockLatch(&pMemTable->latch);
186,159✔
442

443
_exit:
2,922,885✔
444
  if (code) {
2,922,885!
445
    *ppTbData = NULL;
×
446
  } else {
447
    *ppTbData = pTbData;
2,922,885✔
448
  }
449
  return code;
2,922,885✔
450
}
451

452
static void tbDataMovePosTo(STbData *pTbData, SMemSkipListNode **pos, STsdbRowKey *pKey, int32_t flags) {
9,678,819✔
453
  SMemSkipListNode *px;
454
  SMemSkipListNode *pn;
455
  STsdbRowKey       tKey;
456
  int32_t           backward = flags & SL_MOVE_BACKWARD;
9,678,819✔
457
  int32_t           fromPos = flags & SL_MOVE_FROM_POS;
9,678,819✔
458

459
  if (backward) {
9,678,819✔
460
    px = pTbData->sl.pTail;
3,045,349✔
461

462
    if (!fromPos) {
3,045,349!
463
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
6,068,599✔
464
        pos[iLevel] = px;
3,023,247✔
465
      }
466
    }
467

468
    if (pTbData->sl.level) {
3,045,349✔
469
      if (fromPos) px = pos[pTbData->sl.level - 1];
2,859,597!
470

471
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
15,062,398✔
472
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
12,202,945✔
473
        while (pn != pTbData->sl.pHead) {
13,353,395✔
474
          tsdbRowGetKey(&pn->row, &tKey);
13,272,785✔
475

476
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
13,272,851✔
477
          if (c <= 0) {
13,272,976✔
478
            break;
12,122,191✔
479
          } else {
480
            px = pn;
1,150,785✔
481
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
1,150,785✔
482
          }
483
        }
484

485
        pos[iLevel] = px;
12,202,801✔
486
      }
487
    }
488
  } else {
489
    px = pTbData->sl.pHead;
6,633,470✔
490

491
    if (!fromPos) {
6,633,470✔
492
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
7,048,936✔
493
        pos[iLevel] = px;
4,966,718✔
494
      }
495
    }
496

497
    if (pTbData->sl.level) {
6,633,470✔
498
      if (fromPos) px = pos[pTbData->sl.level - 1];
6,631,271✔
499

500
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
34,628,611✔
501
        pn = SL_GET_NODE_FORWARD(px, iLevel);
28,008,744✔
502
        while (pn != pTbData->sl.pTail) {
85,329,331✔
503
          tsdbRowGetKey(&pn->row, &tKey);
84,721,291✔
504

505
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
84,725,543✔
506
          if (c >= 0) {
84,775,934✔
507
            break;
27,389,300✔
508
          } else {
509
            px = pn;
57,386,634✔
510
            pn = SL_GET_NODE_FORWARD(px, iLevel);
57,386,634✔
511
          }
512
        }
513

514
        pos[iLevel] = px;
27,997,340✔
515
      }
516
    }
517
  }
518
}
9,667,271✔
519

520
static FORCE_INLINE int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl) {
521
  int8_t level = 1;
144,426,179✔
522
  int8_t tlevel = TMIN(pSl->maxLevel, pSl->level + 1);
144,426,179✔
523

524
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
192,295,153✔
525
    level++;
47,868,974✔
526
  }
527

528
  return level;
144,157,820✔
529
}
530
static int32_t tbDataDoPut(SMemTable *pMemTable, STbData *pTbData, SMemSkipListNode **pos, TSDBROW *pRow,
144,426,179✔
531
                           int8_t forward) {
532
  int32_t           code = 0;
144,426,179✔
533
  int8_t            level;
534
  SMemSkipListNode *pNode = NULL;
144,426,179✔
535
  SVBufPool        *pPool = pMemTable->pTsdb->pVnode->inUse;
144,426,179✔
536
  int64_t           nSize;
537

538
  // create node
539
  level = tsdbMemSkipListRandLevel(&pTbData->sl);
144,426,179✔
540
  nSize = SL_NODE_SIZE(level);
144,157,820✔
541
  if (pRow->type == TSDBROW_ROW_FMT) {
144,157,820!
542
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize + pRow->pTSRow->len);
144,196,097✔
UNCOV
543
  } else if (pRow->type == TSDBROW_COL_FMT) {
×
544
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize);
43,160✔
545
  }
546
  if (pNode == NULL) {
144,190,913!
547
    code = terrno;
×
548
    goto _exit;
×
549
  }
550

551
  pNode->level = level;
144,190,913✔
552
  pNode->row = *pRow;
144,190,913✔
553
  if (pRow->type == TSDBROW_ROW_FMT) {
144,190,913✔
554
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
144,160,877✔
555
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
144,160,877✔
556
  }
557

558
  // set node
559
  if (forward) {
144,190,913✔
560
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
329,698,203✔
561
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
188,277,054✔
562
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
188,277,054✔
563
    }
564
  } else {
565
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
6,576,423✔
566
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
3,806,659✔
567
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
3,806,659✔
568
    }
569
  }
570

571
  // set forward and backward
572
  if (forward) {
144,190,913✔
573
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
329,937,674✔
574
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
188,359,805✔
575

576
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
188,359,805✔
577
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
188,555,556✔
578

579
      pos[iLevel] = pNode;
188,523,949✔
580
    }
581
  } else {
582
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
6,724,923✔
583
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
3,806,673✔
584

585
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
3,806,673✔
586
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
3,806,689✔
587

588
      pos[iLevel] = pNode;
3,947,735✔
589
    }
590
  }
591

592
  pTbData->sl.size++;
144,496,119✔
593
  if (pTbData->sl.level < pNode->level) {
144,496,119✔
594
    pTbData->sl.level = pNode->level;
411,892✔
595
  }
596

597
_exit:
144,084,227✔
598
  return code;
144,496,119✔
599
}
600

601
static int32_t tsdbInsertColDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
561✔
602
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
603
  int32_t code = 0;
561✔
604

605
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
561✔
606
  int32_t    nColData = TARRAY_SIZE(pSubmitTbData->aCol);
561✔
607
  SColData  *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
561✔
608

609
  // copy and construct block data
610
  SBlockData *pBlockData = vnodeBufPoolMalloc(pPool, sizeof(*pBlockData));
561✔
611
  if (pBlockData == NULL) {
561!
612
    code = terrno;
×
613
    goto _exit;
×
614
  }
615

616
  pBlockData->suid = pTbData->suid;
561✔
617
  pBlockData->uid = pTbData->uid;
561✔
618
  pBlockData->nRow = aColData[0].nVal;
561✔
619
  pBlockData->aUid = NULL;
561✔
620
  pBlockData->aVersion = vnodeBufPoolMalloc(pPool, aColData[0].nData);
561✔
621
  if (pBlockData->aVersion == NULL) {
560!
622
    code = terrno;
×
623
    goto _exit;
×
624
  }
625
  for (int32_t i = 0; i < pBlockData->nRow; i++) {  // todo: here can be optimized
43,925✔
626
    pBlockData->aVersion[i] = version;
43,365✔
627
  }
628

629
  pBlockData->aTSKEY = vnodeBufPoolMalloc(pPool, aColData[0].nData);
560✔
630
  if (pBlockData->aTSKEY == NULL) {
560!
631
    code = terrno;
×
632
    goto _exit;
×
633
  }
634
  memcpy(pBlockData->aTSKEY, aColData[0].pData, aColData[0].nData);
560✔
635

636
  pBlockData->nColData = nColData - 1;
560✔
637
  pBlockData->aColData = vnodeBufPoolMalloc(pPool, sizeof(SColData) * pBlockData->nColData);
560✔
638
  if (pBlockData->aColData == NULL) {
560!
639
    code = terrno;
×
640
    goto _exit;
×
641
  }
642

643
  for (int32_t iColData = 0; iColData < pBlockData->nColData; ++iColData) {
3,896✔
644
    code = tColDataCopy(&aColData[iColData + 1], &pBlockData->aColData[iColData], (xMallocFn)vnodeBufPoolMalloc, pPool);
3,336✔
645
    if (code) goto _exit;
3,336!
646
  }
647

648
  // loop to add each row to the skiplist
649
  SMemSkipListNode *pos[SL_MAX_LEVEL];
650
  TSDBROW           tRow = tsdbRowFromBlockData(pBlockData, 0);
560✔
651
  STsdbRowKey       key;
652

653
  // first row
654
  tsdbRowGetKey(&tRow, &key);
560✔
655
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
560✔
656
  if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0))) goto _exit;
559!
657
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
560✔
658

659
  // remain row
660
  ++tRow.iRow;
560✔
661
  if (tRow.iRow < pBlockData->nRow) {
560✔
662
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
2,427✔
663
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
1,938✔
664
    }
665

666
    while (tRow.iRow < pBlockData->nRow) {
43,256✔
667
      tsdbRowGetKey(&tRow, &key);
42,761✔
668

669
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
42,707✔
670
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
4,737✔
671
      }
672

673
      if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1))) goto _exit;
42,658!
674

675
      ++tRow.iRow;
42,767✔
676
    }
677
  }
678

679
  if (key.key.ts >= pTbData->maxKey) {
566✔
680
    pTbData->maxKey = key.key.ts;
539✔
681
  }
682

683
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
566!
684
    if (tsdbCacheColFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, pBlockData) != 0) {
×
685
      tsdbError("vgId:%d, failed to update cache data from table suid:%" PRId64 " uid:%" PRId64 " at version %" PRId64,
×
686
                TD_VID(pMemTable->pTsdb->pVnode), pTbData->suid, pTbData->uid, version);
687
    }
688
  }
689

690
  // SMemTable
691
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
559✔
692
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
559✔
693
  pMemTable->nRow += pBlockData->nRow;
559✔
694

695
  if (affectedRows) *affectedRows = pBlockData->nRow;
559!
696

697
_exit:
×
698
  return code;
559✔
699
}
700

701
static int32_t tsdbInsertRowDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
2,913,992✔
702
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
703
  int32_t code = 0;
2,913,992✔
704

705
  int32_t           nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
2,913,992✔
706
  SRow            **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
2,913,992✔
707
  STsdbRowKey       key;
708
  SMemSkipListNode *pos[SL_MAX_LEVEL];
709
  TSDBROW           tRow = {.type = TSDBROW_ROW_FMT, .version = version};
2,913,992✔
710
  int32_t           iRow = 0;
2,913,992✔
711

712
  // backward put first data
713
  tRow.pTSRow = aRow[iRow++];
2,913,992✔
714
  tsdbRowGetKey(&tRow, &key);
2,913,992✔
715
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
2,913,994✔
716
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
2,913,981✔
717
  if (code) goto _exit;
2,913,997!
718

719
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
2,913,997✔
720

721
  // forward put rest data
722
  if (iRow < nRow) {
2,913,997✔
723
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
3,205,010✔
724
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
2,522,438✔
725
    }
726

727
    while (iRow < nRow) {
142,213,008✔
728
      tRow.pTSRow = aRow[iRow];
141,534,451✔
729
      tsdbRowGetKey(&tRow, &key);
141,534,451✔
730

731
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
141,506,678✔
732
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
4,545,826✔
733
      }
734

735
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
141,506,614✔
736
      if (code) goto _exit;
141,530,436!
737

738
      iRow++;
141,530,436✔
739
    }
740
  }
741

742
  if (key.key.ts >= pTbData->maxKey) {
2,909,982✔
743
    pTbData->maxKey = key.key.ts;
2,849,846✔
744
  }
745
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
2,909,982!
746
    TAOS_UNUSED(tsdbCacheRowFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, version, nRow, aRow));
×
747
  }
748

749
  // SMemTable
750
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
2,914,002✔
751
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
2,914,002✔
752
  pMemTable->nRow += nRow;
2,914,002✔
753

754
  if (affectedRows) *affectedRows = nRow;
2,914,002!
755

756
_exit:
×
757
  return code;
2,914,002✔
758
}
759

760
int32_t tsdbGetNRowsInTbData(STbData *pTbData) { return pTbData->sl.size; }
25✔
761

762
int32_t tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode) {
833,109✔
763
  int32_t code = 0;
833,109✔
764

765
  int32_t nRef = atomic_fetch_add_32(&pMemTable->nRef, 1);
833,109✔
766
  if (nRef <= 0) {
833,407!
767
    tsdbError("vgId:%d, memtable ref count is invalid, ref:%d", TD_VID(pMemTable->pTsdb->pVnode), nRef);
×
768
  }
769

770
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
833,407✔
771

772
_exit:
833,374✔
773
  return code;
833,374✔
774
}
775

776
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
856,912✔
777
  if (pNode) {
856,912✔
778
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
833,425✔
779
  }
780

781
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
856,916✔
782
    tsdbMemTableDestroy(pMemTable, proactive);
23,493✔
783
  }
784
}
856,924✔
785

786
static FORCE_INLINE int32_t tbDataPCmprFn(const void *p1, const void *p2) {
787
  STbData *pTbData1 = *(STbData **)p1;
788
  STbData *pTbData2 = *(STbData **)p2;
789

790
  if (pTbData1->suid < pTbData2->suid) {
791
    return -1;
792
  } else if (pTbData1->suid > pTbData2->suid) {
793
    return 1;
794
  }
795

796
  if (pTbData1->uid < pTbData2->uid) {
797
    return -1;
798
  } else if (pTbData1->uid > pTbData2->uid) {
799
    return 1;
800
  }
801

802
  return 0;
803
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc