summaryrefslogtreecommitdiff
path: root/src/mongo/gotools/mongostat/stat_consumer/formatter.go
blob: 2fb1a4c55066bca5170cba16c1ae4fa5ea7585df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package stat_consumer

import (
	"sync/atomic"

	"github.com/mongodb/mongo-tools/mongostat/stat_consumer/line"
)

// A LineFormatter formats StatLines for printing.
type LineFormatter interface {
	// FormatLines returns the string representation of the StatLines that are passed in.
	FormatLines(lines []*line.StatLine, headerKeys []string, keyNames map[string]string) string

	// IsFinished returns true iff the formatter cannot print any more data
	IsFinished() bool
}

type limitableFormatter struct {
	// atomic operations are performed on rowCount, so these two variables
	// should stay at the beginning for the sake of variable alignment
	maxRows, rowCount int64
}

func (lf *limitableFormatter) increment() {
	atomic.AddInt64(&lf.rowCount, 1)
}

func (lf *limitableFormatter) IsFinished() bool {
	return lf.maxRows > 0 && atomic.LoadInt64(&lf.rowCount) >= lf.maxRows
}

type FormatterConstructor func(maxRows int64, includeHeader bool) LineFormatter

var FormatterConstructors = map[string]FormatterConstructor{}