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

taosdata / TDengine / #3842

07 Apr 2025 11:21AM UTC coverage: 62.696% (-0.3%) from 63.027%
#3842

push

travis-ci

web-flow
merge: from main to 3.0 branch (#30679)

154855 of 315075 branches covered (49.15%)

Branch coverage included in aggregate %.

6 of 8 new or added lines in 5 files covered. (75.0%)

2309 existing lines in 130 files now uncovered.

240176 of 314995 relevant lines covered (76.25%)

19119980.29 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,824,161✔
42
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
2,824,161✔
43
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
2,824,161✔
44
  if (tbData1->suid < tbData2->suid) return -1;
2,824,161✔
45
  if (tbData1->suid > tbData2->suid) return 1;
2,737,931✔
46
  if (tbData1->uid < tbData2->uid) return -1;
2,663,987✔
47
  if (tbData1->uid > tbData2->uid) return 1;
2,033,743!
48
  return 0;
×
49
}
50

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

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

81
  *ppMemTable = pMemTable;
40,396✔
82
  return code;
40,396✔
83

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

89
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
40,396✔
90
  if (pMemTable) {
40,396!
91
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
40,396✔
92
    taosMemoryFree(pMemTable->aBucket);
40,397!
93
    taosMemoryFree(pMemTable);
40,397!
94
  }
95
}
40,397✔
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];
28,254,406✔
99

100
  while (pTbData) {
28,316,848✔
101
    if (pTbData->uid == uid) break;
18,416,692✔
102
    pTbData = pTbData->next;
62,442✔
103
  }
104

105
  return pTbData;
28,254,406✔
106
}
107

108
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
15,940,913✔
109
  STbData *pTbData;
110

111
  taosRLockLatch(&pMemTable->latch);
15,940,913✔
112
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
15,953,496✔
113
  taosRUnLockLatch(&pMemTable->latch);
15,953,496✔
114

115
  return pTbData;
15,955,066✔
116
}
117

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

125
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
12,189,056!
126
    goto _err;
×
127
  }
128

129
  // create/get STbData to op
130
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
12,189,056✔
131
  if (code) {
12,188,897!
132
    goto _err;
×
133
  }
134

135
  // do insert impl
136
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,188,897✔
137
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
13,731✔
138
  } else {
139
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
12,175,166✔
140
  }
141
  if (code) goto _err;
12,191,308!
142

143
  // update
144
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
12,191,308✔
145
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
12,191,308✔
146

147
  return code;
12,191,308✔
148

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

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

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

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

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

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

200
  if (tsdbCacheDel(pTsdb, suid, uid, sKey, eKey) != 0) {
112,014!
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
112,013✔
207
            " at version %" PRId64,
208
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version);
209
  return code;
112,009✔
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) {
4,535,916✔
219
  int32_t code = 0;
4,535,916✔
220

221
  (*ppIter) = (STbDataIter *)taosMemoryCalloc(1, sizeof(STbDataIter));
4,535,916!
222
  if ((*ppIter) == NULL) {
4,536,163!
223
    code = terrno;
×
224
    goto _exit;
×
225
  }
226

227
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
4,536,163✔
228

229
_exit:
4,534,351✔
230
  return code;
4,534,351✔
231
}
232

233
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
4,535,857✔
234
  if (pIter) {
4,535,857!
235
    taosMemoryFree(pIter);
4,535,983!
236
  }
237
  return NULL;
4,537,184✔
238
}
239

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

245
  pHead = pTbData->sl.pHead;
29,110,570✔
246
  pTail = pTbData->sl.pTail;
29,110,570✔
247
  pIter->pTbData = pTbData;
29,110,570✔
248
  pIter->backward = backward;
29,110,570✔
249
  pIter->pRow = NULL;
29,110,570✔
250
  if (pFrom == NULL) {
29,110,570✔
251
    // create from head or tail
252
    if (backward) {
26,400!
253
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
26,401✔
254
    } else {
255
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
×
256
    }
257
  } else {
258
    // create from a key
259
    if (backward) {
29,084,170✔
260
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
865,843✔
261
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
865,883✔
262
    } else {
263
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
28,218,327✔
264
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
28,212,443✔
265
    }
266
  }
267
}
29,102,796✔
268

269
bool tsdbTbDataIterNext(STbDataIter *pIter) {
2,147,483,647✔
270
  pIter->pRow = NULL;
2,147,483,647✔
271
  if (pIter->backward) {
2,147,483,647✔
272
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
133,104,038!
273
      return false;
×
274
    }
275

276
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
133,104,038✔
277
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
132,912,817✔
278
      return false;
317,436✔
279
    }
280
  } else {
281
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
2,090,242,110!
282
      return false;
×
283
    }
284

285
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
2,090,242,110✔
286
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
2,089,683,378✔
287
      return false;
3,376,178✔
288
    }
289
  }
290

291
  return true;
2,147,483,647✔
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) {
508✔
331
  int32_t             code = 0;
508✔
332
  __tsdb_cache_update cb = (__tsdb_cache_update)func;
508✔
333

334
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
520,746✔
335
    STbData *pTbData = pMemTable->aBucket[i];
520,239✔
336
    while (pTbData) {
534,131✔
337
      code = (*cb)(pMemTable, pTbData->suid, pTbData->uid);
13,893✔
338
      if (code) {
13,892!
UNCOV
339
        TAOS_RETURN(code);
×
340
      }
341

342
      pTbData = pTbData->next;
13,892✔
343
    }
344
  }
345

346
  return code;
507✔
347
}
348

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

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

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

362
    while (pTbData) {
102,400✔
363
      STbData *pNext = pTbData->next;
51,200✔
364

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

369
      pTbData = pNext;
51,200✔
370
    }
371
  }
372

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

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

381
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
12,300,910✔
382
  int32_t code = 0;
12,300,910✔
383

384
  // get
385
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
12,300,910✔
386
  if (pTbData) goto _exit;
12,300,910✔
387

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

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

415
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
1,231,186✔
416
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
1,231,186✔
417
  }
418
  taosInitRWLatch(&pTbData->lock);
246,247✔
419

420
  taosWLockLatch(&pMemTable->latch);
246,240✔
421

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

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

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

441
  taosWUnLockLatch(&pMemTable->latch);
246,237✔
442

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

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

459
  if (backward) {
61,253,500✔
460
    px = pTbData->sl.pTail;
13,054,347✔
461

462
    if (!fromPos) {
13,054,347!
463
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
18,867,424✔
464
        pos[iLevel] = px;
5,812,780✔
465
      }
466
    }
467

468
    if (pTbData->sl.level) {
13,054,347✔
469
      if (fromPos) px = pos[pTbData->sl.level - 1];
12,808,835!
470

471
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
72,115,793✔
472
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
59,320,360✔
473
        while (pn != pTbData->sl.pHead) {
69,019,272✔
474
          tsdbRowGetKey(&pn->row, &tKey);
68,027,239✔
475

476
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
68,026,434✔
477
          if (c <= 0) {
68,024,657✔
478
            break;
58,314,925✔
479
          } else {
480
            px = pn;
9,709,732✔
481
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
9,709,732✔
482
          }
483
        }
484

485
        pos[iLevel] = px;
59,306,958✔
486
      }
487
    }
488
  } else {
489
    px = pTbData->sl.pHead;
48,199,153✔
490

491
    if (!fromPos) {
48,199,153✔
492
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
43,689,299✔
493
        pos[iLevel] = px;
15,470,191✔
494
      }
495
    }
496

497
    if (pTbData->sl.level) {
48,199,153✔
498
      if (fromPos) px = pos[pTbData->sl.level - 1];
48,194,433✔
499

500
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
273,263,992✔
501
        pn = SL_GET_NODE_FORWARD(px, iLevel);
225,082,741✔
502
        while (pn != pTbData->sl.pTail) {
1,038,994,309✔
503
          tsdbRowGetKey(&pn->row, &tKey);
1,032,263,287✔
504

505
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
1,033,095,044✔
506
          if (c >= 0) {
1,033,626,107✔
507
            break;
218,338,537✔
508
          } else {
509
            px = pn;
815,287,570✔
510
            pn = SL_GET_NODE_FORWARD(px, iLevel);
815,287,570✔
511
          }
512
        }
513

514
        pos[iLevel] = px;
225,069,559✔
515
      }
516
    }
517
  }
518
}
61,226,916✔
519

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

524
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
1,319,584,320✔
525
    level++;
330,032,565✔
526
  }
527

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

538
  // create node
539
  level = tsdbMemSkipListRandLevel(&pTbData->sl);
989,551,755✔
540
  nSize = SL_NODE_SIZE(level);
987,748,633✔
541
  if (pRow->type == TSDBROW_ROW_FMT) {
987,748,633✔
542
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize + pRow->pTSRow->len);
976,102,838✔
543
  } else if (pRow->type == TSDBROW_COL_FMT) {
11,645,795!
544
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize);
13,295,913✔
545
  }
546
  if (pNode == NULL) {
989,375,233!
547
    code = terrno;
×
548
    goto _exit;
×
549
  }
550

551
  pNode->level = level;
989,375,233✔
552
  pNode->row = *pRow;
989,375,233✔
553
  if (pRow->type == TSDBROW_ROW_FMT) {
989,375,233✔
554
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
976,086,756✔
555
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
976,086,756✔
556
  }
557

558
  // set node
559
  if (forward) {
989,375,233✔
560
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
2,147,483,647✔
561
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
1,299,883,449✔
562
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
1,299,883,449✔
563
    }
564
  } else {
565
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
28,126,824✔
566
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
16,123,507✔
567
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
16,123,507✔
568
    }
569
  }
570

571
  // set forward and backward
572
  if (forward) {
989,375,233✔
573
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
2,147,483,647✔
574
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
1,299,932,483✔
575

576
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
1,299,932,483✔
577
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
1,305,848,147✔
578

579
      pos[iLevel] = pNode;
1,305,538,573✔
580
    }
581
  } else {
582
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
27,527,674✔
583
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
16,126,090✔
584

585
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
16,126,090✔
586
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
16,142,863✔
587

588
      pos[iLevel] = pNode;
14,492,203✔
589
    }
590
  }
591

592
  pTbData->sl.size++;
993,347,436✔
593
  if (pTbData->sl.level < pNode->level) {
993,347,436✔
594
    pTbData->sl.level = pNode->level;
947,357✔
595
  }
596

597
_exit:
992,400,079✔
598
  return code;
993,347,436✔
599
}
600

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

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

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

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

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

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

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

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

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

659
  // remain row
660
  ++tRow.iRow;
13,729✔
661
  if (tRow.iRow < pBlockData->nRow) {
13,729✔
662
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
67,324✔
663
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
53,685✔
664
    }
665

666
    while (tRow.iRow < pBlockData->nRow) {
16,102,064✔
667
      tsdbRowGetKey(&tRow, &key);
15,848,015✔
668

669
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
14,494,935✔
670
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
3✔
671
      }
672

673
      if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1))) goto _exit;
14,494,935!
674

675
      ++tRow.iRow;
16,088,425✔
676
    }
677
  }
678

679
  if (key.key.ts >= pTbData->maxKey) {
254,139✔
680
    pTbData->maxKey = key.key.ts;
13,723✔
681
  }
682

683
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
254,139!
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);
13,726✔
692
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
13,726✔
693
  pMemTable->nRow += pBlockData->nRow;
13,726✔
694

695
  if (affectedRows) *affectedRows = pBlockData->nRow;
13,726!
696

697
_exit:
×
698
  return code;
13,726✔
699
}
700

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

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

712
  // backward put first data
713
  tRow.pTSRow = aRow[iRow++];
12,174,873✔
714
  tsdbRowGetKey(&tRow, &key);
12,174,873✔
715
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
12,174,623✔
716
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
12,157,674✔
717
  if (code) goto _exit;
12,179,185!
718

719
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
12,179,185✔
720

721
  // forward put rest data
722
  if (iRow < nRow) {
12,179,185✔
723
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
5,425,961✔
724
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
4,275,642✔
725
    }
726

727
    while (iRow < nRow) {
965,291,626✔
728
      tRow.pTSRow = aRow[iRow];
964,157,074✔
729
      tsdbRowGetKey(&tRow, &key);
964,157,074✔
730

731
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
964,082,190✔
732
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
19,980,674✔
733
      }
734

735
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
964,080,961✔
736
      if (code) goto _exit;
964,141,307!
737

738
      iRow++;
964,141,307✔
739
    }
740
  }
741

742
  if (key.key.ts >= pTbData->maxKey) {
12,163,418✔
743
    pTbData->maxKey = key.key.ts;
12,022,072✔
744
  }
745
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
12,163,418!
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);
12,178,254✔
751
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
12,178,254✔
752
  pMemTable->nRow += nRow;
12,178,254✔
753

754
  if (affectedRows) *affectedRows = nRow;
12,178,254!
755

756
_exit:
×
757
  return code;
12,178,254✔
758
}
759

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

762
int32_t tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode) {
4,419,272✔
763
  int32_t code = 0;
4,419,272✔
764

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

770
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
4,423,888✔
771

772
_exit:
4,423,669✔
773
  return code;
4,423,669✔
774
}
775

776
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
4,451,623✔
777
  if (pNode) {
4,451,623✔
778
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
4,423,899✔
779
  }
780

781
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
4,452,049✔
782
    tsdbMemTableDestroy(pMemTable, proactive);
27,913✔
783
  }
784
}
4,452,275✔
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 · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc