diff --git a/src/DotJEM.Json.Index2.Snapshots/IndexSnapshotHandler.cs b/src/DotJEM.Json.Index2.Snapshots/IndexSnapshotHandler.cs index 495dfad..56a0501 100644 --- a/src/DotJEM.Json.Index2.Snapshots/IndexSnapshotHandler.cs +++ b/src/DotJEM.Json.Index2.Snapshots/IndexSnapshotHandler.cs @@ -12,7 +12,7 @@ namespace DotJEM.Json.Index2.Snapshots; -public interface ISnapshot : IDisposable +public interface ISnapshot { long Generation { get; } ISnapshotReader OpenReader(); @@ -21,13 +21,14 @@ public interface ISnapshot : IDisposable public interface IIndexSnapshotHandler { - Task TakeSnapshotAsync(IJsonIndex index, ISnapshotStorage storage, bool leaveOpen = false); - Task RestoreSnapshotAsync(IJsonIndex index, ISnapshot source, bool leaveOpen = false); + Task TakeSnapshotAsync(IJsonIndex index, ISnapshotStorage storage); + Task RestoreSnapshotAsync(IJsonIndex index, ISnapshot source); } public class IndexSnapshotHandler : IIndexSnapshotHandler { - public async Task TakeSnapshotAsync(IJsonIndex index, ISnapshotStorage storage, bool leaveOpen = false) + + public async Task TakeSnapshotAsync(IJsonIndex index, ISnapshotStorage storage) { IndexWriter writer = index.WriterManager.Writer; SnapshotDeletionPolicy sdp = writer.Config.IndexDeletionPolicy as SnapshotDeletionPolicy; @@ -41,14 +42,9 @@ public async Task TakeSnapshotAsync(IJsonIndex index, ISnapshotStorag Directory dir = commit.Directory; ISnapshot snapshot = storage.CreateSnapshot(commit); - ISnapshotWriter snapshotWriter = snapshot.OpenWriter(); + using ISnapshotWriter snapshotWriter = snapshot.OpenWriter(); foreach (string fileName in commit.FileNames) await snapshotWriter.WriteFileAsync(fileName, dir); - - if (leaveOpen) - return snapshot; - - snapshot.Dispose(); return snapshot; } finally @@ -60,7 +56,7 @@ public async Task TakeSnapshotAsync(IJsonIndex index, ISnapshotStorag } } - public async Task RestoreSnapshotAsync(IJsonIndex index, ISnapshot snapshot, bool leaveOpen = false) + public async Task RestoreSnapshotAsync(IJsonIndex index, ISnapshot snapshot) { index.Storage.Delete(); Directory dir = index.Storage.Directory; diff --git a/src/DotJEM.Json.Index2.Snapshots/LuceneSnapshotIndexExtension.cs b/src/DotJEM.Json.Index2.Snapshots/LuceneSnapshotIndexExtension.cs index adbc1b4..b82246d 100644 --- a/src/DotJEM.Json.Index2.Snapshots/LuceneSnapshotIndexExtension.cs +++ b/src/DotJEM.Json.Index2.Snapshots/LuceneSnapshotIndexExtension.cs @@ -6,16 +6,16 @@ namespace DotJEM.Json.Index2.Snapshots; public static class LuceneSnapshotIndexExtension { - public static async Task TakeSnapshotAsync(this IJsonIndex self, ISnapshotStorage storage, bool leaveOpen = false) + public static async Task TakeSnapshotAsync(this IJsonIndex self, ISnapshotStorage storage) { IIndexSnapshotHandler handler = self.ResolveSnapshotHandler(); - return await handler.TakeSnapshotAsync(self, storage, leaveOpen); + return await handler.TakeSnapshotAsync(self, storage); } - public static async Task RestoreSnapshotAsync(this IJsonIndex self, ISnapshot snapshot, bool leaveOpen = false) + public static async Task RestoreSnapshotAsync(this IJsonIndex self, ISnapshot snapshot) { IIndexSnapshotHandler handler = self.ResolveSnapshotHandler(); - return await handler.RestoreSnapshotAsync(self, snapshot, leaveOpen); + return await handler.RestoreSnapshotAsync(self, snapshot); } public static IJsonIndexBuilder WithSnapshoting(this IJsonIndexBuilder self) diff --git a/src/DotJEM.Json.Index2.Snapshots/Zip/ZipFileSnapshot.cs b/src/DotJEM.Json.Index2.Snapshots/Zip/ZipFileSnapshot.cs index 5a4ea99..af464f6 100644 --- a/src/DotJEM.Json.Index2.Snapshots/Zip/ZipFileSnapshot.cs +++ b/src/DotJEM.Json.Index2.Snapshots/Zip/ZipFileSnapshot.cs @@ -1,57 +1,26 @@ using System; using System.Globalization; using System.IO; -using DotJEM.Json.Index2.Util; -namespace DotJEM.Json.Index2.Snapshots.Zip -{ - public class ZipFileSnapshot : Disposable, ISnapshot - { - public long Generation { get; } - public string FilePath { get; } - - private readonly Lazy snapshotReader; - private readonly Lazy snapshotWriter; - - public ISnapshotReader OpenReader() - { - EnsureNotDisposed(); - return snapshotReader.Value; - } - - public ISnapshotWriter OpenWriter() - { - EnsureNotDisposed(); - return snapshotWriter.Value; - } +namespace DotJEM.Json.Index2.Snapshots.Zip; - public ZipFileSnapshot(string path) - : this(path, long.Parse(Path.GetFileNameWithoutExtension(path), NumberStyles.AllowHexSpecifier)) - { - } - - public ZipFileSnapshot(string path, long generation, bool isReadonly = false) - { - FilePath = path; - Generation = generation; +public class ZipFileSnapshot : ISnapshot +{ + public long Generation { get; } + public string FilePath { get; } - this.snapshotReader = new Lazy(() => new ZipSnapshotReader(this)); - this.snapshotWriter = isReadonly - ? new Lazy(() => throw new InvalidOperationException()) - : new Lazy(() => new ZipSnapshotWriter(this)); - } + public ISnapshotReader OpenReader() => new ZipSnapshotReader(this); - protected override void Dispose(bool disposing) - { - if (!disposing || disposed) - return; - base.Dispose(true); + public ISnapshotWriter OpenWriter() => new ZipSnapshotWriter(this); - if(snapshotReader.IsValueCreated) - snapshotReader.Value.Dispose(); + public ZipFileSnapshot(string path) + : this(path, long.Parse(Path.GetFileNameWithoutExtension(path), NumberStyles.AllowHexSpecifier)) + { + } - if(snapshotWriter.IsValueCreated) - snapshotWriter.Value.Dispose(); - } + public ZipFileSnapshot(string path, long generation) + { + FilePath = path; + Generation = generation; } -} \ No newline at end of file +}