⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testdiskhashtable.java

📁 derby database source code.good for you.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*

   Derby - Class org.apache.derbyTesting.functionTests.tests.store.TestDiskHashtable

   Copyright 2005 The Apache Software Foundation or its licensors, as applicable.

   Licensed 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.functionTests.tests.store;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.BitSet;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Vector;

import org.apache.derby.iapi.error.PublicAPI;
import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.sql.conn.ConnectionUtil;
import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
import org.apache.derby.iapi.store.access.DiskHashtable;
import org.apache.derby.iapi.store.access.KeyHasher;
import org.apache.derby.iapi.store.access.TransactionController;
import org.apache.derby.iapi.types.DataValueDescriptor;
import org.apache.derby.iapi.types.Orderable;
import org.apache.derby.iapi.types.SQLInteger;
import org.apache.derby.iapi.types.SQLLongint;
import org.apache.derby.iapi.types.SQLVarchar;
import org.apache.derby.tools.ij;
import org.apache.derbyTesting.functionTests.util.TestUtil;

/**
 * This program tests the org.apache.derby.iapi.store.access.DiskHashtable class.
 * The unit test interface is not used because that is undocumented and very difficult to decipher.
 * Furthermore it is difficult to diagnose problems when using the unit test interface.
 *
 * Created: Wed Feb 09 15:44:12 2005
 *
 * @author <a href="mailto:klebanof@us.ibm.com">Jack Klebanoff</a>
 * @version 1.0
 */
public class TestDiskHashtable 
{
    private TransactionController tc;
    private int failed = 0;
    
    public static void main( String args[])
    {
        int failed = 1;

		REPORT("Test DiskHashtable starting");
        try
        {
			// use the ij utility to read the property file and
			// make the initial connection.
			ij.getPropertyArg(args);
			Connection conn = ij.startJBMS();
            Statement stmt = conn.createStatement();
            stmt.execute("CREATE FUNCTION testDiskHashtable() returns INTEGER EXTERNAL NAME 'org.apache.derbyTesting.functionTests.tests.store.TestDiskHashtable.runTests' LANGUAGE JAVA PARAMETER STYLE JAVA");
            ResultSet rs = stmt.executeQuery( "values( testDiskHashtable())");
            if( rs.next())
                failed = rs.getInt(1);
            stmt.close();
            conn.close();
        }
        catch( SQLException e)
        {
			TestUtil.dumpSQLExceptions( e);
            failed = 1;
        }
        catch( Throwable t)
        {
			REPORT("FAIL -- unexpected exception:" + t.toString());
            failed = 1;
		}
        REPORT( (failed == 0) ? "OK" : "FAILED");
        System.exit( (failed == 0) ? 0 : 1);
    }

    private void REPORT_FAILURE(String msg)
    {
        failed = 1;
        REPORT( msg);
    }
    
    private static void REPORT(String msg)
    {
        System.out.println( msg);
    }
    
    public static int runTests() throws SQLException
    {
        TestDiskHashtable tester = new TestDiskHashtable();
        return tester.doIt();
    }

    private TestDiskHashtable() throws SQLException
    {
        LanguageConnectionContext lcc = ConnectionUtil.getCurrentLCC();
        if( lcc == null)
            throw new SQLException( "Cannot get the LCC");
        tc = lcc.getTransactionExecute();
    }

    private int doIt() throws SQLException
    {
		try {


            REPORT( "Starting single key, keep duplicates test");
            testOneVariant( tc, false, singleKeyTemplate, singleKeyCols, singleKeyRows);
            REPORT( "Starting single key, remove duplicates test");
            testOneVariant( tc, true, singleKeyTemplate, singleKeyCols, singleKeyRows);
            REPORT( "Starting multiple key, keep duplicates test");
            testOneVariant( tc, false, multiKeyTemplate, multiKeyCols, multiKeyRows);
            REPORT( "Starting multiple key, remove duplicates test");
            testOneVariant( tc, true, multiKeyTemplate, multiKeyCols, multiKeyRows);

			tc.commit();
		}
		catch (StandardException se)
		{
            throw PublicAPI.wrapStandardException( se);
        }
        return failed;
    } // end of doIt

    private static final DataValueDescriptor[] singleKeyTemplate = { new SQLInteger(), new SQLVarchar()};
    private static final int[] singleKeyCols = {0};
    private static final DataValueDescriptor[][] singleKeyRows =
    {
        {new SQLInteger(1), new SQLVarchar("abcd")},
        {new SQLInteger(2), new SQLVarchar("abcd")},
        {new SQLInteger(3), new SQLVarchar("e")},
        {new SQLInteger(1), new SQLVarchar("zz")}
    };

    private static final DataValueDescriptor[] multiKeyTemplate = { new SQLLongint(), new SQLVarchar(), new SQLInteger()};
    private static final int[] multiKeyCols = {1, 0};
    private static final DataValueDescriptor[][] multiKeyRows =
    {
        {new SQLLongint(1), new SQLVarchar( "aa"), multiKeyTemplate[2].getNewNull()},
        {new SQLLongint(2), new SQLVarchar( "aa"), new SQLInteger(1)},
        {new SQLLongint(2), new SQLVarchar( "aa"), new SQLInteger(2)},
        {new SQLLongint(2), new SQLVarchar( "b"), new SQLInteger(1)}
    };

    private static final int LOTS_OF_ROWS_COUNT = 50000;
    
    private void testOneVariant( TransactionController tc,
                                 boolean removeDups,
                                 DataValueDescriptor[] template,
                                 int[] keyCols,
                                 DataValueDescriptor[][] rows)
        throws StandardException
    {
        DiskHashtable dht = new DiskHashtable(tc, template, keyCols, removeDups, false);
        boolean[] isDuplicate = new boolean[ rows.length];
        boolean[] found = new boolean[ rows.length];
        HashMap simpleHash = new HashMap( rows.length);

        testElements( removeDups, dht, keyCols, 0, rows, simpleHash, isDuplicate, found);

        for( int i = 0; i < rows.length; i++)
        {
            Object key = KeyHasher.buildHashKey( rows[i], keyCols);
            Vector al = (Vector) simpleHash.get( key);
            isDuplicate[i] = (al != null);
            if( al == null)
            {
                al = new Vector(4);
                simpleHash.put( key, al);
            }
            if( (!removeDups) || !isDuplicate[i])
                al.add( rows[i]);
            
            if( dht.put( key, rows[i]) != (removeDups ? (!isDuplicate[i]) : true))
                REPORT_FAILURE( "  put returned wrong value on row " + i);

            for( int j = 0; j <= i; j++)
            {
                key = KeyHasher.buildHashKey( rows[j], keyCols);
                if( ! rowsEqual( dht.get( key), simpleHash.get( key)))
                    REPORT_FAILURE( "  get returned wrong value on key " + j);
            }

            testElements( removeDups, dht, keyCols, i+1, rows, simpleHash, isDuplicate, found);
        }
        // Remove them
        for( int i = 0; i < rows.length; i++)
        {
            Object key = KeyHasher.buildHashKey( rows[i], keyCols);
            if( ! rowsEqual( dht.remove( key), simpleHash.get( key)))
                REPORT_FAILURE( "  remove returned wrong value on key " + i);
            simpleHash.remove( key);
            if( dht.get( key) != null)
                REPORT_FAILURE( "  remove did not delete key " + i);
        }
        testElements( removeDups, dht, keyCols, 0, rows, simpleHash, isDuplicate, found);

        testLargeTable( dht, keyCols, rows[0]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -