Golang – 使用Appengine datastore进行[]byte比较的筛选查询

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

Golang - Appengine datastore filter query with []byte comparison

问题

我正在尝试在数据存储中对一组实体执行过滤查询,但我要查询的实体字段是 []byte 类型,并且我不知道 appengine 数据存储是否可以执行此比较。

这是我的实体:

  1. type Data struct {
  2. Id int64 `json:"id"`
  3. Version int32 `json:"-"`
  4. HMAC []byte `json:"-"`
  5. Status string `json:"status"`
  6. }

这是我的查询逻辑:

  1. func (view *DataView) GetDataByHMAC(hmac []byte) (Data, error) {
  2. view_key := datastore.NewKey(view.context, "View", "data-view", 0, nil)
  3. data := make([]Data, 0)
  4. query := datastore.
  5. NewQuery("ViewData").
  6. Ancestor(view_key).
  7. Filter("HMAC =", hmac)
  8. _, err := query.GetAll(view.context, &data)
  9. if err != nil {
  10. return Data{}, err
  11. }
  12. if len(data) == 0 {
  13. return Data{}, ErrNoData
  14. }
  15. return data[0], nil
  16. }

代码可以编译通过,但是没有返回任何结果,即使在 10 秒的时间内进行了程序化的重试,所以我不认为这是数据存储和视图数据之间的最终一致性问题。

我的主要问题是:appengine 数据存储是否允许在 []byte 类型的字段上使用比较过滤器进行查询?

英文:

I am trying to perform a filter query on a set of entities in the datastore, but the entity field I am trying to query against, with the equality operator, is of type []byte and I don't know if appengine datastore can perform this comparison

This is my entity:

  1. type Data struct {
  2. Id int64 `json:"id"`
  3. Version int32 `json:"-"`
  4. HMAC []byte `json:"-"`
  5. Status string `json:"status"`
  6. }

And here is my query logic

  1. func (view *DataView) GetDataByHMAC(hmac []byte) (Data, error) {
  2. view_key := datastore.NewKey(view.context, "View", "data-view", 0, nil)
  3. data := make([]Data, 0)
  4. query := datastore.
  5. NewQuery("ViewData").
  6. Ancestor(view_key).
  7. Filter("HMAC = ", hmac)
  8. _, err := query.GetAll(view.context, &data)
  9. if err != nil {
  10. return Data{}, err
  11. }
  12. if len(data) == 0 {
  13. return Data{}, ErrNoData
  14. }
  15. return data[0], nil
  16. }

It builds but does not return any results, even after programmatically retrying over the course of 10 seconds so i do not believe it is an issue of eventual consistency between the datastore and the view data that I've stored there.

My main question is: does the appengine datastore allow for a query to use a comparison filter on a field with type []byte?

答案1

得分: 4

在1.9.11版本中,引入了ByteString类型到数据存储包中。它可以用于存储短的、索引的字节片段。

如果你将实体更改为以下内容,它应该可以工作:

  1. type Data struct {
  2. ID int64 `json:"id"`
  3. Version int32 `json:"-"`
  4. HMAC datastore.ByteString `json:"-"`
  5. Status string `json:"status"`
  6. }

更多信息:https://developers.google.com/appengine/docs/go/datastore/entities#Go_Properties_and_value_types

英文:

In 1.9.11, the ByteString type was introduced to the datastore package. It can be used for storing short, indexed byte slices.

If you change your entity to the following, it should work:

  1. type Data struct {
  2. ID int64 `json:"id"`
  3. Version int32 `json:"-"`
  4. HMAC datastore.ByteString `json:"-"`
  5. Status string `json:"status"`
  6. }

More info: https://developers.google.com/appengine/docs/go/datastore/entities#Go_Properties_and_value_types

答案2

得分: 3

你的主要问题的答案是否定的,因为[]byte被存储为blob,而blob在应用引擎数据存储中不被索引。

在具有筛选器或排序顺序的查询中,无法匹配该实体。
注意:除了你明确声明的任何未索引属性外,类型为[]byte的属性也会自动被视为未索引。

这是文档链接:https://developers.google.com/appengine/docs/go/datastore/indexes#Go_Unindexed_properties

英文:

The answer to your main question is No, because []byte is stored as blob, which is not indexed by the app engine datastore.

  1. queries with a filter or sort order on the unindexed property
  2. will never match that entity.
  3. Note: In addition to any unindexed properties you declare explicitly,
  4. those typed as []byte are automatically treated as unindexed.

Here is the documentation: https://developers.google.com/appengine/docs/go/datastore/indexes#Go_Unindexed_properties

huangapple
  • 本文由 发表于 2014年9月14日 09:08:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/25829202.html
匿名

发表评论

匿名网友

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

确定