[#9] refactor access to the CID cache

Signed-off-by: Aleksey Kravchenko <al.kravchenko@yadro.com>
This commit is contained in:
Aleksey Kravchenko 2025-01-22 19:23:42 +03:00
parent 12b572e62a
commit c719874fd9

View file

@ -572,15 +572,16 @@ func (f *Fs) resolveOrCreateContainer(ctx context.Context, rootDirName string) (
f.m.Lock()
defer f.m.Unlock()
cnrID, err := f.resolveContainerIDHelper(ctx, rootDirName)
if err == nil {
return cnrID, err
cnrIDStr, ok := f.containerIDCache[rootDirName]
if ok {
return parseContainerID(cnrIDStr)
}
cnrID, err := f.resolveCIDByRootDirName(ctx, rootDirName)
if err != nil {
if cnrID, err = f.createContainer(ctx, rootDirName); err != nil {
delete(f.containerIDCache, rootDirName)
return cid.ID{}, fmt.Errorf("createContainer: %w", err)
}
}
f.containerIDCache[rootDirName] = cnrID.String()
return cnrID, nil
@ -1004,7 +1005,10 @@ func (f *Fs) resolveCIDByRootDirName(ctx context.Context, rootDirName string) (c
return cid.ID{}, fmt.Errorf("couldn't resolve container '%s'", rootDirName)
}
func (f *Fs) resolveContainerIDHelper(ctx context.Context, rootDirName string) (cid.ID, error) {
func (f *Fs) resolveContainerID(ctx context.Context, rootDirName string) (cid.ID, error) {
f.m.Lock()
defer f.m.Unlock()
cnrIDStr, ok := f.containerIDCache[rootDirName]
if ok {
return parseContainerID(cnrIDStr)
@ -1017,13 +1021,6 @@ func (f *Fs) resolveContainerIDHelper(ctx context.Context, rootDirName string) (
return cnrID, nil
}
func (f *Fs) resolveContainerID(ctx context.Context, rootDirName string) (cid.ID, error) {
f.m.Lock()
defer f.m.Unlock()
return f.resolveContainerIDHelper(ctx, rootDirName)
}
func (f *Fs) parseContainer(ctx context.Context, rootDirName string) (cid.ID, error) {
cnrID, err := parseContainerID(rootDirName)
if err != nil {