cockroach: Index | Files | Directories

package util

import ""


Package Files

constants.go every_n.go fast_int_map.go fast_int_set.go fast_int_set_str.go hash.go metamorphic_off.go nocopy.go pluralize.go race_off.go reflect.go smalltrace.go strings.go testaddr_random.go topk.go unresolved_addr.go unresolved_addr.pb.go


const MetamorphicBuild = false

MetamorphicBuild is a flag that is set to true if the binary was compiled with the test_constants build tag. This flag can be used to enable expensive checks, test randomizations, or other metamorphic-style perturbations that will not affect test results but will exercise different parts of the code.

const RaceEnabled = false

RaceEnabled is true if CockroachDB was built with the race build tag.


var (
    ErrInvalidLengthUnresolvedAddr = fmt.Errorf("proto: negative length found during unmarshaling")
    ErrIntOverflowUnresolvedAddr   = fmt.Errorf("proto: integer overflow")
var TestAddr = NewUnresolvedAddr("tcp", "")

TestAddr is an address to use for test servers. Listening on port 0 causes the kernel to allocate an unused port.

func CRC32 Uses

func CRC32(data []byte) uint32

CRC32 computes the Castagnoli CRC32 of the given data.

func ConstantWithMetamorphicTestRange Uses

func ConstantWithMetamorphicTestRange(defaultValue, min, max int) int

ConstantWithMetamorphicTestRange is like ConstantWithMetamorphicTestValue except instead of returning a single metamorphic test value, it returns a random test value in a range.

func ConstantWithMetamorphicTestValue Uses

func ConstantWithMetamorphicTestValue(defaultValue, metamorphicValue int) int

ConstantWithMetamorphicTestValue should be used to initialize "magic constants" that should be varied during test scenarios to check for bugs at boundary conditions. When built with the test_constants build tag, the test value will be used. In all other cases, the production value will be used. The constant must be a "metamorphic variable": changing it cannot affect the output of any SQL DMLs. It can only affect the way in which the data is retrieved or processed, because otherwise the main test corpus would fail if this flag were enabled.

An example of a "magic constant" that behaves this way is a batch size. Batch sizes tend to present testing problems, because often the logic that deals with what to do when a batch is finished is less likely to be exercised by simple unit tests that don't use enough data to fill up a batch.

For example, instead of writing:

const batchSize = 64

you should write:

var batchSize = util.ConstantWithMetamorphicTestValue(64, 1)

This will give your code a batch size of 1 in the test_constants build configuration, increasing the amount of exercise the edge conditions get.

func EnableRacePreemptionPoints Uses

func EnableRacePreemptionPoints() func()

EnableRacePreemptionPoints enables goroutine preemption points declared with RacePreempt for builds using the race build tag.

func EqualPtrFields Uses

func EqualPtrFields(src, dst reflect.Value, prefix string) []string

EqualPtrFields uses reflection to check two "mirror" structures for matching pointer fields that point to the same object. Used to verify cloning/deep copy functions.

Returns the names of equal pointer fields.

func GetSingleRune Uses

func GetSingleRune(s string) (rune, error)

GetSingleRune decodes the string s as a single rune if possible.

func GetSmallTrace Uses

func GetSmallTrace(skip int) string

GetSmallTrace returns a comma-separated string containing the top 5 callers from a given skip level.

func MoveTopKToFront Uses

func MoveTopKToFront(data sort.Interface, k int)

MoveTopKToFront moves the top K elements to the front. It makes O(n) calls to data.Less and data.Swap (with very high probability). It uses Hoare's selection algorithm (aka quickselect).

func Pluralize Uses

func Pluralize(n int64) string

Pluralize returns a single character 's' unless n == 1.

func RacePreempt Uses

func RacePreempt()

RacePreempt adds a goroutine preemption point if CockroachDB was built with the race build tag and preemption points have been enabled. The function is a no-op (and should be optimized out through dead code elimination) if the race build tag was not used.

func RemoveTrailingSpaces Uses

func RemoveTrailingSpaces(input string) string

RemoveTrailingSpaces splits the input string into lines, trims any trailing spaces from each line, then puts the lines back together.

Any newlines at the end of the input string are ignored.

The output string always ends in a newline.

func ToLowerSingleByte Uses

func ToLowerSingleByte(b byte) byte

ToLowerSingleByte returns the lowercase of a given single ASCII byte. A non ASCII byte is returned unchanged.

func TruncateString Uses

func TruncateString(s string, maxRunes int) string

TruncateString truncates a string to a given number of runes.

type EveryN Uses

type EveryN struct {
    // N is the minimum duration of time between log messages.
    N   time.Duration

    // contains filtered or unexported fields

EveryN provides a way to rate limit spammy events. It tracks how recently a given event has occurred so that it can determine whether it's worth handling again.

The zero value for EveryN is usable and is equivalent to Every(0), meaning that all calls to ShouldProcess will return true.

NOTE: If you specifically care about log messages, you should use the version of this in the log package, as it integrates with the verbosity flags.

func Every Uses

func Every(n time.Duration) EveryN

Every is a convenience constructor for an EveryN object that allows a log message every n duration.

func (*EveryN) ShouldProcess Uses

func (e *EveryN) ShouldProcess(now time.Time) bool

ShouldProcess returns whether it's been more than N time since the last event.

type FNV64 Uses

type FNV64 struct {
    // contains filtered or unexported fields

FNV64 encapsulates the hash state.

func MakeFNV64 Uses

func MakeFNV64() FNV64

MakeFNV64 initializes a new FNV64 hash state.

func (*FNV64) Add Uses

func (f *FNV64) Add(c uint64)

Add modifies the underlying FNV64 state by accumulating the given integer hash to the existing state.

func (*FNV64) IsInitialized Uses

func (f *FNV64) IsInitialized() bool

IsInitialized returns true if the hash struct was initialized, which happens automatically when created through MakeFNV64 above.

func (*FNV64) Sum Uses

func (f *FNV64) Sum() uint64

Sum returns the hash value accumulated till now.

type FastIntMap Uses

type FastIntMap struct {
    // contains filtered or unexported fields

FastIntMap is a replacement for map[int]int which is more efficient when both keys and values are small. It can be passed by value (but Copy must be used for independent modification of copies).

func (FastIntMap) Copy Uses

func (m FastIntMap) Copy() FastIntMap

Copy returns a FastIntMap that can be independently modified.

func (FastIntMap) Empty Uses

func (m FastIntMap) Empty() bool

Empty returns true if the map is empty.

func (FastIntMap) ForEach Uses

func (m FastIntMap) ForEach(fn func(key, val int))

ForEach calls the given function for each key/value pair in the map (in arbitrary order).

func (FastIntMap) Get Uses

func (m FastIntMap) Get(key int) (value int, ok bool)

Get returns the current value mapped to key, or ok=false if the key is unmapped.

func (FastIntMap) Len Uses

func (m FastIntMap) Len() int

Len returns the number of keys in the map.

func (FastIntMap) MaxKey Uses

func (m FastIntMap) MaxKey() (_ int, ok bool)

MaxKey returns the maximum key that is in the map. If the map is empty, returns ok=false.

func (FastIntMap) MaxValue Uses

func (m FastIntMap) MaxValue() (_ int, ok bool)

MaxValue returns the maximum value that is in the map. If the map is empty, returns ok=false.

func (*FastIntMap) Set Uses

func (m *FastIntMap) Set(key, val int)

Set maps a key to the given value.

func (FastIntMap) String Uses

func (m FastIntMap) String() string

String prints out the contents of the map in the following format:

map[key1:val1 key2:val2 ...]

The keys are in ascending order.

func (*FastIntMap) Unset Uses

func (m *FastIntMap) Unset(key int)

Unset unmaps the given key.

type FastIntSet Uses

type FastIntSet struct {
    // contains filtered or unexported fields

FastIntSet keeps track of a set of integers. It does not perform any allocations when the values are small. It is not thread-safe.

func MakeFastIntSet Uses

func MakeFastIntSet(vals FastIntSet

MakeFastIntSet returns a set initialized with the given values.

func (*FastIntSet) Add Uses

func (s *FastIntSet) Add(i int)

Add adds a value to the set. No-op if the value is already in the set. If the large set is not nil and the value is within the range [0, 63], the value is added to both the large and small sets.

func (*FastIntSet) AddRange Uses

func (s *FastIntSet) AddRange(from, to int)

AddRange adds values 'from' up to 'to' (inclusively) to the set. E.g. AddRange(1,5) adds the values 1, 2, 3, 4, 5 to the set. 'to' must be >= 'from'. AddRange is always more efficient than individual Adds.

func (FastIntSet) Contains Uses

func (s FastIntSet) Contains(i int) bool

Contains returns true if the set contains the value.

func (FastIntSet) Copy Uses

func (s FastIntSet) Copy() FastIntSet

Copy returns a copy of s which can be modified independently.

func (*FastIntSet) CopyFrom Uses

func (s *FastIntSet) CopyFrom(other FastIntSet)

CopyFrom sets the receiver to a copy of other, which can then be modified independently.

func (FastIntSet) Difference Uses

func (s FastIntSet) Difference(rhs FastIntSet) FastIntSet

Difference returns the elements of s that are not in rhs as a new set.

func (*FastIntSet) DifferenceWith Uses

func (s *FastIntSet) DifferenceWith(rhs FastIntSet)

DifferenceWith removes any elements in rhs from this set.

func (FastIntSet) Empty Uses

func (s FastIntSet) Empty() bool

Empty returns true if the set is empty.

func (FastIntSet) Equals Uses

func (s FastIntSet) Equals(rhs FastIntSet) bool

Equals returns true if the two sets are identical.

func (FastIntSet) ForEach Uses

func (s FastIntSet) ForEach(f func(i int))

ForEach calls a function for each value in the set (in increasing order).

func (FastIntSet) Intersection Uses

func (s FastIntSet) Intersection(rhs FastIntSet) FastIntSet

Intersection returns the intersection of s and rhs as a new set.

func (*FastIntSet) IntersectionWith Uses

func (s *FastIntSet) IntersectionWith(rhs FastIntSet)

IntersectionWith removes any elements not in rhs from this set.

func (FastIntSet) Intersects Uses

func (s FastIntSet) Intersects(rhs FastIntSet) bool

Intersects returns true if s has any elements in common with rhs.

func (FastIntSet) Len Uses

func (s FastIntSet) Len() int

Len returns the number of the elements in the set.

func (FastIntSet) Next Uses

func (s FastIntSet) Next(startVal int) (int, bool)

Next returns the first value in the set which is >= startVal. If there is no value, the second return value is false.

func (FastIntSet) Ordered Uses

func (s FastIntSet) Ordered() []int

Ordered returns a slice with all the integers in the set, in increasing order.

func (*FastIntSet) Remove Uses

func (s *FastIntSet) Remove(i int)

Remove removes a value from the set. No-op if the value is not in the set.

func (*FastIntSet) Shift Uses

func (s *FastIntSet) Shift(delta int) FastIntSet

Shift generates a new set which contains elements i+delta for elements i in the original set.

func (FastIntSet) String Uses

func (s FastIntSet) String() string

String returns a list representation of elements. Sequential runs of positive numbers are shown as ranges. For example, for the set {0, 1, 2, 5, 6, 10}, the output is "(0-2,5,6,10)".

func (FastIntSet) SubsetOf Uses

func (s FastIntSet) SubsetOf(rhs FastIntSet) bool

SubsetOf returns true if rhs contains all the elements in s.

func (FastIntSet) Union Uses

func (s FastIntSet) Union(rhs FastIntSet) FastIntSet

Union returns the union of s and rhs as a new set.

func (*FastIntSet) UnionWith Uses

func (s *FastIntSet) UnionWith(rhs FastIntSet)

UnionWith adds all the elements from rhs to this set.

type NoCopy Uses

type NoCopy struct{}

NoCopy may be embedded into structs which must not be copied after the first use.

See for details.

func (*NoCopy) Lock Uses

func (*NoCopy) Lock()

Lock is a no-op used by -copylocks checker from `go vet`.

func (*NoCopy) Unlock Uses

func (*NoCopy) Unlock()

Unlock is a no-op used by -copylocks checker from `go vet`.

type UnresolvedAddr Uses

type UnresolvedAddr struct {
    NetworkField string `protobuf:"bytes,1,opt,name=network_field,json=networkField" json:"network_field"`
    AddressField string `protobuf:"bytes,2,opt,name=address_field,json=addressField" json:"address_field"`

UnresolvedAddr is an unresolved version of net.Addr.

var IsolatedTestAddr *UnresolvedAddr

IsolatedTestAddr is an address to use for tests that need extra isolation by using more addresses than (support for this is platform-specific and only enabled on Linux). Both TestAddr and IsolatedTestAddr guarantee that the chosen port is not in use when allocated, but IsolatedTestAddr draws from a larger pool of addresses so that when tests are run in a tight loop the system is less likely to run out of available ports or give a port to one test immediately after it was closed by another.

IsolatedTestAddr should be used for tests that open and close a large number of sockets, or tests which stop a server and rely on seeing a "connection refused" error afterwards. It cannot be used with tests that operate in secure mode since our test certificates are only valid for

func MakeUnresolvedAddr Uses

func MakeUnresolvedAddr(network, addr string) UnresolvedAddr

MakeUnresolvedAddr populates an UnresolvedAddr from a network and raw address string.

func NewUnresolvedAddr Uses

func NewUnresolvedAddr(network, addr string) *UnresolvedAddr

NewUnresolvedAddr creates a new UnresolvedAddr from a network and raw address string.

func (*UnresolvedAddr) Descriptor Uses

func (*UnresolvedAddr) Descriptor() ([]byte, []int)

func (UnresolvedAddr) IsEmpty Uses

func (a UnresolvedAddr) IsEmpty() bool

IsEmpty returns true if the address has no network or address specified.

func (*UnresolvedAddr) Marshal Uses

func (m *UnresolvedAddr) Marshal() (dAtA []byte, err error)

func (*UnresolvedAddr) MarshalTo Uses

func (m *UnresolvedAddr) MarshalTo(dAtA []byte) (int, error)

func (*UnresolvedAddr) Network Uses

func (a *UnresolvedAddr) Network() string

Network returns the address's network name.

func (*UnresolvedAddr) ProtoMessage Uses

func (*UnresolvedAddr) ProtoMessage()

func (*UnresolvedAddr) Reset Uses

func (m *UnresolvedAddr) Reset()

func (UnresolvedAddr) Resolve Uses

func (a UnresolvedAddr) Resolve() (net.Addr, error)

Resolve attempts to resolve a into a net.Addr.

func (*UnresolvedAddr) Size Uses

func (m *UnresolvedAddr) Size() (n int)

func (UnresolvedAddr) String Uses

func (a UnresolvedAddr) String() string

String returns the address's string form.

func (*UnresolvedAddr) Unmarshal Uses

func (m *UnresolvedAddr) Unmarshal(dAtA []byte) error

func (*UnresolvedAddr) XXX_DiscardUnknown Uses

func (m *UnresolvedAddr) XXX_DiscardUnknown()

func (*UnresolvedAddr) XXX_Marshal Uses

func (m *UnresolvedAddr) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*UnresolvedAddr) XXX_Merge Uses

func (dst *UnresolvedAddr) XXX_Merge(src proto.Message)

func (*UnresolvedAddr) XXX_Size Uses

func (m *UnresolvedAddr) XXX_Size() int

func (*UnresolvedAddr) XXX_Unmarshal Uses

func (m *UnresolvedAddr) XXX_Unmarshal(b []byte) error


ctxgroupPackage ctxgroup wraps with a context func.
encoding/csvPackage csv reads and writes comma-separated values (CSV) files.
flagutilPackage flagutil facilitates creation of rich flag types.
fsmPackage fsm provides an interface for defining and working with finite-state machines.
hlcPackage hlc implements the Hybrid Logical Clock outlined in "Logical Physical Clocks and Consistent Snapshots in Globally Distributed Databases", available online at
intervalPackage interval provides two implementations for an interval tree.
interval/genericPackage generic provides an implementation of a generic immutable interval B-Tree.
leaktestPackage leaktest provides tools to detect leaked goroutines in tests.
logPackage log implements logging.
metricPackage metric provides server metrics (a.k.a.
metric/aggmetricPackage aggmetric provides functionality to create metrics which expose aggregate metrics for internal collection and additionally per-child reporting to prometheus.
prettyPackage pretty prints documents based on a target line width.
quotapoolPackage quotapool provides an abstract implementation of a pool of resources to be distributed among concurrent clients.
sdnotifyPackage sdnotify implements both sides of the systemd readiness protocol.
syncutil/singleflightPackage singleflight provides a duplicate function call suppression mechanism.
sysutilPackage sysutil is a cross-platform compatibility layer on top of package syscall.
timeutil/pgdatePackage pgdate contains parsing functions and types for dates and times in a manner that is compatible with PostgreSQL.

Package util imports 21 packages (graph) and is imported by 845 packages. Updated 2020-10-24. Refresh now. Tools for package owners.