-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple_json_db.go
186 lines (169 loc) · 3.94 KB
/
simple_json_db.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package simple_json_db
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
type SimpleDB struct {
data map[string]string
filePath string
debugEnabled bool
timeout *time.Timer
delay time.Duration
}
func NewSimpleDB(options map[string]interface{}) *SimpleDB {
db := &SimpleDB{
data: make(map[string]string),
filePath: "./db.json",
debugEnabled: false,
delay: 5 * time.Second,
}
if file, ok := options["file"].(string); ok {
db.filePath = file
}
if debug, ok := options["debug"].(bool); ok {
db.debugEnabled = debug
}
if delay, ok := options["delay"].(int); ok {
db.delay = time.Duration(delay) * time.Millisecond
}
db.init()
return db
}
func (db *SimpleDB) init() {
if db.debugEnabled {
fmt.Println("[SimpleDB] starting...")
}
if !strings.HasSuffix(db.filePath, ".json") {
db.filePath += ".json"
}
if !filepath.IsAbs(db.filePath) {
wd, _ := os.Getwd()
db.filePath = filepath.Join(wd, db.filePath)
}
if err := os.MkdirAll(filepath.Dir(db.filePath), os.ModePerm); err != nil {
if db.debugEnabled {
fmt.Printf("[SimpleDB] Error creating directories: %v\n", err)
}
}
if db.debugEnabled {
fmt.Println("[SimpleDB] File path set")
fmt.Printf("[SimpleDB] File path is: %s\n", db.filePath)
}
if _, err := os.Stat(db.filePath); os.IsNotExist(err) {
if db.debugEnabled {
fmt.Printf("[SimpleDB] File does not exist, creating new file: %s\n", db.filePath)
}
file, err := os.Create(db.filePath)
if err != nil && db.debugEnabled {
fmt.Printf("[SimpleDB] Error creating file: %v\n", err)
return
}
err = file.Close()
if err != nil {
fmt.Printf("[SimpleDB] Error closing file after creation: %v\n", err)
return
}
}
fileContent, err := os.ReadFile(db.filePath)
if err == nil {
var tempData map[string]interface{}
err := json.Unmarshal(fileContent, &tempData)
if err != nil {
if db.debugEnabled {
fmt.Printf("[SimpleDB] Error unmarshalling file: %v\n", err)
}
return
}
for k, v := range tempData {
db.data[k] = fmt.Sprintf("%v", v)
}
if db.debugEnabled {
fmt.Println("[SimpleDB] Database ready")
}
} else {
if db.debugEnabled {
fmt.Printf("[SimpleDB] Error reading file: %v\n", err)
}
}
}
func (db *SimpleDB) writeDb() {
if db.debugEnabled {
fmt.Println("[SimpleDB] Writing database to file")
}
fileContent, _ := json.Marshal(db.data)
err := os.WriteFile(db.filePath, fileContent, 0644)
if err != nil {
if db.debugEnabled {
fmt.Printf("[SimpleDB] Error writing file: %v\n", err)
}
return
}
db.timeoutRemove()
}
func (db *SimpleDB) Set(key string, value interface{}) {
if db.debugEnabled {
fmt.Printf("[SimpleDB] Setting data for %s\n", key)
}
if db.timeout != nil {
db.timeoutRemove()
}
db.timeoutSet()
db.data[key] = fmt.Sprintf("%v", value)
}
func (db *SimpleDB) Get(key string) *string {
if db.debugEnabled {
fmt.Printf("[SimpleDB] Returning data for %s\n", key)
}
if value, exists := db.data[key]; exists {
return &value
}
return nil
}
func (db *SimpleDB) Delete(key string) {
if db.debugEnabled {
fmt.Printf("[SimpleDB] Deleting data for %s\n", key)
}
if db.Has(key) {
if db.timeout != nil {
db.timeoutRemove()
}
db.timeoutSet()
delete(db.data, key)
}
}
func (db *SimpleDB) Has(key string) bool {
if db.debugEnabled {
fmt.Printf("[SimpleDB] Checking existence of %s\n", key)
}
_, exists := db.data[key]
return exists
}
func (db *SimpleDB) Keys() []string {
if db.debugEnabled {
fmt.Println("[SimpleDB] Returning keys")
}
keys := make([]string, 0, len(db.data))
for k := range db.data {
keys = append(keys, k)
}
return keys
}
func (db *SimpleDB) timeoutSet() {
if db.debugEnabled {
fmt.Println("[SimpleDB] Setting timeout")
}
db.timeout = time.AfterFunc(db.delay, db.writeDb)
}
func (db *SimpleDB) timeoutRemove() {
if db.debugEnabled {
fmt.Println("[SimpleDB] Removing timeout")
}
if db.timeout != nil {
db.timeout.Stop()
db.timeout = nil
}
}