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

taosdata / TDengine / #3615

18 Feb 2025 07:41AM UTC coverage: 62.953% (+1.6%) from 61.4%
#3615

push

travis-ci

web-flow
Merge pull request #29812 from taosdata/doc/analysis

doc: update tdgpt doc.

146885 of 299602 branches covered (49.03%)

Branch coverage included in aggregate %.

230802 of 300346 relevant lines covered (76.85%)

17263824.17 hits per line

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

77.25
/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,851,066✔
42
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
2,851,066✔
43
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
2,851,066✔
44
  if (tbData1->suid < tbData2->suid) return -1;
2,851,066✔
45
  if (tbData1->suid > tbData2->suid) return 1;
2,741,926✔
46
  if (tbData1->uid < tbData2->uid) return -1;
2,677,779✔
47
  if (tbData1->uid > tbData2->uid) return 1;
2,171,460!
48
  return 0;
×
49
}
50

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

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

81
  *ppMemTable = pMemTable;
28,502✔
82
  return code;
28,502✔
83

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

89
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
28,503✔
90
  if (pMemTable) {
28,503!
91
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
28,503✔
92
    taosMemoryFree(pMemTable->aBucket);
28,503!
93
    taosMemoryFree(pMemTable);
28,503!
94
  }
95
}
28,503✔
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];
27,599,063✔
99

100
  while (pTbData) {
27,665,301✔
101
    if (pTbData->uid == uid) break;
18,277,539✔
102
    pTbData = pTbData->next;
66,238✔
103
  }
104

105
  return pTbData;
27,599,063✔
106
}
107

108
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
14,656,033✔
109
  STbData *pTbData;
110

111
  taosRLockLatch(&pMemTable->latch);
14,656,033✔
112
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
14,668,319✔
113
  taosRUnLockLatch(&pMemTable->latch);
14,668,319✔
114

115
  return pTbData;
14,669,520✔
116
}
117

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

125
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
12,870,878!
126
    goto _err;
×
127
  }
128

129
  // create/get STbData to op
130
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
12,870,878✔
131
  if (code) {
12,871,074!
132
    goto _err;
×
133
  }
134

135
  // do insert impl
136
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
12,871,074✔
137
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
135✔
138
  } else {
139
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
12,870,939✔
140
  }
141
  if (code) goto _err;
12,871,277!
142

143
  // update
144
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
12,871,277✔
145
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
12,871,277✔
146

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

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

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

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

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

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

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

227
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
3,962,834✔
228

229
_exit:
3,962,048✔
230
  return code;
3,962,048✔
231
}
232

233
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
3,963,729✔
234
  if (pIter) {
3,963,729!
235
    taosMemoryFree(pIter);
3,963,797!
236
  }
237
  return NULL;
3,964,354✔
238
}
239

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

245
  pHead = pTbData->sl.pHead;
5,007,795✔
246
  pTail = pTbData->sl.pTail;
5,007,795✔
247
  pIter->pTbData = pTbData;
5,007,795✔
248
  pIter->backward = backward;
5,007,795✔
249
  pIter->pRow = NULL;
5,007,795✔
250
  if (pFrom == NULL) {
5,007,795✔
251
    // create from head or tail
252
    if (backward) {
5,889!
253
      pIter->pNode = SL_GET_NODE_BACKWARD(pTbData->sl.pTail, 0);
5,889✔
254
    } else {
255
      pIter->pNode = SL_GET_NODE_FORWARD(pTbData->sl.pHead, 0);
×
256
    }
257
  } else {
258
    // create from a key
259
    if (backward) {
5,001,906✔
260
      tbDataMovePosTo(pTbData, pos, pFrom, SL_MOVE_BACKWARD);
742,135✔
261
      pIter->pNode = SL_GET_NODE_BACKWARD(pos[0], 0);
742,653✔
262
    } else {
263
      tbDataMovePosTo(pTbData, pos, pFrom, 0);
4,259,771✔
264
      pIter->pNode = SL_GET_NODE_FORWARD(pos[0], 0);
4,253,948✔
265
    }
266
  }
267
}
5,000,915✔
268

269
bool tsdbTbDataIterNext(STbDataIter *pIter) {
1,671,794,607✔
270
  pIter->pRow = NULL;
1,671,794,607✔
271
  if (pIter->backward) {
1,671,794,607✔
272
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
118,708,872!
273
      return false;
×
274
    }
275

276
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
118,708,872✔
277
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
118,554,137✔
278
      return false;
219,142✔
279
    }
280
  } else {
281
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
1,553,085,735!
282
      return false;
×
283
    }
284

285
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
1,553,085,735✔
286
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
1,553,180,308✔
287
      return false;
2,233,214✔
288
    }
289
  }
290

291
  return true;
1,669,282,089✔
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) {
334✔
331
  int32_t             code = 0;
334✔
332
  __tsdb_cache_update cb = (__tsdb_cache_update)func;
334✔
333

334
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
342,280✔
335
    STbData *pTbData = pMemTable->aBucket[i];
341,946✔
336
    while (pTbData) {
343,652✔
337
      code = (*cb)(pMemTable, pTbData->suid, pTbData->uid);
1,706✔
338
      if (code) {
1,706!
339
        TAOS_RETURN(code);
×
340
      }
341

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

346
  return code;
334✔
347
}
348

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

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

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

362
    while (pTbData) {
178,176✔
363
      STbData *pNext = pTbData->next;
89,088✔
364

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

369
      pTbData = pNext;
89,088✔
370
    }
371
  }
372

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

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

381
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
12,930,744✔
382
  int32_t code = 0;
12,930,744✔
383

384
  // get
385
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
12,930,744✔
386
  if (pTbData) goto _exit;
12,930,744✔
387

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

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

415
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
1,062,791✔
416
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
1,062,791✔
417
  }
418
  taosInitRWLatch(&pTbData->lock);
212,560✔
419

420
  taosWLockLatch(&pMemTable->latch);
212,558✔
421

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

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

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

441
  taosWUnLockLatch(&pMemTable->latch);
212,559✔
442

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

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

459
  if (backward) {
38,121,301✔
460
    px = pTbData->sl.pTail;
13,613,462✔
461

462
    if (!fromPos) {
13,613,462!
463
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
18,326,753✔
464
        pos[iLevel] = px;
4,713,219✔
465
      }
466
    }
467

468
    if (pTbData->sl.level) {
13,613,462✔
469
      if (fromPos) px = pos[pTbData->sl.level - 1];
13,403,430!
470

471
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
76,748,505✔
472
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
63,345,116✔
473
        while (pn != pTbData->sl.pHead) {
72,323,514✔
474
          tsdbRowGetKey(&pn->row, &tKey);
71,167,943✔
475

476
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
71,174,363✔
477
          if (c <= 0) {
71,179,650✔
478
            break;
62,189,504✔
479
          } else {
480
            px = pn;
8,990,146✔
481
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
8,990,146✔
482
          }
483
        }
484

485
        pos[iLevel] = px;
63,345,075✔
486
      }
487
    }
488
  } else {
489
    px = pTbData->sl.pHead;
24,507,839✔
490

491
    if (!fromPos) {
24,507,839✔
492
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
8,578,087✔
493
        pos[iLevel] = px;
4,317,797✔
494
      }
495
    }
496

497
    if (pTbData->sl.level) {
24,507,839✔
498
      if (fromPos) px = pos[pTbData->sl.level - 1];
24,497,258✔
499

500
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
141,761,955✔
501
        pn = SL_GET_NODE_FORWARD(px, iLevel);
117,271,774✔
502
        while (pn != pTbData->sl.pTail) {
374,049,196✔
503
          tsdbRowGetKey(&pn->row, &tKey);
369,993,712✔
504

505
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
370,492,999✔
506
          if (c >= 0) {
370,922,957✔
507
            break;
113,209,213✔
508
          } else {
509
            px = pn;
257,713,744✔
510
            pn = SL_GET_NODE_FORWARD(px, iLevel);
257,713,744✔
511
          }
512
        }
513

514
        pos[iLevel] = px;
117,264,697✔
515
      }
516
    }
517
  }
518
}
38,114,183✔
519

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

524
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
863,695,049✔
525
    level++;
215,119,984✔
526
  }
527

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

538
  // create node
539
  level = tsdbMemSkipListRandLevel(&pTbData->sl);
648,575,065✔
540
  nSize = SL_NODE_SIZE(level);
648,475,344✔
541
  if (pRow->type == TSDBROW_ROW_FMT) {
648,475,344!
542
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize + pRow->pTSRow->len);
648,520,219✔
543
  } else if (pRow->type == TSDBROW_COL_FMT) {
×
544
    pNode = (SMemSkipListNode *)vnodeBufPoolMallocAligned(pPool, nSize);
234✔
545
  }
546
  if (pNode == NULL) {
648,512,101!
547
    code = terrno;
×
548
    goto _exit;
×
549
  }
550

551
  pNode->level = level;
648,512,101✔
552
  pNode->row = *pRow;
648,512,101✔
553
  if (pRow->type == TSDBROW_ROW_FMT) {
648,512,101!
554
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
648,515,897✔
555
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
648,515,897✔
556
  }
557

558
  // set node
559
  if (forward) {
648,512,101✔
560
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
1,482,185,562✔
561
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
846,514,507✔
562
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
846,514,507✔
563
    }
564
  } else {
565
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
29,903,279✔
566
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
17,062,233✔
567
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
17,062,233✔
568
    }
569
  }
570

571
  // set forward and backward
572
  if (forward) {
648,512,101✔
573
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
1,482,314,970✔
574
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
846,558,101✔
575

576
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
846,558,101✔
577
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
846,671,233✔
578

579
      pos[iLevel] = pNode;
846,643,874✔
580
    }
581
  } else {
582
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
29,934,212✔
583
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
17,062,418✔
584

585
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
17,062,418✔
586
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
17,063,476✔
587

588
      pos[iLevel] = pNode;
17,093,207✔
589
    }
590
  }
591

592
  pTbData->sl.size++;
648,628,663✔
593
  if (pTbData->sl.level < pNode->level) {
648,628,663✔
594
    pTbData->sl.level = pNode->level;
742,813✔
595
  }
596

597
_exit:
647,885,850✔
598
  return code;
648,628,663✔
599
}
600

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

712
  // backward put first data
713
  tRow.pTSRow = aRow[iRow++];
12,870,866✔
714
  tsdbRowGetKey(&tRow, &key);
12,870,866✔
715
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
12,870,788✔
716
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
12,870,039✔
717
  if (code) goto _exit;
12,871,294!
718

719
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
12,871,294✔
720

721
  // forward put rest data
722
  if (iRow < nRow) {
12,871,294✔
723
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
5,138,516✔
724
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
4,048,263✔
725
    }
726

727
    while (iRow < nRow) {
636,839,342✔
728
      tRow.pTSRow = aRow[iRow];
635,756,855✔
729
      tsdbRowGetKey(&tRow, &key);
635,756,855✔
730

731
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
635,724,996✔
732
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
20,247,907✔
733
      }
734

735
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
635,723,894✔
736
      if (code) goto _exit;
635,749,089!
737

738
      iRow++;
635,749,089✔
739
    }
740
  }
741

742
  if (key.key.ts >= pTbData->maxKey) {
12,863,528✔
743
    pTbData->maxKey = key.key.ts;
12,739,169✔
744
  }
745
  if (!TSDB_CACHE_NO(pMemTable->pTsdb->pVnode->config) && !tsUpdateCacheBatch) {
12,863,528!
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);
12,871,154✔
751
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
12,871,154✔
752
  pMemTable->nRow += nRow;
12,871,154✔
753

754
  if (affectedRows) *affectedRows = nRow;
12,871,154!
755

756
_exit:
×
757
  return code;
12,871,154✔
758
}
759

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

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

765
  int32_t nRef = atomic_fetch_add_32(&pMemTable->nRef, 1);
3,925,284✔
766
  if (nRef <= 0) {
3,929,817!
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,929,817✔
771

772
_exit:
3,929,432✔
773
  return code;
3,929,432✔
774
}
775

776
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
3,946,846✔
777
  if (pNode) {
3,946,846✔
778
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
3,929,669✔
779
  }
780

781
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
3,947,327✔
782
    tsdbMemTableDestroy(pMemTable, proactive);
17,392✔
783
  }
784
}
3,947,643✔
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