dtwncxs3547 2015-03-24 09:13
浏览 68
已采纳

如何过滤GAE查询?

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)
}
  • 写回答

1条回答 默认 最新

  • donglei7152 2015-03-24 09:52
    关注

    The (first) problem is this:

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

    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")
    

    Or just one line:

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

    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.

    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.

    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...
    

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么