public static Schema DecodeSchema(ReadOnlyMemory <byte> buffer) { int bufferPosition = 0; int schemaMessageLength = BinaryPrimitives.ReadInt32LittleEndian(buffer.Span.Slice(bufferPosition)); bufferPosition += sizeof(int); if (schemaMessageLength == MessageSerializer.IpcContinuationToken) { // ARROW-6313, if the first 4 bytes are continuation message, read the next 4 for the length if (buffer.Length <= bufferPosition + sizeof(int)) { throw new InvalidDataException("Corrupted IPC message. Received a continuation token at the end of the message."); } schemaMessageLength = BinaryPrimitives.ReadInt32LittleEndian(buffer.Span.Slice(bufferPosition)); bufferPosition += sizeof(int); } ByteBuffer schemaBuffer = ArrowReaderImplementation.CreateByteBuffer(buffer.Slice(bufferPosition)); //DictionaryBatch not supported for now DictionaryMemo dictionaryMemo = null; var schema = MessageSerializer.GetSchema(ArrowReaderImplementation.ReadMessage <Flatbuf.Schema>(schemaBuffer), ref dictionaryMemo); return(schema); }
public async ValueTask <Schema> ReadSchema() { if (HasReadSchema) { return(Schema); } var moveNextResult = await _flightDataStream.MoveNext().ConfigureAwait(false); if (!moveNextResult) { throw new Exception("No records or schema in this flight"); } //AppMetadata will never be null, but length 0 if empty //Those are skipped if (_flightDataStream.Current.AppMetadata.Length > 0) { _applicationMetadatas.Add(_flightDataStream.Current.AppMetadata); } var header = _flightDataStream.Current.DataHeader.Memory; Message message = Message.GetRootAsMessage( ArrowReaderImplementation.CreateByteBuffer(header)); if (_flightDataStream.Current.FlightDescriptor != null) { _flightDescriptor = new FlightDescriptor(_flightDataStream.Current.FlightDescriptor); } switch (message.HeaderType) { case MessageHeader.Schema: Schema = FlightMessageSerializer.DecodeSchema(message.ByteBuffer); break; default: throw new Exception($"Expected schema as the first message, but got: {message.HeaderType.ToString()}"); } return(Schema); }