Skip to content

Commit

Permalink
Always return the count of actually copied items.
Browse files Browse the repository at this point in the history
Further, reduce duplicated code by introduce `Decode<T>(...) where T :
struct`.
  • Loading branch information
artnim committed Aug 23, 2024
1 parent ff57d16 commit 58cb0f7
Showing 1 changed file with 4 additions and 32 deletions.
36 changes: 4 additions & 32 deletions src/Parquet/Encodings/ParquetPlainEncoder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Buffers;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Parquet.Data;
using Parquet.Extensions;
Expand Down Expand Up @@ -593,54 +595,30 @@ public static void Encode(ReadOnlySpan<int> data, Stream destination) {
Write(destination, bytes);
}

public static int Decode(Span<byte> source, Span<int> data) {
Span<byte> dataBytes = MemoryMarshal.AsBytes(data);
source.CopyWithLimitTo(dataBytes);
return data.Length;
}

public static void Encode(ReadOnlySpan<uint> data, Stream destination) {
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(data);
Write(destination, bytes);
}

public static int Decode(Span<byte> source, Span<uint> data) {
Span<byte> bytes = MemoryMarshal.AsBytes(data);
source.CopyWithLimitTo(bytes);
return data.Length;
}

public static void Encode(ReadOnlySpan<long> data, Stream destination) {
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(data);
Write(destination, bytes);
}

public static int Decode(Span<byte> source, Span<long> data) {
Span<byte> bytes = MemoryMarshal.AsBytes(data);
source.CopyWithLimitTo(bytes);
return data.Length;
}

public static void Encode(ReadOnlySpan<ulong> data, Stream destination) {
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(data);
Write(destination, bytes);
}

public static int Decode(Span<byte> source, Span<ulong> data) {
Span<byte> bytes = MemoryMarshal.AsBytes(data);
source.CopyWithLimitTo(bytes);
return data.Length;
}

public static void Encode(ReadOnlySpan<BigInteger> data, Stream destination) {
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(data);
Write(destination, bytes);
}

public static int Decode(Span<byte> source, Span<BigInteger> data) {
public static int Decode<T>(Span<byte> source, Span<T> data) where T : struct {
Span<byte> bytes = MemoryMarshal.AsBytes(data);
source.CopyWithLimitTo(bytes);
return data.Length;
return Math.Min(data.Length, source.Length / Unsafe.SizeOf<T>());
}

public static void Encode(ReadOnlySpan<decimal> data, Stream destination, SchemaElement tse) {
Expand Down Expand Up @@ -752,12 +730,6 @@ public static void Encode(ReadOnlySpan<double> data, Stream destination) {
Write(destination, bytes);
}

public static int Decode(Span<byte> source, Span<double> data) {
Span<byte> bytes = MemoryMarshal.AsBytes(data);
source.CopyWithLimitTo(bytes);
return data.Length;
}

public static void Encode(ReadOnlySpan<float> data, Stream destination) {
ReadOnlySpan<byte> bytes = MemoryMarshal.AsBytes(data);
Write(destination, bytes);
Expand Down

0 comments on commit 58cb0f7

Please sign in to comment.