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

📄 cattellimpl.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// $Id: CattellImpl.java,v 1.5 2003/11/23 14:55:26 per_nyfelt Exp $

import java.io.*;
import java.util.Random;
import org.ozoneDB.*;
import org.ozoneDB.util.OzoneDebugLevel;
import org.ozoneDB.tools.*;


/**
 * CattellImpl is the central class of the Cattell/OO1 benchmark. It holds all
 * PartImpl objects in an array and provides several methods to perform various
 * benchmarks on them. Because CattellImpl itself is a database object these
 * benchmarks methods run inside the server as transactions.
 *
 *
 * @author <a href="http://www.softwarebuero.de/">SMB</a>
 * @version $Revision: 1.5 $Date: 2003/11/23 14:55:26 $
 */
public class CattellImpl extends OzoneObject implements Cattell, OO1Benchmark, Externalizable {

    public final static long serialVersionUID = 2L;

    public final static byte perms = Database.Public;

    public transient BenchmarkProgressLog log = new BenchmarkProgressLog();

    private Part[] parts = new Part[0];

    private int size = 0;


    public static void main( String[] args ) {
        OO1BenchmarkApp.main( args );
    }


    public CattellImpl() {
    }


    public void setProgressLog( BenchmarkProgressLog blog ) {
        log = blog;
    }


    public ExternalDatabase openDatabase( String serverHost ) throws Exception {
        ExternalDatabase db;
        if (serverHost.equals( "local" )) {
            db = new LocalDatabase();
            String dbDir=System.getProperty("java.io.tmpdir") + "/tmpdb";
            if (!Install.dbExists(dbDir)) {
                log.logMessage( "DB not found, creating in " + dbDir + "..." );
                ((LocalDatabase)db).create( dbDir );                
            }
            ((LocalDatabase)db).open( dbDir, OzoneDebugLevel.INFO_STR );
            log.logMessage( "local DB databaseDir: " + dbDir );
        } else {
            RemoteDatabase rdb = new RemoteDatabase();
            log.logMessage( "server host: " + serverHost );
            rdb.open( serverHost, 3333 );

            db = rdb;

        //          db = new ClientCacheDatabase (rdb, true);
        }
        log.logMessage( "connected..." );

        db.reloadClasses();

        return db;
    }


    public void create( int countParts, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        //cleanup
        Cattell cattell = (Cattell)db.objectForName( objectName );
        if (cattell != null) {
            cattell.done();
            db.deleteObject( cattell );
        }

        long start = System.currentTimeMillis();
        cattell = (Cattell)db.createObject( CattellImpl.class.getName(), perms, objectName );
        cattell.addParts( countParts );
        log.logTime( System.currentTimeMillis() - start );
        db.close();
    }


    public void lookup( int count, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        Cattell cattell = (Cattell)db.objectForName( objectName );
        for (int i = 0; i < 10; i++) {
            long start = System.currentTimeMillis();
            cattell.lookup( count );
            log.logTime( System.currentTimeMillis() - start );
        }
        db.close();
    }


    public void traversal( int depth, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        Cattell cattell = (Cattell)db.objectForName( objectName );
        for (int i = 0; i < 10; i++) {
            long start = System.currentTimeMillis();
            cattell.traversal( depth );
            log.logTime( System.currentTimeMillis() - start );
        }
        db.close();
    }


    public void insertAndDelete( int countParts, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        Cattell cattell = (Cattell)db.objectForName( objectName );
        for (int i = 0; i < 10; i++) {
//            ExternalTransaction tx = db.newTransaction();
//            tx.begin();

            long start = System.currentTimeMillis();
            cattell.insert( countParts );
            cattell.delete( countParts );
            log.logTime( System.currentTimeMillis() - start );

//            tx.commit();
        }
        db.close();
    }


    public void insert( int countParts, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        Cattell cattell = (Cattell)db.objectForName( objectName );
        long start = System.currentTimeMillis();
        cattell.insert( countParts );
        log.logTime( System.currentTimeMillis() - start );
        db.close();
    }


    public void delete( int countParts, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        Cattell cattell = (Cattell)db.objectForName( objectName );
        long start = System.currentTimeMillis();
        cattell.delete( countParts );
        log.logTime( System.currentTimeMillis() - start );
        db.close();
    }


    public void createObjects( int count, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        Cattell cattell;
        // clean up
        for (int i = 0; i < count; i++) {
            cattell = (Cattell)db.objectForName( objectName + String.valueOf( i ) );
            if (cattell != null) {
                cattell.done();
                db.deleteObject( cattell );
            }
        }
        for (int i = 0; i < count; i++) {
            log.logMessage( "name: " + objectName + String.valueOf( i ) );
            long start = System.currentTimeMillis();
            cattell = (Cattell)db.createObject( CattellImpl.class.getName(),
                    OzoneInterface.AllRead | OzoneInterface.AllLock, objectName + String.valueOf( i ) );
            cattell.addParts( 1000 );
            log.logTime( System.currentTimeMillis() - start );
        }
        db.close();
    }


    public void traversalObjects( int count, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        Random rand = new Random();
        //changed to only loop "count" number of times instead of infinite
        for (int loop=0; loop < count; loop++) {
            int i = (int)(rand.nextDouble() * count);
            log.logMessage( "name: " + objectName + String.valueOf( i ) );
            for (int j = 1; j < 5; j++) {
                try {
                    Cattell cattell = (Cattell)db.objectForName( objectName + String.valueOf( i ) );
                    if (cattell != null) {
                        long start = System.currentTimeMillis();
                        cattell.traversal( 8 );
                        log.logTime( System.currentTimeMillis() - start );
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        db.close();
    }


    public void insertObjects( int count, String objectName, String serverHost ) throws Exception {
        ExternalDatabase db = openDatabase( serverHost );
        Random rand = new Random();
        //changed to only loop "count" number of times instead of infinite
        for (int loop=0; loop < count; loop++) {
            int i = (int)(rand.nextDouble() * count);
            log.logMessage( "name: " + objectName + String.valueOf( i ) );
            ExternalTransaction tx = db.newTransaction();
            tx.begin();
            try {
                Cattell cattell = (Cattell)db.objectForName( objectName + String.valueOf( i ) );
                if (cattell != null) {
                    long start = System.currentTimeMillis();
                    cattell.insert( 100 );
                    cattell.delete( 100 );
                    log.logTime( System.currentTimeMillis() - start );
                }
                tx.commit();
            }
            catch (Exception e) {
                tx.rollback();
                throw e;
            }
        }
        db.close();
    }


    public void addParts( int n ) throws Exception {
        int oldSize = size;
        size += n;

        System.out.println( "adding " + n + " to " + oldSize + " old parts..." );

        System.out.println( "copying the array..." );
        Part[] newArray = new Part[size];
        System.arraycopy( parts, 0, newArray, 0, oldSize );
        parts = newArray;

        System.out.println( "creating parts..." );
        OzoneInterface db = database();
        for (int i = oldSize; i < size; i++) {
            if (i % 200 == 0) {
                System.out.println( "" + i );
            }
            parts[i] = (Part)db.createObject( PartImpl.class.getName(), perms, null );
        }
        connectParts( n );
    }


    /**
     * stellt die assoziationen zwischen den letzten num parts her
     */
    public void connectParts( int n ) throws Exception {
        System.out.println( "connecting the last " + n + " of " + size + " parts..." );

        Random rand = new Random();
        for (int i = size - n; i < size; i++) {
            if (i % 200 == 0) {
                System.out.println( "" + i );
            }
            for (int j = 0; j < 3; j++) {
                int cpart;
                if (rand.nextDouble() > 0.1) {
                    cpart = i + (int) (2 * rand.nextDouble() - 1) * size / 200;
                    if (cpart < size / 200F) {
                        cpart += (int) size / 200F;
                    }
                    if (cpart > (size - size / 200F)) {
                        cpart -= (int) size / 200F;
                    }
                } else {
                    cpart = (int) (rand.nextDouble() * size);
                }
                // System.out.println ("connecting " + i + ":" + cpart);
                parts[i].connect( j, parts[cpart] );
            }
        }
    }


    public void lookup( int n ) throws Exception {
        System.out.println( "" + n + " lookups on " + size + " objects..." );
        Random rand = new Random();
        int start = (int)(rand.nextDouble() * (size - n));
        System.out.println( "start: " + start );
        for (int i = 0; i < n; i++) {
            // parts[start+i].set (new DxString("part"), 1, 1);
            parts[start + i].get();
        }
        System.out.println( "lookup: ready" );
    }


    public void traversal( int n ) throws Exception {
        System.out.println( "traversal (" + n + " hops)..." );
        Random rand = new Random();
        int j = (int)(rand.nextDouble() * size);
        parts[j].traversal( 2, 2, n );
    }


    public void insert( int n ) throws Exception {
        int oldSize = size;
        System.out.println( "inserting " + n + " parts..." );
        addParts( n );
        System.out.println( "invoke..." );
        for (int i = oldSize; i < size; i++) {
            parts[i].set( "part", 1, 1 );
        }
    }


    public void delete( int n ) throws Exception {
        OzoneInterface db = database();
        n = n != -1 ? n : size;

        System.out.println( "delete: waiting..." );
        System.out.println( "delete: go on..." );

        System.out.println( "deconnecting " + n + " of " + size + " parts..." );
        for (int i = size - n; i < size; i++) {
            parts[i].deconnect();
        }

        System.out.println( "deleting " + n + " of " + size + " parts..." );
        for (int i = size - n; i < size; i++) {
            db.deleteObject( parts[i] );
        }

        size -= n;
        Part[] newArray = new Part[size];
        System.arraycopy( parts, 0, newArray, 0, size );
        parts = newArray;
        System.out.println( "delete: ready" );
    }


    public void writeExternal( ObjectOutput out ) throws IOException {
        // System.out.println ("CattelImpl.writeExternal " + size + " ...");
        out.writeInt( size );
        for (int i = 0; i < size; i++) {
            out.writeObject( parts[i] );
        }
    }


    public synchronized void readExternal( ObjectInput in ) throws IOException, ClassNotFoundException {
        size = in.readInt();
        // System.out.println ("Cattell read " + size + " ...");
        parts = new Part[size];
        for (int i = 0; i < size; i++) {
            parts[i] = (Part)in.readObject();
        }
    }


    public void done() throws Exception {
        delete( -1 );
    }

}

⌨️ 快捷键说明

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