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

taosdata / TDengine / #5021

10 Apr 2026 06:58AM UTC coverage: 72.291% (+0.01%) from 72.28%
#5021

push

travis-ci

web-flow
merge: from main to 3.0 #35103

77 of 116 new or added lines in 3 files covered. (66.38%)

2783 existing lines in 135 files now uncovered.

257546 of 356263 relevant lines covered (72.29%)

131342782.21 hits per line

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

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
24,505,928✔
34
    SVHashEntry* entry = ht->buckets[i];
24,489,984✔
35
    while (entry != NULL) {
36,727,004✔
36
      SVHashEntry* next = entry->next;
12,237,020✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
12,237,020✔
38
      entry->next = newBuckets[bucketIndex];
12,237,020✔
39
      newBuckets[bucketIndex] = entry;
12,237,020✔
40
      entry = next;
12,237,020✔
41
    }
42
  }
43

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

48
  return;
15,944✔
49
}
50

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

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

61
  (*ht)->hash = hash;
9,617,438✔
62
  (*ht)->compare = compare;
9,615,963✔
63
  (*ht)->numEntries = 0;
9,616,856✔
64
  (*ht)->numBuckets = VNODE_HASH_DEFAULT_NUM_BUCKETS;
9,615,164✔
65
  (*ht)->buckets = (SVHashEntry**)taosMemoryCalloc((*ht)->numBuckets, sizeof(SVHashEntry*));
9,616,424✔
66
  if ((*ht)->buckets == NULL) {
9,616,873✔
67
    taosMemoryFree(*ht);
×
68
    return terrno;
×
69
  }
70

71
  return 0;
9,616,873✔
72
}
73

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

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

86
int32_t vHashPut(SVHashTable* ht, void* obj) {
25,296,073✔
87
  if (ht == NULL || obj == NULL) {
25,296,073✔
UNCOV
88
    return TSDB_CODE_INVALID_PARA;
×
89
  }
90

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
25,296,103✔
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
32,885,025✔
93
    if (ht->compare(entry->obj, obj) == 0) {
7,589,319✔
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
25,294,107✔
99
    vHashRehash(ht, ht->numBuckets * 2);
7,972✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
7,972✔
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
25,294,474✔
104
  if (entry == NULL) {
25,294,330✔
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
25,294,330✔
108
  entry->next = ht->buckets[bucketIndex];
25,294,749✔
109
  ht->buckets[bucketIndex] = entry;
25,294,836✔
110
  ht->numEntries++;
25,295,581✔
111

112
  return 0;
25,293,992✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
110,466,138✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
126,323,468✔
122
    if (ht->compare(entry->obj, obj) == 0) {
101,751,626✔
123
      *retObj = entry->obj;
85,878,570✔
124
      return 0;
85,881,045✔
125
    }
126
  }
127

128
  *retObj = NULL;
24,563,271✔
129
  return TSDB_CODE_NOT_FOUND;
24,562,094✔
130
}
131

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

137
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
25,296,920✔
138
  for (SVHashEntry** entry = &ht->buckets[bucketIndex]; *entry != NULL; entry = &(*entry)->next) {
28,505,433✔
139
    if (ht->compare((*entry)->obj, obj) == 0) {
28,505,433✔
140
      SVHashEntry* tmp = *entry;
25,296,920✔
141
      *entry = (*entry)->next;
25,296,920✔
142
      taosMemoryFree(tmp);
25,296,920✔
143
      ht->numEntries--;
25,296,920✔
144
      if (ht->numBuckets > VNODE_HASH_DEFAULT_NUM_BUCKETS && ht->numEntries < ht->numBuckets / 4) {
25,296,920✔
145
        vHashRehash(ht, ht->numBuckets / 2);
7,972✔
146
      }
147
      return 0;
25,296,920✔
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