CovenantSQL: github.com/CovenantSQL/CovenantSQL/utils Index | Files | Directories

package utils

import "github.com/CovenantSQL/CovenantSQL/utils"

Package utils provides integer math utilities.

Package utils provides useful standalone function for test and network, etc.

Index

Package Files

big.go bytes.go config.go doc.go exec.go integer.go msgpack.go net.go once.go path.go profiler.go remove.go signal.go slice.go

Constants

const (

    // KB is 1024 Bytes
    KB  int64 = 1 << (10 * iota)
    // MB is 1024 KB
    MB
    // GB is 1024 MB
    GB
    // TB is 1024 GB
    TB
    // PB is 1024 TB
    PB
    // EB is 1024 PB
    EB
)

Variables

var (
    // ErrNotEnoughPorts defines error indicating random port allocation failure.
    ErrNotEnoughPorts = errors.New("not enough ports in port range")
)
var FJ = filepath.Join

FJ is short for filepath.Join.

func BigMax Uses

func BigMax(x, y *big.Int) *big.Int

BigMax returns the larger of x or y.

func BigMin Uses

func BigMin(x, y *big.Int) *big.Int

BigMin returns the smaller of x or y.

func BigPow Uses

func BigPow(a, b int64) *big.Int

BigPow returns a ** b as a big integer.

func Byte Uses

func Byte(bigint *big.Int, padlength, n int) byte

Byte returns the byte at position n, with the supplied padlength in Little-Endian encoding. n==0 returns the MSB Example: bigint '5', padlength 32, n=31 => 5.

func ConcatAll Uses

func ConcatAll(args ...[]byte) []byte

ConcatAll concatenate several bytes slice into one.

func CopyFile Uses

func CopyFile(src, dst string) (int64, error)

CopyFile copies from src to dst until either EOF is reached on src or an error occurs. It verifies src exists and removes the dst if it exists.

func DecodeMsgPack Uses

func DecodeMsgPack(buf []byte, out interface{}) error

DecodeMsgPack reverses the encode operation on a byte slice input.

func DecodeMsgPackPlain Uses

func DecodeMsgPackPlain(buf []byte, out interface{}) error

DecodeMsgPackPlain reverses the encode operation on a byte slice input without RawToString setting.

func DupConf Uses

func DupConf(confFile string, newConfFile string) (err error)

DupConf duplicate conf file using random new listen addr to avoid failure on concurrent test cases.

func EncodeMsgPack Uses

func EncodeMsgPack(in interface{}) (*bytes.Buffer, error)

EncodeMsgPack writes an encoded object to a new bytes buffer.

func Exist Uses

func Exist(path string) bool

Exist return if file or path is exist.

func Exp Uses

func Exp(base, exponent *big.Int) *big.Int

Exp implements exponentiation by squaring. Exp returns a newly-allocated big integer and does not change base or exponent. The result is truncated to 256 bits.

Courtesy @karalabe and @chfast.

func FirstBitSet Uses

func FirstBitSet(v *big.Int) int

FirstBitSet returns the index of the first 1 bit in v, counting from LSB.

func GetMsgPackClientCodec Uses

func GetMsgPackClientCodec(c io.ReadWriteCloser) rpc.ClientCodec

GetMsgPackClientCodec returns msgpack client codec for connection.

func GetMsgPackServerCodec Uses

func GetMsgPackServerCodec(c io.ReadWriteCloser) rpc.ServerCodec

GetMsgPackServerCodec returns msgpack server codec for connection.

func GetProjectSrcDir Uses

func GetProjectSrcDir() string

GetProjectSrcDir gets the src code root.

func GetRandomPorts Uses

func GetRandomPorts(bindAddr string, minPort, maxPort, count int) (ports []int, err error)

GetRandomPorts returns available random ports, previously allocated ports will be ignored.

func HomeDirExpand Uses

func HomeDirExpand(path string) string

HomeDirExpand tries to expand the tilde (~) in the front of a path to a fullpath directory.

func MustParseBig256 Uses

func MustParseBig256(s string) *big.Int

MustParseBig256 parses s as a 256 bit big integer and panics if the string is invalid.

func MustParseUint64 Uses

func MustParseUint64(s string) uint64

MustParseUint64 parses s as an integer and panics if the string is invalid.

func PaddedBigBytes Uses

func PaddedBigBytes(bigint *big.Int, n int) []byte

PaddedBigBytes encodes a big integer as a big-endian byte slice. The length of the slice is at least n bytes.

func ParseBig256 Uses

func ParseBig256(s string) (*big.Int, bool)

ParseBig256 parses s as a 256 bit integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.

func ParseUint64 Uses

func ParseUint64(s string) (uint64, bool)

ParseUint64 parses s as an integer in decimal or hexadecimal syntax. Leading zeros are accepted. The empty string parses as zero.

func ReadBits Uses

func ReadBits(bigint *big.Int, buf []byte)

ReadBits encodes the absolute value of bigint as big-endian bytes. Callers must ensure that buf has enough space. If buf is too short the result will be incomplete.

func RegisterInterfaceToMsgPack Uses

func RegisterInterfaceToMsgPack(intf, impl reflect.Type) (err error)

RegisterInterfaceToMsgPack binds interface decode/encode to specified implementation.

func RemoveAll Uses

func RemoveAll(pattern string)

RemoveAll removes files using glob.

func RemoveDuplicatesUnordered Uses

func RemoveDuplicatesUnordered(elements []string) []string

RemoveDuplicatesUnordered de-duplicate elements in string slice.

func RunCommand Uses

func RunCommand(bin string, args []string, processName string, workingDir string, logDir string, toStd bool) (err error)

RunCommand runs a command and capture its output to a log file,

if toStd is true also output to stdout and stderr.

func S256 Uses

func S256(x *big.Int) *big.Int

S256 interprets x as a two's complement number. x must not exceed 256 bits (the result is undefined if it does) and is not modified.

S256(0)        = 0
S256(1)        = 1
S256(2**255)   = -2**255
S256(2**256-1) = -1.

func SafeAdd Uses

func SafeAdd(x, y uint64) (uint64, bool)

SafeAdd returns the result and whether overflow occurred.

func SafeMul Uses

func SafeMul(x, y uint64) (uint64, bool)

SafeMul returns multiplication result and whether overflow occurred.

func SafeSub Uses

func SafeSub(x, y uint64) (uint64, bool)

SafeSub returns subtraction result and whether overflow occurred.

func StartProfile Uses

func StartProfile(cpuprofile, memprofile string) error

StartProfile initializes the CPU and memory profile, if specified.

func StopProfile Uses

func StopProfile()

StopProfile closes the CPU and memory profiles if they are running.

func U256 Uses

func U256(x *big.Int) *big.Int

U256 encodes as a 256 bit two's complement number. This operation is destructive.

func WaitForExit Uses

func WaitForExit() <-chan os.Signal

WaitForExit waits for user cancellation signals: SIGINT/SIGTERM and ignore SIGHUP/SIGTTIN/SIGTTOU.

func WaitForPorts Uses

func WaitForPorts(ctx context.Context, bindAddr string, ports []int, interval time.Duration) (err error)

WaitForPorts returns only when port is ready to listen or canceled by context.

func WaitToConnect Uses

func WaitToConnect(ctx context.Context, bindAddr string, ports []int, interval time.Duration) (err error)

WaitToConnect returns only when port is ready to connect or canceled by context.

type CMD Uses

type CMD struct {
    Cmd     *exec.Cmd
    LogPath string
    LogFD   *os.File
}

CMD is the struct holding exec.Cmd and log path.

func RunCommandNB Uses

func RunCommandNB(bin string, args []string, processName string, workingDir string, logDir string, toStd bool) (cmd *CMD, err error)

RunCommandNB starts a non-blocking command.

type HexOrDecimal256 Uses

type HexOrDecimal256 big.Int

HexOrDecimal256 marshals big.Int as hex or decimal.

func (*HexOrDecimal256) MarshalText Uses

func (i *HexOrDecimal256) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*HexOrDecimal256) UnmarshalText Uses

func (i *HexOrDecimal256) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type HexOrDecimal64 Uses

type HexOrDecimal64 uint64

HexOrDecimal64 marshals uint64 as hex or decimal.

func (HexOrDecimal64) MarshalText Uses

func (i HexOrDecimal64) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (*HexOrDecimal64) UnmarshalText Uses

func (i *HexOrDecimal64) UnmarshalText(input []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Once Uses

type Once struct {
    // contains filtered or unexported fields
}

Once refactors sync.Once and add Reset support.

func (*Once) Do Uses

func (o *Once) Do(f func())

Do handles once logic.

func (*Once) Reset Uses

func (o *Once) Reset()

Reset make the once object to be initialized once again.

Directories

PathSynopsis
log
log/debug
timer
trace

Package utils imports 27 packages (graph) and is imported by 28 packages. Updated 2019-08-13. Refresh now. Tools for package owners.