summaryrefslogtreecommitdiff
path: root/src/lq/Quote.java
diff options
context:
space:
mode:
authorJohn H. Anthony <johnhant@gmail.com>2010-09-04 07:18:07 +0200
committerJohn H. Anthony <johnhant@gmail.com>2010-09-04 07:21:03 +0200
commitb504b5fe05410bc259cd4349d1b51dc355eb0d90 (patch)
tree6bb722c781c4c244234a0b36fcb28f54c0ab417b /src/lq/Quote.java
parentdaa5375585d276e53aadcff670a4eba3ecf2279e (diff)
store sum and number of votes in each quote
This seems to improve the response time. We don't store a list of votes for each quote, which mean we don't have to retrieve every vote when we get the list of quotes. The only time we need to access the vote table is when we add a new vote.
Diffstat (limited to 'src/lq/Quote.java')
-rw-r--r--src/lq/Quote.java20
1 files changed, 11 insertions, 9 deletions
diff --git a/src/lq/Quote.java b/src/lq/Quote.java
index 1d62a25..f4dfde0 100644
--- a/src/lq/Quote.java
+++ b/src/lq/Quote.java
@@ -37,24 +37,23 @@ public class Quote {
private String ip;
@Persistent
- private List<Vote> votes;
+ private double sumVotes;
+
+ @Persistent
+ private int numVotes;
public Quote(Date quoteDate, String author, String content, String ip) {
this.quoteDate = quoteDate;
this.author = author;
this.content = new Text(content);
this.ip = ip;
- this.votes = new ArrayList<Vote>();
-
+ this.sumVotes = 0.0;
+ this.numVotes = 0;
this.timestamp = new Date();
}
public Double getScore() {
- Double score = 0.0;
- for (Vote vote : getVotes()) {
- score = score + vote.getRating() / getVotes().size();
- }
- return score;
+ return (getNumVotes() > 0.0 ? getSumVotes()/getNumVotes() : 0.0);
}
public Long getId() { return id; }
@@ -64,7 +63,8 @@ public class Quote {
public String getAuthor() { return author; }
public String getContent() { return content.getValue(); }
public String getIp() { return ip; }
- public List<Vote> getVotes() { return votes; }
+ public Double getSumVotes() { return sumVotes; }
+ public int getNumVotes() { return numVotes; }
public void setId(Long id) { this.id = id; }
public void setTimestamp(Date timestamp) { this.timestamp = timestamp; }
@@ -73,4 +73,6 @@ public class Quote {
public void setAuthor(String author) { this.author = author; }
public void setContent(String content) { this.content = new Text(content); }
public void setIp(String ip) { this.ip = ip; }
+ public void setSumVotes(double sumVotes) { this.sumVotes = sumVotes; }
+ public void setNumVotes(int numVotes) { this.numVotes = numVotes; }
}