vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/oracle_gtid_set_entry.go (about)

     1  /*
     2     Copyright 2015 Shlomi Noach, courtesy Booking.com
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package inst
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  	"strconv"
    23  	"strings"
    24  )
    25  
    26  var (
    27  	singleValueInterval = regexp.MustCompile("^([0-9]+)$")
    28  	multiValueInterval  = regexp.MustCompile("^([0-9]+)[-]([0-9]+)$")
    29  )
    30  
    31  // OracleGtidSetEntry represents an entry in a set of GTID ranges,
    32  // for example, the entry: "316d193c-70e5-11e5-adb2-ecf4bb2262ff:1-8935:8984-6124596" (may include gaps)
    33  type OracleGtidSetEntry struct {
    34  	UUID   string
    35  	Ranges string
    36  }
    37  
    38  // NewOracleGtidSetEntry parses a single entry text
    39  func NewOracleGtidSetEntry(gtidRangeString string) (*OracleGtidSetEntry, error) {
    40  	gtidRangeString = strings.TrimSpace(gtidRangeString)
    41  	tokens := strings.SplitN(gtidRangeString, ":", 2)
    42  	if len(tokens) != 2 {
    43  		return nil, fmt.Errorf("Cannot parse OracleGtidSetEntry from %s", gtidRangeString)
    44  	}
    45  	if tokens[0] == "" {
    46  		return nil, fmt.Errorf("Unexpected UUID: %s", tokens[0])
    47  	}
    48  	if tokens[1] == "" {
    49  		return nil, fmt.Errorf("Unexpected GTID range: %s", tokens[1])
    50  	}
    51  	gtidRange := &OracleGtidSetEntry{UUID: tokens[0], Ranges: tokens[1]}
    52  	return gtidRange, nil
    53  }
    54  
    55  // String returns a user-friendly string representation of this entry
    56  func (oracleGTIDSetEntry *OracleGtidSetEntry) String() string {
    57  	return fmt.Sprintf("%s:%s", oracleGTIDSetEntry.UUID, oracleGTIDSetEntry.Ranges)
    58  }
    59  
    60  // String returns a user-friendly string representation of this entry
    61  func (oracleGTIDSetEntry *OracleGtidSetEntry) Explode() (result [](*OracleGtidSetEntry)) {
    62  	intervals := strings.Split(oracleGTIDSetEntry.Ranges, ":")
    63  	for _, interval := range intervals {
    64  		if submatch := multiValueInterval.FindStringSubmatch(interval); submatch != nil {
    65  			intervalStart, _ := strconv.Atoi(submatch[1])
    66  			intervalEnd, _ := strconv.Atoi(submatch[2])
    67  			for i := intervalStart; i <= intervalEnd; i++ {
    68  				result = append(result, &OracleGtidSetEntry{UUID: oracleGTIDSetEntry.UUID, Ranges: fmt.Sprintf("%d", i)})
    69  			}
    70  		} else if submatch := singleValueInterval.FindStringSubmatch(interval); submatch != nil {
    71  			result = append(result, &OracleGtidSetEntry{UUID: oracleGTIDSetEntry.UUID, Ranges: interval})
    72  		}
    73  	}
    74  	return result
    75  }