# Copyright (c) 2022, IRIS-HEP
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import json
import os
from pathlib import Path
from typing import List, Optional
from filelock import FileLock
from tinydb import TinyDB, Query, where
from servicex.configuration import Configuration
from servicex.models import TransformRequest, TransformStatus, TransformedResults
[docs]
class CacheException(Exception):
pass
[docs]
class QueryCache:
def __init__(self, config: Configuration):
self.config = config
Path(self.config.cache_path).mkdir(parents=True, exist_ok=True)
self.db = TinyDB(os.path.join(self.config.cache_path, "db.json"))
self.lock = FileLock(os.path.join(self.config.cache_path, "db.lock"))
[docs]
def close(self):
self.db.close()
[docs]
def update_record(self, record: TransformedResults):
transforms = Query()
with self.lock:
self.db.update(json.loads(record.model_dump_json()), transforms.hash == record.hash)
[docs]
def cached_queries(self) -> List[TransformedResults]:
transforms = Query()
with self.lock:
result = [TransformedResults(**doc) for doc in
self.db.search(transforms.request_id.exists())]
return result
[docs]
def delete_record_by_request_id(self, request_id: str):
with self.lock:
self.db.remove(where('request_id') == request_id)
[docs]
def get_codegen_by_backend(self, backend: str) -> Optional[dict]:
codegens = Query()
with self.lock:
records = self.db.search(codegens.backend == backend)
if not records:
return None
if len(records) != 1:
raise CacheException("Multiple records found in db for same backend")
else:
return records[0]
[docs]
def update_codegen_by_backend(self, backend: str, codegen_list: list):
transforms = Query()
with self.lock:
self.db.upsert({'backend': backend, 'codegens': codegen_list},
transforms.backend == backend)
[docs]
def delete_codegen_by_backend(self, backend: str):
with self.lock:
self.db.remove(where('backend') == backend)