github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/puller/sorter/heap.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package sorter
    15  
    16  import "github.com/pingcap/ticdc/cdc/model"
    17  
    18  type sortItem struct {
    19  	entry *model.PolymorphicEvent
    20  	data  interface{}
    21  }
    22  
    23  type sortHeap []*sortItem
    24  
    25  func (h sortHeap) Len() int { return len(h) }
    26  func (h sortHeap) Less(i, j int) bool {
    27  	if h[i].entry.CRTs == h[j].entry.CRTs {
    28  		if h[j].entry.RawKV.OpType == model.OpTypeResolved && h[i].entry.RawKV.OpType != model.OpTypeResolved {
    29  			return true
    30  		}
    31  		if h[i].entry.RawKV.OpType == model.OpTypeDelete && h[j].entry.RawKV.OpType != model.OpTypeDelete {
    32  			return true
    33  		}
    34  	}
    35  	return h[i].entry.CRTs < h[j].entry.CRTs
    36  }
    37  func (h sortHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
    38  func (h *sortHeap) Push(x interface{}) {
    39  	*h = append(*h, x.(*sortItem))
    40  }
    41  
    42  func (h *sortHeap) Pop() interface{} {
    43  	old := *h
    44  	n := len(old)
    45  	x := old[n-1]
    46  	old[n-1] = nil
    47  	*h = old[0 : n-1]
    48  	return x
    49  }