MIkCode
MIkCode

Reputation: 2845

Datastore BLOB that cannot be indexed

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
        }

Upvotes: 4

Views: 263

Answers (1)

icza
icza

Reputation: 418087

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)

Upvotes: 1

Related Questions