Browse Source

Simplify File.Clone()

Signed-off-by: Vadim Markovtsev <vadim@sourced.tech>
Vadim Markovtsev 6 years ago
parent
commit
4ec09fb104
3 changed files with 6 additions and 63 deletions
  1. 2 9
      internal/burndown/file.go
  2. 2 52
      internal/burndown/file_test.go
  3. 2 2
      leaves/burndown.go

+ 2 - 9
internal/burndown/file.go

@@ -108,15 +108,8 @@ func NewFileFromTree(keys []int, vals []int, allocator *rbtree.Allocator, update
 // Clone copies the file. It performs a deep copy of the tree;
 // depending on `clearStatuses` the original updaters are removed or not.
 // Any new `updaters` are appended.
-func (file *File) Clone(allocator *rbtree.Allocator, clearStatuses bool, updaters ...Updater) *File {
-	clone := &File{tree: file.tree.Clone(allocator), updaters: file.updaters}
-	if clearStatuses {
-		clone.updaters = []Updater{}
-	}
-	for _, updater := range updaters {
-		clone.updaters = append(clone.updaters, updater)
-	}
-	return clone
+func (file *File) Clone(allocator *rbtree.Allocator) *File {
+	return &File{tree: file.tree.Clone(allocator), updaters: file.updaters}
 }
 
 // Delete deallocates the file.

+ 2 - 52
internal/burndown/file_test.go

@@ -71,7 +71,7 @@ func TestCloneFile(t *testing.T) {
 	file.Update(4, 20, 10, 0)
 	// 0 0 | 20 4 | 30 1 | 50 0 | 130 -1        [0]: 100, [1]: 20, [4]: 10
 	assert.Equal(t, alloc.Size(), 6)
-	clone := file.Clone(alloc.Clone(), false)
+	clone := file.Clone(alloc.Clone())
 	clone.Update(5, 45, 0, 10)
 	// 0 0 | 20 4 | 30 1 | 45 0 | 120 -1        [0]: 95, [1]: 15, [4]: 10
 	clone.Update(6, 45, 5, 0)
@@ -103,56 +103,6 @@ func TestCloneFile(t *testing.T) {
 
 }
 
-func TestCloneFileClearStatus(t *testing.T) {
-	file, status, alloc := fixtureFile()
-	// 0 0 | 100 -1                             [0]: 100
-	file.Update(1, 20, 30, 0)
-	// 0 0 | 20 1 | 50 0 | 130 -1               [0]: 100, [1]: 30
-	file.Update(2, 20, 0, 5)
-	// 0 0 | 20 1 | 45 0 | 125 -1               [0]: 100, [1]: 25
-	file.Update(3, 20, 0, 5)
-	// 0 0 | 20 1 | 40 0 | 120 -1               [0]: 100, [1]: 20
-	file.Update(4, 20, 10, 0)
-	// 0 0 | 20 4 | 30 1 | 50 0 | 130 -1        [0]: 100, [1]: 20, [4]: 10
-	newStatus := map[int]int64{}
-	clone := file.Clone(alloc.Clone(), true, func(a, b, c int) {
-		updateStatusFile(newStatus, a, b, c)
-	})
-	clone.Update(5, 45, 0, 10)
-	// 0 0 | 20 4 | 30 1 | 45 0 | 120 -1        [0]: -5, [1]: -5
-	clone.Update(6, 45, 5, 0)
-	// 0 0 | 20 4 | 30 1 | 45 6 | 50 0 | 125 -1 [0]: -5, [1]: -5, [6]: 5
-	assert.Equal(t, int64(100), status[0])
-	assert.Equal(t, int64(20), status[1])
-	assert.Equal(t, int64(0), status[2])
-	assert.Equal(t, int64(0), status[3])
-	assert.Equal(t, int64(10), status[4])
-	assert.Equal(t, int64(-5), newStatus[0])
-	assert.Equal(t, int64(-5), newStatus[1])
-	assert.Equal(t, int64(0), newStatus[2])
-	assert.Equal(t, int64(0), newStatus[3])
-	assert.Equal(t, int64(0), newStatus[4])
-	assert.Equal(t, int64(0), newStatus[5])
-	assert.Equal(t, int64(5), newStatus[6])
-	dump := file.Dump()
-	// Output:
-	// 0 0
-	// 20 4
-	// 30 1
-	// 50 0
-	// 130 -1
-	assert.Equal(t, "0 0\n20 4\n30 1\n50 0\n130 -1\n", dump)
-	dump = clone.Dump()
-	// Output:
-	// 0 0
-	// 20 4
-	// 30 1
-	// 45 6
-	// 50 0
-	// 125 -1
-	assert.Equal(t, "0 0\n20 4\n30 1\n45 6\n50 0\n125 -1\n", dump)
-}
-
 func TestLenFile(t *testing.T) {
 	file, _, _ := fixtureFile()
 	assert.Equal(t, 100, file.Len())
@@ -635,7 +585,7 @@ func TestFileMerge(t *testing.T) {
 	// 0 0 | 20 1 | 40 0 | 120 -1               [0]: 100, [1]: 20
 	file1.Update(4, 20, 10, 0)
 	// 0 0 | 20 4 | 30 1 | 50 0 | 130 -1        [0]: 100, [1]: 20, [4]: 10
-	file2 := file1.Clone(alloc.Clone(), false)
+	file2 := file1.Clone(alloc.Clone())
 	file1.Update(TreeMergeMark, 60, 30, 30)
 	// 0 0 | 20 4 | 30 1 | 50 0 | 60 M | 90 0 | 130 -1
 	// [0]: 70, [1]: 20, [4]: 10

+ 2 - 2
leaves/burndown.go

@@ -320,7 +320,7 @@ func (analyser *BurndownAnalysis) Fork(n int) []core.PipelineItem {
 		clone.files = map[string]*burndown.File{}
 		clone.fileAllocator = clone.fileAllocator.Clone()
 		for key, file := range analyser.files {
-			clone.files[key] = file.Clone(clone.fileAllocator, false)
+			clone.files[key] = file.Clone(clone.fileAllocator)
 		}
 		result[i] = &clone
 	}
@@ -373,7 +373,7 @@ func (analyser *BurndownAnalysis) Merge(branches []core.PipelineItem) {
 		for _, burn := range all {
 			if burn.files[key] != files[0] {
 				burn.files[key].Delete()
-				burn.files[key] = files[0].Clone(burn.fileAllocator, false)
+				burn.files[key] = files[0].Clone(burn.fileAllocator)
 			}
 		}
 	}