metadata_store.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. from os import path
  2. from lakesuperior.store.base_lmdb_store import BaseLmdbStore
  3. from lakesuperior import env
  4. class MetadataStore(BaseLmdbStore):
  5. """
  6. LMDB store for RDF metadata.
  7. Note that even though this store connector uses LMDB as the
  8. :py::class:`LmdbStore` class, it is separate because it is not part of the
  9. RDFLib store implementation and carries higher-level concepts such as LDP
  10. resource URIs.
  11. """
  12. db_labels = ('checksums',)
  13. """
  14. At the moment only ``checksums`` is implemented. It is a registry of
  15. LDP resource graphs, indicated in the key by their UID, and their
  16. cryptographic hashes.
  17. """
  18. path = path.join(
  19. env.app_globals.config['application']['store']['ldp_rs']['location'],
  20. 'metadata')
  21. def get_checksum(self, uri):
  22. """
  23. Get the checksum of a resource.
  24. :param str uri: Resource URI (``info:fcres...``).
  25. :rtype: bytes
  26. """
  27. with self.cur(index='checksums') as cur:
  28. cksum = cur.get(uri.encode('utf-8'))
  29. return cksum
  30. def update_checksum(self, uri, cksum):
  31. """
  32. Update the stored checksum of a resource.
  33. :param str uri: Resource URI (``info:fcres...``).
  34. :param bytes cksum: Checksum bytestring.
  35. """
  36. with self.cur(index='checksums', write=True) as cur:
  37. cur.put(uri.encode('utf-8'), cksum)
  38. def delete_checksum(self, uri):
  39. """
  40. Delete the stored checksum of a resource.
  41. :param str uri: Resource URI (``info:fcres...``).
  42. """
  43. with self.cur(index='checksums', write=True) as cur:
  44. if cur.set_key(uri.encode('utf-8')):
  45. cur.delete()