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

taosdata / TDengine / #4871

04 Dec 2025 01:55AM UTC coverage: 64.654% (+0.1%) from 64.545%
#4871

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

869 of 2219 new or added lines in 36 files covered. (39.16%)

441 existing lines in 120 files now uncovered.

159620 of 246882 relevant lines covered (64.65%)

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
19,759,672✔
34
    SVHashEntry* entry = ht->buckets[i];
19,746,816✔
35
    while (entry != NULL) {
29,613,796✔
36
      SVHashEntry* next = entry->next;
9,866,980✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
9,866,980✔
38
      entry->next = newBuckets[bucketIndex];
9,866,980✔
39
      newBuckets[bucketIndex] = entry;
9,866,980✔
40
      entry = next;
9,866,980✔
41
    }
42
  }
43

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

48
  return;
12,856✔
49
}
50

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

56
  (*ht) = (SVHashTable*)taosMemoryMalloc(sizeof(SVHashTable));
9,456,869✔
57
  if (*ht == NULL) {
9,456,869✔
58
    return terrno;
×
59
  }
60

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

71
  return 0;
9,456,869✔
72
}
73

74
void vHashDestroy(SVHashTable** ht) {
9,456,869✔
75
  if (ht == NULL) {
9,456,869✔
76
    return;
×
77
  }
78

79
  if (*ht) {
9,456,869✔
80
    taosMemoryFree((*ht)->buckets);
9,456,869✔
81
    taosMemoryFree(*ht);
9,456,869✔
82
    (*ht) = NULL;
9,456,869✔
83
  }
84
}
85

86
int32_t vHashPut(SVHashTable* ht, void* obj) {
21,819,635✔
87
  if (ht == NULL || obj == NULL) {
21,819,635✔
88
    return TSDB_CODE_INVALID_PARA;
263✔
89
  }
90

91
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
21,819,372✔
92
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
27,958,966✔
93
    if (ht->compare(entry->obj, obj) == 0) {
6,140,265✔
94
      return TSDB_CODE_DUP_KEY;
×
95
    }
96
  }
97

98
  if (ht->numEntries >= ht->numBuckets) {
21,820,768✔
99
    vHashRehash(ht, ht->numBuckets * 2);
6,428✔
100
    bucketIndex = ht->hash(obj) % ht->numBuckets;
6,428✔
101
  }
102

103
  SVHashEntry* entry = (SVHashEntry*)taosMemoryMalloc(sizeof(SVHashEntry));
21,820,640✔
104
  if (entry == NULL) {
21,820,631✔
105
    return terrno;
×
106
  }
107
  entry->obj = obj;
21,820,631✔
108
  entry->next = ht->buckets[bucketIndex];
21,820,631✔
109
  ht->buckets[bucketIndex] = entry;
21,819,250✔
110
  ht->numEntries++;
21,821,167✔
111

112
  return 0;
21,820,892✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
88,672,901✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
101,463,800✔
122
    if (ht->compare(entry->obj, obj) == 0) {
80,075,022✔
123
      *retObj = entry->obj;
67,279,025✔
124
      return 0;
67,272,200✔
125
    }
126
  }
127

128
  *retObj = NULL;
21,391,421✔
129
  return TSDB_CODE_NOT_FOUND;
21,391,421✔
130
}
131

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

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