From 06ef70b16825326dc3fd258bda08f0c5587aa6a1 Mon Sep 17 00:00:00 2001 From: Roman Atachiants Date: Tue, 26 May 2020 00:16:29 +0800 Subject: [PATCH] Updated dependencies (#318) --- go.mod | 17 ++++++------- go.sum | 36 +++++++++++++++------------- internal/event/crdt/durable.go | 20 +++++++--------- internal/event/crdt/set.go | 31 ++---------------------- internal/event/crdt/volatile.go | 14 ++++------- internal/event/crdt/volatile_test.go | 1 + internal/security/channel.go | 10 +++----- internal/security/hash/murmur.go | 17 +++---------- internal/security/key.go | 3 ++- 9 files changed, 54 insertions(+), 95 deletions(-) diff --git a/go.mod b/go.mod index 294a9cfb..e6df6ece 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.14 require ( github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect - github.com/aws/aws-sdk-go v1.30.24 // indirect + github.com/aws/aws-sdk-go v1.31.4 // indirect github.com/axiomhq/hyperloglog v0.0.0-20191112132149-a4c4c47bc57f github.com/coocood/freecache v1.1.0 github.com/dgraph-io/badger v1.6.1 @@ -12,25 +12,26 @@ require ( github.com/emitter-io/address v1.0.0 github.com/emitter-io/config v1.0.0 github.com/emitter-io/stats v1.0.3 - github.com/golang/protobuf v1.4.1 // indirect + github.com/golang/protobuf v1.4.2 // indirect github.com/golang/snappy v0.0.1 github.com/gorilla/websocket v1.4.2 github.com/jawher/mow.cli v1.1.0 - github.com/kelindar/binary v1.0.8 + github.com/kelindar/binary v1.0.9 github.com/kelindar/rate v1.0.0 github.com/kelindar/tcp v1.0.0 - github.com/klauspost/compress v1.10.5 // indirect + github.com/klauspost/compress v1.10.6 // indirect github.com/prometheus/client_golang v1.6.0 + github.com/prometheus/common v0.10.0 // indirect github.com/stretchr/testify v1.5.1 github.com/tidwall/buntdb v1.1.2 github.com/tidwall/gjson v1.6.0 // indirect github.com/tidwall/pretty v1.0.1 // indirect github.com/valyala/fasthttp v1.12.0 github.com/weaveworks/mesh v0.0.0-20191105120815-58dbcc3e8e63 - golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 - golang.org/x/net v0.0.0-20200506145744-7e3656a0809f // indirect - golang.org/x/sys v0.0.0-20200508214444-3aab700007d7 // indirect + golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 + golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect + golang.org/x/sys v0.0.0-20200523222454-059865788121 // indirect golang.org/x/text v0.3.2 // indirect gopkg.in/alexcesaro/statsd.v2 v2.0.0 - gopkg.in/yaml.v2 v2.2.8 // indirect + gopkg.in/yaml.v2 v2.3.0 // indirect ) diff --git a/go.sum b/go.sum index 852ea842..f7253ceb 100644 --- a/go.sum +++ b/go.sum @@ -10,8 +10,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/aws/aws-sdk-go v0.0.0-20181019205654-b2427922671b/go.mod h1:E3/ieXAlvM0XWO57iftYVDLLvQ824smPP3ATZkfNZeM= -github.com/aws/aws-sdk-go v1.30.24 h1:y3JPD51VuEmVqN3BEDVm4amGpDma2cKJcDPuAU1OR58= -github.com/aws/aws-sdk-go v1.30.24/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= +github.com/aws/aws-sdk-go v1.31.4 h1:YZ0uEYIWeanGuAomElHmRWMAbXVqrQixxgf2vtIjO6M= +github.com/aws/aws-sdk-go v1.31.4/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= github.com/axiomhq/hyperloglog v0.0.0-20191112132149-a4c4c47bc57f h1:y06x6vGnFYfXUoVMbrcP1Uzpj4JG01eB5vRps9G8agM= github.com/axiomhq/hyperloglog v0.0.0-20191112132149-a4c4c47bc57f/go.mod h1:2stgcRjl6QmW+gU2h5E7BQXg4HU0gzxKWDuT5HviN9s= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 h1:xJ4a3vCFaGF/jqvzLMYoU8P317H5OQ+Via4RmuPwCS0= @@ -72,8 +72,8 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w= github.com/golang/protobuf v1.4.0 h1:oOuy+ugB+P/kBdUnG5QaMXSIyJ1q38wWSojYCb3z5VQ= github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= -github.com/golang/protobuf v1.4.1 h1:ZFgWrT+bLgsYPirOnRfKLYJLvssAegOj/hgyMFdJZe0= -github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QDs8UjoX8= +github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= +github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY= @@ -110,6 +110,8 @@ github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/u github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= github.com/kelindar/binary v1.0.8 h1:YwahocVzCFY+1+WF5JUqwrYZpe4vcNo7JhWKZA/uBQs= github.com/kelindar/binary v1.0.8/go.mod h1:4zDwr5pQvY3i4xrRd1kC7pcuWvSU/Jbh/v2D0tZUPfE= +github.com/kelindar/binary v1.0.9 h1:Rngq8Kd8BTdqJlmH6fvIlgKypMLcJGykynsyigjbMG8= +github.com/kelindar/binary v1.0.9/go.mod h1:4zDwr5pQvY3i4xrRd1kC7pcuWvSU/Jbh/v2D0tZUPfE= github.com/kelindar/process v0.0.0-20170730150328-69a29e249ec3 h1:6If+E1dikQbdT7DlhZqLplfGkEt6dSoz7+MK+TFC7+U= github.com/kelindar/process v0.0.0-20170730150328-69a29e249ec3/go.mod h1:+lTCLnZFXOkqwD8sLPl6u4erAc0cP8wFegQHfipz7KE= github.com/kelindar/rate v1.0.0 h1:JNZdufLjtDzr/E/rCtWkqo2OVU4yJSScZngJ8LuZ7kU= @@ -117,8 +119,8 @@ github.com/kelindar/rate v1.0.0/go.mod h1:AjT4G+hTItNwt30lucEGZIz8y7Uk5zPho6vurI github.com/kelindar/tcp v1.0.0 h1:585JE7qmc6S5EQPYLAkRqfGo4PqDxalke98AXjxPmrE= github.com/kelindar/tcp v1.0.0/go.mod h1:JB5hj1cshLU60XrLij2BBxW3JQ4hOye8vqbyvuKb52k= github.com/klauspost/compress v1.10.4/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= -github.com/klauspost/compress v1.10.5 h1:7q6vHIqubShURwQz8cQK6yIe/xC3IF0Vm7TGfqjewrc= -github.com/klauspost/compress v1.10.5/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= +github.com/klauspost/compress v1.10.6 h1:SP6zavvTG3YjOosWePXFDlExpKIWMTO4SE/Y8MZB2vI= +github.com/klauspost/compress v1.10.6/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -162,6 +164,8 @@ github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6T github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.9.1 h1:KOMtN28tlbam3/7ZKEYKHhKoJZYYj3gMH4uc62x7X7U= github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= +github.com/prometheus/common v0.10.0 h1:RyRA7RzGXQZiW+tGMr7sxa85G1z0yOpM1qq5c8lNawc= +github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.0.11 h1:DhHlBtkHWPYi8O2y31JkK0TF+DGM+51OopZjH/Ia5qI= @@ -226,8 +230,8 @@ golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79 h1:IaQbIIB2X/Mp/DKctl6ROxz1KyMlKp4uyvL6+kQ7C88= -golang.org/x/crypto v0.0.0-20200429183012-4b2356b1ed79/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 h1:cg5LA/zNPRzIXIWSCxQW10Rvpy94aQh3LT/ShoCpkHw= +golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ= @@ -238,8 +242,8 @@ golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLL golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200506145744-7e3656a0809f h1:QBjCr1Fz5kw158VqdE9JfI9cJnl/ymnJWAdMuinqL7Y= -golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 h1:eDrdRpKgkcCqKZQwyZRyeFZgfqt37SL7Kv3tok06cKE= +golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ= golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -257,8 +261,8 @@ golang.org/x/sys v0.0.0-20190626221950-04f50cda93cb/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200508214444-3aab700007d7 h1:EEDKzZsac90dOwDpumCaAWrqnvbzVPdlUH9lCZ1yExw= -golang.org/x/sys v0.0.0-20200508214444-3aab700007d7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o= +golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= @@ -271,8 +275,8 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0 h1:qdOKuR/EIArgaWNjetjgTzgVTAZ+S/WXVrq9HW9zimw= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= -google.golang.org/protobuf v1.22.0 h1:cJv5/xdbk1NnMPR1VP9+HU6gupuG9MLBoH1r6RHZ2MY= -google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= +google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= +google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/alexcesaro/statsd.v2 v2.0.0 h1:FXkZSCZIH17vLCO5sO2UucTHsH9pc+17F6pl3JVCwMc= gopkg.in/alexcesaro/statsd.v2 v2.0.0/go.mod h1:i0ubccKGzBVNBpdGV5MocxyA/XlLUJzA7SLonnE4drU= @@ -291,5 +295,5 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/internal/event/crdt/durable.go b/internal/event/crdt/durable.go index 35de98c5..35f86142 100644 --- a/internal/event/crdt/durable.go +++ b/internal/event/crdt/durable.go @@ -86,15 +86,15 @@ func (s *Durable) store(tx *buntdb.Tx, key string, t Time) { // Fetch fetches the item either from transaction or cache. func (s *Durable) fetch(item string) Time { - cacheKey := stringToBinary(item) + cacheKey := binary.ToBytes(item) if v, err := s.cache.Get(cacheKey); err == nil { - return decodeTime(binaryToString(&v)) + return decodeTime(binary.ToString(&v)) } tx, _ := s.db.Begin(false) defer tx.Rollback() if t, err := tx.Get(item); err == nil { - s.cache.Set(cacheKey, stringToBinary(t), 60) + s.cache.Set(cacheKey, binary.ToBytes(t), 60) return decodeTime(t) } return Time{} @@ -169,7 +169,7 @@ func (s *Durable) Merge(other Set) { func (s *Durable) Range(prefix []byte, f func(string, Time) bool) { s.db.View(func(tx *buntdb.Tx) error { return tx.Ascend("", func(k, v string) bool { - if !bytes.HasPrefix(stringToBinary(k), prefix) { + if !bytes.HasPrefix(binary.ToBytes(k), prefix) { return true } @@ -250,10 +250,8 @@ func (c *durableCodec) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error) // Write the entire sample e.WriteUvarint(uint64(len(entries))) for _, v := range entries { - e.WriteUvarint(uint64(len(v.Key))) - e.Write(stringToBinary(v.Key)) - e.WriteUvarint(uint64(len(v.Val))) - e.Write(stringToBinary(v.Val)) + e.WriteString(v.Key) + e.WriteString(v.Val) } return } @@ -268,17 +266,17 @@ func (c *durableCodec) DecodeTo(d *binary.Decoder, rv reflect.Value) (err error) out.db.Update(func(tx *buntdb.Tx) error { for i := 0; i < int(size); i++ { - k, err := readBytes(d) + k, err := d.ReadSlice() if err != nil { return nil } - v, err := readBytes(d) + v, err := d.ReadSlice() if err != nil { return nil } - tx.Set(binaryToString(&k), binaryToString(&v), nil) + tx.Set(binary.ToString(&k), binary.ToString(&v), nil) } return nil }) diff --git a/internal/event/crdt/set.go b/internal/event/crdt/set.go index 3bd4b979..59f01ffe 100644 --- a/internal/event/crdt/set.go +++ b/internal/event/crdt/set.go @@ -16,9 +16,7 @@ package crdt import ( bin "encoding/binary" - "reflect" "time" - "unsafe" "github.com/kelindar/binary" ) @@ -71,12 +69,12 @@ func (t Time) Encode() string { n1 := bin.PutVarint(b, t.AddTime) n2 := bin.PutVarint(b[n1:], t.DelTime) b = b[:n1+n2] - return binaryToString(&b) + return binary.ToString(&b) } // DecodeTime decodes the time from a string func decodeTime(t string) (v Time) { - b, n := stringToBinary(t), 0 + b, n := binary.ToBytes(t), 0 v.AddTime, n = bin.Varint(b) v.DelTime, _ = bin.Varint(b[n:]) return @@ -84,31 +82,6 @@ func decodeTime(t string) (v Time) { // ------------------------------------------------------------------------------------ -func readBytes(d *binary.Decoder) (buffer []byte, err error) { - var l uint64 - if l, err = d.ReadUvarint(); err == nil && l > 0 { - buffer, err = d.Slice(int(l)) - } - return -} - -func binaryToString(b *[]byte) string { - return *(*string)(unsafe.Pointer(b)) -} - -func stringToBinary(v string) (b []byte) { - strHeader := (*reflect.StringHeader)(unsafe.Pointer(&v)) - byteHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - byteHeader.Data = strHeader.Data - - l := len(v) - byteHeader.Len = l - byteHeader.Cap = l - return -} - -// ------------------------------------------------------------------------------------ - // The clock for unit-testing type clock func() int64 diff --git a/internal/event/crdt/volatile.go b/internal/event/crdt/volatile.go index c05b6749..6cd1e1c3 100644 --- a/internal/event/crdt/volatile.go +++ b/internal/event/crdt/volatile.go @@ -149,12 +149,8 @@ func (c *codecVolatile) EncodeTo(e *binary.Encoder, rv reflect.Value) (err error e.WriteUvarint(uint64(len(s.data))) for k, t := range s.data { - v := t.Encode() - - e.WriteUvarint(uint64(len(k))) - e.Write(stringToBinary(k)) - e.WriteUvarint(uint64(len(v))) - e.Write(stringToBinary(v)) + e.WriteString(k) + e.WriteString(t.Encode()) } return } @@ -168,17 +164,17 @@ func (c *codecVolatile) DecodeTo(d *binary.Decoder, rv reflect.Value) (err error } for i := 0; i < int(size); i++ { - k, err := readBytes(d) + k, err := d.ReadSlice() if err != nil { return nil } - v, err := readBytes(d) + v, err := d.ReadSlice() if err != nil { return nil } - out.data[binaryToString(&k)] = decodeTime(binaryToString(&v)) + out.data[binary.ToString(&k)] = decodeTime(binary.ToString(&v)) } rv.Set(reflect.ValueOf(*out)) diff --git a/internal/event/crdt/volatile_test.go b/internal/event/crdt/volatile_test.go index 4a9c5f7f..60f65761 100644 --- a/internal/event/crdt/volatile_test.go +++ b/internal/event/crdt/volatile_test.go @@ -199,6 +199,7 @@ func TestRange(t *testing.T) { return true }) assert.Equal(t, 5, count) + assert.Equal(t, 6, state.Count()) } // ------------------------------------------------------------------------------------ diff --git a/internal/security/channel.go b/internal/security/channel.go index 01a71a0d..f97d6d15 100644 --- a/internal/security/channel.go +++ b/internal/security/channel.go @@ -18,10 +18,10 @@ import ( "fmt" "strconv" "time" - "unsafe" "github.com/emitter-io/emitter/internal/config" "github.com/emitter-io/emitter/internal/security/hash" + "github.com/kelindar/binary" ) // Channel types @@ -302,8 +302,8 @@ func (c *Channel) parseOptions(text []byte) (i int, ok bool) { // Set the option c.Options = append(c.Options, ChannelOption{ - Key: binaryToString(&key), - Value: binaryToString(&val), + Key: binary.ToString(&key), + Value: binary.ToString(&val), }) val = val[0:0] @@ -312,7 +312,3 @@ func (c *Channel) parseOptions(text []byte) (i int, ok bool) { return i, true } - -func binaryToString(b *[]byte) string { - return *(*string)(unsafe.Pointer(b)) -} diff --git a/internal/security/hash/murmur.go b/internal/security/hash/murmur.go index 44168a34..94be9bb3 100644 --- a/internal/security/hash/murmur.go +++ b/internal/security/hash/murmur.go @@ -16,8 +16,8 @@ package hash import ( "math/bits" - "reflect" - "unsafe" + + "github.com/kelindar/binary" ) const ( @@ -27,7 +27,7 @@ const ( // OfString returns a murmur32 hash for the string func OfString(value string) uint32 { - return Of(stringToBinary(value)) + return Of(binary.ToBytes(value)) } // Of returns a murmur32 hash for the data slice. @@ -74,14 +74,3 @@ func Of(data []byte) uint32 { return (h1 << 24) | (((h1 >> 8) << 16) & 0xFF0000) | (((h1 >> 16) << 8) & 0xFF00) | (h1 >> 24) } - -func stringToBinary(v string) (b []byte) { - strHeader := (*reflect.StringHeader)(unsafe.Pointer(&v)) - byteHeader := (*reflect.SliceHeader)(unsafe.Pointer(&b)) - byteHeader.Data = strHeader.Data - - l := len(v) - byteHeader.Len = l - byteHeader.Cap = l - return -} diff --git a/internal/security/key.go b/internal/security/key.go index 96f1234b..8e1ff60d 100644 --- a/internal/security/key.go +++ b/internal/security/key.go @@ -21,6 +21,7 @@ import ( "time" "github.com/emitter-io/emitter/internal/security/hash" + "github.com/kelindar/binary" ) // Gets the beginning of time for the timestamp, which is 2010/1/1 00:00:00 @@ -142,7 +143,7 @@ func (k Key) ValidateChannel(ch *Channel) bool { } // Split by `/` - channel := binaryToString(&topic) + channel := binary.ToString(&topic) parts := strings.Split(channel, "/") wc := parts[len(parts)-1] == "#" if wc {