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

taosdata / TDengine / #3558

17 Dec 2024 06:05AM UTC coverage: 59.778% (+1.6%) from 58.204%
#3558

push

travis-ci

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

merge: form main to 3.0 branch

132787 of 287595 branches covered (46.17%)

Branch coverage included in aggregate %.

104 of 191 new or added lines in 5 files covered. (54.45%)

6085 existing lines in 168 files now uncovered.

209348 of 284746 relevant lines covered (73.52%)

8164844.48 hits per line

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

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
220,737✔
34
    SVHashEntry* entry = ht->buckets[i];
220,643✔
35
    while (entry != NULL) {
330,485✔
36
      SVHashEntry* next = entry->next;
109,892✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
109,892✔
38
      entry->next = newBuckets[bucketIndex];
109,842✔
39
      newBuckets[bucketIndex] = entry;
109,842✔
40
      entry = next;
109,842✔
41
    }
42
  }
43

44
  taosMemoryFree(ht->buckets);
94!
45
  ht->buckets = newBuckets;
144✔
46
  ht->numBuckets = newNumBuckets;
144✔
47

48
  return;
144✔
49
}
50

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

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

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

71
  return 0;
11,720✔
72
}
73

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

79
  if (*ht) {
11,721!
80
    taosMemoryFree((*ht)->buckets);
11,721!
81
    taosMemoryFree(*ht);
11,721!
82
    (*ht) = NULL;
11,721✔
83
  }
84
}
85

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

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

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

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

112
  return 0;
153,146✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
487,823✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
630,901✔
122
    if (ht->compare(entry->obj, obj) == 0) {
491,007✔
123
      *retObj = entry->obj;
348,977✔
124
      return 0;
348,977✔
125
    }
126
  }
127

128
  *retObj = NULL;
139,894✔
129
  return TSDB_CODE_NOT_FOUND;
139,894✔
130
}
131

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

137
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
153,190✔
138
  for (SVHashEntry** entry = &ht->buckets[bucketIndex]; *entry != NULL; entry = &(*entry)->next) {
181,338!
139
    if (ht->compare((*entry)->obj, obj) == 0) {
181,338✔
140
      SVHashEntry* tmp = *entry;
153,190✔
141
      *entry = (*entry)->next;
153,190✔
142
      taosMemoryFree(tmp);
153,190!
143
      ht->numEntries--;
153,189✔
144
      if (ht->numBuckets > VNODE_HASH_DEFAULT_NUM_BUCKETS && ht->numEntries < ht->numBuckets / 4) {
153,189✔
145
        vHashRehash(ht, ht->numBuckets / 2);
72✔
146
      }
147
      return 0;
153,189✔
148
    }
149
  }
150

UNCOV
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