2020-10-15 14:55:12 +00:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-10-15 14:55:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func Encode(p *netmap.PlacementPolicy) []string {
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2020-11-05 15:51:43 +00:00
|
|
|
replicas = p.Replicas()
|
|
|
|
selectors = p.Selectors()
|
|
|
|
filters = p.Filters()
|
2020-10-15 14:55:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 1 for container backup factor
|
|
|
|
result := make([]string, 0, len(replicas)+len(selectors)+len(filters)+1)
|
|
|
|
|
|
|
|
// first print replicas
|
|
|
|
encodeReplicas(replicas, &result)
|
|
|
|
|
|
|
|
// then backup factor
|
2020-11-05 15:51:43 +00:00
|
|
|
if backupFactor := p.ContainerBackupFactor(); backupFactor != 0 {
|
2020-10-15 14:55:12 +00:00
|
|
|
result = append(result, fmt.Sprintf("CBF %d", backupFactor))
|
|
|
|
}
|
|
|
|
|
|
|
|
// then selectors
|
|
|
|
encodeSelectors(selectors, &result)
|
|
|
|
|
|
|
|
// then filters
|
|
|
|
encodeFilters(filters, &result)
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeReplicas(replicas []*netmap.Replica, dst *[]string) {
|
|
|
|
builder := new(strings.Builder)
|
|
|
|
|
|
|
|
for _, replica := range replicas {
|
|
|
|
builder.WriteString("REP ")
|
2020-11-05 15:51:43 +00:00
|
|
|
builder.WriteString(strconv.FormatUint(uint64(replica.Count()), 10))
|
2020-10-15 14:55:12 +00:00
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
if s := replica.Selector(); s != "" {
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" IN ")
|
|
|
|
builder.WriteString(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = append(*dst, builder.String())
|
|
|
|
builder.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeSelectors(selectors []*netmap.Selector, dst *[]string) {
|
|
|
|
builder := new(strings.Builder)
|
|
|
|
|
|
|
|
for _, selector := range selectors {
|
|
|
|
builder.WriteString("SELECT ")
|
2020-11-05 15:51:43 +00:00
|
|
|
builder.WriteString(strconv.FormatUint(uint64(selector.Count()), 10))
|
2020-10-15 14:55:12 +00:00
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
if a := selector.Attribute(); a != "" {
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" IN")
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
switch selector.Clause() {
|
|
|
|
case netmap.ClauseSame:
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" SAME ")
|
2020-11-05 15:51:43 +00:00
|
|
|
case netmap.ClauseDistinct:
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" DISTINCT ")
|
|
|
|
default:
|
|
|
|
builder.WriteString(" ")
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.WriteString(a)
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
if f := selector.Filter(); f != "" {
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" FROM ")
|
|
|
|
builder.WriteString(f)
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
if n := selector.Name(); n != "" {
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" AS ")
|
|
|
|
builder.WriteString(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = append(*dst, builder.String())
|
|
|
|
builder.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeFilters(filters []*netmap.Filter, dst *[]string) {
|
|
|
|
builder := new(strings.Builder)
|
|
|
|
|
|
|
|
for _, filter := range filters {
|
|
|
|
builder.WriteString("FILTER ")
|
|
|
|
|
|
|
|
builder.WriteString(encodeFilter(filter))
|
|
|
|
|
|
|
|
*dst = append(*dst, builder.String())
|
|
|
|
builder.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func encodeFilter(filter *netmap.Filter) string {
|
|
|
|
builder := new(strings.Builder)
|
2020-11-05 15:51:43 +00:00
|
|
|
unspecified := filter.Operation() == 0
|
2020-10-15 14:55:12 +00:00
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
if k := filter.Key(); k != "" {
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(k)
|
|
|
|
builder.WriteString(" ")
|
2020-11-05 15:51:43 +00:00
|
|
|
builder.WriteString(filter.Operation().String())
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" ")
|
2020-11-05 15:51:43 +00:00
|
|
|
builder.WriteString(filter.Value())
|
|
|
|
} else if n := filter.Name(); unspecified && n != "" {
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString("@")
|
|
|
|
builder.WriteString(n)
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
for i, subfilter := range filter.InnerFilters() {
|
2020-10-15 14:55:12 +00:00
|
|
|
if i != 0 {
|
|
|
|
builder.WriteString(" ")
|
2020-11-05 15:51:43 +00:00
|
|
|
builder.WriteString(filter.Operation().String())
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" ")
|
|
|
|
}
|
|
|
|
|
|
|
|
builder.WriteString(encodeFilter(subfilter))
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
if n := filter.Name(); n != "" && !unspecified {
|
2020-10-15 14:55:12 +00:00
|
|
|
builder.WriteString(" AS ")
|
|
|
|
builder.WriteString(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.String()
|
|
|
|
}
|