util

package
v0.0.0-...-5b768f9 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OutputStylePlain = "plain"
	OutputStyleTable = "table"
	OutputStyleJson  = "json"
)

output style

Variables

This section is empty.

Functions

func BytesHash64

func BytesHash64(b []byte) int64

BytesHash64 returns the fnv hash of a bytes

func CreateTLSConfig

func CreateTLSConfig(caPath, certPath, keyPath string, insecureSkipVerify bool) (*tls.Config, error)

CreateTLSConfig creats a TLS configuration.

func DecodeRow

func DecodeRow(b []byte) (map[int64][]byte, error)

DecodeRow decodes a byte slice into columns. Row layout: colID1, value1, colID2, value2, ..... It is a simplified and specialized version of `github.com/pingcap/tidb/tablecodec.DecodeRow`.

func EncodeRow

func EncodeRow(cols [][]byte, colIDs []int64, valBuf []byte) ([]byte, error)

EncodeRow encodes row data and column ids into a slice of byte. Row layout: colID1, value1, colID2, value2, ... valBuf and values pass by caller, for reducing EncodeRow allocates temporary bufs. If you pass valBuf and values as nil, EncodeRow will allocate it. It is a simplified and specialized version of `github.com/pingcap/tidb/tablecodec.EncodeRow`.

func Fatal

func Fatal(args ...interface{})

Fatal prints the message and exits the program.

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf prints the message and exits the program.

func FloatToOneString

func FloatToOneString(f interface{}) string

FloatToOneString formats float into string with one digit after dot

func Hash64

func Hash64(n int64) int64

Hash64 returns a fnv Hash of the integer.

func IntToString

func IntToString(i interface{}) string

IntToString formats int value to string

func RandBytes

func RandBytes(r *rand.Rand, b []byte)

RandBytes fills the bytes with alphabetic characters randomly

func RenderJson

func RenderJson(w io.Writer, headers []string, values [][]string)

RnederJson will combine the headers and values and print a json string

func RenderString

func RenderString(w io.Writer, format string, headers []string, values [][]string)

RenderString renders headers and values according to the format provided

func RenderTable

func RenderTable(w io.Writer, headers []string, values [][]string)

RenderTable will use given headers and values to render a table style output

func Slice

func Slice(s string) (b []byte)

Slice converts string to slice without copy. Use at your own risk.

func String

func String(b []byte) (s string)

String converts slice to string without copy. Use at your own risk.

func StringHash64

func StringHash64(s string) int64

StringHash64 returns the fnv hash of a string

Types

type BufPool

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

BufPool is a bytes.Buffer pool

func NewBufPool

func NewBufPool() *BufPool

NewBufPool creates a buffer pool.

func (*BufPool) Get

func (b *BufPool) Get() []byte

Get gets a buffer.

func (*BufPool) Put

func (b *BufPool) Put(buf []byte)

Put returns a buffer.

type ConcurrentMap

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

A "thread" safe map of type int:int64. To avoid lock bottlenecks this map is dived to several (SHARD_COUNT) map shards.

func New

func New(shardCount int) ConcurrentMap

Creates a new concurrent map.

func (ConcurrentMap) Count

func (m ConcurrentMap) Count() int

Count returns the number of elements within the map.

func (ConcurrentMap) Get

func (m ConcurrentMap) Get(key int) (int64, bool)

Get retrieves an element from map under given key.

func (ConcurrentMap) GetShard

func (m ConcurrentMap) GetShard(key int) *ConcurrentMapShared

GetShard returns shard under given key

func (ConcurrentMap) Has

func (m ConcurrentMap) Has(key int) bool

Looks up an item under specified key

func (ConcurrentMap) IsEmpty

func (m ConcurrentMap) IsEmpty() bool

IsEmpty checks if map is empty.

func (ConcurrentMap) Items

func (m ConcurrentMap) Items() map[int]int64

Items returns all items as map[int]int64

func (ConcurrentMap) Iter deprecated

func (m ConcurrentMap) Iter() <-chan Tuple

Iter returns an iterator which could be used in a for range loop.

Deprecated: using IterBuffered() will get a better performence

func (ConcurrentMap) IterBuffered

func (m ConcurrentMap) IterBuffered() <-chan Tuple

IterBuffered returns a buffered iterator which could be used in a for range loop.

func (ConcurrentMap) IterCb

func (m ConcurrentMap) IterCb(fn IterCb)

Callback based iterator, cheapest way to read all elements in a map.

func (ConcurrentMap) Keys

func (m ConcurrentMap) Keys() []int

Keys returns all keys as []int

func (ConcurrentMap) MSet

func (m ConcurrentMap) MSet(data map[int]int64)

func (ConcurrentMap) MarshalJSON

func (m ConcurrentMap) MarshalJSON() ([]byte, error)

Reviles ConcurrentMap "private" variables to json marshal.

func (ConcurrentMap) Pop

func (m ConcurrentMap) Pop(key int) (v int64, exists bool)

Pop removes an element from the map and returns it

func (ConcurrentMap) Remove

func (m ConcurrentMap) Remove(key int)

Remove removes an element from the map.

func (ConcurrentMap) RemoveCb

func (m ConcurrentMap) RemoveCb(key int, cb RemoveCb) bool

RemoveCb locks the shard containing the key, retrieves its current value and calls the callback with those params If callback returns true and element exists, it will remove it from the map Returns the value returned by the callback (even if element was not present in the map)

func (ConcurrentMap) Set

func (m ConcurrentMap) Set(key int, value int64)

Sets the given value under the specified key.

func (ConcurrentMap) SetIfAbsent

func (m ConcurrentMap) SetIfAbsent(key int, value int64) bool

Sets the given value under the specified key if no value was associated with it.

func (ConcurrentMap) Upsert

func (m ConcurrentMap) Upsert(key int, value int64, cb UpsertCb) (res int64)

Insert or Update - updates existing element or inserts a new one using UpsertCb

type ConcurrentMapShared

type ConcurrentMapShared struct {
	sync.RWMutex // Read Write mutex, guards access to internal map.
	// contains filtered or unexported fields
}

A "thread" safe int to int64 map.

type FieldPair

type FieldPair struct {
	Field string
	Value []byte
}

FieldPair is a pair to hold field + value.

type FieldPairs

type FieldPairs []FieldPair

FieldPairs implements sort interface for []FieldPair

func NewFieldPairs

func NewFieldPairs(values map[string][]byte) FieldPairs

NewFieldPairs sorts the map by fields and return a sorted slice of FieldPair.

func (FieldPairs) Len

func (s FieldPairs) Len() int

Len implements sort interface Len

func (FieldPairs) Less

func (s FieldPairs) Less(i, j int) bool

Len implements sort interface Less

func (FieldPairs) Swap

func (s FieldPairs) Swap(i, j int)

Len implements sort interface Swap

type IterCb

type IterCb func(key int, v int64)

Iterator callback,called for every key,value found in maps. RLock is held for all calls for a given shard therefore callback sess consistent view of a shard, but not across the shards

type RemoveCb

type RemoveCb func(key int, v int64, exists bool) bool

RemoveCb is a callback executed in a map.RemoveCb() call, while Lock is held If returns true, the element will be removed from the map

type RowCodec

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

RowCodec is a helper struct to encode and decode TiDB format row

func NewRowCodec

func NewRowCodec(p *properties.Properties) *RowCodec

NewRowCodec creates the RowCodec

func (*RowCodec) Decode

func (r *RowCodec) Decode(row []byte, fields []string) (map[string][]byte, error)

Decode decodes the row and returns a field-value map

func (*RowCodec) Encode

func (r *RowCodec) Encode(buf []byte, values map[string][]byte) ([]byte, error)

Encode encodes the values

type SpinLock

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

SpinLock implements a simple atomic spin lock, the zero value for a SpinLock is an unlocked spinlock.

func (*SpinLock) Lock

func (sl *SpinLock) Lock()

Lock locks sl. If the lock is already in use, the caller blocks until Unlock is called

func (*SpinLock) String

func (sl *SpinLock) String() string

func (*SpinLock) TryLock

func (sl *SpinLock) TryLock() bool

TryLock will try to lock sl and return whether it succeed or not without blocking.

func (*SpinLock) Unlock

func (sl *SpinLock) Unlock()

Unlock unlocks sl, unlike [Mutex.Unlock](http://golang.org/pkg/sync/#Mutex.Unlock), there's no harm calling it on an unlocked SpinLock

type Tuple

type Tuple struct {
	Key int
	Val int64
}

Used by the Iter & IterBuffered functions to wrap two variables together over a channel,

type UpsertCb

type UpsertCb func(exist bool, valueInMap int64, newValue int64) int64

Callback to return new element to be inserted into the map It is called while lock is held, therefore it MUST NOT try to access other keys in same map, as it can lead to deadlock since Go sync.RWLock is not reentrant

Jump to

Keyboard shortcuts

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