Example #1
0
// restore the main proxy data.
func (m *MysqlProxy) InitMain() {
	pr, err := redis.ReadDB("MysqlProxy", "main")
	CheckError(err)
	if len(pr) == 0 {
		return
	}

	for _, proxy := range pr {
		proxy = proxy["main"].(map[string]interface{})
		m.TableTotal = uint64(proxy["TableTotal"].(float64))
		m.SizeTotal = uint64(proxy["SizeTotal"].(float64))
		m.CurGId = uint64(proxy["CurGId"].(float64))

		if ttableIds, isOk := proxy["TableIds"].([]interface{}); isOk && len(ttableIds) > 0 {
			m.TableIds = redis.RestorePrimaryId(ttableIds)
		} else {
			m.TableIds = []string{}
		}

		if dbIds, isOk := proxy["ShardDBIds"].([]interface{}); isOk && len(dbIds) > 0 {
			m.ShardDBIds = redis.RestorePrimaryId(dbIds)
		} else {
			m.ShardDBIds = []string{}
		}

		m.ShardDBCnt = int(proxy["ShardDBCnt"].(float64))
		schema.ShardDBCnt = m.ShardDBCnt
	}

	// panic(fmt.Sprintf("%#v", m))
}
Example #2
0
// to init or restore the table infomation.
func (m *MysqlProxy) InitMysqlTable() {
	if len(m.TableIds) == 0 {
		return
	}

	// 分析数据,并恢复至MysqlProxy结构体中.
	tables := []*schema.MysqlTable{}

	for _, tid := range m.TableIds {
		tbs, err := redis.ReadDB("MysqlTable", tid)
		CheckError(err)
		if len(tbs) != 1 {
			panic("no found relation table for id: " + tid)
		}
		tb := tbs[0][tid].(map[string]interface{})
		// panic(fmt.Sprintf("%#v", tbs))
		shardTbIds := []string{}
		if std, isOk := tb["ShardIds"].([]interface{}); isOk && len(std) > 0 {
			shardTbIds = redis.RestorePrimaryId(std)
		}

		shardTb := []*schema.MysqlShardTable{}

		table := &schema.MysqlTable{
			Id:       tb["Id"].(string),
			Name:     tb["Name"].(string),
			CurGId:   uint64(tb["CurGId"].(float64)),
			RowTotal: uint64(tb["RowTotal"].(float64)),
			ShardIds: shardTbIds,
			Created:  int64(tb["Created"].(float64)),
			Shards:   shardTb,
		}

		if len(shardTbIds) > 0 {
			// create new shard table
			shardTb, err = m.GetShardTableByIds(shardTbIds)
			CheckError(err)
			table.Shards = shardTb

			err = table.RestoreColumnsByDB()
			CheckError(err)
		}

		// fmt.Printf("Init table `%s` done\n", table.Name)
		tables = append(tables, table)
	}

	m.Tables = tables
	schema.Tables = m.Tables
}