r7011 - dumbhippo/trunk/server/src/com/dumbhippo/persistence



Author: otaylor
Date: 2007-12-10 19:46:32 -0600 (Mon, 10 Dec 2007)
New Revision: 7011

Modified:
   dumbhippo/trunk/server/src/com/dumbhippo/persistence/Block.java
   dumbhippo/trunk/server/src/com/dumbhippo/persistence/UserBlockData.java
Log:
Round block timestamps to 1 second when setting them on blocks to avoid weirdness when MySQL rounds

Modified: dumbhippo/trunk/server/src/com/dumbhippo/persistence/Block.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/persistence/Block.java	2007-12-10 23:49:14 UTC (rev 7010)
+++ dumbhippo/trunk/server/src/com/dumbhippo/persistence/Block.java	2007-12-11 01:46:32 UTC (rev 7011)
@@ -78,8 +78,19 @@
 		return new Date(timestamp);
 	}
 
+	// The database only stores timestamps at second-resolution. Things become more reliable
+	// if we round here, rather than rounding when storing into the database. (If we switch
+	// to a database with high-resolution timestamps, this should be removed.)
+	private long roundTimestamp(long timestamp) {
+		// any thing < 0 is just a flag value
+		if (timestamp < 0)
+			return -1000;
+		else
+			return (timestamp / 1000) * 1000;
+	}
+	
 	public void setTimestamp(Date timestamp) {
-		this.timestamp = timestamp.getTime();
+		this.timestamp = roundTimestamp(timestamp.getTime());
 	}	
 	
 	@Transient
@@ -88,7 +99,7 @@
 	}
 	
 	public void setTimestampAsLong(long timestamp) {
-		this.timestamp = timestamp;
+		this.timestamp = roundTimestamp(timestamp);
 	}
 	
 	@Transient

Modified: dumbhippo/trunk/server/src/com/dumbhippo/persistence/UserBlockData.java
===================================================================
--- dumbhippo/trunk/server/src/com/dumbhippo/persistence/UserBlockData.java	2007-12-10 23:49:14 UTC (rev 7010)
+++ dumbhippo/trunk/server/src/com/dumbhippo/persistence/UserBlockData.java	2007-12-11 01:46:32 UTC (rev 7011)
@@ -81,6 +81,17 @@
 		this.block = block;
 	}	
 	
+	// The database only stores timestamps at second-resolution. Things become more reliable
+	// if we round here, rather than rounding when storing into the database. (If we switch
+	// to a database with high-resolution timestamps, this should be removed.)
+	private long roundTimestamp(long timestamp) {
+		// any thing < 0 is just a flag value
+		if (timestamp < 0)
+			return -1000;
+		else
+			return (timestamp / 1000) * 1000;
+	}
+	
 	@Column(nullable=true)
 	public Date getClickedTimestamp() {
 		return clickedTimestamp >= 0 ? new Date(clickedTimestamp) : null;
@@ -92,11 +103,11 @@
 	}
 	
 	public void setClickedTimestamp(Date clickedTimestamp) {
-		this.clickedTimestamp = clickedTimestamp != null ? clickedTimestamp.getTime() : -1;
+		this.clickedTimestamp = roundTimestamp(clickedTimestamp != null ? clickedTimestamp.getTime() : -1);
 	}
 	
 	public void setClickedTimestampAsLong(long clickedTimestamp) {
-		this.clickedTimestamp = clickedTimestamp;
+		this.clickedTimestamp = roundTimestamp(clickedTimestamp);
 	}
 	
 	@Transient
@@ -124,11 +135,11 @@
 	}
 	
 	public void setIgnoredTimestamp(Date ignoredTimestamp) {
-		this.ignoredTimestamp = ignoredTimestamp != null ? ignoredTimestamp.getTime() : -1;
+		this.ignoredTimestamp = roundTimestamp(ignoredTimestamp != null ? ignoredTimestamp.getTime() : -1);
 	}
 	
 	public void setIgnoredTimestampAsLong(long ignoredTimestamp) {
-		this.ignoredTimestamp = ignoredTimestamp;
+		this.ignoredTimestamp = roundTimestamp(ignoredTimestamp);
 	}	
 
 	@Column(nullable=false)
@@ -151,11 +162,11 @@
 	}
 	
 	public void setParticipatedTimestamp(Date participatedTimestamp) {
-		this.participatedTimestamp = participatedTimestamp != null ? participatedTimestamp.getTime() : -1;
+		this.participatedTimestamp = roundTimestamp(participatedTimestamp != null ? participatedTimestamp.getTime() : -1);
 	}
 	
 	public void setParticipatedTimestampAsLong(long participatedTimestamp) {
-		this.participatedTimestamp = participatedTimestamp;
+		this.participatedTimestamp = roundTimestamp(participatedTimestamp);
 	}
 	
 	@Transient
@@ -174,11 +185,11 @@
 	}
 	
 	public void setStackTimestamp(Date stackTimestamp) {
-		this.stackTimestamp = stackTimestamp.getTime();
+		this.stackTimestamp = roundTimestamp(stackTimestamp.getTime());
 	}
 	
 	public void setStackTimestampAsLong(long stackTimestamp) {
-		this.stackTimestamp = stackTimestamp;
+		this.stackTimestamp = roundTimestamp(stackTimestamp);
 	}
 
 	@Column(nullable = false)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]