From 49714c02a61256d0af8303f3ed749cf56d5b7977 Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Tue, 5 Jan 2021 11:36:33 -0500 Subject: [PATCH] only touch blobs when you get them --- db/db.go | 51 ++++++++++++++++++++++--------------------- reflector/uploader.go | 2 +- store/dbbacked.go | 16 ++++++-------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/db/db.go b/db/db.go index 9e19eba..e48bcaf 100644 --- a/db/db.go +++ b/db/db.go @@ -111,6 +111,26 @@ func (s *SQL) AddBlobs(hash []string) error { return nil } +func (s *SQL) insertBlobs(hashes []string) error { + var ( + q string + args []interface{} + ) + dayAgo := time.Now().AddDate(0, 0, -1) + q = "insert into blob_ (hash, is_stored, length, last_accessed_at) values " + for _, hash := range hashes { + q += "(?,?,?,?)," + args = append(args, hash, true, stream.MaxBlobSize, dayAgo) + } + q = strings.TrimSuffix(q, ",") + _, err := s.exec(q, args...) + if err != nil { + return err + } + + return nil +} + func (s *SQL) insertBlob(hash string, length int, isStored bool) (int64, error) { if length <= 0 { return 0, errors.Err("length must be positive") @@ -153,26 +173,6 @@ func (s *SQL) insertBlob(hash string, length int, isStored bool) (int64, error) return blobID, nil } -func (s *SQL) insertBlobs(hashes []string) error { - var ( - q string - args []interface{} - ) - dayAgo := time.Now().AddDate(0, 0, -1) - q = "insert into blob_ (hash, is_stored, length, last_accessed_at) values " - for _, hash := range hashes { - q += "(?,?,?,?)," - args = append(args, hash, true, stream.MaxBlobSize, dayAgo) - } - q = strings.TrimSuffix(q, ",") - _, err := s.exec(q, args...) - if err != nil { - return err - } - - return nil -} - func (s *SQL) insertStream(hash string, sdBlobID int64) (int64, error) { var ( q string @@ -212,8 +212,8 @@ func (s *SQL) insertStream(hash string, sdBlobID int64) (int64, error) { } // HasBlob checks if the database contains the blob information. -func (s *SQL) HasBlob(hash string) (bool, error) { - exists, err := s.HasBlobs([]string{hash}) +func (s *SQL) HasBlob(hash string, touch bool) (bool, error) { + exists, err := s.HasBlobs([]string{hash}, touch) if err != nil { return false, err } @@ -221,15 +221,16 @@ func (s *SQL) HasBlob(hash string) (bool, error) { } // HasBlobs checks if the database contains the set of blobs and returns a bool map. -func (s *SQL) HasBlobs(hashes []string) (map[string]bool, error) { +func (s *SQL) HasBlobs(hashes []string, touch bool) (map[string]bool, error) { exists, idsNeedingTouch, err := s.hasBlobs(hashes) - go func() { + + if touch { if s.TrackAccess == TrackAccessBlobs { s.touchBlobs(idsNeedingTouch) } else if s.TrackAccess == TrackAccessStreams { s.touchStreams(idsNeedingTouch) } - }() + } return exists, err } diff --git a/reflector/uploader.go b/reflector/uploader.go index ae762f4..e5b2688 100644 --- a/reflector/uploader.go +++ b/reflector/uploader.go @@ -74,7 +74,7 @@ func (u *Uploader) Upload(dirOrFilePath string) error { var exists map[string]bool if !u.skipExistsCheck { - exists, err = u.db.HasBlobs(hashes) + exists, err = u.db.HasBlobs(hashes, false) if err != nil { return err } diff --git a/store/dbbacked.go b/store/dbbacked.go index dc88ad2..1484c49 100644 --- a/store/dbbacked.go +++ b/store/dbbacked.go @@ -32,12 +32,12 @@ func (d *DBBackedStore) Name() string { return nameDBBacked } // Has returns true if the blob is in the store func (d *DBBackedStore) Has(hash string) (bool, error) { - return d.db.HasBlob(hash) + return d.db.HasBlob(hash, false) } // Get gets the blob func (d *DBBackedStore) Get(hash string) (stream.Blob, error) { - has, err := d.db.HasBlob(hash) + has, err := d.db.HasBlob(hash, true) if err != nil { return nil, err } @@ -46,12 +46,10 @@ func (d *DBBackedStore) Get(hash string) (stream.Blob, error) { } b, err := d.blobs.Get(hash) - if d.deleteOnMiss { - if err != nil && errors.Is(err, ErrBlobNotFound) { - e2 := d.Delete(hash) - if e2 != nil { - log.Errorf("error while deleting blob from db: %s", errors.FullTrace(err)) - } + if d.deleteOnMiss && errors.Is(err, ErrBlobNotFound) { + e2 := d.Delete(hash) + if e2 != nil { + log.Errorf("error while deleting blob from db: %s", errors.FullTrace(err)) } } @@ -110,7 +108,7 @@ func (d *DBBackedStore) Block(hash string) error { return err } - has, err := d.db.HasBlob(hash) + has, err := d.db.HasBlob(hash, false) if err != nil { return err }