forked from hoangtq219/murmur3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
murmur3_128.go
237 lines (211 loc) · 4.91 KB
/
murmur3_128.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
package murmur3
import (
"log"
"strings"
)
/*
###############################
## guava version13.0 ##
###############################
*/
type Murmur3_128Hasher struct {
H1 int64
H2 int64
Length int
bb *MurmurByteBuffer
}
func initMurmur3_128Hasher(seed int64) *Murmur3_128Hasher {
return &Murmur3_128Hasher{
H1: seed,
H2: seed,
Length: 0,
bb: initMurmurByteBuffer(CHUNK_SIZE),
}
}
func (m *Murmur3_128Hasher) putString(CharSequence string) {
for i := 0; i < len(CharSequence); i++ {
//log.Printf("position: %d, limit: %d", m.bb.buffer.position, m.bb.buffer.limit)
m.putChar(CharSequence[i])
m.munchIfFull()
}
}
// Puts a character into this sink.
func (m *Murmur3_128Hasher) putChar(val byte) {
//HandlePrintf("putChar: " + strconv.FormatInt(int64(index), 10))
m.bb.putCharL(m.bb.ix(m.bb.nextPutIndex(2)), val)
}
func (m *Murmur3_128Hasher) munchIfFull() {
if m.bb.remaining() < 8 {
m.munch()
}
}
func (m *Murmur3_128Hasher) munch() {
//log.Printf("remaining = %d, position: %d, limit: %d", m.bb.remaining(), m.bb.position(), m.bb.limit())
m.bb.flip()
for m.bb.remaining() >= m.bb.chunkSize {
m.process()
}
m.bb.buffer.compact()
}
func (m *Murmur3_128Hasher) process() {
k1 := m.bb.getLong()
k2 := m.bb.getLong()
m.bmix64(k1, k2)
m.Length += 16
}
func (m *Murmur3_128Hasher) bmix64(k1, k2 int64) {
m.H1 ^= mixK1(k1)
m.H1 = rotateLeft(m.H1, 27)
m.H1 += m.H2
m.H1 = m.H1*5 + 1390208809
m.H2 ^= mixK2(k2)
m.H2 = rotateLeft(m.H2, 31)
m.H2 += m.H1
m.H2 = m.H2*5 + 944331445
}
func (m *Murmur3_128Hasher) processRemainingAfterBmixData() {
m.bb.positionFunc(m.bb.limit())
m.bb.limitFunc(m.bb.chunkSize + 7)
for m.bb.position() < m.bb.chunkSize {
m.bb.putLong(0)
}
m.bb.limitFunc(m.bb.chunkSize)
m.bb.flip()
m.process()
}
// HashString returns a 128 bits hasher set with explicit seed value
func HashString(seed int64, data string) *ByteBuffer {
m3 := initMurmur3_128Hasher(seed)
m3.putString(data)
m3.munch()
m3.bb.buffer.flip()
if m3.bb.remaining() > 0 {
m3.processRemaining()
}
return m3.makeHash()
}
func HashStringCustom(seed int64, data, userId string) []byte {
result := make([]byte, 0)
splits :=strings.Split(data, "_")
if len(splits) == 1 {
m3 := HashString(seed, data)
result = m3.AsIntBytes()
result = append(result, IntToBytes(data)...)
result = append(result, IntToBytes(userId)...)
} else if len(splits) == 2 {
m3 := HashString(-1467523828, splits[1])
result = append(m3.AsIntBytes(), IntToBytes(splits[0])...)
result = append(result, m3.AsLongBytes()...)
result = append(result, IntToBytes(userId)...)
} else {
handleWarnPrintf("Error hash with input: " + data)
}
return result
}
// Computes a hash code based on the data that have been provided to this hasher
func (m *Murmur3_128Hasher) makeHash() *ByteBuffer {
m.H1 ^= int64(m.Length)
m.H2 ^= int64(m.Length)
m.H1 += m.H2
m.H2 += m.H1
m.H1 = fmix64(m.H1)
m.H2 = fmix64(m.H2)
m.H1 += m.H2
m.H2 += m.H1
bb := &ByteBuffer{
// isReadOnly false
HB: make([]byte, 16),
Offset: 0,
Mark: -1,
Position: 0,
Limit: 16,
Capacity: 16,
BigEndian: false,
NativeByteOrder: false,
}
bb.putLong(m.H1)
bb.putLong(m.H2)
return bb
}
// Finalization mix - force all bits of a hash block to avalanche
func fmix64(k int64) int64 {
k = k ^ rightShift(k, 33)
k *= -49064778989728563
k = k ^ rightShift(k, 33)
k *= -4265267296055464877
k = k ^ rightShift(k, 33)
return k
}
func mixK1(k1 int64) int64 {
k1 *= c1
k1 = rotateLeft(k1, 31)
k1 *= c2
return k1
}
func mixK2(k2 int64) int64 {
k2 *= c2
k2 = rotateLeft(k2, 33)
k2 *= c1
return k2
}
/*
Sau khi kết thúc quá trình putChar, nếu số bytes còn lại > 0 (các byte này vẫn chưa được process với bmix64)
tiến hành process remaining với số bytes còn lại này trước khi hash
*/
func (m *Murmur3_128Hasher) processRemaining() {
var k1 int64 = 0
var k2 int64 = 0
m.Length += m.bb.remaining()
switch m.bb.remaining() {
case 7:
k1 ^= int64(m.bb.get(6)) << 48
fallthrough
case 6:
k1 ^= int64(m.bb.get(5)) << 40
fallthrough
case 5:
k1 ^= int64(m.bb.get(4)) << 32
fallthrough
case 4:
k1 ^= int64(m.bb.get(3)) << 24
fallthrough
case 3:
k1 ^= int64(m.bb.get(2)) << 16
fallthrough
case 2:
k1 ^= int64(m.bb.get(1)) << 8
fallthrough
case 1:
k1 ^= int64(m.bb.get(0))
break
case 15:
k2 ^= int64(m.bb.get(14)) << 48
fallthrough
case 14:
k2 ^= int64(m.bb.get(13)) << 40
fallthrough
case 13:
k2 ^= int64(m.bb.get(12)) << 32
fallthrough
case 12:
k2 ^= int64(m.bb.get(11)) << 24
fallthrough
case 11:
k2 ^= int64(m.bb.get(10)) << 16
fallthrough
case 10:
k2 ^= int64(m.bb.get(9)) << 8
fallthrough
case 9:
k2 ^= int64(m.bb.get(8))
m.H2 ^= mixK2(k2)
fallthrough
case 8:
k1 ^= m.bb.getLong()
break
default:
log.Println("[E] Should never get here")
}
m.H1 ^= mixK1(k1)
m.H2 ^= mixK2(k2)
}