Line data Source code
1 : // Copyright 2022 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 rangedel 6 : 7 : import ( 8 : "github.com/cockroachdb/pebble/internal/base" 9 : "github.com/cockroachdb/pebble/internal/keyspan" 10 : ) 11 : 12 : // Encode takes a Span containing only range deletions. It invokes the provided 13 : // closure with the encoded internal keys that represent the Span's state. The 14 : // keys and values passed to emit are only valid until the closure returns. If 15 : // emit returns an error, Encode stops and returns the error. 16 1 : func Encode(s *keyspan.Span, emit func(k base.InternalKey, v []byte) error) error { 17 1 : for _, k := range s.Keys { 18 1 : if k.Kind() != base.InternalKeyKindRangeDelete { 19 0 : return base.CorruptionErrorf("pebble: rangedel.Encode cannot encode %s key", k.Kind()) 20 0 : } 21 1 : ik := base.InternalKey{ 22 1 : UserKey: s.Start, 23 1 : Trailer: k.Trailer, 24 1 : } 25 1 : if err := emit(ik, s.End); err != nil { 26 0 : return err 27 0 : } 28 : } 29 1 : return nil 30 : } 31 : 32 : // Decode takes an internal key pair encoding a range deletion and returns a 33 : // decoded keyspan containing the key. If keysDst is provided, the key will be 34 : // appended to keysDst, avoiding an allocation. 35 1 : func Decode(ik base.InternalKey, v []byte, keysDst []keyspan.Key) keyspan.Span { 36 1 : return keyspan.Span{ 37 1 : Start: ik.UserKey, 38 1 : End: v, 39 1 : Keys: append(keysDst, keyspan.Key{ 40 1 : Trailer: ik.Trailer, 41 1 : }), 42 1 : } 43 1 : }