using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using ZeroLevel.Services.FileSystem;
namespace ZeroLevel.Services.PartitionStorage
{
///
/// Options
///
/// Record key
/// The value that is written in the stream
/// Value after compression of TInput values by duplicate keys (TInput list or similar)
/// Meta information for partition search
public class StoreOptions
{
///
/// Method for key comparison
///
public Func KeyComparer { get; set; }
///
/// Storage root directory
///
public string RootFolder { get; set; }
///
/// Maximum degree of parallelis
///
public int MaxDegreeOfParallelism { get; set; } = 64;
///
/// Function for translating a list of TInput into one TValue
///
public Func, TValue> MergeFunction { get; set; }
///
/// List of partitions for accessing the catalog
///
public List> Partitions { get; set; } = new List>();
///
/// File Partition
///
public StoreFilePartition FilePartition { get; set; }
public IndexOptions Index { get; set; } = new IndexOptions
{
Enabled = false,
FileIndexCount = 64
};
internal string GetFileName(TKey key, TMeta info)
{
return FilePartition.PathExtractor(key, info);
}
internal string GetCatalogPath(TMeta info)
{
var path = RootFolder;
foreach (var partition in Partitions)
{
var pathPart = partition.PathExtractor(info);
pathPart = FSUtils.FileNameCorrection(pathPart);
if (string.IsNullOrWhiteSpace(pathPart))
{
throw new Exception($"Partition '{partition.Name}' not return name of part of path");
}
path = Path.Combine(path, pathPart);
}
return path;
}
public StoreOptions Clone()
{
var options = new StoreOptions
{
Index = new IndexOptions
{
Enabled = this.Index.Enabled,
FileIndexCount = this.Index.FileIndexCount
},
FilePartition = this.FilePartition,
KeyComparer = this.KeyComparer,
MaxDegreeOfParallelism = this.MaxDegreeOfParallelism,
MergeFunction = this.MergeFunction,
Partitions = this.Partitions
.Select(p => new StoreCatalogPartition(p.Name, p.PathExtractor))
.ToList(),
RootFolder = this.RootFolder
};
return options;
}
}
}