App Engine数据存储:如何使用golang在属性上设置多个值? [英] App Engine Datastore: How to set multiple values on a property using golang?

查看:90
本文介绍了App Engine数据存储:如何使用golang在属性上设置多个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一块int64,我希望能够存储在Google数据存储中的单个属性并检索。从文档中我可以看到,通过实现PropertyLoadSaver {}接口可以实现对此的支持。但我似乎无法提供正确的实现。



基本上,这是我想完成的:

 类型Post结构{
标题字符串
UpVotes [] int64`json: - xml: - datastore:,multiple`
DownVotes [] int64`json: - xml: - datastore:,multiple`
}

c:= appengine.NewContext(r)
p :=& Post {
标题:name
UpVotes:[] int64 {23,45,67,89,10}
DownVotes:[] int64 {90,87,65 ,43,21,123}
}
k:= datastore.NewIncompleteKey(c,Post,nil)
err:= datastore.Put(c,k,p)

但没有数据存储:无效的实体类型错误。

默认情况下,AppEngine支持多值属性,您无需执行任何特殊的操作即可使其正常工作。您不需要实现 PropertyLoadSaver 接口,并且不需要任何特殊的标记值。



如果结构字段是切片类型,它将自动成为一个多值属性。这段代码有效:

  type Post struct {
标题字符串
UpVotes [] int64
DownVotes [] int64
}

c:= appengine.NewContext(r)
p:=& Post {
标题:name,
UpVotes :[] int64 {23,45,67,89,10},
DownVotes:[] int64 {90,87,65,43,21,123},
}
k:= datastore.NewIncompleteKey(c,Post,nil)
key,err:= datastore.Put(c,k,p)
c.Infof(Result:key:%v,err:% v,key,err)

当然如果你想要的话,你可以为json和xml指定标签值:

 类型Post结构{
标题字符串
UpVotes [] int64`json: - xml : - `
DownVotes [] int64`json: - xml: - `
}

注意:

但请注意,多值属性并不适合存储大量如果属性被编入索引值。这样做需要很多索引(很多写法)来存储和修改实体,并且可能会达到实体的索引限制(请参阅索引限制和爆炸索引以获取更多详细信息)。

例如,您不能使用一个多值属性,用于为 Post 存储数百个上涨和下跌提示。为此,您应该将投票存储为链接到发布的单独/不同实体,例如通过 Post Key ,或者最好是它的 IntID


I am trying to save multiple values for a single property in Google's Datastore using Golang.

I have a slice of int64 that I would like to be able to store and retrieve. From the documentation I can see that there is support for this by implementing the PropertyLoadSaver{} interface. But I cannot seem to come up with a correct implementation.

Essentially, this is what I'd like to accomplish:

type Post struct {
    Title         string
    UpVotes       []int64 `json:"-" xml:"-" datastore:",multiple"`
    DownVotes     []int64 `json:"-" xml:"-" datastore:",multiple"`
}

c := appengine.NewContext(r)
p := &Post{
    Title: "name"
    UpVotes: []int64{23, 45, 67, 89, 10}
    DownVotes: []int64{90, 87, 65, 43, 21, 123}
}
k := datastore.NewIncompleteKey(c, "Post", nil)
err := datastore.Put(c, k, p)

But without the "datastore: invalid entity type" error.

解决方案

Multi-valued properties are supported by AppEngine by default, you don't need to do anything special to make it work. You don't need to implement the PropertyLoadSaver interface, and you don't need any special tag value.

If a struct field is of a slice type, it will automatically be a multi-valued property. This code works:

type Post struct {
    Title         string
    UpVotes       []int64
    DownVotes     []int64
}

c := appengine.NewContext(r)
p := &Post{
    Title: "name",
    UpVotes: []int64{23, 45, 67, 89, 10},
    DownVotes: []int64{90, 87, 65, 43, 21, 123},
}
k := datastore.NewIncompleteKey(c, "Post", nil)
key, err := datastore.Put(c, k, p)
c.Infof("Result: key: %v, err: %v", key, err)

Of course if you want you can specify tag value for json and xml:

type Post struct {
    Title         string
    UpVotes       []int64 `json:"-" xml:"-"`
    DownVotes     []int64 `json:"-" xml:"-"`
}

Notes:

But please note that multi-valued properties are not suitable to store a large number of values if the property is indexed. Doing so would require many indices (many writes) to store and modify the entity, and potentially you could hit the index limit for an entity (see Index limits and Exploding indexes for more details).

So for example you can't use a multi-valued property to store hundreds of up- and downvotes for a Post. For that you should store votes as separate/different entities linking to the Post e.g. by the Key of the Post or preferably just its IntID.

这篇关于App Engine数据存储:如何使用golang在属性上设置多个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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