public async Task <TemporaryReadResult <TKey, TEntry> > ReadAsync(int count) { await _sem.WaitAsync().ConfigureAwait(false); try { int read = 0; Dictionary <string, Serie <TKey, TEntry> > series = new Dictionary <string, Serie <TKey, TEntry> >(); List <FileModificationRef> modifications = new List <FileModificationRef>(); foreach (var fi in _directory.EnumerateFiles("*.dat")) { using (var fs = fi.Open(FileMode.Open, FileAccess.Read)) using (var reader = new BinaryReader(fs, Encoding.ASCII, true)) { while (reader.PeekChar() != -1 && read < count) { var entry = new TEntry(); var id = reader.ReadString(); Serie <TKey, TEntry> serie; if (!series.TryGetValue(id, out serie)) { var key = await _keyConverter.ConvertAsync(id).ConfigureAwait(false); serie = new Serie <TKey, TEntry>(key); series.Add(id, serie); } var timestamp = new DateTime(reader.ReadInt64(), DateTimeKind.Utc); entry.SetTimestamp(timestamp); entry.Read(reader); serie.Entries.Add(entry); read++; } // add the file var reference = new FileModificationRef(fi, fs.Length, fs.Position); modifications.Add(reference); if (read == count) { break; } } } return(new TemporaryReadResult <TKey, TEntry>(series.Values.ToList(), () => DeleteAsync(modifications))); } finally { _sem.Release(); } }
public static async Task <Serie <TKey, TEntry> > Deserialize <TKey, TEntry>(byte[] bytes, IKeyConverter <TKey> keyConverter) where TEntry : IRedisEntry, new() { var stream = new MemoryStream(bytes); var reader = CreateReader(stream); var id = reader.ReadString(); var key = await keyConverter.ConvertAsync(id).ConfigureAwait(false); var serie = new Serie <TKey, TEntry>(key); while (stream.Length != stream.Position) { var entry = DeserializeEntry <TKey, TEntry>(reader); serie.Entries.Add(entry); } return(serie); }