github.com/cockroachdb/pebble@v1.1.2/internal/rangedel/rangedel.go (about)

     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  func Encode(s *keyspan.Span, emit func(k base.InternalKey, v []byte) error) error {
    17  	for _, k := range s.Keys {
    18  		if k.Kind() != base.InternalKeyKindRangeDelete {
    19  			return base.CorruptionErrorf("pebble: rangedel.Encode cannot encode %s key", k.Kind())
    20  		}
    21  		ik := base.InternalKey{
    22  			UserKey: s.Start,
    23  			Trailer: k.Trailer,
    24  		}
    25  		if err := emit(ik, s.End); err != nil {
    26  			return err
    27  		}
    28  	}
    29  	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  func Decode(ik base.InternalKey, v []byte, keysDst []keyspan.Key) keyspan.Span {
    36  	return keyspan.Span{
    37  		Start: ik.UserKey,
    38  		End:   v,
    39  		Keys: append(keysDst, keyspan.Key{
    40  			Trailer: ik.Trailer,
    41  		}),
    42  	}
    43  }