Skip to content

Commit bb2047d

Browse files
author
Christos Kotsis
committed
improvement: [storage] use buffer for reads
We now use buffer for reads to make sure that if an import/read operation fails, we wont be left with an empty data array Before we would dump the data array and then read the file into it. If the file was corrupt, the operation would fail and leave as with an empty data array Now we keep the current data array and dump only if the import/read operation has finished successfully
1 parent 764cd8d commit bb2047d

File tree

2 files changed

+57
-33
lines changed

2 files changed

+57
-33
lines changed

db/state.go

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ const (
1313

1414
// state struct used by dby storage
1515
type state struct {
16-
data, buffer []interface{}
17-
lib map[string]int
18-
ad int
16+
data []interface{}
17+
buffer []*interface{}
18+
lib map[string]int
19+
ad int
1920
}
2021

2122
// newStateFactory for creating a new v3 State
2223
func newStateFactory() *state {
2324
s := state{
2425
data: make([]interface{}, 0),
25-
buffer: make([]interface{}, 0),
26+
buffer: make([]*interface{}, 0),
2627
lib: make(map[string]int),
2728
}
2829
return &s
@@ -33,7 +34,7 @@ func (c *state) Clear() {
3334
c.data, c.buffer, c.lib = nil, nil, nil
3435

3536
c.data = make([]interface{}, 0)
36-
c.buffer = make([]interface{}, 0)
37+
c.buffer = make([]*interface{}, 0)
3738
c.lib = make(map[string]int)
3839
}
3940

@@ -58,7 +59,7 @@ func (c *state) PushData(d interface{}) {
5859

5960
// PushBuffer for appending data to the buffer array
6061
func (c *state) PushBuffer(d interface{}) {
61-
c.buffer = append(c.buffer, d)
62+
c.buffer = append(c.buffer, &d)
6263
}
6364

6465
// GetAllData returns the data array
@@ -67,7 +68,7 @@ func (c *state) GetAllData() []interface{} {
6768
}
6869

6970
// GetAllBuffer returns the buffer array
70-
func (c *state) GetAllBuffer() []interface{} {
71+
func (c *state) GetAllBuffer() []*interface{} {
7172
return c.buffer
7273
}
7374

@@ -100,11 +101,21 @@ func (c *state) SetDataFromIndex(v interface{}, i int) error {
100101
}
101102

102103
// GetBufferFromIndex returns the i'th element from the buffer array
103-
func (c *state) GetBufferFromIndex(i int) (interface{}, error) {
104-
if err := c.IndexInRange(i); err != nil {
105-
return nil, wrapErr(err)
104+
func (c *state) GetBufferFromIndex(i int) (*interface{}, error) {
105+
if len(c.buffer)-1 >= i {
106+
return c.buffer[i], nil
106107
}
107-
return c.data[i], nil
108+
return nil, fmt.Errorf(ire)
109+
}
110+
111+
// SetDataFromIndex sets to input value the i'th element from the data array
112+
func (c *state) SetBufferFromIndex(v interface{}, i int) error {
113+
if len(c.buffer)-1 >= i {
114+
c.buffer[i] = &v
115+
return nil
116+
}
117+
return fmt.Errorf(ire)
118+
108119
}
109120

110121
// IndexInRange check if index is within data array range
@@ -173,10 +184,10 @@ func (c *state) DeleteData(i int) error {
173184
return nil
174185
}
175186

176-
// CopyBufferToData for copying buffer array over data array
177-
func (c *state) CopyBufferToData() {
178-
copy(c.data, c.buffer)
179-
}
187+
// // CopyBufferToData for copying buffer array over data array
188+
// func (c *state) CopyBufferToData() {
189+
// copy(c.data, c.buffer)
190+
// }
180191

181192
// UnsetDataArray for deleting all data. This sets data = nil
182193
func (c *state) UnsetDataArray() {
@@ -197,7 +208,7 @@ func (c *state) UnsetBufferArray() {
197208
// DeleteBuffer deletes the data from the buffer array
198209
func (c *state) DeleteBuffer() {
199210
c.UnsetBufferArray()
200-
c.buffer = append(c.buffer, 0)
211+
c.buffer = make([]*interface{}, 0)
201212
}
202213

203214
// ClearLib removes all keys from the lib map

db/store_lib.go

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -213,43 +213,46 @@ func (s *Storage) ImportDocs(path string, o ...bool) error {
213213
return wrapErr(err)
214214
}
215215

216-
var dataArray []interface{}
217216
var counter int
218217
var data interface{}
218+
s.State.UnsetBufferArray()
219219

220-
if len(o) > 0 {
221-
issueWarning(deprecatedFeature, "ImportDocs(string, bool)", "Storage.DeleteAll(true).ImportDocs(path)")
222-
if o[0] {
223-
s.State.UnsetDataArray()
224-
}
225-
}
226-
227-
data = nil
228220
dec := yaml.NewDecoder(bytes.NewReader(impf))
229221
for {
230-
dataArray = append(dataArray, data)
231-
err := dec.Decode(&dataArray[counter])
222+
err = dec.Decode(&data)
232223
if err == nil {
233-
counter++
224+
s.State.PushBuffer(data)
234225
data = nil
226+
227+
counter++
235228
continue
236229
}
237230

238231
if err.Error() == "EOF" {
239232
break
240233
}
234+
s.State.UnsetBufferArray()
241235
return wrapErr(err)
242236
}
243237

244-
for _, j := range dataArray {
238+
if len(o) > 0 {
239+
issueWarning(deprecatedFeature, "ImportDocs(string, bool)", "Storage.DeleteAll(true).ImportDocs(path)")
240+
if o[0] {
241+
s.State.UnsetDataArray()
242+
s.State.ClearLib()
243+
}
244+
}
245+
246+
for _, j := range s.State.GetAllBuffer() {
245247
if j == nil {
246248
continue
247249
}
248-
if len(j.(map[interface{}]interface{})) == 0 {
250+
if len((*j).(map[interface{}]interface{})) == 0 {
249251
continue
250252
}
251-
s.State.PushData(j)
253+
s.State.PushData(*j)
252254
}
255+
s.State.UnsetBufferArray()
253256
return s.stateReload()
254257
}
255258

@@ -270,24 +273,34 @@ func (s *Storage) Read() error {
270273
s.Lock()
271274
defer s.Unlock()
272275

273-
s.State.DeleteAllData()
276+
s.State.UnsetBufferArray()
274277

275278
var data interface{}
276279
dec := yaml.NewDecoder(bytes.NewReader(f))
277280
for {
278281
err := dec.Decode(&data)
279282
if err == nil {
280-
s.State.PushData(data)
283+
s.State.PushBuffer(data)
281284
data = nil
282285
continue
283286
}
284287

285288
if err.Error() == "EOF" {
286289
break
287290
}
291+
s.State.UnsetBufferArray()
288292
return wrapErr(err)
289293
}
290294

295+
s.State.UnsetDataArray()
296+
297+
for _, j := range s.State.GetAllBuffer() {
298+
if j == nil {
299+
continue
300+
}
301+
s.State.PushData(*j)
302+
}
303+
s.State.UnsetBufferArray()
291304
return nil
292305
}
293306

0 commit comments

Comments
 (0)