Skip to content

Index(multi-col): improve usability of FindXXX APIs #135

@wenchy

Description

@wenchy

Go

For example: Index: (ID,Type,Param,ExtType)@SpecialItem

// Index: (ID,Type,Param,ExtType)@SpecialItem
type ItemConf_Index_SpecialItemKey struct {
Id uint32
Type protoconf.FruitType
Param int32
ExtType protoconf.FruitType
}

// FindSpecialItem finds a slice of all values of the given key.
func (x *ItemConf) FindSpecialItem(key ItemConf_Index_SpecialItemKey) []*protoconf.ItemConf_Item {
return x.indexSpecialItemMap[key]
}
// FindFirstSpecialItem finds the first value of the given key,
// or nil if no value found.
func (x *ItemConf) FindFirstSpecialItem(key ItemConf_Index_SpecialItemKey) *protoconf.ItemConf_Item {
val := x.indexSpecialItemMap[key]
if len(val) > 0 {
return val[0]
}
return nil
}

We want a flat and easy to use APIs just like:

// FindSpecialItem finds a slice of all values of the given keys.
func (x *ItemConf) FindSpecialItem(id uint32, type protoconf.FruitType, param int32, extType protoconf.FruitType) []*protoconf.ItemConf_Item { 
    key := ItemConf_Index_SpecialItemKey{Id: id, Type: type , Param: param, ExtType: extType}
    return x.indexSpecialItemMap[key] 
} 
// FindFirstSpecialItem finds the first value of the given key, 
// or nil if no value found. 
func (x *ItemConf) FindFirstSpecialItem(id uint32, type protoconf.FruitType, param int32, extType protoconf.FruitType) *protoconf.ItemConf_Item {
    key := ItemConf_Index_SpecialItemKey{Id: id, Type: type , Param: param, ExtType: extType} 
    val := x.indexSpecialItemMap[key] 
    if len(val) > 0 { 
        return val[0] 
    } 
    return nil 
} 

C++

struct Index_SpecialItemKey {
uint32_t id;
protoconf::FruitType type;
int32_t param;
protoconf::FruitType ext_type;
bool operator==(const Index_SpecialItemKey& other) const {
return id == other.id && type == other.type && param == other.param && ext_type == other.ext_type;
}
};

const ItemConf::Index_SpecialItemVector* ItemConf::FindSpecialItem(const Index_SpecialItemKey& key) const {
auto iter = index_special_item_map_.find(key);
if (iter == index_special_item_map_.end()) {
return nullptr;
}
return &iter->second;
}
const protoconf::ItemConf::Item* ItemConf::FindFirstSpecialItem(const Index_SpecialItemKey& key) const {
auto conf = FindSpecialItem(key);
if (conf == nullptr || conf->empty()) {
return nullptr;
}
return conf->front();
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions