Skip to content

Commit

Permalink
feat: use net/netip
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Apr 5, 2022
1 parent e46a620 commit 4d714a8
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 55 deletions.
5 changes: 2 additions & 3 deletions block_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ package ch

import (
"bytes"
"net/netip"
"strings"
"testing"

"inet.af/netaddr"

"github.com/go-faster/ch/internal/gold"
"github.com/go-faster/ch/proto"
)
Expand Down Expand Up @@ -58,7 +57,7 @@ func encodeTestIPv6Block() []byte {
} {
var values []proto.IPv6
for _, s := range v {
ip := netaddr.MustParseIP(s)
ip := netip.MustParseAddr(s)
values = append(values, proto.ToIPv6(ip))
}
arr.AppendIPv6(values)
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ require (
go.uber.org/multierr v1.8.0
go.uber.org/zap v1.21.0
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6
)

require (
Expand All @@ -32,8 +31,6 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pascaldekloe/name v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go4.org/intern v0.0.0-20211027215823-ae77deb06f29 // indirect
go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 // indirect
golang.org/x/tools v0.1.9 // indirect
Expand Down
15 changes: 0 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ github.com/dmarkham/enumer v1.5.4 h1:XaD7WWAe1qzgR7CvXKwh4rDBulcsIG24O7RYZBI9p6M
github.com/dmarkham/enumer v1.5.4/go.mod h1:VE3gc4kSCXkpXohQ1zhVI0d0xGM6AYHVyHHEInjCRp4=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw=
github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw=
github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw=
github.com/go-faster/errors v0.5.0 h1:hS/zHFJ2Vb14jcupq5J9tk05XW+PFTmySOkDRByHBo4=
Expand Down Expand Up @@ -47,7 +46,6 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.opentelemetry.io/otel v1.6.0/go.mod h1:bfJD2DZVw0LBxghOTlgnlI0CV3hLDu9XF/QKOUXMTQQ=
go.opentelemetry.io/otel v1.6.1 h1:6r1YrcTenBvYa1x491d0GGpTVBsNECmrc/K6b+zDeis=
Expand All @@ -69,32 +67,22 @@ go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
go4.org/intern v0.0.0-20211027215823-ae77deb06f29 h1:UXLjNohABv4S58tHmeuIZDO6e3mHpW2Dx33gaNt03LE=
go4.org/intern v0.0.0-20211027215823-ae77deb06f29/go.mod h1:cS2ma+47FKrLPdXFpr7CuxiTW3eyJbWew4qx0qtQWDA=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37 h1:Tx9kY6yUkLge/pFG7IEMwDZy6CS2ajFc9TvQdPCW0uA=
go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -106,7 +94,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
Expand All @@ -123,5 +110,3 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6 h1:acCzuUSQ79tGsM/O50VRFySfMm19IoMKL+sZztZkCxw=
inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6/go.mod h1:y3MGhcFMlh0KZPMuXXow8mpjxxAk3yoDNsp4cQz54i8=
11 changes: 5 additions & 6 deletions proto/ipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package proto

import (
"encoding/binary"

"inet.af/netaddr"
"net/netip"
)

// IPv4 represents IPv4 address as uint32 number.
//
// Not using netaddr.IP because uint32 is 5 times faster,
// Not using netip.Addr because uint32 is 5 times faster,
// consumes 6 times less memory and better represents IPv4.
//
// Use ToIP helper for convenience.
Expand All @@ -19,14 +18,14 @@ func (v IPv4) String() string {
}

// ToIP represents IPv4 as netaddr.IP.
func (v IPv4) ToIP() netaddr.IP {
func (v IPv4) ToIP() netip.Addr {
var buf [4]byte
binary.BigEndian.PutUint32(buf[:], uint32(v))
return netaddr.IPFrom4(buf)
return netip.AddrFrom4(buf)
}

// ToIPv4 represents ip as IPv4. Panics if ip is not ipv4.
func ToIPv4(ip netaddr.IP) IPv4 {
func ToIPv4(ip netip.Addr) IPv4 {
b := ip.As4()
return IPv4(binary.BigEndian.Uint32(b[:]))
}
18 changes: 9 additions & 9 deletions proto/ipv4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ package proto

import (
"bytes"
"net/netip"
"testing"

"github.com/stretchr/testify/require"
"inet.af/netaddr"

"github.com/go-faster/ch/internal/gold"
)

func TestIPv4_String(t *testing.T) {
for _, v := range []netaddr.IP{
netaddr.MustParseIP("127.0.0.1"),
netaddr.MustParseIP("1.1.1.1"),
for _, v := range []netip.Addr{
netip.MustParseAddr("127.0.0.1"),
netip.MustParseAddr("1.1.1.1"),
} {
d := ToIPv4(v)
require.Equal(t, v.String(), d.String())
}
}

func TestColIPv4_NetAddr(t *testing.T) {
input := []netaddr.IP{
netaddr.MustParseIP("127.0.0.1"),
netaddr.MustParseIP("127.0.0.2"),
netaddr.MustParseIP("127.0.0.3"),
input := []netip.Addr{
netip.MustParseAddr("127.0.0.1"),
netip.MustParseAddr("127.0.0.2"),
netip.MustParseAddr("127.0.0.3"),
}
var d ColIPv4
for _, v := range input {
Expand All @@ -41,7 +41,7 @@ func TestColIPv4_NetAddr(t *testing.T) {

var dec ColIPv4
require.NoError(t, dec.DecodeColumn(r, len(input)))
var output []netaddr.IP
var output []netip.Addr
for _, v := range dec {
output = append(output, v.ToIP())
}
Expand Down
10 changes: 5 additions & 5 deletions proto/ipv6.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package proto

import (
"inet.af/netaddr"
"net/netip"
)

// IPv6 represents IPv6 address.
Expand All @@ -13,13 +13,13 @@ func (v IPv6) String() string {
return v.ToIP().String()
}

// ToIP represents IPv6 as netaddr.IP.
func (v IPv6) ToIP() netaddr.IP {
return netaddr.IPv6Raw(v)
// ToIP represents IPv6 as netip.IP.
func (v IPv6) ToIP() netip.Addr {
return netip.AddrFrom16(v)
}

// ToIPv6 represents ip as IPv6.
func ToIPv6(ip netaddr.IP) IPv6 { return ip.As16() }
func ToIPv6(ip netip.Addr) IPv6 { return ip.As16() }

func binIPv6(b []byte) IPv6 { return *(*[16]byte)(b) }
func binPutIPv6(b []byte, v IPv6) { copy(b, v[:]) }
20 changes: 10 additions & 10 deletions proto/ipv6_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ package proto
import (
"bytes"
"encoding/binary"
"net/netip"
"testing"

"github.com/stretchr/testify/require"
"inet.af/netaddr"

"github.com/go-faster/ch/internal/gold"
)

func TestIPv6_String(t *testing.T) {
for _, v := range []netaddr.IP{
netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:0"),
netaddr.MustParseIP("2001:4860:4860::8888"),
for _, v := range []netip.Addr{
netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0"),
netip.MustParseAddr("2001:4860:4860::8888"),
} {
d := ToIPv6(v)
require.Equal(t, v.String(), d.String())
Expand All @@ -28,18 +28,18 @@ func IPv6FromInt(v int) IPv6 {
}

func TestToIPv6(t *testing.T) {
v := netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:0")
v := netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0")
b := make([]byte, 16)
binPutIPv6(b, v.As16())
ip := binIPv6(b)
require.Equal(t, v, ip.ToIP())
}

func TestColIPv6_NetAddr(t *testing.T) {
input := []netaddr.IP{
netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:0"),
netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:1"),
netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:2"),
input := []netip.Addr{
netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0"),
netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:1"),
netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:2"),
}
var d ColIPv6
for _, v := range input {
Expand All @@ -56,7 +56,7 @@ func TestColIPv6_NetAddr(t *testing.T) {

var dec ColIPv6
require.NoError(t, dec.DecodeColumn(r, len(input)))
var output []netaddr.IP
var output []netip.Addr
for _, v := range dec {
output = append(output, v.ToIP())
}
Expand Down
8 changes: 4 additions & 4 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"fmt"
"io"
"math/rand"
"net/netip"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"inet.af/netaddr"

"github.com/go-faster/ch/proto"
)
Expand Down Expand Up @@ -345,7 +345,7 @@ func TestClient_Query(t *testing.T) {
require.NoError(t, Conn(t).Do(ctx, selectArr))
require.Equal(t, 1, data.Rows())
t.Logf("%v %s", data[0], data[0].ToIP())
require.Equal(t, netaddr.MustParseIP("127.1.1.5"), data[0].ToIP())
require.Equal(t, netip.MustParseAddr("127.1.1.5"), data[0].ToIP())
})
t.Run("SelectIPv6", func(t *testing.T) {
t.Parallel()
Expand All @@ -362,7 +362,7 @@ func TestClient_Query(t *testing.T) {
require.NoError(t, Conn(t).Do(ctx, selectArr))
require.Equal(t, 1, data.Rows())
t.Logf("%v %s", data[0], data[0].ToIP())
expected := netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:f00d")
expected := netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:f00d")
require.Equal(t, expected, data[0].ToIP())
})
t.Run("SelectDateTime", func(t *testing.T) {
Expand Down Expand Up @@ -414,7 +414,7 @@ func TestClient_Query(t *testing.T) {
},
}))
require.Equal(t, 1, data.Rows())
require.Equal(t, netaddr.MustParseIP("10.10.0.1"), data[0].ToIP())
require.Equal(t, netip.MustParseAddr("10.10.0.1"), data[0].ToIP())
})
t.Run("InsertDateTime", func(t *testing.T) {
t.Parallel()
Expand Down

0 comments on commit 4d714a8

Please sign in to comment.