Skip to content

Commit

Permalink
fixed some naming issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeme committed Nov 10, 2023
1 parent 7ca0e44 commit daf47fc
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public async Task SayHello_ReturnsHell2o()

Assert.That(search.Count(), Is.EqualTo(6));
}

[Test, Explicit]
public async Task SayHello_ReturnsHell2()
{
Expand Down
4 changes: 3 additions & 1 deletion src/DotJEM.Json.Index2.Snapshots/ISnapshotReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

namespace DotJEM.Json.Index2.Snapshots
{
public interface ISnapshotReader : IDisposable, IEnumerable<ILuceneFile>
public interface ISnapshotReader : IDisposable
{
ISnapshot Snapshot { get; }

IEnumerable<ILuceneFile> ReadFiles();
}
}
5 changes: 2 additions & 3 deletions src/DotJEM.Json.Index2.Snapshots/ISnapshotWriter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using System;
using System.Threading.Tasks;
using Lucene.Net.Store;

namespace DotJEM.Json.Index2.Snapshots
{
public interface ISnapshotWriter : IDisposable
{
ISnapshot Snapshot { get; }

void WriteFile(string fileName, Directory dir);
void WriteSegmentsFile(string segmentsFile, Directory dir);
Task WriteFileAsync(string fileName, Directory dir);
}
}
76 changes: 0 additions & 76 deletions src/DotJEM.Json.Index2.Snapshots/IndexInputStream.cs

This file was deleted.

29 changes: 11 additions & 18 deletions src/DotJEM.Json.Index2.Snapshots/IndexSnapshotHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using DotJEM.Json.Index2.Snapshots.Streams;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Directory = Lucene.Net.Store.Directory;

namespace DotJEM.Json.Index2.Snapshots
{
Expand All @@ -21,24 +24,17 @@ public ISnapshot Snapshot(IJsonIndex index, ISnapshotTarget target)
IndexWriter writer = index.WriterManager.Writer;
SnapshotDeletionPolicy sdp = writer.Config.IndexDeletionPolicy as SnapshotDeletionPolicy;
if (sdp == null)
{
throw new InvalidOperationException("Index must use an implementation of the SnapshotDeletionPolicy.");
}

IndexCommit commit = null;
try
{
commit = sdp.Snapshot();
Directory dir = commit.Directory;
string segmentsFile = commit.SegmentsFileName;

using ISnapshotWriter snapshotWriter = target.Open(commit.Generation);
foreach (string fileName in commit.FileNames)
{
if (!fileName.Equals(segmentsFile, StringComparison.Ordinal))
snapshotWriter.WriteFile(fileName, dir);
}
snapshotWriter.WriteSegmentsFile(segmentsFile, dir);
snapshotWriter.WriteFileAsync(fileName, dir);
return snapshotWriter.Snapshot;
}
finally
Expand All @@ -58,30 +54,27 @@ public ISnapshot Restore(IJsonIndex index, ISnapshotSource source)

ILuceneFile sementsFile = null;
List<string> files = new List<string>();
foreach (ILuceneFile file in reader)
foreach (ILuceneFile file in reader.ReadFiles())
{
if (Regex.IsMatch(file.Name, "^" + IndexFileNames.SEGMENTS + "_.*$"))
{
sementsFile = file;
continue;
}
IndexOutput output = dir.CreateOutput(file.Name, IOContext.DEFAULT);
output.WriteBytes(file.Bytes, 0, file.Length);
output.Flush();
output.Dispose();

using IndexOutputStream output = dir.CreateOutputStream(file.Name, IOContext.DEFAULT);
using Stream sourceStream = file.Open();
sourceStream.CopyToAsync(output);
files.Add(file.Name);
}
dir.Sync(files);

if (sementsFile == null)
throw new ArgumentException();

IndexOutput segOutput = dir.CreateOutput(sementsFile.Name, IOContext.DEFAULT);
segOutput.WriteBytes(sementsFile.Bytes, 0, sementsFile.Length);
segOutput.Flush();
segOutput.Dispose();

using IndexOutputStream segOutput = dir.CreateOutputStream(sementsFile.Name, IOContext.DEFAULT);
using Stream sementsSourceStream = sementsFile.Open();
sementsSourceStream.CopyToAsync(segOutput);
dir.Sync(new [] { sementsFile.Name });

SegmentInfos.WriteSegmentsGen(dir, reader.Snapshot.Generation);
Expand Down
21 changes: 13 additions & 8 deletions src/DotJEM.Json.Index2.Snapshots/LuceneFile.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
namespace DotJEM.Json.Index2.Snapshots
using System;
using System.IO;

namespace DotJEM.Json.Index2.Snapshots
{

public interface ILuceneFile
{
string Name { get; }
byte[] Bytes { get; }
int Length { get; }
long Length { get; }
Stream Open();
}

public class LuceneFile : ILuceneFile
{
public byte[] Bytes { get; }
public int Length { get; }
private readonly Func<Stream> streamProvider;
public string Name { get; }
public long Length { get; }


public LuceneFile(string name, byte[] bytes)
public LuceneFile(string name, Func<Stream> streamProvider)
{
Bytes = bytes;
this.streamProvider = streamProvider;
Name = name;
Length = bytes.Length;
}
public Stream Open()
=> streamProvider.Invoke();
}
}
79 changes: 79 additions & 0 deletions src/DotJEM.Json.Index2.Snapshots/Streams/IndexInputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Lucene.Net.Store;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace DotJEM.Json.Index2.Snapshots.Streams;

public class IndexInputStream : Stream
{
public string FileName { get; }
public IndexInput IndexInput { get; }

public IndexInputStream(string fileName, IndexInput indexInput)
{
FileName = fileName;
IndexInput = indexInput;
}

public override void Flush()
{
throw new InvalidOperationException("Cannot flush a readonly stream.");
}

public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
Position = offset;
break;
case SeekOrigin.Current:
Position += offset;
break;
case SeekOrigin.End:
Position = Length - offset;
break;
}
return Position;
}

public override void SetLength(long value)
{
throw new InvalidOperationException("Cannot change length of a readonly stream.");
}

public override int Read(byte[] buffer, int offset, int count)
{
long remaining = IndexInput.Length - IndexInput.Position;
int readCount = (int)Math.Min(remaining, count);
IndexInput.ReadBytes(buffer, offset, readCount);
return readCount;
}

public override void Write(byte[] buffer, int offset, int count)
{
throw new InvalidCastException("Cannot write to a readonly stream.");
}

public override bool CanRead => true;
public override bool CanSeek => true;
public override bool CanWrite => false;
public override long Length => IndexInput.Length;

public override long Position
{
get => IndexInput.Position;
set => IndexInput.Seek(value);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
IndexInput.Dispose();
}
base.Dispose(disposing);
}
}
74 changes: 74 additions & 0 deletions src/DotJEM.Json.Index2.Snapshots/Streams/IndexOutputStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.IO;
using Lucene.Net.Store;

namespace DotJEM.Json.Index2.Snapshots.Streams;

public class IndexOutputStream : Stream
{
public string FileName { get; }
public IndexOutput IndexOutput { get; }

public IndexOutputStream(string fileName, IndexOutput indexOutput)
{
FileName = fileName;
IndexOutput = indexOutput;
}

public override void Flush()
{
IndexOutput.Flush();
}

public override long Seek(long offset, SeekOrigin origin)
{
switch (origin)
{
case SeekOrigin.Begin:
Position = offset;
break;
case SeekOrigin.Current:
Position += offset;
break;
case SeekOrigin.End:
Position = Length - offset;
break;
}
return Position;
}

public override void SetLength(long value)
{
IndexOutput.Length = Length;
}

public override int Read(byte[] buffer, int offset, int count)
{
throw new InvalidOperationException("Cannot read from a writeonly stream.");
}

public override void Write(byte[] buffer, int offset, int count)
{
IndexOutput.WriteBytes(buffer, offset, count);
}

public override bool CanRead => false;
public override bool CanSeek => true;
public override bool CanWrite => true;
public override long Length => IndexOutput.Length;

public override long Position
{
get => IndexOutput.Position;
set => IndexOutput.Seek(value);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
IndexOutput.Dispose();
}
base.Dispose(disposing);
}
}
Loading

0 comments on commit daf47fc

Please sign in to comment.