persistentmap.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Public Domain Software
  3. *
  4. * I (Matthias Ladkau) am the author of the source code in this file.
  5. * I have placed the source code in this file in the public domain.
  6. *
  7. * For further information see: http://creativecommons.org/publicdomain/zero/1.0/
  8. */
  9. package datautil
  10. import (
  11. "encoding/gob"
  12. "os"
  13. )
  14. /*
  15. PersistentMap is a persistent map storing string values. This implementation returns
  16. more encoding / decoding errors since not all possible values are supported.
  17. */
  18. type PersistentMap struct {
  19. filename string // File of the persistent map
  20. Data map[string]interface{} // Data of the persistent map
  21. }
  22. /*
  23. NewPersistentMap creates a new persistent map.
  24. */
  25. func NewPersistentMap(filename string) (*PersistentMap, error) {
  26. pm := &PersistentMap{filename, make(map[string]interface{})}
  27. return pm, pm.Flush()
  28. }
  29. /*
  30. LoadPersistentMap loads a persistent map from a file.
  31. */
  32. func LoadPersistentMap(filename string) (*PersistentMap, error) {
  33. file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0660)
  34. if err != nil {
  35. return nil, err
  36. }
  37. defer file.Close()
  38. pm := &PersistentMap{filename, make(map[string]interface{})}
  39. de := gob.NewDecoder(file)
  40. return pm, de.Decode(&pm.Data)
  41. }
  42. /*
  43. Flush writes contents of the persistent map to the disk.
  44. */
  45. func (pm *PersistentMap) Flush() error {
  46. file, err := os.OpenFile(pm.filename, os.O_CREATE|os.O_RDWR, 0660)
  47. if err != nil {
  48. return err
  49. }
  50. defer file.Close()
  51. en := gob.NewEncoder(file)
  52. return en.Encode(pm.Data)
  53. }
  54. /*
  55. PersistentStringMap is a persistent map storing string values.
  56. */
  57. type PersistentStringMap struct {
  58. filename string // File of the persistent map
  59. Data map[string]string // Data of the persistent map
  60. }
  61. /*
  62. NewPersistentStringMap creates a new persistent map.
  63. */
  64. func NewPersistentStringMap(filename string) (*PersistentStringMap, error) {
  65. pm := &PersistentStringMap{filename, make(map[string]string)}
  66. return pm, pm.Flush()
  67. }
  68. /*
  69. LoadPersistentStringMap loads a persistent map from a file.
  70. */
  71. func LoadPersistentStringMap(filename string) (*PersistentStringMap, error) {
  72. file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0660)
  73. if err != nil {
  74. return nil, err
  75. }
  76. pm := &PersistentStringMap{filename, make(map[string]string)}
  77. de := gob.NewDecoder(file)
  78. de.Decode(&pm.Data)
  79. return pm, file.Close()
  80. }
  81. /*
  82. Flush writes contents of the persistent map to the disk.
  83. */
  84. func (pm *PersistentStringMap) Flush() error {
  85. file, err := os.OpenFile(pm.filename, os.O_CREATE|os.O_RDWR, 0660)
  86. if err != nil {
  87. return err
  88. }
  89. en := gob.NewEncoder(file)
  90. en.Encode(pm.Data)
  91. return file.Close()
  92. }