<commit_before>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// (C) Copyright 2001-2002 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// jhyde, 26 July, 2001
*/

package mondrian.rolap;
import mondrian.olap.*;
import mondrian.xom.XOMUtil;
import mondrian.xom.Parser;
import mondrian.xom.XOMException;
import mondrian.xom.DOMWrapper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * A <code>RolapSchema</code> is a collection of {@link RolapCube}s and
 * shared {@link RolapDimension}s. It is shared betweeen {@link
 * RolapConnection}s. It caches {@link MemberReader}s, etc.
 *
 * @see RolapConnection
 * @author jhyde
 * @since 26 July, 2001
 * @version $Id$
 **/
public class RolapSchema implements Schema
{
	/**
	 * Internal use only.
	 */
	private RolapConnection internalConnection;
	/**
	 * Holds cubes in this schema.
	 */
	final HashMap mapNameToCube = new HashMap();
	/**
	 * Maps {@link String shared hierarchy name} to {@link MemberReader}.
	 * Shared between all statements which use this connection.
	 */
	private final HashMap mapSharedHierarchyToReader = new HashMap();
	/**
	 * Contains {@link HierarchyUsage}s for all hierarchy and fact-table
	 * combinations.
	 **/
	private final HashMap hierarchyUsages = new HashMap();

	/**
	 * Maps {@link String names of shared hierarchies} to {@link
	 * RolapHierarchy the canonical instance of those hierarchies}.
	 */
	final HashMap mapSharedHierarchyNameToHierarchy = new HashMap();

	/**
	 * Creates a {@link RolapSchema}. Use
	 * <code>RolapSchema.Pool.instance().get(catalogName)</code>.
	 */
	private RolapSchema(String catalogName, Util.PropertyList connectInfo) {
		internalConnection = new RolapConnection(connectInfo, this);
		try {
			java.net.URL url = new java.net.URL(catalogName);
			mondrian.xom.Parser xmlParser =
				mondrian.xom.XOMUtil.createDefaultParser();
			MondrianDef.Schema xmlSchema = new MondrianDef.Schema(
				xmlParser.parse(url));
			load(xmlSchema);
		} catch (mondrian.xom.XOMException e) {
			throw Util.newError(e, "while parsing catalog " + catalogName);
		} catch (java.io.IOException e) {
			throw Util.newError(e, "while parsing catalog " + catalogName);
		}
	}

	private void load(MondrianDef.Schema xmlSchema)
	{
		for (int i = 0; i < xmlSchema.cubes.length; i++) {
			MondrianDef.Cube xmlCube = xmlSchema.cubes[i];
			RolapCube cube = new RolapCube(this, xmlSchema, xmlCube);
			if (false && xmlCube.name.equals("Sales")) {
				cube = new RolapCube(this);
			}
			mapNameToCube.put(xmlCube.name, cube);
		}
		for (int i = 0; i < xmlSchema.virtualCubes.length; i++) {
			MondrianDef.VirtualCube xmlVirtualCube = xmlSchema.virtualCubes[i];
			RolapCube cube = new RolapCube(this, xmlSchema, xmlVirtualCube);
			mapNameToCube.put(xmlVirtualCube.name, cube);
		}
	}

	public Dimension createDimension(Cube cube, String xml) {
		MondrianDef.CubeDimension xmlDimension = null;
		try {
			final Parser xmlParser = XOMUtil.createDefaultParser();
			final DOMWrapper def = xmlParser.parse(xml);
			final String tagName = def.getTagName();
			if (tagName.equals("Dimension")) {
				xmlDimension = new MondrianDef.Dimension(def);
			} else if (tagName.equals("DimensionUsage")) {
				xmlDimension = new MondrianDef.DimensionUsage(def);
			} else {
				throw new XOMException("Got <" + tagName +
						"> when expecting <Dimension> or <DimensionUsage>");
			}
		} catch (XOMException e) {
			throw Util.newError(e, "Error while adding dimension to cube '" +
					cube + "' from XML [" + xml + "]");
		}
		return ((RolapCube) cube).createDimension(xmlDimension);
	}

	private static Pool pool = new Pool();
	/**
	 * A pool is a collection of schemas. Call <code>Pool.instance.{@link #get
	 * get(catalogName,jdbcConnectString)</code>.
	 */
	static class Pool {
		HashMap mapUrlToSchema = new HashMap();
		static Pool instance() {
			return pool;
		}
		synchronized RolapSchema get(
				String catalogName, String jdbcConnectString,
				Util.PropertyList connectInfo) {
			final String key = catalogName + ":" + jdbcConnectString;
			RolapSchema schema = (RolapSchema) mapUrlToSchema.get(key);
			if (schema == null) {
				schema = new RolapSchema(catalogName, connectInfo);
				mapUrlToSchema.put(key, schema);
				// Must create RolapConnection after we add to map, otherwise
				// we will loop.
				schema.internalConnection = new RolapConnection(connectInfo, schema);
			}
			return schema;
		}
	}

	public Cube lookupCube(String cube,boolean failIfNotFound)
	{
		Cube mdxCube = lookupCube(cube);
		if (mdxCube == null && failIfNotFound)
			throw Util.getRes().newMdxCubeNotFound(cube);
		return mdxCube;
	}

	/**
	 * Finds a cube called 'cube' in the current catalog, or return null if no
	 * cube exists.
	 */
	protected Cube lookupCube(String cube) {
		return (RolapCube) mapNameToCube.get(cube);<commit_msg>added remove() method to schema pool<commit_after>/*
// $Id$
// This software is subject to the terms of the Common Public License
// Agreement, available at the following URL:
// http://www.opensource.org/licenses/cpl.html.
// (C) Copyright 2001-2002 Kana Software, Inc. and others.
// All Rights Reserved.
// You must accept the terms of that agreement to use this software.
//
// jhyde, 26 July, 2001
*/

package mondrian.rolap;
import mondrian.olap.*;
import mondrian.xom.XOMUtil;
import mondrian.xom.Parser;
import mondrian.xom.XOMException;
import mondrian.xom.DOMWrapper;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

/**
 * A <code>RolapSchema</code> is a collection of {@link RolapCube}s and
 * shared {@link RolapDimension}s. It is shared betweeen {@link
 * RolapConnection}s. It caches {@link MemberReader}s, etc.
 *
 * @see RolapConnection
 * @author jhyde
 * @since 26 July, 2001
 * @version $Id$
 **/
public class RolapSchema implements Schema
{
	/**
	 * Internal use only.
	 */
	private RolapConnection internalConnection;
	/**
	 * Holds cubes in this schema.
	 */
	final HashMap mapNameToCube = new HashMap();
	/**
	 * Maps {@link String shared hierarchy name} to {@link MemberReader}.
	 * Shared between all statements which use this connection.
	 */
	private final HashMap mapSharedHierarchyToReader = new HashMap();
	/**
	 * Contains {@link HierarchyUsage}s for all hierarchy and fact-table
	 * combinations.
	 **/
	private final HashMap hierarchyUsages = new HashMap();

	/**
	 * Maps {@link String names of shared hierarchies} to {@link
	 * RolapHierarchy the canonical instance of those hierarchies}.
	 */
	final HashMap mapSharedHierarchyNameToHierarchy = new HashMap();

	/**
	 * Creates a {@link RolapSchema}. Use
	 * <code>RolapSchema.Pool.instance().get(catalogName)</code>.
	 */
	private RolapSchema(String catalogName, Util.PropertyList connectInfo) {
		internalConnection = new RolapConnection(connectInfo, this);
		try {
			java.net.URL url = new java.net.URL(catalogName);
			mondrian.xom.Parser xmlParser =
				mondrian.xom.XOMUtil.createDefaultParser();
			MondrianDef.Schema xmlSchema = new MondrianDef.Schema(
				xmlParser.parse(url));
			load(xmlSchema);
		} catch (mondrian.xom.XOMException e) {
			throw Util.newError(e, "while parsing catalog " + catalogName);
		} catch (java.io.IOException e) {
			throw Util.newError(e, "while parsing catalog " + catalogName);
		}
	}

	private void load(MondrianDef.Schema xmlSchema)
	{
		for (int i = 0; i < xmlSchema.cubes.length; i++) {
			MondrianDef.Cube xmlCube = xmlSchema.cubes[i];
			RolapCube cube = new RolapCube(this, xmlSchema, xmlCube);
			if (false && xmlCube.name.equals("Sales")) {
				cube = new RolapCube(this);
			}
			mapNameToCube.put(xmlCube.name, cube);
		}
		for (int i = 0; i < xmlSchema.virtualCubes.length; i++) {
			MondrianDef.VirtualCube xmlVirtualCube = xmlSchema.virtualCubes[i];
			RolapCube cube = new RolapCube(this, xmlSchema, xmlVirtualCube);
			mapNameToCube.put(xmlVirtualCube.name, cube);
		}
	}

	public Dimension createDimension(Cube cube, String xml) {
		MondrianDef.CubeDimension xmlDimension = null;
		try {
			final Parser xmlParser = XOMUtil.createDefaultParser();
			final DOMWrapper def = xmlParser.parse(xml);
			final String tagName = def.getTagName();
			if (tagName.equals("Dimension")) {
				xmlDimension = new MondrianDef.Dimension(def);
			} else if (tagName.equals("DimensionUsage")) {
				xmlDimension = new MondrianDef.DimensionUsage(def);
			} else {
				throw new XOMException("Got <" + tagName +
						"> when expecting <Dimension> or <DimensionUsage>");
			}
		} catch (XOMException e) {
			throw Util.newError(e, "Error while adding dimension to cube '" +
					cube + "' from XML [" + xml + "]");
		}
		return ((RolapCube) cube).createDimension(xmlDimension);
	}

	/**
	 * A pool is a collection of schemas. Call <code>Pool.instance.{@link #get
	 * get(catalogName,jdbcConnectString)</code>.
	 */
	public static class Pool {
		private static Pool pool = new Pool();

		private HashMap mapUrlToSchema = new HashMap();

		private Pool() {
		}

		public static Pool instance() {
			return pool;
		}

		synchronized RolapSchema get(
				String catalogName, String jdbcConnectString,
				Util.PropertyList connectInfo) {
			final String key = catalogName + ":" + jdbcConnectString;
			RolapSchema schema = (RolapSchema) mapUrlToSchema.get(key);
			if (schema == null) {
				schema = new RolapSchema(catalogName, connectInfo);
				mapUrlToSchema.put(key, schema);
				// Must create RolapConnection after we add to map, otherwise
				// we will loop.
				// no, this is redundant - its set in the ctor of RolapSchema
				// schema.internalConnection = new RolapConnection(connectInfo, schema);
			}
			return schema;
		}
		public synchronized void remove(String catalogName, String jdbcConnectString) {
			mapUrlToSchema.remove(catalogName + ":" + jdbcConnectString);
		}
	}

	public Cube lookupCube(String cube,boolean failIfNotFound)
	{
		Cube mdxCube = lookupCube(cube);
		if (mdxCube == null && failIfNotFound)
			throw Util.getRes().newMdxCubeNotFound(cube);
		return mdxCube;
	}

	/**
	 * Finds a cube called 'cube' in the current catalog, or return null if no
	 * cube exists.
	 */
	protected Cube lookupCube(String cube) {
		return (RolapCube) mapNameToCube.get(cube);
{'fraction_non_alphanumeric': 0.11715241280458671, 'fraction_numerical': 0.0042044911610129, 'mean_word_length': 3.9437883797827116, 'pattern_counts': {'":': 3, '<': 25, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 54}, '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': '1676860', 'n_tokens_mistral': 3696, 'n_tokens_neox': 3298, 'n_words': 921}