diff --git a/collection.go b/collection.go index 028fd3a..1b81de3 100644 --- a/collection.go +++ b/collection.go @@ -239,7 +239,7 @@ func (c *Collection) DropTrigger(triggerName string) error { } // CreateIndex creates an index column with a specified name which depends on a given -// column. The index function will be applied on the values of the column whenever +// data column. The index function will be applied on the values of the column whenever // a new row is added or updated. func (c *Collection) CreateIndex(indexName, columnName string, fn func(r Reader) bool) error { if fn == nil || columnName == "" || indexName == "" { @@ -275,6 +275,8 @@ func (c *Collection) CreateIndex(indexName, columnName string, fn func(r Reader) return nil } +// CreateSortIndex creates a sorted index column with a specified name which depends +// on a given data column. func (c *Collection) CreateSortIndex(indexName, columnName string) error { if columnName == "" || indexName == "" { return fmt.Errorf("column: create index must specify name & column") @@ -295,7 +297,6 @@ func (c *Collection) CreateSortIndex(indexName, columnName string) error { // Create and add the index column, index := newSortIndex(indexName, columnName) c.lock.Lock() - // index.Grow(uint32(c.opts.Capacity)) c.cols.Store(indexName, index) c.cols.Store(columnName, column, index) c.lock.Unlock() diff --git a/column_index.go b/column_index.go index 26eb091..6980347 100644 --- a/column_index.go +++ b/column_index.go @@ -5,6 +5,7 @@ package column import ( "strings" + "github.com/kelindar/bitmap" "github.com/kelindar/column/commit" @@ -163,27 +164,27 @@ func (c *columnTrigger) Snapshot(chunk commit.Chunk, dst *commit.Buffer) { // ----------------------- Sorted Index -------------------------- -type SortIndexItem struct { - Key string - Value uint32 +type sortIndexItem struct { + Key string + Value uint32 } // columnSortIndex implements a constantly sorted column via BTree type columnSortIndex struct { - btree *btree.BTreeG[SortIndexItem] // 1 constantly sorted data structure - backMap map[uint32]string // for constant key lookups - name string // The name of the target column + btree *btree.BTreeG[sortIndexItem] // 1 constantly sorted data structure + backMap map[uint32]string // for constant key lookups + name string // The name of the target column } // newSortIndex creates a new bitmap index column. func newSortIndex(indexName, columnName string) *column { - byKeys := func (a, b SortIndexItem) bool { + byKeys := func(a, b sortIndexItem) bool { return a.Key < b.Key } return columnFor(indexName, &columnSortIndex{ - btree: btree.NewBTreeG[SortIndexItem](byKeys), + btree: btree.NewBTreeG(byKeys), backMap: make(map[uint32]string), - name: columnName, + name: columnName, }) } @@ -197,7 +198,6 @@ func (c *columnSortIndex) Column() string { return c.name } - // Apply applies a set of operations to the column. func (c *columnSortIndex) Apply(chunk commit.Chunk, r *commit.Reader) { @@ -207,28 +207,27 @@ func (c *columnSortIndex) Apply(chunk commit.Chunk, r *commit.Reader) { switch r.Type { case commit.Put: if delKey, exists := c.backMap[r.Index()]; exists { - c.btree.Delete(SortIndexItem{ - Key: delKey, + c.btree.Delete(sortIndexItem{ + Key: delKey, Value: r.Index(), }) } upsertKey := strings.Clone(r.String()) // alloc required c.backMap[r.Index()] = upsertKey - c.btree.Set(SortIndexItem{ - Key: upsertKey, + c.btree.Set(sortIndexItem{ + Key: upsertKey, Value: r.Index(), }) case commit.Delete: delKey, _ := c.backMap[r.Index()] - c.btree.Delete(SortIndexItem{ - Key: delKey, + c.btree.Delete(sortIndexItem{ + Key: delKey, Value: r.Index(), }) } } } - // Value retrieves a value at a specified index. func (c *columnSortIndex) Value(idx uint32) (v interface{}, ok bool) { return nil, false diff --git a/go.mod b/go.mod index 6128ec7..e738b32 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/kelindar/column go 1.19 require ( - github.com/imdario/mergo v0.3.13 github.com/kelindar/bitmap v1.4.1 github.com/kelindar/intmap v1.1.0 github.com/kelindar/iostream v1.3.0 @@ -11,10 +10,10 @@ require ( github.com/kelindar/smutex v1.0.0 github.com/klauspost/compress v1.15.12 github.com/stretchr/testify v1.8.1 + github.com/tidwall/btree v1.6.0 github.com/zeebo/xxh3 v1.0.2 -) -require github.com/tidwall/btree v1.6.0 // indirect +) require ( github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index efcf7ca..d1bb59c 100644 --- a/go.sum +++ b/go.sum @@ -3,7 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= -github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/kelindar/async v1.0.0 h1:oJiFAt3fVB/b5zVZKPBU+pP9lR3JVyeox9pYlpdnIK8= github.com/kelindar/async v1.0.0/go.mod h1:bJRlwaRiqdHi+4dpVDNHdwgyRyk6TxpA21fByLf7hIY= github.com/kelindar/bitmap v1.4.1 h1:Ih0BWMYXkkZxPMU536DsQKRhdvqFl7tuNjImfLJWC6E= @@ -32,8 +31,6 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/tidwall/btree v1.5.2 h1:5eA83Gfki799V3d3bJo9sWk+yL2LRoTEah3O/SA6/8w= -github.com/tidwall/btree v1.5.2/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY= github.com/tidwall/btree v1.6.0 h1:LDZfKfQIBHGHWSwckhXI0RPSXzlo+KYdjK7FWSqOzzg= github.com/tidwall/btree v1.6.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY= github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= @@ -48,6 +45,5 @@ golang.org/x/time v0.2.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/txn.go b/txn.go index 85ccc13..a935ad2 100644 --- a/txn.go +++ b/txn.go @@ -404,7 +404,7 @@ func (txn *Txn) Ascend(sortIndexName string, fn func(idx uint32)) error { // For each btree key, check if the offset is still in // the txn's index & return if true sortIndexCol, _ := sortIndex.Column.(*columnSortIndex) - sortIndexCol.btree.Scan(func (item SortIndexItem) bool { + sortIndexCol.btree.Scan(func(item sortIndexItem) bool { if txn.index.Contains(item.Value) { // chunk := commit.ChunkAt(item.Value) // lock.RLock(uint(chunk))