Skip to content
This repository was archived by the owner on Mar 14, 2023. It is now read-only.

Prepare proto-definition to marshal/unmarshal Bucket's #27

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,14 @@ up: down
# Regenerate proto files:
protoc:
# ^ protoc: LIBS:=$(LIBS),Mpath/to/another/lib
@go mod tidy -v
@go mod vendor
# Install specific version for gogo-proto
@go list -f '{{.Path}}/...@{{.Version}}' -m github.com/gogo/protobuf | xargs go get -v
# Install specific version for protobuf lib
@go list -f '{{.Path}}/...@{{.Version}}' -m github.com/golang/protobuf | xargs go get -v
@find . -type f -name '*.proto' -not -path './vendor/*' \
-exec protoc \
--proto_path=$(GOPATH)/src:. \
--proto_path=$(GOPATH)/src:./vendor:. \
--gofast_out=plugins=$(LIBS):. '{}' \;

12 changes: 6 additions & 6 deletions aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ const (
)

func initTestBucket(t *testing.T, b *Bucket) {
require.Nil(t, b.AddBucket("/opt:first", Nodes{{0, 1, 2}, {2, 3, 2}}))
require.Nil(t, b.AddBucket("/opt:second/sub:1", Nodes{{1, 2, 3}, {10, 6, 1}}))
require.Nil(t, b.AddBucket("/opt:first", Nodes{{N: 0, C: 1, P: 2}, {N: 2, C: 3, P: 2}}))
require.Nil(t, b.AddBucket("/opt:second/sub:1", Nodes{{N: 1, C: 2, P: 3}, {N: 10, C: 6, P: 1}}))

b.fillNodes()
}
Expand All @@ -37,10 +37,10 @@ func TestNewWeightFunc(t *testing.T) {
copy(nodes, b.nodes)

expected := Nodes{
{10, 6, 1},
{2, 3, 2},
{1, 2, 3},
{0, 1, 2},
{N: 10, C: 6, P: 1},
{N: 2, C: 3, P: 2},
{N: 1, C: 2, P: 3},
{N: 0, C: 1, P: 2},
}

sort.Slice(nodes, func(i, j int) bool { return wf(nodes[i]) > wf(nodes[j]) })
Expand Down
6 changes: 3 additions & 3 deletions cmd/repl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ func read(b *netmap.Bucket, name string) error {
if err != nil {
return err
}
return b.UnmarshalBinary(data)
return b.Unmarshal(data)
}

func write(b *netmap.Bucket, name string) error {
data, err := b.MarshalBinary()
data, err := b.Marshal()
if err != nil {
return err
}
Expand All @@ -161,7 +161,7 @@ func getSelection(c *ishell.Context) {
return
}
}
c.Println(nil)
c.Println()
}

func clearSelection(c *ishell.Context) {
Expand Down
Binary file modified examples/demo
Binary file not shown.
Binary file modified examples/map2
Binary file not shown.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ require (
github.com/fatih/color v1.7.0 // indirect
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
github.com/gogo/protobuf v1.2.0
github.com/golang/protobuf v1.2.0
github.com/golang/protobuf v1.3.1
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/nspcc-dev/hrw v1.0.8
github.com/pkg/errors v0.8.0
github.com/stretchr/testify v1.3.0
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb // indirect
gopkg.in/abiosoft/ishell.v2 v2.0.0
)
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 h1:BMXYYRWT
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:rZfgFAXFS/z/lEd6LJmf9HVZ1LkgYiHx5pHhV5DR16M=
github.com/gogo/protobuf v1.2.0 h1:xU6/SpYbvkNYiptHJYEDRseDLvYE7wSqhYYNy0QSUzI=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/mattn/go-colorable v0.0.9 h1:UVL0vNpWh04HeJXV0KLcaT7r06gOH2l4OW6ddYRUIY4=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.4 h1:bnP0vzxcAdeI1zdubAl5PjU6zsERjGZb7raWodagDYs=
Expand All @@ -34,8 +34,6 @@ github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
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=
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb h1:pf3XwC90UUdNPYWZdFjhGBE7DUFuK3Ct1zWmZ65QN30=
golang.org/x/sys v0.0.0-20181228144115-9a3f9b0469bb/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/abiosoft/ishell.v2 v2.0.0 h1:/J5yh3nWYSSGFjALcitTI9CLE0Tu27vBYHX0srotqOc=
Expand Down
118 changes: 21 additions & 97 deletions policy.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package netmap

import (
"bytes"
"encoding/binary"
"io"
"sort"
"strconv"
"strings"

rw "github.com/gogo/protobuf/io"
"github.com/nspcc-dev/hrw"
"github.com/pkg/errors"
)
Expand All @@ -27,21 +28,6 @@ type (
NodeCount int
}

// Bucket represents netmap as graph.
Bucket struct {
Key string
Value string
nodes Nodes
children []Bucket
}

// Node type represents single graph leaf with index N, capacity C and price P.
Node struct {
N uint32
C uint64
P uint64
}

// Nodes represents slice of graph leafs.
Nodes []Node

Expand Down Expand Up @@ -81,6 +67,13 @@ func (n *Node) Read(r io.Reader) error {
}
return nil
}
func (n Nodes) String() string {
results := make([]string, 0, len(n))
for i := range n {
results = append(results, strconv.FormatUint(uint64(n[i].N), 10))
}
return "[" + strings.Join(results, ", ") + "]"
}

func (n Nodes) Len() int { return len(n) }
func (n Nodes) Less(i, j int) bool { return n[i].N < n[j].N }
Expand Down Expand Up @@ -270,7 +263,9 @@ func (b Bucket) filterSubtree(filter FilterFunc) *Bucket {
}
}
if len(root.nodes) > 0 {
sort.Sort(root.nodes)
nodes := Nodes(root.nodes)
sort.Sort(nodes)
root.nodes = nodes
return &root
}
return nil
Expand Down Expand Up @@ -317,7 +312,9 @@ func (b Bucket) getMaxSelectionC(ss []Select, filter FilterFunc, cut bool) (*Buc
}

if (!cut && count != 0) || count >= ss[0].Count {
sort.Sort(root.nodes)
nodes := Nodes(root.nodes)
sort.Sort(nodes)
root.nodes = nodes
return &root, count

}
Expand Down Expand Up @@ -454,7 +451,9 @@ loop:
}
b.children = append(b.children, c1)
}
sort.Sort(b.nodes)
nodes := Nodes(b.nodes)
sort.Sort(nodes)
b.nodes = nodes
}

// UpdateIndices is auxiliary function used to update
Expand Down Expand Up @@ -496,88 +495,13 @@ func getChildrenByKey(b Bucket, s Select) []Bucket {
// Writes Bucket with this byte structure
// [lnName][Name][lnNodes][Node1]...[NodeN][lnSubprops][sub1]...[subN]
func (b Bucket) Write(w io.Writer) error {
var err error

// writing name
if err = binary.Write(w, binary.BigEndian, int32(len(b.Key)+len(b.Value)+1)); err != nil {
return err
}
if err = binary.Write(w, binary.BigEndian, []byte(b.Name())); err != nil {
return err
}

// writing nodes
if err = b.nodes.Write(w); err != nil {
return err
}

if err = binary.Write(w, binary.BigEndian, int32(len(b.children))); err != nil {
return err
}
for i := range b.children {
if err = b.children[i].Write(w); err != nil {
return err
}
}

return nil
return rw.NewDelimitedWriter(w).WriteMsg(&b)
}

// Read reads Bucket in serialized form:
// [lnName][Name][lnNodes][Node1]...[NodeN][lnSubprops][sub1]...[subN]
func (b *Bucket) Read(r io.Reader) error {
var ln int32
var err error
if err = binary.Read(r, binary.BigEndian, &ln); err != nil {
return err
}
name := make([]byte, ln)
lnE, err := r.Read(name)
if err != nil {
return err
}
if int32(lnE) != ln {
return errors.New("unmarshaller error: cannot read name")
}

b.Key, b.Value, _ = splitKV(string(name))

// reading node list
if err = b.nodes.Read(r); err != nil {
return err
}

if err = binary.Read(r, binary.BigEndian, &ln); err != nil {
return err
}
if ln > 0 {
b.children = make([]Bucket, ln)
for i := range b.children {
if err = b.children[i].Read(r); err != nil {
return err
}
}
}

return nil
}

// MarshalBinary implements the encoding.BinaryMarshaler interface.
func (b Bucket) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
if err := b.Write(buf); err != nil {
return nil, err
}
return buf.Bytes(), nil
}

// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
func (b *Bucket) UnmarshalBinary(data []byte) (err error) {
buf := bytes.NewBuffer(data)
if err = b.Read(buf); err == io.EOF {
return nil
}
return
return rw.NewDelimitedReader(r, 1024*1024).ReadMsg(b)
}

// Name return b's short string identifier.
Expand Down Expand Up @@ -613,7 +537,7 @@ func (b Bucket) Children() []Bucket {

// AddNode adds node n with options opts to b.
func (b *Bucket) AddNode(n uint32, opts ...string) error {
return b.addNode(Node{n, 0, 0}, opts...)
return b.addNode(Node{N: n}, opts...)
}

// AddStrawNode adds straw node n with options opts to b.
Expand Down
Loading