SpanTuple

Namespace: SnowBank.Data.Tuples.Binary · struct

Implements: IVarTuple, IEquatable<IVarTuple>, IComparable<IVarTuple>, IReadOnlyList<object>, IEnumerable<object>, IEnumerable, IReadOnlyCollection<object>, IStructuralEquatable, IStructuralComparable, ITuple, IFormattable, IEquatable<SpanTuple>, IComparable<SpanTuple>, IComparable, ITupleSpanPackable, ITuplePackable, ISliceSerializable, ITupleFormattable

Lazily-evaluated tuple that was unpacked from a key

Constructors

SpanTuple

SpanTuple(ReadOnlySpan<byte> buffer, ReadOnlySpan<Range> slices)

Properties

Count

int Count { get; }

Empty

static SpanTuple Empty { get; }

Item

object Item { get; }

SpanTuple Item { get; }

object Item { get; }

SpanTuple Item { get; }

Methods

AppendItemsTo

int AppendItemsTo(ref FastStringBuilder sb)

Writes the string representation of the items of this tuple

  • sb — Output buffer

Returns: Number of items written

This method should not emit any "(" or ")" delimiters, but should insert ", " separators between items

CompareTo

int CompareTo(SpanTuple other)

int CompareTo(IVarTuple other)

int CompareTo(object other)

CopyTo

void CopyTo(Span<object> destination)

void CopyTo(Span<Slice> destination)

void CopyTo(object[] array, int offset)

Copies all items of the tuple into an array at a specific location.

  • array — The destination array, which must be large enough to contain all the items.
  • offset — The offset at which to start copying items.
var tmp = new object[3];
("Hello", "World", 123).CopyTo(tmp, 0);

Equals

bool Equals(object obj)

bool Equals(SpanTuple other)

bool Equals(IVarTuple other)

Get

T Get<T>(int index)

Returns the typed value of an item of the tuple, given its position

  • index — Position of the item

Returns: Value of the item at position index, adapted into type TItem.

STuple.Create("Hello", "World", 123,).Get<string>(0) => "Hello"STuple.Create("Hello", "World", 123,).Get<int>(2) => 123STuple.Create("Hello", "World", 123,).Get<string>(2) => "123"

T Get<T>(Index index)

Returns the typed value of an item of the tuple, given its position

  • index — Position of the item, with 0 for the first element, and ^1 for the last element

Returns: Value of the item at position index, adapted into type T.

("Hello", "World", 123,).Get<string>(^3) => "Hello"

("Hello", "World", 123,).Get<string>(^2) => "World"

("Hello", "World", 123,).Get<int>(^1) => 123

("Hello", "World", 123,).Get<string>(^1) => "123"

GetElementType

TupleSegmentType GetElementType(int index)

Returns the encoded type of the element at the specified index

Returns: This helps test if a parser element is a string, a number, a boolean, etc...

TupleSegmentType GetElementType(Index index)

Returns the encoded type of the element at the specified index

GetEnumerator

IEnumerator<object> GetEnumerator()

GetFirst

T GetFirst<T>()

Returns the typed value of the first item in this tuple

Returns: Value of the item at the first position, adapted into type T.

("Hello", "World").First<string>() => "Hello"

(123, 456).First<int>() => 123

(123, 456).First<string>() => "123"

GetHashCode

int GetHashCode()

GetLast

T GetLast<T>()

Returns the typed value of the last item of the tuple

Returns: Value of the item at the last position, adapted into type T.

("Hello",).Last<string>() => "Hello"

(123, 456).Last<int>() => 456

(123, 456).Last<string>() => "456"

GetSlice

Slice GetSlice(int index)

Returns the encoded binary representation of the element at the specified index

Slice GetSlice(Index index)

Return the encoded binary representation of the element at the specified index

GetSpan

ReadOnlySpan<byte> GetSpan(int index)

Returns the encoded binary representation of the element at the specified index

ReadOnlySpan<byte> GetSpan(Index index)

Return the encoded binary representation of the element at the specified index

IsBytes

bool IsBytes(int index)

Test if the element at the specified index is a ByteString

Returns: true if this the value is a byte string

This could either be an array of bytes, OR a non-unicode Python string

IsBytesOrDefault

bool IsBytesOrDefault(int index)

Test if the element at the specified index is either a ByteString or a Nil

Returns: true if this the value is either a byte string, or null

This could either be an array of bytes, OR a non-unicode Python string

IsDouble

bool IsDouble(int index)

Test if the element at the specified index is a Double

IsFloatingPoint

bool IsFloatingPoint(int index)

Test if the element at the specified index is any of the floating points types (Single, Double, Triple or Decimal)

IsInteger

bool IsInteger(int index)

Test if the element at the specified index is an Integer

IsNil

bool IsNil(int index)

Test if the element at the specified index is Nil

Returns: true if this the value is either a byte string, or null

IsNumber

bool IsNumber(int index)

Test if the element at the specified index is a number of any type (integer or floating point)

IsSingle

bool IsSingle(int index)

Test if the element at the specified index is a Single

IsUnicodeString

bool IsUnicodeString(int index)

Test if the element at the specified index is a UnicodeString

Returns: true if this the value is either a unicode string

IsUnicodeStringOrDefault

bool IsUnicodeStringOrDefault(int index)

Test if the element at the specified index is either a UnicodeString or a Nil

Returns: true if this the value is either a unicode string, or null

PackTo

void PackTo(TupleWriter writer)

Appends the packed bytes of this instance to the end of a buffer

  • writer — Buffer that will receive the packed bytes of this instance

Repack

static SpanTuple Repack<TTuple>(in TTuple tuple)

Transcode a tuple into the equivalent tuple, but backed by a SpanTuple

This methods can be useful to examine what the result of packing a tuple would be, after a round-trip to the database.

ToArray

object[] ToArray()

ToSlice

Slice ToSlice()

Return the original serialized key blob that is equivalent to this tuple

Returns: Packed tuple

SpanTuple.Unpack(bytes).ToSlice() == bytes

ToString

string ToString()

Returns a human-readable representation of this tuple

void ToString(ref FastStringBuilder sb)

string ToString(string format, IFormatProvider provider = null)

Returns a human-readable representation of this tuple

ToTuple

IVarTuple ToTuple()

TryCopyTo

bool TryCopyTo(Span<object> destination)

bool TryCopyTo(Span<Slice> destination)

TryGetSizeHint

bool TryGetSizeHint(bool embedded, out int sizeHint)

Attempts to quickly estimate the minimum buffer capacity required to pack this tuple

  • embeddedtrue if this is an embedded tuple, false if this is the top-level
  • sizeHint — Receives the estimated size, or 0

Returns: true if the capacity could be quickly estimated, or false if it would require almost as much work as encoding the tuple.

The result MUST include the start/stop bytes for embedded tuples (which will be accounted by the parent)

TryPackTo

bool TryPackTo(ref TupleSpanWriter writer)

Appends the packed bytes of this instance to the end of a buffer, if it is large enough

  • writer — Buffer that will receive the packed bytes of this instance

true if the operation was successful, or false if it was too small

Unpack

static SpanTuple Unpack(Slice packedKey)

Unpack a tuple from a serialized key blob

  • packedKey — Binary key containing a previously packed tuple

Returns: Unpacked tuple, or the empty tuple if the key is Empty

This is the same as Unpack, except that it will expose the concrete type SpanTuple instead of the IVarTuple interface.

static SpanTuple Unpack(ReadOnlySpan<byte> packedKey)

Unpack a tuple from a serialized key blob

  • packedKey — Binary key containing a previously packed tuple

Returns: Unpacked tuple, or the empty tuple if the key is Empty

This is the same as Unpack, except that it will expose the concrete type SpanTuple instead of the IVarTuple interface.

static SpanTuple Unpack(ReadOnlySpan<byte> packedKey, Span<Range> buffer)

Unpack a tuple from a serialized key blob, using a caller-supplied buffer for the element ranges

  • packedKey — Binary key containing a previously packed tuple
  • buffer — Receives the range of each element, typically stackalloc Range[8]. The returned tuple reads from this buffer, so it must stay valid and untouched while the tuple is used.

Returns: Unpacked tuple, or the empty tuple if packedKey is empty

WriteTo

void WriteTo(ref SliceWriter writer)

Serializes the current instance to the specified output buffer