Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.89 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4.   "flag"
  5.   "fmt"
  6.  
  7.   "io/ioutil"
  8.   "database/sql"
  9.   _ "github.com/go-sql-driver/mysql"
  10.   "gopkg.in/yaml.v2"
  11. )
  12.  
  13. // DbCredentials represents MySQL credentials. It is also part of yaml config`
  14. type DbCredentials struct {
  15.     Driver      string
  16.     User        string
  17.     Pass        string
  18.     Port        int
  19.     Host        string
  20.     Name        string
  21. }
  22.  
  23. // Index is part of yaml configuration
  24. type Index struct {
  25.     MinSize     int         `yaml:"min_size"`
  26.     IndexSum    []string    `yaml:"index_sum"`
  27. }
  28.  
  29. // Config is a root structure of yaml of config
  30. type Config struct {
  31.     Parameters  DbCredentials
  32.     Indexes     map[string]Index
  33. }
  34.  
  35. func dbConnection(credentials *DbCredentials) (db *sql.DB) {
  36.     dns := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s", credentials.User, credentials.Pass, credentials.Host, credentials.Port, credentials.Name)
  37.     db, err := sql.Open("mysql", dns)
  38.  
  39.     if err != nil {
  40.         panic(err.Error())
  41.     }
  42.  
  43.     return db
  44. }
  45.  
  46. func parseConfig(configPath string) Config {
  47.     configData, err := ioutil.ReadFile(configPath)
  48.     if err != nil {
  49.         panic("Cannot read config file from "+configPath)
  50.     }
  51.    
  52.     conf := Config{}
  53.     if err = yaml.Unmarshal([]byte(configData), &conf); err != nil {
  54.         panic(err)
  55.     }
  56.  
  57.     return conf
  58. }
  59.  
  60. func countIndex(db *sql.DB, name string) int {
  61.     rows, err := db.Query("SELECT COUNT(*) FROM "+name)
  62.     if err != nil {
  63.         panic(err)
  64.     }
  65.     defer rows.Close();
  66.  
  67.     for rows.Next() {
  68.         var num int
  69.         if err := rows.Scan(&num); err != nil {
  70.             panic(err)
  71.         }
  72.        
  73.         return num
  74.     }
  75.  
  76.     panic("Index not found")
  77. }
  78.  
  79. func checkIndexes(db *sql.DB, indexes *map[string]Index) {
  80.     count := map[string]int{}
  81.  
  82.     for k, v := range *indexes {
  83.         count[k] = countIndex(db, k)
  84.         if v.MinSize > 0 && count[k] < v.MinSize {
  85.             panic(fmt.Sprintf("Index %s is too small. Expected size: atleast %d. Given %d",
  86.                 k, v.MinSize, count[k]))
  87.         }
  88.     }
  89.  
  90.     var sum int
  91.     for k, v := range *indexes {
  92.         if len(v.IndexSum) <= 0 {
  93.             continue
  94.         }
  95.         sum = 0
  96.  
  97.         for _, item := range v.IndexSum {
  98.             if val, ok := count[item]; ok {
  99.                 sum += val
  100.                 continue
  101.             }
  102.  
  103.             panic(fmt.Sprintf("Index %s required to compute sum for %s is not defined.",
  104.                 item, k))
  105.         }
  106.  
  107.         if sum != count[k] {
  108.             panic(fmt.Sprintf("Sum for index %s is invalid", k))
  109.         }
  110.     }
  111. }
  112.  
  113. func main() {
  114.     var configPath string
  115.    
  116.     flag.StringVar(&configPath, "config", "config.yml", "Path to configuration file")
  117.     flag.Parse()
  118.  
  119.     config := parseConfig(configPath)
  120.     var db *sql.DB = dbConnection(&config.Parameters)
  121.     defer db.Close()
  122.  
  123.     checkIndexes(db, &config.Indexes)
  124.  
  125.    
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement