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

taosdata / TDengine / #4987

16 Mar 2026 12:26PM UTC coverage: 73.883% (+36.6%) from 37.305%
#4987

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

209 of 391 new or added lines in 24 files covered. (53.45%)

3062 existing lines in 140 files now uncovered.

261133 of 353439 relevant lines covered (73.88%)

121262425.02 hits per line

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

83.98
/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 "tglobal.h"
17
#include "tsdb.h"
18
#include "util/tsimplehash.h"
19

20
#define MEM_MIN_HASH 1024
21
#define SL_MAX_LEVEL 5
22

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

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

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

42
static int32_t tTbDataCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
1,134,240,753✔
43
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
1,134,240,753✔
44
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
1,134,225,308✔
45
  if (tbData1->suid < tbData2->suid) return -1;
1,134,246,096✔
46
  if (tbData1->suid > tbData2->suid) return 1;
1,126,028,231✔
47
  if (tbData1->uid < tbData2->uid) return -1;
1,112,704,510✔
48
  if (tbData1->uid > tbData2->uid) return 1;
967,181,023✔
49
  return 0;
×
50
}
51

52
int32_t tsdbMemTableCreate(STsdb *pTsdb, SMemTable **ppMemTable) {
9,481,124✔
53
  int32_t    code = 0;
9,481,124✔
54
  SMemTable *pMemTable = NULL;
9,481,124✔
55

56
  pMemTable = (SMemTable *)taosMemoryCalloc(1, sizeof(*pMemTable));
9,481,124✔
57
  if (pMemTable == NULL) {
9,483,000✔
58
    code = terrno;
×
59
    goto _err;
×
60
  }
61
  taosInitRWLatch(&pMemTable->latch);
9,483,000✔
62
  pMemTable->pTsdb = pTsdb;
9,489,451✔
63
  pMemTable->pPool = pTsdb->pVnode->inUse;
9,489,451✔
64
  pMemTable->nRef = 1;
9,490,650✔
65
  pMemTable->minVer = VERSION_MAX;
9,490,488✔
66
  pMemTable->maxVer = VERSION_MIN;
9,489,935✔
67
  pMemTable->minKey = TSKEY_MAX;
9,487,210✔
68
  pMemTable->maxKey = TSKEY_MIN;
9,488,099✔
69
  pMemTable->nRow = 0;
9,488,222✔
70
  pMemTable->nDel = 0;
9,488,375✔
71
  pMemTable->nTbData = 0;
9,484,064✔
72
  pMemTable->nBucket = MEM_MIN_HASH;
9,485,795✔
73
  pMemTable->aBucket = (STbData **)taosMemoryCalloc(pMemTable->nBucket, sizeof(STbData *));
9,483,889✔
74
  if (pMemTable->aBucket == NULL) {
9,491,304✔
75
    code = terrno;
×
76
    taosMemoryFree(pMemTable);
×
77
    goto _err;
×
78
  }
79
  vnodeBufPoolRef(pMemTable->pPool);
9,490,734✔
80
  tRBTreeCreate(pMemTable->tbDataTree, tTbDataCmprFn);
9,491,771✔
81

82
  *ppMemTable = pMemTable;
9,491,409✔
83
  return code;
9,490,431✔
84

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

90
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
9,491,978✔
91
  if (pMemTable) {
9,491,978✔
92
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
9,491,978✔
93
    taosMemoryFree(pMemTable->aBucket);
9,491,978✔
94
    taosMemoryFree(pMemTable);
9,491,473✔
95
  }
96
}
9,491,978✔
97

98
static FORCE_INLINE STbData *tsdbGetTbDataFromMemTableImpl(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
99
  STbData *pTbData = pMemTable->aBucket[TABS(uid) % pMemTable->nBucket];
1,368,486,730✔
100

101
  while (pTbData) {
1,385,617,267✔
102
    if (pTbData->uid == uid) break;
1,173,584,065✔
103
    pTbData = pTbData->next;
17,157,814✔
104
  }
105

106
  return pTbData;
1,368,468,246✔
107
}
108

109
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
691,171,048✔
110
  STbData *pTbData;
111

112
  taosRLockLatch(&pMemTable->latch);
691,171,048✔
113
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
691,292,501✔
114
  taosRUnLockLatch(&pMemTable->latch);
691,292,501✔
115

116
  return pTbData;
691,308,080✔
117
}
118

119
int32_t tsdbInsertTableData(STsdb *pTsdb, int64_t version, SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
674,951,152✔
120
  int32_t    code = 0;
674,951,152✔
121
  SMemTable *pMemTable = pTsdb->mem;
674,951,152✔
122
  STbData   *pTbData = NULL;
674,952,124✔
123
  tb_uid_t   suid = pSubmitTbData->suid;
674,952,857✔
124
  tb_uid_t   uid = pSubmitTbData->uid;
674,953,045✔
125

126
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
674,952,406✔
127
    goto _err;
158✔
128
  }
129

130
  // create/get STbData to op
131
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
674,952,248✔
132
  if (code) {
674,952,749✔
133
    goto _err;
×
134
  }
135

136
  // do insert impl
137
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
674,952,749✔
138
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
6,867,998✔
139
  } else {
140
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
668,083,158✔
141
  }
142
  if (code) goto _err;
674,936,376✔
143

144
  // update
145
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
674,936,376✔
146
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
674,935,608✔
147

148
  return code;
674,934,657✔
149

150
_err:
158✔
151
  terrno = code;
158✔
152
  return code;
158✔
153
}
154

155
int32_t tsdbDeleteTableData(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey,
2,223,044✔
156
                            int8_t secureDelete) {
157
  int32_t    code = 0;
2,223,044✔
158
  SMemTable *pMemTable = pTsdb->mem;
2,223,044✔
159
  STbData   *pTbData = NULL;
2,223,044✔
160
  SVBufPool *pPool = pTsdb->pVnode->inUse;
2,223,044✔
161

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

174
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
2,223,044✔
175
  if (code) {
2,223,044✔
176
    goto _err;
×
177
  }
178

179
  // secureDelete is merged by planner and vnode runtime config.
180
  int8_t doSecureErase = secureDelete;
2,223,044✔
181
  if (doSecureErase) {
2,223,044✔
182

183
    // Phase 2: overwrite on-disk (data file + STT file) blocks.
184
    // Errors are logged but not fatal: delete markers guarantee correctness
185
    // even if the physical overwrite fails.
186
    int32_t eraseCode = tsdbSecureEraseFileRange(pTsdb, suid, uid, sKey, eKey);
588✔
187
    if (eraseCode != 0) {
588✔
NEW
188
      tsdbWarn("vgId:%d, secure erase file range failed for suid:%" PRId64 " uid:%" PRId64
×
189
               " skey:%" PRId64 " eKey:%" PRId64 " since %s",
190
               TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, tstrerror(eraseCode));
191
    }
192
  }
193

194
  // do delete
195
  SDelData *pDelData = (SDelData *)vnodeBufPoolMalloc(pPool, sizeof(*pDelData));
2,223,044✔
196
  if (pDelData == NULL) {
2,223,044✔
197
    code = terrno;
×
198
    goto _err;
×
199
  }
200
  pDelData->version = version;
2,223,044✔
201
  pDelData->sKey = sKey;
2,223,044✔
202
  pDelData->eKey = eKey;
2,223,044✔
203
  pDelData->pNext = NULL;
2,223,044✔
204
  taosWLockLatch(&pTbData->lock);
2,223,044✔
205
  if (pTbData->pHead == NULL) {
2,223,044✔
206
    pTbData->pHead = pTbData->pTail = pDelData;
2,191,692✔
207
  } else {
208
    pTbData->pTail->pNext = pDelData;
31,352✔
209
    pTbData->pTail = pDelData;
31,352✔
210
  }
211
  taosWUnLockLatch(&pTbData->lock);
2,223,044✔
212

213
  pMemTable->nDel++;
2,223,044✔
214
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
2,223,044✔
215
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
2,223,044✔
216

217
  if (tsdbCacheDel(pTsdb, suid, uid, sKey, eKey) != 0) {
2,223,044✔
218
    tsdbError("vgId:%d, failed to delete cache data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64
×
219
              " eKey:%" PRId64 " at version %" PRId64,
220
              TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version);
221
  }
222

223
  tsdbTrace("vgId:%d, delete data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64 " eKey:%" PRId64
2,223,044✔
224
            " at version %" PRId64 " secureDelete:%d",
225
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version, (int)doSecureErase);
226
  return code;
2,223,044✔
227

228
_err:
×
229
  tsdbError("vgId:%d, failed to delete data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64 " eKey:%" PRId64
×
230
            " at version %" PRId64 " since %s",
231
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version, tstrerror(code));
232
  return code;
×
233
}
234

235
int32_t tsdbTbDataIterCreate(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter **ppIter) {
426,502,593✔
236
  int32_t code = 0;
426,502,593✔
237

238
  (*ppIter) = (STbDataIter *)taosMemoryCalloc(1, sizeof(STbDataIter));
426,502,593✔
239
  if ((*ppIter) == NULL) {
426,376,521✔
240
    code = terrno;
×
241
    goto _exit;
×
242
  }
243

244
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
426,404,024✔
245

246
_exit:
426,471,612✔
247
  return code;
426,471,612✔
248
}
249

250
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
426,522,815✔
251
  if (pIter) {
426,522,815✔
252
    taosMemoryFree(pIter);
426,528,971✔
253
  }
254
  return NULL;
426,542,003✔
255
}
256

257
void tsdbTbDataIterOpen(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter *pIter) {
703,220,002✔
258
  SMemSkipListNode *pos[SL_MAX_LEVEL];
703,210,284✔
259
  SMemSkipListNode *pHead;
260
  SMemSkipListNode *pTail;
261

262
  pHead = pTbData->sl.pHead;
703,267,749✔
263
  pTail = pTbData->sl.pTail;
703,337,068✔
264
  pIter->pTbData = pTbData;
703,329,202✔
265
  pIter->backward = backward;
703,317,650✔
266
  pIter->pRow = NULL;
703,255,557✔
267
  if (pFrom == NULL) {
703,124,403✔
268
    // create from head or tail
269
    if (backward) {
8,889,681✔
270
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
8,889,681✔
271
    } else {
272
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
×
273
    }
274
  } else {
275
    // create from a key
276
    if (backward) {
694,234,722✔
277
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
40,825,115✔
278
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
40,817,168✔
279
    } else {
280
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
653,409,607✔
281
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
653,456,778✔
282
    }
283
  }
284
}
703,181,326✔
285

286
bool tsdbTbDataIterNext(STbDataIter *pIter) {
2,147,483,647✔
287
  pIter->pRow = NULL;
2,147,483,647✔
288
  if (pIter->backward) {
2,147,483,647✔
289
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
2,147,483,647✔
290
      return false;
×
291
    }
292

293
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
2,147,483,647✔
294
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
2,147,483,647✔
295
      return false;
37,860,499✔
296
    }
297
  } else {
298
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
2,147,483,647✔
299
      return false;
×
300
    }
301

302
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
2,147,483,647✔
303
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
2,147,483,647✔
304
      return false;
427,429,818✔
305
    }
306
  }
307

308
  return true;
2,147,483,647✔
309
}
310

311
int64_t tsdbCountTbDataRows(STbData *pTbData) {
×
312
  SMemSkipListNode *pNode = pTbData->sl.pHead;
×
313
  int64_t           rowsNum = 0;
×
314

315
  while (NULL != pNode) {
×
316
    pNode = SL_GET_NODE_FORWARD(pNode, 0);
×
317
    if (pNode == pTbData->sl.pTail) {
×
318
      return rowsNum;
×
319
    }
320

321
    rowsNum++;
×
322
  }
323

324
  return rowsNum;
×
325
}
326

327
void tsdbMemTableCountRows(SMemTable *pMemTable, SSHashObj *pTableMap, int64_t *rowsNum) {
×
328
  taosRLockLatch(&pMemTable->latch);
×
329
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
×
330
    STbData *pTbData = pMemTable->aBucket[i];
×
331
    while (pTbData) {
×
332
      void *p = tSimpleHashGet(pTableMap, &pTbData->uid, sizeof(pTbData->uid));
×
333
      if (p == NULL) {
×
334
        pTbData = pTbData->next;
×
335
        continue;
×
336
      }
337

338
      *rowsNum += tsdbCountTbDataRows(pTbData);
×
339
      pTbData = pTbData->next;
×
340
    }
341
  }
342
  taosRUnLockLatch(&pMemTable->latch);
×
343
}
×
344

345
typedef int32_t (*__tsdb_cache_update)(SMemTable *imem, int64_t suid, int64_t uid);
346

347
int32_t tsdbMemTableSaveToCache(SMemTable *pMemTable, void *func) {
56,634✔
348
  int32_t             code = 0;
56,634✔
349
  __tsdb_cache_update cb = (__tsdb_cache_update)func;
56,634✔
350

351
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
58,735,595✔
352
    STbData *pTbData = pMemTable->aBucket[i];
58,678,961✔
353
    while (pTbData) {
66,717,455✔
354
      code = (*cb)(pMemTable, pTbData->suid, pTbData->uid);
8,038,494✔
355
      if (code) {
8,038,494✔
356
        TAOS_RETURN(code);
×
357
      }
358

359
      pTbData = pTbData->next;
8,038,494✔
360
    }
361
  }
362

363
  return code;
56,634✔
364
}
365

366
static int32_t tsdbMemTableRehash(SMemTable *pMemTable) {
17,410✔
367
  int32_t code = 0;
17,410✔
368

369
  int32_t   nBucket = pMemTable->nBucket * 2;
17,410✔
370
  STbData **aBucket = (STbData **)taosMemoryCalloc(nBucket, sizeof(STbData *));
17,410✔
371
  if (aBucket == NULL) {
17,410✔
372
    code = terrno;
×
373
    goto _exit;
×
374
  }
375

376
  for (int32_t iBucket = 0; iBucket < pMemTable->nBucket; iBucket++) {
47,412,226✔
377
    STbData *pTbData = pMemTable->aBucket[iBucket];
47,394,816✔
378

379
    while (pTbData) {
94,789,632✔
380
      STbData *pNext = pTbData->next;
47,394,816✔
381

382
      int32_t idx = TABS(pTbData->uid) % nBucket;
47,394,816✔
383
      pTbData->next = aBucket[idx];
47,394,816✔
384
      aBucket[idx] = pTbData;
47,394,816✔
385

386
      pTbData = pNext;
47,394,816✔
387
    }
388
  }
389

390
  taosMemoryFree(pMemTable->aBucket);
17,410✔
391
  pMemTable->nBucket = nBucket;
17,410✔
392
  pMemTable->aBucket = aBucket;
17,410✔
393

394
_exit:
17,410✔
395
  return code;
17,410✔
396
}
397

398
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
677,174,675✔
399
  int32_t code = 0;
677,174,675✔
400

401
  // get
402
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
677,175,745✔
403
  if (pTbData) goto _exit;
677,175,745✔
404

405
  // create
406
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
70,322,198✔
407
  int8_t     maxLevel = pMemTable->pTsdb->pVnode->config.tsdbCfg.slLevel;
70,322,294✔
408

409
  pTbData = vnodeBufPoolMallocAligned(pPool, sizeof(*pTbData) + SL_NODE_SIZE(maxLevel) * 2);
70,322,294✔
410
  if (pTbData == NULL) {
70,321,695✔
411
    code = terrno;
×
412
    goto _exit;
×
413
  }
414
  pTbData->suid = suid;
70,321,695✔
415
  pTbData->uid = uid;
70,321,695✔
416
  pTbData->minKey = TSKEY_MAX;
70,321,493✔
417
  pTbData->maxKey = TSKEY_MIN;
70,321,493✔
418
  pTbData->pHead = NULL;
70,321,493✔
419
  pTbData->pTail = NULL;
70,321,493✔
420
  pTbData->sl.seed = taosRand();
70,321,493✔
421
  pTbData->sl.size = 0;
70,322,294✔
422
  pTbData->sl.maxLevel = maxLevel;
70,322,294✔
423
  pTbData->sl.level = 0;
70,322,294✔
424
  pTbData->sl.pHead = (SMemSkipListNode *)&pTbData[1];
70,322,294✔
425
  pTbData->sl.pTail = (SMemSkipListNode *)POINTER_SHIFT(pTbData->sl.pHead, SL_NODE_SIZE(maxLevel));
70,322,294✔
426
  pTbData->sl.pHead->level = maxLevel;
70,322,294✔
427
  pTbData->sl.pTail->level = maxLevel;
70,322,294✔
428
  for (int8_t iLevel = 0; iLevel < maxLevel; iLevel++) {
421,908,843✔
429
    SL_NODE_FORWARD(pTbData->sl.pHead, iLevel) = pTbData->sl.pTail;
351,587,163✔
430
    SL_NODE_BACKWARD(pTbData->sl.pTail, iLevel) = pTbData->sl.pHead;
351,583,612✔
431

432
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
351,585,705✔
433
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
351,584,340✔
434
  }
435
  taosInitRWLatch(&pTbData->lock);
70,321,680✔
436

437
  taosWLockLatch(&pMemTable->latch);
70,321,998✔
438

439
  if (pMemTable->nTbData >= pMemTable->nBucket) {
70,320,828✔
440
    code = tsdbMemTableRehash(pMemTable);
17,410✔
441
    if (code) {
17,410✔
442
      taosWUnLockLatch(&pMemTable->latch);
×
443
      goto _exit;
×
444
    }
445
  }
446

447
  int32_t idx = TABS(uid) % pMemTable->nBucket;
70,320,828✔
448
  pTbData->next = pMemTable->aBucket[idx];
70,321,442✔
449
  pMemTable->aBucket[idx] = pTbData;
70,320,828✔
450
  pMemTable->nTbData++;
70,321,442✔
451

452
  if (tRBTreePut(pMemTable->tbDataTree, pTbData->rbtn) == NULL) {
70,321,124✔
453
    taosWUnLockLatch(&pMemTable->latch);
×
454
    code = TSDB_CODE_INTERNAL_ERROR;
×
455
    goto _exit;
×
456
  }
457

458
  taosWUnLockLatch(&pMemTable->latch);
70,316,398✔
459

460
_exit:
677,175,793✔
461
  if (code) {
677,175,793✔
462
    *ppTbData = NULL;
×
463
  } else {
464
    *ppTbData = pTbData;
677,175,793✔
465
  }
466
  return code;
677,175,793✔
467
}
468

469
static void tbDataMovePosTo(STbData *pTbData, SMemSkipListNode **pos, STsdbRowKey *pKey, int32_t flags) {
2,147,483,647✔
470
  SMemSkipListNode *px;
471
  SMemSkipListNode *pn;
472
  STsdbRowKey       tKey;
2,147,483,647✔
473
  int32_t           backward = flags & SL_MOVE_BACKWARD;
2,147,483,647✔
474
  int32_t           fromPos = flags & SL_MOVE_FROM_POS;
2,147,483,647✔
475

476
  if (backward) {
2,147,483,647✔
477
    px = pTbData->sl.pTail;
715,775,294✔
478

479
    if (!fromPos) {
715,773,812✔
480
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
1,547,249,667✔
481
        pos[iLevel] = px;
831,475,918✔
482
      }
483
    }
484

485
    if (pTbData->sl.level) {
715,750,167✔
486
      if (fromPos) px = pos[pTbData->sl.level - 1];
645,381,203✔
487

488
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
2,147,483,647✔
489
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
2,147,483,647✔
490
        while (pn != pTbData->sl.pHead) {
2,147,483,647✔
491
          tsdbRowGetKey(&pn->row, &tKey);
2,147,483,647✔
492

493
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
2,147,483,647✔
494
          if (c <= 0) {
2,147,483,647✔
495
            break;
2,147,483,647✔
496
          } else {
497
            px = pn;
330,452,679✔
498
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
330,452,679✔
499
          }
500
        }
501

502
        pos[iLevel] = px;
2,147,483,647✔
503
      }
504
    }
505
  } else {
506
    px = pTbData->sl.pHead;
2,147,483,647✔
507

508
    if (!fromPos) {
2,147,483,647✔
509
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
2,147,483,647✔
510
        pos[iLevel] = px;
1,598,099,581✔
511
      }
512
    }
513

514
    if (pTbData->sl.level) {
2,147,483,647✔
515
      if (fromPos) px = pos[pTbData->sl.level - 1];
2,147,483,647✔
516

517
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
2,147,483,647✔
518
        pn = SL_GET_NODE_FORWARD(px, iLevel);
2,147,483,647✔
519
        while (pn != pTbData->sl.pTail) {
2,147,483,647✔
520
          tsdbRowGetKey(&pn->row, &tKey);
2,147,483,647✔
521

522
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
2,147,483,647✔
523
          if (c >= 0) {
2,147,483,647✔
524
            break;
2,147,483,647✔
525
          } else {
526
            px = pn;
2,147,483,647✔
527
            pn = SL_GET_NODE_FORWARD(px, iLevel);
2,147,483,647✔
528
          }
529
        }
530

531
        pos[iLevel] = px;
2,147,483,647✔
532
      }
533
    }
534
  }
535
}
2,147,483,647✔
536

537
static FORCE_INLINE int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl) {
538
  int8_t level = 1;
2,147,483,647✔
539
  int8_t tlevel = TMIN(pSl->maxLevel, pSl->level + 1);
2,147,483,647✔
540

541
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
2,147,483,647✔
542
    level++;
2,147,483,647✔
543
  }
544

545
  return level;
2,147,483,647✔
546
}
547
static int32_t tbDataDoPut(SMemTable *pMemTable, STbData *pTbData, SMemSkipListNode **pos, TSDBROW *pRow,
2,147,483,647✔
548
                           int8_t forward) {
549
  int32_t           code = 0;
2,147,483,647✔
550
  int8_t            level;
551
  SMemSkipListNode *pNode = NULL;
2,147,483,647✔
552
  SVBufPool        *pPool = pMemTable->pTsdb->pVnode->inUse;
2,147,483,647✔
553
  int64_t           nSize;
554

555
  // create node
556
  level = tsdbMemSkipListRandLevel(&pTbData->sl);
2,147,483,647✔
557
  nSize = SL_NODE_SIZE(level);
2,147,483,647✔
558
  if (pRow->type == TSDBROW_ROW_FMT) {
2,147,483,647✔
559
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize + pRow->pTSRow->len);
2,147,483,647✔
560
  } else if (pRow->type == TSDBROW_COL_FMT) {
2,147,483,647✔
561
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize);
2,147,483,647✔
562
  }
563
  if (pNode == NULL) {
2,147,483,647✔
564
    code = terrno;
×
565
    goto _exit;
×
566
  }
567

568
  pNode->level = level;
2,147,483,647✔
569
  pNode->row = *pRow;
2,147,483,647✔
570
  if (pRow->type == TSDBROW_ROW_FMT) {
2,147,483,647✔
571
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
2,147,483,647✔
572
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
2,147,483,647✔
573
  }
574

575
  // set node
576
  if (forward) {
2,147,483,647✔
577
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
2,147,483,647✔
578
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
2,147,483,647✔
579
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
2,147,483,647✔
580
    }
581
  } else {
582
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
1,513,575,615✔
583
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
873,034,993✔
584
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
873,035,797✔
585
    }
586
  }
587

588
  // set forward and backward
589
  if (forward) {
2,147,483,647✔
590
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
2,147,483,647✔
591
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
2,147,483,647✔
592

593
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
2,147,483,647✔
594
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
2,147,483,647✔
595

596
      pos[iLevel] = pNode;
2,147,483,647✔
597
    }
598
  } else {
599
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
1,547,985,570✔
600
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
873,034,618✔
601

602
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
873,034,440✔
603
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
873,051,131✔
604

605
      pos[iLevel] = pNode;
873,055,223✔
606
    }
607
  }
608

609
  pTbData->sl.size++;
2,147,483,647✔
610
  if (pTbData->sl.level < pNode->level) {
2,147,483,647✔
611
    pTbData->sl.level = pNode->level;
159,094,277✔
612
  }
613

614
_exit:
2,147,483,647✔
615
  return code;
2,147,483,647✔
616
}
617

618
static int32_t tsdbInsertColDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
6,867,702✔
619
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
620
  int32_t code = 0;
6,867,702✔
621

622
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
6,867,702✔
623
  int32_t    nColData = TARRAY_SIZE(pSubmitTbData->aCol);
6,867,998✔
624
  SColData  *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
6,866,987✔
625

626
  // copy and construct block data
627
  SBlockData *pBlockData = vnodeBufPoolMalloc(pPool, sizeof(*pBlockData));
6,868,333✔
628
  if (pBlockData == NULL) {
6,867,702✔
629
    code = terrno;
×
630
    goto _exit;
×
631
  }
632

633
  pBlockData->suid = pTbData->suid;
6,867,702✔
634
  pBlockData->uid = pTbData->uid;
6,867,367✔
635
  pBlockData->nRow = aColData[0].nVal;
6,867,026✔
636
  pBlockData->aUid = NULL;
6,866,652✔
637
  pBlockData->aVersion = vnodeBufPoolMalloc(pPool, aColData[0].nData);
6,866,691✔
638
  if (pBlockData->aVersion == NULL) {
6,867,657✔
639
    code = terrno;
×
640
    goto _exit;
×
641
  }
642
  for (int32_t i = 0; i < pBlockData->nRow; i++) {  // todo: here can be optimized
2,147,483,647✔
643
    pBlockData->aVersion[i] = version;
2,147,483,647✔
644
  }
645

646
  pBlockData->aTSKEY = vnodeBufPoolMalloc(pPool, aColData[0].nData);
6,868,333✔
647
  if (pBlockData->aTSKEY == NULL) {
6,868,333✔
648
    code = terrno;
×
649
    goto _exit;
×
650
  }
651
  memcpy(pBlockData->aTSKEY, aColData[0].pData, aColData[0].nData);
6,867,702✔
652

653
  pBlockData->nColData = nColData - 1;
6,868,333✔
654
  pBlockData->aColData = vnodeBufPoolMalloc(pPool, sizeof(SColData) * pBlockData->nColData);
6,867,998✔
655
  if (pBlockData->aColData == NULL) {
6,867,998✔
656
    code = terrno;
×
657
    goto _exit;
×
658
  }
659

660
  for (int32_t iColData = 0; iColData < pBlockData->nColData; ++iColData) {
21,137,542✔
661
    code = tColDataCopy(&aColData[iColData + 1], &pBlockData->aColData[iColData], (xMallocFn)vnodeBufPoolMalloc, pPool);
14,268,539✔
662
    if (code) goto _exit;
14,269,505✔
663
  }
664

665
  // loop to add each row to the skiplist
666
  SMemSkipListNode *pos[SL_MAX_LEVEL];
6,867,885✔
667
  TSDBROW           tRow = tsdbRowFromBlockData(pBlockData, 0);
6,868,333✔
668
  STsdbRowKey       key;
6,867,885✔
669

670
  // first row
671
  tsdbRowGetKey(&tRow, &key);
6,868,333✔
672
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
6,868,037✔
673
  if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0))) goto _exit;
6,868,333✔
674
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
6,868,333✔
675

676
  // remain row
677
  ++tRow.iRow;
6,867,819✔
678
  if (tRow.iRow < pBlockData->nRow) {
6,867,819✔
679
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
34,151,740✔
680
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
27,321,634✔
681
    }
682

683
    while (tRow.iRow < pBlockData->nRow) {
2,147,483,647✔
684
      tsdbRowGetKey(&tRow, &key);
2,147,483,647✔
685

686
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
2,147,483,647✔
687
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
166,600✔
688
      }
689

690
      if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1))) goto _exit;
2,147,483,647✔
691

692
      ++tRow.iRow;
2,147,483,647✔
693
    }
694
  }
695

696
  if (key.key.ts >= pTbData->maxKey) {
6,814,935✔
697
    pTbData->maxKey = key.key.ts;
6,866,700✔
698
  }
699

700
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
6,868,372✔
701
    if (tsdbCacheColFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, pBlockData) != 0) {
×
702
      tsdbError("vgId:%d, failed to update cache data from table suid:%" PRId64 " uid:%" PRId64 " at version %" PRId64,
×
703
                TD_VID(pMemTable->pTsdb->pVnode), pTbData->suid, pTbData->uid, version);
704
    }
705
  }
706

707
  // SMemTable
708
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
6,868,037✔
709
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
6,868,037✔
710
  pMemTable->nRow += pBlockData->nRow;
6,867,367✔
711

712
  if (affectedRows) *affectedRows = pBlockData->nRow;
6,867,367✔
713

714
_exit:
6,867,589✔
715
  return code;
6,867,702✔
716
}
717

718
static int32_t tsdbInsertRowDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
668,080,431✔
719
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
720
  int32_t code = 0;
668,080,431✔
721

722
  int32_t           nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
668,080,431✔
723
  SRow            **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
668,082,924✔
724
  STsdbRowKey       key;
668,077,272✔
725
  SMemSkipListNode *pos[SL_MAX_LEVEL];
668,077,392✔
726
  TSDBROW           tRow = {.type = TSDBROW_ROW_FMT, .version = version};
668,081,844✔
727
  int32_t           iRow = 0;
668,082,375✔
728

729
  // backward put first data
730
  tRow.pTSRow = aRow[iRow++];
668,082,375✔
731
  tsdbRowGetKey(&tRow, &key);
668,078,669✔
732
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
668,079,925✔
733
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
668,077,800✔
734
  if (code) goto _exit;
668,063,592✔
735

736
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
668,063,592✔
737

738
  // forward put rest data
739
  if (iRow < nRow) {
668,058,393✔
740
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
1,050,013,768✔
741
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
826,646,477✔
742
    }
743

744
    while (iRow < nRow) {
2,147,483,647✔
745
      tRow.pTSRow = aRow[iRow];
2,147,483,647✔
746
      tsdbRowGetKey(&tRow, &key);
2,147,483,647✔
747

748
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
2,147,483,647✔
749
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
2,147,483,647✔
750
      }
751

752
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
2,147,483,647✔
753
      if (code) goto _exit;
2,147,483,647✔
754

755
      iRow++;
2,147,483,647✔
756
    }
757
  }
758

759
  if (key.key.ts >= pTbData->maxKey) {
627,595,757✔
760
    pTbData->maxKey = key.key.ts;
661,938,033✔
761
  }
762
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
668,078,925✔
763
    TAOS_UNUSED(tsdbCacheRowFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, version, nRow, aRow));
×
764
  }
765

766
  // SMemTable
767
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
668,072,034✔
768
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
668,067,891✔
769
  pMemTable->nRow += nRow;
668,068,617✔
770

771
  if (affectedRows) *affectedRows = nRow;
668,070,562✔
772

773
_exit:
668,068,565✔
774
  return code;
668,072,046✔
775
}
776

777
int32_t tsdbGetNRowsInTbData(STbData *pTbData) { return pTbData->sl.size; }
2,602✔
778

779
int32_t tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode) {
203,551,114✔
780
  int32_t code = 0;
203,551,114✔
781

782
  int32_t nRef = atomic_fetch_add_32(&pMemTable->nRef, 1);
203,551,114✔
783
  if (nRef <= 0) {
203,573,754✔
784
    tsdbError("vgId:%d, memtable ref count is invalid, ref:%d", TD_VID(pMemTable->pTsdb->pVnode), nRef);
×
785
  }
786

787
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
203,573,754✔
788

789
_exit:
203,494,489✔
790
  return code;
203,494,489✔
791
}
792

793
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
208,868,286✔
794
  if (pNode) {
208,868,286✔
795
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
203,605,111✔
796
  }
797

798
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
208,857,629✔
799
    tsdbMemTableDestroy(pMemTable, proactive);
5,269,750✔
800
  }
801
}
208,883,402✔
802

803
static FORCE_INLINE int32_t tbDataPCmprFn(const void *p1, const void *p2) {
804
  STbData *pTbData1 = *(STbData **)p1;
805
  STbData *pTbData2 = *(STbData **)p2;
806

807
  if (pTbData1->suid < pTbData2->suid) {
808
    return -1;
809
  } else if (pTbData1->suid > pTbData2->suid) {
810
    return 1;
811
  }
812

813
  if (pTbData1->uid < pTbData2->uid) {
814
    return -1;
815
  } else if (pTbData1->uid > pTbData2->uid) {
816
    return 1;
817
  }
818

819
  return 0;
820
}
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