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