text:
package com.dci.intellij.dbn.object.impl;

import com.dci.intellij.dbn.browser.DatabaseBrowserUtils;
import com.dci.intellij.dbn.browser.model.BrowserTreeChangeListener;
import com.dci.intellij.dbn.browser.model.BrowserTreeNode;
import com.dci.intellij.dbn.browser.ui.HtmlToolTipBuilder;
import com.dci.intellij.dbn.common.content.DynamicContent;
import com.dci.intellij.dbn.common.content.DynamicContentElement;
import com.dci.intellij.dbn.common.content.loader.DynamicContentLoader;
import com.dci.intellij.dbn.common.content.loader.DynamicContentResultSetLoader;
import com.dci.intellij.dbn.common.event.EventManager;
import com.dci.intellij.dbn.common.ui.tree.TreeEventType;
import com.dci.intellij.dbn.connection.ConnectionHandler;
import com.dci.intellij.dbn.connection.ConnectionUtil;
import com.dci.intellij.dbn.database.DatabaseCompatibilityInterface;
import com.dci.intellij.dbn.database.DatabaseMetadataInterface;
import com.dci.intellij.dbn.editor.DBContentType;
import com.dci.intellij.dbn.object.DBArgument;
import com.dci.intellij.dbn.object.DBCluster;
import com.dci.intellij.dbn.object.DBColumn;
import com.dci.intellij.dbn.object.DBConstraint;
import com.dci.intellij.dbn.object.DBDatabaseLink;
import com.dci.intellij.dbn.object.DBDataset;
import com.dci.intellij.dbn.object.DBDimension;
import com.dci.intellij.dbn.object.DBFunction;
import com.dci.intellij.dbn.object.DBIndex;
import com.dci.intellij.dbn.object.DBMaterializedView;
import com.dci.intellij.dbn.object.DBMethod;
import com.dci.intellij.dbn.object.DBNestedTable;
import com.dci.intellij.dbn.object.DBPackage;
import com.dci.intellij.dbn.object.DBPackageFunction;
import com.dci.intellij.dbn.object.DBPackageProcedure;
import com.dci.intellij.dbn.object.DBPackageType;
import com.dci.intellij.dbn.object.DBProcedure;
import com.dci.intellij.dbn.object.DBProgram;
import com.dci.intellij.dbn.object.DBSchema;
import com.dci.intellij.dbn.object.DBSequence;
import com.dci.intellij.dbn.object.DBSynonym;
import com.dci.intellij.dbn.object.DBTable;
import com.dci.intellij.dbn.object.DBTrigger;
import com.dci.intellij.dbn.object.DBType;
import com.dci.intellij.dbn.object.DBTypeAttribute;
import com.dci.intellij.dbn.object.DBTypeFunction;
import com.dci.intellij.dbn.object.DBTypeProcedure;
import com.dci.intellij.dbn.object.DBUser;
import com.dci.intellij.dbn.object.DBView;
import com.dci.intellij.dbn.object.common.DBObject;
import com.dci.intellij.dbn.object.common.DBObjectImpl;
import com.dci.intellij.dbn.object.common.DBObjectRelationType;
import com.dci.intellij.dbn.object.common.DBObjectType;
import com.dci.intellij.dbn.object.common.DBSchemaObject;
import com.dci.intellij.dbn.object.common.list.DBObjectList;
import com.dci.intellij.dbn.object.common.list.DBObjectListContainer;
import com.dci.intellij.dbn.object.common.list.DBObjectListVisitor;
import com.dci.intellij.dbn.object.common.list.DBObjectNavigationList;
import com.dci.intellij.dbn.object.common.list.DBObjectNavigationListImpl;
import com.dci.intellij.dbn.object.common.list.DBObjectRelation;
import com.dci.intellij.dbn.object.common.list.DBObjectRelationListContainer;
import com.dci.intellij.dbn.object.common.status.DBObjectStatus;
import com.dci.intellij.dbn.object.common.status.DBObjectStatusHolder;
import org.jetbrains.annotations.NotNull;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class DBSchemaImpl extends DBObjectImpl implements DBSchema {
    DBObjectList<DBTable> tables;
    DBObjectList<DBView> views;
    DBObjectList<DBMaterializedView> materializedViews;
    DBObjectList<DBSynonym> synonyms;
    DBObjectList<DBSequence> sequences;
    DBObjectList<DBProcedure> procedures;
    DBObjectList<DBFunction> functions;
    DBObjectList<DBPackage> packages;
    DBObjectList<DBType> types;
    DBObjectList<DBDimension> dimensions;
    DBObjectList<DBCluster> clusters;
    DBObjectList<DBDatabaseLink> databaseLinks;

    boolean isUserSchema;
    boolean isPublicSchema;
    boolean isSystemSchema;

    public DBSchemaImpl(ConnectionHandler connectionHandler, ResultSet resultSet) throws SQLException {
        super(connectionHandler.getObjectBundle(), DBContentType.NONE, resultSet);
    }

    @Override
    protected void initObject(ResultSet resultSet) throws SQLException {
        name = resultSet.getString("SCHEMA_NAME");
        isPublicSchema = resultSet.getString("IS_PUBLIC").equals("Y");
        isSystemSchema = resultSet.getString("IS_SYSTEM").equals("Y");
        isUserSchema = getName().equalsIgnoreCase(getConnectionHandler().getUserName());
    }

    @Override
    protected void initLists() {
        DBObjectListContainer ol = initChildObjects();
        DBObjectRelationListContainer orl = initChildObjectRelations();

        tables = ol.createObjectList(DBObjectType.TABLE, this, TABLES_LOADER, true, false);
        views = ol.createObjectList(DBObjectType.VIEW, this, VIEWS_LOADER, true, false);
        materializedViews = ol.createObjectList(DBObjectType.MATERIALIZED_VIEW, this, MATERIALIZED_VIEWS_LOADER, true, false);
        synonyms = ol.createObjectList(DBObjectType.SYNONYM, this, SYNONYMS_LOADER, true, false);
        sequences = ol.createObjectList(DBObjectType.SEQUENCE, this, SEQUENCES_LOADER, true, false);
        procedures = ol.createObjectList(DBObjectType.PROCEDURE, this, PROCEDURES_LOADER, true, false);
        functions = ol.createObjectList(DBObjectType.FUNCTION, this, FUNCTIONS_LOADER, true, false);
        packages = ol.createObjectList(DBObjectType.PACKAGE, this, PACKAGES_LOADER, true, false);
        types = ol.createObjectList(DBObjectType.TYPE, this, TYPES_LOADER, true, false);
        dimensions = ol.createObjectList(DBObjectType.DIMENSION, this, DIMENSIONS_LOADER, true, false);
        clusters = ol.createObjectList(DBObjectType.CLUSTER, this, CLUSTERS_LOADER, true, false);
        databaseLinks = ol.createObjectList(DBObjectType.DBLINK, this, DATABASE_LINKS_LOADER, true, false);

        DBObjectList constraints = ol.createObjectList(DBObjectType.CONSTRAINT, this, CONSTRAINTS_LOADER, true, false);
        DBObjectList indexes = ol.createObjectList(DBObjectType.INDEX, this, INDEXES_LOADER, true, false);
        DBObjectList triggers = ol.createObjectList(DBObjectType.TRIGGER, this, TRIGGERS_LOADER, true, false);
        DBObjectList nestedTables = ol.createObjectList(DBObjectType.NESTED_TABLE, this, ALL_NESTED_TABLES_LOADER, true, false);
        DBObjectList columns = ol.createObjectList(DBObjectType.COLUMN, this, COLUMNS_LOADER, false, true);
        ol.createObjectList(DBObjectType.PACKAGE_FUNCTION, this, ALL_PACKAGE_FUNCTIONS_LOADER, false, true);
        ol.createObjectList(DBObjectType.PACKAGE_PROCEDURE, this, ALL_PACKAGE_PROCEDURES_LOADER, false, true);
        ol.createObjectList(DBObjectType.PACKAGE_TYPE, this, ALL_PACKAGE_TYPES_LOADER, false, true);
        ol.createObjectList(DBObjectType.TYPE_ATTRIBUTE, this, ALL_TYPE_ATTRIBUTES_LOADER, false, true);
        ol.createObjectList(DBObjectType.TYPE_FUNCTION, this, ALL_TYPE_FUNCTIONS_LOADER, false, true);
        ol.createObjectList(DBObjectType.TYPE_PROCEDURE, this, ALL_TYPE_PROCEDURES_LOADER, false, true);
        ol.createObjectList(DBObjectType.ARGUMENT, this, ALL_ARGUMENTS_LOADER, false, true);

        //ol.createHiddenObjectList(DBObjectType.TYPE_METHOD, this, TYPE_METHODS_LOADER);

        orl.createObjectRelationList(
                DBObjectRelationType.CONSTRAINT_COLUMN, this,
                "Constraint relations",
                CONSTRAINT_COLUMN_RELATION_LOADER,
                constraints,
                columns);

        orl.createObjectRelationList(
                DBObjectRelationType.INDEX_COLUMN, this,
                "Index relations",
                INDEX_COLUMN_RELATION_LOADER,
                indexes,
                columns);
    }

    @Override
    public void initProperties() {}

    @Override
    public DBUser getOwner() {
        return getObjectBundle().getUser(name);
    }

    public DBObjectType getObjectType() {
        return DBObjectType.SCHEMA;
    }

    public boolean isPublicSchema() {
        return isPublicSchema;
    }

    public boolean isUserSchema() {
        return isUserSchema;
    }

    public boolean isSystemSchema() {
        return isSystemSchema;
    }

    @Override
    public DBObject getDefaultNavigationObject() {
        return getOwner();
    }

    public DBObject getChildObject(DBObjectType objectType, String name, boolean lookupHidden) {
        DBObject object = super.getChildObject(objectType, name, lookupHidden);
        if (object == null) {
            DBSynonym synonym = (DBSynonym) super.getChildObject(DBObjectType.SYNONYM, name, lookupHidden);
            if (synonym != null && synonym.getUnderlyingObject().isOfType(objectType)) {
                return synonym;
            }
        } else {
            return object;
        }
        return null;
    }

    @Override
    protected List<DBObjectNavigationList> createNavigationLists() {
        DBUser user = getOwner();
        if (user != null) {
            List<DBObjectNavigationList> objectNavigationLists = new ArrayList<DBObjectNavigationList>();
            objectNavigationLists.add(new DBObjectNavigationListImpl("User", user));
            return objectNavigationLists;
        }
        return null;
    }

    private class ConstraintColumnComparator implements Comparator {
        private DBConstraint constraint;
        ConstraintColumnComparator(DBConstraint constraint) {
            this.constraint = constraint;
        }
        public int compare(Object o1, Object o2) {
            DBColumn column1 = (DBColumn) o1;
            DBColumn column2 = (DBColumn) o2;
            return column1.getConstraintPosition(constraint)-
                    column2.getConstraintPosition(constraint);
        }
    }

    public List<DBTable> getTables() {
        return tables.getObjects();
    }

    public List<DBView> getViews() {
        return views.getObjects();
    }

    public List<DBMaterializedView> getMaterializedViews() {
        return materializedViews.getObjects();
    }

    public List<DBIndex> getIndexes() {
        return initChildObjects().getObjectList(DBObjectType.INDEX).getObjects();
    }

    public List<DBSynonym> getSynonyms() {
        return synonyms.getObjects();
    }

    public List<DBSequence> getSequences() {
        return sequences.getObjects();
    }

    public List<DBProcedure> getProcedures() {
        return procedures.getObjects();
    }

    public List<DBFunction> getFunctions() {
        return functions.getObjects();
    }

    public List<DBPackage> getPackages() {
        return packages.getObjects();
    }

    public List<DBTrigger> getTriggers() {
        return initChildObjects().getObjectList(DBObjectType.TRIGGER).getObjects();
    }

    public List<DBType> getTypes() {
        return types.getObjects();
    }

    public List<DBDimension> getDimensions() {
        return dimensions.getObjects();
    }

    public List<DBCluster> getClusters() {
        return clusters.getObjects();
    }

    public List<DBDatabaseLink> getDatabaseLinks() {
        return databaseLinks.getObjects();
    }

    public DBTable getTable(String name) {
        return tables.getObject(name);
    }

    public DBView getView(String name) {
        return views.getObject(name);
    }

    public DBMaterializedView getMaterializedView(String name) {
        return materializedViews.getObject(name);
    }

    public DBIndex getIndex(String name) {
        return (DBIndex) initChildObjects().getObjectList(DBObjectType.INDEX).getObject(name);
    }

    public DBCluster getCluster(String name) {
        return clusters.getObject(name);
    }

    public DBDatabaseLink getDatabaseLink(String name) {
        return databaseLinks.getObject(name);
    }

    public List<DBDataset> getDatasets() {
        List<DBDataset> datasets = new ArrayList<DBDataset>();
        datasets.addAll(getTables());
        datasets.addAll(getViews());
        datasets.addAll(getMaterializedViews());
        return datasets;
    }

    public DBDataset getDataset(String name) {
        DBDataset dataset = getTable(name);
        if (dataset == null) {
            dataset = getView(name);
            if (dataset == null && DatabaseCompatibilityInterface.getInstance(this).supportsObjectType(DBObjectType.MATERIALIZED_VIEW.getTypeId())) {
                dataset = getMaterializedView(name);
            }
        }
        if (dataset == null) {
            //System.out.println("unknown dataset: " + getName() + "." + name);
        }
        return dataset;
    }

    private DBSchemaObject getObjectFallbackOnSynonym(DBObjectList<? extends DBSchemaObject> objects, String name) {
        DBSchemaObject object = objects.getObject(name);
        if (object == null && DatabaseCompatibilityInterface.getInstance(this).supportsObjectType(DBObjectType.SYNONYM.getTypeId())) {
            DBSynonym synonym = synonyms.getObject(name);
            if (synonym != null && synonym.getUnderlyingObject().getObjectType() == objects.getObjectType()) {
                return (DBSchemaObject) synonym.getUnderlyingObject();
            }
        } else {
            return object;
        }
        return null;
    }

    public DBType getType(String name) {
        return (DBType) getObjectFallbackOnSynonym(types, name);
    }

    public DBPackage getPackage(String name) {
        return (DBPackage) getObjectFallbackOnSynonym(packages, name);
    }

    public DBProcedure getProcedure(String name) {
        return (DBProcedure) getObjectFallbackOnSynonym(procedures, name);
    }

    public DBFunction getFunction(String name) {
        return (DBFunction) getObjectFallbackOnSynonym(functions, name);
    }

    public DBProgram getProgram(String name) {
        DBProgram program = getPackage(name);
        if (program == null) program = getType(name);
        return program;
    }

    public DBMethod getMethod(String name, String type) {
        if (type == null) {
            DBMethod method = getProcedure(name);
            if (method == null) method = getFunction(name);
            return method;
        } else if (type.equalsIgnoreCase("PROCEDURE")) {
            return getProcedure(name);
        } else if (type.equalsIgnoreCase("FUNCTION")) {
            return getFunction(name);
        }
        return null;
    }

    public DBMethod getMethod(String name) {
        return getMethod(name, null);
    }

    public synchronized void refreshObjectsStatus() {
        final Set<BrowserTreeNode> refreshNodes = resetObjectsStatus();
        Connection connection = null;
        ResultSet resultSet = null;
        try {
            connection = getConnectionHandler().getPoolConnection();
            resultSet = getConnectionHandler().getInterfaceProvider().getMetadataInterface().loadInvalidObjects(getName(), connection);
            while (resultSet != null && resultSet.next()) {
                String objectName = resultSet.getString("OBJECT_NAME");
                DBSchemaObject schemaObject = (DBSchemaObject) getChildObjectNoLoad(objectName);
                if (schemaObject != null && schemaObject.getStatus().has(DBObjectStatus.VALID)) {
                    DBObjectStatusHolder objectStatus = schemaObject.getStatus();
                    boolean statusChanged;

                    if (schemaObject.getContentType().isBundle()) {
                        String objectType = resultSet.getString("OBJECT_TYPE");
                        statusChanged = objectType.contains("BODY") ?
                                objectStatus.set(DBContentType.CODE_BODY, DBObjectStatus.VALID, false) :
                                objectStatus.set(DBContentType.CODE_SPEC, DBObjectStatus.VALID, false);
                    }
                    else {
                        statusChanged = objectStatus.set(DBObjectStatus.VALID, false);
                    }
                    if (statusChanged) {
                        refreshNodes.add(schemaObject.getTreeParent());
                    }
                }
            }

            resultSet = getConnectionHandler().getInterfaceProvider().getMetadataInterface().loadDebugObjects(getName(), connection);
            while (resultSet != null && resultSet.next()) {
                String objectName = resultSet.getString("OBJECT_NAME");
                DBSchemaObject schemaObject = (DBSchemaObject) getChildObjectNoLoad(objectName);
                if (schemaObject != null && schemaObject.getStatus().has(DBObjectStatus.DEBUG)) {
                    DBObjectStatusHolder objectStatus = schemaObject.getStatus();
                    boolean statusChanged;

                    if (schemaObject.getContentType().isBundle()) {
                        String objectType = resultSet.getString("OBJECT_TYPE");
                        statusChanged = objectType.contains("BODY") ?
                                objectStatus.set(DBContentType.CODE_BODY, DBObjectStatus.DEBUG, true) :
                                objectStatus.set(DBContentType.CODE_SPEC, DBObjectStatus.DEBUG, true);
                    }
                    else {
                        statusChanged = objectStatus.set(DBObjectStatus.DEBUG, true);
                    }
                    if (statusChanged) {
                        refreshNodes.add(schemaObject.getTreeParent());
                    }
                }
            }

        } catch (SQLException e) {
            getLogger().error("Error loading data model. " + e.getMessage());
        } finally {
            ConnectionUtil.closeResultSet(resultSet);
            getConnectionHandler().freePoolConnection(connection);
        }

        for (BrowserTreeNode treeNode : refreshNodes) {
            EventManager.notify(getProject(), BrowserTreeChangeListener.TOPIC).nodeChanged(treeNode, TreeEventType.NODES_CHANGED);
        }
    }

    private Set<BrowserTreeNode> resetObjectsStatus() {
        ObjectStatusUpdater updater = new ObjectStatusUpdater();
        initChildObjects().visitLists(updater, true);
        return updater.getRefreshNodes();
    }

    class ObjectStatusUpdater implements DBObjectListVisitor {
        private Set<BrowserTreeNode> refreshNodes = new HashSet<BrowserTreeNode>();

        public void visitObjectList(DBObjectList<DBObject> objectList) {
            if (objectList.isLoaded() && !objectList.isDirty() && !objectList.isLoading()) {
                List<DBObject> objects = objectList.getObjects();
                for (DBObject object : objects) {
                    if (object instanceof DBSchemaObject) {
                        DBSchemaObject schemaObject = (DBSchemaObject) object;
                        DBObjectStatusHolder objectStatus = schemaObject.getStatus();
                        if (objectStatus.has(DBObjectStatus.VALID)) {
                            if (objectStatus.set(DBObjectStatus.VALID, true)) {
                                refreshNodes.add(object.getTreeParent());
                            }
                        }
                        if (objectStatus.has(DBObjectStatus.DEBUG)) {
                            if (objectStatus.set(DBObjectStatus.DEBUG, false)) {
                                refreshNodes.add(object.getTreeParent());
                            }
                        }
                    } else {
                        break;
                    }
                }
            }
        }

        public Set<BrowserTreeNode> getRefreshNodes() {
            return refreshNodes;
        }
    }

    public void buildToolTip(HtmlToolTipBuilder ttb) {
        ttb.append(true, getObjectType().getName(), true);
        ttb.createEmptyRow();
        super.buildToolTip(ttb);
    }

    /*********************************************************
     *                     TreeElement                       *
     *********************************************************/
    @NotNull
    public List<BrowserTreeNode> buildAllPossibleTreeChildren() {
        return DatabaseBrowserUtils.createList(
                tables,
                views,
                materializedViews,
                synonyms,
                sequences,
                procedures,
                functions,
                packages,
                types,
                dimensions,
                clusters,
                databaseLinks);
    }

    /*********************************************************
     *                      Relation builders                *
     *********************************************************/
    private static final DynamicContentLoader CONSTRAINT_COLUMN_RELATION_LOADER = new DynamicContentResultSetLoader() {
        public ResultSet createResultSet(DynamicContent dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllConstraintRelations(schema.getName(), connection);
        }

        public DynamicContentElement createElement(DynamicContent dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String datasetName = resultSet.getString("DATASET_NAME");
            String columnName = resultSet.getString("COLUMN_NAME");
            String constraintName = resultSet.getString("CONSTRAINT_NAME");
            int position = resultSet.getInt("POSITION");

/*
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            DBObjectList<DBConstraint> constraints = schema.getObjectLists().getObjectList(DBObjectType.CONSTRAINT);
            DBConstraint constraint = constraints.getObject(constraintName, datasetName);

            if (constraint != null) {
                DBObjectList<DBColumn> columns = schema.getObjectLists().getHiddenObjectList(DBObjectType.COLUMN);
                DBColumn column = columns.getObject(columnName, datasetName);

                if (column != null) {
                    return new DBConstraintColumnRelation(constraint, column, position);
                }
            }
*/
            DBDataset dataset = (DBDataset) loaderCache.getObject(datasetName);
            if (dataset == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                dataset = schema.getDataset(datasetName);
                loaderCache.setObject(datasetName, dataset);
            }

            if (dataset != null) {
                DBConstraint constraint = dataset.getConstraint(constraintName);
                DBColumn column = dataset.getColumn(columnName);
                if (column != null && constraint != null) {
                    return new DBConstraintColumnRelation(constraint, column, position);
                }
            }

            return null;
        }
    };

    private static final DynamicContentLoader INDEX_COLUMN_RELATION_LOADER = new DynamicContentResultSetLoader() {
        public ResultSet createResultSet(DynamicContent dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllIndexRelations(schema.getName(), connection);
        }

        public DBObjectRelation createElement(DynamicContent dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String tableName = resultSet.getString("TABLE_NAME");
            String columnName = resultSet.getString("COLUMN_NAME");
            String indexName = resultSet.getString("INDEX_NAME");

            /*DBSchema schema = (DBSchema) dynamicContent.getParent();
            DBObjectList<DBIndex> indexes = schema.getObjectLists().getObjectList(DBObjectType.INDEX);
            DBIndex index = indexes.getObject(indexName, tableName);

            if (index != null) {
                DBObjectList<DBColumn> columns = schema.getObjectLists().getHiddenObjectList(DBObjectType.COLUMN);
                DBColumn column = columns.getObject(columnName, tableName);

                if (column != null) {
                    return new DBIndexColumnRelation(index, column);
                }
            }*/

            DBTable table = (DBTable) loaderCache.getObject(tableName);
            if (table == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                table = schema.getTable(tableName);
                loaderCache.setObject(tableName, table);
            }

            if (table != null) {
                DBIndex index = table.getIndex(indexName);
                DBColumn column = table.getColumn(columnName);

                if (column != null && index != null) {
                    return new DBIndexColumnRelation(index, column);
                }
            }
            return null;
        }
    };

    /*********************************************************
     *                         Loaders                       *
     *********************************************************/

    private static final DynamicContentLoader TABLES_LOADER = new DynamicContentResultSetLoader<DBTable>() {
        public ResultSet createResultSet(DynamicContent<DBTable> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadTables(schema.getName(), connection);
        }

        public DBTable createElement(DynamicContent<DBTable> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBTableImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader VIEWS_LOADER = new DynamicContentResultSetLoader<DBView>(){
        public ResultSet createResultSet(DynamicContent<DBView> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadViews(schema.getName(), connection);
        }

        public DBView createElement(DynamicContent<DBView> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBViewImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader MATERIALIZED_VIEWS_LOADER = new DynamicContentResultSetLoader<DBMaterializedView>(){
        public ResultSet createResultSet(DynamicContent<DBMaterializedView> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadMaterializedViews(schema.getName(), connection);
        }

        public DBMaterializedView createElement(DynamicContent<DBMaterializedView> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBMaterializedViewImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader SYNONYMS_LOADER = new DynamicContentResultSetLoader<DBSynonym>() {
        public ResultSet createResultSet(DynamicContent<DBSynonym> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadSynonyms(schema.getName(), connection);
        }

        public DBSynonym createElement(DynamicContent<DBSynonym> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBSynonymImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader SEQUENCES_LOADER = new DynamicContentResultSetLoader<DBSequence>() {
        public ResultSet createResultSet(DynamicContent<DBSequence> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadSequences(schema.getName(), connection);
        }

        public DBSequence createElement(DynamicContent<DBSequence> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBSequenceImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader PROCEDURES_LOADER = new DynamicContentResultSetLoader<DBProcedure>() {
        public ResultSet createResultSet(DynamicContent<DBProcedure> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadProcedures(schema.getName(), connection);
        }

        public DBProcedure createElement(DynamicContent<DBProcedure> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBProcedureImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader FUNCTIONS_LOADER = new  DynamicContentResultSetLoader<DBFunction>() {
        public ResultSet createResultSet(DynamicContent<DBFunction> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadFunctions(schema.getName(), connection);
        }
        public DBFunction createElement(DynamicContent<DBFunction> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBFunctionImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader PACKAGES_LOADER = new  DynamicContentResultSetLoader<DBPackage>() {
        public ResultSet createResultSet(DynamicContent<DBPackage> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadPackages(schema.getName(), connection);
        }

        public DBPackage createElement(DynamicContent<DBPackage> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBPackageImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader TYPES_LOADER = new  DynamicContentResultSetLoader<DBType>() {
        public ResultSet createResultSet(DynamicContent<DBType> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadTypes(schema.getName(), connection);
        }

        public DBType createElement(DynamicContent<DBType> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBTypeImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader DIMENSIONS_LOADER = new  DynamicContentResultSetLoader<DBDimension>() {
        public ResultSet createResultSet(DynamicContent<DBDimension> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadDimensions(schema.getName(), connection);
        }

        public DBDimension createElement(DynamicContent<DBDimension> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBDimensionImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader CLUSTERS_LOADER = new  DynamicContentResultSetLoader<DBCluster>() {
        public ResultSet createResultSet(DynamicContent<DBCluster> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadClusters(schema.getName(), connection);
        }

        public DBCluster createElement(DynamicContent<DBCluster> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBClusterImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader DATABASE_LINKS_LOADER = new  DynamicContentResultSetLoader<DBDatabaseLink>() {
        public ResultSet createResultSet(DynamicContent dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadDatabaseLinks(schema.getName(), connection);
        }

        public DBDatabaseLink createElement(DynamicContent<DBDatabaseLink> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return new DBDatabaseLinkImpl(schema, resultSet);
        }
    };

    private static final DynamicContentLoader COLUMNS_LOADER = new  DynamicContentResultSetLoader<DBColumn>() {
        public ResultSet createResultSet(DynamicContent<DBColumn> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllColumns(schema.getName(), connection);
        }

        public DBColumn createElement(DynamicContent<DBColumn> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String datasetName = resultSet.getString("DATASET_NAME");

            DBDataset dataset = (DBDataset) loaderCache.getObject(datasetName);
            if (dataset == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                dataset = schema.getDataset(datasetName);
                loaderCache.setObject(datasetName, dataset);
            }

            // dataset may be null if cluster column!!
            return dataset == null ? null : new DBColumnImpl(dataset, resultSet);
        }
    };

    private static final DynamicContentLoader CONSTRAINTS_LOADER = new  DynamicContentResultSetLoader<DBConstraint>() {
        public ResultSet createResultSet(DynamicContent<DBConstraint> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllConstraints(schema.getName(), connection);
        }

        public DBConstraint createElement(DynamicContent<DBConstraint> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String datasetName = resultSet.getString("DATASET_NAME");

            DBDataset dataset = (DBDataset) loaderCache.getObject(datasetName);
            if (dataset == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                dataset = schema.getDataset(datasetName);
                loaderCache.setObject(datasetName, dataset);
            }

            return dataset == null ? null : new DBConstraintImpl(dataset, resultSet);
        }
    };

    private static final DynamicContentLoader INDEXES_LOADER = new DynamicContentResultSetLoader<DBIndex>() {
        public ResultSet createResultSet(DynamicContent<DBIndex> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllIndexes(schema.getName(), connection);
        }

        public DBIndex createElement(DynamicContent<DBIndex> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String tableName = resultSet.getString("TABLE_NAME");

            DBTable table = (DBTable) loaderCache.getObject(tableName);
            if (table == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                table = schema.getTable(tableName);
                loaderCache.setObject(tableName, table);
            }

            return table == null ? null : new DBIndexImpl(table, resultSet);
        }
    };

    private static final DynamicContentLoader TRIGGERS_LOADER = new  DynamicContentResultSetLoader<DBTrigger>() {
        public ResultSet createResultSet(DynamicContent<DBTrigger> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllTriggers(schema.getName(), connection);
        }

        public DBTrigger createElement(DynamicContent<DBTrigger> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String datasetName = resultSet.getString("DATASET_NAME");
            DBDataset dataset = (DBDataset) loaderCache.getObject(datasetName);
            if (dataset == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                dataset = schema.getDataset(datasetName);
                loaderCache.setObject(datasetName, dataset);
            }
            return new DBTriggerImpl(dataset, resultSet);
        }
    };

    private static final DynamicContentLoader ALL_NESTED_TABLES_LOADER = new  DynamicContentResultSetLoader<DBNestedTable>() {
        public ResultSet createResultSet(DynamicContent dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllNestedTables(schema.getName(), connection);
        }

        public DBNestedTable createElement(DynamicContent<DBNestedTable> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String tableName = resultSet.getString("TABLE_NAME");
            DBTable table = (DBTable) loaderCache.getObject(tableName);
            if (table == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                table = schema.getTable(tableName);
                loaderCache.setObject(tableName, table);
            }
            return new DBNestedTableImpl(table, resultSet);
        }
    };

    private static final DynamicContentLoader ALL_PACKAGE_FUNCTIONS_LOADER = new DynamicContentResultSetLoader<DBPackageFunction>() {
        public ResultSet createResultSet(DynamicContent dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllPackageFunctions(schema.getName(), connection);
        }

        public DBPackageFunction createElement(DynamicContent<DBPackageFunction> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String packageName = resultSet.getString("PACKAGE_NAME");
            DBPackage packagee = (DBPackage) loaderCache.getObject(packageName);
            if (packagee == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                packagee = schema.getPackage(packageName);
                loaderCache.setObject(packageName, packagee);
            }
            return new DBPackageFunctionImpl(packagee, resultSet);
        }
    };

    private static final DynamicContentLoader ALL_PACKAGE_PROCEDURES_LOADER = new DynamicContentResultSetLoader<DBPackageProcedure>() {
        public ResultSet createResultSet(DynamicContent dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllPackageProcedures(schema.getName(), connection);
        }

        public DBPackageProcedure createElement(DynamicContent<DBPackageProcedure> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String packageName = resultSet.getString("PACKAGE_NAME");
            DBPackage packagee = (DBPackage) loaderCache.getObject(packageName);
            if (packagee == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                packagee = schema.getPackage(packageName);
                loaderCache.setObject(packageName, packagee);
            }
            return new DBPackageProcedureImpl(packagee, resultSet);
        }
    };

    private static final DynamicContentLoader ALL_PACKAGE_TYPES_LOADER = new DynamicContentResultSetLoader<DBPackageType>() {
        public ResultSet createResultSet(DynamicContent<DBPackageType> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllPackageTypes(schema.getName(), connection);
        }

        public DBPackageType createElement(DynamicContent<DBPackageType> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String packageName = resultSet.getString("PACKAGE_NAME");
            DBPackage packagee = (DBPackage) loaderCache.getObject(packageName);
            if (packagee == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                packagee = schema.getPackage(packageName);
                loaderCache.setObject(packageName, packagee);
            }
            return new DBPackageTypeImpl(packagee, resultSet);
        }
    };

    private static final DynamicContentLoader ALL_TYPE_ATTRIBUTES_LOADER = new DynamicContentResultSetLoader<DBTypeAttribute>() {
        public ResultSet createResultSet(DynamicContent<DBTypeAttribute> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllTypeAttributes(schema.getName(), connection);
    }

        public DBTypeAttribute createElement(DynamicContent<DBTypeAttribute> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String typeName = resultSet.getString("TYPE_NAME");
            DBType type = (DBType) loaderCache.getObject(typeName);
            if (type == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                type = schema.getType(typeName);
                loaderCache.setObject(typeName, type);
            }
            return new DBTypeAttributeImpl(type, resultSet);
        }
    };

    private static final DynamicContentLoader ALL_TYPE_FUNCTIONS_LOADER = new DynamicContentResultSetLoader<DBTypeFunction>() {
        public ResultSet createResultSet(DynamicContent dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllTypeFunctions(schema.getName(), connection);
        }

        public DBTypeFunction createElement(DynamicContent<DBTypeFunction> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String typeName = resultSet.getString("TYPE_NAME");
            DBType type = (DBType) loaderCache.getObject(typeName);
            if (type == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                type = schema.getType(typeName);
                loaderCache.setObject(typeName, type);
            }
            return type == null ?  null : new DBTypeFunctionImpl(type, resultSet);
        }
    };

    private static final DynamicContentLoader ALL_TYPE_PROCEDURES_LOADER = new DynamicContentResultSetLoader<DBTypeProcedure>() {
        public ResultSet createResultSet(DynamicContent dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllTypeProcedures(schema.getName(), connection);
        }

        public DBTypeProcedure createElement(DynamicContent<DBTypeProcedure> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String typeName = resultSet.getString("TYPE_NAME");
            DBType type = (DBType) loaderCache.getObject(typeName);
            if (type == null) {
                DBSchema schema = (DBSchema) dynamicContent.getParent();
                type = schema.getType(typeName);
                loaderCache.setObject(typeName, type);
            }
            return type == null ? null : new DBTypeProcedureImpl(type, resultSet);
        }
    };

    private static final DynamicContentLoader ALL_ARGUMENTS_LOADER = new DynamicContentResultSetLoader<DBArgument>() {
        public ResultSet createResultSet(DynamicContent<DBArgument> dynamicContent, Connection connection) throws SQLException {
            DatabaseMetadataInterface metadataInterface = dynamicContent.getConnectionHandler().getInterfaceProvider().getMetadataInterface();
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            return metadataInterface.loadAllMethodArguments(schema.getName(), connection);
        }

        public DBArgument createElement(DynamicContent<DBArgument> dynamicContent, ResultSet resultSet, LoaderCache loaderCache) throws SQLException {
            String programName = resultSet.getString("PROGRAM_NAME");
            String methodName = resultSet.getString("METHOD_NAME");
            String methodType = resultSet.getString("METHOD_TYPE");
            int overload = resultSet.getInt("OVERLOAD");
            DBSchema schema = (DBSchema) dynamicContent.getParent();
            DBProgram program = programName == null ? null : schema.getProgram(programName);

            String cacheKey = methodName + methodType;
            DBMethod method = (DBMethod) loaderCache.getObject(cacheKey);

            if (method == null || method.getProgram() != program || method.getOverload() != overload) {
                if (programName == null) {
                    method = schema.getMethod(methodName, methodType);
                } else {
                    method = program == null ? null : program.getMethod(methodName, overload);
                }
                loaderCache.setObject(cacheKey, method);
            }
            return method == null ? null : new DBArgumentImpl(method, resultSet);
        }
    };
}


text:
// Generated by xsd compiler for ios/objective-c
// DO NOT CHANGE!

#import <Foundation/Foundation.h>
#import "PicoClassSchema.h"
#import "PicoPropertySchema.h"
#import "PicoConstants.h"
#import "PicoBindable.h"

/**
 (public class)

 @ingroup AWSECommerceServicePortType
*/
@interface NonNegativeIntegerWithUnits : NSObject <PicoBindable> {

@private
    NSNumber *_value;
    NSString *_units;

}

/**
 (public property)

 type : NSNumber, wrapper for primitive long
*/
@property (nonatomic, strong) NSNumber *value;

/**
 (public property)

 type : NSString, wrapper for primitive string
*/
@property (nonatomic, strong) NSString *units;

@end

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using RabbitMQ.Client.Events;
using RawRabbit.Common;
using RawRabbit.Exceptions;
using RawRabbit.Logging;
using RawRabbit.Operations.Request.Core;
using RawRabbit.Pipe;
using RawRabbit.Serialization;

namespace RawRabbit.Operations.Request.Middleware
{
	public class ResponderExceptionOptions
	{
		public Func<IPipeContext, object> MessageFunc { get; set; }
		public Func<ExceptionInformation, IPipeContext, Task> HandlerFunc { get; set; }
		public Func<IPipeContext, Type> ResponseTypeFunc { get; set; }
		public Func<IPipeContext, BasicDeliverEventArgs> DeliveryArgsFunc { get; set; }
	}

	public class ResponderExceptionMiddleware : Pipe.Middleware.Middleware
	{
		private readonly ISerializer _serializer;
		protected Func<IPipeContext, object> ExceptionInfoFunc;
		protected Func<ExceptionInformation, IPipeContext, Task> HandlerFunc;
		private readonly ILog _logger = LogProvider.For<ResponderExceptionMiddleware>();
		protected Func<IPipeContext, Type> ResponseTypeFunc;
		private readonly Func<IPipeContext, BasicDeliverEventArgs> _deliveryArgFunc;

		public ResponderExceptionMiddleware(ISerializer serializer, ResponderExceptionOptions options = null)
		{
			_serializer = serializer;
			ExceptionInfoFunc = options?.MessageFunc ?? (context => context.GetResponseMessage());
			HandlerFunc = options?.HandlerFunc;
			_deliveryArgFunc = options?.DeliveryArgsFunc ?? (context => context.GetDeliveryEventArgs());
			ResponseTypeFunc = options?.ResponseTypeFunc ?? (context =>
			{
				var type = GetDeliverEventArgs(context)?.BasicProperties.Type;
				return !string.IsNullOrWhiteSpace(type) ? Type.GetType(type, false) : typeof(object);
			});
		}

		public override Task InvokeAsync(IPipeContext context, CancellationToken token = new CancellationToken())
		{
			var responseType = GetResponseType(context);
			if (responseType == typeof(ExceptionInformation))
			{
				var exceptionInfo = GetExceptionInfo(context);
				return HandleRespondException(exceptionInfo, context);
			}
			return Next.InvokeAsync(context, token);
		}

		protected virtual BasicDeliverEventArgs GetDeliverEventArgs(IPipeContext context)
		{
			return _deliveryArgFunc?.Invoke(context);
		}

		protected virtual Type GetResponseType(IPipeContext context)
		{
			return ResponseTypeFunc?.Invoke(context);
		}

		protected virtual byte[] GetMessageBody(IPipeContext context)
		{
			var deliveryArgs = GetDeliverEventArgs(context);
			return deliveryArgs?.Body ?? new byte[0];
		}

		protected virtual ExceptionInformation GetExceptionInfo(IPipeContext context)
		{
			var body = GetMessageBody(context);
			try
			{
				return _serializer.Deserialize<ExceptionInformation>(body);
			}
			catch (Exception e)
			{
				return new ExceptionInformation
				{
					Message =
						$"An unhandled exception was thrown by the responder, but the requesting client was unable to deserialize exception info. {Encoding.UTF8.GetString(body)}.",
					InnerMessage = e.Message,
					StackTrace = e.StackTrace,
					ExceptionType = e.GetType().Name
				};
			}
		}

		protected virtual Task HandleRespondException(ExceptionInformation exceptionInfo, IPipeContext context)
		{
			_logger.Info("An unhandled exception occured when remote tried to handle request.\n  Message: {exceptionMessage}\n  Stack Trace: {stackTrace}", exceptionInfo.Message, exceptionInfo.StackTrace);

			if (HandlerFunc != null)
			{
				return HandlerFunc(exceptionInfo, context);
			}

			var exception = new MessageHandlerException(exceptionInfo.Message)
			{
				InnerExceptionType = exceptionInfo.ExceptionType,
				InnerStackTrace = exceptionInfo.StackTrace,
				InnerMessage = exceptionInfo.InnerMessage
			};
			return TaskUtil.FromException(exception);
		}
	}
}


text:
"""Usage: ./replace.py FILE..
Replaces some defined patterns.
"""

import re
import sys
import logging

# For layoutExt() removal.
SUBS = [
    (re.compile(r"(?m)^\s*return\s*XmlParser::layoutExt\(\$this(,\s*true)?\);\s*^"), ""),
    ]

# For i:display removal.
SUBS = [
        (re.compile(r"(?m)^\s*<i:display.*</i:display>\s*$", re.DOTALL), ""),
    ]

# For i:comment renaming.
SUBS = [
    (re.compile(r"i:help\s*>"), "i:tooltip>"),
    (re.compile(r"i:comment\s*>"), "i:help>"),
    ]

def main():
    args = sys.argv[1:]
    if len(args) == 0:
        print >>sys.stderr, __doc__
        sys.exit(1)

    for filename in args:
        data = open(filename).read()
        changed = data
        for pattern, repl in SUBS:
            changed = pattern.sub(repl, changed)

        if changed != data:
            logging.warn("Changing %s", filename)
            output = open(filename, "w")
            output.write(changed)
            output.close()

main()


text:
package testData.completion.propertyname;

public class Person {

    private String name;
    private int age;
    private boolean propertyWithoutSetter;

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setPropertyWithSetterOnly(String value) {

    }

}


text:
module KeywordsHelper
end


text:
import os
import shutil
import sys
import sqlite3
import unittest
from unittest.mock import patch, Mock

sys.path.append(os.path.dirname(os.path.dirname(os.path.realpath(os.path.abspath(__file__)))))

from taktyk.entry import Entry
from taktyk.db import DB, database_list, set_added_info
from taktyk import settings

class Prepare(unittest.TestCase):
    def setUp(self):
        settings.ENTRIES_ADDED = 0
        settings.COMMENTS_ADDED = 0
        settings.USER_FILES_PATH = os.path.join(settings.SAVE_PATH, 'tests')
        self.path = os.path.join(settings.USER_FILES_PATH, 'db')
        os.makedirs(self.path, exist_ok=True)
        self.entry = Entry(id_=1, author='test_author', date='test_date', body='test_body',
                           body_html='test_body_html', url='test_url', plus='1000',
                           media_url='test_media_url', tags=' testtag1 ', is_nsfw=False,
                           entry_id=None, type_='entry')

    def tearDown(self):
        if os.path.isdir(self.path):
            shutil.rmtree(self.path)

class DBCreationTest(Prepare):
    def test_create_new_without_name(self):
        DB.create_new('')
        self.assertTrue(os.path.isfile(os.path.join(self.path, settings.DB_NAME)))

    def test_create_new_with_name(self):
        DB.create_new('test')
        self.assertTrue(os.path.isfile(os.path.join(self.path, 'test.db')))

    @patch('taktyk.db.Decision')
    def test_ask_and_create_without_name(self, mock_decision):
        dec_ins = Mock()
        dec_ins.run.return_value = ''
        mock_decision.return_value = dec_ins
        DB.ask_and_create()
        self.assertTrue(os.path.isfile(os.path.join(self.path, settings.DB_NAME)))
        self.assertTrue(dec_ins.run.called)

    @patch('taktyk.db.Decision')
    def test_ask_and_create_with_name(self, mock_decision):
        dec_ins = Mock()
        dec_ins.run.return_value = 'test_name'.strip()
        mock_decision.return_value = dec_ins
        DB.ask_and_create()
        self.assertTrue(os.path.isfile(os.path.join(self.path, 'test_name.db')))
        self.assertTrue(dec_ins.run.called)

    def test_if_tables_are_correct(self):
        DB.create_new('test')
        connection = sqlite3.connect(os.path.join(self.path, 'test.db'))
        connection.execute('SELECT * FROM entry')
        connection.execute('SELECT * FROM entry_comment')

class InsertOneTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')
        self.db_path = os.path.join(self.path, 'test.db')
        self.entry = Entry(id_=123, author='test_author', date='test_date', body='test_body',
                           body_html='test_body_html', url='test_url', plus='1000',
                           media_url='test_media_url', tags=' testtag1 testtag2 ', is_nsfw=False,
                           entry_id=None, type_='entry')

    def test_when_object_passed_is_none(self):
        with DB.Connect() as cursor:
            self.assertFalse(DB.insert_one(cursor, []))

    def test_when_entry_passed(self):
        with DB.Connect() as cursor:
            self.assertTrue(DB.insert_one(cursor, self.entry))
            db_entry = cursor.execute('SELECT * FROM entry').fetchone()
            self.assertEqual(db_entry, tuple(self.entry))
            self.assertEqual(1, settings.ENTRIES_ADDED)

    def test_when_entry_comment_passed(self):
        self.entry.type_ = 'entry_comment'
        self.entry.entry_id = 321
        with DB.Connect() as cursor:
            self.assertTrue(DB.insert_one(cursor, self.entry))
            db_entry = cursor.execute('SELECT * FROM entry_comment').fetchone()
            self.assertEqual(db_entry, tuple(self.entry))
            self.assertEqual(1, settings.COMMENTS_ADDED)

    def test_when_try_add_entry_already_in_db(self):
        with DB.Connect() as cursor:
            self.assertTrue(DB.insert_one(cursor, self.entry))
            self.assertFalse(DB.insert_one(cursor, self.entry))

class GetIdsTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')

    def test_if_ids_correctly_returned_from_entry(self):
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 3
            DB.insert_one(cursor, self.entry)
            self.assertEqual({1, 2, 3}, set(DB.get_ids(cursor, 'entry')))

    def test_if_ids_correctly_returned_from_entry_comment(self):
        self.entry.type_ = 'entry_comment'
        self.entry.entry_id = 321
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 3
            DB.insert_one(cursor, self.entry)
            self.assertEqual({1, 2, 3}, set(DB.get_ids(cursor, 'entry_comment')))

    def test_if_ids_returned_if_tag_is_matched(self):
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 3
            self.entry.tags = ' testtag2 '
            DB.insert_one(cursor, self.entry)
            self.assertEqual({1, 2}, set(DB.get_ids(cursor, 'entry', tag='testtag1')))

    def test_if_ids_if_nsfw_filtering_on(self):
        settings.NSFW_FILTER = True
        with DB.Connect() as cursor:
            self.entry.is_nsfw = True
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            self.entry.is_nsfw = True
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 3
            self.entry.is_nsfw = False
            DB.insert_one(cursor, self.entry)
            self.assertEqual({3}, set(DB.get_ids(cursor, 'entry')))

    def test_if_decorator_works_when_cursor_not_passed(self):
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            DB.insert_one(cursor, self.entry)
        self.assertEqual({1, 2}, set(DB.get_ids('entry')))

    def test_if_date_sorting_works(self):
        with DB.Connect() as cursor:
            self.entry.date = '5.06.2017'
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            self.entry.date = '7.06.2017'
            DB.insert_one(cursor, self.entry)
            self.assertEqual([2, 1], list(DB.get_ids('entry')))

class GetEntryRowTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')

    def test_if_row_returned(self):
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            row = DB.get_entry_row(cursor, 1)
        self.assertEqual(row, tuple(self.entry))

    def test_when_id_not_in_db(self):
        with DB.Connect() as cursor:
            row = DB.get_entry_row(cursor, 2)
        self.assertIsNone(row)

    def test_when_cursor_not_passed(self):
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
        row = DB.get_entry_row(1)
        self.assertEqual(row, tuple(self.entry))

class GetCommentsByEntryIdTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')

    def test_if_comments_returned(self):
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 1
            self.entry.entry_id = 1
            self.entry.type_ = 'entry_comment'
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            self.entry.entry_id = 1
            self.entry.type_ = 'entry_comment'
            DB.insert_one(cursor, self.entry)
            result = list(DB.get_comments_by_entry_id(cursor, 1))
            self.assertEqual(2, len(result))
        result = list(DB.get_comments_by_entry_id(1))  # called without cursor passed
        self.assertEqual(2, len(result))

    def test_if_comments_not_found(self):
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            result = list(DB.get_comments_by_entry_id(cursor, 1))
            self.assertEqual([], result)

class GetEntryWithCommentsTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')

    def test_if_entry_with_comments_returned(self):
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 1
            self.entry.entry_id = 1
            self.entry.type_ = 'entry_comment'
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            self.entry.entry_id = 1
            self.entry.type_ = 'entry_comment'
            DB.insert_one(cursor, self.entry)
            entry = DB.get_entry_with_comments(cursor, 1)
            self.assertEqual(1, entry.id_)
            self.assertEqual(2, len(list(entry.comments)))
        entry = DB.get_entry_with_comments(1)
        self.assertEqual(1, entry.id_)
        self.assertEqual(2, len(list(entry.comments)))

class GetAllEntriesWithCommentsTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            self.entry.tags = ' testtag2 '
            DB.insert_one(cursor, self.entry)
            for e_id in (1, 2):
                for i in range(1, 4):
                    self.entry.id_ = i + e_id * 4  # fix for different ids
                    self.entry.entry_id = e_id
                    self.entry.type_ = 'entry_comment'
                    DB.insert_one(cursor, self.entry)

    def test_if_return_is_correct(self):
        with DB.Connect() as cursor:
            result = list(DB.get_all_entries_with_comments(cursor))
            self.assertEqual(2, len(result))
            for entry in result:
                self.assertEqual(3, len(list(entry.comments)))

    def test_if_tag_filter_works(self):
        with DB.Connect() as cursor:
            result = list(DB.get_all_entries_with_comments(cursor, tag='testtag2'))
            self.assertEqual(1, len(result))

class CountTagsTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 3
            self.entry.tags = ' differenttag '
            DB.insert_one(cursor, self.entry)

    def test_if_result_correct(self):
        tags = DB.count_tags()
        self.assertTrue(('testtag1', 2) in tags)
        self.assertTrue(('differenttag', 1) in tags)

    def test_if_tag_filter_works(self):
        tags = DB.count_tags(arg_tag='differenttag')
        self.assertEqual(1, len(tags))

class DeleteEntryTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            self.entry.entry_id = 1
            self.entry.type_ = 'entry_comment'
            DB.insert_one(cursor, self.entry)

    def test_if_everything_is_deleted(self):
        with DB.Connect() as cursor:
            DB.delete_entry(cursor, 1)
            e_ids = DB.get_ids(cursor, 'entry')
            c_ids = DB.get_ids(cursor, 'entry_comment')
        self.assertEqual(0, len(e_ids))
        self.assertEqual(0, len(c_ids))

class CountCommentsTest(Prepare):
    def setUp(self):
        super().setUp()
        DB.create_new('test')
        with DB.Connect() as cursor:
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 2
            self.entry.entry_id = 1
            self.entry.type_ = 'entry_comment'
            DB.insert_one(cursor, self.entry)
            self.entry.id_ = 3
            self.entry.entry_id = 1
            self.entry.type_ = 'entry_comment'
            DB.insert_one(cursor, self.entry)

    def test_if_correct_result(self):
        self.assertEqual(2, DB.count_comments(entry_id=1))

class DatabaseListTest(Prepare):
    def test_if_result_correct(self):
        DB.create_new('test1')
        DB.create_new('test2')
        result = database_list(self.path)
        self.assertTrue('test1.db' in result)
        self.assertTrue('test2.db' in result)

class SetAddedInfoTest(unittest.TestCase):
    def setUp(self):
        settings.ENTRIES_ADDED = 0
        settings.COMMENTS_ADDED = 0

    def test_when_entry_added(self):
        set_added_info('entry')
        self.assertEqual(1, settings.ENTRIES_ADDED)

    def test_when_comment_added(self):
        set_added_info('entry_comment')
        set_added_info('entry_comment')
        self.assertEqual(2, settings.COMMENTS_ADDED)


text:
.. _api:

API reference
=============

This part of the documentation covers the interfaces of kPAL's Python
library.

*k*-mer profiles
----------------

.. module:: kpal.klib

.. autoclass:: Profile
   :members:

*k*-mer profile distances
-------------------------

.. module:: kpal.kdistlib

.. autoclass:: ProfileDistance
   :members:

.. autofunction:: distance_matrix

Metrics
-------

.. automodule:: kpal.metrics
   :members:


text:
package tsi1

import (
	"bytes"
	"errors"
	"fmt"
	"regexp"
	"unsafe"

	"github.com/influxdata/influxdb/v2/pkg/lifecycle"
	"github.com/influxdata/influxdb/v2/tsdb"
	"github.com/influxdata/influxdb/v2/tsdb/seriesfile"
	"github.com/influxdata/influxql"
)

// FileSet represents a collection of files.
type FileSet struct {
	sfile        *seriesfile.SeriesFile
	sfileref     *lifecycle.Reference
	files        []File
	filesref     lifecycle.References
	manifestSize int64 // Size of the manifest file in bytes.
}

// NewFileSet returns a new instance of FileSet.
func NewFileSet(sfile *seriesfile.SeriesFile, files []File) (*FileSet, error) {
	// First try to acquire a reference to the series file.
	sfileref, err := sfile.Acquire()
	if err != nil {
		return nil, err
	}

	// Next, acquire references to all of the passed in files.
	filesref := make(lifecycle.References, 0, len(files))
	for _, f := range files {
		ref, err := f.Acquire()
		if err != nil {
			filesref.Release()
			sfileref.Release()
			return nil, err
		}
		filesref = append(filesref, ref)
	}

	return &FileSet{
		sfile:    sfile,
		sfileref: sfileref,
		files:    files,
		filesref: filesref,
	}, nil
}

// bytes estimates the memory footprint of this FileSet, in bytes.
func (fs *FileSet) bytes() int {
	var b int
	// Do not count SeriesFile because it belongs to the code that constructed this FileSet.
	for _, file := range fs.files {
		b += file.bytes()
	}
	b += int(unsafe.Sizeof(*fs))
	return b
}

func (fs *FileSet) SeriesFile() *seriesfile.SeriesFile { return fs.sfile }

// Release releases all resources on the file set.
func (fs *FileSet) Release() {
	fs.filesref.Release()
	fs.sfileref.Release()
}

// Duplicate returns a copy of the FileSet, acquiring another resource to the
// files and series file for the file set.
func (fs *FileSet) Duplicate() (*FileSet, error) {
	return NewFileSet(fs.sfile, fs.files)
}

// PrependLogFile returns a new file set with f added at the beginning.
// Filters do not need to be rebuilt because log files have no bloom filter.
func (fs *FileSet) PrependLogFile(f *LogFile) (*FileSet, error) {
	return NewFileSet(fs.sfile, append([]File{f}, fs.files...))
}

// Size returns the on-disk size of the FileSet.
func (fs *FileSet) Size() int64 {
	var total int64
	for _, f := range fs.files {
		total += f.Size()
	}
	return total + int64(fs.manifestSize)
}

// MustReplace swaps a list of files for a single file and returns a new file set.
// The caller should always guarantee that the files exist and are contiguous.
func (fs *FileSet) MustReplace(oldFiles []File, newFile File) (*FileSet, error) {
	assert(len(oldFiles) > 0, "cannot replace empty files")

	// Find index of first old file.
	var i int
	for ; i < len(fs.files); i++ {
		if fs.files[i] == oldFiles[0] {
			break
		} else if i == len(fs.files)-1 {
			return nil, errors.New("first replacement file not found")
		}
	}

	// Ensure all old files are contiguous.
	for j := range oldFiles {
		if fs.files[i+j] != oldFiles[j] {
			return nil, fmt.Errorf("cannot replace non-contiguous files: subset=%+v, fileset=%+v", Files(oldFiles).IDs(), Files(fs.files).IDs())
		}
	}

	// Copy to new fileset.
	other := make([]File, len(fs.files)-len(oldFiles)+1)
	copy(other[:i], fs.files[:i])
	other[i] = newFile
	copy(other[i+1:], fs.files[i+len(oldFiles):])

	// Build new fileset and rebuild changed filters.
	return NewFileSet(fs.sfile, other)
}

// MaxID returns the highest file identifier.
func (fs *FileSet) MaxID() int {
	var max int
	for _, f := range fs.files {
		if i := f.ID(); i > max {
			max = i
		}
	}
	return max
}

// Files returns all files in the set.
func (fs *FileSet) Files() []File {
	return fs.files
}

// LogFiles returns all log files from the file set.
func (fs *FileSet) LogFiles() []*LogFile {
	var a []*LogFile
	for _, f := range fs.files {
		if f, ok := f.(*LogFile); ok {
			a = append(a, f)
		}
	}
	return a
}

// IndexFiles returns all index files from the file set.
func (fs *FileSet) IndexFiles() []*IndexFile {
	var a []*IndexFile
	for _, f := range fs.files {
		if f, ok := f.(*IndexFile); ok {
			a = append(a, f)
		}
	}
	return a
}

// LastContiguousIndexFilesByLevel returns the last contiguous files by level.
// These can be used by the compaction scheduler.
func (fs *FileSet) LastContiguousIndexFilesByLevel(level int) []*IndexFile {
	if level == 0 {
		return nil
	}

	var a []*IndexFile
	for i := len(fs.files) - 1; i >= 0; i-- {
		f := fs.files[i]

		// Ignore files above level, stop on files below level.
		if level < f.Level() {
			continue
		} else if level > f.Level() {
			break
		}

		a = append([]*IndexFile{f.(*IndexFile)}, a...)
	}
	return a
}

// Measurement returns a measurement by name.
func (fs *FileSet) Measurement(name []byte) MeasurementElem {
	for _, f := range fs.files {
		if e := f.Measurement(name); e == nil {
			continue
		} else if e.Deleted() {
			return nil
		} else {
			return e
		}
	}
	return nil
}

// MeasurementIterator returns an iterator over all measurements in the index.
func (fs *FileSet) MeasurementIterator() MeasurementIterator {
	a := make([]MeasurementIterator, 0, len(fs.files))
	for _, f := range fs.files {
		itr := f.MeasurementIterator()
		if itr != nil {
			a = append(a, itr)
		}
	}
	return MergeMeasurementIterators(a...)
}

// TagKeyIterator returns an iterator over all tag keys for a measurement.
func (fs *FileSet) TagKeyIterator(name []byte) TagKeyIterator {
	a := make([]TagKeyIterator, 0, len(fs.files))
	for _, f := range fs.files {
		itr := f.TagKeyIterator(name)
		if itr != nil {
			a = append(a, itr)
		}
	}
	return MergeTagKeyIterators(a...)
}

// MeasurementSeriesIDIterator returns a series iterator for a measurement.
func (fs *FileSet) MeasurementSeriesIDIterator(name []byte) tsdb.SeriesIDIterator {
	a := make([]tsdb.SeriesIDIterator, 0, len(fs.files))
	for _, f := range fs.files {
		itr := f.MeasurementSeriesIDIterator(name)
		if itr != nil {
			a = append(a, itr)
		}
	}
	return tsdb.MergeSeriesIDIterators(a...)
}

// MeasurementTagKeysByExpr extracts the tag keys wanted by the expression.
func (fs *FileSet) MeasurementTagKeysByExpr(name []byte, expr influxql.Expr) (map[string]struct{}, error) {
	// Return all keys if no condition was passed in.
	if expr == nil {
		m := make(map[string]struct{})
		if itr := fs.TagKeyIterator(name); itr != nil {
			for e := itr.Next(); e != nil; e = itr.Next() {
				m[string(e.Key())] = struct{}{}
			}
		}
		return m, nil
	}

	switch e := expr.(type) {
	case *influxql.BinaryExpr:
		switch e.Op {
		case influxql.EQ, influxql.NEQ, influxql.EQREGEX, influxql.NEQREGEX:
			tag, ok := e.LHS.(*influxql.VarRef)
			if !ok {
				return nil, fmt.Errorf("left side of '%s' must be a tag key", e.Op.String())
			} else if tag.Val != "_tagKey" {
				return nil, nil
			}

			if influxql.IsRegexOp(e.Op) {
				re, ok := e.RHS.(*influxql.RegexLiteral)
				if !ok {
					return nil, fmt.Errorf("right side of '%s' must be a regular expression", e.Op.String())
				}
				return fs.tagKeysByFilter(name, e.Op, nil, re.Val), nil
			}

			s, ok := e.RHS.(*influxql.StringLiteral)
			if !ok {
				return nil, fmt.Errorf("right side of '%s' must be a tag value string", e.Op.String())
			}
			return fs.tagKeysByFilter(name, e.Op, []byte(s.Val), nil), nil

		case influxql.AND, influxql.OR:
			lhs, err := fs.MeasurementTagKeysByExpr(name, e.LHS)
			if err != nil {
				return nil, err
			}

			rhs, err := fs.MeasurementTagKeysByExpr(name, e.RHS)
			if err != nil {
				return nil, err
			}

			if lhs != nil && rhs != nil {
				if e.Op == influxql.OR {
					return unionStringSets(lhs, rhs), nil
				}
				return intersectStringSets(lhs, rhs), nil
			} else if lhs != nil {
				return lhs, nil
			} else if rhs != nil {
				return rhs, nil
			}
			return nil, nil
		default:
			return nil, fmt.Errorf("invalid operator")
		}

	case *influxql.ParenExpr:
		return fs.MeasurementTagKeysByExpr(name, e.Expr)
	}

	return nil, fmt.Errorf("%#v", expr)
}

// tagKeysByFilter will filter the tag keys for the measurement.
func (fs *FileSet) tagKeysByFilter(name []byte, op influxql.Token, val []byte, regex *regexp.Regexp) map[string]struct{} {
	ss := make(map[string]struct{})
	itr := fs.TagKeyIterator(name)
	if itr != nil {
		for e := itr.Next(); e != nil; e = itr.Next() {
			var matched bool
			switch op {
			case influxql.EQ:
				matched = bytes.Equal(e.Key(), val)
			case influxql.NEQ:
				matched = !bytes.Equal(e.Key(), val)
			case influxql.EQREGEX:
				matched = regex.Match(e.Key())
			case influxql.NEQREGEX:
				matched = !regex.Match(e.Key())
			}

			if !matched {
				continue
			}
			ss[string(e.Key())] = struct{}{}
		}
	}
	return ss
}

// TagKeySeriesIDIterator returns a series iterator for all values across a single key.
func (fs *FileSet) TagKeySeriesIDIterator(name, key []byte) (tsdb.SeriesIDIterator, error) {
	a := make([]tsdb.SeriesIDIterator, 0, len(fs.files))
	for _, f := range fs.files {
		itr, err := f.TagKeySeriesIDIterator(name, key)
		if err != nil {
			return nil, err
		} else if itr != nil {
			a = append(a, itr)
		}
	}
	return tsdb.MergeSeriesIDIterators(a...), nil
}

// HasTagKey returns true if the tag key exists.
func (fs *FileSet) HasTagKey(name, key []byte) bool {
	for _, f := range fs.files {
		if e := f.TagKey(name, key); e != nil {
			return !e.Deleted()
		}
	}
	return false
}

// HasTagValue returns true if the tag value exists.
func (fs *FileSet) HasTagValue(name, key, value []byte) bool {
	for _, f := range fs.files {
		if e := f.TagValue(name, key, value); e != nil {
			return !e.Deleted()
		}
	}
	return false
}

// TagValueIterator returns a value iterator for a tag key.
func (fs *FileSet) TagValueIterator(name, key []byte) TagValueIterator {
	a := make([]TagValueIterator, 0, len(fs.files))
	for _, f := range fs.files {
		itr := f.TagValueIterator(name, key)
		if itr != nil {
			a = append(a, itr)
		}
	}
	return MergeTagValueIterators(a...)
}

// TagValueSeriesIDIterator returns a series iterator for a single tag value.
func (fs *FileSet) TagValueSeriesIDIterator(name, key, value []byte) (tsdb.SeriesIDIterator, error) {
	ss := tsdb.NewSeriesIDSet()

	var ftss *tsdb.SeriesIDSet
	for i := len(fs.files) - 1; i >= 0; i-- {
		f := fs.files[i]

		// Remove tombstones set in previous file.
		if ftss != nil && ftss.Cardinality() > 0 {
			ss.RemoveSet(ftss)
		}

		// Fetch tag value series set for this file and merge into overall set.
		fss, err := f.TagValueSeriesIDSet(name, key, value)
		if err != nil {
			return nil, err
		} else if fss != nil {
			ss.Merge(fss)
		}

		// Fetch tombstone set to be processed on next file.
		if ftss, err = f.TombstoneSeriesIDSet(); err != nil {
			return nil, err
		}
	}
	return tsdb.NewSeriesIDSetIterator(ss), nil
}

// File represents a log or index file.
type File interface {
	Close() error
	Path() string

	ID() int
	Level() int

	Measurement(name []byte) MeasurementElem
	MeasurementIterator() MeasurementIterator
	MeasurementHasSeries(ss *tsdb.SeriesIDSet, name []byte) bool

	TagKey(name, key []byte) TagKeyElem
	TagKeyIterator(name []byte) TagKeyIterator

	TagValue(name, key, value []byte) TagValueElem
	TagValueIterator(name, key []byte) TagValueIterator

	// Series iteration.
	MeasurementSeriesIDIterator(name []byte) tsdb.SeriesIDIterator
	TagKeySeriesIDIterator(name, key []byte) (tsdb.SeriesIDIterator, error)
	TagValueSeriesIDSet(name, key, value []byte) (*tsdb.SeriesIDSet, error)

	// Bitmap series existence.
	SeriesIDSet() (*tsdb.SeriesIDSet, error)
	TombstoneSeriesIDSet() (*tsdb.SeriesIDSet, error)

	// Reference counting.
	Acquire() (*lifecycle.Reference, error)

	// Size of file on disk
	Size() int64

	// Estimated memory footprint
	bytes() int
}

type Files []File

func (a Files) IDs() []int {
	ids := make([]int, len(a))
	for i := range a {
		ids[i] = a[i].ID()
	}
	return ids
}

// fileSetSeriesIDIterator attaches a fileset to an iterator that is released on close.
type fileSetSeriesIDIterator struct {
	fs  *FileSet
	itr tsdb.SeriesIDIterator
}

func newFileSetSeriesIDIterator(fs *FileSet, itr tsdb.SeriesIDIterator) tsdb.SeriesIDIterator {
	if itr == nil {
		fs.Release()
		return nil
	}
	if itr, ok := itr.(tsdb.SeriesIDSetIterator); ok {
		return &fileSetSeriesIDSetIterator{fs: fs, itr: itr}
	}
	return &fileSetSeriesIDIterator{fs: fs, itr: itr}
}

func (itr *fileSetSeriesIDIterator) Next() (tsdb.SeriesIDElem, error) {
	return itr.itr.Next()
}

func (itr *fileSetSeriesIDIterator) Close() error {
	itr.fs.Release()
	return itr.itr.Close()
}

// fileSetSeriesIDSetIterator attaches a fileset to an iterator that is released on close.
type fileSetSeriesIDSetIterator struct {
	fs  *FileSet
	itr tsdb.SeriesIDSetIterator
}

func (itr *fileSetSeriesIDSetIterator) Next() (tsdb.SeriesIDElem, error) {
	return itr.itr.Next()
}

func (itr *fileSetSeriesIDSetIterator) Close() error {
	itr.fs.Release()
	return itr.itr.Close()
}

func (itr *fileSetSeriesIDSetIterator) SeriesIDSet() *tsdb.SeriesIDSet {
	return itr.itr.SeriesIDSet()
}

// fileSetMeasurementIterator attaches a fileset to an iterator that is released on close.
type fileSetMeasurementIterator struct {
	fs  *FileSet
	itr tsdb.MeasurementIterator
}

func newFileSetMeasurementIterator(fs *FileSet, itr tsdb.MeasurementIterator) tsdb.MeasurementIterator {
	if itr == nil {
		fs.Release()
		return nil
	}
	return &fileSetMeasurementIterator{fs: fs, itr: itr}
}

func (itr *fileSetMeasurementIterator) Next() ([]byte, error) {
	return itr.itr.Next()
}

func (itr *fileSetMeasurementIterator) Close() error {
	itr.fs.Release()
	return itr.itr.Close()
}

// fileSetTagKeyIterator attaches a fileset to an iterator that is released on close.
type fileSetTagKeyIterator struct {
	fs  *FileSet
	itr tsdb.TagKeyIterator
}

func newFileSetTagKeyIterator(fs *FileSet, itr tsdb.TagKeyIterator) tsdb.TagKeyIterator {
	if itr == nil {
		fs.Release()
		return nil
	}
	return &fileSetTagKeyIterator{fs: fs, itr: itr}
}

func (itr *fileSetTagKeyIterator) Next() ([]byte, error) {
	return itr.itr.Next()
}

func (itr *fileSetTagKeyIterator) Close() error {
	itr.fs.Release()
	return itr.itr.Close()
}

// fileSetTagValueIterator attaches a fileset to an iterator that is released on close.
type fileSetTagValueIterator struct {
	fs  *FileSet
	itr tsdb.TagValueIterator
}

func newFileSetTagValueIterator(fs *FileSet, itr tsdb.TagValueIterator) tsdb.TagValueIterator {
	if itr == nil {
		fs.Release()
		return nil
	}
	return &fileSetTagValueIterator{fs: fs, itr: itr}
}

func (itr *fileSetTagValueIterator) Next() ([]byte, error) {
	return itr.itr.Next()
}

func (itr *fileSetTagValueIterator) Close() error {
	itr.fs.Release()
	return itr.itr.Close()
}


text:
layout: post
title: SVM:multi-class SVM regularization
description: "SVM"
tags: [SVM, Stanford Machine Learning Notes]
categories: [Machine Learning, Machine Learning Basic,Research&Study]
---

### Review

For the ith sample $$(x_i,y_i)$$ in the training set, we have the following loss function:

$$L_i= \sum_{j≠y_i}max(0,w_j^T\cdot x_i−w_{y_i}^T\cdot x_i+Δ)$$

$$w_j^T\cdot x_i$$ is the score classifying $$x_i$$ to class j,and $$w_{y_i}^T\cdot x_i$$ is the score classifying correctly(classify to class $$y_i$$),$$\omega_i$$ is the $$j$$th row of $$W$$.

### Problem

Problem 1:

Considering the geometrical meaning of the weight vector $$\omega$$, it is easy to find out that $$\omega$$ is not unique, $$\omega$$ can change in a small area and result in the same $$L_i$$.

Problem 2:

It the values in $$\omega$$ is scaled, the loss computed will also be scaled by the same ratio. Considering a loss of 15, if we scale all the weights in $$\omega$$ by 2, the loss will be scaled to 30. But this kind of scaling is meaningless, it doesn't really represent the **loss**.

<!-- more -->

### L2 Regularization

The method to solve the above two problems is adding a penalty term into the loss function to penalize the weight matrix:

$$
R(W)=||W||^2=\sum{W\cdot W}
$$

The penalized loss function turns to be:

$$
L=\frac{1}{N}\sum_iL_i+\lambda R(\omega)
$$

We can understand the first term as "**data loss**" term, its physical meaning is the summation of all loss values when applying the weight matrix $$W$$ to all data samples.

The second the term is the so called "**regularization loss**", it represent the loss from the weight matrix $$W$$'s own structure, which means this term is only related to the weight matrix and **not related to the sample data**.

The equation after the full expansion is:

$$
L=\frac{1}{N}\sum_i \sum_{j≠y_i}max(0,w_j^T\cdot x_i−w_{y_i}^T\cdot x_i+Δ)+\lambda\sum{W\cdot W}
$$

Let's review the meaning of the symbols:

$$N$$ means there are N samples in the training set.

$$i$$ means the $$i$$th sample in all these $$N$$ samples.

$$y_i$$ means the class of the $$i$$th sample

$$j$$ means all the other classes except class $$y_i$$ in all K classes

$$\omega_j$$ means the $$j$$th row of the weight matrix $$W$$, the same for $$\omega_{y_i}$$

$$\Delta$$ is the confidential interval.

$$\lambda$$ is the regularization ratio.

**Usually we use** $$\frac{1}{2}\lambda\omega^2$$ **as the regularization term, because** when updating the $$\omega$$, we need to compute the gradient respect to $$\omega$$, by adding a $$\frac{1}{2}$$ in front the gradient of the regularization term will be $$\lambda\omega$$ instead of $$2\lambda\omega$$.

This regularization on $$\omega^2$$ is the so called L2 regularization, adding this to the loss function means for every update step, **the weights** $$\omega$$ **is decayed linearly**:

$$W=+-lambda*W$$

### Why regularization can solve the problems?

For problem 1, as we added the loss of the weight matrix itself, we can finally get an unique solution.

For problem 2, using the weight matrix's $$L_2$$ form as the penalty term, **means we want the weights in the weight matrix to be small**, which prevents the weights in $$W$$ from meaningless scaling.

**The smaller is the weight matrix's $$L_2$$ form, we know that the smaller will be the weights in $$W$$, what's more, it also contains another information: the weights in $$W$$ will distributed more equally:**

The summation of vector $$\omega_1:\{1,0,0,0\}$$  and the vector $$\omega_2:\{0.25,0.25,0.25,0.25\}$$ are the same, but their $$L_2$$ norms are quite different, $$\omega_1's\ L_2$$  norm is 1 but $$\omega_2's\ L_2$$  norm is 0.25. If both of them can all do the classification job correctly, we apparently will choose $$\omega_2$$, which has the smaller penalty, because its distribution is more equal, and **the more equally distributed weight vector means all the pixels in a sample image are considered.** $$\omega_1$$'s classification result may also be good, but it only consider the 1st pixel, which gets a weight of 1, the left 3 pixels get the weights of 0, which means they are not considered for classification.

But why we can still do the classification correctly without considering the other 3 pixels? **It means the training samples in our training set is very special: the 1st pixels in all the samples occasionally contains enough information for us to do the classification, but in the real case, all the 4 pixels are equally important. Now if we want to classify a new sample image whose 1st pixel doesn't contain enough information to classify the image, the $$\omega_1$$ can not classify this image anymore, this is the so-called "weak generalization ability" . If we choose $$\omega_2$$, it will still work, as it takes all the 4 pixels into consideration.**

### L1 Regularization

L1 regularization means adding $$\lambda\|\omega\|$$, it is also possible to combine L1 and L2 like:

$$
\lambda_1|\omega|+\lambda_2\omega^2
$$

which is called elastic net regularization. As we said that L2 regularization makes the $$\omega$$ decays linearly, for L1 regularization the weights will be reduced by a constant in every update step. Finally all the none-important weights will be disappeared and only the important weights will be left. Which means by using L1 regularization the system will be invariant to the "noisy" inputs. In comparison, using L2 will lead to diffuse, small weights.

### Max norm constraints

Just as its name described, we can also enforce a upper bound for $$\|\omega\|_2$$ of each neuron. In practice, after the normal update, we multiple all weights in $$\omega$$ by a constant $$k$$ to make sure that $$\|\omega\|_2<c$$, $$c$$ is the upper bound we set, which is usually of the order 3 or 4.


text:
#ifndef BITCOIN_QT_TEST_URITESTS_H
#define BITCOIN_QT_TEST_URITESTS_H

#include <QObject>
#include <QTest>

class URITests : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void uriTests();
};

#endif // BITCOIN_QT_TEST_URITESTS_H