本文介绍了Objectify查询结果和Datastore查看器结果的不一致性问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个基于TodoMVC angularjs的示例项目()和后端api与Google App Engine云端点相关联,并且在从App Engine Datastore获取todos侦听时获得一致性结果。



Todo对象存储在应用程序中引擎数据存储使用objectify。



Todo实体编码如下:

  @Entity 
公共类Todo {

@Id
私人长ID;
私有字符串标题;
私有布尔值完成;
私人日期lastEdit;

@Index
private Long userId;
$ b public Todo(){
}
$ b $ public Todo(String title,boolean completed){
this.title = title;
this.completed =已完成;
}

public Todo(Long id,String title,boolean completed){
this.id = id;
this.title = title;
this.completed =已完成;
}

public Long getId(){
return id;
}

public void setId(Long id){
this.id = id;
}

public String getTitle(){
return title;
}

public void setTitle(String title){
this.title = title;
}

public boolean isCompleted(){
return completed;
}

public void setCompleted(boolean completed){
this.completed = completed;
}

public Long getUserId(){
return userId;
}

public void setUserId(Long userId){
this.userId = userId;
}

public Date getLastEdit(){
return lastEdit;
}

public void setLastEdit(Date lastEdit){
this.lastEdit = lastEdit;
}

@Override
public String toString(){
returnTodo {+
id =+ id +
,title ='+ title +'\''+
,completed =+ completed +
,lastEdit =+ lastEdit +
,userId ='+ userId +'\''+
'}';


$ / code $ / pre
$ b $ p在数据存储中保存待办事项的步骤如下: / p>

  @ApiMethod(name =create,httpMethod = ApiMethod.HttpMethod.POST)
public Todo create(User user ,Todo todo){
logger.info(creating todo:+ todo.toString());

todo.setUserId(new Long(user.getUserId()));
todo.setLastEdit(new Date());
ofy()。save()。entity(todo).now();

返回todo;
}



@ApiMethod(name =list,httpMethod = ApiMethod.HttpMethod.GET)
public Collection< Todo> getTodos(用户用户){
logger.info(user:+ user);

列表< Todo> todos = null;
$ b $ if(user!= null){
todos = ofy()。consistency(ReadPolicy.Consistency.STRONG).load()。type(Todo.class).filter(userId ,new Long(user.getUserId()))。list();
}

logger.info(todos:+ todos);

返回todos;
}

假设我在列表中有4个待办事项,我将其全部设置为完成(完成=真)。
我在Datastore查看器中检查它们的状态:



然后,如果我在几秒钟后要求列表,我会有奇怪的一致性问题:

  2014-03-08 13:08:43.326 
fr.xebia.gae.todo.api。 TodoEndpointV2 getTodos:todos:[
Todo {id = 5639274879778816,title ='vélo',completed = false,lastEdit = Fri Mar 07 23:36:08 UTC 2014,userId ='104955400895435072612'},
Todo {id = 5676830073815040,title ='train',completed = true,lastEdit = Sat Mar 08 12:08:30 UTC 2014,userId ='104955400895435072612'},
Todo {id = 5717271485874176,title ='avion ',completed = false,lastEdit = Fri Mar 07 23:36:09 UTC 2014,userId ='104955400895435072612'},
Todo {id = 5757334940811264,title ='voiture',completed = true,lastEdit = Sat Mar 08 12:08:32 UTC 2014,userId ='104955400895435072612'}]

您可以看到,所有待办事项的完成值未设置为true,并且其lastEdit日期为甚至不更新



2分钟后的新请求:

  2014- 03-08 13:10:20.612 
fr.xebia.gae.todo.api.TodoEndpointV2 getTodos:todos:[
Todo {id = 5639274879778816,title ='vélo',completed = false,lastEdit = Fri Mar 07 23:36:08 UTC 2014,userId ='104955400895435072612'},
Todo {id = 5676830073815040,title ='train',completed = true,lastEdit = Sat Mar 08 12:08:30 UTC 2014 ,userId ='104955400895435072612'},
Todo {id = 5717271485874176,title ='avion',completed = false,lastEdit = Fri Mar 07 23:36:09 UTC 2014,userId ='104955400895435072612'},
Todo {id = 5757334940811264,title ='voiture',completed = true,lastEdit = Sat Mar 08 12:08:32 UTC 2014,userId ='104955400895435072612'}]

和17分钟后的新请求,仍然不是很好的值...

  2014-03-08 13:27:07.918 
fr.xebia.gae.todo.api.TodoEndpointV2 getTodos:todos:[Todo {id = 5639274879778816,title ='vélo ',completed = false,lastEdit = Fri Mar 07 23:36:08 UTC 2014,userId ='104955400895435072612'},
Todo {id = 5676830073815040,title ='train',completed = true,lastEdit = Sat Mar 08 12:08:30 UTC 2014,userId ='104955400895435072612'},
Todo {id = 5717271485874176,title ='avion',completed = false,lastEdit = Fri Mar 07 23:36:09 UTC 2014,userId ='104955400895435072612'},
Todo {id = 5757334940811264,title ='voiture',completed = true,lastEdit = Sat Mar 08 12:08:32 UTC 2014,userId ='104955400895435072612'}]

有人知道为什么他们与Objectify查询的内容和GAE Datastore查看器后台?
是一致性问题吗?但如果是这样的话,为什么数据存储查看器没有相同的问题?
当我查询时(如果我在运行过滤器时设置了ofy()。consistency(ReadPolicy.Consistency.STRONG)),我是否滥用了对象化?

解决方案

我没有很好地理解客体化设置,并且错过了它告诉人们应该将以下内容添加到web.xml

 < filter> 
< filter-name> ObjectifyFilter< / filter-name>
< filter-class> com.googlecode.objectify.ObjectifyFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> ObjectifyFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>


I'm coding a sample project based on TodoMVC angularjs (http://todomvc.com/) and with a backend api with Google App Engine Cloud Endpoint and I'm having some consistency result when getting the todos liste from App Engine Datastore.

Todo object are stored in the App Engine Datastore using objectify.

A Todo entity is coded as follow:

@Entity
public class Todo {

    @Id
    private Long id;
    private String title;
    private boolean completed;
    private Date lastEdit;

    @Index
    private Long userId;

    public Todo() {
    }

    public Todo(String title, boolean completed) {
        this.title = title;
        this.completed = completed;
    }

    public Todo(Long id, String title, boolean completed) {
        this.id = id;
        this.title = title;
        this.completed = completed;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isCompleted() {
        return completed;
    }

    public void setCompleted(boolean completed) {
        this.completed = completed;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Date getLastEdit() {
        return lastEdit;
    }

    public void setLastEdit(Date lastEdit) {
        this.lastEdit = lastEdit;
    }

    @Override
    public String toString() {
        return "Todo{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", completed=" + completed +
                ", lastEdit=" + lastEdit +
                ", userId='" + userId + '\'' +
                '}';
    }
}

Saving a Todo in Datastore is done as follow:

@ApiMethod(name = "create", httpMethod =  ApiMethod.HttpMethod.POST)
public Todo create(User user, Todo todo) {
    logger.info("creating todo : " + todo.toString());

    todo.setUserId(new Long(user.getUserId()));
    todo.setLastEdit(new Date());
    ofy().save().entity(todo).now();

    return todo;
}



@ApiMethod(name = "list", httpMethod =  ApiMethod.HttpMethod.GET)
public Collection<Todo> getTodos(User user) {
    logger.info("user:" + user);

    List<Todo> todos = null;

    if (user != null) {
        todos = ofy().consistency(ReadPolicy.Consistency.STRONG).load().type(Todo.class).filter("userId", new Long(user.getUserId())).list();
    }

    logger.info("todos:" + todos);

    return todos;
}

Let say I have 4 todos in my list and I set all of them to completed (completed=true).I check their state in the Datastore viewer:

Then if I request the list a few seconds after I will have the strange consistency issue :

2014-03-08 13:08:43.326
fr.xebia.gae.todo.api.TodoEndpointV2 getTodos: todos:[
Todo{id=5639274879778816, title='vélo', completed=false, lastEdit=Fri Mar 07 23:36:08 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5676830073815040, title='train', completed=true, lastEdit=Sat Mar 08 12:08:30 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5717271485874176, title='avion', completed=false, lastEdit=Fri Mar 07 23:36:09 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5757334940811264, title='voiture', completed=true, lastEdit=Sat Mar 08 12:08:32 UTC 2014, userId='104955400895435072612'}]

as you can see the completed value is not set to true for all the todos and their lastEdit date is even not updated

new request 2 minutes later :

2014-03-08 13:10:20.612
fr.xebia.gae.todo.api.TodoEndpointV2 getTodos: todos:[
Todo{id=5639274879778816, title='vélo', completed=false, lastEdit=Fri Mar 07 23:36:08 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5676830073815040, title='train', completed=true, lastEdit=Sat Mar 08 12:08:30 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5717271485874176, title='avion', completed=false, lastEdit=Fri Mar 07 23:36:09 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5757334940811264, title='voiture', completed=true, lastEdit=Sat Mar 08 12:08:32 UTC 2014, userId='104955400895435072612'}]

and new request 17 minutes later, still not the good values ...

2014-03-08 13:27:07.918
fr.xebia.gae.todo.api.TodoEndpointV2 getTodos: todos:[Todo{id=5639274879778816, title='vélo', completed=false, lastEdit=Fri Mar 07 23:36:08 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5676830073815040, title='train', completed=true, lastEdit=Sat Mar 08 12:08:30 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5717271485874176, title='avion', completed=false, lastEdit=Fri Mar 07 23:36:09 UTC 2014, userId='104955400895435072612'}, 
Todo{id=5757334940811264, title='voiture', completed=true, lastEdit=Sat Mar 08 12:08:32 UTC 2014, userId='104955400895435072612'}]

Does someone knows why their is such differences between what is queried with Objectify and with what is viewable with the GAE Datastore viewer backoffice ?Is is consistency issue ? but if so why the datastore viewer doesn't have the same issue ?Am I misusing objectify when querying (event if I set ofy().consistency(ReadPolicy.Consistency.STRONG) when running filter) ?

解决方案

I didn't read well the objectify setup and I missed the part where it tells that one should add the following to the web.xml

<filter>
     <filter-name>ObjectifyFilter</filter-name>
     <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class>
</filter>
<filter-mapping>
     <filter-name>ObjectifyFilter</filter-name>
     <url-pattern>/*</url-pattern> 
</filter-mapping>

这篇关于Objectify查询结果和Datastore查看器结果的不一致性问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:59