From: Paul H. <pha...@us...> - 2005-04-12 16:17:06
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27129/nhibernate/src/NHibernate/Cfg Modified Files: Binder.cs Configuration.cs Log Message: Refactored to minmize memory utilisation Index: Binder.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/Binder.cs,v retrieving revision 1.47 retrieving revision 1.48 diff -C2 -d -r1.47 -r1.48 *** Binder.cs 31 Mar 2005 12:41:27 -0000 1.47 --- Binder.cs 12 Apr 2005 16:16:26 -0000 1.48 *************** *** 19,23 **** --- 19,41 ---- private static XmlNamespaceManager nsmgr; + + // Make consts of all of these to avoid interning the strings at run-time private const string nsPrefix = "hbm"; + private const string nsKey = nsPrefix + ":key"; + private const string nsColumn = nsPrefix + ":column"; + private const string nsOneToMany = nsPrefix + ":one-to-many"; + private const string nsParam = nsPrefix + ":param"; + private const string nsIndex = nsPrefix + ":index"; + private const string nsGenerator = nsPrefix + ":generator"; + private const string nsCollectionId = nsPrefix + ":collection-id"; + private const string nsClass = nsPrefix + ":class"; + private const string nsSubclass = nsPrefix + ":subclass"; + private const string nsJoinedSubclass = nsPrefix + ":joined-subclass"; + private const string nsQuery = nsPrefix + ":query"; + private const string nsSqlQuery = nsPrefix + ":sql-query"; + private const string nsReturn = nsPrefix + ":return"; + private const string nsSynchronize = nsPrefix + ":synchronize"; + private const string nsImport = nsPrefix + ":import"; + internal static Dialect.Dialect dialect; *************** *** 34,70 **** return null; } ! // TODO: Make this stronger, assumes stuff about presence of culture, public key etc ! // Split off the assembly reference if it's present ! string[] parts = className.Split( ',' ); ! string name = parts[ 0 ]; ! if ( name.IndexOf( '.' ) == -1 && mapping.DefaultNamespace != null ) { ! // No namespace - use the default ! name = mapping.DefaultNamespace + "." + name; } ! ! string assm; ! if ( parts.Length == 1 ) { ! if ( mapping.DefaultAssembly == null ) { ! // No default, let it try and find it automatically ! assm = ""; } ! else { ! // No assembly - use the default ! assm = ", " + mapping.DefaultAssembly; } } - else - { - // Use the assembly reference provided - assm = ", " + parts[ 1 ]; - } - - // Rebuild the name - return name + assm; } --- 52,86 ---- return null; } + int commaPosn = className.IndexOf( ',' ); + int dotPosn = className.IndexOf( '.' ); ! // Check for namespace; ok to have a dot after the comma as it's part of the assembly name ! bool needNamespace = ( dotPosn == -1 || ( commaPosn > -1 && dotPosn > commaPosn ) ) && mapping.DefaultNamespace != null ; ! // Add if we don't have any commas and a default exists ! bool needAssembly = commaPosn == -1 && mapping.DefaultAssembly != null; ! ! if ( needNamespace == false && needAssembly == false ) { ! return className; } ! else { ! StringBuilder sb = new StringBuilder(); ! ! if ( needNamespace ) { ! sb.Append( mapping.DefaultNamespace ); ! sb.Append( "." ); } ! ! sb.Append( className ); ! ! if ( needAssembly ) { ! sb.Append( ", "); ! sb.Append( mapping.DefaultAssembly ); } + return sb.ToString(); } } *************** *** 226,230 **** log.Info("Mapping joined-subclass: " + model.Name + " -> " + model.Table.Name ); ! XmlNode keyNode = node.SelectSingleNode(nsPrefix + ":key", nsmgr); SimpleValue key = new SimpleValue(mytable); model.Key = key; --- 242,246 ---- log.Info("Mapping joined-subclass: " + model.Name + " -> " + model.Table.Name ); ! XmlNode keyNode = node.SelectSingleNode( nsKey, nsmgr ); SimpleValue key = new SimpleValue(mytable); model.Key = key; *************** *** 387,391 **** { int count=0; ! foreach(XmlNode subnode in node.SelectNodes(nsPrefix + ":column", nsmgr)) { Table table = model.Table; --- 403,407 ---- { int count=0; ! foreach(XmlNode subnode in node.SelectNodes( nsColumn, nsmgr )) { Table table = model.Table; *************** *** 596,600 **** InitOuterJoinFetchSetting( node, model ); ! XmlNode oneToManyNode = node.SelectSingleNode(nsPrefix + ":one-to-many", nsmgr); if ( oneToManyNode!=null ) { --- 612,616 ---- InitOuterJoinFetchSetting( node, model ); ! XmlNode oneToManyNode = node.SelectSingleNode( nsOneToMany, nsmgr ); if ( oneToManyNode!=null ) { *************** *** 1070,1078 **** } ! private static void MakeIdentifier(XmlNode node, SimpleValue model, Mappings mappings) { //GENERATOR ! XmlNode subnode = node.SelectSingleNode(nsPrefix + ":generator", nsmgr); if ( subnode!=null ) { --- 1086,1094 ---- } ! private static void MakeIdentifier( XmlNode node, SimpleValue model, Mappings mappings ) { //GENERATOR ! XmlNode subnode = node.SelectSingleNode( nsGenerator, nsmgr ); if ( subnode!=null ) { *************** *** 1097,1102 **** } ! //foreach(XmlNode childNode in subnode.SelectNodes("param")) { ! foreach(XmlNode childNode in subnode.SelectNodes(nsPrefix + ":param", nsmgr)) { parms.Add( --- 1113,1117 ---- } ! foreach( XmlNode childNode in subnode.SelectNodes( nsParam, nsmgr )) { parms.Add( *************** *** 1204,1208 **** BindCollectionSecondPass(node, model, classes, mappings); ! XmlNode subnode = node.SelectSingleNode(nsPrefix + ":index", nsmgr); IntegerValue iv = new IntegerValue( model.CollectionTable ); BindIntegerValue( subnode, iv, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany, mappings ); --- 1219,1223 ---- BindCollectionSecondPass(node, model, classes, mappings); ! XmlNode subnode = node.SelectSingleNode( nsIndex, nsmgr); IntegerValue iv = new IntegerValue( model.CollectionTable ); BindIntegerValue( subnode, iv, IndexedCollection.DefaultIndexColumnName, model.IsOneToMany, mappings ); *************** *** 1214,1218 **** BindCollectionSecondPass(node, model, persitentClasses, mappings); ! XmlNode subnode = node.SelectSingleNode( nsPrefix + ":collection-id", nsmgr ); SimpleValue id = new SimpleValue( model.CollectionTable ); BindSimpleValue( subnode, id, false, IdentifierCollection.DefaultIdentifierColumnName, mappings); --- 1229,1233 ---- BindCollectionSecondPass(node, model, persitentClasses, mappings); ! XmlNode subnode = node.SelectSingleNode( nsCollectionId, nsmgr ); SimpleValue id = new SimpleValue( model.CollectionTable ); BindSimpleValue( subnode, id, false, IdentifierCollection.DefaultIdentifierColumnName, mappings); *************** *** 1391,1395 **** nsmgr.AddNamespace( nsPrefix, Configuration.MappingSchemaXMLNS ); ! foreach(XmlNode n in hmNode.SelectNodes( nsPrefix + ":class", nsmgr ) ) { RootClass rootclass = new RootClass(); --- 1406,1410 ---- nsmgr.AddNamespace( nsPrefix, Configuration.MappingSchemaXMLNS ); ! foreach(XmlNode n in hmNode.SelectNodes( nsClass, nsmgr ) ) { RootClass rootclass = new RootClass(); *************** *** 1398,1402 **** } ! foreach(XmlNode n in hmNode.SelectNodes( nsPrefix + ":subclass", nsmgr ) ) { PersistentClass superModel = GetSuperclass( model, n ); --- 1413,1417 ---- } ! foreach(XmlNode n in hmNode.SelectNodes( nsSubclass, nsmgr ) ) { PersistentClass superModel = GetSuperclass( model, n ); *************** *** 1404,1408 **** } ! foreach(XmlNode n in hmNode.SelectNodes( nsPrefix + ":joined-subclass", nsmgr ) ) { PersistentClass superModel = GetSuperclass(model, n); --- 1419,1423 ---- } ! foreach(XmlNode n in hmNode.SelectNodes( nsJoinedSubclass, nsmgr ) ) { PersistentClass superModel = GetSuperclass(model, n); *************** *** 1410,1414 **** } ! foreach(XmlNode n in hmNode.SelectNodes( nsPrefix + ":query", nsmgr ) ) { string qname = n.Attributes["name"].Value; --- 1425,1429 ---- } ! foreach(XmlNode n in hmNode.SelectNodes( nsQuery, nsmgr ) ) { string qname = n.Attributes["name"].Value; *************** *** 1418,1427 **** } ! foreach(XmlNode n in hmNode.SelectNodes( nsPrefix + ":sql-query", nsmgr ) ) { string qname = n.Attributes["name"].Value; NamedSQLQuery namedQuery = new NamedSQLQuery( n.InnerText ); ! foreach(XmlNode returns in n.SelectNodes( nsPrefix + ":return", nsmgr ) ) { string alias = returns.Attributes["alias"].Value; --- 1433,1442 ---- } ! foreach(XmlNode n in hmNode.SelectNodes( nsSqlQuery, nsmgr ) ) { string qname = n.Attributes["name"].Value; NamedSQLQuery namedQuery = new NamedSQLQuery( n.InnerText ); ! foreach(XmlNode returns in n.SelectNodes( nsReturn, nsmgr ) ) { string alias = returns.Attributes["alias"].Value; *************** *** 1439,1443 **** } ! foreach( XmlNode table in n.SelectNodes( nsPrefix + ":synchronise", nsmgr ) ) { namedQuery.AddSynchronizedTable( table.Attributes["table"].Value ); --- 1454,1458 ---- } ! foreach( XmlNode table in n.SelectNodes( nsSynchronize, nsmgr ) ) { namedQuery.AddSynchronizedTable( table.Attributes["table"].Value ); *************** *** 1448,1452 **** } ! foreach(XmlNode n in hmNode.SelectNodes(nsPrefix + ":import", nsmgr) ) { string className = FullClassName( n.Attributes["class"].Value, model ); --- 1463,1467 ---- } ! foreach(XmlNode n in hmNode.SelectNodes( nsImport, nsmgr) ) { string className = FullClassName( n.Attributes["class"].Value, model ); Index: Configuration.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Cfg/Configuration.cs,v retrieving revision 1.37 retrieving revision 1.38 diff -C2 -d -r1.37 -r1.38 *** Configuration.cs 31 Mar 2005 12:41:27 -0000 1.37 --- Configuration.cs 12 Apr 2005 16:16:26 -0000 1.38 *************** *** 30,51 **** /// </para> /// </remarks> ! public class Configuration : IMapping { ! private Hashtable classes = new Hashtable(); ! private Hashtable imports = new Hashtable(); ! private Hashtable collections = new Hashtable(); ! private Hashtable tables = new Hashtable(); ! private Hashtable namedQueries = new Hashtable(); ! private Hashtable namedSqlQueries = new Hashtable(); ! private ArrayList secondPasses = new ArrayList(); ! private ArrayList propertyReferences = new ArrayList(); ! private IInterceptor interceptor = emptyInterceptor; ! private IDictionary properties = Environment.Properties; ! private IDictionary caches = new Hashtable(); private INamingStrategy namingStrategy = DefaultNamingStrategy.Instance; ! private XmlSchema mappingSchema; ! private XmlSchema cfgSchema; private static readonly ILog log = LogManager.GetLogger( typeof( Configuration ) ); --- 30,51 ---- /// </para> /// </remarks> ! public sealed class Configuration : IMapping { ! private Hashtable classes; ! private Hashtable imports; ! private Hashtable collections; ! private Hashtable tables; ! private Hashtable namedQueries; ! private Hashtable namedSqlQueries; ! private ArrayList secondPasses; ! private ArrayList propertyReferences; ! private IInterceptor interceptor; ! private IDictionary properties; ! private IDictionary caches; private INamingStrategy namingStrategy = DefaultNamingStrategy.Instance; ! private XmlSchemaCollection mappingSchemaCollection; ! private XmlSchemaCollection cfgSchemaCollection; private static readonly ILog log = LogManager.GetLogger( typeof( Configuration ) ); *************** *** 100,111 **** private const string CfgSchemaResource = "NHibernate.nhibernate-configuration-2.0.xsd"; private const string CfgNamespacePrefix = "cfg"; - private static XmlNamespaceManager CfgNamespaceMgr; /// <summary> /// Clear the internal state of the <see cref="Configuration"/> object. /// </summary> ! protected void Reset() { classes = new Hashtable(); collections = new Hashtable(); tables = new Hashtable(); --- 100,112 ---- private const string CfgSchemaResource = "NHibernate.nhibernate-configuration-2.0.xsd"; private const string CfgNamespacePrefix = "cfg"; /// <summary> /// Clear the internal state of the <see cref="Configuration"/> object. /// </summary> ! //protected void Reset() ! private void Reset() { classes = new Hashtable(); + imports = new Hashtable(); collections = new Hashtable(); tables = new Hashtable(); *************** *** 113,118 **** --- 114,121 ---- namedSqlQueries = new Hashtable(); secondPasses = new ArrayList(); + propertyReferences = new ArrayList(); interceptor = emptyInterceptor; properties = Environment.Properties; + caches = new Hashtable(); mapping = new Mapping( classes ); } *************** *** 124,129 **** { Reset(); ! mappingSchema = XmlSchema.Read( Assembly.GetExecutingAssembly().GetManifestResourceStream( MappingSchemaResource ), null ); ! cfgSchema = XmlSchema.Read( Assembly.GetExecutingAssembly().GetManifestResourceStream( CfgSchemaResource ), null ); } --- 127,162 ---- { Reset(); ! } ! ! /// <summary></summary> ! /// <remarks>Allocate on first use as we are expensive in time/space</remarks> ! private XmlSchemaCollection MappingSchemaCollection ! { ! get ! { ! if ( mappingSchemaCollection == null ) ! { ! mappingSchemaCollection = new XmlSchemaCollection(); ! mappingSchemaCollection.Add( XmlSchema.Read( Assembly.GetExecutingAssembly().GetManifestResourceStream( MappingSchemaResource ), null ) ); ! } ! return mappingSchemaCollection; ! } ! set { mappingSchemaCollection = value; } ! } ! ! /// <summary></summary> ! /// <remarks>Allocate on first use as we are expensive in time/space</remarks> ! private XmlSchemaCollection CfgSchemaCollection ! { ! get ! { ! if ( cfgSchemaCollection == null ) ! { ! cfgSchemaCollection = new XmlSchemaCollection(); ! cfgSchemaCollection.Add( XmlSchema.Read( Assembly.GetExecutingAssembly().GetManifestResourceStream( CfgSchemaResource ), null ) ); ! } ! return cfgSchemaCollection; ! } ! set { cfgSchemaCollection = value; } } *************** *** 271,275 **** /// </p> /// </remarks> ! public Configuration AddAssembly(Assembly assembly, bool skipOrdering) { IList resources = null; --- 304,308 ---- /// </p> /// </remarks> ! public Configuration AddAssembly( Assembly assembly, bool skipOrdering ) { IList resources = null; *************** *** 474,478 **** /// <summary> ! /// Adds the Mappings in the XmlReader after validating it against the /// nhibernate-mapping-2.0 schema. /// </summary> --- 507,511 ---- /// <summary> ! /// Adds the Mappings in the XmlReader after validating optionally it against the /// nhibernate-mapping-2.0 schema. /// </summary> *************** *** 481,493 **** public Configuration AddXmlReader( XmlReader hbmReader ) { - XmlValidatingReader validatingReader = null; try { validatingReader = new XmlValidatingReader( hbmReader ); validatingReader.ValidationType = ValidationType.Schema; ! validatingReader.Schemas.Add( mappingSchema ); - XmlDocument hbmDocument = new XmlDocument(); hbmDocument.Load( validatingReader ); Add( hbmDocument ); --- 514,526 ---- public Configuration AddXmlReader( XmlReader hbmReader ) { XmlValidatingReader validatingReader = null; try { + XmlDocument hbmDocument = new XmlDocument(); validatingReader = new XmlValidatingReader( hbmReader ); + validatingReader.ValidationEventHandler += new ValidationEventHandler( ValidationHandler ); validatingReader.ValidationType = ValidationType.Schema; ! validatingReader.Schemas.Add( MappingSchemaCollection ); hbmDocument.Load( validatingReader ); Add( hbmDocument ); *************** *** 504,507 **** --- 537,545 ---- } + private static void ValidationHandler( object o, ValidationEventArgs args ) + { + throw args.Exception; + } + /// <summary> /// Adds the Mappings from a <c>String</c> *************** *** 819,822 **** --- 857,864 ---- ConfigureCaches( settings ); + // Ok, don't need schemas anymore, so free them + MappingSchemaCollection = null; + CfgSchemaCollection = null; + return new SessionFactoryImpl( this, settings ); } *************** *** 839,843 **** /// </summary> /// <returns>A <see cref="Settings"/> object initialized from the settings properties.</returns> ! protected Settings BuildSettings() { return SettingsFactory.BuildSettings( properties ); --- 881,886 ---- /// </summary> /// <returns>A <see cref="Settings"/> object initialized from the settings properties.</returns> ! //protected Settings BuildSettings() ! private Settings BuildSettings() { return SettingsFactory.BuildSettings( properties ); *************** *** 901,904 **** --- 944,958 ---- /// <summary> + /// Set a custom naming strategy + /// </summary> + /// <param name="namingStrategy">the NamingStrategy to set</param> + /// <returns></returns> + public Configuration SetNamingStrategy( INamingStrategy namingStrategy ) + { + this.namingStrategy = namingStrategy; + return this; + } + + /// <summary> /// Gets the value of the configuration property. /// </summary> *************** *** 910,914 **** } ! private void AddProperties( XmlNode parent ) { foreach( XmlNode node in parent.SelectNodes( CfgNamespacePrefix + ":property", CfgNamespaceMgr ) ) --- 964,968 ---- } ! private void AddProperties( XmlNode parent, XmlNamespaceManager CfgNamespaceMgr ) { foreach( XmlNode node in parent.SelectNodes( CfgNamespacePrefix + ":property", CfgNamespaceMgr ) ) *************** *** 963,977 **** /// <summary> - /// Set a custom naming strategy - /// </summary> - /// <param name="namingStrategy">the NamingStrategy to set</param> - /// <returns></returns> - public Configuration SetNamingStrategy( INamingStrategy namingStrategy ) - { - this.namingStrategy = namingStrategy; - return this; - } - - /// <summary> /// Configure NHibernate using a resource contained in an Assembly. /// </summary> --- 1017,1020 ---- *************** *** 1027,1030 **** --- 1070,1074 ---- XmlDocument doc = new XmlDocument(); XmlValidatingReader validatingReader = null; + XmlNamespaceManager cfgNamespaceMgr; try *************** *** 1032,1044 **** validatingReader = new XmlValidatingReader( reader ); validatingReader.ValidationType = ValidationType.Schema; ! validatingReader.Schemas.Add( cfgSchema ); doc.Load( validatingReader ); ! CfgNamespaceMgr = new XmlNamespaceManager( doc.NameTable ); // note that the prefix has absolutely nothing to do with what the user // selects as their prefix in the document. It is the prefix we use to // build the XPath and the nsmgr takes care of translating our prefix into // the user defined prefix... ! CfgNamespaceMgr.AddNamespace( CfgNamespacePrefix, Configuration.CfgSchemaXMLNS ); } catch( Exception e ) --- 1076,1088 ---- validatingReader = new XmlValidatingReader( reader ); validatingReader.ValidationType = ValidationType.Schema; ! validatingReader.Schemas.Add( CfgSchemaCollection ); doc.Load( validatingReader ); ! cfgNamespaceMgr = new XmlNamespaceManager( doc.NameTable ); // note that the prefix has absolutely nothing to do with what the user // selects as their prefix in the document. It is the prefix we use to // build the XPath and the nsmgr takes care of translating our prefix into // the user defined prefix... ! cfgNamespaceMgr.AddNamespace( CfgNamespacePrefix, Configuration.CfgSchemaXMLNS ); } catch( Exception e ) *************** *** 1055,1059 **** } ! XmlNode sfNode = doc.DocumentElement.SelectSingleNode( "//" + CfgNamespacePrefix + ":session-factory", CfgNamespaceMgr ); XmlAttribute name = sfNode.Attributes[ "name" ]; if( name != null ) --- 1099,1103 ---- } ! XmlNode sfNode = doc.DocumentElement.SelectSingleNode( "//" + CfgNamespacePrefix + ":session-factory", cfgNamespaceMgr ); XmlAttribute name = sfNode.Attributes[ "name" ]; if( name != null ) *************** *** 1061,1065 **** properties[ Environment.SessionFactoryName ] = name.Value; } ! AddProperties( sfNode ); foreach( XmlNode mapElement in sfNode.ChildNodes ) --- 1105,1109 ---- properties[ Environment.SessionFactoryName ] = name.Value; } ! AddProperties( sfNode, cfgNamespaceMgr ); foreach( XmlNode mapElement in sfNode.ChildNodes ) *************** *** 1106,1111 **** /// </summary> /// <param name="settings"></param> ! protected void ConfigureCaches( Settings settings ) ! { log.Info( "instantiating and configuring caches" ); --- 1150,1156 ---- /// </summary> /// <param name="settings"></param> ! //protected void ConfigureCaches( Settings settings ) ! private void ConfigureCaches( Settings settings ) ! { log.Info( "instantiating and configuring caches" ); |