Package org.jboss.profiler.memoryprofiler.engine

Source Code of org.jboss.profiler.memoryprofiler.engine.MemorySnapshoDAO

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This 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 software 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 software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.jboss.profiler.memoryprofiler.engine;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;

import org.jboss.profiler.filecollection.FileCollection;
import org.jboss.profiler.filecollection.QuickSorter;
import org.jboss.profiler.memoryprofiler.model.MemoryClass;
import org.jboss.profiler.memoryprofiler.model.MemoryObject;
import org.jboss.profiler.memoryprofiler.model.MemoryReference;
import org.jboss.profiler.memoryprofiler.model.ReferenceContainer;


/**
* Controls database selections.
* @author Clebert Suconic
* */
public class MemorySnapshoDAO
{

    Hashtable cacheClasses = new Hashtable();
    Collection rootView = null;

    MemorySnapshotEngine engine;
    MemoryObject objectKey = new MemoryObject();

    MemorySnapshoDAO (MemorySnapshotEngine engine)
    {
        this.engine=engine;
    }

    /** a ReferenceContainer for the Object passed as parameter */
    public ReferenceContainer selectReferences(MemoryObject object, boolean forward) throws Exception
    {
        MemoryReference key = new MemoryReference();
        QuickSorter sorter = null;
        FileCollection collection = null;
        if (forward)
        {
            key.setReferee(object.getId());
            key.setReferred(-1);

            collection = engine.references;
            sorter = engine.sorter;
        } else
        {
            key.setReferee(-1);
            key.setReferred(object.getId());

            collection = engine.invertedReferences;
            sorter = engine.invertedSorter;
        }

        ReferenceContainer container = new ReferenceContainer(object);

        ArrayList referencesFound = sorter.binarySearchCollection(collection,key);

        Iterator iter = referencesFound.iterator();

        while (iter.hasNext())
        {
            MemoryReference referenceInstance = (MemoryReference)iter.next();
            MemoryObject memoryObject = null;
            if (forward)
            {
                memoryObject = this.selectObject(referenceInstance.getReferred());
            } else
            {
                memoryObject = this.selectObject(referenceInstance.getReferee());
            }
            referenceInstance.setObjectReferred(memoryObject);
        }

        container.setListOfReferences(referencesFound);

        return container;
    }

    /** Return a list of MemoryClasses */
    public Collection selectRoots(long classLoaderId) throws Exception
    {
        Collection result = new ArrayList();
        Iterator classes = selectRoots().iterator();

        while (classes.hasNext())
        {
            MemoryClass clazz = (MemoryClass)classes.next();
            if (clazz.getClassLoaderId()==classLoaderId)
            {
                result.add(clazz);
            }
        }
        return result;

    }

    /** Return a list of MemoryClasses */
    public Collection selectRoots() throws Exception
    {
        return engine.classes.values();
    }

    public Collection selectObjects(MemoryClass clazz) throws Exception
    {
        ArrayList arrayList = new ArrayList();

        Iterator iter = engine.objects.iterator();
        while (iter.hasNext())
        {
            MemoryObject object = (MemoryObject)iter.next();
            if (object.getClassId() == clazz.getId())
            {
                object.setMemoryClass(clazz);
                arrayList.add(object);
            }
        }

        return arrayList;
    }

    public MemoryObject selectObject (long objectId) throws Exception
    {
        objectKey.setId(objectId);

        MemoryObject returnObject = (MemoryObject)engine.objectsSorter.binarySearchItem(engine.objects,objectKey);
        if (returnObject==null)
        {
            returnObject=selectClass(objectId);
        }

        if (returnObject!=null)
        {
            returnObject.setMemoryClass(selectClass(returnObject.getClassId()));
        }

        return returnObject;
    }

    private MemoryObject createObject(ResultSet rset) throws SQLException, Exception
    {
        MemoryObject memObj = new MemoryObject();
        memObj.setId(rset.getLong(1));
        memObj.setSize(rset.getLong(3));
        memObj.setClassId(rset.getLong(2));
        memObj.setMemoryClass(selectClass(memObj.getClassId()));
        return memObj;
    }


    public MemoryClass selectClass (long classId) throws Exception
    {
        return engine.findClass(classId);
    }



    private MemoryClass createMemoryClass(ResultSet set, boolean containCounters) throws SQLException
    {
        Long longId = new Long(set.getLong(1));

        MemoryClass clazz = (MemoryClass)cacheClasses.get(longId);

        if (clazz!=null)
        {
            return clazz;
        } else
        {
            clazz = new MemoryClass();

            clazz.setId(set.getLong(1));
            clazz.setSignature(set.getString(2));
            clazz.setClassLoaderId(set.getLong(3));

            if (containCounters)
            {
                clazz.setNumberOfInstances(set.getInt(4));
                clazz.setLoadedSize(set.getLong(5));
            }


            cacheClasses.put(new Long(clazz.getId()),clazz);

            return clazz;
        }
    }
}
TOP

Related Classes of org.jboss.profiler.memoryprofiler.engine.MemorySnapshoDAO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.