-
Notifications
You must be signed in to change notification settings - Fork 11
/
database.go
executable file
·270 lines (210 loc) · 5.43 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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//Package hare implements a simple DBMS that stores it's data
//in newline-delimited json files.
package hare
import (
"encoding/json"
"sync"
"github.com/jameycribbs/hare/dberr"
)
// Record interface defines the methods a struct representing
// a table record must implement.
type Record interface {
SetID(int)
GetID() int
AfterFind(*Database) error
}
type datastorage interface {
Close() error
CreateTable(string) error
DeleteRec(string, int) error
GetLastID(string) (int, error)
IDs(string) ([]int, error)
InsertRec(string, int, []byte) error
ReadRec(string, int) ([]byte, error)
RemoveTable(string) error
TableExists(string) bool
TableNames() []string
UpdateRec(string, int, []byte) error
}
// Database struct is the main struct for the Hare package.
type Database struct {
store datastorage
locks map[string]*sync.RWMutex
lastIDs map[string]int
}
// New takes a datastorage and returns a pointer to a
// Database struct.
func New(ds datastorage) (*Database, error) {
db := &Database{store: ds}
db.locks = make(map[string]*sync.RWMutex)
db.lastIDs = make(map[string]int)
for _, tableName := range db.store.TableNames() {
db.locks[tableName] = &sync.RWMutex{}
lastID, err := db.store.GetLastID(tableName)
if err != nil {
return nil, err
}
db.lastIDs[tableName] = lastID
}
return db, nil
}
// Close closes the associated datastore.
func (db *Database) Close() error {
for _, lock := range db.locks {
lock.Lock()
}
if err := db.store.Close(); err != nil {
return err
}
for _, lock := range db.locks {
lock.Unlock()
}
db.store = nil
db.locks = nil
db.lastIDs = nil
return nil
}
// CreateTable takes a table name and creates and
// initializes a new table.
func (db *Database) CreateTable(tableName string) error {
if db.TableExists(tableName) {
return dberr.ErrTableExists
}
if err := db.store.CreateTable(tableName); err != nil {
return nil
}
db.locks[tableName] = &sync.RWMutex{}
lastID, err := db.store.GetLastID(tableName)
if err != nil {
return err
}
db.lastIDs[tableName] = lastID
return nil
}
// Delete takes a table name and record id and removes that
// record from the database.
func (db *Database) Delete(tableName string, id int) error {
if !db.TableExists(tableName) {
return dberr.ErrNoTable
}
db.locks[tableName].Lock()
defer db.locks[tableName].Unlock()
if err := db.store.DeleteRec(tableName, id); err != nil {
return err
}
return nil
}
// DropTable takes a table name and deletes the table.
func (db *Database) DropTable(tableName string) error {
if !db.TableExists(tableName) {
return dberr.ErrNoTable
}
db.locks[tableName].Lock()
if err := db.store.RemoveTable(tableName); err != nil {
db.locks[tableName].Unlock()
return err
}
delete(db.lastIDs, tableName)
db.locks[tableName].Unlock()
delete(db.locks, tableName)
return nil
}
// Find takes a table name, a record id, and a pointer to a struct that
// implements the Record interface, finds the associated record from the
// table, and populates the struct.
func (db *Database) Find(tableName string, id int, rec Record) error {
if !db.TableExists(tableName) {
return dberr.ErrNoTable
}
db.locks[tableName].RLock()
defer db.locks[tableName].RUnlock()
rawRec, err := db.store.ReadRec(tableName, id)
if err != nil {
return err
}
err = json.Unmarshal(rawRec, rec)
if err != nil {
return err
}
err = rec.AfterFind(db)
if err != nil {
return err
}
return nil
}
// IDs takes a table name and returns a list of all record ids for
// that table.
func (db *Database) IDs(tableName string) ([]int, error) {
if !db.TableExists(tableName) {
return nil, dberr.ErrNoTable
}
db.locks[tableName].Lock()
defer db.locks[tableName].Unlock()
ids, err := db.store.IDs(tableName)
if err != nil {
return nil, err
}
return ids, err
}
// Insert takes a table name and a struct that implements the Record
// interface and adds a new record to the table. It returns the
// new record's id.
func (db *Database) Insert(tableName string, rec Record) (int, error) {
if !db.TableExists(tableName) {
return 0, dberr.ErrNoTable
}
db.locks[tableName].Lock()
defer db.locks[tableName].Unlock()
id := db.incrementLastID(tableName)
rec.SetID(id)
rawRec, err := json.Marshal(rec)
if err != nil {
return 0, err
}
if err := db.store.InsertRec(tableName, id, rawRec); err != nil {
return 0, err
}
return id, nil
}
// TableExists takes a table name and returns true if the table exists,
// false if it does not.
func (db *Database) TableExists(tableName string) bool {
return db.tableExists(tableName) && db.store.TableExists(tableName)
}
// Update takes a table name and a struct that implements the Record
// interface and updates the record in the table that has that record's
// id.
func (db *Database) Update(tableName string, rec Record) error {
if !db.TableExists(tableName) {
return dberr.ErrNoTable
}
db.locks[tableName].Lock()
defer db.locks[tableName].Unlock()
id := rec.GetID()
rawRec, err := json.Marshal(rec)
if err != nil {
return err
}
if err := db.store.UpdateRec(tableName, id, rawRec); err != nil {
return err
}
return nil
}
// unexported methods
func (db *Database) incrementLastID(tableName string) int {
lastID := db.lastIDs[tableName]
lastID++
db.lastIDs[tableName] = lastID
return lastID
}
func (db *Database) tableExists(tableName string) bool {
_, ok := db.locks[tableName]
if !ok {
return false
}
_, ok = db.lastIDs[tableName]
if !ok {
return false
}
return ok
}