dongshenyu4638 2019-03-10 09:42
浏览 28

Google Cloud Datastore阶层树过滤Go语言

I have a slight feeling I'm missing the obvious but can't seem to figure this one out. I'm migrating an API from a traditional MySQL data model to Cloud Datastore and having trouble recursively building the hierarchy tree when filtering a child entity using the Go Library.

My model looks like this:

    type Country struct {
        Key *datastore.Key `datastore:"__key__"`
        Id int
        Name string
        Region []Region
    }

    type Region struct {
        Key *datastore.Key `datastore:"__key__"`
        Id int
        Name string
        State []State
    }

    type State struct {
        Key *datastore.Key `datastore:"__key__"`
        Id int
        Name string
    }

I'm NOT using nested entities in datastore like the representation above. In datastore I have 3 different entities storing each with its respective Ancestor key as such:

Entity Country: Parent = nil
Entity Region: Parent = Country
Entity State: Parent = Region

I'd like to query for a given list of states and build the correct representation of the entire hierarchy within the structs. I could possible achieve this retrieving all the States entities that satisfy my criteria first:

    var state []*State

    query := datastore.NewQuery("State").Filter("Name=", params[i])

    if _, err = client.GetAll(ctx, query, &state); err != nil {
        return nil, err
    }

and then iterate over state to look for the region each state belongs to:

    var region Region

    if err := client.Get(ctx, state[i].Key.Parent, &region); err != nil {
        return nil, err
    }

The problem with this solution is that more than one state returned can belong to the same region and if I get the parent region for each state, I'll ultimately end up with a slice of regions that will likely have repeated regions in it with a single state each.

I know I could solve this through my code sorting and checking if a region has already been added and then just append the state onto it. However, this solution seem to be a very cumbersome way to solve this problem and I'm wondering if I have another alternatives.

Just as a side note, this model is just a simplified representation of the real one. The real model has 9 levels in the hierarchy and users can search as far as the lowest level and the respective tree needs to be represented in the final JSON.

  • 写回答

1条回答 默认 最新

  • dongshungou7699 2019-03-12 01:54
    关注

    I'm not completely sure what you are doing. However, it sounds like you should be encoding the name of the parent in the parent's key. That way you can display part of the hierarchy you need without fetching the parent entities. E.g. a full child key may look like "(Country, USA), (Region, MidWest), (State, Michigan)".

    If that doesn't work, I think you should follow your initial idea of putting the parent keys to fetch in a set, then fetching all the parents at once.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题