1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
|
/*********** File AM Vct C++ Program Source Code File (.CPP) ***********/
/* PROGRAM NAME: FILAMVCT */
/* ------------- */
/* Version 2.6 */
/* */
/* COPYRIGHT: */
/* ---------- */
/* (C) Copyright to the author Olivier BERTRAND 2005-2015 */
/* */
/* WHAT THIS PROGRAM DOES: */
/* ----------------------- */
/* This program are the VCT file access method classes. */
/* Added in version 2: */
/* - Split Vec format. */
/* - Partial delete. */
/* - Use of tempfile for update. */
/* */
/***********************************************************************/
/***********************************************************************/
/* Include relevant MariaDB header file. */
/***********************************************************************/
#include "my_global.h"
#if defined(WIN32)
#include <io.h>
#include <fcntl.h>
#if defined(__BORLANDC__)
#define __MFC_COMPAT__ // To define min/max as macro
#endif // __BORLAND__
//#include <windows.h>
#include <sys/stat.h>
#else // !WIN32
#if defined(UNIX)
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#define NO_ERROR 0
#else // !UNIX
#include <io.h>
#endif // !UNIX
#include <fcntl.h>
#endif // !WIN32
/***********************************************************************/
/* Include application header files: */
/* global.h is header containing all global declarations. */
/* plgdbsem.h is header containing the DB application declarations. */
/* tabdos.h is header containing the TABDOS class declarations. */
/***********************************************************************/
#include "global.h"
#include "osutil.h" // Unuseful for WIN32
#include "plgdbsem.h"
#include "valblk.h"
#include "filamfix.h"
#include "tabdos.h"
#include "tabvct.h"
#include "maputil.h"
#include "filamvct.h"
#ifndef INVALID_SET_FILE_POINTER
#define INVALID_SET_FILE_POINTER ((DWORD)-1)
#endif
extern int num_read, num_there; // Statistics
static int num_write;
#if defined(UNIX)
// Add dummy strerror (NGC)
char *strerror(int num);
#endif // UNIX
/***********************************************************************/
/* Header containing block info for not split VEC tables. */
/* Block and last values can be calculated from NumRec and Nrec. */
/* This is better than directly storing Block and Last because it */
/* make possible to use the same file with tables having a different */
/* block size value (Element -> Nrec) */
/* Note: can be in a separate file if header=1 or a true header (2) */
/***********************************************************************/
typedef struct _vecheader {
//int Block; /* The number of used blocks */
//int Last; /* The number of used records in last block */
int MaxRec; /* Max number of records (True vector format)*/
int NumRec; /* Number of valid records in the table */
} VECHEADER;
/***********************************************************************/
/* Char VCT column blocks are right filled with blanks (blank = true) */
/* Conversion of block values allowed conditionally for insert only. */
/***********************************************************************/
PVBLK AllocValBlock(PGLOBAL, void *, int, int, int, int,
bool check = true, bool blank = true, bool un = false);
/* -------------------------- Class VCTFAM --------------------------- */
/***********************************************************************/
/* Implementation of the VCTFAM class. */
/***********************************************************************/
VCTFAM::VCTFAM(PVCTDEF tdp) : FIXFAM((PDOSDEF)tdp)
{
Last = tdp->GetLast();
MaxBlk = (tdp->GetEstimate() > 0) ?
((tdp->GetEstimate() - 1) / Nrec + 1) : 0;
NewBlock = NULL;
AddBlock = false;
Split = false;
if ((Header = (MaxBlk) ? tdp->Header : 0))
Block = Last = -1;
Bsize = Nrec;
CurNum = Nrec - 1;
Colfn = NULL;
Tempat = NULL;
Clens = NULL;
Deplac = NULL;
Isnum = NULL;
Ncol = 0;
} // end of VCTFAM standard constructor
VCTFAM::VCTFAM(PVCTFAM txfp) : FIXFAM(txfp)
{
MaxBlk = txfp->MaxBlk;
NewBlock = NULL;
AddBlock = false;
Split = txfp->Split;
Header = txfp->Header;
Bsize = txfp->Bsize;
Colfn = txfp->Colfn;
Tempat = txfp->Tempat;
Clens = txfp->Clens;
Deplac = txfp->Deplac;
Isnum = txfp->Isnum;
Ncol = txfp->Ncol;
} // end of VCTFAM copy constructor
/***********************************************************************/
/* VCT GetFileLength: returns file size in number of bytes. */
/* This function is here to be accessible by VECFAM and VMPFAM. */
/***********************************************************************/
int VCTFAM::GetFileLength(PGLOBAL g)
{
if (Split) {
// Get the total file length
char filename[_MAX_PATH];
char *savfile = To_File;
int i, len = 0;
// Initialize the array of file structures
if (!Colfn) {
// Prepare the column file name pattern and set Ncol
Colfn = (char*)PlugSubAlloc(g, NULL, _MAX_PATH);
Ncol = ((PVCTDEF)Tdbp->GetDef())->MakeFnPattern(Colfn);
} // endif Colfn
To_File = filename;
for (i = 0; i < Ncol; i++) {
sprintf(filename, Colfn, i+1);
len += TXTFAM::GetFileLength(g);
} // endfor i
To_File = savfile;
return len;
} else
return TXTFAM::GetFileLength(g);
} // end of GetFileLength
/***********************************************************************/
/* Reset read/write position values. */
/***********************************************************************/
void VCTFAM::Reset(void)
{
FIXFAM::Reset();
NewBlock = NULL;
AddBlock = false;
CurNum = Nrec - 1;
} // end of Reset
/***********************************************************************/
/* Get the Headlen, Block and Last info from the file header. */
/***********************************************************************/
int VCTFAM::GetBlockInfo(PGLOBAL g)
{
char filename[_MAX_PATH];
int h, k, n;
VECHEADER vh;
if (Header < 1 || Header > 3 || !MaxBlk) {
sprintf(g->Message, "Invalid header value %d", Header);
return -1;
} else
n = (Header == 1) ? (int)sizeof(VECHEADER) : 0;
PlugSetPath(filename, To_File, Tdbp->GetPath());
if (Header == 2)
strcat(PlugRemoveType(filename, filename), ".blk");
if ((h = global_open(g, MSGID_CANNOT_OPEN, filename, O_RDONLY)) == -1
|| !_filelength(h)) {
// Consider this is a void table
Last = Nrec;
Block = 0;
if (h != -1)
close(h);
return n;
} else if (Header == 3)
k = lseek(h, -(int)sizeof(VECHEADER), SEEK_END);
if ((k = read(h, &vh, sizeof(vh))) != sizeof(vh)) {
sprintf(g->Message, "Error reading header file %s", filename);
n = -1;
} else if (MaxBlk * Nrec != vh.MaxRec) {
sprintf(g->Message, "MaxRec=%d doesn't match MaxBlk=%d Nrec=%d",
vh.MaxRec, MaxBlk, Nrec);
n = -1;
} else {
Block = (vh.NumRec > 0) ? (vh.NumRec + Nrec - 1) / Nrec : 0;
Last = (vh.NumRec + Nrec - 1) % Nrec + 1;
} // endif s
close(h);
return n;
} // end of GetBlockInfo
/***********************************************************************/
/* Get the Headlen, Block and Last info from the file header. */
/***********************************************************************/
bool VCTFAM::SetBlockInfo(PGLOBAL g)
{
char filename[_MAX_PATH];
bool rc = false;
size_t n;
VECHEADER vh;
FILE *s;
PlugSetPath(filename, To_File, Tdbp->GetPath());
if (Header != 2) {
if (Stream) {
s = Stream;
if (Header == 1)
/*k =*/ fseek(s, 0, SEEK_SET);
} else
s= global_fopen(g, MSGID_CANNOT_OPEN, filename, "r+b");
} else { // Header == 2
strcat(PlugRemoveType(filename, filename), ".blk");
s= global_fopen(g, MSGID_CANNOT_OPEN, filename, "wb");
} // endif Header
if (!s) {
sprintf(g->Message, "Error opening header file %s", filename);
return true;
} else if (Header == 3)
/*k =*/ fseek(s, -(int)sizeof(VECHEADER), SEEK_END);
vh.MaxRec = MaxBlk * Bsize;
vh.NumRec = (Block - 1) * Nrec + Last;
if ((n = fwrite(&vh, sizeof(vh), 1, s)) != 1) {
sprintf(g->Message, "Error writing header file %s", filename);
rc = true;
} // endif fread
if (Header == 2 || !Stream)
fclose(s);
return rc;
} // end of SetBlockInfo
/***********************************************************************/
/* Use BlockTest to reduce the table estimated size. */
/***********************************************************************/
int VCTFAM::MaxBlkSize(PGLOBAL g, int)
{
int rc = RC_OK, savcur = CurBlk;
int size;
// Roughly estimate the table size as the sum of blocks
// that can contain good rows
for (size = 0, CurBlk = 0; CurBlk < Block; CurBlk++)
if ((rc = Tdbp->TestBlock(g)) == RC_OK)
size += (CurBlk == Block - 1) ? Last : Nrec;
else if (rc == RC_EF)
break;
CurBlk = savcur;
return size;
} // end of MaxBlkSize
/***********************************************************************/
/* VCT Cardinality: returns table cardinality in number of rows. */
/* This function can be called with a null argument to test the */
/* availability of Cardinality implementation (1 yes, 0 no). */
/***********************************************************************/
int VCTFAM::Cardinality(PGLOBAL g)
{
if (!g)
return 1;
if (Block < 0)
if (Split) {
// Separate column files and no pre setting of Block and Last
// This allows to see a table modified externally, but Block
// and Last must be set from the file cardinality.
// Only happens when called by sub classes.
char filename[_MAX_PATH];
PSZ savfn = To_File;
int len, clen, card = -1;
PCOLDEF cdp = Tdbp->GetDef()->GetCols();
if (!Colfn) {
// Prepare the column file name pattern
Colfn = (char*)PlugSubAlloc(g, NULL, _MAX_PATH);
Ncol = ((VCTDEF*)Tdbp->GetDef())->MakeFnPattern(Colfn);
} // endif Colfn
// Use the first column file to calculate the cardinality
clen = cdp->GetClen();
sprintf(filename, Colfn, 1);
To_File = filename;
len = TXTFAM::GetFileLength(g);
To_File = savfn;
if (len >= 0) {
if (!(len % clen))
card = len / clen; // Fixed length file
else
sprintf(g->Message, MSG(NOT_FIXED_LEN), To_File, len, clen);
if (trace)
htrc(" Computed max_K=%d Filen=%d Clen=%d\n", card, len, clen);
} else
card = 0;
// Set number of blocks for later use
Block = (card > 0) ? (card + Nrec - 1) / Nrec : 0;
Last = (card + Nrec - 1) % Nrec + 1;
return card;
} else {
// Vector table having Block and Last info in a Header (file)
if ((Headlen = GetBlockInfo(g)) < 0)
return -1; // Error
} // endif split
return (Block) ? ((Block - 1) * Nrec + Last) : 0;
} // end of Cardinality
/***********************************************************************/
/* GetRowID: return the RowID of last read record. */
/***********************************************************************/
int VCTFAM::GetRowID(void)
{
return 1 + ((CurBlk < Block) ? CurNum + Nrec * CurBlk
: (Block - 1) * Nrec + Last);
} // end of GetRowID
/***********************************************************************/
/* VCT Create an empty file for Vector formatted tables. */
/***********************************************************************/
bool VCTFAM::MakeEmptyFile(PGLOBAL g, char *fn)
{
// Vector formatted file: this will create an empty file of the
// required length if it does not exists yet.
char filename[_MAX_PATH], c = 0;
int h, n;
PlugSetPath(filename, fn, Tdbp->GetPath());
#if defined(WIN32)
h= global_open(g, MSGID_OPEN_EMPTY_FILE, filename, _O_CREAT | _O_WRONLY, S_IREAD | S_IWRITE);
#else // !WIN32
h= global_open(g, MSGID_OPEN_EMPTY_FILE, filename, O_CREAT | O_WRONLY, S_IREAD | S_IWRITE);
#endif // !WIN32
if (h == -1)
return true;
n = (Header == 1 || Header == 3) ? sizeof(VECHEADER) : 0;
if (lseek(h, n + MaxBlk * Nrec * Lrecl - 1, SEEK_SET) < 0)
goto err;
// This actually fills the empty file
if (write(h, &c, 1) < 0)
goto err;
close(h);
return false;
err:
sprintf(g->Message, MSG(MAKE_EMPTY_FILE), To_File, strerror(errno));
close(h);
return true;
} // end of MakeEmptyFile
/***********************************************************************/
/* VCT Access Method opening routine. */
/* New method now that this routine is called recursively (last table */
/* first in reverse order): index blocks are immediately linked to */
/* join block of next table if it exists or else are discarted. */
/***********************************************************************/
bool VCTFAM::OpenTableFile(PGLOBAL g)
{
char opmode[4], filename[_MAX_PATH];
MODE mode = Tdbp->GetMode();
PDBUSER dbuserp = PlgGetUser(g);
/*********************************************************************/
/* Update block info if necessary. */
/*********************************************************************/
if (Block < 0)
if ((Headlen = GetBlockInfo(g)) < 0)
return true;
/*********************************************************************/
/* Open according to input/output mode required. */
/*********************************************************************/
switch (mode) {
case MODE_READ:
strcpy(opmode, "rb");
break;
case MODE_DELETE:
if (!Tdbp->GetNext()) {
// Store the number of deleted lines
DelRows = Cardinality(g);
// This will delete the whole file
strcpy(opmode, "wb");
break;
} // endif
// Selective delete, pass thru
case MODE_UPDATE:
UseTemp = Tdbp->IsUsingTemp(g);
strcpy(opmode, (UseTemp) ? "rb" : "r+b");
break;
case MODE_INSERT:
if (MaxBlk) {
if (!Block)
if (MakeEmptyFile(g, To_File))
return true;
strcpy(opmode, "r+b"); // Required to update empty blocks
} else if (!Block || Last == Nrec)
strcpy(opmode, "ab");
else
strcpy(opmode, "r+b"); // Required to update the last block
break;
default:
sprintf(g->Message, MSG(BAD_OPEN_MODE), mode);
return true;
} // endswitch Mode
/*********************************************************************/
/* Use conventionnal input/output functions. */
/*********************************************************************/
PlugSetPath(filename, To_File, Tdbp->GetPath());
if (!(Stream = PlugOpenFile(g, filename, opmode))) {
if (trace)
htrc("%s\n", g->Message);
return (mode == MODE_READ && errno == ENOENT)
? PushWarning(g, Tdbp) : true;
} // endif Stream
if (trace)
htrc("File %s is open in mode %s\n", filename, opmode);
To_Fb = dbuserp->Openlist; // Keep track of File block
if (!strcmp(opmode, "wb"))
// This will stop the process by
// causing GetProgMax to return 0.
return ResetTableSize(g, 0, Nrec);
num_read = num_there = num_write = 0;
// Allocate the table and column block buffer
return AllocateBuffer(g);
} // end of OpenTableFile
/***********************************************************************/
/* Allocate the block buffers for columns used in the query. */
/***********************************************************************/
bool VCTFAM::AllocateBuffer(PGLOBAL g)
{
MODE mode = Tdbp->GetMode();
PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
PCOLDEF cdp;
PVCTCOL cp = (PVCTCOL)Tdbp->GetColumns();
if (mode == MODE_INSERT) {
bool chk = PlgGetUser(g)->Check & CHK_TYPE;
NewBlock = (char*)PlugSubAlloc(g, NULL, Blksize);
for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
memset(NewBlock + Nrec * cdp->GetPoff(),
(IsTypeNum(cdp->GetType()) ? 0 : ' '),
Nrec * cdp->GetClen());
for (; cp; cp = (PVCTCOL)cp->Next)
cp->Blk = AllocValBlock(g, NewBlock + Nrec * cp->Deplac,
cp->Buf_Type, Nrec, cp->Format.Length,
cp->Format.Prec, chk);
return InitInsert(g); // Initialize inserting
} else {
if (UseTemp || mode == MODE_DELETE) {
// Allocate all that is needed to move lines
int i = 0, n = (MaxBlk) ? MaxBlk : 1;
if (!Ncol)
for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
Ncol++;
Clens = (int*)PlugSubAlloc(g, NULL, Ncol * sizeof(int));
Deplac = (int*)PlugSubAlloc(g, NULL, Ncol * sizeof(int));
Isnum = (bool*)PlugSubAlloc(g, NULL, Ncol * sizeof(bool));
for (cdp = defp->GetCols(); cdp; i++, cdp = cdp->GetNext()) {
Clens[i] = cdp->GetClen();
Deplac[i] = Headlen + cdp->GetPoff() * n * Nrec;
Isnum[i] = IsTypeNum(cdp->GetType());
Buflen = MY_MAX(Buflen, cdp->GetClen());
} // endfor cdp
if (!UseTemp || MaxBlk) {
Buflen *= Nrec;
To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
} else
NewBlock = (char*)PlugSubAlloc(g, NULL, Blksize);
} // endif mode
for (; cp; cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial()) // Not a pseudo column
cp->Blk = AllocValBlock(g, NULL, cp->Buf_Type, Nrec,
cp->Format.Length, cp->Format.Prec);
} //endif mode
return false;
} // end of AllocateBuffer
/***********************************************************************/
/* Do initial action when inserting. */
/***********************************************************************/
bool VCTFAM::InitInsert(PGLOBAL g)
{
// We come here in MODE_INSERT only
if (Last == Nrec) {
CurBlk = Block;
CurNum = 0;
AddBlock = !MaxBlk;
} else {
int rc;
PVCTCOL cp = (PVCTCOL)Tdbp->GetColumns();
// The starting point must be at the end of file as for append.
CurBlk = Block - 1;
CurNum = Last;
// Prepare error return
if (g->jump_level == MAX_JUMP) {
strcpy(g->Message, MSG(TOO_MANY_JUMPS));
return true;
} // endif
if ((rc = setjmp(g->jumper[++g->jump_level])) != 0) {
g->jump_level--;
return true;
} // endif
// Last block must be updated by new values
for (; cp; cp = (PVCTCOL)cp->Next)
cp->ReadBlock(g);
g->jump_level--;
} // endif Last
// We are not currently using a temporary file for Insert
T_Stream = Stream;
return false;
} // end of InitInsert
/***********************************************************************/
/* ReadBuffer: Read one line for a VCT file. */
/***********************************************************************/
int VCTFAM::ReadBuffer(PGLOBAL g)
{
int rc = RC_OK;
MODE mode = Tdbp->GetMode();
if (Placed)
Placed = false;
else if ((++CurNum) >= ((CurBlk < Block - 1) ? Nrec : Last)) {
/*******************************************************************/
/* New block. */
/*******************************************************************/
CurNum = 0;
next:
if (++CurBlk == Block)
return RC_EF; // End of file
/*******************************************************************/
/* Before reading a new block, check whether block optimizing */
/* can be done, as well as for join as for local filtering. */
/*******************************************************************/
switch (Tdbp->TestBlock(g)) {
case RC_EF:
return RC_EF;
case RC_NF:
goto next;
} // endswitch rc
num_there++;
} // endif CurNum
if (OldBlk != CurBlk) {
if (mode == MODE_UPDATE) {
/*****************************************************************/
/* Flush the eventually modified column buffers in old blocks */
/* and read the blocks to modify attached to Set columns. */
/*****************************************************************/
if (MoveLines(g)) // For VECFAM
return RC_FX;
for (PVCTCOL colp = (PVCTCOL)Tdbp->GetSetCols();
colp; colp = (PVCTCOL)colp->Next) {
colp->WriteBlock(g);
colp->ReadBlock(g);
} // endfor colp
} // endif mode
OldBlk = CurBlk; // Last block actually read
} // endif oldblk
if (trace)
htrc(" Read: CurNum=%d CurBlk=%d rc=%d\n", CurNum, CurBlk, RC_OK);
return rc;
} // end of ReadBuffer
/***********************************************************************/
/* Data Base write routine for VCT access method. */
/***********************************************************************/
int VCTFAM::WriteBuffer(PGLOBAL g)
{
if (trace)
htrc("VCT WriteBuffer: R%d Mode=%d CurNum=%d CurBlk=%d\n",
Tdbp->GetTdb_No(), Tdbp->GetMode(), CurNum, CurBlk);
if (Tdbp->GetMode() == MODE_UPDATE) {
// Mode Update is done in ReadDB, we just initialize it here
if (!T_Stream) {
if (UseTemp) {
if (OpenTempFile(g))
return RC_FX;
// Most of the time, not all table columns are updated.
// This why we must completely pre-fill the temporary file.
Fpos = (MaxBlk) ? (Block - 1) * Nrec + Last
: Block * Nrec; // To write last lock
if (MoveIntermediateLines(g))
return RC_FX;
} else
T_Stream = Stream;
} // endif T_Stream
} else {
// Mode Insert
if (MaxBlk && CurBlk == MaxBlk) {
strcpy(g->Message, MSG(TRUNC_BY_ESTIM));
return RC_EF; // Too many lines for vector formatted table
} // endif MaxBlk
if (Closing || ++CurNum == Nrec) {
PVCTCOL cp = (PVCTCOL)Tdbp->GetColumns();
if (!AddBlock) {
// Write back the updated last block values
for (; cp; cp = (PVCTCOL)cp->Next)
cp->WriteBlock(g);
if (!Closing && !MaxBlk) {
// For VCT tables, future blocks must be added
char filename[_MAX_PATH];
// Close the file and reopen it in mode Insert
fclose(Stream);
PlugSetPath(filename, To_File, Tdbp->GetPath());
if (!(Stream= global_fopen(g, MSGID_OPEN_MODE_STRERROR, filename, "ab"))) {
Closing = true; // Tell CloseDB of error
return RC_FX;
} // endif Stream
AddBlock = true;
} // endif Closing
} else {
// Here we must add a new block to the file
if (Closing)
// Reset the overwritten columns for last block extra records
for (; cp; cp = (PVCTCOL)cp->Next)
memset(NewBlock + Nrec * cp->Deplac + Last * cp->Clen,
(cp->Buf_Type == TYPE_STRING) ? ' ' : '\0',
(Nrec - Last) * cp->Clen);
if ((size_t)Nrec !=
fwrite(NewBlock, (size_t)Lrecl, (size_t)Nrec, Stream)) {
sprintf(g->Message, MSG(WRITE_STRERROR), To_File, strerror(errno));
return RC_FX;
} // endif
} // endif AddBlock
if (!Closing) {
CurBlk++;
CurNum = 0;
} // endif Closing
} // endif Closing || CurNum
} // endif Mode
return RC_OK;
} // end of WriteBuffer
/***********************************************************************/
/* Data Base delete line routine for VCT access method. */
/* Note: lines are moved directly in the files (ooops...) */
/* Using temp file depends on the Check setting, false by default. */
/***********************************************************************/
int VCTFAM::DeleteRecords(PGLOBAL g, int irc)
{
bool eof = false;
if (trace)
htrc("VCT DeleteDB: rc=%d UseTemp=%d Fpos=%d Tpos=%d Spos=%d\n",
irc, UseTemp, Fpos, Tpos, Spos);
if (irc != RC_OK) {
/*******************************************************************/
/* EOF: position Fpos at the end-of-file position. */
/*******************************************************************/
Fpos = (Block - 1) * Nrec + Last;
if (trace)
htrc("Fpos placed at file end=%d\n", Fpos);
eof = UseTemp && !MaxBlk;
} else // Fpos is the Deleted line position
Fpos = CurBlk * Nrec + CurNum;
if (Tpos == Spos) {
if (UseTemp) {
/*****************************************************************/
/* Open the temporary file, Spos is at the beginning of file. */
/*****************************************************************/
if (OpenTempFile(g))
return RC_FX;
} else {
/*****************************************************************/
/* First line to delete. Move of eventual preceding lines is */
/* not required here, just the setting of future Spos and Tpos. */
/*****************************************************************/
T_Stream = Stream;
Spos = Tpos = Fpos;
} // endif UseTemp
} // endif Tpos == Spos
/*********************************************************************/
/* Move any intermediate lines. */
/*********************************************************************/
if (MoveIntermediateLines(g, &eof))
return RC_FX;
if (irc == RC_OK) {
/*******************************************************************/
/* Reposition the file pointer and set Spos. */
/*******************************************************************/
#ifdef _DEBUG
assert(Spos == Fpos);
#endif
Spos++; // New start position is on next line
if (trace)
htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
} else {
/*******************************************************************/
/* Last call after EOF has been reached. */
/* Update the Block and Last values. */
/*******************************************************************/
Block = (Tpos > 0) ? (Tpos + Nrec - 1) / Nrec : 0;
Last = (Tpos + Nrec - 1) % Nrec + 1;
if (!UseTemp) { // The UseTemp case is treated in CloseTableFile
if (!MaxBlk) {
/***************************************************************/
/* Because the chsize functionality is only accessible with a */
/* system call we must close the file and reopen it with the */
/* open function (_fopen for MS ??) this is still to be */
/* checked for compatibility with Text files and other OS's. */
/***************************************************************/
char filename[_MAX_PATH];
int h;
/*rc =*/ CleanUnusedSpace(g); // Clean last block
/*rc =*/ PlugCloseFile(g, To_Fb);
Stream = NULL; // For SetBlockInfo
PlugSetPath(filename, To_File, Tdbp->GetPath());
if ((h= global_open(g, MSGID_OPEN_STRERROR, filename, O_WRONLY)) <= 0)
return RC_FX;
/***************************************************************/
/* Remove extra blocks. */
/***************************************************************/
#if defined(UNIX)
if (ftruncate(h, (off_t)(Headlen + Block * Blksize))) {
sprintf(g->Message, MSG(TRUNCATE_ERROR), strerror(errno));
close(h);
return RC_FX;
} // endif
#else
if (chsize(h, Headlen + Block * Blksize)) {
sprintf(g->Message, MSG(CHSIZE_ERROR), strerror(errno));
close(h);
return RC_FX;
} // endif
#endif
close(h);
if (trace)
htrc("done, h=%d irc=%d\n", h, irc);
} else
// Clean the unused space in the file, this is required when
// inserting again with a partial column list.
if (CleanUnusedSpace(g))
return RC_FX;
if (ResetTableSize(g, Block, Last))
return RC_FX;
} // endif UseTemp
} // endif irc
return RC_OK; // All is correct
} // end of DeleteRecords
/***********************************************************************/
/* Open a temporary file used while updating or deleting. */
/***********************************************************************/
bool VCTFAM::OpenTempFile(PGLOBAL g)
{
char *opmode, tempname[_MAX_PATH];
bool rc = false;
/*********************************************************************/
/* Open the temporary file, Spos is at the beginning of file. */
/*********************************************************************/
PlugSetPath(tempname, To_File, Tdbp->GetPath());
strcat(PlugRemoveType(tempname, tempname), ".t");
if (MaxBlk) {
if (MakeEmptyFile(g, tempname))
return true;
opmode = "r+b";
} else
opmode = "wb";
if (!(T_Stream = PlugOpenFile(g, tempname, opmode))) {
if (trace)
htrc("%s\n", g->Message);
rc = true;
} else
To_Fbt = PlgGetUser(g)->Openlist;
return rc;
} // end of OpenTempFile
/***********************************************************************/
/* Move intermediate deleted or updated lines. */
/***********************************************************************/
bool VCTFAM::MoveIntermediateLines(PGLOBAL g, bool *b)
{
int i, dep, off;
int n;
bool eof = (b) ? *b : false;
size_t req, len;
for (n = Fpos - Spos; n > 0 || eof; n -= req) {
/*******************************************************************/
/* Non consecutive line to delete. Move intermediate lines. */
/*******************************************************************/
if (!MaxBlk)
req = (size_t)MY_MIN(n, Nrec - MY_MAX(Spos % Nrec, Tpos % Nrec));
else
req = (size_t)MY_MIN(n, Nrec);
if (req) for (i = 0; i < Ncol; i++) {
if (MaxBlk) {
dep = Deplac[i];
off = Spos * Clens[i];
} else {
if (UseTemp)
To_Buf = NewBlock + Deplac[i] + (Tpos % Nrec) * Clens[i];
dep = Deplac[i] + (Spos / Nrec) * Blksize;
off = (Spos % Nrec) * Clens[i];
} // endif MaxBlk
if (fseek(Stream, dep + off, SEEK_SET)) {
sprintf(g->Message, MSG(READ_SEEK_ERROR), strerror(errno));
return true;
} // endif
len = fread(To_Buf, Clens[i], req, Stream);
if (trace)
htrc("after read req=%d len=%d\n", req, len);
if (len != req) {
sprintf(g->Message, MSG(DEL_READ_ERROR), (int) req, (int) len);
return true;
} // endif len
if (!UseTemp || MaxBlk) {
if (MaxBlk) {
dep = Deplac[i];
off = Tpos * Clens[i];
} else {
dep = Deplac[i] + (Tpos / Nrec) * Blksize;
off = (Tpos % Nrec) * Clens[i];
} // endif MaxBlk
if (fseek(T_Stream, dep + off, SEEK_SET)) {
sprintf(g->Message, MSG(WRITE_SEEK_ERR), strerror(errno));
return true;
} // endif
if ((len = fwrite(To_Buf, Clens[i], req, T_Stream)) != req) {
sprintf(g->Message, MSG(DEL_WRITE_ERROR), strerror(errno));
return true;
} // endif
} // endif UseTemp
if (trace)
htrc("after write pos=%d\n", ftell(Stream));
} // endfor i
Tpos += (int)req;
Spos += (int)req;
if (UseTemp && !MaxBlk && (Tpos % Nrec == 0 || (eof && Spos == Fpos))) {
// Write the full or last block to the temporary file
if ((dep = Nrec - (Tpos % Nrec)) < Nrec)
// Clean the last block in case of future insert,
// must be done here because T_Stream was open in write only.
for (i = 0; i < Ncol; i++) {
To_Buf = NewBlock + Deplac[i] + (Tpos % Nrec) * Clens[i];
memset(To_Buf, (Isnum[i]) ? 0 : ' ', dep * Clens[i]);
} // endfor i
// Write a new block in the temporary file
len = (size_t)Blksize;
if (fwrite(NewBlock, 1, len, T_Stream) != len) {
sprintf(g->Message, MSG(DEL_WRITE_ERROR), strerror(errno));
return true;
} // endif
if (Spos == Fpos)
eof = false;
} // endif UseTemp
if (trace)
htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
} // endfor n
return false;
} // end of MoveIntermediateLines
/***********************************************************************/
/* Clean deleted space in a VCT or Vec table file. */
/***********************************************************************/
bool VCTFAM::CleanUnusedSpace(PGLOBAL g)
{
int i, dep;
int n;
size_t req, len;
if (!MaxBlk) {
/*******************************************************************/
/* Clean last block of the VCT table file. */
/*******************************************************************/
assert(!UseTemp);
if (!(n = Nrec - Last))
return false;
dep = (Block - 1) * Blksize;
req = (size_t)n;
for (i = 0; i < Ncol; i++) {
memset(To_Buf, (Isnum[i]) ? 0 : ' ', n * Clens[i]);
if (fseek(Stream, dep + Deplac[i] + Last * Clens[i], SEEK_SET)) {
sprintf(g->Message, MSG(WRITE_SEEK_ERR), strerror(errno));
return true;
} // endif
if ((len = fwrite(To_Buf, Clens[i], req, Stream)) != req) {
sprintf(g->Message, MSG(DEL_WRITE_ERROR), strerror(errno));
return true;
} // endif
} // endfor i
} else for (n = Fpos - Tpos; n > 0; n -= req) {
/*******************************************************************/
/* Fill VEC file remaining lines with 0's. */
/* Note: this seems to work even column blocks have been made */
/* with Blanks = true. Perhaps should it be set to false for VEC. */
/*******************************************************************/
req = (size_t)MY_MIN(n, Nrec);
memset(To_Buf, 0, Buflen);
for (i = 0; i < Ncol; i++) {
if (fseek(T_Stream, Deplac[i] + Tpos * Clens[i], SEEK_SET)) {
sprintf(g->Message, MSG(WRITE_SEEK_ERR), strerror(errno));
return true;
} // endif
if ((len = fwrite(To_Buf, Clens[i], req, T_Stream)) != req) {
sprintf(g->Message, MSG(DEL_WRITE_ERROR), strerror(errno));
return true;
} // endif
} // endfor i
Tpos += (int)req;
} // endfor n
return false;
} // end of CleanUnusedSpace
/***********************************************************************/
/* Data Base close routine for VCT access method. */
/***********************************************************************/
void VCTFAM::CloseTableFile(PGLOBAL g, bool abort)
{
int rc = 0, wrc = RC_OK;
MODE mode = Tdbp->GetMode();
Abort = abort;
if (mode == MODE_INSERT) {
if (Closing)
wrc = RC_FX; // Last write was in error
else
if (CurNum) {
// Some more inserted lines remain to be written
Last = CurNum;
Block = CurBlk + 1;
Closing = true;
wrc = WriteBuffer(g);
} else {
Last = Nrec;
Block = CurBlk;
wrc = RC_OK;
} // endif CurNum
if (wrc != RC_FX) {
rc = ResetTableSize(g, Block, Last);
} else if (AddBlock) {
// Last block was not written
rc = ResetTableSize(g, CurBlk, Nrec);
longjmp(g->jumper[g->jump_level], 44);
} // endif
} else if (mode == MODE_UPDATE) {
// Write back to file any pending modifications
for (PVCTCOL colp = (PVCTCOL)((PTDBVCT)Tdbp)->To_SetCols;
colp; colp = (PVCTCOL)colp->Next)
colp->WriteBlock(g);
if (UseTemp && T_Stream) {
rc = RenameTempFile(g);
if (Header) {
// Header must be set because it was not set in temp file
Stream = T_Stream = NULL; // For SetBlockInfo
rc = SetBlockInfo(g);
} // endif Header
} // endif UseTemp
} else if (mode == MODE_DELETE && UseTemp && T_Stream) {
if (MaxBlk)
rc = CleanUnusedSpace(g);
if ((rc = RenameTempFile(g)) != RC_FX) {
Stream = T_Stream = NULL; // For SetBlockInfo
rc = ResetTableSize(g, Block, Last);
} // endif rc
} // endif's mode
if (!(UseTemp && T_Stream))
rc = PlugCloseFile(g, To_Fb);
if (trace)
htrc("VCT CloseTableFile: closing %s wrc=%d rc=%d\n",
To_File, wrc, rc);
Stream = NULL;
} // end of CloseTableFile
/***********************************************************************/
/* Data Base close routine for VCT access method. */
/***********************************************************************/
bool VCTFAM::ResetTableSize(PGLOBAL g, int block, int last)
{
bool rc = false;
// Set Block and Last values for TDBVCT::MakeBlockValues
Block = block;
Last = last;
if (!Split) {
if (!Header) {
// Update catalog values for Block and Last
PVCTDEF defp = (PVCTDEF)Tdbp->GetDef();
LPCSTR name = Tdbp->GetName();
defp->SetBlock(Block);
defp->SetLast(Last);
if (!defp->SetIntCatInfo("Blocks", Block) ||
!defp->SetIntCatInfo("Last", Last)) {
sprintf(g->Message, MSG(UPDATE_ERROR), "Header");
rc = true;
} // endif
} else
rc = SetBlockInfo(g);
} // endif Split
Tdbp->ResetSize();
return rc;
} // end of ResetTableSize
/***********************************************************************/
/* Rewind routine for VCT access method. */
/***********************************************************************/
void VCTFAM::Rewind(void)
{
// In mode update we need to read Set Column blocks
if (Tdbp->GetMode() == MODE_UPDATE)
OldBlk = -1;
// Initialize so block optimization is called for 1st block
CurBlk = -1;
CurNum = Nrec - 1;
//rewind(Stream); will be placed by fseek
} // end of Rewind
/***********************************************************************/
/* ReadBlock: Read column values from current block. */
/***********************************************************************/
bool VCTFAM::ReadBlock(PGLOBAL g, PVCTCOL colp)
{
int len;
size_t n;
/*********************************************************************/
/* Calculate the offset and size of the block to read. */
/*********************************************************************/
if (MaxBlk) // True vector format
len = Headlen + Nrec * (colp->Deplac * MaxBlk + colp->Clen * CurBlk);
else // Blocked vector format
len = Nrec * (colp->Deplac + Lrecl * CurBlk);
if (trace)
htrc("len=%d Nrec=%d Deplac=%d Lrecl=%d CurBlk=%d maxblk=%d\n",
len, Nrec, colp->Deplac, Lrecl, CurBlk, MaxBlk);
if (fseek(Stream, len, SEEK_SET)) {
sprintf(g->Message, MSG(FSEEK_ERROR), strerror(errno));
return true;
} // endif
n = fread(colp->Blk->GetValPointer(), (size_t)colp->Clen,
(size_t)Nrec, Stream);
if (n != (size_t)Nrec) {
if (errno == NO_ERROR)
sprintf(g->Message, MSG(BAD_READ_NUMBER), (int) n, To_File);
else
sprintf(g->Message, MSG(READ_ERROR),
To_File, strerror(errno));
if (trace)
htrc(" Read error: %s\n", g->Message);
return true;
} // endif
if (trace)
num_read++;
return false;
} // end of ReadBlock
/***********************************************************************/
/* WriteBlock: Write back current column values for one block. */
/* Note: the test of Status is meant to prevent physical writing of */
/* the block during the checking loop in mode Update. It is set to */
/* BUF_EMPTY when reopening the table between the two loops. */
/***********************************************************************/
bool VCTFAM::WriteBlock(PGLOBAL g, PVCTCOL colp)
{
int len;
size_t n;
/*********************************************************************/
/* Calculate the offset and size of the block to write. */
/*********************************************************************/
if (MaxBlk) // File has Vector format
len = Headlen
+ Nrec * (colp->Deplac * MaxBlk + colp->Clen * colp->ColBlk);
else // Old VCT format
len = Nrec * (colp->Deplac + Lrecl * colp->ColBlk);
if (trace)
htrc("modif=%d len=%d Nrec=%d Deplac=%d Lrecl=%d colblk=%d\n",
Modif, len, Nrec, colp->Deplac, Lrecl, colp->ColBlk);
if (fseek(T_Stream, len, SEEK_SET)) {
sprintf(g->Message, MSG(FSEEK_ERROR), strerror(errno));
return true;
} // endif
// Here Nrec was changed to CurNum in mode Insert,
// this is the true number of records to write,
// this also avoid writing garbage in the file for true vector tables.
n = (Tdbp->GetMode() == MODE_INSERT) ? CurNum : Nrec;
if (n != fwrite(colp->Blk->GetValPointer(),
(size_t)colp->Clen, n, T_Stream)) {
sprintf(g->Message, MSG(WRITE_STRERROR),
(UseTemp) ? To_Fbt->Fname : To_File, strerror(errno));
if (trace)
htrc("Write error: %s\n", strerror(errno));
return true;
} // endif
#if defined(UNIX)
fflush(T_Stream); //NGC
#endif
#ifdef _DEBUG
num_write++;
#endif
return false;
} // end of WriteBlock
/* -------------------------- Class VCMFAM --------------------------- */
/***********************************************************************/
/* Implementation of the VCMFAM class. */
/***********************************************************************/
VCMFAM::VCMFAM(PVCTDEF tdp) : VCTFAM((PVCTDEF)tdp)
{
Memory = NULL;
Memcol = NULL;
} // end of VCMFAM standard constructor
VCMFAM::VCMFAM(PVCMFAM txfp) : VCTFAM(txfp)
{
Memory = txfp->Memory;
Memcol = txfp->Memcol;
} // end of VCMFAM copy constructor
/***********************************************************************/
/* Mapped VCT Access Method opening routine. */
/* New method now that this routine is called recursively (last table */
/* first in reverse order): index blocks are immediately linked to */
/* join block of next table if it exists or else are discarted. */
/***********************************************************************/
bool VCMFAM::OpenTableFile(PGLOBAL g)
{
char filename[_MAX_PATH];
int len;
MODE mode = Tdbp->GetMode();
PFBLOCK fp = NULL;
PDBUSER dbuserp = (PDBUSER)g->Activityp->Aptr;
/*********************************************************************/
/* Update block info if necessary. */
/*********************************************************************/
if (Block < 0)
if ((Headlen = GetBlockInfo(g)) < 0)
return true;
/*********************************************************************/
/* We used the file name relative to recorded datapath. */
/*********************************************************************/
PlugSetPath(filename, To_File, Tdbp->GetPath());
/*********************************************************************/
/* The whole file will be mapped so we can use it as if it were */
/* entirely read into virtual memory. */
/* Firstly we check whether this file have been already mapped. */
/*********************************************************************/
if (mode == MODE_READ) {
for (fp = dbuserp->Openlist; fp; fp = fp->Next)
if (fp->Type == TYPE_FB_MAP && !stricmp(fp->Fname, filename)
&& fp->Count && fp->Mode == mode)
break;
if (trace)
htrc("Mapping VCM file, fp=%p cnt=%d\n", fp, fp->Count);
} else
fp = NULL;
if (fp) {
/*******************************************************************/
/* File already mapped. Just increment use count and get pointer. */
/*******************************************************************/
fp->Count++;
Memory = fp->Memory;
len = fp->Length;
} else {
/*******************************************************************/
/* If required, delete the whole file if no filtering is implied. */
/*******************************************************************/
bool del;
HANDLE hFile;
MEMMAP mm;
MODE mapmode = mode;
if (mode == MODE_INSERT) {
if (MaxBlk) {
if (!Block)
if (MakeEmptyFile(g, To_File))
return true;
// Inserting will be like updating the file
mapmode = MODE_UPDATE;
} else {
strcpy(g->Message, "MAP Insert is for VEC Estimate tables only");
return true;
} // endif MaxBlk
} // endif mode
del = mode == MODE_DELETE && !Tdbp->GetNext();
if (del) {
DelRows = Cardinality(g);
// This will stop the process by causing GetProgMax to return 0.
// ResetTableSize(g, 0, Nrec); must be done later
} // endif del
/*******************************************************************/
/* Create the mapping file object. */
/*******************************************************************/
hFile = CreateFileMap(g, filename, &mm, mapmode, del);
if (hFile == INVALID_HANDLE_VALUE) {
DWORD rc = GetLastError();
if (!(*g->Message))
sprintf(g->Message, MSG(OPEN_MODE_ERROR),
"map", (int) rc, filename);
if (trace)
htrc("%s\n", g->Message);
return (mode == MODE_READ && rc == ENOENT)
? PushWarning(g, Tdbp) : true;
} // endif hFile
/*******************************************************************/
/* Get the file size (assuming file is smaller than 4 GB) */
/*******************************************************************/
len = mm.lenL;
Memory = (char *)mm.memory;
if (!len) { // Empty or deleted file
CloseFileHandle(hFile);
bool rc = ResetTableSize(g, 0, Nrec);
return (mapmode == MODE_UPDATE) ? true : rc;
} // endif len
if (!Memory) {
CloseFileHandle(hFile);
sprintf(g->Message, MSG(MAP_VIEW_ERROR),
filename, GetLastError());
return true;
} // endif Memory
if (mode != MODE_DELETE) {
CloseFileHandle(hFile); // Not used anymore
hFile = INVALID_HANDLE_VALUE; // For Fblock
} // endif Mode
/*******************************************************************/
/* Link a Fblock. This make possible to reuse already opened maps */
/* and also to automatically unmap them in case of error g->jump. */
/* Note: block can already exist for previously closed file. */
/*******************************************************************/
fp = (PFBLOCK)PlugSubAlloc(g, NULL, sizeof(FBLOCK));
fp->Type = TYPE_FB_MAP;
fp->Fname = PlugDup(g, filename);
fp->Next = dbuserp->Openlist;
dbuserp->Openlist = fp;
fp->Count = 1;
fp->Length = len;
fp->Memory = Memory;
fp->Mode = mode;
fp->File = NULL;
fp->Handle = hFile; // Used for Delete
} // endif fp
To_Fb = fp; // Useful when closing
if (trace)
htrc("fp=%p count=%d MapView=%p len=%d Top=%p\n",
fp, fp->Count, Memory, len);
return AllocateBuffer(g);
} // end of OpenTableFile
/***********************************************************************/
/* Allocate the block buffers for columns used in the query. */
/* Give a dummy value (1) to prevent allocating the value block. */
/* It will be set pointing into the memory map of the file. */
/* Note: Memcol must be set for all columns because it can be used */
/* for set columns in Update. Clens values are used only in Delete. */
/***********************************************************************/
bool VCMFAM::AllocateBuffer(PGLOBAL g)
{
int m, i = 0;
bool b = Tdbp->GetMode() == MODE_DELETE;
PVCTCOL cp;
PCOLDEF cdp;
PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
// Calculate the number of columns
if (!Ncol)
for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
Ncol++;
// To store the start position of each column
Memcol = (char**)PlugSubAlloc(g, NULL, Ncol * sizeof(char*));
m = (MaxBlk) ? MaxBlk : 1;
// We will need all column sizes and type for Delete
if (b) {
Clens = (int*)PlugSubAlloc(g, NULL, Ncol * sizeof(int));
Isnum = (bool*)PlugSubAlloc(g, NULL, Ncol * sizeof(bool));
} // endif b
for (cdp = defp->GetCols(); i < Ncol; i++, cdp = cdp->GetNext()) {
if (b) {
Clens[i] = cdp->GetClen();
Isnum[i] = IsTypeNum(cdp->GetType());
} // endif b
Memcol[i] = Memory + Headlen + cdp->GetPoff() * m * Nrec;
} // endfor cdp
for (cp = (PVCTCOL)Tdbp->GetColumns(); cp; cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial()) { // Not a pseudo column
cp->Blk = AllocValBlock(g, (void*)1, cp->Buf_Type, Nrec,
cp->Format.Length, cp->Format.Prec);
cp->AddStatus(BUF_MAPPED);
} // endif IsSpecial
if (Tdbp->GetMode() == MODE_INSERT)
return InitInsert(g);
return false;
} // end of AllocateBuffer
/***********************************************************************/
/* Do initial action when inserting. */
/***********************************************************************/
bool VCMFAM::InitInsert(PGLOBAL g)
{
int rc;
volatile PVCTCOL cp = (PVCTCOL)Tdbp->GetColumns();
// We come here in MODE_INSERT only
if (Last == Nrec) {
CurBlk = Block;
CurNum = 0;
AddBlock = !MaxBlk;
} else {
// The starting point must be at the end of file as for append.
CurBlk = Block - 1;
CurNum = Last;
} // endif Last
// Prepare error return
if (g->jump_level == MAX_JUMP) {
strcpy(g->Message, MSG(TOO_MANY_JUMPS));
return true;
} // endif
if ((rc = setjmp(g->jumper[++g->jump_level])) != 0) {
g->jump_level--;
return true;
} // endif
// Initialize the column block pointer
for (; cp; cp = (PVCTCOL)cp->Next)
cp->ReadBlock(g);
g->jump_level--;
return false;
} // end of InitInsert
/***********************************************************************/
/* Data Base write routine for VMP access method. */
/***********************************************************************/
int VCMFAM::WriteBuffer(PGLOBAL g)
{
if (trace)
htrc("VCM WriteBuffer: R%d Mode=%d CurNum=%d CurBlk=%d\n",
Tdbp->GetTdb_No(), Tdbp->GetMode(), CurNum, CurBlk);
// Mode Update being done in ReadDB we process here Insert mode only.
if (Tdbp->GetMode() == MODE_INSERT) {
if (CurBlk == MaxBlk) {
strcpy(g->Message, MSG(TRUNC_BY_ESTIM));
return RC_EF; // Too many lines for vector formatted table
} // endif MaxBlk
if (Closing || ++CurNum == Nrec) {
PVCTCOL cp = (PVCTCOL)Tdbp->GetColumns();
// Write back the updated last block values
for (; cp; cp = (PVCTCOL)cp->Next)
cp->WriteBlock(g);
if (!Closing) {
CurBlk++;
CurNum = 0;
// Re-initialize the column block pointer
for (cp = (PVCTCOL)Tdbp->GetColumns(); cp; cp = (PVCTCOL)cp->Next)
cp->ReadBlock(g);
} // endif Closing
} // endif Closing || CurNum
} // endif Mode
return RC_OK;
} // end of WriteBuffer
/***********************************************************************/
/* Data Base delete line routine for VMP access method. */
/* Lines between deleted lines are moved in the mapfile view. */
/***********************************************************************/
int VCMFAM::DeleteRecords(PGLOBAL g, int irc)
{
if (trace)
htrc("VCM DeleteDB: irc=%d tobuf=%p Tpos=%p Spos=%p\n",
irc, To_Buf, Tpos, Spos);
if (irc != RC_OK) {
/*******************************************************************/
/* EOF: position Fpos at the top of map position. */
/*******************************************************************/
Fpos = (Block - 1) * Nrec + Last;
if (trace)
htrc("Fpos placed at file top=%p\n", Fpos);
} else // Fpos is the Deleted line position
Fpos = CurBlk * Nrec + CurNum;
if (Tpos == Spos) {
/*******************************************************************/
/* First line to delete. Move of eventual preceding lines is */
/* not required here, just setting of future Spos and Tpos. */
/*******************************************************************/
Tpos = Spos = Fpos;
} else
(void)MoveIntermediateLines(g);
if (irc == RC_OK) {
Spos = Fpos + 1; // New start position
if (trace)
htrc("after: Tpos=%p Spos=%p\n", Tpos, Spos);
} else {
/*******************************************************************/
/* Last call after EOF has been reached. */
/*******************************************************************/
int i, m, n;
/*******************************************************************/
/* Reset the Block and Last values for TDBVCT::MakeBlockValues. */
/*******************************************************************/
Block = (Tpos > 0) ? (Tpos + Nrec - 1) / Nrec : 0;
Last = (Tpos + Nrec - 1) % Nrec + 1;
if (!MaxBlk) {
PFBLOCK fp = To_Fb;
// Clean the unused part of the last block
m = (Block - 1) * Blksize;
n = Nrec - Last;
for (i = 0; i < Ncol; i++)
memset(Memcol[i] + m + Last * Clens[i],
(Isnum[i]) ? 0 : ' ', n * Clens[i]);
// We must Unmap the view and use the saved file handle
// to put an EOF at the end of the last block of the file.
CloseMemMap(fp->Memory, (size_t)fp->Length);
fp->Count = 0; // Avoid doing it twice
// Remove extra blocks
n = Block * Blksize;
#if defined(WIN32)
DWORD drc = SetFilePointer(fp->Handle, n, NULL, FILE_BEGIN);
if (drc == 0xFFFFFFFF) {
sprintf(g->Message, MSG(FUNCTION_ERROR),
"SetFilePointer", GetLastError());
CloseHandle(fp->Handle);
return RC_FX;
} // endif
if (trace)
htrc("done, Tpos=%p newsize=%d drc=%d\n", Tpos, n, drc);
if (!SetEndOfFile(fp->Handle)) {
sprintf(g->Message, MSG(FUNCTION_ERROR),
"SetEndOfFile", GetLastError());
CloseHandle(fp->Handle);
return RC_FX;
} // endif
CloseHandle(fp->Handle);
#else // UNIX
if (ftruncate(fp->Handle, (off_t)n)) {
sprintf(g->Message, MSG(TRUNCATE_ERROR), strerror(errno));
close(fp->Handle);
return RC_FX;
} // endif
close(fp->Handle);
#endif // UNIX
} else
// True vector table, Table file size does not change.
// Just clean the unused part of the file.
for (n = Fpos - Tpos, i = 0; i < Ncol; i++)
memset(Memcol[i] + Tpos * Clens[i], 0, n * Clens[i]);
// Reset Last and Block values in the catalog
PlugCloseFile(g, To_Fb); // in case of Header
ResetTableSize(g, Block, Last);
} // endif irc
return RC_OK; // All is correct
} // end of DeleteRecords
/***********************************************************************/
/* Move intermediate deleted or updated lines. */
/***********************************************************************/
bool VCMFAM::MoveIntermediateLines(PGLOBAL, bool *)
{
int i, m, n;
if ((n = Fpos - Spos) > 0) {
/*******************************************************************/
/* Non consecutive line to delete. Move intermediate lines. */
/*******************************************************************/
if (!MaxBlk) {
// Old VCT format, moving must respect block limits
char *ps, *pt;
int req, soff, toff;
for (; n > 0; n -= req) {
soff = Spos % Nrec;
toff = Tpos % Nrec;
req = (size_t)MY_MIN(n, Nrec - MY_MAX(soff, toff));
for (i = 0; i < Ncol; i++) {
ps = Memcol[i] + (Spos / Nrec) * Blksize + soff * Clens[i];
pt = Memcol[i] + (Tpos / Nrec) * Blksize + toff * Clens[i];
memmove(pt, ps, req * Clens[i]);
} // endfor i
Tpos += req;
Spos += req;
} // endfor n
} else {
// True vector format, all is simple...
for (i = 0; i < Ncol; i++) {
m = Clens[i];
memmove(Memcol[i] + Tpos * m, Memcol[i] + Spos * m, n * m);
} // endfor i
Tpos += n;
} // endif MaxBlk
if (trace)
htrc("move %d bytes\n", n);
} // endif n
return false;
} // end of MoveIntermediate Lines
/***********************************************************************/
/* Data Base close routine for VMP access method. */
/***********************************************************************/
void VCMFAM::CloseTableFile(PGLOBAL g, bool)
{
int wrc = RC_OK;
MODE mode = Tdbp->GetMode();
if (mode == MODE_INSERT) {
if (!Closing) {
if (CurNum) {
// Some more inserted lines remain to be written
Last = CurNum;
Block = CurBlk + 1;
Closing = true;
wrc = WriteBuffer(g);
} else {
Last = Nrec;
Block = CurBlk;
wrc = RC_OK;
} // endif CurNum
} else
wrc = RC_FX; // Last write was in error
PlugCloseFile(g, To_Fb);
if (wrc != RC_FX)
/*rc =*/ ResetTableSize(g, Block, Last);
} else if (mode != MODE_DELETE || Abort)
PlugCloseFile(g, To_Fb);
} // end of CloseTableFile
/***********************************************************************/
/* ReadBlock: Read column values from current block. */
/***********************************************************************/
bool VCMFAM::ReadBlock(PGLOBAL, PVCTCOL colp)
{
char *mempos;
int i = colp->Index - 1;
int n = Nrec * ((MaxBlk || Split) ? colp->Clen : Lrecl);
/*********************************************************************/
/* Calculate the start position of the column block to read. */
/*********************************************************************/
mempos = Memcol[i] + n * CurBlk;
if (trace)
htrc("mempos=%p i=%d Nrec=%d Clen=%d CurBlk=%d\n",
mempos, i, Nrec, colp->Clen, CurBlk);
if (colp->GetStatus(BUF_MAPPED))
colp->Blk->SetValPointer(mempos);
if (trace)
num_read++;
return false;
} // end of ReadBlock
/***********************************************************************/
/* WriteBlock: Write back current column values for one block. */
/* Note: there is nothing to do because we are working directly into */
/* the mapped file, except when checking for Update but in this case */
/* we do not want to write back the modifications either. */
/***********************************************************************/
bool VCMFAM::WriteBlock(PGLOBAL, PVCTCOL colp __attribute__((unused)))
{
#if defined(_DEBUG)
char *mempos;
int i = colp->Index - 1;
int n = Nrec * colp->Clen;
/*********************************************************************/
/* Calculate the offset and size of the block to write. */
/*********************************************************************/
mempos = Memcol[i] + n * CurBlk;
if (trace)
htrc("modif=%d mempos=%p i=%d Nrec=%d Clen=%d colblk=%d\n",
Modif, mempos, i, Nrec, colp->Clen, colp->ColBlk);
#endif // _DEBUG
return false;
} // end of WriteBlock
/* -------------------------- Class VECFAM --------------------------- */
/***********************************************************************/
/* Implementation of the VECFAM class. */
/***********************************************************************/
VECFAM::VECFAM(PVCTDEF tdp) : VCTFAM((PVCTDEF)tdp)
{
Streams = NULL;
To_Fbs = NULL;
To_Bufs = NULL;
Split = true;
Block = Last = -1;
InitUpdate = false;
} // end of VECFAM standard constructor
VECFAM::VECFAM(PVECFAM txfp) : VCTFAM(txfp)
{
Streams = txfp->Streams;
To_Fbs = txfp->To_Fbs;
Clens = txfp->Clens;
To_Bufs = txfp->To_Bufs;
InitUpdate = txfp->InitUpdate;
} // end of VECFAM copy constructor
/***********************************************************************/
/* VEC Access Method opening routine. */
/* New method now that this routine is called recursively (last table */
/* first in reverse order): index blocks are immediately linked to */
/* join block of next table if it exists or else are discarted. */
/***********************************************************************/
bool VECFAM::OpenTableFile(PGLOBAL g)
{
char opmode[4];
int i;
bool b= false;
PCOLDEF cdp;
PVCTCOL cp;
MODE mode = Tdbp->GetMode();
PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
/*********************************************************************/
/* Call Cardinality to set Block and Last values in case it was not */
/* already called (this happens indeed in test xmode) */
/*********************************************************************/
Cardinality(g);
/*********************************************************************/
/* Open according to input/output mode required. */
/*********************************************************************/
switch (mode) {
case MODE_READ:
strcpy(opmode, "rb");
break;
case MODE_DELETE:
if (!Tdbp->GetNext()) {
// Store the number of deleted lines
DelRows = Cardinality(g);
// This will delete the whole file
strcpy(opmode, "wb");
// This will stop the process by causing GetProgMax to return 0.
ResetTableSize(g, 0, Nrec);
break;
} // endif filter
// Selective delete, pass thru
case MODE_UPDATE:
UseTemp = Tdbp->IsUsingTemp(g);
strcpy(opmode, (UseTemp) ? "rb": "r+b");
break;
case MODE_INSERT:
strcpy(opmode, "ab");
break;
default:
sprintf(g->Message, MSG(BAD_OPEN_MODE), mode);
return true;
} // endswitch Mode
/*********************************************************************/
/* Initialize the array of file structures. */
/*********************************************************************/
if (!Colfn) {
// Prepare the column file name pattern and set Ncol
Colfn = (char*)PlugSubAlloc(g, NULL, _MAX_PATH);
Ncol = ((VCTDEF*)Tdbp->GetDef())->MakeFnPattern(Colfn);
} // endif Colfn
Streams = (FILE* *)PlugSubAlloc(g, NULL, Ncol * sizeof(FILE*));
To_Fbs = (PFBLOCK *)PlugSubAlloc(g, NULL, Ncol * sizeof(PFBLOCK));
for (i = 0; i < Ncol; i++) {
Streams[i] = NULL;
To_Fbs[i] = NULL;
} // endif i
/*********************************************************************/
/* Open the files corresponding to columns used in the query. */
/*********************************************************************/
if (mode == MODE_INSERT || mode == MODE_DELETE) {
// All columns must be written or deleted
for (i = 0, cdp = defp->GetCols(); cdp; i++, cdp = cdp->GetNext())
if (OpenColumnFile(g, opmode, i))
return true;
// Check for void table or missing columns
for (b = !Streams[0], i = 1; i < Ncol; i++)
if (b != !Streams[i])
return true;
} else {
/*******************************************************************/
/* Open the files corresponding to updated columns of the query. */
/*******************************************************************/
for (cp = (PVCTCOL)((PTDBVCT)Tdbp)->To_SetCols; cp;
cp = (PVCTCOL)cp->Next)
if (OpenColumnFile(g, opmode, cp->Index - 1))
return true;
// Open in read only mode the used columns not already open
for (cp = (PVCTCOL)Tdbp->GetColumns(); cp; cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial() && !Streams[cp->Index - 1])
if (OpenColumnFile(g, "rb", cp->Index - 1))
return true;
// Check for void table or missing columns
for (i = 0, cp = (PVCTCOL)Tdbp->GetColumns(); cp;
cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial()) {
if (!i++)
b = !Streams[cp->Index - 1];
else if (b != !Streams[cp->Index - 1])
return true;
} // endif Special
} // endif mode
/*********************************************************************/
/* Allocate the table and column block buffer. */
/*********************************************************************/
return (b) ? false : AllocateBuffer(g);
} // end of OpenTableFile
/***********************************************************************/
/* Open the file corresponding to one column. */
/***********************************************************************/
bool VECFAM::OpenColumnFile(PGLOBAL g, char *opmode, int i)
{
char filename[_MAX_PATH];
PDBUSER dup = PlgGetUser(g);
sprintf(filename, Colfn, i+1);
if (!(Streams[i] = PlugOpenFile(g, filename, opmode))) {
if (trace)
htrc("%s\n", g->Message);
return (Tdbp->GetMode() == MODE_READ && errno == ENOENT)
? PushWarning(g, Tdbp) : true;
} // endif Streams
if (trace)
htrc("File %s is open in mode %s\n", filename, opmode);
To_Fbs[i] = dup->Openlist; // Keep track of File blocks
return false;
} // end of OpenColumnFile
/***********************************************************************/
/* Allocate the block buffers for columns used in the query. */
/***********************************************************************/
bool VECFAM::AllocateBuffer(PGLOBAL g)
{
int i;
PVCTCOL cp;
PCOLDEF cdp;
PTDBVCT tdbp = (PTDBVCT)Tdbp;
MODE mode = tdbp->GetMode();
PDOSDEF defp = (PDOSDEF)tdbp->GetDef();
if (mode != MODE_READ) {
// Allocate what is needed by all modes except Read
T_Streams = (FILE* *)PlugSubAlloc(g, NULL, Ncol * sizeof(FILE*));
Clens = (int*)PlugSubAlloc(g, NULL, Ncol * sizeof(int));
// Give default values
for (i = 0; i < Ncol; i++) {
T_Streams[i] = Streams[i];
Clens[i] = 0;
} // endfor i
} // endif mode
if (mode == MODE_INSERT) {
bool chk = PlgGetUser(g)->Check & CHK_TYPE;
To_Bufs = (void**)PlugSubAlloc(g, NULL, Ncol * sizeof(void*));
cdp = defp->GetCols();
for (i = 0; cdp && i < Ncol; i++, cdp = cdp->GetNext()) {
Clens[i] = cdp->GetClen();
To_Bufs[i] = PlugSubAlloc(g, NULL, Nrec * Clens[i]);
if (cdp->GetType() == TYPE_STRING)
memset(To_Bufs[i], ' ', Nrec * Clens[i]);
else
memset(To_Bufs[i], 0, Nrec * Clens[i]);
} // endfor cdp
for (cp = (PVCTCOL)tdbp->Columns; cp; cp = (PVCTCOL)cp->Next)
cp->Blk = AllocValBlock(g, To_Bufs[cp->Index - 1],
cp->Buf_Type, Nrec, cp->Format.Length,
cp->Format.Prec, chk);
return InitInsert(g);
} else {
if (UseTemp || mode == MODE_DELETE) {
// Allocate all that is needed to move lines and make Temp
if (UseTemp) {
Tempat = (char*)PlugSubAlloc(g, NULL, _MAX_PATH);
strcpy(Tempat, Colfn);
PlugSetPath(Tempat, Tempat, Tdbp->GetPath());
strcat(PlugRemoveType(Tempat, Tempat), ".t");
T_Fbs = (PFBLOCK *)PlugSubAlloc(g, NULL, Ncol * sizeof(PFBLOCK));
} // endif UseTemp
if (UseTemp)
for (i = 0; i < Ncol; i++) {
T_Streams[i] = (mode == MODE_UPDATE) ? (FILE*)1 : NULL;
T_Fbs[i] = NULL;
} // endfor i
if (mode == MODE_DELETE) { // All columns are moved
cdp = defp->GetCols();
for (i = 0; cdp && i < Ncol; i++, cdp = cdp->GetNext()) {
Clens[i] = cdp->GetClen();
Buflen = MY_MAX(Buflen, cdp->GetClen());
} // endfor cdp
} else { // Mode Update, only some columns are updated
for (cp = (PVCTCOL)tdbp->To_SetCols; cp; cp = (PVCTCOL)cp->Next) {
i = cp->Index -1;
if (UseTemp)
T_Streams[i] = NULL; // Mark the streams to open
Clens[i] = cp->Clen;
Buflen = MY_MAX(Buflen, cp->Clen);
} // endfor cp
InitUpdate = true; // To be initialized
} // endif mode
To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen * Nrec);
} // endif mode
// Finally allocate column buffers for all modes
for (cp = (PVCTCOL)tdbp->Columns; cp; cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial()) // Not a pseudo column
cp->Blk = AllocValBlock(g, NULL, cp->Buf_Type, Nrec,
cp->Format.Length, cp->Format.Prec);
} // endif mode
return false;
} // end of AllocateBuffer
/***********************************************************************/
/* Do initial action when inserting. */
/***********************************************************************/
bool VECFAM::InitInsert(PGLOBAL)
{
// We come here in MODE_INSERT only
CurBlk = 0;
CurNum = 0;
AddBlock = true;
return false;
} // end of InitInsert
/***********************************************************************/
/* Reset buffer access according to indexing and to mode. */
/* >>>>>>>>>>>>>> TO BE RE-VISITED AND CHECKED <<<<<<<<<<<<<<<<<<<<<< */
/***********************************************************************/
void VECFAM::ResetBuffer(PGLOBAL g)
{
/*********************************************************************/
/* If access is random, performances can be much better when the */
/* reads are done on only one row, except for small tables that can */
/* be entirely read in one block. If the index is just used as a */
/* bitmap filter, as for Update or Delete, reading will be */
/* sequential and we better keep block reading. */
/*********************************************************************/
if (Tdbp->GetKindex() && Block > 1 && Tdbp->GetMode() == MODE_READ) {
Nrec = 1; // Better for random access
Rbuf = 0;
OldBlk = -2; // Has no meaning anymore
Block = Tdbp->Cardinality(g); // Blocks are one line now
Last = 1; // Probably unuseful
} // endif Mode
} // end of ResetBuffer
/***********************************************************************/
/* Data Base write routine for VCT access method. */
/***********************************************************************/
int VECFAM::WriteBuffer(PGLOBAL g)
{
if (trace)
htrc("VCT WriteBuffer: R%d Mode=%d CurNum=%d CurBlk=%d\n",
Tdbp->GetTdb_No(), Tdbp->GetMode(), CurNum, CurBlk);
if (Tdbp->GetMode() == MODE_INSERT) {
if (Closing || ++CurNum == Nrec) {
// Here we must add a new blocks to the files
int i;
size_t n = (size_t)CurNum;
for (i = 0; i < Ncol; i++)
if (n != fwrite(To_Bufs[i], (size_t)Clens[i], n, Streams[i])) {
sprintf(g->Message, MSG(WRITE_STRERROR), To_File, strerror(errno));
return RC_FX;
} // endif
if (!Closing) {
CurBlk++;
CurNum = 0;
} // endif Closing
} // endif Closing || CurNum
} else // Mode Update
// Writing updates being done in ReadDB we do initialization only.
if (InitUpdate) {
if (OpenTempFile(g))
return RC_FX;
InitUpdate = false; // Done
} // endif InitUpdate
return RC_OK;
} // end of WriteBuffer
/***********************************************************************/
/* Data Base delete line routine for split vertical access methods. */
/* Note: lines are moved directly in the files (ooops...) */
/* Using temp file depends on the Check setting, false by default. */
/***********************************************************************/
int VECFAM::DeleteRecords(PGLOBAL g, int irc)
{
if (trace)
htrc("VEC DeleteDB: rc=%d UseTemp=%d Fpos=%d Tpos=%d Spos=%d\n",
irc, UseTemp, Fpos, Tpos, Spos);
if (irc != RC_OK) {
/*******************************************************************/
/* EOF: position Fpos at the end-of-file position. */
/*******************************************************************/
Fpos = Cardinality(g);
if (trace)
htrc("Fpos placed at file end=%d\n", Fpos);
} else // Fpos is the Deleted line position
Fpos = CurBlk * Nrec + CurNum;
if (Tpos == Spos) {
// First line to delete
if (UseTemp) {
/*****************************************************************/
/* Open the temporary files, Spos is at the beginning of file. */
/*****************************************************************/
if (OpenTempFile(g))
return RC_FX;
} else
/*****************************************************************/
/* Move of eventual preceding lines is not required here. */
/* Set the future Tpos, and give Spos a value to block copying. */
/*****************************************************************/
Spos = Tpos = Fpos;
} // endif Tpos == Spos
/*********************************************************************/
/* Move any intermediate lines. */
/*********************************************************************/
if (MoveIntermediateLines(g))
return RC_FX;
if (irc == RC_OK) {
#ifdef _DEBUG
assert(Spos == Fpos);
#endif
Spos++; // New start position is on next line
if (trace)
htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
} else {
/*******************************************************************/
/* Last call after EOF has been reached. */
/*******************************************************************/
if (!UseTemp) {
/*****************************************************************/
/* Because the chsize functionality is only accessible with a */
/* system call we must close the files and reopen them with the */
/* open function (_fopen for MS??) this is still to be checked */
/* for compatibility with other OS's. */
/*****************************************************************/
char filename[_MAX_PATH];
int h; // File handle, return code
for (int i = 0; i < Ncol; i++) {
sprintf(filename, Colfn, i + 1);
/*rc =*/ PlugCloseFile(g, To_Fbs[i]);
if ((h= global_open(g, MSGID_OPEN_STRERROR, filename, O_WRONLY)) <= 0)
return RC_FX;
/***************************************************************/
/* Remove extra records. */
/***************************************************************/
#if defined(UNIX)
if (ftruncate(h, (off_t)(Tpos * Clens[i]))) {
sprintf(g->Message, MSG(TRUNCATE_ERROR), strerror(errno));
close(h);
return RC_FX;
} // endif
#else
if (chsize(h, Tpos * Clens[i])) {
sprintf(g->Message, MSG(CHSIZE_ERROR), strerror(errno));
close(h);
return RC_FX;
} // endif
#endif
close(h);
if (trace)
htrc("done, h=%d irc=%d\n", h, irc);
} // endfor i
} else // UseTemp
// Ok, now delete old files and rename new temp files
if (RenameTempFile(g) == RC_FX)
return RC_FX;
// Reset these values for TDBVCT::MakeBlockValues
Block = (Tpos > 0) ? (Tpos + Nrec - 1) / Nrec : 0;
Last = (Tpos + Nrec - 1) % Nrec + 1;
if (ResetTableSize(g, Block, Last))
return RC_FX;
} // endif irc
return RC_OK; // All is correct
} // end of DeleteRecords
/***********************************************************************/
/* Open temporary files used while updating or deleting. */
/* Note: the files not updated have been given a T_Stream value of 1. */
/***********************************************************************/
bool VECFAM::OpenTempFile(PGLOBAL g)
{
char tempname[_MAX_PATH];
for (int i = 0; i < Ncol; i++)
if (!T_Streams[i]) {
/*****************************************************************/
/* Open the temporary file, Spos is at the beginning of file. */
/*****************************************************************/
sprintf(tempname, Tempat, i+1);
if (!(T_Streams[i] = PlugOpenFile(g, tempname, "wb"))) {
if (trace)
htrc("%s\n", g->Message);
return true;
} else
T_Fbs[i] = PlgGetUser(g)->Openlist;
} else // This is a column that is not updated
T_Streams[i] = NULL; // For RenameTempFile
return false;
} // end of OpenTempFile
/***********************************************************************/
/* Move intermediate updated lines before writing blocks. */
/***********************************************************************/
bool VECFAM::MoveLines(PGLOBAL g)
{
if (UseTemp && !InitUpdate) { // Don't do it in check pass
Fpos = OldBlk * Nrec;
if (MoveIntermediateLines(g)) {
Closing = true; // ???
return true;
} // endif UseTemp
// Spos = Fpos + Nrec;
} // endif UseTemp
return false;
} // end of MoveLines
/***********************************************************************/
/* Move intermediate deleted or updated lines. */
/***********************************************************************/
bool VECFAM::MoveIntermediateLines(PGLOBAL g, bool *)
{
int i, n;
bool b = false;
size_t req, len;
for (n = Fpos - Spos; n > 0; n -= Nrec) {
/*******************************************************************/
/* Non consecutive line to delete. Move intermediate lines. */
/*******************************************************************/
req = (size_t)MY_MIN(n, Nrec);
for (i = 0; i < Ncol; i++) {
if (!T_Streams[i])
continue; // Non updated column
if (!UseTemp || !b)
if (fseek(Streams[i], Spos * Clens[i], SEEK_SET)) {
sprintf(g->Message, MSG(READ_SEEK_ERROR), strerror(errno));
return true;
} // endif
len = fread(To_Buf, Clens[i], req, Streams[i]);
if (trace)
htrc("after read req=%d len=%d\n", req, len);
if (len != req) {
sprintf(g->Message, MSG(DEL_READ_ERROR), (int) req, (int) len);
return true;
} // endif len
if (!UseTemp)
if (fseek(T_Streams[i], Tpos * Clens[i], SEEK_SET)) {
sprintf(g->Message, MSG(WRITE_SEEK_ERR), strerror(errno));
return true;
} // endif
if ((len = fwrite(To_Buf, Clens[i], req, T_Streams[i])) != req) {
sprintf(g->Message, MSG(DEL_WRITE_ERROR), strerror(errno));
return true;
} // endif
if (trace)
htrc("after write pos=%d\n", ftell(Streams[i]));
} // endfor i
Tpos += (int)req;
Spos += (int)req;
if (trace)
htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
b = true;
} // endfor n
return false;
} // end of MoveIntermediate Lines
/***********************************************************************/
/* Delete the old files and rename the new temporary files. */
/***********************************************************************/
int VECFAM::RenameTempFile(PGLOBAL g)
{
char *tempname, filetemp[_MAX_PATH], filename[_MAX_PATH];
int rc = RC_OK;
// Close all files.
// This loop is necessary because, in case of join,
// the table files can have been open several times.
for (PFBLOCK fb = PlgGetUser(g)->Openlist; fb; fb = fb->Next)
rc = PlugCloseFile(g, fb);
for (int i = 0; i < Ncol && rc == RC_OK; i++) {
if (!T_Fbs[i])
continue;
tempname = (char*)T_Fbs[i]->Fname;
if (!Abort) {
sprintf(filename, Colfn, i+1);
PlugSetPath(filename, filename, Tdbp->GetPath());
strcat(PlugRemoveType(filetemp, filename), ".ttt");
remove(filetemp); // May still be there from previous error
if (rename(filename, filetemp)) { // Save file for security
sprintf(g->Message, MSG(RENAME_ERROR),
filename, filetemp, strerror(errno));
rc = RC_FX;
} else if (rename(tempname, filename)) {
sprintf(g->Message, MSG(RENAME_ERROR),
tempname, filename, strerror(errno));
rc = rename(filetemp, filename); // Restore saved file
rc = RC_FX;
} else if (remove(filetemp)) {
sprintf(g->Message, MSG(REMOVE_ERROR),
filetemp, strerror(errno));
rc = RC_INFO; // Acceptable
} // endif's
} else
remove(tempname);
} // endfor i
return rc;
} // end of RenameTempFile
/***********************************************************************/
/* Data Base close routine for VEC access method. */
/***********************************************************************/
void VECFAM::CloseTableFile(PGLOBAL g, bool abort)
{
int rc = 0, wrc = RC_OK;
MODE mode = Tdbp->GetMode();
Abort = abort;
if (mode == MODE_INSERT) {
if (Closing)
wrc = RC_FX; // Last write was in error
else
if (CurNum) {
// Some more inserted lines remain to be written
Last += (CurBlk * Nrec + CurNum -1);
Block += (Last / Nrec);
Last = Last % Nrec + 1;
Closing = true;
wrc = WriteBuffer(g);
} else {
Block += CurBlk;
wrc = RC_OK;
} // endif CurNum
if (wrc != RC_FX)
rc = ResetTableSize(g, Block, Last);
else
longjmp(g->jumper[g->jump_level], 44);
} else if (mode == MODE_UPDATE) {
if (UseTemp && !InitUpdate && !Abort) {
// Write any intermediate lines to temp file
Fpos = OldBlk * Nrec;
Abort = MoveIntermediateLines(g) != RC_OK;
// Spos = Fpos + Nrec;
} // endif UseTemp
// Write back to file any pending modifications
if (wrc == RC_OK)
for (PVCTCOL colp = (PVCTCOL)((PTDBVCT)Tdbp)->To_SetCols;
colp; colp = (PVCTCOL)colp->Next)
colp->WriteBlock(g);
if (wrc == RC_OK && UseTemp && !InitUpdate && !Abort) {
// Write any intermediate lines to temp file
Fpos = (Block - 1) * Nrec + Last;
Abort = MoveIntermediateLines(g) != RC_OK;
} // endif UseTemp
} // endif's mode
if (UseTemp && !InitUpdate) {
// If they are errors, leave files unchanged
rc = RenameTempFile(g);
} else if (Streams)
for (int i = 0; i < Ncol; i++)
if (Streams[i]) {
rc = PlugCloseFile(g, To_Fbs[i]);
Streams[i] = NULL;
To_Fbs[i] = NULL;
} // endif Streams
if (trace)
htrc("VCT CloseTableFile: closing %s wrc=%d rc=%d\n", To_File, wrc, rc);
} // end of CloseTableFile
/***********************************************************************/
/* ReadBlock: Read column values from current block. */
/***********************************************************************/
bool VECFAM::ReadBlock(PGLOBAL g, PVCTCOL colp)
{
int i, len;
size_t n;
/*********************************************************************/
/* Calculate the offset and size of the block to read. */
/*********************************************************************/
len = Nrec * colp->Clen * CurBlk;
i = colp->Index - 1;
if (trace)
htrc("len=%d i=%d Nrec=%d Deplac=%d Lrecl=%d CurBlk=%d\n",
len, i, Nrec, colp->Deplac, Lrecl, CurBlk);
if (fseek(Streams[i], len, SEEK_SET)) {
sprintf(g->Message, MSG(FSEEK_ERROR), strerror(errno));
return true;
} // endif
n = fread(colp->Blk->GetValPointer(), (size_t)colp->Clen,
(size_t)Nrec, Streams[i]);
if (n != (size_t)Nrec && (CurBlk+1 != Block || n != (size_t)Last)) {
char fn[_MAX_PATH];
sprintf(fn, Colfn, colp->Index);
#if defined(WIN32)
if (feof(Streams[i]))
#else // !WIN32
if (errno == NO_ERROR)
#endif // !WIN32
sprintf(g->Message, MSG(BAD_READ_NUMBER), (int) n, fn);
else
sprintf(g->Message, MSG(READ_ERROR),
fn, strerror(errno));
if (trace)
htrc(" Read error: %s\n", g->Message);
return true;
} // endif
if (trace)
num_read++;
return false;
} // end of ReadBlock
/***********************************************************************/
/* WriteBlock: Write back current column values for one block. */
/* Note: the test of Status is meant to prevent physical writing of */
/* the block during the checking loop in mode Update. It is set to */
/* BUF_EMPTY when reopening the table between the two loops. */
/***********************************************************************/
bool VECFAM::WriteBlock(PGLOBAL g, PVCTCOL colp)
{
int i, len;
size_t n;
/*********************************************************************/
/* Calculate the offset and size of the block to write. */
/*********************************************************************/
len = Nrec * colp->Clen * colp->ColBlk;
i = colp->Index - 1;
if (trace)
htrc("modif=%d len=%d i=%d Nrec=%d Deplac=%d Lrecl=%d colblk=%d\n",
Modif, len, i, Nrec, colp->Deplac, Lrecl, colp->ColBlk);
if (Tdbp->GetMode() == MODE_UPDATE && !UseTemp)
if (fseek(T_Streams[i], len, SEEK_SET)) {
sprintf(g->Message, MSG(FSEEK_ERROR), strerror(errno));
return true;
} // endif
// Here Nrec was changed to CurNum in mode Insert,
// this is the true number of records to write,
// this also avoid writing garbage in the file for true vector tables.
n = (Tdbp->GetMode() == MODE_INSERT) ? CurNum
: (colp->ColBlk == Block - 1) ? Last : Nrec;
if (n != fwrite(colp->Blk->GetValPointer(),
(size_t)colp->Clen, n, T_Streams[i])) {
char fn[_MAX_PATH];
sprintf(fn, (UseTemp) ? Tempat : Colfn, colp->Index);
sprintf(g->Message, MSG(WRITE_STRERROR), fn, strerror(errno));
if (trace)
htrc("Write error: %s\n", strerror(errno));
return true;
} else
Spos = Fpos + n;
#if defined(UNIX)
fflush(Streams[i]); //NGC
#endif
return false;
} // end of WriteBlock
/* -------------------------- Class VMPFAM --------------------------- */
/***********************************************************************/
/* Implementation of the VMPFAM class. */
/***********************************************************************/
VMPFAM::VMPFAM(PVCTDEF tdp) : VCMFAM((PVCTDEF)tdp)
{
To_Fbs = NULL;
Split = true;
Block = Last = -1;
} // end of VMPFAM standard constructor
VMPFAM::VMPFAM(PVMPFAM txfp) : VCMFAM(txfp)
{
To_Fbs = txfp->To_Fbs;
} // end of VMPFAM copy constructor
/***********************************************************************/
/* VCT Access Method opening routine. */
/* New method now that this routine is called recursively (last table */
/* first in reverse order): index blocks are immediately linked to */
/* join block of next table if it exists or else are discarted. */
/***********************************************************************/
bool VMPFAM::OpenTableFile(PGLOBAL g)
{
int i;
bool b = false;
MODE mode = Tdbp->GetMode();
PCOLDEF cdp;
PVCTCOL cp;
PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
if (mode == MODE_DELETE && !Tdbp->GetNext()) {
DelRows = Cardinality(g);
// This will stop the process by causing GetProgMax to return 0.
ResetTableSize(g, 0, Nrec);
} else
Cardinality(g); // See comment in VECFAM::OpenTbleFile
/*********************************************************************/
/* Prepare the filename pattern for column files and set Ncol. */
/*********************************************************************/
if (!Colfn) {
// Prepare the column file name pattern
Colfn = (char*)PlugSubAlloc(g, NULL, _MAX_PATH);
Ncol = ((VCTDEF*)Tdbp->GetDef())->MakeFnPattern(Colfn);
} // endif Colfn
/*********************************************************************/
/* Initialize the array of file structures. */
/*********************************************************************/
Memcol = (char* *)PlugSubAlloc(g, NULL, Ncol * sizeof(char *));
To_Fbs = (PFBLOCK *)PlugSubAlloc(g, NULL, Ncol * sizeof(PFBLOCK));
for (i = 0; i < Ncol; i++) {
Memcol[i] = NULL;
To_Fbs[i] = NULL;
} // endif i
/*********************************************************************/
/* Open the files corresponding to columns used in the query. */
/*********************************************************************/
if (mode == MODE_DELETE) {
// All columns are used in Delete mode
for (i = 0, cdp = defp->GetCols(); cdp; i++, cdp = cdp->GetNext())
if (MapColumnFile(g, mode, i))
return true;
} else {
/*******************************************************************/
/* Open the files corresponding to updated columns of the query. */
/*******************************************************************/
for (cp = (PVCTCOL)((PTDBVCT)Tdbp)->To_SetCols; cp;
cp = (PVCTCOL)cp->Next)
if (MapColumnFile(g, MODE_UPDATE, cp->Index - 1))
return true;
/*******************************************************************/
/* Open other non already open used columns (except pseudos) */
/*******************************************************************/
for (cp = (PVCTCOL)Tdbp->GetColumns(); cp; cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial() && !Memcol[cp->Index - 1])
if (MapColumnFile(g, MODE_READ, cp->Index - 1))
return true;
// Check for void table or missing columns
for (i = 0, cp = (PVCTCOL)Tdbp->GetColumns(); cp;
cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial()) {
if (!i++)
b = !Memcol[cp->Index - 1];
else if (b != !Memcol[cp->Index - 1])
return true;
} // endif Special
} // endif mode
/*********************************************************************/
/* Allocate the table and column block buffer. */
/*********************************************************************/
return (b) ? false : AllocateBuffer(g);
} // end of OpenTableFile
/***********************************************************************/
/* Open the file corresponding to one column. */
/***********************************************************************/
bool VMPFAM::MapColumnFile(PGLOBAL g, MODE mode, int i)
{
char filename[_MAX_PATH];
int len;
HANDLE hFile;
MEMMAP mm;
PFBLOCK fp;
PDBUSER dup = PlgGetUser(g);
sprintf(filename, Colfn, i+1);
/*********************************************************************/
/* The whole file will be mapped so we can use it as */
/* if it were entirely read into virtual memory. */
/* Firstly we check whether this file have been already mapped. */
/*********************************************************************/
if (mode == MODE_READ) {
for (fp = dup->Openlist; fp; fp = fp->Next)
if (fp->Type == TYPE_FB_MAP && !stricmp(fp->Fname, filename)
&& fp->Count && fp->Mode == mode)
break;
if (trace)
htrc("Mapping file, fp=%p\n", fp);
} else
fp = NULL;
if (fp) {
/*******************************************************************/
/* File already mapped. Just increment use count and get pointer. */
/*******************************************************************/
fp->Count++;
Memcol[i] = fp->Memory;
len = fp->Length;
} else {
/*******************************************************************/
/* Create the mapping file object. */
/*******************************************************************/
hFile = CreateFileMap(g, filename, &mm, mode, DelRows);
if (hFile == INVALID_HANDLE_VALUE) {
DWORD rc = GetLastError();
if (!(*g->Message))
sprintf(g->Message, MSG(OPEN_MODE_ERROR),
"map", (int) rc, filename);
if (trace)
htrc("%s\n", g->Message);
return (mode == MODE_READ && rc == ENOENT)
? PushWarning(g, Tdbp) : true;
} // endif hFile
/*****************************************************************/
/* Get the file size (assuming file is smaller than 4 GB) */
/*****************************************************************/
len = mm.lenL;
Memcol[i] = (char *)mm.memory;
if (!len) { // Empty or deleted file
CloseFileHandle(hFile);
ResetTableSize(g, 0, Nrec);
return false;
} // endif len
if (!Memcol[i]) {
CloseFileHandle(hFile);
sprintf(g->Message, MSG(MAP_VIEW_ERROR),
filename, GetLastError());
return true;
} // endif Memory
if (mode != MODE_DELETE) {
CloseFileHandle(hFile); // Not used anymore
hFile = INVALID_HANDLE_VALUE; // For Fblock
} // endif Mode
/*******************************************************************/
/* Link a Fblock. This make possible to reuse already opened maps */
/* and also to automatically unmap them in case of error g->jump. */
/* Note: block can already exist for previously closed file. */
/*******************************************************************/
fp = (PFBLOCK)PlugSubAlloc(g, NULL, sizeof(FBLOCK));
fp->Type = TYPE_FB_MAP;
fp->Fname = PlugDup(g, filename);
fp->Next = dup->Openlist;
dup->Openlist = fp;
fp->Count = 1;
fp->Length = len;
fp->Memory = Memcol[i];
fp->Mode = mode;
fp->File = NULL;
fp->Handle = hFile; // Used for Delete
} // endif fp
To_Fbs[i] = fp; // Useful when closing
if (trace)
htrc("fp=%p count=%d MapView=%p len=%d\n",
fp, fp->Count, Memcol[i], len);
return false;
} // end of MapColumnFile
/***********************************************************************/
/* Allocate the block buffers for columns used in the query. */
/* Give a dummy value (1) to prevent allocating the value block. */
/* It will be set pointing into the memory map of the file. */
/***********************************************************************/
bool VMPFAM::AllocateBuffer(PGLOBAL g)
{
PVCTCOL cp;
if (Tdbp->GetMode() == MODE_DELETE) {
PCOLDEF cdp = Tdbp->GetDef()->GetCols();
Clens = (int*)PlugSubAlloc(g, NULL, Ncol * sizeof(int));
for (int i = 0; cdp && i < Ncol; i++, cdp = cdp->GetNext())
Clens[i] = cdp->GetClen();
} // endif mode
for (cp = (PVCTCOL)Tdbp->GetColumns(); cp; cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial()) { // Not a pseudo column
cp->Blk = AllocValBlock(g, (void*)1, cp->Buf_Type, Nrec,
cp->Format.Length, cp->Format.Prec);
cp->AddStatus(BUF_MAPPED);
} // endif IsSpecial
return false;
} // end of AllocateBuffer
/***********************************************************************/
/* Data Base delete line routine for VMP access method. */
/* Lines between deleted lines are moved in the mapfile view. */
/***********************************************************************/
int VMPFAM::DeleteRecords(PGLOBAL g, int irc)
{
int i;
int m, n;
if (trace)
htrc("VMP DeleteDB: irc=%d tobuf=%p Tpos=%p Spos=%p\n",
irc, To_Buf, Tpos, Spos);
if (irc != RC_OK) {
/*******************************************************************/
/* EOF: position Fpos at the top of map position. */
/*******************************************************************/
Fpos = (Block - 1) * Nrec + Last;
if (trace)
htrc("Fpos placed at file top=%p\n", Fpos);
} else // Fpos is the Deleted line position
Fpos = CurBlk * Nrec + CurNum;
if (Tpos == Spos) {
/*******************************************************************/
/* First line to delete. Move of eventual preceding lines is */
/* not required here, just setting of future Spos and Tpos. */
/*******************************************************************/
Tpos = Fpos; // Spos is set below
} else if ((n = Fpos - Spos) > 0) {
/*******************************************************************/
/* Non consecutive line to delete. Move intermediate lines. */
/*******************************************************************/
for (i = 0; i < Ncol; i++) {
m = Clens[i];
memmove(Memcol[i] + Tpos * m, Memcol[i] + Spos * m, m * n);
} // endif i
Tpos += n;
if (trace)
htrc("move %d bytes\n", n);
} // endif n
if (irc == RC_OK) {
Spos = Fpos + 1; // New start position
if (trace)
htrc("after: Tpos=%p Spos=%p\n", Tpos, Spos);
} else {
/*******************************************************************/
/* Last call after EOF has been reached. */
/* We must firstly Unmap the view and use the saved file handle */
/* to put an EOF at the end of the copied part of the file. */
/*******************************************************************/
PFBLOCK fp;
/*******************************************************************/
/* Reset the Block and Last values for TDBVCT::MakeBlockValues. */
/*******************************************************************/
// Block = (Tpos > 0) ? (Tpos + Nrec - 1) / Nrec : 0;
// Last = (Tpos + Nrec - 1) % Nrec + 1;
for (i = 0; i < Ncol; i++) {
fp = To_Fbs[i];
CloseMemMap(fp->Memory, (size_t)fp->Length);
fp->Count = 0; // Avoid doing it twice
/*****************************************************************/
/* Remove extra records. */
/*****************************************************************/
n = Tpos * Clens[i];
#if defined(WIN32)
DWORD drc = SetFilePointer(fp->Handle, n, NULL, FILE_BEGIN);
if (drc == 0xFFFFFFFF) {
sprintf(g->Message, MSG(FUNCTION_ERROR),
"SetFilePointer", GetLastError());
CloseHandle(fp->Handle);
return RC_FX;
} // endif
if (trace)
htrc("done, Tpos=%p newsize=%d drc=%d\n", Tpos, n, drc);
if (!SetEndOfFile(fp->Handle)) {
sprintf(g->Message, MSG(FUNCTION_ERROR),
"SetEndOfFile", GetLastError());
CloseHandle(fp->Handle);
return RC_FX;
} // endif
CloseHandle(fp->Handle);
#else // UNIX
if (ftruncate(fp->Handle, (off_t)n)) {
sprintf(g->Message, MSG(TRUNCATE_ERROR), strerror(errno));
close(fp->Handle);
return RC_FX;
} // endif
close(fp->Handle);
#endif // UNIX
} // endfor i
} // endif irc
return RC_OK; // All is correct
} // end of DeleteRecords
/***********************************************************************/
/* Data Base close routine for VMP access method. */
/***********************************************************************/
void VMPFAM::CloseTableFile(PGLOBAL g, bool)
{
if (Tdbp->GetMode() == MODE_DELETE) {
// Set Block and Nrec values for TDBVCT::MakeBlockValues
Block = (Tpos > 0) ? (Tpos + Nrec - 1) / Nrec : 0;
Last = (Tpos + Nrec - 1) % Nrec + 1;
ResetTableSize(g, Block, Last);
} else if (Tdbp->GetMode() == MODE_INSERT)
assert(false);
for (int i = 0; i < Ncol; i++)
PlugCloseFile(g, To_Fbs[i]);
} // end of CloseTableFile
/* -------------------------- Class BGVFAM --------------------------- */
/***********************************************************************/
/* Implementation of the BGVFAM class. */
/***********************************************************************/
// Constructors
BGVFAM::BGVFAM(PVCTDEF tdp) : VCTFAM(tdp)
{
Hfile = INVALID_HANDLE_VALUE;
Tfile = INVALID_HANDLE_VALUE;
BigDep = NULL;
} // end of BGVFAM constructor
BGVFAM::BGVFAM(PBGVFAM txfp) : VCTFAM(txfp)
{
Hfile = txfp->Hfile;
Tfile = txfp->Tfile;
BigDep= txfp->BigDep;
} // end of BGVFAM copy constructor
/***********************************************************************/
/* Set current position in a big file. */
/***********************************************************************/
bool BGVFAM::BigSeek(PGLOBAL g, HANDLE h, BIGINT pos, bool b)
{
#if defined(WIN32)
char buf[256];
DWORD drc, m = (b) ? FILE_END : FILE_BEGIN;
LARGE_INTEGER of;
of.QuadPart = pos;
of.LowPart = SetFilePointer(h, of.LowPart, &of.HighPart, m);
if (of.LowPart == INVALID_SET_FILE_POINTER &&
(drc = GetLastError()) != NO_ERROR) {
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
(LPTSTR)buf, sizeof(buf), NULL);
sprintf(g->Message, MSG(SFP_ERROR), buf);
return true;
} // endif
#else // !WIN32
if (lseek64(h, pos, (b) ? SEEK_END : SEEK_SET) < 0) {
sprintf(g->Message, MSG(ERROR_IN_LSK), errno);
return true;
} // endif
#endif // !WIN32
return false;
} // end of BigSeek
/***********************************************************************/
/* Read from a big file. */
/***********************************************************************/
bool BGVFAM::BigRead(PGLOBAL g, HANDLE h, void *inbuf, int req)
{
bool rc = false;
#if defined(WIN32)
DWORD nbr, drc, len = (DWORD)req;
bool brc = ReadFile(h, inbuf, len, &nbr, NULL);
if (trace)
htrc("after read req=%d brc=%d nbr=%d\n", req, brc, nbr);
if (!brc || nbr != len) {
char buf[256]; // , *fn = (h == Hfile) ? To_File : "Tempfile";
if (brc)
strcpy(buf, MSG(BAD_BYTE_READ));
else {
drc = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
(LPTSTR)buf, sizeof(buf), NULL);
} // endelse brc
sprintf(g->Message, MSG(READ_ERROR), To_File, buf);
if (trace)
htrc("BIGREAD: %s\n", g->Message);
rc = true;
} // endif brc || nbr
#else // !WIN32
size_t len = (size_t)req;
ssize_t nbr = read(h, inbuf, len);
if (nbr != (ssize_t)len) {
const char *fn = (h == Hfile) ? To_File : "Tempfile";
sprintf(g->Message, MSG(READ_ERROR), fn, strerror(errno));
if (trace)
htrc("BIGREAD: nbr=%d len=%d errno=%d %s\n",
nbr, len, errno, g->Message);
rc = true;
} // endif nbr
#endif // !WIN32
return rc;
} // end of BigRead
/***********************************************************************/
/* Write into a big file. */
/***********************************************************************/
bool BGVFAM::BigWrite(PGLOBAL g, HANDLE h, void *inbuf, int req)
{
bool rc = false;
#if defined(WIN32)
DWORD nbw, drc, len = (DWORD)req;
bool brc = WriteFile(h, inbuf, len, &nbw, NULL);
if (trace)
htrc("after write req=%d brc=%d nbw=%d\n", req, brc, nbw);
if (!brc || nbw != len) {
char buf[256], *fn = (h == Hfile) ? To_File : "Tempfile";
if (brc)
strcpy(buf, MSG(BAD_BYTE_NUM));
else {
drc = GetLastError();
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, drc, 0,
(LPTSTR)buf, sizeof(buf), NULL);
} // endelse brc
sprintf(g->Message, MSG(WRITE_STRERROR), fn, buf);
if (trace)
htrc("BIGWRITE: nbw=%d len=%d errno=%d %s\n",
nbw, len, drc, g->Message);
rc = true;
} // endif brc || nbw
#else // !WIN32
size_t len = (size_t)req;
ssize_t nbw = write(h, inbuf, len);
if (nbw != (ssize_t)len) {
const char *fn = (h == Hfile) ? To_File : "Tempfile";
sprintf(g->Message, MSG(WRITE_STRERROR), fn, strerror(errno));
if (trace)
htrc("BIGWRITE: nbw=%d len=%d errno=%d %s\n",
nbw, len, errno, g->Message);
rc = true;
} // endif nbr
#endif // !WIN32
return rc;
} // end of BigWrite
/***********************************************************************/
/* Get the Headlen, Block and Last info from the file header. */
/***********************************************************************/
int BGVFAM::GetBlockInfo(PGLOBAL g)
{
char filename[_MAX_PATH];
int n;
VECHEADER vh;
HANDLE h;
if (Header < 1 || Header > 3 || !MaxBlk) {
sprintf(g->Message, "Invalid header value %d", Header);
return -1;
} else
n = (Header == 1) ? (int)sizeof(VECHEADER) : 0;
PlugSetPath(filename, To_File, Tdbp->GetPath());
if (Header == 2)
strcat(PlugRemoveType(filename, filename), ".blk");
#if defined(WIN32)
LARGE_INTEGER len;
h = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h != INVALID_HANDLE_VALUE) {
// Get the size of the file (can be greater than 4 GB)
len.LowPart = GetFileSize(h, (LPDWORD)&len.HighPart);
} // endif h
if (h == INVALID_HANDLE_VALUE || !len.QuadPart) {
#else // !WIN32
h = open64(filename, O_RDONLY, 0);
if (h == INVALID_HANDLE_VALUE || !_filelength(h)) {
#endif // !WIN32
// Consider this is a void table
if (trace)
htrc("Void table h=%d\n", h);
Last = Nrec;
Block = 0;
if (h != INVALID_HANDLE_VALUE)
CloseFileHandle(h);
return n;
} else if (Header == 3)
/*b = */ BigSeek(g, h, -(BIGINT)sizeof(vh), true);
if (BigRead(g, h, &vh, sizeof(vh))) {
sprintf(g->Message, "Error reading header file %s", filename);
n = -1;
} else if (MaxBlk * Nrec != vh.MaxRec) {
sprintf(g->Message, "MaxRec=%d doesn't match MaxBlk=%d Nrec=%d",
vh.MaxRec, MaxBlk, Nrec);
n = -1;
} else {
Block = (vh.NumRec > 0) ? (vh.NumRec + Nrec - 1) / Nrec : 0;
Last = (vh.NumRec + Nrec - 1) % Nrec + 1;
if (trace)
htrc("Block=%d Last=%d\n", Block, Last);
} // endif's
CloseFileHandle(h);
return n;
} // end of GetBlockInfo
/***********************************************************************/
/* Set the MaxRec and NumRec info in the file header. */
/***********************************************************************/
bool BGVFAM::SetBlockInfo(PGLOBAL g)
{
char filename[_MAX_PATH];
bool b = false, rc = false;
VECHEADER vh;
HANDLE h = INVALID_HANDLE_VALUE;
PlugSetPath(filename, To_File, Tdbp->GetPath());
if (Header != 2) {
if (Hfile != INVALID_HANDLE_VALUE) {
h = Hfile;
if (Header == 1)
/*bk =*/ BigSeek(g, h, (BIGINT)0);
} else
b = true;
} else // Header == 2
strcat(PlugRemoveType(filename, filename), ".blk");
if (h == INVALID_HANDLE_VALUE) {
#if defined(WIN32)
DWORD creation = (b) ? OPEN_EXISTING : TRUNCATE_EXISTING;
h = CreateFile(filename, GENERIC_READ | GENERIC_WRITE, 0,
NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
#else // !WIN32
int oflag = (b) ? O_RDWR : O_RDWR | O_TRUNC;
h = open64(filename, oflag, 0);
#endif // !WIN32
if (h == INVALID_HANDLE_VALUE) {
sprintf(g->Message, "Error opening header file %s", filename);
return true;
} // endif h
} // endif h
if (Header == 3)
/*bk =*/ BigSeek(g, h, -(BIGINT)sizeof(vh), true);
vh.MaxRec = MaxBlk * Bsize;
vh.NumRec = (Block - 1) * Nrec + Last;
if (BigWrite(g, h, &vh, sizeof(vh))) {
sprintf(g->Message, "Error writing header file %s", filename);
rc = true;
} // endif fread
if (Header == 2 || Hfile == INVALID_HANDLE_VALUE)
CloseFileHandle(h);
return rc;
} // end of SetBlockInfo
/***********************************************************************/
/* VEC Create an empty file for new Vector formatted tables. */
/***********************************************************************/
bool BGVFAM::MakeEmptyFile(PGLOBAL g, char *fn)
{
// Vector formatted file this will create an empty file of the
// required length if it does not exists yet.
char filename[_MAX_PATH], c = 0;
int n = (Header == 1 || Header == 3) ? sizeof(VECHEADER) : 0;
PlugSetPath(filename, fn, Tdbp->GetPath());
#if defined(WIN32)
char *p;
DWORD rc;
bool brc;
LARGE_INTEGER of;
HANDLE h;
h = CreateFile(filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) {
p = MSG(OPENING);
goto err;
} // endif h
of.QuadPart = (BIGINT)n + (BIGINT)MaxBlk * (BIGINT)Blksize - (BIGINT)1;
if (trace)
htrc("MEF: of=%lld n=%d maxblk=%d blksize=%d\n",
of.QuadPart, n, MaxBlk, Blksize);
of.LowPart = SetFilePointer(h, of.LowPart,
&of.HighPart, FILE_BEGIN);
if (of.LowPart == INVALID_SET_FILE_POINTER &&
GetLastError() != NO_ERROR) {
p = MSG(MAKING);
goto err;
} // endif
brc = WriteFile(h, &c, 1, &rc, NULL);
if (!brc || rc != 1) {
p = MSG(WRITING);
goto err;
} // endif
CloseHandle(h);
return false;
err:
rc = GetLastError();
sprintf(g->Message, MSG(EMPTY_FILE), p, filename);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
(LPTSTR)filename, sizeof(filename), NULL);
strcat(g->Message, filename);
if (h != INVALID_HANDLE_VALUE)
CloseHandle(h);
return true;
#else // !WIN32
int h;
BIGINT pos;
h= open64(filename, O_CREAT | O_WRONLY, S_IREAD | S_IWRITE);
if (h == -1)
return true;
pos = (BIGINT)n + (BIGINT)MaxBlk * (BIGINT)Blksize - (BIGINT)1;
if (trace)
htrc("MEF: pos=%lld n=%d maxblk=%d blksize=%d\n",
pos, n, MaxBlk, Blksize);
if (lseek64(h, pos, SEEK_SET) < 0)
goto err;
// This actually fills the empty file
if (write(h, &c, 1) < 0)
goto err;
close(h);
return false;
err:
sprintf(g->Message, MSG(MAKE_EMPTY_FILE), To_File, strerror(errno));
close(h);
return true;
#endif // !WIN32
} // end of MakeEmptyFile
/***********************************************************************/
/* Vopen function: opens a file using Windows or Unix API's. */
/***********************************************************************/
bool BGVFAM::OpenTableFile(PGLOBAL g)
{
char filename[_MAX_PATH];
bool del = false;
MODE mode = Tdbp->GetMode();
PDBUSER dbuserp = PlgGetUser(g);
if ((To_Fb && To_Fb->Count) || Hfile != INVALID_HANDLE_VALUE) {
sprintf(g->Message, MSG(FILE_OPEN_YET), To_File);
return true;
} // endif
/*********************************************************************/
/* Update block info if necessary. */
/*********************************************************************/
if (Block < 0)
if ((Headlen = GetBlockInfo(g)) < 0)
return true;
PlugSetPath(filename, To_File, Tdbp->GetPath());
if (trace)
htrc("OpenTableFile: filename=%s mode=%d Last=%d\n",
filename, mode, Last);
#if defined(WIN32)
DWORD access, creation, share = 0, rc = 0;
/*********************************************************************/
/* Create the file object according to access mode */
/*********************************************************************/
switch (mode) {
case MODE_READ:
access = GENERIC_READ;
share = FILE_SHARE_READ;
creation = OPEN_EXISTING;
break;
case MODE_INSERT:
if (MaxBlk) {
if (!Block)
if (MakeEmptyFile(g, To_File))
return true;
// Required to update empty blocks
access = GENERIC_READ | GENERIC_WRITE;
} else if (Last == Nrec)
access = GENERIC_WRITE;
else
// Required to update the last block
access = GENERIC_READ | GENERIC_WRITE;
creation = OPEN_ALWAYS;
break;
case MODE_DELETE:
if (!Tdbp->GetNext()) {
// Store the number of deleted lines
DelRows = Cardinality(g);
// This will stop the process by
// causing GetProgMax to return 0.
// ResetTableSize(g, 0, Nrec); must be done later
del = true;
// This will delete the whole file
access = GENERIC_READ | GENERIC_WRITE;
creation = TRUNCATE_EXISTING;
break;
} // endif
// Selective delete, pass thru
case MODE_UPDATE:
if ((UseTemp = Tdbp->IsUsingTemp(g)))
access = GENERIC_READ;
else
access = GENERIC_READ | GENERIC_WRITE;
creation = OPEN_EXISTING;
break;
default:
sprintf(g->Message, MSG(BAD_OPEN_MODE), mode);
return true;
} // endswitch
/*********************************************************************/
/* Use specific Windows API functions. */
/*********************************************************************/
Hfile = CreateFile(filename, access, share, NULL, creation,
FILE_ATTRIBUTE_NORMAL, NULL);
if (Hfile == INVALID_HANDLE_VALUE) {
rc = GetLastError();
sprintf(g->Message, MSG(OPEN_ERROR), rc, mode, filename);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
(LPTSTR)filename, sizeof(filename), NULL);
strcat(g->Message, filename);
} // endif Hfile
if (trace)
htrc(" rc=%d access=%p share=%p creation=%d handle=%p fn=%s\n",
rc, access, share, creation, Hfile, filename);
if (mode == MODE_INSERT) {
/*******************************************************************/
/* In Insert mode we must position the cursor at end of file. */
/*******************************************************************/
LARGE_INTEGER of;
of.QuadPart = (BIGINT)0;
of.LowPart = SetFilePointer(Hfile, of.LowPart,
&of.HighPart, FILE_END);
if (of.LowPart == INVALID_SET_FILE_POINTER &&
(rc = GetLastError()) != NO_ERROR) {
sprintf(g->Message, MSG(ERROR_IN_SFP), rc);
CloseHandle(Hfile);
Hfile = INVALID_HANDLE_VALUE;
} // endif
} // endif Mode
#else // UNIX
/*********************************************************************/
/* The open() function has a transitional interface for 64-bit */
/* file offsets. Note that using open64() is equivalent to using */
/* open() with O_LARGEFILE set in oflag (see Xopen in tabfix.cpp). */
/*********************************************************************/
int rc = 0;
int oflag;
mode_t pmd = 0;
/*********************************************************************/
/* Create the file object according to access mode */
/*********************************************************************/
switch (mode) {
case MODE_READ:
oflag = O_RDONLY;
break;
case MODE_INSERT:
if (MaxBlk) {
if (!Block)
if (MakeEmptyFile(g, To_File))
return true;
// Required to update empty blocks
oflag = O_RDWR;
} else if (Last == Nrec)
oflag = O_WRONLY | O_CREAT | O_APPEND;
else
// Required to update the last block
oflag = O_RDWR | O_CREAT | O_APPEND;
pmd = S_IREAD | S_IWRITE;
break;
case MODE_DELETE:
// This is temporary until a partial delete is implemented
if (!Tdbp->GetNext()) {
// Store the number of deleted lines
DelRows = Cardinality(g);
del = true;
// This will delete the whole file and provoque ReadDB to
// return immediately.
oflag = O_RDWR | O_TRUNC;
strcpy(g->Message, MSG(NO_VCT_DELETE));
break;
} // endif
// Selective delete, pass thru
case MODE_UPDATE:
UseTemp = Tdbp->IsUsingTemp(g);
oflag = (UseTemp) ? O_RDONLY : O_RDWR;
break;
default:
sprintf(g->Message, MSG(BAD_OPEN_MODE), mode);
return true;
} // endswitch
Hfile = open64(filename, oflag, pmd); // Enable file size > 2G
if (Hfile == INVALID_HANDLE_VALUE) {
rc = errno;
sprintf(g->Message, MSG(OPEN_ERROR), rc, mode, filename);
strcat(g->Message, strerror(errno));
} // endif Hfile
if (trace)
htrc(" rc=%d oflag=%p mode=%p handle=%d fn=%s\n",
rc, oflag, mode, Hfile, filename);
#endif // UNIX
if (!rc) {
if (!To_Fb) {
To_Fb = (PFBLOCK)PlugSubAlloc(g, NULL, sizeof(FBLOCK));
To_Fb->Fname = To_File;
To_Fb->Type = TYPE_FB_HANDLE;
To_Fb->Memory = NULL;
To_Fb->Length = 0;
To_Fb->File = NULL;
To_Fb->Next = dbuserp->Openlist;
dbuserp->Openlist = To_Fb;
} // endif To_Fb
To_Fb->Count = 1;
To_Fb->Mode = mode;
To_Fb->Handle = Hfile;
if (trace)
htrc("File %s is open in mode %d\n", filename, mode);
if (del)
// This will stop the process by
// causing GetProgMax to return 0.
return ResetTableSize(g, 0, Nrec);
/*********************************************************************/
/* Allocate the table and column block buffers. */
/*********************************************************************/
return AllocateBuffer(g);
} else
return (mode == MODE_READ && rc == ENOENT)
? PushWarning(g, Tdbp) : true;
} // end of OpenTableFile
/***********************************************************************/
/* Allocate the block buffers for columns used in the query. */
/***********************************************************************/
bool BGVFAM::AllocateBuffer(PGLOBAL g)
{
MODE mode = Tdbp->GetMode();
PDOSDEF defp = (PDOSDEF)Tdbp->GetDef();
PCOLDEF cdp;
PVCTCOL cp = (PVCTCOL)Tdbp->GetColumns();
if (mode == MODE_INSERT) {
if (!NewBlock) {
// Not reopening after inserting the last block
bool chk = PlgGetUser(g)->Check & CHK_TYPE;
NewBlock = (char*)PlugSubAlloc(g, NULL, Blksize);
for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
memset(NewBlock + Nrec * cdp->GetPoff(),
(IsTypeNum(cdp->GetType()) ? 0 : ' '),
Nrec * cdp->GetClen());
for (; cp; cp = (PVCTCOL)cp->Next)
cp->Blk = AllocValBlock(g, NewBlock + Nrec * cp->Deplac,
cp->Buf_Type, Nrec, cp->Format.Length,
cp->Format.Prec, chk);
InitInsert(g); // Initialize inserting
// Currently we don't use a temporary file for inserting
Tfile = Hfile;
} // endif NewBlock
} else {
if (UseTemp || mode == MODE_DELETE) {
// Allocate all that is needed to move lines
int i = 0;
if (!Ncol)
for (cdp = defp->GetCols(); cdp; cdp = cdp->GetNext())
Ncol++;
if (MaxBlk)
BigDep = (BIGINT*)PlugSubAlloc(g, NULL, Ncol * sizeof(BIGINT));
else
Deplac = (int*)PlugSubAlloc(g, NULL, Ncol * sizeof(int));
Clens = (int*)PlugSubAlloc(g, NULL, Ncol * sizeof(int));
Isnum = (bool*)PlugSubAlloc(g, NULL, Ncol * sizeof(bool));
for (cdp = defp->GetCols(); cdp; i++, cdp = cdp->GetNext()) {
if (MaxBlk)
BigDep[i] = (BIGINT)Headlen
+ (BIGINT)(cdp->GetPoff() * Nrec) * (BIGINT)MaxBlk;
else
Deplac[i] = cdp->GetPoff() * Nrec;
Clens[i] = cdp->GetClen();
Isnum[i] = IsTypeNum(cdp->GetType());
Buflen = MY_MAX(Buflen, cdp->GetClen());
} // endfor cdp
if (!UseTemp || MaxBlk) {
Buflen *= Nrec;
To_Buf = (char*)PlugSubAlloc(g, NULL, Buflen);
} else
NewBlock = (char*)PlugSubAlloc(g, NULL, Blksize);
} // endif mode
for (; cp; cp = (PVCTCOL)cp->Next)
if (!cp->IsSpecial()) // Not a pseudo column
cp->Blk = AllocValBlock(g, NULL, cp->Buf_Type, Nrec,
cp->Format.Length, cp->Format.Prec);
} //endif mode
return false;
} // end of AllocateBuffer
/***********************************************************************/
/* Data Base write routine for huge VCT access method. */
/***********************************************************************/
int BGVFAM::WriteBuffer(PGLOBAL g)
{
if (trace)
htrc("BGV WriteDB: R%d Mode=%d CurNum=%d CurBlk=%d\n",
Tdbp->GetTdb_No(), Tdbp->GetMode(), CurNum, CurBlk);
if (Tdbp->GetMode() == MODE_UPDATE) {
// Mode Update is done in ReadDB, we just initialize it here
if (Tfile == INVALID_HANDLE_VALUE) {
if (UseTemp) {
if (OpenTempFile(g))
return RC_FX;
// Most of the time, not all table columns are updated.
// This why we must completely pre-fill the temporary file.
Fpos = (MaxBlk) ? (Block - 1) * Nrec + Last
: Block * Nrec; // To write last lock
if (MoveIntermediateLines(g))
return RC_FX;
} else
Tfile = Hfile;
} // endif Tfile
} else {
// Mode Insert
if (MaxBlk && CurBlk == MaxBlk) {
strcpy(g->Message, MSG(TRUNC_BY_ESTIM));
return RC_EF; // Too many lines for a Vector formatted table
} // endif MaxBlk
if (Closing || ++CurNum == Nrec) {
PVCTCOL cp = (PVCTCOL)Tdbp->GetColumns();
if (!AddBlock) {
// Write back the updated last block values
for (; cp; cp = (PVCTCOL)cp->Next)
cp->WriteBlock(g);
if (!Closing && !MaxBlk) {
// Close the VCT file and reopen it in mode Insert
//#if defined(WIN32) //OB
// CloseHandle(Hfile);
//#else // UNIX
// close(Hfile);
//#endif // UNIX
CloseFileHandle(Hfile);
Hfile = INVALID_HANDLE_VALUE;
To_Fb->Count = 0;
Last = Nrec; // Tested in OpenTableFile
if (OpenTableFile(g)) {
Closing = true; // Tell CloseDB of error
return RC_FX;
} // endif Vopen
AddBlock = true;
} // endif Closing
} else {
// Here we must add a new block to the VCT file
if (Closing)
// Reset the overwritten columns for last block extra records
for (; cp; cp = (PVCTCOL)cp->Next)
memset(NewBlock + Nrec * cp->Deplac + Last * cp->Clen,
(cp->Buf_Type == TYPE_STRING) ? ' ' : '\0',
(Nrec - Last) * cp->Clen);
if (BigWrite(g, Hfile, NewBlock, Blksize))
return RC_FX;
} // endif AddBlock
if (!Closing) {
CurBlk++;
CurNum = 0;
} // endif Closing
} // endif
} // endif Mode
return RC_OK;
} // end of WriteBuffer
/***********************************************************************/
/* Data Base delete line routine for BGVFAM access method. */
/***********************************************************************/
int BGVFAM::DeleteRecords(PGLOBAL g, int irc)
{
bool eof = false;
/*********************************************************************/
/* There is an alternative here depending on UseTemp: */
/* 1 - use a temporary file in which are copied all not deleted */
/* lines, at the end the original file will be deleted and */
/* the temporary file renamed to the original file name. */
/* 2 - directly move the not deleted lines inside the original */
/* file, and at the end erase all trailing records. */
/*********************************************************************/
if (trace)
htrc("BGV DeleteDB: irc=%d UseTemp=%d Fpos=%d Tpos=%d Spos=%d\n",
irc, UseTemp, Fpos, Tpos, Spos);
if (irc != RC_OK) {
/*******************************************************************/
/* EOF: position Fpos at the end-of-file position. */
/*******************************************************************/
Fpos = (Block - 1) * Nrec + Last;
if (trace)
htrc("Fpos placed at file end=%d\n", Fpos);
eof = UseTemp && !MaxBlk;
} else // Fpos is the deleted line position
Fpos = CurBlk * Nrec + CurNum;
if (Tpos == Spos) {
if (UseTemp) {
/*****************************************************************/
/* Open the temporary file, Spos is at the beginning of file. */
/*****************************************************************/
if (OpenTempFile(g))
return RC_FX;
} else {
/*****************************************************************/
/* Move of eventual preceding lines is not required here. */
/* Set the target file as being the source file itself. */
/* Set the future Tpos, and give Spos a value to block copying. */
/*****************************************************************/
Tfile = Hfile;
Spos = Tpos = Fpos;
} // endif UseTemp
} // endif Tpos == Spos
/*********************************************************************/
/* Move any intermediate lines. */
/*********************************************************************/
if (MoveIntermediateLines(g, &eof))
return RC_FX;
if (irc == RC_OK) {
#ifdef _DEBUG
assert(Spos == Fpos);
#endif
Spos++; // New start position is on next line
if (trace)
htrc("after: Tpos=%d Spos=%d\n", Tpos, Spos);
} else {
/*******************************************************************/
/* Last call after EOF has been reached. */
/*******************************************************************/
Block = (Tpos > 0) ? (Tpos + Nrec - 1) / Nrec : 0;
Last = (Tpos + Nrec - 1) % Nrec + 1;
if (!UseTemp) { // The UseTemp case is treated in CloseTableFile
if (!MaxBlk) {
if (Last < Nrec) // Clean last block
if (CleanUnusedSpace(g))
return RC_FX;
/***************************************************************/
/* Remove extra records. */
/***************************************************************/
#if defined(WIN32)
BIGINT pos = (BIGINT)Block * (BIGINT)Blksize;
if (BigSeek(g, Hfile, pos))
return RC_FX;
if (!SetEndOfFile(Hfile)) {
DWORD drc = GetLastError();
sprintf(g->Message, MSG(SETEOF_ERROR), drc);
return RC_FX;
} // endif error
#else // !WIN32
if (ftruncate64(Hfile, (BIGINT)(Tpos * Lrecl))) {
sprintf(g->Message, MSG(TRUNCATE_ERROR), strerror(errno));
return RC_FX;
} // endif
#endif // !WIN32
} else // MaxBlk
// Clean the unused space in the file, this is required when
// inserting again with a partial column list.
if (CleanUnusedSpace(g))
return RC_FX;
if (ResetTableSize(g, Block, Last))
return RC_FX;
} // endif UseTemp
} // endif irc
return RC_OK; // All is correct
} // end of DeleteRecords
/***********************************************************************/
/* Open a temporary file used while updating or deleting. */
/***********************************************************************/
bool BGVFAM::OpenTempFile(PGLOBAL g)
{
char *tempname;
PDBUSER dup = PlgGetUser(g);
/*********************************************************************/
/* Open the temporary file, Spos is at the beginning of file. */
/*********************************************************************/
tempname = (char*)PlugSubAlloc(g, NULL, _MAX_PATH);
PlugSetPath(tempname, To_File, Tdbp->GetPath());
strcat(PlugRemoveType(tempname, tempname), ".t");
if (!MaxBlk)
remove(tempname); // Be sure it does not exist yet
else if (MakeEmptyFile(g, tempname))
return true;
#if defined(WIN32)
DWORD access = (MaxBlk) ? OPEN_EXISTING : CREATE_NEW;
Tfile = CreateFile(tempname, GENERIC_WRITE, 0, NULL,
access, FILE_ATTRIBUTE_NORMAL, NULL);
if (Tfile == INVALID_HANDLE_VALUE) {
DWORD rc = GetLastError();
sprintf(g->Message, MSG(OPEN_ERROR), rc, MODE_DELETE, tempname);
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, rc, 0,
(LPTSTR)tempname, _MAX_PATH, NULL);
strcat(g->Message, tempname);
return true;
} // endif Tfile
#else // UNIX
int oflag = (MaxBlk) ? O_WRONLY : O_WRONLY | O_TRUNC;
Tfile = open64(tempname, oflag, S_IWRITE);
if (Tfile == INVALID_HANDLE_VALUE) {
int rc = errno;
sprintf(g->Message, MSG(OPEN_ERROR), rc, MODE_INSERT, tempname);
strcat(g->Message, strerror(errno));
return true;
} //endif Tfile
#endif // UNIX
To_Fbt = (PFBLOCK)PlugSubAlloc(g, NULL, sizeof(FBLOCK));
To_Fbt->Fname = tempname;
To_Fbt->Type = TYPE_FB_HANDLE;
To_Fbt->Memory = NULL;
To_Fbt->Length = 0;
To_Fbt->File = NULL;
To_Fbt->Next = dup->Openlist;
To_Fbt->Count = 1;
To_Fbt->Mode = MODE_INSERT;
To_Fbt->Handle = Tfile;
dup->Openlist = To_Fbt;
return false;
} // end of OpenTempFile
/***********************************************************************/
/* Move intermediate deleted or updated lines. */
/***********************************************************************/
bool BGVFAM::MoveIntermediateLines(PGLOBAL g, bool *b)
{
int i, n, req, dep;
bool eof = (b) ? *b : false;
BIGINT pos;
for (n = Fpos - Spos; n > 0 || eof; n -= req) {
/*******************************************************************/
/* Non consecutive line to delete. Move intermediate lines. */
/*******************************************************************/
if (!MaxBlk)
req = (DWORD)MY_MIN(n, Nrec - MY_MAX(Spos % Nrec, Tpos % Nrec));
else
req = (DWORD)MY_MIN(n, Nrec);
if (req) for (i = 0; i < Ncol; i++) {
if (!MaxBlk) {
if (UseTemp)
To_Buf = NewBlock + Deplac[i] + (Tpos % Nrec) * Clens[i];
pos = (BIGINT)Deplac[i] + (BIGINT)((Spos % Nrec) * Clens[i])
+ (BIGINT)(Spos / Nrec) * (BIGINT)Blksize;
} else
pos = BigDep[i] + (BIGINT)Spos * (BIGINT)Clens[i];
if (BigSeek(g, Hfile, pos))
return true;
if (BigRead(g, Hfile, To_Buf, req * Clens[i]))
return true;
if (!UseTemp || MaxBlk) {
if (!MaxBlk)
pos = (BIGINT)Deplac[i] + (BIGINT)((Tpos % Nrec) * Clens[i])
+ (BIGINT)(Tpos / Nrec) * (BIGINT)Blksize;
else
pos = BigDep[i] + (BIGINT)Tpos * (BIGINT)Clens[i];
if (BigSeek(g, Tfile, pos))
return true;
if (BigWrite(g, Tfile, To_Buf, req * Clens[i]))
return true;
} // endif UseTemp
} // endfor i
Tpos += (int)req;
Spos += (int)req;
if (UseTemp && !MaxBlk && (!(Tpos % Nrec) || (eof && Spos == Fpos))) {
// Write the full or last block to the temporary file
if ((dep = Nrec - (Tpos % Nrec)) < Nrec)
// Clean the last block in case of future insert, must be
// done here because Tfile was open in write only.
for (i = 0; i < Ncol; i++) {
To_Buf = NewBlock + Deplac[i] + (Tpos % Nrec) * Clens[i];
memset(To_Buf, (Isnum[i]) ? 0 : ' ', dep * Clens[i]);
} // endfor i
if (BigWrite(g, Tfile, NewBlock, Blksize))
return true;
if (Spos == Fpos)
eof = false;
} // endif Usetemp...
if (trace)
htrc("loop: Tpos=%d Spos=%d\n", Tpos, Spos);
} // endfor n
return false;
} // end of MoveIntermediateLines
/***********************************************************************/
/* Clean deleted space in a huge VCT or Vec table file. */
/***********************************************************************/
bool BGVFAM::CleanUnusedSpace(PGLOBAL g)
{
int i;
int n;
BIGINT pos, dep;
if (!MaxBlk) {
/*******************************************************************/
/* Clean last block of the VCT table file. */
/*******************************************************************/
assert(!UseTemp); // This case is handled in MoveIntermediateLines
if (!(n = Nrec - Last))
return false;
dep = (BIGINT)((Block - 1) * Blksize);
for (i = 0; i < Ncol; i++) {
memset(To_Buf, (Isnum[i]) ? 0 : ' ', n * Clens[i]);
pos = dep + (BIGINT)(Deplac[i] + Last * Clens[i]);
if (BigSeek(g, Hfile, pos))
return true;
if (BigWrite(g, Hfile, To_Buf, n * Clens[i]))
return true;
} // endfor i
} else {
int req;
memset(To_Buf, 0, Buflen);
for (n = Fpos - Tpos; n > 0; n -= req) {
/*****************************************************************/
/* Fill VEC file remaining lines with 0's. */
/* This seems to work even column blocks have been made with */
/* Blanks = true. Perhaps should it be set to false for VEC. */
/*****************************************************************/
req = MY_MIN(n, Nrec);
for (i = 0; i < Ncol; i++) {
pos = BigDep[i] + (BIGINT)Tpos * (BIGINT)Clens[i];
if (BigSeek(g, Tfile, pos))
return true;
if (BigWrite(g, Tfile, To_Buf, req * Clens[i]))
return true;
} // endfor i
Tpos += req;
} // endfor n
} // endif MaxBlk
return false;
} // end of CleanUnusedSpace
/***********************************************************************/
/* Data Base close routine for huge VEC access method. */
/***********************************************************************/
void BGVFAM::CloseTableFile(PGLOBAL g, bool abort)
{
int rc = 0, wrc = RC_OK;
MODE mode = Tdbp->GetMode();
Abort = abort;
if (mode == MODE_INSERT) {
if (Closing)
wrc = RC_FX; // Last write was in error
else
if (CurNum) {
// Some more inserted lines remain to be written
Last = CurNum;
Block = CurBlk + 1;
Closing = true;
wrc = WriteBuffer(g);
} else {
Last = Nrec;
Block = CurBlk;
wrc = RC_OK;
} // endif CurNum
if (wrc != RC_FX) {
rc = ResetTableSize(g, Block, Last);
} else if (AddBlock) {
// Last block was not written
rc = ResetTableSize(g, CurBlk, Nrec);
longjmp(g->jumper[g->jump_level], 44);
} // endif
} else if (mode == MODE_UPDATE) {
// Write back to file any pending modifications
for (PVCTCOL colp = (PVCTCOL)((PTDBVCT)Tdbp)->GetSetCols();
colp; colp = (PVCTCOL)colp->Next)
colp->WriteBlock(g);
if (UseTemp && Tfile) {
rc = RenameTempFile(g);
Hfile = Tfile = INVALID_HANDLE_VALUE;
if (Header)
// Header must be set because it was not set in temp file
rc = SetBlockInfo(g);
} // endif UseTemp
} else if (mode == MODE_DELETE && UseTemp && Tfile) {
if (MaxBlk)
rc = CleanUnusedSpace(g);
if ((rc = RenameTempFile(g)) != RC_FX) {
Hfile = Tfile = INVALID_HANDLE_VALUE; // For SetBlockInfo
rc = ResetTableSize(g, Block, Last);
} // endif rc
} // endif's mode
if (Hfile != INVALID_HANDLE_VALUE)
rc = PlugCloseFile(g, To_Fb);
if (trace)
htrc("BGV CloseTableFile: closing %s wrc=%d rc=%d\n",
To_File, wrc, rc);
Hfile = INVALID_HANDLE_VALUE;
} // end of CloseDB
/***********************************************************************/
/* Rewind routine for huge VCT access method. */
/***********************************************************************/
void BGVFAM::Rewind(void)
{
// In mode update we need to read Set Column blocks
if (Tdbp->GetMode() == MODE_UPDATE)
OldBlk = -1;
// Initialize so block optimization is called for 1st block
CurBlk = -1;
CurNum = Nrec - 1;
#if 0 // This is probably unuseful as the file is directly accessed
#if defined(WIN32) //OB
SetFilePointer(Hfile, 0, NULL, FILE_BEGIN);
#else // UNIX
lseek64(Hfile, 0, SEEK_SET);
#endif // UNIX
#endif // 0
} // end of Rewind
/***********************************************************************/
/* ReadBlock: Read column values from current block. */
/***********************************************************************/
bool BGVFAM::ReadBlock(PGLOBAL g, PVCTCOL colp)
{
BIGINT pos;
/*********************************************************************/
/* Calculate the offset and size of the block to read. */
/*********************************************************************/
if (MaxBlk) // File has Vector format
pos = (BIGINT)Nrec * ((BIGINT)colp->Deplac * (BIGINT)MaxBlk
+ (BIGINT)colp->Clen * (BIGINT)CurBlk) + (BIGINT)Headlen;
else // Old VCT format
pos = (BIGINT)Nrec * ((BIGINT)colp->Deplac
+ (BIGINT)Lrecl * (BIGINT)CurBlk);
if (trace)
htrc("RB: offset=%lld Nrec=%d Deplac=%d Lrecl=%d CurBlk=%d MaxBlk=%d\n",
pos, Nrec, colp->Deplac, Lrecl, CurBlk, MaxBlk);
if (BigSeek(g, Hfile, pos))
return true;
if (BigRead(g, Hfile, colp->Blk->GetValPointer(), colp->Clen * Nrec))
return true;
if (trace)
num_read++;
return false;
} // end of ReadBlock
/***********************************************************************/
/* WriteBlock: Write back current column values for one block. */
/* Note: the test of Status is meant to prevent physical writing of */
/* the block during the checking loop in mode Update. It is set to */
/* BUF_EMPTY when reopening the table between the two loops. */
/***********************************************************************/
bool BGVFAM::WriteBlock(PGLOBAL g, PVCTCOL colp)
{
int len;
BIGINT pos;
/*********************************************************************/
/* Calculate the offset and size of the block to write. */
/*********************************************************************/
if (MaxBlk) // File has Vector format
pos = (BIGINT)Nrec * ((BIGINT)colp->Deplac * (BIGINT)MaxBlk
+ (BIGINT)colp->Clen * (BIGINT)colp->ColBlk) + (BIGINT)Headlen;
else // Old VCT format
pos = (BIGINT)Nrec * ((BIGINT)colp->Deplac
+ (BIGINT)Lrecl * (BIGINT)colp->ColBlk);
if (trace)
htrc("WB: offset=%lld Nrec=%d Deplac=%d Lrecl=%d ColBlk=%d\n",
pos, Nrec, colp->Deplac, Lrecl, colp->ColBlk);
if (BigSeek(g, Tfile, pos))
return true;
//len = colp->Clen * Nrec; see comment in VCTFAM
len = colp->Clen * ((Tdbp->GetMode() == MODE_INSERT) ? CurNum : Nrec);
if (BigWrite(g, Tfile, colp->Blk->GetValPointer(), len))
return true;
return false;
} // end of WriteBlock
/* ----------------------- End of FilAMVct --------------------------- */
|