environment.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include <unistd.h>
  2. #include "term.h"
  3. #include "environment.h"
  4. /**
  5. * Static handles.
  6. */
  7. #define DEFAULT_CTX_LABEL "urn:lsup:default"
  8. /** @brief Environment "singleton".
  9. */
  10. LSUP_Env *LSUP_default_env = NULL;
  11. LSUP_Env *
  12. LSUP_env_new (
  13. const char *default_ctx, const char *mdb_path,
  14. const LSUP_NSMap *nsmap)
  15. {
  16. LSUP_Env *env;
  17. CALLOC_GUARD (env, NULL);
  18. // Default store context.
  19. LSUP_Term *default_ctx_uri = LSUP_iriref_new (default_ctx, NULL);
  20. env->default_ctx = LSUP_term_serialize (default_ctx_uri);
  21. LSUP_term_free (default_ctx_uri);
  22. log_info ("Set up default context.");
  23. // Set up store if not existing.
  24. if (LSUP_mdbstore_setup (mdb_path, false) != LSUP_OK) return NULL;
  25. env->mdb_store = LSUP_mdbstore_new (mdb_path, env->default_ctx);
  26. if (UNLIKELY (!env->mdb_store)) return NULL;
  27. log_info ("Initialized persistent back end at %s.", mdb_path);
  28. // Get default namespace from store.
  29. RCNL (LSUP_mdbstore_nsm_get (env->mdb_store, &env->nsm));
  30. return env;
  31. }
  32. LSUP_rc
  33. LSUP_init (void)
  34. {
  35. LSUP_rc rc = LSUP_NOACTION;
  36. if (LSUP_env_is_init) return rc;
  37. #ifdef DEBUG
  38. // In debug mode, always use max logging.
  39. int loglevel = LOG_TRACE;
  40. #else
  41. char *_loglevel = getenv ("LSUP_LOGLEVEL");
  42. int loglevel = (_loglevel == NULL) ? LOG_INFO : atoi (_loglevel);
  43. #endif
  44. log_set_level (loglevel);
  45. // URI validation pattern.
  46. MALLOC_GUARD (LSUP_uri_ptn, LSUP_MEM_ERR);
  47. /* Uncomment in case a change in the URI regex results in an error.
  48. int regex_rc = regcomp (LSUP_uri_ptn, LSUP_URI_REGEX_STR, REG_EXTENDED);
  49. if (regex_rc != 0) {
  50. char err_msg[128];
  51. size_t err_msg_sz = regerror (regex_rc, LSUP_uri_ptn, err_msg, 128);
  52. log_error (
  53. "Error compiling regular expression pattern: %s.",
  54. err_msg);
  55. return LSUP_ERROR;
  56. }
  57. */
  58. if (regcomp (LSUP_uri_ptn, LSUP_URI_REGEX_STR, REG_EXTENDED) != 0) {
  59. log_error ("Error compiling regular expression pattern.");
  60. return LSUP_ERROR;
  61. }
  62. // Create and cache default literal datatype key.
  63. LSUP_default_datatype = LSUP_iriref_new (DEFAULT_DTYPE, NULL);
  64. uint32_t dtype_hash = LSUP_term_hash (LSUP_default_datatype );
  65. LSUP_tcache_add (dtype_hash, LSUP_default_datatype);
  66. // Default permanent store path.
  67. char *mdb_path = getenv ("LSUP_MDB_STORE_PATH");
  68. if (!mdb_path) {
  69. mdb_path = DEFAULT_ENV_PATH;
  70. log_warn (
  71. "`LSUP_MDB_STORE_PATH' environment variable is not "
  72. "set. The default location %s will be used as the graph "
  73. "store.", mdb_path
  74. );
  75. }
  76. // Default permanent store.
  77. LSUP_default_env = LSUP_env_new (
  78. DEFAULT_CTX_LABEL, mdb_path, NULL);
  79. if (UNLIKELY (!LSUP_default_env)) return LSUP_ERROR;
  80. // RAM disk store.
  81. if (LSUP_mdbstore_setup (LSUP_MDB_RAMDISK_PATH, true) != LSUP_OK) {
  82. log_error ("Error setting up RAM disk store files.");
  83. return LSUP_DB_ERR;
  84. }
  85. LSUP_default_env->mdb_store_ramdisk = LSUP_mdbstore_new (
  86. LSUP_MDB_RAMDISK_PATH, LSUP_default_env->default_ctx);
  87. if (UNLIKELY (!LSUP_default_env->mdb_store_ramdisk)) {
  88. log_error ("Error setting up RAM disk store.");
  89. return LSUP_DB_ERR;
  90. }
  91. log_info ("Initialized RAM disk back end at %s.", LSUP_MDB_RAMDISK_PATH);
  92. // Set automatic teardown TODO Is this a good idea?
  93. atexit (LSUP_done);
  94. LSUP_env_is_init = true;
  95. return LSUP_OK;
  96. }
  97. void
  98. LSUP_env_free (LSUP_Env *env)
  99. {
  100. LSUP_mdbstore_free (env->mdb_store);
  101. LSUP_mdbstore_free (env->mdb_store_ramdisk);
  102. LSUP_buffer_free (env->default_ctx);
  103. LSUP_nsmap_free (env->nsm);
  104. free (env);
  105. }
  106. void
  107. LSUP_done (void)
  108. {
  109. if (!LSUP_env_is_init) return;
  110. LSUP_env_free (LSUP_default_env);
  111. regfree (LSUP_uri_ptn);
  112. free (LSUP_uri_ptn);
  113. // Free ID cache, including default literal datatype.
  114. struct term_cache_t *entry, *tmp;
  115. HASH_ITER (hh, LSUP_term_cache, entry, tmp) {
  116. HASH_DEL (LSUP_term_cache, entry);
  117. LSUP_term_free (entry->term);
  118. free (entry);
  119. }
  120. LSUP_env_is_init = false;
  121. }