@@ -2,6 +2,7 @@ package allocation
2
2
3
3
import (
4
4
"context"
5
+ "database/sql"
5
6
"encoding/json"
6
7
"fmt"
7
8
"path/filepath"
@@ -10,6 +11,7 @@ import (
10
11
"sync/atomic"
11
12
12
13
"github.com/0chain/blobber/code/go/0chain.net/blobbercore/config"
14
+ "github.com/0chain/blobber/code/go/0chain.net/blobbercore/datastore"
13
15
"github.com/0chain/common/core/util/wmpt"
14
16
"gorm.io/gorm"
15
17
@@ -109,11 +111,30 @@ func (nf *NewDir) ApplyChangeV2(ctx context.Context, allocationRoot, clientPubKe
109
111
return 0 , common .NewError ("invalid_parent_path" , "parent path is not a directory" )
110
112
}
111
113
newRef := reference .NewDirectoryRef ()
114
+ newRef .LookupHash = reference .GetReferenceLookup (nf .AllocationID , nf .Path )
115
+ var refResult struct {
116
+ ID int64
117
+ }
118
+ err := datastore .GetStore ().WithNewTransaction (func (ctx context.Context ) error {
119
+ tx := datastore .GetStore ().GetTransaction (ctx )
120
+ return tx .Model (& reference.Ref {}).Select ("id" ).Where ("lookup_hash = ?" , newRef .LookupHash ).Take (& refResult ).Error
121
+ }, & sql.TxOptions {
122
+ ReadOnly : true ,
123
+ })
124
+ if err != nil && err != gorm .ErrRecordNotFound {
125
+ return 0 , err
126
+ }
127
+ if refResult .ID > 0 {
128
+ collector .LockTransaction ()
129
+ defer collector .UnlockTransaction ()
130
+ txn := datastore .GetStore ().GetTransaction (ctx )
131
+ err = txn .Exec ("UPDATE refs SET custom_meta=? WHERE lookup_hash=?" , nf .CustomMeta , newRef .LookupHash ).Error
132
+ return 0 , err
133
+ }
112
134
newRef .AllocationID = nf .AllocationID
113
135
newRef .Path = nf .Path
114
136
newRef .Name = filepath .Base (nf .Path )
115
137
newRef .ParentPath = parentPath
116
- newRef .LookupHash = reference .GetReferenceLookup (nf .AllocationID , nf .Path )
117
138
newRef .CreatedAt = ts
118
139
newRef .UpdatedAt = ts
119
140
newRef .AllocationRoot = allocationRoot
0 commit comments