A Flight server that numbers incoming/outgoing data.
| 170 | |
| 171 | |
| 172 | class MetadataFlightServer(FlightServerBase): |
| 173 | """A Flight server that numbers incoming/outgoing data.""" |
| 174 | |
| 175 | def __init__(self, options=None, **kwargs): |
| 176 | super().__init__(**kwargs) |
| 177 | self.options = options |
| 178 | |
| 179 | def do_get(self, context, ticket): |
| 180 | data = [ |
| 181 | pa.array([-10, -5, 0, 5, 10]) |
| 182 | ] |
| 183 | table = pa.Table.from_arrays(data, names=['a']) |
| 184 | return flight.GeneratorStream( |
| 185 | table.schema, |
| 186 | self.number_batches(table), |
| 187 | options=self.options) |
| 188 | |
| 189 | def do_put(self, context, descriptor, reader, writer): |
| 190 | counter = 0 |
| 191 | expected_data = [-10, -5, 0, 5, 10] |
| 192 | assert reader.stats.num_messages == 1 |
| 193 | for batch, buf in reader: |
| 194 | assert batch.equals(pa.RecordBatch.from_arrays( |
| 195 | [pa.array([expected_data[counter]])], |
| 196 | ['a'] |
| 197 | )) |
| 198 | assert buf is not None |
| 199 | client_counter, = struct.unpack('<i', buf.to_pybytes()) |
| 200 | assert counter == client_counter |
| 201 | writer.write(struct.pack('<i', counter)) |
| 202 | counter += 1 |
| 203 | assert reader.stats.num_messages == 6 |
| 204 | assert reader.stats.num_record_batches == 5 |
| 205 | |
| 206 | @staticmethod |
| 207 | def number_batches(table): |
| 208 | for idx, batch in enumerate(table.to_batches()): |
| 209 | buf = struct.pack('<i', idx) |
| 210 | yield batch, buf |
| 211 | |
| 212 | |
| 213 | class EchoFlightServer(FlightServerBase): |
no outgoing calls