dt1888 2019-05-17 12:13
浏览 37

无法从使用IncompleteKey()创建的数据存储中检索实体

I am using "cloud.google.com/go/datastore" to store, update, retrieve entities from datastore. Struct created by me is -

type TExp struct {
    Key        string   `json:"key" datastore:"key,noindex"`
    Value      string   `json:"value" datastore:"value,noindex"`
} 

type service struct {
DataLayer *datastore.Client
}

When I create id for this using datastore.IncompleteKey("TExp", nil), I am able to create entity and can see it in real datastore.

func (s service)addTExp(ctx context.Context, request interface{}) (interface{}, error) {

req := request.(*TExp)
var tExp []TExp
var tExp2 TExp
tExp2.Key   = req.Key
tExp2.Value =   req.Value

query := datastore.NewQuery("TExp").Filter("key =", req.Key).Namespace("AdLive")
keys, err := s.DataLayer.GetAll(ctx, query, &tExp)
if err != nil {
    log.Errorf(ctx,"Error occurred while checking existence of key in kind")
}

if keys == nil {
log.Infof(ctx,"Keys: %v ", keys)
k := datastore.IncompleteKey("TExp", nil)
k.Namespace = "AdLive"
k.Kind = "TExp"

if _, err := s.DataLayer.Put(ctx, k, &tExp2); err != nil {
    return nil, marvin.NewJSONStatusResponse(
        "could not add TExp in DS",
        http.StatusBadRequest,
    )} 
   }
   return "OK",nil
}

Problem is before creating any entity, I want to ensure that any entity with same Key value doesn't exist in kind of that datastore already. To do this, first of all I am trying to check existence using GetAll method than if keys are nil, I want to call datastore.Put to add new entity. However, in each case(even for already present entities in Datastore), "keys" variable always get nil value.

keys, err := s.DataLayer.GetAll(ctx, query, &tExp)

Therefore, multiple entities get created with same key value in datastore. I am not able to find out how can I fix this issue in golang.

Note:- in datastore entities have structure

["Name/ID automatically generated by Datastore on calling IncompleteKey method", Key, Value].

  • 写回答

1条回答 默认 最新

  • douqi4673 2019-05-17 22:29
    关注

    First, if you want to fetch a specific entity, you should use Get instead of GetAll, as Get is strongly consistent, and GetAll is not.

    Second, since you are doing a Put with an incomplete key, your will never have a conflict on the new entity that you are only adding to your Datastore. It seems you want to be using a transaction to both Get & Put an entity with the complete key that is passed into your function.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算