mirror of
https://github.com/restic/restic.git
synced 2026-06-12 03:49:44 +00:00
Use array instead of hash for backend.ID
Since backend.ID is always a slice of constant length, use an array
instead of a slice. Mostly, arrays behave as slices, except that an
array cannot be nil, so use `*backend.ID` insteaf of `backend.ID` in
places where the absence of an ID is possible (e.g. for the Subtree of a
Node, which may not present when the node is a file node).
This change allows to directly use backend.ID as the the key for a map,
so that arbitrary data structures (e.g. a Set implemented as a
map[backend.ID]struct{}) can easily be formed.
This commit is contained in:
@@ -33,7 +33,7 @@ type Lock struct {
|
||||
GID uint32 `json:"gid,omitempty"`
|
||||
|
||||
repo *repository.Repository
|
||||
lockID backend.ID
|
||||
lockID *backend.ID
|
||||
}
|
||||
|
||||
// ErrAlreadyLocked is returned when NewLock or NewExclusiveLock are unable to
|
||||
@@ -92,11 +92,13 @@ func newLock(repo *repository.Repository, excl bool) (*Lock, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lock.lockID, err = lock.createLock()
|
||||
lockID, err := lock.createLock()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
lock.lockID = &lockID
|
||||
|
||||
time.Sleep(waitBeforeLockCheck)
|
||||
|
||||
if err = lock.checkForOtherLocks(); err != nil {
|
||||
@@ -137,7 +139,7 @@ func (l *Lock) fillUserInfo() error {
|
||||
// exclusive lock is found.
|
||||
func (l *Lock) checkForOtherLocks() error {
|
||||
return eachLock(l.repo, func(id backend.ID, lock *Lock, err error) error {
|
||||
if id.Equal(l.lockID) {
|
||||
if l.lockID != nil && id.Equal(*l.lockID) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -177,7 +179,7 @@ func eachLock(repo *repository.Repository, f func(backend.ID, *Lock, error) erro
|
||||
func (l *Lock) createLock() (backend.ID, error) {
|
||||
id, err := l.repo.SaveJSONUnpacked(backend.Lock, l)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return backend.ID{}, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
@@ -237,7 +239,7 @@ func (l *Lock) Refresh() error {
|
||||
}
|
||||
|
||||
debug.Log("Lock.Refresh", "new lock ID %v", id.Str())
|
||||
l.lockID = id
|
||||
l.lockID = &id
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -276,7 +278,7 @@ func LoadLock(repo *repository.Repository, id backend.ID) (*Lock, error) {
|
||||
if err := repo.LoadJSONUnpacked(backend.Lock, id, lock); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lock.lockID = id
|
||||
lock.lockID = &id
|
||||
|
||||
return lock, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user