diff --git a/internal/index/index.go b/internal/index/index.go index 01b8546..9b0f4dc 100644 --- a/internal/index/index.go +++ b/internal/index/index.go @@ -67,9 +67,8 @@ func (d *DB) GetSnapshotsByTarget(group string, target string) (entries []*Snaps return } -// GetAllSnapshots returns a list of all snapshots. -func (d *DB) GetAllSnapshots() (entries []*SnapshotEntry) { - iter, err := d.DB.Txn(false).LowerBound(tableSnapshotEntry, "id", "", uint64(0)) +func (d *DB) GetAllSnapshotsByGroup(group string) (entries []*SnapshotEntry) { + iter, err := d.DB.Txn(false).LowerBound(tableSnapshotEntry, "id", "", "", uint64(0)) if err != nil { panic("getting best snapshots failed: " + err.Error()) } @@ -78,33 +77,24 @@ func (d *DB) GetAllSnapshots() (entries []*SnapshotEntry) { if el == nil { break } + if group != "" && el.(*SnapshotEntry).Group != group { + continue + } entries = append(entries, el.(*SnapshotEntry)) } return } -func (d *DB) GetAllSnapshotsByGroup(group string) (entries []*SnapshotEntry) { - iter, err := d.DB.Txn(false).LowerBound(tableSnapshotEntry, "id_prefix", group, "", uint64(0)) - if err != nil { - panic("getting best snapshots failed: " + err.Error()) - } - for { - el := iter.Next() - if el == nil { - break - } - entries = append(entries, el.(*SnapshotEntry)) - } - return +// GetAllSnapshots returns a list of all snapshots. +func (d *DB) GetAllSnapshots() (entries []*SnapshotEntry) { + return d.GetAllSnapshotsByGroup("") } // 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) GetBestSnapshotsByGroup(group string, max int) (entries []*SnapshotEntry) { - var res memdb.ResultIterator - var err error - res, err = d.DB.Txn(false).Get(tableSnapshotEntry, "slot") + res, err := d.DB.Txn(false).Get(tableSnapshotEntry, "slot") if err != nil { panic("getting best snapshots failed: " + err.Error()) diff --git a/internal/index/index_test.go b/internal/index/index_test.go index 8cbce51..38a89de 100644 --- a/internal/index/index_test.go +++ b/internal/index/index_test.go @@ -147,10 +147,23 @@ func TestDB(t *testing.T) { }, db.GetBestSnapshotsByGroup("devnet", -1)) + assert.Equal(t, + []*SnapshotEntry{ + snapshotEntry4, + }, + db.GetAllSnapshotsByGroup("devnet")) + assert.Equal(t, []*SnapshotEntry{ snapshotEntry1, snapshotEntry3, }, db.GetBestSnapshotsByGroup("mainnet", -1)) + + assert.Equal(t, + []*SnapshotEntry{ + snapshotEntry1, + snapshotEntry3, + }, + db.GetAllSnapshotsByGroup("mainnet")) } diff --git a/internal/integrationtest/tracker_test.go b/internal/integrationtest/tracker_test.go index 373844d..1994fb4 100644 --- a/internal/integrationtest/tracker_test.go +++ b/internal/integrationtest/tracker_test.go @@ -119,6 +119,7 @@ func TestTracker(t *testing.T) { }, TotalSize: 1, }, + Group: "test", }, { SnapshotInfo: types.SnapshotInfo{ @@ -136,6 +137,7 @@ func TestTracker(t *testing.T) { }, TotalSize: 1, }, + Group: "test", }, { SnapshotInfo: types.SnapshotInfo{ @@ -153,6 +155,7 @@ func TestTracker(t *testing.T) { }, TotalSize: 1, }, + Group: "test", }, { SnapshotInfo: types.SnapshotInfo{ @@ -170,6 +173,7 @@ func TestTracker(t *testing.T) { }, TotalSize: 1, }, + Group: "test", }, }, snaps) diff --git a/internal/tracker/tracker.go b/internal/tracker/tracker.go index 53fbed1..ebc4df8 100644 --- a/internal/tracker/tracker.go +++ b/internal/tracker/tracker.go @@ -51,6 +51,7 @@ func (h *Handler) createJson(c *gin.Context, entries []*index.SnapshotEntry) { sources[i] = types.SnapshotSource{ SnapshotInfo: *entry.Info, Target: entry.Target, + Group: entry.Group, UpdatedAt: entry.UpdatedAt, } } @@ -110,7 +111,7 @@ func (h *Handler) Health(c *gin.Context) { return } query.Max = 1 - entries := h.DB.GetBestSnapshots(query.Max) + entries := h.DB.GetBestSnapshotsByGroup(query.Group, query.Max) var health struct { MaxSnapshot uint64 diff --git a/types/snapshot.go b/types/snapshot.go index 4f1e469..1565413 100644 --- a/types/snapshot.go +++ b/types/snapshot.go @@ -25,6 +25,7 @@ import ( type SnapshotSource struct { SnapshotInfo Target string `json:"target"` + Group string `json:"group"` UpdatedAt time.Time `json:"updated_at"` }