Documentation
¶
Overview ¶
Package data contains the all data related types and functionalities. such as data.Data, data.ValueType, data.Kind, etc.
Index ¶
- Constants
- Variables
- func GetBaseKindSize(k Kind) int
- func IsCompatibleWithType(t1, t2 ValueType) bool
- type BaseValueType
- func (b *BaseValueType) AssignTo(dest any) error
- func (b *BaseValueType) Clone() ValueType
- func (*BaseValueType) GetTypeKind() Kind
- func (b *BaseValueType) GetTypeName() string
- func (b *BaseValueType) GetTypeSize() uint64
- func (b *BaseValueType) GetValue() any
- func (b *BaseValueType) GetValueSize() uint64
- func (b *BaseValueType) Init(parent ValueType)
- func (*BaseValueType) Parse(_ any) error
- func (b *BaseValueType) To(t Type) (ValueType, error)
- type Batch
- type Buffer
- func (b *Buffer) Clear()
- func (b *Buffer) Clone() *Buffer
- func (b *Buffer) Close() error
- func (b *Buffer) GetLength() uint64
- func (b *Buffer) GetSize() uint64
- func (b *Buffer) IsClosed() bool
- func (b *Buffer) IsEmpty() bool
- func (b *Buffer) Length(length uint64)
- func (b *Buffer) Read() (*Set, error)
- func (b *Buffer) Size(size uint64)
- func (b *Buffer) WithObserver(observer BufferObserver) *Buffer
- func (b *Buffer) Write(data ...*Set) error
- type BufferObserver
- type Data
- type Kind
- type KindGroup
- type Map
- func (m *Map) Delete(key string)
- func (m *Map) Get(key string) Type
- func (m *Map) GetIndex(index int) Type
- func (m *Map) GetNullableMap() map[string]bool
- func (m *Map) GetTypeMap() map[string]Type
- func (m *Map) Has(key string) bool
- func (m *Map) HasDefaultValue(key string) bool
- func (m *Map) HasIndex(index int) bool
- func (m *Map) IsNullable(key string) bool
- func (m *Map) Keys() []string
- func (m *Map) KeysExcept(keys []string) []string
- func (m *Map) Len() int
- func (m *Map) NotNullableKeys() []string
- func (m *Map) Set(key string, value Type, options ...bool)
- func (m *Map) Types() []Type
- type Set
- func (d *Set) Add(data *Data)
- func (d *Set) Clone() *Set
- func (d *Set) Get(key string) *Data
- func (d *Set) GetByIndex(index int) *Data
- func (d *Set) GetKeys() []string
- func (d *Set) GetLength() int
- func (d *Set) GetSize() uint64
- func (d *Set) GetStringValues() []string
- func (d *Set) GetValues() []ValueType
- func (d *Set) Has(key string) bool
- func (d *Set) Remove(key string)
- func (d *Set) RemoveByIndex(index int)
- func (d *Set) Set(key string, value ValueType)
- func (d *Set) SetByIndex(index int, value ValueType)
- func (d *Set) String(delimiter string) string
- func (d *Set) Swap(i, j int)
- type Type
- type ValueType
Constants ¶
const ( DefaultMaxBufferSize = 1024 * 1024 * 256 // 256 MB // DefaultMaxBufferLength is the default maximum length of the buffer. DefaultMaxBufferLength = 500000 )
DefaultMaxBufferSize is the default maximum size of the buffer in bytes.
Variables ¶
var ( ErrValueTypeParseFuncNotImplemented = errors.New("value type parse function not implemented") ErrValueTypeParentNotInitialized = errors.New("value type parent not initialized") ErrDataTypeKindNotMatch = errors.New("data type kind not match") ErrInvalidValue = errors.New("invalid passed value") ErrDestMustBePointer = errors.New("destination must be a pointer") ErrDestNotAssignable = errors.New("destination is not assignable") )
var ( ErrBufferAlreadyIsClosed = errors.New("buffer already is closed") ErrBufferIsClosed = errors.New("buffer is closed") )
Functions ¶
func GetBaseKindSize ¶
GetBaseKindSize returns the size of the base kinds such as int, float, bool, etc. in bytes. It returns 0 if the kind is not a base kind.
func IsCompatibleWithType ¶
Types ¶
type BaseValueType ¶
type BaseValueType struct {
// contains filtered or unexported fields
}
BaseValueType implements ValueType interface. It can be embedded in other types to implement ValueType interface quickly.
func (*BaseValueType) AssignTo ¶
func (b *BaseValueType) AssignTo(dest any) error
AssignTo assigns the value to the given destination. It returns ErrDestMustBePointer if the destination is not a pointer. It returns ErrDataTypeKindNotMatch if the destination type kind is not the same as the receiver type kind.
func (*BaseValueType) Clone ¶
func (b *BaseValueType) Clone() ValueType
Clone returns a copy of the receiver.
func (*BaseValueType) GetTypeKind ¶
func (*BaseValueType) GetTypeKind() Kind
GetTypeKind returns the kind of the type. if not implemented by the parent type, it returns KindUnknown.
func (*BaseValueType) GetTypeName ¶
func (b *BaseValueType) GetTypeName() string
GetTypeName returns the name of the type.
func (*BaseValueType) GetTypeSize ¶
func (b *BaseValueType) GetTypeSize() uint64
GetTypeSize returns the size of the type in bytes.
func (*BaseValueType) GetValue ¶
func (b *BaseValueType) GetValue() any
GetValue returns the value stored in the receiver.
func (*BaseValueType) GetValueSize ¶
func (b *BaseValueType) GetValueSize() uint64
GetValueSize returns the size of the value in bytes.
func (*BaseValueType) Init ¶
func (b *BaseValueType) Init(parent ValueType)
Init initializes the parent type.
func (*BaseValueType) Parse ¶
func (*BaseValueType) Parse(_ any) error
Parse parses the value and stores it in the receiver. It should be implemented by the parent type. Otherwise, it returns ErrValueTypeParseFuncNotImplemented.
type Batch ¶
type Batch []*Set
Batch is a collection of data sets.
func (*Batch) Add ¶
Add adds the given data sets to the batch. It does not if the given set is nil. It does not check for duplicate sets.
func (*Batch) Get ¶
Get returns the data set at the given index. It returns nil if the index is out of range.
func (*Batch) GetSize ¶
GetSize returns approximate size of the batch in bytes. It returns 0 if the batch is nil.
func (*Batch) Pop ¶
Pop returns the first data set in the batch and removes it from the batch. It returns nil if the batch is empty.
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a thread-safe data buffer. It is used to buffer data sets before they are written to the database.
func NewBuffer ¶
NewBuffer creates a new buffer with the given maximum size in bytes. If no size is given, the DefaultMaxBufferLength is used. SizeChanged: the maximum size of the buffer in bytes. And cannot be 0.
func (*Buffer) Clone ¶
Clone returns a copy of the buffer. The copy will not be affected by the original buffer. And Clone always returns a not closed buffer.
func (*Buffer) Close ¶
Close will close the buffer. Closed buffer will not accept new data sets. If the buffer is already closed, it will return an error.
func (*Buffer) Length ¶
Length sets the maximum length of the buffer. If the buffer exceeds the maximum length, it will be blocked until the buffer conditions are met.
func (*Buffer) Read ¶
Read pops the first data set from the buffer. If the buffer is empty, it will be blocked until the next data set is written to the buffer. If the buffer is closed, and all data sets have been read, it will return ErrBufferIsClosed.
func (*Buffer) Size ¶
Size sets the maximum size of the buffer in bytes. If the buffer exceeds the maximum size, it will be blocked until the buffer conditions are met.
func (*Buffer) WithObserver ¶
func (b *Buffer) WithObserver(observer BufferObserver) *Buffer
type BufferObserver ¶
type BufferObserver interface {
// SizeChanged is called when the buffer size changes.
SizeChanged(s uint64)
// LengthChanged is called when the buffer length changes.
LengthChanged(l uint64)
Write(n int)
Read(n int)
}
BufferObserver is an interface that can be implemented to observe the buffer.
type Data ¶
Data is a key-value pair. The key is a string and the value is a Type. The value can be any type that implements the ValueType interface. Data is the smallest unit of data in gloader: Data < Set < Batch Data: {key, value} Set: [{key, value}, {key, value}, {key, value}, ...] Batch: [ [{key, value}, {key, value}, {key, value}, ...], [{key, value}, {key, value}, {key, value}, ...], [{key, value}, {key, value}, {key, value}, ...], ...].
func NewData ¶
NewData creates a new data with the given key and value. The value must implement the ValueType interface.
func (*Data) GetValueType ¶
GetValueType returns the value of the data.
type Kind ¶
type Kind uint8
Kind represents the kind of value. It is a subset of the reflect.Kind. The zero Kind is KindUnknown.
const ( KindUnknown Kind = iota KindString KindInt KindFloat KindBool KindTime KindTimestamp KindDuration KindBytes KindArray KindMap KindStruct KindInterface KindPointer KindFunc KindChan KindSlice KindUint KindUint8 KindUint16 KindUint32 KindUint64 KindInt8 KindInt16 KindInt32 KindInt64 KindFloat32 KindFloat64 )
func GetKindFromName ¶
GetKindFromName returns the kind from the given name. It returns KindUnknown if the name is not found.
func (Kind) GetKindGroup ¶
func (Kind) GetReflectKind ¶
GetReflectKind returns the reflect.Kind of the kind.
func (Kind) IsCompatibleWith ¶
type KindGroup ¶
type KindGroup uint8
KindGroup represents the group of kind. The kinds in the same group are compatible.
type Map ¶
type Map struct {
// contains filtered or unexported fields
}
Map is a map of data types. It is used to determine schema of dataCollection. The key is the name of the data type and the value is the data type.
func (*Map) Get ¶
Get returns the data type with the given name. It returns nil if the data type does not exist.
func (*Map) GetNullableMap ¶
func (*Map) GetTypeMap ¶
func (*Map) HasDefaultValue ¶
func (*Map) IsNullable ¶
func (*Map) KeysExcept ¶
func (*Map) NotNullableKeys ¶
type Set ¶
type Set []*Data
Set is a collection of related data. It's used to collect and organize data. also in relational database, it's called a row.
func (*Set) Get ¶
Get returns the data with the given key. It returns nil if the data does not exist.
func (*Set) GetByIndex ¶
GetByIndex returns the data at the given index. It returns nil if the index is out of range.
func (*Set) GetStringValues ¶
GetStringValues returns the string values of the data set.
func (*Set) Remove ¶
Remove removes the data with the given key. if the key is not found, it does nothing.
func (*Set) RemoveByIndex ¶
RemoveByIndex removes the data at the given index. It does nothing if the index is out of range.
func (*Set) Set ¶
Set sets the value of the data with the given key. If the data does not exist, it creates new data with the given key and value.
func (*Set) SetByIndex ¶
SetByIndex sets the value of the data at the given index. It does nothing if the index is out of range.
type Type ¶
type Type interface {
// GetTypeName returns the name of the type.
GetTypeName() string
// GetTypeKind returns the kind of the type.
GetTypeKind() Kind
// GetTypeSize returns the size of 1 length value of the type in bytes.
GetTypeSize() uint64
}
Type is the interface that has the basic data type methods. It is provided GetTypeName, GetTypeKind methods. All driver types must implement this interface. A Type represents a type in the database.
type ValueType ¶
type ValueType interface {
Type // Type interface
// Parse parses the value and stores it in the receiver.
// It can parse nil and any type that is compatible with the type kind.
Parse(v any) error
// GetValueSize returns the size of the value in bytes.
GetValueSize() uint64
// GetValue returns the value stored in the receiver.
// returned value kind is the same as the type kind which is accessible by GetTypeKind method.
// note: the returned value could be nil.
GetValue() any
// To convert the value to the given type.
To(t Type) (ValueType, error)
// AssignTo assigns the value to the given pointer destination.
// The t should be a pointer to a type that is same with the type kind.
AssignTo(t any) error
// Clone returns a copy of the receiver.
Clone() ValueType
}
ValueType is a Type that holds a value with same kind. It is provided Parse, GetValueSize, GetValue, To, AssignTo, Clone methods. For handling values.