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
|
/*
Copyright (c) 2005 Health Market Science, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
You can contact Health Market Science at info@healthmarketscience.com
or at the following address:
Health Market Science
2700 Horizon Drive
Suite 200
King of Prussia, PA 19406
*/
package com.healthmarketscience.jackcess.impl;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.CursorBuilder;
import com.healthmarketscience.jackcess.DataType;
import com.healthmarketscience.jackcess.IndexBuilder;
import com.healthmarketscience.jackcess.PropertyMap;
import com.healthmarketscience.jackcess.Row;
import com.healthmarketscience.jackcess.RowId;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.util.ErrorHandler;
import com.healthmarketscience.jackcess.util.ExportUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* A single database table
* <p>
* Is not thread-safe.
*
* @author Tim McCune
* @usage _general_class_
*/
public class TableImpl implements Table
{
private static final Log LOG = LogFactory.getLog(TableImpl.class);
private static final short OFFSET_MASK = (short)0x1FFF;
private static final short DELETED_ROW_MASK = (short)0x8000;
private static final short OVERFLOW_ROW_MASK = (short)0x4000;
static final int MAGIC_TABLE_NUMBER = 1625;
private static final int MAX_BYTE = 256;
/**
* Table type code for system tables
* @usage _intermediate_class_
*/
public static final byte TYPE_SYSTEM = 0x53;
/**
* Table type code for user tables
* @usage _intermediate_class_
*/
public static final byte TYPE_USER = 0x4e;
/** comparator which sorts variable length columns based on their index into
the variable length offset table */
private static final Comparator<ColumnImpl> VAR_LEN_COLUMN_COMPARATOR =
new Comparator<ColumnImpl>() {
public int compare(ColumnImpl c1, ColumnImpl c2) {
return ((c1.getVarLenTableIndex() < c2.getVarLenTableIndex()) ? -1 :
((c1.getVarLenTableIndex() > c2.getVarLenTableIndex()) ? 1 :
0));
}
};
/** comparator which sorts columns based on their display index */
private static final Comparator<ColumnImpl> DISPLAY_ORDER_COMPARATOR =
new Comparator<ColumnImpl>() {
public int compare(ColumnImpl c1, ColumnImpl c2) {
return ((c1.getDisplayIndex() < c2.getDisplayIndex()) ? -1 :
((c1.getDisplayIndex() > c2.getDisplayIndex()) ? 1 :
0));
}
};
/** owning database */
private final DatabaseImpl _database;
/** additional table flags from the catalog entry */
private final int _flags;
/** Type of the table (either TYPE_SYSTEM or TYPE_USER) */
private final byte _tableType;
/** Number of actual indexes on the table */
private final int _indexCount;
/** Number of logical indexes for the table */
private final int _logicalIndexCount;
/** page number of the definition of this table */
private final int _tableDefPageNumber;
/** max Number of columns in the table (includes previous deletions) */
private final short _maxColumnCount;
/** max Number of variable columns in the table */
private final short _maxVarColumnCount;
/** List of columns in this table, ordered by column number */
private final List<ColumnImpl> _columns = new ArrayList<ColumnImpl>();
/** List of variable length columns in this table, ordered by offset */
private final List<ColumnImpl> _varColumns = new ArrayList<ColumnImpl>();
/** List of autonumber columns in this table, ordered by column number */
private final List<ColumnImpl> _autoNumColumns = new ArrayList<ColumnImpl>(1);
/** List of indexes on this table (multiple logical indexes may be backed by
the same index data) */
private final List<IndexImpl> _indexes = new ArrayList<IndexImpl>();
/** List of index datas on this table (the actual backing data for an
index) */
private final List<IndexData> _indexDatas = new ArrayList<IndexData>();
/** List of columns in this table which are in one or more indexes */
private final Set<ColumnImpl> _indexColumns = new LinkedHashSet<ColumnImpl>();
/** Table name as stored in Database */
private final String _name;
/** Usage map of pages that this table owns */
private final UsageMap _ownedPages;
/** Usage map of pages that this table owns with free space on them */
private final UsageMap _freeSpacePages;
/** Number of rows in the table */
private int _rowCount;
/** last long auto number for the table */
private int _lastLongAutoNumber;
/** last complex type auto number for the table */
private int _lastComplexTypeAutoNumber;
/** modification count for the table, keeps row-states up-to-date */
private int _modCount;
/** page buffer used to update data pages when adding rows */
private final TempPageHolder _addRowBufferH =
TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
/** page buffer used to update the table def page */
private final TempPageHolder _tableDefBufferH =
TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
/** buffer used to writing single rows of data */
private final TempBufferHolder _singleRowBufferH =
TempBufferHolder.newHolder(TempBufferHolder.Type.SOFT, true);
/** "buffer" used to writing multi rows of data (will create new buffer on
every call) */
private final TempBufferHolder _multiRowBufferH =
TempBufferHolder.newHolder(TempBufferHolder.Type.NONE, true);
/** page buffer used to write out-of-row "long value" data */
private final TempPageHolder _longValueBufferH =
TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
/** optional error handler to use when row errors are encountered */
private ErrorHandler _tableErrorHandler;
/** properties for this table */
private PropertyMap _props;
/** properties group for this table (and columns) */
private PropertyMaps _propertyMaps;
/** foreign-key enforcer for this table */
private final FKEnforcer _fkEnforcer;
/** default cursor for iterating through the table, kept here for basic
table traversal */
private CursorImpl _defaultCursor;
/**
* Only used by unit tests
* @usage _advanced_method_
*/
protected TableImpl(boolean testing, List<ColumnImpl> columns)
throws IOException
{
if(!testing) {
throw new IllegalArgumentException();
}
_database = null;
_tableDefPageNumber = PageChannel.INVALID_PAGE_NUMBER;
_name = null;
_columns.addAll(columns);
for(ColumnImpl col : _columns) {
if(col.getType().isVariableLength()) {
_varColumns.add(col);
}
}
_maxColumnCount = (short)_columns.size();
_maxVarColumnCount = (short)_varColumns.size();
getAutoNumberColumns();
_fkEnforcer = null;
_flags = 0;
_tableType = TYPE_USER;
_indexCount = 0;
_logicalIndexCount = 0;
_ownedPages = null;
_freeSpacePages = null;
}
/**
* @param database database which owns this table
* @param tableBuffer Buffer to read the table with
* @param pageNumber Page number of the table definition
* @param name Table name
*/
protected TableImpl(DatabaseImpl database, ByteBuffer tableBuffer,
int pageNumber, String name, int flags)
throws IOException
{
_database = database;
_tableDefPageNumber = pageNumber;
_name = name;
_flags = flags;
// read table definition
tableBuffer = loadCompleteTableDefinitionBuffer(tableBuffer);
_rowCount = tableBuffer.getInt(getFormat().OFFSET_NUM_ROWS);
_lastLongAutoNumber = tableBuffer.getInt(getFormat().OFFSET_NEXT_AUTO_NUMBER);
if(getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER >= 0) {
_lastComplexTypeAutoNumber = tableBuffer.getInt(
getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER);
}
_tableType = tableBuffer.get(getFormat().OFFSET_TABLE_TYPE);
_maxColumnCount = tableBuffer.getShort(getFormat().OFFSET_MAX_COLS);
_maxVarColumnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_VAR_COLS);
short columnCount = tableBuffer.getShort(getFormat().OFFSET_NUM_COLS);
_logicalIndexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEX_SLOTS);
_indexCount = tableBuffer.getInt(getFormat().OFFSET_NUM_INDEXES);
tableBuffer.position(getFormat().OFFSET_OWNED_PAGES);
_ownedPages = UsageMap.read(getDatabase(), tableBuffer, false);
tableBuffer.position(getFormat().OFFSET_FREE_SPACE_PAGES);
_freeSpacePages = UsageMap.read(getDatabase(), tableBuffer, false);
for (int i = 0; i < _indexCount; i++) {
_indexDatas.add(IndexData.create(this, tableBuffer, i, getFormat()));
}
readColumnDefinitions(tableBuffer, columnCount);
readIndexDefinitions(tableBuffer);
// read column usage map info
while(tableBuffer.remaining() >= 2) {
short umapColNum = tableBuffer.getShort();
if(umapColNum == IndexData.COLUMN_UNUSED) {
break;
}
int pos = tableBuffer.position();
UsageMap colOwnedPages = null;
UsageMap colFreeSpacePages = null;
try {
colOwnedPages = UsageMap.read(getDatabase(), tableBuffer, false);
colFreeSpacePages = UsageMap.read(getDatabase(), tableBuffer, false);
} catch(IllegalStateException e) {
// ignore invalid usage map info
colOwnedPages = null;
colFreeSpacePages = null;
tableBuffer.position(pos + 8);
LOG.warn("Table " + _name + " invalid column " + umapColNum +
" usage map definition: " + e);
}
for(ColumnImpl col : _columns) {
if(col.getColumnNumber() == umapColNum) {
col.setUsageMaps(colOwnedPages, colFreeSpacePages);
break;
}
}
}
// re-sort columns if necessary
if(getDatabase().getColumnOrder() != ColumnOrder.DATA) {
Collections.sort(_columns, DISPLAY_ORDER_COMPARATOR);
}
for(ColumnImpl col : _columns) {
// some columns need to do extra work after the table is completely
// loaded
col.postTableLoadInit();
}
_fkEnforcer = new FKEnforcer(this);
}
public String getName() {
return _name;
}
public boolean isHidden() {
return((_flags & DatabaseImpl.HIDDEN_OBJECT_FLAG) != 0);
}
/**
* @usage _advanced_method_
*/
public int getMaxColumnCount() {
return _maxColumnCount;
}
public int getColumnCount() {
return _columns.size();
}
public DatabaseImpl getDatabase() {
return _database;
}
/**
* @usage _advanced_method_
*/
public JetFormat getFormat() {
return getDatabase().getFormat();
}
/**
* @usage _advanced_method_
*/
public PageChannel getPageChannel() {
return getDatabase().getPageChannel();
}
public ErrorHandler getErrorHandler() {
return((_tableErrorHandler != null) ? _tableErrorHandler :
getDatabase().getErrorHandler());
}
public void setErrorHandler(ErrorHandler newErrorHandler) {
_tableErrorHandler = newErrorHandler;
}
public int getTableDefPageNumber() {
return _tableDefPageNumber;
}
/**
* @usage _advanced_method_
*/
public RowState createRowState() {
return new RowState(TempBufferHolder.Type.HARD);
}
/**
* @usage _advanced_method_
*/
public UsageMap.PageCursor getOwnedPagesCursor() {
return _ownedPages.cursor();
}
/**
* Returns the <i>approximate</i> number of database pages owned by this
* table and all related indexes (this number does <i>not</i> take into
* account pages used for large OLE/MEMO fields).
* <p>
* To calculate the approximate number of bytes owned by a table:
* <code>
* int approxTableBytes = (table.getApproximateOwnedPageCount() *
* table.getFormat().PAGE_SIZE);
* </code>
* @usage _intermediate_method_
*/
public int getApproximateOwnedPageCount() {
// add a page for the table def (although that might actually be more than
// one page)
int count = _ownedPages.getPageCount() + 1;
for(ColumnImpl col : _columns) {
count += col.getOwnedPageCount();
}
// note, we count owned pages from _physical_ indexes, not logical indexes
// (otherwise we could double count pages)
for(IndexData indexData : _indexDatas) {
count += indexData.getOwnedPageCount();
}
return count;
}
protected TempPageHolder getLongValueBuffer() {
return _longValueBufferH;
}
public List<ColumnImpl> getColumns() {
return Collections.unmodifiableList(_columns);
}
public ColumnImpl getColumn(String name) {
for(ColumnImpl column : _columns) {
if(column.getName().equalsIgnoreCase(name)) {
return column;
}
}
throw new IllegalArgumentException("Column with name " + name +
" does not exist in this table");
}
public boolean hasColumn(String name) {
for(ColumnImpl column : _columns) {
if(column.getName().equalsIgnoreCase(name)) {
return true;
}
}
return false;
}
public PropertyMap getProperties() throws IOException {
if(_props == null) {
_props = getPropertyMaps().getDefault();
}
return _props;
}
/**
* @return all PropertyMaps for this table (and columns)
* @usage _advanced_method_
*/
public PropertyMaps getPropertyMaps() throws IOException {
if(_propertyMaps == null) {
_propertyMaps = getDatabase().getPropertiesForObject(
_tableDefPageNumber);
}
return _propertyMaps;
}
public List<IndexImpl> getIndexes() {
return Collections.unmodifiableList(_indexes);
}
public IndexImpl getIndex(String name) {
for(IndexImpl index : _indexes) {
if(index.getName().equalsIgnoreCase(name)) {
return index;
}
}
throw new IllegalArgumentException("Index with name " + name +
" does not exist on this table");
}
public IndexImpl getPrimaryKeyIndex() {
for(IndexImpl index : _indexes) {
if(index.isPrimaryKey()) {
return index;
}
}
throw new IllegalArgumentException("Table " + getName() +
" does not have a primary key index");
}
public IndexImpl getForeignKeyIndex(Table otherTable) {
for(IndexImpl index : _indexes) {
if(index.isForeignKey() && (index.getReference() != null) &&
(index.getReference().getOtherTablePageNumber() ==
((TableImpl)otherTable).getTableDefPageNumber())) {
return index;
}
}
throw new IllegalArgumentException(
"Table " + getName() + " does not have a foreign key reference to " +
otherTable.getName());
}
/**
* @return All of the IndexData on this table (unmodifiable List)
* @usage _advanced_method_
*/
public List<IndexData> getIndexDatas() {
return Collections.unmodifiableList(_indexDatas);
}
/**
* Only called by unit tests
* @usage _advanced_method_
*/
public int getLogicalIndexCount() {
return _logicalIndexCount;
}
public CursorImpl getDefaultCursor() {
if(_defaultCursor == null) {
_defaultCursor = CursorImpl.createCursor(this);
}
return _defaultCursor;
}
public CursorBuilder newCursor() {
return new CursorBuilder(this);
}
public void reset() {
getDefaultCursor().reset();
}
public Row deleteRow(Row row) throws IOException {
deleteRow(row.getId());
return row;
}
/**
* Delete the row with the given id. Provided RowId must have previously
* been returned from this Table.
* @return the given rowId
* @throws IllegalStateException if the given row is not valid
* @usage _intermediate_method_
*/
public RowId deleteRow(RowId rowId) throws IOException {
deleteRow(getDefaultCursor().getRowState(), (RowIdImpl)rowId);
return rowId;
}
/**
* Delete the row for the given rowId.
* @usage _advanced_method_
*/
public void deleteRow(RowState rowState, RowIdImpl rowId)
throws IOException
{
requireValidRowId(rowId);
getPageChannel().startWrite();
try {
// ensure that the relevant row state is up-to-date
ByteBuffer rowBuffer = positionAtRowHeader(rowState, rowId);
if(rowState.isDeleted()) {
// don't care about duplicate deletion
return;
}
requireNonDeletedRow(rowState, rowId);
// delete flag always gets set in the "header" row (even if data is on
// overflow row)
int pageNumber = rowState.getHeaderRowId().getPageNumber();
int rowNumber = rowState.getHeaderRowId().getRowNumber();
// attempt to fill in index column values
Object[] rowValues = null;
if(!_indexDatas.isEmpty()) {
// move to row data to get index values
rowBuffer = positionAtRowData(rowState, rowId);
for(ColumnImpl idxCol : _indexColumns) {
getRowColumn(getFormat(), rowBuffer, idxCol, rowState, null);
}
// use any read rowValues to help update the indexes
rowValues = rowState.getRowValues();
// check foreign keys before proceeding w/ deletion
_fkEnforcer.deleteRow(rowValues);
// move back to the header
rowBuffer = positionAtRowHeader(rowState, rowId);
}
// finally, pull the trigger
int rowIndex = getRowStartOffset(rowNumber, getFormat());
rowBuffer.putShort(rowIndex, (short)(rowBuffer.getShort(rowIndex)
| DELETED_ROW_MASK | OVERFLOW_ROW_MASK));
writeDataPage(rowBuffer, pageNumber);
// update the indexes
for(IndexData indexData : _indexDatas) {
indexData.deleteRow(rowValues, rowId);
}
// make sure table def gets updated
updateTableDefinition(-1);
} finally {
getPageChannel().finishWrite();
}
}
public Row getNextRow() throws IOException {
return getDefaultCursor().getNextRow();
}
/**
* Reads a single column from the given row.
* @usage _advanced_method_
*/
public Object getRowValue(RowState rowState, RowIdImpl rowId,
ColumnImpl column)
throws IOException
{
if(this != column.getTable()) {
throw new IllegalArgumentException(
"Given column " + column + " is not from this table");
}
requireValidRowId(rowId);
// position at correct row
ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
requireNonDeletedRow(rowState, rowId);
return getRowColumn(getFormat(), rowBuffer, column, rowState, null);
}
/**
* Reads some columns from the given row.
* @param columnNames Only column names in this collection will be returned
* @usage _advanced_method_
*/
public RowImpl getRow(
RowState rowState, RowIdImpl rowId, Collection<String> columnNames)
throws IOException
{
requireValidRowId(rowId);
// position at correct row
ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
requireNonDeletedRow(rowState, rowId);
return getRow(getFormat(), rowState, rowBuffer, _columns, columnNames);
}
/**
* Reads the row data from the given row buffer. Leaves limit unchanged.
* Saves parsed row values to the given rowState.
*/
private static RowImpl getRow(
JetFormat format,
RowState rowState,
ByteBuffer rowBuffer,
Collection<ColumnImpl> columns,
Collection<String> columnNames)
throws IOException
{
RowImpl rtn = new RowImpl(rowState.getHeaderRowId(), columns.size());
for(ColumnImpl column : columns) {
if((columnNames == null) || (columnNames.contains(column.getName()))) {
// Add the value to the row data
column.setRowValue(
rtn, getRowColumn(format, rowBuffer, column, rowState, null));
}
}
return rtn;
}
/**
* Reads the column data from the given row buffer. Leaves limit unchanged.
* Caches the returned value in the rowState.
*/
private static Object getRowColumn(JetFormat format,
ByteBuffer rowBuffer,
ColumnImpl column,
RowState rowState,
Map<ColumnImpl,byte[]> rawVarValues)
throws IOException
{
byte[] columnData = null;
try {
NullMask nullMask = rowState.getNullMask(rowBuffer);
boolean isNull = nullMask.isNull(column);
if(column.getType() == DataType.BOOLEAN) {
// Boolean values are stored in the null mask. see note about
// caching below
return rowState.setRowValue(column.getColumnIndex(),
Boolean.valueOf(!isNull));
} else if(isNull) {
// well, that's easy! (no need to update cache w/ null)
return null;
}
// reset position to row start
rowBuffer.reset();
// locate the column data bytes
int rowStart = rowBuffer.position();
int colDataPos = 0;
int colDataLen = 0;
if(!column.isVariableLength()) {
// read fixed length value (non-boolean at this point)
int dataStart = rowStart + format.OFFSET_COLUMN_FIXED_DATA_ROW_OFFSET;
colDataPos = dataStart + column.getFixedDataOffset();
colDataLen = column.getType().getFixedSize(column.getLength());
} else {
int varDataStart;
int varDataEnd;
if(format.SIZE_ROW_VAR_COL_OFFSET == 2) {
// read simple var length value
int varColumnOffsetPos =
(rowBuffer.limit() - nullMask.byteSize() - 4) -
(column.getVarLenTableIndex() * 2);
varDataStart = rowBuffer.getShort(varColumnOffsetPos);
varDataEnd = rowBuffer.getShort(varColumnOffsetPos - 2);
} else {
// read jump-table based var length values
short[] varColumnOffsets = readJumpTableVarColOffsets(
rowState, rowBuffer, rowStart, nullMask);
varDataStart = varColumnOffsets[column.getVarLenTableIndex()];
varDataEnd = varColumnOffsets[column.getVarLenTableIndex() + 1];
}
colDataPos = rowStart + varDataStart;
colDataLen = varDataEnd - varDataStart;
}
// grab the column data
rowBuffer.position(colDataPos);
columnData = ByteUtil.getBytes(rowBuffer, colDataLen);
if((rawVarValues != null) && column.isVariableLength()) {
// caller wants raw value as well
rawVarValues.put(column, columnData);
}
// parse the column data. we cache the row values in order to be able
// to update the index on row deletion. note, most of the returned
// values are immutable, except for binary data (returned as byte[]),
// but binary data shouldn't be indexed anyway.
return rowState.setRowValue(column.getColumnIndex(),
column.read(columnData));
} catch(Exception e) {
// cache "raw" row value. see note about caching above
rowState.setRowValue(column.getColumnIndex(),
ColumnImpl.rawDataWrapper(columnData));
return rowState.handleRowError(column, columnData, e);
}
}
private static short[] readJumpTableVarColOffsets(
RowState rowState, ByteBuffer rowBuffer, int rowStart,
NullMask nullMask)
{
short[] varColOffsets = rowState.getVarColOffsets();
if(varColOffsets != null) {
return varColOffsets;
}
// calculate offsets using jump-table info
int nullMaskSize = nullMask.byteSize();
int rowEnd = rowStart + rowBuffer.remaining() - 1;
int numVarCols = ByteUtil.getUnsignedByte(rowBuffer,
rowEnd - nullMaskSize);
varColOffsets = new short[numVarCols + 1];
int rowLen = rowEnd - rowStart + 1;
int numJumps = (rowLen - 1) / MAX_BYTE;
int colOffset = rowEnd - nullMaskSize - numJumps - 1;
// If last jump is a dummy value, ignore it
if(((colOffset - rowStart - numVarCols) / MAX_BYTE) < numJumps) {
numJumps--;
}
int jumpsUsed = 0;
for(int i = 0; i < numVarCols + 1; i++) {
while((jumpsUsed < numJumps) &&
(i == ByteUtil.getUnsignedByte(
rowBuffer, rowEnd - nullMaskSize-jumpsUsed - 1))) {
jumpsUsed++;
}
varColOffsets[i] = (short)
(ByteUtil.getUnsignedByte(rowBuffer, colOffset - i)
+ (jumpsUsed * MAX_BYTE));
}
rowState.setVarColOffsets(varColOffsets);
return varColOffsets;
}
/**
* Reads the null mask from the given row buffer. Leaves limit unchanged.
*/
private NullMask getRowNullMask(ByteBuffer rowBuffer)
throws IOException
{
// reset position to row start
rowBuffer.reset();
// Number of columns in this row
int columnCount = ByteUtil.getUnsignedVarInt(
rowBuffer, getFormat().SIZE_ROW_COLUMN_COUNT);
// read null mask
NullMask nullMask = new NullMask(columnCount);
rowBuffer.position(rowBuffer.limit() - nullMask.byteSize()); //Null mask at end
nullMask.read(rowBuffer);
return nullMask;
}
/**
* Sets a new buffer to the correct row header page using the given rowState
* according to the given rowId. Deleted state is
* determined, but overflow row pointers are not followed.
*
* @return a ByteBuffer of the relevant page, or null if row was invalid
* @usage _advanced_method_
*/
public static ByteBuffer positionAtRowHeader(RowState rowState,
RowIdImpl rowId)
throws IOException
{
ByteBuffer rowBuffer = rowState.setHeaderRow(rowId);
if(rowState.isAtHeaderRow()) {
// this task has already been accomplished
return rowBuffer;
}
if(!rowState.isValid()) {
// this was an invalid page/row
rowState.setStatus(RowStateStatus.AT_HEADER);
return null;
}
// note, we don't use findRowStart here cause we need the unmasked value
short rowStart = rowBuffer.getShort(
getRowStartOffset(rowId.getRowNumber(),
rowState.getTable().getFormat()));
// check the deleted, overflow flags for the row (the "real" flags are
// always set on the header row)
RowStatus rowStatus = RowStatus.NORMAL;
if(isDeletedRow(rowStart)) {
rowStatus = RowStatus.DELETED;
} else if(isOverflowRow(rowStart)) {
rowStatus = RowStatus.OVERFLOW;
}
rowState.setRowStatus(rowStatus);
rowState.setStatus(RowStateStatus.AT_HEADER);
return rowBuffer;
}
/**
* Sets the position and limit in a new buffer using the given rowState
* according to the given row number and row end, following overflow row
* pointers as necessary.
*
* @return a ByteBuffer narrowed to the actual row data, or null if row was
* invalid or deleted
* @usage _advanced_method_
*/
public static ByteBuffer positionAtRowData(RowState rowState,
RowIdImpl rowId)
throws IOException
{
positionAtRowHeader(rowState, rowId);
if(!rowState.isValid() || rowState.isDeleted()) {
// row is invalid or deleted
rowState.setStatus(RowStateStatus.AT_FINAL);
return null;
}
ByteBuffer rowBuffer = rowState.getFinalPage();
int rowNum = rowState.getFinalRowId().getRowNumber();
JetFormat format = rowState.getTable().getFormat();
if(rowState.isAtFinalRow()) {
// we've already found the final row data
return PageChannel.narrowBuffer(
rowBuffer,
findRowStart(rowBuffer, rowNum, format),
findRowEnd(rowBuffer, rowNum, format));
}
while(true) {
// note, we don't use findRowStart here cause we need the unmasked value
short rowStart = rowBuffer.getShort(getRowStartOffset(rowNum, format));
short rowEnd = findRowEnd(rowBuffer, rowNum, format);
// note, at this point we know the row is not deleted, so ignore any
// subsequent deleted flags (as overflow rows are always marked deleted
// anyway)
boolean overflowRow = isOverflowRow(rowStart);
// now, strip flags from rowStart offset
rowStart = (short)(rowStart & OFFSET_MASK);
if (overflowRow) {
if((rowEnd - rowStart) < 4) {
throw new IOException("invalid overflow row info");
}
// Overflow page. the "row" data in the current page points to
// another page/row
int overflowRowNum = ByteUtil.getUnsignedByte(rowBuffer, rowStart);
int overflowPageNum = ByteUtil.get3ByteInt(rowBuffer, rowStart + 1);
rowBuffer = rowState.setOverflowRow(
new RowIdImpl(overflowPageNum, overflowRowNum));
rowNum = overflowRowNum;
} else {
rowState.setStatus(RowStateStatus.AT_FINAL);
return PageChannel.narrowBuffer(rowBuffer, rowStart, rowEnd);
}
}
}
public Iterator<Row> iterator() {
return getDefaultCursor().iterator();
}
/**
* Writes a new table defined by the given TableCreator to the database.
* @usage _advanced_method_
*/
protected static void writeTableDefinition(TableCreator creator)
throws IOException
{
// first, create the usage map page
createUsageMapDefinitionBuffer(creator);
// next, determine how big the table def will be (in case it will be more
// than one page)
JetFormat format = creator.getFormat();
int idxDataLen = (creator.getIndexCount() *
(format.SIZE_INDEX_DEFINITION +
format.SIZE_INDEX_COLUMN_BLOCK)) +
(creator.getLogicalIndexCount() * format.SIZE_INDEX_INFO_BLOCK);
int colUmapLen = creator.getLongValueColumns().size() * 10;
int totalTableDefSize = format.SIZE_TDEF_HEADER +
(format.SIZE_COLUMN_DEF_BLOCK * creator.getColumns().size()) +
idxDataLen + colUmapLen + format.SIZE_TDEF_TRAILER;
// total up the amount of space used by the column and index names (2
// bytes per char + 2 bytes for the length)
for(ColumnBuilder col : creator.getColumns()) {
int nameByteLen = (col.getName().length() *
JetFormat.TEXT_FIELD_UNIT_SIZE);
totalTableDefSize += nameByteLen + 2;
}
for(IndexBuilder idx : creator.getIndexes()) {
int nameByteLen = (idx.getName().length() *
JetFormat.TEXT_FIELD_UNIT_SIZE);
totalTableDefSize += nameByteLen + 2;
}
// now, create the table definition
PageChannel pageChannel = creator.getPageChannel();
ByteBuffer buffer = pageChannel .createBuffer(Math.max(totalTableDefSize,
format.PAGE_SIZE));
writeTableDefinitionHeader(creator, buffer, totalTableDefSize);
if(creator.hasIndexes()) {
// index row counts
IndexData.writeRowCountDefinitions(creator, buffer);
}
// column definitions
ColumnImpl.writeDefinitions(creator, buffer);
if(creator.hasIndexes()) {
// index and index data definitions
IndexData.writeDefinitions(creator, buffer);
IndexImpl.writeDefinitions(creator, buffer);
}
// write long value column usage map references
for(ColumnBuilder lvalCol : creator.getLongValueColumns()) {
buffer.putShort(lvalCol.getColumnNumber());
TableCreator.ColumnState colState =
creator.getColumnState(lvalCol);
// owned pages umap (both are on same page)
buffer.put(colState.getUmapOwnedRowNumber());
ByteUtil.put3ByteInt(buffer, colState.getUmapPageNumber());
// free space pages umap
buffer.put(colState.getUmapFreeRowNumber());
ByteUtil.put3ByteInt(buffer, colState.getUmapPageNumber());
}
//End of tabledef
buffer.put((byte) 0xff);
buffer.put((byte) 0xff);
// write table buffer to database
if(totalTableDefSize <= format.PAGE_SIZE) {
// easy case, fits on one page
buffer.putShort(format.OFFSET_FREE_SPACE,
(short)(buffer.remaining() - 8)); // overwrite page free space
// Write the tdef page to disk.
pageChannel.writePage(buffer, creator.getTdefPageNumber());
} else {
// need to split across multiple pages
ByteBuffer partialTdef = pageChannel.createPageBuffer();
buffer.rewind();
int nextTdefPageNumber = PageChannel.INVALID_PAGE_NUMBER;
while(buffer.hasRemaining()) {
// reset for next write
partialTdef.clear();
if(nextTdefPageNumber == PageChannel.INVALID_PAGE_NUMBER) {
// this is the first page. note, the first page already has the
// page header, so no need to write it here
nextTdefPageNumber = creator.getTdefPageNumber();
} else {
// write page header
writeTablePageHeader(partialTdef);
}
// copy the next page of tdef bytes
int curTdefPageNumber = nextTdefPageNumber;
int writeLen = Math.min(partialTdef.remaining(), buffer.remaining());
partialTdef.put(buffer.array(), buffer.position(), writeLen);
ByteUtil.forward(buffer, writeLen);
if(buffer.hasRemaining()) {
// need a next page
nextTdefPageNumber = pageChannel.allocateNewPage();
partialTdef.putInt(format.OFFSET_NEXT_TABLE_DEF_PAGE,
nextTdefPageNumber);
}
// update page free space
partialTdef.putShort(format.OFFSET_FREE_SPACE,
(short)(partialTdef.remaining() - 8)); // overwrite page free space
// write partial page to disk
pageChannel.writePage(partialTdef, curTdefPageNumber);
}
}
}
/**
* @param buffer Buffer to write to
* @param columns List of Columns in the table
*/
private static void writeTableDefinitionHeader(
TableCreator creator, ByteBuffer buffer, int totalTableDefSize)
throws IOException
{
List<ColumnBuilder> columns = creator.getColumns();
//Start writing the tdef
writeTablePageHeader(buffer);
buffer.putInt(totalTableDefSize); //Length of table def
buffer.putInt(MAGIC_TABLE_NUMBER); // seemingly constant magic value
buffer.putInt(0); //Number of rows
buffer.putInt(0); //Last Autonumber
buffer.put((byte) 1); // this makes autonumbering work in access
for (int i = 0; i < 15; i++) { //Unknown
buffer.put((byte) 0);
}
buffer.put(TYPE_USER); //Table type
buffer.putShort((short) columns.size()); //Max columns a row will have
buffer.putShort(ColumnImpl.countVariableLength(columns)); //Number of variable columns in table
buffer.putShort((short) columns.size()); //Number of columns in table
buffer.putInt(creator.getLogicalIndexCount()); //Number of logical indexes in table
buffer.putInt(creator.getIndexCount()); //Number of indexes in table
buffer.put((byte) 0); //Usage map row number
ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Usage map page number
buffer.put((byte) 1); //Free map row number
ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Free map page number
}
/**
* Writes the page header for a table definition page
* @param buffer Buffer to write to
*/
private static void writeTablePageHeader(ByteBuffer buffer)
{
buffer.put(PageTypes.TABLE_DEF); //Page type
buffer.put((byte) 0x01); //Unknown
buffer.put((byte) 0); //Unknown
buffer.put((byte) 0); //Unknown
buffer.putInt(0); //Next TDEF page pointer
}
/**
* Writes the given name into the given buffer in the format as expected by
* {@link #readName}.
*/
static void writeName(ByteBuffer buffer, String name, Charset charset)
{
ByteBuffer encName = ColumnImpl.encodeUncompressedText(name, charset);
buffer.putShort((short) encName.remaining());
buffer.put(encName);
}
/**
* Create the usage map definition page buffer. The "used pages" map is in
* row 0, the "pages with free space" map is in row 1. Index usage maps are
* in subsequent rows.
*/
private static void createUsageMapDefinitionBuffer(TableCreator creator)
throws IOException
{
List<ColumnBuilder> lvalCols = creator.getLongValueColumns();
// 2 table usage maps plus 1 for each index and 2 for each lval col
int indexUmapEnd = 2 + creator.getIndexCount();
int umapNum = indexUmapEnd + (lvalCols.size() * 2);
JetFormat format = creator.getFormat();
int umapRowLength = format.OFFSET_USAGE_MAP_START +
format.USAGE_MAP_TABLE_BYTE_LENGTH;
int umapSpaceUsage = getRowSpaceUsage(umapRowLength, format);
PageChannel pageChannel = creator.getPageChannel();
int umapPageNumber = PageChannel.INVALID_PAGE_NUMBER;
ByteBuffer umapBuf = null;
int freeSpace = 0;
int rowStart = 0;
int umapRowNum = 0;
for(int i = 0; i < umapNum; ++i) {
if(umapBuf == null) {
// need new page for usage maps
if(umapPageNumber == PageChannel.INVALID_PAGE_NUMBER) {
// first umap page has already been reserved
umapPageNumber = creator.getUmapPageNumber();
} else {
// need another umap page
umapPageNumber = creator.reservePageNumber();
}
freeSpace = format.DATA_PAGE_INITIAL_FREE_SPACE;
umapBuf = pageChannel.createPageBuffer();
umapBuf.put(PageTypes.DATA);
umapBuf.put((byte) 0x1); //Unknown
umapBuf.putShort((short)freeSpace); //Free space in page
umapBuf.putInt(0); //Table definition
umapBuf.putInt(0); //Unknown
umapBuf.putShort((short)0); //Number of records on this page
rowStart = findRowEnd(umapBuf, 0, format) - umapRowLength;
umapRowNum = 0;
}
umapBuf.putShort(getRowStartOffset(umapRowNum, format), (short)rowStart);
if(i == 0) {
// table "owned pages" map definition
umapBuf.put(rowStart, UsageMap.MAP_TYPE_REFERENCE);
} else if(i == 1) {
// table "free space pages" map definition
umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE);
} else if(i < indexUmapEnd) {
// index umap
int indexIdx = i - 2;
IndexBuilder idx = creator.getIndexes().get(indexIdx);
// allocate root page for the index
int rootPageNumber = pageChannel.allocateNewPage();
// stash info for later use
TableCreator.IndexState idxState = creator.getIndexState(idx);
idxState.setRootPageNumber(rootPageNumber);
idxState.setUmapRowNumber((byte)umapRowNum);
idxState.setUmapPageNumber(umapPageNumber);
// index map definition, including initial root page
umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE);
umapBuf.putInt(rowStart + 1, rootPageNumber);
umapBuf.put(rowStart + 5, (byte)1);
} else {
// long value column umaps
int lvalColIdx = i - indexUmapEnd;
int umapType = lvalColIdx % 2;
lvalColIdx /= 2;
ColumnBuilder lvalCol = lvalCols.get(lvalColIdx);
TableCreator.ColumnState colState =
creator.getColumnState(lvalCol);
umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE);
if((umapType == 1) &&
(umapPageNumber != colState.getUmapPageNumber())) {
// we want to force both usage maps for a column to be on the same
// data page, so just discard the previous one we wrote
--i;
umapType = 0;
}
if(umapType == 0) {
// lval column "owned pages" usage map
colState.setUmapOwnedRowNumber((byte)umapRowNum);
colState.setUmapPageNumber(umapPageNumber);
} else {
// lval column "free space pages" usage map (always on same page)
colState.setUmapFreeRowNumber((byte)umapRowNum);
}
}
rowStart -= umapRowLength;
freeSpace -= umapSpaceUsage;
++umapRowNum;
if((freeSpace <= umapSpaceUsage) || (i == (umapNum - 1))) {
// finish current page
umapBuf.putShort(format.OFFSET_FREE_SPACE, (short)freeSpace);
umapBuf.putShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE,
(short)umapRowNum);
pageChannel.writePage(umapBuf, umapPageNumber);
umapBuf = null;
}
}
}
/**
* Returns a single ByteBuffer which contains the entire table definition
* (which may span multiple database pages).
*/
private ByteBuffer loadCompleteTableDefinitionBuffer(ByteBuffer tableBuffer)
throws IOException
{
int nextPage = tableBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
ByteBuffer nextPageBuffer = null;
while (nextPage != 0) {
if (nextPageBuffer == null) {
nextPageBuffer = getPageChannel().createPageBuffer();
}
getPageChannel().readPage(nextPageBuffer, nextPage);
nextPage = nextPageBuffer.getInt(getFormat().OFFSET_NEXT_TABLE_DEF_PAGE);
ByteBuffer newBuffer = getPageChannel().createBuffer(
tableBuffer.capacity() + getFormat().PAGE_SIZE - 8);
newBuffer.put(tableBuffer);
newBuffer.put(nextPageBuffer.array(), 8, getFormat().PAGE_SIZE - 8);
tableBuffer = newBuffer;
tableBuffer.flip();
}
return tableBuffer;
}
private void readColumnDefinitions(ByteBuffer tableBuffer, short columnCount)
throws IOException
{
int colOffset = getFormat().OFFSET_INDEX_DEF_BLOCK +
_indexCount * getFormat().SIZE_INDEX_DEFINITION;
int dispIndex = 0;
for (int i = 0; i < columnCount; i++) {
ColumnImpl column = new ColumnImpl(this, tableBuffer,
colOffset + (i * getFormat().SIZE_COLUMN_HEADER), dispIndex++);
_columns.add(column);
if(column.isVariableLength()) {
// also shove it in the variable columns list, which is ordered
// differently from the _columns list
_varColumns.add(column);
}
}
tableBuffer.position(colOffset +
(columnCount * getFormat().SIZE_COLUMN_HEADER));
for (int i = 0; i < columnCount; i++) {
ColumnImpl column = _columns.get(i);
column.setName(readName(tableBuffer));
}
Collections.sort(_columns);
getAutoNumberColumns();
// setup the data index for the columns
int colIdx = 0;
for(ColumnImpl col : _columns) {
col.setColumnIndex(colIdx++);
}
// sort variable length columns based on their index into the variable
// length offset table, because we will write the columns in this order
Collections.sort(_varColumns, VAR_LEN_COLUMN_COMPARATOR);
}
private void readIndexDefinitions(ByteBuffer tableBuffer) throws IOException
{
// read index column information
for (int i = 0; i < _indexCount; i++) {
IndexData idxData = _indexDatas.get(i);
idxData.read(tableBuffer, _columns);
// keep track of all columns involved in indexes
for(IndexData.ColumnDescriptor iCol : idxData.getColumns()) {
_indexColumns.add(iCol.getColumn());
}
}
// read logical index info (may be more logical indexes than index datas)
for (int i = 0; i < _logicalIndexCount; i++) {
_indexes.add(new IndexImpl(tableBuffer, _indexDatas, getFormat()));
}
// read logical index names
for (int i = 0; i < _logicalIndexCount; i++) {
_indexes.get(i).setName(readName(tableBuffer));
}
Collections.sort(_indexes);
}
/**
* Writes the given page data to the given page number, clears any other
* relevant buffers.
*/
private void writeDataPage(ByteBuffer pageBuffer, int pageNumber)
throws IOException
{
// write the page data
getPageChannel().writePage(pageBuffer, pageNumber);
// possibly invalidate the add row buffer if a different data buffer is
// being written (e.g. this happens during deleteRow)
_addRowBufferH.possiblyInvalidate(pageNumber, pageBuffer);
// update modification count so any active RowStates can keep themselves
// up-to-date
++_modCount;
}
/**
* Returns a name read from the buffer at the current position. The
* expected name format is the name length followed by the name
* encoded using the {@link JetFormat#CHARSET}
*/
private String readName(ByteBuffer buffer) {
int nameLength = readNameLength(buffer);
byte[] nameBytes = ByteUtil.getBytes(buffer, nameLength);
return ColumnImpl.decodeUncompressedText(nameBytes,
getDatabase().getCharset());
}
/**
* Returns a name length read from the buffer at the current position.
*/
private int readNameLength(ByteBuffer buffer) {
return ByteUtil.getUnsignedVarInt(buffer, getFormat().SIZE_NAME_LENGTH);
}
public Object[] asRow(Map<String,?> rowMap) {
return asRow(rowMap, null, false);
}
/**
* Converts a map of columnName -> columnValue to an array of row values
* appropriate for a call to {@link #addRow(Object...)}, where the generated
* RowId will be an extra value at the end of the array.
* @see ColumnImpl#RETURN_ROW_ID
* @usage _intermediate_method_
*/
public Object[] asRowWithRowId(Map<String,?> rowMap) {
return asRow(rowMap, null, true);
}
public Object[] asUpdateRow(Map<String,?> rowMap) {
return asRow(rowMap, Column.KEEP_VALUE, false);
}
/**
* @return the generated RowId added to a row of values created via {@link
* #asRowWithRowId}
* @usage _intermediate_method_
*/
public RowId getRowId(Object[] row) {
return (RowId)row[_columns.size()];
}
/**
* Converts a map of columnName -> columnValue to an array of row values.
*/
private Object[] asRow(Map<String,?> rowMap, Object defaultValue,
boolean returnRowId)
{
int len = _columns.size();
if(returnRowId) {
++len;
}
Object[] row = new Object[len];
if(defaultValue != null) {
Arrays.fill(row, defaultValue);
}
if(returnRowId) {
row[len - 1] = ColumnImpl.RETURN_ROW_ID;
}
if(rowMap == null) {
return row;
}
for(ColumnImpl col : _columns) {
if(rowMap.containsKey(col.getName())) {
col.setRowValue(row, col.getRowValue(rowMap));
}
}
return row;
}
public Object[] addRow(Object... row) throws IOException {
return addRows(Collections.singletonList(row), _singleRowBufferH).get(0);
}
public <M extends Map<String,Object>> M addRowFromMap(M row)
throws IOException
{
Object[] rowValues = asRow(row);
addRow(rowValues);
returnRowValues(row, rowValues, _autoNumColumns);
return row;
}
public List<? extends Object[]> addRows(List<? extends Object[]> rows)
throws IOException
{
return addRows(rows, _multiRowBufferH);
}
public <M extends Map<String,Object>> List<M> addRowsFromMaps(List<M> rows)
throws IOException
{
List<Object[]> rowValuesList = new ArrayList<Object[]>(rows.size());
for(Map<String,Object> row : rows) {
rowValuesList.add(asRow(row));
}
addRows(rowValuesList);
if(!_autoNumColumns.isEmpty()) {
for(int i = 0; i < rowValuesList.size(); ++i) {
Map<String,Object> row = rows.get(i);
Object[] rowValues = rowValuesList.get(i);
returnRowValues(row, rowValues, _autoNumColumns);
}
}
return rows;
}
private static void returnRowValues(Map<String,Object> row, Object[] rowValues,
List<ColumnImpl> cols)
{
for(ColumnImpl col : cols) {
col.setRowValue(row, col.getRowValue(rowValues));
}
}
/**
* Add multiple rows to this table, only writing to disk after all
* rows have been written, and every time a data page is filled.
* @param inRows List of Object[] row values
* @param writeRowBufferH TempBufferHolder used to generate buffers for
* writing the row data
*/
private List<? extends Object[]> addRows(List<? extends Object[]> rows,
TempBufferHolder writeRowBufferH)
throws IOException
{
if(rows.isEmpty()) {
return rows;
}
getPageChannel().startWrite();
try {
List<Object[]> dupeRows = null;
ByteBuffer[] rowData = new ByteBuffer[rows.size()];
int numCols = _columns.size();
for (int i = 0; i < rows.size(); i++) {
// we need to make sure the row is the right length and is an Object[]
// (fill with null if too short). note, if the row is copied the caller
// will not be able to access any generated auto-number value, but if
// they need that info they should use a row array of the right
// size/type!
Object[] row = rows.get(i);
if((row.length < numCols) || (row.getClass() != Object[].class)) {
row = dupeRow(row, numCols);
// copy the input rows to a modifiable list so we can update the
// elements
if(dupeRows == null) {
dupeRows = new ArrayList<Object[]>(rows);
rows = dupeRows;
}
// we copied the row, so put the copy back into the rows list
dupeRows.set(i, row);
}
// fill in autonumbers
handleAutoNumbersForAdd(row);
// write the row of data to a temporary buffer
rowData[i] = createRow(row,
writeRowBufferH.getPageBuffer(getPageChannel()));
if (rowData[i].limit() > getFormat().MAX_ROW_SIZE) {
throw new IOException("Row size " + rowData[i].limit() +
" is too large");
}
}
ByteBuffer dataPage = null;
int pageNumber = PageChannel.INVALID_PAGE_NUMBER;
for (int i = 0; i < rowData.length; i++) {
int rowSize = rowData[i].remaining();
Object[] row = rows.get(i);
// handle foreign keys before adding to table
_fkEnforcer.addRow(row);
// get page with space
dataPage = findFreeRowSpace(rowSize, dataPage, pageNumber);
pageNumber = _addRowBufferH.getPageNumber();
// write out the row data
int rowNum = addDataPageRow(dataPage, rowSize, getFormat(), 0);
dataPage.put(rowData[i]);
// update the indexes
RowIdImpl rowId = new RowIdImpl(pageNumber, rowNum);
for(IndexData indexData : _indexDatas) {
indexData.addRow(row, rowId);
}
// return rowTd if desired
if((row.length > numCols) && (row[numCols] == ColumnImpl.RETURN_ROW_ID)) {
row[numCols] = rowId;
}
}
writeDataPage(dataPage, pageNumber);
// Update tdef page
updateTableDefinition(rows.size());
} finally {
getPageChannel().finishWrite();
}
return rows;
}
public Row updateRow(Row row) throws IOException {
return updateRowFromMap(
getDefaultCursor().getRowState(), (RowIdImpl)row.getId(), row);
}
/**
* Update the row with the given id. Provided RowId must have previously
* been returned from this Table.
* @return the given row, updated with the current row values
* @throws IllegalStateException if the given row is not valid, or deleted.
* @usage _intermediate_method_
*/
public Object[] updateRow(RowId rowId, Object... row) throws IOException {
return updateRow(
getDefaultCursor().getRowState(), (RowIdImpl)rowId, row);
}
/**
* Update the given column's value for the given row id. Provided RowId
* must have previously been returned from this Table.
* @throws IllegalStateException if the given row is not valid, or deleted.
* @usage _intermediate_method_
*/
public void updateValue(Column column, RowId rowId, Object value)
throws IOException
{
Object[] row = new Object[_columns.size()];
Arrays.fill(row, Column.KEEP_VALUE);
column.setRowValue(row, value);
updateRow(rowId, row);
}
public <M extends Map<String,Object>> M updateRowFromMap(
RowState rowState, RowIdImpl rowId, M row)
throws IOException
{
Object[] rowValues = updateRow(rowState, rowId, asUpdateRow(row));
returnRowValues(row, rowValues, _columns);
return row;
}
/**
* Update the row for the given rowId.
* @usage _advanced_method_
*/
public Object[] updateRow(RowState rowState, RowIdImpl rowId, Object... row)
throws IOException
{
requireValidRowId(rowId);
getPageChannel().startWrite();
try {
// ensure that the relevant row state is up-to-date
ByteBuffer rowBuffer = positionAtRowData(rowState, rowId);
int oldRowSize = rowBuffer.remaining();
requireNonDeletedRow(rowState, rowId);
// we need to make sure the row is the right length & type (fill with
// null if too short).
if((row.length < _columns.size()) || (row.getClass() != Object[].class)) {
row = dupeRow(row, _columns.size());
}
// hang on to the raw values of var length columns we are "keeping". this
// will allow us to re-use pre-written var length data, which can save
// space for things like long value columns.
Map<ColumnImpl,byte[]> keepRawVarValues =
(!_varColumns.isEmpty() ? new HashMap<ColumnImpl,byte[]>() : null);
for(ColumnImpl column : _columns) {
if(_autoNumColumns.contains(column)) {
// fill in any auto-numbers (we don't allow autonumber values to be
// modified)
column.setRowValue(row, getRowColumn(getFormat(), rowBuffer, column,
rowState, null));
} else if(column.getRowValue(row) == Column.KEEP_VALUE) {
// fill in any "keep value" fields
column.setRowValue(row, getRowColumn(getFormat(), rowBuffer, column,
rowState, keepRawVarValues));
} else if(_indexColumns.contains(column)) {
// read row value to help update indexes
getRowColumn(getFormat(), rowBuffer, column, rowState, null);
}
}
// generate new row bytes
ByteBuffer newRowData = createRow(
row, _singleRowBufferH.getPageBuffer(getPageChannel()), oldRowSize,
keepRawVarValues);
if (newRowData.limit() > getFormat().MAX_ROW_SIZE) {
throw new IOException("Row size " + newRowData.limit() +
" is too large");
}
if(!_indexDatas.isEmpty()) {
Object[] oldRowValues = rowState.getRowValues();
// check foreign keys before actually updating
_fkEnforcer.updateRow(oldRowValues, row);
// delete old values from indexes
for(IndexData indexData : _indexDatas) {
indexData.deleteRow(oldRowValues, rowId);
}
}
// see if we can squeeze the new row data into the existing row
rowBuffer.reset();
int rowSize = newRowData.remaining();
ByteBuffer dataPage = null;
int pageNumber = PageChannel.INVALID_PAGE_NUMBER;
if(oldRowSize >= rowSize) {
// awesome, slap it in!
rowBuffer.put(newRowData);
// grab the page we just updated
dataPage = rowState.getFinalPage();
pageNumber = rowState.getFinalRowId().getPageNumber();
} else {
// bummer, need to find a new page for the data
dataPage = findFreeRowSpace(rowSize, null,
PageChannel.INVALID_PAGE_NUMBER);
pageNumber = _addRowBufferH.getPageNumber();
RowIdImpl headerRowId = rowState.getHeaderRowId();
ByteBuffer headerPage = rowState.getHeaderPage();
if(pageNumber == headerRowId.getPageNumber()) {
// new row is on the same page as header row, share page
dataPage = headerPage;
}
// write out the new row data (set the deleted flag on the new data row
// so that it is ignored during normal table traversal)
int rowNum = addDataPageRow(dataPage, rowSize, getFormat(),
DELETED_ROW_MASK);
dataPage.put(newRowData);
// write the overflow info into the header row and clear out the
// remaining header data
rowBuffer = PageChannel.narrowBuffer(
headerPage,
findRowStart(headerPage, headerRowId.getRowNumber(), getFormat()),
findRowEnd(headerPage, headerRowId.getRowNumber(), getFormat()));
rowBuffer.put((byte)rowNum);
ByteUtil.put3ByteInt(rowBuffer, pageNumber);
ByteUtil.clearRemaining(rowBuffer);
// set the overflow flag on the header row
int headerRowIndex = getRowStartOffset(headerRowId.getRowNumber(),
getFormat());
headerPage.putShort(headerRowIndex,
(short)(headerPage.getShort(headerRowIndex)
| OVERFLOW_ROW_MASK));
if(pageNumber != headerRowId.getPageNumber()) {
writeDataPage(headerPage, headerRowId.getPageNumber());
}
}
// update the indexes
for(IndexData indexData : _indexDatas) {
indexData.addRow(row, rowId);
}
writeDataPage(dataPage, pageNumber);
updateTableDefinition(0);
} finally {
getPageChannel().finishWrite();
}
return row;
}
private ByteBuffer findFreeRowSpace(int rowSize, ByteBuffer dataPage,
int pageNumber)
throws IOException
{
// assume incoming page is modified
boolean modifiedPage = true;
if(dataPage == null) {
// find owned page w/ free space
dataPage = findFreeRowSpace(_ownedPages, _freeSpacePages,
_addRowBufferH);
if(dataPage == null) {
// No data pages exist (with free space). Create a new one.
return newDataPage();
}
// found a page, see if it will work
pageNumber = _addRowBufferH.getPageNumber();
// since we just loaded this page, it is not yet modified
modifiedPage = false;
}
if(!rowFitsOnDataPage(rowSize, dataPage, getFormat())) {
// Last data page is full. Write old one and create a new one.
if(modifiedPage) {
writeDataPage(dataPage, pageNumber);
}
_freeSpacePages.removePageNumber(pageNumber, true);
dataPage = newDataPage();
}
return dataPage;
}
static ByteBuffer findFreeRowSpace(
UsageMap ownedPages, UsageMap freeSpacePages,
TempPageHolder rowBufferH)
throws IOException
{
// find last data page (Not bothering to check other pages for free
// space.)
UsageMap.PageCursor revPageCursor = ownedPages.cursor();
revPageCursor.afterLast();
while(true) {
int tmpPageNumber = revPageCursor.getPreviousPage();
if(tmpPageNumber < 0) {
break;
}
// only use if actually listed in free space pages
if(!freeSpacePages.containsPageNumber(tmpPageNumber)) {
continue;
}
ByteBuffer dataPage = rowBufferH.setPage(ownedPages.getPageChannel(),
tmpPageNumber);
if(dataPage.get() == PageTypes.DATA) {
// found last data page with free space
return dataPage;
}
}
return null;
}
/**
* Updates the table definition after rows are modified.
*/
private void updateTableDefinition(int rowCountInc) throws IOException
{
// load table definition
ByteBuffer tdefPage = _tableDefBufferH.setPage(getPageChannel(),
_tableDefPageNumber);
// make sure rowcount and autonumber are up-to-date
_rowCount += rowCountInc;
tdefPage.putInt(getFormat().OFFSET_NUM_ROWS, _rowCount);
tdefPage.putInt(getFormat().OFFSET_NEXT_AUTO_NUMBER, _lastLongAutoNumber);
int ctypeOff = getFormat().OFFSET_NEXT_COMPLEX_AUTO_NUMBER;
if(ctypeOff >= 0) {
tdefPage.putInt(ctypeOff, _lastComplexTypeAutoNumber);
}
// write any index changes
for (IndexData indexData : _indexDatas) {
// write the unique entry count for the index to the table definition
// page
tdefPage.putInt(indexData.getUniqueEntryCountOffset(),
indexData.getUniqueEntryCount());
// write the entry page for the index
indexData.update();
}
// write modified table definition
getPageChannel().writePage(tdefPage, _tableDefPageNumber);
}
/**
* Create a new data page
* @return Page number of the new page
*/
private ByteBuffer newDataPage() throws IOException {
ByteBuffer dataPage = _addRowBufferH.setNewPage(getPageChannel());
dataPage.put(PageTypes.DATA); //Page type
dataPage.put((byte) 1); //Unknown
dataPage.putShort((short)getFormat().DATA_PAGE_INITIAL_FREE_SPACE); //Free space in this page
dataPage.putInt(_tableDefPageNumber); //Page pointer to table definition
dataPage.putInt(0); //Unknown
dataPage.putShort((short)0); //Number of rows on this page
int pageNumber = _addRowBufferH.getPageNumber();
getPageChannel().writePage(dataPage, pageNumber);
_ownedPages.addPageNumber(pageNumber);
_freeSpacePages.addPageNumber(pageNumber);
return dataPage;
}
/**
* @usage _advanced_method_
*/
public ByteBuffer createRow(Object[] rowArray, ByteBuffer buffer)
throws IOException
{
return createRow(rowArray, buffer, 0,
Collections.<ColumnImpl,byte[]>emptyMap());
}
/**
* Serialize a row of Objects into a byte buffer.
*
* @param rowArray row data, expected to be correct length for this table
* @param buffer buffer to which to write the row data
* @param minRowSize min size for result row
* @param rawVarValues optional, pre-written values for var length columns
* (enables re-use of previously written values).
* @return the given buffer, filled with the row data
*/
private ByteBuffer createRow(Object[] rowArray, ByteBuffer buffer,
int minRowSize,
Map<ColumnImpl,byte[]> rawVarValues)
throws IOException
{
buffer.putShort(_maxColumnCount);
NullMask nullMask = new NullMask(_maxColumnCount);
//Fixed length column data comes first
int fixedDataStart = buffer.position();
int fixedDataEnd = fixedDataStart;
for (ColumnImpl col : _columns) {
if(col.isVariableLength()) {
continue;
}
Object rowValue = col.getRowValue(rowArray);
if (col.getType() == DataType.BOOLEAN) {
if(ColumnImpl.toBooleanValue(rowValue)) {
//Booleans are stored in the null mask
nullMask.markNotNull(col);
}
rowValue = null;
}
if(rowValue != null) {
// we have a value to write
nullMask.markNotNull(col);
// remainingRowLength is ignored when writing fixed length data
buffer.position(fixedDataStart + col.getFixedDataOffset());
buffer.put(col.write(rowValue, 0));
}
// always insert space for the entire fixed data column length
// (including null values), access expects the row to always be at least
// big enough to hold all fixed values
buffer.position(fixedDataStart + col.getFixedDataOffset() +
col.getLength());
// keep track of the end of fixed data
if(buffer.position() > fixedDataEnd) {
fixedDataEnd = buffer.position();
}
}
// reposition at end of fixed data
buffer.position(fixedDataEnd);
// only need this info if this table contains any var length data
if(_maxVarColumnCount > 0) {
int maxRowSize = getFormat().MAX_ROW_SIZE;
// figure out how much space remains for var length data. first,
// account for already written space
maxRowSize -= buffer.position();
// now, account for trailer space
int trailerSize = (nullMask.byteSize() + 4 + (_maxVarColumnCount * 2));
maxRowSize -= trailerSize;
// for each non-null long value column we need to reserve a small
// amount of space so that we don't end up running out of row space
// later by being too greedy
for (ColumnImpl varCol : _varColumns) {
if((varCol.getType().isLongValue()) &&
(varCol.getRowValue(rowArray) != null)) {
maxRowSize -= getFormat().SIZE_LONG_VALUE_DEF;
}
}
//Now write out variable length column data
short[] varColumnOffsets = new short[_maxVarColumnCount];
int varColumnOffsetsIndex = 0;
for (ColumnImpl varCol : _varColumns) {
short offset = (short) buffer.position();
Object rowValue = varCol.getRowValue(rowArray);
if (rowValue != null) {
// we have a value
nullMask.markNotNull(varCol);
byte[] rawValue = null;
ByteBuffer varDataBuf = null;
if(((rawValue = rawVarValues.get(varCol)) != null) &&
(rawValue.length <= maxRowSize)) {
// save time and potentially db space, re-use raw value
varDataBuf = ByteBuffer.wrap(rawValue);
} else {
// write column value
varDataBuf = varCol.write(rowValue, maxRowSize);
}
maxRowSize -= varDataBuf.remaining();
if(varCol.getType().isLongValue()) {
// we already accounted for some amount of the long value data
// above. add that space back so we don't double count
maxRowSize += getFormat().SIZE_LONG_VALUE_DEF;
}
buffer.put(varDataBuf);
}
// we do a loop here so that we fill in offsets for deleted columns
while(varColumnOffsetsIndex <= varCol.getVarLenTableIndex()) {
varColumnOffsets[varColumnOffsetsIndex++] = offset;
}
}
// fill in offsets for any remaining deleted columns
while(varColumnOffsetsIndex < varColumnOffsets.length) {
varColumnOffsets[varColumnOffsetsIndex++] = (short) buffer.position();
}
// record where we stopped writing
int eod = buffer.position();
// insert padding if necessary
padRowBuffer(buffer, minRowSize, trailerSize);
buffer.putShort((short) eod); //EOD marker
//Now write out variable length offsets
//Offsets are stored in reverse order
for (int i = _maxVarColumnCount - 1; i >= 0; i--) {
buffer.putShort(varColumnOffsets[i]);
}
buffer.putShort(_maxVarColumnCount); //Number of var length columns
} else {
// insert padding for row w/ no var cols
padRowBuffer(buffer, minRowSize, nullMask.byteSize());
}
nullMask.write(buffer); //Null mask
buffer.flip();
return buffer;
}
/**
* Fill in all autonumber column values.
*/
private void handleAutoNumbersForAdd(Object[] row)
throws IOException
{
if(_autoNumColumns.isEmpty()) {
return;
}
Object complexAutoNumber = null;
for(ColumnImpl col : _autoNumColumns) {
// ignore given row value, use next autonumber
ColumnImpl.AutoNumberGenerator autoNumGen = col.getAutoNumberGenerator();
Object rowValue = null;
if(autoNumGen.getType() != DataType.COMPLEX_TYPE) {
rowValue = autoNumGen.getNext(null);
} else {
// complex type auto numbers are shared across all complex columns
// in the row
complexAutoNumber = autoNumGen.getNext(complexAutoNumber);
rowValue = complexAutoNumber;
}
col.setRowValue(row, rowValue);
}
}
private static void padRowBuffer(ByteBuffer buffer, int minRowSize,
int trailerSize)
{
int pos = buffer.position();
if((pos + trailerSize) < minRowSize) {
// pad the row to get to the min byte size
int padSize = minRowSize - (pos + trailerSize);
ByteUtil.clearRange(buffer, pos, pos + padSize);
ByteUtil.forward(buffer, padSize);
}
}
public int getRowCount() {
return _rowCount;
}
int getNextLongAutoNumber() {
// note, the saved value is the last one handed out, so pre-increment
return ++_lastLongAutoNumber;
}
int getLastLongAutoNumber() {
// gets the last used auto number (does not modify)
return _lastLongAutoNumber;
}
int getNextComplexTypeAutoNumber() {
// note, the saved value is the last one handed out, so pre-increment
return ++_lastComplexTypeAutoNumber;
}
int getLastComplexTypeAutoNumber() {
// gets the last used auto number (does not modify)
return _lastComplexTypeAutoNumber;
}
@Override
public String toString() {
return CustomToStringStyle.builder(this)
.append("type", (_tableType +
((_tableType == TYPE_USER) ? " (USER)" : " (SYSTEM)")))
.append("name", _name)
.append("rowCount", _rowCount)
.append("columnCount", _columns.size())
.append("indexCount(data)", _indexCount)
.append("logicalIndexCount", _logicalIndexCount)
.append("columns", _columns)
.append("indexes", _indexes)
.append("ownedPages", _ownedPages)
.toString();
}
/**
* @return A simple String representation of the entire table in
* tab-delimited format
* @usage _general_method_
*/
public String display() throws IOException {
return display(Long.MAX_VALUE);
}
/**
* @param limit Maximum number of rows to display
* @return A simple String representation of the entire table in
* tab-delimited format
* @usage _general_method_
*/
public String display(long limit) throws IOException {
reset();
StringWriter rtn = new StringWriter();
new ExportUtil.Builder(getDefaultCursor()).setDelimiter("\t").setHeader(true)
.exportWriter(new BufferedWriter(rtn));
return rtn.toString();
}
/**
* Updates free space and row info for a new row of the given size in the
* given data page. Positions the page for writing the row data.
* @return the row number of the new row
* @usage _advanced_method_
*/
public static int addDataPageRow(ByteBuffer dataPage,
int rowSize,
JetFormat format,
int rowFlags)
{
int rowSpaceUsage = getRowSpaceUsage(rowSize, format);
// Decrease free space record.
short freeSpaceInPage = dataPage.getShort(format.OFFSET_FREE_SPACE);
dataPage.putShort(format.OFFSET_FREE_SPACE, (short) (freeSpaceInPage -
rowSpaceUsage));
// Increment row count record.
short rowCount = dataPage.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
dataPage.putShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE,
(short) (rowCount + 1));
// determine row position
short rowLocation = findRowEnd(dataPage, rowCount, format);
rowLocation -= rowSize;
// write row position
dataPage.putShort(getRowStartOffset(rowCount, format),
(short)(rowLocation | rowFlags));
// set position for row data
dataPage.position(rowLocation);
return rowCount;
}
/**
* Returns the row count for the current page. If the page is invalid
* ({@code null}) or the page is not a DATA page, 0 is returned.
*/
static int getRowsOnDataPage(ByteBuffer rowBuffer, JetFormat format)
throws IOException
{
int rowsOnPage = 0;
if((rowBuffer != null) && (rowBuffer.get(0) == PageTypes.DATA)) {
rowsOnPage = rowBuffer.getShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE);
}
return rowsOnPage;
}
/**
* @throws IllegalStateException if the given rowId is invalid
*/
private static void requireValidRowId(RowIdImpl rowId) {
if(!rowId.isValid()) {
throw new IllegalArgumentException("Given rowId is invalid: " + rowId);
}
}
/**
* @throws IllegalStateException if the given row is invalid or deleted
*/
private static void requireNonDeletedRow(RowState rowState, RowIdImpl rowId)
{
if(!rowState.isValid()) {
throw new IllegalArgumentException(
"Given rowId is invalid for this table: " + rowId);
}
if(rowState.isDeleted()) {
throw new IllegalStateException("Row is deleted: " + rowId);
}
}
/**
* @usage _advanced_method_
*/
public static boolean isDeletedRow(short rowStart) {
return ((rowStart & DELETED_ROW_MASK) != 0);
}
/**
* @usage _advanced_method_
*/
public static boolean isOverflowRow(short rowStart) {
return ((rowStart & OVERFLOW_ROW_MASK) != 0);
}
/**
* @usage _advanced_method_
*/
public static short cleanRowStart(short rowStart) {
return (short)(rowStart & OFFSET_MASK);
}
/**
* @usage _advanced_method_
*/
public static short findRowStart(ByteBuffer buffer, int rowNum,
JetFormat format)
{
return cleanRowStart(
buffer.getShort(getRowStartOffset(rowNum, format)));
}
/**
* @usage _advanced_method_
*/
public static int getRowStartOffset(int rowNum, JetFormat format)
{
return format.OFFSET_ROW_START + (format.SIZE_ROW_LOCATION * rowNum);
}
/**
* @usage _advanced_method_
*/
public static short findRowEnd(ByteBuffer buffer, int rowNum,
JetFormat format)
{
return (short)((rowNum == 0) ?
format.PAGE_SIZE :
cleanRowStart(
buffer.getShort(getRowEndOffset(rowNum, format))));
}
/**
* @usage _advanced_method_
*/
public static int getRowEndOffset(int rowNum, JetFormat format)
{
return format.OFFSET_ROW_START + (format.SIZE_ROW_LOCATION * (rowNum - 1));
}
/**
* @usage _advanced_method_
*/
public static int getRowSpaceUsage(int rowSize, JetFormat format)
{
return rowSize + format.SIZE_ROW_LOCATION;
}
private void getAutoNumberColumns() {
for(ColumnImpl c : _columns) {
if(c.isAutoNumber()) {
_autoNumColumns.add(c);
}
}
}
/**
* Returns {@code true} if a row of the given size will fit on the given
* data page, {@code false} otherwise.
* @usage _advanced_method_
*/
public static boolean rowFitsOnDataPage(
int rowLength, ByteBuffer dataPage, JetFormat format)
throws IOException
{
int rowSpaceUsage = getRowSpaceUsage(rowLength, format);
short freeSpaceInPage = dataPage.getShort(format.OFFSET_FREE_SPACE);
int rowsOnPage = getRowsOnDataPage(dataPage, format);
return ((rowSpaceUsage <= freeSpaceInPage) &&
(rowsOnPage < format.MAX_NUM_ROWS_ON_DATA_PAGE));
}
/**
* Duplicates and returns a row of data, optionally with a longer length
* filled with {@code null}.
*/
static Object[] dupeRow(Object[] row, int newRowLength) {
Object[] copy = new Object[newRowLength];
System.arraycopy(row, 0, copy, 0, Math.min(row.length, newRowLength));
return copy;
}
/** various statuses for the row data */
private enum RowStatus {
INIT, INVALID_PAGE, INVALID_ROW, VALID, DELETED, NORMAL, OVERFLOW;
}
/** the phases the RowState moves through as the data is parsed */
private enum RowStateStatus {
INIT, AT_HEADER, AT_FINAL;
}
/**
* Maintains the state of reading a row of data.
* @usage _advanced_class_
*/
public final class RowState implements ErrorHandler.Location
{
/** Buffer used for reading the header row data pages */
private final TempPageHolder _headerRowBufferH;
/** the header rowId */
private RowIdImpl _headerRowId = RowIdImpl.FIRST_ROW_ID;
/** the number of rows on the header page */
private int _rowsOnHeaderPage;
/** the rowState status */
private RowStateStatus _status = RowStateStatus.INIT;
/** the row status */
private RowStatus _rowStatus = RowStatus.INIT;
/** buffer used for reading overflow pages */
private final TempPageHolder _overflowRowBufferH =
TempPageHolder.newHolder(TempBufferHolder.Type.SOFT);
/** the row buffer which contains the final data (after following any
overflow pointers) */
private ByteBuffer _finalRowBuffer;
/** the rowId which contains the final data (after following any overflow
pointers) */
private RowIdImpl _finalRowId = null;
/** true if the row values array has data */
private boolean _haveRowValues;
/** values read from the last row */
private final Object[] _rowValues;
/** null mask for the last row */
private NullMask _nullMask;
/** last modification count seen on the table we track this so that the
rowState can detect updates to the table and re-read any buffered
data */
private int _lastModCount;
/** optional error handler to use when row errors are encountered */
private ErrorHandler _errorHandler;
/** cached variable column offsets for jump-table based rows */
private short[] _varColOffsets;
private RowState(TempBufferHolder.Type headerType) {
_headerRowBufferH = TempPageHolder.newHolder(headerType);
_rowValues = new Object[TableImpl.this.getColumnCount()];
_lastModCount = TableImpl.this._modCount;
}
public TableImpl getTable() {
return TableImpl.this;
}
public ErrorHandler getErrorHandler() {
return((_errorHandler != null) ? _errorHandler :
getTable().getErrorHandler());
}
public void setErrorHandler(ErrorHandler newErrorHandler) {
_errorHandler = newErrorHandler;
}
public void reset() {
_finalRowId = null;
_finalRowBuffer = null;
_rowsOnHeaderPage = 0;
_status = RowStateStatus.INIT;
_rowStatus = RowStatus.INIT;
_varColOffsets = null;
_nullMask = null;
if(_haveRowValues) {
Arrays.fill(_rowValues, null);
_haveRowValues = false;
}
}
public boolean isUpToDate() {
return(TableImpl.this._modCount == _lastModCount);
}
private void checkForModification() {
if(!isUpToDate()) {
reset();
_headerRowBufferH.invalidate();
_overflowRowBufferH.invalidate();
_lastModCount = TableImpl.this._modCount;
}
}
private ByteBuffer getFinalPage()
throws IOException
{
if(_finalRowBuffer == null) {
// (re)load current page
_finalRowBuffer = getHeaderPage();
}
return _finalRowBuffer;
}
public RowIdImpl getFinalRowId() {
if(_finalRowId == null) {
_finalRowId = getHeaderRowId();
}
return _finalRowId;
}
private void setRowStatus(RowStatus rowStatus) {
_rowStatus = rowStatus;
}
public boolean isValid() {
return(_rowStatus.ordinal() >= RowStatus.VALID.ordinal());
}
public boolean isDeleted() {
return(_rowStatus == RowStatus.DELETED);
}
public boolean isOverflow() {
return(_rowStatus == RowStatus.OVERFLOW);
}
public boolean isHeaderPageNumberValid() {
return(_rowStatus.ordinal() > RowStatus.INVALID_PAGE.ordinal());
}
public boolean isHeaderRowNumberValid() {
return(_rowStatus.ordinal() > RowStatus.INVALID_ROW.ordinal());
}
private void setStatus(RowStateStatus status) {
_status = status;
}
public boolean isAtHeaderRow() {
return(_status.ordinal() >= RowStateStatus.AT_HEADER.ordinal());
}
public boolean isAtFinalRow() {
return(_status.ordinal() >= RowStateStatus.AT_FINAL.ordinal());
}
private Object setRowValue(int idx, Object value) {
_haveRowValues = true;
_rowValues[idx] = value;
return value;
}
public Object[] getRowValues() {
return dupeRow(_rowValues, _rowValues.length);
}
public NullMask getNullMask(ByteBuffer rowBuffer) throws IOException {
if(_nullMask == null) {
_nullMask = getRowNullMask(rowBuffer);
}
return _nullMask;
}
private short[] getVarColOffsets() {
return _varColOffsets;
}
private void setVarColOffsets(short[] varColOffsets) {
_varColOffsets = varColOffsets;
}
public RowIdImpl getHeaderRowId() {
return _headerRowId;
}
public int getRowsOnHeaderPage() {
return _rowsOnHeaderPage;
}
private ByteBuffer getHeaderPage()
throws IOException
{
checkForModification();
return _headerRowBufferH.getPage(getPageChannel());
}
private ByteBuffer setHeaderRow(RowIdImpl rowId)
throws IOException
{
checkForModification();
// don't do any work if we are already positioned correctly
if(isAtHeaderRow() && (getHeaderRowId().equals(rowId))) {
return(isValid() ? getHeaderPage() : null);
}
// rejigger everything
reset();
_headerRowId = rowId;
_finalRowId = rowId;
int pageNumber = rowId.getPageNumber();
int rowNumber = rowId.getRowNumber();
if((pageNumber < 0) || !_ownedPages.containsPageNumber(pageNumber)) {
setRowStatus(RowStatus.INVALID_PAGE);
return null;
}
_finalRowBuffer = _headerRowBufferH.setPage(getPageChannel(),
pageNumber);
_rowsOnHeaderPage = getRowsOnDataPage(_finalRowBuffer, getFormat());
if((rowNumber < 0) || (rowNumber >= _rowsOnHeaderPage)) {
setRowStatus(RowStatus.INVALID_ROW);
return null;
}
setRowStatus(RowStatus.VALID);
return _finalRowBuffer;
}
private ByteBuffer setOverflowRow(RowIdImpl rowId)
throws IOException
{
// this should never see modifications because it only happens within
// the positionAtRowData method
if(!isUpToDate()) {
throw new IllegalStateException("Table modified while searching?");
}
if(_rowStatus != RowStatus.OVERFLOW) {
throw new IllegalStateException("Row is not an overflow row?");
}
_finalRowId = rowId;
_finalRowBuffer = _overflowRowBufferH.setPage(getPageChannel(),
rowId.getPageNumber());
return _finalRowBuffer;
}
private Object handleRowError(ColumnImpl column, byte[] columnData,
Exception error)
throws IOException
{
return getErrorHandler().handleRowError(column, columnData,
this, error);
}
@Override
public String toString() {
return CustomToStringStyle.valueBuilder(this)
.append("headerRowId", _headerRowId)
.append("finalRowId", _finalRowId)
.toString();
}
}
}
|