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

taosdata / TDengine / #4824

27 Oct 2025 05:39AM UTC coverage: 61.409% (+0.2%) from 61.242%
#4824

push

travis-ci

web-flow
Merge pull request #33376 from taosdata/3.0

merge 3.0

156919 of 324854 branches covered (48.3%)

Branch coverage included in aggregate %.

371 of 493 new or added lines in 25 files covered. (75.25%)

2822 existing lines in 108 files now uncovered.

208404 of 270043 relevant lines covered (77.17%)

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
167,729,736✔
34
    SVHashEntry* entry = ht->buckets[i];
167,620,608✔
35
    while (entry != NULL) {
251,376,348✔
36
      SVHashEntry* next = entry->next;
83,755,740✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
83,755,740!
38
      entry->next = newBuckets[bucketIndex];
83,755,740✔
39
      newBuckets[bucketIndex] = entry;
83,755,740✔
40
      entry = next;
83,755,740✔
41
    }
42
  }
43

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

48
  return;
109,128✔
49
}
50

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

56
  (*ht) = (SVHashTable*)taosMemoryMalloc(sizeof(SVHashTable));
28,197,841!
57
  if (*ht == NULL) {
28,198,801!
58
    return terrno;
×
59
  }
60

61
  (*ht)->hash = hash;
28,196,997✔
62
  (*ht)->compare = compare;
28,198,090✔
63
  (*ht)->numEntries = 0;
28,198,320✔
64
  (*ht)->numBuckets = VNODE_HASH_DEFAULT_NUM_BUCKETS;
28,198,347✔
65
  (*ht)->buckets = (SVHashEntry**)taosMemoryCalloc((*ht)->numBuckets, sizeof(SVHashEntry*));
28,197,802!
66
  if ((*ht)->buckets == NULL) {
28,198,801!
67
    taosMemoryFree(*ht);
×
68
    return terrno;
×
69
  }
70

71
  return 0;
28,198,801✔
72
}
73

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

79
  if (*ht) {
28,198,801!
80
    taosMemoryFree((*ht)->buckets);
28,198,801!
81
    taosMemoryFree(*ht);
28,196,197!
82
    (*ht) = NULL;
28,197,703✔
83
  }
84
}
85

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

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
125,940,894!
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
177,492,425✔
93
    if (ht->compare(entry->obj, obj) == 0) {
51,543,062!
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
125,937,272✔
99
    vHashRehash(ht, ht->numBuckets * 2);
54,564✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
54,564!
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
125,945,480!
104
  if (entry == NULL) {
125,954,128!
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
125,954,128✔
108
  entry->next = ht->buckets[bucketIndex];
125,946,896✔
109
  ht->buckets[bucketIndex] = entry;
125,948,544✔
110
  ht->numEntries++;
125,951,398✔
111

112
  return 0;
125,946,879✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
405,291,147!
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
513,357,491✔
122
    if (ht->compare(entry->obj, obj) == 0) {
390,628,737✔
123
      *retObj = entry->obj;
282,511,177✔
124
      return 0;
282,526,799✔
125
    }
126
  }
127

128
  *retObj = NULL;
122,684,528✔
129
  return TSDB_CODE_NOT_FOUND;
122,681,168✔
130
}
131

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

137
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
125,961,631!
138
  for (SVHashEntry** entry = &ht->buckets[bucketIndex]; *entry != NULL; entry = &(*entry)->next) {
147,547,138!
139
    if (ht->compare((*entry)->obj, obj) == 0) {
147,547,138✔
140
      SVHashEntry* tmp = *entry;
125,961,336✔
141
      *entry = (*entry)->next;
125,961,879✔
142
      taosMemoryFree(tmp);
125,961,879!
143
      ht->numEntries--;
125,962,427✔
144
      if (ht->numBuckets > VNODE_HASH_DEFAULT_NUM_BUCKETS && ht->numEntries < ht->numBuckets / 4) {
125,962,427✔
145
        vHashRehash(ht, ht->numBuckets / 2);
54,564✔
146
      }
147
      return 0;
125,961,879✔
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