From 65063135c95a945024a0262ca4e91ef740730a9b Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Mon, 23 Dec 2013 14:14:03 +0400 Subject: [PATCH] PackageCollection class. --- debian/package.go | 33 +++++++++++++++++++++++++++++++++ debian/package_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/debian/package.go b/debian/package.go index 8558c5ea..8ee5d1f9 100644 --- a/debian/package.go +++ b/debian/package.go @@ -3,6 +3,7 @@ package debian import ( "bytes" "fmt" + "github.com/smira/aptly/database" "github.com/smira/aptly/utils" debc "github.com/smira/godebiancontrol" "github.com/ugorji/go/codec" @@ -108,3 +109,35 @@ func (p *Package) VerifyFile(filepath string) bool { } return st.Size() == p.Filesize } + +// PackageCollection does management of packages in DB +type PackageCollection struct { + db database.Storage +} + +// NewPackageCollection creates new PackageCollection and binds it to database +func NewPackageCollection(db database.Storage) *PackageCollection { + return &PackageCollection{ + db: db, + } +} + +// ByKey find package in DB by its key +func (collection *PackageCollection) ByKey(key []byte) (*Package, error) { + encoded, err := collection.db.Get(key) + if err != nil { + return nil, err + } + + p := &Package{} + err = p.Decode(encoded) + if err != nil { + return nil, err + } + return p, nil +} + +// Update adds or updates information about package in DB +func (collection *PackageCollection) Update(p *Package) error { + return collection.db.Put(p.Key(), p.Encode()) +} diff --git a/debian/package_test.go b/debian/package_test.go index ec4c3cb0..f022f1db 100644 --- a/debian/package_test.go +++ b/debian/package_test.go @@ -1,6 +1,7 @@ package debian import ( + "github.com/smira/aptly/database" debc "github.com/smira/godebiancontrol" . "launchpad.net/gocheck" ) @@ -66,3 +67,34 @@ func (s *PackageSuite) TestEquals(c *C) { p2.Depends = []string{"package1"} c.Check(p.Equals(p2), Equals, false) } + +type PackageCollectionSuite struct { + collection *PackageCollection + p *Package + db database.Storage +} + +var _ = Suite(&PackageCollectionSuite{}) + +func (s *PackageCollectionSuite) SetUpTest(c *C) { + para := make(debc.Paragraph) + for k, v := range packagePara { + para[k] = v + } + s.p = NewPackageFromControlFile(para) + s.db, _ = database.OpenDB(c.MkDir()) + s.collection = NewPackageCollection(s.db) +} + +func (s *PackageCollectionSuite) TearDownTest(c *C) { + s.db.Close() +} + +func (s *PackageCollectionSuite) TestUpdateByKey(c *C) { + err := s.collection.Update(s.p) + c.Assert(err, IsNil) + + p2, err := s.collection.ByKey(s.p.Key()) + c.Assert(err, IsNil) + c.Assert(p2.Equals(s.p), Equals, true) +}