func (collection *PackageCollection) ByKey(key []byte) (*Package, error) {
	encoded, err := collection.db.Get(key)
	if err != nil {
		return nil, err
	}
	p := &Package{}
	if len(encoded) > 2 && (encoded[0] != 0xc1 || encoded[1] != 0x1) {
		oldp := &oldPackage{}
		decoder := codec.NewDecoderBytes(encoded, collection.codecHandle)
		err = decoder.Decode(oldp)
		if err != nil {
			return nil, err
		}
		p.Name = oldp.Name
		p.Version = oldp.Version
		p.Architecture = oldp.Architecture
		p.IsSource = oldp.IsSource
		p.SourceArchitecture = oldp.SourceArchitecture
		p.Source = oldp.Source
		p.Provides = oldp.Provides
		p.deps = &PackageDependencies{
			Depends:           oldp.Depends,
			BuildDepends:      oldp.BuildDepends,
			BuildDependsInDep: oldp.BuildDependsInDep,
			PreDepends:        oldp.PreDepends,
			Suggests:          oldp.Suggests,
			Recommends:        oldp.Recommends,
		}
		p.extra = &oldp.Extra
		for i := range oldp.Files {
			oldp.Files[i].Filename = filepath.Base(oldp.Files[i].Filename)
		}
		p.UpdateFiles(PackageFiles(oldp.Files))
		err = collection.Update(p)
		if err != nil {
			return nil, err
		}
	} else {
		decoder := codec.NewDecoderBytes(encoded[2:], collection.codecHandle)
		err = decoder.Decode(p)
		if err != nil {
			return nil, err
		}
	}
	p.collection = collection
	return p, nil
}
ByKey find package in DB by its key
package deb
import (
	"bytes"
	"fmt"
	"path/filepath"
	"github.com/aptly-dev/aptly/aptly"
	"github.com/aptly-dev/aptly/database"
	"github.com/ugorji/go/codec"
)
type PackageCollection struct {
	db          database.Storage
	codecHandle *codec.MsgpackHandle
}
var (
	_ PackageCatalog = &PackageCollection{}
)
func NewPackageCollection(db database.Storage) *PackageCollection {
	return &PackageCollection{
		db:          db,
		codecHandle: &codec.MsgpackHandle{},
	}
}
type oldPackage struct {
	IsSource           bool
	Name               string
	Version            string
	Architecture       string
	SourceArchitecture string
	Source             string
	Provides           []string
	Depends            []string
	BuildDepends       []string
	BuildDependsInDep  []string
	PreDepends         []string
	Suggests           []string
	Recommends         []string
	Files              []PackageFile
	Extra              Stanza
}