-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys-query.go
103 lines (81 loc) · 2.47 KB
/
keys-query.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
package minidb
import (
"errors"
"os"
"path"
)
// FindKey gets the key in the keys map and returns its corresponding filename.
// It returns nil if it exists.
func (db *MiniDB) FindKey(key string) (string, error) {
filename, ok := db.content.Keys[key]
if !ok {
return "", errors.New("the key does not exist")
}
return filename, nil
}
// FindCollection gets the key in the keys map and returns its corresponding filename.
// It returns nil if it exists.
func (db *MiniDB) FindCollection(key string) (string, error) {
filename, ok := db.content.Collections[key]
if !ok {
return "", errors.New("the key does not exist")
}
return filename, nil
}
// FindStore gets the key in the keys map and returns its corresponding filename.
// It returns nil if it exists.
func (db *MiniDB) FindStore(key string) (string, error) {
filename, ok := db.content.Store[key]
if !ok {
return "", errors.New("the key does not exist")
}
return filename, nil
}
// RemoveCollection removes the collection key and the files corresponding to it.
// It returns nil if it is successful.
func (db *MiniDB) RemoveCollection(key string) error {
d := db.getOrCreateMutex("delete_cols" + key)
d.Lock()
defer d.Unlock()
// get the filename if it exists
filename, ok := db.content.Collections[key]
if !ok {
return errors.New("collections key does not exist")
}
// remove the key and the filename
delete(db.content.Collections, key)
db.writeToDB()
return os.RemoveAll(path.Join(db.path, filename))
}
// RemoveStore removes the store key and the files corresponding to it.
// It returns nil if it is successful.
func (db *MiniDB) RemoveStore(key string) error {
d := db.getOrCreateMutex("delete_store" + key)
d.Lock()
defer d.Unlock()
// get the filename if it exists
filename, ok := db.content.Store[key]
if !ok {
return errors.New("collections key does not exist")
}
// remove the key and the filename
delete(db.content.Store, key)
db.writeToDB()
return os.RemoveAll(path.Join(db.path, filename))
}
// RemoveKey removes the key and the files corresponding to it.
// It returns nil if it is successful.
func (db *MiniDB) RemoveKey(key string) error {
d := db.getOrCreateMutex("delete_key" + key)
d.Lock()
defer d.Unlock()
// get the filename if it exists
filename, ok := db.content.Keys[key]
if !ok {
return errors.New("collections key does not exist")
}
// remove the key and the filename
delete(db.content.Keys, key)
db.writeToDB()
return os.RemoveAll(path.Join(db.path, filename))
}