Commit bda27583 authored by Frank Bessou's avatar Frank Bessou 🕸
Browse files

Transform RqlIOSchemaHolder into a singleton

Its two class methods get_schema and get_schema_hash will only compute
the schema once. They where computed every time the schema was
requested.

Closes #1.
parent 2ec46e035500
......@@ -28,6 +28,8 @@ class RqlIOSchemaHolder(object):
so it can decide to update the locally stored schema if
the integrity is not valid.
"""
_schema = None
_hash = None
@staticmethod
def format_schema(typeNames,
......@@ -70,10 +72,13 @@ class RqlIOSchemaHolder(object):
} for row in relationsFrom]
}
@cachedproperty
def get_schema(self):
@classmethod
def get_schema(cls, cnx):
if cls._schema is not None:
return cls._schema
entities = []
etypes = self._cw.execute('Any T WHERE T is CWEType')
etypes = cnx.execute('Any T WHERE T is CWEType')
for etype in etypes:
eid = etype[0]
rql_requests = [
......@@ -122,12 +127,16 @@ class RqlIOSchemaHolder(object):
ATTR ordernum ON"""
)
]
rsets = [self._cw.execute(request) for request in rql_requests]
rsets = [cnx.execute(request) for request in rql_requests]
entities.append(RqlIOSchemaHolder.format_schema(*rsets))
return {"entities": entities}
cls._schema = {"entities": entities}
return cls._schema
@cachedproperty
def get_schema_hash(self):
return hashlib.md5(
json_dumps(self.get_schema["entities"]).encode(self._cw.encoding)
@classmethod
def get_schema_hash(cls, cnx):
if cls._hash is not None:
return cls._hash
cls._hash = hashlib.md5(
json_dumps(cls.get_schema(cnx)["entities"]).encode(cnx.encoding)
).hexdigest()
return cls._hash
......@@ -75,15 +75,15 @@ class match_request_content_type(ExpectedValuePredicate):
return header
class RqlIOSchemaController(Controller, RqlIOSchemaHolder):
class RqlIOSchemaController(Controller):
__regid__ = 'rqlio_schema'
__select__ = match_http_method('GET', 'HEAD')
def publish(self, rset=None):
self._cw.set_content_type('application/json')
self._cw.add_header('Etag', self.get_schema_hash)
self._cw.add_header('Etag', RqlIOSchemaHolder.get_schema_hash(self._cw))
return json.dumps(
self.get_schema
RqlIOSchemaHolder.get_schema(self._cw)
).encode(self._cw.encoding)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment