github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/printer/printer.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 printer
    15  
    16  import (
    17  	"bytes"
    18  	"encoding/json"
    19  	"fmt"
    20  	_ "runtime" // import link package
    21  	_ "unsafe"  // required by go:linkname
    22  
    23  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    24  	"github.com/whtcorpsinc/milevadb/config"
    25  	"github.com/whtcorpsinc/milevadb/soliton/israce"
    26  	"github.com/whtcorpsinc/milevadb/soliton/logutil"
    27  	"github.com/whtcorpsinc/milevadb/soliton/versioninfo"
    28  	"go.uber.org/zap"
    29  )
    30  
    31  // PrintMilevaDBInfo prints the MilevaDB version information.
    32  func PrintMilevaDBInfo() {
    33  	logutil.BgLogger().Info("Welcome to MilevaDB.",
    34  		zap.String("Release Version", allegrosql.MilevaDBReleaseVersion),
    35  		zap.String("Edition", versioninfo.MilevaDBEdition),
    36  		zap.String("Git Commit Hash", versioninfo.MilevaDBGitHash),
    37  		zap.String("Git Branch", versioninfo.MilevaDBGitBranch),
    38  		zap.String("UTC Build Time", versioninfo.MilevaDBBuildTS),
    39  		zap.String("GoVersion", buildVersion),
    40  		zap.Bool("Race Enabled", israce.RaceEnabled),
    41  		zap.Bool("Check Block Before Drop", config.CheckBlockBeforeDrop),
    42  		zap.String("EinsteinDB Min Version", versioninfo.EinsteinDBMinVersion))
    43  	configJSON, err := json.Marshal(config.GetGlobalConfig())
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  	logutil.BgLogger().Info("loaded config", zap.ByteString("config", configJSON))
    48  }
    49  
    50  // GetMilevaDBInfo returns the git hash and build time of this milevadb-server binary.
    51  func GetMilevaDBInfo() string {
    52  	return fmt.Sprintf("Release Version: %s\n"+
    53  		"Edition: %s\n"+
    54  		"Git Commit Hash: %s\n"+
    55  		"Git Branch: %s\n"+
    56  		"UTC Build Time: %s\n"+
    57  		"GoVersion: %s\n"+
    58  		"Race Enabled: %v\n"+
    59  		"EinsteinDB Min Version: %s\n"+
    60  		"Check Block Before Drop: %v",
    61  		allegrosql.MilevaDBReleaseVersion,
    62  		versioninfo.MilevaDBEdition,
    63  		versioninfo.MilevaDBGitHash,
    64  		versioninfo.MilevaDBGitBranch,
    65  		versioninfo.MilevaDBBuildTS,
    66  		buildVersion,
    67  		israce.RaceEnabled,
    68  		versioninfo.EinsteinDBMinVersion,
    69  		config.CheckBlockBeforeDrop)
    70  }
    71  
    72  // checkValidity checks whether defcaus and every data have the same length.
    73  func checkValidity(defcaus []string, quantum [][]string) bool {
    74  	defCausLen := len(defcaus)
    75  	if len(quantum) == 0 || defCausLen == 0 {
    76  		return false
    77  	}
    78  
    79  	for _, data := range quantum {
    80  		if defCausLen != len(data) {
    81  			return false
    82  		}
    83  	}
    84  
    85  	return true
    86  }
    87  
    88  func getMaxDeferCausetLen(defcaus []string, quantum [][]string) []int {
    89  	maxDefCausLen := make([]int, len(defcaus))
    90  	for i, defCaus := range defcaus {
    91  		maxDefCausLen[i] = len(defCaus)
    92  	}
    93  
    94  	for _, data := range quantum {
    95  		for i, v := range data {
    96  			if len(v) > maxDefCausLen[i] {
    97  				maxDefCausLen[i] = len(v)
    98  			}
    99  		}
   100  	}
   101  
   102  	return maxDefCausLen
   103  }
   104  
   105  func getPrintDivLine(maxDefCausLen []int) []byte {
   106  	var value = make([]byte, 0)
   107  	for _, v := range maxDefCausLen {
   108  		value = append(value, '+')
   109  		value = append(value, bytes.Repeat([]byte{'-'}, v+2)...)
   110  	}
   111  	value = append(value, '+')
   112  	value = append(value, '\n')
   113  	return value
   114  }
   115  
   116  func getPrintDefCaus(defcaus []string, maxDefCausLen []int) []byte {
   117  	var value = make([]byte, 0)
   118  	for i, v := range defcaus {
   119  		value = append(value, '|')
   120  		value = append(value, ' ')
   121  		value = append(value, []byte(v)...)
   122  		value = append(value, bytes.Repeat([]byte{' '}, maxDefCausLen[i]+1-len(v))...)
   123  	}
   124  	value = append(value, '|')
   125  	value = append(value, '\n')
   126  	return value
   127  }
   128  
   129  func getPrintEvent(data []string, maxDefCausLen []int) []byte {
   130  	var value = make([]byte, 0)
   131  	for i, v := range data {
   132  		value = append(value, '|')
   133  		value = append(value, ' ')
   134  		value = append(value, []byte(v)...)
   135  		value = append(value, bytes.Repeat([]byte{' '}, maxDefCausLen[i]+1-len(v))...)
   136  	}
   137  	value = append(value, '|')
   138  	value = append(value, '\n')
   139  	return value
   140  }
   141  
   142  func getPrintEvents(quantum [][]string, maxDefCausLen []int) []byte {
   143  	var value = make([]byte, 0)
   144  	for _, data := range quantum {
   145  		value = append(value, getPrintEvent(data, maxDefCausLen)...)
   146  	}
   147  	return value
   148  }
   149  
   150  // GetPrintResult gets a result with a formatted string.
   151  func GetPrintResult(defcaus []string, quantum [][]string) (string, bool) {
   152  	if !checkValidity(defcaus, quantum) {
   153  		return "", false
   154  	}
   155  
   156  	var value = make([]byte, 0)
   157  	maxDefCausLen := getMaxDeferCausetLen(defcaus, quantum)
   158  
   159  	value = append(value, getPrintDivLine(maxDefCausLen)...)
   160  	value = append(value, getPrintDefCaus(defcaus, maxDefCausLen)...)
   161  	value = append(value, getPrintDivLine(maxDefCausLen)...)
   162  	value = append(value, getPrintEvents(quantum, maxDefCausLen)...)
   163  	value = append(value, getPrintDivLine(maxDefCausLen)...)
   164  	return string(value), true
   165  }
   166  
   167  //go:linkname buildVersion runtime.buildVersion
   168  var buildVersion string