// CreateShard creates a new shard and tries to fill in the right information. func CreateShard(ts Server, keyspace, shard string) error { name, keyRange, err := ValidateShardName(shard) if err != nil { return err } // start the shard with all serving types. If it overlaps with // other shards for some serving types, remove them. s := &Shard{ KeyRange: keyRange, ServedTypesMap: map[TabletType]*ShardServedType{ TYPE_MASTER: &ShardServedType{}, TYPE_REPLICA: &ShardServedType{}, TYPE_RDONLY: &ShardServedType{}, }, } sis, err := FindAllShardsInKeyspace(ts, keyspace) if err != nil && err != ErrNoNode { return err } for _, si := range sis { if key.KeyRangesIntersect(si.KeyRange, keyRange) { for t, _ := range si.ServedTypesMap { delete(s.ServedTypesMap, t) } } } if len(s.ServedTypesMap) == 0 { s.ServedTypesMap = nil } return ts.CreateShard(keyspace, name, s) }
// intersect returns true if the provided shard intersect with any shard // in the destination array func intersect(si *topo.ShardInfo, allShards []*topo.ShardInfo) bool { for _, shard := range allShards { if key.KeyRangesIntersect(si.KeyRange, shard.KeyRange) { return true } } return false }
// findIntersectingShard will go through the map and take the first // entry in there that intersect with the source array, remove it from // the map, and return it func findIntersectingShard(shardMap map[string]*topo.ShardInfo, sourceArray []*topo.ShardInfo) *topo.ShardInfo { for name, si := range shardMap { for _, sourceShardInfo := range sourceArray { if key.KeyRangesIntersect(si.KeyRange, sourceShardInfo.KeyRange) { delete(shardMap, name) return si } } } return nil }
// This maps a list of keyranges to shard names. func resolveKeyRangeToShards(allShards []topo.SrvShard, kr key.KeyRange) ([]string, error) { shards := make([]string, 0, 1) if !kr.IsPartial() { for j := 0; j < len(allShards); j++ { shards = append(shards, allShards[j].ShardName()) } return shards, nil } for j := 0; j < len(allShards); j++ { shard := allShards[j] if key.KeyRangesIntersect(kr, shard.KeyRange) { shards = append(shards, shard.ShardName()) } } return shards, nil }