Skip to content

Commit

Permalink
make looselinkedlist safe
Browse files Browse the repository at this point in the history
  • Loading branch information
quinchs committed Mar 4, 2024
1 parent 0aa1c3f commit 878fbf4
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 606 deletions.
3 changes: 3 additions & 0 deletions examples/EdgeDB.Examples.CSharp/Examples/QueryBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public async Task ExecuteAsync(EdgeDBClient client)

private static async Task QueryBuilderDemo(EdgeDBClient client)
{
QueryBuilder
.SelectExpression(x => EdgeQL.Count(QueryBuilder.Select<Person>()));

// Selecting a type with autogen shape
var query = QueryBuilder.Select<Person>().Build().Prettify();

Expand Down
8 changes: 4 additions & 4 deletions src/EdgeDB.Net.QueryBuilder/Grammar/Terms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public static QueryWriter Wrapped(this QueryWriter writer, Value value, string s

public static QueryWriter Wrapped(this QueryWriter writer, WriterProxy value, string separator = "()")
{
if (separator.Length != 2)
if (separator.Length != 2)
throw new ArgumentOutOfRangeException(nameof(separator));

return writer.Append('(', value, ')');
return writer.Append(separator[0], value, separator[1]);
}

public static QueryWriter WrappedValues(this QueryWriter writer, string separator = "()", params Value[] values)
Expand Down Expand Up @@ -128,12 +128,12 @@ public static QueryWriter Function(this QueryWriter writer, string name, params
{
var arg = args[i++];

if(writer.AppendIsEmpty(arg.Value, out _, out var nodeRef))
if(writer.AppendIsEmpty(arg.Value, out _, out var node))
continue;

// append the named part if its specified
if (arg.Named is not null)
writer.Prepend(ref nodeRef.Value, Value.Of(writer => writer.Append(arg.Named, " := ")));
writer.Prepend(node, Value.Of(writer => writer.Append(arg.Named, " := ")));

if (i != args.Length)
writer.Append(", ");
Expand Down
6 changes: 3 additions & 3 deletions src/EdgeDB.Net.QueryBuilder/Lexical/Marker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ internal sealed class Marker
public int Position { get; private set; }
public int Size { get; }

public RefBox<LooseLinkedList<Value>.Node> Start { get; set; }
public LooseLinkedList<Value>.Node Start { get; set; }

/// <summary>
/// Gets the closest parent to this marker
Expand Down Expand Up @@ -51,7 +51,7 @@ public IOrderedEnumerable<Marker> Parents

private readonly QueryWriter _writer;

internal Marker(MarkerType type, QueryWriter writer, int size, int position, RefBox<LooseLinkedList<Value>.Node> start)
internal Marker(MarkerType type, QueryWriter writer, int size, int position, LooseLinkedList<Value>.Node start)
{
Type = type;
_writer = writer;
Expand All @@ -68,7 +68,7 @@ internal void Update(int delta)
public void Replace(Value value)
{
_writer
.Replace(ref Start.Value, Position, Size, value);
.Replace(Start, Position, Size, value);
}

public void Replace(WriterProxy value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

internal interface INodeObserver
{
void OnAdd(ref LooseLinkedList<Value>.Node node);
void OnRemove(ref LooseLinkedList<Value>.Node node);
void OnAdd(LooseLinkedList<Value>.Node node);
void OnRemove(LooseLinkedList<Value>.Node node);
}
22 changes: 10 additions & 12 deletions src/EdgeDB.Net.QueryBuilder/Lexical/Observers/LastNodeObserver.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
using System.Runtime.CompilerServices;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace EdgeDB;

internal sealed class LastNodeObserver : INodeObserver, IDisposable
{
[MemberNotNullWhen(true, nameof(Value))]
public bool HasValue
=> !_resultBox.IsNull;
=> Value is not null;

public ref LooseLinkedList<Value>.Node Value
=> ref _resultBox.Value;
public LooseLinkedList<Value>.Node? Value { get; private set; }


private readonly RefBox<LooseLinkedList<Value>.Node> _resultBox = RefBox<LooseLinkedList<Value>.Node>.Null;

private readonly QueryWriter _writer;

public LastNodeObserver(QueryWriter writer)
Expand All @@ -21,17 +20,16 @@ public LastNodeObserver(QueryWriter writer)
_writer.AddObserver(this);
}

public void OnAdd(ref LooseLinkedList<Value>.Node node)
public void OnAdd(LooseLinkedList<Value>.Node node)
{
_resultBox.Set(ref node);
Value = node;
}

public void OnRemove(ref LooseLinkedList<Value>.Node node)
public void OnRemove(LooseLinkedList<Value>.Node node)
{
unsafe
if (Value == node)
{
if(_resultBox.Pointer == Unsafe.AsPointer(ref node))
_resultBox.Set(ref Unsafe.NullRef<LooseLinkedList<Value>.Node>());
Value = null;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/EdgeDB.Net.QueryBuilder/Lexical/Observers/ValueSpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace EdgeDB;

internal sealed class ValueSpan : INodeObserver, IDisposable
{
private readonly List<Value> _nodes;
private readonly List<ValueNode> _nodes;

private readonly QueryWriter _writer;

Expand All @@ -15,14 +15,14 @@ public ValueSpan(QueryWriter writer)
writer.AddObserver(this);
}

public void OnAdd(ref ValueNode node)
public void OnAdd(ValueNode node)
{
_nodes.Add(node.Value);
_nodes.Add(node);
}

public void OnRemove(ref ValueNode node)
public void OnRemove(ValueNode node)
{
_nodes.Remove(node.Value);
_nodes.Remove(node);
}

public void Dispose()
Expand All @@ -32,6 +32,6 @@ public void Dispose()

public Value[] ToTokens()
{
return _nodes.ToArray();
return _nodes.Select(x => x.Value).ToArray();
}
}
Loading

0 comments on commit 878fbf4

Please sign in to comment.