2019-05-22 23:16:55 +02:00
|
|
|
package mergeset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-09-22 22:11:57 +02:00
|
|
|
"os"
|
2021-02-21 21:06:45 +01:00
|
|
|
"reflect"
|
2019-05-22 23:16:55 +02:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-02-08 23:34:18 +01:00
|
|
|
"unsafe"
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2021-06-29 18:49:05 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/cgroup"
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
2021-02-21 21:06:45 +01:00
|
|
|
// Item represents a single item for storing in a mergeset.
|
|
|
|
type Item struct {
|
|
|
|
// Start is start offset for the item in data.
|
|
|
|
Start uint32
|
2019-09-19 19:13:00 +02:00
|
|
|
|
2021-02-21 21:06:45 +01:00
|
|
|
// End is end offset for the item in data.
|
|
|
|
End uint32
|
2019-09-19 19:13:00 +02:00
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
|
|
|
|
// Bytes returns bytes representation of it obtained from data.
|
|
|
|
//
|
|
|
|
// The returned bytes representation belongs to data.
|
|
|
|
func (it Item) Bytes(data []byte) []byte {
|
|
|
|
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
|
|
|
sh.Cap = int(it.End - it.Start)
|
|
|
|
sh.Len = int(it.End - it.Start)
|
|
|
|
sh.Data += uintptr(it.Start)
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string represetnation of it obtained from data.
|
|
|
|
//
|
|
|
|
// The returned string representation belongs to data.
|
|
|
|
func (it Item) String(data []byte) string {
|
|
|
|
sh := (*reflect.SliceHeader)(unsafe.Pointer(&data))
|
|
|
|
sh.Data += uintptr(it.Start)
|
|
|
|
sh.Len = int(it.End - it.Start)
|
|
|
|
return *(*string)(unsafe.Pointer(sh))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) Len() int { return len(ib.items) }
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) Less(i, j int) bool {
|
|
|
|
data := ib.data
|
|
|
|
items := ib.items
|
|
|
|
return string(items[i].Bytes(data)) < string(items[j].Bytes(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) Swap(i, j int) {
|
|
|
|
items := ib.items
|
|
|
|
items[i], items[j] = items[j], items[i]
|
2019-09-19 19:13:00 +02:00
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
type inmemoryBlock struct {
|
|
|
|
commonPrefix []byte
|
|
|
|
data []byte
|
2021-02-21 21:06:45 +01:00
|
|
|
items []Item
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 23:34:18 +01:00
|
|
|
func (ib *inmemoryBlock) SizeBytes() int {
|
2021-02-21 21:06:45 +01:00
|
|
|
return int(unsafe.Sizeof(*ib)) + cap(ib.commonPrefix) + cap(ib.data) + cap(ib.items)*int(unsafe.Sizeof(Item{}))
|
2021-02-08 23:34:18 +01:00
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
func (ib *inmemoryBlock) Reset() {
|
|
|
|
ib.commonPrefix = ib.commonPrefix[:0]
|
|
|
|
ib.data = ib.data[:0]
|
|
|
|
ib.items = ib.items[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) updateCommonPrefix() {
|
|
|
|
ib.commonPrefix = ib.commonPrefix[:0]
|
|
|
|
if len(ib.items) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
items := ib.items
|
|
|
|
data := ib.data
|
|
|
|
cp := items[0].Bytes(data)
|
2019-05-22 23:16:55 +02:00
|
|
|
if len(cp) == 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
for _, it := range items[1:] {
|
|
|
|
cpLen := commonPrefixLen(cp, it.Bytes(data))
|
2019-05-22 23:16:55 +02:00
|
|
|
if cpLen == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cp = cp[:cpLen]
|
|
|
|
}
|
|
|
|
ib.commonPrefix = append(ib.commonPrefix[:0], cp...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func commonPrefixLen(a, b []byte) int {
|
|
|
|
i := 0
|
|
|
|
if len(a) > len(b) {
|
|
|
|
for i < len(b) && a[i] == b[i] {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i < len(a) && a[i] == b[i] {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds x to the end of ib.
|
|
|
|
//
|
|
|
|
// false is returned if x isn't added to ib due to block size contraints.
|
|
|
|
func (ib *inmemoryBlock) Add(x []byte) bool {
|
2021-02-21 21:06:45 +01:00
|
|
|
data := ib.data
|
|
|
|
if len(x)+len(data) > maxInmemoryBlockSize {
|
2019-05-22 23:16:55 +02:00
|
|
|
return false
|
|
|
|
}
|
2022-01-31 23:56:53 +01:00
|
|
|
if cap(data) == 0 {
|
|
|
|
// Pre-allocate data and items in order to reduce memory allocations
|
|
|
|
data = make([]byte, 0, maxInmemoryBlockSize)
|
|
|
|
ib.items = make([]Item, 0, 512)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
dataLen := len(data)
|
|
|
|
data = append(data, x...)
|
|
|
|
ib.items = append(ib.items, Item{
|
|
|
|
Start: uint32(dataLen),
|
|
|
|
End: uint32(len(data)),
|
|
|
|
})
|
|
|
|
ib.data = data
|
2019-05-22 23:16:55 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// maxInmemoryBlockSize is the maximum inmemoryBlock.data size.
|
|
|
|
//
|
|
|
|
// It must fit CPU cache size, i.e. 64KB for the current CPUs.
|
|
|
|
const maxInmemoryBlockSize = 64 * 1024
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) sort() {
|
2021-02-21 21:06:45 +01:00
|
|
|
sort.Sort(ib)
|
|
|
|
data := ib.data
|
|
|
|
items := ib.items
|
2019-05-22 23:16:55 +02:00
|
|
|
bb := bbPool.Get()
|
2022-01-31 23:18:39 +01:00
|
|
|
b := bytesutil.ResizeNoCopyMayOverallocate(bb.B, len(data))
|
2019-05-22 23:16:55 +02:00
|
|
|
b = b[:0]
|
2021-02-21 21:06:45 +01:00
|
|
|
for i, it := range items {
|
|
|
|
bLen := len(b)
|
|
|
|
b = append(b, it.String(data)...)
|
|
|
|
items[i] = Item{
|
|
|
|
Start: uint32(bLen),
|
|
|
|
End: uint32(len(b)),
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
bb.B, ib.data = data, b
|
2019-05-22 23:16:55 +02:00
|
|
|
bbPool.Put(bb)
|
|
|
|
}
|
|
|
|
|
|
|
|
// storageBlock represents a block of data on the storage.
|
|
|
|
type storageBlock struct {
|
|
|
|
itemsData []byte
|
|
|
|
lensData []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sb *storageBlock) Reset() {
|
|
|
|
sb.itemsData = sb.itemsData[:0]
|
|
|
|
sb.lensData = sb.lensData[:0]
|
|
|
|
}
|
|
|
|
|
|
|
|
type marshalType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
marshalTypePlain = marshalType(0)
|
|
|
|
marshalTypeZSTD = marshalType(1)
|
|
|
|
)
|
|
|
|
|
|
|
|
func checkMarshalType(mt marshalType) error {
|
|
|
|
if mt < 0 || mt > 1 {
|
|
|
|
return fmt.Errorf("marshalType must be in the range [0..1]; got %d", mt)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) isSorted() bool {
|
2019-09-19 19:13:00 +02:00
|
|
|
// Use sort.IsSorted instead of sort.SliceIsSorted in order to eliminate memory allocation.
|
2021-02-21 21:06:45 +01:00
|
|
|
return sort.IsSorted(ib)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalUnsortedData marshals unsorted items from ib to sb.
|
|
|
|
//
|
|
|
|
// It also:
|
|
|
|
// - appends first item to firstItemDst and returns the result.
|
|
|
|
// - appends common prefix for all the items to commonPrefixDst and returns the result.
|
|
|
|
// - returns the number of items encoded including the first item.
|
|
|
|
// - returns the marshal type used for the encoding.
|
|
|
|
func (ib *inmemoryBlock) MarshalUnsortedData(sb *storageBlock, firstItemDst, commonPrefixDst []byte, compressLevel int) ([]byte, []byte, uint32, marshalType) {
|
|
|
|
if !ib.isSorted() {
|
|
|
|
ib.sort()
|
|
|
|
}
|
|
|
|
ib.updateCommonPrefix()
|
|
|
|
return ib.marshalData(sb, firstItemDst, commonPrefixDst, compressLevel)
|
|
|
|
}
|
|
|
|
|
2019-09-22 22:11:57 +02:00
|
|
|
var isInTest = func() bool {
|
|
|
|
return strings.HasSuffix(os.Args[0], ".test")
|
|
|
|
}()
|
2019-09-19 19:13:00 +02:00
|
|
|
|
2020-05-14 22:44:01 +02:00
|
|
|
// MarshalSortedData marshals sorted items from ib to sb.
|
2019-05-22 23:16:55 +02:00
|
|
|
//
|
|
|
|
// It also:
|
|
|
|
// - appends first item to firstItemDst and returns the result.
|
|
|
|
// - appends common prefix for all the items to commonPrefixDst and returns the result.
|
|
|
|
// - returns the number of items encoded including the first item.
|
|
|
|
// - returns the marshal type used for the encoding.
|
|
|
|
func (ib *inmemoryBlock) MarshalSortedData(sb *storageBlock, firstItemDst, commonPrefixDst []byte, compressLevel int) ([]byte, []byte, uint32, marshalType) {
|
2019-09-19 19:13:00 +02:00
|
|
|
if isInTest && !ib.isSorted() {
|
|
|
|
logger.Panicf("BUG: %d items must be sorted; items:\n%s", len(ib.items), ib.debugItemsString())
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
ib.updateCommonPrefix()
|
|
|
|
return ib.marshalData(sb, firstItemDst, commonPrefixDst, compressLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) debugItemsString() string {
|
|
|
|
var sb strings.Builder
|
2021-02-21 21:06:45 +01:00
|
|
|
var prevItem string
|
|
|
|
data := ib.data
|
|
|
|
for i, it := range ib.items {
|
|
|
|
item := it.String(data)
|
|
|
|
if item < prevItem {
|
2019-09-23 19:40:38 +02:00
|
|
|
fmt.Fprintf(&sb, "!!! the next item is smaller than the previous item !!!\n")
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
fmt.Fprintf(&sb, "%05d %X\n", i, item)
|
2019-09-23 19:40:38 +02:00
|
|
|
prevItem = item
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preconditions:
|
|
|
|
// - ib.items must be sorted.
|
|
|
|
// - updateCommonPrefix must be called.
|
|
|
|
func (ib *inmemoryBlock) marshalData(sb *storageBlock, firstItemDst, commonPrefixDst []byte, compressLevel int) ([]byte, []byte, uint32, marshalType) {
|
|
|
|
if len(ib.items) <= 0 {
|
|
|
|
logger.Panicf("BUG: inmemoryBlock.marshalData must be called on non-empty blocks only")
|
|
|
|
}
|
|
|
|
if uint64(len(ib.items)) >= 1<<32 {
|
|
|
|
logger.Panicf("BUG: the number of items in the block must be smaller than %d; got %d items", uint64(1<<32), len(ib.items))
|
|
|
|
}
|
|
|
|
|
2021-02-21 21:06:45 +01:00
|
|
|
data := ib.data
|
|
|
|
firstItem := ib.items[0].Bytes(data)
|
|
|
|
firstItemDst = append(firstItemDst, firstItem...)
|
2019-05-22 23:16:55 +02:00
|
|
|
commonPrefixDst = append(commonPrefixDst, ib.commonPrefix...)
|
|
|
|
|
2019-09-20 18:46:47 +02:00
|
|
|
if len(ib.data)-len(ib.commonPrefix)*len(ib.items) < 64 || len(ib.items) < 2 {
|
2019-05-22 23:16:55 +02:00
|
|
|
// Use plain encoding form small block, since it is cheaper.
|
|
|
|
ib.marshalDataPlain(sb)
|
|
|
|
return firstItemDst, commonPrefixDst, uint32(len(ib.items)), marshalTypePlain
|
|
|
|
}
|
|
|
|
|
|
|
|
bbItems := bbPool.Get()
|
|
|
|
bItems := bbItems.B[:0]
|
|
|
|
|
|
|
|
bbLens := bbPool.Get()
|
|
|
|
bLens := bbLens.B[:0]
|
|
|
|
|
|
|
|
// Marshal items data.
|
|
|
|
xs := encoding.GetUint64s(len(ib.items) - 1)
|
|
|
|
defer encoding.PutUint64s(xs)
|
|
|
|
|
|
|
|
cpLen := len(ib.commonPrefix)
|
2021-02-21 21:06:45 +01:00
|
|
|
prevItem := firstItem[cpLen:]
|
2019-05-22 23:16:55 +02:00
|
|
|
prevPrefixLen := uint64(0)
|
2021-02-21 21:06:45 +01:00
|
|
|
for i, it := range ib.items[1:] {
|
|
|
|
it.Start += uint32(cpLen)
|
|
|
|
item := it.Bytes(data)
|
2019-05-22 23:16:55 +02:00
|
|
|
prefixLen := uint64(commonPrefixLen(prevItem, item))
|
|
|
|
bItems = append(bItems, item[prefixLen:]...)
|
|
|
|
xLen := prefixLen ^ prevPrefixLen
|
|
|
|
prevItem = item
|
|
|
|
prevPrefixLen = prefixLen
|
|
|
|
|
|
|
|
xs.A[i] = xLen
|
|
|
|
}
|
|
|
|
bLens = encoding.MarshalVarUint64s(bLens, xs.A)
|
|
|
|
sb.itemsData = encoding.CompressZSTDLevel(sb.itemsData[:0], bItems, compressLevel)
|
|
|
|
|
|
|
|
bbItems.B = bItems
|
|
|
|
bbPool.Put(bbItems)
|
|
|
|
|
|
|
|
// Marshal lens data.
|
2021-02-21 21:06:45 +01:00
|
|
|
prevItemLen := uint64(len(firstItem) - cpLen)
|
|
|
|
for i, it := range ib.items[1:] {
|
|
|
|
itemLen := uint64(int(it.End-it.Start) - cpLen)
|
2019-05-22 23:16:55 +02:00
|
|
|
xLen := itemLen ^ prevItemLen
|
|
|
|
prevItemLen = itemLen
|
|
|
|
|
|
|
|
xs.A[i] = xLen
|
|
|
|
}
|
|
|
|
bLens = encoding.MarshalVarUint64s(bLens, xs.A)
|
|
|
|
sb.lensData = encoding.CompressZSTDLevel(sb.lensData[:0], bLens, compressLevel)
|
|
|
|
|
|
|
|
bbLens.B = bLens
|
|
|
|
bbPool.Put(bbLens)
|
|
|
|
|
|
|
|
if float64(len(sb.itemsData)) > 0.9*float64(len(ib.data)-len(ib.commonPrefix)*len(ib.items)) {
|
|
|
|
// Bad compression rate. It is cheaper to use plain encoding.
|
|
|
|
ib.marshalDataPlain(sb)
|
|
|
|
return firstItemDst, commonPrefixDst, uint32(len(ib.items)), marshalTypePlain
|
|
|
|
}
|
|
|
|
|
|
|
|
// Good compression rate.
|
|
|
|
return firstItemDst, commonPrefixDst, uint32(len(ib.items)), marshalTypeZSTD
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalData decodes itemsCount items from sb and firstItem and stores
|
|
|
|
// them to ib.
|
|
|
|
func (ib *inmemoryBlock) UnmarshalData(sb *storageBlock, firstItem, commonPrefix []byte, itemsCount uint32, mt marshalType) error {
|
|
|
|
ib.Reset()
|
|
|
|
|
|
|
|
if itemsCount <= 0 {
|
|
|
|
logger.Panicf("BUG: cannot unmarshal zero items")
|
|
|
|
}
|
|
|
|
|
|
|
|
ib.commonPrefix = append(ib.commonPrefix[:0], commonPrefix...)
|
|
|
|
|
|
|
|
switch mt {
|
|
|
|
case marshalTypePlain:
|
|
|
|
if err := ib.unmarshalDataPlain(sb, firstItem, itemsCount); err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot unmarshal plain data: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
if !ib.isSorted() {
|
|
|
|
return fmt.Errorf("plain data block contains unsorted items; items:\n%s", ib.debugItemsString())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case marshalTypeZSTD:
|
|
|
|
// it is handled below.
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown marshalType=%d", mt)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal mt = marshalTypeZSTD
|
|
|
|
|
|
|
|
bb := bbPool.Get()
|
|
|
|
defer bbPool.Put(bb)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// Unmarshal lens data.
|
|
|
|
bb.B, err = encoding.DecompressZSTD(bb.B[:0], sb.lensData)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot decompress lensData: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
lb := getLensBuffer(int(2 * itemsCount))
|
|
|
|
defer putLensBuffer(lb)
|
|
|
|
|
|
|
|
prefixLens := lb.lens[:itemsCount]
|
|
|
|
lens := lb.lens[itemsCount:]
|
|
|
|
|
|
|
|
is := encoding.GetUint64s(int(itemsCount) - 1)
|
|
|
|
defer encoding.PutUint64s(is)
|
|
|
|
|
|
|
|
// Unmarshal prefixLens
|
|
|
|
tail, err := encoding.UnmarshalVarUint64s(is.A, bb.B)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot unmarshal prefixLens from lensData: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
prefixLens[0] = 0
|
|
|
|
for i, xLen := range is.A {
|
|
|
|
prefixLens[i+1] = xLen ^ prefixLens[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal lens
|
|
|
|
tail, err = encoding.UnmarshalVarUint64s(is.A, tail)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot unmarshal lens from lensData: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
if len(tail) > 0 {
|
|
|
|
return fmt.Errorf("unexpected tail left unmarshaling %d lens; tail size=%d; contents=%X", itemsCount, len(tail), tail)
|
|
|
|
}
|
|
|
|
lens[0] = uint64(len(firstItem) - len(commonPrefix))
|
2022-01-24 17:49:29 +01:00
|
|
|
dataLen := len(commonPrefix) * int(itemsCount)
|
|
|
|
dataLen += int(lens[0])
|
2019-05-22 23:16:55 +02:00
|
|
|
for i, xLen := range is.A {
|
|
|
|
itemLen := xLen ^ lens[i]
|
|
|
|
lens[i+1] = itemLen
|
2022-01-24 17:49:29 +01:00
|
|
|
dataLen += int(itemLen)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal items data.
|
|
|
|
bb.B, err = encoding.DecompressZSTD(bb.B[:0], sb.itemsData)
|
|
|
|
if err != nil {
|
2020-06-30 21:58:18 +02:00
|
|
|
return fmt.Errorf("cannot decompress lensData: %w", err)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2022-01-24 17:49:29 +01:00
|
|
|
// Resize ib.data to dataLen instead of maxInmemoryBlockSize,
|
|
|
|
// since the data isn't going to be resized after unmarshaling.
|
|
|
|
// This may save memory for caching the unmarshaled block.
|
2022-01-31 23:18:39 +01:00
|
|
|
data := bytesutil.ResizeNoCopyNoOverallocate(ib.data, dataLen)
|
2019-05-22 23:16:55 +02:00
|
|
|
if n := int(itemsCount) - cap(ib.items); n > 0 {
|
2021-02-21 21:06:45 +01:00
|
|
|
ib.items = append(ib.items[:cap(ib.items)], make([]Item, n)...)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
ib.items = ib.items[:itemsCount]
|
|
|
|
data = append(data[:0], firstItem...)
|
2021-02-21 21:06:45 +01:00
|
|
|
items := ib.items
|
|
|
|
items[0] = Item{
|
|
|
|
Start: 0,
|
|
|
|
End: uint32(len(data)),
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
prevItem := data[len(commonPrefix):]
|
|
|
|
b := bb.B
|
|
|
|
for i := 1; i < int(itemsCount); i++ {
|
|
|
|
itemLen := lens[i]
|
|
|
|
prefixLen := prefixLens[i]
|
|
|
|
if prefixLen > itemLen {
|
|
|
|
return fmt.Errorf("prefixLen=%d exceeds itemLen=%d", prefixLen, itemLen)
|
|
|
|
}
|
|
|
|
suffixLen := itemLen - prefixLen
|
|
|
|
if uint64(len(b)) < suffixLen {
|
|
|
|
return fmt.Errorf("not enough data for decoding item from itemsData; want %d bytes; remained %d bytes", suffixLen, len(b))
|
|
|
|
}
|
|
|
|
if prefixLen > uint64(len(prevItem)) {
|
|
|
|
return fmt.Errorf("prefixLen cannot exceed %d; got %d", len(prevItem), prefixLen)
|
|
|
|
}
|
2022-01-24 17:49:29 +01:00
|
|
|
dataStart := len(data)
|
2021-02-21 21:06:45 +01:00
|
|
|
data = append(data, commonPrefix...)
|
2019-05-22 23:16:55 +02:00
|
|
|
data = append(data, prevItem[:prefixLen]...)
|
|
|
|
data = append(data, b[:suffixLen]...)
|
2021-02-21 21:06:45 +01:00
|
|
|
items[i] = Item{
|
2022-01-24 17:49:29 +01:00
|
|
|
Start: uint32(dataStart),
|
2021-02-21 21:06:45 +01:00
|
|
|
End: uint32(len(data)),
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
b = b[suffixLen:]
|
2021-02-21 21:06:45 +01:00
|
|
|
prevItem = data[len(data)-int(itemLen):]
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
if len(b) > 0 {
|
|
|
|
return fmt.Errorf("unexpected tail left after itemsData with len %d: %q", len(b), b)
|
|
|
|
}
|
2022-01-24 17:49:29 +01:00
|
|
|
if len(data) != dataLen {
|
2019-05-22 23:16:55 +02:00
|
|
|
return fmt.Errorf("unexpected data len; got %d; want %d", len(data), dataLen)
|
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
ib.data = data
|
2019-05-22 23:16:55 +02:00
|
|
|
if !ib.isSorted() {
|
|
|
|
return fmt.Errorf("decoded data block contains unsorted items; items:\n%s", ib.debugItemsString())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var bbPool bytesutil.ByteBufferPool
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) marshalDataPlain(sb *storageBlock) {
|
2021-02-21 21:06:45 +01:00
|
|
|
data := ib.data
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// Marshal items data.
|
|
|
|
// There is no need in marshaling the first item, since it is returned
|
|
|
|
// to the caller in marshalData.
|
|
|
|
cpLen := len(ib.commonPrefix)
|
|
|
|
b := sb.itemsData[:0]
|
2021-02-21 21:06:45 +01:00
|
|
|
for _, it := range ib.items[1:] {
|
|
|
|
it.Start += uint32(cpLen)
|
|
|
|
b = append(b, it.String(data)...)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
sb.itemsData = b
|
|
|
|
|
|
|
|
// Marshal length data.
|
|
|
|
b = sb.lensData[:0]
|
2021-02-21 21:06:45 +01:00
|
|
|
for _, it := range ib.items[1:] {
|
|
|
|
b = encoding.MarshalUint64(b, uint64(int(it.End-it.Start)-cpLen))
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
sb.lensData = b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) unmarshalDataPlain(sb *storageBlock, firstItem []byte, itemsCount uint32) error {
|
|
|
|
commonPrefix := ib.commonPrefix
|
|
|
|
|
|
|
|
// Unmarshal lens data.
|
|
|
|
lb := getLensBuffer(int(itemsCount))
|
|
|
|
defer putLensBuffer(lb)
|
|
|
|
|
|
|
|
lb.lens[0] = uint64(len(firstItem) - len(commonPrefix))
|
|
|
|
b := sb.lensData
|
|
|
|
for i := 1; i < int(itemsCount); i++ {
|
|
|
|
if len(b) < 8 {
|
|
|
|
return fmt.Errorf("too short tail for decoding len from lensData; got %d bytes; want at least %d bytes", len(b), 8)
|
|
|
|
}
|
|
|
|
iLen := encoding.UnmarshalUint64(b)
|
|
|
|
b = b[8:]
|
|
|
|
lb.lens[i] = iLen
|
|
|
|
}
|
|
|
|
if len(b) > 0 {
|
|
|
|
return fmt.Errorf("unexpected tail left after lensData with len %d: %q", len(b), b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal items data.
|
2021-02-21 21:06:45 +01:00
|
|
|
data := ib.data
|
|
|
|
items := ib.items
|
2022-01-31 23:18:39 +01:00
|
|
|
dataLen := len(firstItem) + len(sb.itemsData) + len(commonPrefix)*(int(itemsCount)-1)
|
|
|
|
data = bytesutil.ResizeNoCopyNoOverallocate(data, dataLen)
|
2021-02-21 21:06:45 +01:00
|
|
|
data = append(data[:0], firstItem...)
|
|
|
|
items = append(items[:0], Item{
|
|
|
|
Start: 0,
|
|
|
|
End: uint32(len(data)),
|
|
|
|
})
|
2019-05-22 23:16:55 +02:00
|
|
|
b = sb.itemsData
|
|
|
|
for i := 1; i < int(itemsCount); i++ {
|
|
|
|
itemLen := lb.lens[i]
|
|
|
|
if uint64(len(b)) < itemLen {
|
|
|
|
return fmt.Errorf("not enough data for decoding item from itemsData; want %d bytes; remained %d bytes", itemLen, len(b))
|
|
|
|
}
|
2022-01-31 23:18:39 +01:00
|
|
|
dataStart := len(data)
|
2021-02-21 21:06:45 +01:00
|
|
|
data = append(data, commonPrefix...)
|
|
|
|
data = append(data, b[:itemLen]...)
|
|
|
|
items = append(items, Item{
|
2022-01-31 23:18:39 +01:00
|
|
|
Start: uint32(dataStart),
|
2021-02-21 21:06:45 +01:00
|
|
|
End: uint32(len(data)),
|
|
|
|
})
|
2019-05-22 23:16:55 +02:00
|
|
|
b = b[itemLen:]
|
|
|
|
}
|
|
|
|
if len(b) > 0 {
|
|
|
|
return fmt.Errorf("unexpected tail left after itemsData with len %d: %q", len(b), b)
|
|
|
|
}
|
2022-01-31 23:18:39 +01:00
|
|
|
if len(data) != dataLen {
|
|
|
|
return fmt.Errorf("unexpected data len; got %d; want %d", len(data), dataLen)
|
|
|
|
}
|
|
|
|
ib.data = data
|
|
|
|
ib.items = items
|
2019-05-22 23:16:55 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type lensBuffer struct {
|
|
|
|
lens []uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
var lensBufferPool sync.Pool
|
|
|
|
|
|
|
|
func getLensBuffer(n int) *lensBuffer {
|
|
|
|
v := lensBufferPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
v = &lensBuffer{}
|
|
|
|
}
|
|
|
|
lb := v.(*lensBuffer)
|
|
|
|
if nn := n - cap(lb.lens); nn > 0 {
|
|
|
|
lb.lens = append(lb.lens[:cap(lb.lens)], make([]uint64, nn)...)
|
|
|
|
}
|
|
|
|
lb.lens = lb.lens[:n]
|
|
|
|
return lb
|
|
|
|
}
|
|
|
|
|
|
|
|
func putLensBuffer(lb *lensBuffer) {
|
|
|
|
lensBufferPool.Put(lb)
|
|
|
|
}
|
|
|
|
|
2021-05-28 00:09:32 +02:00
|
|
|
func getInmemoryBlock() *inmemoryBlock {
|
2021-06-29 18:49:05 +02:00
|
|
|
select {
|
|
|
|
case ib := <-ibPoolCh:
|
|
|
|
return ib
|
|
|
|
default:
|
2021-05-28 00:09:32 +02:00
|
|
|
return &inmemoryBlock{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func putInmemoryBlock(ib *inmemoryBlock) {
|
|
|
|
ib.Reset()
|
2021-06-29 18:49:05 +02:00
|
|
|
select {
|
|
|
|
case ibPoolCh <- ib:
|
|
|
|
default:
|
|
|
|
// drop ib in order to reduce memory usage on systems with big number of CPU cores
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2021-05-28 00:09:32 +02:00
|
|
|
|
2021-06-29 18:49:05 +02:00
|
|
|
// Every inmemoryBlock struct occupies at least 64KB of memory, e.g. quite big amounts of memory.
|
|
|
|
// Use a chan instead of sync.Pool in order to reduce memory usage on systems
|
|
|
|
// with big number of CPU cores.
|
2021-07-06 12:41:32 +02:00
|
|
|
var ibPoolCh = make(chan *inmemoryBlock, 100*cgroup.AvailableCPUs())
|