Skip to content

Commit

Permalink
Return cursor index (#98)
Browse files Browse the repository at this point in the history
This PR adds `Index()` function to the transaction, returning the
current cursor index.
  • Loading branch information
kelindar authored Oct 13, 2023
1 parent 330c5f4 commit 0af9c37
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
5 changes: 5 additions & 0 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ type Txn struct {
reader *commit.Reader // The commit reader to re-use
}

// Index returns the current index
func (txn *Txn) Index() uint32 {
return txn.cursor
}

// Reset resets the transaction state so it can be used again.
func (txn *Txn) reset() {
for i := range txn.updates {
Expand Down
5 changes: 5 additions & 0 deletions txn_row.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ type Row struct {
txn *Txn
}

// Index returns the index of the row
func (r Row) Index() uint32 {
return r.txn.Index()
}

// --------------------------- Numbers ----------------------------

// Int loads a int value at a particular column
Expand Down
61 changes: 31 additions & 30 deletions txn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ package column

import (
"fmt"
"strconv"
"sync"
"testing"
"strconv"

"github.com/kelindar/xxrand"
"github.com/kelindar/column/commit"
"github.com/kelindar/xxrand"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -283,46 +283,47 @@ func TestSortIndex(t *testing.T) {
})

// Inserts
c.Insert(func (r Row) error {
c.Insert(func(r Row) error {
r.SetString("col1", "bob")
return nil
})
c.Insert(func (r Row) error {
c.Insert(func(r Row) error {
r.SetString("col1", "carter")
return nil
})
c.Insert(func (r Row) error {
c.Insert(func(r Row) error {
r.SetString("col1", "dan")
return nil
})
c.Insert(func (r Row) error {
c.Insert(func(r Row) error {
r.SetString("col1", "alice")
return nil
})

// Update
assert.NoError(t, c.QueryAt(3, func(r Row) error {
assert.Equal(t, uint32(3), r.Index())
r.SetString("col1", "rob")
return nil
}))
assert.Equal(t, 4, indexCol.Column.(*columnSortIndex).btree.Len())

// Delete
assert.Equal(t, true, c.DeleteAt(1))
assert.Equal(t, 3, indexCol.Column.(*columnSortIndex).btree.Len())

// Range
assert.Error(t, c.Query(func (txn *Txn) error {
return txn.Ascend("nonexistent", func (i uint32) {
assert.Error(t, c.Query(func(txn *Txn) error {
return txn.Ascend("nonexistent", func(i uint32) {
return
})
}))

var res [3]string
var resN int = 0
c.Query(func (txn *Txn) error {
c.Query(func(txn *Txn) error {
col1 := txn.String("col1")
return txn.Ascend("sortedCol1", func (i uint32) {
return txn.Ascend("sortedCol1", func(i uint32) {
name, _ := col1.Get()
res[resN] = name
resN++
Expand All @@ -341,16 +342,16 @@ func TestSortIndexLoad(t *testing.T) {

checkN := 0
checks := map[int]string{
4: "Buckner Frazier",
4: "Buckner Frazier",
16: "Marla Todd",
30: "Shelly Kirk",
35: "out of range",
}

players.Query(func (txn *Txn) error {
players.Query(func(txn *Txn) error {
txn = txn.With("human", "mage")
name := txn.String("name")
txn.Ascend("sorted_names", func (i uint32) {
txn.Ascend("sorted_names", func(i uint32) {
n, _ := name.Get()
if res, exists := checks[checkN]; exists {
assert.Equal(t, res, n)
Expand All @@ -365,7 +366,7 @@ func TestSortIndexLoad(t *testing.T) {
func TestSortIndexChunks(t *testing.T) {
N := 100_000
obj := map[string]any{
"name": "1",
"name": "1",
"balance": 12.5,
}

Expand All @@ -374,19 +375,19 @@ func TestSortIndexChunks(t *testing.T) {
players.CreateSortIndex("sorted_names", "name")

for i := 0; i < N; i++ {
players.Insert(func (r Row) error {
players.Insert(func(r Row) error {
return r.SetMany(map[string]any{
"name": strconv.Itoa(i),
"name": strconv.Itoa(i),
"balance": float64(i) + 0.5,
})
})
}

players.Query(func (txn *Txn) error {
players.Query(func(txn *Txn) error {
name := txn.String("name")
txn.Ascend("sorted_names", func (i uint32) {
txn.Ascend("sorted_names", func(i uint32) {
n, _ := name.Get()
if i % 400 == 0 {
if i%400 == 0 {
nInt, _ := strconv.Atoi(n)
assert.Equal(t, nInt, int(i))
}
Expand All @@ -402,12 +403,12 @@ func TestSortIndexChunks(t *testing.T) {

// Do the same test as before at the same time as other updates
go func() {
players.Query(func (txn *Txn) error {
players.Query(func(txn *Txn) error {
name := txn.String("name")
order.Done() // Ensure this txn begins before update
txn.Ascend("sorted_names", func (i uint32) {
txn.Ascend("sorted_names", func(i uint32) {
n, _ := name.Get()
if i % 400 == 0 {
if i%400 == 0 {
nInt, _ := strconv.Atoi(n)
assert.Equal(t, nInt, int(i))
}
Expand All @@ -416,17 +417,17 @@ func TestSortIndexChunks(t *testing.T) {
})
wg.Done()
}()

go func() {
order.Wait() // Wait for scan to begin
idx1 := xxrand.Uint32n(uint32(N / 400)) * 400 // hit checked idxs only
idx2 := xxrand.Uint32n(uint32(N / 400)) * 400
players.Insert(func (r Row) error {
order.Wait() // Wait for scan to begin
idx1 := xxrand.Uint32n(uint32(N/400)) * 400 // hit checked idxs only
idx2 := xxrand.Uint32n(uint32(N/400)) * 400
players.Insert(func(r Row) error {
r.SetString("name", "new")
r.SetFloat64("balance", 43.2)
return nil
})
players.QueryAt(idx1, func (r Row) error {
players.QueryAt(idx1, func(r Row) error {
r.SetString("name", "updated")
return nil
})
Expand Down

0 comments on commit 0af9c37

Please sign in to comment.