示例#1
0
        private static unsafe bool IsCacheValid(ByteArrayReaderCache <ByteArray, string> byteArrayCache, ByteArray byteArray, string str)
        {
            var byteCount = System.Text.Encoding.UTF8.GetByteCount(str);
            var buffer    = byteArrayCache.GetScratchBuffer(byteCount);

            System.Text.Encoding.UTF8.GetBytes(str, 0, str.Length, buffer, 0);

            var cached   = new ReadOnlySpan <byte>((void *)byteArray.Pointer, byteArray.Length);
            var expected = buffer.AsSpan(0, byteCount);

            return(cached.SequenceEqual(expected));
        }
示例#2
0
        private static string ToString(ByteArray byteArray, ByteArrayReaderCache <ByteArray, string> byteArrayCache)
        {
            if (byteArrayCache.TryGetValue(byteArray, out var str))
            {
                // The string seems to already be in the cache. Check that the content matches.
                if (IsCacheValid(byteArrayCache, byteArray, str))
                {
                    return(str);
                }

                // The cache does not appear to be valid anymore.
                byteArrayCache.Clear();
            }

            return(byteArrayCache.Add(byteArray, LogicalRead.ToString(byteArray)));
        }
示例#3
0
        public static Converter GetConverter(LogicalType logicalType, int scale, ByteArrayReaderCache <TPhysical, TLogical> byteArrayCache)
        {
            if (typeof(TLogical) == typeof(bool) ||
                typeof(TLogical) == typeof(int) ||
                typeof(TLogical) == typeof(long) ||
                typeof(TLogical) == typeof(Int96) ||
                typeof(TLogical) == typeof(float) ||
                typeof(TLogical) == typeof(double))
            {
                return((Converter)(Delegate)(LogicalRead <TPhysical, TPhysical> .Converter)((s, dl, d, nl) => ConvertNative(s, d)));
            }

            if (typeof(TLogical) == typeof(bool?) ||
                typeof(TLogical) == typeof(int?) ||
                typeof(TLogical) == typeof(long?) ||
                typeof(TLogical) == typeof(Int96?) ||
                typeof(TLogical) == typeof(float?) ||
                typeof(TLogical) == typeof(double?))
            {
                return((Converter)(Delegate)(LogicalRead <TPhysical?, TPhysical> .Converter)ConvertNative);
            }

            if (typeof(TLogical) == typeof(sbyte))
            {
                return((Converter)(Delegate)(LogicalRead <sbyte, int> .Converter)((s, dl, d, nl) => ConvertInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(sbyte?))
            {
                return((Converter)(Delegate)(LogicalRead <sbyte?, int> .Converter)ConvertInt8);
            }

            if (typeof(TLogical) == typeof(byte))
            {
                return((Converter)(Delegate)(LogicalRead <byte, int> .Converter)((s, dl, d, nl) => ConvertUInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(byte?))
            {
                return((Converter)(Delegate)(LogicalRead <byte?, int> .Converter)ConvertUInt8);
            }

            if (typeof(TLogical) == typeof(short))
            {
                return((Converter)(Delegate)(LogicalRead <short, int> .Converter)((s, dl, d, nl) => ConvertInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(short?))
            {
                return((Converter)(Delegate)(LogicalRead <short?, int> .Converter)ConvertInt16);
            }

            if (typeof(TLogical) == typeof(ushort))
            {
                return((Converter)(Delegate)(LogicalRead <ushort, int> .Converter)((s, dl, d, nl) => ConvertUInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(ushort?))
            {
                return((Converter)(Delegate)(LogicalRead <ushort?, int> .Converter)ConvertUInt16);
            }

            if (typeof(TLogical) == typeof(uint))
            {
                return((Converter)(Delegate)(LogicalRead <uint, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, uint>(s), d)));
            }

            if (typeof(TLogical) == typeof(uint?))
            {
                return((Converter)(Delegate)(LogicalRead <uint?, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, uint>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(ulong))
            {
                return((Converter)(Delegate)(LogicalRead <ulong, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, ulong>(s), d)));
            }

            if (typeof(TLogical) == typeof(ulong?))
            {
                return((Converter)(Delegate)(LogicalRead <ulong?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, ulong>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(decimal))
            {
                var multiplier = Decimal128.GetScaleMultiplier(scale);
                return((Converter)(Delegate)(LogicalRead <decimal, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertDecimal128(s, d, multiplier)));
            }

            if (typeof(TLogical) == typeof(decimal?))
            {
                var multiplier = Decimal128.GetScaleMultiplier(scale);
                return((Converter)(Delegate)(LogicalRead <decimal?, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertDecimal128(s, dl, d, multiplier, nl)));
            }

            if (typeof(TLogical) == typeof(Guid))
            {
                return((Converter)(Delegate)(LogicalRead <Guid, FixedLenByteArray> .Converter)((s, dl, d, nl) => ConvertUuid(s, d)));
            }

            if (typeof(TLogical) == typeof(Guid?))
            {
                return((Converter)(Delegate)(LogicalRead <Guid?, FixedLenByteArray> .Converter)ConvertUuid);
            }

            if (typeof(TLogical) == typeof(Date))
            {
                return((Converter)(Delegate)(LogicalRead <Date, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, Date>(s), d)));
            }

            if (typeof(TLogical) == typeof(Date?))
            {
                return((Converter)(Delegate)(LogicalRead <Date?, int> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <int, Date>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(DateTime))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <DateTime, long> .Converter)((s, dl, d, nl) => ConvertDateTimeMillis(s, d)));

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <DateTime, long> .Converter)((s, dl, d, nl) => ConvertDateTimeMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos))
            {
                return((Converter)(Delegate)(LogicalRead <DateTimeNanos, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, DateTimeNanos>(s), d)));
            }

            if (typeof(TLogical) == typeof(DateTime?))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <DateTime?, long> .Converter)ConvertDateTimeMillis);

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <DateTime?, long> .Converter)ConvertDateTimeMicros);

                case TimeUnit.Nanos:
                    return((Converter)(Delegate)(LogicalRead <TPhysical?, TPhysical> .Converter)ConvertNative);
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos?))
            {
                return((Converter)(Delegate)(LogicalRead <DateTimeNanos?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, DateTimeNanos>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(TimeSpan))
            {
                switch (((TimeLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan, int> .Converter)((s, dl, d, nl) => ConvertTimeSpanMillis(s, d)));

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan, long> .Converter)((s, dl, d, nl) => ConvertTimeSpanMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos))
            {
                return((Converter)(Delegate)(LogicalRead <TimeSpanNanos, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, TimeSpanNanos>(s), d)));
            }

            if (typeof(TLogical) == typeof(TimeSpan?))
            {
                var timeLogicalType = (TimeLogicalType)logicalType;
                var timeUnit        = timeLogicalType.TimeUnit;

                switch (timeUnit)
                {
                case TimeUnit.Millis:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan?, int> .Converter)ConvertTimeSpanMillis);

                case TimeUnit.Micros:
                    return((Converter)(Delegate)(LogicalRead <TimeSpan?, long> .Converter)ConvertTimeSpanMicros);
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos?))
            {
                return((Converter)(Delegate)(LogicalRead <TimeSpanNanos?, long> .Converter)((s, dl, d, nl) => ConvertNative(MemoryMarshal.Cast <long, TimeSpanNanos>(s), dl, d, nl)));
            }

            if (typeof(TLogical) == typeof(string))
            {
                return(byteArrayCache.IsUsable
                    ? (Converter)(Delegate)(LogicalRead <string, ByteArray> .Converter)((s, dl, d, nl) => ConvertString(s, dl, d, nl, (ByteArrayReaderCache <ByteArray, string>)(object) byteArrayCache))
                    : (Converter)(Delegate)(LogicalRead <string, ByteArray> .Converter)ConvertString);
            }

            if (typeof(TLogical) == typeof(byte[]))
            {
                // Do not reuse byte[] instances, as they are not immutable.
                // Perhaps an optional optimisation if there is demand for it?

                //return byteArrayCache.IsUsable
                //    ? (Converter) (Delegate) (LogicalRead<byte[], ByteArray>.Converter) ((s, dl, d, nl) => ConvertByteArray(s, dl, d, nl, (ByteArrayReaderCache<ByteArray, byte[]>) (object) byteArrayCache))
                //    : (Converter) (Delegate) (LogicalRead<byte[], ByteArray>.Converter) ConvertByteArray;

                return((Converter)(Delegate)(LogicalRead <byte[], ByteArray> .Converter)ConvertByteArray);
            }

            throw new NotSupportedException($"unsupported logical system type {typeof(TLogical)} with logical type {logicalType}");
        }
示例#4
0
 private static void ConvertString(ReadOnlySpan <ByteArray> source, ReadOnlySpan <short> defLevels, Span <string> destination, short nullLevel, ByteArrayReaderCache <ByteArray, string> byteArrayCache)
 {
     for (int i = 0, src = 0; i != destination.Length; ++i)
     {
         destination[i] = !defLevels.IsEmpty && defLevels[i] == nullLevel ? null : ToString(source[src++], byteArrayCache);
     }
 }
示例#5
0
        public static Delegate GetConverter(ColumnDescriptor columnDescriptor, ColumnChunkMetaData columnChunkMetaData)
        {
            if (typeof(TLogical) == typeof(bool) ||
                typeof(TLogical) == typeof(int) ||
                typeof(TLogical) == typeof(long) ||
                typeof(TLogical) == typeof(Int96) ||
                typeof(TLogical) == typeof(float) ||
                typeof(TLogical) == typeof(double))
            {
                return(LogicalRead.GetNativeConverter <TPhysical, TPhysical>());
            }

            if (typeof(TLogical) == typeof(bool?) ||
                typeof(TLogical) == typeof(int?) ||
                typeof(TLogical) == typeof(long?) ||
                typeof(TLogical) == typeof(Int96?) ||
                typeof(TLogical) == typeof(float?) ||
                typeof(TLogical) == typeof(double?))
            {
                return(LogicalRead.GetNullableNativeConverter <TPhysical, TPhysical>());
            }

            if (typeof(TLogical) == typeof(sbyte))
            {
                return((LogicalRead <sbyte, int> .Converter)((s, _, d, _) => LogicalRead.ConvertInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(sbyte?))
            {
                return((LogicalRead <sbyte?, int> .Converter)LogicalRead.ConvertInt8);
            }

            if (typeof(TLogical) == typeof(byte))
            {
                return((LogicalRead <byte, int> .Converter)((s, _, d, _) => LogicalRead.ConvertUInt8(s, d)));
            }

            if (typeof(TLogical) == typeof(byte?))
            {
                return((LogicalRead <byte?, int> .Converter)LogicalRead.ConvertUInt8);
            }

            if (typeof(TLogical) == typeof(short))
            {
                return((LogicalRead <short, int> .Converter)((s, _, d, _) => LogicalRead.ConvertInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(short?))
            {
                return((LogicalRead <short?, int> .Converter)LogicalRead.ConvertInt16);
            }

            if (typeof(TLogical) == typeof(ushort))
            {
                return((LogicalRead <ushort, int> .Converter)((s, _, d, _) => LogicalRead.ConvertUInt16(s, d)));
            }

            if (typeof(TLogical) == typeof(ushort?))
            {
                return((LogicalRead <ushort?, int> .Converter)LogicalRead.ConvertUInt16);
            }

            if (typeof(TLogical) == typeof(uint))
            {
                return(LogicalRead.GetNativeConverter <uint, int>());
            }

            if (typeof(TLogical) == typeof(uint?))
            {
                return(LogicalRead.GetNullableNativeConverter <uint, int>());
            }

            if (typeof(TLogical) == typeof(ulong))
            {
                return(LogicalRead.GetNativeConverter <ulong, long>());
            }

            if (typeof(TLogical) == typeof(ulong?))
            {
                return(LogicalRead.GetNullableNativeConverter <ulong, long>());
            }

            if (typeof(TLogical) == typeof(decimal))
            {
                var multiplier = Decimal128.GetScaleMultiplier(columnDescriptor.TypeScale);
                return((LogicalRead <decimal, FixedLenByteArray> .Converter)((s, _, d, _) => LogicalRead.ConvertDecimal128(s, d, multiplier)));
            }

            if (typeof(TLogical) == typeof(decimal?))
            {
                var multiplier = Decimal128.GetScaleMultiplier(columnDescriptor.TypeScale);
                return((LogicalRead <decimal?, FixedLenByteArray> .Converter)((s, dl, d, del) => LogicalRead.ConvertDecimal128(s, dl, d, multiplier, del)));
            }

            if (typeof(TLogical) == typeof(Guid))
            {
                return((LogicalRead <Guid, FixedLenByteArray> .Converter)((s, _, d, _) => LogicalRead.ConvertUuid(s, d)));
            }

            if (typeof(TLogical) == typeof(Guid?))
            {
                return((LogicalRead <Guid?, FixedLenByteArray> .Converter)LogicalRead.ConvertUuid);
            }

            if (typeof(TLogical) == typeof(Date))
            {
                return(LogicalRead.GetNativeConverter <Date, int>());
            }

            if (typeof(TLogical) == typeof(Date?))
            {
                return(LogicalRead.GetNullableNativeConverter <Date, int>());
            }

            var logicalType = columnDescriptor.LogicalType;

            if (typeof(TLogical) == typeof(DateTime))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((LogicalRead <DateTime, long> .Converter)((s, _, d, _) => LogicalRead.ConvertDateTimeMillis(s, d)));

                case TimeUnit.Micros:
                    return((LogicalRead <DateTime, long> .Converter)((s, _, d, _) => LogicalRead.ConvertDateTimeMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos))
            {
                return(LogicalRead.GetNativeConverter <DateTimeNanos, long>());
            }

            if (typeof(TLogical) == typeof(DateTime?))
            {
                switch (((TimestampLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((LogicalRead <DateTime?, long> .Converter)LogicalRead.ConvertDateTimeMillis);

                case TimeUnit.Micros:
                    return((LogicalRead <DateTime?, long> .Converter)LogicalRead.ConvertDateTimeMicros);

                case TimeUnit.Nanos:
                    return((LogicalRead <TPhysical?, TPhysical> .Converter)LogicalRead.ConvertNative);
                }
            }

            if (typeof(TLogical) == typeof(DateTimeNanos?))
            {
                return(LogicalRead.GetNullableNativeConverter <DateTimeNanos, long>());
            }

            if (typeof(TLogical) == typeof(TimeSpan))
            {
                switch (((TimeLogicalType)logicalType).TimeUnit)
                {
                case TimeUnit.Millis:
                    return((LogicalRead <TimeSpan, int> .Converter)((s, _, d, _) => LogicalRead.ConvertTimeSpanMillis(s, d)));

                case TimeUnit.Micros:
                    return((LogicalRead <TimeSpan, long> .Converter)((s, _, d, _) => LogicalRead.ConvertTimeSpanMicros(s, d)));
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos))
            {
                return(LogicalRead.GetNativeConverter <TimeSpanNanos, long>());
            }

            if (typeof(TLogical) == typeof(TimeSpan?))
            {
                var timeLogicalType = (TimeLogicalType)logicalType;
                var timeUnit        = timeLogicalType.TimeUnit;

                switch (timeUnit)
                {
                case TimeUnit.Millis:
                    return((LogicalRead <TimeSpan?, int> .Converter)LogicalRead.ConvertTimeSpanMillis);

                case TimeUnit.Micros:
                    return((LogicalRead <TimeSpan?, long> .Converter)LogicalRead.ConvertTimeSpanMicros);
                }
            }

            if (typeof(TLogical) == typeof(TimeSpanNanos?))
            {
                return(LogicalRead.GetNullableNativeConverter <TimeSpanNanos, long>());
            }

            if (typeof(TLogical) == typeof(string))
            {
                var byteArrayCache = new ByteArrayReaderCache <TPhysical, TLogical>(columnChunkMetaData);

                return(byteArrayCache.IsUsable
                    ? (LogicalRead <string?, ByteArray> .Converter)((s, dl, d, del) => LogicalRead.ConvertString(s, dl, d, del, (ByteArrayReaderCache <ByteArray, string>)(object) byteArrayCache))
                    : LogicalRead.ConvertString);
            }

            if (typeof(TLogical) == typeof(byte[]))
            {
                // Do not reuse byte[] instances, as they are not immutable.
                // Perhaps an optional optimisation if there is demand for it?

                //return byteArrayCache.IsUsable
                //    ? (LogicalRead<byte[], ByteArray>.Converter) ((s, dl, d, nl) => ConvertByteArray(s, dl, d, nl, (ByteArrayReaderCache<ByteArray, byte[]>) (object) byteArrayCache))
                //    : (LogicalRead<byte[], ByteArray>.Converter) ConvertByteArray;

                return((LogicalRead <byte[]?, ByteArray> .Converter)LogicalRead.ConvertByteArray);
            }

            throw new NotSupportedException($"unsupported logical system type {typeof(TLogical)} with logical type {logicalType}");
        }
示例#6
0
 private static string ToString(ByteArray byteArray, ByteArrayReaderCache <ByteArray, string> byteArrayCache)
 {
     return(byteArrayCache.TryGetValue(byteArray, out var value)
         ? value
         : byteArrayCache.Add(byteArray, ToString(byteArray)));
 }