@@ -16,32 +16,39 @@ const (
16
16
CostFlagTrace
17
17
)
18
18
19
+ // CostVer2 is a structure of cost basic of version2
19
20
type CostVer2 struct {
20
21
cost float64
21
22
trace * CostTrace
22
23
}
23
24
25
+ // GetCost returns the cost value of the costVer2
24
26
func (c * CostVer2 ) GetCost () float64 {
25
27
return c .cost
26
28
}
27
29
30
+ // GetTrace returns the trace of current costVer2
28
31
func (c * CostVer2 ) GetTrace () * CostTrace {
29
32
return c .trace
30
33
}
31
34
35
+ // CostTrace record the basic factor and formula in cost est.
32
36
type CostTrace struct {
33
37
factorCosts map [string ]float64 // map[factorName]cost, used to calibrate the cost model
34
38
formula string // It used to trace the cost calculation.
35
39
}
36
40
41
+ // GetFormula return the formula of current costTrace.
37
42
func (c * CostTrace ) GetFormula () string {
38
43
return c .formula
39
44
}
40
45
46
+ // GetFactorCosts return the factors of current costTrace.
41
47
func (c * CostTrace ) GetFactorCosts () map [string ]float64 {
42
48
return c .factorCosts
43
49
}
44
50
51
+ // NewZeroCostVer2 return a new zero costVer2.
45
52
func NewZeroCostVer2 (trace bool ) (ret CostVer2 ) {
46
53
if trace {
47
54
ret .trace = & CostTrace {make (map [string ]float64 ), "" }
@@ -53,14 +60,17 @@ func hasCostFlag(costFlag, flag uint64) bool {
53
60
return (costFlag & flag ) > 0
54
61
}
55
62
63
+ // TraceCost indicates whether to trace cost.
56
64
func TraceCost (option * PlanCostOption ) bool {
57
65
if option != nil && hasCostFlag (option .CostFlag , CostFlagTrace ) {
58
66
return true
59
67
}
60
68
return false
61
69
}
62
70
63
- func NewCostVer2 (option * PlanCostOption , factor CostVer2Factor , cost float64 , lazyFormula func () string ) (ret CostVer2 ) {
71
+ // NewCostVer2 is the constructor of CostVer2.
72
+ func NewCostVer2 (option * PlanCostOption , factor CostVer2Factor , cost float64 ,
73
+ lazyFormula func () string ) (ret CostVer2 ) {
64
74
ret .cost = cost
65
75
if TraceCost (option ) {
66
76
ret .trace = & CostTrace {make (map [string ]float64 ), "" }
@@ -70,15 +80,18 @@ func NewCostVer2(option *PlanCostOption, factor CostVer2Factor, cost float64, la
70
80
return ret
71
81
}
72
82
83
+ // CostVer2Factor is a record of internal cost factor.
73
84
type CostVer2Factor struct {
74
85
Name string
75
86
Value float64
76
87
}
77
88
89
+ // String return the current CostVer2Factor's format string.
78
90
func (f CostVer2Factor ) String () string {
79
91
return fmt .Sprintf ("%s(%v)" , f .Name , f .Value )
80
92
}
81
93
94
+ // SumCostVer2 sum the cost up of all the passed args.
82
95
func SumCostVer2 (costs ... CostVer2 ) (ret CostVer2 ) {
83
96
if len (costs ) == 0 {
84
97
return
@@ -101,6 +114,7 @@ func SumCostVer2(costs ...CostVer2) (ret CostVer2) {
101
114
return ret
102
115
}
103
116
117
+ // DivCostVer2 is div utility func of CostVer2.
104
118
func DivCostVer2 (cost CostVer2 , denominator float64 ) (ret CostVer2 ) {
105
119
ret .cost = cost .cost / denominator
106
120
if cost .trace != nil {
@@ -113,6 +127,7 @@ func DivCostVer2(cost CostVer2, denominator float64) (ret CostVer2) {
113
127
return ret
114
128
}
115
129
130
+ // MulCostVer2 is mul utility func of CostVer2.
116
131
func MulCostVer2 (cost CostVer2 , scale float64 ) (ret CostVer2 ) {
117
132
ret .cost = cost .cost * scale
118
133
if cost .trace != nil {
@@ -125,4 +140,5 @@ func MulCostVer2(cost CostVer2, scale float64) (ret CostVer2) {
125
140
return ret
126
141
}
127
142
143
+ // ZeroCostVer2 is a pre-defined zero CostVer2.
128
144
var ZeroCostVer2 = NewZeroCostVer2 (false )
0 commit comments