You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/PartitionFileStorageTest/Program.cs

253 lines
9.8 KiB

2 years ago
using System.Diagnostics;
using System.Text;
using ZeroLevel.Services.PartitionStorage;
namespace PartitionFileStorageTest
{
2 years ago
public class CallRecordParser
{
2 years ago
private static HashSet<char> _partsOfNumbers = new HashSet<char> { '*', '#', '+', '(', ')', '-' };
private StringBuilder sb = new StringBuilder();
private const string NO_VAL = null;
2 years ago
private string ReadNumber(string line)
{
2 years ago
sb.Clear();
var started = false;
foreach (var ch in line)
{
2 years ago
if (char.IsDigit(ch))
{
if (started)
{
sb.Append(ch);
}
else if (ch != '0')
{
sb.Append(ch);
started = true;
}
}
else if (char.IsWhiteSpace(ch) || _partsOfNumbers.Contains(ch)) continue;
else return NO_VAL;
}
2 years ago
if (sb.Length == 11 && sb[0] == '8') sb[0] = '7';
if (sb.Length == 3 || sb.Length == 4 || sb.Length > 10)
return sb.ToString();
return NO_VAL;
}
private HashSet<string> ReadNumbers(string line)
{
var result = new HashSet<string>();
if (string.IsNullOrWhiteSpace(line) == false)
{
2 years ago
char STX = (char)0x0002;
var values = line.Split(STX);
if (values.Length > 0)
{
foreach (var val in values)
{
var number = ReadNumber(val);
if (number != null)
{
result.Add(number);
}
}
}
}
2 years ago
return result;
}
2 years ago
/// <summary>
/// Парсинг строки исходного файла
/// </summary>
/// <param name="line"></param>
/// <returns></returns>
public CallRecord Parse(string line)
{
2 years ago
var parts = line.Split('\t');
if (parts.Length != 2) return null;
var msisdn = ReadNumber(parts[0].Trim());
if (string.IsNullOrWhiteSpace(msisdn) == false)
{
2 years ago
var numbers = ReadNumbers(parts[1]);
if (numbers != null && numbers.Count > 0)
{
return new CallRecord
{
Msisdn = msisdn,
Msisdns = numbers
};
}
}
2 years ago
return null;
}
2 years ago
}
2 years ago
public class CallRecord
{
public string Msisdn;
public HashSet<string> Msisdns;
}
2 years ago
internal class Program
{
private class Metadata
{
2 years ago
public DateTime Date { get; set; }
public bool Incoming { get; set; }
}
2 years ago
private static void BuildStore(string source, string root)
{
var options = new StoreOptions<ulong, ulong, byte[], Metadata>
{
Index = new IndexOptions { Enabled = true, FileIndexCount = 256 },
2 years ago
RootFolder = root,
FilePartition = new StoreFilePartition<ulong, Metadata>("Last three digits", (ctn, date) => (ctn % 512).ToString()),
2 years ago
MergeFunction = list =>
{
2 years ago
ulong s = 0;
return Compressor.GetEncodedBytes(list.OrderBy(c => c), ref s);
},
Partitions = new List<StoreCatalogPartition<Metadata>>
{
new StoreCatalogPartition<Metadata>("Date", m => m.Date.ToString("yyyyMMdd")),
new StoreCatalogPartition<Metadata>("Date", m => m.Incoming ? "incoming" : "outcoming")
},
KeyComparer = (left, right) => left == right ? 0 : (left < right) ? -1 : 1,
2 years ago
};
var store = new Store<ulong, ulong, byte[], Metadata>(options);
var storeIncoming = store.CreateBuilder(new Metadata { Date = new DateTime(2022, 11, 08), Incoming = true });
var storeOutcoming = store.CreateBuilder(new Metadata { Date = new DateTime(2022, 11, 08), Incoming = false });
2 years ago
var parser = new CallRecordParser();
var sw = new Stopwatch();
sw.Start();
using (FileStream fs = File.Open(source, FileMode.Open, FileAccess.Read, FileShare.None))
{
2 years ago
using (BufferedStream bs = new BufferedStream(fs, 1024 * 1024 * 64))
{
2 years ago
using (StreamReader sr = new StreamReader(bs))
{
string line;
while ((line = sr.ReadLine()) != null)
{
var record = parser.Parse(line);
if (record == null) continue;
if (!string.IsNullOrWhiteSpace(record?.Msisdn ?? string.Empty) && ulong.TryParse(record.Msisdn, out var n))
{
var ctns = record.Msisdns.ParseMsisdns().ToArray();
foreach (var ctn in ctns)
{
storeIncoming.Store(n, ctn);
storeOutcoming.Store(ctn, n);
}
}
}
}
}
}
2 years ago
sw.Stop();
Console.WriteLine($"Fill journal: {sw.ElapsedMilliseconds}ms");
sw.Restart();
storeIncoming.CompleteAddingAndCompress();
storeOutcoming.CompleteAddingAndCompress();
2 years ago
sw.Stop();
Console.WriteLine($"Rebuild journal to store: {sw.ElapsedMilliseconds}ms");
sw.Restart();
storeIncoming.RebuildIndex();
storeOutcoming.RebuildIndex();
sw.Stop();
Console.WriteLine($"Rebuild indexes: {sw.ElapsedMilliseconds}ms");
}
private static void TestReading(string root)
{
var options = new StoreOptions<ulong, ulong, byte[], Metadata>
{
Index = new IndexOptions { Enabled = true, FileIndexCount = 256 },
2 years ago
RootFolder = root,
FilePartition = new StoreFilePartition<ulong, Metadata>("Last three digits", (ctn, date) => (ctn % 512).ToString()),
2 years ago
MergeFunction = list =>
{
2 years ago
ulong s = 0;
return Compressor.GetEncodedBytes(list.OrderBy(c => c), ref s);
},
Partitions = new List<StoreCatalogPartition<Metadata>>
{
new StoreCatalogPartition<Metadata>("Date", m => m.Date.ToString("yyyyMMdd")),
new StoreCatalogPartition<Metadata>("Date", m => m.Incoming ? "incoming" : "outcoming")
2 years ago
},
KeyComparer = (left, right) => left == right ? 0 : (left < right) ? -1 : 1,
2 years ago
};
var store = new Store<ulong, ulong, byte[], Metadata>(options);
var request = new StoreSearchRequest<ulong, Metadata>
{
2 years ago
PartitionSearchRequests = new List<PartitionSearchRequest<ulong, Metadata>>
{
2 years ago
new PartitionSearchRequest<ulong, Metadata>
{
Info = new Metadata { Date = new DateTime(2022, 11, 08), Incoming = true },
Keys = new ulong[] { }
2 years ago
},
new PartitionSearchRequest<ulong, Metadata>
{
2 years ago
Info = new Metadata { Date = new DateTime(2022, 11, 08), Incoming = false },
Keys = new ulong[] { }
}
2 years ago
}
};
var storeIncoming = store.CreateAccessor(new Metadata { Date = new DateTime(2022, 11, 08), Incoming = true });
Console.WriteLine($"Incoming data files: {storeIncoming.CountDataFiles()}");
var storeOutcoming = store.CreateAccessor(new Metadata { Date = new DateTime(2022, 11, 08), Incoming = false });
Console.WriteLine($"Outcoming data files: {storeOutcoming.CountDataFiles()}");
var sw = new Stopwatch();
sw.Start();
var result = store.Search(request).Result;
foreach (var r in result.Results)
{
Console.WriteLine($"Incoming: {r.Key.Incoming}");
foreach (var mr in r.Value)
{
Console.WriteLine($"\tKey: {mr.Key}. Sucess: {mr.Found}");
if (mr.Found && mr.Value.Length > 0)
{
2 years ago
var ctns = Compressor.DecodeBytesContent(mr.Value);
Console.WriteLine($"\t\t{string.Join(';', ctns)}");
}
}
}
2 years ago
sw.Stop();
Console.WriteLine($"Search time: {sw.ElapsedMilliseconds}ms");
}
private struct KeyIndex<TKey>
{
public TKey Key { get; set; }
public long Offset { get; set; }
}
static KeyIndex<long>[] Generate(int count)
{
var arr = new KeyIndex<long>[count];
for (int i = 0; i < count; i++)
{
arr[i] = new KeyIndex<long> { Key = i * 3, Offset = i * 17 };
}
return arr;
}
2 years ago
static void Main(string[] args)
{
var root = @"H:\temp";
var source = @"H:\319a9c31-d823-4dd1-89b0-7fb1bb9c4859.txt";
//BuildStore(source, root);
TestReading(root);
2 years ago
Console.ReadKey();
}
}
}

Powered by TurnKey Linux.