package storage

import "gordenko.dev/dima/qb"

var (

	//PageNoSize    = 4

	// data page
	DataPageSize = 8192

	DataPagePayloadSize int = DataPageSize - DataPageFooterSize

	dataCRC32Idx      = DataPageSize - 4
	timestampsSizeIdx = DataPageSize - 6
	valuesSizeIdx     = DataPageSize - 8
	prevPageIdx       = DataPageSize - 12

	// index page
	IndexPageSize = 1024

	//indexPageIncSize     = IndexPageIncSize
	indexCRC32Idx        = IndexPageSize - 4
	indexRecordsCountIdx = IndexPageSize - 6
	isZeroLevelIdx       = IndexPageSize - 7

	maxRecordsOnIndexPage = (IndexPageSize - IndexPageFooterSize) / IndexRecordSize

	// timestampSize  = 4
	// pairSize       = timestampSize + PageNoSize
	// indexFooterIdx = indexRecordsQtyIdx
	// dataFooterIdx  = timestampsSizeIdx
)

const (
	DataPageFooterSize  = 12
	IndexPageFooterSize = 7
	IndexRecordSize     = 8
)

type IndexRecord struct {
	Timestamp uint32
	PageNo    uint32
}

// TASKS FROM WORKER

type MetricAdd struct {
	MetricID   uint32
	MetricType qb.MetricType
	FracDigits byte
	ResultCh   chan struct{}
}

type MetricDelete struct {
	MetricID       uint32
	FreeIndexPages []uint32
	FreeDataPages  []uint32
	ResultCh       chan struct{}
}

type MeasuresAppendWithGrow struct {
	MetricID               uint32
	LastPageNo             uint32
	TimestampsRewindOffset int // offset on 1st page
	Timestamps             []byte
	ValuesRewindOffset     int // offset on 1st page
	Values                 []byte
	DataPages              []DataPayload
	TailTimestamps         []byte // data level tail
	TailValues             []byte // data level tail
	IndexLevelTails        []IndexLevelTail
	ResultCode             byte
	WrittenCount           int
	ResultCh               chan struct{}
}

type MeasuresAppend struct {
	MetricID               uint32
	TimestampsRewindOffset int // offset on 1st page
	Timestamps             []byte
	ValuesRewindOffset     int // offset on 1st page
	Values                 []byte
	ResultCode             byte
	WrittenCount           int
	ResultCh               chan struct{}
}

type MeasuresDelete struct {
	MetricID       uint32
	FreeIndexPages []uint32
	FreeDataPages  []uint32
	ResultCh       chan struct{}
}

// WRITE RESULTS

// результат
type MeasuresAppendCommited struct {
	MetricID     uint32
	ResultCode   byte
	WrittenCount int
	ResultCh     chan struct{}
}

type MeasuresAppendWithGrowCommited struct {
	MetricID     uint32
	LastPageNo   uint32
	Index        []IndexLevelTail
	ResultCode   byte
	WrittenCount int
	ResultCh     chan struct{}
}

type MeasuresDeleteCommited struct {
	MetricID uint32
	ResultCh chan struct{}
}

type MetricAddCommited struct {
	MetricID uint32
	ResultCh chan struct{}
}

type MetricDeleteCommited struct {
	MetricID uint32
	ResultCh chan struct{}
}
