From 6ff1a90a1d5df8335be9f78089a77fa5e66c0e6d Mon Sep 17 00:00:00 2001 From: siyul-park Date: Fri, 24 Nov 2023 07:38:04 -0500 Subject: [PATCH] refactor: remove hash --- pkg/primitive/binary.go | 9 ------- pkg/primitive/binary_test.go | 6 ----- pkg/primitive/bool.go | 14 ---------- pkg/primitive/bool_test.go | 6 ----- pkg/primitive/float.go | 25 ----------------- pkg/primitive/float_test.go | 14 ---------- pkg/primitive/int.go | 52 ------------------------------------ pkg/primitive/int_test.go | 28 ------------------- pkg/primitive/map.go | 29 -------------------- pkg/primitive/map_test.go | 14 +--------- pkg/primitive/object.go | 1 - pkg/primitive/slice.go | 22 --------------- pkg/primitive/slice_test.go | 9 ------- pkg/primitive/string.go | 9 ------- pkg/primitive/string_test.go | 6 ----- pkg/primitive/uint.go | 52 ------------------------------------ pkg/primitive/uint_test.go | 28 ------------------- 17 files changed, 1 insertion(+), 323 deletions(-) diff --git a/pkg/primitive/binary.go b/pkg/primitive/binary.go index 838139ff..4aea48b6 100644 --- a/pkg/primitive/binary.go +++ b/pkg/primitive/binary.go @@ -3,7 +3,6 @@ package primitive import ( "encoding" "fmt" - "hash/fnv" "reflect" "github.com/pkg/errors" @@ -83,14 +82,6 @@ func (o Binary) Compare(v Object) int { } } -func (o Binary) Hash() uint32 { - h := fnv.New32() - h.Write([]byte{byte(KindBinary), 0}) - h.Write([]byte(o)) - - return h.Sum32() -} - func (o Binary) Interface() any { return []byte(o) } diff --git a/pkg/primitive/binary_test.go b/pkg/primitive/binary_test.go index 87dadc6f..18b7d645 100644 --- a/pkg/primitive/binary_test.go +++ b/pkg/primitive/binary_test.go @@ -37,12 +37,6 @@ func TestBinary_Compare(t *testing.T) { assert.Equal(t, 1, v2.Compare(v1)) } -func TestBinary_Hash(t *testing.T) { - assert.NotEqual(t, NewBinary([]byte{0}).Hash(), NewBinary([]byte{1}).Hash()) - assert.Equal(t, NewBinary(nil).Hash(), NewBinary(nil).Hash()) - assert.Equal(t, NewBinary([]byte{0}).Hash(), NewBinary([]byte{0}).Hash()) -} - func TestBinary_Encode(t *testing.T) { e := NewBinaryEncoder() diff --git a/pkg/primitive/bool.go b/pkg/primitive/bool.go index efcbaab4..5dd3598c 100644 --- a/pkg/primitive/bool.go +++ b/pkg/primitive/bool.go @@ -1,7 +1,6 @@ package primitive import ( - "hash/fnv" "reflect" "github.com/pkg/errors" @@ -58,19 +57,6 @@ func (o Bool) Compare(v Object) int { } } -func (o Bool) Hash() uint32 { - var v byte - if o { - v |= 1 - } - - h := fnv.New32() - h.Write([]byte{byte(KindBool), 0}) - h.Write([]byte{v}) - - return h.Sum32() -} - func (o Bool) Interface() any { return bool(o) } diff --git a/pkg/primitive/bool_test.go b/pkg/primitive/bool_test.go index 4f3865a3..7f2db62b 100644 --- a/pkg/primitive/bool_test.go +++ b/pkg/primitive/bool_test.go @@ -27,12 +27,6 @@ func TestBool_Equal(t *testing.T) { assert.False(t, FALSE.Equal(TRUE)) } -func TestBool_Hash(t *testing.T) { - assert.NotEqual(t, TRUE.Hash(), FALSE.Hash()) - assert.Equal(t, TRUE.Hash(), TRUE.Hash()) - assert.Equal(t, FALSE.Hash(), FALSE.Hash()) -} - func TestBool_Encode(t *testing.T) { e := NewBoolEncoder() diff --git a/pkg/primitive/float.go b/pkg/primitive/float.go index 59d3966e..05daa1e4 100644 --- a/pkg/primitive/float.go +++ b/pkg/primitive/float.go @@ -1,9 +1,6 @@ package primitive import ( - "encoding/binary" - "hash/fnv" - "math" "reflect" "github.com/pkg/errors" @@ -68,17 +65,6 @@ func (o Float32) Compare(v Object) int { } } -func (o Float32) Hash() uint32 { - var buf [4]byte - binary.BigEndian.PutUint32(buf[:], math.Float32bits(float32(o))) - - h := fnv.New32() - h.Write([]byte{byte(KindFloat32), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Float32) Interface() any { return float32(o) } @@ -127,17 +113,6 @@ func (o Float64) Compare(v Object) int { } } -func (o Float64) Hash() uint32 { - var buf [8]byte - binary.BigEndian.PutUint64(buf[:], math.Float64bits(float64(o))) - - h := fnv.New32() - h.Write([]byte{byte(KindFloat64), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Float64) Interface() any { return float64(o) } diff --git a/pkg/primitive/float_test.go b/pkg/primitive/float_test.go index 3507321c..a05a8a68 100644 --- a/pkg/primitive/float_test.go +++ b/pkg/primitive/float_test.go @@ -51,20 +51,6 @@ func TestFloat_Compare(t *testing.T) { }) } -func TestFloat_Hash(t *testing.T) { - t.Run("32", func(t *testing.T) { - assert.NotEqual(t, NewFloat32(0).Hash(), NewFloat32(1).Hash()) - assert.Equal(t, NewFloat32(0).Hash(), NewFloat32(0).Hash()) - assert.Equal(t, NewFloat32(1).Hash(), NewFloat32(1).Hash()) - }) - - t.Run("64", func(t *testing.T) { - assert.NotEqual(t, NewFloat64(0).Hash(), NewFloat64(1).Hash()) - assert.Equal(t, NewFloat64(0).Hash(), NewFloat64(0).Hash()) - assert.Equal(t, NewFloat64(1).Hash(), NewFloat64(1).Hash()) - }) -} - func TestFloat_Encode(t *testing.T) { e := NewFloatEncoder() diff --git a/pkg/primitive/int.go b/pkg/primitive/int.go index f2c2baa2..31e78913 100644 --- a/pkg/primitive/int.go +++ b/pkg/primitive/int.go @@ -1,9 +1,7 @@ package primitive import ( - "hash/fnv" "reflect" - "unsafe" "github.com/pkg/errors" "github.com/siyul-park/uniflow/internal/encoding" @@ -76,16 +74,6 @@ func (o Int) Compare(v Object) int { } } -func (o Int) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindInt), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Int) Interface() any { return int(o) } @@ -134,16 +122,6 @@ func (o Int8) Compare(v Object) int { } } -func (o Int8) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindInt8), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Int8) Interface() any { return int8(o) } @@ -192,16 +170,6 @@ func (o Int16) Compare(v Object) int { } } -func (o Int16) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindInt16), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Int16) Interface() any { return int16(o) } @@ -250,16 +218,6 @@ func (o Int32) Compare(v Object) int { } } -func (o Int32) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindInt32), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Int32) Interface() any { return int32(o) } @@ -308,16 +266,6 @@ func (o Int64) Compare(v Object) int { } } -func (o Int64) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindInt64), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Int64) Interface() any { return int64(o) } diff --git a/pkg/primitive/int_test.go b/pkg/primitive/int_test.go index e253fb9c..18209da7 100644 --- a/pkg/primitive/int_test.go +++ b/pkg/primitive/int_test.go @@ -100,34 +100,6 @@ func TestInt_Compare(t *testing.T) { }) } -func TestInt_Hash(t *testing.T) { - t.Run("", func(t *testing.T) { - assert.NotEqual(t, NewInt(0).Hash(), NewInt(1).Hash()) - assert.Equal(t, NewInt(0).Hash(), NewInt(0).Hash()) - assert.Equal(t, NewInt(1).Hash(), NewInt(1).Hash()) - }) - t.Run("8", func(t *testing.T) { - assert.NotEqual(t, NewInt8(0).Hash(), NewInt8(1).Hash()) - assert.Equal(t, NewInt8(0).Hash(), NewInt8(0).Hash()) - assert.Equal(t, NewInt8(1).Hash(), NewInt8(1).Hash()) - }) - t.Run("16", func(t *testing.T) { - assert.NotEqual(t, NewInt16(0).Hash(), NewInt16(1).Hash()) - assert.Equal(t, NewInt16(0).Hash(), NewInt16(0).Hash()) - assert.Equal(t, NewInt16(1).Hash(), NewInt16(1).Hash()) - }) - t.Run("32", func(t *testing.T) { - assert.NotEqual(t, NewInt32(0).Hash(), NewInt32(1).Hash()) - assert.Equal(t, NewInt32(0).Hash(), NewInt32(0).Hash()) - assert.Equal(t, NewInt32(1).Hash(), NewInt32(1).Hash()) - }) - t.Run("64", func(t *testing.T) { - assert.NotEqual(t, NewInt64(0).Hash(), NewInt64(1).Hash()) - assert.Equal(t, NewInt64(0).Hash(), NewInt64(0).Hash()) - assert.Equal(t, NewInt64(1).Hash(), NewInt64(1).Hash()) - }) -} - func TestInt_Encode(t *testing.T) { e := NewIntEncoder() diff --git a/pkg/primitive/map.go b/pkg/primitive/map.go index 27157f48..82a213b2 100644 --- a/pkg/primitive/map.go +++ b/pkg/primitive/map.go @@ -2,10 +2,8 @@ package primitive import ( "fmt" - "hash/fnv" "reflect" "strings" - "unsafe" "github.com/benbjohnson/immutable" "github.com/iancoleman/strcase" @@ -177,33 +175,6 @@ func (o *Map) Compare(v Object) int { } } -func (o *Map) Hash() uint32 { - h := fnv.New32() - h.Write([]byte{byte(KindMap), 0}) - - itr := o.value.Iterator() - for !itr.Done() { - k, v, _ := itr.Next() - - if k != nil { - hash := k.Hash() - buf := *(*[unsafe.Sizeof(hash)]byte)(unsafe.Pointer(&hash)) - h.Write(buf[:]) - } else { - h.Write([]byte{0}) - } - if v != nil { - hash := v.Hash() - buf := *(*[unsafe.Sizeof(hash)]byte)(unsafe.Pointer(&hash)) - h.Write(buf[:]) - } else { - h.Write([]byte{0}) - } - } - - return h.Sum32() -} - func (o *Map) Interface() any { var keys []any var values []any diff --git a/pkg/primitive/map_test.go b/pkg/primitive/map_test.go index f50a74bf..b949bd3d 100644 --- a/pkg/primitive/map_test.go +++ b/pkg/primitive/map_test.go @@ -27,18 +27,6 @@ func TestMap_Equal(t *testing.T) { assert.False(t, NewMap(k1, v1).Equal(NewMap(k2, v2))) } -func TestMap_Hash(t *testing.T) { - k1 := NewString(faker.Word()) - k2 := NewString(faker.Word()) - v1 := NewString(faker.Word()) - v2 := NewString(faker.Word()) - - assert.NotEqual(t, NewMap(k1, v1).Hash(), NewMap(k2, v2).Hash()) - assert.Equal(t, NewMap().Hash(), NewMap().Hash()) - assert.Equal(t, NewMap(k1, v1).Hash(), NewMap(k1, v1).Hash()) - assert.Equal(t, NewMap(k1, v1, k2, v2).Hash(), NewMap(k2, v2, k1, v1).Hash()) -} - func TestMap_GetAndSetAndDelete(t *testing.T) { k1 := NewString(faker.Word()) v1 := NewString(faker.Word()) @@ -99,7 +87,7 @@ func TestMap_Encode(t *testing.T) { K1: v1.String(), }) assert.NoError(t, err) - assert.Equal(t, NewMap(NewString("k_1"), v1).Hash(), v.Hash()) + assert.True(t, NewMap(NewString("k_1"), v1).Compare(v) == 0) }) } diff --git a/pkg/primitive/object.go b/pkg/primitive/object.go index 7993b960..1bc13b02 100644 --- a/pkg/primitive/object.go +++ b/pkg/primitive/object.go @@ -6,7 +6,6 @@ type ( Kind() Kind Equal(v Object) bool Compare(v Object) int - Hash() uint32 Interface() any } diff --git a/pkg/primitive/slice.go b/pkg/primitive/slice.go index fc3f0316..2d626b5e 100644 --- a/pkg/primitive/slice.go +++ b/pkg/primitive/slice.go @@ -2,9 +2,7 @@ package primitive import ( "fmt" - "hash/fnv" "reflect" - "unsafe" "github.com/benbjohnson/immutable" "github.com/pkg/errors" @@ -120,26 +118,6 @@ func (o *Slice) Compare(v Object) int { } } -func (o *Slice) Hash() uint32 { - h := fnv.New32() - h.Write([]byte{byte(KindSlice), 0}) - - itr := o.value.Iterator() - for !itr.Done() { - _, v := itr.Next() - - if v != nil { - hash := v.Hash() - buf := *(*[unsafe.Sizeof(hash)]byte)(unsafe.Pointer(&hash)) - h.Write(buf[:]) - } else { - h.Write([]byte{0}) - } - } - - return h.Sum32() -} - func (o *Slice) Interface() any { var values []any itr := o.value.Iterator() diff --git a/pkg/primitive/slice_test.go b/pkg/primitive/slice_test.go index 66712060..0e511887 100644 --- a/pkg/primitive/slice_test.go +++ b/pkg/primitive/slice_test.go @@ -79,15 +79,6 @@ func TestSlice_Compare(t *testing.T) { assert.Equal(t, -1, NewSlice(v1, v2).Compare(NewSlice(v2, v1))) } -func TestSlice_Hash(t *testing.T) { - v1 := NewString(faker.Word()) - v2 := NewString(faker.Word()) - - assert.NotEqual(t, NewSlice(v1, v2).Hash(), NewSlice(v2, v1).Hash()) - assert.Equal(t, NewSlice().Hash(), NewSlice().Hash()) - assert.Equal(t, NewSlice(v1, v2).Hash(), NewSlice(v1, v2).Hash()) -} - func TestSlice_Encode(t *testing.T) { e := NewSliceEncoder(NewStringEncoder()) diff --git a/pkg/primitive/string.go b/pkg/primitive/string.go index d63c8e4c..cefc2008 100644 --- a/pkg/primitive/string.go +++ b/pkg/primitive/string.go @@ -2,7 +2,6 @@ package primitive import ( "encoding" - "hash/fnv" "reflect" "github.com/pkg/errors" @@ -61,14 +60,6 @@ func (o String) Compare(v Object) int { } } -func (o String) Hash() uint32 { - h := fnv.New32() - h.Write([]byte{byte(KindString), 0}) - h.Write([]byte(o)) - - return h.Sum32() -} - func (o String) Interface() any { return string(o) } diff --git a/pkg/primitive/string_test.go b/pkg/primitive/string_test.go index 7a3ed52e..19212369 100644 --- a/pkg/primitive/string_test.go +++ b/pkg/primitive/string_test.go @@ -33,12 +33,6 @@ func TestString_Compare(t *testing.T) { assert.Equal(t, -1, NewString("A").Compare(NewString("a"))) } -func TestString_Hash(t *testing.T) { - assert.NotEqual(t, NewString("A").Hash(), NewString("B").Hash()) - assert.Equal(t, NewString("").Hash(), NewString("").Hash()) - assert.Equal(t, NewString("A").Hash(), NewString("A").Hash()) -} - func TestString_Encode(t *testing.T) { e := NewStringEncoder() diff --git a/pkg/primitive/uint.go b/pkg/primitive/uint.go index afceae2f..a7602764 100644 --- a/pkg/primitive/uint.go +++ b/pkg/primitive/uint.go @@ -1,9 +1,7 @@ package primitive import ( - "hash/fnv" "reflect" - "unsafe" "github.com/pkg/errors" "github.com/siyul-park/uniflow/internal/encoding" @@ -76,16 +74,6 @@ func (o Uint) Compare(v Object) int { } } -func (o Uint) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindUint), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Uint) Interface() any { return uint(o) } @@ -134,16 +122,6 @@ func (o Uint8) Compare(v Object) int { } } -func (o Uint8) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindUint8), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Uint8) Interface() any { return uint8(o) } @@ -192,16 +170,6 @@ func (o Uint16) Compare(v Object) int { } } -func (o Uint16) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindUint16), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Uint16) Interface() any { return uint16(o) } @@ -250,16 +218,6 @@ func (o Uint32) Compare(v Object) int { } } -func (o Uint32) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindUint32), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Uint32) Interface() any { return uint32(o) } @@ -308,16 +266,6 @@ func (o Uint64) Compare(v Object) int { } } -func (o Uint64) Hash() uint32 { - buf := *(*[unsafe.Sizeof(o)]byte)(unsafe.Pointer(&o)) - - h := fnv.New32() - h.Write([]byte{byte(KindUint64), 0}) - h.Write(buf[:]) - - return h.Sum32() -} - func (o Uint64) Interface() any { return uint64(o) } diff --git a/pkg/primitive/uint_test.go b/pkg/primitive/uint_test.go index f098ed51..7a0e2ed0 100644 --- a/pkg/primitive/uint_test.go +++ b/pkg/primitive/uint_test.go @@ -100,34 +100,6 @@ func TestUint_Compare(t *testing.T) { }) } -func TestUint_Hash(t *testing.T) { - t.Run("", func(t *testing.T) { - assert.NotEqual(t, NewUint(0).Hash(), NewUint(1).Hash()) - assert.Equal(t, NewUint(0).Hash(), NewUint(0).Hash()) - assert.Equal(t, NewUint(1).Hash(), NewUint(1).Hash()) - }) - t.Run("8", func(t *testing.T) { - assert.NotEqual(t, NewUint8(0).Hash(), NewUint8(1).Hash()) - assert.Equal(t, NewUint8(0).Hash(), NewUint8(0).Hash()) - assert.Equal(t, NewUint8(1).Hash(), NewUint8(1).Hash()) - }) - t.Run("16", func(t *testing.T) { - assert.NotEqual(t, NewUint16(0).Hash(), NewUint16(1).Hash()) - assert.Equal(t, NewUint16(0).Hash(), NewUint16(0).Hash()) - assert.Equal(t, NewUint16(1).Hash(), NewUint16(1).Hash()) - }) - t.Run("32", func(t *testing.T) { - assert.NotEqual(t, NewUint32(0).Hash(), NewUint32(1).Hash()) - assert.Equal(t, NewUint32(0).Hash(), NewUint32(0).Hash()) - assert.Equal(t, NewUint32(1).Hash(), NewUint32(1).Hash()) - }) - t.Run("64", func(t *testing.T) { - assert.NotEqual(t, NewUint64(0).Hash(), NewUint64(1).Hash()) - assert.Equal(t, NewUint64(0).Hash(), NewUint64(0).Hash()) - assert.Equal(t, NewUint64(1).Hash(), NewUint64(1).Hash()) - }) -} - func TestUint_Encode(t *testing.T) { e := NewUintEncoder()