Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NumObjects and Initialized with WriteTxn #42

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,29 @@ func TestDB_Observable(t *testing.T) {
require.False(t, ok, "expected channel to close, got event: %+v", ev)
}

func TestDB_NumObjects(t *testing.T) {
t.Parallel()

db, table, _ := newTestDB(t)
rtxn := db.ReadTxn()
assert.Equal(t, 0, table.NumObjects(rtxn))

txn := db.WriteTxn(table)
assert.Equal(t, 0, table.NumObjects(txn))
table.Insert(txn, testObject{ID: uint64(1)})
assert.Equal(t, 1, table.NumObjects(txn))
table.Insert(txn, testObject{ID: uint64(1)})
table.Insert(txn, testObject{ID: uint64(2)})
assert.Equal(t, 2, table.NumObjects(txn))

assert.Equal(t, 0, table.NumObjects(rtxn))
txn.Commit()
assert.Equal(t, 0, table.NumObjects(rtxn))

rtxn = db.ReadTxn()
assert.Equal(t, 2, table.NumObjects(rtxn))
}

func TestDB_All(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -888,6 +911,7 @@ func TestDB_Initialization(t *testing.T) {

wtxn = db.WriteTxn(table)
done1(wtxn)
require.False(t, table.Initialized(txn), "Initialized should be false")
wtxn.Commit()

// Old read transaction unaffected.
Expand All @@ -900,10 +924,11 @@ func TestDB_Initialization(t *testing.T) {

wtxn = db.WriteTxn(table)
done2(wtxn)
assert.True(t, table.Initialized(wtxn), "Initialized should be true")
wtxn.Commit()

txn = db.ReadTxn()
require.True(t, table.Initialized(txn), "Initialized should be false")
require.True(t, table.Initialized(txn), "Initialized should be true")
require.Empty(t, table.PendingInitializers(txn), "There should be no pending initializers")
}

Expand Down
8 changes: 4 additions & 4 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func (t *genTable[Obj]) Initialized(txn ReadTxn) bool {
return len(t.PendingInitializers(txn)) == 0
}
func (t *genTable[Obj]) PendingInitializers(txn ReadTxn) []string {
return txn.getTxn().root[t.pos].pendingInitializers
return txn.getTxn().getTableEntry(t).pendingInitializers
}

func (t *genTable[Obj]) RegisterInitializer(txn WriteTxn, name string) func(WriteTxn) {
Expand Down Expand Up @@ -201,12 +201,12 @@ func (t *genTable[Obj]) RegisterInitializer(txn WriteTxn, name string) func(Writ
}

func (t *genTable[Obj]) Revision(txn ReadTxn) Revision {
return txn.getTxn().getRevision(t)
return txn.getTxn().getTableEntry(t).revision
}

func (t *genTable[Obj]) NumObjects(txn ReadTxn) int {
table := &txn.getTxn().root[t.tablePos()]
return table.indexes[PrimaryIndexPos].tree.Len()
table := txn.getTxn().getTableEntry(t)
return table.numObjects()
}

func (t *genTable[Obj]) Get(txn ReadTxn, q Query[Obj]) (obj Obj, revision uint64, ok bool) {
Expand Down
6 changes: 3 additions & 3 deletions txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ func txnFinalizer(txn *txn) {
}
}

func (txn *txn) getRevision(meta TableMeta) Revision {
func (txn *txn) getTableEntry(meta TableMeta) *tableEntry {
if txn.modifiedTables != nil {
entry := txn.modifiedTables[meta.tablePos()]
if entry != nil {
return entry.revision
return entry
}
}
return txn.root[meta.tablePos()].revision
return &txn.root[meta.tablePos()]
}

// indexReadTxn returns a transaction to read from the specific index.
Expand Down
6 changes: 6 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,16 @@ type tableEntry struct {

func (t *tableEntry) numObjects() int {
indexEntry := t.indexes[t.meta.indexPos(RevisionIndex)]
if indexEntry.txn != nil {
return indexEntry.txn.Len()
}
return indexEntry.tree.Len()
}

func (t *tableEntry) numDeletedObjects() int {
indexEntry := t.indexes[t.meta.indexPos(GraveyardIndex)]
if indexEntry.txn != nil {
return indexEntry.txn.Len()
}
return indexEntry.tree.Len()
}
Loading