<commit_before>/*
 * Copyright 2019 The Vitess Authors.

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *     http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"regexp"
	"strconv"
	"strings"

	jsonpatch "github.com/evanphx/json-patch"
	yamlpatch "github.com/krishicks/yaml-patch"

	"vitess.io/vitess/go/vt/log"
)

const (
	DefaultWebPort        = 8080
	DefaultGrpcPort       = 15999
	DefaultMysqlPort      = 15306
	DefaultKeyspaceData   = "test_keyspace:2:1:create_messages.sql,create_tokens.sql;unsharded_keyspace:0:0:create_dinosaurs.sql,create_eggs.sql"
	DefaultCell           = "test"
	DefaultExternalDbData = ""
	DefaultTopologyFlags  = "-topo_implementation consul -topo_global_server_address consul1:8500 -topo_global_root vitess/global"

	topologyFlagsUsage  = "Vitess Topology Flags config"
	webPortUsage        = "Web port to be used"
	gRpcPortUsage       = "gRPC port to be used"
	mySqlPortUsage      = "mySql port to be used"
	cellUsage           = "Vitess Cell name"
	keyspaceDataUsage   = "List of keyspace_name/external_db_name:num_of_shards:num_of_replica_tablets:schema_files:<optional>lookup_keyspace_name separated by ';'"
	externalDbDataUsage = "List of Data corresponding to external DBs. List of <external_db_name>,<DB_HOST>,<DB_PORT>,<DB_USER>,<DB_PASS>,<DB_CHARSET> separated by ';'"
)

var (
	tabletsUsed           = 0
	tablesPath            = "tables/"
	baseDockerComposeFile = flag.String("base_yaml", "vtcompose/docker-compose.base.yml", "Starting docker-compose yaml")
	baseVschemaFile       = flag.String("base_vschema", "vtcompose/base_vschema.json", "Starting vschema json")

	topologyFlags  = flag.String("topologyFlags", DefaultTopologyFlags, topologyFlagsUsage)
	webPort        = flag.Int("webPort", DefaultWebPort, webPortUsage)
	gRpcPort       = flag.Int("gRpcPort", DefaultGrpcPort, gRpcPortUsage)
	mySqlPort      = flag.Int("mySqlPort", DefaultMysqlPort, mySqlPortUsage)
	cell           = flag.String("cell", DefaultCell, cellUsage)
	keyspaceData   = flag.String("keyspaceData", DefaultKeyspaceData, keyspaceDataUsage)
	externalDbData = flag.String("externalDbData", DefaultExternalDbData, externalDbDataUsage)
)

type keyspaceInfo struct {
	keyspace        string
	shards          int
	replicaTablets  int
	lookupKeyspace  string
	useLookups      bool
	schemaFile      *os.File
	schemaFileNames []string
}

type externalDbInfo struct {
	dbName    string
	dbHost    string
	dbPort    string
	dbUser    string
	dbPass    string
	dbCharset string
}

type vtOptions struct {
	webPort       int
	gRpcPort      int
	mySqlPort     int
	topologyFlags string
	cell          string
}

func newKeyspaceInfo(
	keyspace string,
	shards int,
	replicaTablets int,
	schemaFiles []string,
	lookupKeyspace string,
) keyspaceInfo {
	k := keyspaceInfo{
		keyspace:        keyspace,
		shards:          shards,
		replicaTablets:  replicaTablets,
		schemaFileNames: schemaFiles,
		lookupKeyspace:  lookupKeyspace,
	}
	if len(strings.TrimSpace(lookupKeyspace)) == 0 {
		k.useLookups = false
	} else {
		k.useLookups = true
	}

	k.schemaFile = nil
	return k
}

func parseKeyspaceInfo(keyspaceData string) map[string]keyspaceInfo {
	keyspaceInfoMap := make(map[string]keyspaceInfo)

	for _, v := range strings.Split(keyspaceData, ";") {
		tokens := strings.Split(v, ":")
		shards, _ := strconv.Atoi(tokens[1])
		replicaTablets, _ := strconv.Atoi(tokens[2])
		schemaFileNames := strings.Split(tokens[3], ",")
		print(shards)

		if len(tokens) > 4 {
			keyspaceInfoMap[tokens[0]] = newKeyspaceInfo(tokens[0], shards, replicaTablets, schemaFileNames, tokens[4])
		} else {
			keyspaceInfoMap[tokens[0]] = newKeyspaceInfo(tokens[0], shards, replicaTablets, schemaFileNames, "")
		}
	}

	return keyspaceInfoMap
}

func newExternalDbInfo(dbName, dbHost, dbPort, dbUser, dbPass, dbCharset string) externalDbInfo {
	return externalDbInfo{dbName: dbName, dbHost: dbHost, dbPort: dbPort, dbUser: dbUser, dbPass: dbPass, dbCharset: dbCharset}
}

func parseExternalDbData(externalDbData string) map[string]externalDbInfo {
	externalDbInfoMap := make(map[string]externalDbInfo)
	for _, v := range strings.Split(externalDbData, ";") {
		tokens := strings.Split(v, ":")
		if len(tokens) > 1 {
			externalDbInfoMap[tokens[0]] = newExternalDbInfo(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5])
		}
	}

	return externalDbInfoMap
}

func main() {
	flag.Parse()
	keyspaceInfoMap := parseKeyspaceInfo(*keyspaceData)
	externalDbInfoMap := parseExternalDbData(*externalDbData)
	vtOpts := vtOptions{
		webPort:       *webPort,
		gRpcPort:      *gRpcPort,
		mySqlPort:     *mySqlPort,
		topologyFlags: *topologyFlags,
		cell:          *cell,
	}

	// Write schemaFile.<commit_msg>Move some declarations<commit_after>/*
 * Copyright 2019 The Vitess Authors.

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *     http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"math"
	"os"
	"regexp"
	"strconv"
	"strings"

	jsonpatch "github.com/evanphx/json-patch"
	yamlpatch "github.com/krishicks/yaml-patch"

	"vitess.io/vitess/go/vt/log"
)

const (
	DefaultWebPort        = 8080
	DefaultGrpcPort       = 15999
	DefaultMysqlPort      = 15306
	DefaultKeyspaceData   = "test_keyspace:2:1:create_messages.sql,create_tokens.sql;unsharded_keyspace:0:0:create_dinosaurs.sql,create_eggs.sql"
	DefaultCell           = "test"
	DefaultExternalDbData = ""
	DefaultTopologyFlags  = "-topo_implementation consul -topo_global_server_address consul1:8500 -topo_global_root vitess/global"

	topologyFlagsUsage  = "Vitess Topology Flags config"
	webPortUsage        = "Web port to be used"
	gRpcPortUsage       = "gRPC port to be used"
	mySqlPortUsage      = "mySql port to be used"
	cellUsage           = "Vitess Cell name"
	keyspaceDataUsage   = "List of keyspace_name/external_db_name:num_of_shards:num_of_replica_tablets:schema_files:<optional>lookup_keyspace_name separated by ';'"
	externalDbDataUsage = "List of Data corresponding to external DBs. List of <external_db_name>,<DB_HOST>,<DB_PORT>,<DB_USER>,<DB_PASS>,<DB_CHARSET> separated by ';'"
)

var (
	tabletsUsed           = 0
	tablesPath            = "tables/"
	baseDockerComposeFile = flag.String("base_yaml", "vtcompose/docker-compose.base.yml", "Starting docker-compose yaml")
	baseVschemaFile       = flag.String("base_vschema", "vtcompose/base_vschema.json", "Starting vschema json")

	topologyFlags  = flag.String("topologyFlags", DefaultTopologyFlags, topologyFlagsUsage)
	webPort        = flag.Int("webPort", DefaultWebPort, webPortUsage)
	gRpcPort       = flag.Int("gRpcPort", DefaultGrpcPort, gRpcPortUsage)
	mySqlPort      = flag.Int("mySqlPort", DefaultMysqlPort, mySqlPortUsage)
	cell           = flag.String("cell", DefaultCell, cellUsage)
	keyspaceData   = flag.String("keyspaceData", DefaultKeyspaceData, keyspaceDataUsage)
	externalDbData = flag.String("externalDbData", DefaultExternalDbData, externalDbDataUsage)
)

type vtOptions struct {
	webPort       int
	gRpcPort      int
	mySqlPort     int
	topologyFlags string
	cell          string
}

type keyspaceInfo struct {
	keyspace        string
	shards          int
	replicaTablets  int
	lookupKeyspace  string
	useLookups      bool
	schemaFile      *os.File
	schemaFileNames []string
}

type externalDbInfo struct {
	dbName    string
	dbHost    string
	dbPort    string
	dbUser    string
	dbPass    string
	dbCharset string
}

func newKeyspaceInfo(
	keyspace string,
	shards int,
	replicaTablets int,
	schemaFiles []string,
	lookupKeyspace string,
) keyspaceInfo {
	k := keyspaceInfo{
		keyspace:        keyspace,
		shards:          shards,
		replicaTablets:  replicaTablets,
		schemaFileNames: schemaFiles,
		lookupKeyspace:  lookupKeyspace,
	}
	if len(strings.TrimSpace(lookupKeyspace)) == 0 {
		k.useLookups = false
	} else {
		k.useLookups = true
	}

	k.schemaFile = nil
	return k
}

func newExternalDbInfo(dbName, dbHost, dbPort, dbUser, dbPass, dbCharset string) externalDbInfo {
	return externalDbInfo{dbName: dbName, dbHost: dbHost, dbPort: dbPort, dbUser: dbUser, dbPass: dbPass, dbCharset: dbCharset}
}

func parseKeyspaceInfo(keyspaceData string) map[string]keyspaceInfo {
	keyspaceInfoMap := make(map[string]keyspaceInfo)

	for _, v := range strings.Split(keyspaceData, ";") {
		tokens := strings.Split(v, ":")
		shards, _ := strconv.Atoi(tokens[1])
		replicaTablets, _ := strconv.Atoi(tokens[2])
		schemaFileNames := strings.Split(tokens[3], ",")
		print(shards)

		if len(tokens) > 4 {
			keyspaceInfoMap[tokens[0]] = newKeyspaceInfo(tokens[0], shards, replicaTablets, schemaFileNames, tokens[4])
		} else {
			keyspaceInfoMap[tokens[0]] = newKeyspaceInfo(tokens[0], shards, replicaTablets, schemaFileNames, "")
		}
	}

	return keyspaceInfoMap
}

func parseExternalDbData(externalDbData string) map[string]externalDbInfo {
	externalDbInfoMap := make(map[string]externalDbInfo)
	for _, v := range strings.Split(externalDbData, ";") {
		tokens := strings.Split(v, ":")
		if len(tokens) > 1 {
			externalDbInfoMap[tokens[0]] = newExternalDbInfo(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5])
		}
	}

	return externalDbInfoMap
}

func main() {
	flag.Parse()
	keyspaceInfoMap := parseKeyspaceInfo(*keyspaceData)
	externalDbInfoMap := parseExternalDbData(*externalDbData)
	vtOpts := vtOptions{
		webPort:       *webPort,
		gRpcPort:      *gRpcPort,
		mySqlPort:     *mySqlPort,
		topologyFlags: *topologyFlags,
		cell:          *cell,
	}

	// Write schemaFile.
{'fraction_non_alphanumeric': 0.10293134727394251, 'fraction_numerical': 0.009548362455838824, 'mean_word_length': 4.303291139240506, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4214686', 'n_tokens_mistral': 3647, 'n_tokens_neox': 3501, 'n_words': 795}