aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorn-peugnet <n.peugnet@free.fr>2021-08-31 18:08:14 +0200
committern-peugnet <n.peugnet@free.fr>2021-08-31 18:08:14 +0200
commit72ec5a391646014531727fb3a71987d87b8a6933 (patch)
treef4e7050ba2fec9b8d947e1203929fbd065d518c7
parente207ff4759e9e2aba9b4938f54ed2f31d362f7ee (diff)
downloaddna-backup-72ec5a391646014531727fb3a71987d87b8a6933.tar.gz
dna-backup-72ec5a391646014531727fb3a71987d87b8a6933.zip
split extractTempChunks into 2 funcs
-rw-r--r--repo.go15
-rw-r--r--repo_test.go6
2 files changed, 16 insertions, 5 deletions
diff --git a/repo.go b/repo.go
index a987dc6..ea27fb4 100644
--- a/repo.go
+++ b/repo.go
@@ -321,10 +321,10 @@ func (r *Repo) matchStream(stream io.Reader, fingerprints FingerprintMap) []Chun
return chunks
}
-// extractTempChunks extracts temporary chunks from an array of chunks.
+// mergeTempChunks joins temporary partial chunks from an array of chunks if possible.
// If a chunk is smaller than the size required to calculate a super-feature,
// it is then appended to the previous consecutive temporary chunk if it exists.
-func extractTempChunks(chunks []Chunk) (ret []Chunk) {
+func mergeTempChunks(chunks []Chunk) (ret []Chunk) {
var prev *TempChunk
var curr *TempChunk
for _, c := range chunks {
@@ -335,6 +335,7 @@ func extractTempChunks(chunks []Chunk) (ret []Chunk) {
} else if curr != nil {
ret = append(ret, curr)
}
+ ret = append(ret, c)
curr = nil
prev = nil
} else {
@@ -351,6 +352,16 @@ func extractTempChunks(chunks []Chunk) (ret []Chunk) {
return
}
+func extractTempChunks(chunks []Chunk) (ret []*TempChunk) {
+ for _, c := range chunks {
+ tmp, isTmp := c.(*TempChunk)
+ if isTmp {
+ ret = append(ret, tmp)
+ }
+ }
+ return
+}
+
func writeFile(filePath string, object interface{}) error {
file, err := os.Create(filePath)
if err == nil {
diff --git a/repo_test.go b/repo_test.go
index 134b55c..eaacefd 100644
--- a/repo_test.go
+++ b/repo_test.go
@@ -127,7 +127,7 @@ func TestExtractNewChunks(t *testing.T) {
&TempChunk{value: []byte{'c'}},
&LoadedChunk{id: &ChunkId{0, 1}},
}
- newChunks := extractTempChunks(chunks)
+ newChunks := extractTempChunks(mergeTempChunks(chunks))
assertLen(t, 2, newChunks, "New chunks:")
assertChunkContent(t, []byte{'a'}, newChunks[0], "First new:")
assertChunkContent(t, []byte{'b', 'c'}, newChunks[1], "Second New:")
@@ -177,7 +177,7 @@ func TestBsdiff(t *testing.T) {
go concatFiles(files, writer)
fingerprints, sketches := hashChunks(oldChunks)
recipe := repo.matchStream(reader, fingerprints)
- newChunks := extractTempChunks(recipe)
+ newChunks := extractTempChunks(mergeTempChunks(recipe))
assertLen(t, 2, newChunks, "New chunks:")
for _, c := range newChunks {
id, exists := findSimilarChunk(c, sketches)
@@ -198,7 +198,7 @@ func TestBsdiff(t *testing.T) {
func assertLen(t *testing.T, expected int, actual interface{}, prefix string) {
s := reflect.ValueOf(actual)
if s.Len() != expected {
- t.Error(prefix, "incorrect length, expected:", expected, ", actual:", s.Len())
+ t.Fatal(prefix, "incorrect length, expected:", expected, ", actual:", s.Len())
}
}