Skip to content

Instantly share code, notes, and snippets.

@dfilppi
Created February 23, 2012 00:05
Show Gist options
  • Save dfilppi/1888549 to your computer and use it in GitHub Desktop.
Save dfilppi/1888549 to your computer and use it in GitHub Desktop.
TimeRecord class
@SpaceClass
public class TimeRecord implements Serializable{
private static Logger log=Logger.
getLogger(TimeRecord.class.getName());
private String location=null;
private List<TimeEntry> timelog=null;
private final int logsize=10; //arbitrary
//Only to be used for constructing templates
public TimeRecord(){}
public TimeRecord(String loc){
this.location=loc;
this.setTimelog(new LinkedList<TimeEntry>());
}
// Always want these in same partition
@SpaceRouting
public int getRoutingId(){
return 1;
}
public void setRoutingId(int id){}
public Long getTime() {
if(getTimelog()==null)
setTimelog(new LinkedList<TimeEntry>());
if(getTimelog().size()==0)return null;
return getTimelog().get(0).getTime();
}
public void setTime(TimeStamp ts) {
if(getTimelog()==null)
setTimelog(new LinkedList<TimeEntry>());
if(getTimelog().size()>=logsize){
getTimelog().remove(9);
}
getTimelog().add(0,new TimeEntry(ts));
}
/**
* Computes average latency from the timelog.
* @return
*/
public double getAveLatency(){
if(getTimelog()==null)
setTimelog(new LinkedList<TimeEntry>());
if(getTimelog().size()==0)return 0;
long totaltime=0;
for(TimeEntry e:getTimelog()){
totaltime+=e.getLatency();
}
return (double)(totaltime/((getTimelog().size())));
}
// Singleton per location
@SpaceId
public String getLocation() {
return location;
}
/*
Uninteresting boilerplate getters and setters and such
.....
*/
}
class TimeEntry implements Serializable{
private long time;
private long latency;
public TimeEntry(TimeStamp ts){
time=ts.getTime();
latency=System.currentTimeMillis()-ts.getTime();
}
public TimeEntry(long time, long latency) {
this.time = time;
this.latency = latency;
}
/* Getters and setters
....
*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment