gpt4 book ai didi

google-app-engine - Objectify 查询结果和 Datastore 查看器结果不一致的问题?

转载 作者:太空宇宙 更新时间:2023-11-03 15:25:05 25 4
gpt4 key购买 nike

我正在编写一个基于 TodoMVC angularjs ( http://todomvc.com/ ) 的示例项目,并使用带有 Google App Engine Cloud Endpoint 的后端 api,当我从 App Engine Datastore 获取待办事项列表时,我得到了一些一致性结果。

Todo 对象使用 objectify 存储在 App Engine 数据存储中。

Todo 实体编码如下:

@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 + '\'' +
'}';
}
}

在 Datastore 中保存 Todo 的操作如下:

@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;
}

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

datastore viewer showing that the completed value is set to true for all todo items

然后,如果我在几秒钟后请求列表,我将遇到奇怪的一致性问题:

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,它们的上次编辑日期甚至未更新

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) 事件)?

最佳答案

我没有很好地阅读 objectify 设置,我错过了告诉应该将以下内容添加到 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>

关于google-app-engine - Objectify 查询结果和 Datastore 查看器结果不一致的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22269411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com