vitess.io/vitess@v0.16.2/go/vt/vttablet/tabletmanager/vreplication/queryhistory/expectation_sequence.go (about)

     1  package queryhistory
     2  
     3  // ExpectationSequence represents a temporal ordering of expectations.
     4  type ExpectationSequence interface {
     5  	Count() int
     6  	// Head returns the head of the sequence. A sequence may only have one
     7  	// head.
     8  	Head() SequencedExpectation
     9  	// Visit every expectation in the sequence, in any order.
    10  	Visit(ExpectationSequenceVisitor)
    11  }
    12  
    13  type ExpectationSequenceVisitor func(SequencedExpectation) VisitControl
    14  
    15  type VisitControl int
    16  
    17  type expectationSequence struct {
    18  	head SequencedExpectation
    19  }
    20  
    21  const (
    22  	VisitContinue VisitControl = iota
    23  	VisitTerminate
    24  )
    25  
    26  func (es *expectationSequence) Count() int {
    27  	count := 0
    28  	es.Visit(func(_ SequencedExpectation) VisitControl {
    29  		count++
    30  		return VisitContinue
    31  	})
    32  	return count
    33  }
    34  
    35  func (es *expectationSequence) Head() SequencedExpectation {
    36  	return es.head
    37  }
    38  
    39  func (es *expectationSequence) Visit(visitor ExpectationSequenceVisitor) {
    40  	next := make([]SequencedExpectation, 0)
    41  	visited := make(map[SequencedExpectation]bool)
    42  
    43  	if es.head != nil {
    44  		next = append(next, es.head)
    45  	}
    46  
    47  	for len(next) > 0 {
    48  		se := next[0]
    49  		next = next[1:]
    50  
    51  		if visited[se] {
    52  			continue
    53  		}
    54  
    55  		control := visitor(se)
    56  
    57  		switch control {
    58  		case VisitTerminate:
    59  			return
    60  		default:
    61  			visited[se] = true
    62  		}
    63  
    64  		if se.ImmediatelyBefore() != nil {
    65  			next = append(next, se.ImmediatelyBefore())
    66  		}
    67  
    68  		next = append(next, se.EventuallyBefore().Slice()...)
    69  	}
    70  }