Changeset View
Changeset View
Standalone View
Standalone View
swh/provenance/mongo/entity.py
- This file was added.
# Copyright (C) 2021 The Software Heritage developers | |||||
# See the AUTHORS file at the top-level directory of this distribution | |||||
# License: GNU General Public License version 3, or any later version | |||||
# See top-level LICENSE file for more information | |||||
from .errors import EntityError, DBError | |||||
class Entity: | |||||
collection = "" | |||||
validate = True | |||||
data_model = {} | |||||
def __init__(self, data=None): | |||||
self.data = data | |||||
def save(self): | |||||
# validate the model if self.validate is true | |||||
# FIXME, set the obj in the cache | |||||
pass | |||||
def get(self, qry): | |||||
# FIXME, get from cache | |||||
pass | |||||
def bulk_write(self, db, writes): | |||||
if not writes: | |||||
return None | |||||
try: | |||||
result = db.get_collection(self.collection).bulk_write(writes) | |||||
except DBError as e: | |||||
# Log as mongo error | |||||
raise e | |||||
else: | |||||
return result | |||||
@staticmethod | |||||
def factory(entity): | |||||
mapping = {"content": Content, "directory": Directory} | |||||
if entity in mapping: | |||||
return mapping[entity]() | |||||
raise EntityError(f"invalid entity type {entity}") | |||||
class Content(Entity): | |||||
collection = "content" | |||||
validate = False | |||||
@property | |||||
def revision(self): | |||||
return self.data.get("revision", {}) | |||||
@property | |||||
def directory(self): | |||||
return self.data.get("directory", {}) | |||||
class Directory(Entity): | |||||
collection = "directory" | |||||
validate = False | |||||
@property | |||||
def revision(self): | |||||
return self.data.get("revision", {}) |