github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/results/blockresults/segheap.go (about)

     1  /*
     2  Copyright 2023.
     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 blockresults
    18  
    19  import (
    20  	. "github.com/siglens/siglens/pkg/segment/utils"
    21  )
    22  
    23  type ResultRecordSort struct {
    24  	Index     int
    25  	Rrc       *RecordResultContainer
    26  	Ascending bool
    27  }
    28  
    29  type SortedResultRecords []*ResultRecordSort
    30  
    31  func (srr SortedResultRecords) Len() int { return len(srr) }
    32  
    33  func (srr SortedResultRecords) Less(i, j int) bool {
    34  
    35  	if !srr[i].Ascending {
    36  		// We want Pop to give us the highest priority so we use greater than here.
    37  		return srr[i].Rrc.SortColumnValue > srr[j].Rrc.SortColumnValue
    38  	} else {
    39  		// We want Pop to give us the lowest priority so we use less than here.
    40  		return srr[i].Rrc.SortColumnValue < srr[j].Rrc.SortColumnValue
    41  	}
    42  }
    43  
    44  func (pq SortedResultRecords) Swap(i, j int) {
    45  	pq[i], pq[j] = pq[j], pq[i]
    46  	pq[i].Index = i
    47  	pq[j].Index = j
    48  }
    49  
    50  func (pq *SortedResultRecords) Push(x interface{}) {
    51  	n := len(*pq)
    52  	item := x.(*ResultRecordSort)
    53  	item.Index = n
    54  	*pq = append(*pq, item)
    55  }
    56  
    57  func (pq *SortedResultRecords) Pop() interface{} {
    58  	old := *pq
    59  	n := len(old)
    60  	item := old[n-1]
    61  	old[n-1] = nil  // avoid memory leak
    62  	item.Index = -1 // for safety
    63  	*pq = old[0 : n-1]
    64  	return item
    65  }