[#168] policy: Adopt replacement of pointer slices with struct slices
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
cdcbaa1677
commit
cef9872b39
4 changed files with 40 additions and 42 deletions
|
@ -40,7 +40,7 @@ func Encode(p *netmap.PlacementPolicy) []string {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeReplicas(replicas []*netmap.Replica, dst *[]string) {
|
func encodeReplicas(replicas []netmap.Replica, dst *[]string) {
|
||||||
builder := new(strings.Builder)
|
builder := new(strings.Builder)
|
||||||
|
|
||||||
for _, replica := range replicas {
|
for _, replica := range replicas {
|
||||||
|
@ -57,7 +57,7 @@ func encodeReplicas(replicas []*netmap.Replica, dst *[]string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeSelectors(selectors []*netmap.Selector, dst *[]string) {
|
func encodeSelectors(selectors []netmap.Selector, dst *[]string) {
|
||||||
builder := new(strings.Builder)
|
builder := new(strings.Builder)
|
||||||
|
|
||||||
for _, selector := range selectors {
|
for _, selector := range selectors {
|
||||||
|
@ -94,13 +94,13 @@ func encodeSelectors(selectors []*netmap.Selector, dst *[]string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func encodeFilters(filters []*netmap.Filter, dst *[]string) {
|
func encodeFilters(filters []netmap.Filter, dst *[]string) {
|
||||||
builder := new(strings.Builder)
|
builder := new(strings.Builder)
|
||||||
|
|
||||||
for _, filter := range filters {
|
for _, filter := range filters {
|
||||||
builder.WriteString("FILTER ")
|
builder.WriteString("FILTER ")
|
||||||
|
|
||||||
builder.WriteString(encodeFilter(filter))
|
builder.WriteString(encodeFilter(&filter))
|
||||||
|
|
||||||
*dst = append(*dst, builder.String())
|
*dst = append(*dst, builder.String())
|
||||||
builder.Reset()
|
builder.Reset()
|
||||||
|
@ -129,7 +129,7 @@ func encodeFilter(filter *netmap.Filter) string {
|
||||||
builder.WriteString(" ")
|
builder.WriteString(" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.WriteString(encodeFilter(subfilter))
|
builder.WriteString(encodeFilter(&subfilter))
|
||||||
}
|
}
|
||||||
|
|
||||||
if n := filter.Name(); n != "" && !unspecified {
|
if n := filter.Name(); n != "" && !unspecified {
|
||||||
|
|
|
@ -41,15 +41,15 @@ func ToJSON(np *netmap.PlacementPolicy) ([]byte, error) {
|
||||||
p.CBF = np.ContainerBackupFactor()
|
p.CBF = np.ContainerBackupFactor()
|
||||||
p.Filters = make([]filter, len(np.Filters()))
|
p.Filters = make([]filter, len(np.Filters()))
|
||||||
for i, f := range np.Filters() {
|
for i, f := range np.Filters() {
|
||||||
p.Filters[i].fromNetmap(f)
|
p.Filters[i].fromNetmap(&f)
|
||||||
}
|
}
|
||||||
p.Selectors = make([]selector, len(np.Selectors()))
|
p.Selectors = make([]selector, len(np.Selectors()))
|
||||||
for i, s := range np.Selectors() {
|
for i, s := range np.Selectors() {
|
||||||
p.Selectors[i].fromNetmap(s)
|
p.Selectors[i].fromNetmap(&s)
|
||||||
}
|
}
|
||||||
p.Replicas = make([]replica, len(np.Replicas()))
|
p.Replicas = make([]replica, len(np.Replicas()))
|
||||||
for i, r := range np.Replicas() {
|
for i, r := range np.Replicas() {
|
||||||
p.Replicas[i].fromNetmap(r)
|
p.Replicas[i].fromNetmap(&r)
|
||||||
}
|
}
|
||||||
return json.Marshal(p)
|
return json.Marshal(p)
|
||||||
}
|
}
|
||||||
|
@ -61,32 +61,32 @@ func FromJSON(data []byte) (*netmap.PlacementPolicy, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
rs := make([]*netmap.Replica, len(p.Replicas))
|
rs := make([]netmap.Replica, len(p.Replicas))
|
||||||
for i := range p.Replicas {
|
for i := range p.Replicas {
|
||||||
rs[i] = p.Replicas[i].toNetmap()
|
rs[i] = *p.Replicas[i].toNetmap()
|
||||||
}
|
}
|
||||||
|
|
||||||
var fs []*netmap.Filter
|
var fs []netmap.Filter
|
||||||
if len(p.Filters) != 0 {
|
if len(p.Filters) != 0 {
|
||||||
fs = make([]*netmap.Filter, len(p.Filters))
|
fs = make([]netmap.Filter, len(p.Filters))
|
||||||
for i := range p.Filters {
|
for i := range p.Filters {
|
||||||
f, err := p.Filters[i].toNetmap()
|
f, err := p.Filters[i].toNetmap()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
fs[i] = f
|
fs[i] = *f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var ss []*netmap.Selector
|
var ss []netmap.Selector
|
||||||
if len(p.Selectors) != 0 {
|
if len(p.Selectors) != 0 {
|
||||||
ss = make([]*netmap.Selector, len(p.Selectors))
|
ss = make([]netmap.Selector, len(p.Selectors))
|
||||||
for i := range p.Selectors {
|
for i := range p.Selectors {
|
||||||
s, err := p.Selectors[i].toNetmap()
|
s, err := p.Selectors[i].toNetmap()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
ss[i] = s
|
ss[i] = *s
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,15 +134,16 @@ func (f *filter) toNetmap() (*netmap.Filter, error) {
|
||||||
return nil, fmt.Errorf("%w: '%s'", ErrUnknownOp, f.Op)
|
return nil, fmt.Errorf("%w: '%s'", ErrUnknownOp, f.Op)
|
||||||
}
|
}
|
||||||
|
|
||||||
var fs []*netmap.Filter
|
var fs []netmap.Filter
|
||||||
if len(f.Filters) != 0 {
|
if len(f.Filters) != 0 {
|
||||||
fs = make([]*netmap.Filter, len(f.Filters))
|
fs = make([]netmap.Filter, len(f.Filters))
|
||||||
for i := range f.Filters {
|
for i := range f.Filters {
|
||||||
var err error
|
var err error
|
||||||
fs[i], err = f.Filters[i].toNetmap()
|
fsp, err := f.Filters[i].toNetmap()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
fs[i] = *fsp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,7 +183,7 @@ func (f *filter) fromNetmap(nf *netmap.Filter) {
|
||||||
if nf.InnerFilters() != nil {
|
if nf.InnerFilters() != nil {
|
||||||
f.Filters = make([]filter, len(nf.InnerFilters()))
|
f.Filters = make([]filter, len(nf.InnerFilters()))
|
||||||
for i, sf := range nf.InnerFilters() {
|
for i, sf := range nf.InnerFilters() {
|
||||||
f.Filters[i].fromNetmap(sf)
|
f.Filters[i].fromNetmap(&sf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,14 +81,14 @@ func (p *policyVisitor) VisitPolicy(ctx *parser.PolicyContext) interface{} {
|
||||||
pl := new(netmap.PlacementPolicy)
|
pl := new(netmap.PlacementPolicy)
|
||||||
|
|
||||||
repStmts := ctx.AllRepStmt()
|
repStmts := ctx.AllRepStmt()
|
||||||
rs := make([]*netmap.Replica, 0, len(repStmts))
|
rs := make([]netmap.Replica, 0, len(repStmts))
|
||||||
for _, r := range repStmts {
|
for _, r := range repStmts {
|
||||||
res, ok := r.Accept(p).(*netmap.Replica)
|
res, ok := r.Accept(p).(*netmap.Replica)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rs = append(rs, res)
|
rs = append(rs, *res)
|
||||||
}
|
}
|
||||||
pl.SetReplicas(rs...)
|
pl.SetReplicas(rs...)
|
||||||
|
|
||||||
|
@ -101,21 +101,21 @@ func (p *policyVisitor) VisitPolicy(ctx *parser.PolicyContext) interface{} {
|
||||||
}
|
}
|
||||||
|
|
||||||
selStmts := ctx.AllSelectStmt()
|
selStmts := ctx.AllSelectStmt()
|
||||||
ss := make([]*netmap.Selector, 0, len(selStmts))
|
ss := make([]netmap.Selector, 0, len(selStmts))
|
||||||
for _, s := range selStmts {
|
for _, s := range selStmts {
|
||||||
res, ok := s.Accept(p).(*netmap.Selector)
|
res, ok := s.Accept(p).(*netmap.Selector)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
ss = append(ss, res)
|
ss = append(ss, *res)
|
||||||
}
|
}
|
||||||
pl.SetSelectors(ss...)
|
pl.SetSelectors(ss...)
|
||||||
|
|
||||||
filtStmts := ctx.AllFilterStmt()
|
filtStmts := ctx.AllFilterStmt()
|
||||||
fs := make([]*netmap.Filter, 0, len(filtStmts))
|
fs := make([]netmap.Filter, 0, len(filtStmts))
|
||||||
for _, f := range filtStmts {
|
for _, f := range filtStmts {
|
||||||
fs = append(fs, f.Accept(p).(*netmap.Filter))
|
fs = append(fs, *f.Accept(p).(*netmap.Filter))
|
||||||
}
|
}
|
||||||
pl.SetFilters(fs...)
|
pl.SetFilters(fs...)
|
||||||
|
|
||||||
|
@ -194,8 +194,8 @@ func (p *policyVisitor) VisitFilterExpr(ctx *parser.FilterExprContext) interface
|
||||||
op := operationFromString(ctx.GetOp().GetText())
|
op := operationFromString(ctx.GetOp().GetText())
|
||||||
f.SetOperation(op)
|
f.SetOperation(op)
|
||||||
|
|
||||||
f1 := ctx.GetF1().Accept(p).(*netmap.Filter)
|
f1 := *ctx.GetF1().Accept(p).(*netmap.Filter)
|
||||||
f2 := ctx.GetF2().Accept(p).(*netmap.Filter)
|
f2 := *ctx.GetF2().Accept(p).(*netmap.Filter)
|
||||||
|
|
||||||
// Consider f1=(.. AND ..) AND f2. This can be merged because our AND operation
|
// Consider f1=(.. AND ..) AND f2. This can be merged because our AND operation
|
||||||
// is of arbitrary arity. ANTLR generates left-associative parse-tree by default.
|
// is of arbitrary arity. ANTLR generates left-associative parse-tree by default.
|
||||||
|
|
|
@ -13,8 +13,8 @@ import (
|
||||||
func TestSimple(t *testing.T) {
|
func TestSimple(t *testing.T) {
|
||||||
q := `REP 3`
|
q := `REP 3`
|
||||||
expected := new(netmap.PlacementPolicy)
|
expected := new(netmap.PlacementPolicy)
|
||||||
expected.SetFilters([]*netmap.Filter{}...)
|
expected.SetFilters([]netmap.Filter{}...)
|
||||||
expected.SetSelectors([]*netmap.Selector{}...)
|
expected.SetSelectors([]netmap.Selector{}...)
|
||||||
expected.SetReplicas(newReplica("", 3))
|
expected.SetReplicas(newReplica("", 3))
|
||||||
|
|
||||||
r, err := Parse(q)
|
r, err := Parse(q)
|
||||||
|
@ -25,8 +25,8 @@ func TestSimple(t *testing.T) {
|
||||||
func TestSimpleWithHRWB(t *testing.T) {
|
func TestSimpleWithHRWB(t *testing.T) {
|
||||||
q := `REP 3 CBF 4`
|
q := `REP 3 CBF 4`
|
||||||
expected := new(netmap.PlacementPolicy)
|
expected := new(netmap.PlacementPolicy)
|
||||||
expected.SetFilters([]*netmap.Filter{}...)
|
expected.SetFilters([]netmap.Filter{}...)
|
||||||
expected.SetSelectors([]*netmap.Selector{}...)
|
expected.SetSelectors([]netmap.Selector{}...)
|
||||||
expected.SetReplicas(newReplica("", 3))
|
expected.SetReplicas(newReplica("", 3))
|
||||||
expected.SetContainerBackupFactor(4)
|
expected.SetContainerBackupFactor(4)
|
||||||
|
|
||||||
|
@ -39,7 +39,7 @@ func TestFromSelect(t *testing.T) {
|
||||||
q := `REP 1 IN SPB
|
q := `REP 1 IN SPB
|
||||||
SELECT 1 IN City FROM * AS SPB`
|
SELECT 1 IN City FROM * AS SPB`
|
||||||
expected := new(netmap.PlacementPolicy)
|
expected := new(netmap.PlacementPolicy)
|
||||||
expected.SetFilters([]*netmap.Filter{}...)
|
expected.SetFilters([]netmap.Filter{}...)
|
||||||
expected.SetSelectors(newSelector(1, netmap.ClauseUnspecified, "City", "*", "SPB"))
|
expected.SetSelectors(newSelector(1, netmap.ClauseUnspecified, "City", "*", "SPB"))
|
||||||
expected.SetReplicas(newReplica("SPB", 1))
|
expected.SetReplicas(newReplica("SPB", 1))
|
||||||
|
|
||||||
|
@ -55,7 +55,7 @@ func TestFromSelectNoAttribute(t *testing.T) {
|
||||||
SELECT 6 FROM *`
|
SELECT 6 FROM *`
|
||||||
|
|
||||||
expected := new(netmap.PlacementPolicy)
|
expected := new(netmap.PlacementPolicy)
|
||||||
expected.SetFilters([]*netmap.Filter{}...)
|
expected.SetFilters([]netmap.Filter{}...)
|
||||||
expected.SetSelectors(newSelector(6, netmap.ClauseUnspecified, "", "*", ""))
|
expected.SetSelectors(newSelector(6, netmap.ClauseUnspecified, "", "*", ""))
|
||||||
expected.SetReplicas(newReplica("", 2))
|
expected.SetReplicas(newReplica("", 2))
|
||||||
|
|
||||||
|
@ -101,7 +101,7 @@ FILTER Property EQ %s AND Something NE 7 AS Filt`
|
||||||
expected := newFilter("Filt", "", "", netmap.OpAND,
|
expected := newFilter("Filt", "", "", netmap.OpAND,
|
||||||
newFilter("", "Property", s[1:len(s)-1], netmap.OpEQ),
|
newFilter("", "Property", s[1:len(s)-1], netmap.OpEQ),
|
||||||
newFilter("", "Something", "7", netmap.OpNE))
|
newFilter("", "Something", "7", netmap.OpNE))
|
||||||
require.EqualValues(t, []*netmap.Filter{expected}, r.Filters())
|
require.EqualValues(t, []netmap.Filter{expected}, r.Filters())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -112,7 +112,7 @@ SELECT 3 IN Country FROM *
|
||||||
SELECT 2 IN SAME City FROM *
|
SELECT 2 IN SAME City FROM *
|
||||||
SELECT 1 IN DISTINCT Continent FROM *`
|
SELECT 1 IN DISTINCT Continent FROM *`
|
||||||
expected := new(netmap.PlacementPolicy)
|
expected := new(netmap.PlacementPolicy)
|
||||||
expected.SetFilters([]*netmap.Filter{}...)
|
expected.SetFilters([]netmap.Filter{}...)
|
||||||
expected.SetSelectors(
|
expected.SetSelectors(
|
||||||
newSelector(3, netmap.ClauseUnspecified, "Country", "*", ""),
|
newSelector(3, netmap.ClauseUnspecified, "Country", "*", ""),
|
||||||
newSelector(2, netmap.ClauseSame, "City", "*", ""),
|
newSelector(2, netmap.ClauseSame, "City", "*", ""),
|
||||||
|
@ -299,8 +299,7 @@ FILTER "UN-LOCODE" EQ "RU LED" AS F`
|
||||||
require.EqualValues(t, expected, r)
|
require.EqualValues(t, expected, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newFilter(name, key, value string, op netmap.Operation, sub ...*netmap.Filter) *netmap.Filter {
|
func newFilter(name, key, value string, op netmap.Operation, sub ...netmap.Filter) (f netmap.Filter) {
|
||||||
f := new(netmap.Filter)
|
|
||||||
f.SetName(name)
|
f.SetName(name)
|
||||||
f.SetKey(key)
|
f.SetKey(key)
|
||||||
f.SetValue(value)
|
f.SetValue(value)
|
||||||
|
@ -309,15 +308,13 @@ func newFilter(name, key, value string, op netmap.Operation, sub ...*netmap.Filt
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func newReplica(s string, c uint32) *netmap.Replica {
|
func newReplica(s string, c uint32) (r netmap.Replica) {
|
||||||
r := new(netmap.Replica)
|
|
||||||
r.SetSelector(s)
|
r.SetSelector(s)
|
||||||
r.SetCount(c)
|
r.SetCount(c)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSelector(count uint32, c netmap.Clause, attr, f, name string) *netmap.Selector {
|
func newSelector(count uint32, c netmap.Clause, attr, f, name string) (s netmap.Selector) {
|
||||||
s := new(netmap.Selector)
|
|
||||||
s.SetCount(count)
|
s.SetCount(count)
|
||||||
s.SetClause(c)
|
s.SetClause(c)
|
||||||
s.SetAttribute(attr)
|
s.SetAttribute(attr)
|
||||||
|
|
Loading…
Reference in a new issue