2019-05-22 21:16:55 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/bytesutil"
|
2020-05-14 19:01:51 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fasttime"
|
2019-05-22 21:16:55 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
)
|
|
|
|
|
|
|
|
// inmemoryPart represents in-memory partition.
|
|
|
|
type inmemoryPart struct {
|
|
|
|
ph partHeader
|
|
|
|
|
|
|
|
timestampsData bytesutil.ByteBuffer
|
|
|
|
valuesData bytesutil.ByteBuffer
|
|
|
|
indexData bytesutil.ByteBuffer
|
|
|
|
metaindexData bytesutil.ByteBuffer
|
|
|
|
|
2020-05-14 19:01:51 +00:00
|
|
|
creationTime uint64
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reset resets mp.
|
|
|
|
func (mp *inmemoryPart) Reset() {
|
|
|
|
mp.ph.Reset()
|
|
|
|
|
|
|
|
mp.timestampsData.Reset()
|
|
|
|
mp.valuesData.Reset()
|
|
|
|
mp.indexData.Reset()
|
|
|
|
mp.metaindexData.Reset()
|
|
|
|
|
2020-05-14 19:01:51 +00:00
|
|
|
mp.creationTime = 0
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// InitFromRows initializes mp from the given rows.
|
|
|
|
func (mp *inmemoryPart) InitFromRows(rows []rawRow) {
|
|
|
|
if len(rows) == 0 {
|
|
|
|
logger.Panicf("BUG: Inmemory.InitFromRows must accept at least one row")
|
|
|
|
}
|
|
|
|
|
|
|
|
mp.Reset()
|
|
|
|
rrm := getRawRowsMarshaler()
|
|
|
|
rrm.marshalToInmemoryPart(mp, rows)
|
|
|
|
putRawRowsMarshaler(rrm)
|
2020-05-14 19:01:51 +00:00
|
|
|
mp.creationTime = fasttime.UnixTimestamp()
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewPart creates new part from mp.
|
|
|
|
//
|
|
|
|
// It is safe calling NewPart multiple times.
|
|
|
|
// It is unsafe re-using mp while the returned part is in use.
|
|
|
|
func (mp *inmemoryPart) NewPart() (*part, error) {
|
|
|
|
ph := mp.ph
|
2019-07-04 16:09:40 +00:00
|
|
|
size := uint64(len(mp.timestampsData.B) + len(mp.valuesData.B) + len(mp.indexData.B) + len(mp.metaindexData.B))
|
|
|
|
return newPart(&ph, "", size, mp.metaindexData.NewReader(), &mp.timestampsData, &mp.valuesData, &mp.indexData)
|
2019-05-22 21:16:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getInmemoryPart() *inmemoryPart {
|
|
|
|
v := mpPool.Get()
|
|
|
|
if v == nil {
|
|
|
|
return &inmemoryPart{}
|
|
|
|
}
|
|
|
|
return v.(*inmemoryPart)
|
|
|
|
}
|
|
|
|
|
|
|
|
func putInmemoryPart(mp *inmemoryPart) {
|
|
|
|
mp.Reset()
|
|
|
|
mpPool.Put(mp)
|
|
|
|
}
|
|
|
|
|
|
|
|
var mpPool sync.Pool
|