|
1 | 1 | package it.agilelab.darwin.common |
2 | 2 |
|
3 | | -import org.apache.avro.Schema |
| 3 | +import java.io.{ InputStream, OutputStream } |
| 4 | +import java.nio.{ ByteBuffer, ByteOrder } |
| 5 | + |
| 6 | +import it.agilelab.darwin.common.compat.RightBiasedEither |
| 7 | +import it.agilelab.darwin.manager.SchemaPayloadPair |
| 8 | +import it.agilelab.darwin.manager.exception.DarwinException |
| 9 | +import it.agilelab.darwin.manager.util.AvroSingleObjectEncodingUtils |
| 10 | +import org.apache.avro.{ Schema, SchemaNormalization } |
4 | 11 |
|
5 | 12 | /** |
6 | 13 | * Generic abstraction of a component capable of reading and writing Schema entities in an external storage. |
@@ -49,4 +56,174 @@ trait Connector extends Serializable { |
49 | 56 | * @return an option that is empty if no schema was found for the ID or defined if a schema was found |
50 | 57 | */ |
51 | 58 | def findSchema(id: Long): Option[Schema] |
| 59 | + |
| 60 | + /** |
| 61 | + * Generate a fingerprint for a schema, the default implementation is SchemaNormalization.parsingFingerprint64 |
| 62 | + * |
| 63 | + * @param schema the schema to fingerprint |
| 64 | + * @return the schema id |
| 65 | + */ |
| 66 | + def fingerprint(schema: Schema): Long = { |
| 67 | + SchemaNormalization.parsingFingerprint64(schema) |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Writes to the given OutputStream the Single Object Encoding header and returns the OutputStream |
| 72 | + * |
| 73 | + * @return the input OutputStream |
| 74 | + */ |
| 75 | + def writeHeaderToStream(byteStream: OutputStream, schemaId: Long, endianness: ByteOrder): OutputStream = { |
| 76 | + AvroSingleObjectEncodingUtils.writeHeaderToStream(byteStream, schemaId, endianness) |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Create an array that creates a Single-Object encoded byte array. |
| 81 | + * By specifications the encoded array is obtained concatenating the V1_HEADER, the schema id and the avro-encoded |
| 82 | + * payload. |
| 83 | + * |
| 84 | + * @param avroPayload avro-serialized payload |
| 85 | + * @param schema the schema used to encode the payload |
| 86 | + * @return a Single-Object encoded byte array |
| 87 | + */ |
| 88 | + def generateAvroSingleObjectEncoded( |
| 89 | + avroPayload: Array[Byte], |
| 90 | + schema: Schema, |
| 91 | + endianness: ByteOrder, |
| 92 | + getId: Schema => Long |
| 93 | + ): Array[Byte] = { |
| 94 | + AvroSingleObjectEncodingUtils.generateAvroSingleObjectEncoded(avroPayload, getId(schema), endianness) |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Writes to the given OutputStream the Single Object Encoding header then the avroValue and returns the OutputStream |
| 99 | + * |
| 100 | + * @param byteStream the stream to write to |
| 101 | + * @param avroValue the value to be written to the stream |
| 102 | + * @param schemaId id of the schema used to encode the payload |
| 103 | + * @return the input OutputStream |
| 104 | + */ |
| 105 | + def generateAvroSingleObjectEncoded( |
| 106 | + byteStream: OutputStream, |
| 107 | + avroValue: Array[Byte], |
| 108 | + schemaId: Long, |
| 109 | + endianness: ByteOrder |
| 110 | + ): OutputStream = { |
| 111 | + AvroSingleObjectEncodingUtils.generateAvroSingleObjectEncoded(byteStream, avroValue, schemaId, endianness) |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Writes to the given OutputStream the Single Object Encoding header then calls the avroWriter function to |
| 116 | + * possibly add data to the stream and finally returns the OutputStream |
| 117 | + * |
| 118 | + * @param byteStream the stream to write to |
| 119 | + * @param schemaId id of the schema used to encode the payload |
| 120 | + * @param avroWriter function that will be called to add user generated avro to the stream |
| 121 | + * @return the input OutputStream |
| 122 | + */ |
| 123 | + def generateAvroSingleObjectEncoded(byteStream: OutputStream, schemaId: Long, endianness: ByteOrder)( |
| 124 | + avroWriter: OutputStream => OutputStream |
| 125 | + ): OutputStream = { |
| 126 | + AvroSingleObjectEncodingUtils.generateAvroSingleObjectEncoded(byteStream, schemaId, endianness)(avroWriter) |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Extracts a Tuple2 that contains the Schema and the Avro-encoded payload |
| 131 | + * |
| 132 | + * @param avroSingleObjectEncoded a byte array of a Single-Object encoded payload |
| 133 | + * @return a pair containing the Schema and the payload of the input array |
| 134 | + */ |
| 135 | + def retrieveSchemaAndAvroPayload( |
| 136 | + avroSingleObjectEncoded: Array[Byte], |
| 137 | + endianness: ByteOrder, |
| 138 | + getSchema: Long => Option[Schema] |
| 139 | + ): (Schema, Array[Byte]) = { |
| 140 | + if (AvroSingleObjectEncodingUtils.isAvroSingleObjectEncoded(avroSingleObjectEncoded)) { |
| 141 | + val id = AvroSingleObjectEncodingUtils.extractId(avroSingleObjectEncoded, endianness) |
| 142 | + getSchema(id) match { |
| 143 | + case Some(schema) => |
| 144 | + schema -> AvroSingleObjectEncodingUtils.dropHeader(avroSingleObjectEncoded) |
| 145 | + case _ => |
| 146 | + throw new DarwinException(s"No schema found for ID $id") |
| 147 | + } |
| 148 | + } else { |
| 149 | + throw AvroSingleObjectEncodingUtils.parseException() |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Extracts the Schema from the ByteBuffer after the method call the ByteBuffer position will be right after the |
| 155 | + * header. |
| 156 | + * |
| 157 | + * @param avroSingleObjectEncoded a ByteBuffer of a Single-Object encoded payload |
| 158 | + * @return the avro Schema |
| 159 | + */ |
| 160 | + def retrieveSchemaAndAvroPayload( |
| 161 | + avroSingleObjectEncoded: ByteBuffer, |
| 162 | + endianness: ByteOrder, |
| 163 | + getSchema: Long => Option[Schema] |
| 164 | + ): Schema = { |
| 165 | + if (AvroSingleObjectEncodingUtils.isAvroSingleObjectEncoded(avroSingleObjectEncoded)) { |
| 166 | + val id = AvroSingleObjectEncodingUtils.extractId(avroSingleObjectEncoded, endianness) |
| 167 | + getSchema(id) match { |
| 168 | + case Some(schema) => schema |
| 169 | + case _ => throw new DarwinException(s"No schema found for ID $id") |
| 170 | + } |
| 171 | + } else { |
| 172 | + throw AvroSingleObjectEncodingUtils.parseException() |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Extracts the schema from the avro single-object encoded at the head of this input stream. |
| 178 | + * The input stream will have 10 bytes consumed if the first two bytes correspond to the single object encoded |
| 179 | + * header, or zero bytes consumed if the InputStream supports marking; if it doesn't, the first bytes (up to 2) will |
| 180 | + * be consumed and returned in the Left part of the Either |
| 181 | + * |
| 182 | + * @param inputStream avro single-object encoded input stream |
| 183 | + * @return the schema ID extracted from the input data |
| 184 | + */ |
| 185 | + def extractSchema( |
| 186 | + inputStream: InputStream, |
| 187 | + endianness: ByteOrder, |
| 188 | + getSchema: Long => Option[Schema] |
| 189 | + ): Either[Array[Byte], Schema] = { |
| 190 | + AvroSingleObjectEncodingUtils.extractId(inputStream, endianness).rightMap { id => |
| 191 | + getSchema(id).getOrElse(throw new DarwinException(s"No schema found for ID $id")) |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * Extracts the schema from the avro single-object encoded in the input array. |
| 197 | + * |
| 198 | + * @param array avro single-object encoded array |
| 199 | + * @return the schema ID extracted from the input data |
| 200 | + */ |
| 201 | + def extractSchema( |
| 202 | + array: Array[Byte], |
| 203 | + endianness: ByteOrder, |
| 204 | + getSchema: Long => Option[Schema] |
| 205 | + ): Either[Exception, Schema] = { |
| 206 | + try { |
| 207 | + val id = AvroSingleObjectEncodingUtils.extractId(array, endianness) |
| 208 | + getSchema(id) |
| 209 | + .toRight(new RuntimeException(s"Cannot find schema with id $id")) |
| 210 | + } catch { |
| 211 | + case ie: IllegalArgumentException => Left(ie) |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Extracts a SchemaPayloadPair that contains the Schema and the Avro-encoded payload |
| 217 | + * |
| 218 | + * @param avroSingleObjectEncoded a byte array of a Single-Object encoded payload |
| 219 | + * @return a SchemaPayloadPair containing the Schema and the payload of the input array |
| 220 | + */ |
| 221 | + def retrieveSchemaAndPayload( |
| 222 | + avroSingleObjectEncoded: Array[Byte], |
| 223 | + endianness: ByteOrder, |
| 224 | + getSchema: Long => Option[Schema] |
| 225 | + ): SchemaPayloadPair = { |
| 226 | + val (schema, payload) = retrieveSchemaAndAvroPayload(avroSingleObjectEncoded, endianness, getSchema) |
| 227 | + SchemaPayloadPair.create(schema, payload) |
| 228 | + } |
52 | 229 | } |
0 commit comments