forked from G-Research/ParquetSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagedRandomAccessFile.cs
More file actions
222 lines (200 loc) · 7.37 KB
/
ManagedRandomAccessFile.cs
File metadata and controls
222 lines (200 loc) · 7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace ParquetSharp.IO
{
/// <summary>
/// Managed wrapper around arrow::io::RandomAccessFile that takes in a .NET Stream instance.
/// </summary>
public sealed class ManagedRandomAccessFile : RandomAccessFile
{
public ManagedRandomAccessFile(Stream stream)
: this(stream, false)
{
}
public ManagedRandomAccessFile(Stream stream, bool leaveOpen)
{
_stream = stream;
_leaveOpen = leaveOpen;
_read = Read;
_close = Close;
_getSize = GetSize;
_tell = Tell;
_seek = Seek;
_closed = Closed;
Handle = Create(_read, _close, _getSize, _tell, _seek, _closed, this);
}
private static ParquetHandle Create(
ReadDelegate read,
CloseDelegate close,
GetSizeDelegate getSize,
TellDelegate tell,
SeekDelegate seek,
ClosedDelegate closed,
ManagedRandomAccessFile managedFile)
{
ExceptionInfo.Check(ManagedRandomAccessFile_Create(read, close, getSize, tell, seek, closed, out var handle));
void Free(IntPtr ptr)
{
RandomAccessFile_Free(ptr);
// Capture and keep a handle to the managed file instance so that if we free the last reference to the
// C++ random access file and trigger a file close, we can ensure the file hasn't been garbage collected.
// Note that this doesn't protect against the case where the C# side handle is disposed or finalized before
// the C++ side has finished with it.
GC.KeepAlive(managedFile);
}
return new ParquetHandle(handle, Free);
}
private byte Read(long nbytes, IntPtr bytesRead, IntPtr dest, out string? exception)
{
try
{
#if !NETSTANDARD2_1_OR_GREATER
var buffer = new byte[(int) Math.Min(nbytes, MaxArraySize)];
#endif
var totalRead = 0L;
while (totalRead < nbytes)
{
var bytesToRead = (int) Math.Min(nbytes - totalRead, MaxArraySize);
int read;
#if NETSTANDARD2_1_OR_GREATER
unsafe
{
read = _stream.Read(new Span<byte>(dest.ToPointer(), bytesToRead));
}
#else
read = _stream.Read(buffer, 0, bytesToRead);
Marshal.Copy(buffer, 0, dest, read);
#endif
if (read == 0)
{
break;
}
totalRead += read;
dest = IntPtr.Add(dest, read);
}
Marshal.WriteInt64(bytesRead, totalRead);
exception = null;
return 0;
}
catch (Exception error)
{
return HandleException(error, out exception);
}
}
private byte Close(out string? exception)
{
try
{
if (!_leaveOpen)
{
_stream.Close();
}
exception = null;
return 0;
}
catch (Exception error)
{
return HandleException(error, out exception);
}
}
private byte GetSize(IntPtr size, out string? exception)
{
try
{
Marshal.WriteInt64(size, _stream.Length);
exception = null;
return 0;
}
catch (Exception error)
{
return HandleException(error, out exception);
}
}
private byte Tell(IntPtr position, out string? exception)
{
try
{
Marshal.WriteInt64(position, _stream.Position);
exception = null;
return 0;
}
catch (Exception error)
{
return HandleException(error, out exception);
}
}
private byte Seek(long position, out string? exception)
{
try
{
_stream.Position = position;
exception = null;
return 0;
}
catch (Exception error)
{
return HandleException(error, out exception);
}
}
private bool Closed()
{
try
{
return !_stream.CanRead;
}
catch
{
return true;
}
}
private byte HandleException(Exception error, out string? exception)
{
if (error is OutOfMemoryException)
{
exception = _exceptionMessage = null;
return (byte) ArrowStatusCode.OutOfMemory;
}
if (error is IOException)
{
exception = _exceptionMessage = error.ToString();
return (byte) ArrowStatusCode.IOError;
}
exception = _exceptionMessage = error.ToString();
return (byte) ArrowStatusCode.UnknownError;
}
[DllImport(ParquetDll.Name)]
private static extern IntPtr ManagedRandomAccessFile_Create(
ReadDelegate read,
CloseDelegate close,
GetSizeDelegate getSize,
TellDelegate tell,
SeekDelegate seek,
ClosedDelegate closed,
out IntPtr randomAccessFile);
private delegate byte ReadDelegate(long nbyte, IntPtr bytesRead, IntPtr dest, [MarshalAs(UnmanagedType.LPStr)] out string? exception);
private delegate byte CloseDelegate([MarshalAs(UnmanagedType.LPStr)] out string? exception);
private delegate byte GetSizeDelegate(IntPtr size, [MarshalAs(UnmanagedType.LPStr)] out string? exception);
private delegate byte TellDelegate(IntPtr position, [MarshalAs(UnmanagedType.LPStr)] out string? exception);
private delegate byte SeekDelegate(long position, [MarshalAs(UnmanagedType.LPStr)] out string? exception);
private delegate bool ClosedDelegate();
private readonly Stream _stream;
private readonly bool _leaveOpen;
// The lifetime of the delegates must match the lifetime of this class.
// ReSharper disable PrivateFieldCanBeConvertedToLocalVariable
private readonly ReadDelegate _read;
private readonly CloseDelegate _close;
private readonly GetSizeDelegate _getSize;
private readonly TellDelegate _tell;
private readonly SeekDelegate _seek;
private readonly ClosedDelegate _closed;
// ReSharper restore PrivateFieldCanBeConvertedToLocalVariable
// The lifetime of the exception message must match the lifetime of this class.
// ReSharper disable NotAccessedField.Local
private string? _exceptionMessage;
// ReSharper restore NotAccessedField.Local
// Maximum size of a byte array,
// see https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/gcallowverylargeobjects-element#remarks
private const long MaxArraySize = 2_147_483_591;
}
}