utils

package
v0.0.0-...-0e96e3e Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2021 License: BSD-3-Clause Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SegmentShift = 7
	SegmentSize  = 2 << (SegmentShift - 1)
)
View Source
const (
	ErrNonNilMsg = "%s must not nil"
)
View Source
const (
	IndexOutOfBoundErrMsg = "index out of bound, index=%d, offset=%d, pos=%s"
)
View Source
const (
	Int64MaxValue = int64(math.MaxInt64)
)

Variables

View Source
var (
	IPAny      = "0.0.0.0"
	EmptyBytes []byte
)
View Source
var (
	ErrorEventNotRegister = errors.New("the event was not registered")
	ErrorEventRegister    = errors.New("register event publisher failed")
	ErrorAddSubscriber    = errors.New("add subscriber failed")
)
View Source
var (
	Crc64Table = crc64.MakeTable(uint64(528))
)
View Source
var (
	ErrArrayOutOfBound = errors.New("array out of bound")
)
View Source
var RaftDefaultScheduler = common.NewRoutinePool(256, 128)
View Source
var (
	RaftLog = logger.NewTestLogger("LRaft")
)

Functions

func AnalyzeIPAndPort

func AnalyzeIPAndPort(address string) (ip string, port int)

func ArrayCopy

func ArrayCopy(src []interface{}, srcPos int32, target []interface{}, targetPos int32, length int32)

func CheckErr

func CheckErr(err error)

func Checksum

func Checksum(b []byte) uint64

func Checksum2Long

func Checksum2Long(a, b uint64) uint64

func DeregisterSubscriber

func DeregisterSubscriber(s Subscriber)

func Exist

func Exist(path string) bool

func FillTargetElement

func FillTargetElement(array []interface{}, e interface{})

func GetBoolFromEnv

func GetBoolFromEnv(key string) (bool, error)

func GetBoolFromEnvOptional

func GetBoolFromEnvOptional(key string, defVal bool) bool

func GetCurrentTimeMs

func GetCurrentTimeMs() int64

func GetCurrentTimeNs

func GetCurrentTimeNs() int64

func GetInt16FromEnv

func GetInt16FromEnv(key string) int16

func GetInt16FromEnvOptional

func GetInt16FromEnvOptional(key string, defVal int16) int16

func GetInt32FromEnv

func GetInt32FromEnv(key string) int32

func GetInt32FromEnvOptional

func GetInt32FromEnvOptional(key string, defVal int32) int32

func GetInt64FormBytes

func GetInt64FormBytes(memory []byte, index int) int64

func GetInt64FromEnv

func GetInt64FromEnv(key string) int64

func GetInt64FromEnvOptional

func GetInt64FromEnvOptional(key string, defVal int64) int64

func GetInt8FromEnv

func GetInt8FromEnv(key string) int8

func GetInt8FromEnvOptional

func GetInt8FromEnvOptional(key string, defVal int8) int8

func GetIntFromEnv

func GetIntFromEnv(key string) (int, error)

func GetIntFromEnvOptional

func GetIntFromEnvOptional(key string, defVal int) int

func GetStringFromEnv

func GetStringFromEnv(key string) string

func Home

func Home() (string, error)

func IF

func IF(expression bool, a, b interface{}) interface{}

func InitEventCenter

func InitEventCenter()

func IsIOError

func IsIOError(err error) bool

func ParseToInt

func ParseToInt(val string) int

func ParseToInt16

func ParseToInt16(val string) int16

func ParseToInt32

func ParseToInt32(val string) int32

func ParseToInt64

func ParseToInt64(val string) int64

func ParseToInt8

func ParseToInt8(val string) int8

func ParseToUint16

func ParseToUint16(val string) uint16

func ParseToUint32

func ParseToUint32(val string) uint32

func ParseToUint64

func ParseToUint64(val string) uint64

func ParseToUint8

func ParseToUint8(val string) uint8

func PrintStack

func PrintStack() string

func PublishEvent

func PublishEvent(event ...Event) error

func PublishEventNonBlock

func PublishEventNonBlock(event ...Event) (bool, error)

func PublishEventWithTimeout

func PublishEventWithTimeout(timeout time.Duration,
	events ...Event) (bool, error)

func RegisterPublisher

func RegisterPublisher(ctx context.Context, event Event, ringBufferSize int64) error

func RegisterPublisherDefault

func RegisterPublisherDefault(ctx context.Context, event Event) error

func RegisterSubscriber

func RegisterSubscriber(s Subscriber) error

func RequireFalse

func RequireFalse(expression bool, format string, args ...interface{})

func RequireNonNil

func RequireNonNil(e interface{}, msg string) (interface{}, error)

func RequireTrue

func RequireTrue(expression bool, format string, args ...interface{}) error

func Shutdown

func Shutdown()

func TestRegisterOnExpire

func TestRegisterOnExpire(f func(event Event))

Types

type AtomicInt64

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

func NewAtomicInt64

func NewAtomicInt64() AtomicInt64

func (AtomicInt64) Decrement

func (a AtomicInt64) Decrement() int64

func (AtomicInt64) Increment

func (a AtomicInt64) Increment() int64

func (AtomicInt64) Value

func (a AtomicInt64) Value() int64

type BinarySearchTree

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

func NewBinarySearchTree

func NewBinarySearchTree(compare func(a, b interface{}) int) *BinarySearchTree

func (*BinarySearchTree) Delete

func (bTree *BinarySearchTree) Delete(v interface{})

func (*BinarySearchTree) Find

func (bTree *BinarySearchTree) Find(v interface{}) *node

func (*BinarySearchTree) FindMax

func (bTree *BinarySearchTree) FindMax() *node

func (*BinarySearchTree) FindMin

func (bTree *BinarySearchTree) FindMin() *node

func (*BinarySearchTree) FindNearbyLeft

func (bTree *BinarySearchTree) FindNearbyLeft(v interface{}) *node
					5
				  /   \
				 3     8
				/ \   / \
            1   4 7   9

if you find 5, will return 3, if find 1, will return nil

func (*BinarySearchTree) FindNearbyRight

func (bTree *BinarySearchTree) FindNearbyRight(v string) *node
					5
				  /   \
				 3     8
				/ \   / \
            1   4 7   9

if you find 5, will return 7, if find 8, will return 9

func (*BinarySearchTree) Insert

func (bTree *BinarySearchTree) Insert(v interface{}, replaceOld bool)

func (*BinarySearchTree) Range

func (bTree *BinarySearchTree) Range(call func(n *node))

func (*BinarySearchTree) SeekLevel

func (bTree *BinarySearchTree) SeekLevel() [][]*node

type ConcurrentMap

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

ConcurrentMap 并发map,读读不阻塞

func NewConcurrentMap

func NewConcurrentMap() *ConcurrentMap

NewConcurrentMap 创建一个新的 ConcurrentMap

func (*ConcurrentMap) Clear

func (cm *ConcurrentMap) Clear()

Clear 清空 map

func (*ConcurrentMap) ComputeIfAbsent

func (cm *ConcurrentMap) ComputeIfAbsent(key interface{}, function func(key interface{}) interface{}) interface{}

ComputeIfAbsent 懒Put操作,通过 key 计算是否存在该 key,如果存在,直接返回,否则执行 function 方法计算对应的 value

func (*ConcurrentMap) Contains

func (cm *ConcurrentMap) Contains(k interface{}) bool

Contains 判断是否包含某个 key

func (*ConcurrentMap) ForEach

func (cm *ConcurrentMap) ForEach(consumer func(k, v interface{}))

ForEach 遍历所有的 key-value

func (*ConcurrentMap) Get

func (cm *ConcurrentMap) Get(k interface{}) interface{}

Get 根据 key 获取一个数据

func (*ConcurrentMap) Keys

func (cm *ConcurrentMap) Keys() []interface{}

Keys 获取所有的 key 数组

func (*ConcurrentMap) Put

func (cm *ConcurrentMap) Put(k, v interface{})

Put 存入一个键值对

func (*ConcurrentMap) PutIfAbsent

func (cm *ConcurrentMap) PutIfAbsent(key, val interface{}) interface{}

PutIfAbsent

func (*ConcurrentMap) Remove

func (cm *ConcurrentMap) Remove(k interface{})

Remove 根据 key 删除一个 key-value

func (*ConcurrentMap) Size

func (cm *ConcurrentMap) Size() int

Size 返回map的元素个数

func (*ConcurrentMap) Values

func (cm *ConcurrentMap) Values() []interface{}

Values 获取所有的 value 数组

type ConcurrentSlice

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

func (*ConcurrentSlice) Add

func (cs *ConcurrentSlice) Add(v interface{})

func (*ConcurrentSlice) ForEach

func (cs *ConcurrentSlice) ForEach(consumer func(index int, v interface{}))

func (*ConcurrentSlice) Get

func (cs *ConcurrentSlice) Get(index int32) (interface{}, error)

func (*ConcurrentSlice) GetFirst

func (cs *ConcurrentSlice) GetFirst() interface{}

func (*ConcurrentSlice) GetLast

func (cs *ConcurrentSlice) GetLast() interface{}

func (*ConcurrentSlice) Remove

func (cs *ConcurrentSlice) Remove(v interface{})

func (*ConcurrentSlice) Size

func (cs *ConcurrentSlice) Size() int32

type CountDownEvent

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

func NewCountDownEvent

func NewCountDownEvent() *CountDownEvent

func (*CountDownEvent) Await

func (cde *CountDownEvent) Await()

func (*CountDownEvent) CountDown

func (cde *CountDownEvent) CountDown()

func (*CountDownEvent) GetAttachment

func (cde *CountDownEvent) GetAttachment() interface{}

func (*CountDownEvent) IncrementAndGet

func (cde *CountDownEvent) IncrementAndGet() int64

func (*CountDownEvent) SetAttachment

func (cde *CountDownEvent) SetAttachment(attachment interface{})

type Event

type Event interface {
	// Topic of the event
	Name() string
	// The sequence number of the event
	Sequence() int64
}

Event interface

type EventCenter

type EventCenter struct {
	Publishers *common.ConcurrentMap // <string:event_name, *Publisher >
	// contains filtered or unexported fields
}

type LifeCycle

type LifeCycle interface {
	Init(args interface{}) error

	Shutdown()
}

type Publisher

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

func (*Publisher) AddSubscriber

func (p *Publisher) AddSubscriber(s Subscriber)

func (*Publisher) PublishEvent

func (p *Publisher) PublishEvent(event ...Event)

func (*Publisher) PublishEventNonBlock

func (p *Publisher) PublishEventNonBlock(events ...Event) bool

func (*Publisher) RemoveSubscriber

func (p *Publisher) RemoveSubscriber(s Subscriber)

type Segment

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

func NewSegment

func NewSegment(owner *SegmentList) *Segment

func (*Segment) Add

func (s *Segment) Add(e interface{})

func (*Segment) AddAll

func (s *Segment) AddAll(src []interface{}, srcPos, size int32)

func (*Segment) Cap

func (s *Segment) Cap() int32

func (*Segment) Clear

func (s *Segment) Clear()

func (*Segment) Get

func (s *Segment) Get(index int32) interface{}

func (*Segment) IsEmpty

func (s *Segment) IsEmpty() bool

func (*Segment) IsReachEnd

func (s *Segment) IsReachEnd() bool

func (*Segment) PeekFirst

func (s *Segment) PeekFirst() interface{}

func (*Segment) PeekLast

func (s *Segment) PeekLast() interface{}

func (*Segment) RemoveFromFirst

func (s *Segment) RemoveFromFirst(toIndex int32) int32

func (*Segment) RemoveFromFirstWhen

func (s *Segment) RemoveFromFirstWhen(predicate func(v interface{}) bool) int32

func (*Segment) RemoveFromLastWhen

func (s *Segment) RemoveFromLastWhen(predicate func(v interface{}) bool) int32

func (*Segment) Size

func (s *Segment) Size() int32

type SegmentList

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

func NewSegmentList

func NewSegmentList() *SegmentList

func (*SegmentList) Add

func (sl *SegmentList) Add(e interface{})

func (*SegmentList) AddAll

func (sl *SegmentList) AddAll(arr []interface{})

func (*SegmentList) Clear

func (sl *SegmentList) Clear()

func (*SegmentList) Get

func (sl *SegmentList) Get(index int32) interface{}

func (*SegmentList) GetFirst

func (sl *SegmentList) GetFirst() *Segment

func (*SegmentList) GetLast

func (sl *SegmentList) GetLast() *Segment

func (*SegmentList) IsEmpty

func (sl *SegmentList) IsEmpty() bool

func (*SegmentList) PeekFirst

func (sl *SegmentList) PeekFirst() interface{}

func (*SegmentList) PeekLast

func (sl *SegmentList) PeekLast() interface{}

func (*SegmentList) RemoveFromFirst

func (sl *SegmentList) RemoveFromFirst(toIndex int32)

func (*SegmentList) RemoveFromFirstWhen

func (sl *SegmentList) RemoveFromFirstWhen(predicate func(v interface{}) bool)

func (*SegmentList) RemoveFromLastWhen

func (sl *SegmentList) RemoveFromLastWhen(predicate func(v interface{}) bool)

func (*SegmentList) SegmentsSize

func (sl *SegmentList) SegmentsSize() int32

func (*SegmentList) Size

func (sl *SegmentList) Size() int32

type Set

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

func NewSet

func NewSet() *Set

func NewSetWithValues

func NewSetWithValues(arr ...interface{}) *Set

func (*Set) Add

func (s *Set) Add(value interface{})

func (*Set) AddAll

func (s *Set) AddAll(values ...interface{})

func (*Set) AddAllWithSet

func (s *Set) AddAllWithSet(set *Set)

func (*Set) Contain

func (s *Set) Contain(value interface{}) bool

func (*Set) IsEmpty

func (s *Set) IsEmpty() bool

func (*Set) Range

func (s *Set) Range(f func(value interface{}))

func (*Set) Remove

func (s *Set) Remove(value interface{})

func (*Set) RemoveAll

func (s *Set) RemoveAll(arr ...interface{})

func (*Set) RemoveAllWithSet

func (s *Set) RemoveAllWithSet(set *Set)

func (*Set) RetainAll

func (s *Set) RetainAll(arr ...interface{})

func (*Set) RetainAllWithSet

func (s *Set) RetainAllWithSet(set *Set)

func (*Set) Size

func (s *Set) Size() int

func (*Set) ToSlice

func (s *Set) ToSlice(arr ...interface{})

type Subscriber

type Subscriber interface {
	OnEvent(event Event, endOfBatch bool)

	IgnoreExpireEvent() bool

	SubscribeType() Event
}

type SyncSet

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

func NewSyncSet

func NewSyncSet() *SyncSet

func (*SyncSet) Add

func (s *SyncSet) Add(value interface{})

func (*SyncSet) Range

func (s *SyncSet) Range(f func(value interface{}))

func (*SyncSet) Remove

func (s *SyncSet) Remove(value interface{})

type TreeMap

type TreeMap struct {
	BinarySearchTree
	// contains filtered or unexported fields
}

func NewTreeMap

func NewTreeMap(compare func(a, b interface{}) int) *TreeMap

func (*TreeMap) Clear

func (tMap *TreeMap) Clear()

func (*TreeMap) ComputeIfAbsent

func (tMap *TreeMap) ComputeIfAbsent(key interface{}, supplier func() interface{}) interface{}

func (*TreeMap) Get

func (tMap *TreeMap) Get(key interface{}) interface{}

func (*TreeMap) IsEmpty

func (tMap *TreeMap) IsEmpty() bool

func (*TreeMap) Put

func (tMap *TreeMap) Put(key, val interface{})

func (*TreeMap) RangeEntry

func (tMap *TreeMap) RangeEntry(consumer func(k, v interface{}))

func (*TreeMap) RangeLessThan

func (tMap *TreeMap) RangeLessThan(key interface{}, consumer func(k, v interface{}))

func (*TreeMap) RemoveKey

func (tMap *TreeMap) RemoveKey(key interface{})

func (*TreeMap) Size

func (tMap *TreeMap) Size() int64

Jump to

Keyboard shortcuts

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