mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-01-12 03:21:33 +00:00
For any action which is multi-step (requires updating more than 1 DB key), use transaction to make update atomic. Also pack big chunks of updates (importing packages for importing and mirror updates) into single transaction to improve aptly performance and get some isolation. Note that still layers up (Collections) provide some level of isolation, so this is going to shine with the future PRs to remove collection locks. Spin-off of #459
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package deb
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"github.com/aptly-dev/aptly/aptly"
|
|
"github.com/aptly-dev/aptly/database"
|
|
"github.com/aptly-dev/aptly/utils"
|
|
"github.com/ugorji/go/codec"
|
|
)
|
|
|
|
// ChecksumCollection does management of ChecksumInfo in DB
|
|
type ChecksumCollection struct {
|
|
db database.ReaderWriter
|
|
codecHandle *codec.MsgpackHandle
|
|
}
|
|
|
|
// NewChecksumCollection creates new ChecksumCollection and binds it to database
|
|
func NewChecksumCollection(db database.ReaderWriter) *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())
|
|
}
|
|
|
|
// Check interface
|
|
var (
|
|
_ aptly.ChecksumStorage = &ChecksumCollection{}
|
|
)
|