本文整理汇总了Golang中github.com/cockroachdb/cockroach/roachpb.Key类的典型用法代码示例。如果您正苦于以下问题:Golang Key类的具体用法?Golang Key怎么用?Golang Key使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Key类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: getDescriptors
// getDescriptors looks up the range descriptor to use for a query over the
// key range [from,to), with the given lookupOptions. The range descriptor
// which contains the range in which the request should start its query is
// returned first; the returned bool is true in case the given range reaches
// outside the first descriptor.
// In case either of the descriptors is discovered stale, the returned closure
// should be called; it evicts the cache appropriately.
// Note that `from` and `to` are not necessarily Key and EndKey from a
// RequestHeader; it's assumed that they've been translated to key addresses
// already (via KeyAddress).
func (ds *DistSender) getDescriptors(from, to roachpb.Key, options lookupOptions) (*roachpb.RangeDescriptor, bool, func(), *roachpb.Error) {
var desc *roachpb.RangeDescriptor
var err error
var descKey roachpb.Key
if !options.useReverseScan {
descKey = from
} else {
descKey = to
}
desc, err = ds.rangeCache.LookupRangeDescriptor(descKey, options)
if err != nil {
return nil, false, nil, roachpb.NewError(err)
}
// Checks whether need to get next range descriptor. If so, returns true.
needAnother := func(desc *roachpb.RangeDescriptor, isReverse bool) bool {
if isReverse {
return from.Less(desc.StartKey)
}
return desc.EndKey.Less(to)
}
evict := func() {
ds.rangeCache.EvictCachedRangeDescriptor(descKey, desc, options.useReverseScan)
}
return desc, needAnother(desc, options.useReverseScan), evict, nil
}
示例2: Add
// Add the specified timestamp to the cache as covering the range of
// keys from start to end. If end is nil, the range covers the start
// key only. txnID is nil for no transaction. readOnly specifies
// whether the command adding this timestamp was read-only or not.
func (tc *TimestampCache) Add(start, end roachpb.Key, timestamp roachpb.Timestamp, txnID []byte, readOnly bool) {
// This gives us a memory-efficient end key if end is empty.
if len(end) == 0 {
end = start.Next()
start = end[:len(start)]
}
if tc.latest.Less(timestamp) {
tc.latest = timestamp
}
// Only add to the cache if the timestamp is more recent than the
// low water mark.
if tc.lowWater.Less(timestamp) {
// Check existing, overlapping entries. Remove superseded
// entries or return without adding this entry if necessary.
key := tc.cache.NewKey(start, end)
for _, o := range tc.cache.GetOverlaps(start, end) {
ce := o.Value.(cacheEntry)
if ce.readOnly != readOnly {
continue
}
if o.Key.Contains(key) && !ce.timestamp.Less(timestamp) {
return // don't add this key; there's already a cache entry with >= timestamp.
} else if key.Contains(o.Key) && !timestamp.Less(ce.timestamp) {
tc.cache.Del(o.Key) // delete existing key; this cache entry supersedes.
}
}
ce := cacheEntry{timestamp: timestamp, txnID: txnID, readOnly: readOnly}
tc.cache.Add(key, ce)
}
}
示例3: clearOverlappingCachedRangeDescriptors
// clearOverlappingCachedRangeDescriptors looks up and clears any
// cache entries which overlap the specified key or descriptor.
func (rdc *rangeDescriptorCache) clearOverlappingCachedRangeDescriptors(key, metaKey roachpb.Key, desc *roachpb.RangeDescriptor) {
if desc.StartKey.Equal(desc.EndKey) { // True for some unittests.
return
}
// Clear out any descriptors which subsume the key which we're going
// to cache. For example, if an existing KeyMin->KeyMax descriptor
// should be cleared out in favor of a KeyMin->"m" descriptor.
k, v, ok := rdc.rangeCache.Ceil(rangeCacheKey(metaKey))
if ok {
descriptor := v.(*roachpb.RangeDescriptor)
if !key.Less(descriptor.StartKey) && !descriptor.EndKey.Less(key) {
if log.V(1) {
log.Infof("clearing overlapping descriptor: key=%s desc=%s", k, descriptor)
}
rdc.rangeCache.Del(k.(rangeCacheKey))
}
}
// Also clear any descriptors which are subsumed by the one we're
// going to cache. This could happen on a merge (and also happens
// when there's a lot of concurrency). Iterate from the range meta key
// after RangeMetaKey(desc.StartKey) to the range meta key for desc.EndKey.
rdc.rangeCache.DoRange(func(k, v interface{}) {
if log.V(1) {
log.Infof("clearing subsumed descriptor: key=%s desc=%s", k, v.(*roachpb.RangeDescriptor))
}
rdc.rangeCache.Del(k.(rangeCacheKey))
}, rangeCacheKey(keys.RangeMetaKey(desc.StartKey).Next()),
rangeCacheKey(keys.RangeMetaKey(desc.EndKey)))
}
示例4: prev
// prev gives the right boundary of the union of all requests which don't
// affect keys larger than the given key.
// TODO(tschottdorf): again, better on BatchRequest itself, but can't pull
// 'keys' into 'proto'.
func prev(ba roachpb.BatchRequest, k roachpb.Key) roachpb.Key {
candidate := roachpb.KeyMin
for _, union := range ba.Requests {
h := union.GetInner().Header()
addr := keys.KeyAddress(h.Key)
eAddr := keys.KeyAddress(h.EndKey)
if len(eAddr) == 0 {
// Can probably avoid having to compute Next() here if
// we're in the mood for some more complexity.
eAddr = addr.Next()
}
if !eAddr.Less(k) {
if !k.Less(addr) {
// Range contains k, so won't be able to go lower.
return k
}
// Range is disjoint from [KeyMin,k).
continue
}
// We want the largest surviving candidate.
if candidate.Less(addr) {
candidate = addr
}
}
return candidate
}
示例5: prettyPrintInternal
// prettyPrintInternal parse key with prefix in keyDict,
// if the key don't march any prefix in keyDict, return its byte value with quotation and false,
// or else return its human readable value and true.
func prettyPrintInternal(key roachpb.Key) (string, bool) {
var buf bytes.Buffer
for _, k := range keyDict {
if key.Compare(k.start) >= 0 && (k.end == nil || key.Compare(k.end) <= 0) {
buf.WriteString(k.name)
if k.end != nil && k.end.Compare(key) == 0 {
buf.WriteString("/Max")
return buf.String(), true
}
hasPrefix := false
for _, e := range k.entries {
if bytes.HasPrefix(key, e.prefix) {
hasPrefix = true
key = key[len(e.prefix):]
fmt.Fprintf(&buf, "%s%s", e.name, e.ppFunc(key))
break
}
}
if !hasPrefix {
key = key[len(k.start):]
fmt.Fprintf(&buf, "/%q", []byte(key))
}
return buf.String(), true
}
}
return fmt.Sprintf("%q", []byte(key)), false
}
示例6: addKeyRange
// addKeyRange adds the specified key range to the interval cache,
// taking care not to add this range if existing entries already
// completely cover the range.
func (tm *txnMetadata) addKeyRange(start, end roachpb.Key) {
// This gives us a memory-efficient end key if end is empty.
// The most common case for keys in the intents interval map
// is for single keys. However, the interval cache requires
// a non-empty interval, so we create two key slices which
// share the same underlying byte array.
if len(end) == 0 {
end = start.Next()
start = end[:len(start)]
}
key := tm.keys.MakeKey(start, end)
for _, o := range tm.keys.GetOverlaps(key.Start, key.End) {
if o.Key.Contains(key) {
return
} else if key.Contains(*o.Key) {
tm.keys.Del(o.Key)
}
}
// Since no existing key range fully covered this range, add it now. The
// strange assignment to pkey makes sure we delay the heap allocation until
// we know it is necessary.
alloc := struct {
key cache.IntervalKey
entry cache.Entry
}{key: key}
alloc.entry.Key = &alloc.key
tm.keys.AddEntry(&alloc.entry)
}
示例7: encodeInclusiveEndValue
// Encodes datum at the end of key, using direction `dir` for the encoding.
// The key is a span end key, which is exclusive, but `val` needs to
// be inclusive. So if datum is the last end constraint, we transform it accordingly.
func encodeInclusiveEndValue(
key roachpb.Key, datum parser.Datum, dir encoding.Direction,
isLastEndConstraint bool) roachpb.Key {
// Since the end of a span is exclusive, if the last constraint is an
// inclusive one, we might need to make the key exclusive by applying a
// PrefixEnd(). We normally avoid doing this by transforming "a = x" to
// "a = x±1" for the last end constraint, depending on the encoding direction
// (since this keeps the key nice and pretty-printable).
// However, we might not be able to do the ±1.
needExclusiveKey := false
if isLastEndConstraint {
if dir == encoding.Ascending {
if datum.IsMax() {
needExclusiveKey = true
} else {
datum = datum.Next()
}
} else {
if datum.IsMin() || !datum.HasPrev() {
needExclusiveKey = true
} else {
datum = datum.Prev()
}
}
}
key, pErr := encodeTableKey(key, datum, dir)
if pErr != nil {
panic(pErr)
}
if needExclusiveKey {
key = key.PrefixEnd()
}
return key
}
示例8: GetIndex
// GetIndex searches the kv list for 'key' and returns its index if found.
func (s SystemConfig) GetIndex(key roachpb.Key) (int, bool) {
l := len(s.Values)
index := sort.Search(l, func(i int) bool {
return bytes.Compare(s.Values[i].Key, key) >= 0
})
if index == l || !key.Equal(s.Values[index].Key) {
return 0, false
}
return index, true
}
示例9: prettyKey
// prettyKey pretty-prints the specified key, skipping over the first `skip`
// fields. The pretty printed key looks like:
//
// /Table/<tableID>/<indexID>/...
//
// We always strip off the /Table prefix and then `skip` more fields. Note that
// this assumes that the fields themselves do not contain '/', but that is
// currently true for the fields we care about stripping (the table and index
// ID).
func prettyKey(key roachpb.Key, skip int) string {
p := key.String()
for i := 0; i <= skip; i++ {
n := strings.IndexByte(p[1:], '/')
if n == -1 {
return ""
}
p = p[n+1:]
}
return p
}
示例10: ComputeSplitKeys
// ComputeSplitKeys takes a start and end key and returns an array of keys
// at which to split the span [start, end).
// The only required splits are at each user table prefix.
func (s *SystemConfig) ComputeSplitKeys(startKey, endKey roachpb.Key) []roachpb.Key {
if TestingDisableTableSplits {
return nil
}
tableStart := roachpb.Key(keys.UserTableDataMin)
if !tableStart.Less(endKey) {
// This range is before the user tables span: no required splits.
return nil
}
startID, ok := ObjectIDForKey(startKey)
if !ok || startID <= keys.MaxReservedDescID {
// The start key is either:
// - not part of the structured data span
// - part of the system span
// In either case, start looking for splits at the first ID usable
// by the user data span.
startID = keys.MaxReservedDescID + 1
} else {
// The start key is either already a split key, or after the split
// key for its ID. We can skip straight to the next one.
startID++
}
// Find the largest object ID.
// We can't keep splitting until we reach endKey as it could be roachpb.KeyMax.
endID, err := s.GetLargestObjectID()
if err != nil {
log.Errorf("unable to determine largest object ID from system config: %s", err)
return nil
}
// Build key prefixes for sequential table IDs until we reach endKey.
var splitKeys roachpb.KeySlice
var key roachpb.Key
// endID could be smaller than startID if we don't have user tables.
for id := startID; id <= endID; id++ {
key = keys.MakeTablePrefix(id)
// Skip if the range starts on a split key.
if !startKey.Less(key) {
continue
}
// Handle the case where EndKey is already a table prefix.
if !key.Less(endKey) {
break
}
splitKeys = append(splitKeys, key)
}
return splitKeys
}
示例11: GetIndex
// GetIndex searches the kv list for 'key' and returns its index if found.
func (s *SystemConfig) GetIndex(key roachpb.Key) (int, bool) {
if s == nil {
return 0, false
}
l := len(s.Values)
index := sort.Search(l, func(i int) bool {
return !s.Values[i].Key.Less(key)
})
if index == l || !key.Equal(s.Values[index].Key) {
return 0, false
}
return index, true
}
示例12: verifyBinarySearchTree
// verifyBinarySearchTree checks to ensure that all keys to the left of the root
// node are less than it, and all nodes to the right of the root node are
// greater than it. It recursively walks the tree to perform this same check.
func verifyBinarySearchTree(t *testing.T, nodes map[string]roachpb.RangeTreeNode, testName string, node *roachpb.RangeTreeNode, keyMin, keyMax roachpb.Key) {
if node == nil {
return
}
if !node.Key.Less(keyMax) {
t.Errorf("%s: Failed Property BST - The key %s is not less than %s.", testName, node.Key, keyMax)
}
// We need the extra check since roachpb.KeyMin is actually a range start key.
if !keyMin.Less(node.Key) && !node.Key.Equal(roachpb.KeyMin) {
t.Errorf("%s: Failed Property BST - The key %s is not greater than %s.", testName, node.Key, keyMin)
}
left, right := getLeftAndRight(t, nodes, testName, node)
verifyBinarySearchTree(t, nodes, testName, left, keyMin, node.Key)
verifyBinarySearchTree(t, nodes, testName, right, node.Key, keyMax)
}
示例13: addKeyRange
// addKeyRange adds the specified key range to the range group,
// taking care not to add this range if existing entries already
// completely cover the range.
func addKeyRange(keys interval.RangeGroup, start, end roachpb.Key) {
// This gives us a memory-efficient end key if end is empty.
// The most common case for keys in the intents interval map
// is for single keys. However, the range group requires
// a non-empty interval, so we create two key slices which
// share the same underlying byte array.
if len(end) == 0 {
end = start.Next()
start = end[:len(start)]
}
keyR := interval.Range{
Start: interval.Comparable(start),
End: interval.Comparable(end),
}
keys.Add(keyR)
}
示例14: check
// TODO(dt): Batch checks of many rows.
func (f baseFKHelper) check(values parser.DTuple) (parser.DTuple, error) {
var key roachpb.Key
if values != nil {
keyBytes, _, err := sqlbase.EncodeIndexKey(f.searchIdx, f.ids, values, f.searchPrefix)
if err != nil {
return nil, err
}
key = roachpb.Key(keyBytes)
} else {
key = roachpb.Key(f.searchPrefix)
}
spans := sqlbase.Spans{sqlbase.Span{Start: key, End: key.PrefixEnd()}}
if err := f.rf.StartScan(f.txn, spans, 1); err != nil {
return nil, err
}
return f.rf.NextRow()
}
示例15: GetMax
// GetMax returns the maximum read and write timestamps which overlap
// the interval spanning from start to end. Cached timestamps matching
// the specified txnID are not considered. If no part of the specified
// range is overlapped by timestamps in the cache, the low water
// timestamp is returned for both read and write timestamps.
//
// The txn ID prevents restarts with a pattern like: read("a"),
// write("a"). The read adds a timestamp for "a". Then the write (for
// the same transaction) would get that as the max timestamp and be
// forced to increment it. This allows timestamps from the same txn
// to be ignored.
func (tc *TimestampCache) GetMax(start, end roachpb.Key, txnID []byte) (roachpb.Timestamp, roachpb.Timestamp) {
if len(end) == 0 {
end = start.Next()
}
maxR := tc.lowWater
maxW := tc.lowWater
for _, o := range tc.cache.GetOverlaps(start, end) {
ce := o.Value.(*cacheValue)
if ce.txnID == nil || txnID == nil || !roachpb.TxnIDEqual(txnID, ce.txnID) {
if ce.readOnly && maxR.Less(ce.timestamp) {
maxR = ce.timestamp
} else if !ce.readOnly && maxW.Less(ce.timestamp) {
maxW = ce.timestamp
}
}
}
return maxR, maxW
}