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

taosdata / TDengine / #4452

05 Jul 2025 10:40AM UTC coverage: 60.734% (-2.8%) from 63.58%
#4452

push

travis-ci

web-flow
Merge pull request #31673 from taosdata/fix/huoh/taos_log

test: fix mnode create failure

149869 of 316704 branches covered (47.32%)

Branch coverage included in aggregate %.

233489 of 314508 relevant lines covered (74.24%)

6990477.86 hits per line

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

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
1,154,259✔
34
    SVHashEntry* entry = ht->buckets[i];
1,153,978✔
35
    while (entry != NULL) {
1,729,958✔
36
      SVHashEntry* next = entry->next;
576,011✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
576,011✔
38
      entry->next = newBuckets[bucketIndex];
575,980✔
39
      newBuckets[bucketIndex] = entry;
575,980✔
40
      entry = next;
575,980✔
41
    }
42
  }
43

44
  taosMemoryFree(ht->buckets);
281!
45
  ht->buckets = newBuckets;
312✔
46
  ht->numBuckets = newNumBuckets;
312✔
47

48
  return;
312✔
49
}
50

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

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

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

71
  return 0;
35,537✔
72
}
73

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

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

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

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
498,579✔
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
804,194✔
93
    if (ht->compare(entry->obj, obj) == 0) {
306,181!
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
498,013✔
99
    vHashRehash(ht, ht->numBuckets * 2);
156✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
156✔
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
498,013!
104
  if (entry == NULL) {
498,576!
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
498,576✔
108
  entry->next = ht->buckets[bucketIndex];
498,576✔
109
  ht->buckets[bucketIndex] = entry;
498,576✔
110
  ht->numEntries++;
498,576✔
111

112
  return 0;
498,576✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
6,829,892✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
9,679,275✔
122
    if (ht->compare(entry->obj, obj) == 0) {
9,180,678✔
123
      *retObj = entry->obj;
6,323,379✔
124
      return 0;
6,323,379✔
125
    }
126
  }
127

128
  *retObj = NULL;
498,597✔
129
  return TSDB_CODE_NOT_FOUND;
498,597✔
130
}
131

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

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