vitess.io/vitess@v0.16.2/go/vt/vtorc/inst/oracle_gtid_set.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  	"strings"
    21  )
    22  
    23  // OracleGtidSet represents a set of GTID ranges as depicted by Retrieved_Gtid_Set, Executed_Gtid_Set or @@gtid_purged.
    24  type OracleGtidSet struct {
    25  	GtidEntries [](*OracleGtidSetEntry)
    26  }
    27  
    28  // Example input:  `230ea8ea-81e3-11e4-972a-e25ec4bd140a:1-10539,
    29  // 316d193c-70e5-11e5-adb2-ecf4bb2262ff:1-8935:8984-6124596,
    30  // 321f5c0d-70e5-11e5-adb2-ecf4bb2262ff:1-56`
    31  func NewOracleGtidSet(gtidSet string) (res *OracleGtidSet, err error) {
    32  	res = &OracleGtidSet{}
    33  
    34  	gtidSet = strings.TrimSpace(gtidSet)
    35  	if gtidSet == "" {
    36  		return res, nil
    37  	}
    38  	entries := strings.Split(gtidSet, ",")
    39  	for _, entry := range entries {
    40  		entry = strings.TrimSpace(entry)
    41  		if entry == "" {
    42  			continue
    43  		}
    44  		if gtidRange, err := NewOracleGtidSetEntry(entry); err == nil {
    45  			res.GtidEntries = append(res.GtidEntries, gtidRange)
    46  		} else {
    47  			return res, err
    48  		}
    49  	}
    50  	return res, nil
    51  }
    52  
    53  // RemoveUUID removes entries that belong to given UUID.
    54  // By way of how this works there can only be one entry matching our UUID, but we generalize.
    55  // We keep order of entries.
    56  func (oracleGTIDSet *OracleGtidSet) RemoveUUID(uuid string) (removed bool) {
    57  	filteredEntries := [](*OracleGtidSetEntry){}
    58  	for _, entry := range oracleGTIDSet.GtidEntries {
    59  		if entry.UUID == uuid {
    60  			removed = true
    61  		} else {
    62  			filteredEntries = append(filteredEntries, entry)
    63  		}
    64  	}
    65  	if removed {
    66  		oracleGTIDSet.GtidEntries = filteredEntries
    67  	}
    68  	return removed
    69  }
    70  
    71  // RetainUUID retains only entries that belong to given UUID.
    72  func (oracleGTIDSet *OracleGtidSet) RetainUUID(uuid string) (anythingRemoved bool) {
    73  	return oracleGTIDSet.RetainUUIDs([]string{uuid})
    74  }
    75  
    76  // RetainUUIDs retains only entries that belong to given UUIDs.
    77  func (oracleGTIDSet *OracleGtidSet) RetainUUIDs(uuids []string) (anythingRemoved bool) {
    78  	retainUUIDs := map[string]bool{}
    79  	for _, uuid := range uuids {
    80  		retainUUIDs[uuid] = true
    81  	}
    82  	filteredEntries := [](*OracleGtidSetEntry){}
    83  	for _, entry := range oracleGTIDSet.GtidEntries {
    84  		if retainUUIDs[entry.UUID] {
    85  			filteredEntries = append(filteredEntries, entry)
    86  		} else {
    87  			anythingRemoved = true
    88  		}
    89  	}
    90  	if anythingRemoved {
    91  		oracleGTIDSet.GtidEntries = filteredEntries
    92  	}
    93  	return anythingRemoved
    94  }
    95  
    96  // SharedUUIDs returns UUIDs (range-less) that are shared between the two sets
    97  func (oracleGTIDSet *OracleGtidSet) SharedUUIDs(other *OracleGtidSet) (shared []string) {
    98  	thisUUIDs := map[string]bool{}
    99  	for _, entry := range oracleGTIDSet.GtidEntries {
   100  		thisUUIDs[entry.UUID] = true
   101  	}
   102  	for _, entry := range other.GtidEntries {
   103  		if thisUUIDs[entry.UUID] {
   104  			shared = append(shared, entry.UUID)
   105  		}
   106  	}
   107  	return shared
   108  }
   109  
   110  // String returns a user-friendly string representation of this entry
   111  func (oracleGTIDSet *OracleGtidSet) Explode() (result [](*OracleGtidSetEntry)) {
   112  	for _, entries := range oracleGTIDSet.GtidEntries {
   113  		result = append(result, entries.Explode()...)
   114  	}
   115  	return result
   116  }
   117  
   118  func (oracleGTIDSet *OracleGtidSet) String() string {
   119  	tokens := []string{}
   120  	for _, entry := range oracleGTIDSet.GtidEntries {
   121  		tokens = append(tokens, entry.String())
   122  	}
   123  	return strings.Join(tokens, ",")
   124  }
   125  
   126  func (oracleGTIDSet *OracleGtidSet) IsEmpty() bool {
   127  	return len(oracleGTIDSet.GtidEntries) == 0
   128  }