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
|
/************* TabDos C++ Program Source Code File (.CPP) **************/
/* PROGRAM NAME: TABDOS */
/* ------------- */
/* Version 4.9.5 */
/* */
/* COPYRIGHT: */
/* ---------- */
/* (C) Copyright to the author Olivier BERTRAND 1998-2020 */
/* */
/* WHAT THIS PROGRAM DOES: */
/* ----------------------- */
/* This program are the DOS tables classes. */
/* */
/***********************************************************************/
/***********************************************************************/
/* Include relevant sections of the System header files. */
/***********************************************************************/
#include "my_global.h"
#if defined(_WIN32)
#include <io.h>
#include <sys\timeb.h> // For testing only
#include <fcntl.h>
#include <errno.h>
#if defined(__BORLANDC__)
#define __MFC_COMPAT__ // To define min/max as macro
#endif // __BORLANDC__
//#include <windows.h>
#else // !_WIN32
#if defined(UNIX)
#include <errno.h>
#include <unistd.h>
#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. */
/* filamtxt.h is header containing the file AM classes declarations. */
/***********************************************************************/
#include "global.h"
#include "osutil.h"
#include "plgdbsem.h"
//#include "catalog.h"
#include "mycat.h"
#include "xindex.h"
#include "filamap.h"
#include "filamfix.h"
#include "filamdbf.h"
#if defined(GZ_SUPPORT)
#include "filamgz.h"
#endif // GZ_SUPPORT
#if defined(ZIP_SUPPORT)
#include "filamzip.h"
#endif // ZIP_SUPPORT
#include "tabdos.h"
#include "tabfix.h"
#include "tabmul.h"
#include "array.h"
#include "blkfil.h"
#include "m_string.h"
/***********************************************************************/
/* DB static variables. */
/***********************************************************************/
int num_read, num_there, num_eq[2]; // Statistics
/***********************************************************************/
/* Size of optimize file header. */
/***********************************************************************/
#define NZ 4
/***********************************************************************/
/* External function. */
/***********************************************************************/
bool ExactInfo(void);
USETEMP UseTemp(void);
/***********************************************************************/
/* Min and Max blocks contains zero ended fields (blank = false). */
/* No conversion of block values (check = true). */
/***********************************************************************/
PVBLK AllocValBlock(PGLOBAL, void *, int, int, int len= 0, int prec= 0,
bool check= true, bool blank= false, bool un= false);
/* --------------------------- Class DOSDEF -------------------------- */
/***********************************************************************/
/* Constructor. */
/***********************************************************************/
DOSDEF::DOSDEF(void)
{
Pseudo = 3;
Fn = NULL;
Ofn = NULL;
Entry = NULL;
To_Indx = NULL;
Pwd = NULL;
Recfm = RECFM_VAR;
Mapped = false;
Zipped = false;
Mulentries = false;
Append = false;
Padded = false;
Huge = false;
Accept = false;
Eof = false;
To_Pos = NULL;
Optimized = 0;
AllocBlks = 0;
Compressed = 0;
Lrecl = 0;
AvgLen = 0;
Block = 0;
Last = 0;
Blksize = 0;
Maxerr = 0;
ReadMode = 0;
Ending = 0;
Teds = 0;
} // end of DOSDEF constructor
/***********************************************************************/
/* DefineAM: define specific AM block values from XDB file. */
/***********************************************************************/
bool DOSDEF::DefineAM(PGLOBAL g, LPCSTR am, int)
{
char buf[8];
bool map = (am && (*am == 'M' || *am == 'm'));
LPCSTR dfm = (am && (*am == 'F' || *am == 'f')) ? "F"
: (am && (*am == 'B' || *am == 'b')) ? "B"
: (am && (*am == 'X' || *am == 'x')) ? "X"
: (am && !stricmp(am, "DBF")) ? "D" : "V";
if ((Zipped = GetBoolCatInfo("Zipped", false))) {
Entry = GetStringCatInfo(g, "Entry", NULL);
Mulentries = (Entry && *Entry) ? strchr(Entry, '*') || strchr(Entry, '?')
: false;
Mulentries = GetBoolCatInfo("Mulentries", Mulentries);
Append = GetBoolCatInfo("Append", false);
Pwd = GetStringCatInfo(g, "Password", NULL);
} // endif Zipped
Desc = Fn = GetStringCatInfo(g, "Filename", NULL);
Ofn = GetStringCatInfo(g, "Optname", Fn);
GetCharCatInfo("Recfm", (PSZ)dfm, buf, sizeof(buf));
Recfm = (toupper(*buf) == 'F') ? RECFM_FIX :
(toupper(*buf) == 'B') ? RECFM_BIN :
(toupper(*buf) == 'X') ? RECFM_NAF : // MGO
(toupper(*buf) == 'D') ? RECFM_DBF : RECFM_VAR;
Lrecl = GetIntCatInfo("Lrecl", 0);
if (Recfm != RECFM_DBF)
Compressed = GetIntCatInfo("Compressed", 0);
Mapped = GetBoolCatInfo("Mapped", map);
//Block = GetIntCatInfo("Blocks", 0);
//Last = GetIntCatInfo("Last", 0);
Ending = GetIntCatInfo("Ending", CRLF);
if (Ending <= 0) {
Ending = (Recfm == RECFM_BIN || Recfm == RECFM_VCT) ? 0 : CRLF;
SetIntCatInfo("Ending", Ending);
} // endif ending
if (Recfm == RECFM_FIX || Recfm == RECFM_BIN) {
Huge = GetBoolCatInfo("Huge", Cat->GetDefHuge());
Padded = GetBoolCatInfo("Padded", false);
Blksize = GetIntCatInfo("Blksize", 0);
Eof = (GetIntCatInfo("EOF", 0) != 0);
Teds = toupper(*GetStringCatInfo(g, "Endian", ""));
} else if (Recfm == RECFM_DBF) {
Maxerr = GetIntCatInfo("Maxerr", 0);
Accept = GetBoolCatInfo("Accept", false);
ReadMode = GetIntCatInfo("Readmode", 0);
} else // (Recfm == RECFM_VAR)
AvgLen = GetIntCatInfo("Avglen", 0);
// Ignore wrong Index definitions for catalog commands
SetIndexInfo();
return false;
} // end of DefineAM
/***********************************************************************/
/* Get the full path/name of the optization file. */
/***********************************************************************/
bool DOSDEF::GetOptFileName(PGLOBAL g, char *filename)
{
PCSZ ftype;
switch (Recfm) {
case RECFM_VAR: ftype = ".dop"; break;
case RECFM_FIX: ftype = ".fop"; break;
case RECFM_BIN: ftype = ".bop"; break;
case RECFM_VCT: ftype = ".vop"; break;
case RECFM_CSV: ftype = ".cop"; break;
case RECFM_DBF: ftype = ".dbp"; break;
default:
snprintf(g->Message, sizeof(g->Message), MSG(INVALID_FTYPE), Recfm);
return true;
} // endswitch Ftype
PlugSetPath(filename, Ofn, GetPath());
strcat(PlugRemoveType(filename, filename), ftype);
return false;
} // end of GetOptFileName
/***********************************************************************/
/* After an optimize error occurred, remove all set optimize values. */
/***********************************************************************/
void DOSDEF::RemoveOptValues(PGLOBAL g)
{
char filename[_MAX_PATH];
PCOLDEF cdp;
// Delete settings of optimized columns
for (cdp = To_Cols; cdp; cdp = cdp->GetNext())
if (cdp->GetOpt()) {
cdp->SetMin(NULL);
cdp->SetMax(NULL);
cdp->SetNdv(0);
cdp->SetNbm(0);
cdp->SetDval(NULL);
cdp->SetBmap(NULL);
} // endif Opt
// Delete block position setting for not fixed tables
To_Pos = NULL;
AllocBlks = 0;
// Delete any eventually ill formed non matching optimization file
if (!GetOptFileName(g, filename))
#if defined(_WIN32)
DeleteFile(filename);
#else // UNIX
remove(filename);
#endif // _WIN32
Optimized = 0;
} // end of RemoveOptValues
/***********************************************************************/
/* DeleteIndexFile: Delete DOS/UNIX index file(s) using platform API. */
/***********************************************************************/
bool DOSDEF::DeleteIndexFile(PGLOBAL g, PIXDEF pxdf)
{
PCSZ ftype;
char filename[_MAX_PATH];
bool sep, rc = false;
if (!To_Indx)
return false; // No index
// If true indexes are in separate files
sep = GetBoolCatInfo("SepIndex", false);
if (!sep && pxdf) {
safe_strcpy(g->Message, sizeof(g->Message), MSG(NO_RECOV_SPACE));
return true;
} // endif sep
switch (Recfm) {
case RECFM_VAR: ftype = ".dnx"; break;
case RECFM_FIX: ftype = ".fnx"; break;
case RECFM_BIN: ftype = ".bnx"; break;
case RECFM_VCT: ftype = ".vnx"; break;
case RECFM_CSV: ftype = ".cnx"; break;
case RECFM_DBF: ftype = ".dbx"; break;
default:
snprintf(g->Message, sizeof(g->Message), MSG(BAD_RECFM_VAL), Recfm);
return true;
} // endswitch Ftype
/*********************************************************************/
/* Check for existence of an index file. */
/*********************************************************************/
if (sep) {
// Indexes are save in separate files
#if defined(_WIN32)
char drive[_MAX_DRIVE];
#else
char *drive = NULL;
#endif
char direc[_MAX_DIR];
char fname[_MAX_FNAME];
bool all = !pxdf;
if (all)
pxdf = To_Indx;
for (; pxdf; pxdf = pxdf->GetNext()) {
_splitpath(Ofn, drive, direc, fname, NULL);
safe_strcat(fname, sizeof(fname), "_");
safe_strcat(fname, sizeof(fname), pxdf->GetName());
_makepath(filename, drive, direc, fname, ftype);
PlugSetPath(filename, filename, GetPath());
#if defined(_WIN32)
if (!DeleteFile(filename))
rc |= (GetLastError() != ERROR_FILE_NOT_FOUND);
#else // UNIX
if (remove(filename))
rc |= (errno != ENOENT);
#endif // UNIX
if (!all)
break;
} // endfor pxdf
} else { // !sep
// Drop all indexes, delete the common file
PlugSetPath(filename, Ofn, GetPath());
safe_strcat(PlugRemoveType(filename, filename), sizeof(filename), ftype);
#if defined(_WIN32)
if (!DeleteFile(filename))
rc = (GetLastError() != ERROR_FILE_NOT_FOUND);
#else // UNIX
if (remove(filename))
rc = (errno != ENOENT);
#endif // UNIX
} // endif sep
if (rc)
snprintf(g->Message, sizeof(g->Message), MSG(DEL_FILE_ERR), filename);
return rc; // Return true if error
} // end of DeleteIndexFile
/***********************************************************************/
/* InvalidateIndex: mark all indexes as invalid. */
/***********************************************************************/
bool DOSDEF::InvalidateIndex(PGLOBAL)
{
if (To_Indx)
for (PIXDEF xp = To_Indx; xp; xp = xp->Next)
xp->Invalid = true;
return false;
} // end of InvalidateIndex
/***********************************************************************/
/* GetTable: makes a new Table Description Block. */
/***********************************************************************/
PTDB DOSDEF::GetTable(PGLOBAL g, MODE mode)
{
// Mapping not used for insert
USETEMP tmp = UseTemp();
bool map = Mapped && mode != MODE_INSERT &&
!(tmp != TMP_NO && Recfm == RECFM_VAR
&& mode == MODE_UPDATE) &&
!(tmp == TMP_FORCE &&
(mode == MODE_UPDATE || mode == MODE_DELETE));
PTXF txfp = NULL;
PTDBASE tdbp;
/*********************************************************************/
/* Allocate table and file processing class of the proper type. */
/* Column blocks will be allocated only when needed. */
/*********************************************************************/
if (Recfm == RECFM_DBF) {
if (Catfunc == FNC_NO) {
if (Zipped) {
if (mode == MODE_READ || mode == MODE_ANY || mode == MODE_ALTER) {
txfp = new(g) UZDFAM(this);
} else {
safe_strcpy(g->Message, sizeof(g->Message), "Zipped DBF tables are read only");
return NULL;
} // endif's mode
} else if (map)
txfp = new(g) DBMFAM(this);
else
txfp = new(g) DBFFAM(this);
tdbp = new(g) TDBFIX(this, txfp);
} else
tdbp = new(g) TDBDCL(this); // Catfunc should be 'C'
} else if (Zipped) {
#if defined(ZIP_SUPPORT)
if (Recfm == RECFM_VAR) {
if (mode == MODE_READ || mode == MODE_ANY || mode == MODE_ALTER) {
txfp = new(g) UNZFAM(this);
} else if (mode == MODE_INSERT) {
txfp = new(g) ZIPFAM(this);
} else {
safe_strcpy(g->Message, sizeof(g->Message), "UPDATE/DELETE not supported for ZIP");
return NULL;
} // endif's mode
tdbp = new(g) TDBDOS(this, txfp);
} else {
if (mode == MODE_READ || mode == MODE_ANY || mode == MODE_ALTER) {
txfp = new(g) UZXFAM(this);
} else if (mode == MODE_INSERT) {
txfp = new(g) ZPXFAM(this);
} else {
safe_strcpy(g->Message, sizeof(g->Message), "UPDATE/DELETE not supported for ZIP");
return NULL;
} // endif's mode
tdbp = new(g)TDBFIX(this, txfp);
} // endif Recfm
#else // !ZIP_SUPPORT
snprintf(g->Message, sizeof(g->Message), MSG(NO_FEAT_SUPPORT), "ZIP");
return NULL;
#endif // !ZIP_SUPPORT
} else if (Recfm != RECFM_VAR && Compressed < 2) {
if (Huge)
txfp = new(g) BGXFAM(this);
else if (map)
txfp = new(g) MPXFAM(this);
else if (Compressed) {
#if defined(GZ_SUPPORT)
txfp = new(g) GZXFAM(this);
#else // !GZ_SUPPORT
snprintf(g->Message, sizeof(g->Message), MSG(NO_FEAT_SUPPORT), "GZ");
return NULL;
#endif // !GZ_SUPPORT
} else
txfp = new(g) FIXFAM(this);
tdbp = new(g) TDBFIX(this, txfp);
} else {
if (Compressed) {
#if defined(GZ_SUPPORT)
if (Compressed == 1)
txfp = new(g) GZFAM(this);
else
txfp = new(g) ZLBFAM(this);
#else // !GZ_SUPPORT
snprintf(g->Message, sizeof(g->Message), MSG(NO_FEAT_SUPPORT), "GZ");
return NULL;
#endif // !GZ_SUPPORT
} else if (map)
txfp = new(g) MAPFAM(this);
else
txfp = new(g) DOSFAM(this);
// Txfp must be set even for not multiple tables because
// it is needed when calling Cardinality in GetBlockValues.
tdbp = new(g) TDBDOS(this, txfp);
} // endif Recfm
if (Multiple)
tdbp = new(g) TDBMUL(tdbp);
else
/*******************************************************************/
/* For block tables, get eventually saved optimization values. */
/*******************************************************************/
if (tdbp->GetBlockValues(g)) {
PushWarning(g, tdbp);
// return NULL; // causes a crash when deleting index
} else if (Recfm == RECFM_VAR || Compressed > 1) {
if (IsOptimized()) {
if (map) {
txfp = new(g) MBKFAM(this);
} else if (Compressed) {
#if defined(GZ_SUPPORT)
if (Compressed == 1)
txfp = new(g) ZBKFAM(this);
else {
txfp->SetBlkPos(To_Pos);
((PZLBFAM)txfp)->SetOptimized(To_Pos != NULL);
} // endelse
#else
snprintf(g->Message, sizeof(g->Message), MSG(NO_FEAT_SUPPORT), "GZ");
return NULL;
#endif
} else
txfp = new(g) BLKFAM(this);
((PTDBDOS)tdbp)->SetTxfp(txfp);
} // endif Optimized
} // endif Recfm
return tdbp;
} // end of GetTable
/* ------------------------ Class TDBDOS ----------------------------- */
/***********************************************************************/
/* Implementation of the TDBDOS class. This is the common class that */
/* contain all that is common between the TDBDOS and TDBMAP classes. */
/***********************************************************************/
TDBDOS::TDBDOS(PDOSDEF tdp, PTXF txfp) : TDBASE(tdp)
{
if ((Txfp = txfp))
Txfp->SetTdbp(this);
Lrecl = tdp->Lrecl;
AvgLen = tdp->AvgLen;
Ftype = tdp->Recfm;
To_Line = NULL;
//To_BlkIdx = NULL;
To_BlkFil = NULL;
SavFil = NULL;
//Xeval = 0;
Beval = 0;
Abort = false;
Indxd = false;
} // end of TDBDOS standard constructor
TDBDOS::TDBDOS(PGLOBAL g, PTDBDOS tdbp) : TDBASE(tdbp)
{
Txfp = (g) ? tdbp->Txfp->Duplicate(g) : tdbp->Txfp;
Lrecl = tdbp->Lrecl;
AvgLen = tdbp->AvgLen;
Ftype = tdbp->Ftype;
To_Line = tdbp->To_Line;
//To_BlkIdx = tdbp->To_BlkIdx;
To_BlkFil = tdbp->To_BlkFil;
SavFil = tdbp->SavFil;
//Xeval = tdbp->Xeval;
Beval = tdbp->Beval;
Abort = tdbp->Abort;
Indxd = tdbp->Indxd;
} // end of TDBDOS copy constructor
// Method
PTDB TDBDOS::Clone(PTABS t)
{
PTDB tp;
PDOSCOL cp1, cp2;
PGLOBAL g = t->G;
tp = new(g) TDBDOS(g, this);
for (cp1 = (PDOSCOL)Columns; cp1; cp1 = (PDOSCOL)cp1->GetNext()) {
cp2 = new(g) DOSCOL(cp1, tp); // Make a copy
NewPointer(t, cp1, cp2);
} // endfor cp1
return tp;
} // end of Clone
/***********************************************************************/
/* Allocate DOS column description block. */
/***********************************************************************/
PCOL TDBDOS::MakeCol(PGLOBAL g, PCOLDEF cdp, PCOL cprec, int n)
{
return new(g) DOSCOL(g, cdp, this, cprec, n);
} // end of MakeCol
/***********************************************************************/
/* Print debug information. */
/***********************************************************************/
void TDBDOS::PrintAM(FILE *f, char *m)
{
fprintf(f, "%s AM(%d): mode=%d\n", m, GetAmType(), Mode);
if (Txfp->To_File)
fprintf(f, "%s File: %s\n", m, Txfp->To_File);
} // end of PrintAM
/***********************************************************************/
/* Remake the indexes after the table was modified. */
/***********************************************************************/
int TDBDOS::ResetTableOpt(PGLOBAL g, bool dop, bool dox)
{
int prc = RC_OK, rc = RC_OK;
if (!GetFileLength(g)) {
// Void table, delete all opt and index files
PDOSDEF defp = (PDOSDEF)To_Def;
defp->RemoveOptValues(g);
return (defp->DeleteIndexFile(g, NULL)) ? RC_INFO : RC_OK;
} // endif GetFileLength
MaxSize = -1; // Size must be recalculated
Cardinal = -1; // as well as Cardinality
To_Filter = NULL; // Disable filtering
//To_BlkIdx = NULL; // and index filtering
To_BlkFil = NULL; // and block filtering
// After the table was modified the indexes
// are invalid and we should mark them as such...
(void)((PDOSDEF)To_Def)->InvalidateIndex(g);
if (dop) {
Columns = NULL; // Not used anymore
if (Txfp->Blocked) {
// MakeBlockValues must be executed in non blocked mode
// except for ZLIB access method.
if (Txfp->GetAmType() == TYPE_AM_MAP) {
Txfp = new(g) MAPFAM((PDOSDEF)To_Def);
#if defined(GZ_SUPPORT)
} else if (Txfp->GetAmType() == TYPE_AM_GZ) {
Txfp = new(g) GZFAM((PDOSDEF)To_Def);
} else if (Txfp->GetAmType() == TYPE_AM_ZLIB) {
Txfp->Reset();
((PZLBFAM)Txfp)->SetOptimized(false);
#endif // GZ_SUPPORT
} else if (Txfp->GetAmType() == TYPE_AM_BLK)
Txfp = new(g) DOSFAM((PDOSDEF)To_Def);
Txfp->SetTdbp(this);
} else
Txfp->Reset();
Use = USE_READY; // So the table can be reopened
Mode = MODE_ANY; // Just to be clean
rc = MakeBlockValues(g); // Redo optimization
} // endif dop
if (dox && (rc == RC_OK || rc == RC_INFO)) {
// Remake eventual indexes
// if (Mode != MODE_UPDATE)
To_SetCols = NULL; // Positions are changed
Columns = NULL; // Not used anymore
Txfp->Reset(); // New start
Use = USE_READY; // So the table can be reopened
Mode = MODE_READ; // New mode
prc = rc;
if (PlgGetUser(g)->Check & CHK_OPT)
// We must remake all indexes.
rc = MakeIndex(g, NULL, false);
rc = (rc == RC_INFO) ? prc : rc;
} // endif dox
return rc;
} // end of ResetTableOpt
/***********************************************************************/
/* Calculate the block sizes so block I/O can be used and also the */
/* Min/Max values for clustered/sorted table columns. */
/***********************************************************************/
int TDBDOS::MakeBlockValues(PGLOBAL g)
{
int i, lg, nrec, rc, n = 0;
int curnum, curblk, block, savndv, savnbm;
void *savmin, *savmax;
bool blocked, xdb2 = false;
//POOLHEADER save;
PCOLDEF cdp;
PDOSDEF defp = (PDOSDEF)To_Def;
PDOSCOL colp = NULL;
PDBUSER dup = PlgGetUser(g);
//void *memp = cat->GetDescp();
(void) defp->GetCat(); // XXX Should be removed?
if ((nrec = defp->GetElemt()) < 2) {
if (!To_Def->Partitioned()) {
// This may be wrong to do in some cases
safe_strcpy(g->Message, sizeof(g->Message), MSG(TABLE_NOT_OPT));
return RC_INFO; // Not to be optimized
} else
return RC_OK;
} else if (GetMaxSize(g) == 0 || !(dup->Check & CHK_OPT)) {
// Suppress the opt file firstly if the table is void,
// secondly when it was modified with OPTIMIZATION unchecked
// because it is no more valid.
defp->RemoveOptValues(g); // Erase opt file
return RC_OK; // void table
} else if (MaxSize < 0)
return RC_FX;
defp->SetOptimized(0);
// Estimate the number of needed blocks
if ((block = (int)((MaxSize + (int)nrec - 1) / (int)nrec)) < 2) {
// This may be wrong to do in some cases
defp->RemoveOptValues(g);
safe_strcpy(g->Message, sizeof(g->Message), MSG(TABLE_NOT_OPT));
return RC_INFO; // Not to be optimized
} // endif block
// We have to use local variables because Txfp->CurBlk is set
// to Rows+1 by unblocked variable length table access methods.
curblk = -1;
curnum = nrec - 1;
//last = 0;
Txfp->Block = block; // This is useful mainly for
Txfp->CurBlk = curblk; // blocked tables (ZLBFAM), for
Txfp->CurNum = curnum; // others it is just to be clean.
/*********************************************************************/
/* Allocate the array of block starting positions. */
/*********************************************************************/
//if (memp)
// save = *(PPOOLHEADER)memp;
Txfp->BlkPos = (int*)PlugSubAlloc(g, NULL, (block + 1) * sizeof(int));
/*********************************************************************/
/* Allocate the blocks for clustered columns. */
/*********************************************************************/
blocked = Txfp->Blocked; // Save
Txfp->Blocked = true; // So column block can be allocated
for (cdp = defp->GetCols(), i = 1; cdp; cdp = cdp->GetNext(), i++)
if (cdp->GetOpt()) {
lg = cdp->GetClen();
if (cdp->GetFreq() && cdp->GetFreq() <= dup->Maxbmp) {
cdp->SetXdb2(true);
savndv = cdp->GetNdv();
cdp->SetNdv(0); // Reset Dval number of values
xdb2 = true;
savmax = cdp->GetDval();
cdp->SetDval(PlugSubAlloc(g, NULL, cdp->GetFreq() * lg));
savnbm = cdp->GetNbm();
cdp->SetNbm(0); // Prevent Bmap allocation
// savmin = cdp->GetBmap();
// cdp->SetBmap(PlugSubAlloc(g, NULL, block * sizeof(int)));
if (trace(1))
htrc("Dval(%p) Bmap(%p) col(%d) %s Block=%d lg=%d\n",
cdp->GetDval(), cdp->GetBmap(), i, cdp->GetName(), block, lg);
// colp will be initialized with proper Dval VALBLK
colp = (PDOSCOL)MakeCol(g, cdp, colp, i);
colp->InitValue(g); // Allocate column value buffer
cdp->SetNbm(savnbm);
// cdp->SetBmap(savmin); // Can be reused if the new size
cdp->SetDval(savmax); // is not greater than this one.
cdp->SetNdv(savndv);
} else {
cdp->SetXdb2(false); // Maxbmp may have been reset
savmin = cdp->GetMin();
savmax = cdp->GetMax();
cdp->SetMin(PlugSubAlloc(g, NULL, block * lg));
cdp->SetMax(PlugSubAlloc(g, NULL, block * lg));
// Valgrind complains if there are uninitialised bytes
// after the null character ending
if (IsTypeChar(cdp->GetType())) {
memset(cdp->GetMin(), 0, block * lg);
memset(cdp->GetMax(), 0, block * lg);
} // endif Type
if (trace(1))
htrc("min(%p) max(%p) col(%d) %s Block=%d lg=%d\n",
cdp->GetMin(), cdp->GetMax(), i, cdp->GetName(), block, lg);
// colp will be initialized with proper opt VALBLK's
colp = (PDOSCOL)MakeCol(g, cdp, colp, i);
colp->InitValue(g); // Allocate column value buffer
cdp->SetMin(savmin); // Can be reused if the number
cdp->SetMax(savmax); // of blocks does not change.
} // endif Freq
} // endif Clustered
// No optimised columns. Still useful for blocked variable tables.
if (!colp && defp->Recfm != RECFM_VAR) {
safe_strcpy(g->Message, sizeof(g->Message), "No optimised columns");
return RC_INFO;
} // endif colp
Txfp->Blocked = blocked;
/*********************************************************************/
/* Now do calculate the optimization values. */
/*********************************************************************/
Mode = MODE_READ;
if (OpenDB(g))
return RC_FX;
if (xdb2) {
/*********************************************************************/
/* Retrieve the distinct values of XDB2 columns. */
/*********************************************************************/
if (GetDistinctColumnValues(g, nrec))
return RC_FX;
OpenDB(g); // Rewind the table file
} // endif xdb2
#if defined(PROG_INFO)
/*********************************************************************/
/* Initialize progress information */
/*********************************************************************/
char *p = (char *)PlugSubAlloc(g, NULL, 24 + strlen(Name));
snprintf(p, 24 + strlen(Name), "%s%s", MSG(OPTIMIZING), Name);
dup->Step = p;
dup->ProgMax = GetProgMax(g);
dup->ProgCur = 0;
#endif // SOCKET_MODE || THREAD
/*********************************************************************/
/* Make block starting pos and min/max values of cluster columns. */
/*********************************************************************/
while ((rc = ReadDB(g)) == RC_OK) {
if (blocked) {
// A blocked FAM class handles CurNum and CurBlk (ZLBFAM)
if (!Txfp->CurNum)
Txfp->BlkPos[Txfp->CurBlk] = Txfp->GetPos();
} else {
if (++curnum >= nrec) {
if (++curblk >= block) {
safe_strcpy(g->Message, sizeof(g->Message), MSG(BAD_BLK_ESTIM));
goto err;
} else
curnum = 0;
// Get block starting position
Txfp->BlkPos[curblk] = Txfp->GetPos();
} // endif CurNum
// last = curnum + 1; // curnum is zero based
Txfp->CurBlk = curblk; // Used in COLDOS::SetMinMax
Txfp->CurNum = curnum; // Used in COLDOS::SetMinMax
} // endif blocked
/*******************************************************************/
/* Now calculate the min and max values for the cluster columns. */
/*******************************************************************/
for (colp = (PDOSCOL)Columns; colp; colp = (PDOSCOL)colp->GetNext())
if (colp->Clustered == 2) {
if (colp->SetBitMap(g))
goto err;
} else
if (colp->SetMinMax(g))
goto err; // Currently: column is not sorted
#if defined(PROG_INFO)
if (!dup->Step) {
safe_strcpy(g->Message, sizeof(g->Message), MSG(OPT_CANCELLED));
goto err;
} else
dup->ProgCur = GetProgCur();
#endif // PROG_INFO
n++; // Used to calculate block and last
} // endwhile
if (rc == RC_EF) {
Txfp->Nrec = nrec;
#if 0 // No good because Curblk and CurNum after EOF are different
// depending on whether the file is mapped or not mapped.
if (blocked) {
// Txfp->Block = Txfp->CurBlk + 1;
Txfp->Last = (Txfp->CurNum) ? Txfp->CurNum : nrec;
// Txfp->Last = (Txfp->CurNum) ? Txfp->CurNum + 1 : nrec;
Txfp->Block = Txfp->CurBlk + (Txfp->Last == nrec ? 0 : 1);
} else {
Txfp->Block = curblk + 1;
Txfp->Last = last;
} // endif blocked
#endif // 0
// New values of Block and Last
Txfp->Block = (n + nrec - 1) / nrec;
Txfp->Last = (n % nrec) ? (n % nrec) : nrec;
// This is needed to be able to calculate the last block size
Txfp->BlkPos[Txfp->Block] = Txfp->GetNextPos();
} else
goto err;
/*********************************************************************/
/* Save the optimization values for this table. */
/*********************************************************************/
if (!SaveBlockValues(g)) {
defp->Block = Txfp->Block;
defp->Last = Txfp->Last;
CloseDB(g);
defp->SetIntCatInfo("Blocks", Txfp->Block);
defp->SetIntCatInfo("Last", Txfp->Last);
return RC_OK;
} // endif SaveBlockValues
err:
// Restore Desc memory suballocation
//if (memp)
// *(PPOOLHEADER)memp = save;
defp->RemoveOptValues(g);
CloseDB(g);
return RC_FX;
} // end of MakeBlockValues
/***********************************************************************/
/* Save the block and Min/Max values for this table. */
/* The problem here is to avoid name duplication, because more than */
/* one data file can have the same name (but different types) and/or */
/* the same data file can be used with different block sizes. This is */
/* why we use Ofn that defaults to the file name but can be set to a */
/* different name if necessary. */
/***********************************************************************/
bool TDBDOS::SaveBlockValues(PGLOBAL g)
{
char filename[_MAX_PATH];
int lg, n[NZ + 2];
size_t nbk, ndv, nbm, block = Txfp->Block;
bool rc = false;
FILE *opfile;
PDOSCOL colp;
PDOSDEF defp = (PDOSDEF)To_Def;
if (defp->GetOptFileName(g, filename))
return true;
if (!(opfile = fopen(filename, "wb"))) {
snprintf(g->Message, sizeof(g->Message), MSG(OPEN_MODE_ERROR),
"wb", (int)errno, filename);
safe_strcat(g->Message, sizeof(g->Message), ": ");
safe_strcat(g->Message, sizeof(g->Message), strerror(errno));
if (trace(1))
htrc("%s\n", g->Message);
return true;
} // endif opfile
memset(n, 0, sizeof(n)); // To avoid valgrind warning
if (Ftype == RECFM_VAR || defp->Compressed == 2) {
/*******************************************************************/
/* Write block starting positions into the opt file. */
/*******************************************************************/
block++;
lg = sizeof(int);
n[0] = Txfp->Last; n[1] = lg; n[2] = Txfp->Nrec; n[3] = Txfp->Block;
if (fwrite(n, sizeof(int), NZ, opfile) != NZ) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_HEAD_WR_ERR), strerror(errno));
rc = true;
} // endif size
if (fwrite(Txfp->BlkPos, lg, block, opfile) != block) {
snprintf(g->Message, sizeof(g->Message), MSG(OPTBLK_WR_ERR), strerror(errno));
rc = true;
} // endif size
block--; // = Txfp->Block;
} // endif Ftype
/*********************************************************************/
/* Write the Min/Max values into the opt file. */
/*********************************************************************/
for (colp = (PDOSCOL)Columns; colp; colp = (PDOSCOL)colp->Next) {
lg = colp->Value->GetClen();
// Now start the writing process
if (colp->Clustered == 2) {
// New XDB2 block optimization. Will be recognized when reading
// because the column index is negated.
ndv = colp->Ndv; nbm = colp->Nbm;
nbk = nbm * block;
n[0] = -colp->Index; n[1] = lg; n[2] = Txfp->Nrec; n[3] = block;
n[4] = ndv; n[5] = nbm;
if (fwrite(n, sizeof(int), NZ + 2, opfile) != NZ + 2) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_HEAD_WR_ERR), strerror(errno));
rc = true;
} // endif size
if (fwrite(colp->Dval->GetValPointer(), lg, ndv, opfile) != ndv) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_DVAL_WR_ERR), strerror(errno));
rc = true;
} // endif size
if (fwrite(colp->Bmap->GetValPointer(), sizeof(int), nbk, opfile) != nbk) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_BMAP_WR_ERR), strerror(errno));
rc = true;
} // endif size
} else {
n[0] = colp->Index; n[1] = lg; n[2] = Txfp->Nrec; n[3] = block;
if (fwrite(n, sizeof(int), NZ, opfile) != NZ) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_HEAD_WR_ERR), strerror(errno));
rc = true;
} // endif size
if (fwrite(colp->Min->GetValPointer(), lg, block, opfile) != block) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_MIN_WR_ERR), strerror(errno));
rc = true;
} // endif size
if (fwrite(colp->Max->GetValPointer(), lg, block, opfile) != block) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_MAX_WR_ERR), strerror(errno));
rc = true;
} // endif size
} // endif Clustered
} // endfor colp
fclose(opfile);
return rc;
} // end of SaveBlockValues
/***********************************************************************/
/* Read the Min/Max values for this table. */
/* The problem here is to avoid name duplication, because more than */
/* one data file can have the same name (but different types) and/or */
/* the same data file can be used with different block sizes. This is */
/* why we use Ofn that defaults to the file name but can be set to a */
/* different name if necessary. */
/***********************************************************************/
bool TDBDOS::GetBlockValues(PGLOBAL g)
{
char filename[_MAX_PATH];
int i, lg, n[NZ];
int nrec, block = 0, last = 0;
int len;
bool newblk = false;
size_t ndv, nbm, nbk, blk;
FILE *opfile;
PCOLDEF cdp;
PDOSDEF defp = (PDOSDEF)To_Def;
PDBUSER dup = PlgGetUser(g);
(void) defp->GetCat(); // XXX Should be removed?
#if 0
if (Mode == MODE_INSERT && Txfp->GetAmType() == TYPE_AM_DOS)
return false;
#endif // _WIN32
if (defp->Optimized || !(dup->Check & CHK_OPT))
return false; // Already done or to be redone
if (Ftype == RECFM_VAR || defp->Compressed == 2) {
/*******************************************************************/
/* Variable length file that can be read by block. */
/*******************************************************************/
nrec = (defp->GetElemt()) ? defp->GetElemt() : 1;
if (nrec > 1) {
// The table can be declared optimized if it is void.
// This is useful to handle Insert in optimized mode.
char filename[_MAX_PATH];
int h;
int flen = -1;
PlugSetPath(filename, defp->Fn, GetPath());
h = open(filename, O_RDONLY);
flen = (h == -1 && errno == ENOENT) ? 0 : _filelength(h);
if (h != -1)
close(h);
if (!flen) {
defp->SetOptimized(1);
return false;
} // endif flen
} else
return false; // Not optimisable
cdp = defp->GetCols();
i = 1;
} else {
/*******************************************************************/
/* Fixed length file. Opt file exists only for clustered columns. */
/*******************************************************************/
// Check for existence of clustered columns
for (cdp = defp->GetCols(), i = 1; cdp; cdp = cdp->GetNext(), i++)
if (cdp->GetOpt())
break;
if (!cdp)
return false; // No optimization needed
if ((len = Cardinality(g)) < 0)
return true; // Table error
else if (!len)
return false; // File does not exist yet
block = Txfp->Block; // Was set in Cardinality
nrec = Txfp->Nrec;
} // endif Ftype
if (defp->GetOptFileName(g, filename))
return true;
if (!(opfile = fopen(filename, "rb")))
return false; // No saved values
if (Ftype == RECFM_VAR || defp->Compressed == 2) {
/*******************************************************************/
/* Read block starting positions from the opt file. */
/*******************************************************************/
lg = sizeof(int);
if (fread(n, sizeof(int), NZ, opfile) != NZ) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_HEAD_RD_ERR), strerror(errno));
goto err;
} // endif size
if (n[1] != lg || n[2] != nrec) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_NOT_MATCH), filename);
goto err;
} // endif
last = n[0];
block = n[3];
blk = block + 1;
defp->To_Pos = (int*)PlugSubAlloc(g, NULL, blk * lg);
if (fread(defp->To_Pos, lg, blk, opfile) != blk) {
snprintf(g->Message, sizeof(g->Message), MSG(OPTBLK_RD_ERR), strerror(errno));
goto err;
} // endif size
} // endif Ftype
/*********************************************************************/
/* Read the Min/Max values from the opt file. */
/*********************************************************************/
for (; cdp; cdp = cdp->GetNext(), i++)
if (cdp->GetOpt()) {
lg = cdp->GetClen();
blk = block;
// Now start the reading process.
if (fread(n, sizeof(int), NZ, opfile) != NZ) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_HEAD_RD_ERR), strerror(errno));
goto err;
} // endif size
if (n[0] == -i) {
// Read the XDB2 opt values from the opt file
if (n[1] != lg || n[2] != nrec || n[3] != block) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_NOT_MATCH), filename);
goto err;
} // endif
if (fread(n, sizeof(int), 2, opfile) != 2) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_HEAD_RD_ERR), strerror(errno));
goto err;
} // endif fread
ndv = n[0]; nbm = n[1]; nbk = nbm * blk;
if (cdp->GetNdv() < (int)ndv || !cdp->GetDval())
cdp->SetDval(PlugSubAlloc(g, NULL, ndv * lg));
cdp->SetNdv((int)ndv);
if (fread(cdp->GetDval(), lg, ndv, opfile) != ndv) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_DVAL_RD_ERR), strerror(errno));
goto err;
} // endif size
if (newblk || cdp->GetNbm() < (int)nbm || !cdp->GetBmap())
cdp->SetBmap(PlugSubAlloc(g, NULL, nbk * sizeof(int)));
cdp->SetNbm((int)nbm);
if (fread(cdp->GetBmap(), sizeof(int), nbk, opfile) != nbk) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_BMAP_RD_ERR), strerror(errno));
goto err;
} // endif size
cdp->SetXdb2(true);
} else {
// Read the Min/Max values from the opt file
if (n[0] != i || n[1] != lg || n[2] != nrec || n[3] != block) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_NOT_MATCH), filename);
goto err;
} // endif
if (newblk || !cdp->GetMin())
cdp->SetMin(PlugSubAlloc(g, NULL, blk * lg));
if (fread(cdp->GetMin(), lg, blk, opfile) != blk) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_MIN_RD_ERR), strerror(errno));
goto err;
} // endif size
if (newblk || !cdp->GetMax())
cdp->SetMax(PlugSubAlloc(g, NULL, blk * lg));
if (fread(cdp->GetMax(), lg, blk, opfile) != blk) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_MAX_RD_ERR), strerror(errno));
goto err;
} // endif size
cdp->SetXdb2(false);
} // endif n[0] (XDB2)
} // endif Clustered
defp->SetBlock(block);
defp->Last = last; // For Cardinality
defp->SetAllocBlks(block);
defp->SetOptimized(1);
fclose(opfile);
MaxSize = -1; // Can be refined later
return false;
err:
defp->RemoveOptValues(g);
fclose(opfile);
// Ignore error if not in mode CHK_OPT
return (PlgGetUser(g)->Check & CHK_OPT) != 0;
} // end of GetBlockValues
/***********************************************************************/
/* This fonction is used while making XDB2 block optimization. */
/* It constructs for each elligible columns, the sorted list of the */
/* distinct values existing in the column. This function uses an */
/* algorithm that permit to get several sets of distinct values by */
/* reading the table only once, which cannot be done using a standard */
/* SQL query. */
/***********************************************************************/
bool TDBDOS::GetDistinctColumnValues(PGLOBAL g, int nrec)
{
char *p;
int rc, blk, n = 0;
PDOSCOL colp;
PDBUSER dup = PlgGetUser(g);
/*********************************************************************/
/* Initialize progress information */
/*********************************************************************/
p = (char *)PlugSubAlloc(g, NULL, 48 + strlen(Name));
snprintf(p, 48 + strlen(Name), "%s%s", MSG(GET_DIST_VALS), Name);
dup->Step = p;
dup->ProgMax = GetProgMax(g);
dup->ProgCur = 0;
while ((rc = ReadDB(g)) == RC_OK) {
for (colp = (PDOSCOL)Columns; colp; colp = (PDOSCOL)colp->Next)
if (colp->Clustered == 2)
if (colp->AddDistinctValue(g))
return true; // Too many distinct values
#if defined(SOCKET_MODE)
if (SendProgress(dup)) {
safe_strcpy(g->Message, sizeof(g->Message), MSG(OPT_CANCELLED));
return true;
} else
#elif defined(THREAD)
if (!dup->Step) {
safe_strcpy(g->Message, sizeof(g->Message), MSG(OPT_CANCELLED));
return true;
} else
#endif // THREAD
dup->ProgCur = GetProgCur();
n++;
} // endwhile
if (rc != RC_EF)
return true;
// Reset the number of table blocks
//nrec = ((PDOSDEF)To_Def)->GetElemt(); (or default value)
blk = (n + nrec - 1) / nrec;
Txfp->Block = blk; // Useful mainly for ZLBFAM ???
// Set Nbm, Bmap for XDB2 columns
for (colp = (PDOSCOL)Columns; colp; colp = (PDOSCOL)colp->Next)
if (colp->Clustered == 2) {
// colp->Cdp->SetNdv(colp->Ndv);
colp->Nbm = (colp->Ndv + MAXBMP - 1) / MAXBMP;
colp->Bmap = AllocValBlock(g, NULL, TYPE_INT, colp->Nbm * blk);
} // endif Clustered
return false;
} // end of GetDistinctColumnValues
/***********************************************************************/
/* Analyze the filter and construct the Block Evaluation Filter. */
/* This is possible when a filter contains predicates implying a */
/* column marked as "clustered" or "sorted" matched to a constant */
/* argument. It is then possible by comparison against the smallest */
/* and largest column values in each block to determine whether the */
/* filter condition will be always true or always false for the block.*/
/***********************************************************************/
PBF TDBDOS::InitBlockFilter(PGLOBAL g, PFIL filp)
{
bool blk = Txfp->Blocked;
if (To_BlkFil)
return To_BlkFil; // Already done
else if (!filp)
return NULL;
else if (blk) {
if (Txfp->GetAmType() == TYPE_AM_DBF)
/*****************************************************************/
/* If RowID is used in this query, block optimization cannot be */
/* used because currently the file must be read sequentially. */
/*****************************************************************/
for (PCOL cp = Columns; cp; cp = cp->GetNext())
if (cp->GetAmType() == TYPE_AM_ROWID && !((RIDBLK*)cp)->GetRnm())
return NULL;
} // endif blk
int i, op = filp->GetOpc(), opm = filp->GetOpm();
bool cnv[2];
PCOL colp;
PXOB arg[2] = {NULL,NULL};
PBF *fp = NULL, bfp = NULL;
switch (op) {
case OP_EQ:
case OP_NE:
case OP_GT:
case OP_GE:
case OP_LT:
case OP_LE:
if (! opm) {
for (i = 0; i < 2; i++) {
arg[i] = filp->Arg(i);
cnv[i] = filp->Conv(i);
} // endfor i
bfp = CheckBlockFilari(g, arg, op, cnv);
break;
} // endif !opm
// if opm, pass thru
// fall through
case OP_IN:
if (filp->GetArgType(0) == TYPE_COLBLK &&
filp->GetArgType(1) == TYPE_ARRAY) {
arg[0] = filp->Arg(0);
arg[1] = filp->Arg(1);
colp = (PCOL)arg[0];
if (colp->GetTo_Tdb() == this) {
// Block evaluation is possible for...
if (colp->GetAmType() == TYPE_AM_ROWID) {
// Special column ROWID and constant array, but
// currently we don't know how to retrieve a RowID
// from a DBF table that is not sequentially read.
// if (Txfp->GetAmType() != TYPE_AM_DBF ||
// ((RIDBLK*)arg[0])->GetRnm())
bfp = new(g) BLKSPCIN(g, this, op, opm, arg, Txfp->Nrec);
} else if (blk && Txfp->Nrec > 1 && colp->IsClustered())
{
// Clustered column and constant array
if (colp->GetClustered() == 2)
bfp = new(g) BLKFILIN2(g, this, op, opm, arg);
else
bfp = new(g) BLKFILIN(g, this, op, opm, arg);
}
} // endif this
#if 0
} else if (filp->GetArgType(0) == TYPE_SCALF &&
filp->GetArgType(1) == TYPE_ARRAY) {
arg[0] = filp->Arg(0);
arg[1] = filp->Arg(1);
if (((PSCALF)arg[0])->GetOp() == OP_ROW &&
arg[1]->GetResultType() == TYPE_LIST) {
PARRAY par = (PARRAY)arg[1];
LSTVAL *vlp = (LSTVAL*)par->GetValue();
((SFROW*)arg[0])->GetParms(n);
if (n != vlp->GetN())
return NULL;
else
n = par->GetNval();
arg[1] = new(g) CONSTANT(vlp);
fp = (PBF*)PlugSubAlloc(g, NULL, n * sizeof(PBF));
cnv[0] = cnv[1] = false;
if (op == OP_IN)
op = OP_EQ;
for (i = 0; i < n; i++) {
par->GetNthValue(vlp, i);
if (!(fp[i] = CheckBlockFilari(g, arg, op, cnv)))
return NULL;
} // endfor i
bfp = new(g) BLKFILLOG(this, (opm == 2 ? OP_AND : OP_OR), fp, n);
} // endif ROW
#endif // 0
} // endif Type
break;
case OP_AND:
case OP_OR:
fp = (PBF*)PlugSubAlloc(g, NULL, 2 * sizeof(PBF));
fp[0] = InitBlockFilter(g, (PFIL)(filp->Arg(0)));
fp[1] = InitBlockFilter(g, (PFIL)(filp->Arg(1)));
if (fp[0] || fp[1])
bfp = new(g) BLKFILLOG(this, op, fp, 2);
break;
case OP_NOT:
fp = (PBF*)PlugSubAlloc(g, NULL, sizeof(PBF));
if ((*fp = InitBlockFilter(g, (PFIL)(filp->Arg(0)))))
bfp = new(g) BLKFILLOG(this, op, fp, 1);
break;
case OP_LIKE:
default:
break;
} // endswitch op
return bfp;
} // end of InitBlockFilter
/***********************************************************************/
/* Analyze the passed arguments and construct the Block Filter. */
/***********************************************************************/
PBF TDBDOS::CheckBlockFilari(PGLOBAL g, PXOB *arg, int op, bool *cnv)
{
//int i, n1, n2, ctype = TYPE_ERROR, n = 0, type[2] = {0,0};
//bool conv = false, xdb2 = false, ok = false, b[2];
//PXOB *xarg1, *xarg2 = NULL, xp[2];
int i, n = 0, type[2] = {0,0};
bool conv = false, xdb2 = false;
PXOB xp[2];
PCOL colp;
PBF bfp = NULL;
for (i = 0; i < 2; i++) {
switch (arg[i]->GetType()) {
case TYPE_CONST:
type[i] = 1;
// ctype = arg[i]->GetResultType();
break;
case TYPE_COLBLK:
conv = cnv[i];
colp = (PCOL)arg[i];
if (colp->GetTo_Tdb() == this) {
if (colp->GetAmType() == TYPE_AM_ROWID) {
// Currently we don't know how to retrieve a RowID
// from a DBF table that is not sequentially read.
// if (Txfp->GetAmType() != TYPE_AM_DBF ||
// ((RIDBLK*)arg[i])->GetRnm())
type[i] = 5;
} else if (Txfp->Blocked && Txfp->Nrec > 1 &&
colp->IsClustered()) {
type[i] = 2;
xdb2 = colp->GetClustered() == 2;
} // endif Clustered
} else if (colp->GetColUse(U_CORREL)) {
// This is a column pointing to the outer query of a
// correlated subquery, it has a constant value during
// each execution of the subquery.
type[i] = 1;
// ctype = arg[i]->GetResultType();
} // endif this
break;
// case TYPE_SCALF:
// if (((PSCALF)arg[i])->GetOp() == OP_ROW) {
// sfr[i] = (SFROW*)arg[i];
// type[i] = 7;
// } // endif Op
// break;
default:
break;
} // endswitch ArgType
if (!type[i])
break;
n += type[i];
} // endfor i
if (n == 3 || n == 6) {
if (conv) {
// The constant has not the good type and will not match
// the block min/max values. Warn and abort.
snprintf(g->Message, sizeof(g->Message), "Block opt: %s", MSG(VALTYPE_NOMATCH));
PushWarning(g, this);
return NULL;
} // endif Conv
if (type[0] == 1) {
// Make it always as Column-op-Value
*xp = arg[0];
arg[0] = arg[1];
arg[1] = *xp;
switch (op) {
case OP_GT: op = OP_LT; break;
case OP_GE: op = OP_LE; break;
case OP_LT: op = OP_GT; break;
case OP_LE: op = OP_GE; break;
} // endswitch op
} // endif
#if defined(_DEBUG)
// assert(arg[0]->GetResultType() == ctype);
#endif
if (n == 3) {
if (xdb2) {
if (((PDOSCOL)arg[0])->GetNbm() == 1)
bfp = new(g) BLKFILAR2(g, this, op, arg);
else // Multiple bitmap made of several ULONG's
bfp = new(g) BLKFILMR2(g, this, op, arg);
} else
bfp = new(g) BLKFILARI(g, this, op, arg);
} else // n = 6
bfp = new(g) BLKSPCARI(this, op, arg, Txfp->Nrec);
#if 0
} else if (n == 8 || n == 14) {
if (n == 8 && ctype != TYPE_LIST) {
// Should never happen
safe_strcpy(g->Message, sizeof(g->Message), "Block opt: bad constant");
throw 99;
} // endif Conv
if (type[0] == 1) {
// Make it always as Column-op-Value
sfr[0] = sfr[1];
arg[1] = arg[0];
switch (op) {
case OP_GT: op = OP_LT; break;
case OP_GE: op = OP_LE; break;
case OP_LT: op = OP_GT; break;
case OP_LE: op = OP_GE; break;
} // endswitch op
} // endif
xarg1 = sfr[0]->GetParms(n1);
if (n == 8) {
vlp = (LSTVAL*)arg[1]->GetValue();
n2 = vlp->GetN();
xp[1] = new(g) CONSTANT((PVAL)NULL);
} else
xarg2 = sfr[1]->GetParms(n2);
if (n1 != n2)
return NULL; // Should we flag an error ?
fp = (PBF*)PlugSubAlloc(g, NULL, n1 * sizeof(PBF));
for (i = 0; i < n1; i++) {
xp[0] = xarg1[i];
if (n == 8)
((CONSTANT*)xp[1])->SetValue(vlp->GetSubVal(i));
else
xp[1] = xarg2[i];
b[0] = b[1] = (xp[0]->GetResultType() != xp[1]->GetResultType());
ok |= ((fp[i] = CheckBlockFilari(g, xp, op, b)) != NULL);
} // endfor i
if (ok)
bfp = new(g) BLKFILLOG(this, OP_AND, fp, n1);
#endif // 0
} // endif n
return bfp;
} // end of CheckBlockFilari
/***********************************************************************/
/* ResetBlkFil: reset the block filter and restore filtering, or make */
/* the block filter if To_Filter was not set when opening the table. */
/***********************************************************************/
void TDBDOS::ResetBlockFilter(PGLOBAL g)
{
if (!To_BlkFil) {
if (To_Filter)
if ((To_BlkFil = InitBlockFilter(g, To_Filter))) {
htrc("BlkFil=%p\n", To_BlkFil);
MaxSize = -1; // To be recalculated
} // endif To_BlkFil
return;
} // endif To_BlkFil
To_BlkFil->Reset(g);
if (SavFil && !To_Filter) {
// Restore filter if it was disabled by optimization
To_Filter = SavFil;
SavFil = NULL;
} // endif
Beval = 0;
} // end of ResetBlockFilter
/***********************************************************************/
/* Block optimization: evaluate the block index filter against */
/* the min and max values of this block and return: */
/* RC_OK: if some records in the block can meet filter criteria. */
/* RC_NF: if no record in the block can meet filter criteria. */
/* RC_EF: if no record in the remaining file can meet filter criteria.*/
/* In addition, temporarily supress filtering if all the records in */
/* the block meet filter criteria. */
/***********************************************************************/
int TDBDOS::TestBlock(PGLOBAL g)
{
int rc = RC_OK;
if (To_BlkFil && Beval != 2) {
// Check for block filtering evaluation
if (Beval == 1) {
// Filter was removed for last block, restore it
To_Filter = SavFil;
SavFil = NULL;
} // endif Beval
// Check for valid records in new block
switch (Beval = To_BlkFil->BlockEval(g)) {
case -2: // No more valid values in file
rc = RC_EF;
break;
case -1: // No valid values in block
rc = RC_NF;
break;
case 1: // All block values are valid
case 2: // All subsequent file values are Ok
// Before suppressing the filter for the block(s) it is
// necessary to reset the filtered columns to NOT_READ
// so their new values are retrieved by the SELECT list.
if (To_Filter) // Can be NULL when externally called (XDB)
To_Filter->Reset();
SavFil = To_Filter;
To_Filter = NULL; // So remove filter
} // endswitch Beval
if (trace(1))
htrc("BF Eval Beval=%d\n", Beval);
} // endif To_BlkFil
return rc;
} // end of TestBlock
/***********************************************************************/
/* Check whether we have to create/update permanent indexes. */
/***********************************************************************/
int TDBDOS::MakeIndex(PGLOBAL g, PIXDEF pxdf, bool add)
{
int k, n, rc = RC_OK;
bool fixed, doit, sep;
PCOL *keycols, colp;
PIXDEF xdp, sxp = NULL;
PKPDEF kdp;
PDOSDEF dfp;
//PCOLDEF cdp;
PXINDEX x;
PXLOAD pxp;
Mode = MODE_READ;
Use = USE_READY;
dfp = (PDOSDEF)To_Def;
if (!Cardinality(g)) {
// Void table erase eventual index file(s)
(void)dfp->DeleteIndexFile(g, NULL);
return RC_OK;
} else
fixed = Ftype != RECFM_VAR;
// Are we are called from CreateTable or CreateIndex?
if (pxdf) {
if (!add && dfp->GetIndx()) {
safe_strcpy(g->Message, sizeof(g->Message), MSG(INDX_EXIST_YET));
return RC_FX;
} // endif To_Indx
if (add && dfp->GetIndx()) {
for (sxp = dfp->GetIndx(); sxp; sxp = sxp->GetNext())
if (!stricmp(sxp->GetName(), pxdf->GetName())) {
snprintf(g->Message, sizeof(g->Message), MSG(INDEX_YET_ON), pxdf->GetName(), Name);
return RC_FX;
} else if (!sxp->GetNext())
break;
sxp->SetNext(pxdf);
// first = false;
} else
dfp->SetIndx(pxdf);
// pxdf->SetDef(dfp);
} else if (!(pxdf = dfp->GetIndx()))
return RC_INFO; // No index to make
try {
// Allocate all columns that will be used by indexes.
// This must be done before opening the table so specific
// column initialization can be done (in particular by TDBVCT)
for (n = 0, xdp = pxdf; xdp; xdp = xdp->GetNext())
for (kdp = xdp->GetToKeyParts(); kdp; kdp = kdp->GetNext()) {
if (!(colp = ColDB(g, kdp->GetName(), 0))) {
snprintf(g->Message, sizeof(g->Message), MSG(INDX_COL_NOTIN), kdp->GetName(), Name);
goto err;
} else if (colp->GetResultType() == TYPE_DECIM) {
snprintf(g->Message, sizeof(g->Message), "Decimal columns are not indexable yet");
goto err;
} // endif Type
colp->InitValue(g);
n = MY_MAX(n, xdp->GetNparts());
} // endfor kdp
keycols = (PCOL*)PlugSubAlloc(g, NULL, n * sizeof(PCOL));
sep = dfp->GetBoolCatInfo("SepIndex", false);
/*********************************************************************/
/* Construct and save the defined indexes. */
/*********************************************************************/
for (xdp = pxdf; xdp; xdp = xdp->GetNext())
if (!OpenDB(g)) {
if (xdp->IsAuto() && fixed)
// Auto increment key and fixed file: use an XXROW index
continue; // XXROW index doesn't need to be made
// On Update, redo only indexes that are modified
doit = !To_SetCols;
n = 0;
if (sxp)
xdp->SetID(sxp->GetID() + 1);
for (kdp = xdp->GetToKeyParts(); kdp; kdp = kdp->GetNext()) {
// Check whether this column was updated
for (colp = To_SetCols; !doit && colp; colp = colp->GetNext())
if (!stricmp(kdp->GetName(), colp->GetName()))
doit = true;
keycols[n++] = ColDB(g, kdp->GetName(), 0);
} // endfor kdp
// If no indexed columns were updated, don't remake the index
// if indexes are in separate files.
if (!doit && sep)
continue;
k = xdp->GetNparts();
// Make the index and save it
if (dfp->Huge)
pxp = new(g) XHUGE;
else
pxp = new(g) XFILE;
if (k == 1) // Simple index
x = new(g) XINDXS(this, xdp, pxp, keycols);
else // Multi-Column index
x = new(g) XINDEX(this, xdp, pxp, keycols);
if (!x->Make(g, sxp)) {
// Retreive define values from the index
xdp->SetMaxSame(x->GetMaxSame());
// xdp->SetSize(x->GetSize());
// store KXYCOL Mxs in KPARTDEF Mxsame
xdp->SetMxsame(x);
#if defined(TRACE)
printf("Make done...\n");
#endif // TRACE
// if (x->GetSize() > 0)
sxp = xdp;
xdp->SetInvalid(false);
} else
goto err;
} else
return RC_INFO; // Error or Physical table does not exist
} catch (int n) {
if (trace(1))
htrc("Exception %d: %s\n", n, g->Message);
rc = RC_FX;
} catch (const char *msg) {
safe_strcpy(g->Message, sizeof(g->Message), msg);
rc = RC_FX;
} // end catch
if (Use == USE_OPEN)
CloseDB(g);
return rc;
err:
if (sxp)
sxp->SetNext(NULL);
else
dfp->SetIndx(NULL);
return RC_FX;
} // end of MakeIndex
/***********************************************************************/
/* Make a dynamic index. */
/***********************************************************************/
bool TDBDOS::InitialyzeIndex(PGLOBAL g, volatile PIXDEF xdp, bool sorted)
{
int k;
volatile bool dynamic;
bool brc;
PCOL colp;
PCOLDEF cdp;
PVAL valp;
PXLOAD pxp;
volatile PKXBASE kxp;
PKPDEF kdp;
if (!xdp && !(xdp = To_Xdp)) {
safe_strcpy(g->Message, sizeof(g->Message), "NULL dynamic index");
return true;
} else
dynamic = To_Filter && xdp->IsUnique() && xdp->IsDynamic();
// dynamic = To_Filter && xdp->IsDynamic(); NIY
// Allocate the key columns definition block
Knum = xdp->GetNparts();
To_Key_Col = (PCOL*)PlugSubAlloc(g, NULL, Knum * sizeof(PCOL));
// Get the key column description list
for (k = 0, kdp = xdp->GetToKeyParts(); kdp; kdp = kdp->GetNext())
if (!(colp = ColDB(g, kdp->GetName(), 0)) || colp->InitValue(g)) {
snprintf(g->Message, sizeof(g->Message), "Wrong column %s", kdp->GetName());
return true;
} else
To_Key_Col[k++] = colp;
#if defined(_DEBUG)
if (k != Knum) {
snprintf(g->Message, sizeof(g->Message), "Key part number mismatch for %s",
xdp->GetName());
return 0;
} // endif k
#endif // _DEBUG
// Allocate the pseudo constants that will contain the key values
To_Link = (PXOB*)PlugSubAlloc(g, NULL, Knum * sizeof(PXOB));
for (k = 0, kdp = xdp->GetToKeyParts(); kdp; k++, kdp = kdp->GetNext()) {
if ((cdp = Key(k)->GetCdp()))
valp = AllocateValue(g, cdp->GetType(), cdp->GetLength());
else { // Special column ?
colp = Key(k);
valp = AllocateValue(g, colp->GetResultType(), colp->GetLength());
} // endif cdp
To_Link[k]= new(g) CONSTANT(valp);
} // endfor k
// Make the index on xdp
if (!xdp->IsAuto()) {
if (!dynamic) {
if (((PDOSDEF)To_Def)->Huge)
pxp = new(g) XHUGE;
else
pxp = new(g) XFILE;
} else
pxp = NULL;
if (Knum == 1) // Single index
kxp = new(g) XINDXS(this, xdp, pxp, To_Key_Col, To_Link);
else // Multi-Column index
kxp = new(g) XINDEX(this, xdp, pxp, To_Key_Col, To_Link);
} else // Column contains same values as ROWID
kxp = new(g) XXROW(this);
try {
if (dynamic) {
ResetBlockFilter(g);
kxp->SetDynamic(dynamic);
brc = kxp->Make(g, xdp);
} else
brc = kxp->Init(g);
if (!brc) {
if (Txfp->GetAmType() == TYPE_AM_BLK) {
// Cannot use indexing in DOS block mode
Txfp = new(g) DOSFAM((PBLKFAM)Txfp, (PDOSDEF)To_Def);
Txfp->AllocateBuffer(g);
To_BlkFil = NULL;
} // endif AmType
To_Kindex= kxp;
if (!(sorted && To_Kindex->IsSorted()) &&
((Mode == MODE_UPDATE && IsUsingTemp(g)) ||
(Mode == MODE_DELETE && Txfp->GetAmType() != TYPE_AM_DBF)))
Indxd = true;
} // endif brc
} catch (int n) {
if (trace(1))
htrc("Exception %d: %s\n", n, g->Message);
brc = true;
} catch (const char *msg) {
safe_strcpy(g->Message, sizeof(g->Message), msg);
brc = true;
} // end catch
return brc;
} // end of InitialyzeIndex
/***********************************************************************/
/* DOS GetProgMax: get the max value for progress information. */
/***********************************************************************/
int TDBDOS::GetProgMax(PGLOBAL g)
{
return (To_Kindex) ? GetMaxSize(g) : GetFileLength(g);
} // end of GetProgMax
/***********************************************************************/
/* DOS GetProgCur: get the current value for progress information. */
/***********************************************************************/
int TDBDOS::GetProgCur(void)
{
return (To_Kindex) ? To_Kindex->GetCur_K() + 1 : GetRecpos();
} // end of GetProgCur
/***********************************************************************/
/* RowNumber: return the ordinal number of the current row. */
/***********************************************************************/
int TDBDOS::RowNumber(PGLOBAL g, bool)
{
if (To_Kindex) {
/*******************************************************************/
/* Don't know how to retrieve RowID from file address. */
/*******************************************************************/
snprintf(g->Message, sizeof(g->Message), MSG(NO_ROWID_FOR_AM),
GetAmName(g, Txfp->GetAmType()));
return 0;
} else
return Txfp->GetRowID();
} // end of RowNumber
/***********************************************************************/
/* DOS 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 TDBDOS::Cardinality(PGLOBAL g)
{
int n = Txfp->Cardinality(NULL);
if (!g)
return (Mode == MODE_ANY) ? 1 : n;
if (Cardinal < 0) {
if (!Txfp->Blocked && n == 0) {
// Info command, we try to return exact row number
PDOSDEF dfp = (PDOSDEF)To_Def;
PIXDEF xdp = dfp->To_Indx;
if (xdp && xdp->IsValid()) {
// Cardinality can be retreived from one index
PXLOAD pxp;
if (dfp->Huge)
pxp = new(g) XHUGE;
else
pxp = new(g) XFILE;
PXINDEX kxp = new(g) XINDEX(this, xdp, pxp, NULL, NULL);
if (!(kxp->GetAllSizes(g, Cardinal)))
return Cardinal;
} // endif Mode
if (Mode == MODE_ANY && ExactInfo()) {
// Using index impossible or failed, do it the hard way
Mode = MODE_READ;
To_Line = (char*)PlugSubAlloc(g, NULL, (size_t)Lrecl + 1);
if (Txfp->OpenTableFile(g))
return (Cardinal = Txfp->Cardinality(g));
for (Cardinal = 0; n != RC_EF;)
if (!(n = Txfp->ReadBuffer(g)))
Cardinal++;
Txfp->CloseTableFile(g, false);
Mode = MODE_ANY;
} else {
// Return the best estimate
int len = GetFileLength(g);
if (len >= 0) {
int rec;
if (trace(1))
htrc("Estimating lines len=%d ending=%d/n",
len, ((PDOSDEF)To_Def)->Ending);
/*************************************************************/
/* Estimate the number of lines in the table (if not known) */
/* by dividing the file length by the average record length. */
/*************************************************************/
rec = ((PDOSDEF)To_Def)->Ending;
if (AvgLen <= 0) // No given average estimate
rec += EstimatedLength();
else // An estimate was given for the average record length
rec += AvgLen;
Cardinal = (len + rec - 1) / rec;
if (trace(1))
htrc("avglen=%d MaxSize%d\n", rec, Cardinal);
} // endif len
} // endif Mode
} else
Cardinal = Txfp->Cardinality(g);
} // endif Cardinal
return Cardinal;
} // end of Cardinality
/***********************************************************************/
/* DOS GetMaxSize: returns file size estimate in number of lines. */
/* This function covers variable record length files. */
/***********************************************************************/
int TDBDOS::GetMaxSize(PGLOBAL g)
{
if (MaxSize >= 0)
return MaxSize;
if (!Cardinality(NULL)) {
int len = GetFileLength(g);
if (len >= 0) {
int rec;
if (trace(1))
htrc("Estimating lines len=%d ending=%d/n",
len, ((PDOSDEF)To_Def)->Ending);
/*****************************************************************/
/* Estimate the number of lines in the table (if not known) by */
/* dividing the file length by minimum record length. */
/*****************************************************************/
rec = EstimatedLength() + ((PDOSDEF)To_Def)->Ending;
MaxSize = (len + rec - 1) / rec;
if (trace(1))
htrc("avglen=%d MaxSize%d\n", rec, MaxSize);
} // endif len
} else
MaxSize = Cardinality(g);
return MaxSize;
} // end of GetMaxSize
/***********************************************************************/
/* DOS EstimatedLength. Returns an estimated minimum line length. */
/***********************************************************************/
int TDBDOS::EstimatedLength(void)
{
int dep = 0;
PCOLDEF cdp = To_Def->GetCols();
if (!cdp->GetNext()) {
// One column table, we are going to return a ridiculous
// result if we set dep to 1
dep = 1 + cdp->GetLong() / 20; // Why 20 ?????
} else for (; cdp; cdp = cdp->GetNext())
if (!(cdp->Flags & (U_VIRTUAL|U_SPECIAL)))
dep = MY_MAX(dep, cdp->GetOffset());
return (int)dep;
} // end of Estimated Length
/***********************************************************************/
/* DOS tables favor the use temporary files for Update. */
/***********************************************************************/
bool TDBDOS::IsUsingTemp(PGLOBAL)
{
USETEMP utp = UseTemp();
return (utp == TMP_YES || utp == TMP_FORCE ||
(utp == TMP_AUTO && Mode == MODE_UPDATE));
} // end of IsUsingTemp
/***********************************************************************/
/* DOS 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 TDBDOS::OpenDB(PGLOBAL g)
{
if (trace(1))
htrc("DOS OpenDB: tdbp=%p tdb=R%d use=%d mode=%d\n",
this, Tdb_No, Use, Mode);
if (Use == USE_OPEN) {
/*******************************************************************/
/* Table already open, just replace it at its beginning. */
/*******************************************************************/
if (!To_Kindex) {
Txfp->Rewind(); // see comment in Work.log
if (SkipHeader(g))
return true;
} else
/*****************************************************************/
/* Table is to be accessed through a sorted index table. */
/*****************************************************************/
To_Kindex->Reset();
ResetBlockFilter(g);
return false;
} // endif use
if (Mode == MODE_DELETE && !Next && Txfp->GetAmType() != TYPE_AM_DOS
#if defined(BSON_SUPPORT)
&& Txfp->GetAmType() != TYPE_AM_BIN
#endif // BSON_SUPPORT
&& Txfp->GetAmType() != TYPE_AM_MGO) {
// Delete all lines. Not handled in MAP or block mode
Txfp = new(g) DOSFAM((PDOSDEF)To_Def);
Txfp->SetTdbp(this);
} else if (Txfp->Blocked && (Mode == MODE_DELETE ||
(Mode == MODE_UPDATE && UseTemp() != TMP_NO))) {
/*******************************************************************/
/* Delete is not currently handled in block mode neither Update */
/* when using a temporary file. */
/*******************************************************************/
if (Txfp->GetAmType() == TYPE_AM_MAP && Mode == MODE_DELETE)
Txfp = new(g) MAPFAM((PDOSDEF)To_Def);
#if defined(GZ_SUPPORT)
else if (Txfp->GetAmType() == TYPE_AM_GZ)
Txfp = new(g) GZFAM((PDOSDEF)To_Def);
#endif // GZ_SUPPORT
else // if (Txfp->GetAmType() != TYPE_AM_DOS) ???
Txfp = new(g) DOSFAM((PDOSDEF)To_Def);
Txfp->SetTdbp(this);
} // endif Mode
/*********************************************************************/
/* Open according to logical input/output mode required. */
/* Use conventionnal input/output functions. */
/* Treat files as binary in Delete mode (for line moving) */
/*********************************************************************/
if (Txfp->OpenTableFile(g))
return true;
Use = USE_OPEN; // Do it now in case we are recursively called
/*********************************************************************/
/* Allocate the block filter tree if evaluation is possible. */
/*********************************************************************/
To_BlkFil = InitBlockFilter(g, To_Filter);
/*********************************************************************/
/* Lrecl does not include line ending */
/*********************************************************************/
size_t linelen = Lrecl + ((PDOSDEF)To_Def)->Ending + 1;
To_Line = (char*)PlugSubAlloc(g, NULL, linelen);
if (Mode == MODE_INSERT) {
// Spaces between fields must be filled with blanks
memset(To_Line, ' ', Lrecl);
To_Line[Lrecl] = '\0';
} else
memset(To_Line, 0, linelen);
if (trace(1))
htrc("OpenDos: R%hd mode=%d To_Line=%p\n", Tdb_No, Mode, To_Line);
if (SkipHeader(g)) // When called from CSV/FMT files
return true;
/*********************************************************************/
/* Reset statistics values. */
/*********************************************************************/
num_read = num_there = num_eq[0] = num_eq[1] = 0;
return false;
} // end of OpenDB
/***********************************************************************/
/* ReadDB: Data Base read routine for DOS access method. */
/***********************************************************************/
int TDBDOS::ReadDB(PGLOBAL g)
{
if (trace(2))
htrc("DOS ReadDB: R%d Mode=%d key=%p link=%p Kindex=%p To_Line=%p\n",
GetTdb_No(), Mode, To_Key_Col, To_Link, To_Kindex, To_Line);
if (To_Kindex) {
/*******************************************************************/
/* Reading is by an index table. */
/*******************************************************************/
int recpos = To_Kindex->Fetch(g);
switch (recpos) {
case -1: // End of file reached
return RC_EF;
case -2: // No match for join
return RC_NF;
case -3: // Same record as non null last one
num_there++;
return RC_OK;
default:
/***************************************************************/
/* Set the file position according to record to read. */
/***************************************************************/
if (SetRecpos(g, recpos))
return RC_FX;
if (trace(2))
htrc("File position is now %d\n", GetRecpos());
if (Mode == MODE_READ)
/*************************************************************/
/* Defer physical reading until one column setting needs it */
/* as it can be a big saving on joins where no other column */
/* than the keys are used, so reading is unnecessary. */
/*************************************************************/
if (Txfp->DeferReading())
return RC_OK;
} // endswitch recpos
} // endif To_Kindex
if (trace(2))
htrc(" ReadDB: this=%p To_Line=%p\n", this, To_Line);
/*********************************************************************/
/* Now start the reading process. */
/*********************************************************************/
return ReadBuffer(g);
} // end of ReadDB
/***********************************************************************/
/* PrepareWriting: Prepare the line to write. */
/***********************************************************************/
bool TDBDOS::PrepareWriting(PGLOBAL)
{
if (Ftype == RECFM_VAR && (Mode == MODE_INSERT || Txfp->GetUseTemp())) {
char *p;
/*******************************************************************/
/* Suppress trailing blanks. */
/* Also suppress eventual null from last line. */
/*******************************************************************/
for (p = To_Line + Lrecl -1; p >= To_Line; p--)
if (*p && *p != ' ')
break;
*(++p) = '\0';
} // endif Mode
return false;
} // end of PrepareWriting
/***********************************************************************/
/* WriteDB: Data Base write routine for DOS access method. */
/***********************************************************************/
int TDBDOS::WriteDB(PGLOBAL g)
{
if (trace(2))
htrc("DOS WriteDB: R%d Mode=%d \n", Tdb_No, Mode);
// Make the line to write
if (PrepareWriting(g))
return RC_FX;
if (trace(2))
htrc("Write: line is='%s'\n", To_Line);
// Now start the writing process
return Txfp->WriteBuffer(g);
} // end of WriteDB
/***********************************************************************/
/* Data Base delete line routine for DOS (and FIX) access method. */
/* RC_FX means delete all. Nothing to do here (was done at open). */
/***********************************************************************/
int TDBDOS::DeleteDB(PGLOBAL g, int irc)
{
return (irc == RC_FX) ? RC_OK : Txfp->DeleteRecords(g, irc);
} // end of DeleteDB
/***********************************************************************/
/* Data Base close routine for DOS access method. */
/***********************************************************************/
void TDBDOS::CloseDB(PGLOBAL g)
{
if (To_Kindex) {
To_Kindex->Close();
To_Kindex = NULL;
} // endif
Txfp->CloseTableFile(g, Abort);
RestoreNrec();
} // end of CloseDB
// ------------------------ DOSCOL functions ----------------------------
/***********************************************************************/
/* DOSCOL public constructor (also called by MAPCOL). */
/***********************************************************************/
DOSCOL::DOSCOL(PGLOBAL g, PCOLDEF cdp, PTDB tp, PCOL cp, int i, PCSZ am)
: COLBLK(cdp, tp, i)
{
char *p;
int prec = Format.Prec;
PTXF txfp = ((PTDBDOS)tp)->Txfp;
assert(cdp);
if (cp) {
Next = cp->GetNext();
cp->SetNext(this);
} else {
Next = tp->GetColumns();
tp->SetColumns(this);
} // endif cprec
// Set additional Dos access method information for column.
Deplac = cdp->GetOffset();
Long = cdp->GetLong();
To_Val = NULL;
Clustered = cdp->GetOpt();
Sorted = (cdp->GetOpt() == 2) ? 1 : 0;
Ndv = 0; // Currently used only for XDB2
Nbm = 0; // Currently used only for XDB2
Min = NULL;
Max = NULL;
Bmap = NULL;
Dval = NULL;
Buf = NULL;
if (txfp && txfp->Blocked && Opt && (cdp->GetMin() || cdp->GetDval())) {
int nblk = txfp->GetBlock();
Clustered = (cdp->GetXdb2()) ? 2 : 1;
Sorted = (cdp->GetOpt() > 1) ? 1 : 0; // Currently ascending only
if (Clustered == 1) {
Min = AllocValBlock(g, cdp->GetMin(), Buf_Type, nblk, Long, prec);
Max = AllocValBlock(g, cdp->GetMax(), Buf_Type, nblk, Long, prec);
} else { // Clustered == 2
// Ndv is the number of distinct values in Dval. Ndv and Nbm
// may be 0 when optimizing because Ndval is not filled yet,
// but the size of the passed Dval memory block is Ok.
Ndv = cdp->GetNdv();
Dval = AllocValBlock(g, cdp->GetDval(), Buf_Type, Ndv, Long, prec);
// Bmap cannot be allocated when optimizing, we must know Nbm first
if ((Nbm = cdp->GetNbm()))
Bmap = AllocValBlock(g, cdp->GetBmap(), TYPE_INT, Nbm * nblk);
} // endif Clustered
} // endif Opt
OldVal = NULL; // Currently used only in MinMax
Dsp = 0;
Ldz = false;
Nod = false;
Dcm = -1;
p = cdp->GetFmt();
Buf = NULL;
if (p && IsTypeNum(Buf_Type)) {
// Formatted numeric value
for (; p && *p && isalpha(*p); p++)
switch (toupper(*p)) {
case 'Z': // Have leading zeros
Ldz = true;
break;
case 'N': // Have no decimal point
Nod = true;
break;
case 'D': // Decimal separator
Dsp = *(++p);
break;
} // endswitch p
// Set number of decimal digits
Dcm = (*p) ? atoi(p) : GetScale();
} // endif fmt
if (trace(1))
htrc(" making new %sCOL C%d %s at %p\n", am, Index, Name, this);
} // end of DOSCOL constructor
/***********************************************************************/
/* DOSCOL constructor used for copying columns. */
/* tdbp is the pointer to the new table descriptor. */
/***********************************************************************/
DOSCOL::DOSCOL(DOSCOL *col1, PTDB tdbp) : COLBLK(col1, tdbp)
{
Deplac = col1->Deplac;
Long = col1->Long;
To_Val = col1->To_Val;
Ldz = col1->Ldz;
Dsp = col1->Dsp;
Nod = col1->Nod;
Dcm = col1->Dcm;
OldVal = col1->OldVal;
Buf = col1->Buf;
Clustered = col1->Clustered;
Sorted = col1->Sorted;
Min = col1->Min;
Max = col1->Max;
Bmap = col1->Bmap;
Dval = col1->Dval;
Ndv = col1->Ndv;
Nbm = col1->Nbm;
} // end of DOSCOL copy constructor
/***********************************************************************/
/* VarSize: This function tells UpdateDB whether or not the block */
/* optimization file must be redone if this column is updated, even */
/* it is not sorted or clustered. This applies to the last column of */
/* a variable length table that is blocked, because if it is updated */
/* using a temporary file, the block size may be modified. */
/***********************************************************************/
bool DOSCOL::VarSize(void)
{
PTDBDOS tdbp = (PTDBDOS)To_Tdb;
PTXF txfp = tdbp->Txfp;
if (Cdp && !Cdp->GetNext() // Must be the last column
&& tdbp->Ftype == RECFM_VAR // of a DOS variable length
&& txfp->Blocked // blocked table
&& txfp->GetUseTemp()) // using a temporary file.
return true;
else
return false;
} // end VarSize
/***********************************************************************/
/* SetBuffer: prepare a column block for write operation. */
/***********************************************************************/
bool DOSCOL::SetBuffer(PGLOBAL g, PVAL value, bool ok, bool check)
{
if (!(To_Val = value)) {
snprintf(g->Message, sizeof(g->Message), MSG(VALUE_ERROR), Name);
return true;
} else if (Buf_Type == value->GetType()) {
// Values are of the (good) column type
if (Buf_Type == TYPE_DATE) {
// If any of the date values is formatted
// output format must be set for the receiving table
if (GetDomain() || ((DTVAL *)value)->IsFormatted())
goto newval; // This will make a new value;
} else if (Buf_Type == TYPE_DOUBLE)
// Float values must be written with the correct (column) precision
// Note: maybe this should be forced by ShowValue instead of this ?
value->SetPrec(GetScale());
Value = value; // Directly access the external value
} else {
// Values are not of the (good) column type
if (check) {
snprintf(g->Message, sizeof(g->Message), MSG(TYPE_VALUE_ERR), Name,
GetTypeName(Buf_Type), GetTypeName(value->GetType()));
return true;
} // endif check
newval:
if (InitValue(g)) // Allocate the matching value block
return true;
} // endif's Value, Buf_Type
// Allocate the buffer used in WriteColumn for numeric columns
if (!Buf && IsTypeNum(Buf_Type))
Buf = (char*)PlugSubAlloc(g, NULL, MY_MAX(64, Long + 1));
else // Text columns do not need additional buffer
Buf = (char*)Value->GetTo_Val();
// Because Colblk's have been made from a copy of the original TDB in
// case of Update, we must reset them to point to the original one.
if (To_Tdb->GetOrig())
To_Tdb = (PTDB)To_Tdb->GetOrig();
// Set the Column
Status = (ok) ? BUF_EMPTY : BUF_NO;
return false;
} // end of SetBuffer
/***********************************************************************/
/* ReadColumn: what this routine does is to access the last line */
/* read from the corresponding table, extract from it the field */
/* corresponding to this column and convert it to buffer type. */
/***********************************************************************/
void DOSCOL::ReadColumn(PGLOBAL g)
{
char *p = NULL;
int i, rc;
int field;
bool err = false;
double dval;
PTDBDOS tdbp = (PTDBDOS)To_Tdb;
if (trace(2))
htrc(
"DOS ReadColumn: col %s R%d coluse=%.4X status=%.4X buf_type=%d\n",
Name, tdbp->GetTdb_No(), ColUse, Status, Buf_Type);
/*********************************************************************/
/* If physical reading of the line was deferred, do it now. */
/*********************************************************************/
if (!tdbp->IsRead())
if ((rc = tdbp->ReadBuffer(g)) != RC_OK) {
if (rc == RC_EF)
snprintf(g->Message, sizeof(g->Message), MSG(INV_DEF_READ), rc);
throw 11;
} // endif
p = tdbp->To_Line + Deplac;
field = Long;
/*********************************************************************/
/* For a variable length file, check if the field exists. */
/*********************************************************************/
if ((tdbp->Ftype == RECFM_VAR || tdbp->Ftype == RECFM_CSV)
&& strlen(tdbp->To_Line) < (unsigned)Deplac)
field = 0;
else if (Dsp)
for(i = 0; i < field; i++)
if (p[i] == Dsp)
p[i] = '.';
switch (tdbp->Ftype) {
case RECFM_VAR:
case RECFM_FIX: // Fixed length text file
case RECFM_CSV: // Variable length CSV or FMT file
case RECFM_DBF: // Fixed length DBase file
if (Nod) switch (Buf_Type) {
case TYPE_INT:
case TYPE_SHORT:
case TYPE_TINY:
case TYPE_BIGINT:
err = Value->SetValue_char(p, field - Dcm);
break;
case TYPE_DOUBLE:
if (!(err = Value->SetValue_char(p, field))) {
dval = Value->GetFloatValue();
for (i = 0; i < Dcm; i++)
dval /= 10.0;
Value->SetValue(dval);
} // endif err
break;
default:
err = Value->SetValue_char(p, field);
if (!err && Buf_Type == TYPE_DECIM) {
char* s = Value->GetCharValue();
if (!(err = ((i = strlen(s)) >= Value->GetClen()))) {
for (int d = Dcm + 1; d; i--, d--)
s[i + 1] = s[i];
s[i + 1] = '.';
} // endif err
} // endif DECIM
break;
} // endswitch Buf_Type
else
err = Value->SetValue_char(p, field);
break;
default:
snprintf(g->Message, sizeof(g->Message), MSG(BAD_RECFM), tdbp->Ftype);
throw 34;
} // endswitch Ftype
if (err) {
snprintf(g->Message, sizeof(g->Message), "Out of range value for column %s at row %d",
Name, tdbp->RowNumber(g));
PushWarning(g, tdbp);
} // endif err
// Set null when applicable
if (Nullable)
Value->SetNull(Value->IsZero());
} // end of ReadColumn
/***********************************************************************/
/* WriteColumn: what this routine does is to access the last line */
/* read from the corresponding table, and rewrite the field */
/* corresponding to this column from the column buffer and type. */
/***********************************************************************/
void DOSCOL::WriteColumn(PGLOBAL g)
{
char *p, fmt[32];
int i, k, n, len, field;
PTDBDOS tdbp = (PTDBDOS)To_Tdb;
if (trace(2))
htrc("DOS WriteColumn: col %s R%d coluse=%.4X status=%.4X\n",
Name, tdbp->GetTdb_No(), ColUse, Status);
p = tdbp->To_Line + Deplac;
if (trace(2))
htrc("Lrecl=%d deplac=%d int=%d\n", tdbp->Lrecl, Deplac, Long);
field = Long;
if (tdbp->Ftype == RECFM_VAR && tdbp->Mode == MODE_UPDATE) {
len = (signed)strlen(tdbp->To_Line);
if (tdbp->IsUsingTemp(g))
// Because of eventual missing field(s) the buffer must be reset
memset(tdbp->To_Line + len, ' ', tdbp->Lrecl - len);
else
// The size actually available must be recalculated
field = MY_MIN(len - Deplac, Long);
} // endif Ftype
if (trace(2))
htrc("Long=%d field=%d coltype=%d colval=%p\n",
Long, field, Buf_Type, Value);
/*********************************************************************/
/* Get the string representation of Value according to column type. */
/*********************************************************************/
if (Value != To_Val)
Value->SetValue_pval(To_Val, false); // Convert the updated value
/*********************************************************************/
/* This test is only useful for compressed(2) tables. */
/*********************************************************************/
if (tdbp->Ftype != RECFM_BIN) {
if (Ldz || Nod || Dcm >= 0) {
switch (Buf_Type) {
case TYPE_SHORT:
safe_strcpy(fmt, sizeof(fmt), (Ldz) ? "%0*hd" : "%*.hd");
i = 0;
if (Nod)
for (; i < Dcm; i++)
safe_strcat(fmt, sizeof(fmt), "0");
len = sprintf(Buf, fmt, field - i, Value->GetShortValue());
break;
case TYPE_INT:
safe_strcpy(fmt, sizeof(fmt), (Ldz) ? "%0*d" : "%*.d");
i = 0;
if (Nod)
for (; i < Dcm; i++)
safe_strcat(fmt,sizeof(fmt), "0");
len = sprintf(Buf, fmt, field - i, Value->GetIntValue());
break;
case TYPE_TINY:
safe_strcpy(fmt, sizeof(fmt), (Ldz) ? "%0*d" : "%*.d");
i = 0;
if (Nod)
for (; i < Dcm; i++)
safe_strcat(fmt, sizeof(fmt), "0");
len = sprintf(Buf, fmt, field - i, Value->GetTinyValue());
break;
case TYPE_DOUBLE:
case TYPE_DECIM:
safe_strcpy(fmt, sizeof(fmt), (Ldz) ? "%0*.*lf" : "%*.*lf");
len = field + ((Nod && Dcm) ? 1 : 0);
snprintf(Buf, len + 1, fmt, len, Dcm, Value->GetFloatValue());
len = strlen(Buf);
if (Nod && Dcm)
for (i = k = 0; i < len; i++, k++)
if (Buf[i] != ' ') {
if (Buf[i] == '.')
k++;
Buf[i] = Buf[k];
} // endif Buf(i)
len = strlen(Buf);
break;
default:
snprintf(g->Message, sizeof(g->Message), "Invalid field format for column %s", Name);
throw 31;
} // endswitch BufType
n = strlen(Buf);
} else // Standard CONNECT format
n = Value->ShowValue(Buf, field);
if (trace(1))
htrc("new length(%p)=%d\n", Buf, n);
if ((len = n) > field) {
char *p = Value->GetCharString(Buf);
snprintf(g->Message, sizeof(g->Message), MSG(VALUE_TOO_LONG), p, Name, field);
throw 31;
} else if (Dsp)
for (i = 0; i < len; i++)
if (Buf[i] == '.')
Buf[i] = Dsp;
if (trace(2))
htrc("buffer=%s\n", Buf);
/*******************************************************************/
/* Updating must be done only when not in checking pass. */
/*******************************************************************/
if (Status) {
memset(p, ' ', field);
memcpy(p, Buf, len);
if (trace(2))
htrc(" col write: '%.*s'\n", len, p);
} // endif Status
} else // BIN compressed table
/*******************************************************************/
/* Check if updating is Ok, meaning col value is not too long. */
/* Updating to be done only during the second pass (Status=true) */
/*******************************************************************/
if (Value->GetBinValue(p, Long, Status)) {
snprintf(g->Message, sizeof(g->Message), MSG(BIN_F_TOO_LONG),
Name, Value->GetSize(), Long);
throw 31;
} // endif
} // end of WriteColumn
/***********************************************************************/
/* SetMinMax: Calculate minimum and maximum values for one block. */
/* Note: TYPE_STRING is stored and processed with zero ended strings */
/* to be matching the way the FILTER Eval function processes them. */
/***********************************************************************/
bool DOSCOL::SetMinMax(PGLOBAL g)
{
PTDBDOS tp = (PTDBDOS)To_Tdb;
ReadColumn(g); // Extract column value from current line
if (CheckSorted(g))
return true;
if (!tp->Txfp->CurNum) {
Min->SetValue(Value, tp->Txfp->CurBlk);
Max->SetValue(Value, tp->Txfp->CurBlk);
} else {
Min->SetMin(Value, tp->Txfp->CurBlk);
Max->SetMax(Value, tp->Txfp->CurBlk);
} // endif CurNum
return false;
} // end of SetMinMax
/***********************************************************************/
/* SetBitMap: Calculate the bit map of existing values in one block. */
/* Note: TYPE_STRING is processed with zero ended strings */
/* to be matching the way the FILTER Eval function processes them. */
/***********************************************************************/
bool DOSCOL::SetBitMap(PGLOBAL g)
{
int i, m, n;
uint *bmp;
PTDBDOS tp = (PTDBDOS)To_Tdb;
PDBUSER dup = PlgGetUser(g);
n = tp->Txfp->CurNum;
bmp = (uint*)Bmap->GetValPtr(Nbm * tp->Txfp->CurBlk);
// Extract column value from current line
ReadColumn(g);
if (CheckSorted(g))
return true;
if (!n) // New block
for (m = 0; m < Nbm; m++)
bmp[m] = 0; // Reset the new bit map
if ((i = Dval->Find(Value)) < 0) {
char buf[32];
snprintf(g->Message, sizeof(g->Message), MSG(DVAL_NOTIN_LIST),
Value->GetCharString(buf), Name);
return true;
} else if (i >= dup->Maxbmp) {
snprintf(g->Message, sizeof(g->Message), MSG(OPT_LOGIC_ERR), i);
return true;
} else {
m = i / MAXBMP;
#if defined(_DEBUG)
assert (m < Nbm);
#endif // _DEBUG
bmp[m] |= (1 << (i % MAXBMP));
} // endif's i
return false;
} // end of SetBitMap
/***********************************************************************/
/* Checks whether a column declared as sorted is sorted indeed. */
/***********************************************************************/
bool DOSCOL::CheckSorted(PGLOBAL g)
{
if (Sorted)
{
if (OldVal) {
// Verify whether this column is sorted all right
if (OldVal->CompareValue(Value) > 0) {
// Column is no more in ascending order
snprintf(g->Message, sizeof(g->Message), MSG(COL_NOT_SORTED), Name, To_Tdb->GetName());
Sorted = false;
return true;
} else
OldVal->SetValue_pval(Value);
} else
OldVal = AllocateValue(g, Value);
}
return false;
} // end of CheckSorted
/***********************************************************************/
/* AddDistinctValue: Check whether this value already exist in the */
/* list and if not add it to the distinct values list. */
/***********************************************************************/
bool DOSCOL::AddDistinctValue(PGLOBAL g)
{
bool found = false;
int i, m, n;
ReadColumn(g); // Extract column value from current line
// Perhaps a better algorithm can be used when Ndv gets bigger
// Here we cannot use Find because we must get the index of where
// to insert a new value if it is not found in the array.
for (n = 0; n < Ndv; n++) {
m = Dval->CompVal(Value, n);
if (m > 0)
continue;
else if (!m)
found = true; // Already there
break;
} // endfor n
if (!found) {
// Check whether we have room for an additional value
if (Ndv == Freq) {
// Too many values because of wrong Freq setting
snprintf(g->Message, sizeof(g->Message), MSG(BAD_FREQ_SET), Name);
return true;
} // endif Ndv
// New value, add it to the list before the nth value
Dval->SetNval(Ndv + 1);
for (i = Ndv; i > n; i--)
Dval->Move(i - 1, i);
Dval->SetValue(Value, n);
Ndv++;
} // endif found
return false;
} // end of AddDistinctValue
/* ------------------------------------------------------------------- */
|