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

taosdata / TDengine / #3568

27 Dec 2024 02:27PM UTC coverage: 63.286% (+0.6%) from 62.733%
#3568

push

travis-ci

web-flow
Merge pull request #29378 from taosdata/revert-29377-revert-29165-enh/TD-29974-improve-trans

Revert "Revert "Enh:[td 29974]improve trans""

139941 of 284289 branches covered (49.22%)

Branch coverage included in aggregate %.

105 of 355 new or added lines in 12 files covered. (29.58%)

4077 existing lines in 132 files now uncovered.

218122 of 281494 relevant lines covered (77.49%)

19355607.91 hits per line

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

77.39
/source/dnode/vnode/src/tsdb/tsdbMemTable.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tsdb.h"
17
#include "util/tsimplehash.h"
18

19
#define MEM_MIN_HASH 1024
20
#define SL_MAX_LEVEL 5
21

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

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

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

41
static int32_t tTbDataCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
2,186,475✔
42
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
2,186,475✔
43
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
2,186,475✔
44
  if (tbData1->suid < tbData2->suid) return -1;
2,186,475✔
45
  if (tbData1->suid > tbData2->suid) return 1;
2,112,864✔
46
  if (tbData1->uid < tbData2->uid) return -1;
2,047,488✔
47
  if (tbData1->uid > tbData2->uid) return 1;
1,627,135!
48
  return 0;
×
49
}
50

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

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

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

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

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

100
  while (pTbData) {
25,533,253✔
101
    if (pTbData->uid == uid) break;
18,492,227✔
102
    pTbData = pTbData->next;
58,868✔
103
  }
104

105
  return pTbData;
25,474,385✔
106
}
107

108
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
11,758,261✔
109
  STbData *pTbData;
110

111
  taosRLockLatch(&pMemTable->latch);
11,758,261✔
112
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
11,768,160✔
113
  taosRUnLockLatch(&pMemTable->latch);
11,768,160✔
114

115
  return pTbData;
11,768,685✔
116
}
117

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

125
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
13,645,374!
126
    goto _err;
×
127
  }
128

129
  // create/get STbData to op
130
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
13,645,374✔
131
  if (code) {
13,643,701!
132
    goto _err;
×
133
  }
134

135
  // do insert impl
136
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
13,643,701✔
137
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
134✔
138
  } else {
139
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
13,643,567✔
140
  }
141
  if (code) goto _err;
13,648,344!
142

143
  // update
144
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
13,648,344✔
145
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
13,648,344✔
146

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

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

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

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

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

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

221
  (*ppIter) = (STbDataIter *)taosMemoryCalloc(1, sizeof(STbDataIter));
3,611,053!
222
  if ((*ppIter) == NULL) {
3,611,395!
223
    code = terrno;
×
224
    goto _exit;
×
225
  }
226

227
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
3,611,395✔
228

229
_exit:
3,609,666✔
230
  return code;
3,609,666✔
231
}
232

233
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
3,611,230✔
234
  if (pIter) {
3,611,230!
235
    taosMemoryFree(pIter);
3,611,294!
236
  }
237
  return NULL;
3,612,234✔
238
}
239

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

245
  pHead = pTbData->sl.pHead;
4,710,649✔
246
  pTail = pTbData->sl.pTail;
4,710,649✔
247
  pIter->pTbData = pTbData;
4,710,649✔
248
  pIter->backward = backward;
4,710,649✔
249
  pIter->pRow = NULL;
4,710,649✔
250
  if (pFrom == NULL) {
4,710,649✔
251
    // create from head or tail
252
    if (backward) {
8,219!
253
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
8,222✔
254
    } else {
UNCOV
255
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
×
256
    }
257
  } else {
258
    // create from a key
259
    if (backward) {
4,702,430✔
260
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
774,252✔
261
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
774,169✔
262
    } else {
263
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
3,928,178✔
264
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
3,921,200✔
265
    }
266
  }
267
}
4,702,823✔
268

269
bool tsdbTbDataIterNext(STbDataIter *pIter) {
1,971,541,582✔
270
  pIter->pRow = NULL;
1,971,541,582✔
271
  if (pIter->backward) {
1,971,541,582✔
272
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
133,732,401!
273
      return false;
×
274
    }
275

276
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
133,732,401✔
277
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
133,495,016✔
278
      return false;
278,144✔
279
    }
280
  } else {
281
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
1,837,809,181!
282
      return false;
×
283
    }
284

285
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
1,837,809,181✔
286
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
1,837,098,642✔
287
      return false;
2,232,808✔
288
    }
289
  }
290

291
  return true;
1,968,082,706✔
292
}
293

294
int64_t tsdbCountTbDataRows(STbData *pTbData) {
×
295
  SMemSkipListNode *pNode = pTbData->sl.pHead;
×
296
  int64_t           rowsNum = 0;
×
297

298
  while (NULL != pNode) {
×
299
    pNode = SL_GET_NODE_FORWARD(pNode, 0);
×
300
    if (pNode == pTbData->sl.pTail) {
×
301
      return rowsNum;
×
302
    }
303

304
    rowsNum++;
×
305
  }
306

307
  return rowsNum;
×
308
}
309

310
void tsdbMemTableCountRows(SMemTable *pMemTable, SSHashObj *pTableMap, int64_t *rowsNum) {
×
311
  taosRLockLatch(&pMemTable->latch);
×
312
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
×
313
    STbData *pTbData = pMemTable->aBucket[i];
×
314
    while (pTbData) {
×
315
      void *p = tSimpleHashGet(pTableMap, &pTbData->uid, sizeof(pTbData->uid));
×
316
      if (p == NULL) {
×
317
        pTbData = pTbData->next;
×
318
        continue;
×
319
      }
320

321
      *rowsNum += tsdbCountTbDataRows(pTbData);
×
322
      pTbData = pTbData->next;
×
323
    }
324
  }
325
  taosRUnLockLatch(&pMemTable->latch);
×
326
}
×
327

328
typedef int32_t (*__tsdb_cache_update)(SMemTable *imem, int64_t suid, int64_t uid);
329

330
int32_t tsdbMemTableSaveToCache(SMemTable *pMemTable, void *func) {
310✔
331
  int32_t             code = 0;
310✔
332
  __tsdb_cache_update cb = (__tsdb_cache_update)func;
310✔
333

334
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
317,745✔
335
    STbData *pTbData = pMemTable->aBucket[i];
317,435✔
336
    while (pTbData) {
318,822✔
337
      code = (*cb)(pMemTable, pTbData->suid, pTbData->uid);
1,387✔
338
      if (code) {
1,387!
339
        TAOS_RETURN(code);
×
340
      }
341

342
      pTbData = pTbData->next;
1,387✔
343
    }
344
  }
345

346
  return code;
310✔
347
}
348

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

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

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

362
    while (pTbData) {
110,592✔
363
      STbData *pNext = pTbData->next;
55,296✔
364

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

369
      pTbData = pNext;
55,296✔
370
    }
371
  }
372

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

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

381
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
13,706,225✔
382
  int32_t code = 0;
13,706,225✔
383

384
  // get
385
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
13,706,225✔
386
  if (pTbData) goto _exit;
13,706,225✔
387

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

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

415
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
910,481✔
416
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
910,481✔
417
  }
418
  taosInitRWLatch(&pTbData->lock);
182,102✔
419

420
  taosWLockLatch(&pMemTable->latch);
182,097✔
421

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

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

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

441
  taosWUnLockLatch(&pMemTable->latch);
182,096✔
442

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

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

459
  if (backward) {
39,463,958✔
460
    px = pTbData->sl.pTail;
14,415,431✔
461

462
    if (!fromPos) {
14,415,431!
463
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
19,739,010✔
464
        pos[iLevel] = px;
5,323,231✔
465
      }
466
    }
467

468
    if (pTbData->sl.level) {
14,415,431✔
469
      if (fromPos) px = pos[pTbData->sl.level - 1];
14,234,823!
470

471
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
80,802,550✔
472
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
66,584,563✔
473
        while (pn != pTbData->sl.pHead) {
75,593,349✔
474
          tsdbRowGetKey(&pn->row, &tKey);
74,485,152✔
475

476
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
74,483,238✔
477
          if (c <= 0) {
74,481,003✔
478
            break;
65,459,530✔
479
          } else {
480
            px = pn;
9,021,473✔
481
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
9,021,473✔
482
          }
483
        }
484

485
        pos[iLevel] = px;
66,567,727✔
486
      }
487
    }
488
  } else {
489
    px = pTbData->sl.pHead;
25,048,527✔
490

491
    if (!fromPos) {
25,048,527✔
492
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
7,807,285✔
493
        pos[iLevel] = px;
3,878,710✔
494
      }
495
    }
496

497
    if (pTbData->sl.level) {
25,048,527✔
498
      if (fromPos) px = pos[pTbData->sl.level - 1];
25,043,741✔
499

500
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
146,137,182✔
501
        pn = SL_GET_NODE_FORWARD(px, iLevel);
121,108,245✔
502
        while (pn != pTbData->sl.pTail) {
396,848,969✔
503
          tsdbRowGetKey(&pn->row, &tKey);
393,641,045✔
504

505
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
393,803,243✔
506
          if (c >= 0) {
394,192,066✔
507
            break;
117,885,517✔
508
          } else {
509
            px = pn;
276,306,549✔
510
            pn = SL_GET_NODE_FORWARD(px, iLevel);
276,306,549✔
511
          }
512
        }
513

514
        pos[iLevel] = px;
121,093,441✔
515
      }
516
    }
517
  }
518
}
39,432,318✔
519

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

524
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
1,056,635,188✔
525
    level++;
263,247,295✔
526
  }
527

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

538
  // create node
539
  level = tsdbMemSkipListRandLevel(&pTbData->sl);
793,387,893✔
540
  nSize = SL_NODE_SIZE(level);
793,216,126✔
541
  if (pRow->type == TSDBROW_ROW_FMT) {
793,216,126!
542
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize + pRow->pTSRow->len);
793,309,766✔
543
  } else if (pRow->type == TSDBROW_COL_FMT) {
×
544
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize);
224✔
545
  }
546
  if (pNode == NULL) {
793,254,526!
547
    code = terrno;
×
548
    goto _exit;
×
549
  }
550

551
  pNode->level = level;
793,254,526✔
552
  pNode->row = *pRow;
793,254,526✔
553
  if (pRow->type == TSDBROW_ROW_FMT) {
793,254,526!
554
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
793,268,221✔
555
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
793,268,221✔
556
  }
557

558
  // set node
559
  if (forward) {
793,254,526✔
560
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
1,817,996,338✔
561
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
1,038,348,497✔
562
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
1,038,348,497✔
563
    }
564
  } else {
565
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
31,687,636✔
566
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
18,080,951✔
567
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
18,080,951✔
568
    }
569
  }
570

571
  // set forward and backward
572
  if (forward) {
793,254,526✔
573
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
1,818,344,141✔
574
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
1,038,389,685✔
575

576
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
1,038,389,685✔
577
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
1,038,574,903✔
578

579
      pos[iLevel] = pNode;
1,038,658,470✔
580
    }
581
  } else {
582
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
31,689,047✔
583
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
18,084,776✔
584

585
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
18,084,776✔
586
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
18,105,225✔
587

588
      pos[iLevel] = pNode;
18,120,192✔
589
    }
590
  }
591

592
  pTbData->sl.size++;
793,558,727✔
593
  if (pTbData->sl.level < pNode->level) {
793,558,727✔
594
    pTbData->sl.level = pNode->level;
638,014✔
595
  }
596

597
_exit:
792,920,713✔
598
  return code;
793,558,727✔
599
}
600

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

683
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
134!
684
    if (tsdbCacheColFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, pBlockData) != 0) {
×
685
      tsdbError("vgId:%d, failed to update cache data from table suid:%" PRId64 " uid:%" PRId64 " at version %" PRId64,
×
686
                TD_VID(pMemTable->pTsdb->pVnode), pTbData->suid, pTbData->uid, version);
687
    }
688
  }
689

690
  // SMemTable
691
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
134✔
692
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
134✔
693
  pMemTable->nRow += pBlockData->nRow;
134✔
694

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

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

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

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

712
  // backward put first data
713
  tRow.pTSRow = aRow[iRow++];
13,643,312✔
714
  tsdbRowGetKey(&tRow, &key);
13,643,312✔
715
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
13,641,178✔
716
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
13,616,162✔
717
  if (code) goto _exit;
13,649,001!
718

719
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
13,649,001✔
720

721
  // forward put rest data
722
  if (iRow < nRow) {
13,649,001✔
723
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
5,052,910✔
724
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
3,978,530✔
725
    }
726

727
    while (iRow < nRow) {
780,940,430✔
728
      tRow.pTSRow = aRow[iRow];
779,873,937✔
729
      tsdbRowGetKey(&tRow, &key);
779,873,937✔
730

731
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
779,815,174✔
732
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
21,120,735✔
733
      }
734

735
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
779,812,713✔
736
      if (code) goto _exit;
779,866,050!
737

738
      iRow++;
779,866,050✔
739
    }
740
  }
741

742
  if (key.key.ts >= pTbData->maxKey) {
13,641,114✔
743
    pTbData->maxKey = key.key.ts;
13,494,203✔
744
  }
745
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
13,641,114!
746
    TAOS_UNUSED(tsdbCacheRowFormatUpdate(pMemTable->pTsdb, pTbData->suid, pTbData->uid, version, nRow, aRow));
×
747
  }
748

749
  // SMemTable
750
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
13,648,512✔
751
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
13,648,512✔
752
  pMemTable->nRow += nRow;
13,648,512✔
753

754
  if (affectedRows) *affectedRows = nRow;
13,648,512!
755

756
_exit:
×
757
  return code;
13,648,512✔
758
}
759

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

762
int32_t tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode) {
3,293,126✔
763
  int32_t code = 0;
3,293,126✔
764

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

770
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
3,296,436✔
771

772
_exit:
3,296,077✔
773
  return code;
3,296,077✔
774
}
775

776
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
3,325,456✔
777
  if (pNode) {
3,325,456✔
778
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
3,296,300✔
779
  }
780

781
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
3,325,853✔
782
    tsdbMemTableDestroy(pMemTable, proactive);
29,382✔
783
  }
784
}
3,326,174✔
785

786
static FORCE_INLINE int32_t tbDataPCmprFn(const void *p1, const void *p2) {
787
  STbData *pTbData1 = *(STbData **)p1;
788
  STbData *pTbData2 = *(STbData **)p2;
789

790
  if (pTbData1->suid < pTbData2->suid) {
791
    return -1;
792
  } else if (pTbData1->suid > pTbData2->suid) {
793
    return 1;
794
  }
795

796
  if (pTbData1->uid < pTbData2->uid) {
797
    return -1;
798
  } else if (pTbData1->uid > pTbData2->uid) {
799
    return 1;
800
  }
801

802
  return 0;
803
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc