-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.go
50 lines (45 loc) · 1.01 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
package tinydb
import (
"io"
"sync"
)
// Database the TinyDB database.
type Database struct {
sync.Mutex
table string
storage Storage
}
// TinyDB create a new database with the interface of storage.
func TinyDB(storage Storage) (*Database, error) {
var database = &Database{sync.Mutex{}, "_default", storage}
database.Lock()
defer database.Unlock()
var data = make(map[string][]interface{})
err := storage.Read(&data)
if err == io.EOF {
err := storage.Write(map[string][]interface{}{"_default": nil})
if err != nil {
return nil, err
}
} else if err != nil {
return nil, err
}
return database, nil
}
// Close the database.
func (db *Database) Close() error {
return db.storage.Close()
}
// Tables get the names of all tables in the database.
func (db *Database) Tables() ([]string, error) {
var data = make(map[string][]interface{})
err := db.storage.Read(&data)
if err != nil {
return nil, err
}
var out = make([]string, 0)
for k := range data {
out = append(out, k)
}
return out, nil
}