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

taosdata / TDengine / #4830

31 Oct 2025 03:37AM UTC coverage: 58.792% (+9.1%) from 49.734%
#4830

push

travis-ci

SallyHuo-TAOS
Merge remote-tracking branch 'origin/cover/3.0' into cover/3.0

# Conflicts:
#	test/ci/run.sh

149913 of 324176 branches covered (46.24%)

Branch coverage included in aggregate %.

199119 of 269498 relevant lines covered (73.89%)

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
207,544,184✔
34
    SVHashEntry* entry = ht->buckets[i];
207,409,152✔
35
    while (entry != NULL) {
311,046,212✔
36
      SVHashEntry* next = entry->next;
103,637,060✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
103,637,060!
38
      entry->next = newBuckets[bucketIndex];
103,637,060✔
39
      newBuckets[bucketIndex] = entry;
103,637,060✔
40
      entry = next;
103,637,060✔
41
    }
42
  }
43

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

48
  return;
135,032✔
49
}
50

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

56
  (*ht) = (SVHashTable*)taosMemoryMalloc(sizeof(SVHashTable));
32,413,991!
57
  if (*ht == NULL) {
32,415,597!
58
    return terrno;
×
59
  }
60

61
  (*ht)->hash = hash;
32,413,958✔
62
  (*ht)->compare = compare;
32,412,630✔
63
  (*ht)->numEntries = 0;
32,413,958✔
64
  (*ht)->numBuckets = VNODE_HASH_DEFAULT_NUM_BUCKETS;
32,414,101✔
65
  (*ht)->buckets = (SVHashEntry**)taosMemoryCalloc((*ht)->numBuckets, sizeof(SVHashEntry*));
32,406,758!
66
  if ((*ht)->buckets == NULL) {
32,415,597!
67
    taosMemoryFree(*ht);
×
68
    return terrno;
×
69
  }
70

71
  return 0;
32,415,597✔
72
}
73

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

79
  if (*ht) {
32,415,597!
80
    taosMemoryFree((*ht)->buckets);
32,415,597!
81
    taosMemoryFree(*ht);
32,414,125!
82
    (*ht) = NULL;
32,415,597✔
83
  }
84
}
85

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

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
155,535,060!
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
219,292,228✔
93
    if (ht->compare(entry->obj, obj) == 0) {
63,753,857!
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
155,517,022✔
99
    vHashRehash(ht, ht->numBuckets * 2);
67,516✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
67,516!
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
155,526,448!
104
  if (entry == NULL) {
155,536,026!
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
155,536,026✔
108
  entry->next = ht->buckets[bucketIndex];
155,531,149✔
109
  ht->buckets[bucketIndex] = entry;
155,537,517✔
110
  ht->numEntries++;
155,527,985✔
111

112
  return 0;
155,523,948✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
466,857,550!
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
600,551,517✔
122
    if (ht->compare(entry->obj, obj) == 0) {
449,838,350✔
123
      *retObj = entry->obj;
316,119,080✔
124
      return 0;
316,143,593✔
125
    }
126
  }
127

128
  *retObj = NULL;
150,671,699✔
129
  return TSDB_CODE_NOT_FOUND;
150,674,546✔
130
}
131

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

137
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
155,549,609!
138
  for (SVHashEntry** entry = &ht->buckets[bucketIndex]; *entry != NULL; entry = &(*entry)->next) {
182,232,957!
139
    if (ht->compare((*entry)->obj, obj) == 0) {
182,232,243✔
140
      SVHashEntry* tmp = *entry;
155,545,563✔
141
      *entry = (*entry)->next;
155,545,682✔
142
      taosMemoryFree(tmp);
155,545,682!
143
      ht->numEntries--;
155,553,060✔
144
      if (ht->numBuckets > VNODE_HASH_DEFAULT_NUM_BUCKETS && ht->numEntries < ht->numBuckets / 4) {
155,553,179✔
145
        vHashRehash(ht, ht->numBuckets / 2);
67,516✔
146
      }
147
      return 0;
155,552,703✔
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