github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/merging_iter_heap.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package bitalosdb
    16  
    17  type mergingIterItem struct {
    18  	index int
    19  	key   InternalKey
    20  	value []byte
    21  }
    22  
    23  type mergingIterHeap struct {
    24  	cmp     Compare
    25  	reverse bool
    26  	items   []mergingIterItem
    27  }
    28  
    29  func (h *mergingIterHeap) len() int {
    30  	return len(h.items)
    31  }
    32  
    33  func (h *mergingIterHeap) clear() {
    34  	h.items = h.items[:0]
    35  }
    36  
    37  func (h *mergingIterHeap) less(i, j int) bool {
    38  	ikey, jkey := h.items[i].key, h.items[j].key
    39  	if c := h.cmp(ikey.UserKey, jkey.UserKey); c != 0 {
    40  		if h.reverse {
    41  			return c > 0
    42  		}
    43  		return c < 0
    44  	}
    45  	if h.reverse {
    46  		return ikey.Trailer < jkey.Trailer
    47  	}
    48  	return ikey.Trailer > jkey.Trailer
    49  }
    50  
    51  func (h *mergingIterHeap) swap(i, j int) {
    52  	h.items[i], h.items[j] = h.items[j], h.items[i]
    53  }
    54  
    55  func (h *mergingIterHeap) init() {
    56  	n := h.len()
    57  	for i := n/2 - 1; i >= 0; i-- {
    58  		h.down(i, n)
    59  	}
    60  }
    61  
    62  func (h *mergingIterHeap) fix(i int) {
    63  	if !h.down(i, h.len()) {
    64  		h.up(i)
    65  	}
    66  }
    67  
    68  func (h *mergingIterHeap) pop() *mergingIterItem {
    69  	n := h.len() - 1
    70  	h.swap(0, n)
    71  	h.down(0, n)
    72  	item := &h.items[n]
    73  	h.items = h.items[:n]
    74  	return item
    75  }
    76  
    77  func (h *mergingIterHeap) up(j int) {
    78  	for {
    79  		i := (j - 1) / 2
    80  		if i == j || !h.less(j, i) {
    81  			break
    82  		}
    83  		h.swap(i, j)
    84  		j = i
    85  	}
    86  }
    87  
    88  func (h *mergingIterHeap) down(i0, n int) bool {
    89  	i := i0
    90  	for {
    91  		j1 := 2*i + 1
    92  		if j1 >= n || j1 < 0 {
    93  			break
    94  		}
    95  		j := j1
    96  		if j2 := j1 + 1; j2 < n && h.less(j2, j1) {
    97  			j = j2
    98  		}
    99  		if !h.less(j, i) {
   100  			break
   101  		}
   102  		h.swap(i, j)
   103  		i = j
   104  	}
   105  	return i > i0
   106  }