Browse Source

Better store init management.

scossu 21 hours ago
parent
commit
ac225a32d1
3 changed files with 51 additions and 18 deletions
  1. 3 3
      scratch.lua
  2. 45 14
      src/core.lua
  3. 3 1
      src/html_generator.lua

+ 3 - 3
scratch.lua

@@ -10,8 +10,8 @@ local val = require "pocket_archive.validator"
 local hgen = require "pocket_archive.html_generator"
 
 
----[[
-pkar.init(true)
+--[[
+_ = pkar.reset_store
 
 sip = sub.generate_sip(
     "test/sample_submission/postcard-bag/data/pkar_submission.csv")
@@ -19,4 +19,4 @@ sip = sub.generate_sip(
 sub.deposit(sip)
 --]]
 
---html = hgen.generate_site()
+html = hgen.generate_site()

+ 45 - 14
src/core.lua

@@ -6,6 +6,13 @@ local term = require "volksdata.term"
 local nsm = require "volksdata.namespace"
 
 
+-- Read only module properties.
+local PROTECTED = {
+    store = true,
+    reset_store = true,
+}
+
+
 local fpath = debug.getinfo(1, "S").source:sub(2)
 local root_path = path.dirname(path.dirname(fpath))
 local config_path = os.getenv("PKAR_CONFIG_DIR") or
@@ -14,9 +21,16 @@ local config_path = os.getenv("PKAR_CONFIG_DIR") or
 local config = dofile(path.join(config_path, "app.lua"))
 local store_id = "file://" .. (os.getenv("PKAR_BASE") or config.fs.dres_path)
 
+-- Fill namespace map from config.
 for pfx, ns in pairs(config.namespace) do nsm.add(pfx, ns) end
 
 
+-- Initialize the store. `clear` wipes all data. YOU HAVE BEEN WARNED!
+local store_init = function(clear)
+    return store.new(store.MDB, store_id, clear)
+end
+
+
 local M = {
     -- Project root path.
     root = root_path,
@@ -55,14 +69,18 @@ local M = {
     -- Common namespaces
     PAR_NS = nsm.get_ns("par"),
     PAS_NS = nsm.get_ns("pas"),
-}
 
+    -- Methods.
 
--- Initialize random ID generator.
-math.randomseed(M.config.id.seed[1], M.config.id.seed[2])
+    -- Escape strings for use in gsub patterns.
+    escape_ptn = function(src)
+        return src:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%0")
+    end,
+
+}
 
 --[[
-  Gnerate pairtree directory and file path from an ID string and prefix.
+  Generate pairtree directory and file path from an ID string and prefix.
 
   The directory is created if not existing.
 
@@ -94,18 +112,31 @@ M.gen_pairtree = function (pfx, id_str, ext, no_create)
 end
 
 
--- Initialize the store. `clear` wipes all data. YOU HAVE BEEN WARNED!
-M.init = function(clear)
-    M.store = store.new(store.MDB, store_id, clear)
-end
+-- No more assignments to the module afte here.
 
 
---[[
-    Escape strings for use in gsub patterns.
---]]
-M.escape_ptn = function(src)
-    return src:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%0")
-end
+local mt = {
+    __index = function(t, k)
+        if (k == "store") then
+            if not rawget(t, "_store") then
+                    rawset(t, "_store", store.new(store.MDB, store_id)) end
+            return rawget(t, "_store")
+
+        -- WIPE ALL DATA from the store and returns the new empty store handle.
+        elseif (k == "reset_store") then
+            rawset(t, "_store", store.new(store.MDB, store_id, true))
+            return rawget(t, "_store")
+        end
+    end,
+
+    __newindex = function () return nil, "Module is read only." end
+}
+
+setmetatable (M, mt)
+
+
+-- Initialize random ID generator.
+math.randomseed(M.config.id.seed[1], M.config.id.seed[2])
 
 
 return M

+ 3 - 1
src/html_generator.lua

@@ -21,7 +21,7 @@ local dbg = require "debugger"
 local NT = {}
 
 -- Default store graph to search all triples.
-local gr = graph.new(pkar.store, term.DEFAULT_CTX)
+local gr
 
 -- HTML templates. Compile them only once.
 -- TODO Add override for user-maintained templates.
@@ -360,6 +360,8 @@ M.generate_site = function()
     if plpath.isdir(M.media_dir) then dir.rmtree(M.media_dir) end
     dir.makepath(plpath.join(M.media_dir, "tn"))
 
+    gr = graph.new(pkar.store, term.DEFAULT_CTX)
+
     assert(M.generate_resources())
     assert(M.generate_idx())
     dir.clonetree("templates/assets", plpath.dirname(M.asset_dir), dir.copyfile)