Skip to content

Commit

Permalink
collection-level lock
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Jun 23, 2021
1 parent fb9819f commit dab5d0c
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 135 deletions.
13 changes: 6 additions & 7 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ type Object = map[string]interface{}

const (
expireColumn = "expire"
shards = 128
)

// Collection represents a collection of objects in a columnar format
type Collection struct {
lock sync.RWMutex // The lock for fill list
count uint64 // The current count of elements
lock sync.RWMutex // The global lock for both fill-list & transactions
cols columns // The map of columns
fill bitmap.Bitmap // The fill-list
size int // The initial size for new columns
count int // The current count of elements
writer commit.Writer // The commit writer
cancel context.CancelFunc // The cancellation function for the context
}
Expand Down Expand Up @@ -80,10 +81,11 @@ func NewCollection(opts ...Options) *Collection {

// next finds the next free index in the collection, atomically.
func (c *Collection) next() uint32 {
atomic.AddUint64(&c.count, 1)

c.lock.Lock()
idx := c.findFreeIndex()
c.fill.Set(idx)
c.count++
c.lock.Unlock()
return idx
}
Expand Down Expand Up @@ -152,10 +154,7 @@ func (c *Collection) DeleteAt(idx uint32) (deleted bool) {

// Count returns the total number of elements in the collection.
func (c *Collection) Count() (count int) {
c.lock.RLock()
count = c.count
c.lock.RUnlock()
return
return int(atomic.LoadUint64(&c.count))
}

// CreateColumnsOf registers a set of columns that are present in the target object.
Expand Down
82 changes: 8 additions & 74 deletions column.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package column

import (
"reflect"
"sync"

"github.com/kelindar/bitmap"
"github.com/kelindar/column/commit"
Expand Down Expand Up @@ -117,7 +116,7 @@ func ForKind(kind reflect.Kind) Column {

// column represents a column wrapper that synchronizes operations
type column struct {
sync.RWMutex
//sync.RWMutex
Column
kind columnType // The type of the colum
name string // The name of the column
Expand All @@ -142,27 +141,6 @@ func (c *column) IsTextual() bool {
return (c.kind & typeTextual) == typeTextual
}

// Intersect performs a logical and operation and updates the destination bitmap.
func (c *column) Intersect(dst *bitmap.Bitmap) {
c.RLock()
dst.And(*c.Index())
c.RUnlock()
}

// Difference performs a logical and not operation and updates the destination bitmap.
func (c *column) Difference(dst *bitmap.Bitmap) {
c.RLock()
dst.AndNot(*c.Index())
c.RUnlock()
}

// Union performs a logical or operation and updates the destination bitmap.
func (c *column) Union(dst *bitmap.Bitmap) {
c.RLock()
dst.Or(*c.Index())
c.RUnlock()
}

// Update performs a series of updates at once
func (c *column) Update(updates []commit.Update, growUntil uint32) {
c.Column.Grow(growUntil)
Expand All @@ -171,91 +149,47 @@ func (c *column) Update(updates []commit.Update, growUntil uint32) {

// Delete deletes a set of items from the column.
func (c *column) Delete(items *bitmap.Bitmap) {
c.Lock()
c.Column.Delete(items)
c.Unlock()
}

// Contains checks whether the column has a value at a specified index.
func (c *column) Contains(idx uint32) (exists bool) {
c.RLock()
exists = c.Column.Contains(idx)
c.RUnlock()
return
}

// Value retrieves a value at a specified index
func (c *column) Value(idx uint32) (v interface{}, ok bool) {
c.RLock()
v, ok = c.loadValue(idx)
c.RUnlock()
v, ok = c.Column.Value(idx)
return
}

// Value retrieves a value at a specified index
func (c *column) String(idx uint32) (v string, ok bool) {
c.RLock()
v, ok = c.loadString(idx)
c.RUnlock()
return
}

// Float64 retrieves a float64 value at a specified index
func (c *column) Float64(idx uint32) (v float64, ok bool) {
c.RLock()
v, ok = c.loadFloat64(idx)
c.RUnlock()
return
}

// Int64 retrieves an int64 value at a specified index
func (c *column) Int64(idx uint32) (v int64, ok bool) {
c.RLock()
v, ok = c.loadInt64(idx)
c.RUnlock()
return
}

// Uint64 retrieves an uint64 value at a specified index
func (c *column) Uint64(idx uint32) (v uint64, ok bool) {
c.RLock()
v, ok = c.loadUint64(idx)
c.RUnlock()
return
}

// loadValue (unlocked) retrieves a value at a specified index
func (c *column) loadValue(idx uint32) (v interface{}, ok bool) {
v, ok = c.Column.Value(idx)
return
}

// loadFloat64 (unlocked) retrieves a float64 value at a specified index
func (c *column) loadString(idx uint32) (v string, ok bool) {
if column, ok := c.Column.(Textual); ok {
v, ok = column.LoadString(idx)
}
return
}

// loadFloat64 (unlocked) retrieves a float64 value at a specified index
func (c *column) loadFloat64(idx uint32) (v float64, ok bool) {
// Float64 retrieves a float64 value at a specified index
func (c *column) Float64(idx uint32) (v float64, ok bool) {
if n, contains := c.Column.(Numeric); contains {
v, ok = n.LoadFloat64(idx)
}
return
}

// loadInt64 (unlocked) retrieves an int64 value at a specified index
func (c *column) loadInt64(idx uint32) (v int64, ok bool) {
// Int64 retrieves an int64 value at a specified index
func (c *column) Int64(idx uint32) (v int64, ok bool) {
if n, contains := c.Column.(Numeric); contains {
v, ok = n.LoadInt64(idx)
}
return
}

// loadUint64 (unlocked) retrieves an uint64 value at a specified index
func (c *column) loadUint64(idx uint32) (v uint64, ok bool) {
// Uint64 retrieves an uint64 value at a specified index
func (c *column) Uint64(idx uint32) (v uint64, ok bool) {
if n, contains := c.Column.(Numeric); contains {
v, ok = n.LoadUint64(idx)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/million/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func main() {
amount, runs := 1000000, 50
amount, runs := 20000000, 50
players := column.NewCollection(column.Options{
Capacity: amount,
})
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/kelindar/column
go 1.16

require (
github.com/cheekybits/genny v1.0.0
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kelindar/bitmap v1.0.8
github.com/stretchr/testify v1.7.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/cheekybits/genny v1.0.0 h1:uGGa4nei+j20rOSeDeP5Of12XVm7TGUd4dJA9RDitfE=
github.com/cheekybits/genny v1.0.0/go.mod h1:+tQajlRqAUrPI7DOSpB0XAqZYtQakVtB7wXkRAgjxjQ=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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=
Expand Down

0 comments on commit dab5d0c

Please sign in to comment.