From b504b5fe05410bc259cd4349d1b51dc355eb0d90 Mon Sep 17 00:00:00 2001 From: "John H. Anthony" Date: Sat, 4 Sep 2010 07:18:07 +0200 Subject: 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. --- src/lq/ImportQuotes.java | 5 +++-- src/lq/Printer.java | 2 +- src/lq/Quote.java | 20 +++++++++++--------- src/lq/Vote.java | 8 +++++++- 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(""); - out.println((score==null?"-":(score+ " (fra " + quote.getVotes().size() +")"))); + out.println((score==null?"-":(score+ " (fra " + quote.getNumVotes() +")"))); out.println("
Vote: "); for(int nv=1; nv<=5; nv++) out.println(""+nv+" "); 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 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(); - + 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 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 @@ -14,6 +14,9 @@ public class Vote { @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) private Key key; + @Persistent + private Long quoteId; + @Persistent private Long rating; @@ -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; } } -- cgit v1.2.3