123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- #include "repo_core.h"
- bool LSR_is_init = false;
- LSUP_Term *LSR_rdf_t = NULL;
- /*
- * Initial namespace map.
- */
- static char *nsm_str[][2] = {
- {LSR_RSRC_PFX, LSR_RSRC_NS},
- {"ebucore", "http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#"},
- {"ore", "http://www.openarchives.org/ore/terms/"},
- {"premis", "http://www.loc.gov/premis/rdf/v1#"},
- {NULL, NULL}
- };
- /*
- * Repo-managed predicates.
- */
- static char *mgd_pred_str[] = {
- "ebucore:hasMimeType",
- "lsup:created",
- "lsup:createdBy",
- "lsup:lastModified",
- "lsup:lastModifiedBy",
- "ore:proxyIn",
- "ore:proxyFor",
- "premis:hasSize",
- "premis:hasMessageDigest",
- NULL
- };
- /*
- * Repo-managed RDF types.
- */
- static char *mgd_type_str[] = {
- "lsup:AdminMetadata",
- "lsup:DataResource",
- "lsup:DescriptiveResource",
- "lsup:List",
- "lsup:ListItem",
- "lsup:Metadata",
- "lsup:Resource",
- "lsup:Set",
- "lsup:UserMetadata",
- "ore:Aggregation",
- "ore:Proxy",
- NULL
- };
- LSR_TermMap *LSR_managed_preds, *LSR_managed_types;
- LSUP_Store *LSR_store = NULL;
- /*
- * Callbacks.
- */
- /// Hash function for term map.
- static uint64_t tmap_hash_fn (const void *item, uint64_t seed0, uint64_t seed1)
- { return LSUP_term_hash ((LSUP_Term *) item); }
- /// Compare function for term map.
- static int tmap_cmp_fn (const void *a, const void *b, void *udata)
- {
- const LSUP_Term *ta = a, *tb = b;
- return strcmp(ta->data, tb->data);
- }
- /// Free element within the term map.
- static void tmap_free_fn (void *item)
- {
- // Partly reimplement LSUP_term_free() without freeing the term handle.
- LSUP_Term *term = item;
- if (LSUP_IS_IRI (term)) free (term->iri_info);
- free (term->data);
- }
- LSUP_rc LSR_init (void)
- {
- if (LSR_is_init) return LSUP_NOACTION;
- LSUP_rc rc = LSUP_init();
- if (rc < 0) return rc;
- LSR_rdf_t = LSUP_iriref_new ("rdf:type", LSUP_default_nsm);
- // Set up default back end.
- // If LSR_BACKEND_URN is not defined in env, LSUP_MDB_STORE_URN is used;
- // if that is also not set, lsup_rdf will use its default—which may be in
- // a NON-PERSISTENT volume!
- const char *store_urn = getenv ("LSR_BACKEND_URN");
- const LSUP_StoreInt *sif = LSUP_store_int (LSR_DEFAULT_BACKEND);
- if (sif->setup_fn) {
- sif->setup_fn (store_urn, false);
- }
- LSR_store = LSUP_store_new (LSR_DEFAULT_BACKEND, store_urn, 0);
- // Load additional LSUP_repo namespaces into store and memory.
- LSUP_NSMap *nsm = LSUP_default_nsm;
- for (int i = 0; nsm_str[i][0] != NULL; i++)
- LSUP_nsmap_add (nsm, nsm_str[i][0], nsm_str[i][1]);
- // Store the additional NS if this is the first time.
- //LSUP_nsmap_add (LSUP_default_mdb_store, nsm);
- // Cache managed predicates.
- LSR_managed_preds = hashmap_new (
- sizeof (LSUP_Term), 0, LSUP_HASH_SEED, 0,
- tmap_hash_fn, tmap_cmp_fn, tmap_free_fn, NULL);
- for (int i = 0; mgd_pred_str[i] != NULL; i++) {
- log_trace ("Storing managed predicate: %s", mgd_pred_str[i]);
- LSUP_Term *uri = LSUP_iriref_new (mgd_pred_str[i], LSUP_default_nsm);
- hashmap_set (LSR_managed_preds, uri);
- free (uri);
- }
- // Cache managed types.
- LSR_managed_types = hashmap_new (
- sizeof (LSUP_Term), 0, LSUP_HASH_SEED, 0,
- tmap_hash_fn, tmap_cmp_fn, tmap_free_fn, NULL);
- for (int i = 0; mgd_type_str[i] != NULL; i++) {
- log_trace ("Storing managed type: %s", mgd_type_str[i]);
- LSUP_Term *uri = LSUP_iriref_new (
- mgd_type_str[i], LSUP_default_nsm);
- hashmap_set (LSR_managed_types, uri);
- free (uri);
- }
- LSR_is_init = true;
- atexit (LSR_done);
- return LSUP_OK;
- }
- void LSR_done (void)
- {
- if (!LSR_is_init) return;
- log_info ("Tearing down LSUP repo environment.");
- LSUP_store_free (LSR_store);
- LSR_store = NULL;
- hashmap_free (LSR_managed_preds);
- hashmap_free (LSR_managed_types);
- LSUP_term_free (LSR_rdf_t);
- LSR_rdf_t = NULL;
- LSUP_done();
- LSR_is_init = false;
- }
- LSUP_Term *LSR_id_to_urn (const uuid_t id, const char *frag);
|