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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

74.81
/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) {
191,587✔
42
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
191,587✔
43
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
191,587✔
44
  if (tbData1->suid < tbData2->suid) return -1;
191,587✔
45
  if (tbData1->suid > tbData2->suid) return 1;
179,256✔
46
  if (tbData1->uid < tbData2->uid) return -1;
168,686✔
47
  if (tbData1->uid > tbData2->uid) return 1;
133,455!
48
  return 0;
×
49
}
50

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

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

81
  *ppMemTable = pMemTable;
14,058✔
82
  return code;
14,058✔
83

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

89
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
14,060✔
90
  if (pMemTable) {
14,060!
91
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
14,060✔
92
    taosMemoryFree(pMemTable->aBucket);
14,060✔
93
    taosMemoryFree(pMemTable);
14,060✔
94
  }
95
}
14,060✔
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];
1,575,467✔
99

100
  while (pTbData) {
1,575,585✔
101
    if (pTbData->uid == uid) break;
1,480,995✔
102
    pTbData = pTbData->next;
118✔
103
  }
104

105
  return pTbData;
1,575,467✔
106
}
107

108
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
507,984✔
109
  STbData *pTbData;
110

111
  taosRLockLatch(&pMemTable->latch);
507,984✔
112
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
508,247✔
113
  taosRUnLockLatch(&pMemTable->latch);
508,247✔
114

115
  return pTbData;
508,258✔
116
}
117

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

125
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
1,065,698!
126
    goto _err;
×
127
  }
128

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

135
  // do insert impl
136
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
1,065,746✔
137
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
129✔
138
  } else {
139
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
1,065,617✔
140
  }
141
  if (code) goto _err;
1,065,886!
142

143
  // update
144
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
1,065,886✔
145
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
1,065,886✔
146

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

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

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

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

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

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

221
  (*ppIter) = (STbDataIter *)taosMemoryCalloc(1, sizeof(STbDataIter));
408,101✔
222
  if ((*ppIter) == NULL) {
408,126✔
223
    code = terrno;
43✔
224
    goto _exit;
43✔
225
  }
226

227
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
408,083✔
228

229
_exit:
407,977✔
230
  return code;
407,977✔
231
}
232

233
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
407,745✔
234
  if (pIter) {
407,745!
235
    taosMemoryFree(pIter);
407,749✔
236
  }
237
  return NULL;
407,960✔
238
}
239

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

245
  pHead = pTbData->sl.pHead;
472,148✔
246
  pTail = pTbData->sl.pTail;
472,148✔
247
  pIter->pTbData = pTbData;
472,148✔
248
  pIter->backward = backward;
472,148✔
249
  pIter->pRow = NULL;
472,148✔
250
  if (pFrom == NULL) {
472,148✔
251
    // create from head or tail
252
    if (backward) {
46!
253
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
46✔
254
    } else {
255
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
×
256
    }
257
  } else {
258
    // create from a key
259
    if (backward) {
472,102✔
260
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
22,576✔
261
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
22,573✔
262
    } else {
263
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
449,526✔
264
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
449,284✔
265
    }
266
  }
267
}
471,888✔
268

269
bool tsdbTbDataIterNext(STbDataIter *pIter) {
54,303,359✔
270
  pIter->pRow = NULL;
54,303,359✔
271
  if (pIter->backward) {
54,303,359✔
272
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
765,801!
273
      return false;
×
274
    }
275

276
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
765,801✔
277
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
765,365✔
278
      return false;
20,780✔
279
    }
280
  } else {
281
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
53,537,558!
282
      return false;
×
283
    }
284

285
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
53,537,558✔
286
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
53,456,769✔
287
      return false;
382,632✔
288
    }
289
  }
290

291
  return true;
53,818,722✔
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

UNCOV
328
static int32_t tsdbMemTableRehash(SMemTable *pMemTable) {
×
UNCOV
329
  int32_t code = 0;
×
330

UNCOV
331
  int32_t   nBucket = pMemTable->nBucket * 2;
×
UNCOV
332
  STbData **aBucket = (STbData **)taosMemoryCalloc(nBucket, sizeof(STbData *));
×
UNCOV
333
  if (aBucket == NULL) {
×
334
    code = terrno;
×
335
    goto _exit;
×
336
  }
337

UNCOV
338
  for (int32_t iBucket = 0; iBucket < pMemTable->nBucket; iBucket++) {
×
UNCOV
339
    STbData *pTbData = pMemTable->aBucket[iBucket];
×
340

UNCOV
341
    while (pTbData) {
×
UNCOV
342
      STbData *pNext = pTbData->next;
×
343

UNCOV
344
      int32_t idx = TABS(pTbData->uid) % nBucket;
×
UNCOV
345
      pTbData->next = aBucket[idx];
×
UNCOV
346
      aBucket[idx] = pTbData;
×
347

UNCOV
348
      pTbData = pNext;
×
349
    }
350
  }
351

UNCOV
352
  taosMemoryFree(pMemTable->aBucket);
×
UNCOV
353
  pMemTable->nBucket = nBucket;
×
UNCOV
354
  pMemTable->aBucket = aBucket;
×
355

UNCOV
356
_exit:
×
UNCOV
357
  return code;
×
358
}
359

360
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
1,067,220✔
361
  int32_t code = 0;
1,067,220✔
362

363
  // get
364
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
1,067,220✔
365
  if (pTbData) goto _exit;
1,067,220✔
366

367
  // create
368
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
25,870✔
369
  int8_t     maxLevel = pMemTable->pTsdb->pVnode->config.tsdbCfg.slLevel;
25,870✔
370

371
  pTbData = vnodeBufPoolMallocAligned(pPool, sizeof(*pTbData) + SL_NODE_SIZE(maxLevel) * 2);
25,870✔
372
  if (pTbData == NULL) {
26,002!
373
    code = terrno;
×
374
    goto _exit;
×
375
  }
376
  pTbData->suid = suid;
26,002✔
377
  pTbData->uid = uid;
26,002✔
378
  pTbData->minKey = TSKEY_MAX;
26,002✔
379
  pTbData->maxKey = TSKEY_MIN;
26,002✔
380
  pTbData->pHead = NULL;
26,002✔
381
  pTbData->pTail = NULL;
26,002✔
382
  pTbData->sl.seed = taosRand();
26,002✔
383
  pTbData->sl.size = 0;
26,003✔
384
  pTbData->sl.maxLevel = maxLevel;
26,003✔
385
  pTbData->sl.level = 0;
26,003✔
386
  pTbData->sl.pHead = (SMemSkipListNode *)&pTbData[1];
26,003✔
387
  pTbData->sl.pTail = (SMemSkipListNode *)POINTER_SHIFT(pTbData->sl.pHead, SL_NODE_SIZE(maxLevel));
26,003✔
388
  pTbData->sl.pHead->level = maxLevel;
26,003✔
389
  pTbData->sl.pTail->level = maxLevel;
26,003✔
390
  for (int8_t iLevel = 0; iLevel < maxLevel; iLevel++) {
156,015✔
391
    SL_NODE_FORWARD(pTbData->sl.pHead, iLevel) = pTbData->sl.pTail;
130,012✔
392
    SL_NODE_BACKWARD(pTbData->sl.pTail, iLevel) = pTbData->sl.pHead;
130,012✔
393

394
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
130,012✔
395
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
130,012✔
396
  }
397
  taosInitRWLatch(&pTbData->lock);
26,003✔
398

399
  taosWLockLatch(&pMemTable->latch);
26,004✔
400

401
  if (pMemTable->nTbData >= pMemTable->nBucket) {
26,004!
UNCOV
402
    code = tsdbMemTableRehash(pMemTable);
×
UNCOV
403
    if (code) {
×
404
      taosWUnLockLatch(&pMemTable->latch);
×
405
      goto _exit;
×
406
    }
407
  }
408

409
  int32_t idx = TABS(uid) % pMemTable->nBucket;
26,004✔
410
  pTbData->next = pMemTable->aBucket[idx];
26,004✔
411
  pMemTable->aBucket[idx] = pTbData;
26,004✔
412
  pMemTable->nTbData++;
26,004✔
413

414
  if (tRBTreePut(pMemTable->tbDataTree, pTbData->rbtn) == NULL) {
26,004!
415
    taosWUnLockLatch(&pMemTable->latch);
×
416
    code = TSDB_CODE_INTERNAL_ERROR;
×
417
    goto _exit;
×
418
  }
419

420
  taosWUnLockLatch(&pMemTable->latch);
26,003✔
421

422
_exit:
1,067,273✔
423
  if (code) {
1,067,273!
424
    *ppTbData = NULL;
×
425
  } else {
426
    *ppTbData = pTbData;
1,067,273✔
427
  }
428
  return code;
1,067,273✔
429
}
430

431
static void tbDataMovePosTo(STbData *pTbData, SMemSkipListNode **pos, STsdbRowKey *pKey, int32_t flags) {
1,771,887✔
432
  SMemSkipListNode *px;
433
  SMemSkipListNode *pn;
434
  STsdbRowKey       tKey;
435
  int32_t           backward = flags & SL_MOVE_BACKWARD;
1,771,887✔
436
  int32_t           fromPos = flags & SL_MOVE_FROM_POS;
1,771,887✔
437

438
  if (backward) {
1,771,887✔
439
    px = pTbData->sl.pTail;
1,088,297✔
440

441
    if (!fromPos) {
1,088,297!
442
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
1,903,439✔
443
        pos[iLevel] = px;
815,133✔
444
      }
445
    }
446

447
    if (pTbData->sl.level) {
1,088,297✔
448
      if (fromPos) px = pos[pTbData->sl.level - 1];
1,062,297!
449

450
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
5,685,728✔
451
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
4,623,700✔
452
        while (pn != pTbData->sl.pHead) {
4,766,146✔
453
          tsdbRowGetKey(&pn->row, &tKey);
4,739,658✔
454

455
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
4,739,534✔
456
          if (c <= 0) {
4,739,491✔
457
            break;
4,596,943✔
458
          } else {
459
            px = pn;
142,548✔
460
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
142,548✔
461
          }
462
        }
463

464
        pos[iLevel] = px;
4,623,431✔
465
      }
466
    }
467
  } else {
468
    px = pTbData->sl.pHead;
683,590✔
469

470
    if (!fromPos) {
683,590✔
471
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
1,667,732✔
472
        pos[iLevel] = px;
1,218,254✔
473
      }
474
    }
475

476
    if (pTbData->sl.level) {
683,590✔
477
      if (fromPos) px = pos[pTbData->sl.level - 1];
683,573✔
478

479
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
2,852,407✔
480
        pn = SL_GET_NODE_FORWARD(px, iLevel);
2,169,342✔
481
        while (pn != pTbData->sl.pTail) {
5,392,922✔
482
          tsdbRowGetKey(&pn->row, &tKey);
5,294,434✔
483

484
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
5,295,612✔
485
          if (c >= 0) {
5,298,789✔
486
            break;
2,070,346✔
487
          } else {
488
            px = pn;
3,228,443✔
489
            pn = SL_GET_NODE_FORWARD(px, iLevel);
3,228,443✔
490
          }
491
        }
492

493
        pos[iLevel] = px;
2,168,834✔
494
      }
495
    }
496
  }
497
}
1,771,110✔
498

499
static FORCE_INLINE int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl) {
500
  int8_t level = 1;
38,098,044✔
501
  int8_t tlevel = TMIN(pSl->maxLevel, pSl->level + 1);
38,098,044✔
502

503
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
50,728,505✔
504
    level++;
12,630,461✔
505
  }
506

507
  return level;
38,090,554✔
508
}
509
static int32_t tbDataDoPut(SMemTable *pMemTable, STbData *pTbData, SMemSkipListNode **pos, TSDBROW *pRow,
38,098,044✔
510
                           int8_t forward) {
511
  int32_t           code = 0;
38,098,044✔
512
  int8_t            level;
513
  SMemSkipListNode *pNode = NULL;
38,098,044✔
514
  SVBufPool        *pPool = pMemTable->pTsdb->pVnode->inUse;
38,098,044✔
515
  int64_t           nSize;
516

517
  // create node
518
  level = tsdbMemSkipListRandLevel(&pTbData->sl);
38,098,044✔
519
  nSize = SL_NODE_SIZE(level);
38,090,554✔
520
  if (pRow->type == TSDBROW_ROW_FMT) {
38,090,554!
521
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize + pRow->pTSRow->len);
38,095,772✔
522
  } else if (pRow->type == TSDBROW_COL_FMT) {
×
523
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize);
213✔
524
  }
525
  if (pNode == NULL) {
38,091,734!
526
    code = terrno;
×
527
    goto _exit;
×
528
  }
529

530
  pNode->level = level;
38,091,734✔
531
  pNode->row = *pRow;
38,091,734✔
532
  if (pRow->type == TSDBROW_ROW_FMT) {
38,091,734!
533
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
38,093,455✔
534
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
38,093,455✔
535
  }
536

537
  // set node
538
  if (forward) {
38,091,734✔
539
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
86,338,521✔
540
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
49,306,640✔
541
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
49,306,640✔
542
    }
543
  } else {
544
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
2,467,282✔
545
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
1,407,429✔
546
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
1,407,429✔
547
    }
548
  }
549

550
  // set forward and backward
551
  if (forward) {
38,091,734✔
552
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
86,353,478✔
553
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
49,312,454✔
554

555
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
49,312,454✔
556
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
49,329,144✔
557

558
      pos[iLevel] = pNode;
49,320,992✔
559
    }
560
  } else {
561
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
2,469,899✔
562
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
1,407,512✔
563

564
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
1,407,512✔
565
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
1,407,826✔
566

567
      pos[iLevel] = pNode;
1,410,651✔
568
    }
569
  }
570

571
  pTbData->sl.size++;
38,103,411✔
572
  if (pTbData->sl.level < pNode->level) {
38,103,411✔
573
    pTbData->sl.level = pNode->level;
89,955✔
574
  }
575

576
_exit:
38,013,456✔
577
  return code;
38,103,411✔
578
}
579

580
static int32_t tsdbInsertColDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
129✔
581
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
582
  int32_t code = 0;
129✔
583

584
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
129✔
585
  int32_t    nColData = TARRAY_SIZE(pSubmitTbData->aCol);
129✔
586
  SColData  *aColData = (SColData *)TARRAY_DATA(pSubmitTbData->aCol);
129✔
587

588
  // copy and construct block data
589
  SBlockData *pBlockData = vnodeBufPoolMalloc(pPool, sizeof(*pBlockData));
129✔
590
  if (pBlockData == NULL) {
129!
591
    code = terrno;
×
592
    goto _exit;
×
593
  }
594

595
  pBlockData->suid = pTbData->suid;
129✔
596
  pBlockData->uid = pTbData->uid;
129✔
597
  pBlockData->nRow = aColData[0].nVal;
129✔
598
  pBlockData->aUid = NULL;
129✔
599
  pBlockData->aVersion = vnodeBufPoolMalloc(pPool, aColData[0].nData);
129✔
600
  if (pBlockData->aVersion == NULL) {
129!
601
    code = terrno;
×
602
    goto _exit;
×
603
  }
604
  for (int32_t i = 0; i < pBlockData->nRow; i++) {  // todo: here can be optimized
342✔
605
    pBlockData->aVersion[i] = version;
213✔
606
  }
607

608
  pBlockData->aTSKEY = vnodeBufPoolMalloc(pPool, aColData[0].nData);
129✔
609
  if (pBlockData->aTSKEY == NULL) {
129!
610
    code = terrno;
×
611
    goto _exit;
×
612
  }
613
  memcpy(pBlockData->aTSKEY, aColData[0].pData, aColData[0].nData);
129✔
614

615
  pBlockData->nColData = nColData - 1;
129✔
616
  pBlockData->aColData = vnodeBufPoolMalloc(pPool, sizeof(SColData) * pBlockData->nColData);
129✔
617
  if (pBlockData->aColData == NULL) {
129!
618
    code = terrno;
×
619
    goto _exit;
×
620
  }
621

622
  for (int32_t iColData = 0; iColData < pBlockData->nColData; ++iColData) {
586✔
623
    code = tColDataCopy(&aColData[iColData + 1], &pBlockData->aColData[iColData], (xMallocFn)vnodeBufPoolMalloc, pPool);
457✔
624
    if (code) goto _exit;
457!
625
  }
626

627
  // loop to add each row to the skiplist
628
  SMemSkipListNode *pos[SL_MAX_LEVEL];
629
  TSDBROW           tRow = tsdbRowFromBlockData(pBlockData, 0);
129✔
630
  STsdbRowKey       key;
631

632
  // first row
633
  tsdbRowGetKey(&tRow, &key);
129✔
634
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
129✔
635
  if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0))) goto _exit;
129!
636
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
129✔
637

638
  // remain row
639
  ++tRow.iRow;
129✔
640
  if (tRow.iRow < pBlockData->nRow) {
129✔
641
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
152✔
642
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
121✔
643
    }
644

645
    while (tRow.iRow < pBlockData->nRow) {
115✔
646
      tsdbRowGetKey(&tRow, &key);
84✔
647

648
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
84!
649
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
×
650
      }
651

652
      if ((code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1))) goto _exit;
84!
653

654
      ++tRow.iRow;
84✔
655
    }
656
  }
657

658
  if (key.key.ts >= pTbData->maxKey) {
129✔
659
    pTbData->maxKey = key.key.ts;
128✔
660
  }
661

662
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config)) {
129!
663
    if (tsdbCacheColFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, pBlockData) != 0) {
×
664
      tsdbError("vgId:%d, failed to update cache data from table suid:%" PRId64 " uid:%" PRId64 " at version %" PRId64,
×
665
                TD_VID(pMemTable->pTsdb->pVnode), pTbData->suid, pTbData->uid, version);
666
    }
667
  }
668

669
  // SMemTable
670
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
129✔
671
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
129✔
672
  pMemTable->nRow += pBlockData->nRow;
129✔
673

674
  if (affectedRows) *affectedRows = pBlockData->nRow;
129!
675

676
_exit:
×
677
  return code;
129✔
678
}
679

680
static int32_t tsdbInsertRowDataToTable(SMemTable *pMemTable, STbData *pTbData, int64_t version,
1,065,589✔
681
                                        SSubmitTbData *pSubmitTbData, int32_t *affectedRows) {
682
  int32_t code = 0;
1,065,589✔
683

684
  int32_t           nRow = TARRAY_SIZE(pSubmitTbData->aRowP);
1,065,589✔
685
  SRow            **aRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
1,065,589✔
686
  STsdbRowKey       key;
687
  SMemSkipListNode *pos[SL_MAX_LEVEL];
688
  TSDBROW           tRow = {.type = TSDBROW_ROW_FMT, .version = version};
1,065,589✔
689
  int32_t           iRow = 0;
1,065,589✔
690

691
  // backward put first data
692
  tRow.pTSRow = aRow[iRow++];
1,065,589✔
693
  tsdbRowGetKey(&tRow, &key);
1,065,589✔
694
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
1,065,588✔
695
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
1,065,337✔
696
  if (code) goto _exit;
1,065,834!
697

698
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
1,065,834✔
699

700
  // forward put rest data
701
  if (iRow < nRow) {
1,065,834✔
702
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
1,377,574✔
703
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
1,083,554✔
704
    }
705

706
    while (iRow < nRow) {
37,340,148✔
707
      tRow.pTSRow = aRow[iRow];
37,045,815✔
708
      tsdbRowGetKey(&tRow, &key);
37,045,815✔
709

710
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
37,034,666✔
711
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
234,141✔
712
      }
713

714
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
37,034,641✔
715
      if (code) goto _exit;
37,046,128!
716

717
      iRow++;
37,046,128✔
718
    }
719
  }
720

721
  if (key.key.ts >= pTbData->maxKey) {
1,066,147✔
722
    pTbData->maxKey = key.key.ts;
1,046,575✔
723
  }
724
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config)) {
1,066,147✔
725
    TAOS_UNUSED(tsdbCacheRowFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, version, nRow, aRow));
13,450✔
726
  }
727

728
  // SMemTable
729
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
1,065,754✔
730
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
1,065,754✔
731
  pMemTable->nRow += nRow;
1,065,754✔
732

733
  if (affectedRows) *affectedRows = nRow;
1,065,754!
734

735
_exit:
×
736
  return code;
1,065,754✔
737
}
738

739
int32_t tsdbGetNRowsInTbData(STbData *pTbData) { return pTbData->sl.size; }
3✔
740

741
int32_t tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode) {
213,792✔
742
  int32_t code = 0;
213,792✔
743

744
  int32_t nRef = atomic_fetch_add_32(&pMemTable->nRef, 1);
213,792✔
745
  if (nRef <= 0) {
213,851!
746
    tsdbError("vgId:%d, memtable ref count is invalid, ref:%d", TD_VID(pMemTable->pTsdb->pVnode), nRef);
×
747
  }
748

749
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
213,851✔
750

751
_exit:
213,852✔
752
  return code;
213,852✔
753
}
754

755
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
221,242✔
756
  if (pNode) {
221,242✔
757
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
213,847✔
758
  }
759

760
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
221,255✔
761
    tsdbMemTableDestroy(pMemTable, proactive);
7,401✔
762
  }
763
}
221,262✔
764

765
static FORCE_INLINE int32_t tbDataPCmprFn(const void *p1, const void *p2) {
766
  STbData *pTbData1 = *(STbData **)p1;
767
  STbData *pTbData2 = *(STbData **)p2;
768

769
  if (pTbData1->suid < pTbData2->suid) {
770
    return -1;
771
  } else if (pTbData1->suid > pTbData2->suid) {
772
    return 1;
773
  }
774

775
  if (pTbData1->uid < pTbData2->uid) {
776
    return -1;
777
  } else if (pTbData1->uid > pTbData2->uid) {
778
    return 1;
779
  }
780

781
  return 0;
782
}
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