github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/integration_tests/cdc/dailytest/data.go (about) 1 // Copyright 2020 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package dailytest 15 16 import ( 17 "fmt" 18 "sync" 19 "time" 20 ) 21 22 var defaultStep int64 = 1 23 24 type datum struct { 25 sync.Mutex 26 27 intValue int64 28 minIntValue int64 29 maxIntValue int64 30 timeValue time.Time 31 step int64 32 33 init bool 34 useRange bool 35 } 36 37 func newDatum() *datum { 38 return &datum{intValue: -1, step: 1} 39 } 40 41 func (d *datum) setInitInt64Value(step int64, min int64, max int64) { 42 d.Lock() 43 defer d.Unlock() 44 45 if d.init { 46 return 47 } 48 49 d.step = step 50 51 if min != -1 { 52 d.minIntValue = min 53 d.intValue = min 54 } 55 56 if min < max { 57 d.maxIntValue = max 58 d.useRange = true 59 } 60 61 d.init = true 62 } 63 64 func (d *datum) uniqInt64() int64 { 65 d.Lock() 66 defer d.Unlock() 67 68 data := d.intValue 69 if d.useRange { 70 if d.intValue+d.step > d.maxIntValue { 71 return data 72 } 73 } 74 75 d.intValue += d.step 76 return data 77 } 78 79 func (d *datum) uniqString(n int) string { 80 d.Lock() 81 d.intValue++ 82 data := d.intValue 83 d.Unlock() 84 85 var value []byte 86 for ; ; n-- { 87 if n == 0 { 88 break 89 } 90 91 idx := data % int64(len(alphabet)) 92 data = data / int64(len(alphabet)) 93 94 value = append(value, alphabet[idx]) 95 96 if data == 0 { 97 break 98 } 99 } 100 101 for i, j := 0, len(value)-1; i < j; i, j = i+1, j-1 { 102 value[i], value[j] = value[j], value[i] 103 } 104 105 return string(value) 106 } 107 108 func (d *datum) uniqTime() string { 109 d.Lock() 110 defer d.Unlock() 111 112 if d.timeValue.IsZero() { 113 d.timeValue = time.Now() 114 } else { 115 d.timeValue = d.timeValue.Add(time.Duration(d.step) * time.Second) 116 } 117 118 return fmt.Sprintf("%02d:%02d:%02d", d.timeValue.Hour(), d.timeValue.Minute(), d.timeValue.Second()) 119 } 120 121 func (d *datum) uniqDate() string { 122 d.Lock() 123 defer d.Unlock() 124 125 if d.timeValue.IsZero() { 126 d.timeValue = time.Now() 127 } else { 128 d.timeValue = d.timeValue.AddDate(0, 0, int(d.step)) 129 } 130 131 return fmt.Sprintf("%04d-%02d-%02d", d.timeValue.Year(), d.timeValue.Month(), d.timeValue.Day()) 132 } 133 134 func (d *datum) uniqTimestamp() string { 135 d.Lock() 136 defer d.Unlock() 137 138 if d.timeValue.IsZero() { 139 d.timeValue = time.Now() 140 } else { 141 d.timeValue = d.timeValue.Add(time.Duration(d.step) * time.Second) 142 } 143 144 return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d", 145 d.timeValue.Year(), d.timeValue.Month(), d.timeValue.Day(), 146 d.timeValue.Hour(), d.timeValue.Minute(), d.timeValue.Second()) 147 } 148 149 func (d *datum) uniqYear() string { 150 d.Lock() 151 defer d.Unlock() 152 153 if d.timeValue.IsZero() { 154 d.timeValue = time.Now() 155 } else { 156 d.timeValue = d.timeValue.AddDate(int(d.step), 0, 0) 157 } 158 159 return fmt.Sprintf("%04d", d.timeValue.Year()) 160 }