Skip to content

Commit 5fa3201

Browse files
authored
Merge pull request #399 from DataObjects-NET/master-temptablemanager-imps
Small improvements of TemporaryTableManager and TupleDescriptor
2 parents 3f5b130 + 5bb9de3 commit 5fa3201

File tree

2 files changed

+47
-42
lines changed

2 files changed

+47
-42
lines changed

Orm/Xtensive.Orm/Orm/Providers/TemporaryTables/TemporaryTableManager.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ public class TemporaryTableManager : DomainBoundHandler
2424
private const string TableNamePattern = "Tmp_{0}";
2525
private const string ColumnNamePattern = "C{0}";
2626

27-
// Preallocated array of 1 column name
28-
private static readonly string[] ColumnNames1 = new[] { string.Format(ColumnNamePattern, 0) };
27+
// Preallocated array of 8 column names
28+
// to get parts of it
29+
private readonly string[] columnNames8 = new[] {
30+
string.Format(ColumnNamePattern, 0), string.Format(ColumnNamePattern, 1),
31+
string.Format(ColumnNamePattern, 2), string.Format(ColumnNamePattern, 3),
32+
string.Format(ColumnNamePattern, 4), string.Format(ColumnNamePattern, 5),
33+
string.Format(ColumnNamePattern, 6), string.Format(ColumnNamePattern, 7),
34+
};
2935

3036
private TemporaryTableBackEnd backEnd;
3137
private bool useTruncate;
@@ -42,7 +48,7 @@ public class TemporaryTableManager : DomainBoundHandler
4248
/// <param name="name">The name of the temporary table.</param>
4349
/// <param name="source">The source.</param>
4450
/// <returns>Built descriptor.</returns>
45-
public TemporaryTableDescriptor BuildDescriptor(ModelMapping modelMapping, string name, TupleDescriptor source)
51+
public TemporaryTableDescriptor BuildDescriptor(ModelMapping modelMapping, string name, in TupleDescriptor source)
4652
{
4753
return BuildDescriptor(modelMapping, name, source, null);
4854
}
@@ -55,7 +61,7 @@ public TemporaryTableDescriptor BuildDescriptor(ModelMapping modelMapping, strin
5561
/// <param name="source">The source.</param>
5662
/// <param name="fieldNames">The names of field in temporary table.</param>
5763
/// <returns>Built descriptor.</returns>
58-
public TemporaryTableDescriptor BuildDescriptor(ModelMapping modelMapping, string name, TupleDescriptor source, string[] fieldNames)
64+
public TemporaryTableDescriptor BuildDescriptor(ModelMapping modelMapping, string name, in TupleDescriptor source, string[] fieldNames)
5965
{
6066
EnsureTemporaryTablesSupported();
6167

@@ -70,14 +76,14 @@ public TemporaryTableDescriptor BuildDescriptor(ModelMapping modelMapping, strin
7076
? new Collation(schema, modelMapping.TemporaryTableCollation)
7177
: null;
7278

73-
fieldNames ??= BuildFieldNames(source);
79+
var resultFieldNames = fieldNames ?? BuildFieldNamesAsSegment(source);
7480

7581
var typeMappings = source
7682
.Select(driver.GetTypeMapping)
7783
.ToArray();
7884

7985
// table
80-
var table = CreateTemporaryTable(schema, name, source, typeMappings, fieldNames, collation);
86+
var table = CreateTemporaryTable(schema, name, source, typeMappings, resultFieldNames, collation);
8187
var tableRef = SqlDml.TableRef(table);
8288

8389
// select statement
@@ -163,16 +169,20 @@ private void EnsureTemporaryTablesSupported()
163169
throw new NotSupportedException(Strings.ExTemporaryTablesAreNotSupportedByCurrentStorage);
164170
}
165171

166-
private string[] BuildFieldNames(in TupleDescriptor source) =>
167-
source.Count == 1
168-
? ColumnNames1
172+
private ArraySegment<string> BuildFieldNamesAsSegment(TupleDescriptor source)
173+
{
174+
var count = source.Count;
175+
return count <= 8
176+
? new ArraySegment<string>(columnNames8, 0, count)
169177
: Enumerable.Range(0, source.Count)
170178
.Select(i => string.Format(ColumnNamePattern, i))
171179
.ToArray();
180+
}
172181

173-
private Table CreateTemporaryTable(Schema schema, string name, TupleDescriptor source, TypeMapping[] typeMappings, string[] fieldNames, Collation collation)
182+
private Table CreateTemporaryTable(Schema schema,
183+
string tempTableName, in TupleDescriptor source, TypeMapping[] typeMappings, ArraySegment<string> fieldNames, Collation collation)
174184
{
175-
var tableName = Handlers.NameBuilder.ApplyNamingRules(string.Format(TableNamePattern, name));
185+
var tableName = Handlers.NameBuilder.ApplyNamingRules(string.Format(TableNamePattern, tempTableName));
176186
var table = backEnd.CreateTemporaryTable(schema, tableName);
177187

178188
if (source.Count > 0) {

Orm/Xtensive.Orm/Tuples/TupleDescriptor.cs

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace Xtensive.Tuples
2525
[Serializable]
2626
public readonly struct TupleDescriptor : IEquatable<TupleDescriptor>, IReadOnlyList<Type>, ISerializable
2727
{
28-
private static readonly TupleDescriptor EmptyDescriptor = new TupleDescriptor(Array.Empty<Type>());
28+
public static readonly TupleDescriptor Empty = new TupleDescriptor(Array.Empty<Type>());
2929

3030
internal readonly int ValuesLength;
3131
internal readonly int ObjectsLength;
@@ -36,18 +36,6 @@ namespace Xtensive.Tuples
3636
[field: NonSerialized]
3737
private Type[] FieldTypes { get; }
3838

39-
private int FieldCount => FieldTypes.Length;
40-
41-
/// <summary>
42-
/// Gets the empty tuple descriptor.
43-
/// </summary>
44-
/// <value>The empty tuple descriptor.</value>
45-
public static TupleDescriptor Empty
46-
{
47-
[DebuggerStepThrough]
48-
get => EmptyDescriptor;
49-
}
50-
5139
#region IList members
5240

5341
/// <inheritdoc/>
@@ -61,13 +49,13 @@ public Type this[int fieldIndex]
6149
public int Count
6250
{
6351
[DebuggerStepThrough]
64-
get => FieldCount;
52+
get => FieldTypes.Length;
6553
}
6654

6755
/// <inheritdoc/>
6856
public IEnumerator<Type> GetEnumerator()
6957
{
70-
for (var index = 0; index < FieldCount; index++) {
58+
for (int index = 0, count = Count; index < count; index++) {
7159
yield return FieldTypes[index];
7260
}
7361
}
@@ -89,11 +77,11 @@ public bool Equals(TupleDescriptor other)
8977
if (FieldTypes == null) {
9078
return other.FieldTypes == null;
9179
}
92-
if (other.FieldTypes == null || FieldCount != other.FieldCount) {
80+
if (other.FieldTypes == null || Count != other.Count) {
9381
return false;
9482
}
9583

96-
for (int i = 0; i < FieldCount; i++) {
84+
for (int i = 0, count = Count; i < count; i++) {
9785
if (FieldTypes[i] != other.FieldTypes[i]) {
9886
return false;
9987
}
@@ -108,35 +96,42 @@ public override bool Equals(object obj) =>
10896
/// <inheritdoc/>
10997
public override int GetHashCode()
11098
{
111-
int result = FieldCount;
112-
for (int i = 0; i < FieldCount; i++)
99+
int result = Count;
100+
for (int i = 0, count = Count; i < count; i++)
113101
result = unchecked (FieldTypes[i].GetHashCode() + 29 * result);
114102
return result;
115103
}
116104

117-
public static bool operator ==(in TupleDescriptor left, in TupleDescriptor right) => left.Equals(right);
118-
public static bool operator !=(in TupleDescriptor left, in TupleDescriptor right) => !(left == right);
105+
public static bool operator ==(in TupleDescriptor left, in TupleDescriptor right)
106+
{
107+
return left.Equals(right);
108+
}
109+
110+
public static bool operator !=(in TupleDescriptor left, in TupleDescriptor right)
111+
{
112+
return !(left == right);
113+
}
119114

120115
#endregion
121116

122117
public void GetObjectData(SerializationInfo info, StreamingContext context)
123118
{
124-
info.AddValue("ValuesLength", ValuesLength);
125-
info.AddValue("ObjectsLength", ObjectsLength);
119+
info.AddValue(nameof(ValuesLength), ValuesLength);
120+
info.AddValue(nameof(ObjectsLength), ObjectsLength);
126121

127122
var typeNames = new string[FieldTypes.Length];
128123
for (var i = 0; i < typeNames.Length; i++)
129124
typeNames[i] = FieldTypes[i].ToSerializableForm();
130125

131-
info.AddValue("FieldTypes", typeNames);
132-
info.AddValue("FieldDescriptors", FieldDescriptors);
126+
info.AddValue(nameof(FieldTypes), typeNames);
127+
info.AddValue(nameof(FieldDescriptors), FieldDescriptors);
133128
}
134129

135130
/// <inheritdoc/>
136131
public override string ToString()
137132
{
138133
var sb = new ValueStringBuilder(stackalloc char[4096]);
139-
for (int i = 0; i < FieldCount; i++) {
134+
for (int i = 0, count = Count; i < count; i++) {
140135
if (i > 0)
141136
sb.Append(", ");
142137
sb.Append(FieldTypes[i].GetShortName());
@@ -177,7 +172,7 @@ public static TupleDescriptor Create(Type[] fieldTypes)
177172
{
178173
ArgumentValidator.EnsureArgumentNotNull(fieldTypes, nameof(fieldTypes));
179174
if (fieldTypes.Length == 0) {
180-
return EmptyDescriptor;
175+
return Empty;
181176
}
182177
return new TupleDescriptor(fieldTypes);
183178
}
@@ -285,12 +280,12 @@ private TupleDescriptor(Type[] fieldTypes)
285280

286281
public TupleDescriptor(SerializationInfo info, StreamingContext context)
287282
{
288-
ValuesLength = info.GetInt32("ValuesLength");
289-
ObjectsLength = info.GetInt32("ObjectsLength");
283+
ValuesLength = info.GetInt32(nameof(ValuesLength));
284+
ObjectsLength = info.GetInt32(nameof(ObjectsLength));
290285

291-
var typeNames = (string[]) info.GetValue("FieldTypes", typeof(string[]));
286+
var typeNames = (string[]) info.GetValue(nameof(FieldTypes), typeof(string[]));
292287
FieldDescriptors = (PackedFieldDescriptor[])info.GetValue(
293-
"FieldDescriptors", typeof(PackedFieldDescriptor[]));
288+
nameof(FieldDescriptors), typeof(PackedFieldDescriptor[]));
294289

295290
FieldTypes = new Type[typeNames.Length];
296291
for (var i = 0; i < typeNames.Length; i++) {

0 commit comments

Comments
 (0)