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

taosdata / TDengine / #4818

20 Oct 2025 02:09AM UTC coverage: 61.04% (-0.1%) from 61.141%
#4818

push

travis-ci

web-flow
Merge e6f7b1ad7 into 7e74ade39

155148 of 324487 branches covered (47.81%)

Branch coverage included in aggregate %.

152 of 185 new or added lines in 22 files covered. (82.16%)

4700 existing lines in 117 files now uncovered.

207487 of 269610 relevant lines covered (76.96%)

127870039.14 hits per line

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

73.23
/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) {
18,016✔
28
  SVHashEntry** newBuckets = (SVHashEntry**)taosMemoryCalloc(newNumBuckets, sizeof(SVHashEntry*));
18,016!
29
  if (newBuckets == NULL) {
18,016!
30
    return;
×
31
  }
32

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
27,690,592✔
34
    SVHashEntry* entry = ht->buckets[i];
27,672,576✔
35
    while (entry != NULL) {
41,499,856✔
36
      SVHashEntry* next = entry->next;
13,827,280✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
13,827,280!
38
      entry->next = newBuckets[bucketIndex];
13,827,280✔
39
      newBuckets[bucketIndex] = entry;
13,827,280✔
40
      entry = next;
13,827,280✔
41
    }
42
  }
43

44
  taosMemoryFree(ht->buckets);
18,016!
45
  ht->buckets = newBuckets;
18,016✔
46
  ht->numBuckets = newNumBuckets;
18,016✔
47

48
  return;
18,016✔
49
}
50

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

56
  (*ht) = (SVHashTable*)taosMemoryMalloc(sizeof(SVHashTable));
10,623,342!
57
  if (*ht == NULL) {
10,623,342!
58
    return terrno;
×
59
  }
60

61
  (*ht)->hash = hash;
10,623,342✔
62
  (*ht)->compare = compare;
10,623,342✔
63
  (*ht)->numEntries = 0;
10,623,342✔
64
  (*ht)->numBuckets = VNODE_HASH_DEFAULT_NUM_BUCKETS;
10,623,342✔
65
  (*ht)->buckets = (SVHashEntry**)taosMemoryCalloc((*ht)->numBuckets, sizeof(SVHashEntry*));
10,623,342!
66
  if ((*ht)->buckets == NULL) {
10,623,342!
67
    taosMemoryFree(*ht);
×
68
    return terrno;
×
69
  }
70

71
  return 0;
10,623,342✔
72
}
73

74
void vHashDestroy(SVHashTable** ht) {
10,623,342✔
75
  if (ht == NULL) {
10,623,342!
76
    return;
×
77
  }
78

79
  if (*ht) {
10,623,342!
80
    taosMemoryFree((*ht)->buckets);
10,623,342!
81
    taosMemoryFree(*ht);
10,623,076!
82
    (*ht) = NULL;
10,623,076✔
83
  }
84
}
85

86
int32_t vHashPut(SVHashTable* ht, void* obj) {
28,118,125✔
87
  if (ht == NULL || obj == NULL) {
28,118,125!
88
    return TSDB_CODE_INVALID_PARA;
×
89
  }
90

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
28,119,443!
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
36,743,429✔
93
    if (ht->compare(entry->obj, obj) == 0) {
8,622,393!
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
28,117,689✔
99
    vHashRehash(ht, ht->numBuckets * 2);
9,008✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
9,008!
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
28,119,896!
104
  if (entry == NULL) {
28,121,515!
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
28,121,515✔
108
  entry->next = ht->buckets[bucketIndex];
28,119,774✔
109
  ht->buckets[bucketIndex] = entry;
28,121,014✔
110
  ht->numEntries++;
28,120,247✔
111

112
  return 0;
28,120,895✔
113
}
114

115
int32_t vHashGet(SVHashTable* ht, const void* obj, void** retObj) {
93,533,905✔
116
  if (ht == NULL || obj == NULL || retObj == NULL) {
93,533,905!
117
    return TSDB_CODE_INVALID_PARA;
×
118
  }
119

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
93,565,423!
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
111,507,907✔
122
    if (ht->compare(entry->obj, obj) == 0) {
84,766,527✔
123
      *retObj = entry->obj;
66,819,334✔
124
      return 0;
66,819,849✔
125
    }
126
  }
127

128
  *retObj = NULL;
26,739,262✔
129
  return TSDB_CODE_NOT_FOUND;
26,738,973✔
130
}
131

132
int32_t vHashDrop(SVHashTable* ht, const void* obj) {
28,122,003✔
133
  if (ht == NULL || obj == NULL) {
28,122,003!
134
    return TSDB_CODE_INVALID_PARA;
×
135
  }
136

137
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
28,122,003!
138
  for (SVHashEntry** entry = &ht->buckets[bucketIndex]; *entry != NULL; entry = &(*entry)->next) {
31,793,825!
139
    if (ht->compare((*entry)->obj, obj) == 0) {
31,793,825✔
140
      SVHashEntry* tmp = *entry;
28,122,003✔
141
      *entry = (*entry)->next;
28,122,003✔
142
      taosMemoryFree(tmp);
28,122,003!
143
      ht->numEntries--;
28,121,737✔
144
      if (ht->numBuckets > VNODE_HASH_DEFAULT_NUM_BUCKETS && ht->numEntries < ht->numBuckets / 4) {
28,121,737✔
145
        vHashRehash(ht, ht->numBuckets / 2);
9,008✔
146
      }
147
      return 0;
28,122,003✔
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