如何过滤 GAE 查询? [英] How to filter a GAE query?

查看:17
本文介绍了如何过滤 GAE 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存两条记录,然后获取第二条记录.问题是过滤器似乎不起作用.虽然我按姓名(Andrew W")过滤,但我总是得到Joe Citizen".当它应该只有一个时,计数器也会指示 2 个记录.这让我发疯.请参阅下面的完整代码.结果打印 counter 2 e2 {"Joe Citizen" "Manager" "2015-03-24 09:08:58.363929 +0000 UTC" ""}

I'm trying to save two records and then get the 2nd one. The issue is that the filter doesn't seem to work. Although I filter by Name ("Andrew W") I always get "Joe Citizen". The counter also indicates 2 records when it should be just one. This drives me crazy. See the full code below. The result prints counter 2 e2 {"Joe Citizen" "Manager" "2015-03-24 09:08:58.363929 +0000 UTC" ""}

package main
import (
    "fmt"
    "time"
    "net/http"

    "google.golang.org/appengine"
    "google.golang.org/appengine/datastore"
)


type Employee struct {
    Name     string
    Role     string
    HireDate time.Time
    Account  string
}
func init(){

    http.HandleFunc("/", handle)
}
func handle(w http.ResponseWriter, r *http.Request) {
    c := appengine.NewContext(r)

    e1 := Employee{
        Name:     "Joe Citizen",
        Role:     "Manager",
        HireDate: time.Now(),
    }

    _, err := datastore.Put(c, datastore.NewKey(c, "employee", "", 0, nil), &e1)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    panic(err)
        return
    }
    e1.Name = "Andrew W"

    _, err = datastore.Put(c, datastore.NewKey(c, "employee", "", 0, nil), &e1)
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    panic(err)
        return
    }

    var e2 Employee
    q :=  datastore.NewQuery("employee")
    q.Filter("Name =", "Andrew W")
    cnt, err  := q.Count(c)
    if err !=nil{
        http.Error(w, err.Error(), http.StatusInternalServerError)
        panic(err)
        return
    }
    for t := q.Run(c); ; {  
        if _, err := t.Next(&e2); err != nil {
             http.Error(w, err.Error(), http.StatusInternalServerError)
            panic(err)
            return
        }
        break
    }   
    fmt.Fprintf(w, "counter %v e2 %q", cnt, e2)
}

推荐答案

(第一)问题是这样的:

The (first) problem is this:

q :=  datastore.NewQuery("employee")
q.Filter("Name =", "Andrew W")

Query.Filter() 返回包含您指定的过滤器的派生查询.您必须存储返回值并持续使用它:

Query.Filter() returns a derivative query with the filter you specified included. You have to store the return value and use it ongoing:

q := datastore.NewQuery("employee")
q = q.Filter("Name =", "Andrew W")

或者只有一行:

q := datastore.NewQuery("employee").Filter("Name =", "Andrew W")

注意:如果没有这个,您执行的查询将没有过滤器,因此将返回所有以前保存的 "employee" 类型的实体,其中 "Joe Citizen" 可能成为您看到打印的第一个.

Note: Without this the query you execute would have no filters and therefore would return all previously saved entities of the kind "employee", where "Joe Citizen" might be the first one which you see printed.

对于第一次运行,您很可能会看到 0 个结果.请注意,由于您不使用祖先查询,最终一致性适用.开发 SDK 模拟了具有最终一致性的高复制数据存储,因此 Put() 操作之后的查询将看不到结果.

For the first run you will most likely see 0 results. Note that since you don't use Ancestor queries, eventual consistency applies. The development SDK simulates the High replication datastore with its eventual consistency, and therefore the query following the Put() operations will not see the results.

如果在继续查询之前放入一个小的time.Sleep(),您将看到您期望的结果:

If you put a small time.Sleep() before proceeding with the query, you will see the results you expect:

time.Sleep(time.Second)

var e2 Employee
q := datastore.NewQuery("employee").Filter("Name=", "Andrew W")
// Rest of your code...

另请注意,在 SDK 中运行您的代码可以通过创建如下上下文来模拟强一致性:

Also note that running your code in the SDK you can simulate strong consistency by creating your context like this:

c, err := aetest.NewContext(&aetest.Options{StronglyConsistentDatastore: true})

当然这只是为了测试目的,你不能在生产中这样做.

But of course this is for testing purposes only, you can't do this in production.

如果您想要高度一致的结果,请在创建密钥时指定祖先密钥,并使用 祖先查询.仅当您想要高度一致的结果时才需要祖先键.如果您对显示结果的延迟几秒钟没有问题,则不必这样做.另请注意,祖先键不必是现有实体的键,它只是语义.您可以创建任何虚构的密钥.对多个实体使用相同的(虚构的)键会将它们放入同一个实体组中,并且该组上的祖先查询将高度一致.

If you want strongly consistent results, specify an ancestor key when creating the key, and use ancestor queries. An ancestor key is only required if you want strongly consistent results. If you're fine with a few seconds delay for the results to show up, you don't have to. Also note that the ancestor key does not have to be the key of an existing entity, it's just semantics. You can create any fictional key. Using the same (fictional) key to multiple entities will put them into the same entity group and ancestor queries on this group will be strongly consistent.

祖先密钥通常是一个现有的密钥,通常来自当前用户或帐户,因为它可以轻松创建/计算并且它保存/存储一些附加信息,但如上所述,它不必是.

Often the ancestor key is an existing key, usually derived from the current user or account, because that can be created/computed easily and it holds/stores some additional information, but as noted above, it doesn't have to be.

这篇关于如何过滤 GAE 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆