Golang - Appengine Data Warehouse Filter Request with Byte Match []

I am trying to fulfill a filter request for a set of objects in a data warehouse, but the field of the object to which I am trying to execute a query with the equality operator is of type [] bytes, and I do not know if the appengine datastore can perform this comparison

This is my essence:

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 a software retry for 10 seconds, so I don’t think this is a problem of possible consistency between the data store and the viewing data that I store there.

My main question is: does the appengine datastore allow me to use a query to use a comparison filter in a field of type [] byte?

+5
source share
2 answers

The answer to your main question: No, because [] bytes are stored as blob, which is not indexed by the application data store.

 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

+3
source

In 1.9.11, the ByteString type was introduced into the data warehouse package. It can be used to store short, indexed byte fragments.

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

 type Data struct { ID int64 `json:"id"` Version int32 `json:"-"` HMAC datastore.ByteString `json:"-"` Status string `json:"status"` } 

Additional information: https://developers.google.com/appengine/docs/go/datastore/entities#Go_Properties_and_value_types

+4
source

Source: https://habr.com/ru/post/1202515/


All Articles