private static Checksum CreateChecksumFromStreamWriter(WellKnownSynchronizationKind kind, Action <ObjectWriter, CancellationToken> writer) { using var stream = SerializableBytes.CreateWritableStream(); using (var objectWriter = new ObjectWriter(stream, leaveOpen: true)) { objectWriter.WriteInt32((int)kind); writer(objectWriter, CancellationToken.None); } stream.Position = 0; return(Checksum.Create(stream)); }
private Checksum CreatePortableExecutableReferenceChecksum(PortableExecutableReference reference, CancellationToken cancellationToken) { using var stream = SerializableBytes.CreateWritableStream(); using (var writer = new ObjectWriter(stream, leaveOpen: true, cancellationToken)) { WritePortableExecutableReferencePropertiesTo(reference, writer, cancellationToken); WriteMvidsTo(TryGetMetadata(reference), writer, cancellationToken); } stream.Position = 0; return(Checksum.Create(stream)); }
/// <summary> /// this is for a project in a solution /// </summary> private static async Task <ValueTuple <bool, SymbolTreeInfo> > LoadOrCreateAsync(Project project, CancellationToken cancellationToken) { if (await project.IsForkedProjectWithSemanticChangesAsync(cancellationToken).ConfigureAwait(false)) { return(ValueTuple.Create(false, await CreateAsync(project, cancellationToken).ConfigureAwait(false))); } var persistentStorageService = WorkspaceService.GetService <IPersistentStorageService>(project.Solution.Workspace); var version = await project.GetSemanticVersionAsync(cancellationToken).ConfigureAwait(false); // attempt to load from persisted state SymbolTreeInfo info; var succeeded = false; using (var storage = persistentStorageService.GetStorage(project.Solution)) { using (var stream = await storage.ReadStreamAsync(project, ProjectSymbolTreeInfoPersistenceName, cancellationToken).ConfigureAwait(false)) { if (stream != null) { using (var reader = new ObjectReader(stream)) { info = ReadFrom(reader); if (info != null && VersionStamp.CanReusePersistedVersion(version, info.version)) { return(ValueTuple.Create(true, info)); } } } } cancellationToken.ThrowIfCancellationRequested(); // compute it if we couldn't load it from cache info = await CreateAsync(project, cancellationToken).ConfigureAwait(false); if (info != null) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new ObjectWriter(stream, cancellationToken: cancellationToken)) { info.WriteTo(writer); stream.Position = 0; succeeded = await storage.WriteStreamAsync(project, ProjectSymbolTreeInfoPersistenceName, stream, cancellationToken).ConfigureAwait(false); } } } return(ValueTuple.Create(succeeded, info)); }
private Stream ReadStream(EsentStorage.Key key, int nameId, object unused1, object unused2, CancellationToken cancellationToken) { using (var accessor = GetAccessor(key)) using (var esentStream = accessor.GetReadStream(key, nameId)) { if (esentStream == null) { return(null); } // this will copy over esent stream and let it go. return(SerializableBytes.CreateReadableStream(esentStream, cancellationToken)); } }
private static async Task <RemotableData> CloneAssetAsync(ISerializerService serializer, RemotableData asset) { using var stream = SerializableBytes.CreateWritableStream(); using var writer = new ObjectWriter(stream); await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false); stream.Position = 0; using var reader = ObjectReader.TryGetReader(stream); var recovered = serializer.Deserialize <object>(asset.Kind, reader, CancellationToken.None); var assetFromStorage = new SolutionAsset(serializer.CreateChecksum(recovered, CancellationToken.None), recovered, serializer); Assert.Equal(asset.Checksum, assetFromStorage.Checksum); return(assetFromStorage); }
private static async Task WriteToVersionAsync( IPersistentStorage storage, string keyName, VersionStamp projectVersion, VersionStamp semanticVersion, CancellationToken cancellationToken) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new ObjectWriter(stream, cancellationToken: cancellationToken)) { writer.WriteInt32(SerializationFormat); projectVersion.WriteTo(writer); semanticVersion.WriteTo(writer); stream.Position = 0; await storage.WriteStreamAsync(keyName, stream, cancellationToken).ConfigureAwait(false); } }
private Stream ReadStream(int projectId, int documentId, int nameId, object unused1, object unused2, CancellationToken cancellationToken) { using (var accessor = _esentStorage.GetDocumentTableAccessor()) using (var esentStream = accessor.GetReadStream(projectId, documentId, nameId)) { if (esentStream == null) { return(null); } // this will copy over esent stream and let it go. return(SerializableBytes.CreateReadableStream(esentStream, cancellationToken)); } }
protected override async Task SaveAsync(TRoot root, CancellationToken cancellationToken) { Contract.ThrowIfFalse(_storage == null); // Cannot save more than once // tree will be always held alive in memory, but nodes come and go. serialize nodes to storage using (var stream = SerializableBytes.CreateWritableStream()) { root.SerializeTo(stream, cancellationToken); stream.Position = 0; _storage = _service.LanguageServices.WorkspaceServices.GetService <ITemporaryStorageService>().CreateTemporaryStreamStorage(cancellationToken); await _storage.WriteStreamAsync(stream, cancellationToken).ConfigureAwait(false); } }
public void WritableStreamTest3() { using var expected = new MemoryStream(); using var stream = SerializableBytes.CreateWritableStream(); var random = new Random(0); for (var i = 0; i < 100; i++) { var position = random.Next(10000); WriteByte(expected, stream, position, i); } StreamEqual(expected, stream); }
public async Task TestUnresolvedAnalyzerReference() { var workspace = new AdhocWorkspace(); var project = workspace.CurrentSolution.AddProject("empty", "empty", LanguageNames.CSharp); var mockFileChangeService = new Mock <IVsFileChangeEx>(); using (var analyzer = new VisualStudioAnalyzer( @"PathToAnalyzer", fileChangeService: mockFileChangeService.Object, hostDiagnosticUpdateSource: null, projectId: project.Id, workspace: workspace, loader: null, language: project.Language)) { var analyzerReference = analyzer.GetReference(); project = project.WithAnalyzerReferences(new AnalyzerReference[] { analyzerReference, }); var checksum = await project.State.GetChecksumAsync(CancellationToken.None).ConfigureAwait(false); Assert.NotNull(checksum); var assetBuilder = new CustomAssetBuilder(workspace); var serializer = new Serializer(workspace); var asset = assetBuilder.Build(analyzerReference, CancellationToken.None); using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new ObjectWriter(stream)) { await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false); stream.Position = 0; using (var reader = ObjectReader.TryGetReader(stream)) { var recovered = serializer.Deserialize <AnalyzerReference>(asset.Kind, reader, CancellationToken.None); var assetFromStorage = assetBuilder.Build(recovered, CancellationToken.None); Assert.Equal(asset.Checksum, assetFromStorage.Checksum); // This won't round trip, but we should get an UnresolvedAnalyzerReference, with the same path Assert.Equal(analyzerReference.FullPath, recovered.FullPath); } } } }
public async Task <bool> SerializeAsync(object documentOrProject, string key, ImmutableArray <DiagnosticData> items, CancellationToken cancellationToken) { using var stream = SerializableBytes.CreateWritableStream(); using var writer = new ObjectWriter(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 static void WriteAllLines(this ITemporaryStreamStorage storage, ImmutableArray <string> values) { using var stream = SerializableBytes.CreateWritableStream(); using var writer = new StreamWriter(stream); foreach (var value in values) { writer.WriteLine(value); } writer.Flush(); stream.Position = 0; storage.WriteStream(stream); }
public static Checksum Create <TChecksums>(string kind, TChecksums checksums) where TChecksums : IEnumerable <Checksum> { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new ObjectWriter(stream)) { writer.WriteString(kind); foreach (var checksum in checksums) { checksum.WriteTo(writer); } return(Create(stream)); } }
private async Task <bool> WriteToStreamAsync(TValue value, TData data, CancellationToken cancellationToken) { using (var stream = SerializableBytes.CreateWritableStream()) { WriteTo(stream, data, cancellationToken); var solution = GetSolution(value); var persistService = solution.Workspace.Services.GetService <IPersistentStorageService>(); using (var storage = persistService.GetStorage(solution)) { stream.Position = 0; return(await WriteStreamAsync(storage, value, stream, cancellationToken).ConfigureAwait(false)); } } }
internal static async Task VerifySnapshotSerializationAsync(ISolutionChecksumService service, Solution solution, SolutionChecksumObject solutionId) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new ObjectWriter(stream)) { await solutionId.WriteToAsync(writer, CancellationToken.None).ConfigureAwait(false); stream.Position = 0; using (var reader = new ObjectReader(stream)) { var serializer = new Serializer(solution.Workspace.Services); var recovered = serializer.Deserialize <SolutionChecksumObject>(SolutionChecksumObject.Name, reader, CancellationToken.None); SnapshotEqual(service, solutionId, recovered); } } }
private Stream ReadBlobIntoPooledStream(SafeSqliteBlobHandle blob, int length) { var bytes = SQLitePersistentStorage.GetPooledBytes(); try { ThrowIfNotOk(NativeMethods.sqlite3_blob_read(blob, bytes, length, offset: 0)); // Copy those bytes into a pooled stream return(SerializableBytes.CreateReadableStream(bytes, length)); } finally { // Return our small array back to the pool. SQLitePersistentStorage.ReturnPooledBytes(bytes); } }
private static SolutionAsset CloneAsset(ISerializerService serializer, SolutionAsset asset) { using var stream = SerializableBytes.CreateWritableStream(); using (var writer = new ObjectWriter(stream, leaveOpen: true)) { serializer.Serialize(asset.Value, writer, CancellationToken.None); } stream.Position = 0; using var reader = ObjectReader.TryGetReader(stream); var recovered = serializer.Deserialize <object>(asset.Kind, reader, CancellationToken.None); var assetFromStorage = new SolutionAsset(serializer.CreateChecksum(recovered, CancellationToken.None), recovered); Assert.Equal(asset.Checksum, assetFromStorage.Checksum); return(assetFromStorage); }
public void WritableStreamTest1() { using var expected = new MemoryStream(); for (var i = 0; i < 10000; i++) { expected.WriteByte((byte)(i % byte.MaxValue)); } expected.Position = 0; using var stream = SerializableBytes.CreateWritableStream(); for (var i = 0; i < 10000; i++) { stream.WriteByte((byte)(i % byte.MaxValue)); } StreamEqual(expected, stream); }
public async Task TestUnresolvedAnalyzerReference() { using (var workspace = new TestWorkspace()) { var lazyWorkspace = new Lazy <VisualStudioWorkspaceImpl>(() => throw Utilities.ExceptionUtilities.Unreachable); var hostDiagnosticUpdateSource = new HostDiagnosticUpdateSource(lazyWorkspace, new MockDiagnosticUpdateSourceRegistrationService()); var project = workspace.CurrentSolution.AddProject("empty", "empty", LanguageNames.CSharp); using var analyzer = new VisualStudioAnalyzer( @"C:\PathToAnalyzer", hostDiagnosticUpdateSource, projectId: project.Id, language: project.Language); var analyzerReference = analyzer.GetReference(); project = project.WithAnalyzerReferences(new[] { analyzerReference }); var checksum = await project.State.GetChecksumAsync(CancellationToken.None).ConfigureAwait(false); Assert.NotNull(checksum); var serializer = workspace.Services.GetService <ISerializerService>(); var asset = WorkspaceAnalyzerReferenceAsset.Create(analyzerReference, serializer, CancellationToken.None); using var stream = SerializableBytes.CreateWritableStream(); using (var writer = new ObjectWriter(stream, leaveOpen: true)) { await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false); } stream.Position = 0; using (var reader = ObjectReader.TryGetReader(stream)) { var recovered = serializer.Deserialize <AnalyzerReference>(asset.Kind, reader, CancellationToken.None); var assetFromStorage = WorkspaceAnalyzerReferenceAsset.Create(recovered, serializer, CancellationToken.None); Assert.Equal(asset.Checksum, assetFromStorage.Checksum); // This won't round trip, but we should get an UnresolvedAnalyzerReference, with the same path Assert.Equal(analyzerReference.FullPath, recovered.FullPath); } } }
public void TestEncodingSerialization() { using var workspace = CreateWorkspace(); var serializer = workspace.Services.GetService <ISerializerService>(); // test with right serializable encoding var sourceText = SourceText.From("Hello", Encoding.UTF8); using (var stream = SerializableBytes.CreateWritableStream()) { using var context = SolutionReplicationContext.Create(); using (var objectWriter = new ObjectWriter(stream, leaveOpen: true)) { serializer.Serialize(sourceText, objectWriter, context, CancellationToken.None); } stream.Position = 0; using var objectReader = ObjectReader.TryGetReader(stream); var newText = serializer.Deserialize <SourceText>(sourceText.GetWellKnownSynchronizationKind(), objectReader, CancellationToken.None); Assert.Equal(sourceText.ToString(), newText.ToString()); } // test with wrong encoding that doesn't support serialization sourceText = SourceText.From("Hello", new NotSerializableEncoding()); using (var stream = SerializableBytes.CreateWritableStream()) { using var context = SolutionReplicationContext.Create(); using (var objectWriter = new ObjectWriter(stream, leaveOpen: true)) { serializer.Serialize(sourceText, objectWriter, context, CancellationToken.None); } stream.Position = 0; using var objectReader = ObjectReader.TryGetReader(stream); var newText = serializer.Deserialize <SourceText>(sourceText.GetWellKnownSynchronizationKind(), objectReader, CancellationToken.None); Assert.Equal(sourceText.ToString(), newText.ToString()); } }
private static async Task <(MSB.Evaluation.Project?project, DiagnosticLog log)> LoadProjectAsync( string path, MSB.Evaluation.ProjectCollection?projectCollection, CancellationToken cancellationToken ) { var log = new DiagnosticLog(); try { var loadedProjects = projectCollection?.GetLoadedProjects(path); if (loadedProjects != null && loadedProjects.Count > 0) { Debug.Assert(loadedProjects.Count == 1); return(loadedProjects.First(), log); } using var stream = FileUtilities.OpenAsyncRead(path); using var readStream = await SerializableBytes .CreateReadableStreamAsync(stream, cancellationToken) .ConfigureAwait(false); using var xmlReader = XmlReader.Create(readStream, s_xmlReaderSettings); var xml = MSB.Construction.ProjectRootElement.Create(xmlReader, projectCollection); // When constructing a project from an XmlReader, MSBuild cannot determine the project file path. Setting the // path explicitly is necessary so that the reserved properties like $(MSBuildProjectDirectory) will work. xml.FullPath = path; var project = new MSB.Evaluation.Project( xml, globalProperties: null, toolsVersion: null, projectCollection ); return(project, log); } catch (Exception e) { log.Add(e, path); return(project : null, log); } }
public void TestEncodingSerialization() { var hostServices = MefHostServices.Create( MefHostServices.DefaultAssemblies.Add(typeof(Host.TemporaryStorageServiceFactory.TemporaryStorageService).Assembly)); var workspace = new AdhocWorkspace(hostServices); var serializer = new Serializer(workspace); // test with right serializable encoding var sourceText = SourceText.From("Hello", Encoding.UTF8); using (var stream = SerializableBytes.CreateWritableStream()) { using (var objectWriter = new ObjectWriter(stream)) { serializer.Serialize(sourceText, objectWriter, CancellationToken.None); } stream.Position = 0; using (var objectReader = ObjectReader.TryGetReader(stream)) { var newText = serializer.Deserialize <SourceText>(sourceText.GetWellKnownSynchronizationKind(), objectReader, CancellationToken.None); Assert.Equal(sourceText.ToString(), newText.ToString()); } } // test with wrong encoding that doesn't support serialization sourceText = SourceText.From("Hello", new NotSerializableEncoding()); using (var stream = SerializableBytes.CreateWritableStream()) { using (var objectWriter = new ObjectWriter(stream)) { serializer.Serialize(sourceText, objectWriter, CancellationToken.None); } stream.Position = 0; using (var objectReader = ObjectReader.TryGetReader(stream)) { var newText = serializer.Deserialize <SourceText>(sourceText.GetWellKnownSynchronizationKind(), objectReader, CancellationToken.None); Assert.Equal(sourceText.ToString(), newText.ToString()); } } }
private static async Task <Asset> CloneAssetAsync(Serializer serializer, AssetBuilder assetBuilder, Asset asset) { using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new ObjectWriter(stream)) { await asset.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false); stream.Position = 0; using (var reader = new ObjectReader(stream)) { var recovered = serializer.Deserialize <object>(asset.Kind, reader, CancellationToken.None); var assetFromStorage = BuildAsset(assetBuilder, asset.Kind, recovered); Assert.Equal(asset.Checksum, assetFromStorage.Checksum); return(assetFromStorage); } } }
private static (byte[] bytes, int length, bool fromPool) GetBytes( Checksum checksumOpt, CancellationToken cancellationToken) { // If we weren't passed a checsum, just pass the singleton empty byte array. // Note: we don't add this to/from our pool. But it likely woudn't be a problem // for us to do that as this instance can't actually be mutated since it's just // an empty array. if (checksumOpt == null) { return(Array.Empty <byte>(), length : 0, fromPool : false); } using var stream = SerializableBytes.CreateWritableStream(); using var writer = new ObjectWriter(stream, cancellationToken: cancellationToken); checksumOpt.WriteTo(writer); stream.Position = 0; return(GetBytes(stream)); }
public async Task <bool> SerializeAsync(IPersistentStorageService persistentService, Project project, Document?document, string key, ImmutableArray <DiagnosticData> items, CancellationToken cancellationToken) { Contract.ThrowIfFalse(document == null || document.Project == project); using var stream = SerializableBytes.CreateWritableStream(); using var writer = new ObjectWriter(stream, cancellationToken: cancellationToken); WriteDiagnosticData(writer, items, cancellationToken); using var storage = persistentService.GetStorage(project.Solution); stream.Position = 0; var writeTask = (document != null) ? storage.WriteStreamAsync(document, key, stream, cancellationToken) : storage.WriteStreamAsync(project, key, stream, cancellationToken); return(await writeTask.ConfigureAwait(false)); }
public static async Task <T> GetValueAsync <T>(this IRemotableDataService service, Checksum checksum) { var syncService = (RemotableDataServiceFactory.Service)service; var syncObject = (await syncService.TestOnly_GetRemotableDataAsync(checksum, CancellationToken.None).ConfigureAwait(false)) !; using var stream = SerializableBytes.CreateWritableStream(); using (var writer = new ObjectWriter(stream, leaveOpen: true)) { await syncObject.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false); } stream.Position = 0; using var reader = ObjectReader.TryGetReader(stream); // deserialize bits to object var serializer = syncService.Serializer_TestOnly; return(serializer.Deserialize <T>(syncObject.Kind, reader, CancellationToken.None)); }
protected static async Task <bool> SaveAsync <T>( Document document, string persistenceName, string formatVersion, T data, CancellationToken cancellationToken) where T : AbstractPersistableState, IObjectWritable { Contract.Requires(!await document.IsForkedDocumentWithSyntaxChangesAsync(cancellationToken).ConfigureAwait(false)); var persistentStorageService = WorkspaceService.GetService <IPersistentStorageService>(document.Project.Solution.Workspace); // attempt to load from persisted state using (var storage = persistentStorageService.GetStorage(document.Project.Solution)) using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new ObjectWriter(stream, cancellationToken: cancellationToken)) { data.WriteVersion(writer, formatVersion); data.WriteTo(writer); stream.Position = 0; return(await storage.WriteStreamAsync(document, persistenceName, stream, cancellationToken).ConfigureAwait(false)); } }
private void GetStorageInfoFromTemporaryStorage(FileKey moduleFileKey, out ITemporaryStreamStorage storage, out Stream stream, out IntPtr pImage) { int size; using (var copyStream = SerializableBytes.CreateWritableStream()) { // open a file and let it go as soon as possible using (var fileStream = FileUtilities.OpenRead(moduleFileKey.FullPath)) { var headers = new PEHeaders(fileStream); var offset = headers.MetadataStartOffset; size = headers.MetadataSize; // given metadata contains no metadata info. // throw bad image format exception so that we can show right diagnostic to user. if (size <= 0) { throw new BadImageFormatException(); } StreamCopy(fileStream, copyStream, offset, size); } // copy over the data to temp storage and let pooled stream go storage = _temporaryStorageService.CreateTemporaryStreamStorage(CancellationToken.None); copyStream.Position = 0; storage.WriteStream(copyStream); } // get stream that owns direct access memory stream = storage.ReadStream(CancellationToken.None); // stream size must be same as what metadata reader said the size should be. Contract.ThrowIfFalse(stream.Length == size); // under VS host, direct access should be supported var directAccess = (ISupportDirectMemoryAccess)stream; pImage = directAccess.GetPointer(); }
public static async Task <T> GetValueAsync <T>(this ISolutionSynchronizationService service, Checksum checksum) { var syncService = (SolutionSynchronizationServiceFactory.Service)service; var syncObject = service.GetRemotableData(checksum, CancellationToken.None); using (var stream = SerializableBytes.CreateWritableStream()) using (var writer = new StreamObjectWriter(stream)) { // serialize asset to bits await syncObject.WriteObjectToAsync(writer, CancellationToken.None).ConfigureAwait(false); stream.Position = 0; using (var reader = new StreamObjectReader(stream)) { // deserialize bits to object var serializer = syncService.Serializer_TestOnly; return(serializer.Deserialize <T>(syncObject.Kind, reader, CancellationToken.None)); } } }
public async Task ReadableStreamTestReadAByteAtATime() { using var expected = new MemoryStream(); for (var i = 0; i < 10000; i++) { expected.WriteByte((byte)(i % byte.MaxValue)); } expected.Position = 0; using var stream = await SerializableBytes.CreateReadableStreamAsync(expected, CancellationToken.None); Assert.Equal(expected.Length, stream.Length); expected.Position = 0; stream.Position = 0; for (var i = 0; i < expected.Length; i++) { Assert.Equal(expected.ReadByte(), stream.ReadByte()); } }