本文整理匯總了Golang中github.com/cockroachdb/cockroach/proto.Key類的典型用法代碼示例。如果您正苦於以下問題:Golang Key類的具體用法?Golang Key怎麽用?Golang Key使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Key類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: 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 proto.BatchRequest, k proto.Key) proto.Key {
candidate := proto.KeyMin
for _, union := range ba.Requests {
h := union.GetValue().(proto.Request).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
}
示例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 proto.Key, timestamp proto.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 proto.Key, desc *proto.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.(*proto.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.(*proto.RangeDescriptor))
}
rdc.rangeCache.Del(k.(rangeCacheKey))
}, rangeCacheKey(keys.RangeMetaKey(desc.StartKey).Next()),
rangeCacheKey(keys.RangeMetaKey(desc.EndKey)))
}
示例4: getConfig
// getConfig retrieves the configuration for the specified key. If the
// key is empty, all configurations are returned. Otherwise, the
// leading "/" path delimiter is stripped and the configuration
// matching the remainder is retrieved. Note that this will retrieve
// the default config if "key" is equal to "/", and will list all
// configs if "key" is equal to "". The body result contains a listing
// of keys and retrieval of a config. The output format is determined
// by the request header.
func getConfig(db *client.DB, configPrefix proto.Key, config gogoproto.Message,
path string, r *http.Request) (body []byte, contentType string, err error) {
// Scan all configs if the key is empty.
if len(path) == 0 {
var rows []client.KeyValue
if rows, err = db.Scan(configPrefix, configPrefix.PrefixEnd(), maxGetResults); err != nil {
return
}
if len(rows) == maxGetResults {
log.Warningf("retrieved maximum number of results (%d); some may be missing", maxGetResults)
}
var prefixes []string
for _, row := range rows {
trimmed := bytes.TrimPrefix(row.Key, configPrefix)
prefixes = append(prefixes, url.QueryEscape(string(trimmed)))
}
// Encode the response.
body, contentType, err = util.MarshalResponse(r, prefixes, util.AllEncodings)
} else {
configkey := keys.MakeKey(configPrefix, proto.Key(path[1:]))
if err = db.GetProto(configkey, config); err != nil {
return
}
body, contentType, err = util.MarshalResponse(r, config, util.AllEncodings)
}
return
}
示例5: 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 proto.Key, options lookupOptions) (*proto.RangeDescriptor, bool, func(), error) {
var desc *proto.RangeDescriptor
var err error
var descKey proto.Key
if !options.useReverseScan {
descKey = from
} else {
descKey = to
}
desc, err = ds.rangeCache.LookupRangeDescriptor(descKey, options)
if err != nil {
return nil, false, nil, err
}
// Checks whether need to get next range descriptor. If so, returns true.
needAnother := func(desc *proto.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
}
示例6: ValidateRangeMetaKey
// ValidateRangeMetaKey validates that the given key is a valid Range Metadata
// key.
func ValidateRangeMetaKey(key proto.Key) error {
// KeyMin is a valid key.
if key.Equal(proto.KeyMin) {
return nil
}
// Key must be at least as long as Meta1Prefix.
if len(key) < len(Meta1Prefix) {
return NewInvalidRangeMetaKeyError("too short", key)
}
prefix, body := proto.Key(key[:len(Meta1Prefix)]), proto.Key(key[len(Meta1Prefix):])
if prefix.Equal(Meta2Prefix) {
if body.Less(proto.KeyMax) {
return nil
}
return NewInvalidRangeMetaKeyError("body of meta2 range lookup is >= KeyMax", key)
}
if prefix.Equal(Meta1Prefix) {
if proto.KeyMax.Less(body) {
return NewInvalidRangeMetaKeyError("body of meta1 range lookup is > KeyMax", key)
}
return nil
}
return NewInvalidRangeMetaKeyError("not a meta key", key)
}
示例7: writeDescriptor
// writeDescriptor takes a Table or Database descriptor and writes it
// if needed, incrementing the descriptor counter.
func (p *planner) writeDescriptor(key proto.Key, descriptor descriptorProto, ifNotExists bool) error {
// Check whether key exists.
gr, err := p.db.Get(key)
if err != nil {
return err
}
if gr.Exists() {
if ifNotExists {
// Noop.
return nil
}
// Key exists, but we don't want it to: error out.
// TODO(marc): prettify the error (strip stuff off the type name)
return fmt.Errorf("%T \"%s\" already exists", descriptor, key.String())
}
// Increment unique descriptor counter.
if ir, err := p.db.Inc(keys.DescIDGenerator, 1); err == nil {
descriptor.SetID(uint32(ir.ValueInt() - 1))
} else {
return err
}
// TODO(pmattis): The error currently returned below is likely going to be
// difficult to interpret.
// TODO(pmattis): Need to handle if-not-exists here as well.
descKey := keys.MakeDescMetadataKey(descriptor.GetID())
return p.db.Txn(func(txn *client.Txn) error {
b := &client.Batch{}
b.CPut(key, descKey, nil)
b.CPut(descKey, descriptor, nil)
return txn.Commit(b)
})
}
示例8: Add
// Add adds a command to the queue which affects the specified key
// range. If end is empty, it is set to start.Next(), meaning the
// command affects a single key. The returned interface is the key for
// the command queue and must be re-supplied on subsequent invocation
// of Remove().
//
// Add should be invoked after waiting on already-executing,
// overlapping commands via the WaitGroup initialized through
// GetWait().
func (cq *CommandQueue) Add(start, end proto.Key, readOnly bool) interface{} {
if len(end) == 0 {
end = start.Next()
}
key := cq.cache.NewKey(start, end)
cq.cache.Add(key, &cmd{readOnly: readOnly})
return key
}
示例9: Get
// Get searches the kv list for 'key' and returns its
// raw byte value if found. ok is true only if the key is found.
func (s *SystemConfig) Get(key proto.Key) ([]byte, 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 nil, false
}
// TODO(marc): I'm pretty sure a Value returned by MVCCScan can
// never be nil. Should check.
return s.Values[index].Value.Bytes, true
}
示例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 proto.Key) []proto.Key {
if TestingDisableTableSplits {
return nil
}
tableStart := proto.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 proto.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 proto.KeySlice
var key proto.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 proto.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: GetWait
// GetWait initializes the supplied wait group with the number of
// executing commands which overlap the specified key range. If end is
// empty, end is set to start.Next(), meaning the command affects a
// single key. The caller should call wg.Wait() to wait for
// confirmation that all gating commands have completed or
// failed. readOnly is true if the requester is a read-only command;
// false for read-write.
func (cq *CommandQueue) GetWait(start, end proto.Key, readOnly bool, wg *sync.WaitGroup) {
// This gives us a memory-efficient end key if end is empty.
if len(end) == 0 {
end = start.Next()
start = end[:len(start)]
}
for _, c := range cq.cache.GetOverlaps(start, end) {
c := c.Value.(*cmd)
// Only add to the wait group if one of the commands isn't read-only.
if !readOnly || !c.readOnly {
c.pending = append(c.pending, wg)
wg.Add(1)
}
}
}
示例13: 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]proto.RangeTreeNode, testName string, node *proto.RangeTreeNode, keyMin, keyMax proto.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 proto.KeyMin is actually a range start key.
if !keyMin.Less(node.Key) && !node.Key.Equal(proto.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)
}
示例14: getDescriptor
// getDescriptor looks up the descriptor at `key`, validates it,
// and unmarshals it into `descriptor`.
func (p *planner) getDescriptor(key proto.Key, descriptor descriptorProto) error {
gr, err := p.db.Get(key)
if err != nil {
return err
}
if !gr.Exists() {
// TODO(marc): prettify the error (strip stuff off the type name)
return fmt.Errorf("%T \"%s\" does not exist", descriptor, key.String())
}
descKey := gr.ValueBytes()
if err := p.db.GetProto(descKey, descriptor); err != nil {
return err
}
return descriptor.Validate()
}
示例15: ObjectIDForKey
// ObjectIDForKey returns the object ID (table or database) for 'key',
// or (_, false) if not within the structured key space.
func ObjectIDForKey(key proto.Key) (uint32, bool) {
if key.Equal(proto.KeyMax) {
return 0, false
}
if key.Equal(keys.TableDataPrefix) {
// TODO(marc): this should eventually return SystemDatabaseID.
return 0, false
}
remaining := bytes.TrimPrefix(key, keys.TableDataPrefix)
if len(remaining) == len(key) {
// TrimPrefix returns the input untouched if the prefix doesn't match.
return 0, false
}
// Consume first encoded int.
_, id64, err := encoding.DecodeUvarint(remaining)
return uint32(id64), err == nil
}