[#229] netmap: make context private
It isn't used anywhere outside of the package. Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
60ef026923
commit
1f7fe6864d
6 changed files with 20 additions and 20 deletions
|
@ -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 {
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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))
|
||||
|
||||
|
|
|
@ -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())
|
||||
|
||||
|
|
|
@ -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{}
|
||||
|
|
|
@ -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))
|
||||
|
|
Loading…
Reference in a new issue