Skip to content

Commit f3eb60b

Browse files
authored
Merge pull request #1466 from 0chain/feat/file-search
Add file search using names
2 parents e2dca5d + 060f8c2 commit f3eb60b

File tree

6 files changed

+47
-20
lines changed

6 files changed

+47
-20
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type NewDir struct {
1919
ConnectionID string `json:"connection_id" validation:"required"`
2020
Path string `json:"filepath" validation:"required"`
2121
AllocationID string `json:"allocation_id"`
22+
CustomMeta string `json:"custom_meta,omitempty"`
2223
}
2324

2425
func (nf *NewDir) ApplyChange(ctx context.Context, rootRef *reference.Ref, change *AllocationChange,
@@ -79,6 +80,7 @@ func (nf *NewDir) ApplyChange(ctx context.Context, rootRef *reference.Ref, chang
7980
fmt.Sprintf("file path %s has no entry in fileID meta", newRef.Path))
8081
}
8182
newRef.FileID = fileID
83+
newRef.CustomMeta = nf.CustomMeta
8284
dirRef.AddChild(newRef)
8385
dirRef = newRef
8486
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ func BlobberHealthCheck() (*transaction.Transaction, error) {
5252
return nil, err
5353
}
5454

55+
setBlobberHealthCheckError(nil)
56+
5557
return txn, nil
5658
}
5759

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

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,6 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b
656656
return nil, common.NewError("invalid_operation", "Operation needs to be performed by the owner of the allocation")
657657
}
658658

659-
if allocationObj.BlobberSizeUsed+connectionObj.Size > allocationObj.BlobberSize {
660-
return nil, common.NewError("max_allocation_size",
661-
"Max size reached for the allocation with this blobber")
662-
}
663-
664659
writeMarkerString := r.FormValue("write_marker")
665660
if writeMarkerString == "" {
666661
return nil, common.NewError("invalid_parameters", "Invalid write marker passed")
@@ -689,11 +684,6 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b
689684
"Latest write marker is in failed state")
690685
}
691686

692-
if latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size != writeMarker.ChainSize {
693-
return nil, common.NewErrorf("invalid_chain_size",
694-
"Invalid chain size. expected:%v got %v", latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size, writeMarker.ChainSize)
695-
}
696-
697687
if latestWriteMarkerEntity.Status != writemarker.Committed {
698688
writeMarker.ChainLength = latestWriteMarkerEntity.WM.ChainLength
699689
}
@@ -758,6 +748,24 @@ func (fsh *StorageHandler) CommitWrite(ctx context.Context, r *http.Request) (*b
758748
if !rootRef.IsPrecommit {
759749
return nil, common.NewError("no_root_change", "No change in root ref")
760750
}
751+
connectionObj.Size = rootRef.Size - allocationObj.BlobberSizeUsed
752+
753+
if writemarkerEntity.WM.Size != connectionObj.Size {
754+
return nil, common.NewError("write_marker_validation_failed", fmt.Sprintf("Write Marker size %v does not match the connection size %v", writemarkerEntity.WM.Size, connectionObj.Size))
755+
}
756+
757+
if allocationObj.BlobberSizeUsed+connectionObj.Size > allocationObj.BlobberSize {
758+
return nil, common.NewError("max_allocation_size",
759+
"Max size reached for the allocation with this blobber")
760+
}
761+
762+
if latestWriteMarkerEntity != nil && latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size != writeMarker.ChainSize {
763+
return nil, common.NewErrorf("invalid_chain_size",
764+
"Invalid chain size. expected:%v got %v", latestWriteMarkerEntity.WM.ChainSize+connectionObj.Size, writeMarker.ChainSize)
765+
} else if latestWriteMarkerEntity == nil && connectionObj.Size != writeMarker.ChainSize {
766+
return nil, common.NewErrorf("invalid_chain_size",
767+
"Invalid chain size. expected:%v got %v", connectionObj.Size, writeMarker.ChainSize)
768+
}
761769

762770
elapsedApplyChanges := time.Since(startTime) - elapsedAllocation - elapsedGetLock -
763771
elapsedGetConnObj - elapsedVerifyWM - elapsedWritePreRedeem
@@ -1245,6 +1253,8 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
12451253
return nil, common.NewError("invalid_parameters", "Invalid dir path passed")
12461254
}
12471255

1256+
customMeta := r.FormValue("custom_meta")
1257+
12481258
exisitingRef, err := fsh.checkIfFileAlreadyExists(ctx, allocationID, dirPath)
12491259
if err != nil {
12501260
Logger.Error("Error file reference", zap.Error(err))
@@ -1257,6 +1267,16 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
12571267
if exisitingRef != nil {
12581268
// target directory exists, return StatusOK
12591269
if exisitingRef.Type == reference.DIRECTORY {
1270+
1271+
if exisitingRef.CustomMeta != customMeta {
1272+
_ = datastore.GetStore().WithNewTransaction(func(ctx context.Context) error {
1273+
err := reference.UpdateCustomMeta(ctx, exisitingRef, customMeta)
1274+
if err != nil {
1275+
logging.Logger.Error("Error updating custom meta", zap.Error(err))
1276+
}
1277+
return err
1278+
})
1279+
}
12601280
return nil, common.NewError("directory_exists", "Directory already exists`")
12611281
}
12621282

@@ -1293,6 +1313,7 @@ func (fsh *StorageHandler) CreateDir(ctx context.Context, r *http.Request) (*all
12931313
newDir.ConnectionID = connectionID
12941314
newDir.Path = dirPath
12951315
newDir.AllocationID = allocationID
1316+
newDir.CustomMeta = customMeta
12961317

12971318
connectionObj.AddChange(allocationChange, &newDir)
12981319

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (fsh *StorageHandler) GetAllocationUpdateTicket(ctx context.Context, r *htt
103103
}
104104

105105
func (fsh *StorageHandler) checkIfFileAlreadyExists(ctx context.Context, allocationID, path string) (*reference.Ref, error) {
106-
return reference.GetLimitedRefFieldsByPath(ctx, allocationID, path, []string{"id", "type"})
106+
return reference.GetLimitedRefFieldsByPath(ctx, allocationID, path, []string{"id", "type", "custom_meta"})
107107
}
108108

109109
func (fsh *StorageHandler) GetFileMeta(ctx context.Context, r *http.Request) (interface{}, error) {
@@ -276,7 +276,7 @@ func (fsh *StorageHandler) GetFileStats(ctx context.Context, r *http.Request) (i
276276

277277
// swagger:route GET /v1/file/list/{allocation} GetListFiles
278278
// List files.
279-
// ListHandler is the handler to respond to list requests from clients,
279+
// ListHandler is the handler to respond to list requests from clients,
280280
// it returns a list of files in the allocation,
281281
// along with the metadata of the files.
282282
//
@@ -344,7 +344,7 @@ func (fsh *StorageHandler) GetFileStats(ctx context.Context, r *http.Request) (i
344344
// responses:
345345
//
346346
// 200: ListResult
347-
// 400:
347+
// 400:
348348

349349
func (fsh *StorageHandler) ListEntities(ctx context.Context, r *http.Request) (*blobberhttp.ListResult, error) {
350350
clientID := ctx.Value(constants.ContextKeyClient).(string)

code/go/0chain.net/blobbercore/reference/ref.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ type Ref struct {
6060
PathHash string `gorm:"column:path_hash;size:64;not null" dirlist:"path_hash" filelist:"path_hash"`
6161
ParentPath string `gorm:"column:parent_path;size:999;index:idx_parent_path_alloc,priority:2"`
6262
PathLevel int `gorm:"column:level;not null;default:0"`
63-
CustomMeta string `gorm:"column:custom_meta;not null" filelist:"custom_meta"`
63+
CustomMeta string `gorm:"column:custom_meta;not null" filelist:"custom_meta" dirlist:"custom_meta"`
6464
ValidationRoot string `gorm:"column:validation_root;size:64;not null;index:idx_validation_alloc,priority:2" filelist:"validation_root"`
6565
PrevValidationRoot string `gorm:"column:prev_validation_root" filelist:"prev_validation_root" json:"prev_validation_root"`
6666
ValidationRootSignature string `gorm:"column:validation_root_signature;size:64" filelist:"validation_root_signature" json:"validation_root_signature,omitempty"`
@@ -144,6 +144,7 @@ type PaginatedRef struct { //Gorm smart select fields.
144144
ActualThumbnailHash string `gorm:"column:actual_thumbnail_hash" json:"actual_thumbnail_hash,omitempty"`
145145
EncryptedKey string `gorm:"column:encrypted_key" json:"encrypted_key,omitempty"`
146146
EncryptedKeyPoint string `gorm:"column:encrypted_key_point" json:"encrypted_key_point,omitempty"`
147+
FileMetaHash string `gorm:"column:file_meta_hash;size:64;not null" dirlist:"file_meta_hash" filelist:"file_meta_hash"`
147148

148149
CreatedAt common.Timestamp `gorm:"column:created_at" json:"created_at,omitempty"`
149150
UpdatedAt common.Timestamp `gorm:"column:updated_at" json:"updated_at,omitempty"`
@@ -282,8 +283,8 @@ func GetReferenceByLookupHashForDownload(ctx context.Context, allocationID, path
282283
func GetReferencesByName(ctx context.Context, allocationID, name string) (refs []*Ref, err error) {
283284
db := datastore.GetStore().GetTransaction(ctx)
284285
err = db.Model(&Ref{}).
285-
Where("allocation_id = ? AND name @@ plainto_tsquery(?)", allocationID, name).
286-
Order("ts_rank_cd(to_tsvector('english', name), plainto_tsquery(?)) DESC").
286+
Where("allocation_id = ? AND name LIKE ?", allocationID, "%"+name+"%").
287+
Limit(20).
287288
Find(&refs).Error
288289
if err != nil {
289290
return nil, err
@@ -669,3 +670,8 @@ func GetListingFieldsMap(refEntity interface{}, tagName string) map[string]inter
669670
}
670671
return result
671672
}
673+
674+
func UpdateCustomMeta(ctx context.Context, ref *Ref, customMeta string) error {
675+
db := datastore.GetStore().GetTransaction(ctx)
676+
return db.Exec("UPDATE reference_objects SET custom_meta = ? WHERE id = ?", customMeta, ref.ID).Error
677+
}

code/go/0chain.net/blobbercore/writemarker/protocol.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ func (wme *WriteMarkerEntity) VerifyMarker(ctx context.Context, dbAllocation *al
7676
return common.NewError("write_marker_validation_failed", "Write Marker is not for the same allocation transaction")
7777
}
7878

79-
if wme.WM.Size != co.Size {
80-
return common.NewError("write_marker_validation_failed", fmt.Sprintf("Write Marker size %v does not match the connection size %v", wme.WM.Size, co.Size))
81-
}
82-
8379
clientPublicKey := ctx.Value(constants.ContextKeyClientKey).(string)
8480
if clientPublicKey == "" {
8581
return common.NewError("write_marker_validation_failed", "Could not get the public key of the client")

0 commit comments

Comments
 (0)