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

taosdata / TDengine / #4997

20 Mar 2026 06:10AM UTC coverage: 71.739% (-0.3%) from 72.069%
#4997

push

travis-ci

web-flow
feat: add query phase tracking for SHOW QUERIES (#34706)

148 of 183 new or added lines in 10 files covered. (80.87%)

9273 existing lines in 172 files now uncovered.

244572 of 340921 relevant lines covered (71.74%)

133392941.95 hits per line

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

86.25
/source/dnode/vnode/src/vnd/vnodeHash.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 "vnodeHash.h"
17

18
#define VNODE_HASH_DEFAULT_NUM_BUCKETS 1024
19

20
typedef struct SVHashEntry SVHashEntry;
21

22
struct SVHashEntry {
23
  SVHashEntry* next;
24
  void*        obj;
25
};
26

27
static void vHashRehash(SVHashTable* ht, uint32_t newNumBuckets) {
15,168✔
28
  SVHashEntry** newBuckets = (SVHashEntry**)taosMemoryCalloc(newNumBuckets, sizeof(SVHashEntry*));
15,168✔
29
  if (newBuckets == NULL) {
15,168✔
30
    return;
×
31
  }
32

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
23,313,216✔
34
    SVHashEntry* entry = ht->buckets[i];
23,298,048✔
35
    while (entry != NULL) {
34,939,488✔
36
      SVHashEntry* next = entry->next;
11,641,440✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
11,641,440✔
38
      entry->next = newBuckets[bucketIndex];
11,641,440✔
39
      newBuckets[bucketIndex] = entry;
11,641,440✔
40
      entry = next;
11,641,440✔
41
    }
42
  }
43

44
  taosMemoryFree(ht->buckets);
15,168✔
45
  ht->buckets = newBuckets;
15,168✔
46
  ht->numBuckets = newNumBuckets;
15,168✔
47

48
  return;
15,168✔
49
}
50

51
int32_t vHashInit(SVHashTable** ht, uint32_t (*hash)(const void*), int32_t (*compare)(const void*, const void*)) {
9,020,807✔
52
  if (ht == NULL || hash == NULL || compare == NULL) {
9,020,807✔
UNCOV
53
    return TSDB_CODE_INVALID_PARA;
×
54
  }
55

56
  (*ht) = (SVHashTable*)taosMemoryMalloc(sizeof(SVHashTable));
9,022,090✔
57
  if (*ht == NULL) {
9,022,090✔
58
    return terrno;
×
59
  }
60

61
  (*ht)->hash = hash;
9,021,527✔
62
  (*ht)->compare = compare;
9,022,090✔
63
  (*ht)->numEntries = 0;
9,021,527✔
64
  (*ht)->numBuckets = VNODE_HASH_DEFAULT_NUM_BUCKETS;
9,020,296✔
65
  (*ht)->buckets = (SVHashEntry**)taosMemoryCalloc((*ht)->numBuckets, sizeof(SVHashEntry*));
9,020,807✔
66
  if ((*ht)->buckets == NULL) {
9,022,090✔
67
    taosMemoryFree(*ht);
×
68
    return terrno;
×
69
  }
70

71
  return 0;
9,022,090✔
72
}
73

74
void vHashDestroy(SVHashTable** ht) {
9,022,090✔
75
  if (ht == NULL) {
9,022,090✔
76
    return;
×
77
  }
78

79
  if (*ht) {
9,022,090✔
80
    taosMemoryFree((*ht)->buckets);
9,022,090✔
81
    taosMemoryFree(*ht);
9,022,090✔
82
    (*ht) = NULL;
9,022,090✔
83
  }
84
}
85

86
int32_t vHashPut(SVHashTable* ht, void* obj) {
23,925,317✔
87
  if (ht == NULL || obj == NULL) {
23,925,317✔
88
    return TSDB_CODE_INVALID_PARA;
×
89
  }
90

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
23,926,451✔
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
31,150,983✔
93
    if (ht->compare(entry->obj, obj) == 0) {
7,223,108✔
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
23,925,601✔
99
    vHashRehash(ht, ht->numBuckets * 2);
7,584✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
7,584✔
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
23,925,088✔
104
  if (entry == NULL) {
23,926,982✔
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
23,926,982✔
108
  entry->next = ht->buckets[bucketIndex];
23,927,471✔
109
  ht->buckets[bucketIndex] = entry;
23,927,127✔
110
  ht->numEntries++;
23,926,298✔
111

112
  return 0;
23,927,790✔
113
}
114

115
int32_t vHashGet(SVHashTable* ht, const void* obj, void** retObj) {
104,610,219✔
116
  if (ht == NULL || obj == NULL || retObj == NULL) {
104,610,219✔
117
    return TSDB_CODE_INVALID_PARA;
163✔
118
  }
119

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
104,619,826✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
119,683,432✔
122
    if (ht->compare(entry->obj, obj) == 0) {
96,442,836✔
123
      *retObj = entry->obj;
81,363,185✔
124
      return 0;
81,350,078✔
125
    }
126
  }
127

128
  *retObj = NULL;
23,242,256✔
129
  return TSDB_CODE_NOT_FOUND;
23,242,256✔
130
}
131

132
int32_t vHashDrop(SVHashTable* ht, const void* obj) {
23,927,894✔
133
  if (ht == NULL || obj == NULL) {
23,927,894✔
134
    return TSDB_CODE_INVALID_PARA;
×
135
  }
136

137
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
23,928,757✔
138
  for (SVHashEntry** entry = &ht->buckets[bucketIndex]; *entry != NULL; entry = &(*entry)->next) {
26,985,784✔
139
    if (ht->compare((*entry)->obj, obj) == 0) {
26,985,784✔
140
      SVHashEntry* tmp = *entry;
23,928,388✔
141
      *entry = (*entry)->next;
23,928,388✔
142
      taosMemoryFree(tmp);
23,928,388✔
143
      ht->numEntries--;
23,928,194✔
144
      if (ht->numBuckets > VNODE_HASH_DEFAULT_NUM_BUCKETS && ht->numEntries < ht->numBuckets / 4) {
23,928,757✔
145
        vHashRehash(ht, ht->numBuckets / 2);
7,584✔
146
      }
147
      return 0;
23,928,757✔
148
    }
149
  }
150

151
  return TSDB_CODE_NOT_FOUND;
×
152
}
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