Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Consider train timetable data, where each service (or "run") has a data structure as such:

public class TimeTable
{
    public int Id {get;set;}
    public List<Run> Runs {get;set;}
}

public class Run 
{
    public List<Stop> Stops {get;set;}
    public int RunId {get;set;}
}

public class Stop 
{
    public int StationId {get;set;}
    public TimeSpan? StopTime {get;set;}
    public bool IsStop {get;set;}
}

We have a list of runs that operate against a particular line (the TimeTable class).

Further, whilst we have a set collection of stations that are on a line, not all runs stop at all stations (that is, IsStop would be false, and StopTime would be null).

Now, imagine that we have received the initial timetable, processed it, and loaded it into the above data structure. Once the initial load is complete, it is persisted into a database - the data structure is used only to load the timetable from its source and to persist it to the database.

We are now receiving an updated timetable.

The updated timetable may or may not have any changes to it - we don't know and are not told whether any changes are present.

What I would like to do is perform a compare for each run in an efficient manner. I don't want to simply replace each run. Instead, I want to have a background task that runs periodically that downloads the updated timetable dataset, and then compares it to the current timetable. If differences are found, some action (not relevant to the question) will take place.

I was initially thinking of some sort of checksum process, where I could, for example, load both runs (that is, the one from the new timetable received and the one that has been persisted to the database) into the data structure and then add up all the hour components of the StopTime, and all the minute components of the StopTime and compare the results (i.e. both the sum of Hours and sum of Minutes would be the same, and differences introduced if a stop time is changed, a stop deleted or a new stop added).

Would that be a valid way to check for differences, or is there a better way to approach this problem? I can see a problem that, for example, one stop is changed to be 2 minutes earlier, and another changed to be 2 minutes later would have a net zero change.

Or am I over thinking this, and would it just be simpler to brute check all stops to ensure that

  1. The updated run stops at the same stations; and
  2. Each stop is at the same time
share|improve this question
    
are you having requirement to track changes between timetable updates? –  Yusubov Sep 18 '12 at 2:12
    
No - just wanting to ensure that, post the update processing, that the latest is what we have. –  Brendan Green Sep 18 '12 at 3:12
    
How do you receive the initial timetable? Can't you save that to a special table in the database, so that you can compare with it later? –  svick Sep 18 '12 at 5:09

1 Answer 1

up vote 1 down vote accepted

am I over thinking this, and would it just be simpler to brute check all stops...

I think you are over thinking this. A check would run very quickly anyway. Even if there is a better way, you will spend so much time for a trivial benefit for the end user. This is especially true if the compare process runs on batch cycle.

You could also write the information to a sequential file (you will need a new structure) then create a hashed value based on all values, but this is kind of complex and you have to choose a hashing algorithm that generates unique keys.

Alternatively, you could use the same linear structure and run a compare utility to flag the difference(s) (if any). If no difference(s) found, you don't load the new schedule, otherwise, perform the update using the deltas.

I don't think there would be a simple way to compare the 2 sets without brute force.

Always try to Keep it simple ;)

share|improve this answer
    
I would still suggest doing an md5 of the whole table and then an md5 of the new table to check if it's even worth brute forcing. Brute forcing is as good a way of doing this as you'll get simply. If this is highly performance critical however he may not be over thinking it, if it's an perf-critical section it warrants these questions. –  Jimmy Hoffa Sep 18 '12 at 4:02
    
Thanks for the comment - I'll have a look into that and see what the results are. –  Brendan Green Sep 18 '12 at 5:16
    
@JimmyHoffa, thanks for your comment. You have a point. My assumption is that with such changes, the change can be reflected during the batch window and is not required to be applied on a button click. –  Emmad Kareem Sep 18 '12 at 5:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.