BiDirectional Key to <-> "CompositKey" lookup in GAE?

huangapple go评论81阅读模式
英文:

BiDirectional Key to <-> "CompositKey" lookup in GAE?

问题

我正在GAE上编写一个Go应用程序
我有一个名为Connection{Token, ToAdress, FromAdress}的类型

我想将它存储在数据存储中,以便我可以通过GetConnectionByT(Token string)进行查找
但也可以通过GetConnectionByA(FromAdress,ToAdress string)进行查找

怎样做才是最好的方式?目前,我的Connect类型有两个不同的.key()函数,并且每次添加时都会插入具有不同键的重复条目,但这感觉非常错误 BiDirectional Key to <-> "CompositKey" lookup in GAE?

datastore.NewQuery("Connection").Filter是正确的方法吗?似乎键应该更快更便宜?但如果不是,如何过滤多个字段?

英文:

Am writing a Go application on GAE
And I have a type called Connection{Token, ToAdress, FromAdress}

I want to store it in the datastore so that I can look it up via
GetConnectionByT(Token string)
But also via
GetConnectionByA(FromAdress,ToAdress string)

What is the best way of doing this? Right now my Connect type have two different .key() functions and I insert duplicate entry's with different keys every time I add but this feels really really wrong BiDirectional Key to <-> "CompositKey" lookup in GAE?

Is datastore.NewQuery(&quot;Connection&quot;).Filter the way to go? Seames that a key should be faster and cheaper? But if not how do I filter multiple fields?

答案1

得分: 1

如果您查看数据存储查询过滤器的文档,您会注意到多个过滤器是AND连接的。所以您可以这样做:

q := datastore.NewQuery("Connection").
    Filter("FromAddress =", a1).
    Filter("ToAddress =", a2).
    Order("-FromAddress")

如果您对实体有一个键,那么使用键可能会更快。然而,从您问题的描述中很难确定。

英文:

If you check the documentation for datastore.Query.Filter you'll note that multiple filters are ANDed together. So you can do this kind of thing:

q := datastore.NewQuery(&quot;Connection&quot;).
    Filter(&quot;FromAddress =&quot;, a1).
    Filter(&quot;ToAddress =&quot;, a2).
    Order(&quot;-FromAddress&quot;)

If you have a key for the entity in question then yes, using the key will probably be faster. However it's a bit difficult to tell from the description in your question.

huangapple
  • 本文由 发表于 2013年2月23日 18:38:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/15039557.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定