From 0ac8398979e665296dea3b9bfd45c0befe203bf9 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Thu, 22 Feb 2024 16:28:27 +0530 Subject: [PATCH] Populate names when fetching records --- x/registry/keeper/keeper.go | 53 ++++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/x/registry/keeper/keeper.go b/x/registry/keeper/keeper.go index 19b1608c..688ae0ec 100644 --- a/x/registry/keeper/keeper.go +++ b/x/registry/keeper/keeper.go @@ -156,15 +156,21 @@ func (k Keeper) HasRecord(ctx sdk.Context, id string) (bool, error) { // ListRecords - get all records. func (k Keeper) ListRecords(ctx sdk.Context) ([]registrytypes.Record, error) { - iter, err := k.Records.Iterate(ctx, nil) + var records []registrytypes.Record + + err := k.Records.Walk(ctx, nil, func(key string, value registrytypes.Record) (bool, error) { + if err := k.populateRecordNames(ctx, &value); err != nil { + return true, err + } + records = append(records, value) + + return false, nil + }) if err != nil { return nil, err } - // TODO: Check if required - // decodeRecordNames(store, &record) - - return iter.Values() + return records, nil } // GetRecordById - gets a record from the store. @@ -174,17 +180,35 @@ func (k Keeper) GetRecordById(ctx sdk.Context, id string) (registrytypes.Record, return registrytypes.Record{}, err } + if err := k.populateRecordNames(ctx, &record); err != nil { + return registrytypes.Record{}, err + } + return record, nil } // GetRecordsByBondId - gets a record from the store. func (k Keeper) GetRecordsByBondId(ctx sdk.Context, bondId string) ([]registrytypes.Record, error) { - iter, err := k.Records.Indexes.BondId.MatchExact(ctx, bondId) + var records []registrytypes.Record + + err := k.Records.Indexes.BondId.Walk(ctx, collections.NewPrefixedPairRange[string, string](bondId), func(bondId string, id string) (bool, error) { + record, err := k.Records.Get(ctx, id) + if err != nil { + return true, err + } + + if err := k.populateRecordNames(ctx, &record); err != nil { + return true, err + } + records = append(records, record) + + return false, nil + }) if err != nil { return []registrytypes.Record{}, err } - return indexes.CollectValues(ctx, k.Records, iter) + return records, nil } // RecordsFromAttributes gets a list of records whose attributes match all provided values @@ -350,6 +374,21 @@ func (k Keeper) processAttributeMap(ctx sdk.Context, n ipld.Node, id string, pre return nil } +func (k Keeper) populateRecordNames(ctx sdk.Context, record *registrytypes.Record) error { + iter, err := k.NameRecords.Indexes.Cid.MatchExact(ctx, record.Id) + if err != nil { + return err + } + + names, err := iter.PrimaryKeys() + if err != nil { + return err + } + record.Names = names + + return nil +} + // GetModuleBalances gets the registry module account(s) balances. func (k Keeper) GetModuleBalances(ctx sdk.Context) []*registrytypes.AccountBalance { var balances []*registrytypes.AccountBalance