storage-local.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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()
  21. local ok = modpol.ocutil.file_write (modpol.file_ledger,
  22. modpol.serpent.dump (modpol.ledger))
  23. if ok ~= true then
  24. modpol.ocutil.fatal_error ("store_data: ledger")
  25. end
  26. local nn = modpol.ocutil.table_length (modpol.ledger)
  27. local str = "entries"
  28. if nn == 1 then str = "entry" end
  29. modpol.ocutil.log (nn .. " global ledger entries stored to disk")
  30. end
  31. -- ===================================================================
  32. -- This function stores "orgs" data to disk.
  33. local store_orgs = function()
  34. local ok = modpol.ocutil.file_write (modpol.file_orgs,
  35. modpol.serpent.dump (modpol.orgs))
  36. if ok ~= true then
  37. modpol.ocutil.fatal_error ("store_data: orgs")
  38. end
  39. local nn = modpol.ocutil.table_length (modpol.orgs)
  40. local str = "entries"
  41. if nn == 1 then str = "entry" end
  42. modpol.ocutil.log (nn .. " orgs stored to disk")
  43. end
  44. -- ===================================================================
  45. -- This function stores "old_ledgers" data to disk.
  46. local store_old_ledgers = function()
  47. local ok = modpol.ocutil.file_write (modpol.file_old_ledgers,
  48. modpol.serpent.dump (modpol.old_ledgers))
  49. if ok ~= true then
  50. modpol.ocutil.fatal_error ("store_data: orgs")
  51. end
  52. local nn = modpol.ocutil.table_length (modpol.old_ledgers)
  53. local str = "entries"
  54. if nn == 1 then str = "entry" end
  55. modpol.ocutil.log (nn .. " orgs stored to disk")
  56. end
  57. -- ===================================================================
  58. -- This function stores data to disk.
  59. modpol.store_data = function()
  60. store_ledger()
  61. store_orgs()
  62. store_old_ledgers()
  63. end
  64. -- ===================================================================
  65. -- This function loads "ledger" data from disk.
  66. local load_ledger = function()
  67. local obj = modpol.ocutil.file_read (modpol.file_ledger )
  68. if obj ~= nil then
  69. local func, err = load (obj)
  70. if err then
  71. modpol.ocutil.fatal_error ("load_data: ledger" )
  72. end
  73. modpol.ledger = func()
  74. local nn = modpol.ocutil.table_length (modpol.ledger)
  75. local str = "entries"
  76. if nn == 1 then str = "entry" end
  77. modpol.ocutil.log (nn .. " global ledger entries loaded from disk")
  78. else
  79. modpol.ocutil.log ("No stored global ledger data found")
  80. end
  81. end
  82. -- ===================================================================
  83. -- This function loads "orgs" data from disk.
  84. local load_orgs = function()
  85. local obj = modpol.ocutil.file_read (modpol.file_orgs )
  86. if obj ~= nil then
  87. local func, err = load (obj)
  88. if err then
  89. modpol.ocutil.fatal_error ("load_data: orgs" )
  90. end
  91. modpol.orgs = func()
  92. local nn = modpol.ocutil.table_length (modpol.orgs)
  93. local str = "entries"
  94. if nn == 1 then str = "entry" end
  95. modpol.ocutil.log (nn .. " orgs loaded from disk")
  96. else
  97. modpol.ocutil.log ("No stored orgs data found")
  98. end
  99. end
  100. -- ===================================================================
  101. -- This function loads "old_ledgers" data from disk.
  102. local load_old_ledgers = function()
  103. local obj = modpol.ocutil.file_read (modpol.file_old_ledgers )
  104. if obj ~= nil then
  105. local func, err = load (obj)
  106. if err then
  107. modpol.ocutil.fatal_error ("load_data: old_ledgers" )
  108. end
  109. modpol.old_ledgers = func()
  110. local nn = modpol.ocutil.table_length (modpol.old_ledgers)
  111. local str = "entries"
  112. if nn == 1 then str = "entry" end
  113. modpol.ocutil.log (nn .. " old ledgers loaded from disk")
  114. else
  115. modpol.ocutil.log ("No stored old ledgers data found")
  116. end
  117. end
  118. -- ===================================================================
  119. -- This function loads stored data from disk.
  120. modpol.load_storage = function()
  121. load_ledger()
  122. load_orgs()
  123. load_old_ledgers()
  124. end
  125. -- ===================================================================
  126. -- End of file.