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

taosdata / TDengine / #3549

06 Dec 2024 09:44AM UTC coverage: 59.948% (+0.1%) from 59.846%
#3549

push

travis-ci

web-flow
Merge pull request #29057 from taosdata/docs/TD-33031-3.0

docs: description of user privileges

118833 of 254191 branches covered (46.75%)

Branch coverage included in aggregate %.

199893 of 277480 relevant lines covered (72.04%)

19006119.35 hits per line

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

93.78
/source/util/src/thashutil.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
#define _DEFAULT_SOURCE
17
#include "tcompare.h"
18
#include "thash.h"
19
#include "types.h"
20
#include "xxhash.h"
21

22
#define ROTL32(x, r) ((x) << (r) | (x) >> (32u - (r)))
23

24
#define DLT  (FLT_COMPAR_TOL_FACTOR * FLT_EPSILON)
25
#define BASE 1000
26

27
#define FMIX32(h)      \
28
  do {                 \
29
    (h) ^= (h) >> 16;  \
30
    (h) *= 0x85ebca6b; \
31
    (h) ^= (h) >> 13;  \
32
    (h) *= 0xc2b2ae35; \
33
    (h) ^= (h) >> 16;  \
34
  } while (0)
35

36
uint32_t taosFastHash(const char *key, uint32_t len) {
598,335,219✔
37
  uint32_t result = 0x55555555;
598,335,219✔
38
  for (uint32_t i = 0; i < len; i++) {
2,147,483,647✔
39
    result ^= (uint8_t)key[i];
2,147,483,647✔
40
    result = ROTL32(result, 5);
2,147,483,647✔
41
  }
42
  return result;
598,335,219✔
43
}
44

45
uint32_t taosDJB2Hash(const char *key, uint32_t len) {
13,453,034✔
46
  uint32_t hash = 5381;
13,453,034✔
47
  for (uint32_t i = 0; i < len; i++) {
274,533,090✔
48
    hash = ((hash << 5) + hash) + (uint8_t)key[i]; /* hash * 33 + c */
261,080,056✔
49
  }
50
  return hash;
13,453,034✔
51
}
52

53
uint32_t MurmurHash3_32(const char *key, uint32_t len) {
972,631,708✔
54
  const uint8_t *data = (const uint8_t *)key;
972,631,708✔
55
  const int32_t  nblocks = len >> 2u;
972,631,708✔
56

57
  uint32_t h1 = 0x12345678;
972,631,708✔
58

59
  const uint32_t c1 = 0xcc9e2d51;
972,631,708✔
60
  const uint32_t c2 = 0x1b873593;
972,631,708✔
61

62
  const uint32_t *blocks = (const uint32_t *)(data + nblocks * 4);
972,631,708✔
63

64
  for (int32_t i = -nblocks; i; i++) {
2,147,483,647✔
65
    uint32_t k1 = blocks[i];
2,147,483,647✔
66

67
    k1 *= c1;
2,147,483,647✔
68
    k1 = ROTL32(k1, 15u);
2,147,483,647✔
69
    k1 *= c2;
2,147,483,647✔
70

71
    h1 ^= k1;
2,147,483,647✔
72
    h1 = ROTL32(h1, 13u);
2,147,483,647✔
73
    h1 = h1 * 5 + 0xe6546b64;
2,147,483,647✔
74
  }
75

76
  const uint8_t *tail = (data + nblocks * 4);
972,631,708✔
77

78
  uint32_t k1 = 0;
972,631,708✔
79

80
  switch (len & 3u) {
972,631,708✔
81
    case 3:
169,211,963✔
82
      k1 ^= tail[2] << 16;
169,211,963✔
83
    case 2:
370,627,183✔
84
      k1 ^= tail[1] << 8;
370,627,183✔
85
    case 1:
554,448,990✔
86
      k1 ^= tail[0];
554,448,990✔
87
      k1 *= c1;
554,448,990✔
88
      k1 = ROTL32(k1, 15u);
554,448,990✔
89
      k1 *= c2;
554,448,990✔
90
      h1 ^= k1;
554,448,990✔
91
  };
92

93
  h1 ^= len;
972,631,708✔
94

95
  FMIX32(h1);
972,631,708✔
96

97
  return h1;
972,631,708✔
98
}
99

100
uint64_t MurmurHash3_64(const char *key, uint32_t len) {
41,193,147✔
101
  const uint64_t m = 0x87c37b91114253d5;
41,193,147✔
102
  const int      r = 47;
41,193,147✔
103
  uint32_t       seed = 0x12345678;
41,193,147✔
104
  uint64_t       h = seed ^ (len * m);
41,193,147✔
105
  const uint8_t *data = (const uint8_t *)key;
41,193,147✔
106
  const uint8_t *end = data + (len - (len & 7));
41,193,147✔
107

108
  while (data != end) {
120,001,597✔
109
    uint64_t k = *((uint64_t *)data);
78,808,450✔
110

111
    k *= m;
78,808,450✔
112
    k ^= k >> r;
78,808,450✔
113
    k *= m;
78,808,450✔
114
    h ^= k;
78,808,450✔
115
    h *= m;
78,808,450✔
116
    data += 8;
78,808,450✔
117
  }
118

119
  switch (len & 7) {
41,193,147✔
120
    case 7:
3,887,200✔
121
      h ^= (uint64_t)data[6] << 48; /* fall-thru */
3,887,200✔
122
    case 6:
11,325,072✔
123
      h ^= (uint64_t)data[5] << 40; /* fall-thru */
11,325,072✔
124
    case 5:
16,415,973✔
125
      h ^= (uint64_t)data[4] << 32; /* fall-thru */
16,415,973✔
126
    case 4:
19,883,109✔
127
      h ^= (uint64_t)data[3] << 24; /* fall-thru */
19,883,109✔
128
    case 3:
25,871,765✔
129
      h ^= (uint64_t)data[2] << 16; /* fall-thru */
25,871,765✔
130
    case 2:
30,604,336✔
131
      h ^= (uint64_t)data[1] << 8; /* fall-thru */
30,604,336✔
132
    case 1:
35,737,710✔
133
      h ^= (uint64_t)data[0];
35,737,710✔
134
      h *= m; /* fall-thru */
35,737,710✔
135
  };
136

137
  h ^= h >> r;
41,193,147✔
138
  h *= m;
41,193,147✔
139
  h ^= h >> r;
41,193,147✔
140
  return h;
41,193,147✔
141
}
142

143
uint32_t taosIntHash_32(const char *key, uint32_t UNUSED_PARAM(len)) { return *(uint32_t *)key; }
1,547,870,899✔
144
uint32_t taosIntHash_16(const char *key, uint32_t UNUSED_PARAM(len)) { return *(uint16_t *)key; }
13,711,037✔
145
uint32_t taosIntHash_8(const char *key, uint32_t UNUSED_PARAM(len)) { return *(uint8_t *)key; }
370,019,724✔
146
uint32_t taosFloatHash(const char *key, uint32_t UNUSED_PARAM(len)) {
3,521✔
147
  float f = GET_FLOAT_VAL(key);
3,521✔
148
  if (isnan(f)) {
3,521!
149
    return 0x7fc00000;
×
150
  }
151

152
  if (FLT_EQUAL(f, 0.0)) {
3,521✔
153
    return 0;
12✔
154
  }
155
  if (fabs(f) < FLT_MAX / BASE - DLT) {
3,509!
156
    int32_t t = (int32_t)(round(BASE * (f + DLT)));
3,509✔
157
    return (uint32_t)t;
3,509✔
158
  } else {
159
    return 0x7fc00000;
×
160
  }
161
}
162
uint32_t taosDoubleHash(const char *key, uint32_t UNUSED_PARAM(len)) {
3,129✔
163
  double f = GET_DOUBLE_VAL(key);
3,129✔
164
  if (isnan(f)) {
3,129!
165
    return 0x7fc00000;
×
166
  }
167

168
  if (FLT_EQUAL(f, 0.0)) {
3,129✔
169
    return 0;
12✔
170
  }
171
  if (fabs(f) < DBL_MAX / BASE - DLT) {
3,117!
172
    int32_t t = (int32_t)(round(BASE * (f + DLT)));
3,117✔
173
    return (uint32_t)t;
3,117✔
174
  } else {
175
    return 0x7fc00000;
×
176
  }
177
}
178
uint32_t taosIntHash_64(const char *key, uint32_t UNUSED_PARAM(len)) {
706,022,236✔
179
  uint64_t val = *(uint64_t *)key;
706,022,236✔
180

181
  uint64_t hash = val >> 16U;
706,022,236✔
182
  hash += (val & 0xFFFFU);
706,022,236✔
183

184
  return (uint32_t)hash;
706,022,236✔
185
}
186

187
_hash_fn_t taosGetDefaultHashFunction(int32_t type) {
200,380,173✔
188
  _hash_fn_t fn = NULL;
200,380,173✔
189
  switch (type) {
200,380,173!
190
    case TSDB_DATA_TYPE_TIMESTAMP:
72,569,880✔
191
    case TSDB_DATA_TYPE_UBIGINT:
192
    case TSDB_DATA_TYPE_BIGINT:
193
      fn = taosIntHash_64;
72,569,880✔
194
      break;
72,569,880✔
195
    case TSDB_DATA_TYPE_BINARY:
72,888,211✔
196
    case TSDB_DATA_TYPE_VARBINARY:
197
    case TSDB_DATA_TYPE_NCHAR:
198
    case TSDB_DATA_TYPE_GEOMETRY:
199
      fn = MurmurHash3_32;
72,888,211✔
200
      break;
72,888,211✔
201
    case TSDB_DATA_TYPE_UINT:
52,363,325✔
202
    case TSDB_DATA_TYPE_INT:
203
      fn = taosIntHash_32;
52,363,325✔
204
      break;
52,363,325✔
205
    case TSDB_DATA_TYPE_SMALLINT:
2,182,106✔
206
    case TSDB_DATA_TYPE_USMALLINT:
207
      fn = taosIntHash_16;
2,182,106✔
208
      break;
2,182,106✔
209
    case TSDB_DATA_TYPE_BOOL:
1,446,992✔
210
    case TSDB_DATA_TYPE_UTINYINT:
211
    case TSDB_DATA_TYPE_TINYINT:
212
      fn = taosIntHash_8;
1,446,992✔
213
      break;
1,446,992✔
214
    case TSDB_DATA_TYPE_FLOAT:
300✔
215
      fn = taosFloatHash;
300✔
216
      break;
300✔
217
    case TSDB_DATA_TYPE_DOUBLE:
252✔
218
      fn = taosDoubleHash;
252✔
219
      break;
252✔
220
    default:
×
221
      fn = taosIntHash_32;
×
222
      break;
×
223
  }
224

225
  return fn;
200,380,173✔
226
}
227

228
int32_t taosFloatEqual(const void *a, const void *b, size_t UNUSED_PARAM(sz)) {
306✔
229
  // getComparFunc(TSDB_DATA_TYPE_FLOAT, -1) will always get function compareFloatVal, which will never be NULL.
230
  return getComparFunc(TSDB_DATA_TYPE_FLOAT, -1)(a, b);
306✔
231
}
232

233
int32_t taosDoubleEqual(const void *a, const void *b, size_t UNUSED_PARAM(sz)) {
264✔
234
  // getComparFunc(TSDB_DATA_TYPE_DOUBLE, -1) will always get function compareDoubleVal, which will never be NULL.
235
  return getComparFunc(TSDB_DATA_TYPE_DOUBLE, -1)(a, b);
264✔
236
}
237

238
_equal_fn_t taosGetDefaultEqualFunction(int32_t type) {
2,076,210✔
239
  _equal_fn_t fn = NULL;
2,076,210✔
240
  switch (type) {
2,076,210✔
241
    case TSDB_DATA_TYPE_FLOAT:
300✔
242
      fn = taosFloatEqual;
300✔
243
      break;
300✔
244
    case TSDB_DATA_TYPE_DOUBLE:
252✔
245
      fn = taosDoubleEqual;
252✔
246
      break;
252✔
247
    default:
2,075,658✔
248
      fn = memcmp;
2,075,658✔
249
      break;
2,075,658✔
250
  }
251
  return fn;
2,076,210✔
252
}
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