我现在有个问题我一直在纠结。我试图使用GAE、云端点和java在我的应用程序中实现一个新闻提要功能。共同的概念是追随者和叶利维,其中一个行动可以看到他的追随者。一个新的追随者也应该看到他的过去的行动,而不仅仅是从他开始跟踪的时间。
我用以下组件做了几次尝试。每一次尝试都很成功,但却缺少一些东西:
所以我请求堆叠溢出社区的建议。关于这件事,你能告诉我吗?如果前瞻性搜索是正确的选择,那么您能提供一些使用云端点的清晰示例java代码吗?
编辑:只是为了强调这里的主要设计需求--新闻提要功能需要能够使用游标获取排序的折叠操作(以避免查询整个批)。
发布于 2013-12-16 08:38:54
使用每个跟随者的拉集模型:定期(或按需)查询所有的折叠动作一次,然后将它们缓存在一个专用的每个跟随者实体中。记住上次查询的时间,所以下一次您只需要从那个点开始查询(假设操作不能添加/更改到过去的时间)。
这将为您提供以下特性(和限制):
get
加载到内存中。这应该是一个很大的成本和节省时间。限制:
发布于 2013-12-15 03:35:19
我希望我正确地理解了这个问题--你想要在你的应用程序中实现一个新闻输入,并允许用户相互跟踪。新的追随者需要能够看到用户的行动。我确信解决这个问题的方法有很多种,但是我将尝试通过提供一个使用JAVA访问数据存储的解决方案来帮助您解决这个问题。
我将首先按照以下方式设计JDO中的实体关系:
1 User to many actions.
1 User to many followers (User).
1 User to many following (User).
下面是简单的JDO类:
用户类别:
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class User {
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String userId; // Google unique user ID, could also store user email.
@Persistent
private Set<Key> actions;
@Persistent
private Set<Key> followers;
@Persistent
private List<Key> following;
public User(Key key, String userId) {
this.key = key;
this.userId = userId;
this.actions = new HashSet<Key>();
this.followers = new HashSet<Key>();
this.following = new HashSet<Key>();
}
public Key getKey() {
return this.key;
}
public void addAction(Key actionKey) {
this.actions.add(actionKey);
}
public void addActions(Set<Key> actionKeys) {
this.actions.addAll(actionKeys);
}
public Set<Key> getActions() {
return this.actions;
}
public void addFollower(Key followerKey) {
this.followers.add(followerKey);
}
public void addFollowers(Set<Key> followerKeys) {
this.followers.addAll(followerKeys);
}
public Set<Key> getFollowers() {
return this.followers;
}
public void addFollowing(Key followingKey) {
this.following.add(followingKey);
}
public void addAllFollowing(Set<Key> followingKeys) {
this.following.addAll(followingKeys);
}
public Set<Key> getFollowing() {
return this.following;
}
}
行动类别:
@PersistenceCapable(identityType=IdentityType.APPLICATION)
public class Action {
@PrimaryKey
@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
Date date;
@Persistent
private String title;
public Action(Key key, String title) {
this.key = key;
this.title = title;
this.date = new Date(); // date of creation (now).
}
public Key getKey() {
return this.key;
}
public void setTitle(String title) {
this.title = title;
}
public String getTitle() {
return this.title;
}
}
Action使用Date属性,您可以参考文档获取数据存储中适用的数据类型。创建动作时,对Date对象进行分配和初始化,使其表示分配的时间,测量到最近的毫秒。。
在上面的示例中,我通过它们的键链接了实体,您可以通过它们的类链接它们,如下所示:
List<Action> actions;
在我的例子中,这种关系是一种没有人拥有的一对多的关系,也许它应该是一对多的。更多的信息,这里,让您看一看,也许可以决定哪一个最适合您的解决方案。
一旦定义了关系,您就可以围绕JDO模型类创建端点类了。这将创建基本的api方法。您可能希望更改端点类方法以满足您的需要,例如,更改创建操作的方式。一个基本的示例是从操作标题创建密钥,如下所示(ActionEnpoint.java):
...
@ApiMethod(name = "insertAction")
public Action insertAction( @Named("title") String title ) {
PersistenceManager pm = getPersistenceManager();
Key key = KeyFactory.createKey(Action.class.getSimpleName(), title);
Action action = null;
try {
action = new Action(key, title);
pm.makePersistent(action);
} finally {
pm.close();
}
return action;
}
...
如果愿意,可以向UserEndpoint类添加一个方法,以查询数据存储,并使用datastore查询对象返回属于该用户的所有操作和每个日期。
您需要向UserEndpoint类添加一个方法,该方法允许您向该用户添加一个操作,下面是一个简单的示例:
...
@ApiMethod(name = "addActionToUser")
public Achiever addActionToUser(
@Named("userId") String userId,
@Named("actionTitle") String actionTitle) {
PersistenceManager pm = getPersistenceManager();
Key userKey = KeyFactory.createKey(User.class.getSimpleName(), userId);
Key actionKey = KeyFactory.createKey(Action.class.getSimpleName(), actionTitle);
User user = null;
try {
user = (User) pm.getObjectById(User.class, userKey);
user.addAction(actionKey);
pm.makePersistent(user);
} catch (Exception e) {
}
return user;
}
...
一旦完成上述所有操作,您就可以通过调用getUser类中的UserEndpoint方法轻松地获得每个用户的操作列表,该方法返回一个user对象。然后可以调用ReturnedUserObject.getActions()。一个新的追随者现在可以通过调用api方法来获取"followees“对象并获得他/她的操作来查看所有的"followees”操作。然后,您可以按日期对操作进行排序,也可以根据您的设想进行排序。
我希望我正确地理解了你的问题,我不确定你提到的第一个部分,但似乎你把你的关系搞混了。我希望这个解决方案至少指向正确的方向:)。
如果你需要任何额外的帮助或澄清,或者我的回答完全偏离了你想要的,那么请告诉我。
你好,Miki
https://stackoverflow.com/questions/20557651
复制相似问题