Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #498

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/YesSql.Abstractions/IConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public interface IConfiguration
ISqlDialect SqlDialect { get; set; }

/// <summary>
/// Gets or sets the identity column size. Default is <see cref="IdentityColumnSize.Int32"/>.
/// Gets or sets the identity column size. Default is <see cref="YesSql.IdentityColumnSize.Int32"/>.
/// </summary>
IdentityColumnSize IdentityColumnSize { get; set; }
}
Expand Down
4 changes: 1 addition & 3 deletions src/YesSql.Abstractions/Indexes/DescribeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public IMapFor<T, TIndex> For<TIndex>() where TIndex : IIndex

public IMapFor<T, TIndex> For<TIndex, TKey>() where TIndex : IIndex
{
List<IDescribeFor> descriptors;

if (!_describes.TryGetValue(typeof(T), out descriptors))
if (!_describes.TryGetValue(typeof(T), out var descriptors))
{
descriptors = _describes[typeof(T)] = new List<IDescribeFor>();
}
Expand Down
13 changes: 5 additions & 8 deletions src/YesSql.Abstractions/Indexes/DescribeFor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class IndexDescriptor<T, TIndex, TKey> : IDescribeFor, IMapFor<T, TIndex>
private Func<object, bool> _filter;

public PropertyInfo GroupProperty { get; set; }
public Type IndexType { get { return typeof(TIndex); } }
public Type IndexType => typeof(TIndex);

public Func<object, bool> Filter => _filter;

Expand Down Expand Up @@ -102,10 +102,10 @@ public IReduceFor<TIndex, TKeyG> Group<TKeyG>(Expression<Func<TIndex, TKeyG>> gr

GroupProperty = property;

var reduceDescibeFor = new IndexDescriptor<T, TIndex, TKeyG>();
_reduceDescribeFor = reduceDescibeFor;
var reduceDescribeFor = new IndexDescriptor<T, TIndex, TKeyG>();
_reduceDescribeFor = reduceDescribeFor;

return reduceDescibeFor;
return reduceDescribeFor;
}

public IDeleteFor<TIndex> Reduce(Func<IGrouping<TKey, TIndex>, TIndex> reduce)
Expand Down Expand Up @@ -166,10 +166,7 @@ public GroupedEnumerable(object key, IEnumerable<IIndex> enumerable)
_enumerable = enumerable;
}

public TKey Key
{
get { return (TKey)_key; }
}
public TKey Key => (TKey)_key;

public IEnumerator<TIndex> GetEnumerator()
{
Expand Down
5 changes: 1 addition & 4 deletions src/YesSql.Abstractions/Indexes/IndexProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ void IIndexProvider.Describe(IDescriptor context)

public string CollectionName { get; set; }

public Type ForType()
{
return typeof(T);
}
public Type ForType() => typeof(T);
}
}
8 changes: 4 additions & 4 deletions src/YesSql.Abstractions/Indexes/MapIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ namespace YesSql.Indexes
{
public abstract class MapIndex : IIndex
{
private Document Document { get; set; }
private Document _document;

public long Id { get; set; }

void IIndex.AddDocument(Document document)
{
Document = document;
_document = document;
}

void IIndex.RemoveDocument(Document document)
{
Document = null;
_document = null;
}

IEnumerable<Document> IIndex.GetAddedDocuments()
{
yield return Document;
yield return _document;
}

IEnumerable<Document> IIndex.GetRemovedDocuments()
Expand Down
28 changes: 8 additions & 20 deletions src/YesSql.Abstractions/Indexes/ReduceIndex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,24 @@ namespace YesSql.Indexes
{
public class ReduceIndex : IIndex
{
public ReduceIndex()
{
Documents = new List<Document>();
}

private readonly List<Document> _removedDocuments = new();
private readonly List<Document> _documents = new();

public long Id { get; set; }

List<Document> RemovedDocuments = new List<Document>();

private List<Document> Documents { get; set; }

void IIndex.AddDocument(Document document)
{
Documents.Add(document);
_documents.Add(document);
}

void IIndex.RemoveDocument(Document document)
{
Documents.Remove(document);
RemovedDocuments.Add(document);
_documents.Remove(document);
_removedDocuments.Add(document);
}

IEnumerable<Document> IIndex.GetAddedDocuments()
{
return Documents;
}
IEnumerable<Document> IIndex.GetAddedDocuments() => _documents;

IEnumerable<Document> IIndex.GetRemovedDocuments()
{
return RemovedDocuments;
}
IEnumerable<Document> IIndex.GetRemovedDocuments() => _removedDocuments;
}
}
2 changes: 1 addition & 1 deletion src/YesSql.Abstractions/QueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static IQuery<T, TIndex> Query<T, TIndex>(this ISession session, Expressi
where T : class
where TIndex : class, IIndex
{
return session.Query(collection).For<T>(filterType).With<TIndex>(predicate);
return session.Query(collection).For<T>(filterType).With(predicate);
}

/// <summary>
Expand Down
15 changes: 7 additions & 8 deletions src/YesSql.Filters.Abstractions/Builders/BooleanEngineBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@

using YesSql.Filters.Abstractions.Nodes;
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Nodes;
using YesSql.Filters.Services;
using Parlot.Fluent;
using static Parlot.Fluent.Parsers;

namespace YesSql.Filters.Abstractions.Builders
namespace YesSql.Filters.Builders
{
public abstract class BooleanEngineBuilder<T, TTermOption> : OperatorEngineBuilder<T, TTermOption> where TTermOption : TermOption
{
private static Parser<OperatorNode> _parser;
private static readonly Parser<OperatorNode> _parser;
protected TTermOption _termOption;

static BooleanEngineBuilder()
Expand All @@ -35,7 +34,7 @@ static BooleanEngineBuilder()

// Default operator.
var OrOperator = Literals.WhiteSpace()
.Then<string>(static x => " ") // Normalize whitespace.
.Then(static _ => " ") // Normalize whitespace.
.AndSkip(Not(NotOrOperators))
.Or(
OrTextOperators
Expand Down Expand Up @@ -72,7 +71,7 @@ static BooleanEngineBuilder()
.Or(Primary);

var AndNode = UnaryNode.And(ZeroOrMany(AndOperator.And(UnaryNode)))
.Then<OperatorNode>(static node =>
.Then(static node =>
{
// unary
var result = node.Item1;
Expand All @@ -86,7 +85,7 @@ static BooleanEngineBuilder()
});

OperatorNode.Parser = AndNode.And(ZeroOrMany(NotOperator.Or(OrOperator).And(AndNode)))
.Then<OperatorNode>(static (node) =>
.Then(static (node) =>
{
static NotNode CreateNotNode(OperatorNode result, (string, OperatorNode) op)
=> new NotNode(result, new UnaryNode(((UnaryNode)op.Item2).Value, ((UnaryNode)op.Item2).Quotes, false), op.Item1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using YesSql.Filters.Abstractions.Nodes;
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Nodes;
using YesSql.Filters.Services;
using Parlot.Fluent;
using static Parlot.Fluent.Parsers;

namespace YesSql.Filters.Abstractions.Builders
namespace YesSql.Filters.Builders
{
public class DefaultTermEngineBuilder<T, TTermOption> : TermEngineBuilder<T, TTermOption> where T : class where TTermOption : TermOption
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using YesSql.Filters.Abstractions.Nodes;
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Nodes;
using YesSql.Filters.Services;
using Parlot.Fluent;
using static Parlot.Fluent.Parsers;

namespace YesSql.Filters.Abstractions.Builders
namespace YesSql.Filters.Builders
{
public class NamedTermEngineBuilder<T, TTermOption> : TermEngineBuilder<T, TTermOption> where TTermOption : TermOption
{
Expand Down
6 changes: 3 additions & 3 deletions src/YesSql.Filters.Abstractions/Builders/OperatorBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using YesSql.Filters.Abstractions.Nodes;
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Nodes;
using YesSql.Filters.Services;
using Parlot.Fluent;

namespace YesSql.Filters.Abstractions.Builders
namespace YesSql.Filters.Builders
{
public abstract class OperatorEngineBuilder<T, TTermOption> where TTermOption : TermOption
{
Expand Down
8 changes: 4 additions & 4 deletions src/YesSql.Filters.Abstractions/Builders/TermEngineBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using YesSql.Filters.Abstractions.Nodes;
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Nodes;
using YesSql.Filters.Services;
using Parlot.Fluent;

namespace YesSql.Filters.Abstractions.Builders
namespace YesSql.Filters.Builders
{
public abstract class TermEngineBuilder<T, TTermOption> where TTermOption : TermOption
{
public TermEngineBuilder(string name)
protected TermEngineBuilder(string name)
{
Name = name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using YesSql.Filters.Abstractions.Nodes;
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Nodes;
using YesSql.Filters.Services;
using Parlot.Fluent;
using static Parlot.Fluent.Parsers;

namespace YesSql.Filters.Abstractions.Builders
namespace YesSql.Filters.Builders
{
public abstract class UnaryEngineBuilder<T, TTermOption> : OperatorEngineBuilder<T, TTermOption> where TTermOption : TermOption
{
Expand All @@ -15,7 +15,7 @@ public abstract class UnaryEngineBuilder<T, TTermOption> : OperatorEngineBuilder

protected TTermOption _termOption;

public UnaryEngineBuilder(TTermOption termOption)
protected UnaryEngineBuilder(TTermOption termOption)
{
_termOption = termOption;
}
Expand Down
24 changes: 24 additions & 0 deletions src/YesSql.Filters.Abstractions/Nodes/AndNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using YesSql.Filters.Services;

namespace YesSql.Filters.Nodes;

public class AndNode : OperatorNode
{
public AndNode(OperatorNode left, OperatorNode right, string value)
{
Left = left;
Right = right;
Value = value;
}

public OperatorNode Left { get; }
public OperatorNode Right { get; }
public string Value { get; }

public override TResult Accept<TArgument, TResult>(IFilterVisitor<TArgument, TResult> visitor, TArgument argument)
=> visitor.Visit(this, argument);
public override string ToNormalizedString()
=> $"({Left.ToNormalizedString()} AND {Right.ToNormalizedString()})";

public override string ToString() => $"{Left} {Value} {Right}";
}
22 changes: 22 additions & 0 deletions src/YesSql.Filters.Abstractions/Nodes/AndTermNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Linq;
using YesSql.Filters.Services;

namespace YesSql.Filters.Nodes;

public class AndTermNode : CompoundTermNode
{
public AndTermNode(TermOperationNode existingTerm, TermOperationNode newTerm) : base(existingTerm.TermName)
{
Children.Add(existingTerm);
Children.Add(newTerm);
}

public override TResult Accept<TArgument, TResult>(IFilterVisitor<TArgument, TResult> visitor, TArgument argument)
=> visitor.Visit(this, argument);

public override string ToNormalizedString()
=> string.Join(" ", Children.Select(c => c.ToNormalizedString()));

public override string ToString()
=> string.Join(" ", Children.Select(c => c.ToString()));
}
12 changes: 12 additions & 0 deletions src/YesSql.Filters.Abstractions/Nodes/CompoundTermNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Collections.Generic;

namespace YesSql.Filters.Nodes;

public abstract class CompoundTermNode : TermNode
{
protected CompoundTermNode(string termName) : base(termName)
{
}

public List<TermOperationNode> Children { get; } = new List<TermOperationNode>();
}
13 changes: 13 additions & 0 deletions src/YesSql.Filters.Abstractions/Nodes/DefaultTermNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace YesSql.Filters.Nodes;

public class DefaultTermNode : TermOperationNode
{
public DefaultTermNode(string termName, OperatorNode operation) : base(termName, operation)
{
}

public override string ToNormalizedString() // normalizing includes the term name even if not specified.
=> $"{TermName}:{Operation.ToNormalizedString()}";

public override string ToString() => $"{Operation}";
}
4 changes: 2 additions & 2 deletions src/YesSql.Filters.Abstractions/Nodes/FilterNode.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using YesSql.Filters.Abstractions.Services;
using YesSql.Filters.Services;

namespace YesSql.Filters.Abstractions.Nodes
namespace YesSql.Filters.Nodes
{
public abstract class FilterNode
{
Expand Down
24 changes: 24 additions & 0 deletions src/YesSql.Filters.Abstractions/Nodes/GroupNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using YesSql.Filters.Services;

namespace YesSql.Filters.Nodes;

/// <summary>
/// Marks a node as being produced by a group request, i.e. () were specified
/// </summary>
public class GroupNode : OperatorNode
{
public GroupNode(OperatorNode operation)
{
Operation = operation;
}

public OperatorNode Operation { get; }

public override TResult Accept<TArgument, TResult>(IFilterVisitor<TArgument, TResult> visitor, TArgument argument)
=> visitor.Visit(this, argument);

public override string ToNormalizedString()
=> ToString();

public override string ToString() => $"({Operation})";
}
12 changes: 12 additions & 0 deletions src/YesSql.Filters.Abstractions/Nodes/NamedTermNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace YesSql.Filters.Nodes;

public class NamedTermNode : TermOperationNode
{
public NamedTermNode(string termName, OperatorNode operation) : base(termName, operation)
{
}

public override string ToNormalizedString() => $"{TermName}:{Operation.ToNormalizedString()}";

public override string ToString() => $"{TermName}:{Operation}";
}
Loading
Loading