-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description GitOrigin-RevId: ad6aefac0505dfd2090ca1773ccd8ea2df6fb8d1
- Loading branch information
1 parent
b8b88f6
commit 8fdf09c
Showing
14 changed files
with
314 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
quine/src/main/scala/com/thatdot/quine/app/serialization/AvroSchemaCache.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.thatdot.quine.app.serialization | ||
|
||
import java.net.URL | ||
|
||
import scala.concurrent.{ExecutionContext, Future, blocking} | ||
import scala.util.Using | ||
|
||
import com.github.blemale.scaffeine.{AsyncLoadingCache, Scaffeine} | ||
import org.apache.avro.Schema | ||
|
||
import com.thatdot.quine.app.serialization.AvroSchemaError.{InvalidAvroSchema, UnreachableAvroSchema} | ||
import com.thatdot.quine.util.ComputeAndBlockingExecutionContext | ||
|
||
/** Provides common utilities for its inheritors to parse avro objects. | ||
*/ | ||
trait AvroSchemaCache { | ||
def getSchema(schemaUrl: URL): Future[Schema] | ||
|
||
} | ||
object AvroSchemaCache { | ||
class AsyncLoading(val ecs: ComputeAndBlockingExecutionContext) extends AvroSchemaCache { | ||
private val avroSchemaCache: AsyncLoadingCache[URL, Schema] = | ||
Scaffeine() | ||
.maximumSize(5) | ||
.buildAsyncFuture { schemaUrl => | ||
// NB if this Future fails (with an error), the cache will not store the schema. | ||
// This allows the user to retry the schema resolution after updating their environment | ||
resolveSchema(schemaUrl)(ecs.blockingDispatcherEC) | ||
} | ||
|
||
/** Invalidate the schema for the given URI. This will cause the next call to [[avroSchemaCache.get]] | ||
* to re-parse the schema. This may be desirable when, for example, a message type lookup fails, even if the | ||
* schema lookup succeeds (so that the user can update their schema file to include the missing type). | ||
*/ | ||
def flush(uri: URL): Unit = | ||
avroSchemaCache.put(uri, Future.successful(null)) | ||
|
||
def getSchema(schemaUrl: URL): Future[Schema] = | ||
avroSchemaCache.get(schemaUrl) | ||
|
||
val parser = new org.apache.avro.Schema.Parser() | ||
|
||
private[this] def resolveSchema(uri: URL)(blockingEc: ExecutionContext): Future[Schema] = | ||
Future(blocking { | ||
Using.resource(uri.openStream())(parser.parse) | ||
})(blockingEc).recoverWith { | ||
case e: org.apache.avro.SchemaParseException => Future.failed(new InvalidAvroSchema(uri, e)) | ||
case e: java.io.IOException => Future.failed(new UnreachableAvroSchema(uri, e)) | ||
}(blockingEc) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.