Skip to content

Commit

Permalink
add row
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Jan 16, 2022
1 parent 3915983 commit b4d8781
Show file tree
Hide file tree
Showing 13 changed files with 494 additions and 229 deletions.
8 changes: 4 additions & 4 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c *Collection) InsertObjectWithTTL(obj Object, ttl time.Duration) (index u
}

// Insert executes a mutable cursor trasactionally at a new offset.
func (c *Collection) Insert(fn func(txn *Txn) error) (index uint32, err error) {
func (c *Collection) Insert(fn func(Row) error) (index uint32, err error) {
err = c.Query(func(txn *Txn) (innerErr error) {
index, innerErr = txn.Insert(fn)
return
Expand All @@ -149,7 +149,7 @@ func (c *Collection) Insert(fn func(txn *Txn) error) (index uint32, err error) {

// InsertWithTTL executes a mutable cursor trasactionally at a new offset and sets the expiration time
// based on the specified time-to-live and returns the allocated index.
func (c *Collection) InsertWithTTL(ttl time.Duration, fn func(txn *Txn) error) (index uint32, err error) {
func (c *Collection) InsertWithTTL(ttl time.Duration, fn func(Row) error) (index uint32, err error) {
err = c.Query(func(txn *Txn) (innerErr error) {
index, innerErr = txn.InsertWithTTL(ttl, fn)
return
Expand Down Expand Up @@ -278,15 +278,15 @@ func (c *Collection) DropIndex(indexName string) error {

// QueryAt jumps at a particular offset in the collection, sets the cursor to the
// provided position and executes given callback fn.
func (c *Collection) QueryAt(idx uint32, fn func(txn *Txn) error) error {
func (c *Collection) QueryAt(idx uint32, fn func(Row) error) error {
return c.Query(func(txn *Txn) error {
return txn.QueryAt(idx, fn)
})
}

// QueryAt jumps at a particular key in the collection, sets the cursor to the
// provided position and executes given callback fn.
func (c *Collection) QueryKey(key string, fn func(txn *Txn) error) error {
func (c *Collection) QueryKey(key string, fn func(Row) error) error {
return c.Query(func(txn *Txn) error {
return txn.QueryKey(key, fn)
})
Expand Down
94 changes: 43 additions & 51 deletions collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ import (

/*
cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
BenchmarkCollection/insert-8 2359 460067 ns/op 24354 B/op 500 allocs/op
BenchmarkCollection/select-at-8 39667978 29.02 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/scan-8 2331 493212 ns/op 101 B/op 0 allocs/op
BenchmarkCollection/count-8 630602 1776 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/range-8 29124 41815 ns/op 12 B/op 0 allocs/op
BenchmarkCollection/update-at-8 3053928 401.5 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/update-all-8 1401 933124 ns/op 3932 B/op 0 allocs/op
BenchmarkCollection/delete-at-8 5927190 181.0 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/delete-all-8 2077669 584.4 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/insert-8 2523 469481 ns/op 24356 B/op 500 allocs/op
BenchmarkCollection/select-at-8 22194190 54.23 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/scan-8 2068 568953 ns/op 122 B/op 0 allocs/op
BenchmarkCollection/count-8 571449 2057 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/range-8 28660 41695 ns/op 3 B/op 0 allocs/op
BenchmarkCollection/update-at-8 5911978 202.8 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/update-all-8 1280 946272 ns/op 3726 B/op 0 allocs/op
BenchmarkCollection/delete-at-8 6405852 188.9 ns/op 0 B/op 0 allocs/op
BenchmarkCollection/delete-all-8 2073188 562.6 ns/op 0 B/op 0 allocs/op
*/
func BenchmarkCollection(b *testing.B) {
amount := 100000
Expand Down Expand Up @@ -60,8 +60,8 @@ func BenchmarkCollection(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
players.QueryAt(20, func(txn *Txn) error {
name, _ = txn.Enum("name").Get()
players.QueryAt(20, func(r Row) error {
name, _ = r.Enum("name")
return nil
})
}
Expand Down Expand Up @@ -117,9 +117,8 @@ func BenchmarkCollection(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
players.QueryAt(20, func(txn *Txn) error {
balance := txn.Float64("balance")
balance.Set(1.0)
players.QueryAt(20, func(r Row) error {
r.SetFloat64("balance", 1.0)
return nil
})
}
Expand Down Expand Up @@ -184,8 +183,8 @@ func TestCollection(t *testing.T) {
}))

{ // Find the object by its index
assert.NoError(t, col.QueryAt(idx, func(txn *Txn) error {
name, ok := txn.String("name").Get()
assert.NoError(t, col.QueryAt(idx, func(r Row) error {
name, ok := r.String("name")
assert.True(t, ok)
assert.Equal(t, "Roman", name)
return nil
Expand All @@ -194,8 +193,8 @@ func TestCollection(t *testing.T) {

{ // Remove the object
assert.True(t, col.DeleteAt(idx))
assert.Error(t, col.QueryAt(idx, func(txn *Txn) error {
if _, ok := txn.String("name").Get(); !ok {
assert.Error(t, col.QueryAt(idx, func(r Row) error {
if _, ok := r.String("name"); !ok {
return fmt.Errorf("unreachable")
}

Expand All @@ -206,35 +205,33 @@ func TestCollection(t *testing.T) {
{ // Add a new one, should replace
newIdx := col.InsertObject(obj)
assert.Equal(t, idx, newIdx)
assert.NoError(t, col.QueryAt(newIdx, func(txn *Txn) error {
name, ok := txn.String("name").Get()
assert.NoError(t, col.QueryAt(newIdx, func(r Row) error {
name, ok := r.String("name")
assert.True(t, ok)
assert.Equal(t, "Roman", name)
return nil
}))
}

{ // Update the wallet
col.QueryAt(idx, func(txn *Txn) error {
wallet := txn.Float64("wallet")
wallet.Set(1000)
col.QueryAt(idx, func(r Row) error {
r.SetFloat64("wallet", 1000)
return nil
})

col.QueryAt(idx, func(txn *Txn) error {
wallet := txn.Float64("wallet")
isRich := txn.Bool("rich")
col.QueryAt(idx, func(r Row) error {
wallet, ok := r.Float64("wallet")
isRich := r.Bool("rich")

balance, ok := wallet.Get()
assert.True(t, ok)
assert.Equal(t, 1000.0, balance)
assert.True(t, isRich.Get())
assert.Equal(t, 1000.0, wallet)
assert.True(t, isRich)
return nil
})

assert.NoError(t, col.QueryAt(idx, func(txn *Txn) error {
wallet, _ := txn.Float64("wallet").Get()
isRich := txn.Bool("rich").Get()
assert.NoError(t, col.QueryAt(idx, func(r Row) error {
wallet, _ := r.Float64("wallet")
isRich := r.Bool("rich")

assert.Equal(t, 1000.0, wallet)
assert.True(t, isRich)
Expand Down Expand Up @@ -271,8 +268,8 @@ func TestInsertObject(t *testing.T) {
col.InsertObject(Object{"name": "B"})

assert.Equal(t, 2, col.Count())
assert.NoError(t, col.QueryAt(0, func(txn *Txn) error {
name, ok := txn.String("name").Get()
assert.NoError(t, col.QueryAt(0, func(r Row) error {
name, ok := r.String("name")
assert.True(t, ok)
assert.Equal(t, "A", name)
return nil
Expand Down Expand Up @@ -446,8 +443,8 @@ func TestConcurrentPointReads(t *testing.T) {
// Reader
go func() {
for i := 0; i < 10000; i++ {
col.QueryAt(99, func(txn *Txn) error {
_, _ = txn.String("name").Get()
col.QueryAt(99, func(r Row) error {
_, _ = r.String("name")
return nil
})
atomic.AddInt64(&ops, 1)
Expand All @@ -459,10 +456,8 @@ func TestConcurrentPointReads(t *testing.T) {
// Writer
go func() {
for i := 0; i < 10000; i++ {

col.QueryAt(99, func(txn *Txn) error {
name := txn.String("name")
name.Set("test")
col.QueryAt(99, func(r Row) error {
r.SetString("name", "test")
return nil
})
atomic.AddInt64(&ops, 1)
Expand All @@ -479,9 +474,8 @@ func TestInsert(t *testing.T) {
c := NewCollection()
c.CreateColumn("name", ForString())

idx, err := c.Insert(func(txn *Txn) error {
name := txn.String("name")
name.Set("Roman")
idx, err := c.Insert(func(r Row) error {
r.SetString("name", "Roman")
return nil
})
assert.Equal(t, uint32(0), idx)
Expand All @@ -492,15 +486,14 @@ func TestInsertWithTTL(t *testing.T) {
c := NewCollection()
c.CreateColumn("name", ForString())

idx, err := c.InsertWithTTL(time.Hour, func(txn *Txn) error {
name := txn.String("name")
name.Set("Roman")
idx, err := c.InsertWithTTL(time.Hour, func(r Row) error {
r.SetString("name", "Roman")
return nil
})
assert.Equal(t, uint32(0), idx)
assert.NoError(t, err)
assert.NoError(t, c.QueryAt(idx, func(txn *Txn) error {
expire, ok := txn.Int64(expireColumn).Get()
assert.NoError(t, c.QueryAt(idx, func(r Row) error {
expire, ok := r.Int64(expireColumn)
assert.True(t, ok)
assert.NotZero(t, expire)
return nil
Expand Down Expand Up @@ -530,9 +523,8 @@ func TestFindFreeIndex(t *testing.T) {
col := NewCollection()
assert.NoError(t, col.CreateColumn("name", ForString()))
for i := 0; i < 100; i++ {
idx, err := col.Insert(func(txn *Txn) error {
name := txn.String("name")
name.Set("Roman")
idx, err := col.Insert(func(r Row) error {
r.SetString("name", "Roman")
return nil
})
assert.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions column_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ func (s numberWriter) Set(value number) {
s.writer.PutNumber(*s.cursor, value)
}

// Increment atomically adds a delta to the value at the current transaction cursor
func (s numberWriter) Increment(delta number) {
// Add atomically adds a delta to the value at the current transaction cursor
func (s numberWriter) Add(delta number) {
s.writer.AddNumber(*s.cursor, delta)
}

Expand Down
40 changes: 20 additions & 20 deletions column_numbers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b4d8781

Please sign in to comment.