2019-05-22 21:16:55 +00:00
|
|
|
package mergeset
|
|
|
|
|
|
|
|
import (
|
2022-07-27 20:47:18 +00:00
|
|
|
"bytes"
|
2019-05-22 21:16:55 +00:00
|
|
|
"fmt"
|
2019-09-22 20:11:57 +00:00
|
|
|
"os"
|
2019-05-22 21:16:55 +00:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-02-08 22:34:18 +00:00
|
|
|
"unsafe"
|
2019-05-22 21:16:55 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
2021-02-21 20:06:45 +00: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 17:13:00 +00:00
|
|
|
|
2021-02-21 20:06:45 +00:00
|
|
|
// End is end offset for the item in data.
|
|
|
|
End uint32
|
2019-09-19 17:13:00 +00:00
|
|
|
}
|
2021-02-21 20:06:45 +00:00
|
|
|
|
|
|
|
// Bytes returns bytes representation of it obtained from data.
|
|
|
|
//
|
|
|
|
// The returned bytes representation belongs to data.
|
|
|
|
func (it Item) Bytes(data []byte) []byte {
|
2022-04-06 15:48:05 +00:00
|
|
|
n := int(it.End - it.Start)
|
2024-02-29 15:29:33 +00:00
|
|
|
return unsafe.Slice((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(data)))+uintptr(it.Start))), n)
|
2021-02-21 20:06:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 20:47:18 +00:00
|
|
|
// String returns string representation of it obtained from data.
|
2021-02-21 20:06:45 +00:00
|
|
|
//
|
|
|
|
// The returned string representation belongs to data.
|
|
|
|
func (it Item) String(data []byte) string {
|
2024-02-29 15:29:33 +00:00
|
|
|
n := int(it.End - it.Start)
|
|
|
|
return unsafe.String((*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(unsafe.SliceData(data)))+uintptr(it.Start))), n)
|
2021-02-21 20:06:45 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:26:33 +00:00
|
|
|
func (ib *inmemoryBlock) Len() int {
|
|
|
|
return len(ib.items)
|
|
|
|
}
|
2021-02-21 20:06:45 +00:00
|
|
|
|
|
|
|
func (ib *inmemoryBlock) Less(i, j int) bool {
|
|
|
|
items := ib.items
|
2022-04-06 15:48:05 +00:00
|
|
|
a := items[i]
|
|
|
|
b := items[j]
|
|
|
|
cpLen := uint32(len(ib.commonPrefix))
|
|
|
|
a.Start += cpLen
|
|
|
|
b.Start += cpLen
|
|
|
|
data := ib.data
|
2022-07-27 20:47:18 +00:00
|
|
|
return a.String(data) < b.String(data)
|
2021-02-21 20:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) Swap(i, j int) {
|
|
|
|
items := ib.items
|
|
|
|
items[i], items[j] = items[j], items[i]
|
2019-09-19 17:13:00 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
type inmemoryBlock struct {
|
2022-04-06 15:48:05 +00:00
|
|
|
// commonPrefix contains common prefix for all the items stored in the inmemoryBlock
|
2019-05-22 21:16:55 +00:00
|
|
|
commonPrefix []byte
|
2022-04-06 15:48:05 +00:00
|
|
|
|
|
|
|
// data contains source data for items
|
|
|
|
data []byte
|
|
|
|
|
|
|
|
// items contains items stored in inmemoryBlock.
|
|
|
|
// Every item contains the prefix specified at commonPrefix.
|
|
|
|
items []Item
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-27 20:47:18 +00:00
|
|
|
func (ib *inmemoryBlock) CopyFrom(src *inmemoryBlock) {
|
|
|
|
ib.commonPrefix = append(ib.commonPrefix[:0], src.commonPrefix...)
|
|
|
|
ib.data = append(ib.data[:0], src.data...)
|
|
|
|
ib.items = append(ib.items[:0], src.items...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) SortItems() {
|
|
|
|
if !ib.isSorted() {
|
|
|
|
ib.updateCommonPrefixUnsorted()
|
|
|
|
sort.Sort(ib)
|
|
|
|
} else {
|
|
|
|
ib.updateCommonPrefixSorted()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 22:34:18 +00:00
|
|
|
func (ib *inmemoryBlock) SizeBytes() int {
|
2021-02-21 20:06:45 +00:00
|
|
|
return int(unsafe.Sizeof(*ib)) + cap(ib.commonPrefix) + cap(ib.data) + cap(ib.items)*int(unsafe.Sizeof(Item{}))
|
2021-02-08 22:34:18 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 21:16:55 +00:00
|
|
|
func (ib *inmemoryBlock) Reset() {
|
|
|
|
ib.commonPrefix = ib.commonPrefix[:0]
|
|
|
|
ib.data = ib.data[:0]
|
|
|
|
ib.items = ib.items[:0]
|
|
|
|
}
|
|
|
|
|
2022-04-06 15:48:05 +00:00
|
|
|
func (ib *inmemoryBlock) updateCommonPrefixSorted() {
|
|
|
|
items := ib.items
|
2024-02-08 11:47:21 +00:00
|
|
|
if len(items) <= 1 {
|
|
|
|
// There is no sense in duplicating a single item or zero items into commonPrefix,
|
|
|
|
// since this only can increase blockHeader size without any benefits.
|
|
|
|
ib.commonPrefix = ib.commonPrefix[:0]
|
2019-05-22 21:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
2024-02-08 11:47:21 +00:00
|
|
|
|
2021-02-21 20:06:45 +00:00
|
|
|
data := ib.data
|
|
|
|
cp := items[0].Bytes(data)
|
2024-02-08 11:47:21 +00:00
|
|
|
cpLen := commonPrefixLen(cp, items[len(items)-1].Bytes(data))
|
|
|
|
cp = cp[:cpLen]
|
2022-04-06 15:48:05 +00:00
|
|
|
ib.commonPrefix = append(ib.commonPrefix[:0], cp...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) updateCommonPrefixUnsorted() {
|
|
|
|
ib.commonPrefix = ib.commonPrefix[:0]
|
|
|
|
items := ib.items
|
|
|
|
if len(items) == 0 {
|
2019-05-22 21:16:55 +00:00
|
|
|
return
|
|
|
|
}
|
2022-04-06 15:48:05 +00:00
|
|
|
data := ib.data
|
|
|
|
cp := items[0].Bytes(data)
|
2021-02-21 20:06:45 +00:00
|
|
|
for _, it := range items[1:] {
|
2022-07-27 20:47:18 +00:00
|
|
|
item := it.Bytes(data)
|
|
|
|
if bytes.HasPrefix(item, cp) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
cpLen := commonPrefixLen(cp, item)
|
2019-05-22 21:16:55 +00: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.
|
|
|
|
//
|
2023-02-13 12:27:13 +00:00
|
|
|
// false is returned if x isn't added to ib due to block size constraints.
|
2019-05-22 21:16:55 +00:00
|
|
|
func (ib *inmemoryBlock) Add(x []byte) bool {
|
2021-02-21 20:06:45 +00:00
|
|
|
data := ib.data
|
|
|
|
if len(x)+len(data) > maxInmemoryBlockSize {
|
2019-05-22 21:16:55 +00:00
|
|
|
return false
|
|
|
|
}
|
2022-01-31 22:56:53 +00: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 21:16:55 +00:00
|
|
|
}
|
2021-02-21 20:06:45 +00: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 21:16:55 +00: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
|
|
|
|
|
|
|
|
// 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 17:13:00 +00:00
|
|
|
// Use sort.IsSorted instead of sort.SliceIsSorted in order to eliminate memory allocation.
|
2021-02-21 20:06:45 +00:00
|
|
|
return sort.IsSorted(ib)
|
2019-05-22 21:16:55 +00: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) {
|
2022-07-27 20:47:18 +00:00
|
|
|
ib.SortItems()
|
2019-05-22 21:16:55 +00:00
|
|
|
return ib.marshalData(sb, firstItemDst, commonPrefixDst, compressLevel)
|
|
|
|
}
|
|
|
|
|
2019-09-22 20:11:57 +00:00
|
|
|
var isInTest = func() bool {
|
|
|
|
return strings.HasSuffix(os.Args[0], ".test")
|
|
|
|
}()
|
2019-09-19 17:13:00 +00:00
|
|
|
|
2020-05-14 20:44:01 +00:00
|
|
|
// MarshalSortedData marshals sorted items from ib to sb.
|
2019-05-22 21:16:55 +00: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 17:13:00 +00:00
|
|
|
if isInTest && !ib.isSorted() {
|
|
|
|
logger.Panicf("BUG: %d items must be sorted; items:\n%s", len(ib.items), ib.debugItemsString())
|
|
|
|
}
|
2022-04-06 15:48:05 +00:00
|
|
|
ib.updateCommonPrefixSorted()
|
2019-05-22 21:16:55 +00:00
|
|
|
return ib.marshalData(sb, firstItemDst, commonPrefixDst, compressLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ib *inmemoryBlock) debugItemsString() string {
|
|
|
|
var sb strings.Builder
|
2021-02-21 20:06:45 +00:00
|
|
|
var prevItem string
|
|
|
|
data := ib.data
|
|
|
|
for i, it := range ib.items {
|
|
|
|
item := it.String(data)
|
|
|
|
if item < prevItem {
|
2019-09-23 17:40:38 +00:00
|
|
|
fmt.Fprintf(&sb, "!!! the next item is smaller than the previous item !!!\n")
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
fmt.Fprintf(&sb, "%05d %X\n", i, item)
|
2019-09-23 17:40:38 +00:00
|
|
|
prevItem = item
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
return sb.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preconditions:
|
|
|
|
// - ib.items must be sorted.
|
2022-04-06 15:48:05 +00:00
|
|
|
// - updateCommonPrefix* must be called.
|
2019-05-22 21:16:55 +00:00
|
|
|
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 20:06:45 +00:00
|
|
|
data := ib.data
|
|
|
|
firstItem := ib.items[0].Bytes(data)
|
|
|
|
firstItemDst = append(firstItemDst, firstItem...)
|
2019-05-22 21:16:55 +00:00
|
|
|
commonPrefixDst = append(commonPrefixDst, ib.commonPrefix...)
|
|
|
|
|
2022-02-24 09:17:36 +00:00
|
|
|
if len(data)-len(ib.commonPrefix)*len(ib.items) < 64 || len(ib.items) < 2 {
|
2019-05-22 21:16:55 +00: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 20:06:45 +00:00
|
|
|
prevItem := firstItem[cpLen:]
|
2019-05-22 21:16:55 +00:00
|
|
|
prevPrefixLen := uint64(0)
|
2021-02-21 20:06:45 +00:00
|
|
|
for i, it := range ib.items[1:] {
|
|
|
|
it.Start += uint32(cpLen)
|
|
|
|
item := it.Bytes(data)
|
2019-05-22 21:16:55 +00: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 20:06:45 +00:00
|
|
|
prevItemLen := uint64(len(firstItem) - cpLen)
|
|
|
|
for i, it := range ib.items[1:] {
|
|
|
|
itemLen := uint64(int(it.End-it.Start) - cpLen)
|
2019-05-22 21:16:55 +00: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)
|
|
|
|
|
2022-02-24 09:17:36 +00:00
|
|
|
if float64(len(sb.itemsData)) > 0.9*float64(len(data)-len(ib.commonPrefix)*len(ib.items)) {
|
2019-05-22 21:16:55 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2022-11-15 14:30:51 +00:00
|
|
|
// UnmarshalData decodes itemsCount items from sb and firstItem and stores them to ib.
|
2019-05-22 21:16:55 +00:00
|
|
|
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 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot unmarshal plain data: %w", err)
|
2019-05-22 21:16:55 +00: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 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot decompress lensData: %w", err)
|
2019-05-22 21:16:55 +00: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 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot unmarshal prefixLens from lensData: %w", err)
|
2019-05-22 21:16:55 +00: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 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot unmarshal lens from lensData: %w", err)
|
2019-05-22 21:16:55 +00: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 16:49:29 +00:00
|
|
|
dataLen := len(commonPrefix) * int(itemsCount)
|
|
|
|
dataLen += int(lens[0])
|
2019-05-22 21:16:55 +00:00
|
|
|
for i, xLen := range is.A {
|
|
|
|
itemLen := xLen ^ lens[i]
|
|
|
|
lens[i+1] = itemLen
|
2022-01-24 16:49:29 +00:00
|
|
|
dataLen += int(itemLen)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal items data.
|
|
|
|
bb.B, err = encoding.DecompressZSTD(bb.B[:0], sb.itemsData)
|
|
|
|
if err != nil {
|
2020-06-30 19:58:18 +00:00
|
|
|
return fmt.Errorf("cannot decompress lensData: %w", err)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
2022-01-24 16:49:29 +00: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 22:18:39 +00:00
|
|
|
data := bytesutil.ResizeNoCopyNoOverallocate(ib.data, dataLen)
|
2019-05-22 21:16:55 +00:00
|
|
|
if n := int(itemsCount) - cap(ib.items); n > 0 {
|
2021-02-21 20:06:45 +00:00
|
|
|
ib.items = append(ib.items[:cap(ib.items)], make([]Item, n)...)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
ib.items = ib.items[:itemsCount]
|
|
|
|
data = append(data[:0], firstItem...)
|
2021-02-21 20:06:45 +00:00
|
|
|
items := ib.items
|
|
|
|
items[0] = Item{
|
|
|
|
Start: 0,
|
|
|
|
End: uint32(len(data)),
|
|
|
|
}
|
2019-05-22 21:16:55 +00: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 16:49:29 +00:00
|
|
|
dataStart := len(data)
|
2021-02-21 20:06:45 +00:00
|
|
|
data = append(data, commonPrefix...)
|
2019-05-22 21:16:55 +00:00
|
|
|
data = append(data, prevItem[:prefixLen]...)
|
|
|
|
data = append(data, b[:suffixLen]...)
|
2021-02-21 20:06:45 +00:00
|
|
|
items[i] = Item{
|
2022-01-24 16:49:29 +00:00
|
|
|
Start: uint32(dataStart),
|
2021-02-21 20:06:45 +00:00
|
|
|
End: uint32(len(data)),
|
|
|
|
}
|
2019-05-22 21:16:55 +00:00
|
|
|
b = b[suffixLen:]
|
2021-02-21 20:06:45 +00:00
|
|
|
prevItem = data[len(data)-int(itemLen):]
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
if len(b) > 0 {
|
|
|
|
return fmt.Errorf("unexpected tail left after itemsData with len %d: %q", len(b), b)
|
|
|
|
}
|
2022-01-24 16:49:29 +00:00
|
|
|
if len(data) != dataLen {
|
2019-05-22 21:16:55 +00:00
|
|
|
return fmt.Errorf("unexpected data len; got %d; want %d", len(data), dataLen)
|
|
|
|
}
|
2021-02-21 20:06:45 +00:00
|
|
|
ib.data = data
|
2019-05-22 21:16:55 +00: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 20:06:45 +00:00
|
|
|
data := ib.data
|
|
|
|
|
2019-05-22 21:16:55 +00: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 20:06:45 +00:00
|
|
|
for _, it := range ib.items[1:] {
|
|
|
|
it.Start += uint32(cpLen)
|
|
|
|
b = append(b, it.String(data)...)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
sb.itemsData = b
|
|
|
|
|
|
|
|
// Marshal length data.
|
|
|
|
b = sb.lensData[:0]
|
2021-02-21 20:06:45 +00:00
|
|
|
for _, it := range ib.items[1:] {
|
|
|
|
b = encoding.MarshalUint64(b, uint64(int(it.End-it.Start)-cpLen))
|
2019-05-22 21:16:55 +00: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 20:06:45 +00:00
|
|
|
data := ib.data
|
|
|
|
items := ib.items
|
2022-01-31 22:18:39 +00:00
|
|
|
dataLen := len(firstItem) + len(sb.itemsData) + len(commonPrefix)*(int(itemsCount)-1)
|
|
|
|
data = bytesutil.ResizeNoCopyNoOverallocate(data, dataLen)
|
2021-02-21 20:06:45 +00:00
|
|
|
data = append(data[:0], firstItem...)
|
|
|
|
items = append(items[:0], Item{
|
|
|
|
Start: 0,
|
|
|
|
End: uint32(len(data)),
|
|
|
|
})
|
2019-05-22 21:16:55 +00: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 22:18:39 +00:00
|
|
|
dataStart := len(data)
|
2021-02-21 20:06:45 +00:00
|
|
|
data = append(data, commonPrefix...)
|
|
|
|
data = append(data, b[:itemLen]...)
|
|
|
|
items = append(items, Item{
|
2022-01-31 22:18:39 +00:00
|
|
|
Start: uint32(dataStart),
|
2021-02-21 20:06:45 +00:00
|
|
|
End: uint32(len(data)),
|
|
|
|
})
|
2019-05-22 21:16:55 +00: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 22:18:39 +00: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 21:16:55 +00: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)
|
|
|
|
}
|