storage-local.lua 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. -- ===================================================================
  2. -- /storage-local.lua
  3. -- Persistent storage in /data using Serpent Serializer
  4. -- Unix systems only
  5. -- ===================================================================
  6. -- Set directories and filenames
  7. modpol.datadir = modpol.topdir .. "/data"
  8. modpol.file_ledger = modpol.datadir .. "/ledger.dat"
  9. modpol.file_orgs = modpol.datadir .. "/orgs.dat"
  10. modpol.file_old_ledgers = modpol.datadir .. "/old_ledgers.dat"
  11. modpol.ocutil.setlogdir (modpol.datadir)
  12. modpol.ocutil.setlogname ("modpol.log")
  13. -- ===================================================================
  14. -- Set up the Serpent Serializer functions.
  15. modpol.serpent = {}
  16. dofile (modpol.topdir .. "/util/serpent/serpent.lua")
  17. -- ===================================================================
  18. -- This function stores "ledger" data to disk.
  19. local store_ledger = function(verbose)
  20. local ok = modpol.ocutil.file_write (modpol.file_ledger, modpol.serpent.dump (modpol.ledger))
  21. if ok ~= true then
  22. modpol.ocutil.fatal_error ("store_data: ledger")
  23. end
  24. local nn = modpol.ocutil.table_length (modpol.ledger)
  25. local str = "entries"
  26. if nn == 1 then str = "entry" end
  27. if verbose then modpol.ocutil.log (nn .. " global ledger entries stored to disk") end
  28. end
  29. -- ===================================================================
  30. -- This function stores "orgs" data to disk.
  31. local store_orgs = function()
  32. local ok = modpol.ocutil.file_write (modpol.file_orgs,
  33. modpol.serpent.dump (modpol.orgs))
  34. if ok ~= true then
  35. modpol.ocutil.fatal_error ("store_data: orgs")
  36. end
  37. local nn = modpol.ocutil.table_length (modpol.orgs.array)
  38. local str = "entries"
  39. if nn == 1 then str = "entry" end
  40. if verbose then modpol.ocutil.log (nn .. " orgs stored to disk") end
  41. end
  42. -- ===================================================================
  43. -- This function stores data to disk.
  44. modpol.store_data = function(verbose)
  45. store_ledger(verbose)
  46. store_orgs(verbose)
  47. end
  48. -- ===================================================================
  49. -- This function loads "ledger" data from disk.
  50. local load_ledger = function()
  51. local obj = modpol.ocutil.file_read (modpol.file_ledger )
  52. if obj ~= nil then
  53. local func, err = load (obj)
  54. if err then
  55. modpol.ocutil.fatal_error ("load_data: ledger" )
  56. end
  57. modpol.ledger = func()
  58. local nn = modpol.ocutil.table_length (modpol.ledger)
  59. local str = "entries"
  60. if nn == 1 then str = "entry" end
  61. modpol.ocutil.log (nn .. " global ledger entries loaded from disk")
  62. else
  63. modpol.ocutil.log ("No stored global ledger data found")
  64. end
  65. end
  66. -- ===================================================================
  67. -- This function loads "orgs" data from disk.
  68. local load_orgs = function()
  69. local obj = modpol.ocutil.file_read (modpol.file_orgs )
  70. if obj ~= nil then
  71. local func, err = load (obj)
  72. if err then
  73. modpol.ocutil.fatal_error ("load_data: orgs" )
  74. end
  75. modpol.orgs = func()
  76. -- this block resets the metatable after being loaded in so that the class functions work
  77. -- for id, org in ipairs(modpol.orgs.array) do
  78. -- setmetatable(org, modpol.orgs)
  79. -- end
  80. local nn = modpol.orgs.count
  81. local str = "entries"
  82. if nn == 1 then str = "entry" end
  83. modpol.ocutil.log (nn .. " orgs loaded from disk")
  84. else
  85. modpol.ocutil.log ("No stored orgs data found")
  86. end
  87. end
  88. -- ===================================================================
  89. -- This function loads "old_ledgers" data from disk.
  90. local load_old_ledgers = function()
  91. local obj = modpol.ocutil.file_read (modpol.file_old_ledgers )
  92. if obj ~= nil then
  93. local func, err = load (obj)
  94. if err then
  95. modpol.ocutil.fatal_error ("load_data: old_ledgers" )
  96. end
  97. modpol.old_ledgers = func()
  98. local nn = modpol.ocutil.table_length (modpol.old_ledgers)
  99. local str = "entries"
  100. if nn == 1 then str = "entry" end
  101. modpol.ocutil.log (nn .. " old ledgers loaded from disk")
  102. else
  103. modpol.ocutil.log ("No stored old ledgers data found")
  104. end
  105. end
  106. -- ===================================================================
  107. -- This function loads stored data from disk.
  108. modpol.load_storage = function()
  109. load_ledger()
  110. load_orgs()
  111. -- load_old_ledgers()
  112. end
  113. -- ===================================================================
  114. -- End of file.