Skip to content

Commit

Permalink
move caching from TypedSchema class to higher level
Browse files Browse the repository at this point in the history
  • Loading branch information
libretto committed Mar 21, 2023
1 parent edb09eb commit b45c16c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
4 changes: 0 additions & 4 deletions karapace/protobuf/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import json


class ProtobufParserRuntimeException(Exception):
pass


class IllegalStateException(Exception):
pass

Expand Down
2 changes: 1 addition & 1 deletion karapace/protobuf/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def collect_dependencies(self, verifier: ProtobufDependencyVerifier):
self.dependencies[key].schema.schema.collect_dependencies(verifier)

# verifier.add_import?? we have no access to own Kafka structure from this class...
# but we need data to analyse imports to avoid ciclyc dependencies...
# but we need data to analyse imports to avoid cyclic dependencies...

package_name = self.proto_file_element.package_name
if package_name is None:
Expand Down
60 changes: 34 additions & 26 deletions karapace/schema_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
IllegalArgumentException,
IllegalStateException,
ProtobufException,
ProtobufParserRuntimeException,
ProtobufUnresolvedDependencyException,
SchemaParseException as ProtobufSchemaParseException,
)
Expand Down Expand Up @@ -114,7 +113,6 @@ def parse(
TypeError,
SchemaError,
AssertionError,
ProtobufParserRuntimeException,
IllegalStateException,
IllegalArgumentException,
ProtobufError,
Expand Down Expand Up @@ -155,11 +153,10 @@ def __init__(
self.schema_type = schema_type
self.references = references
self.dependencies = dependencies
self.schema_str = TypedSchema.normalize_schema_str(schema_str, schema_type, references, dependencies)
self.schema_str = TypedSchema.normalize_schema_str(schema_str, schema_type, schema)
self.max_id: Optional[SchemaId] = None
self._fingerprint_cached: Optional[str] = None
self._str_cached: Optional[str] = None
self._schema_cached: Optional[Union[Draft7Validator, AvroSchema, ProtobufSchema]] = schema

def to_dict(self) -> Dict[str, Any]:
if self.schema_type is SchemaType.PROTOBUF:
Expand All @@ -175,8 +172,9 @@ def fingerprint(self) -> str:
def normalize_schema_str(
schema_str: str,
schema_type: SchemaType,
references: Optional[List[Reference]] = None,
dependencies: Optional[Dict[str, Dependency]] = None,
schema: Optional[Union[Draft7Validator, AvroSchema, ProtobufSchema]] = None,
# references: Optional[List[Reference]] = None,
# dependencies: Optional[Dict[str, Dependency]] = None,
) -> str:
if schema_type is SchemaType.AVRO or schema_type is SchemaType.JSONSCHEMA:
try:
Expand All @@ -185,11 +183,15 @@ def normalize_schema_str(
LOG.error("Schema is not valid JSON")
raise e
elif schema_type == SchemaType.PROTOBUF:
try:
schema_str = str(parse_protobuf_schema_definition(schema_str, references, dependencies, False))
except InvalidSchema as e:
LOG.exception("Schema is not valid ProtoBuf definition")
raise e
if schema:
schema_str = str(schema)
else:
try:
schema_str = str(parse_protobuf_schema_definition(schema_str, None, None, False))
except InvalidSchema as e:
LOG.exception("Schema is not valid ProtoBuf definition")
raise e

else:
_assert_never(schema_type)
return schema_str
Expand All @@ -205,10 +207,16 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return f"TypedSchema(type={self.schema_type}, schema={str(self)})"

def __eq__(self, other: Any) -> bool:
return (
isinstance(other, (TypedSchema))
and self.schema_type is other.schema_type
and str(self) == str(other)
and self.references == other.references
)

@property
def schema(self) -> Union[Draft7Validator, AvroSchema, ProtobufSchema]:
if self._schema_cached is not None:
return self._schema_cached
parsed_typed_schema = parse(
schema_type=self.schema_type,
schema_str=self.schema_str,
Expand All @@ -217,19 +225,7 @@ def schema(self) -> Union[Draft7Validator, AvroSchema, ProtobufSchema]:
references=self.references,
dependencies=self.dependencies,
)
self._schema_cached = parsed_typed_schema.schema
return self._schema_cached

def get_references(self) -> Optional[List[Reference]]:
return self.references

def __eq__(self, other: Any) -> bool:
return (
isinstance(other, (TypedSchema))
and self.schema_type is other.schema_type
and str(self) == str(other)
and self.references == other.references
)
return parsed_typed_schema.schema


class ParsedTypedSchema(TypedSchema):
Expand Down Expand Up @@ -259,6 +255,8 @@ def __init__(
references: Optional[List[Reference]] = None,
dependencies: Optional[Dict[str, Dependency]] = None,
):
self._schema_cached: Optional[Union[Draft7Validator, AvroSchema, ProtobufSchema]] = schema

super().__init__(
schema_type=schema_type, schema_str=schema_str, references=references, dependencies=dependencies, schema=schema
)
Expand All @@ -284,6 +282,16 @@ def __str__(self) -> str:
return str(self.schema)
return super().__str__()

@property
def schema(self) -> Union[Draft7Validator, AvroSchema, ProtobufSchema]:
if self._schema_cached is not None:
return self._schema_cached
self._schema_cached = super().schema
return self._schema_cached

def get_references(self) -> Optional[List[Reference]]:
return self.references


class ValidatedTypedSchema(ParsedTypedSchema):
"""Validated schema resource.
Expand Down

0 comments on commit b45c16c

Please sign in to comment.