public async Task Foo() { using (var store = new FilesStore()) { Stream content = new MemoryStream(); Stream newContent = new MemoryStream(); #region optimistic_concurrency_1 using (IAsyncFilesSession session = store.OpenAsyncSession()) { session.Advanced.UseOptimisticConcurrency = true; session.RegisterUpload("ravendb.exe", content, new RavenJObject { { "Copyright", "hibernatingrhinos.com" } }); await session.SaveChangesAsync(); using (IAsyncFilesSession otherSession = store.OpenAsyncSession()) { FileHeader fileInOtherSession = await otherSession.LoadFileAsync("ravendb.exe"); fileInOtherSession.Metadata["Copyright"] = "Hibernating Rhinos LTD"; await otherSession.SaveChangesAsync(); } FileHeader file = await session.LoadFileAsync("ravendb.exe"); session.RegisterUpload(file, newContent); await session.SaveChangesAsync(); // will throw ConcurrencyException } #endregion #region optimistic_concurrency_2 store.Conventions.DefaultUseOptimisticConcurrency = true; using (IAsyncFilesSession session = store.OpenAsyncSession()) { bool isSessionUsingOptimisticConcurrency = session.Advanced.UseOptimisticConcurrency; // will return true } #endregion #region optimistic_concurrency_3 using (IAsyncFilesSession session = store.OpenAsyncSession()) { FileHeader file = await session.LoadFileAsync("git.exe"); session.RegisterFileDeletion(file, file.Etag); await session.SaveChangesAsync(); } #endregion } }
private Task put_files(IAsyncFilesSession session, string[] files) { foreach (var file in files) { session.RegisterUpload(file, CreateRandomFileStream(3)); } return(session.SaveChangesAsync()); }
public async Task Foo() { IFilesStore store = null; #region session_usage_1 using (IAsyncFilesSession session = store.OpenAsyncSession()) { using (Stream content = File.OpenRead(@"C:\intro.avi")) { session.RegisterUpload("/movies/intro.avi", content); await session.SaveChangesAsync(); } } using (IAsyncFilesSession session = store.OpenAsyncSession()) { FileHeader file = await session.LoadFileAsync("/movies/intro.avi"); using (Stream content = await session.DownloadAsync(file.FullPath)) { /* ... */ } if (file.CreationDate < DateTime.Now.AddDays(-1)) { session.RegisterFileDeletion(file); } await session.SaveChangesAsync(); } #endregion using (IAsyncFilesSession session = store.OpenAsyncSession()) { #region unit_of_work_1 Assert.Same(await session.LoadFileAsync("/movies/intro.avi"), await session.LoadFileAsync("/movies/intro.avi")); #endregion } #region unit_of_work_2 using (IAsyncFilesSession session = store.OpenAsyncSession()) { FileHeader file = await session.LoadFileAsync("/movies/intro.avi"); file.Metadata.Add("Owner", "James"); await session.SaveChangesAsync(); // will sent the metadata update to the file system } #endregion }
public async Task Foo() { IFilesStore store = null; using (IAsyncFilesSession session = store.OpenAsyncSession()) { #region register_upload_3 using (Stream content = File.OpenRead(@"C:\intro.avi")) { session.RegisterUpload("/movies/intro.avi", content); await session.SaveChangesAsync(); } #endregion } using (IAsyncFilesSession session = store.OpenAsyncSession()) { #region register_upload_4 session.RegisterUpload("random.bin", 128, stream => { var bytes = new byte[128]; new Random().NextBytes(bytes); stream.Write(bytes, 0, 128); }); await session.SaveChangesAsync(); #endregion } using (IAsyncFilesSession session = store.OpenAsyncSession()) { #region register_upload_5 FileHeader ravenFile = await session.LoadFileAsync("/movies/intro.avi"); string localFile = @"C:\intro.avi"; if (ravenFile == null || new FileInfo(localFile).LastWriteTime - ravenFile.LastModified > TimeSpan.FromHours(1)) { using (Stream content = File.OpenRead(localFile)) { session.RegisterUpload(ravenFile, content); await session.SaveChangesAsync(); } } #endregion } }
private async Task InsertTask(Guid scenarioId, string fileId, string localFolder) { var localFile = Path.Combine(localFolder, GetFileName(fileId, false)); if (!System.IO.File.Exists(localFile)) { throw new FileNotFoundException($"Results file {localFile} does not exist"); } var uploadFilePath = GetUploadFilePath(scenarioId, fileId, _compression); using (var stream = File.OpenRead(localFile)) using (var outputStream = CompressGZIP(stream)) { _session.RegisterUpload(uploadFilePath, outputStream); await _session.SaveChangesAsync().ConfigureAwait(false); // actually upload the file } }
private Task put_files(IAsyncFilesSession session, string[] files) { foreach (var file in files) { session.RegisterUpload(file, CreateRandomFileStream(3)); } return session.SaveChangesAsync(); }