github.com/zuoyebang/bitalostable@v1.0.1-0.20240229032404-e3b99a834294/internal/keyspan/span.go (about) 1 // Copyright 2018 The LevelDB-Go and Pebble Authors. All rights reserved. Use 2 // of this source code is governed by a BSD-style license that can be found in 3 // the LICENSE file. 4 5 package keyspan // import "github.com/zuoyebang/bitalostable/internal/keyspan" 6 7 import ( 8 "bytes" 9 "fmt" 10 "sort" 11 "strconv" 12 "strings" 13 "unicode" 14 15 "github.com/zuoyebang/bitalostable/internal/base" 16 ) 17 18 // Span represents a set of keys over a span of user key space. All of the keys 19 // within a Span are applied across the span's key span indicated by Start and 20 // End. Each internal key applied over the user key span appears as a separate 21 // Key, with its own kind and sequence number. Optionally, each Key may also 22 // have a Suffix and/or Value. 23 // 24 // Note that the start user key is inclusive and the end user key is exclusive. 25 // 26 // Currently the only supported key kinds are: 27 // 28 // RANGEDEL, RANGEKEYSET, RANGEKEYUNSET, RANGEKEYDEL. 29 type Span struct { 30 // Start and End encode the user key range of all the contained items, with 31 // an inclusive start key and exclusive end key. Both Start and End must be 32 // non-nil, or both nil if representing an invalid Span. 33 Start, End []byte 34 // Keys holds the set of keys applied over the [Start, End) user key range. 35 // Keys is sorted by (SeqNum, Kind) descending, unless otherwise specified 36 // by the context. If SeqNum and Kind are equal, the order of Keys is 37 // undefined. Keys may be empty, even if Start and End are non-nil. 38 // 39 // Keys are a decoded representation of the internal keys stored in batches 40 // or sstable blocks. A single internal key in a range key block may produce 41 // several decoded Keys. 42 Keys []Key 43 KeysOrder KeysOrder 44 } 45 46 // KeysOrder describes the ordering of Keys within a Span. 47 type KeysOrder int8 48 49 const ( 50 // ByTrailerDesc indicates a Span's keys are sorted by Trailer descending. 51 // This is the default ordering, and the ordering used during physical 52 // storage. 53 ByTrailerDesc KeysOrder = iota 54 // BySuffixAsc indicates a Span's keys are sorted by Suffix ascending. This 55 // ordering is used during user iteration of range keys. 56 BySuffixAsc 57 ) 58 59 // Key represents a single key applied over a span of user keys. A Key is 60 // contained by a Span which specifies the span of user keys over which the Key 61 // is applied. 62 type Key struct { 63 // Trailer contains the key kind and sequence number. 64 Trailer uint64 65 // Suffix holds an optional suffix associated with the key. This is only 66 // non-nil for RANGEKEYSET and RANGEKEYUNSET keys. 67 Suffix []byte 68 // Value holds a logical value associated with the Key. It is NOT the 69 // internal value stored in a range key or range deletion block. This is 70 // only non-nil for RANGEKEYSET keys. 71 Value []byte 72 } 73 74 // SeqNum returns the sequence number component of the key. 75 func (k Key) SeqNum() uint64 { 76 return k.Trailer >> 8 77 } 78 79 // Kind returns the kind component of the key. 80 func (k Key) Kind() base.InternalKeyKind { 81 return base.InternalKeyKind(k.Trailer & 0xff) 82 } 83 84 // Equal returns true if this Key is equal to the given key. Two keys are said 85 // to be equal if the two Keys have equal trailers, suffix and value. Suffix 86 // comparison uses the provided base.Compare func. Value comparison is bytewise. 87 func (k Key) Equal(equal base.Equal, b Key) bool { 88 return k.Trailer == b.Trailer && 89 equal(k.Suffix, b.Suffix) && 90 bytes.Equal(k.Value, b.Value) 91 } 92 93 // Valid returns true if the span is defined. 94 func (s *Span) Valid() bool { 95 return s.Start != nil && s.End != nil 96 } 97 98 // Empty returns true if the span does not contain any keys. An empty span may 99 // still be Valid. A non-empty span must be Valid. 100 // 101 // An Empty span may be produced by Visible, or be produced by iterators in 102 // order to surface the gaps between keys. 103 func (s *Span) Empty() bool { 104 return s == nil || len(s.Keys) == 0 105 } 106 107 // SmallestKey returns the smallest internal key defined by the span's keys. 108 // It requires the Span's keys be in ByTrailerDesc order. It panics if the span 109 // contains no keys or its keys are sorted in a different order. 110 func (s *Span) SmallestKey() base.InternalKey { 111 if len(s.Keys) == 0 { 112 panic("bitalostable: Span contains no keys") 113 } else if s.KeysOrder != ByTrailerDesc { 114 panic("bitalostable: span's keys unexpectedly not in trailer order") 115 } 116 // The first key has the highest (sequence number,kind) tuple. 117 return base.InternalKey{ 118 UserKey: s.Start, 119 Trailer: s.Keys[0].Trailer, 120 } 121 } 122 123 // LargestKey returns the largest internal key defined by the span's keys. The 124 // returned key will always be a "sentinel key" at the end boundary. The 125 // "sentinel key" models the exclusive end boundary by returning an InternalKey 126 // with the maximal sequence number, ensuring all InternalKeys with the same 127 // user key sort after the sentinel key. 128 // 129 // It requires the Span's keys be in ByTrailerDesc order. It panics if the span 130 // contains no keys or its keys are sorted in a different order. 131 func (s *Span) LargestKey() base.InternalKey { 132 if len(s.Keys) == 0 { 133 panic("bitalostable: Span contains no keys") 134 } else if s.KeysOrder != ByTrailerDesc { 135 panic("bitalostable: span's keys unexpectedly not in trailer order") 136 } 137 // The last key has the lowest (sequence number,kind) tuple. 138 kind := s.Keys[len(s.Keys)-1].Kind() 139 return base.MakeExclusiveSentinelKey(kind, s.End) 140 } 141 142 // SmallestSeqNum returns the smallest sequence number of a key contained within 143 // the span. It requires the Span's keys be in ByTrailerDesc order. It panics if 144 // the span contains no keys or its keys are sorted in a different order. 145 func (s *Span) SmallestSeqNum() uint64 { 146 if len(s.Keys) == 0 { 147 panic("bitalostable: Span contains no keys") 148 } else if s.KeysOrder != ByTrailerDesc { 149 panic("bitalostable: span's keys unexpectedly not in trailer order") 150 } 151 152 return s.Keys[len(s.Keys)-1].SeqNum() 153 } 154 155 // LargestSeqNum returns the largest sequence number of a key contained within 156 // the span. It requires the Span's keys be in ByTrailerDesc order. It panics if 157 // the span contains no keys or its keys are sorted in a different order. 158 func (s *Span) LargestSeqNum() uint64 { 159 if len(s.Keys) == 0 { 160 panic("bitalostable: Span contains no keys") 161 } else if s.KeysOrder != ByTrailerDesc { 162 panic("bitalostable: span's keys unexpectedly not in trailer order") 163 } 164 return s.Keys[0].SeqNum() 165 } 166 167 // TODO(jackson): Replace most of the calls to Visible with more targeted calls 168 // that avoid the need to construct a new Span. 169 170 // Visible returns a span with the subset of keys visible at the provided 171 // sequence number. It requires the Span's keys be in ByTrailerDesc order. It 172 // panics if the span's keys are sorted in a different order. 173 // 174 // Visible may incur an allocation, so callers should prefer targeted, 175 // non-allocating methods when possible. 176 func (s Span) Visible(snapshot uint64) Span { 177 if s.KeysOrder != ByTrailerDesc { 178 panic("bitalostable: span's keys unexpectedly not in trailer order") 179 } 180 181 ret := Span{Start: s.Start, End: s.End} 182 if len(s.Keys) == 0 { 183 return ret 184 } 185 186 // Keys from indexed batches may force an allocation. The Keys slice is 187 // ordered by sequence number, so ordinarily we can return the trailing 188 // subslice containing keys with sequence numbers less than `seqNum`. 189 // 190 // However, batch keys are special. Only visible batch keys are included 191 // when an Iterator's batch spans are fragmented. They must always be 192 // visible. 193 // 194 // Batch keys can create a sandwich of visible batch keys at the beginning 195 // of the slice and visible committed keys at the end of the slice, forcing 196 // us to allocate a new slice and copy the contents. 197 // 198 // Care is taking to only incur an allocation only when batch keys and 199 // visible keys actually sandwich non-visible keys. 200 201 // lastBatchIdx and lastNonVisibleIdx are set to the last index of a batch 202 // key and a non-visible key respectively. 203 lastBatchIdx := -1 204 lastNonVisibleIdx := -1 205 for i := range s.Keys { 206 if seqNum := s.Keys[i].SeqNum(); seqNum&base.InternalKeySeqNumBatch != 0 { 207 // Batch key. Always visible. 208 lastBatchIdx = i 209 } else if seqNum >= snapshot { 210 // This key is not visible. 211 lastNonVisibleIdx = i 212 } 213 } 214 215 // In the following comments: b = batch, h = hidden, v = visible (committed). 216 switch { 217 case lastNonVisibleIdx == -1: 218 // All keys are visible. 219 // 220 // [b b b], [v v v] and [b b b v v v] 221 ret.Keys = s.Keys 222 case lastBatchIdx == -1: 223 // There are no batch keys, so we can return the continuous subslice 224 // starting after the last non-visible Key. 225 // 226 // h h h [v v v] 227 ret.Keys = s.Keys[lastNonVisibleIdx+1:] 228 case lastNonVisibleIdx == len(s.Keys)-1: 229 // While we have a batch key and non-visible keys, there are no 230 // committed visible keys. The 'sandwich' is missing the bottom layer, 231 // so we can return the continuous sublice at the beginning. 232 // 233 // [b b b] h h h 234 ret.Keys = s.Keys[0 : lastBatchIdx+1] 235 default: 236 // This is the problematic sandwich case. Allocate a new slice, copying 237 // the batch keys and the visible keys into it. 238 // 239 // [b b b] h h h [v v v] 240 ret.Keys = make([]Key, (lastBatchIdx+1)+(len(s.Keys)-lastNonVisibleIdx-1)) 241 copy(ret.Keys, s.Keys[:lastBatchIdx+1]) 242 copy(ret.Keys[lastBatchIdx+1:], s.Keys[lastNonVisibleIdx+1:]) 243 } 244 return ret 245 } 246 247 // VisibleAt returns true if the span contains a key visible at the provided 248 // snapshot. Keys with sequence numbers with the batch bit set are treated as 249 // always visible. 250 // 251 // VisibleAt requires the Span's keys be in ByTrailerDesc order. It panics if 252 // the span's keys are sorted in a different order. 253 func (s *Span) VisibleAt(snapshot uint64) bool { 254 if s.KeysOrder != ByTrailerDesc { 255 panic("bitalostable: span's keys unexpectedly not in trailer order") 256 } 257 if len(s.Keys) == 0 { 258 return false 259 } else if first := s.Keys[0].SeqNum(); first&base.InternalKeySeqNumBatch != 0 { 260 // Only visible batch keys are included when an Iterator's batch spans 261 // are fragmented. They must always be visible. 262 return true 263 } else { 264 // Otherwise we check the last key. Since keys are ordered decreasing in 265 // sequence number, the last key has the lowest sequence number of any 266 // of the span's keys. If any of the keys are visible, the last key must 267 // be visible. Or put differently: if the last key is not visible, then 268 // no key is visible. 269 return s.Keys[len(s.Keys)-1].SeqNum() < snapshot 270 } 271 } 272 273 // ShallowClone returns the span with a Keys slice owned by the span itself. 274 // None of the key byte slices are cloned (see Span.DeepClone). 275 func (s *Span) ShallowClone() Span { 276 c := Span{ 277 Start: s.Start, 278 End: s.End, 279 Keys: make([]Key, len(s.Keys)), 280 KeysOrder: s.KeysOrder, 281 } 282 copy(c.Keys, s.Keys) 283 return c 284 } 285 286 // DeepClone clones the span, creating copies of all contained slices. DeepClone 287 // is intended for non-production code paths like tests, the level checker, etc 288 // because it is allocation heavy. 289 func (s *Span) DeepClone() Span { 290 c := Span{ 291 Start: make([]byte, len(s.Start)), 292 End: make([]byte, len(s.End)), 293 Keys: make([]Key, len(s.Keys)), 294 KeysOrder: s.KeysOrder, 295 } 296 copy(c.Start, s.Start) 297 copy(c.End, s.End) 298 for i := range s.Keys { 299 c.Keys[i].Trailer = s.Keys[i].Trailer 300 if len(s.Keys[i].Suffix) > 0 { 301 c.Keys[i].Suffix = make([]byte, len(s.Keys[i].Suffix)) 302 copy(c.Keys[i].Suffix, s.Keys[i].Suffix) 303 } 304 if len(s.Keys[i].Value) > 0 { 305 c.Keys[i].Value = make([]byte, len(s.Keys[i].Value)) 306 copy(c.Keys[i].Value, s.Keys[i].Value) 307 } 308 } 309 return c 310 } 311 312 // Contains returns true if the specified key resides within the span's bounds. 313 func (s *Span) Contains(cmp base.Compare, key []byte) bool { 314 return cmp(s.Start, key) <= 0 && cmp(key, s.End) < 0 315 } 316 317 // Covers returns true if the span covers keys at seqNum. 318 // 319 // Covers requires the Span's keys be in ByTrailerDesc order. It panics if the 320 // span's keys are sorted in a different order. 321 func (s Span) Covers(seqNum uint64) bool { 322 if s.KeysOrder != ByTrailerDesc { 323 panic("bitalostable: span's keys unexpectedly not in trailer order") 324 } 325 return !s.Empty() && s.Keys[0].SeqNum() > seqNum 326 } 327 328 // CoversAt returns true if the span contains a key that is visible at the 329 // provided snapshot sequence number, and that key's sequence number is higher 330 // than seqNum. 331 // 332 // Keys with sequence numbers with the batch bit set are treated as always 333 // visible. 334 // 335 // CoversAt requires the Span's keys be in ByTrailerDesc order. It panics if the 336 // span's keys are sorted in a different order. 337 func (s *Span) CoversAt(snapshot, seqNum uint64) bool { 338 if s.KeysOrder != ByTrailerDesc { 339 panic("bitalostable: span's keys unexpectedly not in trailer order") 340 } 341 // NB: A key is visible at `snapshot` if its sequence number is strictly 342 // less than `snapshot`. See base.Visible. 343 for i := range s.Keys { 344 if kseq := s.Keys[i].SeqNum(); kseq&base.InternalKeySeqNumBatch != 0 { 345 // Only visible batch keys are included when an Iterator's batch spans 346 // are fragmented. They must always be visible. 347 return kseq > seqNum 348 } else if kseq < snapshot { 349 return kseq > seqNum 350 } 351 } 352 return false 353 } 354 355 // String returns a string representation of the span. 356 func (s Span) String() string { 357 return fmt.Sprint(prettySpan{Span: s, formatKey: base.DefaultFormatter}) 358 } 359 360 // Pretty returns a formatter for the span. 361 func (s Span) Pretty(f base.FormatKey) fmt.Formatter { 362 return prettySpan{s, f} 363 } 364 365 type prettySpan struct { 366 Span 367 formatKey base.FormatKey 368 } 369 370 func (s prettySpan) Format(fs fmt.State, c rune) { 371 if !s.Valid() { 372 fmt.Fprintf(fs, "<invalid>") 373 return 374 } 375 fmt.Fprintf(fs, "%s-%s:{", s.formatKey(s.Start), s.formatKey(s.End)) 376 for i, k := range s.Keys { 377 if i > 0 { 378 fmt.Fprint(fs, " ") 379 } 380 fmt.Fprintf(fs, "(#%d,%s", k.SeqNum(), k.Kind()) 381 if len(k.Suffix) > 0 || len(k.Value) > 0 { 382 fmt.Fprintf(fs, ",%s", k.Suffix) 383 } 384 if len(k.Value) > 0 { 385 fmt.Fprintf(fs, ",%s", k.Value) 386 } 387 fmt.Fprint(fs, ")") 388 } 389 fmt.Fprintf(fs, "}") 390 } 391 392 // SortKeysByTrailer sorts a keys slice by trailer. 393 func SortKeysByTrailer(keys *[]Key) { 394 // NB: keys is a pointer to a slice instead of a slice to avoid `sorted` 395 // escaping to the heap. 396 sorted := (*keysBySeqNumKind)(keys) 397 sort.Sort(sorted) 398 } 399 400 // ParseSpan parses the string representation of a Span. It's intended for 401 // tests. ParseSpan panics if passed a malformed span representation. 402 func ParseSpan(input string) Span { 403 var s Span 404 parts := strings.FieldsFunc(input, func(r rune) bool { 405 switch r { 406 case '-', ':', '{', '}': 407 return true 408 default: 409 return unicode.IsSpace(r) 410 } 411 }) 412 s.Start, s.End = []byte(parts[0]), []byte(parts[1]) 413 414 // Each of the remaining parts represents a single Key. 415 s.Keys = make([]Key, 0, len(parts)-2) 416 for _, p := range parts[2:] { 417 keyFields := strings.FieldsFunc(p, func(r rune) bool { 418 switch r { 419 case '#', ',', '(', ')': 420 return true 421 default: 422 return unicode.IsSpace(r) 423 } 424 }) 425 426 var k Key 427 // Parse the sequence number. 428 seqNum, err := strconv.ParseUint(keyFields[0], 10, 64) 429 if err != nil { 430 panic(fmt.Sprintf("invalid sequence number: %q: %s", keyFields[0], err)) 431 } 432 // Parse the key kind. 433 kind := base.ParseKind(keyFields[1]) 434 k.Trailer = base.MakeTrailer(seqNum, kind) 435 // Parse the optional suffix. 436 if len(keyFields) >= 3 { 437 k.Suffix = []byte(keyFields[2]) 438 } 439 // Parse the optional value. 440 if len(keyFields) >= 4 { 441 k.Value = []byte(keyFields[3]) 442 } 443 s.Keys = append(s.Keys, k) 444 } 445 return s 446 }