diff --git a/netmap/context.go b/netmap/context.go index bce1a2c0..e0d9d5cc 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 3492fac3..68e6c9ee 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 517bc9ef..f955ee5a 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 5aaefc62..c4e2f131 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 eb69fe8b..1dc6b0d8 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 264cb0a0..4d31b464 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))