From 438fa4a587b6e83d963bcf45fe94162f913c4fb2 Mon Sep 17 00:00:00 2001 From: Jarek Kowalski Date: Mon, 4 Jun 2018 17:45:31 -0700 Subject: [PATCH] whenever two blocks in index have identical times, prefer the non-deleted one --- internal/packindex/merged.go | 20 +++++++++++++++----- internal/packindex/merged_test.go | 14 ++++++++++++++ 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/internal/packindex/merged.go b/internal/packindex/merged.go index d54b739d1..a1aad2104 100644 --- a/internal/packindex/merged.go +++ b/internal/packindex/merged.go @@ -31,14 +31,13 @@ func (m Merged) EntryCount() int { // GetInfo returns information about a single block. If a block is not found, returns (nil,nil) func (m Merged) GetInfo(contentID string) (*Info, error) { var best *Info - for _, ndx := range m { i, err := ndx.GetInfo(contentID) if err != nil { return nil, err } if i != nil { - if best == nil || i.TimestampSeconds > best.TimestampSeconds { + if best == nil || i.TimestampSeconds > best.TimestampSeconds || (i.TimestampSeconds == best.TimestampSeconds && !i.Deleted) { best = i } } @@ -53,9 +52,20 @@ type nextInfo struct { type nextInfoHeap []*nextInfo -func (h nextInfoHeap) Len() int { return len(h) } -func (h nextInfoHeap) Less(i, j int) bool { return h[i].it.BlockID < h[j].it.BlockID } -func (h nextInfoHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } +func (h nextInfoHeap) Len() int { return len(h) } +func (h nextInfoHeap) Less(i, j int) bool { + if a, b := h[i].it.BlockID, h[j].it.BlockID; a != b { + return a < b + } + + if a, b := h[i].it.TimestampSeconds, h[j].it.TimestampSeconds; a != b { + return a < b + } + + return !h[i].it.Deleted +} + +func (h nextInfoHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } func (h *nextInfoHeap) Push(x interface{}) { *h = append(*h, x.(*nextInfo)) } diff --git a/internal/packindex/merged_test.go b/internal/packindex/merged_test.go index 0b05e72ff..08f1e40b2 100644 --- a/internal/packindex/merged_test.go +++ b/internal/packindex/merged_test.go @@ -14,6 +14,7 @@ func TestMerged(t *testing.T) { packindex.Info{BlockID: "aabbcc", TimestampSeconds: 1, PackBlockID: "xx", PackOffset: 11}, packindex.Info{BlockID: "ddeeff", TimestampSeconds: 1, PackBlockID: "xx", PackOffset: 111}, packindex.Info{BlockID: "z010203", TimestampSeconds: 1, PackBlockID: "xx", PackOffset: 111}, + packindex.Info{BlockID: "de1e1e", TimestampSeconds: 4, PackBlockID: "xx", PackOffset: 111}, ) if err != nil { t.Fatalf("can't create index: %v", err) @@ -21,6 +22,7 @@ func TestMerged(t *testing.T) { i2, err := indexWithItems( packindex.Info{BlockID: "aabbcc", TimestampSeconds: 3, PackBlockID: "yy", PackOffset: 33}, packindex.Info{BlockID: "xaabbcc", TimestampSeconds: 1, PackBlockID: "xx", PackOffset: 111}, + packindex.Info{BlockID: "de1e1e", TimestampSeconds: 4, PackBlockID: "xx", PackOffset: 222, Deleted: true}, ) if err != nil { t.Fatalf("can't create index: %v", err) @@ -47,12 +49,24 @@ func TestMerged(t *testing.T) { var inOrder []string m.Iterate("", func(i packindex.Info) error { inOrder = append(inOrder, i.BlockID) + if i.BlockID == "de1e1e" { + if i.Deleted { + t.Errorf("iteration preferred deleted block over non-deleted") + } + } return nil }) + if i, err := m.GetInfo("de1e1e"); err != nil { + t.Errorf("error getting deleted block info: %v", err) + } else if i.Deleted { + t.Errorf("GetInfo preferred deleted block over non-deleted") + } + expectedInOrder := []string{ "aabbcc", "ddeeff", + "de1e1e", "k010203", "k020304", "xaabbcc",