Below is my method which is calling two delegate function which in turn return two keys partition and Row key. the methods are same for generating partition and Row key and only difference is which string from list returned to be taken as row key and partition key. I would like to know if there is way I can optimize my below method and use one delegate function instead of two.How do i do it?( if possible someone can show me).
// first strarting point
await csvCopy.UploadNCsvData( rule.GeneratePartitionKey,rule.GenerateRowKey, unzipper, schemaFormat, true, csvSchema );
then defining upload
public async Task UploadNCsvData( Func<Dictionary<string, EntityProperty>, Task<string>> genPartitionKey,
Func<Dictionary<string, EntityProperty>, Task<string>> genRowKey,
Stream lines, string format, bool upsert, IDataClassifier classifier )
{
//mycode and then finally calling WriteToTable()
await WriteToTable( lines, dataclass,
genPartitionKey,
genRowKey, upsert );
}
//finally the writeToTable()
public async Task WriteToTable( Stream lines, DataClass dataclass,
Func<Dictionary<string, EntityProperty>, Task<string>> genPartitionKey,
Func<Dictionary<string, EntityProperty>, Task<string>> genRowKey, bool upsert )
{
const int BatchSize = 100;
if( HasPartitionAndRowKey( dataclass.TableSchema.Fields ) )
{
genPartitionKey = ( Dictionary<string, EntityProperty> props ) => Task.FromResult(props["PartitionKey"].StringValue);
genRowKey = ( Dictionary<string, EntityProperty> props ) => Task.FromResult(props["RowKey"].ToString());
}
var tableRecords = ReadCSV( lines, dataclass.TableSchema.Fields )
.Select( async props => new DynamicTableEntity( await genPartitionKey( props ), await genRowKey( props ), string.Empty, props ) )
.ToList();
await BatchInsertIntoTableStorage( BatchSize, tableRecords, upsert );
}
private IEnumerable<Dictionary<string, EntityProperty>> ReadCSV( Stream source, IEnumerable<TableField> cols )
{
using( TextReader reader = new StreamReader( source, Encoding.UTF8 ) )
{.....
.......
while( csv.Read() )
{
yield return map.ToDictionary(
col => col.Name,
col => EntityProperty.CreateEntityPropertyFromObject( csv.GetField( col.Type, col.Index ) ) );
}
}
}
// These are functions being called and as you see the methods and arguments are same, only return value differ in which string to pass.
internal async Task<string> GeneratePartitionKey( Dictionary<string, EntityProperty> arg)
{
var result =await GenerateKeys(arg);
return result[0].ToString();
}
internal async Task<string> GenerateRowKey( Dictionary<string, EntityProperty> arg )
{
var result = await GenerateKeys( arg );
return result[1].ToString();
}