keys

package
v0.0.0-...-3976360 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 21, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// TagBytes is the number of bytes tag occupied in internal key.
	TagBytes = 8
)

Variables

This section is empty.

Functions

func CombineTag

func CombineTag(buf []byte, seq Sequence, kind Kind)

CombineTag combines sequence and kind into buf. Length of buf must equal to or greater than TagBytes.

func ExtractTag

func ExtractTag(buf []byte) (Sequence, Kind)

ExtractTag extracts sequence and kind from buf. Length of buf must equal to or greater than TagBytes.

func Max

func Max(cmp Comparer, a, b []byte) []byte

Max returns the key which is greater than or equal to the other.

func Min

func Min(cmp Comparer, a, b []byte) []byte

Min returns the key which is less than or equal to the other.

func PutTag

func PutTag(buf []byte, tag Tag)

PutTag puts tag into buf. Length of buf must equal to or greater than TagBytes.

func UnpackTag

func UnpackTag(tag Tag) (Sequence, Kind)

UnpackTag unpacks tag to sequence and kind.

Types

type Comparator

type Comparator interface {
	// Name returns the name of this comparator. A DB created with one comparator
	// can't be opened using a comparator with different name.
	//
	// Client should switch to a new name if the comparator implementation changes
	// in a way that cause the relative order of any two keys varied.
	//
	// Names starting with "leveldb." are reserved and should not used by clients.
	Name() string

	// Compare returns a value 'less than', 'equal to' or 'greater than' 0 depending
	// on whether a is 'less than', 'equal to' or 'greater than' b.
	Compare(a, b []byte) int

	// AppendSuccessor appends a possibly shortest byte sequence in range [start, limit)
	// to dst. Empty limit acts as infinite large. In particularly, if limit equals to
	// start, it returns append(dst, start).
	AppendSuccessor(dst, start, limit []byte) []byte
}

Comparator defines a total order over keys in LevelDB database. Methods of a comparator may be called by concurrent goroutines.

type Comparer

type Comparer interface {
	// Compare returns a value 'less than', 'equal to' or 'greater than' 0 depending
	// on whether a is 'less than', 'equal to' or 'greater than' b.
	Compare(a, b []byte) int
}

Comparer is a interface for algorithms to compare bytes.

type InternalComparator

type InternalComparator struct {
	UserKeyComparator UserComparator
}

InternalComparator is a comparator used to compare byte slices of internal keys.

func (*InternalComparator) AppendSuccessor

func (cmp *InternalComparator) AppendSuccessor(dst, start, limit []byte) []byte

AppendSuccessor implements Comparator.AppendSuccessor.

func (*InternalComparator) Compare

func (cmp *InternalComparator) Compare(a, b []byte) int

Compare implements Comparer.Compare and Comparator.Compare. It orders internal keys by ascending user key and descending sequence and kind. For given internal keys A with parts (Akey, Asequence, Akind) and B with (Bkey, Bsequence, Bkind), if A < B, we have:

  • Akey < Bkey, otherwise Akey == Bkey and:
  • Asequence > Bsequence, otherwise Asequence == Bsequence and:
  • Akind > Bkind.

func (*InternalComparator) Name

func (cmp *InternalComparator) Name() string

Name implements Comparator.Name.

type InternalKey

type InternalKey []byte

InternalKey is a combination of user key, which comes from APIs such as DB.Get, DB.Put, etc. and Tag which is also a combination of Sequence and Kind.

func MakeInternalKey

func MakeInternalKey(buf []byte, key []byte, seq Sequence, kind Kind) InternalKey

MakeInternalKey makes and stores a InternalKey which combines from key, seq and kind in buf. Make sure length of buf is equal to or greater than len(key) plus TagBytes.

func NewInternalKey

func NewInternalKey(key []byte, seq Sequence, kind Kind) InternalKey

NewInternalKey creates a InternalKey from combination of key, seq and kind.

func ToInternalKey

func ToInternalKey(key []byte) (InternalKey, bool)

ToInternalKey converts key to InternalKey.

func (InternalKey) Dup

func (ikey InternalKey) Dup() InternalKey

Dup creates a InternalKey from this one.

func (InternalKey) Split

func (ikey InternalKey) Split() ([]byte, Sequence, Kind)

Split splits this internal key to user key, sequence and kind parts.

func (InternalKey) Tag

func (ikey InternalKey) Tag() Tag

Tag returns tag part of internal key.

func (InternalKey) UserKey

func (ikey InternalKey) UserKey() []byte

UserKey returns key part of internal key.

type Kind

type Kind int

Kind expresses usage of the ambient internal key.

const (

	// Delete represents deletion of this key.
	Delete Kind = 0
	// Value represents value setting of this key.
	Value Kind = 1

	// Seek is maximum(Value, Delete), which is a valid Kind and
	// serves as start point for keys with same sequence.
	//
	// See InternalComparator.Compare for ordering among internal keys.
	Seek = maxKind
)

func (Kind) String

func (k Kind) String() string

type ParsedInternalKey

type ParsedInternalKey struct {
	UserKey  []byte
	Sequence Sequence
	Kind     Kind
}

ParsedInternalKey is a parsed or splited internal representation.

func (*ParsedInternalKey) Append

func (k *ParsedInternalKey) Append(dst []byte) []byte

Append appends this internal key to destination buffer.

func (*ParsedInternalKey) Parse

func (k *ParsedInternalKey) Parse(key []byte) bool

Parse parses input key as internal key and returns true for valid internal key. It is illegal to access other fields and methods after returning false.

func (*ParsedInternalKey) Tag

func (k *ParsedInternalKey) Tag() Tag

Tag returns tag of this internal key.

type Sequence

type Sequence uint64

Sequence is a monotonic value associated with every keys written to LevelDB.

At any time, once a key is written to LevelDB, no matter whether it is a key deletion or value setting, it is associated with a Sequence greater than Sequences associated to previous written keys.

For this we can conclude:

  • No keys have some Sequence.
  • For key with given Sequence, it is written before keys with bigger Sequences, also it is written after keys with smaller Sequences.
const (
	// MaxSequence is the maximum allowed value for Sequence.
	MaxSequence Sequence = (1 << 56) - 1
)

func (Sequence) Next

func (seq Sequence) Next(n uint64) Sequence

Next returns a Sequence which is n greater than this one.

type Tag

type Tag uint64

Tag is a combination of Sequence and Kind. This combination has properties:

* Tag with big Sequence is greater than tag with small Sequence. * With same Sequences, tag with Kind Seek is equal to or greater than other tags.

Thus for a combination with given Sequence and Kind Seek, we can say that:

  • All keys with tag greater than this combination are written after that given Sequence.
  • All keys with tag equal to or less than this combination are written before or at that given Sequence.

Also, due to the fact that no keys written to LevelDB have same Sequence, sometimes Tags are used as Sequences to determine ordering of written.

func GetTag

func GetTag(buf []byte) Tag

GetTag gets tag from buf. Length of buf must equal to or greater than TagBytes.

func PackTag

func PackTag(seq Sequence, kind Kind) Tag

PackTag packs sequence and kind to tag.

type UserComparator

type UserComparator interface {
	Comparator

	// MakePrefixSuccessor returns a byte sequence 'limit' such that all byte sequences
	// falling in [prefix, limit) have 'prefix' as prefix. Zero length 'limit' acts as
	// infinite large.
	MakePrefixSuccessor(start []byte) []byte
}

UserComparator is the interface exported to clients of LevelDB.

var BytewiseComparator UserComparator = bytewiseComparator{}

BytewiseComparator is an lexicographic ordering comparator, it has same ordering with bytes.Compare.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL