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

taosdata / TDengine / #3610

12 Feb 2025 09:54AM UTC coverage: 54.713% (-8.4%) from 63.066%
#3610

push

travis-ci

web-flow
Merge pull request #29745 from taosdata/fix/TD33664-3.0

fix: --version show information check for 3.0

120957 of 286549 branches covered (42.21%)

Branch coverage included in aggregate %.

190849 of 283342 relevant lines covered (67.36%)

4969786.97 hits per line

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

73.03
/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) {
717,241✔
42
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
717,241✔
43
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
717,241✔
44
  if (tbData1->suid < tbData2->suid) return -1;
717,241✔
45
  if (tbData1->suid > tbData2->suid) return 1;
668,015✔
46
  if (tbData1->uid < tbData2->uid) return -1;
643,603✔
47
  if (tbData1->uid > tbData2->uid) return 1;
528,427!
48
  return 0;
×
49
}
50

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

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

81
  *ppMemTable = pMemTable;
25,079✔
82
  return code;
25,079✔
83

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

89
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
25,106✔
90
  if (pMemTable) {
25,106✔
91
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
25,087✔
92
    taosMemoryFree(pMemTable->aBucket);
25,089!
93
    taosMemoryFree(pMemTable);
25,089!
94
  }
95
}
25,108✔
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];
4,656,438✔
99

100
  while (pTbData) {
4,661,769✔
101
    if (pTbData->uid == uid) break;
3,566,785✔
102
    pTbData = pTbData->next;
5,331✔
103
  }
104

105
  return pTbData;
4,656,438✔
106
}
107

108
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
2,630,084✔
109
  STbData *pTbData;
110

111
  taosRLockLatch(&pMemTable->latch);
2,630,084✔
112
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
2,636,212✔
113
  taosRUnLockLatch(&pMemTable->latch);
2,636,212✔
114

115
  return pTbData;
2,636,009✔
116
}
117

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

125
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
2,015,304!
126
    goto _err;
×
127
  }
128

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

135
  // do insert impl
136
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
2,013,064✔
137
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
128✔
138
  } else {
139
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
2,012,936✔
140
  }
141
  if (code) goto _err;
2,017,672!
142

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

147
  return code;
2,017,672✔
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) {
5,483✔
155
  int32_t    code = 0;
5,483✔
156
  SMemTable *pMemTable = pTsdb->mem;
5,483✔
157
  STbData   *pTbData = NULL;
5,483✔
158
  SVBufPool *pPool = pTsdb->pVnode->inUse;
5,483✔
159

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

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

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

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

200
  if (tsdbCacheDel(pTsdb, suid, uid, sKey, eKey) != 0) {
5,483!
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
5,483✔
207
            " at version %" PRId64,
208
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version);
209
  return code;
5,483✔
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,298,407✔
219
  int32_t code = 0;
1,298,407✔
220

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

227
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
1,298,842✔
228

229
_exit:
1,297,392✔
230
  return code;
1,297,392✔
231
}
232

233
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
1,297,485✔
234
  if (pIter) {
1,297,485!
235
    taosMemoryFree(pIter);
1,297,685!
236
  }
237
  return NULL;
1,298,818✔
238
}
239

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

245
  pHead = pTbData->sl.pHead;
2,062,450✔
246
  pTail = pTbData->sl.pTail;
2,062,450✔
247
  pIter->pTbData = pTbData;
2,062,450✔
248
  pIter->backward = backward;
2,062,450✔
249
  pIter->pRow = NULL;
2,062,450✔
250
  if (pFrom == NULL) {
2,062,450✔
251
    // create from head or tail
252
    if (backward) {
3,847✔
253
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
3,846✔
254
    } else {
255
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
1✔
256
    }
257
  } else {
258
    // create from a key
259
    if (backward) {
2,058,603✔
260
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
100,215✔
261
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
100,288✔
262
    } else {
263
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
1,958,388✔
264
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
1,957,152✔
265
    }
266
  }
267
}
2,060,875✔
268

269
bool tsdbTbDataIterNext(STbDataIter *pIter) {
689,444,050✔
270
  pIter->pRow = NULL;
689,444,050✔
271
  if (pIter->backward) {
689,444,050✔
272
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
37,711,761!
273
      return false;
×
274
    }
275

276
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
37,711,761✔
277
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
37,274,236✔
278
      return false;
89,564✔
279
    }
280
  } else {
281
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
651,732,289!
282
      return false;
×
283
    }
284

285
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
651,732,289✔
286
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
652,614,669✔
287
      return false;
1,166,095✔
288
    }
289
  }
290

291
  return true;
688,633,246✔
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) {
57✔
331
  int32_t             code = 0;
57✔
332
  __tsdb_cache_update cb = (__tsdb_cache_update)func;
57✔
333

334
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
58,425✔
335
    STbData *pTbData = pMemTable->aBucket[i];
58,368✔
336
    while (pTbData) {
58,974✔
337
      code = (*cb)(pMemTable, pTbData->suid, pTbData->uid);
606✔
338
      if (code) {
606!
339
        TAOS_RETURN(code);
×
340
      }
341

342
      pTbData = pTbData->next;
606✔
343
    }
344
  }
345

346
  return code;
57✔
347
}
348

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

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

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

362
    while (pTbData) {
×
363
      STbData *pNext = pTbData->next;
×
364

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

369
      pTbData = pNext;
×
370
    }
371
  }
372

373
  taosMemoryFree(pMemTable->aBucket);
×
374
  pMemTable->nBucket = nBucket;
×
375
  pMemTable->aBucket = aBucket;
×
376

377
_exit:
×
378
  return code;
×
379
}
380

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

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

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

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

415
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
377,951✔
416
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
377,951✔
417
  }
418
  taosInitRWLatch(&pTbData->lock);
75,593✔
419

420
  taosWLockLatch(&pMemTable->latch);
75,592✔
421

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

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

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

441
  taosWUnLockLatch(&pMemTable->latch);
75,587✔
442

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

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

459
  if (backward) {
4,632,785✔
460
    px = pTbData->sl.pTail;
2,109,695✔
461

462
    if (!fromPos) {
2,109,695✔
463
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
4,322,939✔
464
        pos[iLevel] = px;
2,213,285✔
465
      }
466
    }
467

468
    if (pTbData->sl.level) {
2,109,695✔
469
      if (fromPos) px = pos[pTbData->sl.level - 1];
2,033,355!
470

471
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
10,243,230✔
472
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
8,219,287✔
473
        while (pn != pTbData->sl.pHead) {
8,744,621✔
474
          tsdbRowGetKey(&pn->row, &tKey);
8,705,364✔
475

476
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
8,702,415✔
477
          if (c <= 0) {
8,699,940✔
478
            break;
8,170,618✔
479
          } else {
480
            px = pn;
529,322✔
481
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
529,322✔
482
          }
483
        }
484

485
        pos[iLevel] = px;
8,209,875✔
486
      }
487
    }
488
  } else {
489
    px = pTbData->sl.pHead;
2,523,090✔
490

491
    if (!fromPos) {
2,523,090✔
492
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
4,132,685✔
493
        pos[iLevel] = px;
2,176,611✔
494
      }
495
    }
496

497
    if (pTbData->sl.level) {
2,523,090!
498
      if (fromPos) px = pos[pTbData->sl.level - 1];
2,523,183✔
499

500
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
12,933,703✔
501
        pn = SL_GET_NODE_FORWARD(px, iLevel);
10,413,315✔
502
        while (pn != pTbData->sl.pTail) {
37,764,190✔
503
          tsdbRowGetKey(&pn->row, &tKey);
37,530,664✔
504

505
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
37,534,196✔
506
          if (c >= 0) {
37,619,861✔
507
            break;
10,176,994✔
508
          } else {
509
            px = pn;
27,442,867✔
510
            pn = SL_GET_NODE_FORWARD(px, iLevel);
27,442,867✔
511
          }
512
        }
513

514
        pos[iLevel] = px;
10,410,520✔
515
      }
516
    }
517
  }
518
}
4,620,578✔
519

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

524
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
134,263,406✔
525
    level++;
33,425,575✔
526
  }
527

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

538
  // create node
539
  level = tsdbMemSkipListRandLevel(&pTbData->sl);
100,837,831✔
540
  nSize = SL_NODE_SIZE(level);
100,742,179✔
541
  if (pRow->type == TSDBROW_ROW_FMT) {
100,742,179!
542
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize + pRow->pTSRow->len);
100,769,605✔
543
  } else if (pRow->type == TSDBROW_COL_FMT) {
×
544
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize);
216✔
545
  }
546
  if (pNode == NULL) {
100,760,470!
547
    code = terrno;
×
548
    goto _exit;
×
549
  }
550

551
  pNode->level = level;
100,760,470✔
552
  pNode->row = *pRow;
100,760,470✔
553
  if (pRow->type == TSDBROW_ROW_FMT) {
100,760,470!
554
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
100,761,017✔
555
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
100,761,017✔
556
  }
557

558
  // set node
559
  if (forward) {
100,760,470✔
560
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
230,245,559✔
561
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
131,483,761✔
562
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
131,483,761✔
563
    }
564
  } else {
565
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
4,625,848✔
566
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
2,627,176✔
567
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
2,627,176✔
568
    }
569
  }
570

571
  // set forward and backward
572
  if (forward) {
100,760,470✔
573
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
230,394,659✔
574
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
131,504,558✔
575

576
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
131,504,558✔
577
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
131,649,159✔
578

579
      pos[iLevel] = pNode;
131,621,901✔
580
    }
581
  } else {
582
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
4,638,914✔
583
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
2,629,386✔
584

585
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
2,629,386✔
586
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
2,643,921✔
587

588
      pos[iLevel] = pNode;
2,651,202✔
589
    }
590
  }
591

592
  pTbData->sl.size++;
100,899,629✔
593
  if (pTbData->sl.level < pNode->level) {
100,899,629✔
594
    pTbData->sl.level = pNode->level;
272,836✔
595
  }
596

597
_exit:
100,626,793✔
598
  return code;
100,899,629✔
599
}
600

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

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

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

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

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

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

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

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

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

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

666
    while (tRow.iRow < pBlockData->nRow) {
119✔
667
      tsdbRowGetKey(&tRow, &key);
89✔
668

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

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

675
      ++tRow.iRow;
89✔
676
    }
677
  }
678

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

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

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

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

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

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

712
  // backward put first data
713
  tRow.pTSRow = aRow[iRow++];
2,012,378✔
714
  tsdbRowGetKey(&tRow, &key);
2,012,378✔
715
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
2,010,700✔
716
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
1,989,266✔
717
  if (code) goto _exit;
2,018,617!
718

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

721
  // forward put rest data
722
  if (iRow < nRow) {
2,018,617✔
723
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
2,942,548✔
724
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
2,315,629✔
725
    }
726

727
    while (iRow < nRow) {
99,502,551✔
728
      tRow.pTSRow = aRow[iRow];
98,875,575✔
729
      tsdbRowGetKey(&tRow, &key);
98,875,575✔
730

731
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
98,863,005✔
732
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
566,261✔
733
      }
734

735
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
98,863,006✔
736
      if (code) goto _exit;
98,875,632!
737

738
      iRow++;
98,875,632✔
739
    }
740
  }
741

742
  if (key.key.ts >= pTbData->maxKey) {
2,018,674✔
743
    pTbData->maxKey = key.key.ts;
1,973,784✔
744
  }
745
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
2,018,674!
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,018,221✔
751
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
2,018,221✔
752
  pMemTable->nRow += nRow;
2,018,221✔
753

754
  if (affectedRows) *affectedRows = nRow;
2,018,221!
755

756
_exit:
×
757
  return code;
2,018,221✔
758
}
759

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

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

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

770
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
811,737✔
771

772
_exit:
811,923✔
773
  return code;
811,923✔
774
}
775

776
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
829,947✔
777
  if (pNode) {
829,947✔
778
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
811,661✔
779
  }
780

781
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
830,393✔
782
    tsdbMemTableDestroy(pMemTable, proactive);
18,375✔
783
  }
784
}
830,466✔
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