Change the way package key works: now it includes FilesHash. #60

Now duplicate packages (the same name/version/arch) but with different set of files
would be handled as separate entities.
This commit is contained in:
Andrey Smirnov
2014-05-29 16:49:05 +04:00
parent 869e83713d
commit f007465d18
6 changed files with 18 additions and 39 deletions
+1 -1
View File
@@ -29,7 +29,7 @@ func (s *PackageListSuite) SetUpTest(c *C) {
stanza["Package"] = "mars-invaders"
s.p3 = NewPackageFromControlFile(stanza)
stanza = packageStanza.Copy()
stanza["Size"] = "42"
stanza["Source"] = "unknown-planet"
s.p4 = NewPackageFromControlFile(stanza)
stanza = packageStanza.Copy()
stanza["Package"] = "lonely-strangers"
+9 -1
View File
@@ -26,6 +26,8 @@ type Package struct {
IsSource bool
// Hash of files section
FilesHash uint64
// Is this >= 0.6 package?
V06Plus bool
// Offload fields
deps *PackageDependencies
extra *Stanza
@@ -41,6 +43,7 @@ func NewPackageFromControlFile(input Stanza) *Package {
Version: input["Version"],
Architecture: input["Architecture"],
Source: input["Source"],
V06Plus: true,
}
delete(input, "Package")
@@ -89,6 +92,7 @@ func NewSourcePackageFromControlFile(input Stanza) (*Package, error) {
Version: input["Version"],
Architecture: "source",
SourceArchitecture: input["Architecture"],
V06Plus: true,
}
delete(input, "Package")
@@ -167,7 +171,11 @@ func NewSourcePackageFromControlFile(input Stanza) (*Package, error) {
// Key returns unique key identifying package
func (p *Package) Key(prefix string) []byte {
return []byte(prefix + "P" + p.Architecture + " " + p.Name + " " + p.Version)
if p.V06Plus {
return []byte(fmt.Sprintf("%sP%s %s %s %08x", prefix, p.Architecture, p.Name, p.Version, p.FilesHash))
}
return []byte(fmt.Sprintf("%sP%s %s %s", prefix, p.Architecture, p.Name, p.Version))
}
// String creates readable representation
+1 -20
View File
@@ -83,7 +83,7 @@ func (collection *PackageCollection) ByKey(key []byte) (*Package, error) {
p.UpdateFiles(PackageFiles(oldp.Files))
// Save in new format
err = collection.internalUpdate(p)
err = collection.Update(p)
if err != nil {
return nil, err
}
@@ -156,25 +156,6 @@ func (collection *PackageCollection) loadFiles(p *Package) *PackageFiles {
// Update adds or updates information about package in DB checking for conficts first
func (collection *PackageCollection) Update(p *Package) error {
existing, err := collection.ByKey(p.Key(""))
if err == nil {
// if .Files is different, consider to be conflict
if p.FilesHash != existing.FilesHash {
return fmt.Errorf("unable to save: %s, conflict with existing package", p)
}
// ok, .Files are the same, but maybe some meta-data is different, proceed to saving
} else {
if err != database.ErrNotFound {
return err
}
// ok, package doesn't exist yet
}
return collection.internalUpdate(p)
}
// internalUpdate updates information in DB about package and offloaded fields
func (collection *PackageCollection) internalUpdate(p *Package) error {
encoder := codec.NewEncoder(&collection.encodeBuffer, &codec.MsgpackHandle{})
collection.encodeBuffer.Reset()
-14
View File
@@ -48,20 +48,6 @@ func (s *PackageCollectionSuite) TestUpdate(c *C) {
c.Assert(err, IsNil)
c.Assert(res.Equals(s.p), Equals, false)
c.Assert(res.Equals(p2), Equals, true)
// change file info
p2 = NewPackageFromControlFile(packageStanza.Copy())
p2.UpdateFiles(nil)
res, err = s.collection.ByKey(p2.Key(""))
err = s.collection.Update(p2)
c.Assert(err, ErrorMatches, ".*conflict with existing package")
p2 = NewPackageFromControlFile(packageStanza.Copy())
files := p2.Files()
files[0].Checksums.MD5 = "abcdef"
p2.UpdateFiles(files)
res, err = s.collection.ByKey(p2.Key(""))
err = s.collection.Update(p2)
c.Assert(err, ErrorMatches, ".*conflict with existing package")
}
func (s *PackageCollectionSuite) TestByKey(c *C) {
+6 -2
View File
@@ -87,8 +87,12 @@ func (s *PackageSuite) TestWithProvides(c *C) {
func (s *PackageSuite) TestKey(c *C) {
p := NewPackageFromControlFile(s.stanza)
c.Check(p.Key(""), DeepEquals, []byte("Pi386 alien-arena-common 7.40-2"))
c.Check(p.Key("xD"), DeepEquals, []byte("xDPi386 alien-arena-common 7.40-2"))
c.Check(p.Key(""), DeepEquals, []byte("Pi386 alien-arena-common 7.40-2 c8901eedd79ac51b"))
c.Check(p.Key("xD"), DeepEquals, []byte("xDPi386 alien-arena-common 7.40-2 c8901eedd79ac51b"))
p.V06Plus = false
c.Check(p.Key(""), DeepEquals, []byte("Pi386 alien-arena-common 7.40-2"))
c.Check(p.Key("xD"), DeepEquals, []byte("xDPi386 alien-arena-common 7.40-2"))
}
func (s *PackageSuite) TestStanza(c *C) {
+1 -1
View File
@@ -31,7 +31,7 @@ func (s *PackageRefListSuite) SetUpTest(c *C) {
stanza["Package"] = "mars-invaders"
s.p3 = NewPackageFromControlFile(stanza)
stanza = packageStanza.Copy()
stanza["Size"] = "42"
stanza["Source"] = "unknown-planet"
s.p4 = NewPackageFromControlFile(stanza)
stanza = packageStanza.Copy()
stanza["Package"] = "lonely-strangers"