无法索引的Datastore BLOB

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

Datastore BLOB that cannot be indexed

问题

我有一个长度约为180个字符的字符串,类型为datastore.ByteString,我希望对其进行索引,以便能够按类型进行过滤。

keys, err := datastore.NewQuery("User").
    Filter("SubscriptionToken =", []byte(subscriptionToken)).Count(c)

当我尝试进行过滤时,出现了以下错误:

{Service:"datastore_v3", Detail:"Property "SubscriptionToken" has a value meaning BLOB that cannot be indexed.", Code:1}

我唯一能想到的原因是bytestring的大小超过了1500k,因此无法进行索引?

但我无法弄清楚如何检查bytestring的大小。

更新: 这是我插入User实体的方式:

type User struct {
    UserEmail         string
    SubscriptionToken datastore.ByteString
}
// subscriptionToken有值
u := User{
    UserEmail:         userEmail,
    SubscriptionToken: datastore.ByteString(subscriptionToken),
}

k := datastore.NewKey(c, "User", userKey, 0, nil)
_, err = datastore.Put(c, k, &u)
if err != nil {
    log.Debugf(c, "Write datastore.Put: %#v ", err)
    return err
}
英文:

I have a string (~180 chars length) as datastore.ByteString type, I want it to be indexed so that I will able to filter by the type.

keys, err := datastore.NewQuery("User").
    Filter("SubscriptionToken= ", []byte(subscriptionToken)).Count(c)

When I'm trying to filter I'm getting this error:

{Service:"datastore_v3", Detail:"Property \"SubscriptionToken\" has a value meaning BLOB that cannot be indexed.", Code:1}

The only reason that I think about is that the size of the bytestring is greater than 1500k and it can't be indexed?

But I can't figure out how to check the bytestring size.

UPDATE: this is how i insert the User entity

type User struct {
	UserEmail         string
	SubscriptionToken datastore.ByteString
}
//subscriptionToken has value
u := User{
			UserEmail:         userEmail,
			SubscriptionToken: datastore.ByteString(subscriptionToken),
		}

k := datastore.NewKey(c, "User", userKey, 0, nil)
		_, err = datastore.Put(c, k, &u)
		if err != nil {
			log.Debugf(c, "Write datastore.Put: %#v ", err)
			return err
		}

答案1

得分: 1

错误消息表明,您在Datastore中的User实体中有一个名为SubscriptionToken的属性,其类型不是ByteString,而是[]byte

类型为[]byte的属性不会被索引。

如果您想要将字节切片类型的属性进行索引,在保存实体时,该属性必须具有datastore.ByteString类型的值,例如:

type User struct {
    SubscriptionToken datastore.ByteString
}

u := User{SubscriptionToken: datastore.ByteString("somevalue")}
// 保存实体
key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "User", nil), &u)
if err != nil {
    // 处理错误
}

datastore.ByteString本质上只是一个字节切片([]byte),没有其他特殊之处(目前而言),它表示您希望保存的字节切片进行索引。

当您尝试通过此属性进行过滤时,要过滤的属性值必须是[]byte类型的值:

q := datastore.NewQuery("User").Filter("SubscriptionToken=", []byte(subscriptionToken))

// 如果要计数:
count, err := q.Count(c)
if err != nil {
    // 处理错误
}

// 查询/过滤:
var users []*User
keys, err := q.GetAll(c, &users)
if err != nil {
    // 处理错误
}

另外,要检查ByteString值的长度,您可以简单地使用内置函数len(),因为ByteString是一个字节切片([]byte):

length := len(u.SubscriptionToken)
英文:

The error message indicates that the User entity that you have in your Datastore has a property SubscriptionToken which is not of type ByteString but rather []byte.

Properties of type []byte are not indexed.

If you want a property of type Byte slice to be indexed, when you save the entity, the property must have a value of type datastore.ByteString, for example:

type User struct {
    SubscriptionToken datastore.ByteString
}

u := User{SubscriptionToken: datastore.ByteString("somevalue")}
// Save u
key, err := datastore.Put(c, datastore.NewIncompleteKey(c, "User", nil), &u)
if err != nil {
    // Handle error
}

datastore.ByteString is basically just a byte slice ([]byte) and nothing more (at the moment): it signals that you want the the byte slice that is being saved to be indexed.

And when you try to filter by this property, the property value to filter by has to be a value of type []byte:

q := datastore.NewQuery("User").Filter("SubscriptionToken=", []byte(subscriptionToken))

// If you want to count:
count, err := q.Count(c)
if err != nil {
    // Handle error
}

// Query/filter:
var users []*User
keys, err := q.GetAll(c, &users)
if err != nil {
    // Handle error
}

Btw to check the length of a ByteString value, you can simply use the builtin function len() since a ByteString is a byte slice ([]byte):

length := len(u.SubscriptionToken)

huangapple
  • 本文由 发表于 2015年5月11日 21:51:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/30169476.html
匿名

发表评论

匿名网友

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

确定