From e49afbcd2dfd3e3fb25f97ee21bf3f8ef475de76 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 29 Jan 2014 17:13:42 +0400 Subject: [PATCH] Snapshot dropping. --- debian/snapshot.go | 26 ++++++++++++++++++++++++++ debian/snapshot_test.go | 21 +++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/debian/snapshot.go b/debian/snapshot.go index 8984da52..221385eb 100644 --- a/debian/snapshot.go +++ b/debian/snapshot.go @@ -224,3 +224,29 @@ func (collection *SnapshotCollection) ForEach(handler func(*Snapshot) error) err func (collection *SnapshotCollection) Len() int { return len(collection.list) } + +// Drop removes snapshot from collection +func (collection *SnapshotCollection) Drop(snapshot *Snapshot) error { + snapshotPosition := -1 + + for i, s := range collection.list { + if s == snapshot { + snapshotPosition = i + break + } + } + + if snapshotPosition == -1 { + panic("snapshot not found!") + } + + collection.list[len(collection.list)-1], collection.list[snapshotPosition], collection.list = + nil, collection.list[len(collection.list)-1], collection.list[:len(collection.list)-1] + + err := collection.db.Delete(snapshot.Key()) + if err != nil { + return err + } + + return collection.db.Delete(snapshot.RefKey()) +} diff --git a/debian/snapshot_test.go b/debian/snapshot_test.go index d621e476..55c9f624 100644 --- a/debian/snapshot_test.go +++ b/debian/snapshot_test.go @@ -170,3 +170,24 @@ func (s *SnapshotCollectionSuite) TestFindByRemoteRepoSource(c *C) { c.Check(s.collection.ByRemoteRepoSource(repo3), DeepEquals, []*Snapshot{}) } + +func (s *SnapshotCollectionSuite) TestDrop(c *C) { + s.collection.Add(s.snapshot1) + s.collection.Add(s.snapshot2) + + snap, _ := s.collection.ByUUID(s.snapshot1.UUID) + c.Check(snap, Equals, s.snapshot1) + + err := s.collection.Drop(s.snapshot1) + c.Check(err, IsNil) + + _, err = s.collection.ByUUID(s.snapshot1.UUID) + c.Check(err, ErrorMatches, "snapshot .* not found") + + collection := NewSnapshotCollection(s.db) + + _, err = collection.ByUUID(s.snapshot1.UUID) + c.Check(err, ErrorMatches, "snapshot .* not found") + + c.Check(func() { s.collection.Drop(s.snapshot1) }, Panics, "snapshot not found!") +}