90 Star 491 Fork 151

平凯星辰(北京)科技有限公司/tidb

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
topn.go 3.05 KB
一键复制 编辑 原始数据 按行查看 历史
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package mocktikv
import (
"container/heap"
"github.com/juju/errors"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/util/types"
"github.com/pingcap/tipb/go-tipb"
)
type sortRow struct {
key []types.Datum
data [][]byte
}
// topNSorter implements sort.Interface. When all rows have been processed, the topNSorter will sort the whole data in heap.
type topNSorter struct {
orderByItems []*tipb.ByItem
rows []*sortRow
err error
sc *variable.StatementContext
}
func (t *topNSorter) Len() int {
return len(t.rows)
}
func (t *topNSorter) Swap(i, j int) {
t.rows[i], t.rows[j] = t.rows[j], t.rows[i]
}
func (t *topNSorter) Less(i, j int) bool {
for index, by := range t.orderByItems {
v1 := t.rows[i].key[index]
v2 := t.rows[j].key[index]
ret, err := v1.CompareDatum(t.sc, &v2)
if err != nil {
t.err = errors.Trace(err)
return true
}
if by.Desc {
ret = -ret
}
if ret < 0 {
return true
} else if ret > 0 {
return false
}
}
return false
}
// topNHeap holds the top n elements using heap structure. It implements heap.Interface.
// When we insert a row, topNHeap will check if the row can become one of the top n element or not.
type topNHeap struct {
topNSorter
// totalCount is equal to the limit count, which means the max size of heap.
totalCount int
// heapSize means the current size of this heap.
heapSize int
}
func (t *topNHeap) Len() int {
return t.heapSize
}
func (t *topNHeap) Push(x interface{}) {
t.rows = append(t.rows, x.(*sortRow))
t.heapSize++
}
func (t *topNHeap) Pop() interface{} {
return nil
}
func (t *topNHeap) Less(i, j int) bool {
for index, by := range t.orderByItems {
v1 := t.rows[i].key[index]
v2 := t.rows[j].key[index]
ret, err := v1.CompareDatum(t.sc, &v2)
if err != nil {
t.err = errors.Trace(err)
return true
}
if by.Desc {
ret = -ret
}
if ret > 0 {
return true
} else if ret < 0 {
return false
}
}
return false
}
// tryToAddRow tries to add a row to heap.
// When this row is not less than any rows in heap, it will never become the top n element.
// Then this function returns false.
func (t *topNHeap) tryToAddRow(row *sortRow) bool {
success := false
if t.heapSize == t.totalCount {
t.rows = append(t.rows, row)
// When this row is less than the top element, it will replace it and adjust the heap structure.
if t.Less(0, t.heapSize) {
t.Swap(0, t.heapSize)
heap.Fix(t, 0)
success = true
}
t.rows = t.rows[:t.heapSize]
} else {
heap.Push(t, row)
success = true
}
return success
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/pingcap/tidb.git
git@gitee.com:pingcap/tidb.git
pingcap
tidb
tidb
v1.0.8

搜索帮助

0d507c66 1850385 C8b1a773 1850385