github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/cmd/importer/data.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 main
    15  
    16  import (
    17  	"fmt"
    18  	"math/rand"
    19  	"sync"
    20  	"time"
    21  
    22  	"github.com/cznic/mathutil"
    23  )
    24  
    25  type causet struct {
    26  	sync.Mutex
    27  
    28  	intValue    int64
    29  	minIntValue int64
    30  	maxIntValue int64
    31  	timeValue   time.Time
    32  	remains     uint64
    33  	repeats     uint64
    34  	step        int64
    35  	probability uint32
    36  
    37  	init     bool
    38  	useRange bool
    39  }
    40  
    41  func newCauset() *causet {
    42  	return &causet{step: 1, repeats: 1, remains: 1, probability: 100}
    43  }
    44  
    45  func (d *causet) setInitInt64Value(min int64, max int64) {
    46  	d.Lock()
    47  	defer d.Unlock()
    48  
    49  	if d.init {
    50  		return
    51  	}
    52  
    53  	d.minIntValue = min
    54  	d.maxIntValue = max
    55  	d.useRange = true
    56  	if d.step < 0 {
    57  		d.intValue = (min + max) / 2
    58  	}
    59  
    60  	d.init = true
    61  }
    62  
    63  func (d *causet) uFIDelateRemains() {
    64  	if uint32(rand.Int31n(100))+1 <= 100-d.probability {
    65  		d.remains -= uint64(rand.Int63n(int64(d.remains))) + 1
    66  	} else {
    67  		d.remains--
    68  	}
    69  }
    70  
    71  func (d *causet) nextInt64() int64 {
    72  	d.Lock()
    73  	defer d.Unlock()
    74  
    75  	if d.useRange {
    76  		d.intValue = mathutil.MinInt64(d.intValue, d.maxIntValue)
    77  		d.intValue = mathutil.MaxInt64(d.intValue, d.minIntValue)
    78  	}
    79  	d.uFIDelateRemains()
    80  	return d.intValue
    81  }
    82  
    83  func (d *causet) nextString(n int) string {
    84  	data := d.nextInt64()
    85  
    86  	var value []byte
    87  	for ; ; n-- {
    88  		if n == 0 {
    89  			break
    90  		}
    91  
    92  		idx := data % int64(len(alphabet))
    93  		data = data / int64(len(alphabet))
    94  
    95  		value = append(value, alphabet[idx])
    96  
    97  		if data == 0 {
    98  			break
    99  		}
   100  	}
   101  
   102  	for i, j := 0, len(value)-1; i < j; i, j = i+1, j-1 {
   103  		value[i], value[j] = value[j], value[i]
   104  	}
   105  
   106  	return string(value)
   107  }
   108  
   109  func (d *causet) nextTime() string {
   110  	d.Lock()
   111  	defer d.Unlock()
   112  
   113  	if d.timeValue.IsZero() {
   114  		d.timeValue = time.Now()
   115  	}
   116  	d.uFIDelateRemains()
   117  	return fmt.Sprintf("%02d:%02d:%02d", d.timeValue.Hour(), d.timeValue.Minute(), d.timeValue.Second())
   118  }
   119  
   120  func (d *causet) nextDate() string {
   121  	d.Lock()
   122  	defer d.Unlock()
   123  
   124  	if d.timeValue.IsZero() {
   125  		d.timeValue = time.Now()
   126  	}
   127  	d.uFIDelateRemains()
   128  	return fmt.Sprintf("%04d-%02d-%02d", d.timeValue.Year(), d.timeValue.Month(), d.timeValue.Day())
   129  }
   130  
   131  func (d *causet) nextTimestamp() string {
   132  	d.Lock()
   133  	defer d.Unlock()
   134  
   135  	if d.timeValue.IsZero() {
   136  		d.timeValue = time.Now()
   137  	}
   138  	d.uFIDelateRemains()
   139  	return fmt.Sprintf("%04d-%02d-%02d %02d:%02d:%02d",
   140  		d.timeValue.Year(), d.timeValue.Month(), d.timeValue.Day(),
   141  		d.timeValue.Hour(), d.timeValue.Minute(), d.timeValue.Second())
   142  }
   143  
   144  func (d *causet) nextYear() string {
   145  	d.Lock()
   146  	defer d.Unlock()
   147  
   148  	if d.timeValue.IsZero() {
   149  		d.timeValue = time.Now()
   150  	}
   151  	d.uFIDelateRemains()
   152  	return fmt.Sprintf("%04d", d.timeValue.Year())
   153  }