From 1f7fe6864d347886e36b1ae4fb019000879cfb9d Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 20 May 2022 15:45:12 +0300 Subject: [PATCH] [#229] netmap: make context private It isn't used anywhere outside of the package. Signed-off-by: Evgenii Stratonikov --- netmap/context.go | 14 +++++++------- netmap/filter.go | 10 +++++----- netmap/filter_test.go | 6 +++--- netmap/netmap.go | 2 +- netmap/selector.go | 6 +++--- netmap/selector_test.go | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/netmap/context.go b/netmap/context.go index bce1a2c..e0d9d5c 100644 --- a/netmap/context.go +++ b/netmap/context.go @@ -6,8 +6,8 @@ import ( "github.com/nspcc-dev/hrw" ) -// Context contains references to named filters and cached numeric values. -type Context struct { +// context contains references to named filters and cached numeric values. +type context struct { // Netmap is a netmap structure to operate on. Netmap *Netmap // Filters stores processed filters. @@ -47,10 +47,10 @@ var ( ErrUnnamedTopFilter = errors.New("netmap: all filters on top level must be named") ) -// NewContext creates new context. It contains various caches. +// newContext creates new context. It contains various caches. // In future it may create hierarchical netmap structure to work with. -func NewContext(nm *Netmap) *Context { - return &Context{ +func newContext(nm *Netmap) *context { + return &context{ Netmap: nm, Filters: make(map[string]*Filter), Selectors: make(map[string]*Selector), @@ -63,14 +63,14 @@ func NewContext(nm *Netmap) *Context { } } -func (c *Context) setPivot(pivot []byte) { +func (c *context) setPivot(pivot []byte) { if len(pivot) != 0 { c.pivot = pivot c.pivotHash = hrw.Hash(pivot) } } -func (c *Context) setCBF(cbf uint32) { +func (c *context) setCBF(cbf uint32) { if cbf == 0 { c.cbf = defaultCBF } else { diff --git a/netmap/filter.go b/netmap/filter.go index 3492fac..68e6c9e 100644 --- a/netmap/filter.go +++ b/netmap/filter.go @@ -15,12 +15,12 @@ type Filter netmap.Filter const MainFilterName = "*" // applyFilter applies named filter to b. -func (c *Context) applyFilter(name string, b *Node) bool { +func (c *context) applyFilter(name string, b *Node) bool { return name == MainFilterName || c.match(c.Filters[name], b) } // processFilters processes filters and returns error is any of them is invalid. -func (c *Context) processFilters(p *PlacementPolicy) error { +func (c *context) processFilters(p *PlacementPolicy) error { filters := p.Filters() for i := range filters { if err := c.processFilter(&filters[i], true); err != nil { @@ -31,7 +31,7 @@ func (c *Context) processFilters(p *PlacementPolicy) error { return nil } -func (c *Context) processFilter(f *Filter, top bool) error { +func (c *context) processFilter(f *Filter, top bool) error { if f == nil { return fmt.Errorf("%w: FILTER", ErrMissingField) } @@ -86,7 +86,7 @@ func (c *Context) processFilter(f *Filter, top bool) error { // match matches f against b. It returns no errors because // filter should have been parsed during context creation // and missing node properties are considered as a regular fail. -func (c *Context) match(f *Filter, b *Node) bool { +func (c *context) match(f *Filter, b *Node) bool { switch f.Operation() { case OpAND, OpOR: for _, lf := range f.InnerFilters() { @@ -106,7 +106,7 @@ func (c *Context) match(f *Filter, b *Node) bool { } } -func (c *Context) matchKeyValue(f *Filter, b *Node) bool { +func (c *context) matchKeyValue(f *Filter, b *Node) bool { switch f.Operation() { case OpEQ: return b.Attribute(f.Key()) == f.Value() diff --git a/netmap/filter_test.go b/netmap/filter_test.go index 517bc9e..f955ee5 100644 --- a/netmap/filter_test.go +++ b/netmap/filter_test.go @@ -19,7 +19,7 @@ func TestContext_ProcessFilters(t *testing.T) { } nm, err := NewNetmap(nil) require.NoError(t, err) - c := NewContext(nm) + c := newContext(nm) p := newPlacementPolicy(1, nil, nil, fs) require.NoError(t, c.processFilters(p)) require.Equal(t, 3, len(c.Filters)) @@ -72,7 +72,7 @@ func TestContext_ProcessFiltersInvalid(t *testing.T) { } for _, tc := range errTestCases { t.Run(tc.name, func(t *testing.T) { - c := NewContext(new(Netmap)) + c := newContext(new(Netmap)) p := newPlacementPolicy(1, nil, nil, []Filter{tc.filter}) err := c.processFilters(p) require.True(t, errors.Is(err, tc.err), "got: %v", err) @@ -87,7 +87,7 @@ func TestFilter_MatchSimple_InvalidOp(t *testing.T) { }} f := newFilter("Main", "Rating", "5", OpEQ) - c := NewContext(new(Netmap)) + c := newContext(new(Netmap)) p := newPlacementPolicy(1, nil, nil, []Filter{f}) require.NoError(t, c.processFilters(p)) diff --git a/netmap/netmap.go b/netmap/netmap.go index 5aaefc6..c4e2f13 100644 --- a/netmap/netmap.go +++ b/netmap/netmap.go @@ -48,7 +48,7 @@ func (m *Netmap) GetPlacementVectors(cnt ContainerNodes, pivot []byte) ([]Nodes, // Order of returned nodes corresponds to order of replicas in p. // pivot is a seed for HRW sorting. func (m *Netmap) GetContainerNodes(p *PlacementPolicy, pivot []byte) (ContainerNodes, error) { - c := NewContext(m) + c := newContext(m) c.setPivot(pivot) c.setCBF(p.ContainerBackupFactor()) diff --git a/netmap/selector.go b/netmap/selector.go index eb69fe8..1dc6b0d 100644 --- a/netmap/selector.go +++ b/netmap/selector.go @@ -13,7 +13,7 @@ import ( type Selector netmap.Selector // processSelectors processes selectors and returns error is any of them is invalid. -func (c *Context) processSelectors(p *PlacementPolicy) error { +func (c *context) processSelectors(p *PlacementPolicy) error { selectors := p.Selectors() for i, s := range selectors { if s.Filter() != MainFilterName { @@ -49,7 +49,7 @@ func GetNodesCount(_ *PlacementPolicy, s *Selector) (int, int) { // getSelection returns nodes grouped by s.attribute. // Last argument specifies if more buckets can be used to fulfill CBF. -func (c *Context) getSelection(p *PlacementPolicy, s *Selector) ([]Nodes, error) { +func (c *context) getSelection(p *PlacementPolicy, s *Selector) ([]Nodes, error) { bucketCount, nodesInBucket := GetNodesCount(p, s) buckets := c.getSelectionBase(p.SubnetID(), s) @@ -124,7 +124,7 @@ type nodeAttrPair struct { // getSelectionBase returns nodes grouped by selector attribute. // It it guaranteed that each pair will contain at least one node. -func (c *Context) getSelectionBase(subnetID *subnetid.ID, s *Selector) []nodeAttrPair { +func (c *context) getSelectionBase(subnetID *subnetid.ID, s *Selector) []nodeAttrPair { f := c.Filters[s.Filter()] isMain := s.Filter() == MainFilterName result := []nodeAttrPair{} diff --git a/netmap/selector_test.go b/netmap/selector_test.go index 264cb0a..4d31b46 100644 --- a/netmap/selector_test.go +++ b/netmap/selector_test.go @@ -214,7 +214,7 @@ func TestPlacementPolicy_ProcessSelectors(t *testing.T) { nm, err := NewNetmap(NodesFromInfo(nodes)) require.NoError(t, err) - c := NewContext(nm) + c := newContext(nm) c.setCBF(p.ContainerBackupFactor()) require.NoError(t, c.processFilters(p)) require.NoError(t, c.processSelectors(p))