@@ -15,13 +15,15 @@ import (
15
15
)
16
16
17
17
const (
18
- v32 = "32"
19
- v128 = "128" // default
18
+ v32Hash = "v32_hash"
19
+ v32Hex = "v32_hex"
20
+ v128Hash = "v128_hash" // default
21
+ v128Hex = "v128_hex"
20
22
)
21
23
22
24
type MurmurHash3Arguments [K any ] struct {
23
25
Target ottl.StringGetter [K ]
24
- Version ottl.Optional [string ] // 32-bit or 128-bit
26
+ Version ottl.Optional [string ]
25
27
}
26
28
27
29
func NewMurmurHash3Factory [K any ]() ottl.Factory [K ] {
@@ -35,45 +37,60 @@ func createMurmurHash3Function[K any](_ ottl.FunctionContext, oArgs ottl.Argumen
35
37
return nil , fmt .Errorf ("MurmurHash3Factory args must be of type *MurmurHash3Arguments[K]" )
36
38
}
37
39
38
- version := v128
40
+ version := v128Hash
39
41
if ! args .Version .IsEmpty () {
40
42
v := args .Version .Get ()
41
43
42
44
switch v {
43
- case v32 , v128 :
45
+ case v32Hash , v32Hex , v128Hash , v128Hex :
44
46
version = v
45
47
default :
46
- return nil , fmt .Errorf ("invalid arguments: %s. Version should be either \" 32 \" or \" 128 \" " , v )
48
+ return nil , fmt .Errorf ("invalid arguments: %s" , v )
47
49
}
48
50
}
49
51
50
- return HexStringLittleEndianVariant (args .Target , version )
52
+ return murmurHash3 (args .Target , version )
51
53
}
52
54
53
- // HexStringLittleEndianVariant returns the hexadecimal representation of the hash in little-endian format.
54
- // MurmurHash3, developed by Austin Appleby, is sensitive to endianness. Other languages like Python, Ruby,
55
- // and Java (using Guava) return a hexadecimal string in the little-endian variant. This function does the same.
56
- func HexStringLittleEndianVariant [K any ](target ottl.StringGetter [K ], version string ) (ottl.ExprFunc [K ], error ) {
55
+ func murmurHash3 [K any ](target ottl.StringGetter [K ], version string ) (ottl.ExprFunc [K ], error ) {
57
56
return func (ctx context.Context , tCtx K ) (any , error ) {
58
57
val , err := target .Get (ctx , tCtx )
59
58
if err != nil {
60
59
return nil , err
61
60
}
62
61
63
62
switch version {
64
- case v32 :
63
+ case v32Hash :
65
64
h := murmur3 .Sum32 ([]byte (val ))
66
- b := make ([]byte , 4 )
67
- binary .LittleEndian .PutUint32 (b , h )
68
- return hex .EncodeToString (b ), nil
69
- case v128 :
65
+ return int64 (h ), nil
66
+ case v128Hash :
70
67
h1 , h2 := murmur3 .Sum128 ([]byte (val ))
71
- b := make ([]byte , 16 )
72
- binary .LittleEndian .PutUint64 (b [:8 ], h1 )
73
- binary .LittleEndian .PutUint64 (b [8 :], h2 )
74
- return hex .EncodeToString (b ), nil
68
+ return []int64 {int64 (h1 ), int64 (h2 )}, nil
69
+ case v32Hex , v128Hex :
70
+ return hexStringLittleEndianVariant (val , version )
75
71
default :
76
72
return nil , fmt .Errorf ("invalid argument: %s" , version )
77
73
}
78
74
}, nil
79
75
}
76
+
77
+ // hexStringLittleEndianVariant returns the hexadecimal representation of the hash in little-endian format.
78
+ // MurmurHash3, developed by Austin Appleby, is sensitive to endianness. Other languages like Python, Ruby,
79
+ // and Java (using Guava) return a hexadecimal string in the little-endian variant. This function does the same.
80
+ func hexStringLittleEndianVariant (target string , version string ) (string , error ) {
81
+ switch version {
82
+ case v32Hex :
83
+ h := murmur3 .Sum32 ([]byte (target ))
84
+ b := make ([]byte , 4 )
85
+ binary .LittleEndian .PutUint32 (b , h )
86
+ return hex .EncodeToString (b ), nil
87
+ case v128Hex :
88
+ h1 , h2 := murmur3 .Sum128 ([]byte (target ))
89
+ b := make ([]byte , 16 )
90
+ binary .LittleEndian .PutUint64 (b [:8 ], h1 )
91
+ binary .LittleEndian .PutUint64 (b [8 :], h2 )
92
+ return hex .EncodeToString (b ), nil
93
+ default :
94
+ return "" , fmt .Errorf ("invalid argument: %s" , version )
95
+ }
96
+ }
0 commit comments