Golang - Appengine数据存储过滤器查询与[]字节比较 [英] Golang - Appengine datastore filter query with []byte comparison

查看:187
本文介绍了Golang - Appengine数据存储过滤器查询与[]字节比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对数据存储中的一组实体执行过滤器查询,但是我试图用等号运算符查询的实体字段的类型是[]字节,我不知道是否appengine datastore可以执行这个比较

这是我的实体:

$ $ $ $ $ $ $ $数据结构{
Id int64`json:id`
版本int32`json: - `
HMAC [] byte`json: - `
状态字符串`json:status`
}

这里是我的查询逻辑

  func(view * DataView)GetDataByHMAC(hmac [] byte)(Data,error){
view_key:= datastore.NewKey (view.context,View,data-view,0,nil)
data:= make([] Data,0)
query:= datastore。
NewQuery(ViewData)。
祖先(view_key)。
Filter(HMAC =,hmac)
_,err:= query.GetAll(view.context,& data)
if err!= nil {
return Data {},err
}
如果len(data)== 0 {
返回Data {},ErrNoData
}
返回数据[0],nil



$ b $ p
$ b

它会建立但不会返回任何结果,即使经过编程重试10秒,所以我不相信这是数据存储和我存储在那里的视图数据之间的最终一致性问题。



我的主要问题是:appengine数据存储允许查询在类型为[]字节的字段上使用比较过滤器? 解决方案

您的主要问题的答案因为[]字节是以blob存储的,所以没有被应用程序引擎数据存储区索引。

 带有过滤器的查询或对未标记的属性
的排序将永远不匹配该实体。
注意:除了显式声明的任何未指定索引的属性外,键入为[]字节的
将被自动视为未索引。

以下是文档: https://developers.google.com/appengine/docs/go/datastore/indexes#Go_Unindexed_properties


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:

type Data struct {
 Id          int64  `json:"id"`
 Version     int32  `json:"-"`
 HMAC        []byte `json:"-"`
 Status      string `json:"status"`
}

And here is my query logic

func (view *DataView) GetDataByHMAC(hmac []byte) (Data, error) {
    view_key := datastore.NewKey(view.context, "View", "data-view", 0, nil)
    data := make([]Data, 0)
    query := datastore.
       NewQuery("ViewData").
       Ancestor(view_key).
       Filter("HMAC = ", hmac)
    _, err := query.GetAll(view.context, &data)
    if err != nil {
       return Data{}, err
    }
    if len(data) == 0 {
       return Data{}, ErrNoData
    }
    return data[0], nil
}

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?

解决方案

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

queries with a filter or sort order on the unindexed property 
will never match that entity.
Note: In addition to any unindexed properties you declare explicitly, 
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

这篇关于Golang - Appengine数据存储过滤器查询与[]字节比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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