Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions cmd/protoc-gen-cpp-tableau-loader/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,20 @@ func genCppIndexLoader(g *protogen.GeneratedFile, descriptor *index.IndexDescrip
if levelMessage.FD == nil {
break
}
if !levelMessage.NextLevel.NeedGen() {
break
}
g.P(strings.Repeat(" ", depth), "for (auto&& "+itemName+" : "+parentDataName+"."+helper.ParseIndexFieldName(levelMessage.FD)+"()) {")
parentDataName = itemName
if levelMessage.FD.IsMap() {
parentDataName = itemName + ".second"
}
defer g.P(strings.Repeat(" ", depth), "}")
depth++
}
for i := depth - 1; i > 0; i-- {
g.P(strings.Repeat(" ", i), "}")
}
genIndexSorter(g, descriptor)
}

func genOneCppIndexLoader(g *protogen.GeneratedFile, depth int, index *index.LevelIndex, parentDataName string) {
Expand Down Expand Up @@ -148,31 +154,40 @@ func genOneCppIndexLoader(g *protogen.GeneratedFile, depth int, index *index.Lev
// multi-column index
generateOneCppMulticolumnIndex(g, depth, index, parentDataName, nil)
}
if len(index.KeyFields) != 0 {
g.P(strings.Repeat(" ", depth+1), "for (auto&& item : ", indexContainerName, ") {")
g.P(strings.Repeat(" ", depth+2), "std::sort(item.second.begin(), item.second.end(),")
g.P(strings.Repeat(" ", depth+7), "[](const ", helper.ParseCppClassType(index.MD), "* a, const ", helper.ParseCppClassType(index.MD), "* b) {")
for i, field := range index.KeyFields {
fieldName := ""
for i, leveledFd := range field.LeveledFDList {
accessOperator := "."
if i == 0 {
accessOperator = "->"
g.P(strings.Repeat(" ", depth), "}")
}

func genIndexSorter(g *protogen.GeneratedFile, descriptor *index.IndexDescriptor) {
for levelMessage := descriptor.LevelMessage; levelMessage != nil; levelMessage = levelMessage.NextLevel {
for _, index := range levelMessage.Indexes {
indexContainerName := "index_" + strcase.ToSnake(index.Name()) + "_map_"
if len(index.KeyFields) != 0 {
g.P(" // Index(sort): ", index.Index)
g.P(strings.Repeat(" ", 1), "for (auto&& item : ", indexContainerName, ") {")
g.P(strings.Repeat(" ", 2), "std::sort(item.second.begin(), item.second.end(),")
g.P(strings.Repeat(" ", 7), "[](const ", helper.ParseCppClassType(index.MD), "* a, const ", helper.ParseCppClassType(index.MD), "* b) {")
for i, field := range index.KeyFields {
fieldName := ""
for i, leveledFd := range field.LeveledFDList {
accessOperator := "."
if i == 0 {
accessOperator = "->"
}
fieldName += accessOperator + helper.ParseIndexFieldName(leveledFd) + "()"
}
if i == len(index.KeyFields)-1 {
g.P(strings.Repeat(" ", 8), "return a", fieldName, " < b", fieldName, ";")
} else {
g.P(strings.Repeat(" ", 8), "if (a", fieldName, " != b", fieldName, ") {")
g.P(strings.Repeat(" ", 9), "return a", fieldName, " < b", fieldName, ";")
g.P(strings.Repeat(" ", 8), "}")
}
}
fieldName += accessOperator + helper.ParseIndexFieldName(leveledFd) + "()"
}
if i == len(index.KeyFields)-1 {
g.P(strings.Repeat(" ", depth+8), "return a", fieldName, " < b", fieldName, ";")
} else {
g.P(strings.Repeat(" ", depth+8), "if (a", fieldName, " != b", fieldName, ") {")
g.P(strings.Repeat(" ", depth+9), "return a", fieldName, " < b", fieldName, ";")
g.P(strings.Repeat(" ", depth+8), "}")
g.P(strings.Repeat(" ", 7), "});")
g.P(strings.Repeat(" ", 1), "}")
}
}
g.P(strings.Repeat(" ", depth+7), "});")
g.P(strings.Repeat(" ", depth+1), "}")
}
g.P(strings.Repeat(" ", depth), "}")
}

func generateOneCppMulticolumnIndex(g *protogen.GeneratedFile, depth int, index *index.LevelIndex, parentDataName string, keys []string) []string {
Expand Down
1 change: 0 additions & 1 deletion cmd/protoc-gen-cpp-tableau-loader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func main() {
- messager: only generate "*.pc.h/cc" for each .proto files.
`)
shards = flags.Int("shards", 1, "count of generated hub cpp files for distributed compiling speed-up")
flag.Parse()

protogen.Options{
ParamFunc: flags.Set,
Expand Down
49 changes: 32 additions & 17 deletions cmd/protoc-gen-go-tableau-loader/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ func genIndexLoader(gen *protogen.Plugin, g *protogen.GeneratedFile, descriptor
if levelMessage.FD == nil {
break
}
if !levelMessage.NextLevel.NeedGen() {
break
}
g.P("for _, ", itemName, " := range "+parentDataName+".Get"+helper.ParseIndexFieldName(gen, levelMessage.FD)+"() {")
parentDataName = itemName
defer g.P("}")
depth++
}
for i := depth - 1; i > 0; i-- {
g.P("}")
}
genIndexSorter(gen, g, descriptor)
}

func genOneIndexLoader(gen *protogen.Plugin, g *protogen.GeneratedFile, depth int, index *index.LevelIndex,
Expand Down Expand Up @@ -105,26 +111,35 @@ func genOneIndexLoader(gen *protogen.Plugin, g *protogen.GeneratedFile, depth in
// multi-column index
generateOneMulticolumnIndex(gen, g, depth, index, parentDataName, messagerName, nil)
}
if len(index.KeyFields) != 0 {
g.P("for _, item := range x.", indexContainerName, " {")
g.P(sortPackage.Ident("Slice"), "(item, func(i, j int) bool {")
for i, field := range index.KeyFields {
fieldName := ""
for _, leveledFd := range field.LeveledFDList {
fieldName += ".Get" + helper.ParseIndexFieldName(gen, leveledFd) + "()"
}
if i == len(index.KeyFields)-1 {
g.P("return item[i]", fieldName, " < item[j]", fieldName)
} else {
g.P("if item[i]", fieldName, " != item[j]", fieldName, " {")
g.P("return item[i]", fieldName, " < item[j]", fieldName)
g.P("}")
}

func genIndexSorter(gen *protogen.Plugin, g *protogen.GeneratedFile, descriptor *index.IndexDescriptor) {
for levelMessage := descriptor.LevelMessage; levelMessage != nil; levelMessage = levelMessage.NextLevel {
for _, index := range levelMessage.Indexes {
indexContainerName := "index" + strcase.ToCamel(index.Name()) + "Map"
if len(index.KeyFields) != 0 {
g.P(" // Index(sort): ", index.Index)
g.P("for _, item := range x.", indexContainerName, " {")
g.P(sortPackage.Ident("Slice"), "(item, func(i, j int) bool {")
for i, field := range index.KeyFields {
fieldName := ""
for _, leveledFd := range field.LeveledFDList {
fieldName += ".Get" + helper.ParseIndexFieldName(gen, leveledFd) + "()"
}
if i == len(index.KeyFields)-1 {
g.P("return item[i]", fieldName, " < item[j]", fieldName)
} else {
g.P("if item[i]", fieldName, " != item[j]", fieldName, " {")
g.P("return item[i]", fieldName, " < item[j]", fieldName)
g.P("}")
}
}
g.P("})")
g.P("}")
}
}
g.P("})")
g.P("}")
}
g.P("}")
}

func generateOneMulticolumnIndex(gen *protogen.Plugin, g *protogen.GeneratedFile,
Expand Down
1 change: 0 additions & 1 deletion cmd/protoc-gen-go-tableau-loader/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ var pkg *string
func main() {
var flags flag.FlagSet
pkg = flags.String("pkg", "tableau", "tableau package name")
flag.Parse()

protogen.Options{
ParamFunc: flags.Set,
Expand Down
7 changes: 7 additions & 0 deletions internal/index/descriptor.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ type LevelMessage struct {
Indexes []*LevelIndex
}

func (l *LevelMessage) NeedGen() bool {
if l == nil {
return false
}
return len(l.Indexes) != 0 || l.NextLevel.NeedGen()
}

type LevelIndex struct {
*Index
MD protoreflect.MessageDescriptor
Expand Down
32 changes: 17 additions & 15 deletions test/cpp-tableau-loader/src/protoconf/item_conf.pc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ bool ItemConf::ProcessAfterLoad() {
for (auto&& item2 : item1.second.param_list()) {
index_item_info_map_[item2].push_back(&item1.second);
}
for (auto&& item : index_item_info_map_) {
std::sort(item.second.begin(), item.second.end(),
[](const protoconf::ItemConf::Item* a, const protoconf::ItemConf::Item* b) {
return a->id() < b->id();
});
}
}
{
// Index: Default@ItemDefaultInfo
Expand All @@ -68,15 +62,6 @@ bool ItemConf::ProcessAfterLoad() {
// Index: (ID,Name)<Type,UseEffectType>@AwardItem
Index_AwardItemKey key{item1.second.id(), item1.second.name()};
index_award_item_map_[key].push_back(&item1.second);
for (auto&& item : index_award_item_map_) {
std::sort(item.second.begin(), item.second.end(),
[](const protoconf::ItemConf::Item* a, const protoconf::ItemConf::Item* b) {
if (a->type() != b->type()) {
return a->type() < b->type();
}
return a->use_effect().type() < b->use_effect().type();
});
}
}
{
// Index: (ID,Type,Param,ExtType)@SpecialItem
Expand Down Expand Up @@ -106,6 +91,23 @@ bool ItemConf::ProcessAfterLoad() {
index_use_effect_type_map_[item1.second.use_effect().type()].push_back(&item1.second);
}
}
// Index(sort): Param<ID>@ItemInfo
for (auto&& item : index_item_info_map_) {
std::sort(item.second.begin(), item.second.end(),
[](const protoconf::ItemConf::Item* a, const protoconf::ItemConf::Item* b) {
return a->id() < b->id();
});
}
// Index(sort): (ID,Name)<Type,UseEffectType>@AwardItem
for (auto&& item : index_award_item_map_) {
std::sort(item.second.begin(), item.second.end(),
[](const protoconf::ItemConf::Item* a, const protoconf::ItemConf::Item* b) {
if (a->type() != b->type()) {
return a->type() < b->type();
}
return a->use_effect().type() < b->use_effect().type();
});
}
return true;
}

Expand Down
15 changes: 8 additions & 7 deletions test/cpp-tableau-loader/src/protoconf/test_conf.pc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ bool ActivityConf::ProcessAfterLoad() {
{
// Index: ChapterName<AwardID>@NamedChapter
index_named_chapter_map_[item2.second.chapter_name()].push_back(&item2.second);
for (auto&& item : index_named_chapter_map_) {
std::sort(item.second.begin(), item.second.end(),
[](const protoconf::ActivityConf::Activity::Chapter* a, const protoconf::ActivityConf::Activity::Chapter* b) {
return a->award_id() < b->award_id();
});
}
}
for (auto&& item3 : item2.second.section_map()) {
for (auto&& item4 : item3.second.section_item_list()) {
Expand All @@ -73,6 +67,13 @@ bool ActivityConf::ProcessAfterLoad() {
}
}
}
// Index(sort): ChapterName<AwardID>@NamedChapter
for (auto&& item : index_named_chapter_map_) {
std::sort(item.second.begin(), item.second.end(),
[](const protoconf::ActivityConf::Activity::Chapter* a, const protoconf::ActivityConf::Activity::Chapter* b) {
return a->award_id() < b->award_id();
});
}
return true;
}

Expand Down Expand Up @@ -228,7 +229,7 @@ const ActivityConf::Index_AwardVector* ActivityConf::FindAward(uint32_t id) cons
return &iter->second;
}

const protoconf::Item* ActivityConf::FindFirstAward(uint32_t id) const {
const protoconf::Section::SectionItem* ActivityConf::FindFirstAward(uint32_t id) const {
auto conf = FindAward(id);
if (conf == nullptr || conf->size() == 0) {
return nullptr;
Expand Down
4 changes: 2 additions & 2 deletions test/cpp-tableau-loader/src/protoconf/test_conf.pc.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ class ActivityConf : public Messager {

// Index: SectionItemID@Award
public:
using Index_AwardVector = std::vector<const protoconf::Item*>;
using Index_AwardVector = std::vector<const protoconf::Section::SectionItem*>;
using Index_AwardMap = std::unordered_map<uint32_t, Index_AwardVector>;
const Index_AwardMap& FindAward() const;
const Index_AwardVector* FindAward(uint32_t id) const;
const protoconf::Item* FindFirstAward(uint32_t id) const;
const protoconf::Section::SectionItem* FindFirstAward(uint32_t id) const;

private:
Index_AwardMap index_award_map_;
Expand Down
28 changes: 15 additions & 13 deletions test/go-tableau-loader/protoconf/loader/item_conf.pc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 10 additions & 9 deletions test/go-tableau-loader/protoconf/loader/test_conf.pc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading