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

taosdata / TDengine / #3951

28 Apr 2025 05:42AM UTC coverage: 62.445% (-0.4%) from 62.853%
#3951

push

travis-ci

web-flow
fix: mnode-status-case (#30871)

155256 of 317429 branches covered (48.91%)

Branch coverage included in aggregate %.

240626 of 316539 relevant lines covered (76.02%)

6221630.89 hits per line

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

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
1,141,997✔
34
    SVHashEntry* entry = ht->buckets[i];
1,141,751✔
35
    while (entry != NULL) {
1,711,580✔
36
      SVHashEntry* next = entry->next;
569,887✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
569,887✔
38
      entry->next = newBuckets[bucketIndex];
569,829✔
39
      newBuckets[bucketIndex] = entry;
569,829✔
40
      entry = next;
569,829✔
41
    }
42
  }
43

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

48
  return;
304✔
49
}
50

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

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

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

71
  return 0;
27,316✔
72
}
73

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

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

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

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
482,341✔
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
784,629✔
93
    if (ht->compare(entry->obj, obj) == 0) {
302,493!
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

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

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

112
  return 0;
482,322✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
6,776,913✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
10,602,952✔
122
    if (ht->compare(entry->obj, obj) == 0) {
10,121,744✔
123
      *retObj = entry->obj;
6,296,098✔
124
      return 0;
6,296,098✔
125
    }
126
  }
127

128
  *retObj = NULL;
481,208✔
129
  return TSDB_CODE_NOT_FOUND;
481,208✔
130
}
131

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

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