-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
239 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using EdgeDB.Binary.Protocol.Common.Descriptors; | ||
using EdgeDB.DataTypes; | ||
using EdgeDB.Models.DataTypes; | ||
|
||
namespace EdgeDB.Binary.Codecs; | ||
|
||
internal sealed class MultiRangeCodec<T> : BaseCodec<MultiRange<T>>, IWrappingCodec, ICacheableCodec | ||
where T : struct | ||
{ | ||
private RangeCodec<T> _rangeCodec; | ||
|
||
public MultiRangeCodec(in Guid id, ICodec<T> rangeInnerCodec, CodecMetadata? metadata) : base(in id, metadata) | ||
{ | ||
_rangeCodec = new RangeCodec<T>(in id, rangeInnerCodec, metadata); | ||
} | ||
|
||
public override void Serialize(ref PacketWriter writer, MultiRange<T> value, CodecContext context) | ||
{ | ||
writer.Write(value.Length); | ||
|
||
for (int i = 0; i != value.Length; i++) | ||
{ | ||
writer.WriteToWithInt32Length( | ||
(ref PacketWriter innerWriter) => _rangeCodec.Serialize(ref innerWriter, value[i], context)); | ||
} | ||
} | ||
|
||
public override MultiRange<T> Deserialize(ref PacketReader reader, CodecContext context) | ||
{ | ||
var length = reader.ReadInt32(); | ||
|
||
var elements = new Range<T>[length]; | ||
|
||
for (int i = 0; i != length; i++) | ||
{ | ||
reader.Limit = reader.ReadInt32(); | ||
elements[i] = _rangeCodec.Deserialize(ref reader, context); | ||
reader.Limit = -1; | ||
} | ||
|
||
return new MultiRange<T>(elements); | ||
} | ||
|
||
public ICodec InnerCodec | ||
{ | ||
get => _rangeCodec; | ||
set | ||
{ | ||
if (value is not RangeCodec<T> r) | ||
throw new ArgumentException($"Expected a range codec, but got {value}"); | ||
|
||
_rangeCodec = r; | ||
} | ||
} | ||
|
||
public override string ToString() | ||
=> "multirange"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/EdgeDB.Net.Driver/Binary/Protocol/V2.0/Descriptors/MultiRangeDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using EdgeDB.Binary.Protocol.Common.Descriptors; | ||
|
||
namespace EdgeDB.Binary.Protocol.V2._0.Descriptors; | ||
|
||
internal readonly struct MultiRangeDescriptor : ITypeDescriptor, IMetadataDescriptor | ||
{ | ||
public readonly Guid Id; | ||
|
||
public readonly string Name; | ||
|
||
public readonly bool IsSchemaDefined; | ||
|
||
public readonly ushort[] Ancestors; | ||
|
||
public readonly ushort Type; | ||
public MultiRangeDescriptor(ref PacketReader reader, in Guid id) | ||
{ | ||
Id = id; | ||
|
||
Name = reader.ReadString(); | ||
IsSchemaDefined = reader.ReadBoolean(); | ||
|
||
var ancestorsCount = reader.ReadUInt16(); | ||
var ancestors = new ushort[ancestorsCount]; | ||
|
||
for (var i = 0; i != ancestorsCount; i++) | ||
{ | ||
ancestors[i] = reader.ReadUInt16(); | ||
} | ||
|
||
Ancestors = ancestors; | ||
|
||
Type = reader.ReadUInt16(); | ||
} | ||
|
||
|
||
public CodecMetadata? GetMetadata(RelativeCodecDelegate relativeCodec, | ||
RelativeDescriptorDelegate relativeDescriptor) | ||
=> new(Name, IsSchemaDefined, | ||
IMetadataDescriptor.ConstructAncestors(Ancestors, relativeCodec, relativeDescriptor)); | ||
|
||
unsafe ref readonly Guid ITypeDescriptor.Id | ||
{ | ||
get | ||
{ | ||
fixed (Guid* ptr = &Id) | ||
return ref *ptr; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
using EdgeDB.DataTypes; | ||
using System.Collections; | ||
|
||
namespace EdgeDB.Models.DataTypes; | ||
|
||
/// <summary> | ||
/// Represents the <c>multirange</c> type in EdgeDB. | ||
/// </summary> | ||
/// <typeparam name="T">The inner type of the multirange.</typeparam> | ||
public readonly struct MultiRange<T> : IEnumerable<Range<T>> | ||
where T : struct | ||
{ | ||
/// <summary> | ||
/// Gets the length of this multirange. | ||
/// </summary> | ||
public int Length | ||
=> _ranges.Length; | ||
|
||
/// <summary> | ||
/// Gets a <see cref="Range{T}"/> element within this multirange. | ||
/// </summary> | ||
/// <param name="i"></param> | ||
public readonly ref Range<T> this[int i] | ||
=> ref _ranges[i]; | ||
|
||
private readonly Range<T>[] _ranges; | ||
|
||
/// <summary> | ||
/// Constructs a new <see cref="MultiRange{T}"/>. | ||
/// </summary> | ||
/// <param name="set">A set of ranges to put within this multirange.</param> | ||
public MultiRange(HashSet<Range<T>> set) | ||
{ | ||
_ranges = set.ToArray(); | ||
} | ||
|
||
internal MultiRange(Range<T>[] ranges) | ||
{ | ||
_ranges = ranges; | ||
} | ||
|
||
#region Enumerator | ||
private sealed class MultiRangeEnumerator : IEnumerator<Range<T>> | ||
{ | ||
private readonly MultiRange<T> _multiRange; | ||
private int _index; | ||
|
||
internal MultiRangeEnumerator(in MultiRange<T> multiRange) | ||
{ | ||
_multiRange = multiRange; | ||
_index = -1; | ||
} | ||
|
||
public bool MoveNext() | ||
{ | ||
var index = _index + 1; | ||
if (index >= _multiRange.Length) | ||
{ | ||
_index = _multiRange.Length; | ||
return false; | ||
} | ||
_index = index; | ||
return true; | ||
} | ||
|
||
public void Reset() => _index = -1; | ||
|
||
public Range<T> Current | ||
{ | ||
get | ||
{ | ||
var index = _index; | ||
|
||
if (index >= _multiRange.Length) | ||
{ | ||
if (index < 0) | ||
{ | ||
throw new InvalidOperationException("Enumeration hasn't started"); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("The enumeration has finished"); | ||
} | ||
} | ||
|
||
return _multiRange[index]; | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ } | ||
|
||
object IEnumerator.Current => Current; | ||
} | ||
#endregion | ||
|
||
/// <inheritdoc /> | ||
public IEnumerator<Range<T>> GetEnumerator() => new MultiRangeEnumerator(in this); | ||
|
||
/// <inheritdoc /> | ||
IEnumerator IEnumerable.GetEnumerator() => _ranges.GetEnumerator(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters