forked from cockroachdb/cockroach
/
keys.go
680 lines (608 loc) · 24.6 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// Copyright 2015 The Cockroach Authors.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
// Author: Tobias Schottdorf (tobias.schottdorf@gmail.com)
package keys
import (
"bytes"
"fmt"
"github.com/pkg/errors"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/encoding"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)
func makeKey(keys ...[]byte) []byte {
return bytes.Join(keys, nil)
}
// MakeStoreKey creates a store-local key based on the metadata key
// suffix, and optional detail.
func MakeStoreKey(suffix, detail roachpb.RKey) roachpb.Key {
key := make(roachpb.Key, 0, len(localStorePrefix)+len(suffix)+len(detail))
key = append(key, localStorePrefix...)
key = append(key, suffix...)
key = append(key, detail...)
return key
}
// StoreIdentKey returns a store-local key for the store metadata.
func StoreIdentKey() roachpb.Key {
return MakeStoreKey(localStoreIdentSuffix, nil)
}
// StoreGossipKey returns a store-local key for the gossip bootstrap metadata.
func StoreGossipKey() roachpb.Key {
return MakeStoreKey(localStoreGossipSuffix, nil)
}
// NodeLivenessKey returns the key for the node liveness record.
func NodeLivenessKey(nodeID roachpb.NodeID) roachpb.Key {
key := make(roachpb.Key, 0, len(NodeLivenessPrefix)+9)
key = append(key, NodeLivenessPrefix...)
key = encoding.EncodeUvarintAscending(key, uint64(nodeID))
return key
}
// NodeStatusKey returns the key for accessing the node status for the
// specified node ID.
func NodeStatusKey(nodeID roachpb.NodeID) roachpb.Key {
key := make(roachpb.Key, 0, len(StatusNodePrefix)+9)
key = append(key, StatusNodePrefix...)
key = encoding.EncodeUvarintAscending(key, uint64(nodeID))
return key
}
// NodeLastUsageReportKey returns the key for accessing the node last update check
// time (when version check or usage reporting was done).
func NodeLastUsageReportKey(nodeID roachpb.NodeID) roachpb.Key {
prefix := append([]byte(nil), UpdateCheckPrefix...)
return encoding.EncodeUvarintAscending(prefix, uint64(nodeID))
}
func makePrefixWithRangeID(prefix []byte, rangeID roachpb.RangeID, infix roachpb.RKey) roachpb.Key {
// Size the key buffer so that it is large enough for most callers.
key := make(roachpb.Key, 0, 32)
key = append(key, prefix...)
key = encoding.EncodeUvarintAscending(key, uint64(rangeID))
key = append(key, infix...)
return key
}
// MakeRangeIDPrefix creates a range-local key prefix from
// rangeID for both replicated and unreplicated data.
func MakeRangeIDPrefix(rangeID roachpb.RangeID) roachpb.Key {
return makePrefixWithRangeID(LocalRangeIDPrefix, rangeID, nil)
}
// MakeRangeIDReplicatedPrefix creates a range-local key prefix from
// rangeID for all Raft replicated data.
func MakeRangeIDReplicatedPrefix(rangeID roachpb.RangeID) roachpb.Key {
return makePrefixWithRangeID(LocalRangeIDPrefix, rangeID, localRangeIDReplicatedInfix)
}
// MakeRangeIDReplicatedKey creates a range-local key based on the range's
// Range ID, metadata key suffix, and optional detail.
func MakeRangeIDReplicatedKey(rangeID roachpb.RangeID, suffix, detail roachpb.RKey) roachpb.Key {
if len(suffix) != localSuffixLength {
panic(fmt.Sprintf("suffix len(%q) != %d", suffix, localSuffixLength))
}
key := MakeRangeIDReplicatedPrefix(rangeID)
key = append(key, suffix...)
key = append(key, detail...)
return key
}
// DecodeRangeIDKey parses a local range ID key into range ID, infix,
// suffix, and detail.
func DecodeRangeIDKey(
key roachpb.Key,
) (rangeID roachpb.RangeID, infix, suffix, detail roachpb.Key, err error) {
if !bytes.HasPrefix(key, LocalRangeIDPrefix) {
return 0, nil, nil, nil, errors.Errorf("key %s does not have %s prefix", key, LocalRangeIDPrefix)
}
// Cut the prefix, the Range ID, and the infix specifier.
b := key[len(LocalRangeIDPrefix):]
b, rangeInt, err := encoding.DecodeUvarintAscending(b)
if err != nil {
return 0, nil, nil, nil, err
}
if len(b) < localSuffixLength+1 {
return 0, nil, nil, nil, errors.Errorf("malformed key does not contain range ID infix and suffix")
}
infix = b[:1]
b = b[1:]
suffix = b[:localSuffixLength]
b = b[localSuffixLength:]
return roachpb.RangeID(rangeInt), infix, suffix, b, nil
}
// AbortCacheKey returns a range-local key by Range ID for an
// abort cache entry, with detail specified by encoding the
// supplied transaction ID.
func AbortCacheKey(rangeID roachpb.RangeID, txnID uuid.UUID) roachpb.Key {
key := MakeRangeIDReplicatedKey(rangeID, LocalAbortCacheSuffix, nil)
key = encoding.EncodeBytesAscending(key, txnID.GetBytes())
return key
}
// DecodeAbortCacheKey decodes the provided abort cache entry,
// returning the transaction ID.
func DecodeAbortCacheKey(key roachpb.Key, dest []byte) (uuid.UUID, error) {
_, _, suffix, detail, err := DecodeRangeIDKey(key)
if err != nil {
return uuid.UUID{}, err
}
if !bytes.Equal(suffix, LocalAbortCacheSuffix) {
return uuid.UUID{}, errors.Errorf("key %s does not contain the abort cache suffix %s",
key, LocalAbortCacheSuffix)
}
// Decode the id.
detail, idBytes, err := encoding.DecodeBytesAscending(detail, dest)
if err != nil {
return uuid.UUID{}, err
}
if len(detail) > 0 {
return uuid.UUID{}, errors.Errorf("key %q has leftover bytes after decode: %s; indicates corrupt key", key, detail)
}
txnID, err := uuid.FromBytes(idBytes)
return txnID, err
}
// RaftTombstoneKey returns a system-local key for a raft tombstone.
func RaftTombstoneKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalRaftTombstoneSuffix, nil)
}
// RaftAppliedIndexKey returns a system-local key for a raft applied index.
func RaftAppliedIndexKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalRaftAppliedIndexSuffix, nil)
}
// LeaseAppliedIndexKey returns a system-local key for a lease applied index.
func LeaseAppliedIndexKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalLeaseAppliedIndexSuffix, nil)
}
// RaftTruncatedStateKey returns a system-local key for a RaftTruncatedState.
func RaftTruncatedStateKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalRaftTruncatedStateSuffix, nil)
}
// RangeFrozenStatusKey returns a system-local key for the frozen status.
func RangeFrozenStatusKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalRangeFrozenStatusSuffix, nil)
}
// RangeLeaseKey returns a system-local key for a range lease.
func RangeLeaseKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalRangeLeaseSuffix, nil)
}
// RangeStatsKey returns the key for accessing the MVCCStats struct
// for the specified Range ID.
func RangeStatsKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalRangeStatsSuffix, nil)
}
// RangeLastGCKey returns a system-local key for the last GC.
func RangeLastGCKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalRangeLastGCSuffix, nil)
}
// RangeTxnSpanGCThresholdKey returns a system-local key for last used GC
// threshold on the txn span.
func RangeTxnSpanGCThresholdKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDReplicatedKey(rangeID, LocalTxnSpanGCThresholdSuffix, nil)
}
// MakeRangeIDUnreplicatedPrefix creates a range-local key prefix from
// rangeID for all unreplicated data.
func MakeRangeIDUnreplicatedPrefix(rangeID roachpb.RangeID) roachpb.Key {
return makePrefixWithRangeID(LocalRangeIDPrefix, rangeID, localRangeIDUnreplicatedInfix)
}
// MakeRangeIDUnreplicatedKey creates a range-local unreplicated key based
// on the range's Range ID, metadata key suffix, and optional detail.
func MakeRangeIDUnreplicatedKey(
rangeID roachpb.RangeID, suffix roachpb.RKey, detail roachpb.RKey,
) roachpb.Key {
if len(suffix) != localSuffixLength {
panic(fmt.Sprintf("suffix len(%q) != %d", suffix, localSuffixLength))
}
key := MakeRangeIDUnreplicatedPrefix(rangeID)
key = append(key, suffix...)
key = append(key, detail...)
return key
}
// RaftHardStateKey returns a system-local key for a Raft HardState.
func RaftHardStateKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDUnreplicatedKey(rangeID, LocalRaftHardStateSuffix, nil)
}
// RaftLastIndexKey returns a system-local key for the last index of the
// Raft log.
func RaftLastIndexKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDUnreplicatedKey(rangeID, LocalRaftLastIndexSuffix, nil)
}
// RaftLogPrefix returns the system-local prefix shared by all entries
// in a Raft log.
func RaftLogPrefix(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDUnreplicatedKey(rangeID, LocalRaftLogSuffix, nil)
}
// RaftLogKey returns a system-local key for a Raft log entry.
func RaftLogKey(rangeID roachpb.RangeID, logIndex uint64) roachpb.Key {
key := RaftLogPrefix(rangeID)
key = encoding.EncodeUint64Ascending(key, logIndex)
return key
}
// RangeLastReplicaGCTimestampKey returns a range-local key for
// the range's last replica GC timestamp.
func RangeLastReplicaGCTimestampKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDUnreplicatedKey(rangeID, LocalRangeLastReplicaGCTimestampSuffix, nil)
}
// RangeLastVerificationTimestampKeyDeprecated returns a range-local
// key for the range's last verification timestamp.
func RangeLastVerificationTimestampKeyDeprecated(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDUnreplicatedKey(rangeID, LocalRangeLastVerificationTimestampSuffixDeprecated, nil)
}
// RangeReplicaDestroyedErrorKey returns a range-local key for
// the range's replica destroyed error.
func RangeReplicaDestroyedErrorKey(rangeID roachpb.RangeID) roachpb.Key {
return MakeRangeIDUnreplicatedKey(rangeID, LocalRangeReplicaDestroyedErrorSuffix, nil)
}
// MakeRangeKey creates a range-local key based on the range
// start key, metadata key suffix, and optional detail (e.g. the
// transaction ID for a txn record, etc.).
func MakeRangeKey(key, suffix, detail roachpb.RKey) roachpb.Key {
if len(suffix) != localSuffixLength {
panic(fmt.Sprintf("suffix len(%q) != %d", suffix, localSuffixLength))
}
buf := MakeRangeKeyPrefix(key)
buf = append(buf, suffix...)
buf = append(buf, detail...)
return buf
}
// MakeRangeKeyPrefix creates a key prefix under which all range-local keys
// can be found.
func MakeRangeKeyPrefix(key roachpb.RKey) roachpb.Key {
buf := make(roachpb.Key, 0, len(LocalRangePrefix)+len(key)+1)
buf = append(buf, LocalRangePrefix...)
buf = encoding.EncodeBytesAscending(buf, key)
return buf
}
// DecodeRangeKey decodes the range key into range start key,
// suffix and optional detail (may be nil).
func DecodeRangeKey(key roachpb.Key) (startKey, suffix, detail roachpb.Key, err error) {
if !bytes.HasPrefix(key, LocalRangePrefix) {
return nil, nil, nil, errors.Errorf("key %q does not have %q prefix",
key, LocalRangePrefix)
}
// Cut the prefix and the Range ID.
b := key[len(LocalRangePrefix):]
b, startKey, err = encoding.DecodeBytesAscending(b, nil)
if err != nil {
return nil, nil, nil, err
}
if len(b) < localSuffixLength {
return nil, nil, nil, errors.Errorf("key %q does not have suffix of length %d",
key, localSuffixLength)
}
// Cut the suffix.
suffix = b[:localSuffixLength]
detail = b[localSuffixLength:]
return
}
// RangeDescriptorKey returns a range-local key for the descriptor
// for the range with specified key.
func RangeDescriptorKey(key roachpb.RKey) roachpb.Key {
return MakeRangeKey(key, LocalRangeDescriptorSuffix, nil)
}
// TransactionKey returns a transaction key based on the provided
// transaction key and ID. The base key is encoded in order to
// guarantee that all transaction records for a range sort together.
func TransactionKey(key roachpb.Key, txnID uuid.UUID) roachpb.Key {
rk, err := Addr(key)
if err != nil {
panic(err)
}
return MakeRangeKey(rk, localTransactionSuffix, roachpb.RKey(txnID.GetBytes()))
}
// IsLocal performs a cheap check that returns true iff a range-local key is
// passed, that is, a key for which `Addr` would return a non-identical RKey
// (or a decoding error).
//
// TODO(tschottdorf): we need a better name for these keys as only some of
// them are local and it's been identified as an area that is not understood
// by many of the team's developers. An obvious suggestion is "system" (as
// opposed to "user") keys, but unfortunately that name has already been
// claimed by a related (but not identical) concept.
func IsLocal(k roachpb.Key) bool {
return bytes.HasPrefix(k, localPrefix)
}
// Addr returns the address for the key, used to lookup the range containing
// the key. In the normal case, this is simply the key's value. However, for
// local keys, such as transaction records, range-spanning binary tree node
// pointers, the address is the inner encoded key, with the local key prefix
// and the suffix and optional detail removed. This address unwrapping is
// performed repeatedly in the case of doubly-local keys. In this way, local
// keys address to the same range as non-local keys, but are stored separately
// so that they don't collide with user-space or global system keys.
//
// However, not all local keys are addressable in the global map. Only range
// local keys incorporating a range key (start key or transaction key) are
// addressable (e.g. range metadata and txn records). Range local keys
// incorporating the Range ID are not (e.g. abort cache entries, and range
// stats).
func Addr(k roachpb.Key) (roachpb.RKey, error) {
if !IsLocal(k) {
return roachpb.RKey(k), nil
}
for {
if bytes.HasPrefix(k, localStorePrefix) {
return nil, errors.Errorf("store-local key %q is not addressable", k)
}
if bytes.HasPrefix(k, LocalRangeIDPrefix) {
return nil, errors.Errorf("local range ID key %q is not addressable", k)
}
if !bytes.HasPrefix(k, LocalRangePrefix) {
return nil, errors.Errorf("local key %q malformed; should contain prefix %q",
k, LocalRangePrefix)
}
k = k[len(LocalRangePrefix):]
var err error
// Decode the encoded key, throw away the suffix and detail.
if _, k, err = encoding.DecodeBytesAscending(k, nil); err != nil {
return nil, err
}
if !bytes.HasPrefix(k, localPrefix) {
break
}
}
return roachpb.RKey(k), nil
}
// MustAddr calls Addr and panics on errors.
func MustAddr(k roachpb.Key) roachpb.RKey {
rk, err := Addr(k)
if err != nil {
panic(errors.Wrapf(err, "could not take address of '%s'", k))
}
return rk
}
// AddrUpperBound returns the address for the key, used to lookup the range containing
// the key. However, unlike Addr, it will return the following key that local range
// keys address to. This is necessary because range-local keys exist conceptually in the
// space between regular keys. Addr() returns the regular key that is just to the left
// of a range-local key, which is guaranteed to be located on the same range. AddrUpperBound()
// returns the regular key that is just to the right, which may not be on the same range
// but is suitable for use as the EndKey of a span involving a range-local key.
func AddrUpperBound(k roachpb.Key) (roachpb.RKey, error) {
rk, err := Addr(k)
if err != nil {
return rk, err
}
if bytes.HasPrefix(k, localPrefix) {
// The upper bound for a range-local key that addresses to key k
// is the key directly after k.
rk = rk.Next()
}
return rk, nil
}
// RangeMetaKey returns a range metadata (meta1, meta2) indexing key for the
// given key.
//
// - For RKeyMin, KeyMin is returned.
// - For a meta1 key, KeyMin is returned.
// - For a meta2 key, a meta1 key is returned.
// - For an ordinary key, a meta2 key is returned.
func RangeMetaKey(key roachpb.RKey) roachpb.Key {
if len(key) == 0 { // key.Equal(roachpb.RKeyMin)
return roachpb.KeyMin
}
var prefix roachpb.Key
switch key[0] {
case meta1PrefixByte:
return roachpb.KeyMin
case meta2PrefixByte:
prefix = Meta1Prefix
key = key[len(Meta2Prefix):]
default:
prefix = Meta2Prefix
}
buf := make(roachpb.Key, 0, len(prefix)+len(key))
buf = append(buf, prefix...)
buf = append(buf, key...)
return buf
}
// UserKey returns an ordinary key for the given range metadata (meta1, meta2)
// indexing key.
//
// - For RKeyMin, Meta1Prefix is returned.
// - For a meta1 key, a meta2 key is returned.
// - For a meta2 key, an ordinary key is returned.
// - For an ordinary key, the input key is returned.
func UserKey(key roachpb.RKey) roachpb.RKey {
if len(key) == 0 { // key.Equal(roachpb.RKeyMin)
return roachpb.RKey(Meta1Prefix)
}
var prefix roachpb.Key
switch key[0] {
case meta1PrefixByte:
prefix = Meta2Prefix
key = key[len(Meta1Prefix):]
case meta2PrefixByte:
key = key[len(Meta2Prefix):]
}
buf := make(roachpb.RKey, 0, len(prefix)+len(key))
buf = append(buf, prefix...)
buf = append(buf, key...)
return buf
}
// validateRangeMetaKey validates that the given key is a valid Range Metadata
// key. This checks only the constraints common to forward and backwards scans:
// correct prefix and not exceeding KeyMax.
func validateRangeMetaKey(key roachpb.RKey) error {
// KeyMin is a valid key.
if key.Equal(roachpb.RKeyMin) {
return nil
}
// Key must be at least as long as Meta1Prefix.
if len(key) < len(Meta1Prefix) {
return NewInvalidRangeMetaKeyError("too short", key)
}
prefix, body := key[:len(Meta1Prefix)], key[len(Meta1Prefix):]
if !prefix.Equal(Meta2Prefix) && !prefix.Equal(Meta1Prefix) {
return NewInvalidRangeMetaKeyError("not a meta key", key)
}
if roachpb.RKeyMax.Less(body) {
return NewInvalidRangeMetaKeyError("body of meta key range lookup is > KeyMax", key)
}
return nil
}
// MetaScanBounds returns the range [start,end) within which the desired meta
// record can be found by means of an engine scan. The given key must be a
// valid RangeMetaKey as defined by validateRangeMetaKey.
// TODO(tschottdorf): a lot of casting going on inside.
func MetaScanBounds(key roachpb.RKey) (roachpb.Key, roachpb.Key, error) {
if err := validateRangeMetaKey(key); err != nil {
return nil, nil, err
}
if key.Equal(Meta2KeyMax) {
return nil, nil, NewInvalidRangeMetaKeyError("Meta2KeyMax can't be used as the key of scan", key)
}
if key.Equal(roachpb.RKeyMin) {
// Special case KeyMin: find the first entry in meta1.
return Meta1Prefix, Meta1Prefix.PrefixEnd(), nil
}
if key.Equal(Meta1KeyMax) {
// Special case Meta1KeyMax: this is the last key in Meta1, we don't want
// to start at Next().
return Meta1KeyMax, Meta1Prefix.PrefixEnd(), nil
}
// Otherwise find the first entry greater than the given key in the same meta prefix.
return key.Next().AsRawKey(), key[:len(Meta1Prefix)].PrefixEnd().AsRawKey(), nil
}
// MetaReverseScanBounds returns the range [start,end) within which the desired
// meta record can be found by means of a reverse engine scan. The given key
// must be a valid RangeMetaKey as defined by validateRangeMetaKey.
func MetaReverseScanBounds(key roachpb.RKey) (roachpb.Key, roachpb.Key, error) {
if err := validateRangeMetaKey(key); err != nil {
return nil, nil, err
}
if key.Equal(roachpb.RKeyMin) || key.Equal(Meta1Prefix) {
return nil, nil, NewInvalidRangeMetaKeyError("KeyMin and Meta1Prefix can't be used as the key of reverse scan", key)
}
if key.Equal(Meta2Prefix) {
// Special case Meta2Prefix: this is the first key in Meta2, and the scan
// interval covers all of Meta1.
return Meta1Prefix, key.Next().AsRawKey(), nil
}
// Otherwise find the first entry greater than the given key and find the last entry
// in the same prefix. For MVCCReverseScan the endKey is exclusive, if we want to find
// the range descriptor the given key specified,we need to set the key.Next() as the
// MVCCReverseScan`s endKey. For example:
// If we have ranges [a,f) and [f,z), then we'll have corresponding meta records
// at f and z. If you're looking for the meta record for key f, then you want the
// second record (exclusive in MVCCReverseScan), hence key.Next() below.
return key[:len(Meta1Prefix)].AsRawKey(), key.Next().AsRawKey(), nil
}
// MakeTablePrefix returns the key prefix used for the table's data.
func MakeTablePrefix(tableID uint32) []byte {
return encoding.EncodeUvarintAscending(nil, uint64(tableID))
}
// DecodeTablePrefix validates that the given key has a table prefix, returning
// the remainder of the key (with the prefix removed) and the decoded descriptor
// ID of the table.
func DecodeTablePrefix(key roachpb.Key) ([]byte, uint64, error) {
if encoding.PeekType(key) != encoding.Int {
return key, 0, errors.Errorf("invalid key prefix: %q", key)
}
return encoding.DecodeUvarintAscending(key)
}
// SentinelFamilyID indicates that MakeFamilyKey should make a sentinel key.
const SentinelFamilyID = 0
// MakeFamilyKey returns the key for the family in the given row by appending to
// the passed key. If SentinelFamilyID is passed, a sentinel key (which is the
// first key in a sql table row) is returned.
func MakeFamilyKey(key []byte, famID uint32) []byte {
if famID == SentinelFamilyID {
return encoding.EncodeUvarintAscending(key, 0)
}
size := len(key)
key = encoding.EncodeUvarintAscending(key, uint64(famID))
// Note that we assume that `len(key)-size` will always be encoded to a
// single byte by EncodeUvarint. This is currently always true because the
// varint encoding will encode 1-9 bytes.
return encoding.EncodeUvarintAscending(key, uint64(len(key)-size))
}
// MakeRowSentinelKey creates the first key in a sql table row.
func MakeRowSentinelKey(key []byte) []byte {
return MakeFamilyKey(key, SentinelFamilyID)
}
// EnsureSafeSplitKey transforms an SQL table key such that it is a valid split key
// (i.e. does not occur in the middle of a row).
func EnsureSafeSplitKey(key roachpb.Key) (roachpb.Key, error) {
if encoding.PeekType(key) != encoding.Int {
// Not a table key, so already a split key.
return key, nil
}
n := len(key)
// The column ID length is encoded as a varint and we take advantage of the
// fact that the column ID itself will be encoded in 0-9 bytes and thus the
// length of the column ID data will fit in a single byte.
buf := key[n-1:]
if encoding.PeekType(buf) != encoding.Int {
// The last byte is not a valid column ID suffix.
return nil, errors.Errorf("%s: not a valid table key", key)
}
// Strip off the family ID / column ID suffix from the buf. The last byte of the buf
// contains the length of the column ID suffix (which might be 0 if the buf
// does not contain a column ID suffix).
_, colIDLen, err := encoding.DecodeUvarintAscending(buf)
if err != nil {
return nil, err
}
if int(colIDLen)+1 > n {
// The column ID length was impossible. colIDLen is the length of
// the encoded column ID suffix. We add 1 to account for the byte
// holding the length of the encoded column ID and if that total
// (colIDLen+1) is greater than the key suffix (n == len(buf))
// then we bail. Note that we don't consider this an error because
// EnsureSafeSplitKey can be called on keys that look like table
// keys but which do not have a column ID length suffix (e.g
// SystemConfig.ComputeSplitKeys).
return nil, errors.Errorf("%s: malformed table key", key)
}
return key[:len(key)-int(colIDLen)-1], nil
}
// Range returns a key range encompassing all the keys in the Batch.
func Range(ba roachpb.BatchRequest) (roachpb.RSpan, error) {
from := roachpb.RKeyMax
to := roachpb.RKeyMin
for _, arg := range ba.Requests {
req := arg.GetInner()
if _, ok := req.(*roachpb.NoopRequest); ok {
continue
}
h := req.Header()
if !roachpb.IsRange(req) && len(h.EndKey) != 0 {
return roachpb.RSpan{}, errors.Errorf("end key specified for non-range operation: %s", req)
}
key, err := Addr(h.Key)
if err != nil {
return roachpb.RSpan{}, err
}
if key.Less(from) {
// Key is smaller than `from`.
from = key
}
if !key.Less(to) {
// Key.Next() is larger than `to`.
if bytes.Compare(key, roachpb.RKeyMax) > 0 {
return roachpb.RSpan{}, errors.Errorf("%s must be less than KeyMax", key)
}
to = key.Next()
}
if len(h.EndKey) == 0 {
continue
}
endKey, err := AddrUpperBound(h.EndKey)
if err != nil {
return roachpb.RSpan{}, err
}
if bytes.Compare(roachpb.RKeyMax, endKey) < 0 {
return roachpb.RSpan{}, errors.Errorf("%s must be less than or equal to KeyMax", endKey)
}
if to.Less(endKey) {
// EndKey is larger than `to`.
to = endKey
}
}
return roachpb.RSpan{Key: from, EndKey: to}, nil
}