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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

web-flow
Merge pull request #29874 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 hits per line

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

0.0
/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

UNCOV
27
static void vHashRehash(SVHashTable* ht, uint32_t newNumBuckets) {
×
UNCOV
28
  SVHashEntry** newBuckets = (SVHashEntry**)taosMemoryCalloc(newNumBuckets, sizeof(SVHashEntry*));
×
UNCOV
29
  if (newBuckets == NULL) {
×
30
    return;
×
31
  }
32

UNCOV
33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
×
UNCOV
34
    SVHashEntry* entry = ht->buckets[i];
×
UNCOV
35
    while (entry != NULL) {
×
UNCOV
36
      SVHashEntry* next = entry->next;
×
UNCOV
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
×
UNCOV
38
      entry->next = newBuckets[bucketIndex];
×
UNCOV
39
      newBuckets[bucketIndex] = entry;
×
UNCOV
40
      entry = next;
×
41
    }
42
  }
43

UNCOV
44
  taosMemoryFree(ht->buckets);
×
UNCOV
45
  ht->buckets = newBuckets;
×
UNCOV
46
  ht->numBuckets = newNumBuckets;
×
47

UNCOV
48
  return;
×
49
}
50

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

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

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

UNCOV
71
  return 0;
×
72
}
73

UNCOV
74
void vHashDestroy(SVHashTable** ht) {
×
UNCOV
75
  if (ht == NULL) {
×
76
    return;
×
77
  }
78

UNCOV
79
  if (*ht) {
×
UNCOV
80
    taosMemoryFree((*ht)->buckets);
×
UNCOV
81
    taosMemoryFree(*ht);
×
UNCOV
82
    (*ht) = NULL;
×
83
  }
84
}
85

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

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

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

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

UNCOV
112
  return 0;
×
113
}
114

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

UNCOV
120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
×
UNCOV
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
×
UNCOV
122
    if (ht->compare(entry->obj, obj) == 0) {
×
UNCOV
123
      *retObj = entry->obj;
×
UNCOV
124
      return 0;
×
125
    }
126
  }
127

UNCOV
128
  *retObj = NULL;
×
UNCOV
129
  return TSDB_CODE_NOT_FOUND;
×
130
}
131

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

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