go_kd_segment_tree

package module
v0.0.0-...-442754a Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2020 License: Apache-2.0 Imports: 8 Imported by: 0

README

中文

Introduction

Generally, we need to establish data indexes to speed up data retrieval in the following two situations:

  • Build indexes to optimize data retrieval in some database.
  • Build indexes to optimize constraints matching in some strategy engine, advertising engine, experimental platform engine and so on.

This project is working in the second situation.

avatar

Database indexes are mainly to be used to accelerate data point search, and can be constructed based on B-Tree, B+Tree, R-Tree, KD-Tree, Radix-Tree, HashTree or other data structures. The basic principle of these data point tree indexes is to narrow the query scope by designing decomposition hyperplanes.

avatar

Constraint retrieval can also speed up retrieval by cutting the plane, but it requires some special processing. This project optimized the algorithm for selecting the cutting plane and achieved good results.

avatar

Performance

Select 10 in 10-dimensional discrete space

there are 100 optional values per discrete space

Number of constraints to be queried QPS without index QPS with index speedup
100 80998 485201 6
1000 7767 427899 55
10000 704 239578 340
100000 25 201207 8140

Select 5 in 5-dimensional numerical space

Number of constraints to be queried QPS without index QPS with index speedup
100 93110 270197 3
1000 8904 162522 18
10000 863 90926 105
100000 43 49111 1153

Select 3 for 5-dimensional numerical space and 5 for 20-dimensional discrete space

Number of constraints to be queried QPS without index QPS with index speedup
100 69842 173792 2
1000 6313 88660 14
10000 482 43420 90
100000 17 24226 1399

Example

// build a new tree
tree1 := NewTree(DimTypes{
	"Field1": DimTypeDiscrete, // discrete space
	"Field2": DimTypeReal, // real space
}, &TreeOptions{
	TreeLevelMax:                16, // tree's max height
	LeafNodeDataMax:                 4, // max data number within leaf node
	BranchingDecreasePercentMin: 0.1, // min split ratio
})

err := tree1.Add(Rect{
	"Field1": Measures{MeasureString("one"), MeasureString("two"), MeasureString("three")}, // targeting discrete string 
	"Field2": Interval{MeasureFloat(0.1), MeasureFloat(2.0)}}, // target real interval
	"target1")
if err != nil {
	log.Fatal("node add error:", err)
}
tree1.Build() // build tree

// search point
result := tree1.Search(Point{"Field1": MeasureString("one"), "Field2": MeasureFloat(0.3)})

if len(result) != 1 || result[0].(string) != "target1" {
	log.Fatal("tree search error")
}

Documentation

Index

Constants

View Source
const DefaultBranchDecreasePercentMin = 0.1
View Source
const DefaultLeafDataMax = 4
View Source
const DefaultTreeLevelMax = 16

Variables

View Source
var DimTypeDiscrete = DimType{Type: 0}
View Source
var DimTypeReal = DimType{Type: 1}

Functions

func NewBinaryNode

func NewBinaryNode(tree *Tree,
	segments []*Segment,
	dimName interface{},
	decreasePercent float64,
	level int,
) (*BinaryNode, []*Segment, []*Segment, []*Segment)

func NewHashNode

func NewHashNode(tree *Tree,
	segments []*Segment,
	dimName interface{},
	decreasePercent float64,
	level int,
) (*HashNode, []*Segment, map[Measure][]*Segment)

Types

type BinaryNode

type BinaryNode struct {
	TreeNode

	Tree *Tree

	DimName         interface{}
	Level           int
	DecreasePercent float64

	Mid Measure

	Left  TreeNode
	Right TreeNode

	Pass TreeNode
}

func (*BinaryNode) Dumps

func (node *BinaryNode) Dumps(prefix string) string

func (*BinaryNode) Insert

func (node *BinaryNode) Insert(seg *Segment) error

func (*BinaryNode) Search

func (node *BinaryNode) Search(p Point) []interface{}

func (*BinaryNode) SearchRect

func (node *BinaryNode) SearchRect(r Rect) []interface{}

type ConjunctionDimDiscreteNode

type ConjunctionDimDiscreteNode struct {
	ConjunctionNode
	// contains filtered or unexported fields
}

func NewDiscreteConjunctionNode

func NewDiscreteConjunctionNode(segments []*Segment, dimName interface{}) *ConjunctionDimDiscreteNode

func (*ConjunctionDimDiscreteNode) MaxInvertNode

func (node *ConjunctionDimDiscreteNode) MaxInvertNode() int

func (*ConjunctionDimDiscreteNode) Search

func (node *ConjunctionDimDiscreteNode) Search(measure Measure) []int

func (*ConjunctionDimDiscreteNode) SearchRect

func (node *ConjunctionDimDiscreteNode) SearchRect(scatters interface{}) []int

type ConjunctionDimNode

type ConjunctionDimNode interface {
	Search(measure Measure) []int
	SearchRect(rect interface{}) []int
	MaxInvertNode() int
}

type ConjunctionDimRealNode

type ConjunctionDimRealNode struct {
	ConjunctionDimNode
	// contains filtered or unexported fields
}

func NewConjunctionRealNode

func NewConjunctionRealNode(segments []*Segment, dimName interface{}) *ConjunctionDimRealNode

func (*ConjunctionDimRealNode) MaxInvertNode

func (dimNode *ConjunctionDimRealNode) MaxInvertNode() int

func (*ConjunctionDimRealNode) Search

func (dimNode *ConjunctionDimRealNode) Search(measure Measure) []int

func (*ConjunctionDimRealNode) SearchRect

func (dimNode *ConjunctionDimRealNode) SearchRect(measure interface{}) []int

type ConjunctionNode

type ConjunctionNode struct {
	TreeNode

	Tree *Tree

	DimName         interface{}
	Level           int
	DecreasePercent float64
	// contains filtered or unexported fields
}

func NewConjunctionNode

func NewConjunctionNode(tree *Tree,
	segments []*Segment,
	dimName interface{},
	decreasePercent float64,
	level int,
) *ConjunctionNode

func (*ConjunctionNode) Dumps

func (node *ConjunctionNode) Dumps(prefix string) string

func (*ConjunctionNode) Insert

func (node *ConjunctionNode) Insert(seg *Segment) error

func (*ConjunctionNode) MaxInvertNodeNum

func (node *ConjunctionNode) MaxInvertNodeNum() int

func (*ConjunctionNode) Search

func (node *ConjunctionNode) Search(p Point) []interface{}

func (*ConjunctionNode) SearchRect

func (node *ConjunctionNode) SearchRect(r Rect) []interface{}

type DimType

type DimType struct{ Type int }

type DimTypes

type DimTypes map[interface{}]DimType

type HashNode

type HashNode struct {
	TreeNode

	Tree            *Tree
	DimName         interface{}
	Level           int
	DecreasePercent float64
	// contains filtered or unexported fields
}

func (*HashNode) Dumps

func (node *HashNode) Dumps(prefix string) string

func (*HashNode) Insert

func (node *HashNode) Insert(seg *Segment) error

func (*HashNode) Search

func (node *HashNode) Search(p Point) []interface{}

func (*HashNode) SearchRect

func (node *HashNode) SearchRect(r Rect) []interface{}

type Interval

type Interval [2]Measure

func (Interval) Contains

func (i Interval) Contains(p Measure) bool

type Intervals

type Intervals []Interval

type LeafNode

type LeafNode struct {
	TreeNode
	Segments []*Segment
}

func (*LeafNode) Dumps

func (node *LeafNode) Dumps(prefix string) string

func (*LeafNode) Insert

func (node *LeafNode) Insert(seg *Segment) error

func (*LeafNode) Search

func (node *LeafNode) Search(p Point) []interface{}

func (*LeafNode) SearchRect

func (node *LeafNode) SearchRect(r Rect) []interface{}

type Measure

type Measure interface {
	Bigger(b interface{}) bool
	Smaller(b interface{}) bool
	Equal(b interface{}) bool
	BiggerOrEqual(b interface{}) bool
	SmallerOrEqual(b interface{}) bool
}

type MeasureFloat

type MeasureFloat float64

func (MeasureFloat) Bigger

func (a MeasureFloat) Bigger(b interface{}) bool

func (MeasureFloat) BiggerOrEqual

func (a MeasureFloat) BiggerOrEqual(b interface{}) bool

func (MeasureFloat) Equal

func (a MeasureFloat) Equal(b interface{}) bool

func (MeasureFloat) Smaller

func (a MeasureFloat) Smaller(b interface{}) bool

func (MeasureFloat) SmallerOrEqual

func (a MeasureFloat) SmallerOrEqual(b interface{}) bool

type MeasureString

type MeasureString string

func (MeasureString) Bigger

func (a MeasureString) Bigger(b interface{}) bool

func (MeasureString) BiggerOrEqual

func (a MeasureString) BiggerOrEqual(b interface{}) bool

func (MeasureString) Equal

func (a MeasureString) Equal(b interface{}) bool

func (MeasureString) Smaller

func (a MeasureString) Smaller(b interface{}) bool

func (MeasureString) SmallerOrEqual

func (a MeasureString) SmallerOrEqual(b interface{}) bool

type MeasureTime

type MeasureTime time.Time

func (MeasureTime) Bigger

func (f MeasureTime) Bigger(b interface{}) bool

func (MeasureTime) BiggerOrEqual

func (f MeasureTime) BiggerOrEqual(b interface{}) bool

func (MeasureTime) Equal

func (f MeasureTime) Equal(b interface{}) bool

func (MeasureTime) Smaller

func (f MeasureTime) Smaller(b interface{}) bool

func (MeasureTime) SmallerOrEqual

func (f MeasureTime) SmallerOrEqual(b interface{}) bool

type Measures

type Measures []Measure

func (Measures) Contains

func (s Measures) Contains(p Measure) bool

type Point

type Point map[interface{}]Measure

type Rect

type Rect map[interface{}]interface{}

func (Rect) Clone

func (rect Rect) Clone() Rect

func (Rect) Contains

func (rect Rect) Contains(p Point) bool

func (Rect) HasIntersect

func (rect Rect) HasIntersect(s Rect) bool

func (Rect) Key

func (rect Rect) Key() string

type Segment

type Segment struct {
	Rect Rect
	Data mapset.Set
	// contains filtered or unexported fields
}

func MergeSegments

func MergeSegments(segments []*Segment) []*Segment

func (*Segment) Clone

func (s *Segment) Clone() *Segment

func (*Segment) String

func (s *Segment) String() string

type Tree

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

func NewTree

func NewTree(dimTypes map[interface{}]DimType, opts *TreeOptions) *Tree

func (*Tree) Add

func (tree *Tree) Add(rect Rect, data interface{}) error

func (*Tree) Build

func (tree *Tree) Build()

func (*Tree) Dumps

func (tree *Tree) Dumps() string

func (*Tree) Insert

func (tree *Tree) Insert(rect Rect, data interface{}) error

func (*Tree) Remove

func (tree *Tree) Remove(data interface{})

func (*Tree) Search

func (tree *Tree) Search(p Point) []interface{}

func (*Tree) SearchRect

func (tree *Tree) SearchRect(r Rect) ([]interface{}, error)

type TreeNode

type TreeNode interface {
	Search(p Point) []interface{}
	Insert(seg *Segment) error
	SearchRect(rect Rect) []interface{}
	Dumps(prefix string) string
}

func NewNode

func NewNode(segments []*Segment,
	tree *Tree,
	level int,
) TreeNode

type TreeOptions

type TreeOptions struct {
	TreeLevelMax                int
	LeafNodeDataMax             int
	BranchingDecreasePercentMin float64
	ConjunctionTargetRateMin    float64
}

Jump to

Keyboard shortcuts

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