From 6994e35119fe98aaa07e9be150dedad5188a4454 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Tue, 18 Apr 2017 00:37:34 +0300 Subject: [PATCH] ChecksumCollection implementation --- deb/checksum_collection.go | 61 +++++++++++++++++++++++++++++++++ deb/checksum_collection_test.go | 47 +++++++++++++++++++++++++ deb/collections.go | 14 ++++++++ 3 files changed, 122 insertions(+) create mode 100644 deb/checksum_collection.go create mode 100644 deb/checksum_collection_test.go diff --git a/deb/checksum_collection.go b/deb/checksum_collection.go new file mode 100644 index 00000000..81e59460 --- /dev/null +++ b/deb/checksum_collection.go @@ -0,0 +1,61 @@ +package deb + +import ( + "bytes" + + "github.com/smira/aptly/database" + "github.com/smira/aptly/utils" + "github.com/ugorji/go/codec" +) + +// ChecksumCollection does management of ChecksumInfo in DB +type ChecksumCollection struct { + db database.Storage + codecHandle *codec.MsgpackHandle +} + +// NewChecksumCollection creates new ChecksumCollection and binds it to database +func NewChecksumCollection(db database.Storage) *ChecksumCollection { + return &ChecksumCollection{ + db: db, + codecHandle: &codec.MsgpackHandle{}, + } +} + +func (collection *ChecksumCollection) dbKey(path string) []byte { + return []byte("C" + path) +} + +// Get finds checksums in DB by path +func (collection *ChecksumCollection) Get(path string) (*utils.ChecksumInfo, error) { + encoded, err := collection.db.Get(collection.dbKey(path)) + if err != nil { + if err == database.ErrNotFound { + return nil, nil + } + return nil, err + } + + c := &utils.ChecksumInfo{} + + decoder := codec.NewDecoderBytes(encoded, collection.codecHandle) + err = decoder.Decode(c) + if err != nil { + return nil, err + } + + return c, nil +} + +// Update adds or updates information about checksum in DB +func (collection *ChecksumCollection) Update(path string, c *utils.ChecksumInfo) error { + var encodeBuffer bytes.Buffer + + encoder := codec.NewEncoder(&encodeBuffer, collection.codecHandle) + err := encoder.Encode(c) + if err != nil { + return err + } + + return collection.db.Put(collection.dbKey(path), encodeBuffer.Bytes()) +} diff --git a/deb/checksum_collection_test.go b/deb/checksum_collection_test.go new file mode 100644 index 00000000..a93e6d26 --- /dev/null +++ b/deb/checksum_collection_test.go @@ -0,0 +1,47 @@ +package deb + +import ( + "github.com/smira/aptly/database" + "github.com/smira/aptly/utils" + + . "gopkg.in/check.v1" +) + +type ChecksumCollectionSuite struct { + collection *ChecksumCollection + c utils.ChecksumInfo + db database.Storage +} + +var _ = Suite(&ChecksumCollectionSuite{}) + +func (s *ChecksumCollectionSuite) SetUpTest(c *C) { + s.c = utils.ChecksumInfo{ + Size: 124, + MD5: "da39a3ee5e6b4b0d3255bfef95601890afd80709", + SHA1: "da39a3ee5e6b4b0d3255bfef95601890afd80709", + SHA256: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", + } + s.db, _ = database.OpenDB(c.MkDir()) + s.collection = NewChecksumCollection(s.db) +} + +func (s *ChecksumCollectionSuite) TearDownTest(c *C) { + s.db.Close() +} + +func (s *ChecksumCollectionSuite) TestFlow(c *C) { + // checksum not stored + checksum, err := s.collection.Get("some/path") + c.Assert(err, IsNil) + c.Check(checksum, IsNil) + + // store checksum + err = s.collection.Update("some/path", &s.c) + c.Assert(err, IsNil) + + // load it back + checksum, err = s.collection.Get("some/path") + c.Assert(err, IsNil) + c.Check(*checksum, DeepEquals, s.c) +} diff --git a/deb/collections.go b/deb/collections.go index b7cbf2ea..4b810616 100644 --- a/deb/collections.go +++ b/deb/collections.go @@ -15,6 +15,7 @@ type CollectionFactory struct { snapshots *SnapshotCollection localRepos *LocalRepoCollection publishedRepos *PublishedRepoCollection + checksums *ChecksumCollection } // NewCollectionFactory creates new factory @@ -89,6 +90,18 @@ func (factory *CollectionFactory) PublishedRepoCollection() *PublishedRepoCollec return factory.publishedRepos } +// ChecksumCollection returns (or creates) new ChecksumCollection +func (factory *CollectionFactory) ChecksumCollection() *ChecksumCollection { + factory.Lock() + defer factory.Unlock() + + if factory.checksums == nil { + factory.checksums = NewChecksumCollection(factory.db) + } + + return factory.checksums +} + // Flush removes all references to collections, so that memory could be reclaimed func (factory *CollectionFactory) Flush() { factory.Lock() @@ -99,4 +112,5 @@ func (factory *CollectionFactory) Flush() { factory.remoteRepos = nil factory.publishedRepos = nil factory.packages = nil + factory.checksums = nil }