ultra_table

package module
v1.0.23 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: MIT Imports: 6 Imported by: 0

README

Overview

Ultra_table provides a possibility to quickly build an in-memory table, It has good performance and high scalability, The current version already supports serialization of gogoproto and easyjson, The most important thing is that he is based on the latest golang 1.18 implementation, which is the implementation of go generics. We try to apply sdk to internal applications. Requires Go 1.18 or newer.

Getting started

To install Ultra_table, use go get:

$ go get github.com/longbridgeapp/ultra-table

Example

package main

import (
	"log"

	ultra_table "github.com/longbridgeapp/ultra-table"
	"github.com/longbridgeapp/ultra-table/test_data/easyjson"
	"github.com/longbridgeapp/ultra-table/test_data/pb"
)

func main() {
	baseEasyjson() //serialization based easyjson
	basegogo()     //serialization based gogo protobuf
}

func baseEasyjson() {
	table := ultra_table.New[*easyjson.Person]()

	err := table.Add(&easyjson.Person{
		Name:     "jacky",
		Phone:    "+8613575468007",
		Age:      31,
		BirthDay: 19901111,
		Gender:   0,
	})
	if err != nil {
		log.Fatal(err)
	}
	err = table.Add(&easyjson.Person{
		Name:     "rose",
		Phone:    "+8613575468008",
		Age:      31,
		BirthDay: 19901016,
		Gender:   1,
	})
	if err != nil {
		log.Fatalln("easyjson", err)
	}

	infos, err := table.GetWithIdx("Phone", "+8613575468007")
	if err != nil {
		log.Fatalln("easyjson", err)
	}
	for i := 0; i < len(infos); i++ {
		log.Printf("easyjson %+v \n", infos[i])
	}

	_, err = table.GetWithIdxIntersection(map[string]interface{}{
		"Age":  31,
		"Name": "rose",
	})
	log.Println("easyjson", err)
}

func basegogo() {
	table := ultra_table.New[*pb.Person]()

	err := table.Add(&pb.Person{
		Name:     "jacky",
		Phone:    "+8613575468007",
		Age:      31,
		BirthDay: 19901111,
		Gender:   pb.Gender_men,
	})
	if err != nil {
		log.Fatal(err)
	}
	err = table.Add(&pb.Person{
		Name:     "rose",
		Phone:    "+8613575468008",
		Age:      31,
		BirthDay: 19901016,
		Gender:   pb.Gender_women,
	})
	if err != nil {
		log.Fatalln("gogo", err)
	}

	infos, err := table.GetWithIdx("Phone", "+8613575468007")
	if err != nil {
		log.Fatalln("gogo", err)
	}
	for i := 0; i < len(infos); i++ {
		log.Printf("gogo %+v \n", infos[i])
	}

	_, err = table.GetWithIdxIntersection(map[string]interface{}{
		"Age":  31,
		"Name": "rose",
	})
	log.Println("gogo", err)
}

Benchmarks

GOMAXPROCS=1 go test -v -bench=. -benchmem benchmark_test.go -benchtime=10s
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkAddWithGoGo                             3587053              4568 ns/op             713 B/op         16 allocs/op
BenchmarkAddWithEasyjson                         2884038              5259 ns/op             667 B/op         16 allocs/op
BenchmarkGetWithUniqueIndexWithGoGo             56969133               220.0 ns/op            29 B/op          3 allocs/op
BenchmarkGetWithUniqueIndexWithEasyjson         27537194               504.4 ns/op            29 B/op          3 allocs/op
BenchmarkGetWithNormalIndex                     25512718               416.2 ns/op           109 B/op          5 allocs/op
BenchmarkGetWithIdxIntersectionNotFound          9719488              1231 ns/op             304 B/op         13 allocs/op
BenchmarkGetWithIdxIntersection                  9484707              1474 ns/op             336 B/op         16 allocs/op
BenchmarkRemoveWithIndex                        60868005               191.7 ns/op            32 B/op          2 allocs/op
BenchmarkUpdateWithIndex                        16691726               710.2 ns/op           148 B/op          5 allocs/op
BenchmarkAddAndRemove                            4346674              2928 ns/op            1002 B/op         45 allocs/op

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RecordNotFound = errors.New("record not found")
	OnlySupportPtr = errors.New("only support ptr")
	ValueNotNil    = errors.New("value can not be nil")
	UniqueIndex    = errors.New("unique index duplicate")
)

Functions

func GetTag added in v0.0.6

func GetTag(dest interface{}, offset uintptr, indexType IndexType) (*tag, error)

Types

type BitMap added in v0.0.7

type BitMap struct {
	*roaring.Bitmap
}

func NewBitMap added in v0.0.7

func NewBitMap() *BitMap

func (*BitMap) Add added in v0.0.7

func (b *BitMap) Add(num uint32)

func (*BitMap) Clear added in v0.0.7

func (b *BitMap) Clear()

func (*BitMap) CloneIterator added in v0.0.7

func (b *BitMap) CloneIterator(f func(uint32))

func (*BitMap) IsEmpty added in v1.0.21

func (b *BitMap) IsEmpty() bool

func (*BitMap) IsExist added in v0.0.7

func (b *BitMap) IsExist(num uint32) bool

func (*BitMap) Iterator added in v0.0.7

func (b *BitMap) Iterator(f func(uint32))

func (*BitMap) Length added in v0.0.7

func (b *BitMap) Length() int

func (*BitMap) Min added in v0.0.7

func (b *BitMap) Min() uint32

func (*BitMap) Remove added in v0.0.7

func (b *BitMap) Remove(num uint32)

type FieldType added in v0.0.6

type FieldType string
const (
	String     FieldType = `string`
	Int        FieldType = `int`
	Int8       FieldType = `int8`
	Int16      FieldType = `int16`
	Int32      FieldType = `int32`
	Int64      FieldType = `int64`
	Uint       FieldType = `uint`
	Uint8      FieldType = `uint8`
	Uint16     FieldType = `uint16`
	Uint32     FieldType = `uint32`
	Uint64     FieldType = `uint64`
	Float32    FieldType = `float32`
	Float64    FieldType = `float64`
	Complex64  FieldType = `complex64`
	Complex128 FieldType = `complex128`
	Byte       FieldType = `byte`
	Rune       FieldType = `rune`
)

type IDeepCp added in v1.0.21

type IDeepCp[T IRow] interface {
	DeepCp() T
}

type IRow added in v1.0.21

type IRow interface {
	Marshal() ([]byte, error)
	Unmarshal([]byte) error
}

type IndexType added in v0.0.6

type IndexType string
const (
	UNIQUE IndexType = `unique`
	NORMAL IndexType = `normal`
)

type UltraTableV2 added in v1.0.21

type UltraTableV2[T IRow] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func New added in v1.0.21

func New[T IRow](deepCp IDeepCp[T]) *UltraTableV2[T]

New ultraTable with generics

func NewWithInitializeData added in v1.0.21

func NewWithInitializeData[T IRow](ts []T, deepCp IDeepCp[T]) (*UltraTableV2[T], error)

func (*UltraTableV2[T]) Add added in v1.0.21

func (u *UltraTableV2[T]) Add(t T) error

func (*UltraTableV2[T]) Cap added in v1.0.21

func (u *UltraTableV2[T]) Cap() uint32

func (*UltraTableV2[T]) Clear added in v1.0.21

func (u *UltraTableV2[T]) Clear()

func (*UltraTableV2[T]) Get added in v1.0.21

func (u *UltraTableV2[T]) Get(f iterator[T]) []T

func (*UltraTableV2[T]) GetAll added in v1.0.21

func (u *UltraTableV2[T]) GetAll() []T

func (*UltraTableV2[T]) GetWithIdx added in v1.0.21

func (u *UltraTableV2[T]) GetWithIdx(idxKey string, vKey interface{}) ([]T, error)

Get benchmark performance near O(1)

func (*UltraTableV2[T]) GetWithIdxAggregate added in v1.0.21

func (u *UltraTableV2[T]) GetWithIdxAggregate(conditions map[string]interface{}) ([]T, error)

GetWithIdxAggregate like where a=? or b=?

func (*UltraTableV2[T]) GetWithIdxAggregateCount added in v1.0.21

func (u *UltraTableV2[T]) GetWithIdxAggregateCount(conditions map[string]interface{}) uint32

func (*UltraTableV2[T]) GetWithIdxCount added in v1.0.21

func (u *UltraTableV2[T]) GetWithIdxCount(idxKey string, vKey interface{}) uint32

func (*UltraTableV2[T]) GetWithIdxIntersection added in v1.0.21

func (u *UltraTableV2[T]) GetWithIdxIntersection(conditions map[string]interface{}) ([]T, error)

GetWithIdxIntersection like where a=? and b=?

func (*UltraTableV2[T]) GetWithIdxIntersectionCount added in v1.0.21

func (u *UltraTableV2[T]) GetWithIdxIntersectionCount(conditions map[string]interface{}) uint32

func (*UltraTableV2[T]) Has added in v1.0.21

func (u *UltraTableV2[T]) Has(f iterator[T]) bool

func (*UltraTableV2[T]) HasWithIdx added in v1.0.21

func (u *UltraTableV2[T]) HasWithIdx(idxKey string, vKey interface{}) bool

func (*UltraTableV2[T]) Len added in v1.0.21

func (u *UltraTableV2[T]) Len() uint32

func (*UltraTableV2[T]) Remove added in v1.0.21

func (u *UltraTableV2[T]) Remove(f iterator[T]) int

Get benchmark performance near O(n), it is recommended to use GetWithIdx

func (*UltraTableV2[T]) RemoveWithIdx added in v1.0.21

func (u *UltraTableV2[T]) RemoveWithIdx(idxKey string, vKey interface{}) int

RemoveWithIdx benchmark performance near O(1)

func (*UltraTableV2[T]) RemoveWithIdxAggregate added in v1.0.21

func (u *UltraTableV2[T]) RemoveWithIdxAggregate(conditions map[string]interface{}) int

func (*UltraTableV2[T]) RemoveWithIdxIntersection added in v1.0.21

func (u *UltraTableV2[T]) RemoveWithIdxIntersection(conditions map[string]interface{}) int

func (*UltraTableV2[T]) SaveWithNormalIdxAggregate added in v1.0.21

func (u *UltraTableV2[T]) SaveWithNormalIdxAggregate(conditions map[string]interface{}, t T) (uint32, error)

func (*UltraTableV2[T]) SaveWithNormalIdxIntersection added in v1.0.21

func (u *UltraTableV2[T]) SaveWithNormalIdxIntersection(conditions map[string]interface{}, t T) (uint32, error)

func (*UltraTableV2[T]) SaveWithUniqueIdx added in v1.0.21

func (u *UltraTableV2[T]) SaveWithUniqueIdx(idxKey string, vKey interface{}, t T) error

SaveWithIdx update or insert

func (*UltraTableV2[T]) UpdateWithNormalIdx added in v1.0.21

func (u *UltraTableV2[T]) UpdateWithNormalIdx(idxKey string, vKey interface{}, t T) (uint32, error)

UpdateWithNormalIdx update record with normal idx

func (*UltraTableV2[T]) UpdateWithUniqueIdx added in v1.0.21

func (u *UltraTableV2[T]) UpdateWithUniqueIdx(idxKey string, vKey interface{}, t T) error

UpdateWithIdx update record with unique idx

Directories

Path Synopsis
test_data
pb

Jump to

Keyboard shortcuts

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