2019-05-22 23:16:55 +02:00
|
|
|
package mergeset
|
|
|
|
|
|
|
|
import (
|
2022-12-06 00:15:00 +01:00
|
|
|
"path/filepath"
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/encoding"
|
2022-12-06 00:15:00 +01:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type inmemoryPart struct {
|
|
|
|
ph partHeader
|
|
|
|
bh blockHeader
|
|
|
|
mr metaindexRow
|
|
|
|
|
|
|
|
metaindexData bytesutil.ByteBuffer
|
|
|
|
indexData bytesutil.ByteBuffer
|
|
|
|
itemsData bytesutil.ByteBuffer
|
|
|
|
lensData bytesutil.ByteBuffer
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:28:39 +02:00
|
|
|
func (mp *inmemoryPart) Reset() {
|
|
|
|
mp.ph.Reset()
|
|
|
|
mp.bh.Reset()
|
|
|
|
mp.mr.Reset()
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2021-07-06 15:28:39 +02:00
|
|
|
mp.metaindexData.Reset()
|
|
|
|
mp.indexData.Reset()
|
|
|
|
mp.itemsData.Reset()
|
|
|
|
mp.lensData.Reset()
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2023-04-14 07:11:56 +02:00
|
|
|
// MustStoreToDisk stores mp to the given path on disk.
|
|
|
|
func (mp *inmemoryPart) MustStoreToDisk(path string) {
|
|
|
|
fs.MustMkdirFailIfExist(path)
|
|
|
|
|
2023-03-25 21:39:38 +01:00
|
|
|
metaindexPath := filepath.Join(path, metaindexFilename)
|
2023-04-14 06:42:11 +02:00
|
|
|
fs.MustWriteSync(metaindexPath, mp.metaindexData.B)
|
2023-04-14 06:33:15 +02:00
|
|
|
|
2023-03-25 21:39:38 +01:00
|
|
|
indexPath := filepath.Join(path, indexFilename)
|
2023-04-14 06:42:11 +02:00
|
|
|
fs.MustWriteSync(indexPath, mp.indexData.B)
|
2023-04-14 06:33:15 +02:00
|
|
|
|
2023-03-25 21:39:38 +01:00
|
|
|
itemsPath := filepath.Join(path, itemsFilename)
|
2023-04-14 06:42:11 +02:00
|
|
|
fs.MustWriteSync(itemsPath, mp.itemsData.B)
|
2023-04-14 06:33:15 +02:00
|
|
|
|
2023-03-25 21:39:38 +01:00
|
|
|
lensPath := filepath.Join(path, lensFilename)
|
2023-04-14 06:42:11 +02:00
|
|
|
fs.MustWriteSync(lensPath, mp.lensData.B)
|
2023-04-14 06:33:15 +02:00
|
|
|
|
|
|
|
mp.ph.MustWriteMetadata(path)
|
|
|
|
|
2023-04-14 06:18:39 +02:00
|
|
|
fs.MustSyncPath(path)
|
|
|
|
// Do not sync parent directory - it must be synced by the caller.
|
2022-12-06 00:15:00 +01:00
|
|
|
}
|
|
|
|
|
2021-07-06 15:28:39 +02:00
|
|
|
// Init initializes mp from ib.
|
|
|
|
func (mp *inmemoryPart) Init(ib *inmemoryBlock) {
|
|
|
|
mp.Reset()
|
2022-03-03 15:46:35 +01:00
|
|
|
|
|
|
|
// Re-use mp.itemsData and mp.lensData in sb.
|
|
|
|
// This eliminates copying itemsData and lensData from sb to mp later.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2247
|
|
|
|
sb := &storageBlock{}
|
|
|
|
sb.itemsData = mp.itemsData.B[:0]
|
|
|
|
sb.lensData = mp.lensData.B[:0]
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
// Use the minimum possible compressLevel for compressing inmemoryPart,
|
|
|
|
// since it will be merged into file part soon.
|
2022-02-25 14:32:27 +01:00
|
|
|
// See https://github.com/facebook/zstd/releases/tag/v1.3.4 for details about negative compression level
|
|
|
|
compressLevel := -5
|
2022-03-03 13:38:03 +01:00
|
|
|
mp.bh.firstItem, mp.bh.commonPrefix, mp.bh.itemsCount, mp.bh.marshalType = ib.MarshalUnsortedData(sb, mp.bh.firstItem[:0], mp.bh.commonPrefix[:0], compressLevel)
|
2021-07-06 15:28:39 +02:00
|
|
|
|
|
|
|
mp.ph.itemsCount = uint64(len(ib.items))
|
|
|
|
mp.ph.blocksCount = 1
|
|
|
|
mp.ph.firstItem = append(mp.ph.firstItem[:0], ib.items[0].String(ib.data)...)
|
|
|
|
mp.ph.lastItem = append(mp.ph.lastItem[:0], ib.items[len(ib.items)-1].String(ib.data)...)
|
|
|
|
|
2022-03-03 15:46:35 +01:00
|
|
|
mp.itemsData.B = sb.itemsData
|
2021-07-06 15:28:39 +02:00
|
|
|
mp.bh.itemsBlockOffset = 0
|
2022-03-03 13:38:03 +01:00
|
|
|
mp.bh.itemsBlockSize = uint32(len(mp.itemsData.B))
|
2021-07-06 15:28:39 +02:00
|
|
|
|
2022-03-03 15:46:35 +01:00
|
|
|
mp.lensData.B = sb.lensData
|
2021-07-06 15:28:39 +02:00
|
|
|
mp.bh.lensBlockOffset = 0
|
2022-03-03 13:38:03 +01:00
|
|
|
mp.bh.lensBlockSize = uint32(len(mp.lensData.B))
|
2021-07-06 15:28:39 +02:00
|
|
|
|
2022-03-03 16:08:41 +01:00
|
|
|
bb := inmemoryPartBytePool.Get()
|
|
|
|
bb.B = mp.bh.Marshal(bb.B[:0])
|
2022-12-04 07:34:43 +01:00
|
|
|
mp.indexData.B = encoding.CompressZSTDLevel(mp.indexData.B[:0], bb.B, compressLevel)
|
2021-07-06 15:28:39 +02:00
|
|
|
|
|
|
|
mp.mr.firstItem = append(mp.mr.firstItem[:0], mp.bh.firstItem...)
|
|
|
|
mp.mr.blockHeadersCount = 1
|
|
|
|
mp.mr.indexBlockOffset = 0
|
2022-03-03 16:08:41 +01:00
|
|
|
mp.mr.indexBlockSize = uint32(len(mp.indexData.B))
|
|
|
|
bb.B = mp.mr.Marshal(bb.B[:0])
|
2022-12-04 07:34:43 +01:00
|
|
|
mp.metaindexData.B = encoding.CompressZSTDLevel(mp.metaindexData.B[:0], bb.B, compressLevel)
|
2022-03-03 16:08:41 +01:00
|
|
|
inmemoryPartBytePool.Put(bb)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
2022-03-03 16:08:41 +01:00
|
|
|
var inmemoryPartBytePool bytesutil.ByteBufferPool
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
// It is safe calling NewPart multiple times.
|
2021-07-06 15:28:39 +02:00
|
|
|
// It is unsafe re-using mp while the returned part is in use.
|
|
|
|
func (mp *inmemoryPart) NewPart() *part {
|
|
|
|
size := mp.size()
|
2023-04-15 00:46:09 +02:00
|
|
|
p := newPart(&mp.ph, "", size, mp.metaindexData.NewReader(), &mp.indexData, &mp.itemsData, &mp.lensData)
|
2019-05-22 23:16:55 +02:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-07-06 15:28:39 +02:00
|
|
|
func (mp *inmemoryPart) size() uint64 {
|
2022-12-04 07:30:30 +01:00
|
|
|
return uint64(cap(mp.metaindexData.B) + cap(mp.indexData.B) + cap(mp.itemsData.B) + cap(mp.lensData.B))
|
2019-07-04 18:09:40 +02:00
|
|
|
}
|