core.c 4.0 KB

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