private static Checksum CreateChecksumFromStreamWriter(string kind, Action<ObjectWriter, CancellationToken> writer) { using (var stream = SerializableBytes.CreateWritableStream()) using (var objectWriter = new StreamObjectWriter(stream)) { objectWriter.WriteString(kind); writer(objectWriter, CancellationToken.None); return Checksum.Create(stream); } }
public Checksum CreateChecksum(AnalyzerReference reference, CancellationToken cancellationToken) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new StreamObjectWriter(stream, cancellationToken: cancellationToken)) { WriteTo(reference, writer, cancellationToken); stream.Position = 0; return Checksum.Create(stream); } }
private async Task SerializeDiagnosticResultAsync(string streamName, DiagnosticAnalysisResultMap<string, DiagnosticAnalysisResultBuilder> result) { using (RoslynLogger.LogBlock(FunctionId.CodeAnalysisService_SerializeDiagnosticResultAsync, GetResultLogInfo, result, CancellationToken)) using (var stream = await DirectStream.GetAsync(streamName, CancellationToken).ConfigureAwait(false)) { using (var writer = new StreamObjectWriter(stream)) { DiagnosticResultSerializer.Serialize(writer, result, CancellationToken); } await stream.FlushAsync(CancellationToken).ConfigureAwait(false); } }
public void VersionStamp_RoundTripText() { using (var writerStream = new MemoryStream()) using (var writer = new StreamObjectWriter(writerStream)) { var versionStamp = VersionStamp.Create(); versionStamp.WriteTo(writer); using (var readerStream = new MemoryStream(writerStream.ToArray())) using (var reader = new StreamObjectReader(readerStream)) { var deserializedVersionStamp = VersionStamp.ReadFrom(reader); Assert.Equal(versionStamp, deserializedVersionStamp); } } }
public async Task<bool> SerializeAsync(object documentOrProject, string key, ImmutableArray<DiagnosticData> items, CancellationToken cancellationToken) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new StreamObjectWriter(stream, cancellationToken: cancellationToken)) { WriteTo(writer, items, cancellationToken); var solution = GetSolution(documentOrProject); var persistService = solution.Workspace.Services.GetService<IPersistentStorageService>(); using (var storage = persistService.GetStorage(solution)) { stream.Position = 0; return await WriteStreamAsync(storage, documentOrProject, key, stream, cancellationToken).ConfigureAwait(false); } } }
public async Task TestSymbolTreeInfoSerialization() { var solution = GetSolution(WorkspaceKind.SingleClass); var compilation = await solution.Projects.First().GetCompilationAsync(); var assembly = compilation.GetSpecialType(SpecialType.System_Byte).ContainingAssembly; ////var assembly = compilation.Assembly; // create symbol tree info from assembly var version = VersionStamp.Create(); var info = SymbolTreeInfo.CreateSourceSymbolTreeInfo( solution, version, assembly, "", cancellationToken: CancellationToken.None); using (var writerStream = new MemoryStream()) { using (var writer = new StreamObjectWriter(writerStream)) { info.WriteTo(writer); } using (var readerStream = new MemoryStream(writerStream.ToArray())) using (var reader = new StreamObjectReader(readerStream)) { var readInfo = SymbolTreeInfo.ReadSymbolTreeInfo_ForTestingPurposesOnly(reader); info.AssertEquivalentTo(readInfo); } } }
public void TestSerialization() { var stream = new MemoryStream(); var bloomFilter = new BloomFilter(0.001, false, new[] { "Hello, World" }); using (var writer = new StreamObjectWriter(stream)) { bloomFilter.WriteTo(writer); } stream.Position = 0; using (var reader = new StreamObjectReader(stream)) { var rehydratedFilter = BloomFilter.ReadFrom(reader); Assert.True(bloomFilter.IsEquivalent(rehydratedFilter)); } }
private static Checksum CreateChecksum(AnalyzerReference reference) { using (var stream = SerializableBytes.CreateWritableStream()) using (var objectWriter = new StreamObjectWriter(stream)) { objectWriter.WriteString(WellKnownSynchronizationKinds.AnalyzerReference); objectWriter.WriteString(reference.FullPath); return Checksum.Create(stream); } }
private static async Task<RemotableData> CloneAssetAsync(Serializer serializer, RemotableData asset) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new StreamObjectWriter(stream)) { await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false); stream.Position = 0; using (var reader = StreamObjectReader.TryGetReader(stream)) { var recovered = serializer.Deserialize<object>(asset.Kind, reader, CancellationToken.None); var assetFromStorage = SolutionAsset.Create(serializer.CreateChecksum(recovered, CancellationToken.None), recovered, serializer); Assert.Equal(asset.Checksum, assetFromStorage.Checksum); return assetFromStorage; } } }
private static async Task VerifyOptionSetsAsync(Workspace workspace, string language) { var assetBuilder = new CustomAssetBuilder(workspace); var serializer = new Serializer(workspace); var asset = assetBuilder.Build(workspace.Options, language, CancellationToken.None); using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new StreamObjectWriter(stream)) { await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false); stream.Position = 0; using (var reader = StreamObjectReader.TryGetReader(stream)) { var recovered = serializer.Deserialize<OptionSet>(asset.Kind, reader, CancellationToken.None); var assetFromStorage = assetBuilder.Build(recovered, language, CancellationToken.None); Assert.Equal(asset.Checksum, assetFromStorage.Checksum); // option should be exactly same Assert.Equal(0, recovered.GetChangedOptions(workspace.Options).Count()); } } }
private static async Task<bool> SaveAsync( Document document, string persistenceName, string formatVersion, SyntaxTreeIndex data, CancellationToken cancellationToken) { Contract.Requires(!await document.IsForkedDocumentWithSyntaxChangesAsync(cancellationToken).ConfigureAwait(false)); var persistentStorageService = document.Project.Solution.Workspace.Services.GetService<IPersistentStorageService>(); try { using (var storage = persistentStorageService.GetStorage(document.Project.Solution)) using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new StreamObjectWriter(stream, cancellationToken: cancellationToken)) { data.WriteVersion(writer, formatVersion); data.WriteTo(writer); stream.Position = 0; return await storage.WriteStreamAsync(document, persistenceName, stream, cancellationToken).ConfigureAwait(false); } } catch (Exception e) when (IOUtilities.IsNormalIOException(e)) { // Storage APIs can throw arbitrary exceptions. } return false; }
private Checksum CreatePortableExecutableReferenceChecksum(PortableExecutableReference reference, CancellationToken cancellationToken) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new StreamObjectWriter(stream, cancellationToken: cancellationToken)) { WritePortableExecutableReferencePropertiesTo(reference, writer, cancellationToken); WriteMvidsTo(TryGetMetadata(reference), writer, cancellationToken); stream.Position = 0; return Checksum.Create(stream); } }
private static async Task WriteToVersionAsync( IPersistentStorage storage, string keyName, VersionStamp projectVersion, VersionStamp semanticVersion, CancellationToken cancellationToken) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new StreamObjectWriter(stream, cancellationToken: cancellationToken)) { writer.WriteInt32(SerializationFormat); projectVersion.WriteTo(writer); semanticVersion.WriteTo(writer); stream.Position = 0; await storage.WriteStreamAsync(keyName, stream, cancellationToken).ConfigureAwait(false); } }
private bool TrySaveIdentifierSetVersion( EsentStorage.IdentifierLocationTableAccessor accessor, EsentStorage.Key key, VersionStamp version) { int identifierId; if (!TryGetIdentifierSetVersionId(out identifierId)) { return false; } accessor.PrepareBatchOneInsert(); using (var stream = accessor.GetWriteStream(key, identifierId)) using (var writer = new StreamObjectWriter(stream)) { version.WriteTo(writer); } accessor.FinishBatchOneInsert(); return true; }
private bool WriteIdentifierLocations(EsentStorage.Key key, Document document, VersionStamp version, SyntaxNode root, CancellationToken cancellationToken) { // delete any existing data if (!DeleteIdentifierLocations(key, cancellationToken)) { return false; } var identifierMap = SharedPools.StringIgnoreCaseDictionary<int>().AllocateAndClear(); Dictionary<string, List<int>> map = null; try { map = CreateIdentifierLocations(document, root, cancellationToken); // okay, write new data using (var accessor = _esentStorage.GetIdentifierLocationTableAccessor()) { // make sure I have all identifier ready before starting big insertion int identifierId; foreach (var identifier in map.Keys) { if (!TryGetUniqueIdentifierId(identifier, out identifierId)) { return false; } identifierMap[identifier] = identifierId; } // save whole map var uncommittedCount = 0; foreach (var kv in map) { cancellationToken.ThrowIfCancellationRequested(); var identifier = kv.Key; var positions = kv.Value; if ((uncommittedCount + positions.Count) > FlushThreshold) { accessor.Flush(); uncommittedCount = 0; } accessor.PrepareBatchOneInsert(); identifierId = identifierMap[identifier]; using (var stream = accessor.GetWriteStream(key, identifierId)) using (var writer = new StreamObjectWriter(stream, cancellationToken: cancellationToken)) { writer.WriteString(IdentifierSetSerializationVersion); WriteList(writer, positions); } accessor.FinishBatchOneInsert(); uncommittedCount += positions.Count; } // save special identifier that indicates version for this document if (!TrySaveIdentifierSetVersion(accessor, key, version)) { return false; } return accessor.ApplyChanges(); } } finally { SharedPools.StringIgnoreCaseDictionary<int>().ClearAndFree(identifierMap); Free(map); } }