package aggregate

import (
	"math"

	"github.com/pingcap-inc/clinic/pkg/ngm/statements/api"
	"github.com/pingcap-inc/clinic/pkg/ngm/statements/load"
)

func AggMaxTotalKeys() Aggregator {
	return &MaxTotalKeys{math.MinInt64}
}

type MaxTotalKeys struct {
	max int64
}

func (a *MaxTotalKeys) Add(record *load.StmtRecord) {
	if record.MaxTotalKeys > a.max {
		a.max = record.MaxTotalKeys
	}
}

func (a *MaxTotalKeys) Merge(agg Aggregator) {
	if agg, ok := agg.(*MaxTotalKeys); ok {
		if agg.max > a.max {
			a.max = agg.max
		}
	}
}

func (a *MaxTotalKeys) Output(model *api.Model) {
	model.MaxTotalKeys = int(a.max)
}
