Пример #1
0
// CreateRequiredTables ensures all the tables passed in are created if necessary
func CreateRequiredTables(c *gocql.ClusterConfig, rt []CTable) error {
	tl, err := GetTables(c)
	if err != nil {
		return err
	}

	tm := map[string]CTable{}
	rtl := []string{}
	for _, v := range rt {
		tm[v.Name] = v
		rtl = append(rtl, v.Name)
	}
	tset := set.NewStringSet(tl)
	rset := set.NewStringSet(rtl)
	diff := rset.Difference(tset)
	missing := diff.Items()
	if len(missing) > 0 {
		for _, t := range missing {
			ts := tm[t]
			err = CreateTable(c, ts)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
Пример #2
0
// CreateRequiredTypes ensures all the types passed in are created if necessary
func CreateRequiredTypes(c *gocql.ClusterConfig, rt []UDT) error {
	rtn := []string{}
	etn := []string{}
	rtm := map[string]UDT{}
	for _, u := range rt {
		rtn = append(rtn, u.Name)
		rtm[u.Name] = u
	}
	rts := set.NewStringSet(rtn)
	s, err := c.CreateSession()
	if err != nil {
		return err
	}
	q := `SELECT type_name FROM system.schema_usertypes WHERE keyspace_name = '%v';`
	q = fmt.Sprintf(q, c.Keyspace)
	iter := s.Query(q).Iter()
	for n := ""; iter.Scan(&n); {
		etn = append(etn, n)
	}
	if err := iter.Close(); err != nil {
		return err
	}
	ets := set.NewStringSet(etn)
	missing := rts.Difference(ets).Items()
	if len(missing) > 0 {
		for _, mt := range missing {
			err := CreateUDT(c, rtm[mt])
			if err != nil {
				return err
			}
		}
	}
	return nil
}
Пример #3
0
// CreateKeyspaceWithNetworkTopologyStrategy creates a keyspace if necesary with NetworkTopologyStrategy
// ks -> keyspace name
// rfmap -> map of datacenter name to replication factor for that DC
func CreateKeyspaceWithNetworkTopologyStrategy(c *gocql.ClusterConfig, ks string, rfmap map[string]uint) error {
	kis, err := GetKeyspaces(c)
	if err != nil {
		return err
	}
	kss := set.NewStringSet(kis)
	if !kss.Contains(ks) {
		log.Printf("Creating keyspace: %v\n", ks)
		c.Keyspace = ""
		s, err := c.CreateSession()
		if err != nil {
			return err
		}
		defer s.Close()
		q := fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %v WITH REPLICATION = {'class': '%v', ", ks, "NetworkTopologyStrategy")
		rfsl := []string{}
		for dc, rf := range rfmap {
			rfsl = append(rfsl, fmt.Sprintf("'%v' : %v", dc, rf))
		}
		q = fmt.Sprintf("%v%v};", q, strings.Join(rfsl, ", "))
		err = s.Query(q).Exec()
		if err != nil {
			return err
		}
	}
	c.Keyspace = ks
	return nil
}
Пример #4
0
// CreateKeyspace creates a keyspace if necessary
// ks -> keyspace name
// rs -> replication strategy class
// rf -> replication factor
func CreateKeyspace(c *gocql.ClusterConfig, ks string, rs string, rf int) error {
	kis, err := GetKeyspaces(c)
	if err != nil {
		return err
	}
	kss := set.NewStringSet(kis)
	if !kss.Contains(ks) {
		log.Printf("Creating keyspace: %v\n", ks)
		c.Keyspace = ""
		s, err := c.CreateSession()
		if err != nil {
			return err
		}
		defer s.Close()
		if rs == "" {
			rs = "SimpleStrategy"
		}
		err = s.Query(fmt.Sprintf("CREATE KEYSPACE IF NOT EXISTS %v WITH REPLICATION = {'class': '%v', 'replication_factor': %v};", ks, rs, rf)).Exec()
		if err != nil {
			return err
		}
	}
	c.Keyspace = ks
	return nil
}