-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathutil.go
582 lines (554 loc) · 15.3 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package concurrent
import (
"fmt"
"hash/fnv"
"io"
"math/rand"
"reflect"
"sync/atomic"
"unsafe"
)
const (
intSize = unsafe.Sizeof(1)
ptrSize = unsafe.Sizeof((*int)(nil))
bigEndian = false
)
var (
hasherT = reflect.TypeOf((*Hashable)(nil)).Elem()
defaultEqualsfunc func(k1 interface{}, k2 interface{}) bool
hasherEng *hashEnginer
boolEng *hashEnginer
intEng *hashEnginer
int8Eng *hashEnginer
int16Eng *hashEnginer
int32Eng *hashEnginer
int64Eng *hashEnginer
uintEng *hashEnginer
uint8Eng *hashEnginer
uint16Eng *hashEnginer
uint32Eng *hashEnginer
uint64Eng *hashEnginer
uintptrEng *hashEnginer
float32Eng *hashEnginer
float64Eng *hashEnginer
complex64Eng *hashEnginer
complex128Eng *hashEnginer
stringEng *hashEnginer
engM map[reflect.Kind]*hashEnginer
)
func init() {
hasherEng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
w.Write(k.(Hashable).HashBytes())
},
}
boolEng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(bool)
w.Write((*((*[1]byte)(unsafe.Pointer(&k1))))[:])
},
}
intEng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(int)
w.Write((*((*[intSize]byte)(unsafe.Pointer(&k1))))[:])
},
}
int8Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(int8)
w.Write((*((*[1]byte)(unsafe.Pointer(&k1))))[:])
},
}
int16Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(int16)
w.Write((*((*[2]byte)(unsafe.Pointer(&k1))))[:])
},
}
int32Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(int32)
w.Write((*((*[4]byte)(unsafe.Pointer(&k1))))[:])
},
}
int64Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(int64)
w.Write((*((*[8]byte)(unsafe.Pointer(&k1))))[:])
},
}
uintEng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(uint)
w.Write((*((*[intSize]byte)(unsafe.Pointer(&k1))))[:])
},
}
uint8Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(uint8)
w.Write((*((*[1]byte)(unsafe.Pointer(&k1))))[:])
},
}
uint16Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(uint16)
w.Write((*((*[2]byte)(unsafe.Pointer(&k1))))[:])
},
}
uint32Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(uint32)
w.Write((*((*[4]byte)(unsafe.Pointer(&k1))))[:])
},
}
uint64Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(uint64)
w.Write((*((*[8]byte)(unsafe.Pointer(&k1))))[:])
},
}
uintptrEng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(uintptr)
w.Write((*((*[intSize]byte)(unsafe.Pointer(&k1))))[:])
},
}
float32Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(float32)
//Nan != Nan, so use a rand number to generate hash code
if k1 != k1 {
k1 = rand.Float32()
}
w.Write((*((*[4]byte)(unsafe.Pointer(&k1))))[:])
},
}
float64Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(float64)
//Nan != Nan, so use a rand number to generate hash code
if k1 != k1 {
k1 = rand.Float64()
}
w.Write((*((*[8]byte)(unsafe.Pointer(&k1))))[:])
},
}
complex64Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(complex64)
w.Write((*((*[8]byte)(unsafe.Pointer(&k1))))[:])
},
}
complex128Eng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(complex128)
w.Write((*((*[128]byte)(unsafe.Pointer(&k1))))[:])
},
}
stringEng = &hashEnginer{
putFunc: func(w io.Writer, k interface{}) {
k1 := k.(string)
w.Write([]byte(k1))
},
}
engM = map[reflect.Kind]*hashEnginer{
reflect.Bool: boolEng,
reflect.Int: intEng,
reflect.Int8: int8Eng,
reflect.Int16: int16Eng,
reflect.Int32: int32Eng,
reflect.Int64: int64Eng,
reflect.Uint: uintEng,
reflect.Uint8: uint8Eng,
reflect.Uint16: uint16Eng,
reflect.Uint32: uint32Eng,
reflect.Uint64: uint64Eng,
reflect.Uintptr: uintptrEng,
reflect.Float32: float32Eng,
reflect.Float64: float64Eng,
reflect.Complex64: complex64Eng,
reflect.Complex128: complex128Eng,
reflect.String: stringEng,
}
}
func hashKey(key interface{}, m *ConcurrentMap, isRead bool) (hashCode uint32, err error) {
h := fnv.New32a()
switch v := key.(type) {
case bool:
h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case int:
h.Write((*((*[intSize]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case int8:
h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case int16:
h.Write((*((*[2]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case int32:
h.Write((*((*[4]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case int64:
h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case uint:
h.Write((*((*[intSize]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case uint8:
h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case uint16:
h.Write((*((*[2]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case uint32:
h.Write((*((*[4]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case uint64:
h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case uintptr:
h.Write((*((*[intSize]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case float32:
//Nan != Nan, so use a rand number to generate hash code
if v != v {
v = rand.Float32()
}
h.Write((*((*[4]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case float64:
//Nan != Nan, so use a rand number to generate hash code
if v != v {
v = rand.Float64()
}
h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case complex64:
h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case complex128:
h.Write((*((*[128]byte)(unsafe.Pointer(&v))))[:])
hashCode = h.Sum32()
case string:
h.Write([]byte(v))
hashCode = h.Sum32()
default:
//if key is not simple type
if her, ok := key.(Hashable); ok {
h.Write(her.HashBytes())
} else {
if err = m.parseKey(key); err != nil {
return
}
if isRead {
eng := (*hashEnginer)(atomic.LoadPointer(&m.eng))
eng.putFunc(h, key)
} else {
eng := (*hashEnginer)(m.eng)
eng.putFunc(h, key)
}
hashCode = h.Sum32()
}
}
return
}
////hash a interface using FNVa
//func hashI(val interface{}) (hashCode uint32) {
// h := fnv.New32a()
// switch v := val.(type) {
// case bool:
// h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case int:
// h.Write((*((*[intSize]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case int8:
// h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case int16:
// h.Write((*((*[2]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case int32:
// h.Write((*((*[4]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case int64:
// h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case uint:
// h.Write((*((*[1]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case uint8:
// h.Write((*((*[intSize]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case uint16:
// h.Write((*((*[2]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case uint32:
// h.Write((*((*[4]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case uint64:
// h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case uintptr:
// h.Write((*((*[intSize]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case float32:
// //Nan != Nan, so use a rand number to generate hash code
// if v != v {
// v = rand.Float32()
// }
// h.Write((*((*[4]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case float64:
// //Nan != Nan, so use a rand number to generate hash code
// if v != v {
// v = rand.Float64()
// }
// h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case complex64:
// h.Write((*((*[8]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case complex128:
// h.Write((*((*[128]byte)(unsafe.Pointer(&v))))[:])
// hashCode = h.Sum32()
// case string:
// h.Write([]byte(v))
// hashCode = h.Sum32()
// default:
// //some types can be used as key, we can use equals to test
// _ = val == val
// //support array, struct, channel, interface, pointer
// //don't support slice, function, map
// rv := reflect.ValueOf(val)
// switch rv.Kind() {
// case reflect.Ptr:
// //ei.word stores the memory address of value that v points to, we use address to generate hash code
// ei := (*emptyInterface)(unsafe.Pointer(&val))
// hashCode = hashI(uintptr(ei.word))
// case reflect.Interface:
// //for interface, we use contained value to generate the hash code
// hashCode = hashI(rv.Elem())
// default:
// //for array, struct and chan, will get byte array to calculate the hash code
// hashMem(rv, h)
// hashCode = h.Sum32()
// fmt.Println("array, struct or chan", rv.Interface(), hashCode, reflect.ValueOf(rv).Type().Size())
// }
// }
// return
//}
////hashMem writes byte array of underlying value to hash function
//func hashMem(i interface{}, hashFunc hash.Hash32) {
// fmt.Println("hashMem")
// size := reflect.ValueOf(i).Type().Size()
// ei := (*emptyInterface)(unsafe.Pointer(&i))
// //if size of underlying value is greater than pointer size, ei.word will store the pointer that point to underlying value
// //else ei.word will store underlying value
// if size > ptrSize {
// addr := ei.word
// hashPtrData(unsafe.Pointer(uintptr(addr)), size, hashFunc)
// } else {
// data := ei.word
// fmt.Println("hashData", uintptr(data), size, ptrSize)
// hashData(uintptr(data), size, hashFunc)
// }
// return
//}
//func hashPtrData(basePtr unsafe.Pointer, size uintptr, hashFunc hash.Hash32) {
// offset := uintptr(0)
// for {
// /* cannot store unsafe.Pointer in an uintptr according to https://groups.google.com/forum/#!topic/golang-dev/bfMdPAQigfM
// * but the expression
// * unsafe.Pointer(uintptr(basePtr) + offset)
// * is safe under Go 1.3
// */
// //d := uintptr(basePtr) + offset
// //ptr := unsafe.Pointer(d)
// ptr := unsafe.Pointer(uintptr(basePtr) + offset)
// if size >= 32 {
// bytes := *(*[32]byte)(ptr)
// size -= 32
// offset += 32
// fmt.Println("hashPtrData", ptr, bytes[:])
// hashFunc.Write(bytes[:])
// } else if size >= 16 {
// bytes := *(*[16]byte)(ptr)
// size -= 16
// offset += 16
// hashFunc.Write(bytes[:])
// } else if size >= 8 {
// bytes := *(*[8]byte)(ptr)
// size -= 8
// offset += 8
// hashFunc.Write(bytes[:])
// } else if size >= 4 {
// bytes := *(*[4]byte)(ptr)
// size -= 4
// offset += 4
// hashFunc.Write(bytes[:])
// } else if size >= 2 {
// bytes := *(*[2]byte)(ptr)
// size -= 2
// offset += 2
// hashFunc.Write(bytes[:])
// } else if size == 1 {
// bytes := *(*[1]byte)(ptr)
// hashFunc.Write(bytes[:])
// return
// }
// if size == 0 {
// return
// }
// }
//}
//func hashData(data uintptr, size uintptr, hashFunc hash.Hash32) {
// bytes := (*((*[ptrSize]byte)(unsafe.Pointer(&data))))
// hashFunc.Write(bytes[0:size])
// return
//}
func isNil(v interface{}) bool {
if v == nil {
return true
}
rv := reflect.ValueOf(v)
k := rv.Type().Kind()
switch k {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
return rv.IsNil()
default:
return false
}
}
//// emptyInterface is the header for an interface{} value.
//type emptyInterface struct {
// typ uintptr
// word unsafe.Pointer
//}
type keyInfo struct {
isHasher bool
/*-- kind of key type --*/
kind reflect.Kind
/*-- index of field if it is a field of struct --*/
index []int
/*-- field informations of struct --*/
fields []*keyInfo
/*-- element information of array --*/
elementInfo *keyInfo
size int
}
//获取t对应的类型信息,不支持slice, function, map, pointer, interface, channel
func getKeyInfo(t reflect.Type) (ki *keyInfo, err error) {
return getKeyInfoByParent(t, nil, make([]int, 0, 0))
}
//获取t对应的类型信息,不支持slice, function, map, pointer, interface, channel
//如果parentIdx的长度>0,则表示t是strut中的字段的类型信息, t为字段对应的类型
func getKeyInfoByParent(t reflect.Type, parent *keyInfo, parentIdx []int) (ki *keyInfo, err error) {
ki = &keyInfo{}
//判断是否实现了hasher接口
if t.Implements(hasherT) {
ki.isHasher = true
return
}
ki.kind = t.Kind()
if _, ok := engM[ki.kind]; ok {
//简单类型,不需要再分解元素类型的信息
ki.index = parentIdx
} else {
//some types can be used as key, we can use equals to test
switch ki.kind {
case reflect.Chan, reflect.Slice, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface:
err = NonSupportKey
case reflect.Struct:
if parent == nil {
//parent==nil表示t不是一个嵌套的struct,所以这里需要初始化fields
parent = ki
ki.fields = make([]*keyInfo, 0, t.NumField())
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
//skip unexported field,
if len(f.PkgPath) > 0 {
continue
}
idx := make([]int, len(parentIdx), len(parentIdx)+1)
copy(idx, parentIdx)
idx = append(idx, i)
if fi, e := getKeyInfoByParent(f.Type, parent, idx); e != nil {
err = e
return
} else {
//fi.index = i
parent.fields = append(ki.fields, fi)
}
}
case reflect.Array:
if ki.elementInfo, err = getKeyInfo(t.Elem()); err != nil {
return
}
ki.size = t.Len()
ki.index = parentIdx
}
}
return
}
func getPutFunc(ki *keyInfo) func(w io.Writer, k interface{}) {
if ki.isHasher {
return hasherEng.putFunc
}
//Printf("getPutFunc, ki = %v\n", ki)
if eng, ok := engM[ki.kind]; ok {
return eng.putFunc
} else {
if ki.kind == reflect.Struct {
//Printf("getPutFunc, ki = %v, other case\n", ki)
putFunc := func(w io.Writer, k interface{}) {
rv := reflect.ValueOf(k)
for _, fieldInfo := range ki.fields {
//深度遍历每个field,并将其[]byte写入hash函数
putF := getPutFunc(fieldInfo)
//Printf("getPutFunc, ki = %#v, fieldInfo = %#v, %#v\n", ki, fieldInfo, rv.Interface())
//Printf("getPutFunc, value = %v\n", rv.FieldByIndex(fieldInfo.index).Interface())
putF(w, rv.FieldByIndex(fieldInfo.index).Interface())
}
}
//Printf("getPutFunc, ki=%v, putFunc = %v, other case\n", ki, putFunc)
return putFunc
} else if ki.kind == reflect.Array {
putFunc := func(w io.Writer, k interface{}) {
rv := reflect.ValueOf(k)
putF := getPutFunc(ki.elementInfo)
for i := 0; i < ki.size; i++ {
//遍历数组元素,并将其[]byte写入hash函数
putF(w, rv.Index(i).Interface())
}
}
return putFunc
}
Printf("getPutFunc, return nil")
}
return nil
}
func equals(k1, k2 interface{}) bool {
if h1, ok := k1.(Hashable); ok {
return h1.Equals(k2)
} else {
return k1 == k2
}
}
func Printf(format string, a ...interface{}) (n int, err error) {
if Debug {
return fmt.Printf(format, a...)
} else {
return 0, nil
}
}
func Println(a ...interface{}) (n int, err error) {
if Debug {
return fmt.Println(a...)
} else {
return 0, nil
}
}