dongtan2017 2016-05-26 21:16
浏览 14
已采纳

具有可选计数的Golang数据存储区查询

I'm having some strange issues with datastore queries on golang. The query executes fine however when it comes to pulling the total (cli.Count()) it fails every time consistently by timing out. Can someone explain if I'm doing something wrong or if there is a correct way to do this? The strange part is just that it fails on a query count and I don't get why, I've tried to create a NewQuery and intializing a new client too but neither works.

// GetListByID provides an iterative list of all items filtered by ID.
func GetListByID(theID, limit, offset int) ([]Item, int, error) {
    var itemList []Item
    cli, ctx, err := getClient()
    if err != nil {
        return itemList, 0, err
    }
    if limit <= 0 {
        limit = 20
    }

    q := datastore.NewQuery("SomeKind").Filter("MasterID = ", theID)
    ql := q.Limit(limit).Offset(offset)
    _, err = cli.GetAll(ctx, ql, &itemList)
    if err != nil {
        return itemList, 0, err
    }
    if len(itemList) < limit && offset == 0 {
        return itemList, len(itemList), nil
    }
    total, err := cli.Count(ctx, q)

    return itemList, total, err
}

Please note that it does not run on appengine. It runs on instances in AWS and in datacenter. (Don't ask)

  • 写回答

2条回答 默认 最新

  • dongniechi7825 2016-06-11 21:35
    关注

    Answer is actually doing what I expect it to, there was a bug that was addressed recently by OkDave on github ( I subitted an issue ), link is available here: https://github.com/GoogleCloudPlatform/gcloud-golang/issues/268. I do appologize for forgetting to get back to here to update with an answer and I do appreciate your details as it explains a lot of things that most may actually miss. For my needs I don't want to use cursors as it would break the functionality of what is implemented. I'm expecting something along the lines of select items 50 - 100, when the query is executed again a second time items 50 - 100 should not be the same as there were additional items inserted into the db prior to the second query. There are reasons to use cursors and there are reasons to not... in my case I don't want it. Issue was more related to the count which was fixed though.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应