Example #1
0
func (s *Server) parseSchemas() error {
	r, err := router.NewRouter(s.cfg)
	if err != nil {
		return err
	}

	s.schemas = make(map[string]*Schema)

	for _, v := range s.cfg.Schemas {
		if _, ok := s.schemas[v.DB]; ok {
			return fmt.Errorf("duplicate schema %s", v.DB)
		}

		if len(v.Nodes) == 0 {
			return fmt.Errorf("schema %s must have a node", v.DB)
		}

		nodes := make(map[string]*Node)
		for _, n := range v.Nodes {
			if s.getNode(n) == nil {
				return fmt.Errorf("schema %s node %s is not exists", v.DB, n)
			}

			if _, ok := nodes[n]; ok {
				return fmt.Errorf("schema %s node %s duplicate", v.DB, n)
			}

			nodes[n] = s.getNode(n)
		}

		dbRules := r.GetDBRules(v.DB)
		if dbRules == nil {
			if len(v.Nodes) != 1 {
				return fmt.Errorf("schema %s must be set a rule for multi nodes %v", v.DB, v.Nodes)
			} else {
				dbRules = router.NewDefaultDBRules(v.DB, v.Nodes[0])
			}
		}

		for _, v := range dbRules.Rules {
			for _, n := range v.Nodes {
				if s.getNode(n) == nil {
					return fmt.Errorf("rule %s node %s is not exists", v, n)
				}
			}
		}

		if s.getNode(dbRules.DefaultRule.Nodes[0]) == nil {
			return fmt.Errorf("rule %s node %s is not exists", v, dbRules.DefaultRule.Nodes[0])
		}

		s.schemas[v.DB] = &Schema{
			db:    v.DB,
			nodes: nodes,
			rule:  dbRules,
		}
	}

	return nil
}
Example #2
0
func newTestDBRule() *router.DBRules {
	var s = `
rules:
-
    db: mixer
    table: test1
    key: id
    type: hash
    nodes: node(1-10)

-
    db: mixer
    table: test2 
    key: id
    nodes: node1,node2,node3    
    type: range
    # range is -inf-10000 10000-20000 20000-+inf 
    range: -10000-20000-

-   db: mixer
    table: 
    key:
    nodes: node1
    type: default
`

	cfg, err := config.ParseConfigData([]byte(s))
	if err != nil {
		println(err.Error())
		panic(err)
	}

	var r *router.Router

	r, err = router.NewRouter(cfg)
	if err != nil {
		println(err.Error())
		panic(err)
	}

	return r.GetDBRules("mixer")
}