diff options
-rw-r--r-- | TODO.md | 9 | ||||
-rw-r--r-- | recipe_test.go | 24 | ||||
-rw-r--r-- | slice/slice.go (renamed from recipe.go) | 30 | ||||
-rw-r--r-- | slice/slice_test.go | 21 |
4 files changed, 44 insertions, 40 deletions
@@ -46,7 +46,14 @@ priority 2 reunion 7/09 ------------ - [ ] save recipe consecutive chunks as extents -- [ ] **TODO: Priority 1** store recipe and files incrementally. +- [ ] **TODO: Priority 1** store recipe incrementally. + - [x] patch and diff for recipes + - [ ] store recipe updates per version. + - [ ] load all recipes incrementally. +- [ ] **TODO: Priority 2** store file list incrementally. + - [ ] patch and diff for slices + - [ ] store file lists updates per version. + - [ ] load all file lists incrementally. - [x] compress recipe - [x] compress file list - [ ] make size comparison between recipe and chunks with some datasets diff --git a/recipe_test.go b/recipe_test.go deleted file mode 100644 index f5c9b29..0000000 --- a/recipe_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import "testing" - -func TestRecipe(t *testing.T) { - c1 := &StoredChunk{Id: &ChunkId{0, 1}} - c2 := &StoredChunk{Id: &ChunkId{0, 2}} - c3 := &StoredChunk{Id: &ChunkId{0, 3}} - c4 := &StoredChunk{Id: &ChunkId{0, 4}} - c5 := &StoredChunk{Id: &ChunkId{0, 5}} - c6 := &StoredChunk{Id: &ChunkId{0, 6}} - c7 := &StoredChunk{Id: &ChunkId{0, 7}} - source := Recipe{c1, c2, c3, c4} - target := Recipe{c2, c5, c3, c6, c4, c7} - patch := diffRecipe(source, target) - assertSame(t, []RecipeDel{0}, patch.Del, "Patch del part") - assertSame(t, []RecipeIns{ - {1, []Chunk{c5}}, - {3, []Chunk{c6}}, - {5, []Chunk{c7}}, - }, patch.Ins, "Patch ins part") - actual := patchRecipe(source, patch) - assertSame(t, target, actual, "Target obtained from patch application") -} diff --git a/recipe.go b/slice/slice.go index 92385d9..15be5dd 100644 --- a/recipe.go +++ b/slice/slice.go @@ -1,25 +1,25 @@ -package main +package slice import "reflect" -type Recipe []Chunk +type Slice []interface{} -type RecipeDel int +type SliceDel int -type RecipeIns struct { +type SliceIns struct { Idx int - Value []Chunk + Value []interface{} } -type RecipePatch struct { - Del []RecipeDel - Ins []RecipeIns +type SlicePatch struct { + Del []SliceDel + Ins []SliceIns } -func patchRecipe(source Recipe, patch RecipePatch) (target Recipe) { +func PatchSlice(source Slice, patch SlicePatch) (target Slice) { // apply Del part from patch to source into temp size := len(source) - len(patch.Del) - temp := make(Recipe, size) + temp := make(Slice, size) fill := 0 prev := 0 for _, del := range patch.Del { @@ -33,7 +33,7 @@ func patchRecipe(source Recipe, patch RecipePatch) (target Recipe) { for _, ins := range patch.Ins { size += len(ins.Value) } - target = make(Recipe, size) + target = make(Slice, size) fill = 0 prev = 0 tpos := 0 @@ -49,7 +49,7 @@ func patchRecipe(source Recipe, patch RecipePatch) (target Recipe) { return } -func diffRecipe(source Recipe, target Recipe) (patch RecipePatch) { +func DiffSlice(source Slice, target Slice) (patch SlicePatch) { var si, ti int var found bool for ; si < len(source); si++ { @@ -57,18 +57,18 @@ func diffRecipe(source Recipe, target Recipe) (patch RecipePatch) { found = reflect.DeepEqual(target[i], source[si]) if found { if i != ti { - patch.Ins = append(patch.Ins, RecipeIns{ti, target[ti:i]}) + patch.Ins = append(patch.Ins, SliceIns{ti, target[ti:i]}) } ti = i + 1 break } } if !found { - patch.Del = append(patch.Del, RecipeDel(si)) + patch.Del = append(patch.Del, SliceDel(si)) } } if ti < len(target) { - patch.Ins = append(patch.Ins, RecipeIns{ti, target[ti:]}) + patch.Ins = append(patch.Ins, SliceIns{ti, target[ti:]}) } return } diff --git a/slice/slice_test.go b/slice/slice_test.go new file mode 100644 index 0000000..b73946e --- /dev/null +++ b/slice/slice_test.go @@ -0,0 +1,21 @@ +package slice + +import ( + "testing" + + "github.com/n-peugnet/dna-backup/testutils" +) + +func TestPatch(t *testing.T) { + source := Slice{1, 2, 3, 4} + target := Slice{2, 5, 3, 6, 4, 7, 8} + patch := DiffSlice(source, target) + testutils.AssertSame(t, []SliceDel{0}, patch.Del, "Patch del part") + testutils.AssertSame(t, []SliceIns{ + {1, Slice{5}}, + {3, Slice{6}}, + {5, Slice{7, 8}}, + }, patch.Ins, "Patch ins part") + actual := PatchSlice(source, patch) + testutils.AssertSame(t, target, actual, "Target obtained from patch application") +} |