Skip to content

Commit 42ee5f7

Browse files
committed
feat: add snapshot counter to branches and list of the clones to snapshot details (#572)
1 parent e7fc543 commit 42ee5f7

File tree

5 files changed

+38
-26
lines changed

5 files changed

+38
-26
lines changed

engine/internal/cloning/snapshots.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func (c *Base) fetchSnapshots() error {
3232
var latestSnapshot *models.Snapshot
3333

3434
snapshots := make(map[string]*models.Snapshot, len(entries))
35-
cloneCounter := c.cloneCounter()
35+
cloneCounters := c.counterClones()
3636

3737
for _, entry := range entries {
38-
numClones := 0
38+
cloneList := []string{}
3939

40-
if num, ok := cloneCounter[entry.ID]; ok {
41-
numClones = num
40+
if foundList, ok := cloneCounters[entry.ID]; ok {
41+
cloneList = foundList
4242
}
4343

4444
currentSnapshot := &models.Snapshot{
@@ -49,7 +49,8 @@ func (c *Base) fetchSnapshots() error {
4949
LogicalSize: entry.LogicalReferenced,
5050
Pool: entry.Pool,
5151
Branch: entry.Branch,
52-
NumClones: numClones,
52+
NumClones: len(cloneList),
53+
Clones: cloneList,
5354
}
5455

5556
snapshots[entry.ID] = currentSnapshot
@@ -63,20 +64,21 @@ func (c *Base) fetchSnapshots() error {
6364
return nil
6465
}
6566

66-
func (c *Base) cloneCounter() map[string]int {
67-
cloneCounter := make(map[string]int)
67+
func (c *Base) counterClones() map[string][]string {
68+
clones := make(map[string][]string, 0)
6869

6970
c.cloneMutex.RLock()
7071

7172
for cloneName := range c.clones {
7273
if c.clones[cloneName] != nil && c.clones[cloneName].Clone.Snapshot != nil {
73-
cloneCounter[c.clones[cloneName].Clone.Snapshot.ID]++
74+
snapshotID := c.clones[cloneName].Clone.Snapshot.ID
75+
clones[snapshotID] = append(clones[snapshotID], cloneName)
7476
}
7577
}
7678

7779
c.cloneMutex.RUnlock()
7880

79-
return cloneCounter
81+
return clones
8082
}
8183

8284
func (c *Base) resetSnapshots(snapshotMap map[string]*models.Snapshot, latestSnapshot *models.Snapshot) {

engine/internal/cloning/snapshots_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,13 @@ func TestInitialCloneCounter(t *testing.T) {
158158
c.clones["test_clone002"] = cloneWrapper02
159159
c.clones["test_clone003"] = cloneWrapper03
160160

161-
counters := c.cloneCounter()
161+
counters := c.counterClones()
162162

163-
require.Equal(t, 2, len(counters))
164-
require.Equal(t, 2, counters["testSnapshotID"])
165-
require.Equal(t, 1, counters["testSnapshotID2"])
163+
require.Len(t, counters, 2)
164+
require.Len(t, counters["testSnapshotID"], 2)
165+
require.Len(t, counters["testSnapshotID2"], 1)
166+
require.Len(t, counters["testSnapshotID3"], 0)
167+
require.ElementsMatch(t, []string{"test_clone001", "test_clone002"}, counters["testSnapshotID"])
166168
}
167169

168170
func TestLatestSnapshots(t *testing.T) {

engine/internal/srv/branch.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,15 @@ func (s *Server) listBranches(w http.ResponseWriter, r *http.Request) {
5757
continue
5858
}
5959

60+
numSnapshots, parentSnapshot := findBranchParent(repo.Snapshots, snapshotDetails.ID, branchEntity.Name)
61+
6062
branchView := models.BranchView{
61-
Name: branchEntity.Name,
62-
Parent: findBranchParent(repo.Snapshots, snapshotDetails.ID, branchEntity.Name),
63-
DataStateAt: snapshotDetails.DataStateAt,
64-
SnapshotID: snapshotDetails.ID,
65-
Dataset: snapshotDetails.Dataset,
63+
Name: branchEntity.Name,
64+
Parent: parentSnapshot,
65+
DataStateAt: snapshotDetails.DataStateAt,
66+
SnapshotID: snapshotDetails.ID,
67+
Dataset: snapshotDetails.Dataset,
68+
NumSnapshots: numSnapshots,
6669
}
6770

6871
if position, ok := branchRegistry[branchEntity.Name]; ok {
@@ -83,13 +86,16 @@ func (s *Server) listBranches(w http.ResponseWriter, r *http.Request) {
8386
}
8487
}
8588

86-
func findBranchParent(snapshots map[string]models.SnapshotDetails, parentID, branch string) string {
89+
func findBranchParent(snapshots map[string]models.SnapshotDetails, parentID, branch string) (int, string) {
90+
snapshotCounter := 0
91+
8792
for i := len(snapshots); i > 0; i-- {
8893
snapshotPointer := snapshots[parentID]
94+
snapshotCounter++
8995

9096
if containsString(snapshotPointer.Root, branch) {
9197
if len(snapshotPointer.Branch) > 0 {
92-
return snapshotPointer.Branch[0]
98+
return snapshotCounter, snapshotPointer.Branch[0]
9399
}
94100

95101
break
@@ -102,7 +108,7 @@ func findBranchParent(snapshots map[string]models.SnapshotDetails, parentID, bra
102108
parentID = snapshotPointer.Parent
103109
}
104110

105-
return "-"
111+
return snapshotCounter, "-"
106112
}
107113

108114
func containsString(slice []string, s string) bool {

engine/pkg/models/branch.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ type SnapshotDetails struct {
3333

3434
// BranchView describes branch view.
3535
type BranchView struct {
36-
Name string `json:"name"`
37-
Parent string `json:"parent"`
38-
DataStateAt string `json:"dataStateAt"`
39-
SnapshotID string `json:"snapshotID"`
40-
Dataset string `json:"dataset"`
36+
Name string `json:"name"`
37+
Parent string `json:"parent"`
38+
DataStateAt string `json:"dataStateAt"`
39+
SnapshotID string `json:"snapshotID"`
40+
Dataset string `json:"dataset"`
41+
NumSnapshots int `json:"numSnapshots"`
4142
}
4243

4344
// BranchEntity defines a branch-snapshot pair.

engine/pkg/models/snapshot.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type Snapshot struct {
1313
LogicalSize uint64 `json:"logicalSize"`
1414
Pool string `json:"pool"`
1515
NumClones int `json:"numClones"`
16+
Clones []string `json:"clones"`
1617
Branch string `json:"branch"`
1718
}
1819

0 commit comments

Comments
 (0)