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

taosdata / TDengine / #4988

16 Mar 2026 12:26PM UTC coverage: 75.821% (+1.9%) from 73.883%
#4988

push

travis-ci

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

274 of 464 new or added lines in 29 files covered. (59.05%)

4404 existing lines in 23 files now uncovered.

337108 of 444611 relevant lines covered (75.82%)

146708292.94 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) {
2,147,483,647✔
43
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
2,147,483,647✔
44
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
2,147,483,647✔
45
  if (tbData1->suid < tbData2->suid) return -1;
2,147,483,647✔
46
  if (tbData1->suid > tbData2->suid) return 1;
2,147,483,647✔
47
  if (tbData1->uid < tbData2->uid) return -1;
2,147,483,647✔
48
  if (tbData1->uid > tbData2->uid) return 1;
1,934,362,046✔
UNCOV
49
  return 0;
×
50
}
51

52
int32_t tsdbMemTableCreate(STsdb *pTsdb, SMemTable **ppMemTable) {
18,962,248✔
53
  int32_t    code = 0;
18,962,248✔
54
  SMemTable *pMemTable = NULL;
18,962,248✔
55

56
  pMemTable = (SMemTable *)taosMemoryCalloc(1, sizeof(*pMemTable));
18,962,248✔
57
  if (pMemTable == NULL) {
18,966,000✔
UNCOV
58
    code = terrno;
×
59
    goto _err;
×
60
  }
61
  taosInitRWLatch(&pMemTable->latch);
18,966,000✔
62
  pMemTable->pTsdb = pTsdb;
18,978,902✔
63
  pMemTable->pPool = pTsdb->pVnode->inUse;
18,978,902✔
64
  pMemTable->nRef = 1;
18,981,300✔
65
  pMemTable->minVer = VERSION_MAX;
18,980,976✔
66
  pMemTable->maxVer = VERSION_MIN;
18,979,870✔
67
  pMemTable->minKey = TSKEY_MAX;
18,974,420✔
68
  pMemTable->maxKey = TSKEY_MIN;
18,976,198✔
69
  pMemTable->nRow = 0;
18,976,444✔
70
  pMemTable->nDel = 0;
18,976,750✔
71
  pMemTable->nTbData = 0;
18,968,128✔
72
  pMemTable->nBucket = MEM_MIN_HASH;
18,971,590✔
73
  pMemTable->aBucket = (STbData **)taosMemoryCalloc(pMemTable->nBucket, sizeof(STbData *));
18,967,778✔
74
  if (pMemTable->aBucket == NULL) {
18,982,608✔
UNCOV
75
    code = terrno;
×
76
    taosMemoryFree(pMemTable);
×
77
    goto _err;
×
78
  }
79
  vnodeBufPoolRef(pMemTable->pPool);
18,981,468✔
80
  tRBTreeCreate(pMemTable->tbDataTree, tTbDataCmprFn);
18,983,542✔
81

82
  *ppMemTable = pMemTable;
18,982,818✔
83
  return code;
18,980,862✔
84

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

90
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
18,983,956✔
91
  if (pMemTable) {
18,983,956✔
92
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
18,983,956✔
93
    taosMemoryFree(pMemTable->aBucket);
18,983,956✔
94
    taosMemoryFree(pMemTable);
18,982,946✔
95
  }
96
}
18,983,956✔
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];
2,147,483,647✔
100

101
  while (pTbData) {
2,147,483,647✔
102
    if (pTbData->uid == uid) break;
2,147,483,647✔
103
    pTbData = pTbData->next;
34,315,628✔
104
  }
105

106
  return pTbData;
2,147,483,647✔
107
}
108

109
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
1,382,342,096✔
110
  STbData *pTbData;
111

112
  taosRLockLatch(&pMemTable->latch);
1,382,342,096✔
113
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
1,382,585,002✔
114
  taosRUnLockLatch(&pMemTable->latch);
1,382,585,002✔
115

116
  return pTbData;
1,382,616,160✔
117
}
118

119
int32_t tsdbInsertTableData(STsdb *pTsdb, int64_t version, SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
1,349,902,304✔
120
  int32_t    code = 0;
1,349,902,304✔
121
  SMemTable *pMemTable = pTsdb->mem;
1,349,902,304✔
122
  STbData   *pTbData = NULL;
1,349,904,248✔
123
  tb_uid_t   suid = pSubmitTbData->suid;
1,349,905,714✔
124
  tb_uid_t   uid = pSubmitTbData->uid;
1,349,906,090✔
125

126
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
1,349,904,812✔
127
    goto _err;
316✔
128
  }
129

130
  // create/get STbData to op
131
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
1,349,904,496✔
132
  if (code) {
1,349,905,498✔
UNCOV
133
    goto _err;
×
134
  }
135

136
  // do insert impl
137
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,349,905,498✔
138
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
13,735,996✔
139
  } else {
140
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
1,336,166,316✔
141
  }
142
  if (code) goto _err;
1,349,872,752✔
143

144
  // update
145
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
1,349,872,752✔
146
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
1,349,871,216✔
147

148
  return code;
1,349,869,314✔
149

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

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

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

174
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
4,446,088✔
175
  if (code) {
4,446,088✔
UNCOV
176
    goto _err;
×
177
  }
178

179
  // secureDelete is merged by planner and vnode runtime config.
180
  int8_t doSecureErase = secureDelete;
4,446,088✔
181
  if (doSecureErase) {
4,446,088✔
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);
1,176✔
187
    if (eraseCode != 0) {
1,176✔
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));
4,446,088✔
196
  if (pDelData == NULL) {
4,446,088✔
UNCOV
197
    code = terrno;
×
UNCOV
198
    goto _err;
×
199
  }
200
  pDelData->version = version;
4,446,088✔
201
  pDelData->sKey = sKey;
4,446,088✔
202
  pDelData->eKey = eKey;
4,446,088✔
203
  pDelData->pNext = NULL;
4,446,088✔
204
  taosWLockLatch(&pTbData->lock);
4,446,088✔
205
  if (pTbData->pHead == NULL) {
4,446,088✔
206
    pTbData->pHead = pTbData->pTail = pDelData;
4,383,384✔
207
  } else {
208
    pTbData->pTail->pNext = pDelData;
62,704✔
209
    pTbData->pTail = pDelData;
62,704✔
210
  }
211
  taosWUnLockLatch(&pTbData->lock);
4,446,088✔
212

213
  pMemTable->nDel++;
4,446,088✔
214
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
4,446,088✔
215
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
4,446,088✔
216

217
  if (tsdbCacheDel(pTsdb, suid, uid, sKey, eKey) != 0) {
4,446,088✔
UNCOV
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
4,446,088✔
224
            " at version %" PRId64 " secureDelete:%d",
225
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version, (int)doSecureErase);
226
  return code;
4,446,088✔
227

UNCOV
228
_err:
×
UNCOV
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));
UNCOV
232
  return code;
×
233
}
234

235
int32_t tsdbTbDataIterCreate(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter **ppIter) {
853,005,186✔
236
  int32_t code = 0;
853,005,186✔
237

238
  (*ppIter) = (STbDataIter *)taosMemoryCalloc(1, sizeof(STbDataIter));
853,005,186✔
239
  if ((*ppIter) == NULL) {
852,753,042✔
UNCOV
240
    code = terrno;
×
UNCOV
241
    goto _exit;
×
242
  }
243

244
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
852,808,048✔
245

246
_exit:
852,943,224✔
247
  return code;
852,943,224✔
248
}
249

250
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
853,045,630✔
251
  if (pIter) {
853,045,630✔
252
    taosMemoryFree(pIter);
853,057,942✔
253
  }
254
  return NULL;
853,084,006✔
255
}
256

257
void tsdbTbDataIterOpen(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter *pIter) {
1,406,440,004✔
258
  SMemSkipListNode *pos[SL_MAX_LEVEL];
1,406,420,568✔
259
  SMemSkipListNode *pHead;
260
  SMemSkipListNode *pTail;
261

262
  pHead = pTbData->sl.pHead;
1,406,535,498✔
263
  pTail = pTbData->sl.pTail;
1,406,674,136✔
264
  pIter->pTbData = pTbData;
1,406,658,404✔
265
  pIter->backward = backward;
1,406,635,300✔
266
  pIter->pRow = NULL;
1,406,511,114✔
267
  if (pFrom == NULL) {
1,406,248,806✔
268
    // create from head or tail
269
    if (backward) {
17,779,362✔
270
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
17,779,362✔
271
    } else {
UNCOV
272
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
×
273
    }
274
  } else {
275
    // create from a key
276
    if (backward) {
1,388,469,444✔
277
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
81,650,230✔
278
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
81,634,336✔
279
    } else {
280
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
1,306,819,214✔
281
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
1,306,913,556✔
282
    }
283
  }
284
}
1,406,362,652✔
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✔
UNCOV
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;
75,720,998✔
296
    }
297
  } else {
298
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
2,147,483,647✔
UNCOV
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;
854,859,636✔
305
    }
306
  }
307

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

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

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

UNCOV
321
    rowsNum++;
×
322
  }
323

UNCOV
324
  return rowsNum;
×
325
}
326

UNCOV
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];
×
UNCOV
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);
×
UNCOV
339
      pTbData = pTbData->next;
×
340
    }
341
  }
UNCOV
342
  taosRUnLockLatch(&pMemTable->latch);
×
UNCOV
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) {
113,268✔
348
  int32_t             code = 0;
113,268✔
349
  __tsdb_cache_update cb = (__tsdb_cache_update)func;
113,268✔
350

351
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
117,471,190✔
352
    STbData *pTbData = pMemTable->aBucket[i];
117,357,922✔
353
    while (pTbData) {
133,434,910✔
354
      code = (*cb)(pMemTable, pTbData->suid, pTbData->uid);
16,076,988✔
355
      if (code) {
16,076,988✔
356
        TAOS_RETURN(code);
×
357
      }
358

359
      pTbData = pTbData->next;
16,076,988✔
360
    }
361
  }
362

363
  return code;
113,268✔
364
}
365

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

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

376
  for (int32_t iBucket = 0; iBucket < pMemTable->nBucket; iBucket++) {
94,824,452✔
377
    STbData *pTbData = pMemTable->aBucket[iBucket];
94,789,632✔
378

379
    while (pTbData) {
189,579,264✔
380
      STbData *pNext = pTbData->next;
94,789,632✔
381

382
      int32_t idx = TABS(pTbData->uid) % nBucket;
94,789,632✔
383
      pTbData->next = aBucket[idx];
94,789,632✔
384
      aBucket[idx] = pTbData;
94,789,632✔
385

386
      pTbData = pNext;
94,789,632✔
387
    }
388
  }
389

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

394
_exit:
34,820✔
395
  return code;
34,820✔
396
}
397

398
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
1,354,349,350✔
399
  int32_t code = 0;
1,354,349,350✔
400

401
  // get
402
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
1,354,351,490✔
403
  if (pTbData) goto _exit;
1,354,351,490✔
404

405
  // create
406
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
140,644,396✔
407
  int8_t     maxLevel = pMemTable->pTsdb->pVnode->config.tsdbCfg.slLevel;
140,644,588✔
408

409
  pTbData = vnodeBufPoolMallocAligned(pPool, sizeof(*pTbData) + SL_NODE_SIZE(maxLevel) * 2);
140,644,588✔
410
  if (pTbData == NULL) {
140,643,390✔
UNCOV
411
    code = terrno;
×
UNCOV
412
    goto _exit;
×
413
  }
414
  pTbData->suid = suid;
140,643,390✔
415
  pTbData->uid = uid;
140,643,390✔
416
  pTbData->minKey = TSKEY_MAX;
140,642,986✔
417
  pTbData->maxKey = TSKEY_MIN;
140,642,986✔
418
  pTbData->pHead = NULL;
140,642,986✔
419
  pTbData->pTail = NULL;
140,642,986✔
420
  pTbData->sl.seed = taosRand();
140,642,986✔
421
  pTbData->sl.size = 0;
140,644,588✔
422
  pTbData->sl.maxLevel = maxLevel;
140,644,588✔
423
  pTbData->sl.level = 0;
140,644,588✔
424
  pTbData->sl.pHead = (SMemSkipListNode *)&pTbData[1];
140,644,588✔
425
  pTbData->sl.pTail = (SMemSkipListNode *)POINTER_SHIFT(pTbData->sl.pHead, SL_NODE_SIZE(maxLevel));
140,644,588✔
426
  pTbData->sl.pHead->level = maxLevel;
140,644,588✔
427
  pTbData->sl.pTail->level = maxLevel;
140,644,588✔
428
  for (int8_t iLevel = 0; iLevel < maxLevel; iLevel++) {
843,817,686✔
429
    SL_NODE_FORWARD(pTbData->sl.pHead, iLevel) = pTbData->sl.pTail;
703,174,326✔
430
    SL_NODE_BACKWARD(pTbData->sl.pTail, iLevel) = pTbData->sl.pHead;
703,167,224✔
431

432
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
703,171,410✔
433
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
703,168,680✔
434
  }
435
  taosInitRWLatch(&pTbData->lock);
140,643,360✔
436

437
  taosWLockLatch(&pMemTable->latch);
140,643,996✔
438

439
  if (pMemTable->nTbData >= pMemTable->nBucket) {
140,641,656✔
440
    code = tsdbMemTableRehash(pMemTable);
34,820✔
441
    if (code) {
34,820✔
UNCOV
442
      taosWUnLockLatch(&pMemTable->latch);
×
UNCOV
443
      goto _exit;
×
444
    }
445
  }
446

447
  int32_t idx = TABS(uid) % pMemTable->nBucket;
140,641,656✔
448
  pTbData->next = pMemTable->aBucket[idx];
140,642,884✔
449
  pMemTable->aBucket[idx] = pTbData;
140,641,656✔
450
  pMemTable->nTbData++;
140,642,884✔
451

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

458
  taosWUnLockLatch(&pMemTable->latch);
140,632,796✔
459

460
_exit:
1,354,351,586✔
461
  if (code) {
1,354,351,586✔
UNCOV
462
    *ppTbData = NULL;
×
463
  } else {
464
    *ppTbData = pTbData;
1,354,351,586✔
465
  }
466
  return code;
1,354,351,586✔
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;
1,431,550,588✔
478

479
    if (!fromPos) {
1,431,547,624✔
480
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
2,147,483,647✔
481
        pos[iLevel] = px;
1,662,951,836✔
482
      }
483
    }
484

485
    if (pTbData->sl.level) {
1,431,500,334✔
486
      if (fromPos) px = pos[pTbData->sl.level - 1];
1,290,762,406✔
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;
660,905,358✔
498
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
660,905,358✔
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;
2,147,483,647✔
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✔
UNCOV
564
    code = terrno;
×
UNCOV
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++) {
2,147,483,647✔
583
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
1,746,069,986✔
584
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
1,746,071,594✔
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--) {
2,147,483,647✔
600
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
1,746,069,236✔
601

602
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
1,746,068,880✔
603
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
1,746,102,262✔
604

605
      pos[iLevel] = pNode;
1,746,110,446✔
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;
318,188,554✔
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,
13,735,404✔
619
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
620
  int32_t code = 0;
13,735,404✔
621

622
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
13,735,404✔
623
  int32_t    nColData = TARRAY_SIZE(pSubmitTbData->aCol);
13,735,996✔
624
  SColData  *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
13,733,974✔
625

626
  // copy and construct block data
627
  SBlockData *pBlockData = vnodeBufPoolMalloc(pPool, sizeof(*pBlockData));
13,736,666✔
628
  if (pBlockData == NULL) {
13,735,404✔
UNCOV
629
    code = terrno;
×
UNCOV
630
    goto _exit;
×
631
  }
632

633
  pBlockData->suid = pTbData->suid;
13,735,404✔
634
  pBlockData->uid = pTbData->uid;
13,734,734✔
635
  pBlockData->nRow = aColData[0].nVal;
13,734,052✔
636
  pBlockData->aUid = NULL;
13,733,304✔
637
  pBlockData->aVersion = vnodeBufPoolMalloc(pPool, aColData[0].nData);
13,733,382✔
638
  if (pBlockData->aVersion == NULL) {
13,735,314✔
UNCOV
639
    code = terrno;
×
UNCOV
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);
13,736,666✔
647
  if (pBlockData->aTSKEY == NULL) {
13,736,666✔
UNCOV
648
    code = terrno;
×
UNCOV
649
    goto _exit;
×
650
  }
651
  memcpy(pBlockData->aTSKEY, aColData[0].pData, aColData[0].nData);
13,735,404✔
652

653
  pBlockData->nColData = nColData - 1;
13,736,666✔
654
  pBlockData->aColData = vnodeBufPoolMalloc(pPool, sizeof(SColData) * pBlockData->nColData);
13,735,996✔
655
  if (pBlockData->aColData == NULL) {
13,735,996✔
656
    code = terrno;
×
657
    goto _exit;
×
658
  }
659

660
  for (int32_t iColData = 0; iColData < pBlockData->nColData; ++iColData) {
42,275,084✔
661
    code = tColDataCopy(&aColData[iColData + 1], &pBlockData->aColData[iColData], (xMallocFn)vnodeBufPoolMalloc, pPool);
28,537,078✔
662
    if (code) goto _exit;
28,539,010✔
663
  }
664

665
  // loop to add each row to the skiplist
666
  SMemSkipListNode *pos[SL_MAX_LEVEL];
13,735,770✔
667
  TSDBROW           tRow = tsdbRowFromBlockData(pBlockData, 0);
13,736,666✔
668
  STsdbRowKey       key;
13,735,770✔
669

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

676
  // remain row
677
  ++tRow.iRow;
13,735,638✔
678
  if (tRow.iRow < pBlockData->nRow) {
13,735,638✔
679
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
68,303,480✔
680
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
54,643,268✔
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);
333,200✔
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) {
13,629,870✔
697
    pTbData->maxKey = key.key.ts;
13,733,400✔
698
  }
699

700
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
13,736,744✔
UNCOV
701
    if (tsdbCacheColFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, pBlockData) != 0) {
×
UNCOV
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);
13,736,074✔
709
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
13,736,074✔
710
  pMemTable->nRow += pBlockData->nRow;
13,734,734✔
711

712
  if (affectedRows) *affectedRows = pBlockData->nRow;
13,734,734✔
713

714
_exit:
13,735,178✔
715
  return code;
13,735,404✔
716
}
717

718
static int32_t tsdbInsertRowDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
1,336,160,862✔
719
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
720
  int32_t code = 0;
1,336,160,862✔
721

722
  int32_t           nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
1,336,160,862✔
723
  SRow            **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
1,336,165,848✔
724
  STsdbRowKey       key;
1,336,154,544✔
725
  SMemSkipListNode *pos[SL_MAX_LEVEL];
1,336,154,784✔
726
  TSDBROW           tRow = {.type = TSDBROW_ROW_FMT, .version = version};
1,336,163,688✔
727
  int32_t           iRow = 0;
1,336,164,750✔
728

729
  // backward put first data
730
  tRow.pTSRow = aRow[iRow++];
1,336,164,750✔
731
  tsdbRowGetKey(&tRow, &key);
1,336,157,338✔
732
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
1,336,159,850✔
733
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
1,336,155,600✔
734
  if (code) goto _exit;
1,336,127,184✔
735

736
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
1,336,127,184✔
737

738
  // forward put rest data
739
  if (iRow < nRow) {
1,336,116,786✔
740
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
2,100,027,536✔
741
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
1,653,292,954✔
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) {
1,255,191,514✔
760
    pTbData->maxKey = key.key.ts;
1,323,876,066✔
761
  }
762
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
1,336,157,850✔
UNCOV
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);
1,336,144,068✔
768
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
1,336,135,782✔
769
  pMemTable->nRow += nRow;
1,336,137,234✔
770

771
  if (affectedRows) *affectedRows = nRow;
1,336,141,124✔
772

773
_exit:
1,336,137,130✔
774
  return code;
1,336,144,092✔
775
}
776

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

779
int32_t tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode) {
407,102,228✔
780
  int32_t code = 0;
407,102,228✔
781

782
  int32_t nRef = atomic_fetch_add_32(&pMemTable->nRef, 1);
407,102,228✔
783
  if (nRef <= 0) {
407,147,508✔
UNCOV
784
    tsdbError("vgId:%d, memtable ref count is invalid, ref:%d", TD_VID(pMemTable->pTsdb->pVnode), nRef);
×
785
  }
786

787
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
407,147,508✔
788

789
_exit:
406,988,978✔
790
  return code;
406,988,978✔
791
}
792

793
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
417,736,572✔
794
  if (pNode) {
417,736,572✔
795
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
407,210,222✔
796
  }
797

798
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
417,715,258✔
799
    tsdbMemTableDestroy(pMemTable, proactive);
10,539,500✔
800
  }
801
}
417,766,804✔
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