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

taosdata / TDengine / #3565

25 Dec 2024 05:34AM UTC coverage: 51.098% (-11.1%) from 62.21%
#3565

push

travis-ci

web-flow
Merge pull request #29316 from taosdata/enh/3.0/TD-33266

enh(ut):Add wal & config UT.

111558 of 284773 branches covered (39.17%)

Branch coverage included in aggregate %.

1 of 2 new or added lines in 2 files covered. (50.0%)

39015 existing lines in 102 files now uncovered.

177882 of 281666 relevant lines covered (63.15%)

15090998.35 hits per line

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

6.6
/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

UNCOV
41
static int32_t tTbDataCmprFn(const SRBTreeNode *n1, const SRBTreeNode *n2) {
×
UNCOV
42
  STbData *tbData1 = TCONTAINER_OF(n1, STbData, rbtn);
×
UNCOV
43
  STbData *tbData2 = TCONTAINER_OF(n2, STbData, rbtn);
×
UNCOV
44
  if (tbData1->suid < tbData2->suid) return -1;
×
UNCOV
45
  if (tbData1->suid > tbData2->suid) return 1;
×
UNCOV
46
  if (tbData1->uid < tbData2->uid) return -1;
×
UNCOV
47
  if (tbData1->uid > tbData2->uid) return 1;
×
48
  return 0;
×
49
}
50

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

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

81
  *ppMemTable = pMemTable;
20✔
82
  return code;
20✔
83

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

89
void tsdbMemTableDestroy(SMemTable *pMemTable, bool proactive) {
20✔
90
  if (pMemTable) {
20!
91
    vnodeBufPoolUnRef(pMemTable->pPool, proactive);
20✔
92
    taosMemoryFree(pMemTable->aBucket);
20!
93
    taosMemoryFree(pMemTable);
20!
94
  }
95
}
20✔
96

97
static FORCE_INLINE STbData *tsdbGetTbDataFromMemTableImpl(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
UNCOV
98
  STbData *pTbData = pMemTable->aBucket[TABS(uid) % pMemTable->nBucket];
×
99

UNCOV
100
  while (pTbData) {
×
UNCOV
101
    if (pTbData->uid == uid) break;
×
UNCOV
102
    pTbData = pTbData->next;
×
103
  }
104

UNCOV
105
  return pTbData;
×
106
}
107

UNCOV
108
STbData *tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid) {
×
109
  STbData *pTbData;
110

UNCOV
111
  taosRLockLatch(&pMemTable->latch);
×
UNCOV
112
  pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
×
UNCOV
113
  taosRUnLockLatch(&pMemTable->latch);
×
114

UNCOV
115
  return pTbData;
×
116
}
117

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

UNCOV
125
  if (tsBypassFlag & TSDB_BYPASS_RB_TSDB_WRITE_MEM) {
×
126
    goto _err;
×
127
  }
128

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

135
  // do insert impl
UNCOV
136
  if (pSubmitTbData->flags & SUBMIT_REQ_COLUMN_DATA_FORMAT) {
×
UNCOV
137
    code = tsdbInsertColDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
×
138
  } else {
UNCOV
139
    code = tsdbInsertRowDataToTable(pMemTable, pTbData, version, pSubmitTbData, affectedRows);
×
140
  }
UNCOV
141
  if (code) goto _err;
×
142

143
  // update
UNCOV
144
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
×
UNCOV
145
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
×
146

UNCOV
147
  return code;
×
148

149
_err:
×
150
  terrno = code;
×
151
  return code;
×
152
}
153

UNCOV
154
int32_t tsdbDeleteTableData(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey) {
×
UNCOV
155
  int32_t    code = 0;
×
UNCOV
156
  SMemTable *pMemTable = pTsdb->mem;
×
UNCOV
157
  STbData   *pTbData = NULL;
×
UNCOV
158
  SVBufPool *pPool = pTsdb->pVnode->inUse;
×
159

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

UNCOV
172
  code = tsdbGetOrCreateTbData(pMemTable, suid, uid, &pTbData);
×
UNCOV
173
  if (code) {
×
174
    goto _err;
×
175
  }
176

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

UNCOV
196
  pMemTable->nDel++;
×
UNCOV
197
  pMemTable->minVer = TMIN(pMemTable->minVer, version);
×
UNCOV
198
  pMemTable->maxVer = TMAX(pMemTable->maxVer, version);
×
199

UNCOV
200
  if (tsdbCacheDel(pTsdb, suid, uid, sKey, eKey) != 0) {
×
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

UNCOV
206
  tsdbTrace("vgId:%d, delete data from table suid:%" PRId64 " uid:%" PRId64 " skey:%" PRId64 " eKey:%" PRId64
×
207
            " at version %" PRId64,
208
            TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, version);
UNCOV
209
  return code;
×
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

UNCOV
218
int32_t tsdbTbDataIterCreate(STbData *pTbData, STsdbRowKey *pFrom, int8_t backward, STbDataIter **ppIter) {
×
UNCOV
219
  int32_t code = 0;
×
220

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

UNCOV
227
  tsdbTbDataIterOpen(pTbData, pFrom, backward, *ppIter);
×
228

UNCOV
229
_exit:
×
UNCOV
230
  return code;
×
231
}
232

UNCOV
233
void *tsdbTbDataIterDestroy(STbDataIter *pIter) {
×
UNCOV
234
  if (pIter) {
×
UNCOV
235
    taosMemoryFree(pIter);
×
236
  }
UNCOV
237
  return NULL;
×
238
}
239

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

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

UNCOV
269
bool tsdbTbDataIterNext(STbDataIter *pIter) {
×
UNCOV
270
  pIter->pRow = NULL;
×
UNCOV
271
  if (pIter->backward) {
×
UNCOV
272
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
×
273
      return false;
×
274
    }
275

UNCOV
276
    pIter->pNode = SL_GET_NODE_BACKWARD(pIter->pNode, 0);
×
UNCOV
277
    if (pIter->pNode == pIter->pTbData->sl.pHead) {
×
UNCOV
278
      return false;
×
279
    }
280
  } else {
UNCOV
281
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
×
282
      return false;
×
283
    }
284

UNCOV
285
    pIter->pNode = SL_GET_NODE_FORWARD(pIter->pNode, 0);
×
UNCOV
286
    if (pIter->pNode == pIter->pTbData->sl.pTail) {
×
UNCOV
287
      return false;
×
288
    }
289
  }
290

UNCOV
291
  return true;
×
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

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

UNCOV
334
  for (int32_t i = 0; i < pMemTable->nBucket; ++i) {
×
UNCOV
335
    STbData *pTbData = pMemTable->aBucket[i];
×
UNCOV
336
    while (pTbData) {
×
UNCOV
337
      code = (*cb)(pMemTable, pTbData->suid, pTbData->uid);
×
UNCOV
338
      if (code) {
×
339
        TAOS_RETURN(code);
×
340
      }
341

UNCOV
342
      pTbData = pTbData->next;
×
343
    }
344
  }
345

UNCOV
346
  return code;
×
347
}
348

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

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

UNCOV
359
  for (int32_t iBucket = 0; iBucket < pMemTable->nBucket; iBucket++) {
×
UNCOV
360
    STbData *pTbData = pMemTable->aBucket[iBucket];
×
361

UNCOV
362
    while (pTbData) {
×
UNCOV
363
      STbData *pNext = pTbData->next;
×
364

UNCOV
365
      int32_t idx = TABS(pTbData->uid) % nBucket;
×
UNCOV
366
      pTbData->next = aBucket[idx];
×
UNCOV
367
      aBucket[idx] = pTbData;
×
368

UNCOV
369
      pTbData = pNext;
×
370
    }
371
  }
372

UNCOV
373
  taosMemoryFree(pMemTable->aBucket);
×
UNCOV
374
  pMemTable->nBucket = nBucket;
×
UNCOV
375
  pMemTable->aBucket = aBucket;
×
376

UNCOV
377
_exit:
×
UNCOV
378
  return code;
×
379
}
380

UNCOV
381
static int32_t tsdbGetOrCreateTbData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData) {
×
UNCOV
382
  int32_t code = 0;
×
383

384
  // get
UNCOV
385
  STbData *pTbData = tsdbGetTbDataFromMemTableImpl(pMemTable, suid, uid);
×
UNCOV
386
  if (pTbData) goto _exit;
×
387

388
  // create
UNCOV
389
  SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse;
×
UNCOV
390
  int8_t     maxLevel = pMemTable->pTsdb->pVnode->config.tsdbCfg.slLevel;
×
391

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

UNCOV
415
    SL_NODE_BACKWARD(pTbData->sl.pHead, iLevel) = NULL;
×
UNCOV
416
    SL_NODE_FORWARD(pTbData->sl.pTail, iLevel) = NULL;
×
417
  }
UNCOV
418
  taosInitRWLatch(&pTbData->lock);
×
419

UNCOV
420
  taosWLockLatch(&pMemTable->latch);
×
421

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

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

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

UNCOV
441
  taosWUnLockLatch(&pMemTable->latch);
×
442

UNCOV
443
_exit:
×
UNCOV
444
  if (code) {
×
445
    *ppTbData = NULL;
×
446
  } else {
UNCOV
447
    *ppTbData = pTbData;
×
448
  }
UNCOV
449
  return code;
×
450
}
451

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

UNCOV
459
  if (backward) {
×
UNCOV
460
    px = pTbData->sl.pTail;
×
461

UNCOV
462
    if (!fromPos) {
×
UNCOV
463
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
×
UNCOV
464
        pos[iLevel] = px;
×
465
      }
466
    }
467

UNCOV
468
    if (pTbData->sl.level) {
×
UNCOV
469
      if (fromPos) px = pos[pTbData->sl.level - 1];
×
470

UNCOV
471
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
×
UNCOV
472
        pn = SL_GET_NODE_BACKWARD(px, iLevel);
×
UNCOV
473
        while (pn != pTbData->sl.pHead) {
×
UNCOV
474
          tsdbRowGetKey(&pn->row, &tKey);
×
475

UNCOV
476
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
×
UNCOV
477
          if (c <= 0) {
×
UNCOV
478
            break;
×
479
          } else {
UNCOV
480
            px = pn;
×
UNCOV
481
            pn = SL_GET_NODE_BACKWARD(px, iLevel);
×
482
          }
483
        }
484

UNCOV
485
        pos[iLevel] = px;
×
486
      }
487
    }
488
  } else {
UNCOV
489
    px = pTbData->sl.pHead;
×
490

UNCOV
491
    if (!fromPos) {
×
UNCOV
492
      for (int8_t iLevel = pTbData->sl.level; iLevel < pTbData->sl.maxLevel; iLevel++) {
×
UNCOV
493
        pos[iLevel] = px;
×
494
      }
495
    }
496

UNCOV
497
    if (pTbData->sl.level) {
×
UNCOV
498
      if (fromPos) px = pos[pTbData->sl.level - 1];
×
499

UNCOV
500
      for (int8_t iLevel = pTbData->sl.level - 1; iLevel >= 0; iLevel--) {
×
UNCOV
501
        pn = SL_GET_NODE_FORWARD(px, iLevel);
×
UNCOV
502
        while (pn != pTbData->sl.pTail) {
×
UNCOV
503
          tsdbRowGetKey(&pn->row, &tKey);
×
504

UNCOV
505
          int32_t c = tsdbRowKeyCmpr(&tKey, pKey);
×
UNCOV
506
          if (c >= 0) {
×
UNCOV
507
            break;
×
508
          } else {
UNCOV
509
            px = pn;
×
UNCOV
510
            pn = SL_GET_NODE_FORWARD(px, iLevel);
×
511
          }
512
        }
513

UNCOV
514
        pos[iLevel] = px;
×
515
      }
516
    }
517
  }
UNCOV
518
}
×
519

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

UNCOV
524
  while ((taosRandR(&pSl->seed) & 0x3) == 0 && level < tlevel) {
×
UNCOV
525
    level++;
×
526
  }
527

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

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

UNCOV
551
  pNode->level = level;
×
UNCOV
552
  pNode->row = *pRow;
×
UNCOV
553
  if (pRow->type == TSDBROW_ROW_FMT) {
×
UNCOV
554
    pNode->row.pTSRow = (SRow *)((char *)pNode + nSize);
×
UNCOV
555
    memcpy(pNode->row.pTSRow, pRow->pTSRow, pRow->pTSRow->len);
×
556
  }
557

558
  // set node
UNCOV
559
  if (forward) {
×
UNCOV
560
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
×
UNCOV
561
      SL_NODE_FORWARD(pNode, iLevel) = SL_NODE_FORWARD(pos[iLevel], iLevel);
×
UNCOV
562
      SL_NODE_BACKWARD(pNode, iLevel) = pos[iLevel];
×
563
    }
564
  } else {
UNCOV
565
    for (int8_t iLevel = 0; iLevel < level; iLevel++) {
×
UNCOV
566
      SL_NODE_FORWARD(pNode, iLevel) = pos[iLevel];
×
UNCOV
567
      SL_NODE_BACKWARD(pNode, iLevel) = SL_NODE_BACKWARD(pos[iLevel], iLevel);
×
568
    }
569
  }
570

571
  // set forward and backward
UNCOV
572
  if (forward) {
×
UNCOV
573
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
×
UNCOV
574
      SMemSkipListNode *pNext = pos[iLevel]->forwards[iLevel];
×
575

UNCOV
576
      SL_SET_NODE_FORWARD(pos[iLevel], iLevel, pNode);
×
UNCOV
577
      SL_SET_NODE_BACKWARD(pNext, iLevel, pNode);
×
578

UNCOV
579
      pos[iLevel] = pNode;
×
580
    }
581
  } else {
UNCOV
582
    for (int8_t iLevel = level - 1; iLevel >= 0; iLevel--) {
×
UNCOV
583
      SMemSkipListNode *pPrev = pos[iLevel]->forwards[pos[iLevel]->level + iLevel];
×
584

UNCOV
585
      SL_SET_NODE_FORWARD(pPrev, iLevel, pNode);
×
UNCOV
586
      SL_SET_NODE_BACKWARD(pos[iLevel], iLevel, pNode);
×
587

UNCOV
588
      pos[iLevel] = pNode;
×
589
    }
590
  }
591

UNCOV
592
  pTbData->sl.size++;
×
UNCOV
593
  if (pTbData->sl.level < pNode->level) {
×
UNCOV
594
    pTbData->sl.level = pNode->level;
×
595
  }
596

UNCOV
597
_exit:
×
UNCOV
598
  return code;
×
599
}
600

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

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

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

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

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

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

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

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

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

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

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

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

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

UNCOV
675
      ++tRow.iRow;
×
676
    }
677
  }
678

UNCOV
679
  if (key.key.ts >= pTbData->maxKey) {
×
UNCOV
680
    pTbData->maxKey = key.key.ts;
×
681
  }
682

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

UNCOV
695
  if (affectedRows) *affectedRows = pBlockData->nRow;
×
696

697
_exit:
×
UNCOV
698
  return code;
×
699
}
700

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

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

712
  // backward put first data
UNCOV
713
  tRow.pTSRow = aRow[iRow++];
×
UNCOV
714
  tsdbRowGetKey(&tRow, &key);
×
UNCOV
715
  tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_BACKWARD);
×
UNCOV
716
  code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 0);
×
UNCOV
717
  if (code) goto _exit;
×
718

UNCOV
719
  pTbData->minKey = TMIN(pTbData->minKey, key.key.ts);
×
720

721
  // forward put rest data
UNCOV
722
  if (iRow < nRow) {
×
UNCOV
723
    for (int8_t iLevel = pos[0]->level; iLevel < pTbData->sl.maxLevel; iLevel++) {
×
UNCOV
724
      pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel);
×
725
    }
726

UNCOV
727
    while (iRow < nRow) {
×
UNCOV
728
      tRow.pTSRow = aRow[iRow];
×
UNCOV
729
      tsdbRowGetKey(&tRow, &key);
×
730

UNCOV
731
      if (SL_NODE_FORWARD(pos[0], 0) != pTbData->sl.pTail) {
×
UNCOV
732
        tbDataMovePosTo(pTbData, pos, &key, SL_MOVE_FROM_POS);
×
733
      }
734

UNCOV
735
      code = tbDataDoPut(pMemTable, pTbData, pos, &tRow, 1);
×
UNCOV
736
      if (code) goto _exit;
×
737

UNCOV
738
      iRow++;
×
739
    }
740
  }
741

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

749
  // SMemTable
UNCOV
750
  pMemTable->minKey = TMIN(pMemTable->minKey, pTbData->minKey);
×
UNCOV
751
  pMemTable->maxKey = TMAX(pMemTable->maxKey, pTbData->maxKey);
×
UNCOV
752
  pMemTable->nRow += nRow;
×
753

UNCOV
754
  if (affectedRows) *affectedRows = nRow;
×
755

756
_exit:
×
UNCOV
757
  return code;
×
758
}
759

UNCOV
760
int32_t tsdbGetNRowsInTbData(STbData *pTbData) { return pTbData->sl.size; }
×
761

UNCOV
762
int32_t tsdbRefMemTable(SMemTable *pMemTable, SQueryNode *pQNode) {
×
UNCOV
763
  int32_t code = 0;
×
764

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

UNCOV
770
  vnodeBufPoolRegisterQuery(pMemTable->pPool, pQNode);
×
771

UNCOV
772
_exit:
×
UNCOV
773
  return code;
×
774
}
775

776
void tsdbUnrefMemTable(SMemTable *pMemTable, SQueryNode *pNode, bool proactive) {
2✔
777
  if (pNode) {
2!
UNCOV
778
    vnodeBufPoolDeregisterQuery(pMemTable->pPool, pNode, proactive);
×
779
  }
780

781
  if (atomic_sub_fetch_32(&pMemTable->nRef, 1) == 0) {
2!
782
    tsdbMemTableDestroy(pMemTable, proactive);
2✔
783
  }
784
}
2✔
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