@@ -11,6 +11,8 @@ import (
1111 "github.com/0chain/blobber/code/go/0chain.net/blobbercore/util"
1212 "github.com/0chain/blobber/code/go/0chain.net/core/common"
1313 "github.com/0chain/blobber/code/go/0chain.net/core/encryption"
14+ "github.com/0chain/blobber/code/go/0chain.net/core/logging"
15+ "go.uber.org/zap"
1416 "gorm.io/gorm"
1517)
1618
@@ -33,6 +35,19 @@ func (nf *NewDir) ApplyChange(ctx context.Context,
3335 if parentRef == nil || parentRef .ID == 0 {
3436 _ , err = reference .Mkdir (ctx , nf .AllocationID , nf .Path , allocationVersion , ts , collector )
3537 } else {
38+ collector .LockTransaction ()
39+ defer collector .UnlockTransaction ()
40+ dirLookupHash := reference .GetReferenceLookup (nf .AllocationID , nf .Path )
41+ dRef , err := reference .GetLimitedRefFieldsByLookupHash (ctx , nf .AllocationID , dirLookupHash , []string {"id" })
42+ if err != nil && err != gorm .ErrRecordNotFound {
43+ logging .Logger .Error ("ApplyChange:Newdir" , zap .Error (err ))
44+ return err
45+ }
46+ err = nil
47+ // already exists
48+ if dRef != nil && dRef .ID != 0 {
49+ return nil
50+ }
3651 parentIDRef := & parentRef .ID
3752 newRef := reference .NewDirectoryRef ()
3853 newRef .AllocationID = nf .AllocationID
@@ -43,7 +58,7 @@ func (nf *NewDir) ApplyChange(ctx context.Context,
4358 newRef .Name = filepath .Base (nf .Path )
4459 newRef .PathLevel = len (strings .Split (strings .TrimRight (nf .Path , "/" ), "/" ))
4560 newRef .ParentID = parentIDRef
46- newRef .LookupHash = reference . GetReferenceLookup ( nf . AllocationID , nf . Path )
61+ newRef .LookupHash = dirLookupHash
4762 newRef .CreatedAt = ts
4863 newRef .UpdatedAt = ts
4964 newRef .FileMetaHash = encryption .FastHash (newRef .GetFileMetaHashData ())
0 commit comments