File: T_FileSystemData.java

package info (click to toggle)
derby 10.14.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 79,056 kB
  • sloc: java: 691,961; sql: 42,686; xml: 20,512; sh: 3,373; sed: 96; makefile: 60
file content (1162 lines) | stat: -rw-r--r-- 36,851 bytes parent folder | download | duplicates (4)
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
/*

   Derby - Class org.apache.derbyTesting.unitTests.store.T_FileSystemData

   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

 */

package org.apache.derbyTesting.unitTests.store;

import org.apache.derby.impl.store.raw.data.*;

import org.apache.derbyTesting.unitTests.harness.T_MultiThreadedIterations;
import org.apache.derbyTesting.unitTests.harness.T_Fail;

import org.apache.derby.iapi.services.context.ContextService;
import org.apache.derby.iapi.services.context.ContextManager;
import org.apache.derby.iapi.services.locks.*;
import org.apache.derby.iapi.services.monitor.Monitor;
import org.apache.derby.shared.common.sanity.SanityManager;
import org.apache.derby.iapi.services.io.Storable;
import org.apache.derby.iapi.services.property.PropertyUtil;

import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.store.raw.*;

import org.apache.derby.iapi.store.raw.xact.RawTransaction;
import org.apache.derby.iapi.store.raw.data.RawContainerHandle;

import org.apache.derby.iapi.store.access.conglomerate.LogicalUndo;
import org.apache.derby.iapi.reference.Property;
import java.io.*;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.PrivilegedAction;
import java.security.AccessController;
import java.util.Properties;
/**
	An Impl unittest for rawstore data that is based on the FileSystem
*/

public class T_FileSystemData extends T_MultiThreadedIterations {

	private static final String testService = "fileSystemDataTest";

	static final String REC_001 = "McLaren";
	static final String REC_002 = "Ferrari";
	static final String REC_003 = "Benetton";
	static final String REC_004 = "Prost";
	static final String REC_005 = "Tyrell";
	static final String REC_006 = "Derby, Natscape, Goatscape, the popular names";
	static final String REC_007 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";

	static final String SP1 = "savepoint1";
	static final String SP2 = "savepoint2";


	static RawStoreFactory	factory;
	static LockFactory lf;
	static long commonContainer = -1;

	static boolean testRollback; // initialize in start
	static final String TEST_ROLLBACK_OFF = "derby.RawStore.RollbackTestOff";

	private static ContextService contextService;
	private T_Util t_util;

	public T_FileSystemData() 
	{
		super();
	}

	/**
	  @exception StandardException cannot startup the context service
	 */
	public void boot(boolean create, Properties startParams)
		 throws StandardException
	{
		super.boot(create, startParams);
		contextService = getContextService();
	}


	/*
	** Methods required by T_Generic
	*/

	protected String getModuleToTestProtocolName() {
		return RawStoreFactory.MODULE;
	}


	/**
		Run the tests

		@exception T_Fail Unexpected behaviour from the API
	 */
	protected void setupTest() throws T_Fail 
	{
		String rollbackOff = PropertyUtil.getSystemProperty(TEST_ROLLBACK_OFF);
		testRollback = !Boolean.valueOf(rollbackOff).booleanValue();


		// don't automatic boot this service if it gets left around
		if (startParams == null) {
			startParams = new Properties();
		}

		// see if we are testing encryption
		startParams = T_Util.setEncryptionParam(startParams);

		startParams.put(Property.NO_AUTO_BOOT, Boolean.TRUE.toString());
		// remove the service directory to ensure a clean run
		startParams.put(Property.DELETE_ON_CREATE, Boolean.TRUE.toString());

		try {
			factory = (RawStoreFactory) createPersistentService(getModuleToTestProtocolName(),
								testService, startParams);
			if (factory == null) {
				throw T_Fail.testFailMsg(getModuleToTestProtocolName() + " service not started.");
			}

			lf = factory.getLockFactory();
			if (lf == null) {
				throw T_Fail.testFailMsg("LockFactory.MODULE not found");
			}
		} catch (StandardException mse) {
			throw T_Fail.exceptionFail(mse);
		}

		t_util = new T_Util(factory, lf, contextService);
		commonContainer = commonContainer();

		return;
	}


	/**
	 * T_MultiThreadedIteration method
	 *
	 * @exception T_Fail Unexpected behaviour from the API
	 */
	protected void joinSetupTest() throws T_Fail {

		T_Fail.T_ASSERT(factory != null, "raw store factory not setup ");
		T_Fail.T_ASSERT(contextService != null, "Context service not setup ");
		T_Fail.T_ASSERT(commonContainer != -1, "common container not setup ");

		t_util = new T_Util(factory, lf, contextService);

	}

	protected T_MultiThreadedIterations newTestObject() {
		return new T_FileSystemData();
	}

	/**
	  run the test

	  @exception T_Fail Unexpected behaviour from the API
	*/
	protected void runTestSet() throws T_Fail {

		// get a utility helper

		ContextManager cm1 = contextService.newContextManager();
		contextService.setCurrentContextManager(cm1);

		try {

			runCostEstimationTests();
			runAllocationTests();

		} catch (StandardException se) {

            //Assume database is not active. DERBY-4856 thread dump
            cm1.cleanupOnError(se, false);
			throw T_Fail.exceptionFail(se);
		}
		finally {

			contextService.resetCurrentContextManager(cm1);
		}
	}

	/*
	 * create a container that all threads can use
	 */
	private long commonContainer() throws T_Fail
	{
		ContextManager cm1 = contextService.newContextManager();
		contextService.setCurrentContextManager(cm1);
		long cid;

		try {
			Transaction t = t_util.t_startTransaction();
			cid = t_util.t_addContainer(t, 0);
			t_util.t_commit(t);
			t.close();
		}
		catch (StandardException se) {

            //Assume database is not active. DERBY-4856 thread dump
            cm1.cleanupOnError(se, false);
			throw T_Fail.exceptionFail(se);
		}
		finally {
			contextService.resetCurrentContextManager(cm1);
		}
		return cid;
	}

	protected void runCostEstimationTests() throws T_Fail, StandardException
	{
		CostEstimationTest1();
	}

	protected void runAllocationTests() throws T_Fail, StandardException
	{
		// don't run these for > 2 threads
		if (threadNumber < 2)
		{
			AllocTest1();			// test remove and reuse of page
			AllocTest2();			// test remove and drop and rollback of remove 
			AllocTest3();			// test multiple alloc page
			AllocTest4();			// test preallocation
		}

		// can't get this test to pass consistently because it depends on
		// timing of the cache.
		// AllocTest5();			// test gettting 1/2 filled page for insert

		AllocMTest1(commonContainer); // test multi thread access to the same container
	}

	/**
		@exception T_Fail Unexpected behaviour from the API
		@exception StandardException Standard Derby error policy
	*/
	protected void CostEstimationTest1() throws StandardException, T_Fail 
	{
		// getEstimatedRowCount(0), setEstimatedRowCount(long count, int flag),
		// getEstimatedPageCount(int flag);

		Transaction t = t_util.t_startTransaction();
		long cid = t_util.t_addContainer(t, 0);
		t_util.t_commit(t);

		ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);

		try
		{
			int numRows = 10;
			T_RawStoreRow row = new T_RawStoreRow(REC_001);
			RecordHandle rh[] = new RecordHandle[numRows];

			// insert numRows rows into container
			for (int i = 0; i < numRows; i++)
				rh[i] = t_util.t_insert(c, row);

			t_util.t_commit(t);

			c = t_util.t_openContainer(t, 0, cid, true);
			if ((c.getEstimatedRowCount(0) != numRows) &&
			    (c.getEstimatedRowCount(0) != (numRows - 1)))
            {
                // due to timing, sometimes estimate row count is 9 rather than
                // 10.

				throw T_Fail.testFailMsg(
                    "expect estimated row count to be " + (numRows - 1) + 
                    " or " + numRows +
                         ", got " + c.getEstimatedRowCount(0));
            }

			// now update them that cause overflowing - expect the same row count
			T_RawStoreRow longRow = new T_RawStoreRow(REC_007);
			for (int i = 0; i < numRows; i++)
				t_util.t_update(c, rh[i], longRow);

			t_util.t_commit(t);

			c = t_util.t_openContainer(t, 0, cid, true);
			if (c.getEstimatedRowCount(0) != numRows)

			if ((c.getEstimatedRowCount(0) != numRows) &&
			    (c.getEstimatedRowCount(0) != (numRows - 1)))
            {
                // due to timing, sometimes estimate row count is 9 rather than
                // 10.
                
				throw T_Fail.testFailMsg(
                    "expect after update same estimated row count, but it is not." +
                    "expect estimated row count to be " + (numRows - 1) + 
                    " or " + numRows + ", got " + c.getEstimatedRowCount(0));
            }

			// now focibly set the row count
			c.setEstimatedRowCount(2*numRows, 0);

			if (c.getEstimatedRowCount(0) != 2*numRows)
				throw T_Fail.testFailMsg("forcibly setting estimated row count doesn't seem to work");

			// now purge some rows, this should alter the row count.
			Page p = null;
			long pnum = 0;
			long purgedCount = 0;
			for (p = c.getFirstPage(); p != null; p = c.getNextPage(pnum)) 
			{
				int rcount = p.recordCount()/3;
				pnum = p.getPageNumber();

				p.deleteAtSlot(0, true, (LogicalUndo)null);
				p.purgeAtSlot(rcount, rcount, true); // purget the middle 1/3 of the page
				purgedCount += rcount + 1;

				p.unlatch();
			}
		
			t_util.t_commit(t);

			c = t_util.t_openContainer(t, 0, cid, true);
			if (c.getEstimatedRowCount(0) != (2*numRows - purgedCount))
				throw T_Fail.testFailMsg("expect " + (2*numRows-purgedCount) + 
										 " after purge"); 
		
			// now get rid of some pages to alter the row count
			REPORT("before page delete, estRC = " + (2*numRows) + " - " + purgedCount);

			for (p = c.getFirstPage(); p != null; p = c.getNextPage(pnum))
			{
				pnum = p.getPageNumber();
				if ((pnum%2) == 0)
				{
					purgedCount += p.nonDeletedRecordCount();
					c.removePage(p);
				}
				else
					p.unlatch();
			}

			t_util.t_commit(t);

			c = t_util.t_openContainer(t, 0, cid, true);
			if (c.getEstimatedRowCount(0) != (2*numRows - purgedCount))
				throw T_Fail.testFailMsg("expect " + (2*numRows-purgedCount) + 
										 " after page remove, got " + c.getEstimatedRowCount(0)); 

			PASS("CostEstimationTest1");
		}
		finally
		{
			t_util.t_commit(t);
			t.close();
		}


	}

	protected void AllocTest1() throws StandardException, T_Fail 
	{
		/**
		  test remove and reuse of page
		*/
		Transaction t = t_util.t_startTransaction();

		try
		{
			long cid = t_util.t_addContainer(t, 0);
			t_util.t_commit(t);

			ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);

		// create 5 pages, each insert a row into it, then remove 2 of them

			Page page1 = t_util.t_getPage(c, ContainerHandle.FIRST_PAGE_NUMBER);
			long p1 = page1.getPageNumber();
			T_RawStoreRow row1 = new T_RawStoreRow(REC_001);
			t_util.t_insert(page1, row1);

			Page page2 = t_util.t_addPage(c);
			long p2 = page2.getPageNumber();
			T_RawStoreRow row2 = new T_RawStoreRow(REC_002);
			int rid2 = t_util.t_insert(page2, row2).getId();

			Page page3 = t_util.t_addPage(c);
			long p3 = page3.getPageNumber();
			T_RawStoreRow row3 = new T_RawStoreRow(REC_003);
			t_util.t_insert(page3, row3);

			Page page4 = t_util.t_addPage(c);
			long p4 = page4.getPageNumber();
			T_RawStoreRow row4 = new T_RawStoreRow(REC_004);
			int rid4 = t_util.t_insert(page4, row4).getId();

			Page page5 = t_util.t_addPage(c);
			long p5 = page5.getPageNumber();
			T_RawStoreRow row5 = new T_RawStoreRow(REC_005);
			t_util.t_insert(page5, row5);

			t_util.t_removePage(c, page2);
			t_util.t_removePage(c, page4);
			t_util.t_commit(t);

		// now all the pages are unlatched
		// pages 2, 4 has been removed, pages 1, 3, 5 has not
		// make sure pages that are removed cannot be found again
			c = t_util.t_openContainer(t, 0, cid, true);

            if (SanityManager.DEBUG)
                SanityManager.DEBUG("SpaceTrace", "containeropened");

			Page p = c.getFirstPage();
			if (p == null)
				throw T_Fail.testFailMsg("get first page failed: expect " + p1 + " got null");
			if (p.getPageNumber() != p1)
				throw T_Fail.testFailMsg("get first page failed: expect " + p1
										 + " got " + p.getPageNumber());

			t_util.t_commit(t);		

		// closing the transaction many times to see if we can get the
		// deallocated page to free

			c = t_util.t_openContainer(t, 0, cid, true);
			p = c.getNextPage(p1);
			if (p == null || p.getPageNumber() != p3)
				throw T_Fail.testFailMsg("get next page failed");
			t_util.t_commit(t);

			c = t_util.t_openContainer(t, 0, cid, true);
			p = c.getNextPage(p3);
			if (p == null || p.getPageNumber() != p5)
				throw T_Fail.testFailMsg("get next page failed");
			t_util.t_commit(t);
		
			c = t_util.t_openContainer(t, 0, cid, true);
			p = t_util.t_getLastPage(c);	// make sure it skips over p5
			if (p == null || p.getPageNumber() != p5)
				throw T_Fail.testFailMsg("getLastPage failed");
			t_util.t_commit(t);

		// see if we can get any deallocated page back in 10 attempts
		// of add page
			int tries = 100;
			T_RawStoreRow row6 = new T_RawStoreRow(REC_001);

			long pnums[] = new long[tries];
			int  rids[] = new int[tries];
			pnums[0] = p2;			// pages 2 and 4 have been removed for a long time
			rids[0] = rid2;
			pnums[1] = p4;
			rids[1] = rid4;

			int match = -1;
			int i;
			for (i = 2 ; match < 0 && i < tries; i++)
			{
				c = t_util.t_openContainer(t, 0, cid, true);
				p = t_util.t_addPage(c);
				pnums[i] =  p.getPageNumber();

				for (int j = 0; j < i-1; j++)
				{
					if (pnums[j] == pnums[i])
					{
						match = j;
						break;
					}
				}

				if (match >= 0)
				{
					// p is a reused one, make sure it is empty
					t_util.t_checkEmptyPage(p);
					RecordHandle rh = t_util.t_insert(p, row6);
					if (rh.getId() == rids[match])
						throw T_Fail.testFailMsg("reused page recordId is not preserved");
					break;
				}
				else
					rids[i] = t_util.t_insert(p, row6).getId();

				t_util.t_removePage(c, p);
				t_util.t_commit(t);
			}
			t_util.t_dropContainer(t, 0, cid); // cleanup

			if (match >= 0)
				PASS("AllocTest1 success in " + i + " tries");
			else
				REPORT("AllocTest1 Not successful in " + i + 
					   " tries.  This is a timing depenedent test so this is not necessarily an indication of failure.");
		}
		finally
		{
			t_util.t_commit(t);
			t.close();
		}

	}

	protected void AllocTest2() throws StandardException, T_Fail 
	{
		/**
		  More Test remove and reuse of page
		*/

		Transaction t = t_util.t_startTransaction();
		int numpages = 30;

		try
		{
			long cid = t_util.t_addContainer(t, 0);
			ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);

			Page[] page = new Page[numpages];

			for (int i = 0; i < numpages; i++)
			{
				page[i] = t_util.t_addPage(c);
				t_util.t_removePage(c, page[i]);
			}

			// make sure a dropped container does not cause problem for page
			// that's been removed
			t_util.t_dropContainer(t, 0, cid); 

			t_util.t_commit(t);

			if (testRollback)
			{
				cid = t_util.t_addContainer(t, 0);
				c = t_util.t_openContainer(t, 0, cid, true);

				for (int i = 0; i < numpages; i++)
				{
					page[i] = t_util.t_addPage(c);
					t_util.t_removePage(c, page[i]);
				}

				t_util.t_abort(t);
			}
		}
		finally
		{
			t_util.t_commit(t);
			t.close();
		}


		PASS("AllocTest2");
	}

	protected void AllocTest3() throws StandardException, T_Fail 
	{
		/* test multiple alloc pages */

		if (!SanityManager.DEBUG)
		{
			REPORT("allocTest3 cannot be run on an insane server");
			return;
		}
        else
        {
            SanityManager.DEBUG_SET(AllocPage.TEST_MULTIPLE_ALLOC_PAGE);

            Transaction t = t_util.t_startTransaction();

            try
            {
                long cid = t_util.t_addContainer(t, 0);
                t_util.t_commit(t);

                ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);

                T_RawStoreRow row = new T_RawStoreRow(REC_001);
                int numrows = 10; // create 10 pages with 1 row each
                
                String threadName = Thread.currentThread().getName();

                Page page;
                for (int i = 0; i < numrows; i++)
                {
                    page = t_util.t_addPage(c);
                    t_util.t_insert(page, row);
                    page.unlatch();
                }

                int checkrows = 0;
                long pnum;
                for (page = c.getFirstPage();
                     page != null;
                     page = c.getNextPage(pnum))
                {
                    pnum = page.getPageNumber();
                    if (page.recordCount() > 0)
                    {
                        t_util.t_checkFetchFirst(page, REC_001);
                        checkrows++;
                    }
                    page.unlatch();
                }
                if (checkrows != numrows)
                    throw T_Fail.testFailMsg("number of rows differ");

                t.setSavePoint(SP1, null);

                // now remove 1/2 of the pages and check results
                int removedPages = 0;
                for (page = c.getFirstPage();
                     page != null;
                     page = c.getNextPage(pnum))
                {
                    pnum = page.getPageNumber();
                    if ((pnum % 2) == 0)
                    {
                        t_util.t_removePage(c, page);
                        removedPages++;
                    }
                    else
                        page.unlatch();
                }

                checkrows = 0;
                for (page = c.getFirstPage();
                     page != null;
                     page = c.getNextPage(pnum))
                {
                    pnum = page.getPageNumber();
                    if (page.recordCount() > 0)
                    {
                        t_util.t_checkFetchFirst(page, REC_001);
                        checkrows++;
                    }
                    page.unlatch();
                }
                if (checkrows != numrows - removedPages)
                    throw T_Fail.testFailMsg("number of rows differ");

                // remove every page backwards
                long lastpage = ContainerHandle.INVALID_PAGE_NUMBER;
                while((page = t_util.t_getLastPage(c)) != null)	// remove the last page
                {
                    if (lastpage == page.getPageNumber())
                        throw T_Fail.testFailMsg("got a removed last page");

                    lastpage = page.getPageNumber();
                    t_util.t_removePage(c, page);
                }

                if (c.getFirstPage() != null)
                    throw T_Fail.testFailMsg("get last page returns null but get fisrt page retuns a page");

                t.rollbackToSavePoint(SP1, null);	// roll back removes
                c = t_util.t_openContainer(t, 0, cid, true);

                checkrows = 0;
                for (page = c.getFirstPage();
                     page != null;
                     page = c.getNextPage(pnum))
                {
                    pnum = page.getPageNumber();
                    if (page.recordCount() > 0)
                    {
                        t_util.t_checkFetchFirst(page, REC_001);
                        checkrows++;
                    }
                    page.unlatch();
                }
                if (checkrows != numrows)
                    throw T_Fail.testFailMsg(threadName + "number of rows differ expect " +
                                             numrows + " got " + checkrows);


                t_util.t_abort(t);	// abort the whole thing, no rows left
                c = t_util.t_openContainer(t, 0, cid, true);

                int countPages = 0;
                for (page = c.getFirstPage();
                     page != null;
                     page = c.getNextPage(pnum))
                {
                    countPages++;
                    pnum = page.getPageNumber();
                    if (page.nonDeletedRecordCount() > 0)
                    {
                        throw T_Fail.testFailMsg("failed to remove everything " +
                                                 page.nonDeletedRecordCount() + 
                                                 " rows left on page " + pnum);
                    }
                    page.unlatch();
                }			

                if (countPages < numrows)
                    throw T_Fail.testFailMsg("rollback of user transaction should not remove allocated pages");

                t_util.t_dropContainer(t, 0, cid); 

            }
            finally
            {
                SanityManager.DEBUG_CLEAR(AllocPage.TEST_MULTIPLE_ALLOC_PAGE);
                t_util.t_commit(t);
                t.close();
            }
            PASS("AllocTest3");
        }
	}

	protected void AllocTest4() throws StandardException, T_Fail
	{
		if (!SanityManager.DEBUG)
		{
			REPORT("allocTest3 cannot be run on an insane server");
			return;
		}
        else
        {

            SanityManager.DEBUG_SET(AllocPage.TEST_MULTIPLE_ALLOC_PAGE);
            Transaction t = t_util.t_startTransaction();

            try
            {
                ////////////////////////////////////////////////////////
                // first test preallocation large table
                ////////////////////////////////////////////////////////
                Properties tableProperties = new Properties();
                tableProperties.put(Property.PAGE_SIZE_PARAMETER, Integer.toString(1024));
                tableProperties.put(RawStoreFactory.CONTAINER_INITIAL_PAGES, Integer.toString(100));

                long cid1 = 
                    t.addContainer(
                        0, ContainerHandle.DEFAULT_ASSIGN_ID, 
                        ContainerHandle.MODE_DEFAULT, tableProperties, 0);

                if (cid1 < 0)
                    throw T_Fail.testFailMsg("addContainer");

                ContainerHandle c1 = t_util.t_openContainer(t, 0, cid1, true);

                Page p1 = c1.getFirstPage();
                if (p1.getPageNumber() != ContainerHandle.FIRST_PAGE_NUMBER)
                    throw T_Fail.testFailMsg("expect first page to have FIRST_PAGE_NUMBER");
                p1.unlatch();

                if (c1.getNextPage(ContainerHandle.FIRST_PAGE_NUMBER) != null)
                    throw T_Fail.testFailMsg("expect to have only 1 page allocated");

                t_util.t_commit(t);

                REPORT("AllocTest4 - create preallocated container " + cid1);

                ////////////////////////////////////////////////////////
                // next test special addpage interface
                ////////////////////////////////////////////////////////
                long cid2 = t_util.t_addContainer(t, 0, 1024, 0, 1, false);
                t_util.t_commit(t);

                ContainerHandle c2 = t_util.t_openContainer(t, 0, cid2, true);

                // add page for bulk load
                p1 = c2.addPage(ContainerHandle.ADD_PAGE_BULK);	
                long pnum1 = p1.getPageNumber();
                p1.unlatch();

                // since the interface does not guarentee that anything special will
                // actually happen, can't really test that. Just make sure that
                // everything else works
                Page p2 = c2.addPage();
                long pnum2 = p2.getPageNumber();
                p2.unlatch();

                Page p3 = c2.addPage(ContainerHandle.ADD_PAGE_BULK);	
                long pnum3 = p3.getPageNumber();
                p3.unlatch();

                Page p = c2.getFirstPage(); // this is the first page that came with the
                                       // container when it was created

                try
                {
                    long pnum0 = p.getPageNumber();
                    p.unlatch();
                    p = c2.getNextPage(pnum0);
                    if (p.getPageNumber() != pnum1)
                        throw T_Fail.testFailMsg("expected pagenum " + pnum1 + " got " + p.getPageNumber());
                    p.unlatch();
                    p = null;

                    p = c2.getNextPage(pnum1);
                    if (p.getPageNumber() != pnum2)
                        throw T_Fail.testFailMsg("expected pagenum " + pnum2 + " got " + p.getPageNumber());
                    p.unlatch();
                    p = null;

                    p = c2.getNextPage(pnum2);
                    if (p.getPageNumber() != pnum3)
                        throw T_Fail.testFailMsg("expected pagenum " + pnum3 + " got " + p.getPageNumber());
                    p.unlatch();
                    p = null;

                    p = c2.getNextPage(pnum3);
                    if (p != null)
                        throw T_Fail.testFailMsg("expected null page after " + pnum3 +
                                             " got " + p.getPageNumber());

                    // make sure rollback is unaffected
                    if (testRollback)
                    {
                        t_util.t_abort(t);
                        c2 = t_util.t_openContainer(t, 0, cid2, true);
                        p = t_util.t_getPage(c2, pnum0);
                        t_util.t_checkEmptyPage(p);
                        p.unlatch();
                        p = null;
                
                        p = t_util.t_getPage(c2, pnum1);
                        t_util.t_checkEmptyPage(p);
                        p.unlatch();
                        p = null;

                        p = t_util.t_getPage(c2, pnum2);
                        t_util.t_checkEmptyPage(p);
                        p.unlatch();
                        p = null;

                        p = t_util.t_getPage(c2, pnum3);
                        t_util.t_checkEmptyPage(p);
                        p.unlatch();
                        p = null;

                        p = t_util.t_getLastPage(c2);
                        if (p.getPageNumber() != pnum3)
                            throw T_Fail.testFailMsg("expect last page to be " + pnum3
                                                 + " got " + p.getPageNumber());
                        p.unlatch();
                        p = null;
                    }
                }
                finally
                {
                    if (p != null)
                        p.unlatch();
                    p = null;
                }
                REPORT("AllocTest4 - special addPage interface " + cid2);


                ////////////////////////////////////////////////////////
                // next test preallocate interface
                ////////////////////////////////////////////////////////			
                long cid3 = t_util.t_addContainer(t, 0, 1024);
                ContainerHandle c3 = t_util.t_openContainer(t, 0, cid3, true);

                // now preallocate 10 pages
                c3.preAllocate(10);

                p1 = c3.getFirstPage();
                if (p1.getPageNumber() != ContainerHandle.FIRST_PAGE_NUMBER)
                    throw T_Fail.testFailMsg("expect first page to have FIRST_PAGE_NUMBER");
                p1.unlatch();

                if (c3.getNextPage(ContainerHandle.FIRST_PAGE_NUMBER) != null)
                    throw T_Fail.testFailMsg("expect to have only 1 page allocated");

                REPORT("AllocTest4 - preallocate interface " + cid3);

                PASS("AllocTest4 ");

            }
            finally
            {
                SanityManager.DEBUG_CLEAR(AllocPage.TEST_MULTIPLE_ALLOC_PAGE);
                t_util.t_commit(t);
                t.close();
            }
        }
	}

	protected void AllocTest5() throws StandardException, T_Fail
	{
		// first create 10 1/2 filled pages with various degree of fillness
		Transaction t = t_util.t_startTransaction();

		try
		{
			long cid = t_util.t_addContainer(t, 0, 1024, 0, 90, false);
			ContainerHandle c = t_util.t_openContainer(t, 0, cid, true);
			Page p;

			// the number of rows that is expected to fit into one page
			// secret raw store calculation for 1 column rows
			int numRows = (1024-60)/(95+8);

			T_RawStoreRow rows[] = new T_RawStoreRow[numRows];

			for (int j = 0; j < numRows; j++)
				rows[j] = new T_RawStoreRow("row " + j);

			for (int i = 0; i < numRows; i++)
			{
				p = t_util.t_addPage(c);

				// validate allocation cache by getting the first page
				t_util.t_getPage(c, 1).unlatch();

				// insert different number of rows into these pages
				for (int j = 0; j <= i; j++)
				{
					if (t_util.t_insert(p, rows[j]) == null)
						throw T_Fail.testFailMsg("failed to insert " + (j+1) +
												 " rows into page " + p);
				}

				p.unlatch();
			}

			// page 1 has 0 row
			// page 2 has 1 row
			// page 3 has 2 rows
			// page 4 has 3 rows
			// page 5 has 4 rows
			// page 6 has 5 rows (filled)
			// page 7 has 6 rows (filled)
			// page 8 has 7 rows (filled)
			// page 9 has 8 rows (filled)
			// page 10 has 9 rows (filled)


			// these pages should be accounted for correctly because each
			// subsequent page has > 1/8 for all the records in the container

			// now go thru and use up all the space
			p =  c.getPageForInsert(0);
			if (p != null)
				throw T_Fail.testFailMsg("Expect last page to be full");

			// now go thru and use up all the space - since we skipped page 1
			// on the first loop, it won't know it is a 1/2 filled page.
			for (int i = 2; i < 6; i++)
			{
				p = c.getPageForInsert(ContainerHandle.GET_PAGE_UNFILLED);
				if (p == null)
					throw T_Fail.testFailMsg("Expect next unfilled page to be " + i);

				if (p.getPageNumber() != i)
					throw T_Fail.testFailMsg("Expect next unfilled page to be "
											 + i + ", it is " + p.getPageNumber());

				t_util.t_insert(p, rows[i]);
				p.unlatch();

				// we should keep getting the same page back until it is full
				while ((p = c.getPageForInsert(0)) != null)
				{
					if (p.getPageNumber() != i)
						throw T_Fail.testFailMsg("Don't expect page number to change from " +
												 i + " to " + p.getPageNumber());
					t_util.t_insert(p, rows[i]);
					p.unlatch();
				}

			}
		
			p = c.getPageForInsert(ContainerHandle.GET_PAGE_UNFILLED);
			if (p != null)
				throw T_Fail.testFailMsg("don't expect any more pages to be found");

		}
		finally
		{
			t_util.t_commit(t);
			t.close();
		}
		PASS("AllocTest5 ");

	}

	/*
	 * MT tests on  the same container
	 */
	protected void AllocMTest1(long cid) throws StandardException, T_Fail 
	{
        if (SanityManager.DEBUG)
        {
            SanityManager.DEBUG_SET(AllocPage.TEST_MULTIPLE_ALLOC_PAGE);

            // each thread will add N pages and remove N pages and still finds
            // its own pages.  Do that serveral times.
            int N = 20;

            RecordHandle rh[] = new RecordHandle[N];

            Transaction t = t_util.t_startTransaction();

            try
            {
                T_RawStoreRow row = new T_RawStoreRow(REC_002);
                ContainerHandle c;
                Page p;

                for (int iteration = 0; iteration < 5; iteration++)
                {
                    for (int i = 0; i < N; i++)
                    {
                        c = t_util.t_openContainer(t, 0, cid, true);

                        p = t_util.t_addPage(c);
                        rh[i] = t_util.t_insert(p, row);
                        p.unlatch();

                        t_util.t_commit(t);
                    }

                    for (int i = 0; i < N; i++)
                    {
                        c = t_util.t_openContainer(t, 0, cid, true);
                        t_util.t_checkFetch(c, rh[i], REC_002);

                        t.setSavePoint(SP1, null);

                        p = t_util.t_getPage(c, rh[i].getPageNumber());
                        t_util.t_removePage(c, p);

                        if ((iteration%3) == 1)
                        {
                            t.rollbackToSavePoint(SP1, null);
                        }

                        // sometimes commit sometimes abort
                        if (iteration % 2 == 0)
                            t_util.t_abort(t);
                        else
                            t_util.t_commit(t);
                    }

                    // if I aborted, remove them now
                    if ((iteration % 2) == 0 ||
                        (iteration % 3) == 1)
                    {
                        for (int i = 0; i < N; i++)
                        {
                            c = t_util.t_openContainer(t, 0, cid, true);
                            t_util.t_checkFetch(c, rh[i], REC_002);

                            p = t_util.t_getPage(c, rh[i].getPageNumber());
                            t_util.t_removePage(c, p);
                            t_util.t_commit(t);
                        }
                    }

                    // at any given time, there should be <= (N*numthread)+1 pages
                    int max = (N*getNumThreads())+1;

                    c = t_util.t_openContainer(t, 0, cid, false);
                    long pnum = 0;
                    int countPages = 0;

                    for (p = c.getFirstPage();
                         p != null;
                         p = c.getNextPage(pnum))
                    {
                        countPages++;
                        pnum = p.getPageNumber();
                        p.unlatch();
                        t_util.t_commit(t);	// release container lock

                        c = t_util.t_openContainer(t, 0, cid, false);
                    }

                    t_util.t_commit(t);	// release container lock

                    if (countPages > max)
                        throw T_Fail.testFailMsg("some pages may not be reused, expect " +
                                                 max + " got " + countPages);
                    else
                        REPORT("AllocMTest1 got " + countPages );
                }
                
            }
            finally
            {
                SanityManager.DEBUG_CLEAR(AllocPage.TEST_MULTIPLE_ALLOC_PAGE);
                t_util.t_commit(t);
                t.close();
            }

            PASS("AllocMTest1");
        }
        else
        {
			REPORT("AllocMTest1 cannot be run on an insane server");
			return;
        }
	}
    
    /**
     * Privileged lookup of the ContextService. Must be private so that user code
     * can't call this entry point.
     */
    private  static  ContextService    getContextService()
    {
        if ( System.getSecurityManager() == null )
        {
            return ContextService.getFactory();
        }
        else
        {
            return AccessController.doPrivileged
                (
                 new PrivilegedAction<ContextService>()
                 {
                     public ContextService run()
                     {
                         return ContextService.getFactory();
                     }
                 }
                 );
        }
    }

    /**
     * Privileged startup. Must be private so that user code
     * can't call this entry point.
     */
    private  static  Object createPersistentService( final String factoryInterface, final String serviceName, final Properties properties ) 
        throws StandardException
    {
        try {
            return AccessController.doPrivileged
                (
                 new PrivilegedExceptionAction<Object>()
                 {
                     public Object run()
                         throws StandardException
                     {
                         return Monitor.createPersistentService( factoryInterface, serviceName, properties );
                     }
                 }
                 );
        } catch (PrivilegedActionException pae)
        {
            throw StandardException.plainWrapException( pae );
        }
    }

}