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

taosdata / TDengine / #3541

26 Nov 2024 03:56AM UTC coverage: 60.776% (-0.07%) from 60.846%
#3541

push

travis-ci

web-flow
Merge pull request #28920 from taosdata/fix/TD-33008-3.0

fix(query)[TD-33008]. fix error handling in tsdbCacheRead

120076 of 252763 branches covered (47.51%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

1395 existing lines in 154 files now uncovered.

200995 of 275526 relevant lines covered (72.95%)

19612328.37 hits per line

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

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

33
  for (int32_t i = 0; i < ht->numBuckets; i++) {
232,571✔
34
    SVHashEntry* entry = ht->buckets[i];
232,472✔
35
    while (entry != NULL) {
347,808✔
36
      SVHashEntry* next = entry->next;
115,389✔
37
      uint32_t     bucketIndex = ht->hash(entry->obj) % newNumBuckets;
115,389✔
38
      entry->next = newBuckets[bucketIndex];
115,336✔
39
      newBuckets[bucketIndex] = entry;
115,336✔
40
      entry = next;
115,336✔
41
    }
42
  }
43

44
  taosMemoryFree(ht->buckets);
99✔
45
  ht->buckets = newBuckets;
152✔
46
  ht->numBuckets = newNumBuckets;
152✔
47

48
  return;
152✔
49
}
50

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

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

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

71
  return 0;
21,257✔
72
}
73

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

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

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

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

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

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

112
  return 0;
207,162✔
113
}
114

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

120
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
626,896✔
121
  for (SVHashEntry* entry = ht->buckets[bucketIndex]; entry != NULL; entry = entry->next) {
777,779✔
122
    if (ht->compare(entry->obj, obj) == 0) {
597,879✔
123
      *retObj = entry->obj;
447,732✔
124
      return 0;
447,732✔
125
    }
126
  }
127

128
  *retObj = NULL;
179,900✔
129
  return TSDB_CODE_NOT_FOUND;
179,900✔
130
}
131

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

137
  uint32_t bucketIndex = ht->hash(obj) % ht->numBuckets;
207,100✔
138
  for (SVHashEntry** entry = &ht->buckets[bucketIndex]; *entry != NULL; entry = &(*entry)->next) {
236,834✔
139
    if (ht->compare((*entry)->obj, obj) == 0) {
236,717✔
140
      SVHashEntry* tmp = *entry;
206,885✔
141
      *entry = (*entry)->next;
206,885✔
142
      taosMemoryFree(tmp);
206,885✔
143
      ht->numEntries--;
207,194✔
144
      if (ht->numBuckets > VNODE_HASH_DEFAULT_NUM_BUCKETS && ht->numEntries < ht->numBuckets / 4) {
207,194✔
145
        vHashRehash(ht, ht->numBuckets / 2);
76✔
146
      }
147
      return 0;
207,200✔
148
    }
149
  }
150

151
  return TSDB_CODE_NOT_FOUND;
117✔
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

© 2025 Coveralls, Inc