-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
database.go
154 lines (116 loc) · 2.74 KB
/
database.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
package database
import (
"fmt"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/fulldump/inceptiondb/collection"
)
const (
StatusOpening = "opening"
StatusOperating = "operating"
StatusClosing = "closing"
)
type Config struct {
Dir string
}
type Database struct {
Config *Config
status string
Collections map[string]*collection.Collection
exit chan struct{}
}
func NewDatabase(config *Config) *Database { // todo: return error?
s := &Database{
Config: config,
status: StatusOpening,
Collections: map[string]*collection.Collection{},
exit: make(chan struct{}),
}
return s
}
func (db *Database) GetStatus() string {
return db.status
}
func (db *Database) CreateCollection(name string) (*collection.Collection, error) {
_, exists := db.Collections[name]
if exists {
return nil, fmt.Errorf("collection '%s' already exists", name)
}
filename := path.Join(db.Config.Dir, name)
col, err := collection.OpenCollection(filename)
if err != nil {
return nil, err
}
db.Collections[name] = col
return col, nil
}
func (db *Database) DropCollection(name string) error { // TODO: rename drop?
col, exists := db.Collections[name]
if !exists {
return fmt.Errorf("collection '%s' not found", name)
}
filename := path.Join(db.Config.Dir, name)
err := os.Remove(filename)
if err != nil {
return err // TODO: wrap?
}
delete(db.Collections, name) // TODO: protect section! not threadsafe
return col.Close()
}
func (db *Database) Load() error {
fmt.Printf("Loading database %s...\n", db.Config.Dir) // todo: move to logger
dir := db.Config.Dir
err := os.MkdirAll(dir, 0755)
if err != nil {
return err
}
err = filepath.WalkDir(dir, func(filename string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
name := filename
name = strings.TrimPrefix(name, dir)
name = strings.TrimPrefix(name, "/")
t0 := time.Now()
col, err := collection.OpenCollection(filename)
if err != nil {
fmt.Printf("ERROR: open collection '%s': %s\n", filename, err.Error()) // todo: move to logger
return err
}
fmt.Println(name, len(col.Rows), time.Since(t0)) // todo: move to logger
db.Collections[name] = col
return nil
})
if err != nil {
db.status = StatusClosing
return err
}
db.status = StatusOperating
return nil
}
func (db *Database) Start() error {
go db.Load()
<-db.exit
return nil
}
func (db *Database) Stop() error {
defer close(db.exit)
db.status = StatusClosing
var lastErr error
for name, col := range db.Collections {
fmt.Printf("Closing '%s'...\n", name)
err := col.Close()
if err != nil {
fmt.Printf("ERROR: close(%s): %s", name, err.Error())
lastErr = err
}
}
return lastErr
}