This is for a small scheduling app. I need an algorithm to efficiently compare two "schedules", find differences, and update only the data rows which have been changed, as well as entries in another table having this table as a foreign key. This is a big question, so I'll say right away I'm looking for either general advice or specific solutions.
EDIT: As suggested, I have significantly shortened the question.
In one table, I associate resources with a span of time when they are used.
I also have a second table (Table B) which uses the ID from Table A as a foreign key.
The entry from Table A corresponding to Table B will have a span of time which subsumes the span of time from Table B. Not all entries in Table A will have an entry in Table B.
I'm providing an interface for users to edit the resource schedule in Table A. They basically provide a new set of data for Table A that I need to treat as a diff from the version in the DB.
If they completely remove an object from Table A that is pointed to by Table B, I want to remove the entry from Table B as well.
So, given the following 3 sets:
- The original objects from Table A (from the DB)
- The original objects from Table B (from the DB)
- The edited set of objects from Table A (from the user, so no unique IDs)
I need an algorithm that will:
- Leave rows in Table A and Table B untouched if no changes are needed for those objects.
- Add rows to Table A as needed.
- Remove rows from Table A and Table B as needed.
- Modify rows in Table A and Table B as needed.
Just sorting the objects into an arrangement where I can apply the appropriate database operations is more than adequate for a solution.
Again, please answer as specifically or generally as you like, I'm looking for advice but if someone has a complete algorithm that would just make my day. :)
EDIT: In response to lassvek, I am providing some additional detail:
Table B's items are always contained entirely within Table A items, not merely overlapping.
Importantly, Table B's items are quantized so they should fall either entirely within or entirely outside. If this doesn't happen, then I have a data integrity error that I'll have to handle separately.
For example (to use a shorthand):
Table A ID Resource Start End 01 Resource A 10/6 7:00AM 10/6 11:00AM 02 Resource A 10/6 1:00PM 10/6 3:00PM Table B ID Table_A_ID Start End 01 02 10/6 1:00PM 10/6 2:00PM
So I want the following behaviours:
- If I remove ID 02 from table A, or shorten it to 2:00PM - 3:00PM, I should remove ID 01 from Table B.
- If I extend Table A ID 01 to where it ends at 1:00PM, these two entries should be merged together into one row, and Table B ID 01 should now point to table A ID 01.
- If I remove 8:00AM-10:00AM from Table A ID 01, that entry should be split into two entries: One for 7:00AM-8:00AM, and a new entry (ID 03) for 10:00AM-11:00AM.