📄 t_sortcontroller.java
字号:
REPORT("(sortAllDuplicates) no aggregation"); expected = new T_AccessRow[5]; expected[0] = new T_AccessRow(1, 1, 1); expected[1] = new T_AccessRow(1, 1, 1); expected[2] = new T_AccessRow(1, 1, 1); expected[3] = new T_AccessRow(1, 1, 1); expected[4] = new T_AccessRow(1, 1, 1); sortid = tc.createSort(implParameters, template.getRowArray(), order, new T_DummySortObserver(template), alreadyInOrder, estimatedRows, estimatedRowSize); if (!testSort(tc, input, expected, sortid)) failcount++; /* ** If we're doing duplicate elimination, we expect ** one row back (since they're all duplicates). */ REPORT("(sortAllDuplicates) eliminate duplicates"); expected = new T_AccessRow[1]; expected[0] = new T_AccessRow(1, 1, 1); sortid = tc.createSort(implParameters, template.getRowArray(), order, new T_DuplicateEliminator(template), alreadyInOrder, estimatedRows, estimatedRowSize); if (!testSort(tc, input, expected, sortid)) failcount++; /* ** Another aggregation, this time summing up the ** third column. */ REPORT("(sortAllDuplicates) sum aggregate"); expected = new T_AccessRow[1]; expected[0] = new T_AccessRow(1, 1, 5); sortid = tc.createSort(implParameters, template.getRowArray(), order, new T_SumForIntCol(2), alreadyInOrder, estimatedRows, estimatedRowSize); if (!testSort(tc, input, expected, sortid)) failcount++; return failcount == 0; } /** Test a sort where we have some ascending and descending keys. **/ boolean sortDescending(TransactionController tc) throws StandardException { int failcount = 0; long sortid; Properties implParameters; T_AccessRow template; ColumnOrdering order[]; boolean alreadyInOrder; long estimatedRows; int estimatedRowSize; T_AccessRow expected[]; /* ** The following sort parameters will be used in every ** sort in this method. */ implParameters = null; template = new T_AccessRow(1, 1, 1); alreadyInOrder = false; estimatedRows = 10; estimatedRowSize = 12; /* ** Straight descending sort. */ REPORT("(sortDescending) no aggregation"); order = new ColumnOrdering[2]; order[0] = new T_ColumnOrderingImpl(0, false); // descending order[1] = new T_ColumnOrderingImpl(1, false); // descending expected = new T_AccessRow[10]; expected[0] = new T_AccessRow(8, 1, 1); expected[1] = new T_AccessRow(4, 8, 1); expected[2] = new T_AccessRow(4, 2, 1); expected[3] = new T_AccessRow(4, 1, 1); expected[4] = new T_AccessRow(3, 8, 1); expected[5] = new T_AccessRow(3, 5, 1); expected[6] = new T_AccessRow(3, 3, 1); expected[7] = new T_AccessRow(3, 3, 1); expected[8] = new T_AccessRow(3, 3, 1); expected[9] = new T_AccessRow(1, 1, 1); sortid = tc.createSort(implParameters, template.getRowArray(), order, new T_DummySortObserver(template), alreadyInOrder, estimatedRows, estimatedRowSize); if (!testSort(tc, getSortDescendingInput(), expected, sortid)) failcount++; /* ** Descending sort eliminating duplicates */ REPORT("(sortDescending) eliminate duplicates"); order = new ColumnOrdering[2]; order[0] = new T_ColumnOrderingImpl(0, false); // descending order[1] = new T_ColumnOrderingImpl(1, false); // descending expected = new T_AccessRow[8]; expected[0] = new T_AccessRow(8, 1, 1); expected[1] = new T_AccessRow(4, 8, 1); expected[2] = new T_AccessRow(4, 2, 1); expected[3] = new T_AccessRow(4, 1, 1); expected[4] = new T_AccessRow(3, 8, 1); expected[5] = new T_AccessRow(3, 5, 1); expected[6] = new T_AccessRow(3, 3, 1); expected[7] = new T_AccessRow(1, 1, 1); sortid = tc.createSort(implParameters, template.getRowArray(), order, new T_DuplicateEliminator(template), alreadyInOrder, estimatedRows, estimatedRowSize); if (!testSort(tc, getSortDescendingInput(), expected, sortid)) failcount++; /* ** Eliminate duplicates, ascending on second column. */ REPORT("(sortDescending) descending/ascending - eliminate duplicates"); order = new ColumnOrdering[2]; order[0] = new T_ColumnOrderingImpl(0, false); // descending order[1] = new T_ColumnOrderingImpl(1, true); // ascending expected = new T_AccessRow[8]; expected[0] = new T_AccessRow(8, 1, 1); expected[1] = new T_AccessRow(4, 1, 1); expected[2] = new T_AccessRow(4, 2, 1); expected[3] = new T_AccessRow(4, 8, 1); expected[4] = new T_AccessRow(3, 3, 1); expected[5] = new T_AccessRow(3, 5, 1); expected[6] = new T_AccessRow(3, 8, 1); expected[7] = new T_AccessRow(1, 1, 1); sortid = tc.createSort(implParameters, template.getRowArray(), order, new T_DuplicateEliminator(template), alreadyInOrder, estimatedRows, estimatedRowSize); if (!testSort(tc, getSortDescendingInput(), expected, sortid)) failcount++; return failcount == 0; } private T_AccessRow[] getSortDescendingInput() { T_AccessRow[] input; input = new T_AccessRow[10]; input[0] = new T_AccessRow(8, 1, 1); input[1] = new T_AccessRow(1, 1, 1); input[2] = new T_AccessRow(3, 5, 1); input[3] = new T_AccessRow(4, 1, 1); input[4] = new T_AccessRow(3, 3, 1); input[5] = new T_AccessRow(3, 8, 1); input[6] = new T_AccessRow(3, 3, 1); input[7] = new T_AccessRow(3, 3, 1); input[8] = new T_AccessRow(4, 2, 1); input[9] = new T_AccessRow(4, 8, 1); return input; } /** Insert the given rows into the given sort, and check that the rows retrieved from the sort match the output rows. **/ boolean testSort(TransactionController tc, T_AccessRow in[], T_AccessRow outrow[], long sortid) throws StandardException { // Open a sort controller for inserting the rows. SortController sort = tc.openSort(sortid); // Insert the rows to be sorted. for (int i = 0; i < in.length; i++) { if (verbose) REPORT("(testSort) in: " + in[i]); sort.insert(in[i].getRowArray()); } // Close the sort controller. This makes the rows // available to be retrieved. // It also means we are getting final sort statistics. sort.close(); // Test the SortInfo part of sort. SortInfo sort_info = sort.getSortInfo(); Properties sortprop = sort_info.getAllSortInfo(null); String sortType = sortprop.getProperty( MessageService.getTextMessage(SQLState.STORE_RTS_SORT_TYPE)); int numRowsInput = Integer.parseInt(sortprop.getProperty( MessageService.getTextMessage(SQLState.STORE_RTS_NUM_ROWS_INPUT))); int numRowsOutput = Integer.parseInt(sortprop.getProperty( MessageService.getTextMessage(SQLState.STORE_RTS_NUM_ROWS_OUTPUT))); String external = MessageService.getTextMessage(SQLState.STORE_RTS_EXTERNAL); String internal = MessageService.getTextMessage(SQLState.STORE_RTS_INTERNAL); if (sortType.compareTo(internal) != 0 && sortType.compareTo(external) != 0) FAIL("(testSort) unknown sortType. Expected internal or external, got " + sortType); if (numRowsInput != in.length) FAIL("(testSort) SortInfo.numRowsInput (value: " + numRowsInput + ") is not equal to in.length (value: " + in.length + ")"); if (numRowsOutput != outrow.length) FAIL("(testSort) SortInfo.numRowsOutput (value: " + numRowsOutput + ") is not equal to outrow.length (value: " + outrow.length + ")"); if (sortType.equals(external)) { int numMergeRuns = Integer.parseInt(sortprop.getProperty( MessageService.getTextMessage(SQLState.STORE_RTS_NUM_MERGE_RUNS))); Vector mergeRuns = new Vector(); StringTokenizer st = new StringTokenizer(sortprop.getProperty( MessageService.getTextMessage(SQLState.STORE_RTS_MERGE_RUNS_SIZE)), "[],",false); while (st.hasMoreTokens()) mergeRuns.addElement(Integer.valueOf(st.nextToken().trim())); if (mergeRuns.size() != numMergeRuns) FAIL("(testSort) the number of elements in vector SortInfo.mergeRunsSize (value: " + mergeRuns.size() + " ) is not equal to SortInfo.numMergeRuns (value: " + numMergeRuns + " )"); int totRunSize = 0; for (int i = 0; i < mergeRuns.size(); i++) totRunSize += ((Integer) mergeRuns.elementAt(i)).intValue(); if (totRunSize != numRowsInput) FAIL("(testSort) the sum of the elements of the vector SortInfo.mergeRunsSize (value: " + totRunSize + " ) is not equal to SortInfo.numRowsInput (value: " + numRowsInput + " )"); } sort = null; // Open a sort scan for reading the rows back. ScanController scan = tc.openSortScan(sortid, false); // Things that could go wrong. boolean mismatch = false; boolean toofew = false; boolean toomany = false; // Fetch the sorted rows and compare them to the rows // in the outrow array. T_AccessRow result = new T_AccessRow(3); for (int i = 0; i < outrow.length; i++) { if (scan.next() == false) { // We were expecting the i'th row from outrow, but // it didn't show up! toofew = true; FAIL("(testSort) Too few rows in sort output"); break; } scan.fetch(result.getRowArray()); if (verbose) REPORT("(testSort) out: " + result); if (!result.equals(outrow[i])) { // The i'th row from the sort didn't match the // i'th row from out. mismatch = true; FAIL("(testSort) row " + result + " != " + outrow[i]); } } // We should not see any more rows out of the sort, // since we've exhausted the out array. while (scan.next() == true) { scan.fetch(result.getRowArray()); if (verbose) REPORT("(testSort) out: " + result); toomany = true; FAIL("(testSort) Extra row"); } // Test the ScanInfo part of sort. ScanInfo scan_info = scan.getScanInfo(); Properties prop = scan_info.getAllScanInfo(null); if (prop.getProperty( MessageService.getTextMessage(SQLState.STORE_RTS_SCAN_TYPE) ).compareTo( MessageService.getTextMessage(SQLState.STORE_RTS_SORT)) != 0) { FAIL("(testSort) wrong scanType. Expected sort, got " + prop.getProperty( MessageService.getTextMessage( SQLState.STORE_RTS_SCAN_TYPE))); } if (tc.countOpens(TransactionController.OPEN_CREATED_SORTS) != 1) { FAIL("(testSort) sort count before close is wrong: " + tc.countOpens(TransactionController.OPEN_CREATED_SORTS)); } // Close the scan controller (which implicitly destroys the sort). scan.close(); scan = null; if (tc.countOpens(TransactionController.OPEN_CREATED_SORTS) != 1) { FAIL("(testSort) sort count after close is wrong: " + tc.countOpens(TransactionController.OPEN_CREATED_SORTS)); } tc.dropSort(sortid); if (tc.countOpens(TransactionController.OPEN_CREATED_SORTS) > 0) { FAIL("(testSort) a sort is still open."); } return (!mismatch && !toofew && !toomany); }}class T_DummySortObserver implements SortObserver{ T_AccessRow template; Vector vector; T_DummySortObserver(T_AccessRow template) { this.template = template; vector = new Vector(); } /* * Methods of SortObserver */ public DataValueDescriptor[] insertNonDuplicateKey( DataValueDescriptor[] insertRow) { return insertRow; } public DataValueDescriptor[] insertDuplicateKey( DataValueDescriptor[] insertRow, DataValueDescriptor[] existingRow) { return insertRow; } public void addToFreeList( DataValueDescriptor[] objectArray, int maxFreeListSize) { if (vector.size() < maxFreeListSize) { vector.addElement(objectArray); } } public DataValueDescriptor[] getArrayClone() throws StandardException { int lastElement = vector.size(); if (lastElement > 0) { DataValueDescriptor[] retval = (DataValueDescriptor[]) vector.elementAt(lastElement - 1); vector.removeElementAt(lastElement - 1); return retval; } return template.getRowArrayClone(); }}class T_DuplicateEliminator extends T_DummySortObserver{ T_DuplicateEliminator(T_AccessRow template) { super(template); } /* * Methods of SortObserver */ public DataValueDescriptor[] insertNonDuplicateKey( DataValueDescriptor[] insertRow) { return insertRow; } public DataValueDescriptor[] insertDuplicateKey( DataValueDescriptor[] insertRow, DataValueDescriptor[] existingRow) { return null; }}class T_SumForIntCol implements SortObserver{ private int columnId; T_SumForIntCol(int columnId) { this.columnId = columnId; } /* * Methods of SortObserver */ public DataValueDescriptor[] insertNonDuplicateKey( DataValueDescriptor[] insertRow) { return insertRow; } public DataValueDescriptor[] insertDuplicateKey( DataValueDescriptor[] insertRow, DataValueDescriptor[] existingRow) throws StandardException { // We know, because this is a test program and it's only // used this way, that we can safely cast the arguments // to SQLInteger. SQLInteger increment = (SQLInteger) insertRow[columnId]; SQLInteger sum = (SQLInteger) existingRow[columnId]; // Perform the aggregation. sum.plus(sum, increment, sum); return null; } public void addToFreeList( DataValueDescriptor[] objectArray, int maxFreeListSize) { } public DataValueDescriptor[] getArrayClone() throws StandardException { return null; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -