2020-08-13 12:43:47 +00:00
|
|
|
package netmap
|
2020-08-12 10:34:15 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"math/bits"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy) StableMarshal(buf []byte) ([]byte, error) {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, m.StableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
i, n, offset int
|
|
|
|
)
|
|
|
|
|
|
|
|
// Write replication factor field.
|
|
|
|
|
|
|
|
buf[i] = 0x08 // id:0x1 << 3 | wiretype:0x0
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(m.ReplFactor))
|
|
|
|
i += 1 + offset
|
|
|
|
|
|
|
|
// write select/filter groups field
|
2020-08-14 07:22:15 +00:00
|
|
|
for j := range m.FilterGroups {
|
2020-08-12 10:34:15 +00:00
|
|
|
buf[i] = 0x12 // id:0x2 << 3 | wiretype:0x2
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
n, _ = m.FilterGroups[j].stableSizeWithExclude()
|
2020-08-12 10:34:15 +00:00
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(n))
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
_, err := m.FilterGroups[j].StableMarshal(buf[i+1+offset:])
|
2020-08-12 10:34:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "can't marshal SFGroup id:%d", j)
|
|
|
|
}
|
|
|
|
|
|
|
|
i += 1 + offset + n
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy) StableSize() int {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ln, size int
|
|
|
|
)
|
|
|
|
_ = ln
|
|
|
|
|
|
|
|
// size of replication factor field
|
|
|
|
size += 1 + uvarIntSize(uint64(m.ReplFactor)) // wiretype + varint
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
for i := range m.FilterGroups {
|
|
|
|
ln, _ = m.FilterGroups[i].stableSizeWithExclude()
|
2020-08-12 10:34:15 +00:00
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln // wiretype + size of struct + struct
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup) StableMarshal(buf []byte) ([]byte, error) {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
size, excludeSize := m.stableSizeWithExclude()
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, size)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
i, n, offset int
|
|
|
|
)
|
|
|
|
|
|
|
|
// write filters field
|
|
|
|
for j := range m.Filters {
|
|
|
|
buf[i] = 0x0A // id:0x1 << 3 | wiretype:0x2
|
|
|
|
n = m.Filters[j].stableSize()
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(n))
|
|
|
|
_, err := m.Filters[j].StableMarshal(buf[i+1+offset:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "can't marshal Filter id:%d", j)
|
|
|
|
}
|
|
|
|
i += 1 + offset + n
|
|
|
|
}
|
|
|
|
|
|
|
|
// write selectors field
|
|
|
|
for j := range m.Selectors {
|
|
|
|
buf[i] = 0x12 // id:0x2 << 3 | wiretype:0x2
|
|
|
|
n = m.Selectors[j].stableSize()
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(n))
|
|
|
|
_, err := m.Selectors[j].StableMarshal(buf[i+1+offset:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "can't marshal Selector id:%d", j)
|
|
|
|
}
|
|
|
|
i += 1 + offset + n
|
|
|
|
}
|
|
|
|
|
|
|
|
// write excluded field in packed format
|
|
|
|
buf[i] = 0x1A // id:0x3 << 3 | wiretype:0x2
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(excludeSize))
|
|
|
|
i += 1 + offset
|
|
|
|
for j := range m.Exclude {
|
|
|
|
offset = binary.PutUvarint(buf[i:], uint64(m.Exclude[j]))
|
|
|
|
i += offset
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup) stableSizeWithExclude() (int, int) {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ln, size int
|
|
|
|
)
|
|
|
|
|
|
|
|
// size of filters field
|
|
|
|
for i := range m.Filters {
|
|
|
|
ln = m.Filters[i].stableSize()
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln // wiretype + size of struct + struct
|
|
|
|
}
|
|
|
|
|
|
|
|
// size of selectors field
|
|
|
|
for i := range m.Selectors {
|
|
|
|
ln = m.Selectors[i].stableSize()
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln // wiretype + size of struct + struct
|
|
|
|
}
|
|
|
|
|
|
|
|
// size of exclude field
|
|
|
|
ln = 0
|
|
|
|
for i := range m.Exclude {
|
|
|
|
ln += uvarIntSize(uint64(m.Exclude[i]))
|
|
|
|
}
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln // wiretype + packed varints size + packed varints
|
|
|
|
|
|
|
|
return size, ln
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup_Selector) StableMarshal(buf []byte) ([]byte, error) {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, m.stableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
i, offset int
|
|
|
|
)
|
|
|
|
|
|
|
|
// write count field
|
|
|
|
buf[i] = 0x8 // id:0x1 << 3 | wiretype:0x0
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(m.Count))
|
|
|
|
i += 1 + offset
|
|
|
|
|
|
|
|
// write key field
|
|
|
|
buf[i] = 0x12 // id:0x2 << 3 | wiretype:0x2
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(len(m.Key)))
|
|
|
|
copy(buf[i+1+offset:], m.Key)
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup_Selector) stableSize() int {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ln, size int
|
|
|
|
)
|
|
|
|
|
|
|
|
// size of count field
|
|
|
|
size += 1 + uvarIntSize(uint64(m.Count))
|
|
|
|
|
|
|
|
// size of key field
|
|
|
|
ln = len(m.Key)
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup_Filter) StableMarshal(buf []byte) ([]byte, error) {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, m.stableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
i, n, offset int
|
|
|
|
)
|
|
|
|
|
|
|
|
// write key field
|
|
|
|
buf[i] = 0x0A // id:0x1 << 3 | wiretype:0x2
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(len(m.Key)))
|
|
|
|
n = copy(buf[i+1+offset:], m.Key)
|
|
|
|
i += 1 + offset + n
|
|
|
|
|
|
|
|
// write simple filter field
|
|
|
|
if m.F != nil {
|
|
|
|
buf[i] = 0x12 // id:0x2 << 3 | wiretype:0x2
|
|
|
|
n = m.F.stableSize()
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(n))
|
|
|
|
_, err := m.F.StableMarshal(buf[i+1+offset:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't marshal netmap filter")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup_Filter) stableSize() int {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ln, size int
|
|
|
|
)
|
|
|
|
|
|
|
|
// size of key field
|
|
|
|
ln = len(m.Key)
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln
|
|
|
|
|
|
|
|
// size of simple filter
|
|
|
|
if m.F != nil {
|
|
|
|
ln = m.F.stableSize()
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup_Filter_SimpleFilter) StableMarshal(buf []byte) ([]byte, error) {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, m.stableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
i, n, offset int
|
|
|
|
)
|
|
|
|
|
|
|
|
// write key field
|
|
|
|
buf[i] = 0x08 // id:0x1 << 3 | wiretype:0x0
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(m.Op))
|
|
|
|
i += 1 + offset
|
|
|
|
|
|
|
|
// write value if present
|
2020-08-14 07:22:15 +00:00
|
|
|
if val, ok := m.Args.(*PlacementPolicy_FilterGroup_Filter_SimpleFilter_Value); ok {
|
2020-08-12 10:34:15 +00:00
|
|
|
buf[i] = 0x12 // id:0x2 << 3 | wiretype:0x2
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(len(val.Value)))
|
|
|
|
copy(buf[i+1+offset:], val.Value)
|
2020-08-14 07:22:15 +00:00
|
|
|
} else if filters, ok := m.Args.(*PlacementPolicy_FilterGroup_Filter_SimpleFilter_FArgs); ok {
|
2020-08-12 10:34:15 +00:00
|
|
|
if filters.FArgs != nil {
|
|
|
|
buf[i] = 0x1A // id:0x3 << 3 | wiretype:0x2
|
|
|
|
n = filters.FArgs.stableSize()
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(n))
|
|
|
|
_, err := filters.FArgs.StableMarshal(buf[i+1+offset:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't marshal simple filters")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup_Filter_SimpleFilter) stableSize() int {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ln, size int
|
|
|
|
)
|
|
|
|
|
|
|
|
// size of key field
|
|
|
|
size += 1 + uvarIntSize(uint64(m.Op))
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
if val, ok := m.Args.(*PlacementPolicy_FilterGroup_Filter_SimpleFilter_Value); ok {
|
2020-08-12 10:34:15 +00:00
|
|
|
// size of value if present
|
|
|
|
ln = len(val.Value)
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln
|
2020-08-14 07:22:15 +00:00
|
|
|
} else if filters, ok := m.Args.(*PlacementPolicy_FilterGroup_Filter_SimpleFilter_FArgs); ok {
|
2020-08-12 10:34:15 +00:00
|
|
|
// size of simple filters if present
|
|
|
|
if filters.FArgs != nil {
|
|
|
|
ln = filters.FArgs.stableSize()
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup_Filter_SimpleFilter_SimpleFilters) StableMarshal(buf []byte) ([]byte, error) {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return []byte{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if buf == nil {
|
|
|
|
buf = make([]byte, m.stableSize())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
i, n, offset int
|
|
|
|
)
|
|
|
|
|
|
|
|
// write filters field
|
|
|
|
for j := range m.Filters {
|
|
|
|
buf[i] = 0x0A // id:0x1 << 3 | wiretype:0x2
|
|
|
|
n = m.Filters[j].stableSize()
|
|
|
|
offset = binary.PutUvarint(buf[i+1:], uint64(n))
|
|
|
|
_, err := m.Filters[j].StableMarshal(buf[i+1+offset:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "can't marshal simple filter id:%d", j)
|
|
|
|
}
|
|
|
|
i += 1 + offset + n
|
|
|
|
}
|
|
|
|
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:15 +00:00
|
|
|
func (m *PlacementPolicy_FilterGroup_Filter_SimpleFilter_SimpleFilters) stableSize() int {
|
2020-08-12 10:34:15 +00:00
|
|
|
if m == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
ln, size int
|
|
|
|
)
|
|
|
|
|
|
|
|
// size of key field
|
|
|
|
for i := range m.Filters {
|
|
|
|
ln = m.Filters[i].stableSize()
|
|
|
|
size += 1 + uvarIntSize(uint64(ln)) + ln
|
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
// uvarIntSize returns length of varint byte sequence for uint64 value 'x'.
|
|
|
|
func uvarIntSize(x uint64) int {
|
|
|
|
return (bits.Len64(x|1) + 6) / 7
|
|
|
|
}
|