core.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #include "core.h"
  2. bool LSR_is_init = false;
  3. /*
  4. * Initial namespace map.
  5. */
  6. static char *nsm_str[][2] = {
  7. {LSR_RSRC_PFX, LSR_RSRC_NS},
  8. {"ebucore", "http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#"},
  9. {"ore", "http://www.openarchives.org/ore/terms/"},
  10. {"premis", "http://www.loc.gov/premis/rdf/v1#"},
  11. {NULL, NULL}
  12. };
  13. /*
  14. * Repo-managed predicates.
  15. */
  16. static char *mgd_pred_str[] = {
  17. "ebucore:hasMimeType",
  18. "lsup:created",
  19. "lsup:createdBy",
  20. "lsup:lastModified",
  21. "lsup:lastModifiedBy",
  22. "ore:proxyIn",
  23. "ore:proxyFor",
  24. "premis:hasSize",
  25. "premis:hasMessageDigest",
  26. NULL
  27. };
  28. /*
  29. * Repo-managed RDF types.
  30. */
  31. static char *mgd_type_str[] = {
  32. "lsup:AdminMetadata",
  33. "lsup:DataResource",
  34. "lsup:DescriptiveResource",
  35. "lsup:List",
  36. "lsup:ListItem",
  37. "lsup:Metadata",
  38. "lsup:Resource",
  39. "lsup:Set",
  40. "lsup:UserMetadata",
  41. "ore:Aggregation",
  42. "ore:Proxy",
  43. NULL
  44. };
  45. LSR_TermMap *LSR_managed_preds, *LSR_managed_types;
  46. LSUP_Store *LSR_store = NULL;
  47. /*
  48. * Callbacks.
  49. */
  50. /// Hash function for term map.
  51. static uint64_t tmap_hash_fn (const void *item, uint64_t seed0, uint64_t seed1)
  52. { return LSUP_term_hash ((LSUP_Term *) item); }
  53. /// Compare function for term map.
  54. static int tmap_cmp_fn (const void *a, const void *b, void *udata)
  55. {
  56. const LSUP_Term *ta = a, *tb = b;
  57. return strcmp(ta->data, tb->data);
  58. }
  59. /// Free element within the term map.
  60. static void tmap_free_fn (void *item)
  61. {
  62. // Partly reimplement LSUP_term_free() without freeing the term handle.
  63. LSUP_Term *term = item;
  64. if (LSUP_IS_IRI (term)) free (term->iri_info);
  65. free (term->data);
  66. }
  67. LSUP_rc LSR_init (void)
  68. {
  69. if (LSR_is_init) return LSUP_NOACTION;
  70. LSUP_rc rc = LSUP_init();
  71. if (rc < 0) return rc;
  72. // Set up default back end.
  73. // If LSR_BACKEND_URN is not defined in env, LSUP_MDB_STORE_URN is used;
  74. // if that is also not set, lsup_rdf will use its default—which may be in
  75. // a NON-PERSISTENT volume!
  76. const char *store_urn = getenv ("LSR_BACKEND_URN");
  77. const LSUP_StoreInt *sif = LSUP_store_int (LSR_DEFAULT_BACKEND);
  78. if (sif->setup_fn) {
  79. sif->setup_fn (store_urn, false);
  80. }
  81. LSR_store = LSUP_store_new (LSR_DEFAULT_BACKEND, store_urn, 0);
  82. // Load additional LSUP_repo namespaces into store and memory.
  83. LSUP_NSMap *nsm = LSUP_default_nsm;
  84. for (int i = 0; nsm_str[i][0] != NULL; i++)
  85. LSUP_nsmap_add (nsm, nsm_str[i][0], nsm_str[i][1]);
  86. // Store the additional NS if this is the first time.
  87. //LSUP_nsmap_add (LSUP_default_mdb_store, nsm);
  88. // Cache managed predicates.
  89. LSR_managed_preds = hashmap_new (
  90. sizeof (LSUP_Term), 0, LSUP_HASH_SEED, 0,
  91. tmap_hash_fn, tmap_cmp_fn, tmap_free_fn, NULL);
  92. for (int i = 0; mgd_pred_str[i] != NULL; i++) {
  93. log_trace ("Storing managed predicate: %s", mgd_pred_str[i]);
  94. LSUP_Term *uri = LSUP_iriref_new (mgd_pred_str[i], LSUP_default_nsm);
  95. hashmap_set (LSR_managed_preds, uri);
  96. free (uri);
  97. }
  98. // Cache managed types.
  99. LSR_managed_types = hashmap_new (
  100. sizeof (LSUP_Term), 0, LSUP_HASH_SEED, 0,
  101. tmap_hash_fn, tmap_cmp_fn, tmap_free_fn, NULL);
  102. for (int i = 0; mgd_type_str[i] != NULL; i++) {
  103. log_trace ("Storing managed type: %s", mgd_type_str[i]);
  104. LSUP_Term *uri = LSUP_iriref_new (
  105. mgd_type_str[i], LSUP_default_nsm);
  106. hashmap_set (LSR_managed_types, uri);
  107. free (uri);
  108. }
  109. LSR_is_init = true;
  110. atexit (LSR_done);
  111. return LSUP_OK;
  112. }
  113. void LSR_done (void)
  114. {
  115. if (!LSR_is_init) return;
  116. log_info ("Tearing down LSUP repo environment.");
  117. LSUP_store_free (LSR_store);
  118. LSR_store = NULL;
  119. hashmap_free (LSR_managed_preds);
  120. hashmap_free (LSR_managed_types);
  121. LSUP_done();
  122. LSR_is_init = false;
  123. }
  124. LSUP_Term *LSR_id_to_urn (const uuid_t id, const char *frag);