Skip to content

Commit

Permalink
Merge pull request #10 from iamcarbon/cq11
Browse files Browse the repository at this point in the history
Improve Code Quality (7 of x)
  • Loading branch information
PetroProtsyk authored Feb 11, 2023
2 parents 65b938f + a69fb67 commit 2b97c48
Show file tree
Hide file tree
Showing 27 changed files with 173 additions and 134 deletions.
41 changes: 26 additions & 15 deletions Src/Protsyk.PMS.FullText.Core.UnitTests/TextPositionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@ private void TextIndex(IFullTextIndex index)
{
Assert.Equal(new TextPosition[]
{
TextPosition.P(0, 5),
TextPosition.P(6, 5)
new(0, 5),
new(6, 5)
},
index.GetPositions(1, 1));

Assert.Equal(new TextPosition[]
{
TextPosition.P(0, 5),
TextPosition.P(6, 9),
TextPosition.P(16, 6)
new(0, 5),
new(6, 9),
new(16, 6)
},
index.GetPositions(2, 1));

Assert.Equal(new TextPosition[]
{
TextPosition.P(0, 11),
TextPosition.P(12, 2),
TextPosition.P(15, 4),
TextPosition.P(20, 8),
TextPosition.P(30, 8),
TextPosition.P(39, 3),
TextPosition.P(43, 4),
TextPosition.P(49, 4),
TextPosition.P(54, 2),
TextPosition.P(57, 9)
new(0, 11),
new(12, 2),
new(15, 4),
new(20, 8),
new(30, 8),
new(39, 3),
new(43, 4),
new(49, 4),
new(54, 2),
new(57, 9)
},
index.GetPositions(6, 1));

Expand All @@ -71,4 +71,15 @@ private void TextIndex(IFullTextIndex index)
Assert.Equal("very", doc.TokenAt(15));
Assert.Equal("fantastic", doc.TokenAt(60));
}

[Theory]
[InlineData("[0,0]", 0, 0)]
[InlineData("[1,5]", 1, 5)]
public void CanParse(string text, int offset, int length)
{
var result = TextPosition.Parse(text);

Assert.Equal(offset, result.Offset);
Assert.Equal(length, result.Length);
}
}
4 changes: 2 additions & 2 deletions Src/Protsyk.PMS.FullText.Core/Automata/DFA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Protsyk.PMS.FullText.Core.Automata;
/// </summary>
public class DFA
{
public static readonly int NoState = -1;
public const int NoState = -1;

private readonly List<List<(CharRange, int)>> transitions = new();
private readonly HashSet<int> final = new ();
Expand All @@ -33,7 +33,7 @@ public void AddTransition(int from, int to, CharRange c)

public int Next(int s, char c)
{
if (s == NoState)
if (s is NoState)
{
return NoState;
}
Expand Down
2 changes: 1 addition & 1 deletion Src/Protsyk.PMS.FullText.Core/Automata/NFA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private void EpsilonClosure(HashSet<int> setState)
return transitions.Where(t => t.Item1 == from_state);
}

class SetStateComparer : IEqualityComparer<HashSet<int>>
private sealed class SetStateComparer : IEqualityComparer<HashSet<int>>
{
public bool Equals(HashSet<int> x, HashSet<int> y)
{
Expand Down
4 changes: 2 additions & 2 deletions Src/Protsyk.PMS.FullText.Core/Collections/BtreePersistent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1405,8 +1405,8 @@ public static DataLink FromBytes(ReadOnlySpan<byte> buffer)

private readonly struct NodeData
{
public static readonly int HeaderLength = 3 * sizeof(int);
public static readonly int DataHeaderLength = HeaderLength + sizeof(int);
public const int HeaderLength = 3 * sizeof(int);
public const int DataHeaderLength = HeaderLength + sizeof(int);

private readonly byte[] data;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class PersistentHashTable<TKey, TValue> : IEnumerable<KeyValuePair<TKey,
private const int MinCapacity = 8;
private static ReadOnlySpan<byte> Header => "PMS-HASH"u8;
private static readonly int HeaderSize = Header.Length;
private static readonly int IndexRecordSize = sizeof(long) + sizeof(int);
private const int IndexRecordSize = sizeof(long) + sizeof(int);

private readonly IDataSerializer<TKey> keySerializer;
private readonly IDataSerializer<TValue> valueSerializer;
Expand Down Expand Up @@ -54,7 +54,7 @@ public PersistentHashTable(IPersistentStorage dataStorage, int capacity, IEquali
this.dataStorage.WriteInt32LittleEndian(HeaderSize + sizeof(int), count);

// Index
var zero = new byte[IndexRecordSize*1024];
var zero = new byte[IndexRecordSize * 1_024];
var i = 0;
while (i < capacity)
{
Expand All @@ -63,7 +63,7 @@ public PersistentHashTable(IPersistentStorage dataStorage, int capacity, IEquali
{
toWrite = (capacity-i) % 1024;
}
this.dataStorage.WriteAll(headerSize + i*IndexRecordSize, zero.AsSpan(0, IndexRecordSize * toWrite));
this.dataStorage.WriteAll(headerSize + i* IndexRecordSize, zero.AsSpan(0, IndexRecordSize * toWrite));
i += toWrite;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ namespace Protsyk.PMS.FullText.Core.Common.Compression;

public partial class HuTuckerBuilder
{
class CombinationList
private sealed class CombinationList
{
ListItem First { get; set; }
ListItem Last { get; set; }
public List<TreeLeaf> list = new List<TreeLeaf>();
public List<TreeLeaf> list = new();

public void Add(TreeLeaf item)
{
Expand All @@ -34,10 +34,12 @@ public void Add(TreeLeaf item)

public TreeNode Combine(ListItem left, ListItem right)
{
var node = new TreeNode();
node.Left = left;
node.Right = right;
node.W = left.W + right.W;
var node = new TreeNode
{
Left = left,
Right = right,
W = left.W + right.W
};

if (left.Next != right)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ public string GetCode()
}
}

class HuffmanEncoding : VarLenCharEncoding
private sealed class HuffmanEncoding : VarLenCharEncoding
{
public HuffmanEncoding(IEncodingNode root)
: base(root)
{
}
}

class TreeNode : IEncodingNode
private class TreeNode : IEncodingNode
{
private static int nextId = 0;

Expand All @@ -88,7 +88,7 @@ public TreeNode(int weight)
}
}

class CharNode : TreeNode, IEncodingLeafNode
private sealed class CharNode : TreeNode, IEncodingLeafNode
{
public readonly char c;

Expand All @@ -101,7 +101,7 @@ public CharNode(char c, int weight)
char IEncodingLeafNode.V => c;
}

class CombineNode : TreeNode, IEncodingTreeNode
private sealed class CombineNode : TreeNode, IEncodingTreeNode
{
public readonly TreeNode left;
public readonly TreeNode right;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
}

class ByteToBitEn : IEnumerator<byte>
private sealed class ByteToBitEn : IEnumerator<byte>
{
private readonly IEnumerator<byte> data;
private int c;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,5 @@ public int GetMaxEncodedLength(int maxTokenLength)
=> 4 * maxTokenLength;

public IDfaMatcher<byte> CreateMatcher(IDfaMatcher<char> charMatcher, int maxLength)
=> (IDfaMatcher<byte>)new DecodingMatcherForVarLenCharEncoding(charMatcher, encoding);
=> new DecodingMatcherForVarLenCharEncoding(charMatcher, encoding);
}
45 changes: 45 additions & 0 deletions Src/Protsyk.PMS.FullText.Core/Helpers/StringSplitter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Protsyk.PMS;

internal ref struct StringSplitter
{
private readonly ReadOnlySpan<char> _text;
private readonly char _separator;

private int _position;

public StringSplitter(ReadOnlySpan<char> text, char separator)
{
_text = text;
_separator = separator;
_position = 0;
}

public bool TryRead(out ReadOnlySpan<char> result)
{
if (_position == _text.Length)
{
result = default;

return false;
}

int start = _position;

int separatorIndex = _text.Slice(_position).IndexOf(_separator);

if (separatorIndex > -1)
{
_position += separatorIndex + 1;

result = _text.Slice(start, separatorIndex);
}
else
{
_position = _text.Length;

result = _text.Slice(start);
}

return true;
}
}
53 changes: 23 additions & 30 deletions Src/Protsyk.PMS.FullText.Core/IndexModels/Occurrence.cs
Original file line number Diff line number Diff line change
@@ -1,72 +1,67 @@
using System.Globalization;
using System.Text.RegularExpressions;

namespace Protsyk.PMS.FullText.Core;

public readonly struct Occurrence : IEquatable<Occurrence>, IComparable<Occurrence>
{
#region Fields
/// <summary>
/// Not valid Id
/// </summary>
public static readonly ulong NoId = 0;
public const ulong NoId = 0;

/// <summary>
/// Empty occurrence
/// </summary>
public static readonly Occurrence Empty = new(NoId, NoId, NoId);

private static readonly Regex regParse = new Regex("^\\[(?<docId>\\d+),(?<fieldId>\\d+),(?<tokenId>\\d+)\\]$",
RegexOptions.Compiled | RegexOptions.Singleline);
public Occurrence(ulong documentId, ulong fieldId, ulong tokenId)
{
DocumentId = documentId;
FieldId = fieldId;
TokenId = tokenId;
}

/// <summary>
/// Document Id
/// </summary>
public readonly ulong DocumentId;
public ulong DocumentId { get; }

/// <summary>
/// Fields Id
/// </summary>
public readonly ulong FieldId;
public ulong FieldId { get; }

/// <summary>
/// Token Id
/// </summary>
public readonly ulong TokenId;
#endregion
public ulong TokenId { get; }

#region Static Methods

public static Occurrence Parse(string text)
public static Occurrence Parse(ReadOnlySpan<char> text)
{
var match = regParse.Match(text);
if (!match.Success)
if (text is [ '[', .. var inner, ']' ])
{
throw new InvalidOperationException($"Occurrence text has invalid format: {text}");
var splitter = new StringSplitter(inner, ',');

if (splitter.TryRead(out var t0) && ulong.TryParse(t0, NumberStyles.None, CultureInfo.InvariantCulture, out ulong docId) &&
splitter.TryRead(out var t1) && ulong.TryParse(t1, NumberStyles.None, CultureInfo.InvariantCulture, out ulong fieldId) &&
splitter.TryRead(out var t2) && ulong.TryParse(t2, NumberStyles.None, CultureInfo.InvariantCulture, out ulong tokenId))
{
return new Occurrence(docId, fieldId, tokenId);
}
}

return new Occurrence(
ulong.Parse(match.Groups["docId"].ValueSpan, provider: CultureInfo.InvariantCulture),
ulong.Parse(match.Groups["fieldId"].ValueSpan, provider: CultureInfo.InvariantCulture),
ulong.Parse(match.Groups["tokenId"].ValueSpan, provider: CultureInfo.InvariantCulture));
throw new InvalidOperationException($"Occurrence text has invalid format. Was {text}");
}
#endregion

#region Methods
public Occurrence(ulong documentId, ulong fieldId, ulong tokenId)
{
this.DocumentId = documentId;
this.FieldId = fieldId;
this.TokenId = tokenId;
}
#endregion

public override string ToString()
{
return string.Create(CultureInfo.InvariantCulture, $"[{DocumentId},{FieldId},{TokenId}]");
}
#endregion

#region IEquatable<Occurrence>

public bool Equals(Occurrence other)
{
return DocumentId == other.DocumentId &&
Expand All @@ -81,8 +76,6 @@ public override bool Equals(object obj)

public override int GetHashCode() => HashCode.Combine(DocumentId, FieldId, TokenId);

#endregion

#region IComparable
public int CompareTo(Occurrence other)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public readonly struct PostingListAddress : IEquatable<PostingListAddress>
{
public static PostingListAddress Null = new(-1);
public static readonly PostingListAddress Null = new(-1);

public readonly long Offset;

Expand Down
Loading

0 comments on commit 2b97c48

Please sign in to comment.