From d9f9b77d68c1e84261da480aa0f310e9a3bf70c3 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 25 Oct 2015 15:28:01 +0100 Subject: [PATCH] Add Index.Packs() and IDSet.Equals() --- backend/idset.go | 21 +++++++++++++++++++++ repository/index.go | 13 +++++++++++++ repository/index_test.go | 15 +++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/backend/idset.go b/backend/idset.go index 4f27f3489..817f80fff 100644 --- a/backend/idset.go +++ b/backend/idset.go @@ -39,6 +39,27 @@ func (s IDSet) List() IDs { return list } +// Equals returns true iff s equals other. +func (s IDSet) Equals(other IDSet) bool { + if len(s) != len(other) { + return false + } + + for id := range s { + if _, ok := other[id]; !ok { + return false + } + } + + for id := range other { + if _, ok := s[id]; !ok { + return false + } + } + + return true +} + func (s IDSet) String() string { str := s.List().String() if len(str) < 2 { diff --git a/repository/index.go b/repository/index.go index b89d8f25e..66c734585 100644 --- a/repository/index.go +++ b/repository/index.go @@ -205,6 +205,19 @@ func (idx *Index) Each(done chan struct{}) <-chan PackedBlob { return ch } +// Packs returns all packs in this index +func (idx *Index) Packs() backend.IDSet { + idx.m.Lock() + defer idx.m.Unlock() + + packs := backend.NewIDSet() + for _, entry := range idx.pack { + packs.Insert(entry.packID) + } + + return packs +} + // Count returns the number of blobs of type t in the index. func (idx *Index) Count(t pack.BlobType) (n uint) { debug.Log("Index.Count", "counting blobs of type %v", t) diff --git a/repository/index_test.go b/repository/index_test.go index 75d69cd0b..331ce6a7f 100644 --- a/repository/index_test.go +++ b/repository/index_test.go @@ -325,3 +325,18 @@ func TestConvertIndex(t *testing.T) { } }) } + +func TestIndexPacks(t *testing.T) { + idx := repository.NewIndex() + packs := backend.NewIDSet() + + for i := 0; i < 20; i++ { + packID := randomID() + idx.Store(pack.Data, randomID(), packID, 0, 23) + + packs.Insert(packID) + } + + idxPacks := idx.Packs() + Assert(t, packs.Equals(idxPacks), "packs in index do not match packs added to index") +}