Skip to content

Commit 04a53c9

Browse files
committed
add lookuphash index
1 parent cae2988 commit 04a53c9

File tree

7 files changed

+29
-52
lines changed

7 files changed

+29
-52
lines changed

code/go/0chain.net/blobbercore/allocation/allocationchange.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ func (change *AllocationChange) Save(ctx context.Context) error {
9898
return db.Save(change).Error
9999
}
100100

101+
func (change *AllocationChange) Update(ctx context.Context) error {
102+
db := datastore.GetStore().GetTransaction(ctx)
103+
return db.Table(change.TableName()).Where("lookup_hash = ?", change.LookupHash).Updates(map[string]interface{}{
104+
"size": change.Size,
105+
"updated_at": time.Now(),
106+
"input": change.Input,
107+
}).Error
108+
}
109+
101110
func (change *AllocationChange) Create(ctx context.Context) error {
102111
db := datastore.GetStore().GetTransaction(ctx)
103112
return db.Create(change).Error
@@ -180,7 +189,7 @@ func GetConnectionObj(ctx context.Context, connectionID, allocationID, clientID
180189
cc.AllocationID = allocationID
181190
cc.ClientID = clientID
182191
cc.Status = NewConnection
183-
err = cc.Save(ctx)
192+
err = cc.Create(ctx)
184193
if err != nil {
185194
return nil, err
186195
}

code/go/0chain.net/blobbercore/allocation/file_changer_base.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ type FileCommand interface {
9494
ProcessThumbnail(allocationObj *Allocation) error
9595

9696
// UpdateChange update AllocationChangeProcessor. It will be president in db for committing transcation
97-
UpdateChange(ctx context.Context, connectionObj *AllocationChangeCollector) error
97+
UpdateChange(ctx context.Context) error
9898

9999
// AddChange add Allocation change to db
100100
AddChange(ctx context.Context) error

code/go/0chain.net/blobbercore/handler/file_command_delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (cmd *DeleteFileCommand) IsValidated(ctx context.Context, req *http.Request
6262
}
6363

6464
// UpdateChange add DeleteFileChange in db
65-
func (cmd *DeleteFileCommand) UpdateChange(ctx context.Context, connectionObj *allocation.AllocationChangeCollector) error {
65+
func (cmd *DeleteFileCommand) UpdateChange(ctx context.Context) error {
6666
err := cmd.AddChange(ctx)
6767
if err == gorm.ErrDuplicatedKey {
6868
return nil

code/go/0chain.net/blobbercore/handler/file_command_update.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -205,30 +205,11 @@ func (cmd *UpdateFileCommand) reloadChange() {
205205
}
206206

207207
// UpdateChange add UpdateFileChanger in db
208-
func (cmd *UpdateFileCommand) UpdateChange(ctx context.Context, connectionObj *allocation.AllocationChangeCollector) error {
209-
cmd.fileChanger.AllocationID = connectionObj.AllocationID
210-
for _, c := range connectionObj.Changes {
211-
filePath, _ := c.GetOrParseAffectedFilePath()
212-
if c.Operation != sdkConst.FileOperationUpdate || cmd.fileChanger.Path != filePath {
213-
continue
214-
}
215-
216-
c.Size = connectionObj.Size
217-
c.Input, _ = cmd.fileChanger.Marshal()
218-
219-
//c.ModelWithTS.UpdatedAt = time.Now()
220-
err := connectionObj.Save(ctx)
221-
if err != nil {
222-
return err
223-
}
224-
225-
return c.Save(ctx)
226-
}
227-
228-
//NOT FOUND
229-
connectionObj.AddChange(cmd.allocationChange, cmd.fileChanger)
230-
231-
return connectionObj.Save(ctx)
208+
func (cmd *UpdateFileCommand) UpdateChange(ctx context.Context) error {
209+
connectionInput, _ := cmd.fileChanger.Marshal()
210+
cmd.allocationChange.LookupHash = reference.GetReferenceLookup(cmd.fileChanger.AllocationID, cmd.fileChanger.Path)
211+
cmd.allocationChange.Input = connectionInput
212+
return cmd.allocationChange.Update(ctx)
232213
}
233214

234215
func (cmd *UpdateFileCommand) AddChange(ctx context.Context) error {

code/go/0chain.net/blobbercore/handler/file_command_upload.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -223,29 +223,11 @@ func (cmd *UploadFileCommand) reloadChange() {
223223
}
224224

225225
// UpdateChange replace AddFileChange in db
226-
func (cmd *UploadFileCommand) UpdateChange(ctx context.Context, connectionObj *allocation.AllocationChangeCollector) error {
227-
cmd.fileChanger.AllocationID = connectionObj.AllocationID
228-
for _, c := range connectionObj.Changes {
229-
filePath, _ := c.GetOrParseAffectedFilePath()
230-
if c.Operation != constants.FileOperationInsert || cmd.fileChanger.Path != filePath {
231-
continue
232-
}
233-
c.Size = cmd.fileChanger.Size
234-
c.Input, _ = cmd.fileChanger.Marshal()
235-
236-
//c.ModelWithTS.UpdatedAt = time.Now()
237-
err := connectionObj.Save(ctx)
238-
if err != nil {
239-
return err
240-
}
241-
242-
return c.Save(ctx)
243-
}
244-
245-
//NOT FOUND
246-
connectionObj.AddChange(cmd.allocationChange, cmd.fileChanger)
247-
248-
return connectionObj.Save(ctx)
226+
func (cmd *UploadFileCommand) UpdateChange(ctx context.Context) error {
227+
connectionInput, _ := cmd.fileChanger.Marshal()
228+
cmd.allocationChange.LookupHash = reference.GetReferenceLookup(cmd.fileChanger.AllocationID, cmd.fileChanger.Path)
229+
cmd.allocationChange.Input = connectionInput
230+
return cmd.allocationChange.Update(ctx)
249231
}
250232

251233
func (cmd *UploadFileCommand) AddChange(ctx context.Context) error {

code/go/0chain.net/blobbercore/handler/object_operation_handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,11 +1297,11 @@ func (fsh *StorageHandler) WriteFile(ctx context.Context, r *http.Request) (*all
12971297
}
12981298
// Update/Save the change
12991299
if res.UpdateChange {
1300-
dbConnectionObj, err := allocation.GetConnectionObj(ctx, connectionID, allocationID, clientID)
1300+
_, err := allocation.GetConnectionObj(ctx, connectionID, allocationID, clientID)
13011301
if err != nil {
13021302
return nil, err
13031303
}
1304-
err = cmd.UpdateChange(ctx, dbConnectionObj)
1304+
err = cmd.UpdateChange(ctx)
13051305
if err != nil {
13061306
return nil, err
13071307
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
4+
CREATE INDEX idx_allocation_changes_lookup_hash ON allocation_changes USING HASH(lookup_hash);
5+
-- +goose StatementEnd

0 commit comments

Comments
 (0)