testkeys

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2024 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package testkeys provides facilities for generating and comparing human-readable test keys for use in tests and benchmarks. This package provides a single Comparer implementation that compares all keys generated by this package.

Keys generated by this package may optionally have a 'suffix' encoding an MVCC timestamp. This suffix is of the form "@<integer>". Comparisons on the suffix are performed using integer value, not the byte representation.

Index

Constants

This section is empty.

Variables

View Source
var Comparer = &base.Comparer{
	Compare: compare,
	Equal:   func(a, b []byte) bool { return compare(a, b) == 0 },
	AbbreviatedKey: func(k []byte) uint64 {
		return base.DefaultComparer.AbbreviatedKey(k[:split(k)])
	},
	FormatKey: base.DefaultFormatter,
	Separator: func(dst, a, b []byte) []byte {
		ai := split(a)
		if ai == len(a) {
			return append(dst, a...)
		}
		bi := split(b)
		if bi == len(b) {
			return append(dst, a...)
		}

		if bytes.Equal(a[:ai], b[:bi]) {
			return append(dst, a...)
		}
		n := len(dst)
		dst = base.DefaultComparer.Separator(dst, a[:ai], b[:bi])

		buf := dst[n:]
		if bytes.Equal(a[:ai], buf) {
			return append(dst[:n], a...)
		}

		return dst
	},
	Successor: func(dst, a []byte) []byte {
		ai := split(a)
		if ai == len(a) {
			return append(dst, a...)
		}
		n := len(dst)
		dst = base.DefaultComparer.Successor(dst, a[:ai])

		buf := dst[n:]
		if bytes.Equal(a[:ai], buf) {
			return append(dst[:n], a...)
		}

		return dst
	},
	ImmediateSuccessor: func(dst, a []byte) []byte {

		ai := split(a)
		if ai != len(a) {
			panic("pebble: ImmediateSuccessor invoked with a non-prefix key")
		}
		return append(append(dst, a...), 0x00)
	},
	Split: split,
	Name:  "pebble.internal.testkeys",
}

Comparer is the comparer for test keys generated by this package.

View Source
var MaxSuffixLen = 1 + len(fmt.Sprintf("%d", int64(math.MaxInt64)))

MaxSuffixLen is the maximum length of a suffix generated by this package.

Functions

func Key

func Key(k Keyspace, i int64) []byte

Key returns the i-th unsuffixed key within the keyspace.

func KeyAt

func KeyAt(k Keyspace, i int64, t int64) []byte

KeyAt returns the i-th key within the keyspace with a suffix encoding the timestamp t.

func ParseSuffix

func ParseSuffix(s []byte) (int64, error)

ParseSuffix returns the integer representation of the encoded suffix.

func RandomSeparator

func RandomSeparator(dst, a, b []byte, suffix int64, maxLength int, rng *rand.Rand) []byte

RandomSeparator returns a random alphabetic key k such that a < k < b, pulling randomness from the provided random number generator. If dst is provided and the generated key fits within dst's capacity, the returned slice will use dst's memory.

If a prefix P exists such that Prefix(a) < P < Prefix(b), the generated key will consist of the prefix P appended with the provided suffix. A zero suffix generates an unsuffixed key. If no such prefix P exists, RandomSeparator will try to find a key k with either Prefix(a) or Prefix(b) such that a < k < b, but the generated key will not use the provided suffix. Note that it's possible that no separator key exists (eg, a='a@2', b='a@1'), in which case RandomSeparator returns nil.

If RandomSeparator generates a new prefix, the generated prefix will have length at most MAX(maxLength, len(Prefix(a)), len(Prefix(b))).

RandomSeparator panics if a or b fails to decode.

func Suffix

func Suffix(t int64) []byte

Suffix returns the test keys suffix representation of timestamp t.

func SuffixLen

func SuffixLen(t int64) int

SuffixLen returns the exact length of the given suffix when encoded.

func WriteKey

func WriteKey(dst []byte, k Keyspace, i int64) int

WriteKey writes the i-th unsuffixed key within the keyspace to the buffer dst. It returns the number of bytes written.

func WriteKeyAt

func WriteKeyAt(dst []byte, k Keyspace, i int64, t int64) int

WriteKeyAt writes the i-th key within the keyspace to the buffer dst, with a suffix encoding the timestamp t suffix. It returns the number of bytes written.

func WriteSuffix

func WriteSuffix(dst []byte, t int64) int

WriteSuffix writes the test keys suffix representation of timestamp t to dst, returning the number of bytes written.

Types

type Keyspace

type Keyspace interface {
	// Count returns the number of keys that exist within this keyspace.
	Count() int64

	// MaxLen returns the maximum length, in bytes, of a key within this
	// keyspace. This is only guaranteed to return an upper bound.
	MaxLen() int

	// Slice returns the sub-keyspace from index i, inclusive, to index j,
	// exclusive. The receiver is unmodified.
	Slice(i, j int64) Keyspace

	// EveryN returns a key space that includes 1 key for every N keys in the
	// original keyspace. The receiver is unmodified.
	EveryN(n int64) Keyspace
	// contains filtered or unexported methods
}

Keyspace describes a finite keyspace of unsuffixed test keys.

func Alpha

func Alpha(maxLength int) Keyspace

Alpha constructs a keyspace consisting of all keys containing characters a-z, with at most `maxLength` characters.

func Divvy

func Divvy(ks Keyspace, n int64) []Keyspace

Divvy divides the provided keyspace into N equal portions, containing disjoint keys evenly distributed across the keyspace.

Jump to

Keyboard shortcuts

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