public WriteContentToStream ( Stream stream ) : void | ||
stream | Stream | |
return | void |
/// <summary> /// Extract the masterstream to mimic the folder structure the AzureAsyncAppender /// </summary> /// <returns></returns> public async Task ExtractMasterStream(DirectoryInfo output, IAppenderNamingPolicy namingPolicy, long afterVersion, int maxCount, bool writeIndexFile = true) { var events = await this.ReadRecords(afterVersion, maxCount); var versions = new Dictionary <string, int>(); var rootindex = new FileInfo(Path.Combine(output.FullName, namingPolicy.GetIndexPath("master"))); if (!output.Exists) { output.Create(); } using (var masterindexwriter = new StreamWriter(System.IO.File.OpenWrite(rootindex.FullName))) { foreach (var @event in events) { var streampath = namingPolicy.GetStreamPath(@event.Name); var filename = new FileInfo(Path.Combine(output.FullName, streampath)); if (!versions.ContainsKey(@event.Name)) { versions[@event.Name] = 1; } else { versions[@event.Name]++; } var record = new FileRecord(@event.Data, @event.Name, versions[@event.Name]); if (!filename.Directory.Exists) { filename.Directory.Create(); } using (var fs = System.IO.File.OpenWrite(filename.FullName)) { record.WriteContentToStream(fs); } if (writeIndexFile) { var indexfile = Path.Combine(output.FullName, namingPolicy.GetIndexPath(@event.Name)); var hash = string.Join("", record.Hash.Select(x => x.ToString("x2"))); FileStream fileStream = System.IO.File.Exists(indexfile) ? System.IO.File.Open(indexfile, FileMode.Append, FileAccess.Write) : System.IO.File.Create(indexfile); using (var streamindex = new StreamWriter(fileStream)) { streamindex.WriteLine(String.Join("\t", record.Name, record.Version, hash, streampath)); masterindexwriter.WriteLine(String.Join("\t", record.Name, record.Version, hash, streampath)); streamindex.Flush(); } } } masterindexwriter.Flush(); } }
/// <summary> /// Extract the masterstream to mimic the folder structure the AzureAsyncAppender /// </summary> /// <returns></returns> public async Task ExtractMasterStream(DirectoryInfo output, IAppenderNamingPolicy namingPolicy, long afterVersion, int maxCount, bool writeIndexFile = true) { var events = await this.ReadRecords(afterVersion, maxCount); var versions = new Dictionary<string, int>(); var rootindex = new FileInfo(Path.Combine(output.FullName, namingPolicy.GetIndexPath("master"))); if (!output.Exists) { output.Create(); } using (var masterindexwriter = new StreamWriter(System.IO.File.OpenWrite(rootindex.FullName))) { foreach (var @event in events) { var streampath = namingPolicy.GetStreamPath(@event.Name); var filename = new FileInfo(Path.Combine(output.FullName, streampath)); if (!versions.ContainsKey(@event.Name)) { versions[@event.Name] = 1; } else { versions[@event.Name]++; } var record = new FileRecord(@event.Data, @event.Name, versions[@event.Name]); if (!filename.Directory.Exists) { filename.Directory.Create(); } using (var fs = System.IO.File.OpenWrite(filename.FullName)) { record.WriteContentToStream(fs); } if (writeIndexFile) { var indexfile = Path.Combine(output.FullName, namingPolicy.GetIndexPath(@event.Name)); var hash = string.Join("", record.Hash.Select(x => x.ToString("x2"))); FileStream fileStream = System.IO.File.Exists(indexfile) ? System.IO.File.Open(indexfile, FileMode.Append, FileAccess.Write) : System.IO.File.Create(indexfile); using (var streamindex = new StreamWriter(fileStream)) { streamindex.WriteLine(String.Join("\t", record.Name, record.Version, hash, streampath)); masterindexwriter.WriteLine(String.Join("\t", record.Name, record.Version, hash, streampath)); streamindex.Flush(); } } } masterindexwriter.Flush(); } }
public async Task WriteContent(string name, byte[] content, long expectedStreamVersion, int retry = 0) { this.index = new FileIndexLock(storage, container, options.NamingPolicy.GetIndexPath(name)); try { //if (expectedStreamVersion == 0) //{ // await this.index.CreateIfNotExist(); // await this.masterindex.CreateIfNotExist(); //} var canWrite = await index.GetLeaseAndRead(expectedStreamVersion != 0); if (!canWrite) { if (retry < options.RetryPolicy.Length) { await Task.Delay(retry); await this.WriteContent(name, content, expectedStreamVersion, retry++); return; } await this.index.CreateIfNotExist(); await this.index.ReadIndex(); throw new AppendOnlyTimeoutException(expectedStreamVersion, index.OrderedIndex.Any() ? this.index.ReadLast().Version : 0, name); } await this.index.ReadIndex(); var last = index.ReadLast(); if ((last == null && expectedStreamVersion != 0) || (last != null && expectedStreamVersion != last.Version) || (last != null && last.Version != expectedStreamVersion )) { throw new AppendOnlyStoreConcurrencyException(expectedStreamVersion, last?.Version ?? 0, name); } var blobcache = GetStreamCache(name); if (last == null) { //no file, create the stream to append to... await blobcache.CreateOrReplaceAsync(); } var master = this.GetMasterCache(); using (var memstream = new MemoryStream()) { var record = new FileRecord(content, name, expectedStreamVersion + 1); record.WriteContentToStream(memstream); var mybytes = memstream.ToArray(); var appendmaster = Task.Run(async () => { using (var masterstream = new MemoryStream(memstream.ToArray())) { await master.AppendBlockAsync(masterstream); } }); var up1 = blobcache.AppendFromByteArrayAsync(mybytes, 0, mybytes.Length); var up3 = index.AppendWrite(record, blobcache.Name); var up4 = masterindex.AppendWrite(record, blobcache.Name); await index.EnsureLease(); if (!options.DisableMasterIndex) { await Task.WhenAll(up1, appendmaster, up3); } else { await Task.WhenAll(up4, up1, appendmaster, up3); } } } finally { await index.Release(); } }