Skip to content

Commit

Permalink
Merge place metadata cache for place info API (#1159)
Browse files Browse the repository at this point in the history
  • Loading branch information
shifucun authored Jun 7, 2023
1 parent 64455c8 commit e4f5458
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
85 changes: 49 additions & 36 deletions internal/server/place/places.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,53 +207,66 @@ func GetPlaceMetadataHelper(
return nil, err
}
result := map[string]*pb.PlaceMetadata{}
metaMap := map[string]*pb.PlaceMetadataCache_PlaceInfo{}
for _, btData := range btDataList {
for _, row := range btData {
entity := row.Parts[0]
if _, ok := result[entity]; ok {
continue
}
raw, ok := row.Data.(*pb.PlaceMetadataCache)
if !ok {
continue
}
processed := pb.PlaceMetadata{}
metaMap := map[string]*pb.PlaceMetadataCache_PlaceInfo{}
for _, info := range raw.Places {
metaMap[info.Dcid] = info
if currInfo, ok := metaMap[info.Dcid]; !ok {
metaMap[info.Dcid] = info
} else {
// Merge place info from new import group.
if currInfo.Name == "" {
currInfo.Name = info.Name
}
if currInfo.Type == "" {
currInfo.Type = info.Type
}
currInfo.Parents = append(currInfo.Parents, info.Parents...)
}
}
}
}
for _, entity := range entities {
entInfo, ok := metaMap[entity]
if !ok {
entInfo = &pb.PlaceMetadataCache_PlaceInfo{Dcid: entity}
}
processed := pb.PlaceMetadata{}
processed.Self = &pb.PlaceMetadata_PlaceInfo{
Dcid: entity,
Name: entInfo.Name,
Type: entInfo.Type,
}
visited := map[string]struct{}{}
parents := entInfo.Parents
for {
if len(parents) == 0 {
break
}
processed.Self = &pb.PlaceMetadata_PlaceInfo{
Dcid: entity,
Name: metaMap[entity].Name,
Type: metaMap[entity].Type,
curr := parents[0]
parents = parents[1:]
if _, ok := visited[curr]; ok {
continue
}
visited := map[string]struct{}{}
parents := metaMap[entity].Parents
for {
if len(parents) == 0 {
break
}
curr := parents[0]
parents = parents[1:]
if _, ok := visited[curr]; ok {
continue
}
// To handle potential data issue in the cache, where parent node
// is not in the PlaceInfo.Places field.
info, ok := metaMap[curr]
if !ok {
continue
}
processed.Parents = append(processed.Parents, &pb.PlaceMetadata_PlaceInfo{
Dcid: curr,
Name: info.Name,
Type: info.Type,
})
visited[curr] = struct{}{}
parents = append(parents, info.Parents...)
// To handle potential data issue in the cache, where parent node
// is not in the PlaceInfo.Places field.
info, ok := metaMap[curr]
if !ok {
continue
}
result[entity] = &processed
processed.Parents = append(processed.Parents, &pb.PlaceMetadata_PlaceInfo{
Dcid: curr,
Name: info.Name,
Type: info.Type,
})
visited[curr] = struct{}{}
parents = append(parents, info.Parents...)
}
result[entity] = &processed
}
return result, nil
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{
"data": [
{
"node": "earth"
"node": "earth",
"info": {
"self": {
"dcid": "earth"
}
}
},
{
"node": "geoId/02158000100",
Expand Down

0 comments on commit e4f5458

Please sign in to comment.