Skip to content

Commit 2a0a95b

Browse files
committed
Some changes that it will work better (I think), and handle errors as well
1 parent 0911d23 commit 2a0a95b

File tree

1 file changed

+140
-94
lines changed

1 file changed

+140
-94
lines changed

simple_json_db.go

Lines changed: 140 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -10,123 +10,169 @@ import (
1010
)
1111

1212
type SimpleDB struct {
13-
data map[string]interface{}
14-
filePath string
15-
debugEnabled bool
16-
timeout *time.Timer
13+
data map[string]string
14+
filePath string
15+
debugEnabled bool
16+
timeout *time.Timer
1717
}
1818

1919
func NewSimpleDB(options map[string]interface{}) *SimpleDB {
20-
db := &SimpleDB{
21-
data: make(map[string]interface{}),
22-
filePath: "./db.json",
23-
debugEnabled: false,
24-
}
25-
26-
if file, ok := options["file"].(string); ok {
27-
db.filePath = file
28-
}
29-
if debug, ok := options["debug"].(bool); ok {
30-
db.debugEnabled = debug
31-
}
32-
33-
db.init()
34-
return db
20+
db := &SimpleDB{
21+
data: make(map[string]string),
22+
filePath: "./db.json",
23+
debugEnabled: false,
24+
}
25+
26+
if file, ok := options["file"].(string); ok {
27+
db.filePath = file
28+
}
29+
if debug, ok := options["debug"].(bool); ok {
30+
db.debugEnabled = debug
31+
}
32+
33+
db.init()
34+
return db
3535
}
3636

3737
func (db *SimpleDB) init() {
38-
if db.debugEnabled {
39-
fmt.Println("[SimpleDB] starting...")
40-
}
41-
if !strings.HasSuffix(db.filePath, ".json") {
42-
db.filePath += ".json"
43-
}
44-
if !filepath.IsAbs(db.filePath) {
45-
wd, _ := os.Getwd()
46-
db.filePath = filepath.Join(wd, db.filePath)
47-
}
48-
if err := os.MkdirAll(filepath.Dir(db.filePath), os.ModePerm); err != nil {
49-
if db.debugEnabled {
50-
fmt.Printf("[SimpleDB] Error creating directories: %v\n", err)
51-
}
52-
}
53-
if db.debugEnabled {
54-
fmt.Println("[SimpleDB] File path set")
55-
fmt.Printf("[SimpleDB] File path is: %s\n", db.filePath)
56-
}
57-
58-
fileContent, err := os.ReadFile(db.filePath)
59-
if err == nil {
60-
json.Unmarshal(fileContent, &db.data)
61-
}
62-
if db.debugEnabled {
63-
fmt.Println("[SimpleDB] Database ready")
64-
}
38+
if db.debugEnabled {
39+
fmt.Println("[SimpleDB] starting...")
40+
}
41+
if !strings.HasSuffix(db.filePath, ".json") {
42+
db.filePath += ".json"
43+
}
44+
if !filepath.IsAbs(db.filePath) {
45+
wd, _ := os.Getwd()
46+
db.filePath = filepath.Join(wd, db.filePath)
47+
}
48+
if err := os.MkdirAll(filepath.Dir(db.filePath), os.ModePerm); err != nil {
49+
if db.debugEnabled {
50+
fmt.Printf("[SimpleDB] Error creating directories: %v\n", err)
51+
}
52+
}
53+
if db.debugEnabled {
54+
fmt.Println("[SimpleDB] File path set")
55+
fmt.Printf("[SimpleDB] File path is: %s\n", db.filePath)
56+
}
57+
58+
if _, err := os.Stat(db.filePath); os.IsNotExist(err) {
59+
if db.debugEnabled {
60+
fmt.Printf("[SimpleDB] File does not exist, creating new file: %s\n", db.filePath)
61+
}
62+
file, err := os.Create(db.filePath)
63+
if err != nil && db.debugEnabled {
64+
fmt.Printf("[SimpleDB] Error creating file: %v\n", err)
65+
return
66+
}
67+
err = file.Close()
68+
if err != nil {
69+
fmt.Printf("[SimpleDB] Error closing file after creation: %v\n", err)
70+
return
71+
}
72+
}
73+
74+
fileContent, err := os.ReadFile(db.filePath)
75+
if err == nil {
76+
var tempData map[string]interface{}
77+
err := json.Unmarshal(fileContent, &tempData)
78+
if err != nil {
79+
if db.debugEnabled {
80+
fmt.Printf("[SimpleDB] Error unmarshalling file: %v\n", err)
81+
}
82+
return
83+
}
6584

85+
for k, v := range tempData {
86+
db.data[k] = fmt.Sprintf("%v", v)
87+
}
88+
if db.debugEnabled {
89+
fmt.Println("[SimpleDB] Database ready")
90+
}
91+
} else {
92+
if db.debugEnabled {
93+
fmt.Printf("[SimpleDB] Error reading file: %v\n", err)
94+
}
95+
}
6696
}
6797

6898
func (db *SimpleDB) writeDb() {
69-
if db.debugEnabled {
70-
fmt.Println("[SimpleDB] Writing database to file")
71-
}
72-
fileContent, _ := json.Marshal(db.data)
73-
os.WriteFile(db.filePath, fileContent, 0644)
74-
db.timeoutRemove()
99+
if db.debugEnabled {
100+
fmt.Println("[SimpleDB] Writing database to file")
101+
}
102+
fileContent, _ := json.Marshal(db.data)
103+
err := os.WriteFile(db.filePath, fileContent, 0644)
104+
if err != nil {
105+
if db.debugEnabled {
106+
fmt.Printf("[SimpleDB] Error writing file: %v\n", err)
107+
}
108+
return
109+
}
110+
db.timeoutRemove()
75111
}
76112

77-
func (db *SimpleDB) Set(key string, value interface{}) {
78-
if db.debugEnabled {
79-
fmt.Printf("[SimpleDB] Setting data for %s\n", key)
80-
}
81-
if db.timeout != nil {
82-
db.timeoutRemove()
83-
}
84-
db.timeoutSet()
85-
db.data[key] = value
113+
func (db *SimpleDB) Set(key, value string) {
114+
if db.debugEnabled {
115+
fmt.Printf("[SimpleDB] Setting data for %s\n", key)
116+
}
117+
if db.timeout != nil {
118+
db.timeoutRemove()
119+
}
120+
db.timeoutSet()
121+
db.data[key] = value
86122
}
87123

88-
func (db *SimpleDB) Get(key string) interface{} {
89-
if db.debugEnabled {
90-
fmt.Printf("[SimpleDB] Returning data for %s\n", key)
91-
}
92-
return db.data[key]
124+
func (db *SimpleDB) Get(key string) string {
125+
if db.debugEnabled {
126+
fmt.Printf("[SimpleDB] Returning data for %s\n", key)
127+
}
128+
return db.data[key]
93129
}
94130

95131
func (db *SimpleDB) Delete(key string) {
96-
if db.debugEnabled {
97-
fmt.Printf("[SimpleDB] Deleting data for %s\n", key)
98-
}
99-
if db.timeout != nil {
100-
db.timeoutRemove()
101-
}
102-
db.timeoutSet()
103-
delete(db.data, key)
132+
if db.debugEnabled {
133+
fmt.Printf("[SimpleDB] Deleting data for %s\n", key)
134+
}
135+
if db.Has(key) {
136+
if db.timeout != nil {
137+
db.timeoutRemove()
138+
}
139+
db.timeoutSet()
140+
delete(db.data, key)
141+
}
142+
}
143+
144+
func (db *SimpleDB) Has(key string) bool {
145+
if db.debugEnabled {
146+
fmt.Printf("[SimpleDB] Checking existence of %s\n", key)
147+
}
148+
_, exists := db.data[key]
149+
return exists
104150
}
105151

106152
func (db *SimpleDB) Keys() []string {
107-
if db.debugEnabled {
108-
fmt.Println("[SimpleDB] returning keys")
109-
}
110-
keys := make([]string, 0, len(db.data))
111-
for k := range db.data {
112-
keys = append(keys, k)
113-
}
114-
return keys
153+
if db.debugEnabled {
154+
fmt.Println("[SimpleDB] Returning keys")
155+
}
156+
keys := make([]string, 0, len(db.data))
157+
for k := range db.data {
158+
keys = append(keys, k)
159+
}
160+
return keys
115161
}
116162

117163
func (db *SimpleDB) timeoutSet() {
118-
if db.debugEnabled {
119-
fmt.Println("[SimpleDB] Setting timeout")
120-
}
121-
db.timeout = time.AfterFunc(500*time.Millisecond, db.writeDb)
164+
if db.debugEnabled {
165+
fmt.Println("[SimpleDB] Setting timeout")
166+
}
167+
db.timeout = time.AfterFunc(500*time.Millisecond, db.writeDb)
122168
}
123169

124170
func (db *SimpleDB) timeoutRemove() {
125-
if db.debugEnabled {
126-
fmt.Println("[SimpleDB] Removing timeout")
127-
}
128-
if db.timeout != nil {
129-
db.timeout.Stop()
130-
db.timeout = nil
131-
}
132-
}
171+
if db.debugEnabled {
172+
fmt.Println("[SimpleDB] Removing timeout")
173+
}
174+
if db.timeout != nil {
175+
db.timeout.Stop()
176+
db.timeout = nil
177+
}
178+
}

0 commit comments

Comments
 (0)