-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.go
224 lines (209 loc) · 5.09 KB
/
drive.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
package jsondb
import (
"context"
"encoding/json"
"errors"
"os"
"path"
"sync"
)
var (
ErrColumnNotExists = errors.New("column not exists")
ErrPrimaryKeyNotExists = errors.New("primary key not exists")
ErrRecordNotFound = errors.New("record not found")
)
type (
Drive struct {
ctx context.Context
mutex sync.Mutex
baseDir string
}
)
func (drive *Drive) Open(ctx context.Context, directory string) (err error) {
if ctx == nil {
ctx = context.Background()
}
drive.ctx = ctx
drive.baseDir = directory
if _, err = os.Stat(drive.baseDir); err != nil {
err = os.MkdirAll(drive.baseDir, 0755)
}
return
}
func (drive *Drive) loadFromFile(ctx context.Context, model Model) (documents []Document, err error) {
var (
fp *os.File
)
if fp, err = os.Open(path.Join(drive.baseDir, model.TableName()+".json")); err != nil {
return
}
defer func() {
_ = fp.Close()
}()
documents = make([]Document, 0, 20)
err = json.NewDecoder(fp).Decode(&documents)
return
}
func (drive *Drive) flushToFile(ctx context.Context, tableName string, documents []Document) (err error) {
var (
fp *os.File
)
if len(documents) <= 0 {
return
}
if fp, err = os.OpenFile(path.Join(drive.baseDir, tableName+".json"), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644); err != nil {
return
}
err = json.NewEncoder(fp).Encode(documents)
return
}
//Query create record query object
func (drive *Drive) Query(ctx context.Context, model Model) *Query {
var (
err error
schema *Schema
documents []Document
)
if documents, err = drive.loadFromFile(ctx, model); err != nil {
documents = make([]Document, 0)
}
if schema, err = parseModelSchema(model); err != nil {
panic(err)
}
return newQuery(model, schema, documents)
}
//Insert add record to database
func (drive *Drive) Insert(ctx context.Context, model Model) (err error) {
var (
documents []Document
)
drive.mutex.Lock()
defer drive.mutex.Unlock()
if documents, err = drive.loadFromFile(ctx, model); err != nil {
documents = make([]Document, 0)
}
documents = append(documents, toDocument(model))
err = drive.flushToFile(ctx, model.TableName(), documents)
return
}
//Find get record by primary key
func (drive *Drive) Find(ctx context.Context, model Model) (err error) {
var (
n int
schema *Schema
documents []Document
pk interface{}
comparePK interface{}
)
drive.mutex.Lock()
defer drive.mutex.Unlock()
if schema, err = parseModelSchema(model); err != nil {
return
}
if pk, err = schema.GetPrimaryValue(model); err != nil {
return
}
if documents, err = drive.loadFromFile(ctx, model); err != nil {
return ErrRecordNotFound
}
for _, document := range documents {
if comparePK = document.Getter(schema.PrimaryColumn().Name, schema.PrimaryColumn().JsonName); err == nil {
if n, err = compare(pk, comparePK); err == nil && n == 0 {
err = document.Decode(model)
return
}
}
}
err = ErrRecordNotFound
return
}
//Update update record by primary key
func (drive *Drive) Update(ctx context.Context, model Model) (err error) {
var (
n int
ok bool
schema *Schema
documents []Document
pk interface{}
comparePK interface{}
)
drive.mutex.Lock()
defer drive.mutex.Unlock()
if schema, err = parseModelSchema(model); err != nil {
return
}
if pk, err = schema.GetPrimaryValue(model); err != nil {
return
}
if documents, err = drive.loadFromFile(ctx, model); err != nil {
return ErrRecordNotFound
}
for i, v := range documents {
if comparePK = v.Getter(schema.PrimaryColumn().Name, schema.PrimaryColumn().JsonName); err == nil {
if n, err = compare(pk, comparePK); err == nil && n == 0 {
documents[i] = toDocument(model)
ok = true
break
}
}
}
if ok {
err = drive.flushToFile(ctx, model.TableName(), documents)
} else {
err = ErrRecordNotFound
}
return
}
//ReplaceInto add or update record by primary key
func (drive *Drive) ReplaceInto(ctx context.Context, model Model) (err error) {
if err = drive.Update(ctx, model); err != nil {
if errors.Is(err, ErrRecordNotFound) {
err = drive.Insert(ctx, model)
}
}
return
}
//Delete remove record by primary key
func (drive *Drive) Delete(ctx context.Context, model Model) (err error) {
var (
n int
ok bool
schema *Schema
documents []Document
pk interface{}
comparePK interface{}
)
drive.mutex.Lock()
defer drive.mutex.Unlock()
if schema, err = parseModelSchema(model); err != nil {
return
}
if pk, err = schema.GetPrimaryValue(model); err != nil {
return
}
if documents, err = drive.loadFromFile(ctx, model); err != nil {
return ErrRecordNotFound
}
for i, v := range documents {
if comparePK = v.Getter(schema.PrimaryColumn().Name, schema.PrimaryColumn().JsonName); comparePK != nil {
if n, err = compare(pk, comparePK); err == nil && n == 0 {
documents = append(documents[:i], documents[i+1:]...)
ok = true
break
}
}
}
if ok {
err = drive.flushToFile(ctx, model.TableName(), documents)
} else {
err = ErrRecordNotFound
}
return
}
func (drive *Drive) Close() (err error) {
return
}
func New() *Drive {
d := &Drive{}
return d
}