aboutsummaryrefslogblamecommitdiffstats
path: root/lib/tv/src/tv_etsread.erl
blob: b3e38f9d453242bb30895dd68997fdf625d18707 (plain) (tree)
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770


























                                                                              


                                                       



































































































































































































































































































































































































































































































































































































































































































































































                                                                                                            
%%
%% %CopyrightBegin%
%% 
%% Copyright Ericsson AB 1997-2009. All Rights Reserved.
%% 
%% The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved online at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% %CopyrightEnd%
%%%*********************************************************************
%%%
%%%   Description:      Module containing the interface towards ETS tables,
%%%                     i.e., handling the polling and thereafter sending the 
%%%                     result to the database part of the table tool.
%%%
%%%*********************************************************************


-module(tv_etsread).
-compile([{nowarn_deprecated_function,{gs,destroy,1}},
          {nowarn_deprecated_function,{gs,start,0}},
          {nowarn_deprecated_function,{gs,window,3}}]).



-export([etsread/2]).



-include("tv_int_def.hrl").
-include("tv_int_msg.hrl").






%%%*********************************************************************
%%% EXTERNAL FUNCTIONS
%%%*********************************************************************




etsread(MasterPid, ErrorMsgMode) ->
    process_flag(trap_exit, true),
    put(error_msg_mode, ErrorMsgMode),
    blocked(MasterPid).





%%%*********************************************************************
%%% INTERNAL FUNCTIONS
%%%*********************************************************************



blocked(MasterPid) ->
    receive
        Msg ->
            case Msg of 

                #etsread_deblock{} ->
                    deblock(Msg, MasterPid);

		{'EXIT', Pid, Reason} ->
	            exit_signals({Pid, Reason}, MasterPid),
		    blocked(MasterPid);

		{error_msg_mode, Mode} ->
		    put(error_msg_mode, Mode),
		    blocked(MasterPid);

                _Other ->
		    %% io:format("Received signal ~p~n", [_Other]),
                    blocked(MasterPid)
            end
    end.
    






deblock(Msg, MasterPid) ->
    #etsread_deblock{dbs_pid       = DbsPid,
		     table_type    = KindOfTable,
		     node          = Node,
		     local_node    = LocalNode,
		     table_id      = TableId,
		     poll_interval = PollInt}  = Msg,
    PollInterval = case PollInt of 
		       infinity ->
			   PollInt;
		       _Other ->
			   PollInt * 1000
		   end,
       %% Get table info!
    case catch get_table_info(Node, LocalNode, TableId, KindOfTable) of
	nodedown ->
	    MasterPid ! #pc_nodedown{sender            = self(),
				     automatic_polling = false},
	    blocked(MasterPid);
	no_table ->
	    MasterPid ! #pc_dead_table{sender = self(),
				       automatic_polling = false},
	    blocked(MasterPid);
	mnesia_not_started ->
	    MasterPid ! #pc_dead_table{sender = self(),
				       automatic_polling = false},
	    blocked(MasterPid);
	{unexpected_error,_Reason} ->
	    MasterPid ! #pc_dead_table{sender = self(),
				       automatic_polling = false},
	    blocked(MasterPid);
	{Type, Pos, Protection} ->
	    MasterPid ! #etsread_deblock_cfm{sender     = self(),
					     type       = Type,
					     keypos     = Pos,
					     protection = Protection
					    },
	    
	    timer:sleep(500),
	    case catch read_table(Node, LocalNode, TableId, KindOfTable, DbsPid) of
		nodedown ->
		    MasterPid ! #pc_nodedown{sender            = self(),
					     automatic_polling = false},
		    blocked(MasterPid);
		no_table ->
		    MasterPid ! #pc_dead_table{sender = self(),
					       automatic_polling = false},
		    blocked(MasterPid);
		mnesia_not_started ->
		    MasterPid ! #pc_dead_table{sender = self(),
					       automatic_polling = false},
		    blocked(MasterPid);
		{unexpected_error,_Reason} ->
		    MasterPid ! #pc_dead_table{sender = self(),
					       automatic_polling = false},
		    blocked(MasterPid);
		_ElapsedTime ->
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode, TableId, 
				   KindOfTable, PollInterval)
	    end
    end.


		    
	    
					     

get_table_info(Node, LocalNode, TableId, KindOfTable) ->
    case KindOfTable of
	ets ->
	       % Check whether table is 'bag' or 'set' type.
	    Type       = tv_ets_rpc:info(Node, LocalNode, TableId, type),
	       % Get position for the key.
	    Pos        = tv_ets_rpc:info(Node, LocalNode, TableId, keypos),
	    Protection = tv_ets_rpc:info(Node, LocalNode, TableId, protection),
	    {Type, Pos, Protection};
	mnesia ->
	    Type = tv_mnesia_rpc:table_info(Node, LocalNode, TableId, type),
	    Pos  = 2,
	       %% All Mnesia tables are regarded as being public!
	    {Type, Pos, public}
    end.
	    



    

deblocked_loop(MasterPid,DbsPid,Node,LocalNode,TableId,KindOfTable,PollInterval) ->
    receive
	Msg ->

	    case Msg of 
		
		#etsread_poll_table{} ->
		    case catch read_table(Node, LocalNode, TableId, KindOfTable, DbsPid) of
			   %% No automatic polling here!
			nodedown ->
			    MasterPid ! #pc_nodedown{sender            = self(),
						     automatic_polling = false};
			no_table ->
			    MasterPid ! #pc_dead_table{sender = self(),
						       automatic_polling = false};
			mnesia_not_started ->
			    MasterPid ! #pc_dead_table{sender = self(),
						       automatic_polling = false};
			{unexpected_error,_Reason} ->
			    MasterPid ! #pc_dead_table{sender = self(),
						       automatic_polling = false};
			_ElapsedTime ->
			    done
		    end,
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode,
				   TableId, KindOfTable, PollInterval);
		    

		#etsread_set_poll_interval{interval = PollInt} ->
		    NewPollInterval = case PollInt of 
					  infinity ->
					      PollInt;
					  _Other ->
					      PollInt * 1000
				      end,
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode,
				   TableId, KindOfTable, NewPollInterval);


		#etsread_deblock{} ->
		    deblock(Msg, MasterPid);
		

		#etsread_update_object{key_no=KeyNo, object=Obj, old_object=OldObj} ->
		    update_object(KindOfTable, Node, LocalNode, TableId, DbsPid, 
				  KeyNo, Obj, OldObj, MasterPid, PollInterval),
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode, TableId, KindOfTable, 
				   PollInterval);


		#etsread_new_object{object=Obj} ->
		    new_object(KindOfTable, Node, LocalNode, TableId, DbsPid, 
				  Obj, MasterPid, PollInterval),
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode, TableId, KindOfTable, 
				   PollInterval);


		#etsread_delete_object{object=Obj} ->
		    delete_object(KindOfTable, Node, LocalNode, TableId, DbsPid, 
				  Obj, MasterPid, PollInterval),
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode, TableId, KindOfTable, 
				   PollInterval);


		#ip_dead_table{} ->
		    AutoPoll = case PollInterval of
				   infinity ->
				       false;
				   _Other ->
				       true
			       end,
		    MasterPid ! #pc_dead_table{sender            = self(),
					       automatic_polling = AutoPoll},
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode, TableId, 
				   KindOfTable, infinity);
		    

		#etsread_nodedown{} ->
		    AutoPoll = case PollInterval of
				   infinity ->
				       false;
				   _Other ->
				       true
			       end,
		    MasterPid ! #pc_nodedown{sender            = self(),
					     automatic_polling = AutoPoll},
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode, TableId, 
				   KindOfTable, infinity);


		{error_msg_mode, Mode} ->
		    put(error_msg_mode, Mode),
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode, TableId, KindOfTable, 
				   PollInterval);
		

		{'EXIT', Pid, Reason} ->
	            exit_signals({Pid, Reason}, MasterPid),
		    deblocked_loop(MasterPid, DbsPid, Node, LocalNode,
				   TableId, KindOfTable, PollInterval)
	    end
    
    after PollInterval ->
	       %% Automatic polling must be on, otherwise these 
	       %% lines would never be executed!
	    NewPollInterval =
		case catch read_table(Node,LocalNode,TableId,KindOfTable,DbsPid) of
		    nodedown ->
			MasterPid ! #pc_nodedown{sender            = self(),
						 automatic_polling = true},
			infinity;
		    no_table ->
			MasterPid ! #pc_dead_table{sender = self(),
						   automatic_polling = true},
			infinity;
		    mnesia_not_started ->
			MasterPid ! #pc_dead_table{sender = self(),
						   automatic_polling = true},
			infinity;
		    {unexpected_error,_Reason} ->
			MasterPid ! #pc_dead_table{sender = self(),
						   automatic_polling = true},
			infinity;
		    ElapsedMilliseconds ->
			if
			    (ElapsedMilliseconds * 1000) >= PollInterval ->
				infinity;
			    true ->
				PollInterval
			end
		end,
	    deblocked_loop(MasterPid, DbsPid, Node, LocalNode,
			   TableId, KindOfTable, NewPollInterval)
    end.





exit_signals(ExitInfo, MasterPid) ->
    case ExitInfo of
        {MasterPid, _Reason} ->
            exit(normal);
        _Other ->
	    done
    end.




update_object(KindOfTable, Node, LocalNode, TableId, DbsPid, KeyNo, Obj, OldObj, MasterPid, PollInterval) ->
    AutoPoll = 
	case PollInterval of
	    infinity ->
		false;
	    _Other ->
		true
	end,
    case check_record_format(KindOfTable, Node, LocalNode, TableId, Obj) of
	bad_format ->
	    DbsPid ! #etsread_update_object_cfm{sender   = self(),
						success  = false};
	ok ->
	       %% Check that we are allowed to edit the table!
	    case catch update_object2(KindOfTable, Node, LocalNode, TableId, DbsPid, KeyNo, 
				      Obj, OldObj) of
		
		nodedown ->
		    DbsPid ! #etsread_update_object_cfm{sender  = self(),
							success = false},
		    MasterPid ! #pc_nodedown{sender             = self(),
					     automatic_polling  = AutoPoll};
		
		no_table ->
		    DbsPid ! #etsread_update_object_cfm{sender   = self(),
							success  = false},
		    MasterPid ! #pc_dead_table{sender = self(),
					       automatic_polling = AutoPoll};
		
		mnesia_not_started ->
		    DbsPid ! #etsread_update_object_cfm{sender   = self(),
							success  = false},
		    MasterPid ! #pc_dead_table{sender            = self(),
					       automatic_polling = AutoPoll};
		
		
		{unexpected_error,_Reason} ->
		    DbsPid ! #etsread_update_object_cfm{sender   = self(),
							success  = false},
		    MasterPid ! #pc_dead_table{sender            = self(),
					       automatic_polling = AutoPoll};
		
		ok ->
		    DbsPid ! #etsread_update_object_cfm{sender  = self(),
							success = true}
	    end
    end.





update_object2(ets, Node, LocalNode, Tab, _DbsPid, KeyNo, Obj, OldObj) ->
       %% We shall update a specific object! If the table is a 'set' table,
       %% it is just to insert the altered object. However, if the table
       %% is a 'bag', or a 'duplicate_bag', we first have to remove the
       %% old object, and then insert the altered one. 
       %% But, we aren't finished with that... we also want to preserve
       %% the time order, meaning we have to delete *ALL* objects having the
       %% very same key, and then insert them again! (Actually we would have 
       %% to do this anyhow, due to limitations in the interface functions,
       %% but this remark has to be noted!)
    OldKey = element(KeyNo, OldObj),
    InsertList = 
	case tv_ets_rpc:info(Node, LocalNode, Tab, type) of
	    set ->
		   %% Have to remove old object, because the key may be what's changed.
		tv_ets_rpc:delete(Node, LocalNode, Tab, OldKey),
		[Obj];
	    ordered_set ->
		   %% Have to remove old object, because the key may be what's changed.
		tv_ets_rpc:delete(Node, LocalNode, Tab, OldKey),
		[Obj];
	    _Other ->  %% 'bag' or 'duplicate_bag'
		OldList = tv_ets_rpc:lookup(Node, LocalNode, Tab, OldKey),
		tv_ets_rpc:delete(Node, LocalNode, Tab, OldKey),
		   %% Have to beware of duplicate_bag tables,
		   %% i.e., the same object may occur more than
		   %% once, but we only want to replace it once!
		{_Replaced, TmpList} =
		    lists:foldl(
		      fun(Data, {Replaced,Acc}) when Data =/= OldObj ->
			      {Replaced, [Data | Acc]};
			 (_Data, {Replaced,Acc}) when not Replaced ->
			      {true, [Obj | Acc]};
			 (Data, {Replaced,Acc}) ->
			      {Replaced, [Data | Acc]}
		      end,
		      {false, []},
		      OldList),
		lists:reverse(TmpList)
	end,
    lists:foreach(fun(H) ->
			  tv_ets_rpc:insert(Node, LocalNode, Tab, H)
		  end,
		  InsertList),
    ok;
update_object2(mnesia, Node, LocalNode, Tab, _DbsPid, KeyNo, Obj, OldObj) ->
    OldKey = element(KeyNo, OldObj),
    InsertList = 
	case tv_mnesia_rpc:table_info(Node, LocalNode, Tab, type) of
	    set ->
		tv_mnesia_rpc:transaction(
		  Node,
		  LocalNode,
		  fun() ->
			  mnesia:delete(Tab,OldKey,write)
		  end),
		[Obj];
	    ordered_set ->
		tv_mnesia_rpc:transaction(
		  Node,
		  LocalNode,
		  fun() ->
			  mnesia:delete(Tab,OldKey,write)
		  end),
		[Obj];
	    _Other ->  %% 'bag' or 'duplicate_bag'
		{atomic, OldList} = 
		    tv_mnesia_rpc:transaction(
		      Node, 
		      LocalNode,
		      fun() -> 
			      mnesia:read(Tab,OldKey,read)
		      end),
		   %% We can't use mnesia:delete_object here, because
		   %% time order wouldn't be preserved then!!!
		tv_mnesia_rpc:transaction(
		  Node,
		  LocalNode,
		  fun() ->
			  mnesia:delete(Tab,OldKey,write)
		  end),
		ChangeFun =
		    fun(H) when H =:= OldObj ->
			    Obj;
		       (H) ->
			    H
		    end,
		[ChangeFun(X) || X <- OldList]
	end,
    lists:foreach(fun(H) ->
			  tv_mnesia_rpc:transaction(
			    Node, 
			    LocalNode,
			    fun() ->
				       %% This mnesia call shall not be distributed,
				       %% since the transaction sees to that it is
				       %% executed on the right node!!!
				    mnesia:write(Tab,H,write)
			    end)
		  end,
		  InsertList),
    ok.

		  




delete_object(KindOfTable, Node, LocalNode, TableId, DbsPid, Obj, MasterPid, PollInterval) ->
    AutoPoll = 
	case PollInterval of
	    infinity ->
		false;
	    _Other ->
		true
	end,
    case catch delete_object2(KindOfTable, Node, LocalNode, TableId, DbsPid, Obj) of

	nodedown ->
	    DbsPid ! #etsread_delete_object_cfm{sender  = self(),
						success = false},
	    MasterPid ! #pc_nodedown{sender             = self(),
				     automatic_polling  = AutoPoll};

	no_table ->
	    DbsPid ! #etsread_delete_object_cfm{sender   = self(),
						success  = false},
	    MasterPid ! #pc_dead_table{sender = self(),
				       automatic_polling = AutoPoll};
	
	mnesia_not_started ->
	    DbsPid ! #etsread_delete_object_cfm{sender   = self(),
						success  = false},
	    MasterPid ! #pc_dead_table{sender            = self(),
				       automatic_polling = AutoPoll};

	{unexpected_error,_Reason} ->
	    DbsPid ! #etsread_delete_object_cfm{sender   = self(),
						success  = false},
	    MasterPid ! #pc_dead_table{sender            = self(),
				       automatic_polling = AutoPoll};

	ok ->
	    DbsPid ! #etsread_delete_object_cfm{sender  = self(),
						success = true}
    end.




delete_object2(ets, Node, LocalNode, Tab, _DbsPid, Obj) ->
    KeyNo = tv_ets_rpc:info(Node, LocalNode, Tab, keypos),
    Key   = element(KeyNo, Obj),
    InsertList = 
	case tv_ets_rpc:info(Node, LocalNode, Tab, type) of
	    set ->
		   %% Have to remove old object, because the key may be what's changed.
		tv_ets_rpc:delete(Node, LocalNode, Tab, Key),
		[];
	    ordered_set ->
		   %% Have to remove old object, because the key may be what's changed.
		tv_ets_rpc:delete(Node, LocalNode, Tab, Key),
		[];
	    _Other ->  %% 'bag' or 'duplicate_bag'
		OldList = tv_ets_rpc:lookup(Node, LocalNode, Tab, Key),
		tv_ets_rpc:delete(Node, LocalNode, Tab, Key),
		OldList -- [Obj]
	end,

    lists:foreach(fun(H) ->
			  tv_ets_rpc:insert(Node, LocalNode, Tab, H)
		  end,
		  InsertList),
    ok;
delete_object2(mnesia, Node, LocalNode, Tab, _DbsPid, Obj) ->
    tv_mnesia_rpc:transaction(
      Node, 
      LocalNode,
      fun() ->
	         %% This mnesia call shall not be distributed,
	         %% since the transaction sees to that it is
	         %% executed on the right node!!!
	      mnesia:delete_object(Tab,Obj,write)
      end),
    ok.





new_object(KindOfTable, Node, LocalNode, TableId, DbsPid, Obj, MasterPid, PollInterval) ->
    AutoPoll = 
	case PollInterval of
	    infinity ->
		false;
	    _Other ->
		true
	end,
    case check_record_format(KindOfTable, Node, LocalNode, TableId, Obj) of
	bad_format ->
	    DbsPid ! #etsread_new_object_cfm{sender   = self(),
						success  = false};
	ok ->
	       %% Check that we are allowed to edit the table!
	    case catch new_object2(KindOfTable, Node, LocalNode, TableId, DbsPid, Obj) of
		
		nodedown ->
		    DbsPid ! #etsread_new_object_cfm{sender  = self(),
						     success = false},
		    MasterPid ! #pc_nodedown{sender             = self(),
					     automatic_polling  = AutoPoll};
		
		no_table ->
		    DbsPid ! #etsread_new_object_cfm{sender   = self(),
						     success  = false},
		    MasterPid ! #pc_dead_table{sender = self(),
					       automatic_polling = AutoPoll};
		
		mnesia_not_started ->
		    DbsPid ! #etsread_new_object_cfm{sender   = self(),
						     success  = false},
		    MasterPid ! #pc_dead_table{sender            = self(),
					       automatic_polling = AutoPoll};
		
		{unexpected_error,_Reason} ->
		    DbsPid ! #etsread_new_object_cfm{sender   = self(),
						     success  = false},
		    MasterPid ! #pc_dead_table{sender            = self(),
					       automatic_polling = AutoPoll};
		
		ok ->
		    DbsPid ! #etsread_new_object_cfm{sender  = self(),
						     success = true}
	    end
    end.





new_object2(ets, Node, LocalNode, Tab, _DbsPid, Obj) ->
    tv_ets_rpc:insert(Node, LocalNode, Tab, Obj),
    ok;
new_object2(mnesia, Node, LocalNode, Tab, _DbsPid, Obj) ->
    tv_mnesia_rpc:transaction(
      Node, 
      LocalNode,
      fun() ->
	         %% This mnesia call shall not be distributed,
	         %% since the transaction sees to that it is
	         %% executed on the right node!!!
	      mnesia:write(Tab,Obj,write)
      end),
    ok.
    
		  



check_record_format(mnesia, Node, LocalNode, Tab, Obj) ->
    Arity = tv_mnesia_rpc:table_info(Node, LocalNode, Tab, arity),
    case size(Obj) of
	Arity ->
	    ok;
	_Other ->
	    gs:window(etsreadwin, gs:start(), []),
	    case get(error_msg_mode) of
		normal ->
		    tv_utils:notify(etsreadwin, "TV Notification", 
				    ["The record is not complete,",
				     "too few fields are specified!"]);
		haiku ->
		    tv_utils:notify(etsreadwin, "TV Notification", 
				    ["The attempt to change",
				     "The specified record size",
				     "Is simply ignored."])
	    end,
	    gs:destroy(etsreadwin),
	    bad_format
    end;
check_record_format(ets, _Node, _LocalNode, _Tab, _Obj) ->
    ok.
	    
	    
		      




read_table(Node, LocalNode, Tab, KindOfTable, DbsPid) ->
    T1 = time(),

    {TableContent, ListOfKeys} = 
	case KindOfTable of
	    ets ->
		{tv_ets_rpc:tab2list(Node, LocalNode, Tab), 
		 [tv_ets_rpc:info(Node, LocalNode, Tab, keypos)]
		};
	    mnesia ->
		   %% It may be tempting to use Mnesia event subscription,
		   %% but will this really save the day? The main drawback
		   %% is that we will then have to update the table copy we
		   %% store internally in two different ways: one for the 
		   %% Mnesia tables, and one for the ETS tables. Also, if
		   %% the Mnesia tables are frequently updated, this will 
		   %% cause TV to work all the time too (either updating the
		   %% table copy for each inserted/deleted object, or storing
		   %% these objects until polling is ordered). To make this 
		   %% work smoothly requires a bit of work...
		   %% The second drawback is that it doesn't seem clear in all
		   %% circumstances how the subscription actually works - i.e.,
		   %% if we only use subscriptions, can we actually be sure that
		   %% the *real* state of the table is the same as the one kept
		   %% in TV? For example, imagine the scenario that Mnesia is
		   %% stopped, all Mnesia directories are removed (from the UNIX 
		   %% shell), and then Mnesia once again is started. The first 
		   %% problem is that we have to check for start/stop of Mnesia,
		   %% the second is that we then have to rescan the actual table.
		   %% The logic for this may require som effort to write!
		   %% Also, what will happen if the table is killed/dies? 
		   %% Will we get messages for each element in the table?
		   %% (I havent't checked this last issue, this is just som thoughts.)
		   %% And generally, there is always a risk that a message is lost,
		   %% which will result in TV showing an erroneous table content.
		   %%
		   %% All in all, using Mnesia subscriptions *may* be a sub-optimization.
		   %% The current solution works fine, is also easy to control, and is 
		   %% mainly the same for both ETS and Mnesia tables.
		   %% My suggestion is that it is used until someone actually complains
		   %% about the polling time being too long for huge tables!  :-)
		   %% (However, it shall be emphasized that it is this module that
		   %% actually polls the Mnesia/ETS tables, meaning that it is
		   %% mainly this module that has to be modified, should the usage of
		   %% subscriptions be desired. The other module that has to be modified
		   %% is the one maintaining the internal copy of the table.)
                WildPattern = tv_mnesia_rpc:table_info(Node,LocalNode,Tab,wild_pattern),
                {atomic, Content} = 
                    tv_mnesia_rpc:transaction(
		      Node,
		      LocalNode,
		      fun() ->
			         %% This mnesia call shall not be distributed,
			         %% since the transaction sees to that it is
			         %% executed on the right node!!!
			      mnesia:match_object(Tab, WildPattern, read)
		      end),
                {Content, [2 | tv_mnesia_rpc:table_info(Node, LocalNode,Tab, index)]}
	end,
    
    T2 = time(),

    ElapsedTime = compute_elapsed_seconds(T1, T2),

    DbsPid ! #dbs_new_data{sender             = self(),
			   data               = TableContent,
			   keys               = ListOfKeys,
			   time_to_read_table = ElapsedTime
			  },

    ElapsedTime.







compute_elapsed_seconds({H1, M1, S1}, {H2, M2, S2}) ->
    ElapsedHours   = get_time_diff(hours, H1, H2),
    ElapsedMinutes = get_time_diff(minutes, M1, M2),
    ElapsedSeconds = get_time_diff(seconds, S1, S2),
    (ElapsedHours * 3600) + (ElapsedMinutes * 60) + ElapsedSeconds + 1.





get_time_diff(_Type, T1, T2) when T1 =< T2 ->
    T2 - T1;
get_time_diff(hours, T1, T2) ->
    T2 + 24 - T1;
get_time_diff(minutes, T1, T2) ->
    T2 + 60 - T1;
get_time_diff(seconds, T1, T2) ->
    T2 + 60 - T1.