Refactor code
Some checks failed
Integration Tests / test-integration (pull_request) Successful in 2m53s
Build / build (pull_request) Successful in 2m54s
E2E Tests / test-e2e (pull_request) Failing after 4m36s
Unit Tests / test-unit (pull_request) Successful in 2m47s
SDK Tests / sdk_tests_nameservice_expiry (pull_request) Successful in 10m12s
SDK Tests / sdk_tests (pull_request) Failing after 12m18s
SDK Tests / sdk_tests_auctions (pull_request) Failing after 15m50s

This commit is contained in:
Prathamesh Musale 2024-08-30 11:10:52 +05:30
parent 75b2244fb4
commit d9edc38c7f
3 changed files with 44 additions and 91 deletions

View File

@ -62,7 +62,7 @@ func (k *Keeper) ExportGenesis(ctx sdk.Context) (*registry.GenesisState, error)
return nil, err return nil, err
} }
records, err := k.ListRecords(ctx) records, _, err := k.PaginatedListRecords(ctx, nil)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -186,10 +186,12 @@ func (k Keeper) HasRecord(ctx sdk.Context, id string) (bool, error) {
return has, nil return has, nil
} }
// ListRecords - get all records. // PaginatedListRecords - get all records with optional pagination.
func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) { func (k Keeper) PaginatedListRecords(ctx sdk.Context, pagination *query.PageRequest) ([]registrytypes.Record, *query.PageResponse, error) {
var records []registrytypes.Record var records []registrytypes.Record
var pageResp *query.PageResponse
if pagination == nil {
err := k.Records.Walk(ctx, nil, func(key string, value registrytypes.Record) (bool, error) { err := k.Records.Walk(ctx, nil, func(key string, value registrytypes.Record) (bool, error) {
if err := k.populateRecordNames(ctx, &value); err != nil { if err := k.populateRecordNames(ctx, &value); err != nil {
return true, err return true, err
@ -199,15 +201,11 @@ func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) {
return false, nil return false, nil
}) })
if err != nil { if err != nil {
return nil, err return nil, nil, err
} }
} else {
return records, nil var err error
} records, pageResp, err = query.CollectionPaginate(ctx, k.Records, pagination, func(key string, value registrytypes.Record) (registrytypes.Record, error) {
// PaginatedListRecords - get all records with pagination.
func (k Keeper) PaginatedListRecords(ctx sdk.Context, pagination *query.PageRequest) ([]registrytypes.Record, *query.PageResponse, error) {
records, pageResp, err := query.CollectionPaginate(ctx, k.Records, pagination, func(key string, value registrytypes.Record) (registrytypes.Record, error) {
if err := k.populateRecordNames(ctx, &value); err != nil { if err := k.populateRecordNames(ctx, &value); err != nil {
return registrytypes.Record{}, err return registrytypes.Record{}, err
} }
@ -217,6 +215,7 @@ func (k Keeper) PaginatedListRecords(ctx sdk.Context, pagination *query.PageRequ
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
}
return records, pageResp, nil return records, pageResp, nil
} }
@ -259,58 +258,18 @@ func (k Keeper) GetRecordsByBondId(ctx sdk.Context, bondId string) ([]registryty
return records, nil return records, nil
} }
// RecordsFromAttributes gets a list of records whose attributes match all provided values
func (k Keeper) RecordsFromAttributes(
ctx sdk.Context,
attributes []*registrytypes.QueryRecordsRequest_KeyValueInput,
all bool,
) ([]registrytypes.Record, error) {
resultRecordIds := []string{}
for i, attr := range attributes {
suffix, err := QueryValueToJSON(attr.Value)
if err != nil {
return nil, err
}
mapKey := collections.Join(attr.Key, string(suffix))
recordIds, err := k.getAttributeMapping(ctx, mapKey)
if err != nil {
return nil, err
}
if i == 0 {
resultRecordIds = recordIds
} else {
resultRecordIds = getIntersection(recordIds, resultRecordIds)
}
}
records := []registrytypes.Record{}
for _, id := range resultRecordIds {
record, err := k.GetRecordById(ctx, id)
if err != nil {
return nil, err
}
if record.Deleted {
continue
}
if !all && len(record.Names) == 0 {
continue
}
records = append(records, record)
}
return records, nil
}
// PaginatedRecordsFromAttributes gets a list of records whose attributes match all provided values // PaginatedRecordsFromAttributes gets a list of records whose attributes match all provided values
// with pagination. // with optional pagination.
func (k Keeper) PaginatedRecordsFromAttributes( func (k Keeper) PaginatedRecordsFromAttributes(
ctx sdk.Context, ctx sdk.Context,
attributes []*registrytypes.QueryRecordsRequest_KeyValueInput, attributes []*registrytypes.QueryRecordsRequest_KeyValueInput,
all bool, all bool,
pagination *query.PageRequest, pagination *query.PageRequest,
) ([]registrytypes.Record, *query.PageResponse, error) { ) ([]registrytypes.Record, *query.PageResponse, error) {
resultRecordIds := []string{} var resultRecordIds []string
var pageResp *query.PageResponse
filteredRecordIds := []string{}
for i, attr := range attributes { for i, attr := range attributes {
suffix, err := QueryValueToJSON(attr.Value) suffix, err := QueryValueToJSON(attr.Value)
if err != nil { if err != nil {
@ -323,16 +282,20 @@ func (k Keeper) PaginatedRecordsFromAttributes(
} }
if i == 0 { if i == 0 {
resultRecordIds = recordIds filteredRecordIds = recordIds
} else { } else {
resultRecordIds = getIntersection(recordIds, resultRecordIds) filteredRecordIds = getIntersection(recordIds, filteredRecordIds)
} }
} }
paginatedResultRecordIds, pageRes := paginate(resultRecordIds, pagination) if pagination != nil {
resultRecordIds, pageResp = paginate(filteredRecordIds, pagination)
} else {
resultRecordIds = filteredRecordIds
}
records := []registrytypes.Record{} records := []registrytypes.Record{}
for _, id := range paginatedResultRecordIds { for _, id := range resultRecordIds {
record, err := k.GetRecordById(ctx, id) record, err := k.GetRecordById(ctx, id)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -346,7 +309,7 @@ func (k Keeper) PaginatedRecordsFromAttributes(
records = append(records, record) records = append(records, record)
} }
return records, pageRes, nil return records, pageResp, nil
} }
// TODO not recursive, and only should be if we want to support querying with whole sub-objects, // TODO not recursive, and only should be if we want to support querying with whole sub-objects,

View File

@ -43,22 +43,12 @@ func (qs queryServer) Records(c context.Context, req *registrytypes.QueryRecords
var pageResp *query.PageResponse var pageResp *query.PageResponse
var err error var err error
if len(attributes) > 0 { if len(attributes) > 0 {
if req.Pagination != nil {
records, pageResp, err = qs.k.PaginatedRecordsFromAttributes(ctx, attributes, all, req.Pagination) records, pageResp, err = qs.k.PaginatedRecordsFromAttributes(ctx, attributes, all, req.Pagination)
} else {
records, err = qs.k.RecordsFromAttributes(ctx, attributes, all)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }
} else { } else {
if req.Pagination != nil {
records, pageResp, err = qs.k.PaginatedListRecords(ctx, req.Pagination) records, pageResp, err = qs.k.PaginatedListRecords(ctx, req.Pagination)
} else {
records, err = qs.k.ListRecords(ctx)
}
if err != nil { if err != nil {
return nil, err return nil, err
} }