当前位置: 首页>>代码示例>>Golang>>正文


Golang Key.Compare方法代码示例

本文整理汇总了Golang中github.com/cockroachdb/cockroach/proto.Key.Compare方法的典型用法代码示例。如果您正苦于以下问题:Golang Key.Compare方法的具体用法?Golang Key.Compare怎么用?Golang Key.Compare使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/cockroachdb/cockroach/proto.Key的用法示例。


在下文中一共展示了Key.Compare方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: MatchByPrefix

// MatchByPrefix returns the longest matching PrefixConfig. If the key
// specified does not match an existing prefix, a panic will
// result. Based on the comments in NewPrefixConfigMap, that example
// will have a final list of PrefixConfig entries which look like:
//
//   "/":          config1
//   "/db1":       config2
//   "/db1/table": config3
//   "/db1/tablf": config2
//   "/db2":       config1
//   "/db3":       config4
//   "/db4":       config1
//
// To find the longest matching prefix, we take the lower bound of the
// specified key.
func (p PrefixConfigMap) MatchByPrefix(key proto.Key) *PrefixConfig {
	n := sort.Search(len(p), func(i int) bool {
		return key.Compare(p[i].Prefix) < 0
	})
	if n == 0 || n > len(p) {
		panic("should never match a key outside of default range")
	}
	// If the matched prefix config is already canonical, return it immediately.
	pc := p[n-1]
	if pc.Canonical == nil {
		return pc
	}
	// Otherwise, search for the canonical prefix config.
	n = sort.Search(len(p), func(i int) bool {
		return pc.Canonical.Compare(p[i].Prefix) <= 0
	})
	// Should find an exact match every time.
	if n >= len(p) || !pc.Canonical.Equal(p[n].Prefix) {
		panic(fmt.Sprintf("canonical lookup for key %q failed", string(pc.Canonical)))
	}
	return p[n]
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:37,代码来源:prefix.go

示例2: VisitPrefixes

// VisitPrefixes invokes the visitor function for each prefix overlapped
// by the specified key range [start, end). If visitor returns done=true
// or an error, the visitation is halted.
func (p PrefixConfigMap) VisitPrefixes(start, end proto.Key,
	visitor func(start, end proto.Key, config gogoproto.Message) (bool, error)) error {
	comp := start.Compare(end)
	if comp > 0 {
		return util.Errorf("start key %q not less than or equal to end key %q", start, end)
	}
	startIdx := sort.Search(len(p), func(i int) bool {
		return start.Compare(p[i].Prefix) < 0
	})
	// Common case of start == end.
	endIdx := startIdx
	if comp != 0 {
		endIdx = sort.Search(len(p), func(i int) bool {
			return end.Compare(p[i].Prefix) < 0
		})
	}

	if startIdx > len(p) || endIdx > len(p) {
		return util.Errorf("start and/or end keys (%q, %q) fall outside prefix range; "+
			"startIdx: %d, endIdx: %d, len(p): %d", start, end, startIdx, endIdx, len(p))
	}

	if startIdx == endIdx {
		_, err := visitor(start, end, p[startIdx-1].Config)
		return err
	}
	for i := startIdx; i < endIdx; i++ {
		done, err := visitor(start, p[i].Prefix, p[i-1].Config)
		if done || err != nil {
			return err
		}
		if p[i].Prefix.Equal(end) {
			return nil
		}
		start = p[i].Prefix
	}
	done, err := visitor(start, end, p[endIdx-1].Config)
	if done || err != nil {
		return err
	}

	return nil
}
开发者ID:nkhuyu,项目名称:cockroach,代码行数:46,代码来源:prefix.go


注:本文中的github.com/cockroachdb/cockroach/proto.Key.Compare方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。