Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / ndp / fx / src / DataEntity / System / Data / Map / ViewGeneration / QueryRewriting / FragmentQuery.cs / 2 / FragmentQuery.cs
using System; using System.Diagnostics; using System.Collections.Generic; using System.Text; using System.Data.Common.Utils; using System.Data.Common.Utils.Boolean; using System.Data.Mapping.ViewGeneration.Structures; using System.Data.Metadata.Edm; using System.Linq; using System.Globalization; namespace System.Data.Mapping.ViewGeneration.QueryRewriting { //using QueryKnowledgeBase = KnowledgeBase>; internal class FragmentQuery : ITileQuery { private BoolExpression m_fromVariable; // optional private string m_label; // optional private HashSet m_attributes; private BoolExpression m_condition; public HashSet Attributes { get { return m_attributes; } } public BoolExpression Condition { get { return m_condition; } } public static FragmentQuery Create(BoolExpression fromVariable, CellQuery cellQuery) { BoolExpression whereClause = cellQuery.WhereClause; whereClause = whereClause.MakeCopy(); whereClause.ExpensiveSimplify(); return new FragmentQuery(null /*label*/, fromVariable, new HashSet (cellQuery.GetProjectedMembers()), whereClause); } public static FragmentQuery Create(string label, RoleBoolean roleBoolean, CellQuery cellQuery) { BoolExpression whereClause = cellQuery.WhereClause.Create(roleBoolean); whereClause = BoolExpression.CreateAnd(whereClause, cellQuery.WhereClause); //return new FragmentQuery(label, null /* fromVariable */, new HashSet (cellQuery.GetProjectedMembers()), whereClause); // don't need any attributes whereClause = whereClause.MakeCopy(); whereClause.ExpensiveSimplify(); return new FragmentQuery(label, null /* fromVariable */, new HashSet (), whereClause); } public static FragmentQuery Create(IEnumerable attrs, BoolExpression whereClause) { return new FragmentQuery(null /* no name */, null /* no fromVariable*/, attrs, whereClause); } public static FragmentQuery Create(BoolExpression whereClause) { return new FragmentQuery(null /* no name */, null /* no fromVariable*/, new MemberPath[] {}, whereClause); } internal FragmentQuery(string label, BoolExpression fromVariable, IEnumerable attrs, BoolExpression condition) { m_label = label; m_fromVariable = fromVariable; m_condition = condition; m_attributes = new HashSet (attrs); } public BoolExpression FromVariable { get { return m_fromVariable; } } public string Description { get { string label = m_label; if (label == null && m_fromVariable != null) { label = m_fromVariable.ToString(); } return label; } } public override string ToString() { // attributes StringBuilder b = new StringBuilder(); foreach (MemberPath value in this.Attributes) { if (b.Length > 0) { b.Append(','); } b.Append(value.ToString()); } if (Description != null && Description != b.ToString()) { return String.Format(CultureInfo.InvariantCulture, "{0}: [{1} where {2}]", Description, b, this.Condition); } else { return String.Format(CultureInfo.InvariantCulture, "[{0} where {1}]", b, this.Condition); } } #region Static methods // creates a condition member=value internal static BoolExpression CreateMemberCondition(MemberPath path, CellConstant domainValue, MemberDomainMap domainMap, MetadataWorkspace workspace) { if (domainValue is TypeConstant) { return BoolExpression.CreateLiteral(new OneOfTypeConst(CreateSlot(path, workspace), new CellConstantDomain(domainValue, domainMap.GetDomain(path))), domainMap); } else { return BoolExpression.CreateLiteral(new OneOfScalarConst(CreateSlot(path, workspace), new CellConstantDomain(domainValue, domainMap.GetDomain(path))), domainMap); } } // creates a fake join tree slot so we can construct a BoolExpression [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Data.Mapping.ViewGeneration.Structures.ExtentJoinTreeNode")] internal static JoinTreeSlot CreateSlot(MemberPath path, MetadataWorkspace workspace) { IList members = path.Members; if (members.Count > 0) { MemberJoinTreeNode leaf = new MemberJoinTreeNode(members[members.Count - 1], false, new MemberJoinTreeNode[] { }, workspace); MemberJoinTreeNode node = leaf; for (int i = members.Count - 2; i >= 0; i--) { node = new MemberJoinTreeNode(members[i], false, new MemberJoinTreeNode[] { node }, workspace); } ExtentJoinTreeNode extentNode = new ExtentJoinTreeNode(path.Extent, new MemberJoinTreeNode[] { node }, workspace); return new JoinTreeSlot(leaf); } else { ExtentJoinTreeNode extentNode = new ExtentJoinTreeNode(path.Extent, new MemberJoinTreeNode[] { }, workspace); return new JoinTreeSlot(extentNode); } } internal static IEqualityComparer GetEqualityComparer(FragmentQueryProcessor qp) { return new FragmentQueryEqualityComparer(qp); } //internal static IComparer GetComparer(IEnumerable attributes) //{ // return new FragmentQueryComparer(attributes); //} #endregion #region Equality Comparer // Two queries are "equal" if they project the same set of attributes // and their WHERE clauses are equivalent private class FragmentQueryEqualityComparer : IEqualityComparer { FragmentQueryProcessor _qp; internal FragmentQueryEqualityComparer(FragmentQueryProcessor qp) { _qp = qp; } #region IEqualityComparer Members public bool Equals(FragmentQuery x, FragmentQuery y) { if (!x.Attributes.SetEquals(y.Attributes)) { return false; } return _qp.IsEquivalentTo(x, y); } // Hashing a bit naive: it exploits syntactic properties, // i.e., some semantically equivalent queries may produce different hash codes // But that's fine for usage scenarios in QueryRewriter.cs public int GetHashCode(FragmentQuery q) { int attrHashCode = 0; foreach (MemberPath member in q.Attributes) { attrHashCode ^= MemberPath.EqualityComparer.GetHashCode(member); } int varHashCode = 0; int constHashCode = 0; foreach (OneOfConst oneOf in q.Condition.OneOfConstVariables) { varHashCode ^= MemberPath.EqualityComparer.GetHashCode(oneOf.Slot.MemberPath); foreach (CellConstant constant in oneOf.Values.Values) { constHashCode ^= CellConstant.EqualityComparer.GetHashCode(constant); } } return attrHashCode * 13 + varHashCode * 7 + constHashCode; } #endregion } #endregion //#region Comparer //// queries with largest attribute overlap go first //private class FragmentQueryComparer : IComparer //{ // private HashSet _attributes; // internal FragmentQueryComparer(IEnumerable attributes) // { // _attributes = new HashSet (attributes); // } // #region IComparer Members // public int Compare(FragmentQuery x, FragmentQuery y) // { // HashSet xAttr = new HashSet (x.Attributes); // HashSet yAttr = new HashSet (y.Attributes); // xAttr.IntersectWith(_attributes); // yAttr.IntersectWith(_attributes); // if (xAttr.Count > yAttr.Count) // { // return -1; // } // else if (xAttr.Count < yAttr.Count) // { // return 1; // } // return 0; // } // #endregion //} //#endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved. using System; using System.Diagnostics; using System.Collections.Generic; using System.Text; using System.Data.Common.Utils; using System.Data.Common.Utils.Boolean; using System.Data.Mapping.ViewGeneration.Structures; using System.Data.Metadata.Edm; using System.Linq; using System.Globalization; namespace System.Data.Mapping.ViewGeneration.QueryRewriting { //using QueryKnowledgeBase = KnowledgeBase >; internal class FragmentQuery : ITileQuery { private BoolExpression m_fromVariable; // optional private string m_label; // optional private HashSet m_attributes; private BoolExpression m_condition; public HashSet Attributes { get { return m_attributes; } } public BoolExpression Condition { get { return m_condition; } } public static FragmentQuery Create(BoolExpression fromVariable, CellQuery cellQuery) { BoolExpression whereClause = cellQuery.WhereClause; whereClause = whereClause.MakeCopy(); whereClause.ExpensiveSimplify(); return new FragmentQuery(null /*label*/, fromVariable, new HashSet (cellQuery.GetProjectedMembers()), whereClause); } public static FragmentQuery Create(string label, RoleBoolean roleBoolean, CellQuery cellQuery) { BoolExpression whereClause = cellQuery.WhereClause.Create(roleBoolean); whereClause = BoolExpression.CreateAnd(whereClause, cellQuery.WhereClause); //return new FragmentQuery(label, null /* fromVariable */, new HashSet (cellQuery.GetProjectedMembers()), whereClause); // don't need any attributes whereClause = whereClause.MakeCopy(); whereClause.ExpensiveSimplify(); return new FragmentQuery(label, null /* fromVariable */, new HashSet (), whereClause); } public static FragmentQuery Create(IEnumerable attrs, BoolExpression whereClause) { return new FragmentQuery(null /* no name */, null /* no fromVariable*/, attrs, whereClause); } public static FragmentQuery Create(BoolExpression whereClause) { return new FragmentQuery(null /* no name */, null /* no fromVariable*/, new MemberPath[] {}, whereClause); } internal FragmentQuery(string label, BoolExpression fromVariable, IEnumerable attrs, BoolExpression condition) { m_label = label; m_fromVariable = fromVariable; m_condition = condition; m_attributes = new HashSet (attrs); } public BoolExpression FromVariable { get { return m_fromVariable; } } public string Description { get { string label = m_label; if (label == null && m_fromVariable != null) { label = m_fromVariable.ToString(); } return label; } } public override string ToString() { // attributes StringBuilder b = new StringBuilder(); foreach (MemberPath value in this.Attributes) { if (b.Length > 0) { b.Append(','); } b.Append(value.ToString()); } if (Description != null && Description != b.ToString()) { return String.Format(CultureInfo.InvariantCulture, "{0}: [{1} where {2}]", Description, b, this.Condition); } else { return String.Format(CultureInfo.InvariantCulture, "[{0} where {1}]", b, this.Condition); } } #region Static methods // creates a condition member=value internal static BoolExpression CreateMemberCondition(MemberPath path, CellConstant domainValue, MemberDomainMap domainMap, MetadataWorkspace workspace) { if (domainValue is TypeConstant) { return BoolExpression.CreateLiteral(new OneOfTypeConst(CreateSlot(path, workspace), new CellConstantDomain(domainValue, domainMap.GetDomain(path))), domainMap); } else { return BoolExpression.CreateLiteral(new OneOfScalarConst(CreateSlot(path, workspace), new CellConstantDomain(domainValue, domainMap.GetDomain(path))), domainMap); } } // creates a fake join tree slot so we can construct a BoolExpression [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "System.Data.Mapping.ViewGeneration.Structures.ExtentJoinTreeNode")] internal static JoinTreeSlot CreateSlot(MemberPath path, MetadataWorkspace workspace) { IList members = path.Members; if (members.Count > 0) { MemberJoinTreeNode leaf = new MemberJoinTreeNode(members[members.Count - 1], false, new MemberJoinTreeNode[] { }, workspace); MemberJoinTreeNode node = leaf; for (int i = members.Count - 2; i >= 0; i--) { node = new MemberJoinTreeNode(members[i], false, new MemberJoinTreeNode[] { node }, workspace); } ExtentJoinTreeNode extentNode = new ExtentJoinTreeNode(path.Extent, new MemberJoinTreeNode[] { node }, workspace); return new JoinTreeSlot(leaf); } else { ExtentJoinTreeNode extentNode = new ExtentJoinTreeNode(path.Extent, new MemberJoinTreeNode[] { }, workspace); return new JoinTreeSlot(extentNode); } } internal static IEqualityComparer GetEqualityComparer(FragmentQueryProcessor qp) { return new FragmentQueryEqualityComparer(qp); } //internal static IComparer GetComparer(IEnumerable attributes) //{ // return new FragmentQueryComparer(attributes); //} #endregion #region Equality Comparer // Two queries are "equal" if they project the same set of attributes // and their WHERE clauses are equivalent private class FragmentQueryEqualityComparer : IEqualityComparer { FragmentQueryProcessor _qp; internal FragmentQueryEqualityComparer(FragmentQueryProcessor qp) { _qp = qp; } #region IEqualityComparer Members public bool Equals(FragmentQuery x, FragmentQuery y) { if (!x.Attributes.SetEquals(y.Attributes)) { return false; } return _qp.IsEquivalentTo(x, y); } // Hashing a bit naive: it exploits syntactic properties, // i.e., some semantically equivalent queries may produce different hash codes // But that's fine for usage scenarios in QueryRewriter.cs public int GetHashCode(FragmentQuery q) { int attrHashCode = 0; foreach (MemberPath member in q.Attributes) { attrHashCode ^= MemberPath.EqualityComparer.GetHashCode(member); } int varHashCode = 0; int constHashCode = 0; foreach (OneOfConst oneOf in q.Condition.OneOfConstVariables) { varHashCode ^= MemberPath.EqualityComparer.GetHashCode(oneOf.Slot.MemberPath); foreach (CellConstant constant in oneOf.Values.Values) { constHashCode ^= CellConstant.EqualityComparer.GetHashCode(constant); } } return attrHashCode * 13 + varHashCode * 7 + constHashCode; } #endregion } #endregion //#region Comparer //// queries with largest attribute overlap go first //private class FragmentQueryComparer : IComparer //{ // private HashSet _attributes; // internal FragmentQueryComparer(IEnumerable attributes) // { // _attributes = new HashSet (attributes); // } // #region IComparer Members // public int Compare(FragmentQuery x, FragmentQuery y) // { // HashSet xAttr = new HashSet (x.Attributes); // HashSet yAttr = new HashSet (y.Attributes); // xAttr.IntersectWith(_attributes); // yAttr.IntersectWith(_attributes); // if (xAttr.Count > yAttr.Count) // { // return -1; // } // else if (xAttr.Count < yAttr.Count) // { // return 1; // } // return 0; // } // #endregion //} //#endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- httpserverutility.cs
- DeviceSpecific.cs
- DesignerLoader.cs
- XmlSchemaExternal.cs
- ErrorRuntimeConfig.cs
- HtmlProps.cs
- CqlGenerator.cs
- ResourceExpressionBuilder.cs
- PostBackOptions.cs
- DPTypeDescriptorContext.cs
- AssemblyResourceLoader.cs
- ConvertersCollection.cs
- FaultDescription.cs
- SecurityRequiresReviewAttribute.cs
- StringResourceManager.cs
- CollectionViewSource.cs
- _AutoWebProxyScriptWrapper.cs
- Operator.cs
- PerformanceCounterLib.cs
- SessionPageStatePersister.cs
- DriveInfo.cs
- HtmlFormWrapper.cs
- _NetRes.cs
- SqlTransaction.cs
- Directory.cs
- CultureInfoConverter.cs
- HttpListenerRequest.cs
- DataTableCollection.cs
- SymbolEqualComparer.cs
- EditingCommands.cs
- Internal.cs
- CancellationHandlerDesigner.cs
- SpecularMaterial.cs
- FixedSOMPageElement.cs
- AliasGenerator.cs
- TextTreeUndoUnit.cs
- EncoderParameter.cs
- TransactionFlowBindingElementImporter.cs
- RSAPKCS1KeyExchangeDeformatter.cs
- XmlElementAttribute.cs
- GenericEnumerator.cs
- BamlBinaryReader.cs
- COM2ExtendedTypeConverter.cs
- SpecialNameAttribute.cs
- HwndSourceKeyboardInputSite.cs
- AdCreatedEventArgs.cs
- DnsPermission.cs
- TdsParserStaticMethods.cs
- TraceContext.cs
- MergablePropertyAttribute.cs
- X509Certificate2Collection.cs
- PerspectiveCamera.cs
- Block.cs
- SqlCrossApplyToCrossJoin.cs
- Transform.cs
- BufferModeSettings.cs
- SoapSchemaExporter.cs
- TextTreeNode.cs
- MarkerProperties.cs
- RegistrationContext.cs
- ExecutionTracker.cs
- ChannelSinkStacks.cs
- AuthenticationSection.cs
- QilList.cs
- ConfigurationPermission.cs
- ALinqExpressionVisitor.cs
- Vector3DAnimationBase.cs
- SortDescription.cs
- tibetanshape.cs
- ComponentChangingEvent.cs
- Version.cs
- TreeSet.cs
- ConfigXmlWhitespace.cs
- DrawingContext.cs
- MarkerProperties.cs
- DocumentPaginator.cs
- ImmComposition.cs
- ByteConverter.cs
- RegexMatch.cs
- CharAnimationUsingKeyFrames.cs
- TextDocumentView.cs
- EndpointDispatcher.cs
- SystemGatewayIPAddressInformation.cs
- XomlCompilerHelpers.cs
- RelatedCurrencyManager.cs
- Model3D.cs
- exports.cs
- HttpRuntime.cs
- TransformDescriptor.cs
- SerializationObjectManager.cs
- SoapFault.cs
- SqlCacheDependency.cs
- Matrix.cs
- XmlImplementation.cs
- XomlDesignerLoader.cs
- SQLMembershipProvider.cs
- NamedElement.cs
- DrawingAttributeSerializer.cs
- SymDocumentType.cs
- CodeTypeDeclarationCollection.cs