package aggregate

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

func AggPlanInCache() Aggregator {
	return &PlanInCache{}
}

type PlanInCache struct {
	v  bool
	ts int64
}

var _ Aggregator = (*PlanInCache)(nil)

func (a *PlanInCache) Add(record *load.StmtRecord) {
	if record.Begin > a.ts {
		// use latest value
		a.v = record.PlanInCache
		a.ts = record.Begin
	}
}

func (a *PlanInCache) Merge(agg Aggregator) {
	if agg, ok := agg.(*PlanInCache); ok {
		if agg.ts > a.ts {
			// use latest value
			a.v = agg.v
			a.ts = agg.ts
		}
	}
}

func (a *PlanInCache) Output(model *api.Model) {
	model.PlanInCache = a.v
}
