summaryrefslogtreecommitdiff
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
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.
-rw-r--r--src/lq/ImportQuotes.java5
-rw-r--r--src/lq/Printer.java2
-rw-r--r--src/lq/Quote.java20
-rw-r--r--src/lq/Vote.java8
4 files changed, 22 insertions, 13 deletions
diff --git a/src/lq/ImportQuotes.java b/src/lq/ImportQuotes.java
index 30ad811..d750b7b 100644
--- a/src/lq/ImportQuotes.java
+++ b/src/lq/ImportQuotes.java
@@ -148,9 +148,10 @@ public class ImportQuotes extends HttpServlet {
Date timestamp = lq.DateUtil.timestampFormat.parse(getChildWithName("time", voteRow));
String ip = getChildWithName("ip", voteRow);
- Vote vote = new Vote(rating, ip);
+ Vote vote = new Vote(quoteId, rating, ip);
vote.setTimestamp(timestamp);
- quote.getVotes().add(vote);
+ quote.setSumVotes(quote.getSumVotes() + rating);
+ quote.setNumVotes(quote.getNumVotes() + 1);
return vote;
}
diff --git a/src/lq/Printer.java b/src/lq/Printer.java
index 7677450..e5bdc0b 100644
--- a/src/lq/Printer.java
+++ b/src/lq/Printer.java
@@ -21,7 +21,7 @@ public class Printer {
String date = DateUtil.dateFormat.format(quote.getQuoteDate());
out.println("Dato: " + date + ", Score: ");
out.println("<span id=\"v" + quote.getId() + "\">");
- out.println((score==null?"-":(score+ " (fra " + quote.getVotes().size() +")")));
+ out.println((score==null?"-":(score+ " (fra " + quote.getNumVotes() +")")));
out.println("<br> Vote: <font size=\"-1\">");
for(int nv=1; nv<=5; nv++)
out.println("<a href=\"javascript:ajaxvote(" + quote.getId() + ","+nv+")\">"+nv+"</a> ");
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; }
}
diff --git a/src/lq/Vote.java b/src/lq/Vote.java
index 830b0cc..ee7c18d 100644
--- a/src/lq/Vote.java
+++ b/src/lq/Vote.java
@@ -15,6 +15,9 @@ public class Vote {
private Key key;
@Persistent
+ private Long quoteId;
+
+ @Persistent
private Long rating;
@Persistent
@@ -23,9 +26,10 @@ public class Vote {
@Persistent
private String ip;
- public Vote(Long rating, String ip) {
+ public Vote(Long quoteId, Long rating, String ip) {
this.ip = ip;
this.rating = rating;
+ this.quoteId = quoteId;
timestamp = new Date();
}
@@ -33,9 +37,11 @@ public class Vote {
public Date getTimestamp() { return timestamp; }
public String getIp() { return ip; }
public Long getRating() { return rating; }
+ public Long getQuoteId() { return quoteId; }
public void setKey(Key key) { this.key = key; }
public void setTimestamp(Date timestamp) { this.timestamp = timestamp; }
public void setIp(String ip) { this.ip = ip; }
public void setRating(Long rating) { this.rating = rating; }
+ public void setQuote(Long quoteId) { this.quoteId = quoteId; }
}