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

taosdata / TDengine / #4979

09 Mar 2026 09:42AM UTC coverage: 68.512% (+0.07%) from 68.439%
#4979

push

travis-ci

web-flow
doc: update user manual. (#34693)

211961 of 309380 relevant lines covered (68.51%)

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
23,079,592✔
34
    SVHashEntry* entry = ht->buckets[i];
23,064,576✔
35
    while (entry != NULL) {
34,589,356✔
36
      SVHashEntry* next = entry->next;
11,524,780✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
11,524,780✔
38
      entry->next = newBuckets[bucketIndex];
11,524,780✔
39
      newBuckets[bucketIndex] = entry;
11,524,780✔
40
      entry = next;
11,524,780✔
41
    }
42
  }
43

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

48
  return;
15,016✔
49
}
50

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

56
  (*ht) = (SVHashTable*)taosMemoryMalloc(sizeof(SVHashTable));
8,957,084✔
57
  if (*ht == NULL) {
8,957,084✔
58
    return terrno;
×
59
  }
60

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

71
  return 0;
8,957,084✔
72
}
73

74
void vHashDestroy(SVHashTable** ht) {
8,957,084✔
75
  if (ht == NULL) {
8,957,084✔
76
    return;
×
77
  }
78

79
  if (*ht) {
8,957,084✔
80
    taosMemoryFree((*ht)->buckets);
8,957,084✔
81
    taosMemoryFree(*ht);
8,956,372✔
82
    (*ht) = NULL;
8,957,084✔
83
  }
84
}
85

86
int32_t vHashPut(SVHashTable* ht, void* obj) {
24,005,696✔
87
  if (ht == NULL || obj == NULL) {
24,005,696✔
88
    return TSDB_CODE_INVALID_PARA;
167✔
89
  }
90

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
24,005,722✔
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
31,157,242✔
93
    if (ht->compare(entry->obj, obj) == 0) {
7,150,505✔
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
24,005,835✔
99
    vHashRehash(ht, ht->numBuckets * 2);
7,508✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
7,508✔
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
24,005,258✔
104
  if (entry == NULL) {
24,004,786✔
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
24,004,786✔
108
  entry->next = ht->buckets[bucketIndex];
24,005,608✔
109
  ht->buckets[bucketIndex] = entry;
24,006,269✔
110
  ht->numEntries++;
24,005,755✔
111

112
  return 0;
24,003,998✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
104,100,543✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
119,030,307✔
122
    if (ht->compare(entry->obj, obj) == 0) {
95,687,023✔
123
      *retObj = entry->obj;
80,756,449✔
124
      return 0;
80,751,912✔
125
    }
126
  }
127

128
  *retObj = NULL;
23,347,321✔
129
  return TSDB_CODE_NOT_FOUND;
23,347,321✔
130
}
131

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

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