This commit is contained in:
Roy Crihfield 2023-10-19 09:13:47 -05:00 committed by Nabarun
parent 69cf8f7b56
commit 9bf5fa589c

View File

@ -150,20 +150,13 @@ func (k Keeper) ListRecords(ctx sdk.Context) []types.Record {
// RecordsFromAttributes gets a list of records whose attributes match all provided values // RecordsFromAttributes gets a list of records whose attributes match all provided values
func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.QueryListRecordsRequest_KeyValueInput, all bool) ([]types.Record, error) { func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.QueryListRecordsRequest_KeyValueInput, all bool) ([]types.Record, error) {
log := logger(ctx).With("function", "RecordsFromAttributes")
resultRecordIds := []string{} resultRecordIds := []string{}
for i, attr := range attributes { for i, attr := range attributes {
val, err := EncodeAttributeValue2(attr.Value) suffix, err := EncodeQueryValue(attr.Value)
if err != nil { if err != nil {
return nil, err return nil, err
} }
attributeIndex := GetAttributesIndexKey(attr.Key, val) attributeIndex := GetAttributesIndexKey(attr.Key, suffix)
log.Debug("attribute index",
"key", attr.Key,
"value", val,
"value_type", fmt.Sprintf("%T", val),
"index", attributeIndex)
recordIds, err := k.GetAttributeMapping(ctx, attributeIndex) recordIds, err := k.GetAttributeMapping(ctx, attributeIndex)
if err != nil { if err != nil {
return nil, err return nil, err
@ -191,8 +184,8 @@ func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.Query
return records, nil return records, nil
} }
// TODO non recursive // TODO not recursive, and only should be if we want to support querying with whole sub-objects
func EncodeAttributeValue2(input *types.QueryListRecordsRequest_ValueInput) ([]byte, error) { func EncodeQueryValue(input *types.QueryListRecordsRequest_ValueInput) ([]byte, error) {
np := basicnode.Prototype.Any np := basicnode.Prototype.Any
nb := np.NewBuilder() nb := np.NewBuilder()
@ -343,7 +336,7 @@ func (k Keeper) processRecord(ctx sdk.Context, record *types.ReadableRecord, isR
return err return err
} }
expiryTimeKey := GetAttributesIndexKey(ExpiryTimeAttributeName, record.ExpiryTime) expiryTimeKey := GetAttributesIndexKey(ExpiryTimeAttributeName, []byte(record.ExpiryTime))
if err := k.SetAttributeMapping(ctx, expiryTimeKey, record.ID); err != nil { if err := k.SetAttributeMapping(ctx, expiryTimeKey, record.ID); err != nil {
return err return err
} }
@ -390,7 +383,6 @@ func (k Keeper) processAttributeMap(ctx sdk.Context, n ipld.Node, id string, pre
return err return err
} }
// for key, value := range attrs {
if valuenode.Kind() == ipld.Kind_Map { if valuenode.Kind() == ipld.Kind_Map {
err := k.processAttributeMap(ctx, valuenode, id, key) err := k.processAttributeMap(ctx, valuenode, id, key)
if err != nil { if err != nil {
@ -411,9 +403,10 @@ func (k Keeper) processAttributeMap(ctx sdk.Context, n ipld.Node, id string, pre
return nil return nil
} }
func GetAttributesIndexKey(key string, value interface{}) []byte { func GetAttributesIndexKey(key string, suffix []byte) []byte {
keyString := fmt.Sprintf("%s=%s", key, value) keyString := fmt.Sprintf("%s=%s", key, suffix)
return append(PrefixAttributesIndex, []byte(keyString)...) return append(PrefixAttributesIndex, []byte(keyString)...)
// return append(append(PrefixAttributesIndex, key...), suffix...)
} }
func (k Keeper) SetAttributeMapping(ctx sdk.Context, key []byte, recordID string) error { func (k Keeper) SetAttributeMapping(ctx sdk.Context, key []byte, recordID string) error {