Handling dynamic Networks
Sometimes it happens that a Network could be subject to rather frequent changes: some new Links require to be added, obsolete Links require to be removed, other Links may assume a different Cost, one-ways could be reversed, the discipline of pedestrian areas could be modified and so on.
A VirtualRouting Table is always based on a companion Binary Data Table, that is intrinsically static, and consequently you are required to re-create both them from time to time in order to support all recent changes affecting the underlaying Network.
The optimal frequency for cyclically refreshing the Routing Tables strictly depends on specific requirements, but the two overall approaches are commonly adopted:
- low frequency refresh: best fit for slowly evolving Networks.
In this case re-creating the Network Tables once a month / week / day could be reasonably enough.
Recreating the Tables from scratch usually requires several seconds (or even less, depending on the number of Links).
The refresh activities could be opportunely planned at low traffic hours (e.g. during the night), and CreateRouting() could be usefully called by enabling the overwrite option.
- medium-high frequency refresh: best fit for quickly evolving Networks.
Re-creating the Network Tables once per hour (or even more frequently) could be strictly required, and frequent out of service periods while waiting for the refresh process to complete could easily be unacceptable.
In this case you could usefully adopt a multi-threaded strategy:
- thread #1 (the reader): this first thread is intended to service any incoming Routing request. It will be always active, and will target a well known VirtualRouting Table (e.g. my_routing based on my_routing_data).
- thread #2 (the writer): this second thread is just intended to re-create both Network Tables at predefined intervals, and it will sleep between an interval and the other.
When this thread awakens will re-create both Network Tables by using different names, and will overwrite the standard ones just at the very end of the process (activating a semaphore during this short-timed last step is highly recommended).
Something like this pseudo-code exemplifies:
SELECT CreateRouting('new_my_routing_data', 'new_my_routing', ...);
--> start the semaphore so to lock the other thread
BEGIN;
DROP TABLE my_routing;
DROP TABLE my_routing_data;
SELECT CloneTable('MAIN', 'new_my_routing_data', 'my_routing_data', 0);
CREATE VIRTUAL TABLE my_routing USING VirtualRouting('my_routing_data');
DROP TABLE new_my_routing;
DROP TABLE new_my_routing_data;
COMMIT;
--> remove the semaphore
Note: strictly respecting the above sequence of SQL operations is absolutely required.
|