Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Jun 15, 2021
1 parent ad43347 commit 32eb2f3
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 173 deletions.
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func main(){
// Same condition as above, but we also select the actual names of those
// players and iterate through them.
players.Query(func(txn column.Txn) error {
txn.With("human", "mage", "old").Select(func(v column.Selector) bool {
txn.With("human", "mage", "old").Range(func(v column.Selector) bool {
println(v.String()) // prints the name
return true
}, "name") // The column to select
Expand All @@ -91,14 +91,14 @@ func main(){

```
cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
BenchmarkCollection/insert-8 26252403 48.08 ns/op 2 B/op 0 allocs/op
BenchmarkCollection/fetch-8 29705175 35.74 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/count-8 102036 10886 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/count-idx-8 9166742 127.7 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/find-8 107601 11519 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/find-idx-8 1557285 769.6 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/update-at-8 25257255 47.87 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/update-all-8 51469 22525 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/delete-at-8 2319102 509.2 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/delete-all-8 169375 7377 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/insert-8 27589314 43.05 ns/op 1 B/op 0 allocs/op
BenchmarkCollection/fetch-8 21041593 56.84 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/count-slow-8 109107 11001 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/count-8 9300270 128.6 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/range-8 1871557 641.0 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/select-8 1214799 975.8 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/update-at-8 28573945 41.99 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/update-all-8 184694 6481 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/delete-at-8 2613982 459.1 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/delete-all-8 324321 3730 ns/op 0 B/op 0 allocs/op
```
31 changes: 11 additions & 20 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,40 +166,31 @@ func (c *Collection) Query(fn func(txn *Txn) error) error {
c.lock.RUnlock()

// Execute the query and keep the error for later
err := fn(txn)

// TODO: should we have txn.Commit() ?
if err := fn(txn); err != nil {
txn.Rollback()
releaseTxn(txn)
return err
}

// Now that the iteration has finished, we can range over the pending action
// queue and apply all of the actions that were requested by the cursor.
// queue and apply all of the actions that were requested by the Selector.
txn.Commit()

releaseTxn(txn)

//c.optimize()
return err
}

/*
func (c *Collection) optimize() {
if v := atomic.AddUint64(&c.sort, 1); v%1000 == 0 {
c.cols.Optimize()
}
return nil
}
*/

// Fetch retrieves an object by its handle and returns a selector for it.
func (c *Collection) Fetch(idx uint32) (Cursor, bool) {
// Fetch retrieves an object by its handle and returns a Selector for it.
func (c *Collection) Fetch(idx uint32) (Selector, bool) {
c.lock.RLock()
contains := c.fill.Contains(idx)
c.lock.RUnlock()

// If it's empty or over the sequence, not found
if idx >= uint32(len(c.fill))*64 || !contains {
return Cursor{}, false
return Selector{}, false
}

return Cursor{
return Selector{
index: idx,
owner: c,
}, true
Expand Down
62 changes: 22 additions & 40 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ import (
"github.com/stretchr/testify/assert"
)

// BenchmarkCollection/insert-8 28124318 42.16 ns/op 1 B/op 0 allocs/op
// BenchmarkCollection/fetch-8 25258159 48.32 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/count-slow-8 106300 10820 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/count-8 9076928 132.0 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/range-8 1000000 1046 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/select-8 2005491 603.2 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/select-many-8 2006061 597.5 ns/op 48 B/op 1 allocs/op
// BenchmarkCollection/update-at-8 29553374 41.97 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/update-all-8 146821 8500 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/delete-at-8 2629766 448.8 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/delete-all-8 297520 3497 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/insert-8 27327879 42.93 ns/op 1 B/op 0 allocs/op
// BenchmarkCollection/fetch-8 21438984 55.84 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/count-slow-8 111388 10835 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/count-8 9427291 128.9 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/range-8 1870438 649.2 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/select-8 1238320 972.2 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/update-at-8 27687408 41.31 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/update-all-8 184694 6481 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/delete-at-8 2583535 463.5 ns/op 0 B/op 0 allocs/op
// BenchmarkCollection/delete-all-8 324331 3712 ns/op 0 B/op 0 allocs/op
func BenchmarkCollection(b *testing.B) {
players := loadPlayers()
obj := Object{
Expand Down Expand Up @@ -51,7 +50,7 @@ func BenchmarkCollection(b *testing.B) {
b.ResetTimer()
for n := 0; n < b.N; n++ {
if s, ok := players.Fetch(20); ok {
name = s.StringOf("name")
name = s.StringAt("name")
}
}
assert.NotEmpty(b, name)
Expand Down Expand Up @@ -92,23 +91,6 @@ func BenchmarkCollection(b *testing.B) {
for n := 0; n < b.N; n++ {
players.Query(func(txn *Txn) error {
txn.With("human", "mage", "old").Range(func(v Cursor) bool {
count++
name = v.StringOf("name")
return true
})
return nil
})
}
assert.NotEmpty(b, name)
})

b.Run("select", func(b *testing.B) {
count, name := 0, ""
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
players.Query(func(txn *Txn) error {
txn.With("human", "mage", "old").Select(func(v Selector) bool {
count++
name = v.String()
return true
Expand All @@ -119,17 +101,17 @@ func BenchmarkCollection(b *testing.B) {
assert.NotEmpty(b, name)
})

b.Run("select-many", func(b *testing.B) {
b.Run("select", func(b *testing.B) {
count, name := 0, ""
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
players.Query(func(txn *Txn) error {
txn.With("human", "mage", "old").SelectMany(func(v []Selector) bool {
txn.With("human", "mage", "old").Select(func(v Selector) bool {
count++
name = v[0].String()
name = v.StringAt("name")
return true
}, "name")
})
return nil
})
}
Expand Down Expand Up @@ -158,9 +140,9 @@ func BenchmarkCollection(b *testing.B) {
columnName := columns[n%len(columns)]
players.Query(func(txn *Txn) error {
txn.Range(func(v Cursor) bool {
v.Update(columnName, 1.0)
v.Update(1.0)
return true
})
}, columnName)
return nil
})
}
Expand All @@ -184,7 +166,7 @@ func BenchmarkCollection(b *testing.B) {
for n := 0; n < b.N; n++ {
fill.Clone(&c.fill) // Restore
c.Query(func(txn *Txn) error {
txn.Range(func(v Cursor) bool {
txn.Select(func(v Selector) bool {
v.Delete()
return true
})
Expand Down Expand Up @@ -216,7 +198,7 @@ func TestCollection(t *testing.T) {
{ // Find the object by its index
v, ok := col.Fetch(idx)
assert.True(t, ok)
assert.Equal(t, "Roman", v.StringOf("name"))
assert.Equal(t, "Roman", v.StringAt("name"))
}

{ // Remove the object
Expand All @@ -229,15 +211,15 @@ func TestCollection(t *testing.T) {
idx := col.Insert(obj)
v, ok := col.Fetch(idx)
assert.True(t, ok)
assert.Equal(t, "Roman", v.StringOf("name"))
assert.Equal(t, "Roman", v.StringAt("name"))
}

{ // Update the wallet
col.UpdateAt(0, "wallet", float64(1000))
v, ok := col.Fetch(idx)
assert.True(t, ok)
assert.Equal(t, int64(1000), v.IntOf("wallet"))
assert.Equal(t, true, v.BoolOf("rich"))
assert.Equal(t, int64(1000), v.IntAt("wallet"))
assert.Equal(t, true, v.BoolAt("rich"))
}
}

Expand Down

0 comments on commit 32eb2f3

Please sign in to comment.