Skip to content

Commit

Permalink
Lets go back to the more simple design with snapshots for now.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeme committed Nov 29, 2023
1 parent 82b4891 commit d9731b0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 62 deletions.
18 changes: 7 additions & 11 deletions src/DotJEM.Json.Index2.Snapshots/IndexSnapshotHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace DotJEM.Json.Index2.Snapshots;

public interface ISnapshot : IDisposable
public interface ISnapshot
{
long Generation { get; }
ISnapshotReader OpenReader();
Expand All @@ -21,13 +21,14 @@ public interface ISnapshot : IDisposable

public interface IIndexSnapshotHandler
{
Task<ISnapshot> TakeSnapshotAsync(IJsonIndex index, ISnapshotStorage storage, bool leaveOpen = false);
Task<ISnapshot> RestoreSnapshotAsync(IJsonIndex index, ISnapshot source, bool leaveOpen = false);
Task<ISnapshot> TakeSnapshotAsync(IJsonIndex index, ISnapshotStorage storage);
Task<ISnapshot> RestoreSnapshotAsync(IJsonIndex index, ISnapshot source);
}

public class IndexSnapshotHandler : IIndexSnapshotHandler
{
public async Task<ISnapshot> TakeSnapshotAsync(IJsonIndex index, ISnapshotStorage storage, bool leaveOpen = false)

public async Task<ISnapshot> TakeSnapshotAsync(IJsonIndex index, ISnapshotStorage storage)
{
IndexWriter writer = index.WriterManager.Writer;
SnapshotDeletionPolicy sdp = writer.Config.IndexDeletionPolicy as SnapshotDeletionPolicy;
Expand All @@ -41,14 +42,9 @@ public async Task<ISnapshot> 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
Expand All @@ -60,7 +56,7 @@ public async Task<ISnapshot> TakeSnapshotAsync(IJsonIndex index, ISnapshotStorag
}
}

public async Task<ISnapshot> RestoreSnapshotAsync(IJsonIndex index, ISnapshot snapshot, bool leaveOpen = false)
public async Task<ISnapshot> RestoreSnapshotAsync(IJsonIndex index, ISnapshot snapshot)
{
index.Storage.Delete();
Directory dir = index.Storage.Directory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ namespace DotJEM.Json.Index2.Snapshots;

public static class LuceneSnapshotIndexExtension
{
public static async Task<ISnapshot> TakeSnapshotAsync(this IJsonIndex self, ISnapshotStorage storage, bool leaveOpen = false)
public static async Task<ISnapshot> 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<ISnapshot> RestoreSnapshotAsync(this IJsonIndex self, ISnapshot snapshot, bool leaveOpen = false)
public static async Task<ISnapshot> 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)
Expand Down
63 changes: 16 additions & 47 deletions src/DotJEM.Json.Index2.Snapshots/Zip/ZipFileSnapshot.cs
Original file line number Diff line number Diff line change
@@ -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<ISnapshotReader> snapshotReader;
private readonly Lazy<ISnapshotWriter> 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<ISnapshotReader>(() => new ZipSnapshotReader(this));
this.snapshotWriter = isReadonly
? new Lazy<ISnapshotWriter>(() => throw new InvalidOperationException())
: new Lazy<ISnapshotWriter>(() => 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;
}
}
}

0 comments on commit d9731b0

Please sign in to comment.