Skip to content

Commit 0af9c37

Browse files
authored
Return cursor index (#98)
This PR adds `Index()` function to the transaction, returning the current cursor index.
1 parent 330c5f4 commit 0af9c37

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

txn.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ type Txn struct {
8989
reader *commit.Reader // The commit reader to re-use
9090
}
9191

92+
// Index returns the current index
93+
func (txn *Txn) Index() uint32 {
94+
return txn.cursor
95+
}
96+
9297
// Reset resets the transaction state so it can be used again.
9398
func (txn *Txn) reset() {
9499
for i := range txn.updates {

txn_row.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ type Row struct {
1515
txn *Txn
1616
}
1717

18+
// Index returns the index of the row
19+
func (r Row) Index() uint32 {
20+
return r.txn.Index()
21+
}
22+
1823
// --------------------------- Numbers ----------------------------
1924

2025
// Int loads a int value at a particular column

txn_test.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ package column
55

66
import (
77
"fmt"
8+
"strconv"
89
"sync"
910
"testing"
10-
"strconv"
1111

12-
"github.com/kelindar/xxrand"
1312
"github.com/kelindar/column/commit"
13+
"github.com/kelindar/xxrand"
1414
"github.com/stretchr/testify/assert"
1515
)
1616

@@ -283,46 +283,47 @@ func TestSortIndex(t *testing.T) {
283283
})
284284

285285
// Inserts
286-
c.Insert(func (r Row) error {
286+
c.Insert(func(r Row) error {
287287
r.SetString("col1", "bob")
288288
return nil
289289
})
290-
c.Insert(func (r Row) error {
290+
c.Insert(func(r Row) error {
291291
r.SetString("col1", "carter")
292292
return nil
293293
})
294-
c.Insert(func (r Row) error {
294+
c.Insert(func(r Row) error {
295295
r.SetString("col1", "dan")
296296
return nil
297297
})
298-
c.Insert(func (r Row) error {
298+
c.Insert(func(r Row) error {
299299
r.SetString("col1", "alice")
300300
return nil
301301
})
302-
302+
303303
// Update
304304
assert.NoError(t, c.QueryAt(3, func(r Row) error {
305+
assert.Equal(t, uint32(3), r.Index())
305306
r.SetString("col1", "rob")
306307
return nil
307308
}))
308309
assert.Equal(t, 4, indexCol.Column.(*columnSortIndex).btree.Len())
309-
310+
310311
// Delete
311312
assert.Equal(t, true, c.DeleteAt(1))
312313
assert.Equal(t, 3, indexCol.Column.(*columnSortIndex).btree.Len())
313314

314315
// Range
315-
assert.Error(t, c.Query(func (txn *Txn) error {
316-
return txn.Ascend("nonexistent", func (i uint32) {
316+
assert.Error(t, c.Query(func(txn *Txn) error {
317+
return txn.Ascend("nonexistent", func(i uint32) {
317318
return
318319
})
319320
}))
320321

321322
var res [3]string
322323
var resN int = 0
323-
c.Query(func (txn *Txn) error {
324+
c.Query(func(txn *Txn) error {
324325
col1 := txn.String("col1")
325-
return txn.Ascend("sortedCol1", func (i uint32) {
326+
return txn.Ascend("sortedCol1", func(i uint32) {
326327
name, _ := col1.Get()
327328
res[resN] = name
328329
resN++
@@ -341,16 +342,16 @@ func TestSortIndexLoad(t *testing.T) {
341342

342343
checkN := 0
343344
checks := map[int]string{
344-
4: "Buckner Frazier",
345+
4: "Buckner Frazier",
345346
16: "Marla Todd",
346347
30: "Shelly Kirk",
347348
35: "out of range",
348349
}
349350

350-
players.Query(func (txn *Txn) error {
351+
players.Query(func(txn *Txn) error {
351352
txn = txn.With("human", "mage")
352353
name := txn.String("name")
353-
txn.Ascend("sorted_names", func (i uint32) {
354+
txn.Ascend("sorted_names", func(i uint32) {
354355
n, _ := name.Get()
355356
if res, exists := checks[checkN]; exists {
356357
assert.Equal(t, res, n)
@@ -365,7 +366,7 @@ func TestSortIndexLoad(t *testing.T) {
365366
func TestSortIndexChunks(t *testing.T) {
366367
N := 100_000
367368
obj := map[string]any{
368-
"name": "1",
369+
"name": "1",
369370
"balance": 12.5,
370371
}
371372

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

376377
for i := 0; i < N; i++ {
377-
players.Insert(func (r Row) error {
378+
players.Insert(func(r Row) error {
378379
return r.SetMany(map[string]any{
379-
"name": strconv.Itoa(i),
380+
"name": strconv.Itoa(i),
380381
"balance": float64(i) + 0.5,
381382
})
382383
})
383384
}
384385

385-
players.Query(func (txn *Txn) error {
386+
players.Query(func(txn *Txn) error {
386387
name := txn.String("name")
387-
txn.Ascend("sorted_names", func (i uint32) {
388+
txn.Ascend("sorted_names", func(i uint32) {
388389
n, _ := name.Get()
389-
if i % 400 == 0 {
390+
if i%400 == 0 {
390391
nInt, _ := strconv.Atoi(n)
391392
assert.Equal(t, nInt, int(i))
392393
}
@@ -402,12 +403,12 @@ func TestSortIndexChunks(t *testing.T) {
402403

403404
// Do the same test as before at the same time as other updates
404405
go func() {
405-
players.Query(func (txn *Txn) error {
406+
players.Query(func(txn *Txn) error {
406407
name := txn.String("name")
407408
order.Done() // Ensure this txn begins before update
408-
txn.Ascend("sorted_names", func (i uint32) {
409+
txn.Ascend("sorted_names", func(i uint32) {
409410
n, _ := name.Get()
410-
if i % 400 == 0 {
411+
if i%400 == 0 {
411412
nInt, _ := strconv.Atoi(n)
412413
assert.Equal(t, nInt, int(i))
413414
}
@@ -416,17 +417,17 @@ func TestSortIndexChunks(t *testing.T) {
416417
})
417418
wg.Done()
418419
}()
419-
420+
420421
go func() {
421-
order.Wait() // Wait for scan to begin
422-
idx1 := xxrand.Uint32n(uint32(N / 400)) * 400 // hit checked idxs only
423-
idx2 := xxrand.Uint32n(uint32(N / 400)) * 400
424-
players.Insert(func (r Row) error {
422+
order.Wait() // Wait for scan to begin
423+
idx1 := xxrand.Uint32n(uint32(N/400)) * 400 // hit checked idxs only
424+
idx2 := xxrand.Uint32n(uint32(N/400)) * 400
425+
players.Insert(func(r Row) error {
425426
r.SetString("name", "new")
426427
r.SetFloat64("balance", 43.2)
427428
return nil
428429
})
429-
players.QueryAt(idx1, func (r Row) error {
430+
players.QueryAt(idx1, func(r Row) error {
430431
r.SetString("name", "updated")
431432
return nil
432433
})

0 commit comments

Comments
 (0)