<commit_before>/*
 * Copyright (C) 2013      <NAME> <EMAIL>
 * Copyright (C) 2010-2013 <NAME> <EMAIL>
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jkiss.dbeaver.ext.db2.model;

import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.db2.DB2Constants;
import org.jkiss.dbeaver.ext.db2.DB2Utils;
import org.jkiss.dbeaver.ext.db2.model.cache.DB2TablespaceContainerCache;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2OwnerType;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2TablespaceDataType;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2TablespaceType;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2YesNo;
import org.jkiss.dbeaver.model.DBPNamedObject;
import org.jkiss.dbeaver.model.DBPRefreshableObject;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.meta.Association;
import org.jkiss.dbeaver.model.meta.Property;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.runtime.VoidProgressMonitor;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Collection;

/**
 * DB2 Tablespace
 * 
 * @author <NAME>
 */
public class DB2Tablespace extends DB2GlobalObject implements DBPNamedObject, DBPRefreshableObject {

    private final DB2TablespaceContainerCache containerCache = new DB2TablespaceContainerCache();

    private String name;
    private String owner;
    private DB2OwnerType ownerType;
    private Timestamp createTime;
    private Integer tbspaceId;
    private DB2TablespaceType tbspaceType;
    private DB2TablespaceDataType dataType;
    private Integer extentSize;
    private Integer prefetchSize;
    private Double overHead;
    private Double transferRate;
    private Double writeOverHead;
    private Double writeTransferRate;
    private Integer pageSize;
    private String dbpgName;
    private Boolean dropRecovery;
    private Integer dataTag;
    private DB2StorageGroup storageGroup;
    private Integer effectivePrefetchSize;
    private String remarks;

    private DB2Bufferpool bufferpool;

    // -----------------------
    // Constructors
    // -----------------------

    public DB2Tablespace(DB2DataSource db2DataSource, String db2TablespaceName) throws DBException
    {
        super(db2DataSource, false);
        this.name = db2TablespaceName;
    }

    public DB2Tablespace(DB2DataSource db2DataSource, ResultSet dbResult) throws DBException
    {
        super(db2DataSource, dbResult != null);
        this.name = JDBCUtils.safeGetString(dbResult, "TBSPACE");
        this.owner = JDBCUtils.safeGetString(dbResult, "OWNER");
        this.ownerType = DB2OwnerType.valueOf(JDBCUtils.safeGetString(dbResult, "OWNERTYPE"));
        this.createTime = JDBCUtils.safeGetTimestamp(dbResult, "CREATE_TIME");
        this.tbspaceId = JDBCUtils.safeGetInteger(dbResult, "TBSPACEID");
        this.tbspaceType = DB2TablespaceType.valueOf(JDBCUtils.safeGetString(dbResult, "TBSPACETYPE"));
        this.dataType = DB2TablespaceDataType.valueOf(JDBCUtils.safeGetString(dbResult, "DATATYPE"));
        this.extentSize = JDBCUtils.safeGetInteger(dbResult, "EXTENTSIZE");
        this.prefetchSize = JDBCUtils.safeGetInteger(dbResult, "PREFETCHSIZE");
        this.overHead = JDBCUtils.safeGetDouble(dbResult, "OVERHEAD");
        this.transferRate = JDBCUtils.safeGetDouble(dbResult, "TRANSFERRATE");
        this.writeOverHead = JDBCUtils.safeGetDouble(dbResult, "WRITEOVERHEAD");
        this.writeTransferRate = JDBCUtils.safeGetDouble(dbResult, "WRITETRANSFERRATE");
        this.pageSize = JDBCUtils.safeGetInteger(dbResult, "PAGESIZE");
        this.dbpgName = JDBCUtils.safeGetString(dbResult, "DBPGNAME");
        this.dropRecovery = JDBCUtils.safeGetBoolean(dbResult, "DROP_RECOVERY", DB2YesNo.Y.name());
        this.remarks = JDBCUtils.safeGetString(dbResult, "REMARKS");

        if (db2DataSource.isAtLeastV10_1()) {
            this.dataTag = JDBCUtils.safeGetInteger(dbResult, "DATATAG");
            this.effectivePrefetchSize = JDBCUtils.safeGetInteger(dbResult, "EFFECTIVEPREFETCHSIZE");

            String storageGroupName = JDBCUtils.safeGetString(dbResult, "SGNAME");
            if (storageGroupName != null) {
                this.storageGroup = db2DataSource.getStorageGroup(VoidProgressMonitor.INSTANCE, storageGroupName);
            }
        }

        Integer bufferpoolId = JDBCUtils.safeGetInteger(dbResult, "BUFFERPOOLID");
        bufferpool = DB2Utils.findBufferpoolById(VoidProgressMonitor.INSTANCE, db2DataSource, bufferpoolId);

    }

    @Override
    public boolean refreshObject(DBRProgressMonitor monitor) throws DBException
    {
        containerCache.clearCache();
        return true;
    }

    // -----------------
    // Properties
    // -----------------

    @Override
    @Property(viewable = true, order = 1)
    public String getName()
    {
        return name;
    }

    @Property(viewable = true, order = 2)
    public Integer getTbspaceId()
    {
        return tbspaceId;
    }

    @Property(viewable = true, order = 3)
    public DB2Bufferpool getBufferPool()
    {
        return bufferpool;
    }

    @Property(viewable = true, order = 4)
    public DB2StorageGroup getStorageGroup()
    {
        return storageGroup;
    }

    @Property(viewable = true, order = 5)
    public Integer getPageSize()
    {
        return pageSize;
    }

    @Property(viewable = true, order = 6)
    public DB2TablespaceType getTbspaceType()
    {
        return tbspaceType;
    }

    @Property(viewable = true, order = 7)
    public DB2TablespaceDataType getDataType()
    {
        return dataType;
    }

    @Property(viewable = false, category = DB2Constants.CAT_OWNER)
    public String getOwner()
    {
        return owner;
    }

    @Property(viewable = false, category = DB2Constants.CAT_OWNER)
    public DB2OwnerType getOwnerType()
    {
        return ownerType;
    }

    @Property(viewable = false, category = DB2Constants.CAT_DATETIME)
    public Timestamp getCreateTime()
    {
        return createTime;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Integer getExtentSize()
    {
        return extentSize;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Integer getPrefetchSize()
    {
        return prefetchSize;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Double getOverHead()
    {
        return overHead;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Double getTransferRate()
    {
        return transferRate;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Double getWriteOverHead()
    {
        return writeOverHead;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Double getWriteTransferRate()
    {
        return writeTransferRate;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Integer getEffectivePrefetchSize()
    {
        return effectivePrefetchSize;
    }

    @Property(viewable = false)
    public String getDbpgName()
    {
        return dbpgName;
    }

    @Property(viewable = false)
    public Boolean getDropRecovery()
    {
        return dropRecovery;
    }

    @Property(viewable = false)
    public Integer getDataTag()
    {
        return dataTag;
    }

    @Override
    @Property(viewable = false)
    public String getDescription()
    {
        return remarks;
    }

    // -----------------
    // Associations
    // -----------------

    @Association
    public Collection<DB2TablespaceContainer> getContainers(DBRProgressMonitor monitor) throws DBException
    {
        return containerCache.getObjects(monitor, this);
    }

    public DB2TablespaceContainer getContainer(DBRProgressMonitor monitor, long containerId) throws DBException
    {
        for (DB2TablespaceContainer container : containerCache.getObjects(monitor, this)) {
            if (container.getContainerId() == containerId) {
                return container;
            }
        }
        return null;
    }

    // ---------------
    // hashCode/equals
    // DF: This is required for "lazy" tablespace loading
    // It is used to match the "fake" tablespace with the "real one"
    // ---------------

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DB2Tablespace other = (DB2Tablespace) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}
<commit_msg>DB2: Lazy loading of tablespaces<commit_after>/*
 * Copyright (C) 2013      <NAME> <EMAIL>
 * Copyright (C) 2010-2013 <NAME> <EMAIL>
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.jkiss.dbeaver.ext.db2.model;

import org.jkiss.dbeaver.DBException;
import org.jkiss.dbeaver.ext.db2.DB2Constants;
import org.jkiss.dbeaver.ext.db2.DB2Utils;
import org.jkiss.dbeaver.ext.db2.model.cache.DB2TablespaceContainerCache;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2OwnerType;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2TablespaceDataType;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2TablespaceType;
import org.jkiss.dbeaver.ext.db2.model.dict.DB2YesNo;
import org.jkiss.dbeaver.model.DBPNamedObject;
import org.jkiss.dbeaver.model.DBPRefreshableObject;
import org.jkiss.dbeaver.model.impl.jdbc.JDBCUtils;
import org.jkiss.dbeaver.model.meta.Association;
import org.jkiss.dbeaver.model.meta.Property;
import org.jkiss.dbeaver.model.runtime.DBRProgressMonitor;
import org.jkiss.dbeaver.runtime.VoidProgressMonitor;

import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.Collection;

/**
 * DB2 Tablespace
 * 
 * @author <NAME>
 */
public class DB2Tablespace extends DB2GlobalObject implements DBPNamedObject, DBPRefreshableObject {

    private final DB2TablespaceContainerCache containerCache = new DB2TablespaceContainerCache();

    private String name;
    private String owner;
    private DB2OwnerType ownerType;
    private Timestamp createTime;
    private Integer tbspaceId;
    private DB2TablespaceType tbspaceType;
    private DB2TablespaceDataType dataType;
    private Integer extentSize;
    private Integer prefetchSize;
    private Double overHead;
    private Double transferRate;
    private Double writeOverHead;
    private Double writeTransferRate;
    private Integer pageSize;
    private String dbpgName;
    private Boolean dropRecovery;
    private Integer dataTag;
    private DB2StorageGroup storageGroup;
    private Integer effectivePrefetchSize;
    private String remarks;

    private DB2Bufferpool bufferpool;

    // -----------------------
    // Constructors
    // -----------------------

    // Constructeur for lazy loading, acts as a placeholder.
    public DB2Tablespace(DB2DataSource db2DataSource, String db2TablespaceName) throws DBException
    {
        super(db2DataSource, false);
        this.name = db2TablespaceName;
    }

    public DB2Tablespace(DB2DataSource db2DataSource, ResultSet dbResult) throws DBException
    {
        super(db2DataSource, true);
        this.name = JDBCUtils.safeGetString(dbResult, "TBSPACE");
        this.owner = JDBCUtils.safeGetString(dbResult, "OWNER");
        this.ownerType = DB2OwnerType.valueOf(JDBCUtils.safeGetString(dbResult, "OWNERTYPE"));
        this.createTime = JDBCUtils.safeGetTimestamp(dbResult, "CREATE_TIME");
        this.tbspaceId = JDBCUtils.safeGetInteger(dbResult, "TBSPACEID");
        this.tbspaceType = DB2TablespaceType.valueOf(JDBCUtils.safeGetString(dbResult, "TBSPACETYPE"));
        this.dataType = DB2TablespaceDataType.valueOf(JDBCUtils.safeGetString(dbResult, "DATATYPE"));
        this.extentSize = JDBCUtils.safeGetInteger(dbResult, "EXTENTSIZE");
        this.prefetchSize = JDBCUtils.safeGetInteger(dbResult, "PREFETCHSIZE");
        this.overHead = JDBCUtils.safeGetDouble(dbResult, "OVERHEAD");
        this.transferRate = JDBCUtils.safeGetDouble(dbResult, "TRANSFERRATE");
        this.writeOverHead = JDBCUtils.safeGetDouble(dbResult, "WRITEOVERHEAD");
        this.writeTransferRate = JDBCUtils.safeGetDouble(dbResult, "WRITETRANSFERRATE");
        this.pageSize = JDBCUtils.safeGetInteger(dbResult, "PAGESIZE");
        this.dbpgName = JDBCUtils.safeGetString(dbResult, "DBPGNAME");
        this.dropRecovery = JDBCUtils.safeGetBoolean(dbResult, "DROP_RECOVERY", DB2YesNo.Y.name());
        this.remarks = JDBCUtils.safeGetString(dbResult, "REMARKS");

        if (db2DataSource.isAtLeastV10_1()) {
            this.dataTag = JDBCUtils.safeGetInteger(dbResult, "DATATAG");
            this.effectivePrefetchSize = JDBCUtils.safeGetInteger(dbResult, "EFFECTIVEPREFETCHSIZE");

            String storageGroupName = JDBCUtils.safeGetString(dbResult, "SGNAME");
            if (storageGroupName != null) {
                this.storageGroup = db2DataSource.getStorageGroup(VoidProgressMonitor.INSTANCE, storageGroupName);
            }
        }

        Integer bufferpoolId = JDBCUtils.safeGetInteger(dbResult, "BUFFERPOOLID");
        bufferpool = DB2Utils.findBufferpoolById(VoidProgressMonitor.INSTANCE, db2DataSource, bufferpoolId);

    }

    @Override
    public boolean refreshObject(DBRProgressMonitor monitor) throws DBException
    {
        containerCache.clearCache();
        return true;
    }

    // -----------------
    // Properties
    // -----------------

    @Override
    @Property(viewable = true, order = 1)
    public String getName()
    {
        return name;
    }

    @Property(viewable = true, order = 2)
    public Integer getTbspaceId()
    {
        return tbspaceId;
    }

    @Property(viewable = true, order = 3)
    public DB2Bufferpool getBufferPool()
    {
        return bufferpool;
    }

    @Property(viewable = true, order = 4)
    public DB2StorageGroup getStorageGroup()
    {
        return storageGroup;
    }

    @Property(viewable = true, order = 5)
    public Integer getPageSize()
    {
        return pageSize;
    }

    @Property(viewable = true, order = 6)
    public DB2TablespaceType getTbspaceType()
    {
        return tbspaceType;
    }

    @Property(viewable = true, order = 7)
    public DB2TablespaceDataType getDataType()
    {
        return dataType;
    }

    @Property(viewable = false, category = DB2Constants.CAT_OWNER)
    public String getOwner()
    {
        return owner;
    }

    @Property(viewable = false, category = DB2Constants.CAT_OWNER)
    public DB2OwnerType getOwnerType()
    {
        return ownerType;
    }

    @Property(viewable = false, category = DB2Constants.CAT_DATETIME)
    public Timestamp getCreateTime()
    {
        return createTime;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Integer getExtentSize()
    {
        return extentSize;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Integer getPrefetchSize()
    {
        return prefetchSize;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Double getOverHead()
    {
        return overHead;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Double getTransferRate()
    {
        return transferRate;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Double getWriteOverHead()
    {
        return writeOverHead;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Double getWriteTransferRate()
    {
        return writeTransferRate;
    }

    @Property(viewable = false, category = DB2Constants.CAT_PERFORMANCE)
    public Integer getEffectivePrefetchSize()
    {
        return effectivePrefetchSize;
    }

    @Property(viewable = false)
    public String getDbpgName()
    {
        return dbpgName;
    }

    @Property(viewable = false)
    public Boolean getDropRecovery()
    {
        return dropRecovery;
    }

    @Property(viewable = false)
    public Integer getDataTag()
    {
        return dataTag;
    }

    @Override
    @Property(viewable = false)
    public String getDescription()
    {
        return remarks;
    }

    // -----------------
    // Associations
    // -----------------

    @Association
    public Collection<DB2TablespaceContainer> getContainers(DBRProgressMonitor monitor) throws DBException
    {
        return containerCache.getObjects(monitor, this);
    }

    public DB2TablespaceContainer getContainer(DBRProgressMonitor monitor, long containerId) throws DBException
    {
        for (DB2TablespaceContainer container : containerCache.getObjects(monitor, this)) {
            if (container.getContainerId() == containerId) {
                return container;
            }
        }
        return null;
    }

    // ---------------
    // hashCode/equals
    // DF: This is required for "lazy" tablespace loading
    // It is used to match the "fake" tablespace with the "real one"
    // ---------------

    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DB2Tablespace other = (DB2Tablespace) obj;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

}

{'fraction_non_alphanumeric': 0.10205437448896157, 'fraction_numerical': 0.010782910874897793, 'mean_word_length': 2.8363066065477356, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1093208', 'n_tokens_mistral': 5820, 'n_tokens_neox': 5637, 'n_words': 1313}