本文整理汇总了C#中System.Collections.Dictionary.GetOrCreate方法的典型用法代码示例。如果您正苦于以下问题:C# Dictionary.GetOrCreate方法的具体用法?C# Dictionary.GetOrCreate怎么用?C# Dictionary.GetOrCreate使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.Dictionary
的用法示例。
在下文中一共展示了Dictionary.GetOrCreate方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConstructGraph
/// <summary>
/// Constructs the graph pattern specified by the MATCH clause.
/// The graph pattern may consist of multiple fully-connected sub-graphs.
/// </summary>
/// <param name="query">The SELECT query block</param>
/// <returns>A graph object contains all the connected componeents</returns>
private MatchGraph ConstructGraph(WSelectQueryBlock query)
{
if (query == null || query.MatchClause == null)
return null;
var columnsOfNodeTables = _graphMetaData.ColumnsOfNodeTables;
var nodeViewMapping = _graphMetaData.NodeViewMapping;
var unionFind = new UnionFind();
var edgeColumnToAliasesDict = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
var pathDictionary = new Dictionary<string, MatchPath>(StringComparer.OrdinalIgnoreCase);
var reversedEdgeDict = new Dictionary<string, MatchEdge>();
var matchClause = query.MatchClause;
var nodes = new Dictionary<string, MatchNode>(StringComparer.OrdinalIgnoreCase);
var connectedSubGraphs = new List<ConnectedComponent>();
var subGrpahMap = new Dictionary<string, ConnectedComponent>(StringComparer.OrdinalIgnoreCase);
var parent = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
unionFind.Parent = parent;
// Constructs the graph pattern specified by the path expressions in the MATCH clause
foreach (var path in matchClause.Paths)
{
var index = 0;
MatchEdge preEdge = null;
for (var count = path.PathEdgeList.Count; index < count; ++index)
{
var currentNodeTableRef = path.PathEdgeList[index].Item1;
var currentEdgeColumnRef = path.PathEdgeList[index].Item2;
var currentNodeExposedName = currentNodeTableRef.BaseIdentifier.Value;
var nextNodeTableRef = index != count - 1
? path.PathEdgeList[index + 1].Item1
: path.Tail;
var nextNodeExposedName = nextNodeTableRef.BaseIdentifier.Value;
var patternNode = nodes.GetOrCreate(currentNodeExposedName);
if (patternNode.NodeAlias == null)
{
patternNode.NodeAlias = currentNodeExposedName;
patternNode.Neighbors = new List<MatchEdge>();
patternNode.External = false;
var nodeTable = _context[currentNodeExposedName] as WNamedTableReference;
if (nodeTable != null)
{
patternNode.NodeTableObjectName = nodeTable.TableObjectName;
if (patternNode.NodeTableObjectName.SchemaIdentifier == null)
patternNode.NodeTableObjectName.Identifiers.Insert(0, new Identifier {Value = "dbo"});
}
}
Identifier edgeIdentifier = currentEdgeColumnRef.MultiPartIdentifier.Identifiers.Last();
string schema = patternNode.NodeTableObjectName.SchemaIdentifier.Value.ToLower();
string nodeTableName = patternNode.NodeTableObjectName.BaseIdentifier.Value;
string bindTableName =
_context.EdgeNodeBinding[
new Tuple<string, string>(nodeTableName.ToLower(), edgeIdentifier.Value.ToLower())].ToLower();
var bindNodeTableObjName = new WSchemaObjectName(
new Identifier {Value = schema},
new Identifier {Value = bindTableName}
);
var edgeColumn =
columnsOfNodeTables[
WNamedTableReference.SchemaNameToTuple(bindNodeTableObjName)][
currentEdgeColumnRef.MultiPartIdentifier.Identifiers.Last().Value];
string edgeAlias = currentEdgeColumnRef.Alias;
//string revEdgeAlias = edgeAlias;
bool isReversed = path.IsReversed || edgeColumn.EdgeInfo.IsReversedEdge;
string currentRevEdgeName = null;
// get original edge name
var currentEdgeName = currentEdgeColumnRef.MultiPartIdentifier.Identifiers.Last().Value;
var originalSourceName = isReversed ?
(_context[nextNodeExposedName] as WNamedTableReference).TableObjectName.BaseIdentifier.Value
: (_context[currentNodeExposedName] as WNamedTableReference).TableObjectName.BaseIdentifier.Value;
if (isReversed)
{
var i = currentEdgeName.IndexOf(originalSourceName, StringComparison.OrdinalIgnoreCase) +
originalSourceName.Length;
currentRevEdgeName = currentEdgeName.Substring(i + 1,
currentEdgeName.Length - "Reversed".Length - i - 1);
}
else
{
var srcTuple = WNamedTableReference.SchemaNameToTuple(patternNode.NodeTableObjectName);
// [nodeView]-[edge]->[node/nodeView]
if (edgeColumn.Role == WNodeTableColumnRole.Edge && nodeViewMapping.ContainsKey(srcTuple))
{
var physicalNodeName =
_context.EdgeNodeBinding[new Tuple<string, string>(srcTuple.Item2, currentEdgeName.ToLower())];
currentRevEdgeName = physicalNodeName + "_" + currentEdgeName + "Reversed";
}
else
{
currentRevEdgeName = originalSourceName + "_" + currentEdgeName + "Reversed";
}
}
//.........这里部分代码省略.........
示例2: ChangeSelectStarExpression
/// <summary>
/// Replaces the SELECT * expression with all visible columns
/// </summary>
/// <param name="node"></param>
/// <param name="graph"></param>
private void ChangeSelectStarExpression(WSelectQueryBlock node, MatchGraph graph)
{
var newSelectElements = new List<WSelectElement>();
Dictionary<string, List<WSelectElement>> starReplacement = null;
foreach (var element in node.SelectElements)
{
var starElement = element as WSelectStarExpression;
if (starElement != null)
{
if (starReplacement == null)
{
starReplacement =
new Dictionary<string, List<WSelectElement>>(StringComparer.OrdinalIgnoreCase);
// Fetch table in order
foreach (var table in _context.NodeTableDictionary)
{
var alias = table.Key;
var namedTable = table.Value as WNamedTableReference;
if (namedTable != null)
{
foreach (
var column in
_graphMetaData.ColumnsOfNodeTables[
WNamedTableReference.SchemaNameToTuple(namedTable.TableObjectName)].Where(
e => e.Value.Role != WNodeTableColumnRole.Edge).Select(e => e.Key))
{
var elementList = starReplacement.GetOrCreate(alias);
elementList.Add(new WSelectScalarExpression
{
SelectExpr = new WColumnReferenceExpression
{
MultiPartIdentifier = new WMultiPartIdentifier
{
Identifiers = new List<Identifier>
{
new Identifier {Value = alias},
new Identifier {Value = column}
}
}
}
});
}
if (graph == null) continue;
foreach (var subGraph in graph.ConnectedSubGraphs)
{
if (subGraph.Nodes.ContainsKey(alias))
{
var matchNode = subGraph.Nodes[alias];
foreach (var edge in matchNode.Neighbors)
{
var schemaName = edge.SourceNode.NodeTableObjectName.SchemaIdentifier ==
null
? "dbo"
: edge.SourceNode.NodeTableObjectName.SchemaIdentifier.Value.ToLower();
var nodeTuple = new Tuple<string, string>(schemaName,
edge.SourceNode.NodeTableObjectName.BaseIdentifier.Value.ToLower());
var edgeColumnName =
edge.EdgeColumn.MultiPartIdentifier.Identifiers.Last().Value.ToLower();
if (!_graphMetaData.ColumnsOfNodeTables[nodeTuple].ContainsKey(edgeColumnName))
{
throw new GraphViewException("Invalid Edge Alias");
}
foreach (
var column in
_graphMetaData.ColumnsOfNodeTables[nodeTuple][edgeColumnName].EdgeInfo
.ColumnAttributes)
{
var elementList = starReplacement.GetOrCreate(edge.EdgeAlias);
elementList.Add(new WSelectScalarExpression
{
SelectExpr = new WColumnReferenceExpression
{
MultiPartIdentifier = new WMultiPartIdentifier
{
Identifiers = new List<Identifier>
{
new Identifier {Value = edge.EdgeAlias},
new Identifier {Value = column}
}
}
}
});
}
}
}
}
}
else
{
var derivedTable = table.Value as WQueryDerivedTable;
if (derivedTable == null)
continue;
var elementList = starReplacement.GetOrCreate(alias);
//.........这里部分代码省略.........
示例3: ConstructGraph
/// <summary>
/// Construct Graph from the match clause. The Graph can consist of multiple connected SubGraph.
/// Not supported in this version
/// </summary>
/// <param name="query"></param>
/// <returns></returns>
private MatchGraph ConstructGraph(WSelectQueryBlock query)
{
var unionFind = new UnionFind();
if (query.MatchClause == null)
return null;
var edgeTableReferenceDict = new Dictionary<string, List<string>>(StringComparer.CurrentCultureIgnoreCase);
var matchClause = query.MatchClause;
var nodes = new Dictionary<string, MatchNode>(StringComparer.CurrentCultureIgnoreCase);
var connectedSubGraphs = new List<ConnectedComponent>();
var subGrpahMap = new Dictionary<string, ConnectedComponent>(StringComparer.CurrentCultureIgnoreCase);
var parent = new Dictionary<string, string>(StringComparer.CurrentCultureIgnoreCase);
unionFind.Parent = parent;
HashSet<Tuple<string, string>> nodeTypes = new HashSet<Tuple<string, string>>();
//Construct Graph from Match Pattern
foreach (var path in matchClause.Paths)
{
var index = 0;
MatchEdge preEdge = null;
for (var count = path.PathNodeList.Count; index < count; ++index)
{
var currentNode = path.PathNodeList[index].Item1;
var currentEdge = path.PathNodeList[index].Item2;
var currentNodeExposedName = currentNode.BaseIdentifier.Value;
var nextNode = index != count - 1
? path.PathNodeList[index + 1].Item1
: path.Tail;
var nextNodeExposedName = nextNode.BaseIdentifier.Value;
var node = nodes.GetOrCreate(currentNodeExposedName);
if (node.NodeAlias == null)
{
node.NodeAlias = currentNodeExposedName;
node.Neighbors = new List<MatchEdge>();
node.External = false;
var nodeTable = _context[currentNodeExposedName] as WNamedTableReference;
if (nodeTable != null)
{
node.TableObjectName = nodeTable.TableObjectName;
if (node.TableObjectName.SchemaIdentifier == null)
node.TableObjectName.Identifiers.Insert(0, new Identifier { Value = "dbo" });
var nodeTypeTuple = WNamedTableReference.SchemaNameToTuple(node.TableObjectName);
if (!nodeTypes.Contains(nodeTypeTuple))
nodeTypes.Add(nodeTypeTuple);
}
}
if (currentEdge.AliasRole == AliasType.Default)
{
var currentEdgeName = currentEdge.MultiPartIdentifier.Identifiers.Last().Value;
if (edgeTableReferenceDict.ContainsKey(currentEdgeName))
{
edgeTableReferenceDict[currentEdgeName].Add(currentEdge.Alias);
}
else
{
edgeTableReferenceDict.Add(currentEdgeName, new List<string> { currentEdge.Alias });
}
}
var edge = new MatchEdge
{
SourceNode = node,
EdgeColumn = new WColumnReferenceExpression
{
MultiPartIdentifier = new WMultiPartIdentifier
{
Identifiers = new List<Identifier>
{
new Identifier {Value = node.NodeAlias},
currentEdge.MultiPartIdentifier.Identifiers.Last()
}
}
},
EdgeAlias = currentEdge.Alias
};
if (preEdge != null)
{
preEdge.SinkNode = node;
}
preEdge = edge;
if (!parent.ContainsKey(currentNodeExposedName))
parent[currentNodeExposedName] = currentNodeExposedName;
if (!parent.ContainsKey(nextNodeExposedName))
parent[nextNodeExposedName] = nextNodeExposedName;
unionFind.Union(currentNodeExposedName, nextNodeExposedName);
node.Neighbors.Add(edge);
_context.AddEdgeReference(currentEdge.Alias, edge.SourceNode.TableObjectName, currentEdge);
//.........这里部分代码省略.........
示例4: RetrieveStatistics
//.........这里部分代码省略.........
srcNodeStatisticsDict[new Tuple<string, bool>(edge.EdgeAlias, isReversed)] = null;
var sinkBytes = reader["Sink"] as byte[];
if (sinkBytes == null)
{
edge.Statistics = new Statistics
{
Density = 0,
Histogram = new Dictionary<long, Tuple<double, bool>>(),
MaxValue = 0,
RowCount = 0,
//Selectivity = 1.0
};
continue;
}
List<long> sinkList = new List<long>();
cursor = 0;
while (cursor < sinkBytes.Length)
{
var sink = BitConverter.ToInt64(sinkBytes, cursor);
cursor += 8;
sinkList.Add(sink);
}
Statistics.UpdateEdgeHistogram(edge, sinkList);
edge.AverageDegree = Convert.ToDouble(reader["AverageDegree"])*sinkList.Count*1.0/
Convert.ToInt64(reader["SampleRowCount"]);
var path = edge as MatchPath;
if (path != null)
{
if (path.AverageDegree > 1)
if (path.MaxLength != -1)
{
path.AverageDegree = Math.Pow(path.AverageDegree, path.MaxLength) -
(path.MinLength > 0
? Math.Pow(path.AverageDegree, path.MinLength - 1)
: 0);
}
else
path.AverageDegree = double.MaxValue;
}
}
}
// Retrieves density value for each node table
string tempTableName = Path.GetRandomFileName().Replace(".", "").Substring(0, 8);
var dbccDensityQuery = new StringBuilder();
dbccDensityQuery.Append(string.Format(@"CREATE TABLE #{0} (Density float, Len int, Col sql_variant);
INSERT INTO #{0} EXEC('", tempTableName));
Dictionary<Tuple<string, string>, List<MatchNode>> schemaTableToNodeListMapping =
new Dictionary<Tuple<string, string>, List<MatchNode>>();
foreach (var subGraph in graph.ConnectedSubGraphs)
{
foreach (var node in subGraph.Nodes.Values)
{
var tableTuple = WNamedTableReference.SchemaNameToTuple(node.NodeTableObjectName);
if (_graphMetaData.NodeViewMapping.ContainsKey(tableTuple))
{
node.GlobalNodeIdDensity = Statistics.DefaultDensity;
}
else
{
var nodeList = schemaTableToNodeListMapping.GetOrCreate(tableTuple);
nodeList.Add(node);
}
}
}
foreach (var tableTuple in schemaTableToNodeListMapping.Keys)
{
dbccDensityQuery.Append(string.Format(
"DBCC SHOW_STATISTICS (\"{0}.{1}\", [{0}{1}_PK_GlobalNodeId]) with DENSITY_VECTOR;\n",
tableTuple.Item1,
tableTuple.Item2));
}
dbccDensityQuery.Append("');\n");
dbccDensityQuery.Append(string.Format("SELECT Density FROM #{0} WHERE Col = 'GlobalNodeId'", tempTableName));
command.CommandText = dbccDensityQuery.ToString();
using (var reader = command.ExecuteReader())
{
foreach (var item in schemaTableToNodeListMapping)
{
double density;
if (!reader.Read())
density = Statistics.DefaultDensity;
else
{
density = Convert.ToDouble(reader["Density"]);
if (Math.Abs(density - 1.0) < 0.0001)
density = Statistics.DefaultDensity;
}
foreach (var node in item.Value)
{
node.GlobalNodeIdDensity = density;
}
}
}
}
}
示例5: ConstructGraph
/// <summary>
/// Constructs the graph pattern specified by the MATCH clause.
/// The graph pattern may consist of multiple fully-connected sub-graphs.
/// </summary>
/// <param name="query">The SELECT query block</param>
/// <returns>A graph object contains all the connected componeents</returns>
private MatchGraph ConstructGraph(WSelectQueryBlock query)
{
if (query == null || query.MatchClause == null)
return null;
var unionFind = new UnionFind();
var edgeColumnToAliasesDict = new Dictionary<string, List<string>>(StringComparer.OrdinalIgnoreCase);
var pathDictionary = new Dictionary<string, MatchPath>(StringComparer.OrdinalIgnoreCase);
var matchClause = query.MatchClause;
var nodes = new Dictionary<string, MatchNode>(StringComparer.OrdinalIgnoreCase);
var connectedSubGraphs = new List<ConnectedComponent>();
var subGrpahMap = new Dictionary<string, ConnectedComponent>(StringComparer.OrdinalIgnoreCase);
var parent = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
unionFind.Parent = parent;
// Constructs the graph pattern specified by the path expressions in the MATCH clause
foreach (var path in matchClause.Paths)
{
var index = 0;
MatchEdge preEdge = null;
for (var count = path.PathEdgeList.Count; index < count; ++index)
{
var currentNodeTableRef = path.PathEdgeList[index].Item1;
var currentEdgeColumnRef = path.PathEdgeList[index].Item2;
var currentNodeExposedName = currentNodeTableRef.BaseIdentifier.Value;
var nextNodeTableRef = index != count - 1
? path.PathEdgeList[index + 1].Item1
: path.Tail;
var nextNodeExposedName = nextNodeTableRef.BaseIdentifier.Value;
var patternNode = nodes.GetOrCreate(currentNodeExposedName);
if (patternNode.NodeAlias == null)
{
patternNode.NodeAlias = currentNodeExposedName;
patternNode.Neighbors = new List<MatchEdge>();
patternNode.External = false;
var nodeTable = _context[currentNodeExposedName] as WNamedTableReference;
if (nodeTable != null)
{
patternNode.NodeTableObjectName = nodeTable.TableObjectName;
if (patternNode.NodeTableObjectName.SchemaIdentifier == null)
patternNode.NodeTableObjectName.Identifiers.Insert(0, new Identifier {Value = "dbo"});
}
}
string edgeAlias = currentEdgeColumnRef.Alias;
if (edgeAlias == null)
{
var currentEdgeName = currentEdgeColumnRef.MultiPartIdentifier.Identifiers.Last().Value;
edgeAlias = string.Format("{0}_{1}_{2}", currentNodeExposedName, currentEdgeName,
nextNodeExposedName);
if (edgeColumnToAliasesDict.ContainsKey(currentEdgeName))
{
edgeColumnToAliasesDict[currentEdgeName].Add(edgeAlias);
}
else
{
edgeColumnToAliasesDict.Add(currentEdgeName, new List<string> { edgeAlias });
}
}
Identifier edgeIdentifier = currentEdgeColumnRef.MultiPartIdentifier.Identifiers.Last();
string schema = patternNode.NodeTableObjectName.SchemaIdentifier.Value.ToLower();
string nodeTableName = patternNode.NodeTableObjectName.BaseIdentifier.Value;
string bindTableName =
_context.EdgeNodeBinding[
new Tuple<string, string>(nodeTableName.ToLower(), edgeIdentifier.Value.ToLower())].ToLower();
MatchEdge edge;
if (currentEdgeColumnRef.MinLength == 1 && currentEdgeColumnRef.MaxLength == 1)
{
edge = new MatchEdge
{
SourceNode = patternNode,
EdgeColumn = currentEdgeColumnRef,
EdgeAlias = edgeAlias,
BindNodeTableObjName =
new WSchemaObjectName(
new Identifier {Value = schema},
new Identifier {Value = bindTableName}
),
};
_context.AddEdgeReference(edge);
}
else
{
MatchPath matchPath = new MatchPath
{
SourceNode = patternNode,
EdgeColumn = currentEdgeColumnRef,
EdgeAlias = edgeAlias,
BindNodeTableObjName =
new WSchemaObjectName(
new Identifier {Value = schema},
new Identifier {Value = bindTableName}
),
//.........这里部分代码省略.........