storage-local.lua 4.6 KB

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