Skip to content

Commit

Permalink
add fetch methods
Browse files Browse the repository at this point in the history
  • Loading branch information
linuskendall authored Dec 19, 2024
1 parent 7a6e3dd commit f396195
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
40 changes: 26 additions & 14 deletions internal/index/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,15 @@ func (d *DB) GetAllSnapshots() (entries []*SnapshotEntry) {
// GetBestSnapshots returns newest-to-oldest snapshots.
// The `max` argument controls the max number of snapshots to return.
// If max is negative, it returns all snapshots.
func (d *DB) GetBestSnapshots(max int) (entries []*SnapshotEntry) {
res, err := d.DB.Txn(false).Get(tableSnapshotEntry, "slot")
func (d *DB) GetBestSnapshotsByGroup(max int, group string) (entries []*SnapshotEntry) {
var res memdb.ResultIterator
var err error
if group != "" {
res, err = d.DB.Txn(false).Get(tableSnapshotEntry, "slot_by_group", group)
} else {
res, err = d.DB.Txn(false).Get(tableSnapshotEntry, "slot")
}

if err != nil {
panic("getting best snapshots failed: " + err.Error())
}
Expand All @@ -102,21 +109,16 @@ func (d *DB) GetBestSnapshots(max int) (entries []*SnapshotEntry) {
}

// Fetches the snapshots that are at a given slot.
func (d *DB) GetSnapshotsAtSlot(slot uint64) (entries []*SnapshotEntry) {
res, err := d.DB.Txn(false).Get(tableSnapshotEntry, "base_slot", slot)
if err != nil {
panic("getting best snapshots failed: " + err.Error())
}
func (d *DB) GetSnapshotsAtSlotByGroup(group string, slot uint64) (entries []*SnapshotEntry) {
var res memdb.ResultIterator
var err error

for entry := res.Next(); entry != nil; entry = res.Next() {
entries = append(entries, entry.(*SnapshotEntry))
if group != "" {
res, err = d.DB.Txn(false).Get(tableSnapshotEntry, "base_slot_by_group", slot, group)
} else {
res, err = d.DB.Txn(false).Get(tableSnapshotEntry, "base_slot", slot)
}
return
}

// Fetches the snapshots that are at a given slot.
func (d *DB) GetSnapshotsAtSlotByGroup(group string, slot uint64) (entries []*SnapshotEntry) {
res, err := d.DB.Txn(false).Get(tableSnapshotEntry, "base_slot_by_group", group, slot)
if err != nil {
panic("getting best snapshots failed: " + err.Error())
}
Expand All @@ -127,6 +129,16 @@ func (d *DB) GetSnapshotsAtSlotByGroup(group string, slot uint64) (entries []*Sn
return
}

// Fetches the best snapshots
func (d *DB) GetBestSnapshots(max int) (entries []*SnapshotEntry) {
return d.GetBestSnapshotsByGroup(max, "")
}

// Fetches the snapshots that are at a given slot.
func (d *DB) GetSnapshotsAtSlot(slot uint64) (entries []*SnapshotEntry) {
return d.GetSnapshotsAtSlotByGroup("", slot)
}

// DeleteOldSnapshots delete snapshot entry older than the given timestamp.
func (d *DB) DeleteOldSnapshots(minTime time.Time) (n int) {
txn := d.DB.Txn(true)
Expand Down
12 changes: 12 additions & 0 deletions internal/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,16 @@ func TestDB(t *testing.T) {
snapshotEntry3,
},
db.GetBestSnapshots(-1))

assert.Equal(t,
[]*SnapshotEntry{
snapshotEntry4,
},
db.GetBestSnapshotsByGroup(-1, "devnet"))

assert.Equal(t,
[]*SnapshotEntry{
snapshotEntry1,
snapshotEntry3

Check failure on line 153 in internal/index/index_test.go

View workflow job for this annotation

GitHub Actions / unit_tests

missing ',' before newline in composite literal

Check failure on line 153 in internal/index/index_test.go

View workflow job for this annotation

GitHub Actions / unit_tests

missing ',' before newline in composite literal
}, )
}

0 comments on commit f396195

Please sign in to comment.